|
9 | 9 |
|
10 | 10 | import pytest |
11 | 11 |
|
12 | | -from canonical_authority_fixture import initialize_canonical_authority, isolate_sqlite_runtime |
| 12 | +from canonical_authority_fixture import initialize_canonical_authority, isolate_sqlite_runtime, promoted_create_fixture |
13 | 13 | from loopx.control_plane.coordination.runtime_shadow import build_todo_runtime_shadow_projection |
14 | 14 | from loopx.control_plane.coordination.local_authority import read_canonical_todos_if_promoted |
15 | 15 | from loopx.control_plane.todos.markdown import render_todo_markdown |
| 16 | +from loopx.control_plane.todos.completion_validation_store import read_completion_validation_declaration |
| 17 | +from loopx.todos import add_goal_todo |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.parametrize("provider", ["file", "sqlite"]) |
| 21 | +def test_validator_revision_uses_current_claim_through_public_cli(tmp_path, monkeypatch, provider): |
| 22 | + isolate_sqlite_runtime(tmp_path, monkeypatch) |
| 23 | + registry, runtime, state = promoted_create_fixture( |
| 24 | + tmp_path, provider=provider, handoff_mode="hard_lease", |
| 25 | + registered_agents=["agent-a", "agent-b"], |
| 26 | + ) |
| 27 | + created = add_goal_todo( |
| 28 | + registry_path=registry, goal_id="goal-a", role="agent", agent_id="agent-a", |
| 29 | + text="Validate the delivered behavior", claimed_by="agent-a", |
| 30 | + validation_command_json=json.dumps([sys.executable, "-c", "raise SystemExit(4)"]), |
| 31 | + ) |
| 32 | + todo_id = created["todo_id"] |
| 33 | + state.unlink() # The canonical provider must own revision and claim decisions. |
| 34 | + |
| 35 | + def cli(*args, ok=True): |
| 36 | + process = subprocess.run( |
| 37 | + [sys.executable, "-m", "loopx.cli", "--format", "json", "--registry", str(registry), |
| 38 | + "--runtime-root", str(runtime), *args, "--goal-id", "goal-a", "--todo-id", todo_id], |
| 39 | + capture_output=True, text=True, timeout=45, |
| 40 | + ) |
| 41 | + assert (process.returncode == 0) is ok, process.stdout + process.stderr |
| 42 | + return json.loads(process.stdout) |
| 43 | + |
| 44 | + def snapshot(): |
| 45 | + return read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="goal-a", include_leases=True) |
| 46 | + |
| 47 | + claim = cli("todo", "claim", "--agent-id", "agent-a", "--claimed-by", "agent-a", |
| 48 | + "--claim-operation-id", "validator-execution") |
| 49 | + proof = ("--claim-operation-id", claim["claim_operation_id"]) |
| 50 | + before = snapshot() |
| 51 | + old_digest = before["todos"][0]["completion_validation_sha256"] |
| 52 | + marker = tmp_path / "validator-executed" |
| 53 | + argv = [sys.executable, "-c", f"from pathlib import Path; Path({str(marker)!r}).touch(); raise SystemExit(4)"] |
| 54 | + |
| 55 | + def revision(operation, provider_revision, *, actor="agent-a", extra=(), ok=True): |
| 56 | + return cli("todo", "update", "--agent-id", actor, "--update-operation-id", operation, |
| 57 | + "--update-expected-provider-revision", provider_revision, |
| 58 | + "--validation-command-json", json.dumps(argv), *extra, ok=ok) |
| 59 | + |
| 60 | + # Proof is required independently of the validator's provider CAS witness. |
| 61 | + missing = revision("missing-proof", before["provider_revision"], ok=False) |
| 62 | + assert missing["error_code"] == "lease_fence_required" |
| 63 | + assert snapshot() == before |
| 64 | + preview = revision("revise-validator", before["provider_revision"], extra=(*proof, "--dry-run")) |
| 65 | + assert preview["status"] == "planned" |
| 66 | + assert snapshot() == before |
| 67 | + assert not marker.exists() |
| 68 | + assert revision("revise-validator", before["provider_revision"], extra=proof)["status"] == "applied" |
| 69 | + updated = snapshot() |
| 70 | + todo = updated["todos"][0] |
| 71 | + assert todo["status"] == "open" and todo["completion_validation_required"] is True |
| 72 | + assert todo["completion_validation_sha256"] != old_digest |
| 73 | + history = todo["completion_validation_revision_history"] |
| 74 | + assert len(history) == 1 |
| 75 | + assert history[0]["previous_declaration_sha256"] == old_digest |
| 76 | + assert history[0]["declaration_sha256"] == todo["completion_validation_sha256"] |
| 77 | + assert history[0]["actor_agent_id"] == "agent-a" |
| 78 | + stored = read_completion_validation_declaration(runtime_root=runtime, goal_id="goal-a", todo_id=todo_id) |
| 79 | + assert stored["validation_command_argv"] == argv |
| 80 | + assert updated["leases"] == before["leases"] |
| 81 | + assert revision("revise-validator", before["provider_revision"], extra=proof)["status"] == "replayed" |
| 82 | + assert snapshot() == updated |
| 83 | + assert not marker.exists(), "revision, preview and replay must not run the validator" |
| 84 | + |
| 85 | + for operation, provider_revision, actor, error in [ |
| 86 | + ("stale-revision", before["provider_revision"], "agent-a", "provider_revision_mismatch"), |
| 87 | + ("foreign-claim", updated["provider_revision"], "agent-b", "claim_execution_unavailable"), |
| 88 | + ]: |
| 89 | + rejected = revision(operation, provider_revision, actor=actor, extra=proof, ok=False) |
| 90 | + assert rejected["error_code"] == error |
| 91 | + assert snapshot() == updated |
| 92 | + assert read_completion_validation_declaration(runtime_root=runtime, goal_id="goal-a", todo_id=todo_id) == stored |
| 93 | + |
| 94 | + failed = cli("todo", "complete", "--agent-id", "agent-a", *proof, "--evidence", "evidence:checked", ok=False) |
| 95 | + assert failed["validation_blocked_completion"] is True |
| 96 | + assert failed["validation_failure"]["validation_receipt"]["validation_declaration_sha256"] == todo["completion_validation_sha256"] |
| 97 | + assert marker.exists() and snapshot()["todos"][0]["status"] == "open" |
| 98 | + |
| 99 | + lease = cli("task-lease", "inspect")["lease"] |
| 100 | + cli("task-lease", "release", "--owner", "agent-a", "--idempotency-key", lease["idempotency_key"], |
| 101 | + "--expected-version", str(lease["version"])) |
| 102 | + retired = snapshot() |
| 103 | + assert revision("released-claim", retired["provider_revision"], extra=proof, ok=False)["error_code"] == "claim_execution_unavailable" |
| 104 | + assert snapshot() == retired |
| 105 | + cli("todo", "claim", "--agent-id", "agent-a", "--claimed-by", "agent-a", "--claim-operation-id", "next-execution") |
| 106 | + current = snapshot() |
| 107 | + assert revision("old-generation", current["provider_revision"], extra=proof, ok=False)["error_code"] == "claim_execution_unavailable" |
| 108 | + assert snapshot() == current |
| 109 | + # Existing explicit proof remains a legitimate transport for the same fence. |
| 110 | + lease = cli("task-lease", "inspect")["lease"] |
| 111 | + explicit = ("--task-lease-idempotency-key", lease["idempotency_key"], "--task-lease-expected-version", str(lease["version"])) |
| 112 | + argv = [sys.executable, "-c", "raise SystemExit(0)"] |
| 113 | + assert revision("explicit-proof", current["provider_revision"], extra=explicit)["status"] == "applied" |
| 114 | + completed = cli("todo", "complete", "--agent-id", "agent-a", "--claim-operation-id", "next-execution", |
| 115 | + "--evidence", "evidence:validated") |
| 116 | + assert completed["validation_receipt"]["passed"] is True |
| 117 | + assert snapshot()["todos"][0]["status"] == "done" |
| 118 | + assert cli("task-lease", "inspect")["lease"]["status"] == "released" |
16 | 119 |
|
17 | 120 |
|
18 | 121 | @pytest.mark.parametrize("provider", ["file", "sqlite"]) |
|
0 commit comments