Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 35 additions & 33 deletions openadapt/transducer_fact_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -914,38 +913,41 @@ 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,
commit_idx,
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",
Expand All @@ -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."
),
)
109 changes: 107 additions & 2 deletions tests/test_transducer_fact_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 -----------------------------------------------------
Expand Down