Skip to content

perf(backend): give the grant-broker seam's active-installation read a covered index (TASK-175) - #1974

Merged
lilyshen0722 merged 3 commits into
mainfrom
kai/task175-seam-index
Sep 27, 2026
Merged

lilyshen0722 merged 3 commits into
mainfrom
kai/task175-seam-index

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

TASK-175 follow-up, cut from merged main (8faa6e32) — the hot-path cost Vera named on #1971, closed without weakening the check. One schema index, one service test, perf(backend) scope: no cli version bump, nothing to deploy beyond the backend image.

The cost

ownerIdsFor (services/seatGrantConfinement.ts, #1971) runs AgentInstallation.find({ status: 'active' }) with no identity filter, on every callTool and every tools/list the broker serves. On the instance that is 502 rows / 288 active, and Vera's index read is right: podId+status, agentName+podId+instanceId and installedBy+status — a status-only filter uses none of them.

Why not the two fixes the gate suggested

An identity $in on candidate agentNames would be fail-open. The seam matches on seatEnvironmentKey, which normalises BOTH parts (trim().toLowerCase()), and it has to: the schema lowercases agentName but not instanceId, which seatEnvironmentProjection.ts states outright ("a raw string compare would miss a seat stored as Quill"). Any DB-side filter on identity is therefore narrower than the key match — it can only ever remove refusals from a check whose whole purpose is to add them. That is the same argument the module's own comment already makes for matching in JS. Measured, too: an { agentName, status } index is not even chosen for the read the seam runs — still COLLSCAN, docsExamined=502.

A short-TTL cache keyed by the seat moves staleness inside a fail-closed check: a miss inside the window admits a seat the projection would have refused, and the window is exactly when installs change. Not taken.

What landed instead

The query, the projection and the JS key match stay byte-identical. Only the access path changes:

AgentInstallationSchema.index({ status: 1, agentName: 1, instanceId: 1, installedBy: 1, _id: 1 });

Measured in a real mongod (7.0.14, the pinned version) on the live shape — 502 rows, 288 active, an opaque config map on each, the projection the seam selects:

state winning plan docsExamined keysExamined
before PROJECTION_SIMPLE > COLLSCAN 502 0
after PROJECTION_COVERED > IXSCAN 0 288

Two details that are load-bearing rather than cosmetic: _id is a key because an inclusion projection still returns it (without it the same index is chosen but gains a FETCH — 288 documents pulled to read fields the index already holds), and installedBy is a key because the seam selects it (drop it and the plan fetches again).

Self-applying: backend/config/db.ts leaves mongoose's default autoIndex on, so a declared index is created at boot — the precedent is TASK-063's migration docblock ("autoIndex CREATES the indexes a schema declares but never DROPS one it does not know about"). No script, no migration. Disclosed: the first calls after a boot that creates it still scan until the build finishes (trivial at this size).

Evidence

  • backend/__tests__/service/agentInstallationActiveScanIndex.test.js — 3 tests against real mongod: the seam's filter+projection is a covered IXSCAN with docsExamined === 0 and exactly the 288 active keys; a positive control that the same instrument sees a COLLSCAN (so "not COLLSCAN" cannot pass on a broken plan walker); and the declaration's key order.
  • Every suite that imports the model: 133 suites / 1309 tests / 0 failures / 0 runtime errors. (Two files matching the import grep did not run — __tests__/utils/leakMatrix*.js, which jest.config.js ignores by directory, as it says it does.)
  • lint:ts on the changed file: zero findings added — the rule+message multiset of its output is identical to origin/main's copy of the file, warnings included.
  • Mutation ledger, 5 mutations, no survivors: index not declared (2 arms); _id dropped (2 arms); key order reordered to lead with agentName (1); installedBy dropped (2); and an instrument check retargeting the control filter. Ledger lesson worth keeping: a filter on a non-leading key is not an unservable filter — { agentName } is served by this index as IXSCAN, so my first control proved nothing until it was retargeted to a field no key covers. A control that cannot fail is not a control.

Coupling and residuals

  • This index exists for the seam's read, which lands in fix(grants): the broker call refuses a seat that cannot confine it (TASK-175) #1971 (CLEAR at 1699799a, head 9886cb9d). The two are independent in both directions — the query does not reference the index, so landing order is free, and if fix(grants): the broker call refuses a seat that cannot confine it (TASK-175) #1971 is declined this drops with it.
  • The covered plan is asserted for the filter and projection the seam sends today. A future caller that adds a selected field loses coverage silently — the plan degrades to a FETCH, not an error. That test is the guard; it reddens when it happens.
  • The read is still O(active installations) in keys examined. The index removes the document fetches and the 214 non-active rows, not the identity-free scan. If the fleet's installation count grows by orders of magnitude, the sound fix is to make identity filterable — canonicalise instanceId at write time so a query filter is a superset of the key match — never to filter on raw identity, for the reason above.

…key set (TASK-175)

Vera (74943): `toContainEqual` on a plain object ignores key order, so a
reorder to lead with `agentName` passed this arm while only the explain arm
caught it. The keys are now asserted as an array against the declared order,
which makes the test's name true and kills the reorder in the leaf arm too.
@lilyshen0722
lilyshen0722 added this pull request to the merge queue Sep 27, 2026
Merged via the queue into main with commit 09bc7f6 Sep 27, 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