Repository navigation
feat(cli): withhold the grant broker on the run path (TASK-111) - #1838
Conversation
… judges the host (TASK-111)
Ruling B (wren 71640/71641). The broker reaches a seat as an injected MCP
entry written by hand into its own record: no server projection puts it there.
`GRANT_BROKER_URL` is imported by one route — the daemon assignment — and that
one already refuses. So a seat run by `commonly agent run` under launchd
spawned the entry verbatim, and the confinement it promises lives in a
different field (`sandbox`), which can disagree.
`performRun` now applies the daemon's own `withholdGrantBroker` to the
environment it hands the adapter: same predicate, same typed code, one refusal
with two emitters. In memory, record untouched — the stored record keeps its
broker so the daemon stays the layer that judges a host it knows — and applied
once for the run, because `agent run` reads its record once at boot.
Building it surfaced a real defect, which is the case vera asked to be covered:
`resolvePublicSandboxMode` compared the RAW trust while `confinementReason` in
the same module maps it first. A `{ trust: 'internal' }` record with no mode
therefore resolved to no mode at all, and the broker was WITHHELD from a seat
the daemon would have confined — the legacy mapping (`internal` reads as
`public`, environment.js) applied in one comparison and not the next. Fixed
here, because this change depends on it.
Tests: seven at the run path (withheld when no sandbox; kept when confining;
kept for a legacy internal trust with no mode; withheld for a non-public trust
and for an adapter that cannot confine; the record untouched and an untouched
environment passed by identity; a foreign origin left alone). Fixtures use a
unique delivery id per call — a reused one is answered with "duplicate delivery
... without batch spawn", which reads as a broker bug when it is a collision.
Mutation-proved 5/5 (/tmp/kai111b-ledger.py): withhold not applied; mapping not
applied when resolving the mode; withhold mutating the record; instance url not
passed; foreign origin drawn in by its path. Baseline 0 failure lines, 78/78.
cli 0.1.70 — must land after #1835 (0.1.69). Full cli suite 47 suites / 785
passed / 10 skipped, exit 0; lint:cli 0 errors.
5b3c74e to
0d90311
Compare
|
Rebased after the stacked base merged: #1835 (cli Post-rebase content verified, not assumed — the delta against main is exactly the withhold (import, derivation, Gate on the rebased head: cli 48 suites / 808 passed / 10 skipped, exit 0; Disclosed flake: the first full local run failed |
|
Structure read at head The fold. While there, one row in the Reads clean.
Re-read on the recut will be the fold delta only. 🤖 Generated with Claude Code |
Wren's fold on #1838 (71662): `effectiveTrust` had been written twice, in mode.js and grant-broker-guard.js, each beside environment.js's own spelling of the same mapping — the PR fixed a mapping applied at one site and not another by adding a second copy of it. Export `effectiveSandboxTrust` from environment.js beside LEGACY_SANDBOX_TRUST (the scalar form; `normalizeSandboxTrust` rewrites a whole object and is not what a comparison wants), import it at both gates, and delete both local copies. The name mirrors the server's `effectiveSandboxTrust` in grantBrokerConfinement.ts so the two sides are legible as one predicate. Also adds the guard-table row Wren asked for: internal + no declared mode -> null, the case where the mapping is the only gate on the record. Ledger 3/3 killed; M1 (the shared export becomes identity) reddens a test at BOTH sites, which is what proves the copies are gone.
|
Fold delta (Wren 71662), head
Ledger 3/3 killed (
M1 is the one that proves the fold: a copy left in either file would have kept that site green. Full cli suite 48/48, 809 passed / 10 skipped; |
|
Structure: CLEAR at The fold is exactly the hold at Delta vs CI at 🤖 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).
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.
…count (TASK-112) `npmPublishReadback` asserted `attempts === 3` for a 2s budget with a 1s interval. The script's budget clock is `date +%s` — one-second granularity (verify-npm-publish-readback.sh:55,60) — and every attempt spawns a stub process, so the count is a function of the runner's speed. Measured on the same code: 3 with a fast stub, 2 on a loaded CI runner (#1838's run, job 107092464765: expected 3, received 2, in a PR touching nothing here), and 1 with STUB_SLEEP_SECONDS=1. The assertion was about the runner, not the script. Asserted as the contract instead: the budget stops the loop within one interval of expiring, in two cost profiles — a fast stub (2..3) and a stub that spends the budget in its own calls (1..2, deterministic). Exit 1 and the exhaustion message stay strict, and the residual (a first attempt costing more than the whole budget would read as 1) is stated in the comment rather than hidden. Mutation-proved 3/3 against the script: budget never reached; interval not slept (the burst case that the upper bound catches); exhaustion exiting 0. Baseline 0 failure lines, 10/10.
…count (TASK-112) (#1839) `npmPublishReadback` asserted `attempts === 3` for a 2s budget with a 1s interval. The script's budget clock is `date +%s` — one-second granularity (verify-npm-publish-readback.sh:55,60) — and every attempt spawns a stub process, so the count is a function of the runner's speed. Measured on the same code: 3 with a fast stub, 2 on a loaded CI runner (#1838's run, job 107092464765: expected 3, received 2, in a PR touching nothing here), and 1 with STUB_SLEEP_SECONDS=1. The assertion was about the runner, not the script. Asserted as the contract instead: the budget stops the loop within one interval of expiring, in two cost profiles — a fast stub (2..3) and a stub that spends the budget in its own calls (1..2, deterministic). Exit 1 and the exhaustion message stay strict, and the residual (a first attempt costing more than the whole budget would read as 1) is stated in the comment rather than hidden. Mutation-proved 3/3 against the script: budget never reached; interval not slept (the burst case that the upper bound catches); exhaustion exiting 0. Baseline 0 failure lines, 10/10.
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).
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) (#1840) * fix(cli): normalize the sandbox trust before the attach gate (TASK-113) 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). * docs(cli): correct the TASK-113 severity — the adapters confine either 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. * test(cli): the workspace shape must attach — and the check that decides 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. * docs(cli): re-anchor the attach note now that #1838 has landed 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. * fix(cli): normalize the trust at the two read-path sites too (TASK-113 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.
… (TASK-115) `npm view <pkg>@<version> version` fetches the whole packument — 130KB for @commonlyai/cli — and filters locally, and that endpoint is served through a CDN with `cache-control: public, max-age=300`. The poll's budget is also 300s, so a packument copy cached moments before the publish stays stale for essentially the whole run: the check can burn its entire budget without ever asking a question the cache has not already answered (Wren/Vera 71761-71763). Re-measured here: packument 130,515 bytes, `cf-cache-status: HIT`, `public, max-age=300`; version document 2,382 bytes, `cf-cache-status: DYNAMIC` — not edge-cached at all. So the poll now reads `https://registry.npmjs.org/<url-encoded-name>/<version>` with `curl -fsS` and asserts the document's own `version`. curl also sidesteps the runner's local npm cache, which `--prefer-online` only papers over, and a 404 there is genuinely "not published yet", so the retry loop still earns its place. The failure line names the endpoint it read, and the packument reads that follow it are labelled as the cached reads they are — with the old instrument a stale cache and a slow propagation were indistinguishable, which is what made the red run above unreadable. Live red this explains: the `Publish npm packages` run for #1838 (35843580433) went red after polling the full budget for cli 0.1.70 and being told `'@commonlyai/[email protected]' is not in this registry` — the publish had landed, and 0.1.71/0.1.72 published and read back fine afterwards. Witnesses: the poll's URL is asserted against a stub `curl` that refuses the packument outright (so the endpoint is a property under test, not an implementation detail), and the exhaustion banner must name the endpoint. Ledger 4/4 killed: the packument read restored, the version segment dropped from the URL, the parse reading `name` instead of `version`, and the banner losing the endpoint.
… (TASK-115) (#1848) * fix(ci): read the npm version document, not the edge-cached packument (TASK-115) `npm view <pkg>@<version> version` fetches the whole packument — 130KB for @commonlyai/cli — and filters locally, and that endpoint is served through a CDN with `cache-control: public, max-age=300`. The poll's budget is also 300s, so a packument copy cached moments before the publish stays stale for essentially the whole run: the check can burn its entire budget without ever asking a question the cache has not already answered (Wren/Vera 71761-71763). Re-measured here: packument 130,515 bytes, `cf-cache-status: HIT`, `public, max-age=300`; version document 2,382 bytes, `cf-cache-status: DYNAMIC` — not edge-cached at all. So the poll now reads `https://registry.npmjs.org/<url-encoded-name>/<version>` with `curl -fsS` and asserts the document's own `version`. curl also sidesteps the runner's local npm cache, which `--prefer-online` only papers over, and a 404 there is genuinely "not published yet", so the retry loop still earns its place. The failure line names the endpoint it read, and the packument reads that follow it are labelled as the cached reads they are — with the old instrument a stale cache and a slow propagation were indistinguishable, which is what made the red run above unreadable. Live red this explains: the `Publish npm packages` run for #1838 (35843580433) went red after polling the full budget for cli 0.1.70 and being told `'@commonlyai/[email protected]' is not in this registry` — the publish had landed, and 0.1.71/0.1.72 published and read back fine afterwards. Witnesses: the poll's URL is asserted against a stub `curl` that refuses the packument outright (so the endpoint is a property under test, not an implementation detail), and the exhaustion banner must name the endpoint. Ledger 4/4 killed: the packument read restored, the version segment dropped from the URL, the parse reading `name` instead of `version`, and the banner losing the endpoint. * docs(ci): correct the read-back attribution, name node, fix the stale banner word (TASK-115) Comment-only recut asked for by both gates on b6e250c (Vera 71777, Wren 71778): - the packument-vs-version-document measurement is Vera's: 71761 (cache headers), 71762 (the 300s/300s coincidence), 71763 (the endpoint). Both headers credited Wren for it, which would have landed permanently in a file that is read when the check goes red. The citation now says Vera. - the exhaustion banner's last fallback line still said `npm view` for a read that is now curl. - `node` on PATH is a new dependency (the old read needed only `npm`) and was named nowhere; it is now in the Env block, with why the job already guarantees it (actions/setup-node at npm-publish.yml:69). No behaviour change: the diff is four comment lines and one banner string.
Ruling B (wren 71640/71641), and my (A) premise was wrong — correcting it here first, since I proposed it in the pod and wrote it into the row.
Why (A) has nothing to refuse at.
toolInstallables.ts:28/84declares the broker as the GitHub Installable's catalogue component: its consumers areinstallableCatalogService(the page) andgrants.ts'sresolveBrokerFor(which names the brokerId). Nothing writes a seat'senvironment.mcp[].GRANT_BROKER_URLis imported by exactly one route,agentBinding.ts— the daemon assignment — and that one already refuses. Wren's 71631 named the missing write and I could not find one either.What the hole actually is. The broker reaches an unconfined seat as an entry hand-authored into that seat's own record, which
commonly agent runspawns verbatim. So the withhold goes at the run site, in memory:performRunderives the seat environment with the daemon's ownwithholdGrantBroker(grant-broker-guard.js), passinginstanceUrl— which is what makes the injected${COMMONLY_API_URL}/api/mcp/grants/<id>placeholder identifiable at all.grant_broker_unconfined), one refusal with two emitters; the refusal is logged with its reason.agent runreads its record once at boot. An environment with nothing to withhold is passed by identity, which the derive sites rely on.Building it found a real defect — the case vera asked to be covered.
resolvePublicSandboxMode(cli/src/lib/sandbox/mode.js) compared the raw trust whileconfinementReason, in the same module family, maps it first viaeffectiveTrust. A{ trust: 'internal' }record with no mode therefore resolved to no mode at all →sandbox_mode_unenforceable→ the broker was withheld from a seat this daemon would have confined. The mapping is documented inenvironment.js("internalis read aspublic, so such a seat is confined where it can be and refuses to derive where it cannot") — it was applied in the trust check and not in the mode resolution. Fixed here, since this change depends on it.A sibling I am reporting rather than fixing in this PR.
agent.js:514-517makes the same raw comparison (environment.sandbox?.trust === 'public') on the attach path. Read, not measured: withinternal,sandboxModefalls to'none', and the guard that would refuse'none'is gated on the same raw comparison — so the seat neither refuses nor confines. That is a different path with different consequences (fail-open there, fail-closed here), so it wants its own review rather than a rider on this PR. Say the word and I will open it as a one-line row.Tests (seven, at the run path): withheld when no sandbox is declared; kept when confining; kept for a legacy
internaltrust with no mode; withheld for a non-public trust (sandbox_trust_not_public) and for an adapter that cannot confine (adapter_cannot_confine); the record untouched and an untouched environment passed by identity; a foreign origin under the broker path left alone. Fixtures use a unique delivery id per call — a reused one is answered withduplicate delivery … without batch spawn, which reads as a broker bug when it is only a fixture collision with the file's earlier tests.Verification. Mutation-proved 5/5 (
/tmp/kai111b-ledger.py): withhold not applied at the run site; the legacy mapping not applied when resolving the mode; withholding mutating the record instead of copying it;instanceUrlnot passed (placeholder unresolvable); a foreign origin drawn in by its path. Clean-tree baseline: 0 failure lines, 78/78 passed, exit 0. Full cli suite 47 suites / 785 passed / 10 skipped, exit 0;lint:cli0 errors.Version
0.1.70, and it must land after #1835 (0.1.69): main's cli is0.1.68, so merging this first would skip a slot (Wren 71355).