From 055be8296c380a3f63fdaaa30b65b7e6a876c703 Mon Sep 17 00:00:00 2001 From: abrichr Date: Tue, 1 Dec 2026 18:57:03 -0500 Subject: [PATCH] fix(transducer): step-cap VERIFIED needs a Rung 2 or Rung 3 pass and every required fact When an agent runs out of steps without declaring DONE, it may have stopped mid-task. finalize_on_step_cap still had two ways to stamp VERIFIED that do not check the end state: a passing verification_frame on the last observation, and required facts the ledger picked up earlier in the run. Now the step-cap receipt is VERIFIED only when the episode made a save, no compiled obligation is open, every required fact is proven, and an attached Rung 3 or Rung 2 verifier passes on the final observation. Unproven facts settle the receipt as COMPLETED_UNVERIFIED before any verifier runs. Everything else is COMPLETED_UNVERIFIED with delivery_uncertain set. --- openadapt/transducer_fact_ledger.py | 68 +++++++++-------- tests/test_transducer_fact_ledger.py | 109 ++++++++++++++++++++++++++- 2 files changed, 142 insertions(+), 35 deletions(-) diff --git a/openadapt/transducer_fact_ledger.py b/openadapt/transducer_fact_ledger.py index 4743459de9..bb81d71031 100644 --- a/openadapt/transducer_fact_ledger.py +++ b/openadapt/transducer_fact_ledger.py @@ -835,16 +835,15 @@ def _oracle_tampered(self, state: EpisodeState) -> list[str]: def finalize_on_step_cap(self, observation: Any, task: Any) -> TransducerReceipt: """At ``max_steps``, run one read-only end-state check before settling. - Never calls the wrapped agent or a reopen path, and never promotes an - episode solely from an earlier intermediate ``COMMIT_VERIFIED`` hold - when the agent never declared ``DONE`` (hitting ``max_steps`` mid-task - means remaining steps may be unfinished). Promotes to ``VERIFIED`` only - when an explicit end-state verifier (Rung 3, Rung 2, a fresh - ``verification_frame``, or all ``required_fact_ids``) holds and no - compiled task obligations remain unverified. An episode that reached - no commit boundary has no save to verify, so it never becomes - ``VERIFIED`` here, whatever an end-state check says. Otherwise returns - ``COMPLETED_UNVERIFIED`` with ``delivery_uncertain=True``. + Never calls the wrapped agent or a reopen path. Hitting ``max_steps`` + without ``DONE`` means the agent may have stopped mid-task, so an + earlier verified save, a ``verification_frame`` or facts the ledger + picked up along the way do not prove the end state. ``VERIFIED`` + requires all of: the episode reached a commit boundary, no compiled + obligation is still open, every ``required_fact_ids`` entry is proven, + and an attached Rung 3 or Rung 2 verifier passes on the final + observation. Otherwise returns ``COMPLETED_UNVERIFIED`` with + ``delivery_uncertain=True`` (``commit_index=0`` when no save was made). """ owner = self.owner run_id, task_id = owner._resolve_ids(observation, task) @@ -914,12 +913,34 @@ def finalize_on_step_cap(self, observation: Any, task: Any) -> TransducerReceipt "obligation(s) remaining." ), ) + # Facts the task requires but nobody has seen yet mean the task may be + # unfinished; no end-state check can stand in for them. + required = required_fact_ids(task, observation) + missing = state.fact_ledger.unproven_facts(required) + if missing: + return owner._emit_receipt( + state=state, + outcome="COMPLETED_UNVERIFIED", + oracle_tier=0, + observed_effect_strength=None, + authorization_passed=True, + identity_passed=True, + postcondition_passed=False, + effect_passed=False, + delivery_uncertain=True, + commit_index=commit_idx, + reason=f"Step cap reached with unproven facts: {', '.join(missing)}.", + ) oracles = ( (owner.rung3_verifier, 2, "Rung 3 system-of-record"), (owner.rung2_verifier, 1, "Rung 2 independent-session"), ) + attached = False for verifier, tier, label in oracles: - if verifier is not None and _ok(verifier(observation, task, state)): + if verifier is None: + continue + attached = True + if _ok(verifier(observation, task, state)): owner._mark_verified(state, commit_idx, tier=tier) return self._emit_verified( state, @@ -927,25 +948,6 @@ def finalize_on_step_cap(self, observation: Any, task: Any) -> TransducerReceipt tier, f"Step cap reached; verified by attached {label} check.", ) - frame = _get(observation, "verification_frame") - if frame is not None: - frame_ok, why, strength = owner._evaluate_verification_frame(frame) - if frame_ok and strength is not None: - tier = 1 if strength == "independent_session" else 0 - owner._mark_verified(state, commit_idx, tier=tier) - return self._emit_verified( - state, commit_idx, tier, f"Step cap reached; {why}" - ) - required = required_fact_ids(task, observation) - missing = state.fact_ledger.unproven_facts(required) - if required and not missing: - owner._mark_verified(state, commit_idx, tier=0) - return self._emit_verified( - state, - commit_idx, - 0, - f"Step cap reached; all {len(required)} required facts proven.", - ) return owner._emit_receipt( state=state, outcome="COMPLETED_UNVERIFIED", @@ -958,8 +960,8 @@ def finalize_on_step_cap(self, observation: Any, task: Any) -> TransducerReceipt delivery_uncertain=True, commit_index=commit_idx, reason=( - "Step cap reached with no read-only check holding" - + (f"; unproven facts: {', '.join(missing)}" if missing else "") - + "." + "Step cap reached with no read-only check holding." + if attached + else "Step cap reached with no Rung 2 or Rung 3 verifier attached." ), ) diff --git a/tests/test_transducer_fact_ledger.py b/tests/test_transducer_fact_ledger.py index 15a9826cf3..faaa30a607 100644 --- a/tests/test_transducer_fact_ledger.py +++ b/tests/test_transducer_fact_ledger.py @@ -363,15 +363,120 @@ def test_step_cap_audit_defaults_to_uncertain_when_nothing_holds() -> None: assert agent.calls == 1 -def test_step_cap_audit_accepts_facts_proven_on_the_final_view() -> None: +def test_step_cap_verifies_when_rung2_holds_and_all_facts_are_proven() -> None: agent = _Agent([_Action(type="click", is_commit=True, target_name="Move")]) - wrapped = SimplexTransducer(agent) + calls: list[int] = [] + + def rung2(observation: Any, task: Any, state: Any) -> bool: + calls.append(state.step_count) + return True + + wrapped = SimplexTransducer(agent, rung2_verifier=rung2) task = {"run_id": "r", "task_id": "cap3", "required_facts": ["moved"]} wrapped.act(_explorer("Documents", ["a.txt"]), task) receipt = wrapped.finalize_on_step_cap( _explorer("Archive", ["a.txt"], proven_facts=["moved"]), task ) assert receipt.outcome == "VERIFIED" + assert receipt.oracle_tier == 1 + assert len(calls) == 1 + assert wrapped.wal.is_commit_verified("r", "cap3", 1) + + +def test_step_cap_verifies_when_rung3_holds_and_all_facts_are_proven() -> None: + agent = _Agent([_Action(type="click", is_commit=True, target_name="Move")]) + wrapped = SimplexTransducer( + agent, + rung3_verifier=lambda o, t, s: {"verified": True}, + rung2_verifier=lambda o, t, s: False, + ) + task = {"run_id": "r", "task_id": "cap3-r3", "required_facts": ["moved"]} + wrapped.act(_explorer("Documents", ["a.txt"]), task) + receipt = wrapped.finalize_on_step_cap( + _explorer("Archive", ["a.txt"], proven_facts=["moved"]), task + ) + assert receipt.outcome == "VERIFIED" + assert receipt.oracle_tier == 2 + + +def test_step_cap_facts_proven_without_an_end_state_verifier_are_not_verified() -> None: + """Facts seen along the way do not prove the end state of an unfinished run.""" + agent = _Agent([_Action(type="click", is_commit=True, target_name="Move")]) + wrapped = SimplexTransducer(agent) + task = {"run_id": "r", "task_id": "cap3-noverifier", "required_facts": ["moved"]} + wrapped.act(_explorer("Documents", ["a.txt"]), task) + receipt = wrapped.finalize_on_step_cap( + _explorer("Archive", ["a.txt"], proven_facts=["moved"]), task + ) + assert receipt.outcome == "COMPLETED_UNVERIFIED" + assert receipt.delivery_uncertain is True + assert "no Rung 2 or Rung 3 verifier attached" in receipt.reason + assert not wrapped.wal.is_commit_verified("r", "cap3-noverifier", 1) + + +def test_step_cap_ignores_a_passing_verification_frame_without_a_verifier() -> None: + agent = _Agent([_Action(type="click", is_commit=True, target_name="Submit")]) + wrapped = SimplexTransducer(agent) + task = {"run_id": "r", "task_id": "cap-frame"} + wrapped.act({"url": "https://a/form"}, task) + receipt = wrapped.finalize_on_step_cap( + { + "url": "https://a/form", + "verification_frame": { + "is_editable": False, + "is_toast": False, + "has_delta": True, + "record_status": "Submitted", + "separate_session": True, + }, + }, + task, + ) + assert receipt.outcome == "COMPLETED_UNVERIFIED" + assert receipt.delivery_uncertain is True + assert not wrapped.wal.is_commit_verified("r", "cap-frame", 1) + + +def test_step_cap_after_earlier_saves_with_unproven_facts_is_not_verified() -> None: + """HealthAdminBench Wave 2.1 shape: the agent saved twice and each save was + verified mid-run, then it ran out of steps before DONE. A required fact is + still unproven, so the step-cap receipt must not be VERIFIED, and the + end-state check is not consulted again.""" + agent = _Agent( + [ + _Action(type="type", text="CO-50 triage note"), + _Action(type="click", is_commit=True, target_name="Submit disposition"), + _Action(type="click", target_name="Next denial"), + _Action(type="click", is_commit=True, target_name="Save note"), + _Action(type="click", target_name="Fax queue"), + ] + ) + calls: list[int] = [] + + def rung2(observation: Any, task: Any, state: Any) -> bool: + calls.append(state.step_count) + return True + + wrapped = SimplexTransducer(agent, rung2_verifier=rung2) + task = { + "run_id": "r", + "task_id": "hab-stepcap", + "max_steps": 5, + "required_facts": ["disposition_saved", "fax_sent"], + } + for i in range(5): + wrapped.act({"url": f"https://emr/denied/{i}", "dom_elements": [i]}, task) + state = wrapped.get_state("r", "hab-stepcap") + assert state.next_commit_index > 2, "the agent made two saves" + assert wrapped.wal.is_commit_verified("r", "hab-stepcap", 1) + calls.clear() + receipt = wrapped.finalize_on_step_cap( + {"url": "https://emr/fax", "proven_facts": ["disposition_saved"]}, task + ) + assert receipt.outcome == "COMPLETED_UNVERIFIED" + assert receipt.delivery_uncertain is True + assert "unproven facts: fax_sent" in receipt.reason + assert calls == [] # -- Non-GUI modality -----------------------------------------------------