feat(migration): phase 1 — Session + Tracer + RunConfig factory
Three foundation modules per PLAYBOOK §2.8 / §2.9 / §2.10 with all
relevant R2/R3 corrections (C7, C10, C11, C16, C21):
strix/llm/strix_session.py SessionABC wrapper around the
legacy MemoryCompressor; on any
compression failure, returns
uncompressed history and
permanently disables compression
for the rest of the run (C10 +
Round 3.4 W5/E2).
strix/telemetry/strix_processor.py SDK TracingProcessor that writes
events.jsonl in our schema. All
hooks SYNC per ABC (F3); writes
protected by per-path
threading.Lock (C7); OSError
swallowed and logged (C16); PII
scrubbed via the existing
TelemetrySanitizer.
strix/run_config_factory.py make_run_config() with our
defaults: parallel_tool_calls=
False (C1 Phase-1 safe default),
retry policy explicitly excludes
401/403/400 (C11), reasoning
effort + model_settings_override
merge path (C21).
make_agent_context() returns the
canonical per-agent dict
including is_whitebox/diff_scope/
run_id (C21).
32 new smoke tests (197/197 total). mypy strict + ruff clean. Per-file
ignores added for tests/** S105/PT018 and for the two new src modules'
intentional broad-Exception catches (BLE001).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,206 @@
|
||||
"""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 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:
|
||||
from openai.types.shared import Reasoning
|
||||
|
||||
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,
|
||||
) -> 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.
|
||||
"""
|
||||
return {
|
||||
"bus": bus,
|
||||
"sandbox_session": sandbox_session,
|
||||
"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,
|
||||
}
|
||||
Reference in New Issue
Block a user