Skip to content

feat(verify): openadapt verify draft — a model drafts the contract, qualification admits it - #2438

Open
abrichr wants to merge 13 commits into
mainfrom
claude/verify-draft
Open

abrichr wants to merge 13 commits into
mainfrom
claude/verify-draft

Conversation

@abrichr

@abrichr abrichr commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Summary

openadapt verify draft hands a new system's write contract to a model at setup time. OpenAdapt admits the contract only after it qualifies it on a sandbox. Runs stay deterministic: an admitted contract is a plain openadapt.verify/v1 file and makes no model calls.

  • Inputs. The model gets four things:
    • the system's description: OpenAPI, GraphQL, a FHIR CapabilityStatement, the introspected database schema (db), or none;
    • one HAR trace, with sign-in headers and secret-looking fields masked;
    • the rows that write moved when replayed once in the sandbox;
    • the task.
  • Model. Pinned to claude-opus-5-5 at effort medium with prompt verify-draft-prompt/2. The key comes from ANTHROPIC_API_KEY. The model, its response model id and the prompt's SHA-256 go into the contract's provenance.
  • Draft filter. It is an allowlist. A draft may only read the database with plain SQL SELECTs, and may not use ${...}. A REST read, URL, credential, hook, app profile, admission or learned section is refused before anything runs.
  • Admission. The draft is admitted only if it passes all three:
    • planted faults: wrong value (each written column), wrong row, duplicate, sibling record edit, late revert (refuted at the recheck), and lost update (the other user saves first through the app, then the agent's save);
    • 5 clean writes and their rechecks;
    • the causal-attribution probe: 2 users x 2 payloads, plus a repeat of the first write. It attributes each moved column to the values written, the user, the clock or nothing. The contract's bookkeeping exclusions must agree: an exclusion may not cover a column that follows the values written, nor a column that never moved.
  • Feedback. Failures go back to the model, at most 3 rounds. Then the command refuses and says why.
  • The one question. A person is asked at most once, only when a planted fault could go only into excluded columns. With no answer, the draft is refused.
  • Untestable cases. A case the sandbox can't build (no second user, no other record) is never a pass. It stops the command with the operator fix.
  • Edits after admission. An edit to the check voids the admission (--check).

Docs: packages/flow/docs/VERIFY_DRAFT.md. Protocol: benchmark/verify_draft/PROTOCOL.md. Results: benchmark/verify_draft/RESULTS.md. The CAPABILITIES row sits next to the contract generator.

Measured

App Tuned on? Setup minutes Person decisions Drafts admitted / rejected Planted faults caught, admission / held out False stops, admission / held out Model USD
MockLoan (create, SQLite) yes 1.4 0 1 / 0 8/8 / 10/10 0/5 / 0/5 0.05
Workbench, MockMed profile (update, PostgreSQL) yes 1.3 0 1 / 0 6/6 / 6/6 0/5 / 0/5 0.08
Mealie 2.8.0 (update), first valid run no 9.2 0 0 / 3 best 6/8 / not run best 0/5 / - 0.58
Directus 11.5.1 (update) no 6.0 0 1 / 1 7/7 / 8/8 0/5 / 0/5 0.17

Verdict: the kill criterion fires on time.

  • On Mealie no contract was admitted. Its store keeps no version, author or save history, so a lost update can't be seen there, and qualification refused every draft.
  • On Directus the first draft passed every case, including a lost update through the app. It was admitted in round 2 at 6.0 minutes.
    • Round 1 was lost to a qualifier bug found in review after the run. The agent signed in inside the demonstration's window, so the model saw the sign-in's rows (last_access, a session) as part of the write and excluded them.
    • The committed contract's directus_sessions: skip would be refused by the fixed rule.
    • The lost update was refuted by frame.versions ("another user saved a version of the target inside the window"), checked in the case evidence.
    • Fixed since the run: every actor signs in before the first read, a skipped or append-only table that never moved is refused, and secret-named column values are masked. Directus was not rerun after the fix.
  • No admitted contract missed a planted fault: 24/24 held out.
  • Decisions were 0 everywhere. Total model spend was USD 2.52 of the USD 30 cap.
  • An admission round takes 130 to 180 s on the lab apps over a tunnel. That, not the model, is now the time constraint.

Earlier runs are kept under results/void and results/v1, listed in RESULTS.md with what changed after each. The qualifier was changed after Mealie's first valid run, before Directus was touched, and the addendum lists every change.

Generality

  1. No per-app code in the engine. openadapt_flow/verify_draft/ names no app. The per-app knowledge is the model's draft (data) and the operator's sandbox.yaml (facts). App setup lives only in benchmark/verify_draft/apps.py. scripts/check_engine_generality.py passes.
  2. One model for the class. One admission suite works for any SQL store (SQLite, PostgreSQL, MySQL). It resets by restoring the rows read at the start, with no reset hooks.
  3. Held-out evidence. Mealie and Directus were never set up before. Directus came after the code and prompt were frozen and pushed (afc9d38).

Tests

packages/flow/tests/test_verify_draft.py, 19 tests. They use MockLoan in-process with a fixed drafter, so no model is called. They cover:

  • admission and its provenance binding;
  • a weak draft refused, with its failures fed back;
  • the 3-round cap;
  • unsafe drafts that never run;
  • the allowlist (REST read, auth, ${ANTHROPIC_API_KEY}, profile, admission, learned);
  • untestable is never a pass;
  • the one question, and refusal without an answer;
  • sign-in before the first read;
  • a skip of a table that never moves is refused;
  • the spend ledger;
  • input masking;
  • that the run path imports neither the Anthropic SDK nor the draft code.

Also run locally: launcher test_cli_root_task_router.py and test_cli_tasks.py (483 passed), flow test_import_layers.py and test_qualify_business_rule.py (2250 passed), mypy on the new package, and ruff.

Install

The command talks to the app through httpx, a base dependency. Drafting needs pip install anthropic, and a PostgreSQL or MySQL sandbox needs its DB-API driver, as any SQL contract does. Without the SDK the command stops before drafting.

Not covered

  • Systems reachable only through an API: the late-revert case needs the whole-database recheck.
  • A legitimate concurrent edit by another user.
  • A write to another record among the clean probes: the literal-pin rule covers that instead.
  • A grader written by someone else.

…ualification

openadapt_flow.verify_draft: the pinned model drafts an openadapt.verify/v1
contract from the system description, one trace and the task; the sandbox
qualifies it (planted faults, five clean writes, a two-actor by two-payload
attribution probe) and feeds failures back, at most three rounds. Prompt
frozen here before the untuned app (verify-draft-prompt/1).
…rite, preflights the read account; launcher command; docs; benchmark runs on MockLoan, workbench and Mealie
…fts, marks unbuildable cases untestable; protocol addendum before Directus
@abrichr

abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: b837cf5

First review of this PR, at the head named in #211 (7060086856). Author is GitHub abrichr, not Grok. Review only; I am not merging.

The draft allowlist is an allowlist. _disallowed / prepare refuse unknown top-level, verifier, frame, database, versions and attribution keys, require kind: sql with a sql_query on every declared read, reject ${...} in the parsed document, run guard_problem / assert_read_only_sql on each query, and then overwrite every read's connection with the sandbox's. test_drafts_are_allowlisted covers a REST verifier, a bearer_env, a ${...} literal, a database profile, and extra admission/learned keys. Effect keys are not recursively allowlisted; they are still constrained by Contract.from_dict (record_written / field_equals / exact_new_set and match literals), and a ${...} in an effect still fails prepare.

Fail-open admission is mostly closed. _judge sets passed=False for untestable cases first, so they cannot admit (test_untestable_is_never_a_pass). A refused write on a fault is marked untestable, not a pass. The only intended not-applicable passes are a create's lost-update (nothing to overwrite) and an app that refuses the stale save. Clean writes the app refuses are skipped, not counted as passed.

Attribution: _cause labels payload vs actor vs clock/fresh/constant from the 2x2 plus a repeated first write. An exclusion that moves with the written values is a problem (excluded, but it moves with the values written), and test_attribution_flags_masks covers that. A whole-table skip is the same mask.

The run path is independent of the model. prepare and Qualifier.run drop provenance. QUALIFY_ONLY_SECTIONS in verify_run.py now includes provenance, so verify-run ignores it. test_run_path_makes_no_model_calls runs begin/verify in a subprocess with anthropic blocked and asserts openadapt_flow.verify_draft is never imported.

RESULTS.md's headline arithmetic matches its table (held-out 10+6+8 = 24 faults, 5+5+5 = 15 clean writes; Mealie 9.2 min not admitted; Directus 6.0 min admitted in round 2; USD 0.05+0.08+0.58+0.17 plus the stated void/dev spend = 2.52). I did not re-parse every raw result.json.

Residual only: if the demonstration has no task-value columns, the wrong_value fault is created as not applicable and _judge counts that as a pass. That is a narrow path; the demonstrated writes I looked at do carry task values.

…ses an unmoved table exclusion, masks secret columns, uses httpx
@abrichr

abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: 946316c

Delta review from b837cf54ab11f2d9d0aa8de3b3c3c7333503a6c5 (my earlier APPROVE, 7060133618), at the exact head named in #211 (7060174635). One unique commit. Author is GitHub abrichr, not Grok. Review only; I am not merging.

The four requested points still hold. The allowlist, ${...} refusal, sandbox rewrite of SQL reads, fail-closed untestable path, and QUALIFY_ONLY_SECTIONS / test_run_path_makes_no_model_calls are unchanged in this commit.

Sign-in now happens before the first store read. App.sign_in_all walks every configured actor, and both draft.run and the held-out benchmark runner call it before store.read. Qualifier cases reuse that same App, so later writes do not lazily sign in inside a case window. test_actors_sign_in_before_the_store_is_read pins that the agent and other sessions are created before the first Store.read.

A skipped or append-only table that never moved in the attribution probe is now refused (a skipped table / an append-only table, but it never moved in any probe write: nothing to excuse). Payload exclusions are still flagged when a masked column moves with the written values. The new test refuses a skip of a table that never moves.

Secret-named column values in Demo.moved_text are replaced with *** (the name stays). The sandbox HTTP client is now httpx.Client with follow_redirects=False and the configured timeout; write bodies use content=. I found no leftover requests use under verify_draft.

RESULTS.md still reports the same headline figures (Mealie 9.2 min not admitted, Directus 6.0 min admitted in round 2, 24/24 held-out faults, 0 person decisions, USD 2.52) and now states the corrected cause of Directus's lost round 1. I did not re-parse every raw result.json.

Residuals only (not enough to flip this unique delta): wrong_value with no task-value columns is still a not-applicable pass; the request-body _SECRET pattern and the new column _SECRET_COLUMN pattern are not the same (session/csrf/auth vs hash/salt/otp/tfa).

@abrichr

abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: REQUEST CHANGES

From: Codex
Reviewed-Head: fdd364c

Two cases can satisfy admission without establishing the property named in the report.

  1. A failed HTTP request can count as lost-update protection. After the other user saves, run marks every HTTP status of 400 or greater as untestable. The lost-update case then clears that flag for the entire category. A 503 or an expired session is consequently counted as a pass with the explanation that the application prevented a stale save. Preserve the untestable result for unrelated failures. For a genuine stale-write refusal, verify the conflict and read back the first user's saved value before crediting the application. Cover both a normal conflict and a service outage in the synthetic fixture. HTTP handling · Lost-update classification
  2. A create can be admitted with three attribution writes instead of the required five. The sandbox accepts an agent as its sole actor. For a create, lost-update testing is inapplicable. The attribution planner then silently omits the second actor and the final admission decision checks only the cases it received. Mark the missing actor axis untestable and refuse admission. A single-actor create is a regression case distinct from the existing successful five-write test. Probe plan · Admission decision

The changes since the previous approval register benchmark artifacts and disable abbreviated benchmark arguments after merging main. The protected runner still treats provenance as setup metadata. Both findings are in the qualification path included in this PR. The fixes can apply across applications by distinguishing an observed outcome from a missing observation. The reported 24/24 held-out faults and 15 clean writes concern the three admitted contracts. Those totals do not cover these missing-observation cases. I inspected source, test cases and the published benchmark report without running a model or a sandbox.

The committed results and docs gave per-call and total USD figures. They
now give model calls and token counts. The spend ledgers are gone from
results/, the benchmark's ledger default moves out of the repository, and
run.py drops prices from result.json and the draft report it commits.
fail-open class: absent-evidence-as-success.

- A stale save the app refused counted as lost-update protection for any
  HTTP error. Now only a 409 or 412 that left the other user's save in the
  store is credited; an outage or an expired session stays untestable.
- A sandbox with no second user silently skipped the actor axis of the
  attribution probe, so a create was admitted on three writes instead of
  five. The missing axis is now an untestable case, and admission refuses.
@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Gemini
Reviewed-Head: b982f64

Codex is unavailable until Nov 27, so Gemini (gemini-3.1-pro-preview) reviewed this protected, Claude-authored PR instead. It received the PR summary, Codex's two blockers at fdd364c0, the delta since then (the two fix commits after the merge of main), and the full PR diff against main. Benchmark result data, llms-full.txt and public-artifacts.json were left out for size. Gemini's findings, summarised:

  1. Lost update (Codex blocker 1): fixed. In verify_draft/admit.py, lost_update now credits the app only when every refused status is 409 or 412 and the other user's save is still readable in the store. A 503 or a 401 stays untestable (fails closed). A status list that mixes 409 and 5xx also stays untestable.
  2. Attribution (Codex blocker 2): fixed. _attribute adds an untestable attribution:other case when the sandbox has no second user. That case is never a pass, so admission refuses.
  3. Regression tests cover both fixes: test_a_refused_stale_save_is_credited_only_for_a_kept_conflict and test_a_single_user_sandbox_never_admits.

Gemini also checked:

  • The only_untestable() path.
  • The removal of model prices from the public proof: model_usd is dropped, and usd is filtered out of model_calls.
  • The full PR diff for new fail-open paths. It found none.

The launcher CI runs against the released engine, which has no
verify_draft module, so 'openadapt verify draft --help' exited 1. It now
goes through the same optional-engine entry as wrap and hooks: pure help
when the module is absent, exit 2 for a run, and a broken installed
dependency still fails loudly.
@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Gemini
Reviewed-Head: 1e4d8e7

Delta review by Gemini (gemini-3.1-pro-preview), following its approval of b982f64a. One commit was added since then, to fix the launcher CI. The launcher CI runs against the released engine, which has no verify_draft module, so openadapt verify draft --help exited 1. The command now goes through _optional_engine_entrypoint, as wrap and hooks do. Gemini received the commit and the unchanged helper. Its findings, summarised:

  1. The fix is correct, and the command fails closed. With the module absent, --help shows help, and a run exits 2. A broken installed dependency still fails loudly, because the helper re-raises a ModuleNotFoundError whose name isn't one of the command's own modules.
  2. The tests are updated correctly. tests/test_optional_engine_commands.py now includes verify draft, and the multi-word command is split into argv tokens.

No blockers.

@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Note on the non-required windows replay (Patient Notes, WindowsBackend) failure at 1e4d8e7: the Desktop replay workflow doesn't run on main, so there's no main result to compare. On this PR it passed at b982f64 (run at 00:52 UTC). At 1e4d8e7 it failed in tests/e2e/test_native_keyboard_lease.py::test_native_keyboard_lease_actual_delivery_and_refusal with PermissionError: [Errno 13] Permission denied on a temp lease-replaced.json. The only change between those heads is the launcher's verify draft help path, which that test doesn't exercise. The same job also failed on claude/model-failclosed in the same window, so this looks like a Windows runner file-lock flake, not this PR.

@abrichr abrichr closed this Nov 22, 2026
@abrichr abrichr reopened this Nov 22, 2026

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