Skip to content

fix(broker): refuse a grant that predates the connection row it resolves to (TASK-148) - #1924

Merged
lilyshen0722 merged 1 commit into
mainfrom
kai/task148-grant-predates-connection
Sep 26, 2026
Merged

lilyshen0722 merged 1 commit into
mainfrom
kai/task148-grant-predates-connection

Conversation

@lilyshen0722

@lilyshen0722 lilyshen0722 commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

The defect

A RoomGrant stores the installation id in connectionId (installableCatalogService.ts:79), and resolveConnection looks the row up by that same value. So a grant issued for a row that has since been deleted keeps resolving after the same GitHub installation is added again: the re-added row answers the lookup, createdBy on it reads as the new owner (grants.ts:106), and the old grant runs against it.

#1922 closed the write half — every removal now revokes before the row moves. This is the consumer half: the broker refuses a grant that predates the row it resolves to.

The fix

In resolveConnection (toolBrokerService.ts), after the row is resolved and validated:

  • connection_superseded — the grant's createdAt is earlier than the resolved row's. The row a grant belongs to always predates the grant; a row younger than the grant is a different row.
  • connection_untracked — an absent or unreadable createdAt on either side. Not a pass: the population this guard exists for is the anomalous row, so an a && b && c test would wave through exactly the rows it is meant to refuse. The fail-closed branch is also what narrows both values for the comparison.

This lives inside resolveConnection, not in one caller, so both broker entry points inherit it: callTool (:597) and the approved-envelope path (executeApproved, :748), which executes a parked write-with-confirm call.

Evidence

Three witnesses, each red when reverted. Run on Node 22, __tests__/unit/services/toolBrokerService.test.js:

witness asserts
a row older than its grant resolves — control the normal state; a guard that refused it takes the broker offline for every seat
a row created after the grant connection_superseded, no provider call, ToolCall records that code
an absent / non-Date createdAt on either side connection_untracked, no provider call
the parked-write path (executeApprovedToolCall), row re-added after propose connection_superseded, no provider call, trail records it — same owner/repo, so the row's age is the only divergence (Vera 74521)

Placement, and why the fourth witness exists. The arm lives inside resolveConnection so both entry points inherit it, but that claim is only as good as the test that fails without it: moving the arm to the callTool caller — the placement the spec first asked for, which needs the return widened because the row's createdAt is not in it — leaves all three callTool witnesses green and reddens exactly one: the approved-envelope case. Measured, not argued: 1 failed / 29 passed across the two suites under that mutation, 30 passed unmutated.

Mutation ledger (guard suite, 19 passed at baseline; restored green after each):

mutation result
both refusal branches dropped 4 red — the predates witness + all three untracked cases
fail-open form (a && b && c, compare only when both present) 3 red — every untracked case; the predates witness stays green
grant side of the fail-closed check dropped (unreadable ⇒ epoch, never refused) 2 red — the two grant-side cases; the row-side case stays green
comparison inverted 11 red, the control first
connection_superseded renamed to the generic connection_mismatch 1 red — the code is asserted, not just the refusal

Removing the whole fail-closed branch does not compile (TS18047: 'grantCreatedAt' is possibly 'null'): the branch is also the narrowing for the comparison.

Fixture fallout, disclosed: two sibling suites (toolBrokerApprovalService, and this one's shared fixtures) mocked the connection row without createdAt — the field both models always write (timestamps: true). With the guard in place those fixtures are refused, which is what turned 8 tests red on the first run. The fixtures now carry the field, and all nine suites that touch toolBrokerService pass: 9 suites / 77 tests.

No production sweep: the population was measured empty (connector-ops 74472).

Scope

github-app only, which is the only grantable type today. TASK-147 (the same ordering requirement for the other removal paths) and TASK-146 (the tools/list disclosure) are separate rows.

Gate: Vera.

…ves to (TASK-148)

A `RoomGrant` stores the installation id as `connectionId`, so it keeps
resolving after the row it was minted for is deleted and the same GitHub
installation is added again — the re-added row answers the installationId
lookup and the old grant runs against it. #1922 closed the write half
(removal revokes first); this is the consumer half: `resolveConnection`
now compares the grant's `createdAt` against the resolved row's and
refuses when the grant is older, with its own code
(`connection_superseded`).

Both timestamps are required. The population this guard exists for is the
anomalous row, so an `a && b && c` test would pass exactly the rows it is
meant to refuse; an absent or unreadable `createdAt` on either side is a
refusal under `connection_untracked`. The fail-closed branch is also what
narrows the values for the comparison.

Both broker call sites inherit the check because it lives inside
`resolveConnection` rather than in one caller: `callTool` and the
approved-envelope path (`executeApproved`) both resolve there.

Witnesses (each reddens when reverted):
- the parked-write path (`executeApprovedToolCall`) refuses a re-added row
  with `connection_superseded`, no provider call, and the trail records it —
  without this the arm could be moved to the `callTool` caller and every
  other witness would stay green while this path silently lost the guard
  (Vera 74521);

- a row older than its grant resolves (control) — the normal state, and a
  guard that refused it would take the broker offline for every seat;
- a row created after the grant is refused `connection_superseded`, no
  provider call, and the trail records that code;
- an absent or non-Date `createdAt` on either side is refused
  `connection_untracked`.

Two sibling suites mocked both rows without the field the models always
write; the fixtures now carry it, which is why they were red at the first
run of the guard. No production sweep: 74472 measured an empty population.
@samxu01
samxu01 force-pushed the kai/task148-grant-predates-connection branch from 13ef4e8 to 6dbbf85 Compare September 26, 2026 16:16
@lilyshen0722
lilyshen0722 added this pull request to the merge queue Sep 26, 2026
Merged via the queue into main with commit 7ccc6ac Sep 26, 2026
16 of 19 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