1ac32df817
Six SDK function tools that drive the AgentMessageBus from Phase 0, replacing the legacy _agent_graph / _agent_messages / _agent_instances globals: - view_agent_graph: render parent/child tree from bus.parent_of with a per-status summary (running / waiting / completed / crashed / stopped). - agent_status: per-agent lifecycle + pending-message count snapshot. - send_message_to_agent: queue into bus.inboxes; rejects sends to finalized targets so the model gets feedback rather than a silent drop (the bus's own send method drops to support the C13 cleanup, but the tool surfaces it as a structured error). - wait_for_message: poll inbox once per second up to timeout. Polling rather than asyncio.Event because a missed wakeup on Event would be hard to debug; the bus already serializes through its own lock. - create_agent: spawn a child via asyncio.create_task(Runner.run(...)). Pulls an agent_factory callable from ctx.context (the Phase 5 root assembly is the one that wires it in). Registers the child with the bus before the task starts, stores the task handle in bus.tasks so cancel_descendants can cascade (C9), builds the child's identity block + optional inherited parent context, and runs the child with StrixOrchestrationHooks. - agent_finish: subagent-only termination. Flips agent_finish_called so the on_agent_end hook records "completed" instead of "crashed" (C8), and posts a structured <agent_completion_report> XML envelope to the parent's inbox. run_config_factory.make_agent_context grows two fields: sandbox_client (reused across child runs) and agent_factory (Phase 3 needs it; Phase 5 fills it in). PLC0415 fixed by hoisting the openai.types.shared.Reasoning import to module-level. Tests: 17 new tests in test_sdk_graph_tools.py — registration, all six tools' happy and error paths, real AgentMessageBus integration so the tools exercise production code paths, create_agent verified for spawn shape (task created, bus registered, identity block in input) plus a bus.cancel_descendants integration check. Refs: PLAYBOOK.md §4.3, AUDIT_R2 §1.4 (cancel_descendants), AUDIT_R3 C8. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
216 lines
8.2 KiB
Python
216 lines
8.2 KiB
Python
"""make_run_config — assemble a Strix-flavored ``RunConfig`` for ``Runner.run``.
|
|
|
|
Factory pattern: every Strix scan goes through here so the defaults are
|
|
applied uniformly. Per-call overrides are accepted via ``model_settings_override``
|
|
for the rare case a single run wants different reasoning effort or
|
|
``tool_choice`` (C21).
|
|
|
|
References:
|
|
- PLAYBOOK.md §2.10
|
|
- AUDIT.md §2.1 (C1 — parallel_tool_calls=False until Phase 6 relaxes the
|
|
legacy tool server's per-agent task slot serialization)
|
|
- AUDIT_R2.md §1.6 (C11 — retry policy explicitly excludes 401/403/400;
|
|
auth and validation errors must fail fast, not waste retries)
|
|
- AUDIT_R3.md C21 — RunConfig override + context fields including
|
|
``is_whitebox``, ``diff_scope``, ``run_id``
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING, Any, Literal
|
|
|
|
from agents import RunConfig
|
|
from agents.model_settings import ModelSettings
|
|
from agents.retry import (
|
|
ModelRetryBackoffSettings,
|
|
ModelRetrySettings,
|
|
retry_policies,
|
|
)
|
|
from agents.sandbox import SandboxRunConfig
|
|
from openai.types.shared import Reasoning
|
|
|
|
from strix.llm.multi_provider_setup import build_multi_provider
|
|
from strix.orchestration.filter import inject_messages_filter
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
from agents.sandbox.session.base_sandbox_session import BaseSandboxSession
|
|
|
|
from strix.orchestration.bus import AgentMessageBus
|
|
|
|
|
|
# Phase 1-5 default. Phase 6 relaxes the legacy tool server's per-agent
|
|
# task-slot serialization (``runtime/tool_server.py:94-97``) and flips this
|
|
# to ``True`` after the multi-agent stress tests confirm safety.
|
|
_PHASE1_PARALLEL_DEFAULT = False
|
|
|
|
# Default retry policy. Explicitly does NOT include 401/403/400 — those are
|
|
# auth and validation errors that retrying cannot fix; they should fail fast
|
|
# so the user sees the real error within seconds. 429/5xx is the right set.
|
|
_RETRYABLE_HTTP_STATUSES = (429, 500, 502, 503, 504)
|
|
|
|
# Default retry budget. Mirrors the legacy ``llm.py`` retry loop: 5 attempts
|
|
# with ``min(90, 2*2^n)`` backoff.
|
|
_DEFAULT_MAX_RETRIES = 5
|
|
_DEFAULT_BACKOFF = ModelRetryBackoffSettings(
|
|
initial_delay=2.0,
|
|
max_delay=90.0,
|
|
multiplier=2.0,
|
|
jitter=False,
|
|
)
|
|
|
|
|
|
def _default_retry_policy() -> Any:
|
|
"""Build the default retry policy.
|
|
|
|
Built from ``retry_policies.any(...)``: any of the listed conditions
|
|
triggers a retry. ``provider_suggested`` honors server-sent
|
|
``Retry-After`` hints; ``network_error`` covers connection / timeout;
|
|
``http_status`` whitelists transient HTTP codes.
|
|
"""
|
|
return retry_policies.any(
|
|
retry_policies.provider_suggested(),
|
|
retry_policies.network_error(),
|
|
retry_policies.http_status(_RETRYABLE_HTTP_STATUSES),
|
|
)
|
|
|
|
|
|
#: Default ``max_turns`` callers should pass to ``Runner.run``.
|
|
#: Mirrors the legacy ``AgentState.max_iterations = 300``
|
|
#: (``HARNESS_WIKI.md §5.2``).
|
|
STRIX_DEFAULT_MAX_TURNS = 300
|
|
|
|
|
|
def make_run_config(
|
|
*,
|
|
sandbox_session: BaseSandboxSession | None,
|
|
model: str = "strix/claude-sonnet-4.6",
|
|
parallel_tool_calls: bool = _PHASE1_PARALLEL_DEFAULT,
|
|
tool_choice: Literal["auto", "required", "none"] | None = "required",
|
|
reasoning_effort: Literal["low", "medium", "high"] | None = None,
|
|
model_settings_override: ModelSettings | None = None,
|
|
sandbox_client: Any | None = None,
|
|
) -> RunConfig:
|
|
"""Build a ``RunConfig`` with Strix defaults.
|
|
|
|
Note: ``max_turns`` and ``isolate_parallel_failures`` are NOT
|
|
``RunConfig`` fields — they are passed directly to ``Runner.run``.
|
|
Use ``STRIX_DEFAULT_MAX_TURNS`` for the budget; pass
|
|
``isolate_parallel_failures=False`` to ``Runner.run`` if Phase 6 has
|
|
not yet flipped ``parallel_tool_calls=True``.
|
|
|
|
Args:
|
|
sandbox_session: Live sandbox session shared by every agent in this
|
|
scan (one container per scan; see ``strix.sandbox.session_manager``).
|
|
``None`` is allowed for unit tests and dry runs.
|
|
model: Model alias to pass to ``MultiProvider``. Defaults to the
|
|
current production-favored Anthropic alias.
|
|
parallel_tool_calls: Phase 1 default is ``False`` to keep behavior
|
|
sequential per the legacy tool server's slot serialization (C1).
|
|
tool_choice: Forces tool use per turn unless explicitly relaxed.
|
|
Mirrors the legacy ``4f90a56`` prompt hardening at the model
|
|
level. Pass ``None`` to omit.
|
|
reasoning_effort: ``"low" | "medium" | "high"``; routes to
|
|
``ModelSettings.reasoning``. ``None`` defers to provider default.
|
|
model_settings_override: Optional ``ModelSettings`` to merge over
|
|
the factory defaults (C21 — per-run override path).
|
|
sandbox_client: Optional pre-built sandbox client (e.g., the Strix
|
|
Docker subclass). Defaults to ``None``; the SDK will instantiate
|
|
its built-in if a session is supplied without a client.
|
|
|
|
Returns:
|
|
A ``RunConfig`` ready to pass to ``Runner.run``.
|
|
"""
|
|
base_settings = ModelSettings(
|
|
parallel_tool_calls=parallel_tool_calls,
|
|
tool_choice=tool_choice,
|
|
retry=ModelRetrySettings(
|
|
max_retries=_DEFAULT_MAX_RETRIES,
|
|
backoff=_DEFAULT_BACKOFF,
|
|
policy=_default_retry_policy(),
|
|
),
|
|
)
|
|
if reasoning_effort is not None:
|
|
base_settings = base_settings.resolve(
|
|
ModelSettings(reasoning=Reasoning(effort=reasoning_effort)),
|
|
)
|
|
if model_settings_override is not None:
|
|
# ``ModelSettings.resolve`` merges another ModelSettings into self
|
|
# with override-wins semantics — exactly what we want.
|
|
base_settings = base_settings.resolve(model_settings_override)
|
|
|
|
sandbox_config = (
|
|
SandboxRunConfig(client=sandbox_client, session=sandbox_session)
|
|
if sandbox_session is not None
|
|
else None
|
|
)
|
|
|
|
return RunConfig(
|
|
model=model,
|
|
model_provider=build_multi_provider(),
|
|
model_settings=base_settings,
|
|
sandbox=sandbox_config,
|
|
call_model_input_filter=inject_messages_filter,
|
|
tracing_disabled=False,
|
|
trace_include_sensitive_data=False,
|
|
)
|
|
|
|
|
|
def make_agent_context(
|
|
*,
|
|
bus: AgentMessageBus,
|
|
sandbox_session: BaseSandboxSession | None,
|
|
sandbox_token: str | None,
|
|
tool_server_host_port: int | None,
|
|
caido_host_port: int | None,
|
|
agent_id: str,
|
|
agent_name: str,
|
|
parent_id: str | None,
|
|
tracer: Any | None,
|
|
model: str = "strix/claude-sonnet-4.6",
|
|
model_settings: ModelSettings | None = None,
|
|
max_turns: int = 300,
|
|
is_whitebox: bool = False,
|
|
diff_scope: dict[str, Any] | None = None,
|
|
run_id: str | None = None,
|
|
sandbox_client: Any | None = None,
|
|
agent_factory: Any | None = None,
|
|
) -> dict[str, Any]:
|
|
"""Build the per-agent ``context`` dict passed to ``Runner.run(context=...)``.
|
|
|
|
The dict is the canonical place where bus, sandbox handles, identity,
|
|
tracer reference, and per-agent toggles live. Tools, hooks, and the
|
|
``inject_messages_filter`` all reach in via ``ctx.context.get(...)``.
|
|
|
|
C21 (AUDIT_R3): includes ``is_whitebox``, ``diff_scope`` and ``run_id``
|
|
fields that the legacy code relied on but the original PLAYBOOK §2.10
|
|
skeleton omitted.
|
|
|
|
``agent_factory`` is a callable ``(name, skills) -> agents.Agent`` used by
|
|
the ``create_agent`` graph tool to spin up children. The actual factory
|
|
lives in the Phase 4/5 root-assembly module; Phase 3 only requires that
|
|
it be present in context. ``sandbox_client`` is the host-side Docker
|
|
subclass; ``create_agent`` reuses it across child runs.
|
|
"""
|
|
return {
|
|
"bus": bus,
|
|
"sandbox_session": sandbox_session,
|
|
"sandbox_client": sandbox_client,
|
|
"sandbox_token": sandbox_token,
|
|
"tool_server_host_port": tool_server_host_port,
|
|
"caido_host_port": caido_host_port,
|
|
"agent_id": agent_id,
|
|
"agent_name": agent_name,
|
|
"parent_id": parent_id,
|
|
"tracer": tracer,
|
|
"model": model,
|
|
"model_settings": model_settings,
|
|
"max_turns": max_turns,
|
|
"turn_count": 0,
|
|
"agent_finish_called": False,
|
|
"is_whitebox": is_whitebox,
|
|
"diff_scope": diff_scope,
|
|
"run_id": run_id,
|
|
"agent_factory": agent_factory,
|
|
}
|