From 4f2c4628d97c48ee2dfaa84f29b49e896b165d4a Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 23 Nov 2026 08:02:57 -0600 Subject: [PATCH] fix(opencode-claimant): re-audit verify_implementation.sh against the split implementation (#13254) The in-directory verifier still asserted the pre-split monolith's shape and failed on its first file check. Every expectation is now asserted against the mechanism that owns it: step functions in claimant.py, selection and timing constants in poll.py, per-task launch in launch_one.py. Stale file expectations updated to their replacements (SLICE1_IMPLEMENTATION_SUMMARY.md, SLICE1_COMPLETION_REPORT.md, run_hermetic_tests.py); the repo-queue dependency check now tests the path the code actually uses ($HOME/.local/bin/repo-queue) with an expandable $HOME instead of a quoted tilde that could never match. Checks are counted (check_passed/check_failed, per PR #13253's shape) so the summary is derived, not hardcoded. Incidental fix in the same pass: TestCodeSourceFireLog built a real git clone, which the hermetic runner forbids (subprocess ban), so the canonical suite reported 2 errors on main. The tests now fake git at the subprocess boundary; the asserted contracts are unchanged. The runner's separate violation gate still trips on pre-existing caught attempts in other modules -- filed as #13344; the verifier's test step asserts the suite's test verdict and says so. Verification: verifier runs 43/43 green, exit 0; mutation check (CONCURRENCY_CAP 36 -> 35 in a scratch copy) fails with exit 1. Hermetic suite: 226 tests, OK. --- pm-stack/opencode-claimant/test_claimant.py | 70 +++--- .../verify_implementation.sh | 231 ++++++++++++------ 2 files changed, 188 insertions(+), 113 deletions(-) diff --git a/pm-stack/opencode-claimant/test_claimant.py b/pm-stack/opencode-claimant/test_claimant.py index b9d6435a44..cb2f54a281 100644 --- a/pm-stack/opencode-claimant/test_claimant.py +++ b/pm-stack/opencode-claimant/test_claimant.py @@ -1718,43 +1718,34 @@ def setUp(self): def tearDown(self): claimant._code_source = None - def _make_clone(self, tmp: str) -> Path: - clone = Path(tmp) / "clone" - clone.mkdir() - env = dict(os.environ) - env.update( - { - "GIT_AUTHOR_NAME": "test", - "GIT_AUTHOR_EMAIL": "test@example.invalid", - "GIT_COMMITTER_NAME": "test", - "GIT_COMMITTER_EMAIL": "test@example.invalid", - } - ) - subprocess.run( - ["git", "init", "-b", "main", str(clone)], - check=True, - capture_output=True, - env=env, - ) - (clone / "f.txt").write_text("x") - subprocess.run( - ["git", "-C", str(clone), "add", "f.txt"], - check=True, - capture_output=True, - env=env, - ) - subprocess.run( - ["git", "-C", str(clone), "commit", "-m", "init"], - check=True, - capture_output=True, - env=env, - ) - return clone + # These tests fake git at the subprocess boundary instead of + # building a real clone: the hermetic suite (run_hermetic_tests.py) + # forbids subprocess outright, so a real `git init` errors there. + # read_code_source() treats every git failure uniformly, so the + # contracts asserted here (ok read, fail-open on error) are the + # same ones a real clone would exercise. + def _fake_git( + self, + branch="main", + commit="0123456789abcdef0123456789abcdef01234567", + returncode=0, + ): + def run(argv, **kwargs): + stdout = "" + if returncode == 0: + stdout = (branch if "--abbrev-ref" in argv else commit) + "\n" + return subprocess.CompletedProcess(argv, returncode, stdout=stdout) + + return run def test_reads_branch_and_commit(self): with tempfile.TemporaryDirectory() as tmp: - clone = self._make_clone(tmp) - with patch.object(claimant, "PRIMARY_CLONE", clone): + clone = Path(tmp) / "clone" + clone.mkdir() + with ( + patch.object(claimant, "PRIMARY_CLONE", clone), + patch.object(claimant.subprocess, "run", self._fake_git()), + ): source = claimant.read_code_source() self.assertEqual(source["state"], "ok") self.assertEqual(source["branch"], "main") @@ -1764,7 +1755,12 @@ def test_unreadable_clone_fails_open(self): with tempfile.TemporaryDirectory() as tmp: not_a_repo = Path(tmp) / "nope" not_a_repo.mkdir() - with patch.object(claimant, "PRIMARY_CLONE", not_a_repo): + with ( + patch.object(claimant, "PRIMARY_CLONE", not_a_repo), + patch.object( + claimant.subprocess, "run", self._fake_git(returncode=1) + ), + ): source = claimant.read_code_source() self.assertEqual(source["state"], "unreadable") self.assertIsNone(source["branch"]) @@ -1772,13 +1768,15 @@ def test_unreadable_clone_fails_open(self): def test_fire_entry_carries_code_source(self): with tempfile.TemporaryDirectory() as tmp: - clone = self._make_clone(tmp) + clone = Path(tmp) / "clone" + clone.mkdir() state_dir = Path(tmp) / "state" fires = state_dir / "fires.jsonl" with ( patch.object(claimant, "PRIMARY_CLONE", clone), patch.object(claimant, "STATE_DIR", state_dir), patch.object(claimant, "FIRES_FILE", fires), + patch.object(claimant.subprocess, "run", self._fake_git()), ): claimant.log_fire("live", [], [], 3, []) entries = [ diff --git a/pm-stack/opencode-claimant/verify_implementation.sh b/pm-stack/opencode-claimant/verify_implementation.sh index 3dd2baa298..5e7f49006f 100755 --- a/pm-stack/opencode-claimant/verify_implementation.sh +++ b/pm-stack/opencode-claimant/verify_implementation.sh @@ -1,24 +1,55 @@ #!/bin/bash -# Verification script for OpenCode lane claimant/executor slice 1 implementation +# Verification script for the OpenCode lane claimant/executor slice 1 +# implementation (#3026), run against the in-directory sources. +# +# Expectations re-audited against the split implementation (#13254): +# the pre-split monolith's shape is gone -- claimant.py holds the step +# functions, poll.py owns selection, the timing constants and the +# ledger read, and launch_one.py owns per-task launch. Each check +# below asserts the mechanism in the file that owns it now (the same +# treatment PR #13253 gave the repo-root verify_slice1_implementation.sh). +# +# Run from anywhere: the script cds to its own directory first. +# The unit-test step runs the hermetic suite (run_hermetic_tests.py), +# which stages its own disposable HOME, so no live state is touched. set -e +cd "$(dirname "$0")" + echo "==========================================" echo "OpenCode Claimant/Executor Verification" echo "==========================================" echo -# Check files exist +success_count=0 +total_checks=0 + +check_passed() { + echo "✓ $1" + success_count=$((success_count + 1)) + total_checks=$((total_checks + 1)) +} + +check_failed() { + echo "✗ $1" + exit 1 +} + +# Check files exist. The slice-1 record files were renamed with the +# SLICE1_ prefix, CHANGES.md's role is served by SLICE1_COMPLETION_REPORT.md, +# and test_implementation.py was replaced by the hermetic suite runner. echo "Checking required files..." FILES=( "claimant.py" "poll.py" + "launch_one.py" "README.md" - "IMPLEMENTATION_SUMMARY.md" - "test_implementation.py" + "SLICE1_IMPLEMENTATION_SUMMARY.md" + "SLICE1_COMPLETION_REPORT.md" + "run_hermetic_tests.py" "Makefile" - "CHANGES.md" "migration-0021.sql" "migration-0022.sql" "migration-0023.sql" @@ -28,65 +59,68 @@ FILES=( for file in "${FILES[@]}"; do if [ -f "$file" ]; then - echo "✓ $file" + check_passed "$file" else - echo "✗ $file MISSING" - exit 1 + check_failed "$file MISSING" fi done # Check executables if [ -x "claimant.py" ]; then - echo "✓ claimant.py is executable" + check_passed "claimant.py is executable" else - echo "✗ claimant.py is not executable" - exit 1 + check_failed "claimant.py is not executable" fi if [ -x "poll.py" ]; then - echo "✓ poll.py is executable" + check_passed "poll.py is executable" else - echo "✗ poll.py is not executable" - exit 1 + check_failed "poll.py is not executable" fi -# Check constants in claimant.py +# Check constants. The cap is declared in both claimant.py and +# poll.py (CAP = 36, aliased as CONCURRENCY_CAP); the active-window +# timing constant moved to poll.py with the stuck-bound selection. echo echo "Checking constants..." -if grep -q "CONCURRENCY_CAP = 36" claimant.py; then - echo "✓ CONCURRENCY_CAP = 36" +if grep -q "CONCURRENCY_CAP = 36" claimant.py && + grep -q "CAP = 36" poll.py && + grep -q "CONCURRENCY_CAP = CAP" poll.py; then + check_passed "CONCURRENCY_CAP = 36 (claimant.py and poll.py CAP)" else - echo "✗ CONCURRENCY_CAP not set correctly" - exit 1 + check_failed "CONCURRENCY_CAP not set correctly" fi -if grep -q "LAUNCHED_BY_RECOVERY_ZERO_MESSAGE" claimant.py; then - echo "✓ Zero-message recovery constant defined" +if grep -q "ACTIVE_WINDOW_MS = 30 \* 60 \* 1000" poll.py; then + check_passed "ACTIVE_WINDOW_MS = 30 * 60 * 1000 (poll.py)" else - echo "✗ Zero-message recovery constant missing" - exit 1 + check_failed "ACTIVE_WINDOW_MS not found or incorrect in poll.py" fi -if grep -q "LAUNCHED_BY_RECOVERY_GONE_SESSION" claimant.py; then - echo "✓ Gone-session recovery constant defined" +if grep -q 'LAUNCHED_BY_RECOVERY_ZERO_MESSAGE = "opencode-claimant-recovery:zero-message-deleted"' claimant.py; then + check_passed "Zero-message recovery constant defined" else - echo "✗ Gone-session recovery constant missing" - exit 1 + check_failed "Zero-message recovery constant missing" +fi + +if grep -q 'LAUNCHED_BY_RECOVERY_GONE_SESSION = "opencode-claimant-recovery:gone-session"' claimant.py; then + check_passed "Gone-session recovery constant defined" +else + check_failed "Gone-session recovery constant missing" fi # Check queues echo echo "Checking queues..." -if grep -q "repo-health-code-pilot" claimant.py && - grep -q "repo-health-opencode-pilot" claimant.py && - grep -q "repo-health-openchamber-pilot" claimant.py && - grep -q "repo-health-nix-pilot" claimant.py; then - echo "✓ All 4 queues defined" +if grep -q '"repo-health-code-pilot"' claimant.py && + grep -q '"repo-health-opencode-pilot"' claimant.py && + grep -q '"repo-health-openchamber-pilot"' claimant.py && + grep -q '"repo-health-nix-pilot"' claimant.py; then + check_passed "All 4 queues defined" else - echo "✗ Some queues missing" - exit 1 + check_failed "Some queues missing" fi # Check hard rules in README @@ -105,14 +139,13 @@ HARD_RULES=( for rule in "${HARD_RULES[@]}"; do if grep -q "$rule" README.md; then - echo "✓ $rule documented" + check_passed "$rule documented" else - echo "✗ $rule not documented" - exit 1 + check_failed "$rule not documented" fi done -# Check execution steps +# Check execution steps (claimant.py step functions) echo echo "Checking execution steps..." @@ -121,15 +154,42 @@ STEPS=( "gate_step" "cap_step" "launch_step" - "log_fire" ) for step in "${STEPS[@]}"; do if grep -q "def $step" claimant.py; then - echo "✓ Step: $step" + check_passed "Step: $step" + else + check_failed "Step: $step missing" + fi +done + +# The per-task launcher split out of claimant.py into launch_one.py; +# its launch() is the per-task create/bind/prompt path. +if grep -q "def launch(" launch_one.py; then + check_passed "launch_one.py launch() function exists" +else + check_failed "launch_one.py launch() function missing" +fi + +# Selection lives in poll.py (candidate discovery and stuck-bound +# classification over the ledger read). +echo +echo "Checking poll.py selection..." + +POLL_FUNCS=( + "is_candidate" + "is_stuck_bound" + "is_active_root" + "count_active_roots" + "get_candidates" +) + +for func in "${POLL_FUNCS[@]}"; do + if grep -q "def $func" poll.py; then + check_passed "poll.py: $func" else - echo "✗ Step: $step missing" - exit 1 + check_failed "poll.py: $func missing" fi done @@ -140,10 +200,9 @@ echo "Checking recovery rules..." if grep -q "Zero-Message Recovery" README.md && grep -q "Gone-Session Recovery" README.md && grep -q "Exhausted Attempts" README.md; then - echo "✓ Recovery rules documented" + check_passed "Recovery rules documented" else - echo "✗ Recovery rules not documented" - exit 1 + check_failed "Recovery rules not documented" fi # Check mode file handling @@ -151,17 +210,15 @@ echo echo "Checking mode file handling..." if grep -q "def read_mode" claimant.py; then - echo "✓ read_mode function exists" + check_passed "read_mode function exists" else - echo "✗ read_mode function missing" - exit 1 + check_failed "read_mode function missing" fi -if grep -q "shadow" claimant.py && grep -q "live" claimant.py; then - echo "✓ Mode handling implemented" +if grep -q "doesn't exist or is unreadable, defaults to \`shadow\`" README.md; then + check_passed "Mode handling documented (defaults to shadow)" else - echo "✗ Mode handling incomplete" - exit 1 + check_failed "Mode handling documentation incomplete" fi # Check state directory creation @@ -169,10 +226,9 @@ echo echo "Checking state directory..." if grep -q "ensure_state_dir" claimant.py; then - echo "✓ State directory creation implemented" + check_passed "State directory creation implemented" else - echo "✗ State directory creation missing" - exit 1 + check_failed "State directory creation missing" fi # Check fire logging @@ -180,29 +236,39 @@ echo echo "Checking fire logging..." if grep -q "def log_fire" claimant.py; then - echo "✓ Fire logging function exists" + check_passed "Fire logging function exists" else - echo "✗ Fire logging function missing" - exit 1 + check_failed "Fire logging function missing" fi -# Run unit tests +# Run unit tests: the hermetic suite replaced test_implementation.py. +# This step asserts the suite's test verdict. The runner additionally +# keeps a violation gate (any attempted subprocess/socket I/O fails +# the run even when the code under test catches the attempt); that +# gate currently trips on pre-existing caught attempts in other +# modules, tracked as #13344, so the runner's combined exit code is +# not the test verdict and is not asserted here. echo echo "Running unit tests..." -if python test_implementation.py >/dev/null 2>&1; then - echo "✓ All unit tests pass" +test_output=$(python3 -I run_hermetic_tests.py 2>&1) || true +if printf '%s\n' "$test_output" | grep -qx "OK"; then + check_passed "All unit tests pass (run_hermetic_tests.py)" else echo "✗ Some unit tests failed" - python test_implementation.py + printf '%s\n' "$test_output" exit 1 fi -# Check dependencies +# Check dependencies. Advisory only: the claimant invokes the packaged +# fleet binary via its repo-queue argv0 alias (~/.local/bin/repo-queue, +# see REPO_QUEUE_CMD in claimant.py); it is a deployment property, not +# a source property. (The pre-split check tested "~/fleet-brokers/..." +# with a quoted tilde, which never expands, so it could never pass.) echo echo "Checking dependencies..." -if [ -f "~/fleet-brokers/bin/repo-queue" ]; then +if [ -f "$HOME/.local/bin/repo-queue" ]; then echo "✓ repo-queue CLI available" else echo "⚠ repo-queue CLI not found (expected in production)" @@ -210,18 +276,29 @@ fi echo echo "==========================================" -echo "✅ All verifications passed!" +echo "Verification Summary" echo "==========================================" +echo "Passed: $success_count / $total_checks checks" echo -echo "Implementation Summary:" -echo "- Slice 1 of #3026 complete" -echo "- All 6 execution steps implemented" -echo "- Recovery system (#4758) implemented" -echo "- Hard rules enforced" -echo "- 12/12 unit tests passing" -echo "- Documentation complete" -echo -echo "Ready for:" -echo "- Shadow mode testing (no mutations)" -echo "- Live mode testing (with OpenCode server)" -echo "- Integration with fleet scheduler" + +if [ $success_count -eq $total_checks ]; then + echo "✅ All verifications passed!" + echo + echo "Implementation Summary:" + echo "- Slice 1 of #3026 complete" + echo "- Execution steps implemented (claimant.py; per-task launch in launch_one.py)" + echo "- Selection and timing constants in poll.py" + echo "- Recovery system (#4758) implemented" + echo "- Hard rules enforced" + echo "- Hermetic unit suite passing (run_hermetic_tests.py)" + echo "- Documentation complete" + echo + echo "Ready for:" + echo "- Shadow mode testing (no mutations)" + echo "- Live mode testing (with OpenCode server)" + echo "- Integration with fleet scheduler" + exit 0 +else + echo "=== SOME CHECKS FAILED ===" + exit 1 +fi