Skip to content

fix(kernel): autopilot requires a signed VERIFIED_AUTOPILOT pack - #2779

Merged
abrichr merged 3 commits into
mainfrom
fix/kernel-autopilot-stage-gate
Nov 30, 2026
Merged

abrichr merged 3 commits into
mainfrom
fix/kernel-autopilot-stage-gate

Conversation

@abrichr

@abrichr abrichr commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Problem

In openadapt-kernel, autopilot did not check the pack's autonomy stage.
_load_portal_pack read stage from unsigned JSON, and run_portal_pack
went on to click the commit control without looking at it. So
openadapt-kernel run --mode autopilot --commit-selector ... submitted a
SHADOW pack. The registry's Ed25519 verifier (pull_verified_pack) existed,
but the runner never called it. Three more paths let a caller or a model widen
autonomy:

  • NonDomKernelExecutor.execute(autonomy_stage=...) replaced the pack's stage
    instead of capping it.
  • A self-healed run qualified its own pack against the values it had just
    written (observed_readback=expected_vars), promoted it to
    VERIFIED_AUTOPILOT and published it.
  • The MCP tool kernel_run_pack let the model choose mode: autopilot, the
    signing key path and any output path.
  • Even with the gate, a signed pack did not bind the run to its
    portal_origin. A pack signed for one origin could submit a form on another
    through url or cdp_url, including over MCP with --allow-autopilot.
  • Only one escalation after the commit click (readback_refuted_after_commit)
    recorded the possibly-landed write. commit_not_observed and the others did
    not.

Fix (fail closed)

  • Autopilot gate. run_portal_pack(mode="autopilot") loads the pack only
    through pull_verified_pack against a pinned key: trusted_pack_public_key,
    or --trusted-pack-pub on the CLI. The signed stage must be
    VERIFIED_AUTOPILOT. A missing key, an unsigned file, a foreign key, an
    envelope edited after signing, or any other stage returns ESCALATED with a
    reason, before the browser opens. Nothing is submitted. Draft mode still
    accepts flat or signed packs.

  • Origin binding. The signed portal_origin is the only origin autopilot
    may act on. Unless the page has exactly that scheme, host and port (default
    ports 80 and 443), the run returns ESCALATED with
    autopilot_origin_not_signed. The check runs at four points:

    • before the browser opens, when url is elsewhere;
    • after navigation, redirects or CDP attach, before anything is staged;
    • on any popup, before it is staged;
    • right before the commit click.

    A signed pack without a valid http(s) origin cannot run autopilot. MCP tool
    calls still choose url and commit_selector, but they cannot move the
    click off the signed origin.

  • Every post-click escalation records an uncertain write. Once the commit
    control is clicked, any exit that does not end VERIFIED adds a pending
    uncertain_write_reconcile obligation to the ledger and the signed receipt
    and sets RunResult.uncertain_write. That covers:

    • commit_not_observed;
    • readback_refuted_after_commit;
    • a completion-closure failure (recorded too, because the run did not
      finish, even though the write was read back);
    • an exception after the click, which now returns ESCALATED with
      post_commit_error:<type> instead of raising without a receipt.

    One place in _finish does this, so a new post-click exit cannot skip it.

  • Nondom override. autonomy_stage can only lower the stage. An unknown
    stage string resolves to SHADOW.

  • Self-heal. The heal path no longer promotes or publishes the pack.
    auto_publish_on_heal and registry_dir are still accepted but publish
    nothing. Promotion now happens only by signing outside the run.

  • Heal never repairs and commits in one run. The signed stage covers the
    pack as signed, not an in-memory repair from coding_agent_fn.

    • When staging fails or the commit control is blocked, the agent may propose
      a repair inside the zero-PHI twin. The run returns ESCALATED with
      heal_proposed=True (reason suffix self_heal_proposed_not_applied) and
      clicks nothing on the live page. Before, the repair was replayed live; a
      Save button behind an overlay was clicked and the run ended VERIFIED.
    • When read-back refutes persistence after the commit click, the run returns
      ESCALATED with readback_refuted_after_commit and records an uncertain
      write (see above). It never heals, re-clicks or re-submits. Before, the heal re-submitted: a dropped
      write was posted twice and reported VERIFIED. This reason replaces
      readback_refuted_persistence.
    • Draft mode had no heal path and is unchanged.
  • MCP. The mode ceiling, trusted pack key, receipt key and output root are
    server configuration: McpServerConfig, set by
    serve-mcp --allow-autopilot --trusted-pack-pub --priv --output-root. Tool
    calls work as follows:

    • They default to draft.
    • They get an error if they ask for autopilot on a server that doesn't
      allow it.
    • They get an error for private_key_path or any other undeclared argument.
    • They can write out_path and receipt_out only under the output root
      (default ~/.openadapt/kernel/mcp).
  • pull_verified_pack now raises ValueError for non-envelope files,
    non-Ed25519 keys and bad signatures. It used to rely on assert, which
    python -O strips.

  • New openadapt-kernel publish-pack. This wasn't in the original scope,
    but without it there was no CLI way to produce a signed pack for autopilot.
    Whoever holds the pack key approves autopilot.

  • README. Dropped the "< 3,200 LOC" claim (src is about 16,500 lines) and
    documented the signing requirement and the MCP limits.

  • Version. Kernel goes to 0.2.0 (breaking: autopilot needs a signed pack)
    and the CHANGELOG has an entry. The root uv.lock pins the PyPI 0.1.0 and is
    unchanged.

Tests

New file tests/test_autopilot_stage_gate.py, plus two tests in
test_receipt_bridge_and_nondom.py. I ran each bypass test against the
unmodified src first, and each one failed there. The two heal tests were
run against this PR's first commit, 7e87bad60, and the origin and
uncertain-write tests against 0b879c8cb:

Test On main With this fix
Unsigned VERIFIED_AUTOPILOT JSON in autopilot VERIFIED, form submitted ESCALATED, nothing saved
CLI run --mode autopilot on a SHADOW JSON pack exit 0, form submitted exit 1, nothing saved
Signed SHADOW pack, trusted key (could not load an envelope) autopilot_pack_stage_not_verified:SHADOW
Foreign key; stage edited after signing (could not load an envelope) autopilot_pack_signature_invalid
Nondom: DRAFT_TO_COMMIT/SHADOW/unknown pack with autonomy_stage="VERIFIED_AUTOPILOT" commit clicked READY_FOR_HUMAN_COMMIT, zero submit clicks
MCP asks for mode: autopilot / passes private_key_path ran isError, nothing submitted, no receipt written
MCP out_path outside the output root written isError, not written
Autopilot, Save behind an overlay, coding_agent_fn set repair clicked Save, VERIFIED ESCALATED, zero POSTs
Autopilot, write dropped after commit, coding_agent_fn set second POST, VERIFIED readback_refuted_after_commit, one POST, agent not called, uncertain write in receipt
Pack signed for origin A, run on B (direct url, same host on the default port, a redirect to B, MCP with allow_autopilot=True) B received the POST autopilot_origin_not_signed, zero POSTs to A or B; the same pack still submits on A
POST answered 500, nothing stored commit_not_observed, no obligation in receipt commit_not_observed, one POST, uncertain_write_reconcile in the signed receipt
Read-back raises after the click exception, no receipt post_commit_error:RuntimeError, uncertain write in the signed receipt

Positive controls: a trusted signed VERIFIED_AUTOPILOT pack still submits
through the API, CLI and MCP (with the server configured for it), and a
signed pack runs in draft mode.

Existing tests changed. About 15 autopilot calls in 6 files ran on unsigned
packs, most with no stage or with SHADOW. In other words, they relied on the
bypass. They now sign a VERIFIED_AUTOPILOT pack through a small helper
(tests/_signed_packs.py). The self-heal test's publish assertions are
inverted: it now asserts that nothing is published. That test and the
bijective-symbols heal test used to assert a live heal ending VERIFIED. They
now assert ESCALATED with heal_proposed=True and no commit. Two tests
signed a pack for http://127.0.0.1 with no port and ran it on a server with a
random port; they now sign a pack for each server's own origin.

All evidence here comes from synthetic local fixtures: local HTTP servers and
headless Chromium on this machine. Nothing ran against a real portal, and there
is no held-out evidence.

Kernel results: pytest tests gives 149 passed; ruff check . and
mypy --ignore-missing-imports src are clean.

Not covered here

  • NonDomKernelExecutor.execute still trusts the stage on the in-memory
    PortalPack it is handed. It can no longer be raised, but the caller's loader
    decides whether that stage was signed.
  • In draft mode the runner still clicks popup_submit_selector in a child
    popup before returning.
  • A twin-verified repair proposal is recorded only as heal_proposed and the
    egress ledger hash. There is no step for a person to review and apply it
    yet.
  • MCP read paths (pack_path, evidence_path) are not sandboxed.
  • The popup trigger click on the main page relies on the post-navigation
    origin check; there is no separate check right before that click.
  • copilot.py receipt signing and verification still use assert isinstance.

Autopilot read the pack's stage from unsigned JSON and never checked it,
so a SHADOW pack could submit through run_portal_pack, the CLI and MCP.

- run_portal_pack(mode="autopilot") now loads the pack through the
  registry's signature, digest and zero-PII check against a pinned key
  (trusted_pack_public_key / --trusted-pack-pub) and requires the signed
  stage to be VERIFIED_AUTOPILOT. Anything else returns ESCALATED before
  the browser opens. Draft mode still accepts flat or signed packs.
- NonDomKernelExecutor: autonomy_stage can only lower the pack's stage.
- Self-heal no longer promotes or publishes its own pack; it qualified
  the pack against the values it had just written.
- MCP server: mode ceiling, keys and output root are server config
  (serve-mcp flags). Tool calls default to draft, cannot pass key paths
  or other undeclared arguments, and write only under the output root.
- pull_verified_pack raises ValueError instead of relying on assert.
- New publish-pack command; README drops the stale line-count claim.
- Version 0.2.0.
A pack's signed VERIFIED_AUTOPILOT stage covers the pack as signed, not an
in-memory repair from coding_agent_fn. Before this change:

- When staging failed or the commit control was blocked, the model's
  unqualified repair was replayed on the live page and could click Save.
  A blocked Save behind an overlay ended VERIFIED.
- When read-back refuted persistence after the commit click, the heal
  re-submitted. A dropped write was posted twice and reported VERIFIED.

Now, in autopilot, the heal runs in the zero-PHI twin only and the run
returns ESCALATED with heal_proposed=True and nothing clicked. A refuted
read-back after the click escalates with readback_refuted_after_commit and
adds a pending uncertain_write_reconcile obligation; it never heals,
re-clicks or re-submits. Draft mode had no heal path and is unchanged.
@abrichr

abrichr commented Nov 30, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: REQUEST CHANGES

From: Grok
Reviewed-Head: 0b879c8

The stage gate works as described. Each bypass in the PR body is closed at this head, and I found no exception path that falls through to a commit. Two gaps remain on the autopilot commit path. I reproduced both at this head, and both are small fixes.

Blocking

1. A signed pack does not bind the run to the pack's portal_origin, so the signature works as a bearer token for autopilot on any site.

  • runner.py:264-268 checks the signature and stage. After that, the run navigates to the caller's url (runner.py:430-431), or attaches to whatever page cdp_url holds (runner.py:418-422), and clicks the caller's commit_selector (runner.py:586). Nothing compares the page origin with the signed pack.portal_origin.
  • That origin is used only for 2xx and read-back attribution (runner.py:472, :588-600), and both of those run after the click.
  • Over MCP, a server started with --allow-autopilot lets the model pick pack_path, url, cdp_url and commit_selector (mcp_server.py:213-230). That undercuts the module docstring's claim that the model doesn't choose how far a run may go.
  • Repro: I signed a VERIFIED_AUTOPILOT pack for local origin A and ran autopilot with url set to local origin B. I did this through run_portal_pack and through dispatch_tool_call with McpServerConfig(allow_autopilot=True, ...). Result: ESCALATED commit_not_observed, origin A received 0 POSTs, and origin B received the form POST ({'member_id': 'MEM-1'}).
  • This isn't a regression, since main had no gate at all. But once the signed stage is the approval for autopilot, the approval has to cover where the click happens.
  • Suggested fix: after navigation or CDP attach, and again right before the commit click, refuse with ESCALATED (for example autopilot_origin_not_signed) unless the page origin equals pack.portal_origin. Add a test where a pack signed for A is run against B and B gets zero POSTs.

2. commit_not_observed after the click records no uncertain write.

  • runner.py:612-622 escalates after page.locator(commit_selector).first.click() with no uncertain_write_reconcile obligation. The new readback_refuted_after_commit branch (runner.py:624-648) does record one.
  • Both are the same defect class: an escalation after the commit click, where the write may have landed.
  • In the repro above, the write did land on B. The signed receipt (receipt_out plus a signing key) does not contain uncertain_write_reconcile.
  • Per the Generality gate's "fix the class" line, every post-click escalation should add the obligation, not only the read-back one. The repro covers both fixes: once the origin check is in, the click doesn't happen there. A non-2xx or out-of-origin write on the signed origin still reaches this branch.

Checked and OK

  • _load_autopilot_pack (runner.py:111-127) refuses each of these with ESCALATED before Playwright starts: a missing key, a non-envelope file, a digest mismatch, a non-Ed25519 key, a bad or non-base64 signature (validate=True), a PII hit, or any stage other than VERIFIED_AUTOPILOT. It catches every Exception, so nothing in it fails open.
  • The digest and signature are computed over the re-serialized parsed dict, and the pack is built from that same dict. A duplicate JSON key therefore can't make the verified bytes and the loaded pack differ.
  • Mode spoofing: unknown mode values return unknown_run_mode from the API and are refused at MCP (_resolve_run_mode). MCP defaults to draft and rejects undeclared arguments such as private_key_path (_check_arguments, mcp_server.py:152).
  • _sandboxed_output_path (mcp_server.py:52) checks the path after resolve(), so .. and symlinks are handled.
  • _lower_stage (nondom_adapter.py:352, used at :471) can only lower the stage, and unknown stages resolve to SHADOW.
  • Repair and commit in the same run: both heal call sites (runner.py:497-516 and :561-583) only propose inside the twin and return ESCALATED. The heal path has no live click left, and the read-back refutation path neither heals nor re-clicks.
  • Publishing: auto_publish_on_heal and registry_dir publish nothing.

Generality gate

  1. No per-app code: pass. The kernel diff names no app or vendor. The origin and stage checks are generic over packs.
  2. Fix the class: partial. See finding 2. The post-click "uncertain write" class has two sites, and only one is fixed.
  3. Held-out evidence: the PR shows the fixes on synthetic local fixtures only and doesn't claim held-out evidence. That's acceptable for a safety gate, but the PR should say so.

Tests at this head (box, Linux, py3.12, Playwright Chromium)

  • uv sync --all-extras --frozen, then pytest tests in packages/kernel: 145 passed, 1 skipped. The skip is test_nav_and_desktop_live.py:156, a macOS-only AX test.
  • ruff check .: clean. mypy --ignore-missing-imports src: clean.
  • Ad-hoc probes I wrote (not in the PR): a signed pack for A run against B via the API and via MCP with allow_autopilot=True. Result is above: B got the POST, and there was no uncertain-write obligation in the receipt.

Not run

  • The macOS live AX test (Linux box).
  • No real portal: everything ran against local fixtures.

CI at this head

Every job that ran passed, including openadapt-kernel (uv py3.12 + ruff + mypy + pytest), openadapt-kernel (py3.12 + Chromium), smoke, install weight and packages-gate. The other package jobs were skipped by the path filter.

To the PR author: please add the origin binding and the post-click obligation, with tests, and request a re-review on the new head. I didn't push to this branch.

…lick uncertain write

A signed VERIFIED_AUTOPILOT pack now approves its own portal_origin only.
Autopilot returns ESCALATED (autopilot_origin_not_signed) unless the page has
exactly that scheme, host and port: before the browser opens when url is
elsewhere, after navigation, redirects or CDP attach, on any popup before it
is staged, and right before the commit click. A pack without a valid http(s)
origin cannot run autopilot.

Every exit after the commit click that does not end VERIFIED now adds an
uncertain_write_reconcile obligation to the ledger and signed receipt and
sets RunResult.uncertain_write: commit_not_observed, read-back refutation,
completion-closure failure, and an exception after the click (now
ESCALATED post_commit_error:<type> instead of raising).
@abrichr

abrichr commented Nov 30, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: 0e99cfc

Both blockers from my review of 0b879c8cb are fixed at this head. I reproduced each fix and tried new bypasses against the delta. None of them gets a click onto a page the pack is not signed for, and none of them leaves a post-click exit without the uncertain-write obligation.

Prior blockers

1. Origin binding: fixed.

  • _origin_tuple / _origin_refusal (runner.py:79-106) compare scheme, host and port, with the default ports filled in. _load_autopilot_pack refuses a signed pack that has no valid http(s) origin (runner.py:162-163).
  • The page is checked:
    • before the browser opens (:317-321);
    • after goto, redirect or CDP attach (:509-511);
    • on a popup before staging (:604-606);
    • right before the commit click (:668-670).
  • I re-ran my earlier repro, a pack signed for local origin A run with url on origin B:
    • Direct run_portal_pack: ESCALATED autopilot_origin_not_signed:page_origin_mismatch. A got 0 POSTs and B got 0 POSTs.
    • MCP dispatch_tool_call with McpServerConfig(allow_autopilot=True): same result, 0 POSTs on either origin.
  • Origin parsing table:
    • Same origin: case differences, explicit default ports (:80, :443), a path in the signed origin, userinfo (user@host), and IPv6 literals.
    • Refused:
      • host@evil.test
      • an http/https downgrade
      • a trailing-dot host
      • an out-of-range port
      • blob: URLs
      • a punycode vs Unicode host (fails closed)
      • about:blank or an empty signed origin
  • A commit selector that tries to cross into an embedded cross-origin iframe (iframe >> internal:control=enter-frame >> …) never reaches the click. probe_before_click uses querySelector, which raises on it, so there are 0 POSTs on either origin.

2. Uncertain write after the click: fixed, as a class.

  • commit_clicked is set immediately before the click (runner.py:673).
  • _finish adds uncertain_write_reconcile and sets RunResult.uncertain_write on every non-VERIFIED exit after that point (:445-452). That covers commit_not_observed (:700), readback_refuted_after_commit (:712), the completion-closure exit (:759), and the new post_commit_error:<type> handler (:781-787).
  • Exceptions before the click still raise, as before. verify_completion_closure never returns VERIFIED as its decision, so that exit can't skip the obligation.
  • New probe: a page on the signed origin whose form posts to another origin gives ESCALATED commit_not_observed, uncertain_write=True, and the obligation is in the signed receipt.
  • New probe: a page on the signed origin that navigates elsewhere 1.5 s after load. The click landed on the signed origin, and read-back on the new page refuted it: readback_refuted_after_commit, uncertain_write=True.

3. Evidence statement: the PR body now says the evidence is synthetic local fixtures only and that there is no held-out evidence.

Non-blocking notes

  • popup_submit_selector (runner.py:622-630) clicks a control in the popup before commit_clicked is set, and the popup's origin is not re-checked between staging and that click. If a popup's submit control writes on its own, a later escalation (blocked commit control, origin refusal) records no uncertain write. This is Python-API only (not exposed through the CLI or MCP) and predates this PR. Consider setting the flag before the popup submit click.
  • The last origin check and the click are separate calls, so a page that navigates in that gap is not re-checked. My 1.5 s timer probe could not hit the gap.
  • An origin refusal before the browser opens returns without a receipt, the same as the other pre-browser refusals (signature, stage).

Generality gate

  1. No per-app code: pass. The origin and uncertain-write logic is generic over packs and names no app or vendor.
  2. Fix the class: pass. The origin check covers every point where a page enters the run (initial URL, navigation or attach, popup, pre-click). The uncertain-write record now comes from one flag in _finish instead of being added at each exit.
  3. Held-out evidence: synthetic local fixtures only, and the PR says so. That is acceptable for a safety gate.

Tests at this head (box, Linux, py3.12, uv sync --all-extras --frozen, Playwright Chromium)

  • pytest tests in packages/kernel: 148 passed, 1 skipped. The skip is the macOS-only AX test.
  • ruff check .: clean. mypy --ignore-missing-imports src: clean.
  • I put packages/kernel/src back to 0b879c8cb and kept this head's tests. All three new tests then fail:
    • test_autopilot_pack_signed_for_one_origin_never_submits_on_another
    • test_commit_not_observed_after_click_records_uncertain_write
    • test_error_after_commit_click_escalates_with_uncertain_write
  • My own probes (not in the PR): the earlier API and MCP origin repros, the iframe selector, a cross-origin POST from the signed page, a delayed redirect, and the origin parsing table. Every result is quoted above.

Not run

  • The macOS live AX test (Linux box).
  • No real portal: everything ran against local fixtures.

CI at this head

12 passed, 33 skipped by the path filter, none failing or pending. The passing jobs include openadapt-kernel (uv py3.12 + ruff + mypy + pytest), openadapt-kernel (py3.12 + Chromium), smoke, install weight and packages-gate.

I didn't push to this branch.

@abrichr
abrichr added this pull request to the merge queue Nov 30, 2026
Merged via the queue into main with commit 955b051 Nov 30, 2026
45 checks passed
@abrichr
abrichr deleted the fix/kernel-autopilot-stage-gate branch November 30, 2026 05:19
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