Skip to content

feat(flow): verdict tiers, entity-scoped frames and shadow mode by default - #2425

Open
abrichr wants to merge 13 commits into
mainfrom
fix/verdict-tiers
Open

abrichr wants to merge 13 commits into
mainfrom
fix/verdict-tiers

Conversation

@abrichr

@abrichr abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member

What this changes

A whole-database frame stops a correct write when any row anywhere moves. In
the false-stop study (openadapt-internal evidence/false-stops-2026-11),
rows that move with no harm stopped correct writes on three apps: scheduler
and task-queue rows, totals the app recomputes after a save, and a
notification for each watcher of a record. This pull request separates those
from real harm, without letting a planted fault through.

It builds on #2413 (learn per-request bookkeeping from read-only requests)
and #2377 (two rechecks that held correct writes UNCERTAIN). Both are merged
into this branch, not rebased, so review the diff against them.

Three verdict tiers

  • Hard (REFUTED, blocks): the target missing, a wrong value, a
    duplicate, a row the write doesn't declare that changed or was removed in a
    business-entity table (another user's row and an unexcused column of the
    acting user's row included), a late revert, a lost update, any removed row,
    and anything no row comparison explains.
  • Logged (CONFIRMED): the write holds, and rows were added or changed
    only outside the entity tables. They are listed in the frame evidence as
    side_effects (table, change, row count, column names; no keys or
    values) and offered to openadapt-flow learn as candidate excuses.
  • Retry: before judging, the verifier waits for the declared records to
    become visible (0.5, 1, 2, 4 s; 8 s budget; OPENADAPT_VERIFY_RETRY_S).
    It reads only through database verifiers, decides nothing, and the full
    judge runs afterwards. Settling ignores motion outside the entity tables.

The outcome contract doesn't change. A logged pass is CONFIRMED, so a harm
that slips through it is counted as a silent pass by anything that grades
the outcome. WriteResult gains tier, side_effects, enforcement and
blocks.

Entity-scoped frames

frame.database.entity_tables is opt-in. Unset, every table is judged as
before. The tables the frame already names (views, judged_by,
new_row_fields, side records, learned excuses, owner tables, profile rules)
always count as entities. openadapt-flow learn entities derives the list
from the setup: each kind's main table, every table a contract's read
selects from, and the acting user's table (derived from the manifest's actor
parameters by foreign key; with none, no scope is proposed). No app is
named in engine code. On a frame that diffs the whole database, a generated
read's own check columns (pin__*, empty__*) are no longer compared on
undeclared rows. One flipped with the clock on rows nobody wrote.

The safety bar

runtime/effects/admission.py admits a relaxation only when, under it,
planted wrong-row, wrong-value, duplicate, sibling-edit, late-revert and
lost-update faults, plus the two actor-table attacks, all ran and were all
caught. It also requires that every case caught before still runs. learn entities apply writes a scope only with such results, run against the
staged scope (the proposal digest must match), and writes every contract or
none.

Shadow mode by default

A new contract is in shadow: its verdict is computed and recorded in full,
but WriteResult.blocks is false. After N clean checks in a row (20 to 50,
default 30, OPENADAPT_GATE_CLEAN_RUNS), gating is offered. A person turns
it on with openadapt-flow gate enable. gate label marks a stop as a real
catch, which keeps the run, or as a false stop. OPENADAPT_ENFORCEMENT=gating
restores the old behaviour everywhere. The ledger still holds refuted
attempts, so a retry is never sent silently.

Docs: packages/flow/docs/VERDICT_TIERS.md, the command map, a
CAPABILITIES row and llms-full.txt.

Not in this pull request

  • Callers that stop an agent on a verdict don't read blocks yet. Their
    stopping is unchanged. Wiring them is a separate change.
  • A null task value bound in a hand-written contract's query is still
    refused at begin_write. That refusal is right for an arbitrary SQL
    query, so the fix belongs in the generator's null-safe template.
  • Another user's unattributed edit to a record in an entity table still
    stops the write.
  • A harm confined to a non-entity table is logged, not stopped.

Tests

tests/test_verdict_tiers.py (SQLite, real reads) checks the following:

  • With no scope, background rows refute. With a scope, the write passes as
    logged, with names only.
  • Each safety-bar fault still refutes under a scope: a sibling record,
    another user's row, another column of the actor's row, a record read
    through, a removed side-table row, a duplicate and a wrong value.
  • A derived check column doesn't move the frame.
  • Settling holds under a queue that never stops.
  • The retry wait backs off and stays within its budget, and never reads
    through an API.
  • Shadow mode: N clean checks, labels, and enable refused until gating is
    offered.
  • Logged side effects reach the learner.
  • The admission refuses any missed or unrun class.
  • learn entities derives, stages and applies only on matching, all-caught
    results.

The existing frame, learned-excuse, verifier, verify-run, recheck, ledger,
receipt, shadow and CLI-census suites pass, with test_verifier_api
updated for the new WriteResult field.

Real-app replay on the false-stop harness is pending and will be reported on
this pull request.

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.
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.
Rows that move with no harm (a scheduler, a task queue, totals the app
recomputes, notifications) stopped correct writes under the whole-database
frame. With frame.database.entity_tables, rows added or changed outside the
business-entity tables are logged side effects on a CONFIRMED result and
candidates for the learner; removed rows, entity-table rows the write
doesn't declare, another user's row and an unexcused column of the actor's
row still refute. learn entities derives the scope from the setup and
applies it only when planted faults are all still caught under it
(runtime/effects/admission.py). The verifier waits for declared records
with backoff before judging, settling ignores motion outside the scope, and
a generated read's check columns are not compared on undeclared rows. New
contracts start in shadow (WriteResult.blocks false) until N clean checks
in a row, then a person can turn gating on (openadapt-flow gate).
@abrichr

abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: 7c624aa

Protected-path review of the unique work versus 0fc06e298 (the merged #2413/#2377 stack). I did not merge. Neither this review nor CI is a measurement claim; the request says the paired replay is still running.

  1. Side-effect split. A collateral change becomes a logged side effect only when all of these hold: the surface is in the database hashers, the row was seen, the change is added or changed, it is not on_target / other_on_target / audit_only, and table_name(surface) is outside the entity scope. Removed rows, unread tables (not in hashers), entity-table rows, and on-target / other-on-target / audit-only changes stay in collateral and still refute. Tests cover a sibling entity edit, another user's actor-table row, an unexcused actor column, a location table, a duplicate, a wrong value, and a delete outside the scope.

  2. Entity scope. entity_scope always unions the frame's own named tables (views, judged_by, new_row_fields, side records, learned excuse tables, attribution owners, profile rules). learn entities will not propose a scope without the acting user's table, includes it in entity_tables, and binds proposal_sha256 over entity_tables and actor_table.

  3. Retry wait. await_effects only waits until declared record effects are visible, on database verifier kinds (surfaces, sql, sqlite); any other kind, or any failure, skips or ends the wait. The judge then reads the store again in full. Settling ignores motion outside the scope between post-save snapshots, but those tables are still diffed against the baseline (and logged, not stopped, when they pass the split).

  4. Shadow mode. enforcement.observe records the already-computed outcome and returns the mode. It does not change outcome. The ledger is still attached on WriteResult after observe. blocks is true only when the result is not CONFIRMED and enforcement == "gating". A new contract is shadow until N clean checks and a person enables gating.

  5. Admission. learn entities apply refuses a digest mismatch, then admit(..., required=SAFETY_BAR + ACTOR_ATTACKS) refuses a required class that was not run, a case that passed under the relaxation, and (when before is supplied) a case caught before that was not rerun.

Residual: at judge time, entity_scope does not itself re-derive the actor table. A later hand edit of entity_tables that drops it would let actor-table harms be logged as side effects. The propose/apply path does not allow that, and the CAPABILITIES row states the remaining product limits (an unattributed entity-table edit still stops; a harm confined to a non-entity table is logged, not stopped).

# Conflicts:
#	CAPABILITIES.md
#	llms-full.txt
#	packages/flow/docs/LEARNED_EXCUSES.md
#	packages/flow/docs/commands/11-guard-an-agent-you-already-run/learn.md
#	packages/flow/openadapt_flow/__main__.py
#	packages/flow/openadapt_flow/learn.py
#	packages/flow/openadapt_flow/learn_probe.py
#	packages/flow/tests/test_learn_probe.py
@abrichr

abrichr commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Gemini
Reviewed-Head: 6767bcc

Delta review versus previously approved head 7c624aa69: clean merge of origin/main (4519814c7). Verified packages/flow/openadapt_flow/__main__.py preserves both cli_gate.register and main's subcommand registrations (readiness, flight, hooks, plan, proxy). Zero behavior regression. Approve for merge on green CI.

@abrichr

abrichr commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

From: Gemini
Reviewed-Head: ed65850

@abrichr

abrichr commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Claude
Reviewed-Head: ed65850

Scope: the delta from Gemini's approved head 6767bcc7 to ed65850d, which also supersedes Grok's approval at 7c624aa6. The range has one merge, a49180ed, which merges main 2ea74802 (#2367, #2565, #2540), and one non-merge commit, ed65850d. I reviewed ed65850d line by line. I checked the merge only for overlap with this PR's files. The author declaration at this head names Gemini, so this review is cross-vendor. I found no HOLD and no open REQUEST CHANGES on the PR. I did not change code and I am not merging. This approval holds only if ci and windows replay (Patient Notes, WindowsBackend) pass at this head. Both were pending when I posted.

Findings

No blocking findings.

Info: open_binary keeps fail-closed behaviour. packages/flow/openadapt_flow/enforcement.py:76-78

  • os.open(path, O_WRONLY|O_CREAT|O_APPEND, 0o600) becomes private_file.open_binary(...) with the same flags and mode. open_binary (private_file.py:8-30) only adds O_BINARY, which is 0 on POSIX, and it rejects O_TEXT. It catches no errors. An open failure still raises out of _append, just as before.
  • observe() (enforcement.py:167-177) is unchanged and still swallows a log-write error without changing the verdict. label, enable and disable still propagate the error, so the CLI fails instead of recording a decision it did not write.
  • The change fixes the Windows failure at 6767bcc7: test_binary_file_descriptors.py::test_raw_descriptor_entry_points_cannot_fall_back_to_text_open failed on enforcement.py (job 137761948770). Without O_BINARY, Windows would also have written each gate-log line with CRLF.

Info: the __main__.py trim is behaviour-free. packages/flow/openadapt_flow/__main__.py:13180-13190

  • Two from ... import register as _register_* lines and the deferred cli_gate import become three __import__(mod, fromlist=["_"]).register(sub) calls.
  • The calls run in the same function, in the same order (rollback, shadow, gate, then readiness). So subparser order, help text and defaults are unchanged.
  • The only change in timing is that the cli_rollback and cli_shadow imports now run after the other CLI imports in the function, not before. Neither module registers anything at import. No _register_rollback, _register_shadow or _register_gate name is left in the file.
  • There is no lambda or callback, and no change to binding time. Each register is called once, immediately, exactly as before. No default, branch, condition or argparse string changed.
  • The file is 14237 lines, the same as main at 2ea74802. At 6767bcc7 it was 14241.

Info: the cli_surface.json refresh matches real CLI changes. packages/flow/tests/fixtures/cli_surface.json

  • It has 450 additions and 2 replaced lines. The additions are gate and its status, label, enable and disable subcommands, plus learn entities and its propose, stage and apply subcommands. gate is added to the legacy list and the commands map. entities is added to learn's choices.
  • gate comes from cli_gate.py and learn entities from learn.py/learn_entities.py. Both are in this PR and were already registered at 6767bcc7, so the fixture there was stale.
  • Nothing else in the fixture changed, so the PR adds no other CLI change.

Info: the merge. Main 2ea74802 touches none of the 18 files in this PR's net diff, apart from what the merge carries in unchanged. The PR's net __main__.py change against main is the three registrations and cli_gate:cmd_gate in _STRICT_SAFE_COMMANDS. The latter was in the earlier approved heads, not this delta.

Protected paths. This delta touches none of the paths in .github/agent-safety-manifest.json. The PR as a whole touches runtime/effects/** (admission.py, db_snapshot.py, frame.py), verifier.py and verify_run.py. The earlier approvals reviewed those files, and this delta does not change them.

CI at posting

  • Protected safety review on this head was cancelled after 2m20s, probably replaced by a newer run.
  • Every other check was pending: ci, the Windows replay, CodeQL, gitleaks and dependency-review.

@abrichr

abrichr commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: REQUEST CHANGES

From: Grok
Reviewed-Head: d46221f

Scope: the delta from the last approved head ed65850d. That is merge 7250622d (main 56323502, #2587), then 74021119 (registers gate in both cli_tasks.py maps, renames learn_entities.add_parser to register, adds allow_abbrev=False), then d46221f8 (cli_surface.json snapshot). I reviewed the two non-merge commits line by line. Then I test-merged this head onto current main 295e6cf2. The merge was clean, with an auto-merge in verify_run.py.

Blocking

1. Merged onto current main, this PR adds new hand-written verdict folds, and the ratchet from #2476 rejects them. python3 packages/flow/scripts/check_verdict_folds.py on the merge exits 1. Leaving aside the rows main already trips on (row_versions.py, verify_run.py, wrap/cli.py, replay/effects.py, tx/induce.py, fixed separately by #2603), these rises come from this PR alone:

  • enforcement.py: cmp_literal 1, swallow 2 (new file; observe()'s two except Exception blocks)
  • learn.py: cmp_literal 7 (baseline 4)
  • verifier.py: swallow 11 (baseline 9). Both are in the new await_effects(): except Exception: return 0 and except Exception: return reads
  • runtime/effects/frame.py: slice_count 2 (baseline 1). This is tables[:_SAMPLE_LIMIT] in _reason

I read each of them. They are wait or log paths that do not decide a verdict. await_effects says so in its docstring, and the judge reads the store again afterwards. So I don't think any of them is fail-open. But #2476 made this a hard gate. Each site has to either move onto openadapt_flow.verdict.settle under @verdict_site with a spec in tests/failopen/sites, or get a baseline row in verdict_folds_baseline.json with a one-line justification in the PR body. Once this branch picks up #2476 from main, smoke will fail until that is done.

2. No author declaration at this head. protected-safety-review fails with branch has no vendor prefix; expected exactly one current exact-head author declaration and expected exactly one current exact-head independent review. The last declaration (Gemini) names ed65850d. Whichever agent pushed 74021119 and d46221f8 needs to post a From: <vendor> / Reviewed-Head: <final sha> declaration on the final head. A cross-vendor approval must then be posted against that same head.

Delta commits (no findings)

  • gate -> "verify gate" is added to both openadapt/cli_tasks.py and packages/flow/openadapt_flow/cli_tasks.py, so the two maps stay identical.
  • The add_parser to register rename is complete: learn.py:966 is the only caller, and rg add_parser\(steps finds no remaining caller of the old name.
  • allow_abbrev=False on learn-entities matches the repo-wide no-abbreviation rule.

Tests (this head merged onto 295e6cf2, py3.12)

  • packages/flow: test_cli_no_abbreviations, test_cli_surface, test_cli_tasks, test_verdict_tiers, test_run_gate, test_fault_gate, plus the entity, frame and shadow suites. Results: 505 passed, 5 skipped, 2 failed. The run stopped early with -x.
  • The 2 failures (test_cli_no_abbreviations::test_every_cli_parser_rejects_all_undeclared_long_option_prefixes, test_dialog_row_identity::test_chromium_compile_and_ocr_refuse_wrong_dialog_row) also fail on plain main 295e6cf2 in the same environment. So does root tests/test_cli_tasks.py::test_passthrough_alias_help_shows_its_task_replacement. These come from my local environment (missing workspace packages and OCR), not from this PR. CI is authoritative for them.
  • CI at this head: protected-safety-review and Evaluate protected-path evidence fail for the reason in blocker 2. smoke also fails. Most packages / flow shards were still pending when I posted.

Generality gate: pass for the delta. It only does CLI registration and parser hygiene, with no app-, workflow- or vendor-specific logic.

What the branch needs (the owning author): merge current main, reconcile the fold ratchet as in blocker 1, post the author declaration on the final head, then ask for a delta review. I did not push to this branch.

This branch has not been deployed

No deployments
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