feat(migration): phase 5 — root agent factory + entry point

Three new modules that wire Phases 0-4 into a runnable Strix scan:

- strix/agents/sdk_prompt.py: standalone Jinja-based system prompt
  renderer. Reuses the existing strix/agents/StrixAgent/system_prompt.
  jinja template (508 lines, the actual production prompt) so behavior
  parity with the legacy LLM._load_system_prompt is byte-identical.
  Skill resolution mirrors LLM._get_skills_to_load (caller skills →
  scan_modes/<mode> → whitebox pair, deduped). Fail-soft: template
  errors return empty string and log; agent construction must never
  blow up on prompt load.

- strix/agents/sdk_factory.py: build_strix_agent(name, skills, is_root)
  assembles an agents.Agent. Root carries finish_scan and stops there;
  child carries agent_finish and stops there (C4). Caido tools come
  from CaidoCapability automatically — we don't include them in
  _BASE_TOOLS to avoid double-registration when the SDK runtime merges
  capability tools. model=None so RunConfig drives the model alias
  through MultiProvider rather than the SDK default. make_child_factory
  returns a closure over scan-level config (scan_mode, is_whitebox,
  interactive, scope context) for ctx.context['agent_factory'] — the
  Phase 3 create_agent tool calls it with (name, skills) per child.

- strix/sdk_entry.py: run_strix_scan() — the top-level coroutine.
  Builds the bus, brings up (or reuses) a sandbox session via
  session_manager, builds the root Agent and the child factory, builds
  the per-agent context dict, registers the root in the bus, builds
  the RunConfig, calls Runner.run, and cleans up the session in a
  finally. Cancels descendants before re-raising any exception (C9).
  cleanup_on_exit toggle preserves the cached session for resume
  scenarios. _build_root_task and _build_scope_context preserve the
  legacy StrixAgent.execute_scan task formatting + scope context shape
  so the prompt template sees identical inputs.

Tests: 21 new tests (10 for factory + prompt, 11 for entry point).
Factory: root vs child tool list parity, finish_scan/agent_finish
placement, tool_use_behavior dict shape, Caido absence (capability-
provided), make_child_factory closure semantics. Entry point (all
mocked, no real Docker/LLM): wiring shape verification — context dict
carries every field downstream consumers read, session manager called
with correct scan_id, cleanup runs even on Runner.run failure,
cleanup skipped when disabled, scan_id auto-generation, scan-level
config (scan_mode, is_whitebox) flows into the factory. Task and scope
builders verified against the same shape as legacy.

Per-file ruff ignores added: TC002 on sdk_factory (Tool used at
runtime in _BASE_TOOLS tuple), TC003 + PLR0912 on sdk_entry (Path
runtime-imported; _build_root_task's per-target-type branches are
intentional and well-bounded).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
0xallam
2026-04-25 00:58:32 -07:00
parent 1d86e4506a
commit f0e254c1fd
6 changed files with 1162 additions and 0 deletions
+201
View File
@@ -0,0 +1,201 @@
"""Phase 5 tests for the SDK agent factory + prompt renderer.
These two modules are the keystone wiring between Phases 2-4 and an
actual ``Runner.run`` invocation. The tests verify:
- The prompt renderer reuses the existing Jinja template (parity with
legacy LLM._load_system_prompt) and degrades gracefully when the
template isn't available.
- ``build_strix_agent(is_root=True)`` carries ``finish_scan`` and
stops on it; child agents carry ``agent_finish`` and stop on it.
- ``make_child_factory`` snapshots scan-level config into a closure
so each spawned child inherits the right scan_mode / is_whitebox /
prompt context without create_agent having to re-derive it.
"""
from __future__ import annotations
from typing import Any
from unittest.mock import patch
from agents import Agent
from agents.tool import FunctionTool
from strix.agents.sdk_factory import build_strix_agent, make_child_factory
from strix.agents.sdk_prompt import _resolve_skills, render_system_prompt
# --- prompt renderer ----------------------------------------------------
def test_resolve_skills_deduplicates_and_orders() -> None:
out = _resolve_skills(
requested=["recon", "xss", "recon"],
scan_mode="deep",
is_whitebox=False,
)
assert out == ["recon", "xss", "scan_modes/deep"]
def test_resolve_skills_adds_whitebox_pair() -> None:
out = _resolve_skills(requested=None, scan_mode="fast", is_whitebox=True)
# The whitebox pair sits at the tail; scan_modes goes in the middle
# because callers can append more skills after it via the requested arg.
assert out == [
"scan_modes/fast",
"coordination/source_aware_whitebox",
"custom/source_aware_sast",
]
def test_render_system_prompt_returns_string() -> None:
"""Smoke: the StrixAgent template is on disk and renders to non-empty."""
out = render_system_prompt(skills=[], scan_mode="deep")
assert isinstance(out, str)
# The first line of the template starts with 'You are Strix'.
assert out.startswith("You are Strix")
def test_render_system_prompt_swallows_template_errors() -> None:
"""If the template path can't be resolved, return an empty string
(not raise) — agent construction must never blow up on prompt load."""
with patch(
"strix.agents.sdk_prompt.get_strix_resource_path",
side_effect=RuntimeError("missing"),
):
out = render_system_prompt(skills=[])
assert out == ""
# --- factory: shape + tools --------------------------------------------
def test_root_agent_carries_finish_scan_and_stops_there() -> None:
agent = build_strix_agent(name="strix", is_root=True)
assert isinstance(agent, Agent)
tool_names = {t.name for t in agent.tools if isinstance(t, FunctionTool)}
assert "finish_scan" in tool_names
assert "agent_finish" not in tool_names
behavior = agent.tool_use_behavior
# StopAtTools is a TypedDict at runtime → behavior is a dict.
assert isinstance(behavior, dict)
assert behavior["stop_at_tool_names"] == ["finish_scan"]
def test_child_agent_carries_agent_finish_and_stops_there() -> None:
agent = build_strix_agent(name="recon-bot", is_root=False)
tool_names = {t.name for t in agent.tools if isinstance(t, FunctionTool)}
assert "agent_finish" in tool_names
assert "finish_scan" not in tool_names
behavior = agent.tool_use_behavior
assert isinstance(behavior, dict)
assert behavior["stop_at_tool_names"] == ["agent_finish"]
def test_root_and_child_share_base_tool_set() -> None:
"""The base tool set (think/todo/notes/file_edit/web_search/etc) is
identical between root and child — only the terminator differs."""
root = build_strix_agent(is_root=True)
child = build_strix_agent(is_root=False)
root_names = {t.name for t in root.tools if isinstance(t, FunctionTool)}
child_names = {t.name for t in child.tools if isinstance(t, FunctionTool)}
# Drop the terminators and compare.
assert root_names - {"finish_scan"} == child_names - {"agent_finish"}
def test_agent_includes_graph_and_sandbox_tools() -> None:
"""The graph + sandbox tool families are required for parity with
legacy. Spot-check the ones most likely to be forgotten in a refactor."""
agent = build_strix_agent(is_root=True)
names = {t.name for t in agent.tools if isinstance(t, FunctionTool)}
expected = {
"think",
"create_todo",
"create_note",
"web_search",
"str_replace_editor",
"create_vulnerability_report",
"load_skill",
"browser_action",
"terminal_execute",
"python_action",
"view_agent_graph",
"agent_status",
"send_message_to_agent",
"wait_for_message",
"create_agent",
}
missing = expected - names
assert not missing, f"missing tools: {missing}"
def test_agent_does_not_include_caido_tools() -> None:
"""Caido tools come from CaidoCapability.tools(); the agent doesn't
declare them directly to avoid double-registration when the SDK
runtime merges capability tools."""
agent = build_strix_agent(is_root=True)
names = {t.name for t in agent.tools if isinstance(t, FunctionTool)}
caido = {
"list_requests",
"view_request",
"send_request",
"repeat_request",
"scope_rules",
"list_sitemap",
"view_sitemap_entry",
}
overlap = names & caido
assert overlap == set(), f"unexpected Caido tools in agent.tools: {overlap}"
def test_agent_uses_run_config_model() -> None:
"""``model=None`` so the RunConfig drives the model alias through
MultiProvider rather than an SDK default like gpt-4.1."""
agent = build_strix_agent(is_root=True)
assert agent.model is None
def test_agent_instructions_contain_rendered_prompt() -> None:
"""The factory must wire the rendered prompt into ``instructions``."""
agent = build_strix_agent(is_root=True, scan_mode="deep")
assert isinstance(agent.instructions, str)
assert agent.instructions.startswith("You are Strix")
# --- child factory ------------------------------------------------------
def test_make_child_factory_returns_callable_that_builds_child() -> None:
factory = make_child_factory(scan_mode="deep", is_whitebox=False)
assert callable(factory)
child = factory(name="sub-1", skills=["recon"])
assert isinstance(child, Agent)
assert child.name == "sub-1"
behavior = child.tool_use_behavior
assert isinstance(behavior, dict)
assert behavior["stop_at_tool_names"] == ["agent_finish"]
def test_make_child_factory_passes_scan_level_config() -> None:
"""Verify scan_mode + is_whitebox flow into the rendered prompt
via the closure rather than the create_agent call site."""
captured: dict[str, Any] = {}
def fake_render(**kwargs: Any) -> str:
captured.update(kwargs)
return "stub-prompt"
factory = make_child_factory(
scan_mode="fast",
is_whitebox=True,
interactive=True,
system_prompt_context={"scope_source": "test"},
)
with patch("strix.agents.sdk_factory.render_system_prompt", side_effect=fake_render):
factory(name="child", skills=["xss"])
assert captured["scan_mode"] == "fast"
assert captured["is_whitebox"] is True
assert captured["interactive"] is True
assert captured["system_prompt_context"] == {"scope_source": "test"}
assert captured["skills"] == ["xss"]