SubprocessToolHost#
Let a model keep its own tools and notes in a repository on disk. See the guide.
- class zett_agent.jit_harness.host.SubprocessToolHost(*, python=None, sandbox=None, call_timeout=300.0, load_timeout=120.0, env=None)[source]#
Import and run a repository in a child process, and call back over a pipe.
The child starts with
sys.executable, imports the package, and then answers two kinds of frame: the agent asks it to run a tool, and the tool asks the agent to run a loaded tool throughharness.call_tool. Nested calls therefore take the same path as before, with extensions, events, and profiling in the agent.A crash, a hang, or a runaway allocation stays in the child;
invoke()gives up aftercall_timeoutand kills it. The nextload()starts a fresh one.One host backs one repository: it owns a single child, so create one host per session. Sharing an instance between harnesses routes every version into whichever child the last load started.
Lifecycle, and when a process exists:
repo_create ──► no process repo_load ──► spawn ──► import ──► tool schemas ▲ | source changed ┤ | invoke / call frames │ ▼ crash, timeout ─┤ reuse the same child or close() ──┘ | | close/forget_session ─────────────────► shutdown, kill after 2 sA reload is a new interpreter: the previous version’s modules, its module-level state, and the alias it registered are gone instead of lingering until the process exits. Tool calls never spawn: with no live child they fail with
JITHostCrashedand the model must callrepo_loadagain.- Parameters:
python (str | None) – Interpreter for the child; the agent’s own by default.
sandbox (Sandbox | None) – Boundary to start the child inside, such as one built by
create_sandbox(). The child only needs to read the repository and the interpreter, and never writes.call_timeout (float | None) – Seconds one tool may run before it is killed and reported as a timeout.
Nonewaits as long as it takes.load_timeout (float | None) – Seconds an import may take. Module bodies run here too, so this is a real limit, not a formality.
env (Mapping[str, str] | None) – Extra environment variables for the child.
Note
Values cross the pipe as JSON. A tool returns JSON values,
TextContent,ImageContent, bytes, or lists and dictionaries of them; anything else raisesToolNotSerializable, because pickle would let the child run code in the agent’s process. User code’sprintgoes to stderr.Examples
Run each repository in a read-only sandbox:
policy = SandboxPolicy(...) host = SubprocessToolHost(sandbox=create_sandbox(policy)) harness = JITHarness(directory, host=host)
- bind_dispatch(dispatch)[source]#
Remember the agent-side dispatcher used for nested
harnesscalls.- Parameters:
dispatch (Callable[[str, dict[str, JsonValue]], Awaitable[ToolResult]])
- Return type:
None
- load(directory, scan, bridge)[source]#
Start a fresh child process, import the package there, and build its tools.
Any earlier child is stopped first. A reload therefore begins from an empty interpreter: the previous version’s modules, its module-level state, and any alias it registered are gone, which is what the in-process host gets from discarding its old package.
- Parameters:
directory (Path)
scan (RepoScan)
bridge (HarnessCalls)
- Return type:
- property needs_reload: bool#
Whether the child must import the repository again.
A live child is not enough: only a child that finished a load holds the version the parent’s fingerprint describes. One still starting, or one whose import failed, holds nothing, and trusting the fingerprint would report the old tools as loaded while calls fail in a process that does not have them.