Hosted Hermes serves many tenants' agents from one process, and each agent's commands, code, background processes and files must live in that agent's own remote sandbox. Until now the environment a tool reached was keyed by task id, session key or profile path, so nothing tied it to the agent the bound scope names, and a hosted scope could fall back to running on this host. The sandbox an operation reaches is now derived from the bound scope's (tenant, agent) alone (tools/environments/sandbox_scope.py). In a hosted scope: - every environment key is the agent's sandbox key, whatever task id a call carries; the terminal.backend provider must declare serves_hosted_scopes (built-ins are refused) and receives scope= and sandbox= at creation, with task_id set to the identity's key; - core wraps the returned environment in ScopedEnvironment, which refuses any call made under another agent's scope (or none) and passes the bound scope to execute/fetch_* when the backend declares it; a cached environment that is not the agent's own is refused at lookup; - terminal, execute_code (always the remote kernel), background processes and read/write/patch/search go through that environment; file-tool bookkeeping never stats or opens the requested path on the host; - background processes carry the sandbox key they were started under, and another agent's process is indistinguishable from a missing one (get, list, prefix lookup, kill sweeps). Environments gain start_process/attach_process/stop_process with a bidirectional byte stream (ProcessStream), implemented for the local backend and exec-prefix backends; a ProcessRef naming another sandbox is refused. Credentials are typed as broker references only (CredentialRef); no broker exists yet, so none are passed. Local installs are unchanged: with no hosted scope bound every branch keeps today's keying and backends. A conformance module with a recording stub backend runs the isolation checks through the real tool registry, so a hosted backend plugin can run the same checks against its own provider. process_registry.py's handoff handler moves to process_registry_handoff.py to keep the facade under its size cap.
47 lines
2.8 KiB
Python
47 lines
2.8 KiB
Python
"""``process_manage(action="handoff")``: a subagent hands a running background process to its parent."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Optional
|
|
|
|
_MAX_HANDOFFS_PER_CHILD = 3
|
|
|
|
|
|
def handoff_process(session_id: str, args: dict, task_id: Optional[str]) -> dict:
|
|
"""Subagent-only: transfer a running background process to the parent agent so its completion is delivered THERE
|
|
(child-owned process notices are suppressed and child teardown kills what it owns). Validated against the live spawn
|
|
tree: the caller must be a registered child and must own the process; anything else is an error, never a silent
|
|
no-op, so a PID mentioned in prose can't masquerade as a transfer."""
|
|
from tools.process_registry import process_registry
|
|
from tools.delegate_tool_registry import _active_subagents, _active_subagents_lock
|
|
from tools.terminal_tool import _resolve_container_task_id
|
|
with _active_subagents_lock:
|
|
record = _active_subagents.get(str(task_id or ""))
|
|
child = record.get("agent") if record else None
|
|
parent_ref = getattr(child, "_delegate_parent_ref", None)
|
|
parent = parent_ref() if callable(parent_ref) else None
|
|
if parent is None:
|
|
return {"error": "handoff is only available to a running subagent with a live parent; you are not one."}
|
|
parent_owner = str(getattr(parent, "_current_task_id", "") or getattr(parent, "session_id", "") or "")
|
|
if not parent_owner:
|
|
return {"error": "parent has no process owner id yet; retry after the parent's turn has started."}
|
|
handed = getattr(child, "_handed_off_processes", None)
|
|
if handed is None:
|
|
handed = child._handed_off_processes = []
|
|
if len(handed) >= _MAX_HANDOFFS_PER_CHILD:
|
|
return {"error": f"handoff cap reached ({_MAX_HANDOFFS_PER_CHILD} per subagent); wait on or kill the rest yourself."}
|
|
note = str(args.get("data") or "").strip()
|
|
if not note:
|
|
return {"error": "handoff requires `data`: one sentence saying what the process is for and what the parent should do with its result."}
|
|
session = process_registry.transfer_ownership(
|
|
session_id, from_owner=str(task_id or ""), to_owner=parent_owner,
|
|
to_task_id=_resolve_container_task_id(parent_owner),
|
|
to_session_key=str(getattr(parent, "session_id", "") or ""), note=note)
|
|
if session is None:
|
|
return {"error": f"cannot hand off {session_id}: not a running process you own (already exited? read its result "
|
|
"with poll/log and report it instead)."}
|
|
handed.append({"session_id": session.id, "command": session.command, "note": note})
|
|
return {"status": "handed_off", "session_id": session.id, "command": session.command,
|
|
"note": "Your parent now owns this process and will receive its completion; you will not. Mention the handoff "
|
|
"in your final answer."}
|