Repository navigation
perf(backend): give the grant-broker seam's active-installation read a covered index (TASK-175) - #1974
Merged
Conversation
…a covered index (TASK-175)
…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.
This was referenced Sep 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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) runsAgentInstallation.find({ status: 'active' })with no identity filter, on everycallTooland everytools/listthe broker serves. On the instance that is 502 rows / 288 active, and Vera's index read is right:podId+status,agentName+podId+instanceIdandinstalledBy+status— astatus-only filter uses none of them.Why not the two fixes the gate suggested
An identity
$inon candidate agentNames would be fail-open. The seam matches onseatEnvironmentKey, which normalises BOTH parts (trim().toLowerCase()), and it has to: the schema lowercasesagentNamebut notinstanceId, whichseatEnvironmentProjection.tsstates outright ("a raw string compare would miss a seat stored asQuill"). 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 — stillCOLLSCAN,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:
Measured in a real mongod (7.0.14, the pinned version) on the live shape — 502 rows, 288 active, an opaque
configmap on each, the projection the seam selects:PROJECTION_SIMPLE > COLLSCANPROJECTION_COVERED > IXSCANTwo details that are load-bearing rather than cosmetic:
_idis a key because an inclusion projection still returns it (without it the same index is chosen but gains aFETCH— 288 documents pulled to read fields the index already holds), andinstalledByis a key because the seam selects it (drop it and the plan fetches again).Self-applying:
backend/config/db.tsleaves mongoose's defaultautoIndexon, 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 coveredIXSCANwithdocsExamined === 0and exactly the 288 active keys; a positive control that the same instrument sees aCOLLSCAN(so "not COLLSCAN" cannot pass on a broken plan walker); and the declaration's key order.__tests__/utils/leakMatrix*.js, whichjest.config.jsignores by directory, as it says it does.)lint:tson the changed file: zero findings added — the rule+message multiset of its output is identical toorigin/main's copy of the file, warnings included._iddropped (2 arms); key order reordered to lead withagentName(1);installedBydropped (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 asIXSCAN, 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
1699799a, head9886cb9d). 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.FETCH, not an error. That test is the guard; it reddens when it happens.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 — canonicaliseinstanceIdat write time so a query filter is a superset of the key match — never to filter on raw identity, for the reason above.