Skip to content

fix(broker): a suspended owner's connection stops authorising calls (TASK-181) - #2029

Merged
lilyshen0722 merged 3 commits into
mainfrom
kai/task181-broker-ban
Sep 29, 2026
Merged

lilyshen0722 merged 3 commits into
mainfrom
kai/task181-broker-ban

Conversation

@lilyshen0722

@lilyshen0722 lilyshen0722 commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

The hole, at source

A ban is enforced at sign-in (middleware/auth.ts:74) and on the socket handshake, and nowhere else. But the broker's caller is an agent, which never signs in as the owner: it presents its own runtime token (mcpGrants.ts:50 → agentRuntimeAuth, which does not read banned) and the broker spends the credential the owner connected. The in-process path (nativeRuntimeService.ts:1110 → dispatchHostedBrokerTool → callTool) is the same shape. So banning a person stopped the person and left their authority running.

Checked first, because it bounds the fix: bots cannot be banned at all — routes/admin/users.ts:249 refuses isBot targets. banned is a human-only state, and the gap is exactly the human's hosted row outliving their ban.

The fix — plan §7, as ratified

assertHostedOwnerUsable, called from resolveHostedConnection. Both broker paths funnel through resolveConnection, so the check sits where the credential is chosen rather than in either driver.

  • The predicate comes from sessionAccountService (loadSessionAccount + sessionRefusal), the one live-row read the session verifiers share. That is not style: the service's own doc names the failure of recomputing it — "a verifier that forgets one of the two fields in its projection reads undefined and refuses nothing." A check written as select('banned') omits isBot, so a bot-owned row stays callable; sessionRefusal reads both fields together. The ledger carries that exact mutation (M07) and it dies on the bot arm.
  • All three codes, each naming its remedy: connection_owner_banned (lift the ban), connection_owner_missing (remove the row so its owner can be replaced), connection_owner_bot. All 403, beside connection_superseded.
  • A row with no createdBy is missing, not a database question: an id that cannot name an account never reaches findById, whose CastError would arrive as a 500.
  • hosted-mcp only. A github-app row's token belongs to the app installation, not to the admin who created the row, so a suspension must not disable the app connector for every pod holding a grant on it. The exclusion is pinned by an arm beside the code, and inverting it is ledger mutant M02 (23 red).
  • Read at call time — listToolsForGrant, callTool and executeApprovedToolCall all resolve through it, so one check covers the offer, the call, and a call approved after the ban. Nothing is written when the ban is set, and a lifted ban needs no reconnect. A refused list writes no row, because a list is not a call.

One existing arm re-pointed rather than deleted

The park-path arm showing that a permanent refusal keeps its own code used to reach resolveApprovalPodId's connection_mismatch through a row with no createdBy. The owner check now refuses that fixture earlier and more specifically, so the arm walks the same path through invalid_target instead. Its subject is unchanged — only the cause moved — and the comment says so, because a guard added above an existing one can invalidate the arms below it without changing a line of them.

A refused owner refusal records the owner it names (vera 75542)

The guard throws inside resolveHostedConnection, which is where connection.ownerUserId is resolved, so the catch's local was unset on exactly the row whose reason names the owner: a connection_owner_banned record could not answer which suspended person's connection was this. Not an authority gap — an audit one.

The guard now carries the account id on the error's details and both catches prefer it over the local (refusedCredentialOwner). Only an account-shaped id is carried (/^[a-f\d]{24}$/i): the column is read as an owner id, so a malformed value would be a guess wearing a fact's clothes — the blank is more honest. The empty case stays blank either way. Two sites still deliberately record no owner, and both read undefined, not a guess: a call refused before a connection resolved, and a row with no createdBy.

No authority moves. This changes which string lands in the audit column, never whether a call is refused.

Wren's rule (75546), pinned as arms

A record names an owner only once the row is PROVEN the grant's own. connection_mismatch, connection_untracked and connection_superseded are that proof failing, and they record nobody: a superseded row is the re-added one, whose createdBy never made the grant. The owner check runs after the proof, so its codes carry the id as staged.

Verified at source rather than taken: resolveHostedConnection runs type/status/revokedAt → entryId match → both creation timestamps (connection_untracked) → supersession (connection_superseded), and only then reads row.createdBy. So the rule holds by construction, and it is now pinned by fixtures that would read the row's creator — the superseded and untracked arms assert the refused row's credentialOwnerId is undefined while their fixture row carries OWNER. The three docblocks (recordCall's overrides, refusedCredentialOwner, and the callTool local) now state the rule instead of an over-broad claim.

Evidence at 84765504

Rebased onto main 8e3270ef (#2028 landed as 1d0b82b9), so this branch is cut from current main and not stacked.

  • Touched suites 57/57 (github 21, hosted 25, approval 11); neighbourhood widened to toolBroker|approval|ToolCall|grants|seatGrant|roomGrant|nativeRuntime|sessionAccount|hostedMcp — 44 suites / 426 passed / 0 failed.
  • Full backend 501 suites / 4733 tests / 4708 passed / 1 failed / 24 skipped. The single failure is agentsRuntime.rateLimitTiers under full-suite load; it passes 3/3 in isolation (verified this run).
  • eslint on services/toolBrokerService.ts: 0 findings; tsc:check clean and the touched files add no error to the full tsc --noEmit (its 50 errors are all in four root scratch files). Modified test files: +0 / −1 against main by message-multiset diff (max-len, on a line this branch rewraps).
  • Ledger (/tmp/kai-181-rebase-ledger.py, occurrence-asserted anchors with explicit nth, pristine restored after every run, baseline asserted before every run): 13 mutants / 13 killed / 0 survivors / 0 compiler-refused / 0 NAMED-ARM MISSES / all files restored. Nine for the owner check (call site, the §7 inversion, each code mapping, the status, the predicate recomputed from banned alone, a missing id sent to the database, the check reading the seat instead of the owner), three for the carried id (the refused row ignoring what the guard carried, the guard refusing without carrying it, a value that names no account recorded anyway), and one for Wren's rule (a proof-failing refusal recording the row's owner anyway).
  • Acceptance controls are in the ledger's named arms on purpose: a guard that refused everything would pass a refusal-only suite, so each path has an arm showing the same fixture still runs when the owner is usable, and the §7 arm shows a suspended github-app owner is left alone.

Landed: the merge-order note is resolved

#2028 pressed as 1d0b82b9; this head is rebased past it. The two assertions the note named — the parked-call arm's credentialOwnerId and the approval-proposal arm's credentialOwnerId — now read OWNER, as the note said they must. 'owner-1' remains correct only inside the malformed-owner arm, where naming no account is the point.

Gate: Vera (code), asked at this head.

samxu01 pushed a commit that referenced this pull request Sep 29, 2026
…only hosted (§7)

Rework after Vera's hold on #2029. The first cut diverged from the ratified plan
on main in three ways, and the plan is specific:

- The predicate now comes from `sessionAccountService` (`loadSessionAccount` +
  `sessionRefusal`), the one live-row read the session verifiers share. The
  service's own doc names the failure of recomputing it: a verifier that forgets
  one of the two fields in its projection reads `undefined` and refuses nothing.
  `select('banned')` omits `isBot`, so a bot-owned row stayed callable — for that
  reason alone, since `sessionRefusal` reads both fields together. A mutation that
  recomputes the predicate from `banned` alone is in the ledger and dies on the
  bot arm.
- All three codes are rendered, each naming its own remedy: `connection_owner_banned`
  (lift the ban), `connection_owner_missing` (remove the row so its owner can be
  replaced), `connection_owner_bot`. A row with no `createdBy` at all is `missing`
  rather than a database question, so an uncastable value never reaches `findById`.
- It applies to `hosted-mcp` ONLY. A `github-app` row's token belongs to the app
  installation, not to the admin who created the row, so suspending that admin
  must not disable the app connector for every pod holding a grant on it. The
  exclusion is pinned by an arm beside it, and the §7 inversion is a ledger mutant.

One existing arm re-pointed, not deleted: the park-path arm showing that a
permanent refusal keeps its own code used to reach `resolveApprovalPodId`'s
`connection_mismatch` through a row with no `createdBy`. The owner check now
refuses that fixture earlier and more specifically, so the arm walks the same
path through `invalid_target` instead. Its subject is unchanged; only the cause
moved, and the reason is in the comment.

Ledger: 9 mutants, 9 killed, 0 survivors, 0 NAMED-ARM MISSES.
…TASK-181)

A ban was enforced only at sign-in (`middleware/auth.ts`). An agent acting on
someone's connection never signs in as them: it presents its own runtime token
and the broker spends the owner's stored credential, so banning the person
stopped the human and left the authority working — on both broker paths, since
neither `mcpGrants`' agentRuntimeAuth nor the in-process native runtime reads
`banned`.

The check goes where every broker call already resolves the connection:
`resolveConnection`, through one `assertOwnerNotBanned` called by both resolvers
(the github-app branch and the hosted-MCP branch). Both paths — the MCP endpoint
and `nativeRuntimeService` → `dispatchHostedBrokerTool` → `callTool` — reach it
there, and a new connection type is a third call site the helper's doc names.

Refusal is `connection_owner_banned`, 403, recorded on the trail like every other
refused call, so the listing refuses too: a list is a capability disclosure and
must not offer tools every call would reject.

Two boundaries, both pinned by arms: a `createdBy` that cannot be an id shape is
never looked up (the value is written server-side from the authenticated user's
id and typed ObjectId; skipping keeps an uncastable string away from `findById`,
whose CastError would otherwise have to be swallowed, and a swallowed database
error would allow the call), and an owner row that no longer exists is not a
ban — "no ban recorded" is not "banned".

No migration and no new state: banned is already the User document's field.
…only hosted (§7)

Rework after Vera's hold on #2029. The first cut diverged from the ratified plan
on main in three ways, and the plan is specific:

- The predicate now comes from `sessionAccountService` (`loadSessionAccount` +
  `sessionRefusal`), the one live-row read the session verifiers share. The
  service's own doc names the failure of recomputing it: a verifier that forgets
  one of the two fields in its projection reads `undefined` and refuses nothing.
  `select('banned')` omits `isBot`, so a bot-owned row stayed callable — for that
  reason alone, since `sessionRefusal` reads both fields together. A mutation that
  recomputes the predicate from `banned` alone is in the ledger and dies on the
  bot arm.
- All three codes are rendered, each naming its own remedy: `connection_owner_banned`
  (lift the ban), `connection_owner_missing` (remove the row so its owner can be
  replaced), `connection_owner_bot`. A row with no `createdBy` at all is `missing`
  rather than a database question, so an uncastable value never reaches `findById`.
- It applies to `hosted-mcp` ONLY. A `github-app` row's token belongs to the app
  installation, not to the admin who created the row, so suspending that admin
  must not disable the app connector for every pod holding a grant on it. The
  exclusion is pinned by an arm beside it, and the §7 inversion is a ledger mutant.

One existing arm re-pointed, not deleted: the park-path arm showing that a
permanent refusal keeps its own code used to reach `resolveApprovalPodId`'s
`connection_mismatch` through a row with no `createdBy`. The owner check now
refuses that fixture earlier and more specifically, so the arm walks the same
path through `invalid_target` instead. Its subject is unchanged; only the cause
moved, and the reason is in the comment.

Ledger: 9 mutants, 9 killed, 0 survivors, 0 NAMED-ARM MISSES.
The owner check throws inside `resolveHostedConnection`, which is where
`connection.ownerUserId` is resolved, so the catch's local was unset
exactly on the row whose reason names the owner: a
`connection_owner_banned` record could not answer "which suspended
person's connection was this".

The guard now carries the account id on the error's details and both
catches prefer it over the local. Only an account-shaped id is carried —
`VARCHAR(255)` is read as an owner id, so a malformed value would be a
guess wearing a fact's clothes; the blank is more honest.

Also pins Wren's rule (75546) as arms: a record names an owner only once
the row is PROVEN the grant's own. `connection_mismatch`,
`connection_untracked` and `connection_superseded` are that proof
failing; a superseded row is the re-added one, whose `createdBy` never
made the grant, so those refusals record nobody even though the row
carries the owner. The docblocks now say so, and the superseded and
untracked arms assert it against a fixture row that does carry an owner.

No authority moves: this changes which string lands in the audit column,
never whether a call is refused.
@samxu01
samxu01 force-pushed the kai/task181-broker-ban branch from ff25d41 to 8476550 Compare September 29, 2026 22:15
@lilyshen0722
lilyshen0722 added this pull request to the merge queue Sep 29, 2026
Merged via the queue into main with commit f289592 Sep 29, 2026
23 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