Skip to content

fix(tx): count read-only POSTs as reads, from protocol or sandbox evidence - #2397

Merged
abrichr merged 6 commits into
mainfrom
claude/tx-post-reads-2389
Nov 23, 2026
Merged

abrichr merged 6 commits into
mainfrom
claude/tx-post-reads-2389

Conversation

@abrichr

@abrichr abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member

Fixes #2389.

Problem

Reads sent as POST (batch reads, GraphQL queries, search forms, AJAX fragments) used up the tap's single mutation, so the real save got 403. With a relaxed budget the extra "mutations" made a correct save REFUTED. Reproduced on a live Medplum through the merged engine, without model calls. The sequence was a batch read, a GraphQL query, then the PUT save:

statuses outcome
merged, max_mutations=1 200, 403, 403 REFUTED (wire_target_mismatch, wire_collateral_mutation, wire_mutation_blocked)
merged, max_mutations=None 200, 200, 200 REFUTED (wire_evidence_incomplete, wire_target_mismatch x2, wire_collateral_mutation)
this PR, either budget 200, 200, 200 UNCERTAIN (unqualified_observation_only only)

Change

Three general classes. No engine code names an app.

  1. GraphQL on an operation-style route. /$graphql is classified like /graphql. Mutations stay mutations, and graphql elsewhere in a path doesn't count.

  2. Read-only batches. A Bundle of type batch or transaction whose every entry is a GET or HEAD is a read. Another method, a resource body, a missing method or an empty batch makes it a mutation.

  3. Declared read routes, learned on a sandbox. Any other POST is a read only when it matches a ReadRoute in WirePolicy.read_routes: the method, the exact path, and every top-level body field that was constant across the observed samples. A write on the same RPC route with a different operation field never matches. probe_read_routes learns the routes as follows:

    • It replays every POST route in a trace on a loopback sandbox.
    • Before each replay, a caller-supplied reset restores the pre-trace state.
    • A route is accepted only if the store was idle, every replay returned 2xx, and a caller-supplied digest of the whole store did not change.
    • Credential-like body fields are never stored.

    Without the reset, replaying a recorded save writes a value the store already holds and looks like a read. A test pins that.

Known limit (stated in the module docstring): a write route whose every recorded sample wrote values the pre-trace store already held can't be told from a read. That request changed nothing in the trace either, and the engine's read-back of the target still judges any write that reaches it.

Tests

  • tests/test_tx_post_reads.py runs in CI against a local SQLite-backed server, so the digest is exact. It covers the static classes, read-route matching (body discriminator, forms, repeated names), and the probe. The probe learns search and RPC reads, refuses the save, refuses everything if the store isn't idle, refuses a non-loopback sandbox, and never stores a CSRF field. With the learned routes, the save goes through and an RPC write is still blocked by the budget.
  • tests/test_tx_post_reads_live.py is lab only. It needs OAFLOW_TX_LIVE_MEDPLUM_STATE and uses the benchmark/fhir_medplum fixture; the digest hashes all 524 tables in the app's Postgres. All 5 pass:
    • batch and GraphQL reads are reads;
    • POST /fhir/R4/Patient/_search is learned as a read, and the save is not probed as one;
    • a save after three POST reads goes through, a second write gets 403, and the stored value is the intended one;
    • the learned route doesn't cover a POST create;
    • the engine's wrap_action returns UNCERTAIN with only unqualified_observation_only.
  • The rest of the tx suites pass on the lab's WSL (242 passed). The one failure is the flaky background-writer count in test_o1_write_completeness_with_10k_rows_and_5_concurrent_writers, which also fails on unchanged code under load.

Not in this PR

OpenEMR's form/AJAX POSTs have not been run live; they need a logged-in session, and the declared-route path is the same. Also not here: the RPC read template (#2390), NULL versus '' (#2391), and redirects (#2392).

…dence (#2389)

Reads sent as POST used up the tap's single mutation, so the real save got
403, and with a relaxed budget the extra mutations made a correct save
REFUTED (wire_evidence_incomplete, wire_target_mismatch,
wire_collateral_mutation). Reproduced on a live Medplum through the merged
engine: a batch read, a GraphQL query and a PUT save gave [200, 403, 403]
and REFUTED; relaxed, [200, 200, 200] and REFUTED.

Three general classes, none naming an app:

- A GraphQL query on an operation-style route (/$graphql) is classified
  like one on /graphql. Mutations stay mutations.
- A batch Bundle (batch or transaction) whose every entry is a GET or HEAD
  is a read. Any other entry, a resource body, or a missing method makes
  it a mutation.
- Any other POST is a read only when it matches a ReadRoute declared in
  WirePolicy.read_routes: method, exact path, and every top-level body
  field that was constant across the observed samples, so a write on the
  same RPC route with a different operation field never matches.

probe_read_routes learns those routes. It replays each POST route of a
trace on a loopback sandbox, after a caller-supplied reset to the
pre-trace state, and accepts a route only when the store was idle, every
replay returned 2xx and a caller-supplied digest of the whole store did
not change. Without the reset, replaying a recorded save writes the value
the store already holds and looks like a read; a test pins that.
Credential-like body fields are never stored in a route.

Live on Medplum (every table in the app's database hashed): batch and
GraphQL reads are reads, POST /Patient/_search is learned as a read, the
PUT save is not, a wrapped save after three POST reads goes through with
a second write still blocked, and the engine returns UNCERTAIN with only
unqualified_observation_only.

Signed-off-by: abrichr <richard.abrich@mldsai.com>
@abrichr

abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: REQUEST CHANGES

From: Grok
Reviewed-Head: 05e8bde

The static classes are fail-closed: /$graphql is classified like /graphql (only the last path segment, after stripping $), mutations stay mutations, and a Bundle is a read only when its type is batch or transaction, every entry is GET/HEAD with a URL, and no extra resource body is present. The sandbox probe requires a loopback URL, an idle store, a caller reset before every replay, 2xx, and an unchanged whole-store digest, and it never stores credential-like fields. The stated no-op-write limit is honest.

matches_read_route is fail-open when body_equals is empty: all(()) is true, so a learned route with only a method and path matches any body on that path, including a write. _constant_fields can return () when every top-level field varies or is stripped as a credential (a realistic form POST that shares a path with the save). In that case the live save would be counted as a read and would not use the mutation budget. Do not learn a route with empty body_equals, or require a non-empty discriminator / an exact sample-body match; keep the request a mutation until then. Add a test for a write on the same path after a discriminator-less learned route. Not merged, as requested.

Pointer: openadapt-internal#211

Review finding: with an empty body_equals, all(()) matched any body on the
route's path, so a write sent to a path that also served reads would be
counted as a read and skip the mutation budget. _constant_fields returns
() when every field varies or is a credential, which a form that both
searches and saves produces.

ReadRoute now rejects an empty body_equals, matches_read_route skips any
such entry, and probe_read_routes refuses a route whose samples share no
constant field. A test pins that a write on such a path still uses the
budget.

Signed-off-by: abrichr <richard.abrich@mldsai.com>
@abrichr

abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: cc1567a

The empty-body_equals finding at 05e8bdef is fixed. ReadRoute no longer defaults to an empty tuple; a validator rejects one; matches_read_route skips any entry that still has one; and probe_read_routes refuses a path whose samples share no constant field, with the stated reason, so nothing is learned. GraphQL/Bundle fail-closed behavior is unchanged in this increment.

The new tests cover constructor rejection and a probe that learns nothing. Residual only: the follow-up write in test_probe_refuses_a_route_whose_bodies_share_no_field is sent to /rec/1, not to the same /search path the request described. read_routes == () already holds the safety property, so this is not a block.

Do not merge: the request was review only.

Pointer: openadapt-internal#211

# Conflicts:
#	CAPABILITIES.md
#	llms-full.txt
@abrichr

abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: 8b552c9

Delta re-review after the approved head cc1567a474bcead4a7b72c01930ad2e90386efc2. Author is still GitHub abrichr, not Grok. I will not merge.

The only unique commit is a merge of origin/main (dc5de98cb). The conflicts were in CAPABILITIES.md and generated llms-full.txt. This PR's lens row keeps its GraphQL and declared-read-route wording, then inserts main's #2392 sentence immediately before "Not checked:". wire_tap.py took main's redirect change in the merge (90 insertions); I did not re-litigate that #2392 behavior here.

Review only; I am not merging.

# Conflicts:
#	CAPABILITIES.md
#	llms-full.txt
@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Claude (independent reviewer)
Reviewed-Head: c9ff9a8

No blockers.

  • The only new commit since 8b552c9 is c9ff9a8, a merge of origin/main (e754d56). It had no conflicts in this PR's files.
  • The PR's net diff under packages/ against its merge base with main is identical to the approved head's net diff, line for line except hunk offsets: tx/__init__.py, tx/read_probe.py, tx/wire_tap.py, tests/test_tx_post_reads.py, tests/test_tx_post_reads_live.py.
  • In CAPABILITIES.md and llms-full.txt, the PR only adds its two sentences (GraphQL or batch-GET reads, and declared read routes from probe_read_routes). Every sentence on main's version of that row is still there.
  • Tests at c9ff9a8: all tests/test_tx_*.py give 291 passed, 11 skipped.

@abrichr abrichr closed this Nov 22, 2026
@abrichr abrichr reopened this Nov 22, 2026
# Conflicts:
#	CAPABILITIES.md
#	llms-full.txt
@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Claude (independent reviewer)
Reviewed-Head: 2edefbd

The only new commit since the approved head c9ff9a80e is 2edefbda5, a merge of origin/main at a23cf5a65 (the current main tip). The merge is clean.

  • Packages unchanged. git diff $(git merge-base origin/main <head>) <head> -- packages is byte-identical at the old and the new head (1,264 lines). Main changed no file this PR touches (tx/__init__.py, tx/read_probe.py, tx/wire_tap.py and the two test files).
  • Net diff against main is only the PR. git diff a23cf5a65 2edefbda5 touches the same 7 files with the same +1140/-8 as before the merge. Main's content is not dropped or reverted anywhere, and no conflict markers remain.
  • CAPABILITIES.md tx-lens row. I compared the row sentence by sentence. All 3 of the PR's sentences are present: GraphQL and batched GET count as reads, another POST counts as a read only on a declared read route, and probe_read_routes uses a whole-store digest. None of main's sentences were lost, including fix(tx): learn NULL versus empty string per field as a codec #2402's redirect sentence and its null_empty codec sentences. This is the only line that differs from main.
  • llms-full.txt. Running python scripts/build_llms_full.py in a detached worktree at this head leaves the tree clean, so the committed file matches the generator.

@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Gemini
Reviewed-Head: 2edefbd

Independent cross-vendor adversarial review of 2edefbda5cb4c54bc81c5c3b84b7d84b980d0ec4 (Claude-authored claude/tx-post-reads-2389; I authored none of this PR).

Findings & fail-closed verification

  • Static protocol classes (wire_tap.py):
    • _is_graphql_path matches only the final path segment (/graphql or /$graphql); GraphQL mutations remain mutations via _classify_single_graphql_doc and are never downgraded by read_routes (not is_gql_mut in _begin_request).
    • _is_read_only_batch accepts only a Bundle of type batch/transaction with a non-empty entry list where every entry has only {"request", "fullUrl"} (no resource body), request.method in {"GET", "HEAD"}, and a string request.url.
  • Declared read routes (ReadRoute, matches_read_route, _top_level_body_fields):
    • ReadRoute enforces extra="forbid", frozen=True, and rejects empty body_equals via @field_validator, preventing a read route from matching arbitrary writes on the same path.
    • _top_level_body_fields rejects duplicate form parameter names and non-UTF-8 form bodies; _begin_request only applies matches_read_route to allowed origins and non-GraphQL mutations.
  • Sandbox probe (read_probe.py):
    • Enforces loopback sandbox via _assert_sandbox_url_allowed, verifies store idleness before probing (read_routes returns () if not idle), calls store_reset() before every sample replay, uses _NoRedirect and allowlisted headers, strips credential-like keys (_CREDENTIAL_KEY_RE), and accepts a route only if all replays return 2xx with unchanged whole-store digests and at least one constant body discriminator. Known no-op-write limit is documented honestly.

Generality gate

  1. No per-app code in engine: Verified; handles standard GraphQL/FHIR-Bundle protocol structures and data-driven WirePolicy.read_routes learned by probe_read_routes.
  2. Fix the class, not the instance: Verified; classifies read-only POST requests across GraphQL operation routes, read-only bundles, and sandbox-proven RPC/search routes under one fail-closed wire-tap policy.
  3. Held-out evidence: Verified via local SQLite-backed CI tests (test_tx_post_reads.py) and live Medplum Postgres 524-table digest tests (test_tx_post_reads_live.py), with OpenEMR honestly noted as not yet run live.

# Conflicts:
#	CAPABILITIES.md
#	llms-full.txt
@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Gemini
Reviewed-Head: 97b093c

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

  • Only CAPABILITIES.md and llms-full.txt conflicted; resolved cleanly preserving both main (#2437 row) and #2397's wire-tap read-route note, and regenerated llms-full.txt via scripts/build_llms_full.py.
  • All PR code and test files (openadapt_flow/tx/read_probe.py, openadapt_flow/tx/wire_tap.py, openadapt_flow/tx/__init__.py, tests/test_tx_post_reads.py, tests/test_tx_post_reads_live.py) merged cleanly without conflict.
  • Targeted pytest (tests/test_tx_post_reads.py, tests/test_tx_post_reads_live.py): 21 passed, 5 skipped (opt-in live tests).
  • Generality & durability: protocol/sandbox-evidence-driven read classification across apps, no per-app engine code.

@abrichr
abrichr merged commit 463c801 into main Nov 23, 2026
57 of 58 checks passed
@abrichr
abrichr deleted the claude/tx-post-reads-2389 branch November 23, 2026 00:31
abrichr added a commit that referenced this pull request Nov 23, 2026
#2518 split the page into capabilities/ row files from main as of 17:13 EST.
Six later first-parent commits updated the stamped page and were kept on
merge, but not the row files, so the stamp test and refresh workflow went
red (#2562). Fold those edits into the sources and regenerate the stamp.
CAPABILITIES.md is unchanged.

- #2471: VERIFIED-run prose; fault-test and openIMIS measured-results notes
- #2477: grade file/PDF/email checks by artifact provenance
- #2472: api_recheck row
- #2397: GraphQL / probe_read_routes text on the transaction-lens row
- #2363 / #2368: second-factor row, including terminal/Windows console

Fixes #2562
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.

tx: classify read-only POSTs so they don't use up max_mutations

1 participant