Skip to content

perf(flow): a checkpoint store derives its at-rest key once per run (28 s -> 3.5 s quickstart) - #2375

Merged
abrichr merged 4 commits into
mainfrom
claude/checkpoint-store-key-memo
Nov 21, 2026
Merged

abrichr merged 4 commits into
mainfrom
claude/checkpoint-store-key-memo

Conversation

@abrichr

@abrichr abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member

This changes a control approved in #2341. #2341 removed the process-wide derived-key cache on purpose and accepted the extra KDF work. On the quickstart, that cost is large. Every sealed checkpoint read runs scrypt again: one headless MockMed run makes 131 derivations, about 20 of its 28-29 s. On Flow 3.3.0 the same run takes 3.5 s.

The change. crypto.ContainerKeys is a derived-key handle owned by one caller, and a CheckpointStore creates one for its own lifetime. Keys are stored by (salt, n, r, p).

  • The handle never copies the passphrase and keeps no verifier, hash or MAC. The store passes its passphrase on every call. rebind keeps only a reference to the key object the store already holds, and drops every key when that object changes.
  • It is used only when the store has an explicit key. A store that reads the key from the environment derives on every read, as before.
  • It adds no module-level state, and test_repeated_payload_operations_do_not_use_a_global_cache is unchanged.
  • Every read still authenticates the container on disk with AES-GCM. The wrong-key and tamper tests are unchanged.
  • The wire format and scrypt parameters are unchanged.

Tests. The three tests that pinned per-operation counts now pin the new property:

  • one store derives once for a whole run;
  • a second store on the same run derives again;
  • a changed key drops the keys;
  • a store without an explicit key keeps none;
  • a durable run derives 1-3 times, and a fresh reader once more.

559 crypto, at-rest, durable and sealed tests pass locally.

Bisect (first-parent, from openadapt-flow-v3.3.0 to main d10f0a2b). Each figure is one headless MockMed run (record_tutorial, compile_recording, then run_tutorial_workflow, as in openadapt.quickstart.engine.FlowEngine). Runs used a CPU slot at load 9-15.

commit seconds
3.3.0 c6748ab1c 4.18, 3.41, 3.49
d93fa43e7 (parent of #2341) 4.12, 3.43
95cc7878b (#2341) 28.20, 27.87
main d10f0a2b 28.72, 29.05, 49.83
main + this PR 4.26, 3.50, 3.64 (1 derivation)

A cProfile of main shows Scrypt.derive ×131 = 19.98 s. The derivations come through CheckpointStore._read_json → decrypt_bytes, mostly from continuation_state_digest and checkpoints() rescans in _sync_authority and update_audit_evidence. All 117 read derivations are on the single store created at runtime/replayer.py:1966.

The #2341 bullet in the v3.4.0 changelog is amended, not given a new Unreleased entry. This must ship in the same release as #2341: Flow 3.4.0 is not out yet, and without this change it would ship 8x slower. docs/phi_at_rest.md is updated, and llms-full.txt is regenerated.

Merge order: after #2371 (privacy 1.1.4 and the Flow hold), before the Flow privacy-floor PR.

#2341 removed the process-wide derived-key cache, so every sealed
checkpoint read ran scrypt again: 131 derivations and 28 s for the MockMed
quickstart run, against 3.5 s on Flow 3.3.0. A CheckpointStore is a
caller-owned handle; it now keeps the keys it derived from its explicit
key (crypto.ContainerKeys, keyed by salt and work factors, no passphrase
copy or verifier, dropped when the key changes) for its own lifetime.
Every read still authenticates with AES-GCM. No module-level state.
@abrichr

abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: 1214606

This is a caller-owned handle, not a return of the process-wide cache #2341 removed.

  • No module state: ContainerKeys is an instance on CheckpointStore; test_repeated_payload_operations_do_not_use_a_global_cache still asserts not hasattr(crypto, "_DERIVED") and that two encrypts plus two decrypts with a shared salt still derive four times when no handle is passed.
  • The handle stores only derived AES-256 keys, keyed by (salt, n, r, p). It does not copy the passphrase and keeps no verifier, hash or MAC. rebind holds a reference to the store's key object and clears the map when that object changes (is, not equality).
  • _keys() returns None unless the store was constructed with an explicit key, so an environment-key reader still derives on every read (tested).
  • Every encrypt_bytes / decrypt_bytes path still uses AES-GCM with a fresh nonce; a wrong passphrase on another store, and a flipped ciphertext after a cached successful read, both raise DecryptionError. Wire format and scrypt parameters are unchanged.
  • The changelog amends the existing Remove global credential and plaintext caches #2341 bullet under dated ## v3.4.0 (2026-11-20) rather than adding ## Unreleased, which is right for a fix that must ship in the still-unreleased 3.4.0 cut.

I am not merging: the public OpenAdapt merge HOLD stands until the privacy-floor PR merges.

Logged on openadapt-internal#211.

@abrichr

abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: af740fd

The only change since the APPROVE at 1214606c73aec0faad7f110f06178444bdeddf44 is a merge of main (b167a357, #2371). crypto.py, checkpoint.py and the derivation tests are byte-identical to that head. ContainerKeys remains a caller-owned handle: no module state, no passphrase copy, used only with an explicit store key, keys dropped on rebind, every read still AES-GCM.

The changelog still amends the #2341 bullet under dated 3.4.0. The heading now matches main: ## v3.4.0 (held until PyPI serves openadapt-privacy 1.1.4).

I am not merging in this pass. #2371 has landed, but the coordinator's merge order still waits on the packages-gate run and then the Flow floor PR before later Flow PRs go in.

Logged on openadapt-internal#211.

@abrichr

abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: b05f97f

Delta re-review after my APPROVE at af740fd6bcb5660656c4df35ff45db5b7f9a4c33. Author is still GitHub abrichr, not Grok. I will not merge.

First-parent commits since that head: a merge of current main, then b05f97f6c. The only unique source change is one word in the ContainerKeys docstring in packages/flow/openadapt_flow/crypto.py: "on one MockMed run" is now "on one tutorial run". Behavior, key lifetime, and the no-passphrase-copy contract are unchanged. Unique-versus-main is still the previously approved checkpoint-key memoization plus this wording fix.

What I ran

  • Confirmed the PR is OPEN, not draft, head still b05f97f6cab81906e0ca7260cd1d1ea8fbd43a56. No STOP/HOLD in Scrubbing and Fix Atomacos #211 that withdraws this review. No Grok verdict at this head.
  • Read git show b05f97f6c (one file, one substitution) and git diff origin/main...HEAD (still the six previously approved paths, with that docstring word updated).

Review only; I am not merging.

@abrichr
abrichr merged commit 66e9277 into main Nov 21, 2026
57 of 58 checks passed
@abrichr
abrichr deleted the claude/checkpoint-store-key-memo branch November 21, 2026 07:58
yunusarda-sudo pushed a commit to yunusarda-sudo/OpenAdapt that referenced this pull request Nov 22, 2026
….1.4

Keep OpenAdaptAI#2375's amendment of the OpenAdaptAI#2341 note and this branch's notes side by
side, note the Flow merges since (OpenAdaptAI#2383, OpenAdaptAI#2439, OpenAdaptAI#2451, OpenAdaptAI#2454-OpenAdaptAI#2456, OpenAdaptAI#2459),
and relock packages/flow on openadapt-privacy 1.1.4 (was 1.0.0).
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