mirror of
https://github.com/usestrix/strix.git
synced 2026-08-18 17:52:32 +02:00
Phase 2.1 — sandbox dispatch helper:
- strix/tools/_sandbox_dispatch.py: post_to_sandbox() centralizes the
host->container HTTP wire format. Connect=10s, read=150s timeouts mirror
legacy executor.py. 50 MB response cap (C18) prevents OOM from a runaway
tool. All errors surface as {"error": str} so the model can recover
instead of the run dying.
Phase 2.2 — C6 lock-protected JSONL writes:
- strix/tools/notes/notes_actions.py: notes.jsonl appends are now wrapped
in _notes_lock so concurrent agents can't interleave half-written lines.
Regression test in test_notes_jsonl_concurrency.py verifies 1000 parallel
writes produce exactly 1000 valid JSON lines.
Phase 2.3 — thin-slice SDK wrappers (think + todo + notes):
- strix/tools/_legacy_adapter.py: LegacyAgentStateAdapter shim — exposes
just enough surface (.agent_id) for legacy tools that close over
agent_state, sourced from ctx.context['agent_id'].
- strix/tools/thinking/thinking_sdk_tools.py: 1 tool (think).
- strix/tools/todo/todo_sdk_tools.py: 6 tools (create/list/update/done/
pending/delete) with bulk-form preserved.
- strix/tools/notes/notes_sdk_tools.py: 5 tools (create/list/get/update/
delete) with asyncio.to_thread around the lock-protected file I/O.
Tests: 22 new tests pass (10 sandbox dispatch + 2 concurrency + 10 SDK
local). Full suite still green.
Per-file ruff ignores added for SDK wrapper files: TC002 (RunContextWrapper
must be runtime-importable because the SDK calls get_type_hints() to
derive the JSON schema) and PLR0911 (sandbox dispatch's 10 short-circuit
returns are intentional, each a distinct documented failure mode).
Refs: PLAYBOOK.md §3.4, AUDIT_R3.md C6/C18.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
133 lines
4.6 KiB
Python
133 lines
4.6 KiB
Python
"""post_to_sandbox — host-to-container HTTP transport for sandbox tools.
|
|
|
|
Every Strix tool that runs inside the Kali container (browser, terminal,
|
|
python, the seven Caido tools) has the same wire shape: POST a JSON body
|
|
to ``http://localhost:{tool_server_host_port}/execute`` with a Bearer
|
|
token header and ``{"agent_id", "tool_name", "kwargs"}`` as the body.
|
|
|
|
This helper centralizes that transport so:
|
|
|
|
- Every sandbox tool gets the same timeout policy
|
|
(``connect=10s`` / ``read=150s``).
|
|
- Every sandbox tool inherits the same response-size cap (50 MB) so a
|
|
runaway tool body cannot OOM the host (C18).
|
|
- Auth/transport errors surface as predictable error strings instead of
|
|
exceptions, so the model can retry / pick a different tool without the
|
|
run dying.
|
|
|
|
References:
|
|
- PLAYBOOK.md §3.4
|
|
- AUDIT_R3.md C18 (sandbox response size cap)
|
|
- HARNESS_WIKI.md §7.2 (legacy executor.py wire format we mirror)
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
import httpx
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
from agents import RunContextWrapper
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
# Connect: how long to wait for the TCP handshake to complete.
|
|
# Read: how long the tool may spend executing before we abandon the call.
|
|
# Mirrors the legacy executor.py (``SANDBOX_EXECUTION_TIMEOUT = 120 + 30``).
|
|
_SANDBOX_TIMEOUT = httpx.Timeout(connect=10.0, read=150.0, write=150.0, pool=150.0)
|
|
|
|
#: Cap on response body size from the tool server. Anything bigger is
|
|
#: replaced by an error string so the model sees something coherent and
|
|
#: the host doesn't OOM trying to allocate the buffer (C18).
|
|
_MAX_RESPONSE_BYTES = 50 * 1024 * 1024 # 50 MB
|
|
|
|
|
|
def _ctx_dict(ctx: RunContextWrapper) -> dict[str, Any] | None:
|
|
"""Return ``ctx.context`` if it's a dict, else ``None``.
|
|
|
|
Strix's runtime always passes a dict (``make_agent_context``); other
|
|
callers might not. Be defensive so a sandbox tool never raises just
|
|
because the context shape is wrong.
|
|
"""
|
|
inner = getattr(ctx, "context", None)
|
|
return inner if isinstance(inner, dict) else None
|
|
|
|
|
|
async def post_to_sandbox(
|
|
ctx: RunContextWrapper,
|
|
tool_name: str,
|
|
kwargs: dict[str, Any],
|
|
) -> dict[str, Any]:
|
|
"""POST a tool invocation to the in-container FastAPI tool server.
|
|
|
|
Returns:
|
|
On success: ``{"result": <whatever the tool returned>}``.
|
|
On any failure: ``{"error": "<human-readable error string>"}``.
|
|
|
|
Never raises. Tool authors call this and pass the return value
|
|
straight to the model (or extract ``result`` for further shaping).
|
|
"""
|
|
inner = _ctx_dict(ctx)
|
|
if inner is None:
|
|
return {"error": "Sandbox not initialized: context is missing or not a dict."}
|
|
|
|
port = inner.get("tool_server_host_port")
|
|
token = inner.get("sandbox_token")
|
|
agent_id = inner.get("agent_id", "unknown")
|
|
|
|
if not port or not token:
|
|
return {"error": "Sandbox not initialized: tool server port or token missing."}
|
|
|
|
url = f"http://127.0.0.1:{port}/execute"
|
|
headers = {
|
|
"Authorization": f"Bearer {token}",
|
|
"Content-Type": "application/json",
|
|
}
|
|
body = {"agent_id": agent_id, "tool_name": tool_name, "kwargs": kwargs}
|
|
|
|
try:
|
|
async with httpx.AsyncClient(timeout=_SANDBOX_TIMEOUT) as client:
|
|
response = await client.post(url, json=body, headers=headers)
|
|
except httpx.TimeoutException:
|
|
return {
|
|
"error": (f"Sandbox tool '{tool_name}' timed out after {_SANDBOX_TIMEOUT.read}s."),
|
|
}
|
|
except httpx.RequestError as e:
|
|
# ConnectError, ReadError, NetworkError, etc.
|
|
return {"error": f"Sandbox connection failed: {e!s}"[:300]}
|
|
|
|
if response.status_code == 401:
|
|
return {"error": "Sandbox authorization failed (Bearer token invalid)."}
|
|
if response.status_code >= 400:
|
|
return {
|
|
"error": (
|
|
f"Sandbox tool '{tool_name}' failed with HTTP "
|
|
f"{response.status_code}: {response.text[:300]}"
|
|
),
|
|
}
|
|
|
|
# Cap response size before parsing so a 1 GB rogue payload never lands
|
|
# in our heap. Most legitimate tool responses are well under 100 KB.
|
|
raw = response.content
|
|
if len(raw) > _MAX_RESPONSE_BYTES:
|
|
return {
|
|
"error": (f"Sandbox response too large ({len(raw)} bytes; max {_MAX_RESPONSE_BYTES})."),
|
|
}
|
|
|
|
try:
|
|
data: Any = response.json()
|
|
except ValueError:
|
|
return {
|
|
"error": (f"Sandbox tool '{tool_name}' returned non-JSON: {response.text[:200]}"),
|
|
}
|
|
|
|
if not isinstance(data, dict):
|
|
return {"error": f"Sandbox tool '{tool_name}' returned non-object JSON."}
|
|
|
|
return data
|