JITHarness#
Let a model keep its own tools and notes in a repository on disk. See the guide.
- class zett_agent.jit_harness.runtime.JITHarness(directory, *, tools=(), manage_base_tools=True, max_call_depth=32, error_capacity=100, error_message_chars=4000, call_capacity=2000, host=None)[源代码]#
An independently evolving toolbox of Python tools and Markdown notes.
The toolbox is one repository: a Python package in a directory, written with ordinary file tools.
load_repo()imports it and registers every@toolit defines, and lists the Markdown notes underresources/. The notes are files the model reads itself, not tools.- 参数:
directory (str | Path) -- Where this harness keeps its repository. The repository is a subdirectory of it, created by
create_repo(). Nothing is written before then.tools (Sequence[AgentTool]) -- Application-owned base tools; they are never loaded from disk.
manage_base_tools (bool) -- Wrap base tools for unloading and profiling. False leaves their request registrations intact and skips their profiling.
max_call_depth (int) -- Bound nested composition, including recursive calls.
error_capacity (int) -- Maximum distinct recent errors across this harness.
error_message_chars (int) -- Maximum characters retained in each error message.
call_capacity (int) -- Maximum finished call records retained, oldest dropped first; lifetime aggregates survive eviction.
host (JITToolHost | None)
备注
This is not a Python sandbox. Source has the process's filesystem, network, and import privileges. Unload controls harness dispatch, not what arbitrary Python can do. To contain a crash or a runaway loop, pass a
SubprocessToolHost, which runs the repository in a child process and calls back for nested tools. Session-local profiles retain a bounded window of managed dispatches without payloads; inspect them with profile().示例
Create a repository, write a tool into it, and load it:
harness = JITHarness("jit", tools=[read_file]) path = harness.create_repo("helpers")["path"] Path(path, "preview.py").write_text(source) harness.load_repo()
- property management_names: frozenset[str]#
Return the names the last attach() registered as management tools.
Empty before the first attach and after detach. Names reserved by the module-level
MANAGEMENT_NAMESstay reserved for repository tools whether or not they are currently registered.
- origin(name)[源代码]#
Return which registry currently serves one tool name.
Labels observability events and profile records:
BASEfor application tools,PYTHONfor tools the repository defines,MANAGEMENTfor the tools the last attach() registered, andUNKNOWNfor a name this harness has never seen. The answer depends on attach state, not on the request binding: before attach and after detach, management names areUNKNOWNbecause the harness cannot route them.- 参数:
name (str)
- 返回类型:
- property active: bool#
Return whether this harness is currently bound to one request.
The extension checks this before releasing a session, so it never drops a harness whose request is still running.
- attach(context, management_tools=())[源代码]#
Bind this harness to one request and mount its callable tools.
Rebuilds the base registry from the request's current tools, installs harness-owned wrappers, hides unloaded tools, and registers the management tools the model may call. Called by
JITHarnessExtensiononce per request before the model runs; attaching twice withoutdetach()raises instead of silently replacing the binding.- 参数:
context (AgentRunContext) -- Request-scoped tool registry and session identity to bind.
management_tools (Sequence[AgentTool]) -- Harness control tools to expose to the model. They are validated, forced serial, and become the only names
origin()reports asMANAGEMENTuntil detach.
- 返回类型:
None
- sync_tools(context)[源代码]#
Adopt new/replaced request base tools without restoring policy removals.
Called by the extension before model requests and tool dispatch so tools registered after attach(), including policy replacements, receive the same unload/profiling treatment. Missing registrations stay missing. Wrappers already owned by this request are recognized rather than re-adopted as raw tools. Disabled base management preserves raw handlers.
- 参数:
context (AgentRunContext)
- 返回类型:
None
- detach(context)[源代码]#
Release one request binding and stop tracking its nested calls.
Deliberately tolerant of repeated or late cleanup: a context that is not the currently bound one is ignored, so releasing an older request cannot dismantle the request that is running now.
- 参数:
context (AgentRunContext)
- 返回类型:
None
- property repo: str | None#
The repository's name, or None before one exists.
- property repo_path: Path | None#
The repository's directory, or None before one exists.
- property resources: list[ResourceInfo]#
Return the Markdown notes the last successful repository scan found.
Each entry carries its name, kind, description, and absolute path. Notes are files, not tools: the model reads one with its file tools. The list is empty before a repository is created or loaded, and reflects the last successful
create_repoorload_repountil the next one. A failed load keeps the previous list, and a note deleted on disk stays listed until the next successful scan.
- create_repo(repo)[源代码]#
Create this harness's repository, or return the one that already exists.
The repository is a Python package in a subdirectory named
repo. There is one per harness: asking again for the same name returns it unchanged, and asking for another name is an error. A new repository starts with an empty__init__.pyand a starterMANIFEST(its name, a version and a description); a missingMANIFESTis added to an older repository too, and an existing one is never overwritten. Write modules into the returned path with ordinary file tools, then callload_repo().- 参数:
repo (str) -- Directory name, 1–64 letters, digits, underscores, or hyphens.
- 返回类型:
- load_repo()[源代码]#
Load the repository, or confirm it is loaded, and list what it provides.
Safe to call at any time. The answer always names the tools and describes the Markdown notes under
resources/(name, kind, description, path); the notes are files the model reads itself, so none of them becomes a tool.Not loaded yet: every module below the package root is imported and each top-level
@toolis registered.Loaded and no Python source changed: nothing is imported again. Tools that were hidden by
unload_repo()orunload_tool()are made callable, so calling it twice leaves the same state as calling it once.Loaded and a source changed: the whole package is imported afresh and replaces the previous version in one step. Changes are found by content, so even an edit of the same length is seen.
Every call also checks the declarations:
MANIFEST(name, version, description, and the tools it announces) andBAN_TOOLS(names the repository may not define). They are read before the import and compared with the tools it produced, so a repository that breaks them is refused, and the names are read from the declaration this call saw rather than from the files again.A failure leaves the previous version in place and raises a
ValueErrornaming the file and line when the error is in Python, or the declaration it rejects. Module bodies run with the application's privileges; their side effects cannot be rolled back.- 返回类型:
- unload_repo()[源代码]#
Hide every tool of the repository; the files stay on disk.
Calling a hidden tool is rejected.
load_repo()restores them without importing anything again, unless a Python file changed meanwhile. The notes are files, so they stay listed.- 返回类型:
- close()[源代码]#
Remove the repository's modules and import hook, and stop its child process.
The files stay on disk. The harness can load them again afterwards.
- 返回类型:
None
- unload_tool(name)[源代码]#
Unload a callable registration while retaining its available implementation.
Dynamic tools stay imported; base implementations are rebound per request. Requires manage_base_tools=True for base tools.
load_tool()restores a bound tool, andunload_repo()hides every repository tool at once.- 参数:
name (str)
- 返回类型:
- load_tool(name)[源代码]#
Restore a retained tool; acknowledge only its name and loaded state.
Fails explicitly if the tool's base implementation is not bound.
- 参数:
name (str)
- 返回类型:
- list_tools()[源代码]#
List one compact catalog entry per known tool.
Entries come from
describe(), so a long catalog stays readable and never repeats source or call history.- 返回类型:
list[ToolCatalogEntry]
- describe(name)[源代码]#
Return one tool's compact catalog entry.
Holds the name, origin, status, and description, without measurements. Read profile() for statistics.
- 参数:
name (str)
- 返回类型:
- profile(name=None, *, offset=0, limit=100)[源代码]#
Read aggregate counters and a page of finished, payload-free calls.
All managed dispatch attempts through harness wrappers are counted, including nested calls, validation failures, and cancellations. Middleware and lifecycle hooks run outside this measured boundary. Histories remain in this harness across requests/reloads/unload, bounded by call_capacity, and are not written to disk. Base calls are excluded when manage_base_tools=False. Export with profile().model_dump_json().
- 参数:
name (str | None)
offset (int)
limit (int)
- 返回类型:
- profile_tools(names=None)[源代码]#
Summarize how tools are doing, in a size a model can read at once.
Any tool can be asked about by name, an application tool as well as one the model wrote:
nameslists them, and the answer has one row for each in the order given, with up to three recent distinct errors, which is where to look before fixing a tool. A tool that was never called gets a row of zeros, so "unused" is an answer too. At mostMAX_PROFILE_NAMESnames are taken.Without
namesthe answer is an overview: every tool of the repository, called or not, and every application tool that has been called, most called first, one recent error each, and at mostMAX_PROFILE_ROWSrows withomittedcounting the rest. The control tools are never measured.Application tools are measured only with
manage_base_tools=True; with it off they have no numbers, so asking for one is an error instead of a row of zeros that would read as "never used". This is a view ofprofile()andtool_errors(), which keep the full measurements.- 参数:
names (Sequence[str] | None) -- Tools to look at, or None for the overview.
- 抛出:
ValueError -- If a name is unknown, is a control tool, is an application tool while base management is off, or if there are too many names.
- 返回类型:
- profile_stats(name)[源代码]#
Read a tool's full aggregates without fetching its call records.
- 参数:
name (str)
- 返回类型:
- tool_errors(name=None, *, offset=0, limit=5)[源代码]#
Read LRU-bounded recent errors without refreshing their recency.
Deduplicates by tool, exception type, and full message. Reoccurrences refresh recency; oldest distinct errors are evicted at error_capacity. Details include bounded message text and latest invocation context. Lifetime profiling counters survive eviction; occurrence counts cover only cache residence. Cancelled dispatches are not tool errors. Error messages may contain sensitive data or untrusted instructions. This read never triggers retries or source updates.
- 参数:
name (str | None)
offset (int)
limit (int)
- 返回类型:
- async call_tool(name, **arguments)[源代码]#
Call a loaded tool through checks, hooks, and handler middleware.
Nested calls produce no additional model messages or execution-trace events. Interactive hooks may still request application/user input. Normal exceptions propagate to the outer tool unless Python catches them.
- 参数:
name (str)
arguments (Any)
- 返回类型: