Skip to content

feat(broker): a hosted tool reaches the vendor, and a seat can see it (TASK-172 slice 3c-2) - #2005

Merged
lilyshen0722 merged 3 commits into
mainfrom
kai/task172-catalogue
Sep 29, 2026
Merged

lilyshen0722 merged 3 commits into
mainfrom
kai/task172-catalogue

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

Slice 3c-2 — the last of TASK-172's stack, cut from merged main now that #2001 and #2002 have landed. A hosted-MCP Connection could already be resolved by the broker (#1993/#1995) and minted into a grant (#2002). This is the slice where a hosted tool actually reaches the vendor, and where a seat's tool list contains it.

What changed

  • ToolConnection and ToolDefinition are discriminated unions — a GitHub App connection and a hosted-MCP Connection row, with the matching two definition members. A definition's call declares the connection it runs against, so the compiler refuses the reads that would otherwise be undefined at runtime.
  • providerConnection rebuilds each member field by field. It used to narrow every connection to {type, installationId, owner, repo} — exactly where a hosted connection's entryId would have disappeared on its way to the provider.
  • allToolDefinitions() is the one source both surfaces read — the runtime projection (hostedBrokerToolsForRun) and listToolsForGrant — while getToolDefinitions() stays the GitHub record that toolInstallables validates a GitHub grant against, so a hosted name cannot pass a GitHub-only gate.

resolveHostedConnection is the hosted half of resolveConnection: the row is found by _id (a hosted grant's connection id is the row's _id, so the TASK-148 class cannot arise by a second row matching), it must be connected, its config.entryId must equal the entry the definition was projected from, and the TASK-148 creation-time guard runs unchanged. It carries the row's createdBy as ownerUserId, because resolveApprovalPodId needs it to open the approval room for a seat-targeted grant.

hostedMcpToolDefinitions projects each entry's pins: the call resolves the row, fetches the credential inside the call (so listing a seat's tools never refreshes one, and a call that never happens never spends one) and posts one JSON-RPC tools/call to the entry's resource — the same URL RFC 8707 put in the authorization request, so the audience the vendor validated is the audience it is presented to. Both JSON and SSE replies are accepted; a 401/403 from the vendor is refused as credential_rejected (only a new consent restores such a connection); the vendor's own tool-level error is returned as the result rather than thrown, so the model reads the vendor's explanation.

Claims the arms exist to stop pinning wrongly

  • The row's config.entryId must equal the entry the definition was projected from, so a grant on one vendor's row cannot run another vendor's tool. The arm asserts the message, not just the code: the tool's own call refuses the same mismatch one layer down with the same code, so the code alone left a mutant alive in the arm whose own name claims it.
  • A parked hosted write needs the row's createdBy as ownerUserId. The control arm asserts no room is requested and the vendor is never reached.
  • getToolDefinitions() must NOT contain a hosted name; allToolDefinitions() must.
  • Joined by NAME, not index: hostedMcpToolDefinitions throws if a projection names no pin, because an index join would silently pair a projection with the wrong tool the first time entryToolProjections stops mapping one-to-one.

Recorded, not fixed here

  1. The park path's catch converts every failure, so a missing-owner connection_mismatch surfaces as approval_unavailable. That is how the arm above observes the missing owner; the conversion is pre-existing and outside this slice.
  2. listToolsForGrant resolves one connection per connection TYPE. Correct today — the TASK-148 unique index covers the pair, and v1 has at most one hosted row per definition — and a second hosted row for the same entry would need it revisited.

Due with the FIRST vendor entry, not with this PR

HOSTED_MCP_ENTRIES is [] on main, so no hosted row or grant can exist yet and none of this is reachable in production paths. The two preconditions @vera set for the first entry therefore stay open: (1) assertHostedMcpEntries throws when a read pin's own annotations carry destructiveHint: true / readOnlyHint: false; (2) the same /^[a-z0-9-]+$/ charset on tool.name. Non-blocking at the same gate: assess returns entryId from its argument, and the in-flight promise is not cached. Carried forward from the 6g review: the cross-origin Connect XHR must send credentials, or Set-Cookie is dropped and every callback refuses invalid_state.

Evidence

  • baseline (2, 30) on the rebased tree; tsc:check exit 0
  • ledger: 18 mutants, 14 killed, 4 compiler-refused, 0 survivors, 0 named-arm misses, 0 not-applied
  • rebased with --onto origin/main a7bb93a9 (the branch's own three commits); range-diff shows content preservation; delta is 6 files, +1114/−42

Two instrument findings, both recorded because each cost a run: assertGrantUsable's tool is optional, and findById(...).lean() resolves to null rather than being null. The ledger itself needed explicit occurrence selection on its anchors — resolveHostedConnection and resolveConnection carry verbatim-identical nine-line guards, so a mutant aimed at the hosted copy was silently hitting the GitHub copy and reading as a survivor; selecting the occurrence and printing its match count turned it into a kill with no test change. And where a guard's shape is what makes the lines below it typecheck, if (false) is a compiler refusal, not a test gap — recorded as evidence, with a compiling sibling mutant added so the claim also has a runtime witness.

… its own (TASK-172)

`ToolConnection` becomes a discriminated union of a GitHub App connection and a
hosted-MCP Connection row, and `ToolDefinition` becomes a union of the matching
two members. A definition's `call` now declares the connection it can run
against, so the compiler refuses the reads that would otherwise be `undefined`
at runtime — and `providerConnection` rebuilds each member field by field,
because it used to narrow every connection to `{type, installationId, owner,
repo}`, which is exactly where a hosted connection's `entryId` would have
disappeared on its way to the provider.

`resolveHostedConnection` is the hosted half of `resolveConnection`: the row is
found by `_id` (a hosted grant's connection id IS the row's `_id` — there is no
installation id to reuse, so the TASK-148 class cannot arise by a second row
matching), it must be `connected`, its `config.entryId` must be the entry the
definition was projected from, and the TASK-148 creation-time guard runs
unchanged. It carries the row's `createdBy` as `ownerUserId` because
`resolveApprovalPodId` needs it to open the approval room for a seat-targeted
grant: without it a parked hosted write refuses `connection_mismatch` instead of
parking.

`hostedMcpToolDefinitions` builds the definitions themselves: each entry's
projection plus the `call` that resolves the row, fetches §4's credential
INSIDE the call (so listing a seat's tools never refreshes one, and a call that
never happens never spends one) and posts one JSON-RPC `tools/call` to the
entry's `resource` — the same URL RFC 8707 put in the authorization request, so
the audience the vendor validated is the audience it is presented to. Both JSON
and SSE replies are accepted, a 401/403 from the vendor is refused as
`credential_rejected` (only a new consent restores such a connection), and the
vendor's own tool-level error is returned as the result rather than thrown, so
the model reads the vendor's explanation.

Joined by NAME, not by index: `hostedMcpToolDefinitions` throws if a projection
names no pin, because an index join would silently pair a projection with the
wrong tool the first time `entryToolProjections` stops mapping one-to-one.
… (TASK-172)

The broker could resolve a hosted connection but never ran a hosted tool: the
seat's tool list is built from the GitHub record only, so a hosted grant
projected zero tools, and the two dispatch sites looked the tool up in that same
record. `allToolDefinitions` is now the one source both surfaces read — the
runtime projection and `listToolsForGrant` — while `getToolDefinitions` stays
the GitHub record `toolInstallables` validates a GitHub grant against, so a
hosted name cannot pass a GitHub-only gate.

Three arms carry the claims that a passing suite could otherwise pin wrongly:

- The row's `config.entryId` must equal the entry the definition was projected
  from, so a grant on one vendor's row cannot run another vendor's tool.
- A parked hosted write needs the row's `createdBy` as `ownerUserId`, because
  that is what opens the approval room for a seat-targeted grant. Without it the
  write cannot park, and the control arm asserts no room is requested and the
  vendor is never reached — the missing owner shows up as `approval_unavailable`
  (the park path's catch converts every failure), which is recorded as an
  observation rather than fixed here.
- The GitHub record stays GitHub-only: `getToolDefinitions()` must NOT contain a
  hosted name, and `allToolDefinitions()` must.

Two harness facts the arms taught, both recorded because they cost a run each:
`assertGrantUsable`'s `tool` is optional (`listToolsForGrant` asserts the grant
without naming one), and `findById(...).lean()` resolves to null rather than
being null.
The arm asserted the code alone, and the tool's own `call` refuses the same
mismatch one layer down with the same code — so a mutant that broke the
broker-level guard survived the arm whose name claims it. The arm now asserts the
message, which only the broker-level guard produces.
@samxu01
samxu01 force-pushed the kai/task172-catalogue branch from d41b1fb to fabf343 Compare September 29, 2026 14:01
@lilyshen0722
lilyshen0722 added this pull request to the merge queue Sep 29, 2026
Merged via the queue into main with commit 3c5cbff Sep 29, 2026
16 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