From 9a0e1a54343bee8fa92e08d842969b90a0204ede Mon Sep 17 00:00:00 2001 From: abrichr Date: Fri, 20 Nov 2026 20:47:13 -0500 Subject: [PATCH] fix(flow): two rechecks that held correct writes UNCERTAIN A write begun with ledger=None was rechecked against the default ledger, found no attempt there, and was held UNCERTAIN. A recheck that slept until its due time could wake a few milliseconds before the wall clock reached it, read the time as not yet due, and hold the write UNCERTAIN. --- packages/flow/openadapt_flow/verifier.py | 14 +++++- packages/flow/tests/test_verifier_recheck.py | 47 ++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/packages/flow/openadapt_flow/verifier.py b/packages/flow/openadapt_flow/verifier.py index e3751d4c41..6ef16f82da 100644 --- a/packages/flow/openadapt_flow/verifier.py +++ b/packages/flow/openadapt_flow/verifier.py @@ -2890,7 +2890,11 @@ def _recheck_attempt( if isinstance(write, WriteResult): write_id, key = write.write_id, write.write_key - if ledger is _NO_LEDGER and write.ledger is not None: + if ledger is _NO_LEDGER: + # The ledger the write was begun under. A write begun with no + # ledger (``begin_write(ledger=None)``) has no attempt to bind, + # so its recheck uses none either: resolving the default ledger + # here would find no attempt and hold a correct write UNCERTAIN. ledger = write.ledger else: receipt = write.payload.get("receipt") or {} @@ -3479,7 +3483,13 @@ def _judge_recheck( if wait_s > 0: if not wait: raise RecheckNotDue(state.due_at, wait_s) - time.sleep(wait_s) + # Sleep until the wall clock, not only the sleep timer, has reached + # the due time: the read time is the wall clock truncated to the + # second, and a wake a few milliseconds early would make a correct + # recheck read "not yet due" (held UNCERTAIN). + while wait_s > 0: + time.sleep(wait_s + 0.05) + wait_s = seconds_until(state.due_at) excused: list[tuple[Mapping[str, Any], Mapping[str, object]]] = [] for i, other in enumerate(excused_by): later = other.recheck if isinstance(other, WriteResult) else other diff --git a/packages/flow/tests/test_verifier_recheck.py b/packages/flow/tests/test_verifier_recheck.py index 6f52ee5289..2c5767cd27 100644 --- a/packages/flow/tests/test_verifier_recheck.py +++ b/packages/flow/tests/test_verifier_recheck.py @@ -1595,3 +1595,50 @@ def test_positive_recheck_after_confirmed_still_records_rechecked() -> None: assert after is not None assert after.rechecked is not None and after.recheck_owed is None assert after.resolved["outcome"] == "CONFIRMED" # type: ignore[index] + + +def test_a_write_begun_without_a_ledger_rechecks_without_one( + tmp_path: Path, due_now: None +) -> None: + # begin_write(ledger=None): the default recheck call must not look the + # attempt up in the default ledger (where it never was) and hold a + # correct write UNCERTAIN; it reads again and judges. + db = _db(tmp_path) + contract = Contract.from_dict(_doc(db)) + before = begin_write(contract, _params(2), actor=BOT, ledger=None) + _sql(db, *_save_sql(2)) + result = verify_write(before) + assert result.verified, result.reasons + late = _recheck(contract, result) + assert late.outcome == "CONFIRMED", late.reasons + # Late collateral still stops the write without a ledger (held, never + # confirmed). + before = begin_write(contract, _params(3), actor=BOT, ledger=None) + _sql(db, *_save_sql(3)) + result = verify_write(before) + assert result.verified, result.reasons + _sql(db, "UPDATE patient_data SET email = 'x@example.test' WHERE pid = 1") + late = _recheck(contract, result) + assert late.outcome != "CONFIRMED" and not late.verified + + +def test_a_sleep_that_wakes_early_still_reads_when_due( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + # The sleep timer and the wall clock can disagree by milliseconds; a + # wake just before the due second must not make a correct recheck read + # "not yet due" (UNCERTAIN). + import openadapt_flow.verifier as v + + contract, _, result = _first(tmp_path, recheck_after_s=1) + real = v.time.sleep + calls: list[float] = [] + + def early(seconds: float) -> None: + calls.append(seconds) + real(max(0.0, seconds - 0.3) if len(calls) == 1 else seconds) + + monkeypatch.setattr(v.time, "sleep", early) + late = _recheck(contract, result, wait=True) + assert late.outcome == "CONFIRMED", late.reasons + assert late.read_at >= late.due_at