Skip to content

fix(grants): the broker call refuses a seat that cannot confine it (TASK-175) - #1971

Merged
lilyshen0722 merged 2 commits into
mainfrom
kai/task175-broker-refusal
Sep 27, 2026
Merged

lilyshen0722 merged 2 commits into
mainfrom
kai/task175-broker-refusal

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

TASK-175. The confinement refusal ran where the broker is offered — the server projection, the grant read, the daemon's derive — and never where it is used. routes/mcpGrants.ts handed any caller holding a token and a grant id straight to callTool. For a write that is bounded by the approval park; for a read it is not bounded at all (reads never park, scope note §5), so confinement was the only bound and it held only on the offering side. Grant-generic, not hosted-specific. Backend only — no version bump (the one cli/ file is a test).

The two halves

1. The adapter is runtime.adapter OR runtime.runtimeType. commonly agent attach pi writes config.runtime = {runtimeType: 'pi', host: 'byo'} with no adapter key (cli/src/commands/agent.js:664 falls back to the adapter's name for the tag, :688 writes the pair), so the old rule — normalizeAdapter(runtime.adapter) — admitted exactly the seat it exists to refuse. A declared adapter still wins, as it does in the daemon (daemon.js:142); a row naming neither field is still not refused, because undeclared the daemon resolves only claude or codex, never pi, and failing closed there would refuse working claude seats.

2. The refusal now runs at the broker call. A new judgeSeatConfinement resolves the calling seat through the same projectSeatEnvironments the daemon's work list is built from, and callTool + listToolsForGrant throw RoomGrantError('grant_broker_unconfined', <the predicate's detail>, 403, {reason, decidedBy}) — the shared catch trails it refused, and it runs before the connection is resolved, the approval parked, and the budget line reserved, so a refusal is not a spendable call. tools/list is refused too: a seat that may not use the grant must not be handed its definitions (TASK-146's rule, now for the seat's own confinement).

Placement disclosed: Wren's wording was "the grants endpoint evaluates … before callTool". I put it inside the two service functions that endpoint calls, not in the route, because the ToolCall trail row and the budget reserve live there — a route-level refusal would have been untrailed, which the same sentence requires. It is still the endpoint's judgement, before any tool work, on both verbs.

Who judges the seat. req.agentInstallation is installations[0] on the bot-token path and can name a row from another pod, so the row is not the authority. The owners consulted are the installers of the seat's own installation rows, and this is complete rather than a sample: projectSeatEnvironments filters {installedBy} and keys each entry by the row's identity, so a seat can appear in a projection only when that owner installed one of its rows. Every installer is consulted, so two projections that disagree fail closed — the refusing one wins, whichever order they are scanned in.

The native path is out of scope (hosted: true, set only by grantBrokerProjectionService.dispatchHostedBrokerTool): a hosted turn has no shell, web or file tools, so there is nothing for a sandbox to confine (Wren 74882). Without the flag a hosted seat carrying a pi declaration would be refused the path the ruling excludes.

Evidence

seatGrantConfinement.test.js is new and runs on memory Mongo with real RoomGrant / User / AgentInstallation rows — the resolution and the predicate are the things under test, and a mocked model would prove a fact about the mock. The broker call is driven end to end against a real grant (assertGrantUsable real); only the Postgres trail, the connection and the provider are mocked, as the neighbouring broker suites do. No Postgres is reached.

  • 18 arms in the new suite; 153 tests / 12 suites across everything this change touches, 0 failures, 0 runtime errors.
  • Wren's six requested arms are all present: pi-in-audience refused on list and call, mode none refused, {public, workspace} passing to the audience check, absent passing, a refused call not spent, and the {runtimeType:'pi'}/{runtimeType:'claude-code'} pair — plus the two he added at 12:48Z ({host:'byo'} with neither field admitted; {adapter:'claude', runtimeType:'pi'} admitted) and the webhook connect-page seat.
  • Positive controls throughout: the same grant and seat on claude-code runs; the list resolves; grant_revoked still wins over grant_broker_unconfined (the new check does not preempt grant-level rules).
  • CLI pair pin (Wren 12:10Z): cli/__tests__/adapter-confinement-parity.test.mjs asserts listAdapterNames() minus claude/codex/stub equals ['pi'], with a comment naming CONFINEMENTLESS_ADAPTERS. The two literal pins are the guard, because neither side can import the other and the polarities are opposite (backend denylist, daemon allowlist).

Ledger — 12 mutations, 11 killed, one disclosed survivor

Each run asserts the (suites, tests) pair, so a suite that fails to compile cannot read as a kill. Killed: the runtimeType fallback dropped (predicate-level and call-level), the hosted exemption removed, the refusal moved after the budget reserve, the judge dropped from the list, the judge dropped from the call, "first projection decides both ways", the non-ObjectId guard removed, the bot-row key fallback removed, the judged flag collapsed to unbound. The call-level drops are killed by two suites at once (the real-Mongo one and the mocked seam), which is the point of having both.

Survivor (kept as a tripwire): removing the keys.has(...) filter in the installer scan changes no verdict. Extra owners are harmless because the projection lookup is itself keyed, so an owner with no row for this seat contributes nothing. The filter is an intentional bound on how many projections one call makes, not a rule — it is redundant by construction and no arm can make it load-bearing.

A mutation that was wrong, disclosed because it nearly passed: my first "move the refusal after the reserve" duplicated the call site instead of moving it, so it survived while the code was correct. Fixing the mutation exposed the real defect in the arm: the refusal test's grant carried no budget, so reserveBudgetLineage was unreachable and expect(...).not.toHaveBeenCalled() was vacuous. The grant now carries a budget on purpose.

Not closed here

grantBrokerRefusal is returned by the grants read (routes/grants.ts:513) and no frontend file reads it — verified by grep over frontend/src. The grant's page showing the refusal, which Wren 74883 asked for, is therefore still open and is not touched by this PR. Also unchanged: the server is a denylist and the daemon an allowlist, which is why the adapter-list pin exists.

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