Skip to content

Backfill cutoff tests pin text order, not control flow — both survive their own named regression #1149

Description

@lilyshen0722

Two tests in backend/__tests__/unit/models/threadingCutoffRecord.test.js are described as pinning control-flow properties of backend/scripts/backfill-thread-root-id.ts. Both are SCRIPT.indexOf position comparisons over the script's source text, and a text-position comparison cannot see a return. I mutated each at c0da084f and both stayed green on a script that has the regression the test names.

Nothing here is a defect in the script. I read both blocks at the merged head and the behaviour is correct. This is about what the guards would catch if someone edits the script later.

1. the UPDATE and the ledger INSERT are one transaction (:97)

Comment: "losing the INSERT after the UPDATE leaves every chain rooted and no cutoff recorded."

Mutation — insert between the UPDATE and the INSERT, inside the try:

if (process.env.SKIP_LEDGER) return;

The ledger write becomes unreachable. All four anchors (BEGIN, UPDATE messages m, INSERT INTO migration_records, COMMIT) keep their relative positions, so begin < update < insert < commit still holds. 26/26 script tests passed, including this one.

(A first attempt that also added a COMMIT did trip the test — but via indexOf("await client.query('COMMIT')") matching the added line, not via the unreachable INSERT. That mutant was invalid; the bare return above is the isolated one.)

2. a run that finds the ledger row reports it and never re-measures (:85)

Asserts ledgerRead < populationRead. The behaviour it describes rests entirely on the return at :209 — reading the ledger first is not the property, stopping is.

Mutation — delete that one line:

       console.log(`${MIGRATION_NAME} already recorded at ${ledger.applied_at}: `
         + `cutoff = ${ledger.cutoff ?? '(none)'}. Nothing to do — the boundary is not re-measured.`);
-      return;

The script now reads the ledger, reports it, and re-measures anyway — printing the oldest reply ever written as a "would record" value, which is the exact failure mode #1148 was written to remove. The test passed. Both indices are unchanged.

Suggested fix

Assert the absence of a control-flow break between the anchors rather than only their order — e.g. that SCRIPT.slice(update, insert) contains no return, and that the ledger block's reported-and-stopped path contains one. Cheap, and it makes each test fail on the mutation its own comment describes.

Method note

Controls ran first in both arms. The only failures in either arm were 3 config/schema.sql tests failing identically on my stale local branch (the migration_records DDL landed with #1106); all 26 script-reading tests are unaffected by that.

Candidate for a docs/development/review-checklist.md rule: a test that compares source positions pins text order, never control flow — if the property is "it stops", the guard must look for the thing that stops it.

Found while gating #1148 (@ux-lead 57402/57404/57407); posted here rather than on the PR because #1148 merged at c0da084f before I finished.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions