项目文件夹

文件
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

771 行
24 KiB
Python

from pathlib import Path
import pytest
from pentestgpt_agent.execution import ExecutionOutcome, ValidExecution
from pentestgpt_agent.memory import MemoryKernel, RunSnapshot, RunSpec
from pentestgpt_agent.plan import (
PlanValidationError,
SupervisorDecision,
TaskKind,
TaskProposal,
TaskStatus,
compile_plan,
)
def _complete_task(
memory: MemoryKernel,
snapshot: RunSnapshot,
*,
task_id: str,
kind: TaskKind,
target: str,
evidence: str,
basis_ids: tuple[str, ...] = (),
depends_on: tuple[str, ...] = (),
) -> RunSnapshot:
commit = memory.commit_plan(
compile_plan(
SupervisorDecision(
base_revision=snapshot.revision,
new_tasks=(
TaskProposal(
task_id,
kind,
target,
f"Execute {task_id}.",
f"Evidence for {task_id} is recorded.",
basis_ids=basis_ids,
depends_on=depends_on,
),
),
next_task_id=task_id,
finish=False,
summary=f"Select {task_id}.",
),
snapshot,
)
)
assert commit.lease is not None
return memory.commit_execution(
ValidExecution(
run_id=snapshot.run_id,
task_id=task_id,
attempt_id=commit.lease.attempt_id,
lease_revision=commit.lease.revision,
trace_episode_id=f"executor-{task_id}",
outcome=ExecutionOutcome.DONE,
summary=f"Completed {task_id}.",
observation=evidence,
evidence_sequences=(1,),
)
)
def test_valid_supervisor_decision_atomically_adds_and_leases_one_task(
tmp_path: Path,
) -> None:
memory = MemoryKernel(tmp_path / "state.sqlite3")
snapshot = memory.create_run(
RunSpec(
run_id="run-1",
goal="Assess the authorized target.",
allowed_targets=("http://127.0.0.1:8080",),
)
)
decision = SupervisorDecision(
base_revision=snapshot.revision,
new_tasks=(
TaskProposal(
id="discover-http",
kind=TaskKind.DISCOVER,
target="http://127.0.0.1:8080",
objective="Inspect the HTTP service and identify its exposed surface.",
done_when="The response, technology clues, and reachable paths are recorded.",
),
),
next_task_id="discover-http",
finish=False,
summary="Start with bounded HTTP discovery.",
)
commit = memory.commit_plan(compile_plan(decision, snapshot))
recovered = MemoryKernel(tmp_path / "state.sqlite3").snapshot("run-1")
assert commit.revision == 1
assert commit.lease is not None
assert commit.lease.task_id == "discover-http"
assert recovered.revision == 1
assert len(recovered.tasks) == 1
assert recovered.tasks[0].status is TaskStatus.ACTIVE
def test_discovered_url_path_is_inside_its_authorized_origin(tmp_path: Path) -> None:
memory = MemoryKernel(tmp_path / "state.sqlite3")
snapshot = memory.create_run(
RunSpec(
run_id="run-1",
goal="Assess the authorized local application.",
allowed_targets=("http://127.0.0.1:51153",),
)
)
decision = SupervisorDecision(
base_revision=0,
new_tasks=(
TaskProposal(
"enumerate-apply",
TaskKind.ENUMERATE,
"http://127.0.0.1:51153/apply",
"Enumerate the discovered application form.",
"The form surface is recorded.",
),
),
next_task_id="enumerate-apply",
finish=False,
summary="Follow the discovered same-origin path.",
)
compiled = compile_plan(decision, snapshot)
assert compiled.new_tasks[0].target == "http://127.0.0.1:51153/apply"
@pytest.mark.parametrize(
"target",
[
"http://127.0.0.1:51154/apply",
"http://127.0.0.1:51153@outside.example/apply",
],
)
def test_url_scope_rejects_targets_outside_the_authorized_origin(
tmp_path: Path,
target: str,
) -> None:
snapshot = MemoryKernel(tmp_path / "state.sqlite3").create_run(
RunSpec(
run_id="run-1",
goal="Assess the authorized local application.",
allowed_targets=("http://127.0.0.1:51153",),
)
)
decision = SupervisorDecision(
base_revision=0,
new_tasks=(
TaskProposal(
"outside",
TaskKind.DISCOVER,
target,
"Inspect a target.",
"The response is recorded.",
),
),
next_task_id="outside",
finish=False,
summary="Inspect a target.",
)
with pytest.raises(PlanValidationError, match="outside scope"):
compile_plan(decision, snapshot)
@pytest.mark.parametrize(
"target",
[
"http://127.0.0.1:51153/application",
"http://127.0.0.1:51153/app/../admin",
"http://127.0.0.1:51153/app/%2e%2e/admin",
"http://127.0.0.1:51153/app/%252e%252e/admin",
"http://127.0.0.1:51153/app%2f..%2fadmin",
"http://127.0.0.1:51153/app#outside",
],
)
def test_url_subtree_scope_rejects_prefix_and_ambiguous_path_escapes(
tmp_path: Path,
target: str,
) -> None:
snapshot = MemoryKernel(tmp_path / "state.sqlite3").create_run(
RunSpec(
run_id="run-1",
goal="Assess the authorized application subtree.",
allowed_targets=("http://127.0.0.1:51153/app",),
)
)
decision = SupervisorDecision(
base_revision=0,
new_tasks=(
TaskProposal(
"candidate",
TaskKind.DISCOVER,
target,
"Inspect a target.",
"The response is recorded.",
),
),
next_task_id="candidate",
finish=False,
summary="Inspect a target.",
)
with pytest.raises(PlanValidationError, match="outside scope"):
compile_plan(decision, snapshot)
def test_supervisor_cannot_create_a_speculative_task_backlog(tmp_path: Path) -> None:
memory = MemoryKernel(tmp_path / "state.sqlite3")
snapshot = memory.create_run(
RunSpec(
run_id="run-1",
goal="Assess the authorized target.",
allowed_targets=("http://127.0.0.1:8080",),
)
)
target = "http://127.0.0.1:8080"
decision = SupervisorDecision(
base_revision=0,
new_tasks=(
TaskProposal("first", TaskKind.DISCOVER, target, "Inspect HTTP.", "HTTP is recorded."),
TaskProposal("second", TaskKind.TEST, target, "Test HTTP.", "HTTP is tested."),
),
next_task_id="first",
finish=False,
summary="Create a broad backlog.",
)
with pytest.raises(PlanValidationError, match="at most one new task"):
compile_plan(decision, snapshot)
assert memory.snapshot("run-1") == snapshot
def test_new_task_must_be_selected_immediately_instead_of_deferred(tmp_path: Path) -> None:
memory = MemoryKernel(tmp_path / "state.sqlite3")
initial = memory.create_run(
RunSpec(
run_id="run-1",
goal="Assess the authorized target.",
allowed_targets=("http://127.0.0.1:8080",),
)
)
first = memory.commit_plan(
compile_plan(
SupervisorDecision(
base_revision=0,
new_tasks=(
TaskProposal(
"discover",
TaskKind.DISCOVER,
"http://127.0.0.1:8080",
"Inspect the target.",
"The surface is recorded.",
),
),
next_task_id="discover",
finish=False,
summary="Start discovery.",
),
initial,
)
)
assert first.lease is not None
progressed = memory.commit_execution(
ValidExecution(
run_id="run-1",
task_id="discover",
attempt_id=first.lease.attempt_id,
lease_revision=first.lease.revision,
trace_episode_id="executor-discover",
outcome=ExecutionOutcome.PROGRESS,
summary="One request remains.",
observation="partial surface",
evidence_sequences=(1,),
)
)
with pytest.raises(PlanValidationError, match="new task must be selected immediately"):
compile_plan(
SupervisorDecision(
base_revision=progressed.revision,
new_tasks=(
TaskProposal(
"test-input",
TaskKind.TEST,
"http://127.0.0.1:8080/input",
"Confirm the named hypothesis.",
"The behavior is recorded.",
depends_on=("discover",),
),
),
next_task_id="discover",
finish=False,
summary="Do not create deferred work.",
),
progressed,
)
def test_unknown_basis_observation_is_rejected(tmp_path: Path) -> None:
snapshot = MemoryKernel(tmp_path / "state.sqlite3").create_run(
RunSpec(
run_id="run-1",
goal="Assess the authorized target.",
allowed_targets=("http://127.0.0.1:8080",),
)
)
decision = SupervisorDecision(
base_revision=0,
new_tasks=(
TaskProposal(
"test-http",
TaskKind.TEST,
"http://127.0.0.1:8080",
"Test the discovered input.",
"The behavior is recorded.",
basis_ids=("fabricated:observation",),
),
),
next_task_id="test-http",
finish=False,
summary="Test unsupported evidence.",
)
with pytest.raises(PlanValidationError, match="unknown basis observations"):
compile_plan(decision, snapshot)
def test_basis_producer_must_be_a_dependency(tmp_path: Path) -> None:
memory = MemoryKernel(tmp_path / "state.sqlite3")
initial = memory.create_run(
RunSpec(
run_id="run-1",
goal="Assess the authorized target.",
allowed_targets=("http://127.0.0.1:8080",),
)
)
discovery = memory.commit_plan(
compile_plan(
SupervisorDecision(
base_revision=0,
new_tasks=(
TaskProposal(
"discover-http",
TaskKind.DISCOVER,
"http://127.0.0.1:8080",
"Inspect HTTP.",
"HTTP is recorded.",
),
),
next_task_id="discover-http",
finish=False,
summary="Discover HTTP.",
),
initial,
)
)
assert discovery.lease is not None
discovered = memory.commit_execution(
ValidExecution(
run_id="run-1",
task_id="discover-http",
attempt_id=discovery.lease.attempt_id,
lease_revision=discovery.lease.revision,
trace_episode_id="executor-discover",
outcome=ExecutionOutcome.DONE,
summary="HTTP discovered.",
observation="200 OK",
evidence_sequences=(1,),
)
)
observation_id = discovered.observations[0].id
decision = SupervisorDecision(
base_revision=discovered.revision,
new_tasks=(
TaskProposal(
"test-http",
TaskKind.TEST,
"http://127.0.0.1:8080",
"Test the discovered input.",
"The behavior is recorded.",
basis_ids=(observation_id,),
depends_on=(),
),
),
next_task_id="test-http",
finish=False,
summary="Test HTTP.",
)
with pytest.raises(PlanValidationError, match="basis-producing tasks missing"):
compile_plan(decision, discovered)
def test_exploit_requires_the_latest_test_evidence_on_the_same_surface(
tmp_path: Path,
) -> None:
memory = MemoryKernel(tmp_path / "state.sqlite3")
target = "http://127.0.0.1:8080/submit"
initial = memory.create_run(
RunSpec(
run_id="run-1",
goal="Capture the flag.",
allowed_targets=("http://127.0.0.1:8080",),
)
)
reflected = _complete_task(
memory,
initial,
task_id="test-reflection",
kind=TaskKind.TEST,
target=target,
evidence="Thank you MARKER",
)
reflection_observation = reflected.observations[-1]
confirmed = _complete_task(
memory,
reflected,
task_id="test-ssti",
kind=TaskKind.TEST,
target=target,
evidence="Thank you 49",
basis_ids=(reflection_observation.id,),
depends_on=("test-reflection",),
)
ssti_observation = confirmed.observations[-1]
stale_decision = SupervisorDecision(
base_revision=confirmed.revision,
new_tasks=(
TaskProposal(
"exploit-ssti",
TaskKind.EXPLOIT,
target,
"Use the confirmed SSTI to capture the flag.",
"The exact flag is recorded.",
basis_ids=(reflection_observation.id,),
depends_on=("test-reflection",),
),
),
next_task_id="exploit-ssti",
finish=False,
summary="Exploit stale reflection evidence.",
)
with pytest.raises(PlanValidationError, match="must cite latest completed test observation"):
compile_plan(stale_decision, confirmed)
current_decision = SupervisorDecision(
base_revision=confirmed.revision,
new_tasks=(
TaskProposal(
"exploit-ssti",
TaskKind.EXPLOIT,
target,
"Use the confirmed SSTI to capture the flag.",
"The exact flag is recorded.",
basis_ids=(ssti_observation.id,),
depends_on=("test-ssti",),
),
),
next_task_id="exploit-ssti",
finish=False,
summary="Exploit current SSTI evidence.",
)
compiled = compile_plan(current_decision, confirmed)
assert compiled.new_tasks[0].basis_ids == (ssti_observation.id,)
def test_exploit_provenance_requires_the_exact_test_target_including_query(
tmp_path: Path,
) -> None:
memory = MemoryKernel(tmp_path / "state.sqlite3")
initial = memory.create_run(
RunSpec(
run_id="run-1",
goal="Capture the flag.",
allowed_targets=("http://127.0.0.1:8080",),
)
)
tested = _complete_task(
memory,
initial,
task_id="test-mode",
kind=TaskKind.TEST,
target="http://127.0.0.1:8080/submit?mode=test",
evidence="template result: 49",
)
observation = tested.observations[-1]
with pytest.raises(PlanValidationError, match="completed test basis"):
compile_plan(
SupervisorDecision(
base_revision=tested.revision,
new_tasks=(
TaskProposal(
"exploit-mode",
TaskKind.EXPLOIT,
"http://127.0.0.1:8080/submit?mode=admin",
"Use the confirmed primitive.",
"The goal artifact is recorded.",
basis_ids=(observation.id,),
depends_on=("test-mode",),
),
),
next_task_id="exploit-mode",
finish=False,
summary="Do not cross target queries.",
),
tested,
)
def test_supervisor_cannot_finish_while_work_is_open(tmp_path: Path) -> None:
memory = MemoryKernel(tmp_path / "state.sqlite3")
initial = memory.create_run(
RunSpec(
run_id="run-1",
goal="Assess the authorized target.",
allowed_targets=("http://127.0.0.1:8080",),
)
)
target = "http://127.0.0.1:8080"
plan = compile_plan(
SupervisorDecision(
base_revision=0,
new_tasks=(
TaskProposal(
"discover-http",
TaskKind.DISCOVER,
target,
"Inspect HTTP.",
"HTTP is recorded.",
),
),
next_task_id="discover-http",
finish=False,
summary="Inspect the target.",
),
initial,
)
memory.commit_plan(plan)
active = memory.snapshot("run-1")
with pytest.raises(PlanValidationError, match="open tasks"):
compile_plan(
SupervisorDecision(
base_revision=active.revision,
new_tasks=(),
next_task_id=None,
finish=True,
summary="Finish prematurely.",
),
active,
)
def test_finish_requires_an_explicit_existing_canonical_basis(tmp_path: Path) -> None:
memory = MemoryKernel(tmp_path / "state.sqlite3")
initial = memory.create_run(
RunSpec(
run_id="run-1",
goal="Assess the authorized target.",
allowed_targets=("http://127.0.0.1:8080",),
)
)
completed = _complete_task(
memory,
initial,
task_id="discover-http",
kind=TaskKind.DISCOVER,
target="http://127.0.0.1:8080",
evidence="200 OK",
)
observation_id = completed.observations[0].id
with pytest.raises(PlanValidationError, match="finish requires canonical basis"):
compile_plan(
SupervisorDecision(
base_revision=completed.revision,
new_tasks=(),
next_task_id=None,
finish=True,
summary="Finish without naming evidence.",
),
completed,
)
with pytest.raises(PlanValidationError, match="unknown finish basis"):
compile_plan(
SupervisorDecision(
base_revision=completed.revision,
new_tasks=(),
next_task_id=None,
finish=True,
summary="Finish with fabricated evidence.",
finish_basis_ids=("fabricated:observation",),
),
completed,
)
compiled = compile_plan(
SupervisorDecision(
base_revision=completed.revision,
new_tasks=(),
next_task_id=None,
finish=True,
summary="Finish from the recorded response.",
finish_basis_ids=(observation_id,),
),
completed,
)
assert compiled.finish_basis_ids == (observation_id,)
def test_finish_basis_must_come_from_a_completed_task(tmp_path: Path) -> None:
memory = MemoryKernel(tmp_path / "state.sqlite3")
initial = memory.create_run(
RunSpec(
run_id="run-1",
goal="Assess the authorized target.",
allowed_targets=("http://127.0.0.1:8080",),
max_attempts_per_task=1,
)
)
commit = memory.commit_plan(
compile_plan(
SupervisorDecision(
base_revision=0,
new_tasks=(
TaskProposal(
"test-input",
TaskKind.TEST,
"http://127.0.0.1:8080",
"Confirm whether the input transforms data.",
"The behavior is recorded.",
),
),
next_task_id="test-input",
finish=False,
summary="Test the input.",
),
initial,
)
)
assert commit.lease is not None
exhausted = memory.commit_execution(
ValidExecution(
run_id="run-1",
task_id="test-input",
attempt_id=commit.lease.attempt_id,
lease_revision=commit.lease.revision,
trace_episode_id="executor-test",
outcome=ExecutionOutcome.PROGRESS,
summary="The premise remains unresolved.",
observation="partial response",
evidence_sequences=(1,),
)
)
assert exhausted.tasks[0].status is TaskStatus.FAILED
with pytest.raises(PlanValidationError, match="completed tasks"):
compile_plan(
SupervisorDecision(
base_revision=exhausted.revision,
new_tasks=(),
next_task_id=None,
finish=True,
summary="Do not finish from partial progress.",
finish_basis_ids=(exhausted.observations[0].id,),
),
exhausted,
)
@pytest.mark.parametrize(
"objective",
[
"Confirm injection, then read the flag.",
"Test the parameter and capture the flag if it works.",
"Probe the input and dump credentials from the server.",
"Try the hypothesis and gain a reverse shell.",
],
)
def test_test_task_cannot_embed_exploitation_as_its_objective(
tmp_path: Path,
objective: str,
) -> None:
snapshot = MemoryKernel(tmp_path / "state.sqlite3").create_run(
RunSpec(
run_id="run-1",
goal="Capture the benchmark flag.",
allowed_targets=("http://127.0.0.1:8080",),
)
)
with pytest.raises(PlanValidationError, match="TEST task crosses into exploitation"):
compile_plan(
SupervisorDecision(
base_revision=0,
new_tasks=(
TaskProposal(
"test-input",
TaskKind.TEST,
"http://127.0.0.1:8080/check",
objective,
"The injection behavior is recorded.",
),
),
next_task_id="test-input",
finish=False,
summary="Test the input.",
),
snapshot,
)
def test_plan_text_and_reference_collections_are_bounded(tmp_path: Path) -> None:
snapshot = MemoryKernel(tmp_path / "state.sqlite3").create_run(
RunSpec(
run_id="run-1",
goal="Assess the authorized target.",
allowed_targets=("http://127.0.0.1:8080",),
)
)
oversized_objective = SupervisorDecision(
base_revision=0,
new_tasks=(
TaskProposal(
"discover",
TaskKind.DISCOVER,
"http://127.0.0.1:8080",
"x" * 2_001,
"The surface is recorded.",
),
),
next_task_id="discover",
finish=False,
summary="Begin discovery.",
)
too_many_references = SupervisorDecision(
base_revision=0,
new_tasks=(
TaskProposal(
"discover",
TaskKind.DISCOVER,
"http://127.0.0.1:8080",
"Inspect the target.",
"The surface is recorded.",
basis_ids=tuple(f"obs-{index}" for index in range(9)),
),
),
next_task_id="discover",
finish=False,
summary="Begin discovery.",
)
with pytest.raises(PlanValidationError, match="objective exceeds"):
compile_plan(oversized_objective, snapshot)
with pytest.raises(PlanValidationError, match="at most 8 basis"):
compile_plan(too_many_references, snapshot)