mirror of
https://github.com/usestrix/strix.git
synced 2026-08-20 02:23:35 +02:00
* fix(proxy,tooling): serialize+reconnect Caido client, actionable HTTPQL errors, sandbox tool guidance
Addresses the top recurring agent tool-call failures observed in telemetry:
- proxy: the shared Caido client had no locking or reconnect, so concurrent
agent calls raced ("Transport is already connected") and a dead transport
poisoned the rest of the run ("Connector is closed"/"Server disconnected").
Add an asyncio lock + bounded reconnect in caido_api.call_with_client (sandbox
path) and a scan-wide caido_lock in the run context that host-side proxy tools
hold around every call. Deterministic errors are not retried.
- proxy: list_requests now returns Caido's exact parser message, echoes the
offending query, and includes a corrected-syntax hint so agents self-correct
instead of retrying a broken HTTPQL filter.
- shell/prompt: document that write_stdin requires a process started with
tty=true; nudge toward writing Python to a file over deeply-nested one-liners;
note the venv pre-installs common libs.
- agent-browser: distinguish daemon/connection failures (run doctor, don't loop)
from malformed commands; invoke directly (no sh -c wrapper).
- containers: use POSIX '.' instead of the bashism 'source' in generated rc
files (fixes 'sh: source: not found'); add file + xxd and pre-install
requests/httpx/beautifulsoup4/lxml/pyjwt/cryptography in the sandbox venv.
- tests: cover proxy serialization/reconnect/no-retry and HTTPQL errors.
* fix(proxy): host-side reconnect, close stale clients, don't retry mutations
Addresses Greptile review on the reconnect logic:
- Host path had no reconnect: a dead shared context client (Caido restart /
network blip) previously disabled proxy tools for the rest of the scan. Add
SharedCaidoClient, a serialized reconnect-safe holder stored once per scan in
the run context and shared across agents. On a dead transport it rebuilds via
reconnect_caido, which re-selects the SAME Caido project (preserving captured
traffic) instead of creating a new empty one.
- Don't repeat completed mutations: call_with_client / SharedCaidoClient.call
take idempotent=. Reads retry once on reconnect; replay + scope
create/update/delete heal the client but re-raise instead of risking a
double-apply.
- Don't leak replaced clients: the stale client is aclose()d (best-effort) on
every reconnect.
- Extend tests to cover close-on-reconnect, non-idempotent re-raise, and the
SharedCaidoClient holder.
* fix(proxy): close replacement Caido client when project.select fails
Addresses Greptile P1: in reconnect_caido (and bootstrap_caido) a successful
connect() followed by a failing project.select()/create() discarded the
connected client without closing it, so a missing/unavailable project could
leak a transport on every retry. Close the client before re-raising.
---------
Co-authored-by: Alex Schapiro <bearsyankees@gmail.com>
199 lines
6.8 KiB
Python
199 lines
6.8 KiB
Python
"""Per-scan sandbox session lifecycle."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import shutil
|
|
from pathlib import Path
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
from agents.sandbox.entries import BaseEntry, LocalDir
|
|
from agents.sandbox.manifest import Environment, Manifest
|
|
|
|
from strix.config import load_settings
|
|
from strix.runtime.backends import get_backend
|
|
from strix.runtime.caido_bootstrap import bootstrap_caido, reconnect_caido
|
|
from strix.runtime.local_dir_staging import stage_symlink_safe_dir
|
|
from strix.tools.proxy.caido_api import SharedCaidoClient
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
from caido_sdk_client import Client as CaidoClient
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
# In-container Caido sidecar port (matches the image's caido-cli bind).
|
|
_CONTAINER_CAIDO_PORT = 48080
|
|
|
|
|
|
_SESSION_CACHE: dict[str, dict[str, Any]] = {}
|
|
|
|
# Manifest root inside the container; entry keys hang off this path.
|
|
_WORKSPACE_ROOT = "/workspace"
|
|
|
|
|
|
def build_session_entries(
|
|
local_sources: list[dict[str, Any]],
|
|
) -> tuple[dict[str | Path, BaseEntry], list[dict[str, Any]], list[Path]]:
|
|
"""Split local sources into copied manifest entries and host bind mounts.
|
|
|
|
Sources flagged ``mount`` are bind-mounted read-only at
|
|
``/workspace/<workspace_subdir>`` (not added to the manifest, so the SDK
|
|
does not stream them in file-by-file). Every other source becomes a
|
|
``LocalDir`` entry copied into the container as before. Trees containing
|
|
symlinks (which the SDK's ``LocalDir`` walker refuses outright) are first
|
|
staged into a symlink-safe temp copy; those temp dirs are returned so the
|
|
caller can remove them once the upload completes.
|
|
"""
|
|
entries: dict[str | Path, BaseEntry] = {}
|
|
bind_mounts: list[dict[str, Any]] = []
|
|
staged_dirs: list[Path] = []
|
|
for src in local_sources:
|
|
ws_subdir = src.get("workspace_subdir") or ""
|
|
host_path = src.get("source_path") or ""
|
|
if not ws_subdir or not host_path:
|
|
continue
|
|
resolved = Path(host_path).expanduser().resolve()
|
|
if src.get("mount"):
|
|
bind_mounts.append(
|
|
{
|
|
"source": str(resolved),
|
|
"target": f"{_WORKSPACE_ROOT}/{ws_subdir}",
|
|
"read_only": True,
|
|
}
|
|
)
|
|
else:
|
|
upload_path, staged = stage_symlink_safe_dir(resolved)
|
|
if staged is not None:
|
|
staged_dirs.append(staged)
|
|
entries[ws_subdir] = LocalDir(src=upload_path)
|
|
return entries, bind_mounts, staged_dirs
|
|
|
|
|
|
async def create_or_reuse(
|
|
scan_id: str,
|
|
*,
|
|
image: str,
|
|
local_sources: list[dict[str, Any]],
|
|
) -> dict[str, Any]:
|
|
"""Return the existing session bundle for ``scan_id`` or create a new one.
|
|
|
|
Each ``local_sources`` entry exposes its host ``source_path`` at
|
|
``/workspace/<workspace_subdir>`` inside the container — copied in, or
|
|
bind-mounted read-only when the entry is flagged ``mount``.
|
|
"""
|
|
cached = _SESSION_CACHE.get(scan_id)
|
|
if cached is not None:
|
|
logger.info("Reusing existing sandbox session for scan %s", scan_id)
|
|
return cached
|
|
|
|
entries, bind_mounts, staged_dirs = build_session_entries(local_sources)
|
|
|
|
# Caido runs as an in-container sidecar; HTTP(S) traffic from any
|
|
# process started via ``session.exec`` (the SDK's Shell tool, etc.)
|
|
# picks up these env vars automatically. ``NO_PROXY`` keeps the
|
|
# agent-browser CDP daemon's localhost traffic from looping back
|
|
# through Caido.
|
|
container_caido_url = f"http://127.0.0.1:{_CONTAINER_CAIDO_PORT}"
|
|
manifest = Manifest(
|
|
entries=entries,
|
|
environment=Environment(
|
|
value={
|
|
"PYTHONUNBUFFERED": "1",
|
|
"HOST_GATEWAY": "host.docker.internal",
|
|
"http_proxy": container_caido_url,
|
|
"https_proxy": container_caido_url,
|
|
"ALL_PROXY": container_caido_url,
|
|
"NO_PROXY": "localhost,127.0.0.1",
|
|
},
|
|
),
|
|
)
|
|
|
|
backend_name = load_settings().runtime.backend
|
|
backend = get_backend(backend_name)
|
|
|
|
logger.info(
|
|
"Creating sandbox session for scan %s (backend=%s, image=%s)",
|
|
scan_id,
|
|
backend_name,
|
|
image,
|
|
)
|
|
try:
|
|
client, session = await backend(
|
|
image=image,
|
|
manifest=manifest,
|
|
exposed_ports=(_CONTAINER_CAIDO_PORT,),
|
|
bind_mounts=bind_mounts,
|
|
)
|
|
finally:
|
|
for staged in staged_dirs:
|
|
shutil.rmtree(staged, ignore_errors=True)
|
|
|
|
caido_endpoint = await session.resolve_exposed_port(_CONTAINER_CAIDO_PORT)
|
|
scheme = "https" if caido_endpoint.tls else "http"
|
|
host_caido_url = f"{scheme}://{caido_endpoint.host}:{caido_endpoint.port}"
|
|
logger.debug("Caido host endpoint resolved: %s", host_caido_url)
|
|
|
|
caido_client, caido_project_id = await bootstrap_caido(
|
|
session,
|
|
host_url=host_caido_url,
|
|
container_url=container_caido_url,
|
|
)
|
|
|
|
async def _reconnect_caido() -> CaidoClient:
|
|
return await reconnect_caido(
|
|
session,
|
|
host_url=host_caido_url,
|
|
container_url=container_caido_url,
|
|
project_id=caido_project_id,
|
|
)
|
|
|
|
bundle = {
|
|
"client": client,
|
|
"session": session,
|
|
"caido_client": SharedCaidoClient(caido_client, _reconnect_caido),
|
|
}
|
|
_SESSION_CACHE[scan_id] = bundle
|
|
logger.info("Sandbox session for scan %s ready and cached", scan_id)
|
|
return bundle
|
|
|
|
|
|
async def cleanup(scan_id: str) -> None:
|
|
"""Tear down ``scan_id``'s container and drop its cache entry.
|
|
|
|
Best-effort: any error during ``client.delete`` is logged and
|
|
swallowed. We never want a cleanup failure to prevent the next
|
|
scan from starting; the worst case is a stranded container that
|
|
Docker's normal reaping will catch on next ``docker prune``.
|
|
"""
|
|
bundle = _SESSION_CACHE.pop(scan_id, None)
|
|
if bundle is None:
|
|
logger.debug("cleanup(%s): no cached session", scan_id)
|
|
return
|
|
|
|
caido_client = bundle.get("caido_client")
|
|
if caido_client is not None:
|
|
try:
|
|
await caido_client.aclose()
|
|
except Exception: # noqa: BLE001
|
|
logger.debug("cleanup(%s): caido_client.aclose() raised", scan_id, exc_info=True)
|
|
|
|
client = bundle["client"]
|
|
try:
|
|
await client.delete(bundle["session"])
|
|
logger.info("Cleaned up sandbox session for scan %s", scan_id)
|
|
except Exception:
|
|
logger.exception(
|
|
"cleanup(%s): client.delete raised; container may need manual reaping",
|
|
scan_id,
|
|
)
|
|
|
|
docker_client = getattr(client, "docker_client", None)
|
|
if docker_client is not None:
|
|
try:
|
|
docker_client.close()
|
|
except Exception: # noqa: BLE001
|
|
logger.debug("cleanup(%s): docker_client.close() raised", scan_id, exc_info=True)
|