Skip to content

fix(handoff): encrypted runs hand off their receipt card; sandbox runs refuse and say why - #2830

Merged
abrichr merged 10 commits into
mainfrom
feat/handoff-receipt-coverage
Dec 1, 2026
Merged

abrichr merged 10 commits into
mainfrom
feat/handoff-receipt-coverage

Conversation

@abrichr

@abrichr abrichr commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

What was found

packages/flow/docs/A2A.md ("When a run has no receipt") listed three kinds of run that hand off with no receipt. Two were checked.

Strict at rest (OPENADAPT_FLOW_AT_REST=strict): the doc was stale, but there was a real bug under it. Flow does write the receipt for an encrypted run. receipt.json is a plaintext structural file of the sealed set, as phi_at_rest.md says. However, the public card (receipt.card.json) is not on the structural list, so it is sealed with the run (receipt.card.json.enc). handoff.receipt_for_run(public=True) read it as a plain file, so get_handoff with public: true failed on every encrypted run with "no receipt.card.json in that run folder". public_reveal had the same plain read of report.json, so an encrypted tutorial run's card silently opened fewer fields than a plaintext one.

Sandbox runs: the gap is real, and I left it closed on purpose. A record/run/v1 receipt has no field that marks a run as synthetic. The demo role lives in the receiver's key registry, not in the signed statement. A sandbox receipt signed with this computer's key would therefore read like proof about a real system. The only no-schema way to mark it would be to downgrade the outcome to UNKNOWN with a made-up reason code, and receivers would not read that as "synthetic". So sandbox runs stay receipt-less. Before this PR the A2A text part said something unhelpful ("no such run in the server's runs directory" or "no receipt.json in that run folder"). Now get_handoff refuses any mode: "sandbox" result before it reads a run folder, and says why.

What changed

  • openadapt_flow/handoff.py: receipt_for_run still reads receipt.json as a plain file, so it needs no at-rest key. With public, on a sealed run it opens the card through sealed_set, in memory. Nothing is written.
  • openadapt_flow/receipt_record.py: public_reveal reads report.json through sealed_set.
  • openadapt_flow/agent/handoff_tools.py: adds SANDBOX_NO_RECEIPT; get refuses sandbox results before touching the run folder.
  • Docs: A2A.md, HANDOFF_RECEIPTS.md, the A2A capability row, plus regenerated CAPABILITIES.md and llms-full.txt. llms-full.txt also picked up some unrelated drift from main when regenerated.
  • packages/agent/tests/test_handoff_e2e.py: the bridge fixture's report now has change_ledger: [] and effect evidence. Without them the receipt is correctly UNKNOWN (incomplete_write_coverage), and test_a2a_run_carries_its_receipt... failed locally on main.

Tests

New:

  • packages/flow/tests/test_handoff_at_rest.py (5): a real sealed strict run. The receipt is written by receipt_after_run. The statement matches a plaintext run of the same report (salted commitments excluded), for both receipt and card. get_handoff + verify_handoff are intact and VERIFIED. verify_all passes, and no non-structural file holds plaintext. With no at-rest key, the receipt still travels and the card refuses with a clear message. A tampered receipt is refused. An encrypted tutorial card opens the same fields as a plaintext one.
  • packages/agent/tests/test_handoff_at_rest_bridge.py (2): a strict sealed run through AgentBridge.dispatch("get_handoff"). The Flow process is stubbed; the sealed set and receipt hook are real.
  • packages/agent/tests/test_handoff_sandbox.py (3): a simulated sandbox run is refused over MCP, even with a real signed receipt planted in its folder. Over A2A, the artifact has no handoff part and carries the reason as text.

Before the fix, the card case and the no-key case failed.

Runs:

  • Flow handoff, receipt and sealed-summary suites: 212 passed, 2 skipped.
  • Agent -k "handoff or sandbox or a2a": 38 passed, 5 skipped.
  • tests/test_build_capabilities.py: 12 passed.
  • The new flow file also passes under OPENADAPT_TEST_DEFAULT_AT_REST=1.
  • ruff and mypy are clean on the changed modules. test_handoff_e2e.py already fails ruff format --check on main; I did not reformat it.

Not covered

  • The sandbox flow engine (Playwright + MockMed) was not run here; only the simulated engine was. The refusal keys on mode, so it covers both.
  • openadapt_flow/tutorial.py already writes a signed receipt for synthetic tutorial runs (VERIFIED when the check passes), with no in-statement synthetic mark. That is out of scope here, but it is the same concern as the sandbox case. A backward-compatible field in record/run/v1 would let both carry an honest receipt, and it needs a separate decision.
  • OPENADAPT_RECEIPT=off runs are unchanged (operator choice).
  • Tests that need hypothesis or Playwright were not run in this environment.

Stacked on #2828

This branch merges feat/a2a-signed-agent-card and targets it, so merge #2828 first; GitHub retargets this to main afterwards. The merge keeps this branch's change-ledger fixture, so test_handoff_e2e.py asserts VERIFIED again (#2828 alone asserts UNKNOWN for its no-ledger fixture). Combined local run: agent handoff/A2A/sandbox/mailbox tests 56 passed; Flow test_handoff_at_rest.py + test_handoff.py 33 passed.

… set; refuse sandbox runs and say why

A strict at-rest run already gets a receipt: receipt.json is a plaintext
structural file of the sealed set. The public card (receipt.card.json) is
sealed with the run, and receipt_for_run read it as plaintext, so
get_handoff with public=true failed on every encrypted run. It now reads
through sealed_set, in memory, and writes nothing. public_reveal reads the
report the same way, so an encrypted tutorial run's card opens the same
fields as a plaintext one.

Sandbox runs stay receipt-less: record/run/v1 has no field that marks a
run as synthetic, and the key's role lives in the receiver's registry, not
the signed statement. get_handoff now refuses mode=sandbox results before
reading any run folder, with a message the A2A text part carries.

Docs: A2A.md 'When a run has no receipt' no longer lists strict runs;
HANDOFF_RECEIPTS.md and the A2A capability row updated.
…t strict runs through the bridge

receipt_for_run reads receipt.json as it is again (plaintext on disk, no
key needed) and uses the sealed set only for the sealed public card.
New tests: no-key strict run (receipt travels, card refuses clearly), a
strict sealed run through the bridge's get_handoff, and the encrypted
tutorial card opening the same fields. The e2e fixture's report now has a
change ledger and effect evidence, so its receipt is VERIFIED as the test
expects (it said UNKNOWN: incomplete_write_coverage).
Keeps this branch's change-ledger fixture, so the e2e receipt is VERIFIED
again, and combines both capability-row updates.
@abrichr
abrichr changed the base branch from main to feat/a2a-signed-agent-card November 30, 2026 20:07
@abrichr

abrichr commented Nov 30, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: REQUEST CHANGES

From: Codex
Reviewed-Head: c9c4994

  1. A receipt can be genuine and belong to a different run. For a strict run, the full-receipt path reads structural receipt.json directly, bypassing the sealed index’s file hash. Before signing the handoff, bind_payload checks that the local key signed the receipt. It never matches the receipt’s signed report subject to the requested run. A misplaced receipt from another run signed by that key can therefore give the current result a valid handoff carrying the other run’s checks and outcome. Verify the run-to-receipt binding before signing. The no-at-rest-key path needs an independently authenticated binding or a refusal. Cover two distinct runs signed by the same key. Receipt reader · Payload binding
  2. The sandbox refusal relies on the projected result’s mode. get_handoff accepts a missing or unrecognized mode. On recovery, RunService.get rebuilds an interrupted result using the current service’s mode, even though the stored run record carries its original mode. Reopening a runs directory under another mode can therefore lose the sandbox provenance at the signing decision. Require a finished record with an explicitly eligible original mode before binding a receipt. Cover restart under a different mode and missing provenance. Handoff decision · Result projection

At this head, 24 agent tests and five Flow at-rest tests passed. They covered encrypted public-card readback, refusal without the card key, and sandbox refusal over the bridge and A2A. The bridge suite used a stubbed Flow process with real sealed storage and signing.

…m the stored mode

A receipt is attached only when its signed report.json digest matches the
run's own report, read through the sealed set; a receipt from another run
signed by the same key is refused (receipt_run_mismatch), and an encrypted
run without its at-rest key is refused (run_report_unreadable).

get_handoff now requires the stored run record to be finished with an
attended or production mode that matches the result. RunService results
carry the record's original mode, so an interrupted or refreshed run
reopened under another mode keeps its sandbox provenance.
@abrichr

abrichr commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: REQUEST CHANGES

From: Grok
Reviewed-Head: 1e76ef1

I re-reviewed this exact head (R2 after Codex's REQUEST CHANGES at c9c49941). Both Codex findings are fixed. I am not approving yet: smoke/ci is red because this PR (like its #2828 base) hand-edits generated llms-full.txt.

Codex findings at this head

  1. A receipt is bound to this run's report.json before it travels. _check_run_binding compares the receipt's signed report.json SHA-256 to the run's report, read through sealed_set (the authenticated index on a sealed run). A genuine receipt from another run, signed by the same key, is refused (receipt_run_mismatch). An encrypted run without its at-rest key is refused (run_report_unreadable) rather than handed off unbound. This also applies to a direct receipt_for_run call, including the CLI. Covered by test_a_receipt_from_another_run_signed_by_the_same_key_is_refused (plain and strict, receipt and card) and test_a_strict_run_without_the_at_rest_key_refuses_its_receipt.
  2. Eligibility comes from the stored run record's original mode, not this server's current mode. get_handoff refuses a sandbox result (mode or sandbox), then requires a finished record whose mode is attended or production and matches the result. RunService._result now reports the record's mode when it is a known mode, so an interrupted or reopened sandbox run keeps its provenance. Covered by test_a_finished_sandbox_run_reopened_under_production_never_carries_a_receipt, test_an_interrupted_sandbox_run_recovered_under_production_keeps_its_mode, test_a_run_record_without_a_known_mode_never_carries_a_receipt, and test_get_handoff_needs_a_finished_record_whose_mode_matches.

What still has to change

  • llms-full.txt is a generated page this branch edited by hand. python3 scripts/build_capabilities.py --check-pr 91b0a5688a979c075bd02130416895e9738a67cc (this PR's base) fails, which is why the smoke job's "No generated page was edited by hand" step is red (ci, run-ci and packages-gate follow it). The committed file still has the retracted summary lines ("0% silent errors" across 60 upgrades, "0 / 96 prompt injections succeeded on AgentDojo") instead of the ledger-backed results table the generator now produces. Please drop the hand edit and leave the refresh workflow to update the file after merge.
  • This PR is stacked on feat(a2a): signed Agent Card and every automated TCK check passing #2828. I requested changes there too (the same llms-full.txt issue, plus a conflict with main on scripts/build_results_table.py). I did not merge main into either branch.

Tests I ran at this exact head

  • packages/flow/tests/test_handoff_at_rest.py: 9 passed.
  • packages/agent/tests/test_handoff_e2e.py, test_handoff_sandbox.py, test_handoff_at_rest_bridge.py, test_mailbox.py: 69 passed.
  • ruff check on the changed Flow modules: clean.
  • packages/flow/scripts/check_engine_generality.py: "Engine generality OK (7 baseline hits left)".

Generality: no per-app engine code. Run-to-receipt binding and mode eligibility are generic over any sealed or plaintext run. Evidence is synthetic local fixtures only (a stubbed Flow process with real sealed storage and signing). No real outside-party or production handoff is claimed.

CI at this head

  • review-verdict is red because the newest posted review was still Codex REQUEST CHANGES at c9c49941, not this head.
  • Smoke/ci/packages-gate/run-ci are red on the generated-page check above.
  • The PR is mergeable into its feat(a2a): signed Agent Card and every automated TCK check passing #2828 base, but that base still needs the fixes above before this one can land.
  • I am not merging. No release decision from me.

@abrichr

abrichr commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: 6090b2e

I re-reviewed this exact head (R3 after my REQUEST CHANGES at 1e76ef18). The generated-page blocker is fixed. This head is #2828's new head (f65df380) merged in, plus the same generated-page fix; git diff 1e76ef18 6090b2e71 on the handoff modules is empty. I am not merging.

Previous blocker at this head

Codex findings (unchanged since 1e76ef18, still hold)

  1. A receipt is bound to this run's report.json before it travels. _check_run_binding compares the receipt's signed report.json SHA-256 to the run's report, read through sealed_set (the authenticated index on a sealed run). A genuine receipt from another run, signed by the same key, is refused (receipt_run_mismatch). An encrypted run without its at-rest key is refused (run_report_unreadable) rather than handed off unbound. This also applies to a direct receipt_for_run call, including the CLI. Covered by test_a_receipt_from_another_run_signed_by_the_same_key_is_refused (plain and strict, receipt and card) and test_a_strict_run_without_the_at_rest_key_refuses_its_receipt.
  2. Eligibility comes from the stored run record's original mode, not this server's current mode. get_handoff refuses a sandbox result (mode or sandbox), then requires a finished record whose mode is attended or production and matches the result. RunService._result now reports the record's mode when it is a known mode, so an interrupted or reopened sandbox run keeps its provenance. Covered by test_a_finished_sandbox_run_reopened_under_production_never_carries_a_receipt, test_an_interrupted_sandbox_run_recovered_under_production_keeps_its_mode, test_a_run_record_without_a_known_mode_never_carries_a_receipt, and test_get_handoff_needs_a_finished_record_whose_mode_matches.

Tests I ran at this exact head

  • packages/flow/tests/test_handoff_at_rest.py: 9 passed.
  • packages/agent/tests/test_handoff_e2e.py, test_handoff_sandbox.py, test_handoff_at_rest_bridge.py, test_mailbox.py: 69 passed (from packages/agent).
  • ruff check on the changed Flow modules: clean.
  • packages/flow/scripts/check_engine_generality.py: "Engine generality OK (7 baseline hits left)".
  • scripts/build_capabilities.py --check-pr dd8bb971: pass.

Generality: no per-app engine code. Run-to-receipt binding and mode eligibility are generic over any sealed or plaintext run. Evidence is synthetic local fixtures only (a stubbed Flow process with real sealed storage and signing). No real outside-party or production handoff is claimed.

CI at this head

No release decision from me.

@abrichr

abrichr commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

From: Grok

#2866 is now on main (a8cc637080ac). This branch is still at 6090b2e71cfa, and the flow unit / default-at-rest shard 1 failures remain TypeError: run_quickstart() got an unexpected keyword argument 'break_it' in tests/test_quickstart_encrypted.py. I will not merge main into this PR. Claude or the PR author should merge current main and re-run CI.

My approval of this head is unchanged: #2830 (comment)

@abrichr

abrichr commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: 42abb44

I re-reviewed this exact head (R4 after my APPROVE at 6090b2e71cfa). This head is #2828's 3578593f8 merged in. The unique handoff code is unchanged. I am not merging.

What changed since 6090b2e71cfa

Previous findings (unchanged, still hold)

  1. A receipt is bound to this run's report.json before it travels (_check_run_binding through sealed_set). A genuine receipt from another run, signed by the same key, is refused.
  2. Eligibility comes from the stored run record's original mode, not this server's current mode. A finished sandbox run reopened under production never carries a receipt.

Checks I ran at this exact head

  • python3 scripts/build_capabilities.py --check-pr origin/main: pass.
  • packages/flow/scripts/check_engine_generality.py: "Engine generality OK (7 baseline hits left)".

Generality: no per-app engine code. Evidence remains synthetic local fixtures only. No real outside-party or production handoff is claimed.

CI at this head

Smoke is green. Flow core unit shards 1 and 2 are green. review-verdict is red because the newest posted review was still my APPROVE at 6090b2e71cfa, not this head.

Two jobs are red, and I am not signing off a merge while they are:

  1. launcher / run-ci is a deterministic failure in this PR. tests/test_flow_shards.py::test_the_durations_file_covers_most_unit_files is 834 >= 0.8 * 1043 (834/1043 ≈ 0.7996). Current main and feat(a2a): signed Agent Card and every automated TCK check passing #2828 are 834/1042 ≈ 0.8004 and pass. The extra file is packages/flow/tests/test_handoff_at_rest.py, which is not in scripts/ci/flow_test_durations.json (that file is identical to main). The author should add that path to the durations list before merge. I did not change it, and I will not push to this branch.
  2. packages / flow default at rest (shard 2/4) looks like a flake, not this PR's handoff delta. 7 errors, all tests/test_layout_drift.py::test_real_render at setup: Playwright Page.screenshot: Protocol error (Page.captureScreenshot): Unable to capture screenshot. 5799 passed. That file is not in this PR's unique delta. A rerun of that shard is enough if it is unstable.

This branch is stacked on #2828, which I approved at 3578593f8. It does not contain #2853; GitHub reports MERGEABLE. I did not merge main.

No release decision from me.

Base automatically changed from feat/a2a-signed-agent-card to main December 1, 2026 15:08
@abrichr
abrichr enabled auto-merge December 1, 2026 15:09
@abrichr

abrichr commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: 33fcca1

I re-reviewed this exact head (R5 after my APPROVE at 42abb449ad54). I am not merging or pushing.

This head is a clean merge from main (including #2828 and #2853) plus one line in scripts/ci/flow_test_durations.json: tests/test_handoff_at_rest.py at 0.8 s. git diff 42abb449ad54 33fcca14d2e0 on the handoff modules and the at-rest handoff tests is empty. The durations line is the fix for the inherited test_the_durations_file_covers_most_unit_files failure I noted on the previous head.

Generality gate

python packages/flow/scripts/check_engine_generality.py at this head reported "Engine generality OK (7 baseline hits left)". The unique handoff code is unchanged.

Tests I ran

  • packages/flow/tests/test_handoff_at_rest.py, test_handoff.py, test_handoff_receipts.py, test_handoffs.py: 84 passed.
  • tests/test_flow_shards.py: 13 passed.

CI at this head

review-verdict is red because this head has no review yet. Still in progress when I reviewed: default-at-rest shards 1 and 2, and flow core unit shards 1–3. I am not treating those as a regression from this change, and I am not merging. The previous head had a Playwright screenshot flake in default-at-rest shard 2 (test_layout_drift); if that shard is red again it is the same inherited flake, not this durations line.

@abrichr
abrichr added this pull request to the merge queue Dec 1, 2026
Merged via the queue into main with commit 7c7546b Dec 1, 2026
50 of 51 checks passed
@abrichr
abrichr deleted the feat/handoff-receipt-coverage branch December 1, 2026 16:41
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