Skip to content

fix(transducer): resume from the WAL without losing or duplicating a save - #2871

Merged
abrichr merged 5 commits into
mainfrom
fix/crash-resume-no-duplicate
Dec 1, 2026
Merged

abrichr merged 5 commits into
mainfrom
fix/crash-resume-no-duplicate

Conversation

@abrichr

@abrichr abrichr commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Fixes #2869. Stacked on #2867, because it reuses its field-bound saved writes.

Problem

After a crash, openadapt.wrap could not confirm saves made before the restart: the values it looks for were held in memory. It also did not stop the fresh session from saving them again. The WAL skips a repeated commit only once that commit is VERIFIED, and pre-crash commits are still PENDING_EFFECT.

A live control on a web EMR (synthetic data) shows the effect: save, kill the process, then a fresh session redoes the item. It ended with two records and a VERIFIED receipt, a false VERIFIED.

Change

  • Persist. Each commit stores the field-bound values it saved (field_writes) in its WAL summary.
  • Restore and confirm. On resume, those writes are rebuilt. When they reappear on a saved screen of the pinned record (verbatim, or in a reopened untouched form), the commit is marked verified and the agent gets COMMIT_ALREADY_SAVED.
  • Refuse a duplicate. A commit whose form carries exactly the values of an earlier commit is refused with a DUPLICATE_SAVE hint to check first. This includes an unconfirmed commit from before the restart. A second attempt hands the item to a person.
  • Done gate. It never verifies when, on the pinned record, more commits carry identical values than the task asks for.
  • Scope. A duplicate counts only when both saves were made on a screen showing the pinned record. The same values on another record are not a duplicate. A repeat is allowed when the instruction names that content (its longest value) as many times as there would be saves. The in-session form latch follows the same repeat rule. Without a pinned identity, nothing is refused.
  • What counts as the form. A navigation click ends the form a commit saves, so a search query typed on the way is not part of it.

Within one process, the existing in-memory form latch already blocked an identical re-save. This PR covers the restart.

Integration note

After a restart, the read-back provider should treat the new session's start as the time of the last save, which happened in the dead process. A form loaded in the new session, after a screen without it, then counts as reopened.

Evidence (live controls, real OpenEMR 8.0.0.3, scripted agent, arm B's exact read-back path)

Control Before After
crash after the form save, fresh session redoes the item 2 records, VERIFIED 1 record, refused, not verified
crash after the form save, fresh session opens the record and finishes unverified (pending commits) VERIFIED, 1 record
crash after the encounter save, fresh session adds the form to that encounter not run VERIFIED, 1 encounter, 1 form
legitimate repeat: identical values on two different patients (two items, one batch WAL) not run VERIFIED on both
legitimate repeat: the task asks for two identical entries on one record not run VERIFIED, two forms with exactly the intended values
must-never-verify set (wrong chart, off-by-one date, units 12 for 1, refused save, wrong auth number) unverified unverified

Tests

  • test_commit_persists_the_values_it_saved
  • test_restart_confirms_the_pre_crash_save_on_screen
  • test_restart_refuses_to_save_the_same_values_again
  • test_identical_re_save_in_one_session_is_not_released
  • test_same_values_on_two_records_both_verify
  • test_task_that_names_two_identical_entries_verifies

No app names in engine code.

…save

After a crash, wrap could neither confirm the saves made before the
restart (the values it would look for were in memory) nor stop the
fresh session from saving them again: the WAL skips a repeated commit
only once it is VERIFIED, and pre-crash commits are still pending. A
live control on a web EMR (save, kill, redo the item) ended with two
records and a VERIFIED receipt (#2869).

Each commit now stores the field-bound values it saved in its WAL
summary. On resume they are rebuilt; once they reappear on a saved
screen of the pinned record, the commit is marked verified and the
agent is told the record already exists. A commit whose form carries
exactly the values of an earlier commit (including an unconfirmed one
from before the restart) is refused with a hint to check first, and a
second attempt hands the item to a person. The done gate never
verifies when two commits carry identical values. A navigation click
ends the form a commit saves, so a search query on the way is not part
of it.

Fixes #2869.
@abrichr

abrichr commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: REQUEST CHANGES

From: Grok
Reviewed-Head: 8bce1e3

I reviewed this exact head. I am not approving, and I will not merge or push.

This PR is stacked on #2867. I reviewed this PR's own WAL-resume and duplicate-save delta.

Blocker

  1. Any non-commit click clears form_keys, so a later save is not treated as a form save. after_step clears ep.form_keys on every click that is not a commit boundary, with a comment that this is a "navigation click." after_step Then note_commit marks each draft in_form=key in ep.form_keys. note_commit _form_groups and duplicate_of only look at in_form writes and the current form_keys set. duplicate_of

    A click used to focus the next field, open a date picker, or switch a tab is not a commit, so it wipes the latch. I ran a fill_form of the fixture comments and start date, then a non-commit click, then the same save: both writes were stored with in_form=False. After a crash-resume I repeated that sequence with the same values: the second save was released (is_commit=True) and there was no DUPLICATE_SAVE hint. The restored pre-crash writes were in_form=True; the new writes were in_form=False, so duplicate_of saw an empty form_keys set and returned None.

    The same hole appears if the agent types field A, clicks field B, types field B, then saves: only B stays in_form. The new tests only cover fill_form immediately followed by the commit click. Distinguishing a search box on the way to the form from the form itself is worth keeping, but it cannot be implemented as "any click that is not a save leaves the form."

What holds

  • On the happy path (fill_form, then the commit click, no click in between), the WAL grows a redacted field_writes list, a new process restores those writes from a cold WAL, and a reopened pinned record confirms them and emits COMMIT_ALREADY_SAVED.
  • Repeating the exact same fill and save in a fresh process, with no mid-form click, warns once and then abstains with DUPLICATE_SAVE.
  • has_duplicate_commits in pre_done releases unverified rather than signing a duplicated pair. That check also depends on in_form groups, so it inherits the same hole.
  • Vault redaction of field_writes is the existing secret-scrub, not a new plaintext leak of vault values.

Generality gate

python packages/flow/scripts/check_engine_generality.py at the #2873 stack head (which contains this commit) reported "Engine generality OK (7 baseline hits left)". This delta is generic form latch and WAL resume. The tests use a synthetic pinned record, not an application-specific engine path. No held-out real-app evidence is claimed.

Tests I ran

  • tests/test_transducer_v3_runtime.py at this stack: 29 passed, including the three new crash-resume tests.
  • Four local probes of a mid-form click, not in the PR, all failed as described above.

CI at this head

The review-verdict check is red because there was no Grok or Codex review on protected paths; the package jobs were skipped as a result. A few independent checks (gitleaks, dependency-review, smoke, install weight, launcher run-ci) were green on later stack heads. I am not merging.

Limitations, not blockers

Restore only runs when loaded_from_cold_wal is set, and older WAL rows without field_writes cannot be confirmed or used for the duplicate check. has_duplicate_commits only runs in the obligations / completeness branch of pre_done.

…repeats the task names

The same values on another record, or a second identical entry the task
asks for, are not duplicates. A commit now records whether its screen
showed the pinned record. The refusal and the done rule compare only
commits on the pinned record, and allow as many identical commits as the
instruction names that content (its longest value). Without a pinned
identity nothing is refused. The in-session form latch follows the same
rule for repeats the task names.
…, not the fields typed since the last click

Review (Grok) found that any non-commit click cleared the set of fields a
save submits, so a click inside the form (a date picker, the next field)
dropped earlier fields from the save. A resumed duplicate then went
through, and the typed-values gate could miss a dropped value.

A draft field now belongs to a save when it is still on screen when the
save is clicked and, when the read-back provider reports a container
(frame or form), sits in the save control's container. Clicks no longer
matter. A search box in another container is still left out. Tests cover
a click inside the form before a crash and redo, typing A, clicking B
and typing B, and a search box in another container.
@abrichr

abrichr commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Response to the Grok review at 8bce1e3. Fixed at 75f97fa.

Blocker: any non-commit click cleared form_keys. I agree, and I removed the click rule. A draft field now belongs to a save when both hold:

  • it is still on screen when the save is clicked;
  • when the read-back provider reports a container (container, form or frame on a DOM element), it sits in the save control's container.

Clicks inside the form (a date picker, the next field) no longer matter. A search box in another container is still left out.

New tests:

  • test_click_inside_the_form_keeps_fields_in_the_save_after_a_restart: your probe. Fill, click a form field, save, crash, then the fresh session does the same. The second save gets DUPLICATE_SAVE and is not released.
  • test_type_a_click_b_type_b_keeps_both_fields
  • test_search_box_in_another_container_is_not_part_of_the_save

Also since your review (d44cb10):

  • The duplicate refusal and the done rule are scoped to the same pinned record.
  • A repeat is allowed when the instruction names the content as many times as there would be saves.
  • Tests: test_same_values_on_two_records_both_verify, test_task_that_names_two_identical_entries_verifies.

Live controls (real OpenEMR 8.0.0.3, scripted agent, integration build a8f5deb09): 14 of 14 pass. That includes your case live: crash, then redo with a click inside the form before each save, ends with 1 record and is not verified. The two legitimate-repeat controls verify: identical values on two patients, and a task that asks for two identical entries.

The limitations you listed stand. has_duplicate_commits and the typed-values gate run only in the obligations branch of pre_done, and WAL rows written before this change carry no field_writes.

Re-review requested at 75f97fa.

… a pre-crash commit goes to a person

With no pinned identity the duplicate refusal could not be scoped to a
record and refused nothing, so a crash-then-redo duplicate could get
through. When a session resumed from the WAL is about to save exactly
the values of a commit from before the restart and no record identity
is pinned, wrap now abstains (RESUME_DUPLICATE_UNCERTAIN) and hands the
item to a person instead of saving.
@abrichr

abrichr commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Added 1cc4cbf: with no pinned record identity, a session resumed from the WAL that is about to save exactly the values of a commit from before the restart now abstains (RESUME_DUPLICATE_UNCERTAIN) and hands the item to a person instead of saving. Before this, nothing was refused without pins. Unit test: test_unpinned_resumed_save_with_pre_crash_values_goes_to_a_person. Live control on real OpenEMR (crash, then redo, no pinned identity): HALTED, not verified, one record. Re-review requested at the new head.

@abrichr

abrichr commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: 1cc4cbf

I re-reviewed this exact head. The blocker I raised at 8bce1e344f368ca4c7f892748155c6d880e4b90b is fixed.

Behavior

after_step no longer clears a form latch on every non-commit click. A save now takes its form from _form_of: draft fields that are still on screen when the save is clicked, and, when the provider reports containers, in the save control's container. A date-picker or next-field click inside the form keeps both fields; a search box in another container is excluded.

The cases that failed on the old head now have tests and pass on this one:

  • test_click_inside_the_form_keeps_fields_in_the_save_after_a_restart
  • test_type_a_click_b_type_b_keeps_both_fields
  • test_search_box_in_another_container_is_not_part_of_the_save

Happy-path WAL resume, COMMIT_ALREADY_SAVED, and DUPLICATE_SAVE still hold. New coverage also passed: the same values on two pinned records both verify; a task that names two identical entries is allowed; without a pinned identity, a resumed save that repeats a pre-crash commit abstains with RESUME_DUPLICATE_UNCERTAIN.

I ran tests/test_transducer_v3_runtime.py against this worktree on Python 3.12: 33 passed in 0.36s. packages/flow/scripts/check_engine_generality.py reported "Engine generality OK (7 baseline hits left)". The delta is generic form membership and WAL resume, with synthetic records. No held-out real-app evidence is claimed.

Limitation, not a blocker for this head

_form_of uses ep.screen_map from observe_saved at the start of the turn (the screen when the save is decided). If the provider gives no labeled controls, the on-screen set is empty and no write is marked in_form, so the duplicate check has nothing to group. If containers are omitted, every on-screen draft is included. Those are provider-quality limits, not a return of the click-clears-the-latch hole.

CI at this head: packages scope, packages-gate, install weight, launcher run-ci, smoke, dependency-review, validate-platform-manifest and gitleaks passed. review-verdict is red because the newest Grok review was still REQUEST CHANGES at 8bce1e34. I am not merging. A release decision, if any, still sits with the maintainer.

…sume-no-duplicate

# Conflicts:
#	tests/test_transducer_v3_runtime.py
@abrichr

abrichr commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: a295b4c

I re-reviewed this exact head (R2 after my APPROVE at 1cc4cbf7761f). This head merges #2867's ca4b84a2c. The unique WAL-resume code is unchanged. I am not merging.

What changed since 1cc4cbf7761f

Tests I ran at this exact head

Generality: no per-app engine code. Fixtures are synthetic.

CI at this head

Packages, smoke, and launcher are green. review-verdict is red because the newest posted review was still my APPROVE at 1cc4cbf7761f, not this head.

This PR is stacked on #2867. I did not merge.

No release decision from me.

Base automatically changed from fix/obligations-across-saved-screens to main December 1, 2026 16:41
@abrichr
abrichr added this pull request to the merge queue Dec 1, 2026
Merged via the queue into main with commit 10d283a Dec 1, 2026
42 of 43 checks passed
@abrichr
abrichr deleted the fix/crash-resume-no-duplicate branch December 1, 2026 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wrap: crash resume can verify a duplicate and cannot confirm pre-crash saves

1 participant