Skip to content
Merged
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
14 changes: 12 additions & 2 deletions packages/flow/openadapt_flow/verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions packages/flow/tests/test_verifier_recheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '[email protected]' 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
Loading