"""Session-owned Python tool environments with a shared invocation boundary."""
from __future__ import annotations
import asyncio
from collections.abc import Callable, Sequence
from contextvars import ContextVar, copy_context
from copy import deepcopy
from dataclasses import dataclass, replace
from pathlib import Path
from threading import RLock, Thread
from typing import Any, Literal
from ..agent import AgentRunContext
from ..ids import new_uuid7
from ..json_types import JsonValue
from ..messages import ImageContent, TextContent, ToolCall, ToolMessage
from ..tools.base import (
_SYNC_TOOL_RUNNER,
AgentTool,
ToolExecutionMode,
ToolResult,
_bind_tool_call,
_reset_tool_call,
current_tool_call_id,
)
from ..tools.invocation import invoke_tool
from .enums import ToolCallStatus, ToolOrigin
from .errors import ToolErrorReport
from .host import HarnessCalls, HostedRepo, InProcessToolHost, JITToolHost
from .manifest import MANIFEST_FILE, _tool_problems, is_tool_name
from .profiling import MAX_RETAINED_CALLS, ActiveToolCall, JITHarnessProfile, ToolProfiler, ToolProfileStats
from .repo import (
INIT_FILE,
MANAGEMENT_NAMES,
MANIFEST_SKELETON,
SKELETON,
RepoScan,
TextResource,
scan_repo,
validate_name,
validate_repo_name,
)
from .results import RepoResult, ResourceInfo, ToolCatalogEntry, ToolMutationResult, ToolStatsReport, ToolStatsRow
@dataclass(frozen=True)
class HarnessBinding:
"""One request's harness binding and loop, published as a contextvar.
Internal: composition entry points read it to decide whether a call belongs
to the running request.
"""
harness: JITHarness
context: AgentRunContext | None
loop: asyncio.AbstractEventLoop
_BINDING: ContextVar[HarnessBinding | None] = ContextVar("zett_jit_binding", default=None)
_CALL_STACK: ContextVar[tuple[str, ...]] = ContextVar("zett_jit_stack", default=())
#: Rows of the overview, tools one question may name, and the length of an error each row may quote.
#: A row with its errors is under a thousand characters, so both fit one response comfortably.
MAX_PROFILE_ROWS = 20
MAX_PROFILE_NAMES = 10
PROFILE_ERROR_CHARS = 160
_PROFILE_CALL: ContextVar[tuple[JITHarness, ActiveToolCall] | None] = ContextVar("zett_jit_profile_call", default=None)
async def _run_sync_tool(function: Callable[..., Any], **arguments: Any) -> ToolResult:
"""Use one worker per active sync invocation, never a pool parents can block.
Cancelling the await cannot stop Python's worker. It is daemonized, receives
an independent Context, and can no longer dispatch after request detachment.
"""
loop = asyncio.get_running_loop()
completed = loop.create_future()
copied = copy_context()
def settle(value: Any, error: BaseException | None) -> None:
if not completed.done():
if error is None:
completed.set_result(value)
else:
completed.set_exception(error)
def run() -> None:
try:
value, error = copied.run(function, **arguments), None
except BaseException as failure:
value, error = None, failure
try:
loop.call_soon_threadsafe(settle, value, error)
except RuntimeError:
# An uninterruptible worker may finish after its loop has closed.
pass
Thread(target=run, name="zett-jit-tool", daemon=True).start()
return await completed
[文档]
class JITHarness(HarnessCalls):
"""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. :meth:`load_repo` imports it and registers every ``@tool``
it defines, and lists the Markdown notes under ``resources/``. The notes are
files the model reads itself, not tools.
Args:
directory: Where this harness keeps its repository. The repository is a
subdirectory of it, created by :meth:`create_repo`. Nothing is
written before then.
tools: Application-owned base tools; they are never loaded from disk.
manage_base_tools: Wrap base tools for unloading and profiling. False
leaves their request registrations intact and skips their profiling.
max_call_depth: Bound nested composition, including recursive calls.
error_capacity: Maximum distinct recent errors across this harness.
error_message_chars: Maximum characters retained in each error message.
call_capacity: Maximum finished call records retained, oldest dropped
first; lifetime aggregates survive eviction.
Note:
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
:class:`~zett_agent.jit_harness.host.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().
Examples:
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()
"""
def __init__(
self,
directory: str | Path,
*,
tools: Sequence[AgentTool] = (),
manage_base_tools: bool = True,
max_call_depth: int = 32,
error_capacity: int = 100,
error_message_chars: int = 4000,
call_capacity: int = MAX_RETAINED_CALLS,
host: JITToolHost | None = None,
) -> None:
if max_call_depth < 1:
raise ValueError("max_call_depth must be positive")
if not isinstance(manage_base_tools, bool):
raise ValueError("manage_base_tools must be a boolean")
self.manage_base_tools = manage_base_tools
self.max_call_depth = max_call_depth
self._directory = Path(directory).expanduser().absolute()
# Guards every registry mutation below. It is a plain re-entrant lock
# because helpers such as load_repo call each other.
self._lock = RLock()
# True only while a module body executes. It stops that trusted code from
# mutating the registry or dispatching tools through the harness.
self._compiling = False
# The request currently bound by attach(). Every wrapper checks this
# identity, so a stale wrapper cannot run against a later request.
self._context: AgentRunContext | None = None
# Tasks performing composed calls, mapped to the request that owns them.
# detach() cancels them so nested work cannot outlive its request.
self._nested_tasks: dict[asyncio.Task[Any], AgentRunContext] = {}
# Origin of every dynamic tool (always PYTHON) for the repository's
# currently loaded version. Unloading keeps these entries.
self._kinds: dict[str, ToolOrigin] = {}
# The Python tools of the loaded version. A reload replaces them as one
# set; unload only hides the names.
self._dynamic: dict[str, AgentTool] = {}
# The Markdown notes the last successful scan found. They are files the
# model reads itself, so nothing about them is registered or hidden.
self._resources: dict[str, TextResource] = {}
# The repository this harness serves, if any: its name, and the imported
# package while it is loaded. A failed first load leaves the name only.
self._repo: str | None = None
self._loaded: HostedRepo | None = None
# Where repository Python runs and how its tools are called. The default
# imports into this process; a SubprocessToolHost keeps crashes and hangs
# in a child.
self._host: JITToolHost = host or InProcessToolHost()
self._host.bind_dispatch(self._dispatch_nested)
# True while unload_repo() hides the loaded version.
self._hidden = False
# Whether the session directory was already searched for a repository
# left by an earlier process; done once, on the first attach().
self._adopted = False
# Base tools bound for the current request: constructor tools plus any a
# tool-registering extension added. Rebuilt on attach, dropped on detach.
self._bases: dict[str, AgentTool] = {}
# Tools passed to the constructor. They are the ones that stay known
# across requests when a request adds no binding.
self._provided: dict[str, AgentTool] = {}
# Control tools registered for the current request; cleared on detach
# because they close over that request's harness state.
self._management: dict[str, AgentTool] = {}
# Names the last attach() registered as management tools. Routing and
# profiling trust this set, not the module-level reservation, so a
# subclass can add or remove control tools.
self._management_names: frozenset[str] = frozenset()
# Wrapper handlers created in this request. Registry synchronization
# recognizes old wrappers (for example a restored policy baseline)
# without mistaking them for a new base implementation or wrapping twice.
self._wrapper_handlers: dict[int, Callable[..., Any]] = {}
# Session-local counters, call records, and the recent-error cache. Kept
# for the lifetime of this harness.
self._profiler = ToolProfiler(
error_capacity=error_capacity, error_message_chars=error_message_chars, call_capacity=call_capacity
)
# Every base name this harness has seen, accumulated across requests. An
# unbound name is reported as "missing" instead of unknown.
self._base_names: set[str] = set()
# Names hidden from dispatch. Unload only edits this set; loading removes
# the name again.
self._unloaded: set[str] = set()
for candidate in tools:
validate_name(candidate.name)
if candidate.name in self._provided:
raise ValueError(f"Duplicate tool: {candidate.name}")
if candidate.local_tool_search:
raise ValueError("JIT harness does not manage client-side tool search")
self._provided[candidate.name] = replace(candidate, parameters=deepcopy(candidate.parameters))
self._base_names.update(self._provided)
self._bases = dict(self._provided)
def _check_mutation(self) -> None:
if self._compiling:
raise RuntimeError("Cannot change the harness while loading Python source")
binding = _BINDING.get()
if binding is not None and binding.harness is self and binding.context is not self._context:
raise RuntimeError("This tool's request has already closed")
def _known(self, name: str) -> None:
"""Reject a dispatch name the harness cannot route."""
if name in self._management_names:
raise ValueError("JIT management tools cannot be changed or composed")
if name not in self._kinds and name not in self._base_names:
raise ValueError(f"Unknown tool: {name}")
def _known_mutation(self, name: str) -> None:
"""Reject unloading or loading a management name, including a removed one."""
if name in self._management_names or name in MANAGEMENT_NAMES:
raise ValueError("JIT management tools cannot be changed or composed")
self._known(name)
@property
def management_names(self) -> 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_NAMES`` stay reserved for repository tools
whether or not they are currently registered.
"""
with self._lock:
return self._management_names
[文档]
def origin(self, name: str) -> ToolOrigin:
"""Return which registry currently serves one tool name.
Labels observability events and profile records: ``BASE`` for application
tools, ``PYTHON`` for tools the repository defines, ``MANAGEMENT`` for the
tools the last attach() registered, and ``UNKNOWN`` for 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 are
``UNKNOWN`` because the harness cannot route them.
"""
with self._lock:
return self._resolve_origin(name)
def _resolve_origin(self, name: str) -> ToolOrigin:
if name in self._management_names:
return ToolOrigin.MANAGEMENT
if name in self._kinds:
return self._kinds[name]
if name in self._base_names:
return ToolOrigin.BASE
return ToolOrigin.UNKNOWN
def _raw_tool(self, name: str, context: AgentRunContext | None) -> AgentTool:
if self._compiling:
raise RuntimeError("Cannot call tools while loading Python source")
self._known(name)
if context is not self._context:
raise RuntimeError("This tool's request has already closed")
if self._is_unloaded(name):
raise ValueError(f"Tool {name!r} is unloaded")
if context is not None and name not in context.tools:
raise ValueError(f"Tool {name!r} is not registered in the current request")
registered = self._dynamic.get(name) or self._bases.get(name)
if registered is None:
raise ValueError(f"Missing base tool: {name}")
return registered
def _is_unloaded(self, name: str) -> bool:
return name in self._unloaded and (self.manage_base_tools or name in self._kinds)
def _wrapper(self, registered: AgentTool, context: AgentRunContext | None) -> AgentTool:
name = registered.name
async def guarded(**arguments: Any) -> ToolResult:
# Resolve on every invocation: an old wrapper cannot call a tool
# after unload or retain a superseded implementation.
return await self._profiled_call(name, context, arguments)
if context is not None:
self._wrapper_handlers[id(guarded)] = guarded
# Preserve provider deferral: a base tool marked deferred must keep
# reaching the provider as such, and the manager never defers its own.
return replace(registered, handler=guarded, parameters=deepcopy(registered.parameters))
async def _execute_handler(
self, name: str, target: AgentTool, context: AgentRunContext | None, arguments: dict[str, Any]
) -> ToolResult:
binding_token = _BINDING.set(HarnessBinding(self, context, asyncio.get_running_loop()))
stack_token = _CALL_STACK.set((*_CALL_STACK.get(), name))
worker_token = _SYNC_TOOL_RUNNER.set(_run_sync_tool)
try:
return await target(arguments)
finally:
_SYNC_TOOL_RUNNER.reset(worker_token)
_CALL_STACK.reset(stack_token)
_BINDING.reset(binding_token)
async def _dispatch_nested(self, name: str, arguments: dict[str, JsonValue]) -> ToolResult:
"""Answer one ``harness.call_tool`` that arrived from a child process.
The call belongs to the request currently bound to this harness, so it takes
the same path as an in-process nested call: name checks, extension hooks, the
request's tool registry, and profiling. The binding is set here because the
frame is handled outside the task that is awaiting the outer tool.
"""
binding_token = _BINDING.set(HarnessBinding(self, self._context, asyncio.get_running_loop()))
try:
return await self.call_tool(name, **arguments)
finally:
_BINDING.reset(binding_token)
async def _profiled_call(self, name: str, context: AgentRunContext | None, arguments: dict[str, Any]) -> ToolResult:
# Invalid source-loading calls and stale references cannot append to a
# new request's profile. Every actual dispatch attempt is tracked once.
with self._lock:
if self._compiling:
raise RuntimeError("Cannot call tools while loading Python source")
if context is not self._context:
raise RuntimeError("This tool's request has already closed")
origin = self._resolve_origin(name)
if origin is ToolOrigin.BASE and not self.manage_base_tools:
target = self._raw_tool(name, context)
if len(_CALL_STACK.get()) >= self.max_call_depth:
raise RuntimeError(f"JIT tool call depth exceeded {self.max_call_depth}")
else:
target = None
if target is not None:
return await self._execute_handler(name, target, context, arguments)
previous = _PROFILE_CALL.get()
parent = previous[1] if previous is not None and previous[0] is self else None
call_id = current_tool_call_id()
# Direct Python calls do not bind an ID; do not reuse their parent's ID.
if call_id is None or (parent is not None and call_id == parent.call_id):
call_id = new_uuid7()
active = self._profiler.start(
name,
arguments,
call_id=call_id,
parent=parent,
origin=origin,
)
token = _PROFILE_CALL.set((self, active))
output = None
error: BaseException | None = None
status = ToolCallStatus.REJECTED
try:
with self._lock:
if name in self._management_names:
if context is None or name not in context.tools:
raise ValueError(f"Tool {name!r} is not registered in the current request")
target = self._management[name]
else:
target = self._raw_tool(name, context)
stack = _CALL_STACK.get()
if len(stack) >= self.max_call_depth:
raise RuntimeError(f"JIT tool call depth exceeded {self.max_call_depth}")
status = ToolCallStatus.FAILED
output = await self._execute_handler(name, target, context, arguments)
status = ToolCallStatus.COMPLETED
return output
except asyncio.CancelledError as failure:
status, error = ToolCallStatus.CANCELLED, failure
raise
except BaseException as failure:
error = failure
raise
finally:
try:
self._profiler.finish(active, status, output, error)
finally:
_PROFILE_CALL.reset(token)
def _publish(self, name: str) -> None:
context = self._context
if context is not None:
if self._is_unloaded(name):
context.tools.pop(name, None)
else:
registered = self._dynamic.get(name) or self._bases.get(name)
if registered is not None:
context.tools[name] = (
registered
if name in self._bases and not self.manage_base_tools
else self._wrapper(registered, context)
)
@property
def active(self) -> 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.
"""
with self._lock:
return self._context is not None
[文档]
def attach(self, context: AgentRunContext, management_tools: Sequence[AgentTool] = ()) -> None:
"""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
:class:`~zett_agent.jit_harness.extension.JITHarnessExtension` once per
request before the model runs; attaching twice without :meth:`detach`
raises instead of silently replacing the binding.
Args:
context: Request-scoped tool registry and session identity to bind.
management_tools: Harness control tools to expose to the model. They
are validated, forced serial, and become the only names
:meth:`origin` reports as ``MANAGEMENT`` until detach.
"""
with self._lock:
if self._context is not None:
raise RuntimeError("This harness already has an active request")
bases = dict(self._provided)
for registered in context.tools.values():
validate_name(registered.name)
if registered.name in self._dynamic:
raise ValueError(f"Base tool conflicts with dynamic tool: {registered.name}")
if registered.local_tool_search:
raise ValueError("JIT harness does not manage client-side tool search")
bases[registered.name] = registered
tools = tuple(management_tools)
for tool in tools:
if not isinstance(tool, AgentTool):
raise ValueError("Management tools must be AgentTool instances")
names = [tool.name for tool in tools]
if len(names) != len(set(names)):
raise ValueError("Duplicate management tool name")
for tool in tools:
if not is_tool_name(tool.name):
raise ValueError(f"Invalid management tool name: {tool.name!r}")
if tool.local_tool_search or tool.deferred:
raise ValueError("Management tools must be ordinary, non-deferred function tools")
if tool.name in bases or tool.name in self._base_names or tool.name in self._kinds:
raise ValueError(f"Management tool conflicts with an existing tool: {tool.name}")
tools = tuple(replace(tool, execution_mode=ToolExecutionMode.SERIAL) for tool in tools)
self._context = context
self._bases = bases
self._base_names.update(bases)
# Publish the actual names before adopting a repository: its loader
# must reject a tool that would shadow a management name.
self._management = {tool.name: tool for tool in tools}
self._management_names = frozenset(self._management)
self._adopt_existing()
for name in self._base_names | self._kinds.keys():
self._publish(name)
for registered in self._management.values():
context.register_tool(self._wrapper(registered, context))
[文档]
def detach(self, context: AgentRunContext) -> None:
"""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.
"""
with self._lock:
if self._context is context:
self._context = None
# Extension tools often close over a request. Do not retain
# those callables or allow them to run after their request.
self._bases = dict(self._provided)
self._management.clear()
self._management_names = frozenset()
self._wrapper_handlers.clear()
for task, owner in tuple(self._nested_tasks.items()):
if owner is context:
task.get_loop().call_soon_threadsafe(task.cancel)
def _adopt_existing(self) -> None:
"""Pick up the repository an earlier process left in this directory, once.
A resumed session should find its tools again without the model asking.
Only a single repository is adopted, and a load failure is not fatal here:
the model can call ``repo_load`` to see the error and fix the files.
"""
if self._adopted:
return
self._adopted = True
if self._repo is not None or not self._directory.is_dir():
return
found = [
path.name
for path in self._directory.iterdir()
if path.is_dir() and (path / INIT_FILE).is_file() and not path.name.startswith(".")
]
if len(found) == 1:
self._repo = found[0]
try:
self._load()
except Exception:
# The files stay on disk and ``repo_load`` reports the error.
pass
@property
def repo(self) -> str | None:
"""The repository's name, or None before one exists."""
return self._repo
@property
def repo_path(self) -> Path | None:
"""The repository's directory, or None before one exists."""
return None if self._repo is None else self._directory / self._repo
def _result(self, status: Literal["created", "loaded", "unloaded"] | None = None) -> RepoResult:
assert self._repo is not None
if status is None:
status = "unloaded" if self._hidden or self._loaded is None else "loaded"
return {
"repo": self._repo,
"path": str(self._directory / self._repo),
"status": status,
"tools": sorted(self._dynamic),
"resources": self._resource_infos(),
}
def _resource_infos(self) -> list[ResourceInfo]:
assert self._repo is not None
root = self._directory / self._repo
return [
{
"name": resource.name,
"kind": resource.kind,
"description": resource.description,
"path": str(root / resource.path),
}
for resource in sorted(self._resources.values(), key=lambda item: item.name)
]
@property
def resources(self) -> 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_repo`` or ``load_repo`` until the next one. A failed
load keeps the previous list, and a note deleted on disk stays listed
until the next successful scan.
"""
with self._lock:
return self._resource_infos() if self._repo is not None else []
[文档]
def create_repo(self, repo: str) -> RepoResult:
"""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__.py`` and a starter ``MANIFEST`` (its name, a version and a
description); a missing ``MANIFEST`` is added to an older repository too, and an
existing one is never overwritten. Write modules into the returned path with
ordinary file tools, then call :meth:`load_repo`.
Args:
repo: Directory name, 1–64 letters, digits, underscores, or hyphens.
"""
with self._lock:
self._check_mutation()
validate_repo_name(repo)
self._adopt_existing()
if self._repo is not None and self._repo != repo:
raise ValueError(f"This session already has the repository {self._repo!r}; keep using it")
path = self._directory / repo
existed = (path / INIT_FILE).is_file()
path.mkdir(parents=True, exist_ok=True)
# Each skeleton is written only when its file is absent, so a MANIFEST the model
# wrote before calling repo_create is kept, and an older repository without one
# gains it here.
if not existed:
(path / INIT_FILE).write_text(SKELETON, encoding="utf-8")
if not (path / MANIFEST_FILE).is_file():
(path / MANIFEST_FILE).write_text(MANIFEST_SKELETON.format(name=repo), encoding="utf-8")
self._repo = repo
return self._result(None if existed else "created")
def _require_tools(self, scan: RepoScan) -> None:
"""Judge the tools of an existing version against the declarations just read.
Used when the import is skipped, because that version is what the next request
would see. A load that imports checks in the loader instead, against what the
import produced; judging there with the previous version's names would refuse
every edit that adds or removes a declared tool.
"""
assert self._loaded is not None and scan.policy.manifest is not None
problems = _tool_problems(scan.policy.manifest, scan.policy.banned, self._loaded.tools)
if problems:
raise ValueError("; ".join(problems))
def _load(self) -> None:
"""Make the repository's tools current and list its notes.
The files are checked first, so a bad one fails the call before anything
changes. When no Python source differs from what is imported, nothing is
imported again: the modules are resident, and importing them would only run
their bodies a second time, so the tools on record are the ones the declarations
must agree with. Otherwise the new package is imported before anything changes,
and the loader judges the tools that import produced against the declarations
this scan read, so a failure keeps the previous version registered. Module
bodies run with ``_compiling`` set, which stops them from mutating the harness
or dispatching tools. The host decides where the import happens.
"""
assert self._repo is not None
directory = self._directory / self._repo
scan = scan_repo(directory)
if self._loaded is not None and self._loaded.fingerprint == scan.fingerprint and not self._host.needs_reload:
self._require_tools(scan)
self._resources = scan.resources
self._reveal()
return
self._compiling = True
try:
loaded = self._host.load(directory, scan, self)
finally:
self._compiling = False
management_clash = sorted(loaded.tools.keys() & self._management_names)
if management_clash:
loaded.close()
raise ValueError(f"Tool {management_clash[0]!r} is reserved for JIT management")
clash = sorted(loaded.tools.keys() & self._base_names)
if clash:
loaded.close()
raise ValueError(f"Tool {clash[0]!r} is already an application tool; choose another name")
# A third-party host may import without going through ``repo.load_repo``, so the
# declarations are enforced here too, against the same snapshot.
assert scan.policy.manifest is not None
problems = _tool_problems(scan.policy.manifest, scan.policy.banned, loaded.tools)
if problems:
loaded.close()
raise ValueError("; ".join(problems))
self._resources = scan.resources
previous, previous_names = self._loaded, set(self._dynamic)
self._loaded = loaded
self._dynamic = dict(loaded.tools)
self._kinds = dict.fromkeys(loaded.tools, ToolOrigin.PYTHON)
# A fresh version starts loaded, whatever state the old one was in.
self._hidden = False
self._unloaded -= previous_names | self._dynamic.keys()
context = self._context
if context is not None:
for name in previous_names - self._dynamic.keys():
context.tools.pop(name, None)
for name in self._dynamic:
self._publish(name)
if previous is not None:
previous.close()
def _reveal(self) -> None:
"""Make every tool of the loaded version callable again, importing nothing."""
revived = set(self._dynamic) if self._hidden else self._unloaded & self._dynamic.keys()
self._hidden = False
self._unloaded -= revived
for name in revived:
self._publish(name)
[文档]
def load_repo(self) -> RepoResult:
"""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 ``@tool`` is registered.
* **Loaded and no Python source changed**: nothing is imported again. Tools
that were hidden by :meth:`unload_repo` or :meth:`unload_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) and ``BAN_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 ``ValueError``
naming 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.
"""
with self._lock:
self._check_mutation()
if self._repo is None:
raise ValueError("Create the repository first with repo_create")
self._load()
return self._result()
[文档]
def unload_repo(self) -> RepoResult:
"""Hide every tool of the repository; the files stay on disk.
Calling a hidden tool is rejected. :meth:`load_repo` restores them without
importing anything again, unless a Python file changed meanwhile. The notes
are files, so they stay listed.
"""
with self._lock:
self._check_mutation()
if self._repo is None:
raise ValueError("Create the repository first with repo_create")
self._unloaded.update(self._dynamic)
self._hidden = True
for name in self._dynamic:
self._publish(name)
return self._result("unloaded")
[文档]
def close(self) -> None:
"""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.
"""
with self._lock:
if self._loaded is not None:
self._loaded.close()
self._loaded = None
self._host.close()
[文档]
def describe(self, name: str) -> ToolCatalogEntry:
"""Return one tool's compact catalog entry.
Holds the name, origin, status, and description, without measurements.
Read profile() for statistics.
"""
with self._lock:
self._known_mutation(name)
registered = self._dynamic.get(name) or self._bases.get(name)
status: Literal["loaded", "unloaded", "missing", "unavailable"] = (
"unloaded" if self._is_unloaded(name) else "loaded" if registered is not None else "missing"
)
if self._context is not None and status == "loaded" and name not in self._context.tools:
status = "unavailable"
return {
"name": name,
"origin": self._resolve_origin(name),
"status": status,
"description": registered.description if registered is not None else "",
}
[文档]
def profile(self, name: str | None = None, *, offset: int = 0, limit: int = 100) -> JITHarnessProfile:
"""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().
"""
return self._profiler.report(name, offset, limit)
[文档]
def profile_stats(self, name: str) -> ToolProfileStats:
"""Read a tool's full aggregates without fetching its call records."""
return self._profiler.summary(name)
@staticmethod
def _result_content(registered: AgentTool, output: ToolResult) -> str | list[TextContent | ImageContent]:
if isinstance(output, (TextContent, ImageContent)):
return [output]
if (
isinstance(output, list)
and output
and all(isinstance(part, (TextContent, ImageContent)) for part in output)
):
return output
return registered.serialize_result(output)