"""Regression coverage for accountable dispatch and annotation objective outcomes.""" import copy import pytest from test_agent_cycle import m, verdict from test_agent_cycle import setup as legacy_setup setup = legacy_setup @pytest.fixture def v2(setup): c, plan, clock = setup plan["schema_version"] = m.SCHEMA_V2 plan["objective_contract"] = dict( deliverable="Approved annotation evidence for the bounded packet", min_admitted_units=1, required_checks=["artifact_verified", "regressions_resolved"], ) return c, plan, clock def add_unit(plan, name): plan["units"].append(dict(plan["units"][0], id=name)) def sent(c, identity, unit, stage, actor): claim = c.claim(identity, unit, stage, actor) c.confirm_dispatch(claim["token"], "root", actor, "Actual handoff receipt") return claim def finish(c, identity, unit="one", accepted=True): claim = sent(c, identity, unit, "annotate", "author") sha = c.complete(claim["token"], "author", dict(reason="Source inspected")) claim = sent(c, identity, unit, "review", "critic") c.complete( claim["token"], "critic", dict( reason="Source and proposal checked", proposal_sha256=sha, verdict="accept" if accepted else "reject", ), ) def outcome(**changes): result = dict( verdict(), objective_outcome="achieved", objective_checks=dict(artifact_verified=True, regressions_resolved=True), outcome_reason="Approved proposals checked against the frozen packet target", remaining_work=[], ) result.update(changes) return result def test_unconfirmed_handoff_blocks_new_work_and_receipt_is_fenced(v2): c, plan, _ = v2 add_unit(plan, "two") identity = c.begin(plan) first = c.claim(identity, "one", "annotate", "author") state = c.status(identity) assert state["unconfirmed_handoffs"][0]["token"] == first["token"] assert state["progress"]["proposed"] == 0 with pytest.raises(ValueError, match="handoff"): c.claim(identity, "two", "annotate", "second") with pytest.raises(ValueError, match="root"): c.confirm_dispatch(first["token"], "author", "executor/one", "sent") receipt = c.confirm_dispatch(first["token"], "root", "executor/one", "sent") assert c.confirm_dispatch(first["token"], "root", "executor/one", "sent") == receipt with pytest.raises(ValueError, match="Conflicting"): c.confirm_dispatch(first["token"], "root", "executor/other", "sent") assert c.status(identity)["unconfirmed_handoffs"] == [] assert c.claim(identity, "two", "annotate", "second")["token"] != first["token"] def test_fast_completion_before_root_receipt_is_not_rejected(v2): c, plan, _ = v2 identity = c.begin(plan) claim = c.claim(identity, "one", "annotate", "author") result = dict(reason="Executor finished before root recorded dispatch") sha = c.complete(claim["token"], "author", result) c.confirm_dispatch(claim["token"], "root", "executor/one", "actual message reference") assert c.complete(claim["token"], "author", result) == sha assert c.status(identity)["ready_tasks"] == [dict(unit="one", stage="review")] def test_unconfirmed_expired_handoff_never_releases_capacity(v2): c, plan, clock = v2 add_unit(plan, "two") identity = c.begin(plan) claim = c.claim(identity, "one", "annotate", "author") clock[0] = claim["expires"] assert c.status(identity)["progress"]["unreconciled"] == 1 with pytest.raises(ValueError, match="handoff"): c.claim(identity, "two", "annotate", "second") c.reconcile(claim["token"], "root", "Verified no running executor; explicit recovery") with pytest.raises(ValueError, match="superseded"): c.confirm_dispatch(claim["token"], "root", "executor/one", "late") assert c.claim(identity, "two", "annotate", "second") def test_review_streams_while_other_author_is_running(v2): c, plan, _ = v2 add_unit(plan, "two") identity = c.begin(plan) first = sent(c, identity, "one", "annotate", "author-a") sent(c, identity, "two", "annotate", "author-b") sha = c.complete(first["token"], "author-a", dict(reason="First packet ready")) with pytest.raises(ValueError, match="own"): c.claim(identity, "one", "review", "author-a") review = sent(c, identity, "one", "review", "critic") assert review["proposal_sha256"] == sha tasks = c.status(identity)["tasks"] assert any(t["unit"] == "two" and t["state"] == "active" for t in tasks) def test_ready_review_has_priority_over_new_author(v2): c, plan, _ = v2 add_unit(plan, "two") identity = c.begin(plan) first = sent(c, identity, "one", "annotate", "author") c.complete(first["token"], "author", dict(reason="Ready")) assert c.status(identity)["ready_tasks"][0] == dict(unit="one", stage="review") with pytest.raises(ValueError, match="Ready reviews"): c.claim(identity, "two", "annotate", "second") def test_partial_admission_is_preserved_without_false_objective_completion(v2): c, plan, _ = v2 add_unit(plan, "two") plan["objective_contract"]["min_admitted_units"] = 2 identity = c.begin(plan) finish(c, identity) c.cancel_ready(identity, "root", "Second unit unavailable; preserve accepted first unit") review = outcome(inspected_units=["one", "two"]) with pytest.raises(ValueError, match="not achieved"): c.decide(identity, "root", review) review.update(objective_outcome="not_achieved", remaining_work=["Review second unit"]) c.decide(identity, "root", review) state = c.status(identity) assert state["root_review"]["admitted_units"] == ["one"] assert state["progress"]["admitted"] == 1 assert state["progress"]["objective_outcome"] == "not_achieved" def test_frozen_one_of_two_target_can_be_achieved(v2): c, plan, _ = v2 add_unit(plan, "two") identity = c.begin(plan) finish(c, identity) finish(c, identity, "two", accepted=False) c.decide(identity, "root", outcome(inspected_units=["one", "two"])) state = c.status(identity) assert state["progress"]["reviewed"] == 2 assert state["progress"]["review_accepted"] == 1 assert state["progress"]["objective_outcome"] == "achieved" @pytest.mark.parametrize( "checks", [ {}, dict(artifact_verified=True), dict(artifact_verified=True, regressions_resolved=True, invented=True), dict(artifact_verified=True, regressions_resolved="true"), dict(artifact_verified=True, regressions_resolved=False), ], ) def test_achievement_requires_exact_frozen_boolean_checks(v2, checks): c, plan, _ = v2 identity = c.begin(plan) finish(c, identity) with pytest.raises(ValueError, match="(?i)objective"): c.decide(identity, "root", outcome(objective_checks=checks)) assert c.status(identity)["state"] == "root_pending" def test_budget_exhaustion_is_inconclusive_not_achievement(v2): c, plan, clock = v2 identity = c.begin(plan) clock[0] += plan["limits"]["seconds"] review = outcome(verdict="rejected", next="revise", admitted_units=[]) with pytest.raises(ValueError, match="not achieved"): c.decide(identity, "root", review) review.update( objective_outcome="inconclusive", remaining_work=["No units executed; repair dispatch"], objective_checks=dict(artifact_verified=False, regressions_resolved=True), ) c.decide(identity, "root", review) assert c.status(identity)["progress"]["objective_outcome"] == "inconclusive" @pytest.mark.parametrize("target", [0, 2, True]) def test_infeasible_annotation_target_is_rejected_before_claim(v2, target): c, plan, _ = v2 plan["objective_contract"]["min_admitted_units"] = target with pytest.raises(ValueError, match="feasible"): c.begin(plan) assert c.db.execute("SELECT count(*) FROM agent_attempts").fetchone()[0] == 0 def test_weaker_acceptance_alone_does_not_authorize_repeating_same_work(v2): c, plan, _ = v2 add_unit(plan, "two") plan["objective_contract"]["min_admitted_units"] = 2 identity = c.begin(plan) finish(c, identity) finish(c, identity, "two") c.decide( identity, "root", outcome(inspected_units=["one", "two"], admitted_units=["one", "two"]) ) successor = copy.deepcopy(plan) successor["parent"] = identity successor["objective_contract"]["min_admitted_units"] = 1 with pytest.raises(ValueError, match="No progress"): c.begin(successor) def test_v1_history_keeps_original_decision_and_does_not_require_dispatch_receipt(setup): from test_agent_cycle import done c, plan, _ = setup identity = c.begin(plan) done(c, identity) decision = verdict() sha = c.decide(identity, "root", decision) reopened = m.Cycle(c.root, clock=c.clock) try: assert reopened.begin(plan) == identity state = reopened.status(identity) assert state["plan"] == plan and state["root_review"] == decision assert m.digest(state["root_review"]) == sha assert "progress" not in state finally: reopened.close() @pytest.mark.parametrize( ("target", "dispatches", "feasible"), [ (1, 1, False), (1, 2, True), (2, 3, False), (2, 4, True), ], ) def test_target_requires_both_author_and_review_budget(v2, target, dispatches, feasible): c, plan, _ = v2 add_unit(plan, "two") plan["objective_contract"]["min_admitted_units"] = target plan["limits"]["dispatches"] = dispatches if feasible: assert c.begin(plan) else: with pytest.raises(ValueError, match="Dispatch budget"): c.begin(plan) assert c.db.execute("SELECT count(*) FROM agent_cycles").fetchone()[0] == 0 def test_receipt_recovery_after_close_preserves_frozen_outcome(v2): c, plan, _ = v2 identity = c.begin(plan) author = c.claim(identity, "one", "annotate", "author") sha = c.complete(author["token"], "author", dict(reason="Fast completion")) review = c.claim(identity, "one", "review", "critic") c.complete( review["token"], "critic", dict(reason="Checked", verdict="accept", proposal_sha256=sha) ) c.decide(identity, "root", outcome()) before = c.status(identity) reopened = m.Cycle(c.root, clock=c.clock) try: receipt = reopened.confirm_dispatch( author["token"], "root", "executor/author", "Recovered actual message reference" ) assert ( reopened.confirm_dispatch( author["token"], "root", "executor/author", "Recovered actual message reference" ) == receipt ) with pytest.raises(ValueError, match="Conflicting"): reopened.confirm_dispatch(author["token"], "root", "executor/wrong", "different") assert reopened.status(identity) == before finally: reopened.close()