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)[源代码]#

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 through harness.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 after call_timeout and kills it. The next load() 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 s

A 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 JITHostCrashed and the model must call repo_load again.

参数:
  • 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. None waits 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.

备注

Values cross the pipe as JSON. A tool returns JSON values, TextContent, ImageContent, bytes, or lists and dictionaries of them; anything else raises ToolNotSerializable, because pickle would let the child run code in the agent's process. User code's print goes to stderr.

示例

Run each repository in a read-only sandbox:

policy = SandboxPolicy(...)
host = SubprocessToolHost(sandbox=create_sandbox(policy))
harness = JITHarness(directory, host=host)
bind_dispatch(dispatch)[源代码]#

Remember the agent-side dispatcher used for nested harness calls.

参数:

dispatch (Callable[[str, dict[str, JsonValue]], Awaitable[ToolResult]])

返回类型:

None

load(directory, scan, bridge)[源代码]#

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.

参数:
返回类型:

HostedRepo

close()[源代码]#

Stop the child process, failing anything still waiting on it.

返回类型:

None

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.

async invoke(name, arguments)[源代码]#

Run one tool in the child process and return its decoded result.

参数:
  • name (str)

  • arguments (dict[str, JsonValue])

返回类型:

ToolResult