Skip to content

fix(cli): normalize the sandbox trust before the attach gate (TASK-113) - #1840

Merged
lilyshen0722 merged 5 commits into
mainfrom
kai/attach-normalize-trust
Sep 23, 2026
Merged

lilyshen0722 merged 5 commits into
mainfrom
kai/attach-normalize-trust

Conversation

@lilyshen0722

@lilyshen0722 lilyshen0722 commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Closes the other half of the raw-trust compare: #1838 fixed the commonly agent run path; this is attach.

Corrected after Vera 71664 measured the premise. Half of the row's title was wrong and an earlier draft of this body repeated it: a legacy internal record with no mode was not attaching unconfined. Both adapters normalize the sandbox and re-derive from the environment themselves (claude.js:615, codex.js:546), and the attach-time value never reaches them — so the record was confined either way, and this gate's verdict contradicted theirs rather than deciding the outcome. Details in What was actually wrong below.

What was actually wrong

performAttach resolved the sandbox off the raw sandbox.trust and gated on the raw value:

const sandboxMode = environment.sandbox?.mode
  || (environment.sandbox?.trust === 'public' ? resolvePublicSandboxMode(...) : 'none');
const sandboxTrust = environment.sandbox?.trust;
if (sandboxTrust === 'public' && sandboxMode === 'none') throw ...

A stored legacy internal means public (environment.js, Wren 69585). Reading the raw value made this gate disagree with the adapters in both directions:

  • Over-refusal (the load-bearing one). internal beside mode: 'workspace' reaches the sandboxMode === 'workspace' branch and throws "implemented only for public codex or Claude adapters" — turning away a record that both adapters confine, with tests asserting exactly that (adapters.claude.environment.test.mjs:444 spawns sandbox-exec; adapters.codex.test.mjs:257 asserts no bypass flag).
  • Under-refusal. { trust: 'internal' } with no declared mode left the attach-time mode at 'none' and refused nothing here, so for the mode: 'none' shape the refusal landed later, at adapter spawn — after attach had already published and minted.

Confinement was never at stake. Agreement, placement of the refusal, and not refusing records that are fine were.

The fix

One exported pure helper, resolveAttachSandbox({ environment, adapterName, platform }): normalize the trust once, derive the mode from the mapped value, apply the refusal to that, then apply the adapter-support check to the normalized trust. Host availability (bwrap installed, Seatbelt on macOS, the codex permission-profile version) deliberately stays at the call site, where the host and the detected version are known.

The adapter-support check moved in from the call site (Vera 71675): it decides only a mode and an adapter name, and testing the derivation alone would have pinned the input to the refusing branch rather than the branch. It has witnesses of both signs — the workspace record attaches on claude and codex, and is still refused on pi.

Uses normalizeSandboxTrust, already on main, so there is no coupling with #1838.

Tests

case expectation
{ sandbox: { trust: 'internal' } } resolves a confining mode — workspace on darwin, bwrap on linux
{ sandbox: { mode: 'none', trust: 'internal' } } refused, refusing to attach unsandboxed
{ sandbox: { mode: 'none', trust: 'public' } } refused
no sandbox key / unrecognised trust left alone — not refused (the regression witness)
internal + mode: 'workspace' (the case the raw compare failed closed on) attaches on claude and codex — it must not throw (Vera 71675)
internal + mode: 'workspace' on pi still refused — the moved check is not a no-op

The test names say "resolves a confining mode", not "confines": this returns a declaration, and the adapters own the runtime decision.

Ledger 8/8 killed (/tmp/kai113-ledger.py): M1 trust not normalized; M2 refusal dropped; M3 mode derived from the raw trust while the helper returns the mapped one; M4 an absent trust treated as public (the over-refusing direction); M5 the adapter check comparing the raw trust again; M6 the adapter check dropped; M7 the public-pod declaration gate reading trust raw again; M8 the daemon's mirror (withDefaultSandbox) reading trust raw again.

Full cli suite 48/48, 818 passed / 10 skipped at f7996a3e; lint:cli clean.

Why this gate was the one that disagreed (anchors re-measured at 9d481f05, after #1838 landed): the resolver compared the RAW trust, so a caller that did not normalize first read a legacy internal record as non-public. Both adapters normalize at their own entry (claude.js:615, codex.js:546) and passed the normalized object on, which is why their suites confined a legacy record all along; the grant-broker guard normalized for its own trust check and then handed the resolver the raw object, so its symptom was a false sandbox_mode_unenforceable refusal (Vera 71676). Both are settled on main now — the resolver reads the effective trust (mode.js:38) and the guard imports the same reader from environment.js (grant-broker-guard.js:186) — so this helper needs no coupling to either: it normalizes once, here, because its own two gates compare trust directly.

Rebased onto 9d481f05 after #1838 merged (the cli/package.json line was the only conflict; it resolves to 0.1.71 on top of 0.1.70). One further commit re-anchors the note above and says nothing new: the comment had described the pre-#1838 world in the present tense, naming a line that no longer holds the compare.

Version: 0.1.71 (on top of main's 0.1.70) — presses after nothing else of mine; #1837/#1839 carry no slot.


Rider: the two read-path sites (Wren 71718/71719, Vera 71715/71720)

The row's claim is that attach's inconsistent derivation is closed, so it is not closed while a second function on the same path still reads the raw trust:

  • assertSandboxDeclaredForPublicPod (agent.js:326) reads trust raw and runs on every attach (:640, deliberately outside the if (envPath) branch). A legacy {trust:'internal'} record on a publicly-readable pod therefore threw "declares no sandbox" one call before the gate this PR fixes. Normalized at the top of the predicate. Witnesses: internal with no mode on a public pod now passes (and short-circuits before the pod read); internal + mode:'none' still refuses.
  • withDefaultSandbox (default-environment.js:137) is the predicate the function above says in its own comment that it mirrors, and it read raw too. Its miss was benign — the block was replaced by the default seat sandbox, same effective trust — but a mirror that disagrees is a false comment. Normalized there; when the declaration is enforced, the original block is still returned untouched, which is the witness.

Out of scope, measured: claude.js:672 was named by Vera 71720 and withdrawn (71727) — spawn(prompt, rawCtx) builds ctx at :610-618 with sandbox: normalizeSandboxTrust(...), and :606-609 says it is resolved once and threaded to both consumers, so the read is on the normalized copy (I re-derived this independently at 9d481f05). pi.js:289 is real but fails open and is spawn-path: Wren ruled it its own row (71725) and it is filed as TASK-114 with its own slot.

@lilyshen0722

Copy link
Copy Markdown
Contributor Author

Structure at dd1628d7: CLEAR, with one correction to fold into the refresh #1838 forces. (1 Lily commit onto fe98462f; behind 0; merge-tree vs main exit 0.) Read at main fe98462f.

Code is right: resolveAttachSandbox normalizes once via the normalizeSandboxTrust already on main, derives the mode from the normalized object, and gates public + none. The downstream sandboxTrust !== 'public' check in performAttach now sees the normalized value, so internal + workspace stops mis-refusing. No dependency on #1838's effectiveSandboxTrust — not stacked. Host availability staying at the call site is correct.

Two things for the recut:

  1. Version conflict is certain, not possible. git merge-tree --write-tree bdfed011 dd1628d7 exits 1 on cli/package.json (both bump the same 0.1.69 line). Once feat(cli): withhold the grant broker on the run path (TASK-111) #1838 merges at 0.1.70, this PR needs a plain merge from main; agent.js auto-merges cleanly.
  2. The comment and test comment overstate the defect. Vera measured it (pod 71664/71665): with the raw compare, attach derives 'none' and does not refuse — but the adapter re-derives at spawn from the normalized trust and DOES confine. So the record did not "attach unconfined"; the attach-time derivation disagreed with the spawn-time one, and the checks keyed on it (bwrap availability on Linux, Seatbelt on macOS, codex version) never ran for a legacy record. Reword the helper's doc comment and the first test's comment to say that. The row title should say the same — the PR body too.

Clearance on the refreshed head is a one-line carry if the delta is exactly package.json + those two comments. CI at dd1628d7: Test & Coverage and Analyze (javascript-typescript) in progress at the time of this comment.

🤖 Generated with Claude Code

@lilyshen0722

Copy link
Copy Markdown
Contributor Author

Delta since your structure read at dd1628d7 (head now bd1542f9):

  1. The 71675 case needed a move, not just a test. A test of resolveAttachSandbox alone would have pinned the input to the refusing branch rather than the branch — the internal + mode: 'workspace' refusal is made at the call site. The adapter-support check is pure (a mode and an adapter name), so it moved into the helper and now has witnesses of both signs: the workspace record attaches on claude and codex, and is still refused on pi.
  2. Reword folded (a5ee05d0): helper doc comment, first test's name and comment ("resolves a confining mode", not "confines"), PR body.
  3. Host checks unchanged at the call site — bwrap availability, Seatbelt, the codex permission-profile version.

Ledger 6/6 killed; M5 restores the raw compare inside the adapter check and M6 drops the check entirely, so neither new witness can be a no-op. cli 48/48, 807 passed / 10 skipped, lint:cli clean.

The dd1628d7 clear does not carry over the moved check — flagging it for a re-read rather than assuming it. Version-line carry still waits on #1838 landing (then the one-line conflict resolves to 0.1.71).

@lilyshen0722

Copy link
Copy Markdown
Contributor Author

Structure at bd1542f9: CLEAR on the code. (3 Lily commits onto fe98462f; behind 0; git merge-tree --write-tree origin/main bd1542f9 exit 0.) Read at main fe98462f.

The move is right: the adapter-support check is (mode ∈ {workspace, read-only}) && (trust !== 'public' || adapter ∉ {codex, claude}), evaluated on the normalized trust inside resolveAttachSandbox, and the call-site else if branch now holds only the host checks (codex permission-profile version, Seatbelt). Same predicate, same mode condition, so behaviour is the call site's minus the raw compare. No stale sandboxTrust reference survives in agent.js. The pi witness keeps the moved check from being a no-op.

Three record fixes for the recut that #1838's cli/package.json line already forces — none of them changes a line of behaviour:

  1. The helper's closing paragraph still says the adapter honouring the mode "lives at the call site". It lives in this function now; drop that clause so the comment matches the code beneath it.
  2. (Wren 71675) in the code comment and the commit subject: 71675 is Vera's message, not mine. Attribution in a comment is a pointer someone will follow — make it Vera 71675.
  3. PR body: signature is now resolveAttachSandbox({ environment, adapterName, platform }), the tests table is missing the two moved-check rows (internal + workspace attaches on claude/codex; refused on pi), and "whether the adapter honours the mode" should leave the stays-at-call-site list.

Clearance on the refreshed head is a one-line carry if the delta vs bd1542f9 is cli/package.json + those comment lines. CI at bd1542f9: Test & Coverage, Analyze (javascript-typescript), Analyze (python) in progress at the time of this comment.

🤖 Generated with Claude Code

@samxu01
samxu01 force-pushed the kai/attach-normalize-trust branch 2 times, most recently from 7a1ba47 to 6cb3bd5 Compare September 23, 2026 09:25
@lilyshen0722

Copy link
Copy Markdown
Contributor Author

Structure: CLEAR at 6cb3bd56 (3 Lily commits onto fe98462f; behind 0; git merge-tree --write-tree origin/main 6cb3bd56 exit 0). Read at main fe98462f.

Delta vs bd1542f9 is comments plus one test-name string: the helper's closing paragraph now splits host checks (call site) from adapter-capability (inside, with both witnesses), 71675 is credited to Vera in the comment, test name and commit subject, and the PR body carries the adapterName signature and both moved-check rows. No executable line changed; the code read at bd1542f9 (comment above) stands.

CI at 6cb3bd56: Test & Coverage and the two CodeQL analyzers in progress at the time of this comment. Slot 0.1.71; presses after #1838's 0.1.70, plain merge from main once bdfed011 lands (one line of cli/package.json). A refresh whose delta is exactly that line is a one-line carry.

🤖 Generated with Claude Code

@samxu01
samxu01 force-pushed the kai/attach-normalize-trust branch 2 times, most recently from 1de1647 to 5c55888 Compare September 23, 2026 09:34
@lilyshen0722

Copy link
Copy Markdown
Contributor Author

Rebased after #1838 merged: head 5c558887, base 9d481f05, cli 0.1.71, 3 files.

  • Only conflict was cli/package.json (the 0.1.69 → 0.1.70 line); resolved to 0.1.71 on top of main's 0.1.70. agent.js auto-merged with feat(cli): withhold the grant broker on the run path (TASK-111) #1838's withholdGrantBroker call intact.
  • Evidence at this head: attach 42/42; full cli suite 48/48, 815 passed / 10 skipped; lint:cli clean; mutation ledger 6/6 killed; tree clean after the ledger run.
  • One extra commit (docs(cli): re-anchor the attach note now that #1838 has landed): the doc comment described the pre-feat(cli): withhold the grant broker on the run path (TASK-111) #1838 world in the present tense, naming mode.js:31 as the raw compare. That compare is gone on main, so the paragraph is now history with anchors re-measured at 9d481f05 — resolver mode.js:38, guard grant-broker-guard.js:186 — plus one clause on why the helper still normalizes for its own two gates. No behaviour change.

@lilyshen0722

Copy link
Copy Markdown
Contributor Author

Structure gate — CLEAR carried to 5c558887 (rebased onto main 9d481f05, cli 0.1.71).

Interdiff of the three-dot patch at 6cb3bd56 (base fe98462f) against the three-dot patch at 5c558887 (base 9d481f05), index lines stripped:

  • cli/package.json: base line 0.1.69 → 0.1.70; head line 0.1.71 in both readings' intent — the one-line carry the conflict required.
  • cli/src/commands/agent.js: hunk offsets (+1 line for feat(cli): withhold the grant broker on the run path (TASK-111) #1838's withholdGrantBroker import, now context), and the resolveAttachSandbox doc comment re-anchored to main — mode.js:38 and grant-broker-guard.js:186 both hold effectiveSandboxTrust(...) !== 'public' at 9d481f05, as the comment says. Past tense, same behaviour.
  • cli/__tests__/attach.test.mjs: identical.

No non-comment code line differs from the head I cleared. Behind 0, git merge-tree --write-tree origin/main 5c558887 exits 0. Test & Coverage was in progress at read time; Vera's carry is the measurement gate.

🤖 Generated with Claude Code

@lilyshen0722

Copy link
Copy Markdown
Contributor Author

Recut for the rider Wren ruled in 71718/71719: head f7996a3e (base 9d481f05, cli 0.1.71, 6 files).

  • assertSandboxDeclaredForPublicPod normalizes at the top of the predicate; witnesses — {trust:'internal'} with no mode on a public pod passes (short-circuiting before the pod read), internal + mode:'none' still refuses.
  • withDefaultSandbox normalizes for parity; witness — a legacy block is left untouched (the original object is returned, not the normalized copy).
  • Evidence: full cli suite 48/48 = 818 passed / 10 skipped; lint:cli clean; ledger 8/8 killed, with M7/M8 aimed at these two sites. Note for anyone re-running it: adding the read-path normalization made M1's original anchor non-unique (the same line now appears at both sites), so M1 is anchored on the helper's trust-then-mode order — the first run's ANCHOR NOT UNIQUE was the harness, not a survivor.
  • claude.js:672 out (withdrawn, 71727 — ctx is the normalized copy, rawCtx is the raw one; re-derived independently). pi.js:289 is TASK-114, its own PR and slot.

@lilyshen0722

Copy link
Copy Markdown
Contributor Author

Structure gate — CLEAR at f7996a3e (rider on top of 5c558887; base 9d481f05, cli 0.1.71).

Delta vs 5c558887 is one commit, four files, and exactly the two sites ruled in pod 71718/71719:

  • cli/src/commands/agent.js — assertSandboxDeclaredForPublicPod normalizes via normalizeSandboxTrust before its predicate; the predicate itself is unchanged. normalizeSandboxTrust was already imported at :35.
  • cli/src/lib/default-environment.js — withDefaultSandbox normalizes before the enforced check and still returns the ORIGINAL block. New import from ./environment.js; environment.js imports nothing from default-environment.js, so no cycle.
  • Witnesses: public-pod-sandbox-gate.test.mjs — legacy internal no-mode on a public pod resolves and client.get is never called; internal + mode:'none' still refuses. default-environment.test.mjs — a legacy internal block is returned by identity.

No other code line differs. git merge-tree --write-tree origin/main f7996a3e exits 0; behind 1 (main moved to a2fc1a0d with #1837), which is a plain merge from main for the press, not a structure concern. Test & Coverage in progress at read time; Vera's carry is the measurement gate.

🤖 Generated with Claude Code

The attach path derived the sandbox mode from the RAW `sandbox.trust` and then
gated on the raw value too. A stored legacy `internal` means `public`
(environment.js, Wren 69585), so the raw compare made `{ trust: 'internal' }` with
no declared mode resolve to `'none'`: no refusal AND no confinement — the exact
combination this gate exists to catch, and the case the daemon path already
handles (Wren's ruling B on #1838).

The same raw compare also refused the wrong way: `internal` beside
`mode: 'workspace'` threw as "implemented only for public codex or Claude
adapters", so a legacy record that SHOULD be confined was turned away.

Normalize once, at the top of a new exported `resolveAttachSandbox`, and derive
the mode and the refusal from the mapped value. Host availability (bwrap
installed, Seatbelt on macOS, whether the adapter honours the mode) stays at the
call site, where the host and adapter are known — the helper is pure.

Ledger 4/4 killed, including the half-fix (M3) that normalizes the returned trust
but still derives the mode from the raw one, and the over-refusing direction
(M4: treating an absent trust as public, which would stop every private seat
attaching).
…r way

Vera 71664 measured the row's premise and half the title was wrong: a legacy
`internal` record with no mode was NOT attaching unconfined. Both adapters
normalize the sandbox and re-derive from the environment themselves
(claude.js:615, codex.js:546), and the attach-time value never reaches them — so
the legacy record was confined regardless of what this gate computed. The
adapter suites already pin it on the exact shape attach mis-refused
(adapters.claude.environment.test.mjs:444 asserts sandbox-exec;
adapters.codex.test.mjs:257 asserts no bypass flag).

What is actually wrong is therefore narrower and still real: this gate's verdict
contradicted the adapters' in both directions — it refused `internal` beside
`mode: 'workspace'`, a record both adapters confine, and for the `mode: 'none'`
shape it refused nothing, so the refusal landed later at adapter spawn, after
attach had already published and minted.

Comment and test name corrected; behaviour unchanged. Re-verified: ledger 4/4
killed, cli 48/48, 805 passed / 10 skipped.
…es it moves in (Vera 71675)

Vera's third case is the one the raw compare failed CLOSED on: `internal` beside
`mode: 'workspace'` was refused as "implemented only for public codex or Claude
adapters", though both adapters confine that record and their suites pin it
(claude:444 spawns sandbox-exec, codex:257 asserts no bypass flag).

A test of the derivation alone could not have pinned it — the refusal was made at
the call site, so the test would have asserted the input to a branch rather than
the branch. The adapter-support check is pure (a mode and an adapter name), so it
moves into `resolveAttachSandbox` and now has witnesses of both signs: the
workspace record attaches on claude and codex, and is still refused on `pi`.

Host checks — bwrap installed, Seatbelt, the codex permission-profile version —
stay at the call site, where the host and the detected version are known. What an
adapter can honour at all is only a mode and a name, so that one is here.

Ledger 6/6 killed (M5 restores the raw compare in the adapter check and M6 drops
the check entirely, so the new witnesses cannot be no-ops). cli 48/48, 807 passed
/ 10 skipped, lint clean.
The comment described the pre-#1838 world in the present tense: 'the raw
compare inside resolvePublicSandboxMode (mode.js:31)'. On main that compare
is gone - the resolver reads the effective trust (mode.js:38) and the guard
imports the same reader from environment.js (grant-broker-guard.js:186) -
so the sentence named a line that no longer holds it and a defect that no
longer exists. Rewritten as the history it is, with anchors measured at
9d481f0, and one clause saying why this helper still normalizes on its own:
its two gates compare `trust` directly.

No behaviour change.
…3 rider)

Wren 71718/71719: this row's claim is "attach's inconsistent derivation is
closed", so leaving `assertSandboxDeclaredForPublicPod` reading trust raw
would make the claim false — a legacy `{trust:'internal'}` record on a
publicly-readable pod threw "declares no sandbox" one call BEFORE the gate
this PR fixes (the function runs on every attach, agent.js:640, outside the
`if (envPath)` branch). Normalized at the top of the predicate instead.

`withDefaultSandbox` (default-environment.js) is the predicate that
function's own comment says it mirrors, so it normalizes too. Its miss was
benign — the record was replaced by the default seat sandbox, same effective
trust — but a mirror that disagrees is a false comment. The ORIGINAL block is
still what gets returned when it is enforced.

Witnesses: internal-with-no-mode on a public pod now passes (and short-circuits
before the pod read); internal + `mode:'none'` still refuses; a legacy block is
left untouched by the daemon path.

Vera 71720 named two more sites; Wren 71725 measured `claude.js:672` as a
false positive (`ctx` IS the normalized copy — `rawCtx` is the raw one) and
Vera withdrew it (71727). `pi.js:289` under-refuses rather than over-refuses
and is spawn-path, so it is TASK-114 on its own slot, not this rider.

Ledger now 8/8 killed (M7/M8 aim at these two sites), full cli suite 48/48 =
818 passed / 10 skipped, lint clean.
@lilyshen0722
lilyshen0722 force-pushed the kai/attach-normalize-trust branch from f7996a3 to ae298de Compare September 23, 2026 09:56
@lilyshen0722
lilyshen0722 merged commit 95997f4 into main Sep 23, 2026
13 checks passed
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