## Summary - Every node of a sub-agent DAG ran `exec` on the host however `tools.sandbox` was configured: `run_dag` grew a `sandbox` seam that no production caller ever fed, so the `raven_loop` node backend built its `ExecTool` on `DirectExecutor` -- silently, exactly what `docs/sandbox/usage.md` promises never happens, on the path most likely to process untrusted input in parallel (#796). - The dormant seam becomes a factory. `run_dag`'s parameter is now `sandbox_factory`, called once per node with the graph's workdir; the executor enters before the node's backend runs and exits after the node's bookkeeping, so a node's VM lives exactly as long as the node. One VM per node is the spawn paths' own rule (manager.py: "each subagent runs its own sandbox VM"), and the graph's semaphore already bounds how many boot at once. With no factory handed over, a node backend still receives `None`, byte-for-byte today's behavior. - `SubagentManager.sandbox_executor_for(workspace)` is the builder both dispatch routes share: the one sandbox config, owned-id set, home mount and sandbox dir a spawn's executor gets. The two inline spawn build sites now call it too, so the routes cannot drift apart. `SubAgentDagTool` takes it as `sandbox_for` and bridges it into `run_dag`; all four construction sites (both wiring, both playbook) pass the manager's builder. - Verified on real infrastructure as well as in tests: `sandbox_executor_for` under `backend: boxlite` starts a VM where `uname -s` is `Linux` and `pwd` is the mounted `/workspace` (macOS arm64 host, boxlite 0.9.5) -- the reproduction from the issue, now answering what it should. ## Type - [x] Fix ## Verification - [x] Relevant tests pass locally: `uv run pytest tests/test_subagent_dag_runner.py tests/test_subagent_dag_core.py tests/test_subagent_manager.py tests/test_playbook_stint.py tests/test_subagent_dag_control_tools.py tests/test_subagent_dag_live.py tests/test_rpc_dag.py tests/test_agent_playbook_delegate.py -q` -> 948 passed; `uv run pytest tests/test_cli_playbook_commands.py tests/test_harness_curator_hosting.py tests/test_subagent_third_party.py tests/test_cli_subagent_setup.py -q` -> 408 passed - [x] New tests failed first: the runner-level factory test raised `TypeError` (`run_dag() got an unexpected keyword argument 'sandbox_factory'`) and the tool-level test raised `TypeError` on `sandbox_for` before their implementations landed; the no-factory test pins today's ladder and passes on both sides - [x] Relevant lint / type checks pass locally: `ruff check` and `ruff format --check` on all eight changed files -> clean - [x] User-facing docs or screenshots are updated when needed: no user-facing surface changes; the fix makes behavior match what `docs/sandbox/usage.md` already promises ## Risk - [x] Security impact considered: this is the security fix -- a prompt-injected command in a DAG node now runs inside the configured VM instead of a host shell. The factory only selects between executors the spawn paths already build; it adds no new privilege - [x] Backward compatibility considered: no `sandbox_for` handed over (tests, offline CLI paths, old hosts) leaves every node backend receiving `executor=None`, exactly as before -- pinned by test; the `run_dag` parameter rename has no production caller passing the old name - [x] Rollback path is clear: plain revert; node exec returns to the host exactly as it was ## Related Issues Fixes #796
465 lines
18 KiB
Python
465 lines
18 KiB
Python
"""A playbook is reached through a tool, never by intercepting the turn.
|
|
|
|
This file used to assert the opposite: a funnel ran ahead of the turn, and on a
|
|
hit it returned the playbook's reply *instead of* running the model, recording
|
|
both sides of the exchange itself because it had skipped ``_save_turn``. What is
|
|
guarded now is that no such branch exists -- every message reaches the model, and
|
|
what the loop does per turn is give the tool the material it needs to describe
|
|
itself.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from raven.agent.loop import AgentLoop
|
|
from raven.agent.loop.bundles import EngineWiring, ToolWiring, TurnPolicy
|
|
from raven.contracts.llm_provider import LLMResponse
|
|
from raven.contracts.tool import Tool
|
|
from raven.spine.message import ChatType, Source
|
|
from raven.spine.turn import Origin, TurnRequest
|
|
|
|
|
|
class _Provider:
|
|
def __init__(self) -> None:
|
|
self.calls = 0
|
|
|
|
async def chat_with_retry(self, **kwargs) -> LLMResponse:
|
|
self.calls += 1
|
|
return LLMResponse(content="normal turn ran")
|
|
|
|
def get_default_model(self) -> str:
|
|
return "fake/default"
|
|
|
|
|
|
class _StubPlaybooks:
|
|
empty = False
|
|
|
|
def __init__(self) -> None:
|
|
self.contexts: list[dict] = []
|
|
|
|
def listing(self, message: str = "") -> list[tuple[str, str]]:
|
|
return [("weekly-feedback", "weekly user-feedback analysis")]
|
|
|
|
def names(self) -> list[str]:
|
|
return ["weekly-feedback"]
|
|
|
|
def set_context(self, **kwargs) -> None:
|
|
self.contexts.append(kwargs)
|
|
|
|
|
|
def _stub_edges(loop: AgentLoop) -> None:
|
|
async def _noop() -> None:
|
|
return None
|
|
|
|
loop._start_executor = _noop
|
|
loop._connect_mcp = _noop
|
|
|
|
|
|
def _req(text: str, *, conversation: str) -> TurnRequest:
|
|
return TurnRequest(
|
|
origin=Origin.USER,
|
|
source=Source(channel="web", chat_id="c", sender_id="u", chat_type=ChatType.DM),
|
|
text=text,
|
|
conversation=conversation,
|
|
)
|
|
|
|
|
|
async def test_a_message_that_names_a_playbook_still_reaches_the_model(tmp_path):
|
|
"""The decision belongs to the turn, so the turn has to happen.
|
|
|
|
A message matching a playbook's vocabulary used to be answered without the
|
|
model running at all -- one gate call decided, on this message alone, with no
|
|
conversation history. Now the model sees the message and the playbook in the
|
|
same breath and picks.
|
|
"""
|
|
provider = _Provider()
|
|
loop = AgentLoop(provider=provider, workspace=tmp_path, model="fake/default")
|
|
_stub_edges(loop)
|
|
loop._playbooks = _StubPlaybooks()
|
|
|
|
reply, _ = await loop._process_message(_req("sort out this week's user feedback", conversation="web:abc"))
|
|
|
|
assert reply == "normal turn ran"
|
|
assert provider.calls == 1
|
|
# One user entry: nothing records the exchange twice now that no branch
|
|
# returns ahead of _save_turn.
|
|
history = loop.sessions.get_or_create("web:abc").messages
|
|
assert sum(1 for m in history if m.get("role") == "user") == 1
|
|
|
|
|
|
async def test_the_turn_message_reaches_the_tool_so_its_listing_can_be_ranked(tmp_path):
|
|
"""The description is rendered per turn and narrowed to what fits the request.
|
|
|
|
Without this hand-off the tool would rank against nothing, and a library too
|
|
large to list whole would show an arbitrary slice of itself on every turn.
|
|
"""
|
|
provider = _Provider()
|
|
loop = AgentLoop(provider=provider, workspace=tmp_path, model="fake/default")
|
|
_stub_edges(loop)
|
|
loop._playbooks = _StubPlaybooks()
|
|
|
|
seen: list[str] = []
|
|
|
|
class _Tool(Tool):
|
|
@property
|
|
def name(self) -> str:
|
|
return "load_playbook"
|
|
|
|
@property
|
|
def description(self) -> str:
|
|
return "stub"
|
|
|
|
@property
|
|
def parameters(self) -> dict:
|
|
return {"type": "object", "properties": {}}
|
|
|
|
async def execute(self, **kwargs) -> str:
|
|
return "not called"
|
|
|
|
def set_turn_message(self, message: str) -> None:
|
|
seen.append(message)
|
|
|
|
loop.tools.register(_Tool())
|
|
|
|
await loop._process_message(_req("sort out this week's user feedback", conversation="web:abc"))
|
|
|
|
assert seen == ["sort out this week's user feedback"]
|
|
|
|
|
|
async def test_every_origin_records_the_playbook_address(tmp_path):
|
|
"""A cron turn can call load_playbook too, and its graph announces somewhere.
|
|
|
|
The address comes from the per-turn tool-context pass rather than from a
|
|
user-only branch, so a cron-started run reports into its own conversation
|
|
instead of the last human one (or the cli:direct default on a cold process).
|
|
"""
|
|
provider = _Provider()
|
|
loop = AgentLoop(provider=provider, workspace=tmp_path, model="fake/default")
|
|
_stub_edges(loop)
|
|
stub = _StubPlaybooks()
|
|
loop._playbooks = stub
|
|
|
|
cron_req = TurnRequest(
|
|
origin=Origin.CRON,
|
|
source=Source(channel="slack", chat_id="#team", sender_id="cron", chat_type=ChatType.DM),
|
|
text="run the weekly briefing playbook",
|
|
conversation="cron:job-1",
|
|
)
|
|
await loop._process_message(cron_req, origin=Origin.CRON)
|
|
|
|
assert {"channel": "slack", "chat_id": "#team", "session_key": "cron:job-1"} in stub.contexts
|
|
|
|
|
|
def test_a_playbook_config_that_is_on_registers_both_entries_even_empty(tmp_path) -> None:
|
|
"""The one path production takes: construct the loop *with* a playbook config.
|
|
|
|
Every other test in the suite assembles the playbook runtime directly, which
|
|
is why an ordering bug in ``AgentLoop.__init__`` survived a full green run:
|
|
the build read ``self._mcp_servers`` several assignments before that field
|
|
existed, raised ``AttributeError`` into the guard that turns a build failure
|
|
into "feature disabled", and left ``playbooks.enabled: true`` doing nothing
|
|
but writing one warning line. Asserting on the registry is what makes that
|
|
reachable -- a runtime that fails to build registers no tools.
|
|
|
|
Both tools register whenever the feature is on, and the empty library is the
|
|
case that decides it. The loader used to wait for content, on the grounds
|
|
that a tool answering nothing but "unknown" should not be offered -- but
|
|
``create_playbook`` writes the first one *into this conversation*, and the
|
|
tool that loads it did not exist until the next process. Its description
|
|
says there is nothing installed when there is nothing installed, which costs
|
|
a sentence and keeps the pair reachable together.
|
|
|
|
The pair is bundled plugin cargo now (``raven/plugins/bundled/playbook``):
|
|
production discovery hands it in as plugin tools, so this test hands the
|
|
loop the same two tools unbound and asserts the binding leaves both
|
|
serving.
|
|
"""
|
|
from raven.agent.tools.create_playbook import CreatePlaybookTool
|
|
from raven.agent.tools.load_playbook import LoadPlaybookTool
|
|
from raven.config.schema import PlaybookConfig
|
|
|
|
loop = AgentLoop(
|
|
provider=_Provider(),
|
|
workspace=tmp_path,
|
|
model="fake/default",
|
|
policy=TurnPolicy(max_iterations=2),
|
|
engine=EngineWiring(playbook_config=PlaybookConfig(enabled=True)),
|
|
tools=ToolWiring(plugin_tools=[LoadPlaybookTool(), CreatePlaybookTool()]),
|
|
)
|
|
|
|
assert loop._playbooks is not None, "enabled: true must leave a runtime behind"
|
|
assert loop.tools.has("create_playbook")
|
|
assert loop.tools.has("load_playbook")
|
|
loader = loop.tools.get("load_playbook")
|
|
assert "No playbooks are installed" in loader.description
|
|
# The schema an empty library produces has to be one a provider will take and
|
|
# a call will survive. `"enum": None` is neither: it is not a JSON Schema, and
|
|
# `Tool._validate` tests `val not in schema["enum"]` on the key being present,
|
|
# so every call raised `argument of type 'NoneType' is not iterable` before it
|
|
# ran. Unreachable while the loader was withheld over an empty library; the
|
|
# fresh-install state once it is not.
|
|
name_schema = loader.to_schema()["function"]["parameters"]["properties"]["name"]
|
|
assert "enum" not in name_schema, name_schema
|
|
|
|
|
|
def test_the_playbook_runtime_dag_tool_is_wired_to_the_manager_sandbox(tmp_path):
|
|
"""The funnel's private graph tool is one of the sites that must hand over
|
|
the manager's sandbox builder. A stored graph's nodes exec too, and a site
|
|
that drops the keyword returns them to the host silently -- the shape of
|
|
the defect the builder seam was added for.
|
|
"""
|
|
from raven.config.schema import PlaybookConfig
|
|
from raven.playbook import PlaybookExecutor
|
|
|
|
captured = {}
|
|
orig_init = PlaybookExecutor.__init__
|
|
|
|
def recording_init(self, *args, **kwargs):
|
|
captured["dag_tool"] = kwargs.get("dag_tool")
|
|
return orig_init(self, *args, **kwargs)
|
|
|
|
import pytest
|
|
|
|
monkeypatch = pytest.MonkeyPatch()
|
|
monkeypatch.setattr(PlaybookExecutor, "__init__", recording_init)
|
|
try:
|
|
loop = AgentLoop(
|
|
provider=_Provider(),
|
|
workspace=tmp_path,
|
|
model="fake/default",
|
|
policy=TurnPolicy(max_iterations=2),
|
|
engine=EngineWiring(playbook_config=PlaybookConfig(enabled=True)),
|
|
)
|
|
finally:
|
|
monkeypatch.undo()
|
|
assert loop._playbooks is not None, "the runtime must build for its tool to be pinned"
|
|
assert captured["dag_tool"]._sandbox_for == loop.subagents.sandbox_executor_for
|
|
|
|
|
|
def test_a_user_playbook_in_the_library_registers_the_loader(tmp_path) -> None:
|
|
"""The flip side of the empty check: content is what gates the loader.
|
|
|
|
A hand-written directory under the user layer is a playbook -- no generator
|
|
involved -- and one file is enough to open the entry point.
|
|
"""
|
|
from raven.agent.tools.load_playbook import LoadPlaybookTool
|
|
from raven.config.schema import PlaybookConfig
|
|
|
|
target = tmp_path / "playbooks" / "weekly-feedback"
|
|
target.mkdir(parents=True)
|
|
(target / "playbook.md").write_text(
|
|
"---\nname: weekly-feedback\ndescription: weekly user-feedback analysis\n---\n\n"
|
|
"body\n\n"
|
|
"```yaml playbook-spec\n"
|
|
"version: 1\nmode: prompt\ntaskSummary: weekly user-feedback analysis for the team\nconfirm: true\n"
|
|
"triggers:\n keywords: [weekly feedback]\n"
|
|
"prompts: one research node\n"
|
|
"```\n",
|
|
encoding="utf-8",
|
|
)
|
|
|
|
loop = AgentLoop(
|
|
provider=_Provider(),
|
|
workspace=tmp_path,
|
|
model="fake/default",
|
|
policy=TurnPolicy(max_iterations=2),
|
|
engine=EngineWiring(playbook_config=PlaybookConfig(enabled=True)),
|
|
tools=ToolWiring(plugin_tools=[LoadPlaybookTool()]),
|
|
)
|
|
|
|
assert loop.tools.has("load_playbook")
|
|
|
|
|
|
def test_a_playbook_config_that_is_off_tells_the_model_nothing(tmp_path) -> None:
|
|
"""Off, neither entry exists -- the library is invisible rather than refused.
|
|
|
|
The pair still arrives (discovery does not read the feature flag), declines
|
|
its binding, and leaves the table.
|
|
"""
|
|
from raven.agent.tools.create_playbook import CreatePlaybookTool
|
|
from raven.agent.tools.load_playbook import LoadPlaybookTool
|
|
from raven.config.schema import PlaybookConfig
|
|
|
|
loop = AgentLoop(
|
|
provider=_Provider(),
|
|
workspace=tmp_path,
|
|
model="fake/default",
|
|
policy=TurnPolicy(max_iterations=2),
|
|
engine=EngineWiring(playbook_config=PlaybookConfig(enabled=False)),
|
|
tools=ToolWiring(plugin_tools=[LoadPlaybookTool(), CreatePlaybookTool()]),
|
|
)
|
|
|
|
assert loop._playbooks is None
|
|
assert not loop.tools.has("create_playbook")
|
|
assert not loop.tools.has("load_playbook")
|
|
|
|
|
|
def test_both_graph_tools_are_one_surface_to_a_consumer(tmp_path) -> None:
|
|
"""Who dispatched a run must not be observable from outside.
|
|
|
|
A ``mode: dag`` playbook is dispatched by the engine's own graph tool, which
|
|
is deliberately unregistered -- the model never calls it. Every consumer that
|
|
asks a graph tool something, though, has to get the same answer for that run
|
|
as for one the model composed: where its progress goes, whether it is live,
|
|
whether a cancel lands. Asking the registered instance alone answered "not
|
|
happening" to all three, which on the page meant a playbook's graph never
|
|
appeared in the conversation and its stop button reported nothing to stop.
|
|
"""
|
|
from raven.config.schema import PlaybookConfig
|
|
|
|
loop = AgentLoop(
|
|
provider=_Provider(),
|
|
workspace=tmp_path,
|
|
model="fake/default",
|
|
policy=TurnPolicy(max_iterations=2),
|
|
engine=EngineWiring(playbook_config=PlaybookConfig(enabled=True)),
|
|
)
|
|
|
|
tools = loop.dag_tools()
|
|
assert len(tools) == 2, "the registered instance and the engine's private one"
|
|
assert tools[0] is loop.tools.get("run_subagent_dag")
|
|
assert tools[1] is not tools[0], "the private one is a second instance, not the same object"
|
|
|
|
sink = object()
|
|
loop.set_dag_progress_sink(sink)
|
|
assert [t._sink for t in tools] == [sink, sink], "progress reaches the page from either"
|
|
|
|
# Cancel consults both. Neither owns this id, so the honest answer is False --
|
|
# what matters is that the private one was asked at all.
|
|
assert loop.cancel_dag_run("nope") is False
|
|
tools[1]._cancels["run-x"] = _Flag()
|
|
assert loop.cancel_dag_run("run-x") is True, "a run only the engine owns is still cancellable"
|
|
|
|
|
|
def test_resolve_dag_node_reaches_a_node_the_playbook_engine_owns(tmp_path) -> None:
|
|
"""A suspended node's desk lives on whichever tool dispatched its run.
|
|
|
|
A mode: dag playbook's run is dispatched by the engine's private tool, so
|
|
the desk holding its open nodes lives there too, never on the registered
|
|
instance. resolve_dag_node has to consult both the way cancel_dag_run
|
|
already does, or the model would be told a node is done waiting while
|
|
dag_status -- which reads liveness through the same dag_tools() union --
|
|
still shows it open.
|
|
"""
|
|
from raven.agent.subagent.dag_adjudication import AdjudicationDesk
|
|
from raven.config.schema import PlaybookConfig
|
|
|
|
loop = AgentLoop(
|
|
provider=_Provider(),
|
|
workspace=tmp_path,
|
|
model="fake/default",
|
|
policy=TurnPolicy(max_iterations=2),
|
|
engine=EngineWiring(playbook_config=PlaybookConfig(enabled=True)),
|
|
)
|
|
registered, private = loop.dag_tools()
|
|
|
|
desk = AdjudicationDesk()
|
|
desk.open("a")
|
|
private._desks["run-x"] = desk
|
|
|
|
assert loop.resolve_dag_node("run-x", "missing", "abandon", None) is False
|
|
assert loop.resolve_dag_node("run-x", "a", "continue", "use staging") is True
|
|
|
|
|
|
class _Flag:
|
|
def __init__(self) -> None:
|
|
self.set_called = False
|
|
|
|
def set(self) -> None:
|
|
self.set_called = True
|
|
|
|
|
|
def test_liveness_is_the_union_across_graph_tools(tmp_path) -> None:
|
|
"""A run either tool owns is in flight; a broken tool is not fatal.
|
|
|
|
Advisory by design: what this answer decides is whether a row is drawn as
|
|
running, and a graph tool that cannot say must not turn that into an error
|
|
the panel shows instead of a list.
|
|
"""
|
|
from raven.config.schema import PlaybookConfig
|
|
|
|
loop = AgentLoop(
|
|
provider=_Provider(),
|
|
workspace=tmp_path,
|
|
model="fake/default",
|
|
policy=TurnPolicy(max_iterations=2),
|
|
engine=EngineWiring(playbook_config=PlaybookConfig(enabled=True)),
|
|
)
|
|
registered, private = loop.dag_tools()
|
|
|
|
registered._cancels["from-the-model"] = _Flag()
|
|
private._cancels["from-a-playbook"] = _Flag()
|
|
assert loop.active_dag_run_ids() == {"from-the-model", "from-a-playbook"}
|
|
|
|
class _Broken:
|
|
def active_run_ids(self):
|
|
raise RuntimeError("gone")
|
|
|
|
loop.dag_tools = lambda: [_Broken(), private]
|
|
assert loop.active_dag_run_ids() == {"from-a-playbook"}, "one broken tool must not hide the other"
|
|
|
|
|
|
def test_a_runtime_that_cannot_build_leaves_the_feature_off(tmp_path, monkeypatch) -> None:
|
|
"""A library that fails to load is not a reason to refuse every turn.
|
|
|
|
This guard is also what hid the ordering bug for a release: it turned an
|
|
AttributeError into a warning line and an agent that quietly had no
|
|
playbooks. Kept, because the alternative is a broken library taking down the
|
|
loop -- but now asserted, so "off" is a tested outcome rather than a
|
|
side effect nobody looked at.
|
|
"""
|
|
from raven.config.schema import PlaybookConfig
|
|
|
|
def _boom(self, cfg):
|
|
raise RuntimeError("library is a rock")
|
|
|
|
from raven.agent.tools.create_playbook import CreatePlaybookTool
|
|
from raven.agent.tools.load_playbook import LoadPlaybookTool
|
|
|
|
monkeypatch.setattr(AgentLoop, "_build_playbook_runtime", _boom)
|
|
loop = AgentLoop(
|
|
provider=_Provider(),
|
|
workspace=tmp_path,
|
|
model="fake/default",
|
|
policy=TurnPolicy(max_iterations=2),
|
|
engine=EngineWiring(playbook_config=PlaybookConfig(enabled=True)),
|
|
tools=ToolWiring(plugin_tools=[LoadPlaybookTool(), CreatePlaybookTool()]),
|
|
)
|
|
|
|
assert loop._playbooks is None
|
|
assert not loop.tools.has("load_playbook") and not loop.tools.has("create_playbook")
|
|
assert loop.dag_tools() == [loop.tools.get("run_subagent_dag")], "only the registered one is left"
|
|
|
|
|
|
def test_both_graph_tools_are_handed_the_answer_route_predicate(tmp_path):
|
|
"""The playbook lane builds its own SubAgentDagTool, and it can suspend too.
|
|
|
|
Its comment claims the same manager hooks as the registered tool, and the
|
|
hooks that let a node suspend are among them: a verdict provider, an
|
|
exception announcer, and a background executor. What it lacked was the
|
|
predicate saying whether an answer can come back, so `_route_available(None)`
|
|
read the host's known-unreachable route as open and the node waited out its
|
|
whole adjudication window -- the failure this branch exists to remove, alive
|
|
on the second of the two construction sites.
|
|
|
|
Asserted over `dag_tools()` rather than the registered instance alone,
|
|
because naming one site is what let the other drift.
|
|
"""
|
|
from raven.config.schema import PlaybookConfig
|
|
|
|
loop = AgentLoop(
|
|
provider=_Provider(),
|
|
workspace=tmp_path,
|
|
model="fake/default",
|
|
policy=TurnPolicy(max_iterations=2),
|
|
engine=EngineWiring(playbook_config=PlaybookConfig(enabled=True)),
|
|
)
|
|
|
|
assert loop._playbooks is not None, "the second construction site has to exist for this to test anything"
|
|
tools = loop.dag_tools()
|
|
assert len(tools) == 2, f"one registered, one private to the playbook engine; got {len(tools)}"
|
|
|
|
unwired = [t for t in tools if getattr(t, "_control_reachable", None) is None]
|
|
assert unwired == [], "every graph tool that can suspend a node needs the predicate, not just the registered one"
|
|
for t in tools:
|
|
assert t._control_reachable() == loop.dag_control_reachable(), "and it must be the host's own answer"
|