项目文件夹

文件
Gelei Deng ab5fbb4d90 feat: ship the durable multi-model autonomous PentestGPT runtime (#493)
* first refactor

* feat: dockerized tool with persistent Claude+Codex login + multi-model benchmark

Run the autonomous CTF/pentest tool in Docker with a one-time, persistent login for
BOTH Claude Code and Codex, and add a multi-model benchmark harness.

Backend (multi-model):
- Add `--backend {claude,codex}` to the CTF pipeline. CodexBackend (pentestgpt/core/
  backend.py) wraps unified_agent's Codex backend and translates its events into
  AgentMessages, so the same pipeline runs on Claude (opus/sonnet) or Codex
  (gpt-5.5/gpt-5.4-mini). Wired through config.backend, pipeline stage construction,
  and the CLI (+ PENTESTGPT_CODEX_EFFORT; greppable [CODEX_USAGE] under PENTESTGPT_BENCH=1).

Docker tool (tool-only image; the benchmark stays OUTSIDE the image):
- Extend Dockerfile: Codex CLI (@openai/codex) + openai_codex SDK + unified_agent/
  pentestgpt_agent/pentestgpt_legacy packages + gobuster/dirb + socat. Add .dockerignore
  (keeps creds/benchmark/workspace out of the build context).
- Persistent dual login (the hard part) — asymmetric by token model:
  * Claude: `setup-token` -> token stored in the pentestgpt-claude volume; entrypoint
    exports CLAUDE_CODE_OAUTH_TOKEN (setup-token does not write .credentials.json; macOS
    host creds live in the Keychain and can't be copied).
  * Codex: the container does its OWN `codex login` (NOT seeding -- ChatGPT refresh tokens
    are single-use, so a shared/copied login 401s on first refresh). The 127.0.0.1:1455
    OAuth callback is forwarded into the container via a socat hop (-p 1455:8455).
  * scripts/docker-login.sh is idempotent: checks logins live, logs in only the missing one(s).
- docker-compose codex-config volume (+ pinned names); entrypoint token-export + non-blocking
  preflight; scripts/docker-auth-status.sh; Make targets (docker-build/login/auth-status/
  run/shell/down/nuke).
- Verified end-to-end: one `make docker-login` -> a fresh container reports claude+codex
  logged in with live round-trips; the CTF pipeline (Codex) captured a flag against an
  isolated fixture and the pentest pipeline ran cleanly; persists across recreation, no re-login.

Benchmark (multi-model, host-side):
- benchmark/pilot/ harness (run_pilot.py + report.py): builds each xbow challenge, discovers
  the loopback port, runs the pipeline across the 4 model combos, judges by the baked
  FLAG{sha256(UPPER-dir)}, and renders REPORT.md (infra failures excluded from solve rates).
  Includes the partial pilot's results (results.jsonl + REPORT.md).

Docs: docs/docker-dev-plan.md (full plan + implementation status); CLAUDE.md and README
docker quickstart; benchmark/pilot/README.md; design-doc roadmap (docs/redesign).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix: fail controller on backend error messages

* fix: allow listing sessions without target

* docs: add docker xbow benchmark report

* fix: infer concrete backend constructor type

* docs: refresh docker benchmark documentation

* feat(benchmark): add pure single-agent baseline + pipeline comparison

Add a "pure single agent" benchmark variant -- one bare `claude -p` /
`codex exec` call per target (no pipeline) -- to quantify what the 3-stage
PentestGPT pipeline buys over an un-orchestrated agent on the xbow targets.

- pentestgpt/prompts/stages.py: ctf_single_agent_{system,task}_prompt -- the
  pipeline's shared fragments collapsed into ONE turn, so prompt content is
  held constant and the only variable is the multi-stage decomposition.
- benchmark/pilot/run_docker_bench.py: docker-network runner
  (--variant single|pipeline). Brings the target up, discovers the container's
  internal IP+network (skips DB side-cars/ports), docker-runs the tool image on
  that network, and scores the ground-truth flag against the agent's *assistant
  text* only (parity with the pipeline's raw streaming). Reads stdout in chunks
  to handle >64KB JSON lines. Resumable; --dry-run supported.
- benchmark/pilot/report_comparison.py -> DOCKER_COMPARISON.md: head-to-head
  pipeline-vs-single per model on the common non-infra set.
- tests/unit/test_single_agent_prompt.py: prompt-builder coverage.
- docs: README, CLAUDE.md, benchmark README, DOCKER_REPORT updated.

Recorded result (10 medium/hard targets x 4 models, container-to-container,
same baseline image digest 0c4c0f3e..., commit dca0019 image):

  Model               Pipeline   Single
  Claude Opus           5/10      7/10   (single +2)
  Claude Sonnet         6/10      4/10   (pipeline +2)
  Codex gpt-5.5         7/10      7/10   (tie)
  Codex gpt-5.4-mini    3/10      4/10   (single +1)
  TOTAL                21/40     22/40

Single agent matches the pipeline on solve rate (55% vs 52%) while using
~40% fewer Codex tokens (13.0M vs 21.8M) and solving faster. The pipeline
only clearly helps Claude Sonnet (which times out solo); Opus is better solo.
Full per-challenge grid in DOCKER_COMPARISON.md; raw records in
docker_single_results.jsonl.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* feat(benchmark): add pentestgpt_agent docker harness

* bench: refresh pentestgpt_agent smoke result

* fix(benchmark): make repeat rows variant-aware

* fix(agent): fall back for semantic executor labels

* fix(agent): tolerate executor prose evidence

* fix(benchmark): score accepted framework findings

* bench: append partial framework repeat results

* bench: complete framework repeat sweep

* bench: expose framework executor concurrency

* bench: add extended parallel framework sweep

* checkpoint: preserve working agent and benchmark state

* feat: harden durable agent loop and xbow qualification

* fix: reserve an exploit result turn

* docs: record clean xbow qualification

* build: consume unified-agent from the git wrapper repo

Repoint pentestgpt_agent_new's unified-agent dependency from the local
editable path (../../UnifiedAgentPoC, now renamed and gone) to the pinned
git source PentestGPT-Project/UnifedAgentWrapper@d05d21f. Regenerate uv.lock
and update test_dependency.py to assert the external package is installed
from that VCS URL (not the repo-root vendored copy) at version 0.2.0.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* refactor: make pentestgpt_agent_new the sole framework

Remove the retired ledger-based pentestgpt_agent package (instructor/executor/
judge) and its orphaned unit + smoke tests. The nested pentestgpt_agent_new
project (Supervisor/Executor over a durable SQLite loop, consuming unified-agent
from the git wrapper) is now the single maintained framework.

Repoint the top-level tooling to it:
- pyproject: drop the pentestgpt-agent console script and pentestgpt_agent from
  the wheel packages.
- Makefile: lint/format target parent code only; typecheck/check/ci now run the
  nested framework's own gate (ruff, format, mypy, pytest) via test-agent-new /
  check-agent-new, so `make check` finally covers it; `make run` delegates to the
  pentestgpt-agent-new CLI.
- Dockerfile: stop copying the removed package (kept the build working); note the
  framework is not baked into the image yet.
- docker container-health test: import the substrate packages that actually ship.
- CLAUDE.md / AGENT.md: describe the new framework, the git-sourced wrapper, and
  the deprioritized benchmark/Docker rewire.

The XBOW `--variant framework` path and docker-bench Makefile targets still point
at the old in-image framework and are left as a pending rewire (benchmarks
deprioritized); the naive `--variant single` path is unaffected.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* refactor: rename pentestgpt_agent_new -> pentestgpt_agent

The framework reclaims the clean name now that the old ledger-based package is
gone. Rename the nested project folder, its src package, the distribution
(pentestgpt-agent-new -> pentestgpt-agent) and CLI, and every import/reference in
the package, the umbrella Makefile, the Dockerfile, the docker health test, and
CLAUDE.md / AGENT.md. Regenerate uv.lock. The audit CLI stays pentestgpt-agent-audit;
the git-sourced unified-agent dependency is unchanged. `make check` is green
(108 nested tests). The two historical *_REPORT.md files keep the old name as
dated records.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore: extract benchmark harness to sibling xbow-benchmark repo

Move PentestGPT/benchmark/ out to ../xbow-benchmark (its own repo) to keep this
project clean. The harness was decoupled from the framework code (it scores
container output, never imports pentestgpt_agent/unified_agent), so only
operational ties remain and they now live in the sibling repo.

- Remove benchmark/ and the 4 harness unit tests (relocated + repointed there).
- Strip the docker-bench-*/bench-* targets and their config vars from the
  Makefile; keep the tool-image lifecycle (docker-build/login/run/...) and add a
  help pointer to `make -C ../xbow-benchmark help`.

The sibling repo mounts this checkout read-only (--source-root ../PentestGPT) and
runs the pentestgpt:latest image built here.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat: harden autonomous framework and runtime integration

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-13 16:49:08 +08:00

648 行
21 KiB
Python

import json
import sqlite3
from collections.abc import AsyncIterator
from dataclasses import replace
from pathlib import Path
import pytest
from unified_agent import (
AgentEvent,
CommandRun,
RunOptions,
SandboxPolicy,
TurnCompleted,
UnifiedAgent,
)
from pentestgpt_agent import trial
from pentestgpt_agent.agents import (
EXECUTOR_INSTRUCTIONS,
SUPERVISOR_INSTRUCTIONS,
Executor,
Supervisor,
)
from pentestgpt_agent.audit import _is_hidden_provider_memory_action, audit_run
from pentestgpt_agent.memory import MemoryKernel, RunSpec
from pentestgpt_agent.trace import EpisodeRunner, TraceStore
from pentestgpt_agent.trial import TrialConfig, run_trial
class TrialSupervisorBackend:
name = "scripted"
def __init__(self) -> None:
self.calls = 0
async def stream(self, prompt: str, opts: RunOptions) -> AsyncIterator[AgentEvent]:
if self.calls == 0:
output = {
"base_revision": 0,
"new_tasks": [
{
"id": "capture-flag",
"kind": "verify",
"target": "http://target.test",
"objective": "Capture the benchmark flag.",
"done_when": "The exact flag is recorded.",
"basis_ids": [],
"depends_on": [],
}
],
"next_task_id": "capture-flag",
"finish": False,
"finish_basis_ids": [],
"summary": "Capture the flag directly.",
}
else:
state = json.loads(prompt.split("\n\n", 1)[1])
output = {
"base_revision": 2,
"new_tasks": [],
"next_task_id": None,
"finish": True,
"finish_basis_ids": [state["observations"][-1]["id"]],
"summary": "The exact benchmark flag was captured.",
}
self.calls += 1
yield CommandRun(command="inspect-planning-workspace", exit_code=0, output="ok")
yield TurnCompleted(success=True, structured_output=output)
class TrialExecutorBackend:
name = "scripted"
async def stream(self, prompt: str, opts: RunOptions) -> AsyncIterator[AgentEvent]:
yield CommandRun(command="read-flag", exit_code=0, output="FLAG{trial-oracle}")
yield TurnCompleted(
success=True,
structured_output={
"task_id": "capture-flag",
"outcome": "done",
"summary": "Captured the exact flag.",
"evidence_excerpt": "FLAG{trial-oracle}",
},
)
class FailureSupervisorBackend:
name = "scripted"
async def stream(self, prompt: str, opts: RunOptions) -> AsyncIterator[AgentEvent]:
yield TurnCompleted(
success=True,
structured_output={
"base_revision": 0,
"new_tasks": [
{
"id": "test-target",
"kind": "test",
"target": "http://target.test",
"objective": "Test the target.",
"done_when": "The behavior is recorded.",
"basis_ids": [],
"depends_on": [],
}
],
"next_task_id": "test-target",
"finish": False,
"finish_basis_ids": [],
"summary": "Test the target.",
},
)
class FailureExecutorBackend:
name = "scripted"
async def stream(self, prompt: str, opts: RunOptions) -> AsyncIterator[AgentEvent]:
yield CommandRun(command="curl http://target.test", exit_code=0, output="partial")
yield TurnCompleted(
success=False,
stop_reason="error_max_turns",
error="Reached maximum number of turns (2)",
)
raise RuntimeError("Command failed with exit code 1")
class InvalidSupervisorBackend:
name = "scripted"
async def stream(self, prompt: str, opts: RunOptions) -> AsyncIterator[AgentEvent]:
yield TurnCompleted(success=True, structured_output={"invalid": "decision"})
class MustNotRunBackend:
name = "scripted"
async def stream(self, prompt: str, opts: RunOptions) -> AsyncIterator[AgentEvent]:
raise AssertionError("a completed resumed trial must not invoke a provider")
yield # pragma: no cover
def test_claude_provider_memory_is_disabled_for_fresh_episodes() -> None:
assert trial._provider_environment("claude") == {
"CLAUDE_CODE_DISABLE_AUTO_MEMORY": "1",
"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1",
"DISABLE_AUTOUPDATER": "1",
}
assert trial._provider_environment("codex") == {}
def test_audit_recognizes_provider_side_memory_writes() -> None:
assert _is_hidden_provider_memory_action(
{
"type": "tool_call",
"name": "Write",
"input": {"file_path": "/home/pentester/.claude/projects/-trial/memory/MEMORY.md"},
}
)
assert not _is_hidden_provider_memory_action(
{
"type": "tool_call",
"name": "Write",
"input": {"file_path": "/trial/workspaces/run/notes.md"},
}
)
def test_audit_emits_an_incomplete_record_for_an_empty_episode_directory(
tmp_path: Path,
) -> None:
run_dir = tmp_path / "runs" / "incomplete-trial"
MemoryKernel(run_dir / "state.sqlite3").create_run(
RunSpec(
run_id="incomplete-trial",
goal="Assess the target.",
allowed_targets=("http://target.test",),
)
)
(run_dir / "traces" / "executor-incomplete").mkdir(parents=True)
audit = audit_run(run_dir, expected_flag="FLAG{not-present}")
assert audit["passed"] is False
assert audit["checks"]["all_episodes_complete"] is False
assert audit["episodes"] == [
{
"episode_id": "executor-incomplete",
"run_id": None,
"input_episode_id": None,
"role": None,
"state_revision": None,
"task_id": None,
"attempt_id": None,
"opened_at": None,
"closed_at": None,
"success": False,
"duration_ms": None,
"cost_usd": 0.0,
"usage": {},
"structured_output": None,
"event_counts": {},
"actions": [],
"events": [],
"truncated_tail": False,
"complete": False,
"integrity_errors": [
"missing input.json",
"missing output.json",
"missing events.jsonl",
],
}
]
def test_audit_accounts_for_an_initialization_crash_settled_by_run_transition(
tmp_path: Path,
) -> None:
run_dir = tmp_path / "runs" / "settled-supervisor-crash"
memory = MemoryKernel(run_dir / "state.sqlite3")
memory.create_run(
RunSpec(
run_id="settled-supervisor-crash",
goal="Assess the target.",
allowed_targets=("http://target.test",),
)
)
(run_dir / "traces" / "supervisor-r0").mkdir(parents=True)
memory.commit_run_failure(
"settled-supervisor-crash",
0,
failure_kind="supervisor_contract",
failure_message="initialization was interrupted twice",
)
audit = audit_run(run_dir, expected_flag="FLAG{not-present}")
assert audit["checks"]["all_episodes_complete"] is True
assert audit["checks"]["transition_timeline_complete"] is True
assert audit["passed"] is False
def test_audit_emits_partial_events_and_integrity_errors_for_malformed_trace_files(
tmp_path: Path,
) -> None:
run_dir = tmp_path / "runs" / "malformed-trial"
MemoryKernel(run_dir / "state.sqlite3").create_run(
RunSpec(
run_id="malformed-trial",
goal="Assess the target.",
allowed_targets=("http://target.test",),
)
)
episode_dir = run_dir / "traces" / "executor-malformed"
episode_dir.mkdir(parents=True)
(episode_dir / "input.json").write_text("{", encoding="utf-8")
(episode_dir / "events.jsonl").write_text(
'{"sequence":1,"type":"assistant_text","text":"partial"}\n{',
encoding="utf-8",
)
(episode_dir / "output.json").write_text("[", encoding="utf-8")
audit = audit_run(run_dir, expected_flag="FLAG{not-present}")
assert audit["passed"] is False
episode = audit["episodes"][0]
assert episode["complete"] is False
assert episode["truncated_tail"] is True
assert episode["events"] == [{"sequence": 1, "type": "assistant_text", "text": "partial"}]
assert episode["integrity_errors"] == [
"malformed input.json",
"malformed output.json",
"malformed events.jsonl at line 2",
]
@pytest.mark.asyncio
async def test_trial_writes_a_run_that_the_artifact_auditor_can_verify(
tmp_path: Path,
) -> None:
runs_root = tmp_path / "runs"
traces = TraceStore(runs_root)
supervisor = Supervisor(
EpisodeRunner(
UnifiedAgent(
TrialSupervisorBackend(),
workspace=tmp_path / "supervisor",
sandbox=SandboxPolicy.FULL_ACCESS,
instructions=SUPERVISOR_INSTRUCTIONS,
),
traces,
)
)
executor = Executor(
EpisodeRunner(
UnifiedAgent(
TrialExecutorBackend(),
workspace=tmp_path / "executor",
sandbox=SandboxPolicy.FULL_ACCESS,
instructions=EXECUTOR_INSTRUCTIONS,
),
traces,
)
)
config = TrialConfig(
run_id="trial-run",
goal="Capture the benchmark flag.",
targets=("http://target.test",),
backend="claude",
model="claude-opus-4-8",
runs_root=runs_root,
workspace_root=tmp_path / "agents",
effort="xhigh",
max_decisions=4,
supervisor_max_turns=2,
executor_max_turns=2,
)
summary = await run_trial(config, supervisor=supervisor, executor=executor)
audit = audit_run(runs_root / "trial-run", expected_flag="FLAG{trial-oracle}")
assert summary["status"] == "completed"
assert summary["model"] == "claude-opus-4-8"
assert summary["effort"] == "xhigh"
assert summary["runtime_policy_revision"] == 2
assert [attempt["status"] for attempt in summary["attempts"]] == ["done"]
assert [transition["kind"] for transition in summary["transitions"]] == [
"run_created",
"plan_committed",
"attempt_committed",
"plan_committed",
]
assert (runs_root / "trial-run" / "summary.json").exists()
assert audit["passed"] is True
assert audit["checks"] == {
"run_completed": True,
"oracle_in_canonical_observation": True,
"no_active_tasks": True,
"no_active_attempts": True,
"all_episodes_complete": True,
"all_observations_grounded": True,
"all_observations_are_direct_quotes": True,
"all_observation_identities_match": True,
"all_basis_ids_exist": True,
"all_basis_producers_are_dependencies": True,
"all_exploit_bases_current": True,
"completion_basis_valid": True,
"transition_timeline_complete": True,
"all_failed_episodes_settled": True,
}
assert audit["schema_version"] == 2
assert audit["totals"]["supervisor_actions"] == 2
assert len(audit["episodes"]) == 3
trial_identity = json.loads(
(runs_root / "trial-run" / "trial-config.json").read_text(encoding="utf-8")
)
assert trial_identity["schema_version"] == 2
assert trial_identity["supervisor_sandbox"] == "full_access"
assert trial_identity["executor_sandbox"] == "full_access"
executor_episode = next(
episode
for episode in (runs_root / "trial-run" / "traces").iterdir()
if json.loads((episode / "input.json").read_text(encoding="utf-8"))["role"] == "executor"
)
events_path = executor_episode / "events.jsonl"
events = [json.loads(line) for line in events_path.read_text(encoding="utf-8").splitlines()]
command_receipt = next(event for event in events if event["type"] == "command_run")
command_receipt["exit_code"] = 255
events_path.write_text(
"".join(json.dumps(event) + "\n" for event in events),
encoding="utf-8",
)
nonzero_exit_audit = audit_run(
runs_root / "trial-run",
expected_flag="FLAG{trial-oracle}",
)
assert nonzero_exit_audit["checks"]["all_observations_grounded"] is True
assert nonzero_exit_audit["checks"]["all_observations_are_direct_quotes"] is True
database = runs_root / "trial-run" / "state.sqlite3"
with sqlite3.connect(database) as connection:
connection.execute(
"UPDATE observations SET statement = ?",
("FLAG{trial-oracle} with uncaptured suffix",),
)
tampered = audit_run(runs_root / "trial-run", expected_flag="FLAG{trial-oracle}")
assert tampered["checks"]["oracle_in_canonical_observation"] is True
assert tampered["checks"]["all_observations_are_direct_quotes"] is False
assert tampered["passed"] is False
@pytest.mark.asyncio
async def test_trial_interface_can_explicitly_resume_a_persisted_run(tmp_path: Path) -> None:
runs_root = tmp_path / "runs"
traces = TraceStore(runs_root)
initial_config = TrialConfig(
run_id="resumable-trial",
goal="Capture the benchmark flag.",
targets=("http://target.test",),
backend="claude",
model="claude-opus-4-8",
runs_root=runs_root,
workspace_root=tmp_path / "agents",
max_decisions=4,
supervisor_max_turns=2,
executor_max_turns=2,
)
initial = await run_trial(
initial_config,
supervisor=Supervisor(
EpisodeRunner(
UnifiedAgent(
TrialSupervisorBackend(),
workspace=tmp_path / "supervisor",
sandbox=SandboxPolicy.FULL_ACCESS,
instructions=SUPERVISOR_INSTRUCTIONS,
),
traces,
)
),
executor=Executor(
EpisodeRunner(
UnifiedAgent(
TrialExecutorBackend(),
workspace=tmp_path / "executor",
sandbox=SandboxPolicy.FULL_ACCESS,
instructions=EXECUTOR_INSTRUCTIONS,
),
traces,
)
),
)
assert initial["status"] == "completed"
resume_config = replace(initial_config, resume=True)
resumed = await run_trial(
resume_config,
supervisor=Supervisor(
EpisodeRunner(
UnifiedAgent(
MustNotRunBackend(),
workspace=tmp_path / "supervisor-resume",
sandbox=SandboxPolicy.FULL_ACCESS,
instructions=SUPERVISOR_INSTRUCTIONS,
),
traces,
)
),
executor=Executor(
EpisodeRunner(
UnifiedAgent(
MustNotRunBackend(),
workspace=tmp_path / "executor-resume",
sandbox=SandboxPolicy.FULL_ACCESS,
instructions=EXECUTOR_INSTRUCTIONS,
),
traces,
)
),
)
assert resumed["status"] == "completed"
assert resumed["revision"] == initial["revision"]
assert resumed["episodes"] == initial["episodes"]
with pytest.raises(ValueError, match="does not match requested config"):
await run_trial(replace(resume_config, model="different-model"))
with pytest.raises(ValueError, match="does not match requested config"):
await run_trial(replace(resume_config, effort="high"))
def test_build_roles_forward_effort_and_grant_full_access(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
class CapturedAgent:
def __init__(self, backend: object, **options: object) -> None:
self.backend = backend
self.effort = options["effort"]
self.sandbox = options["sandbox"]
self.instructions = options["instructions"]
monkeypatch.setattr(trial, "UnifiedAgent", CapturedAgent)
config = TrialConfig(
run_id="effort-trial",
goal="Assess the target.",
targets=("http://target.test",),
backend="claude",
model="claude-opus-4-8",
runs_root=tmp_path / "runs",
workspace_root=tmp_path / "agents",
effort="xhigh",
)
supervisor, executor = trial._build_roles(config, TraceStore(config.runs_root))
assert supervisor.runner.agent.effort == "xhigh"
assert executor.runner.agent.effort == "xhigh"
assert supervisor.runner.agent.sandbox is SandboxPolicy.FULL_ACCESS
assert executor.runner.agent.sandbox is SandboxPolicy.FULL_ACCESS
@pytest.mark.asyncio
async def test_trial_rejects_a_path_like_run_id_before_creating_artifacts(
tmp_path: Path,
) -> None:
config = TrialConfig(
run_id="../escape",
goal="Assess the target.",
targets=("http://target.test",),
backend="claude",
model="claude-opus-4-8",
runs_root=tmp_path / "runs",
workspace_root=tmp_path / "agents",
)
with pytest.raises(ValueError, match="run_id must be"):
await run_trial(config)
assert not (tmp_path / "escape").exists()
@pytest.mark.asyncio
async def test_failed_trial_summary_has_typed_failure_and_no_active_lease(
tmp_path: Path,
) -> None:
runs_root = tmp_path / "runs"
traces = TraceStore(runs_root)
supervisor = Supervisor(
EpisodeRunner(
UnifiedAgent(
FailureSupervisorBackend(),
workspace=tmp_path / "supervisor",
sandbox=SandboxPolicy.FULL_ACCESS,
instructions=SUPERVISOR_INSTRUCTIONS,
),
traces,
)
)
executor = Executor(
EpisodeRunner(
UnifiedAgent(
FailureExecutorBackend(),
workspace=tmp_path / "executor",
sandbox=SandboxPolicy.FULL_ACCESS,
instructions=EXECUTOR_INSTRUCTIONS,
),
traces,
),
max_turns=2,
)
config = TrialConfig(
run_id="failed-trial",
goal="Assess the target.",
targets=("http://target.test",),
backend="claude",
model="claude-opus-4-8",
runs_root=runs_root,
workspace_root=tmp_path / "agents",
max_decisions=2,
supervisor_max_turns=2,
executor_max_turns=2,
)
summary = await run_trial(config, supervisor=supervisor, executor=executor)
audit = audit_run(runs_root / "failed-trial", expected_flag="FLAG{not-present}")
assert summary["status"] == "failed"
assert summary["error"] == "max_turns: Reached maximum number of turns (2)"
assert summary["attempts"][0]["status"] == "error"
assert summary["attempts"][0]["failure_kind"] == "max_turns"
assert summary["tasks"][0]["status"] == "failed"
assert audit["checks"]["no_active_tasks"] is True
assert audit["checks"]["no_active_attempts"] is True
assert audit["checks"]["all_failed_episodes_settled"] is True
assert audit["checks"]["transition_timeline_complete"] is True
assert audit["passed"] is False
@pytest.mark.asyncio
async def test_run_level_supervisor_failure_is_present_in_the_trial_summary(
tmp_path: Path,
) -> None:
runs_root = tmp_path / "runs"
traces = TraceStore(runs_root)
supervisor = Supervisor(
EpisodeRunner(
UnifiedAgent(
InvalidSupervisorBackend(),
workspace=tmp_path / "supervisor",
sandbox=SandboxPolicy.FULL_ACCESS,
instructions=SUPERVISOR_INSTRUCTIONS,
),
traces,
)
)
executor = Executor(
EpisodeRunner(
UnifiedAgent(
FailureExecutorBackend(),
workspace=tmp_path / "executor",
sandbox=SandboxPolicy.FULL_ACCESS,
instructions=EXECUTOR_INSTRUCTIONS,
),
traces,
)
)
config = TrialConfig(
run_id="supervisor-failure",
goal="Assess the target.",
targets=("http://target.test",),
backend="claude",
model="claude-opus-4-8",
runs_root=runs_root,
workspace_root=tmp_path / "agents",
)
summary = await run_trial(config, supervisor=supervisor, executor=executor)
assert summary["status"] == "failed"
assert summary["error"].startswith("supervisor_contract: Supervisor failed after 2 attempts")
assert summary["transitions"][-1]["kind"] == "supervisor_failed"
def test_cli_returns_nonzero_for_a_canonical_failed_run(
monkeypatch: pytest.MonkeyPatch,
) -> None:
async def failed_run(config: TrialConfig) -> dict[str, object]:
assert config.effort == "xhigh"
return {"status": "failed", "run_id": config.run_id}
monkeypatch.setattr(trial, "run_trial", failed_run)
exit_code = trial.main(
[
"--goal",
"Assess the target.",
"--target",
"http://target.test",
"--run-id",
"failed-cli",
"--effort",
"xhigh",
]
)
assert exit_code == 1