Skip to content

feat(flow): learn per-request bookkeeping from read-only requests - #2413

Merged
abrichr merged 11 commits into
mainfrom
fix/learn-bookkeeping-read-probe
Nov 23, 2026
Merged

abrichr merged 11 commits into
mainfrom
fix/learn-bookkeeping-read-probe

Conversation

@abrichr

@abrichr abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member

Summary

On apps OpenAdapt had never seen, the largest cause of false stops was per-request bookkeeping on the acting user's own rows. Examples are the user's last-login time and the API token's last-used time. The whole-database frame saw those rows move on every write and refuted every correct write. learn (#1679) can excuse such rows, but only from writes a shadow can check, and then only with a person's approval. On apps whose requests name their target in the URL, it learned nothing (second retest: 0 excuses on every app).

openadapt-flow learn probe learns the same moves without a write and without a person:

  1. Under one contract's whole-database frame, it reads the database, sends only GET and HEAD requests as the agent, and reads again, three times. No write is sent.
  2. It proposes with the learner's existing rules (learn.propose). It admits only a changed row of the acting user (actor_row), with the exact columns. It never admits a column named by a probe query string, a parameter or the contract. A changed move seen in one read-only round is enough, because no round carried a task.
  3. It writes the excuses into each contract's frame.learned with source: read_only_probe, the new field this PR adds. Receipts name the source.

It does not admit:

  • Added rows, such as a first-login row. Their excuse names columns, not values, so it would also cover a row the agent adds for itself. They are listed under needs_approval for learn approve.
  • Other users' rows, and rows no user owns, such as background-worker settings. They stay a stop and are listed.
  • Anything without a derivable users table. The users table is derived from the manifest: the table that the columns bound to an actor parameter reference by foreign key. Without one, nothing is learned.
  • Contracts that already have frame.learned. They are refused, so a person reconciles them.
  • Updates or found checks with no version source. Another user's edit that the agent's stale save reverts leaves nothing changed, so before this change only the bookkeeping move stopped it. The replay showed these lost updates passing without this rule, and stopped with it.

No app name appears in engine code. The per-app knowledge is the probe's output, written as contract data.

Measured (replay of the first-contact retest's graded cases)

Measured in openadapt-internal evidence/false-stops-2026-11. Each case was judged by the frozen verifier, by main and by this branch, and scored with the retest's own score.py. Agent cases were replayed from their recorded transcripts, with no model calls.

The cases were judged by the frozen verifier, main and this branch (the merged head of this PR and #2377, plus the lost-update rule) on the same writes. The apps are anonymized here; the private report names them.

App Correct writes stopped: frozen / main / this branch In-scope harms stopped: frozen / main / this branch
A: last-login bookkeeping 77/77, 77/77, 36/77 26/26, 26/26, 26/26 (exact reason 21, 21, 22)
B: token last-used bookkeeping 46/46, 46/46, 34/46 (replayed agent writes; full app 67/67 to 26/67 before the lost-update rule) its 2 planted lost updates and 1 agent lost update: still stopped (they passed before the rule)
C: no per-request bookkeeping 29/69, 69/69, 29/69 29/29 each
D: first-login row (an added row, held for approval) unchanged unchanged
Held-out E and F 19/137, 103/137, 19/137 35/46, 38/46, 35/46

The main column is worse than frozen on C, E and F. That comes from the recheck defect #2377 fixes: every recheck of a write begun without a ledger is held UNCERTAIN. Without the bookkeeping refutation, one out-of-scope late duplicate on A now passes. The old setup allow-lists its own record table, so the recheck can't see the duplicate. This is a pre-existing gap that the bookkeeping refutation used to hide; it is reported, not fixed here.

Tests

tests/test_learn_probe.py runs on SQLite, and on MariaDB and PostgreSQL when given. It covers:

  • a correct write passes once the probe has learned the bookkeeping;
  • seven harms on top of the learned bookkeeping all stay stops: another user's last-login, another column of the actor's row, another user's token, a property row for another user, an extra property row for the actor, and a token deletion;
  • a row no user owns and a colleague's concurrent move are never learned;
  • a column the probe's query names is never learned;
  • an added row stays a stop until a person approves it;
  • the read-only method check;
  • refusing to overwrite frame.learned;
  • deriving the users table from a manifest.

Locally, test_learn_probe, test_learned_excuses, test_verifier_recheck, test_verifier_api, test_verifier_snapshot_api, test_frame_owner_attribution, test_continuous_learning and test_verifier_finality gave 327 passed, 68 skipped. Ruff and mypy pass on the new module.

A companion PR, #2377, fixes two recheck false stops found by the same replay.

openadapt-flow learn probe reads the whole-database frame, sends only GET
and HEAD requests as the agent, and reads again. A changed row of the
acting user that moved (a last-login or token-use time) becomes a
frame.learned excuse with source read_only_probe, with no approval. Added
rows, rows of other users and rows no user owns are not admitted.
@abrichr

abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: REQUEST CHANGES

From: Codex
Reviewed-Head: 53910b3

P1. The probe can learn an unrelated edit to the acting user's row. The before/after window records the row owner and the changed columns. Another session can change an ordinary field on that same row during one probe round. One observation qualifies for automatic application, and the verifier will then excuse future changes with that column set. The existing concurrency test changes a different user's row. Keep same-owner changes with uncertain provenance pending approval. Auto-apply only where the evidence ties the change to the read request. A synthetic same-owner concurrent edit should stay a stop while ordinary request bookkeeping still qualifies. Probe and proposal · Runtime matching

P1. A mixed contract can receive an excuse while an existing target has no version coverage. The eligibility test counts only record_written effects. A contract that creates a record and checks an existing record with field_equals qualifies as create-only. The alternative check accepts any frame.versions, even when its target_surface covers only one of two write surfaces. Both cases can lift the bookkeeping stop on a stale save to the uncovered target. Check each write target against the surfaces the version judge actually covers. Add mixed-target controls with and without coverage of the existing record. Eligibility test · Version judge

I reviewed the current source, test assertions and recorded CI. The Flow lint job reports missing annotations for kept and held. Unit shard 2 fails the backend-parity and standalone CLI inventory checks. I did not rerun those jobs. Mypy · Unit shard

Generality and evidence

  1. The implementation adds application-independent engine code.
  2. Request attribution and version coverage must hold for every affected target before the verifier can automatically excuse a change.
  3. The PR reports paired replay on held-out apps for the combined tree with fix(flow): two rechecks that held correct writes UNCERTAIN #2377. Its listed controls leave the same-owner concurrent edit and mixed-target cases above unmeasured.

@abrichr

abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: REQUEST CHANGES

From: Gemini
Reviewed-Head: 53910b3

Gemini (gemini-3.1-pro-preview) reviewed the diff, the PR body, runtime/effects/learned.py and the version judge in verify_run.py. This is a fallback: the local Codex CLI could not run (config and model mismatch). The head is unchanged since the earlier Codex review, and every blocker from that review is still open.

  1. Not fixed: a concurrent edit to the acting user's own row is learned. learn_probe.propose calls _learn.propose(..., k=1, ...) (learn_probe.py line 207), so one move seen in one probe window is applied automatically. Another session can change an ordinary column of the actor's row during that window, and the probe will learn it as an excuse. The concurrency test only covers a different user's row.
  2. Not fixed: a mixed contract can get an excuse without version coverage. _sees_its_own_harm (line 294) looks only at record_written effects. A contract that creates one record and checks an existing record with field_equals therefore counts as create-only. The function also accepts any frame.versions without checking that its target_surface covers every write surface.
  3. Not fixed: CI is red. kept, held = [], [] (line 209) has no type annotations, and the flow core lint job (mypy) still fails on it. Unit shard 2 (backend parity and the standalone CLI inventory) also still fails.

…eside an update the versions don't cover

Review blockers on #2413:
- A move must show in every read-only round (at least two), so a one-off
  edit to the acting user's own row during one round is not excused.
- _sees_its_own_harm judged only record_written effects. Now every
  effect counts: create-only means every effect is a count_new_only
  record_written (or a zero-count guard); otherwise frame.versions must
  cover each other effect on its target_surface.
- Annotate the kept/held lists (mypy).
- learn probe takes --base-url for read-only HTTP requests: record why in
  BACKEND_PARITY.md's URL flags table.
- Rename learn_probe.add_parser to register_parser so the parser
  constructor inventory doesn't read the call as an argparse constructor.
@abrichr

abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

From: Claude
Reviewed-Head: 9397a64

@abrichr

abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: REQUEST CHANGES

From: Codex
Reviewed-Head: 9397a64

P1. A repeated change to the actor’s row still becomes an automatic excuse without evidence of who made it. The learner now requires the same column set in each of at least two distinct rounds. That fixes the one-round case. Each round still records a before/after difference and the row owner. A background writer updating the actor’s row throughout the rounds produces the same evidence as request bookkeeping. The verifier will then excuse future changes to those columns on that actor’s row. Keep candidates with only this timing evidence pending human approval. Automatic application needs an existing source that attributes the change to the request. Probe evidence · Proposal filter · Runtime match

P1. An unscoped version read can still miss a write target on a second surface. The new guard correctly refuses a mixed create/update with no versions and an update outside an explicitly named version surface. When target_surface is unset, it accepts every combination of surfaces. The version judge receives the target’s textual ID and deduplicates on that ID alone. Distinct records on two surfaces can have the same local ID, so the second record’s version check is skipped. Keep the automatic excuse disabled on such multi-surface contracts unless the version source distinguishes every target. This can be enforced in probe eligibility. Eligibility · Target collection · Version deduplication

I checked the type annotations, the backend-parity entry and both ends of the parser registration rename. Those changes address the earlier CI findings in source. The author reports 173 targeted tests passing. GitHub CI was still pending at review time. This was a source review of the two updated commits. The changes are application-independent. I have no new held-out runtime result for the two remaining cases. GitHub CI

abrichr added a commit that referenced this pull request Nov 21, 2026
learn_probe._sees_its_own_harm now takes both: every effect must create,
or frame.versions / a bound frame.row_versions must cover every other
effect's surface. A derived row_versions is written only when the
resulting contract passes that check. Docs describe the binding and the
one-table read.
# Conflicts:
#	CAPABILITIES.md
#	llms-full.txt
… for unscoped versions

Codex review blockers on #2413.

fail-open class: correlation-as-attribution. A change to the acting user's
row in every read-only round was taken as the request's bookkeeping, but a
background writer produces the same evidence. Each round now has a control
window of the same length that sends nothing. A move that shows there, or
any move when control windows are missing, goes under needs_approval
instead of becoming an automatic excuse.

fail-open class: ambiguous-subject-binding. With frame.versions and no
target_surface, a contract whose other effects span several surfaces was
eligible, though the version judge tells targets apart by local id alone.
Such a contract now gets no automatic excuse.
@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: REQUEST CHANGES

From: Gemini
Reviewed-Head: 83138c0

Gemini cross-vendor review of the delta that answers Codex's two blockers (control windows; unscoped versions over several surfaces), and of the full PR diff vs main. Summary of Gemini's output:

  1. Blocker: packages/flow/openadapt_flow/learn_probe.py propose (around line 268). A control window whose frame read fails, for example on a database connection error, gives an episode with has_database: False and no moves. propose only checks that a control episode exists for each round (len(controls) < len(eps)), so a failed control window adds nothing to the background set. A background writer that moves a row during that window is then missed, and the move becomes an automatic excuse (fail-open). Every control window must also have read the database, for example not all(c.get("has_database") for c in controls).

Checked:

  • Background-writer attribution through the control windows.
  • Matching control moves against excuses by table and columns, including the empty-columns fallback.
  • Edge cases for empty control windows, which is where the issue above was found.
  • Unscoped version reads over several surfaces, now correctly restricted to one surface.
  • Test coverage for the new logic.

…und move

Gemini review blocker on #2413 (fail-open class: absent-evidence-as-success).
A control window whose frame was not read listed no moves, so a background
writer during it could still become an automatic excuse. Every control
window must have read the database (frame held or refuted); otherwise the
probe's excuses wait under needs_approval.
@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Gemini
Reviewed-Head: c274d55

Gemini cross-vendor review of the two delta commits since Codex's review at 9397a64:

  • 83138c0 adds control windows and restricts unscoped versions to one surface.
  • c274d55 makes an unreadable control window count as no evidence.

The review also covers the full PR diff vs main. Summary of Gemini's output; it reported no findings.

  • Background writers: a control window of the same length follows each request window. A move seen in a control window sends the excuse to needs_approval.
  • Failed or partial reads: every control window must have read the database (has_database) with its frame held or refuted. Otherwise the excuses default to needs_approval.
  • Ambiguous subject binding: with an unscoped version read (target_surface unset), _sees_its_own_harm requires every other effect to be on one surface.
  • Added rows: still go to needs_approval, because their excuses name columns, not values.
  • Other users' rows: the relation is derived from the schema's foreign keys (users_for_actor_params), so only the acting user's rows are excused.
  • Tests: test_a_background_writer_on_the_actors_row_is_not_excused, test_an_unreadable_control_window_excuses_nothing_automatically and test_unscoped_versions_over_two_surfaces_do_not_cover_updates cover these cases.

@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

From: Claude
Reviewed-Head: c274d55

@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

From: Claude
Reviewed-Head: 9117e38

@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Gemini
Reviewed-Head: 9117e38

Verified clean merge with origin/main (24034b59d72ea4c05960b4e729d9aea72a771b06) over the previously approved head c274d556aad313eab70ebdcba45c3699656e5e65:

  • Only packages/flow/docs/BACKEND_PARITY.md conflicted (two adjacent rows: openadapt-flow learn probe and openadapt-flow wrap-dispatch); preserved both and regenerated llms-full.txt via scripts/build_llms_full.py.
  • Regenerated packages/flow/tests/fixtures/cli_surface.json (OPENADAPT_WRITE_CLI_SURFACE=1) for the newly added openadapt-flow learn probe subcommand and updated openadapt-flow learn --help.
  • All PR code and test files (openadapt_flow/learn.py, openadapt_flow/learn_probe.py, openadapt_flow/runtime/effects/frame.py, openadapt_flow/runtime/effects/learned.py, tests/test_learn_probe.py) merged cleanly without conflict.
  • Targeted pytest: tests/test_learn_probe.py (26 passed, 43 skipped opt-in live) and tests/test_cli_surface.py (3 passed, 1 skipped).
  • Generality & durability: general read-only probe for learning per-request bookkeeping tables across apps, fail-closed on unreadable control windows, no per-app engine code.

@abrichr

abrichr commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

From: Claude
Reviewed-Head: 2144dc5

@abrichr

abrichr commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Gemini
Reviewed-Head: 2144dc5

Delta over 9117e38ef3aeda3a5093817205aa3ae243c62b44:

  • Ran scripts/build_command_map.py and scripts/build_llms_full.py (2 lines updated in CAPABILITIES.md and llms-full.txt) to include openadapt-flow learn probe in the generated command map.
  • Verified tests/test_capabilities_map.py: 2 passed in 2.38s. No code or behavior change.

@abrichr
abrichr merged commit 49cdd76 into main Nov 23, 2026
57 of 58 checks passed
@abrichr
abrichr deleted the fix/learn-bookkeeping-read-probe branch November 23, 2026 02:45
abrichr added a commit that referenced this pull request Nov 23, 2026
…urces

- new_artifact_key moves to sealed_set (at_rest must not import upward).
- A type ignore for the unfinished-run resume key, as on the creation path.
- The checkpoint lifecycle test checks the refusal when the key is used.
- CAPABILITIES.md rows now come from capabilities/: the two at-rest rows
  move into their sources, and the learn-probe row from #2413 gets one.
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.

1 participant