Files
hermes-agent/tests/tools/test_arg_validation.py
Ben Barclay 178b1d0349 feat(sandbox): a hosted scope's tools reach only its agent's sandbox
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.
2026-11-11 10:22:54 -05:00

54 lines
2.9 KiB
Python

"""Argument-name checks in front of built-in tools, driven through the real dispatch."""
import json
from model_tools import handle_function_call
from tools.registry import registry
_SKILL = "---\nname: flat-shape-skill\ndescription: Use when probing. Does nothing.\n---\n# Probe\nbody\n"
def test_missing_required_argument_is_refused_before_the_handler_runs(monkeypatch, tmp_path):
searched = []
monkeypatch.setattr("tools.file_tools.search_tool", lambda *a, **kw: searched.append(kw) or "{}")
refused = json.loads(handle_function_call("search_files", {"path": str(tmp_path)}, task_id="t"))
assert "pattern" in refused["error"] and "NOT invoked" in refused["error"]
assert searched == []
# A files listing takes no pattern: a required key the handler does without stays its call.
handle_function_call("search_files", {"target": "files", "path": str(tmp_path)}, task_id="t")
handle_function_call("search_files", {"pattern": "\t", "path": str(tmp_path)}, task_id="t") # a tab is a pattern
assert [(kw["target"], kw["pattern"]) for kw in searched] == [("files", ""), ("content", "\t")]
# Every other built-in is refused by the generic check, before its handler.
monkeypatch.setattr("tools.file_tools.read_file_tool", lambda *a, **kw: searched.append("read") or "{}")
refused = json.loads(handle_function_call("read_file", {"offset": 3}, task_id="t"))
assert "path" in refused["error"] and "NOT invoked" in refused["error"] and "read" not in searched
# The flat single-op shape old transcripts replay omits the required ``operations``.
created = json.loads(handle_function_call(
"skill_manage", {"action": "create", "name": "flat-shape-skill", "content": _SKILL}, task_id="t"))
assert created["success"] is True
def test_unknown_argument_reaches_the_handler_and_is_flagged(monkeypatch):
received = {}
monkeypatch.setattr("tools.process_registry_handoff.handoff_process",
lambda session_id, args, task_id: received.update(args) or {"status": "ok"})
call = {"action": "handoff", "session_id": "proc_1", "sesion_id": "proc_2"}
result = json.loads(handle_function_call("process_manage", dict(call), task_id="t"))
assert received == call
assert result["status"] == "ok" and "sesion_id" in result["_warning"]
# A third-party schema's property list is no promise about what its handler accepts. The
# handler is a ``tools`` one, as an MCP tool's is, so only the ``mcp-*`` toolset exempts it.
registry.register(
name="mcp_probe_handoff", toolset="mcp-probe", handler=registry.get_entry("process_manage").handler,
schema={"name": "mcp_probe_handoff", "description": "probe",
"parameters": {"type": "object", "properties": {"a": {"type": "string"}}, "required": ["a"]}})
try:
assert json.loads(handle_function_call("mcp_probe_handoff", dict(call), task_id="t")) == {"status": "ok"}
finally:
registry.deregister("mcp_probe_handoff")