Skip to content
14 changes: 14 additions & 0 deletions docs/development/review-checklist.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,17 @@
17. **A mutation test proves a term matters to the suite. It cannot tell you the suite's shape is real.** Deleting a term and watching a test go red is the standard way to show the term is load-bearing, and it is sound *within* the harness — which is exactly the boundary that gets skipped, because the red test feels like production talking back. It isn't: the harness chose the input shape, so a mutation only ever reports which terms that chosen shape reaches. If the shape is one production never produces, every result is valid and every conclusion is about a world that does not exist. **The check that closes it is one line and orthogonal to the mutation: grep what the real middleware assigns, and confirm the harness produces that same shape.** Two tells that it does not, both visible without running anything: the fixture *constructs* the auth object inline rather than calling the middleware, and — the loud one — the suite `jest.mock`s the real auth middleware into a pass-through, so the shape under discussion is not merely unexercised but deliberately excluded. Corollary for the fix: **a term found dead this way is often dead in the safe direction, so confirm what reviving it would do before calling it a bug.** And a rider on the closing check itself, because it has its own failure mode: **bind the projection to its own query, not to line proximity.** `grep '\.select(' ` near an assignment finds the wrong query whenever two run in the same block — a `.select('_id')` belonging to an interleaved `Pod.find` reads exactly like a projection on the `User.findOne` thirty-odd lines above it, and turns a live term into a confidently-reported dead one. Read the call the projection is chained to. *(Earned: 2026-08-22, the `tasksApi` identity terms. `resolveAgentInstanceId` reads `req.user.isBot`, while `agentRuntimeAuth` assigns only `req.agentUser` — so on every real agent call the term is undefined and `claimKey` falls through to the bot User's ObjectId. Dropping `req.user?.username` turned exactly one test red, and that was reported as the term being live; the harness sets `req.user = { id, _id, username, isBot }` from a test header and mocks `agentRuntimeAuth` to a bare `next()`, one line below. The reviewer had quoted that shim in their own review of the same file. *(Rider added 2026-08-23 by @sprint-review, from a near-miss running this very check against the fix that earned the rule: `agentRuntimeAuth.ts:98` is `.select('_id').lean()` on an interleaved DM-pod `Pod.find`, thirty-seven lines below the `User.findOne` — read as the User projection it would have condemned `req.agentUser?.username` as dead on arrival. Both `User.findOne` calls are in fact unprojected, so the term is live.)* Reviving the term would have made things worse, not better: `resolveHolder` only loads the holder's User row when `claimedBy` is a 24-hex ObjectId, so a readable instanceId key would null the holder, empty the `agentName` narrowing on the install lookup, and degrade the lease-rescue liveness check to buy a prettier column.)*

18. **A source assertion is the right instrument when the claim is about absence — because absence of code cannot be demonstrated by execution.** The default suspicion of a test that greps the source instead of running it is correct and should stay: it pins text rather than behaviour, it goes red on a rename that changed nothing, and it is the lazy substitute for the behavioural test somebody did not want to write. But there is one class it is the *only* instrument for. A behavioural test can show that a branch produces the right answer; **it cannot show that a branch is gone.** Dead code is invisible to execution — it never runs, contributes nothing to any assertion, and sits there for the next reader to revive in good faith because it looks like a real path. So the question that separates the two cases is not "is this test grepping?" but **"is the property behavioural or structural?"** If the claim is *this input yields that output*, run it. If the claim is *this call site no longer exists*, *nothing outside this module reads that field*, or *no path constructs the deprecated shape*, then execution is silent on it by construction, and a source assertion is the instrument rather than the shortcut. Two riders. First, **say which it is in the test itself** — an unlabelled grep-test reads as the lazy kind to every future reviewer, and gets deleted by someone applying the default suspicion correctly. Second, **an absence assertion needs a positive control like any other negative**: a grep that matches nothing because the pattern is wrong is indistinguishable from one that matches nothing because the code is gone (see rule 12). There are two ways to match nothing, though, and they are closed by different things — so name which one you closed. **An empty haystack** is closed for free by how the file is loaded: a bare `readFileSync` throws on a bad path, so the source string can never quietly be `''` and every absence assertion in such a suite is already controlled against that half. **A wrong needle** is not, and no property of the loader touches it — a typo'd identifier or an over-anchored regex matches nothing against a file where the code is sitting in plain sight. That half needs the control: assert the same pattern-construction does match something you know is present in that file. The distinction is worth stating because the free half is the one people notice, and citing it reads as having controlled the assertion when the live risk is untouched. *(Rider sharpened 2026-08-25 by @sprint-review, against the exemplar this rule is built on: the `backfillPending` probe carries no positive control and is nonetheless sound, because `read()` is an unguarded `readFileSync`. Naming that mechanism is the point — the test was safe by construction and not by intent, and the behavioural test in the same PR got an explicit `CONTROL:` while the structural one, which is this rider's own subject, did not.)* *(Earned: 2026-08-23, #1149/#1162 — the `backfillPending` absence check was defended as "defensible as a barrier", which is a verdict about this one test rather than a rule anyone could reuse. The generalisation is what makes it decidable: it also explains why converting the ledger-read regex to an executing test was a strict gain in the same review — that property *was* behavioural, and the `catch` branch had no executing coverage at all.)*

## Failure direction

19. **When a guard cannot do its job, it must fail toward the noisy outcome — and "noisy" names an audience, not a log line.** Every guard has a degraded path: the projection came back empty, the lookup threw, the input is a value the guard has no theory of. That path picks a direction, and the direction is a design decision that gets made silently because each site argues it locally and each argument is good. The reviewer's question is one breath long and works on any guard on any surface: **which way does this fail, and who hears it?** Both halves are load-bearing. The first sorts the guard into over-delivering (a noisier pod, a redundant wake, an unnecessary expand) versus under-delivering (a message nobody sees, a thread silently collapsed, an event retired unread) — and under-delivering is the one that is invisible in production by construction, because the evidence of the failure is the thing that failed to appear. The second stops the rule being satisfiable by a `console.warn`: a failure that is loud to the operator and silent to the party that was owed the outcome is still a silent failure, wearing a receipt. This codebase already follows the rule in at least five independent places, none of which cite each other: a missing ledger row expands rather than collapses (#1115); `cutoffUnknown` resolves to `false` so an unresolved cutoff never collapses a thread (`ThreadUserState.ts` — the CASE-arm ORDER *is* the ruling); `resolveBotUserIds` returns an empty map on failure, so the narrowing degrades to unscoped delivery rather than dropping wakes (`agentMentionService.ts` — "losing the narrowing is a noisier pod; losing the wake is a message nobody sees"); `reRootOrphanedChains` is wrapped so repair can never fail the delete (`models/pg/Message.ts`); and `BARE_RUNTIME_ARTIFACTS` is an observed-values set rather than a shape heuristic, so an unsighted artifact posts (`agentMessageService.ts`). Five correct calls made independently is not a pattern the codebase is protected by — it is five coin flips that happened to land the same way, and nothing stops the sixth landing backwards. **The audience half earns its keep on the sixth site.** The `attempts` cap in `agentEventService.ts` reads as the counterexample and is really the sharpest instance: the retire pass exists *precisely* to make exhaustion visible, because leaving a poison event at `delivered` reproduced the Task #67 symptom — stuck, unretried, unsurfaced — for the full retention window. It transitions to `status: 'failed'` with a reason string and warns with a count. And `list()` hardcodes `status: 'pending'`, so the agent that was owed the event is never told it was retired. Direction: correct. Audience: the operator, not the consumer. **Verified rather than inferred** (@sprint-review, 2026-08-25): `status: 'failed'` on `AgentEvent` has **three** readers in the whole tree — `routes/admin/agentEvents.ts` (twice) and `scripts/seat-output.ts`, which aggregates it into a `deadLettered` column. All three are operator-side; none is reachable by the agent that was owed the event. And `garbageCollect` deletes those rows once past `failedRetentionHours`, default **168h**, so the receipt is not merely out of the consumer's reach — it expires in seven days. Stated unqualified on purpose: the first version of this said "the only reader in any route or controller", which was true, and a scoped count is one careless quotation away from a false one. Where a qualifier is load-bearing, the number is usually the wrong thing to lead with. (Worth noting what the third reader believes: its own comment reads "Nothing else shows them" — two operator surfaces, each written as if it were the only one, which is the same independence problem this rule is about.)

**The best citation for this rule is not the guard, it is that third reader's header**, because it argues the rule in the first person and carries the scar (@sprint-review's find):

> Everything here is already persisted on AgentEvent (`delivery.outcome`, `delivery.reason`, `status`, `attempts`). Nothing surfaced it, so answering "is this agent working?" meant reading wrapper logs on the operator's laptop — which on 2026-08-18 produced a wrong answer for 19 hours, because the log line for "posted, then returned the sentinel" is identical to the line for "produced nothing".
>
> The kernel knows. It records `outcome: 'posted'` with the messageId. Ask it.

Read what that is an account of. Two opposite outcomes — working correctly, and producing nothing — rendered identically in the only surface anyone could reach, and the state that distinguished them existed the whole time. Nineteen hours of a confidently wrong answer, from a system that already held the right one. That is this rule's failure mode described by someone who walked into it, not a hypothetical.

And then note where the fix landed. Someone hit the audience gap, felt it, and built a surface — **for the operator**. A third operator-side reader, next to two that already existed. The agent half is untouched: the party whose own liveness is being adjudicated still cannot see any of it, and a dead-lettered event still expires unmentioned at 168h. That is not a criticism of the script, which is good and was the urgent half. It is the point: **feeling the audience gap acutely is not the same as closing it, and the reflex is to build another surface for whoever is holding the question at the time.** The reviewer's second half — *who hears it?* — is the question that would have made the other audience visible while the fix was being written. That residue is defensible, and it is only *visible* if the question asks who hears it. Note what the audience half does NOT license: it is not an argument that every guard must notify its consumer. It is an argument that the answer must be **stated**, because "we logged it" and "the party that needed the outcome was told" are different claims and only one of them is usually true. *(Earned: 2026-08-25, from a survey by @sprint-review of guards verified on main. The five sites each argue the principle from scratch in a comment, which is why nobody noticed they were writing the same argument repeatedly — the arguments are good, and a good local argument is exactly what stops a reader looking for the general form.)*
Loading