test(canonical-urls): the suite cannot leave the tree dirty, and says so when it cannot put it back #2485

Merged
jaylouisw merged 1 commit from test/canonical-restores into main 2026-11-23 23:42:37 +00:00
Owner

The defect, measured

test/canonical-urls.test.js plants a dead badge into README.md, runs the checker, then restores the file. The restore was the last statement of a try block, so any interruption that skipped the statement skipped the restore: a killed process, a timed-out terminal, a ctrl-C. The tree was left with M README.md, and the next suite run reported six failures that were one dirty file - three tool calls and one wrong diagnosis before git checkout -- . turned 6 failures into 52 passed, 0 failed.

The runner now refuses to start on a dirty tree (#2468), so the next run cannot be lied to. This is the other half: the suite must not be able to leave the tree dirty in the first place, and must say so when the restore fails.

What changed (test/canonical-urls.test.js only)

Restoration is now a property of touching the file, not a statement after the probe:

  • Captured once, restored from memory. The original bytes are read once at start; every restore writes those bytes. A re-read of a file the probe already overwrote is not the original, and letting "restored" mean "whatever is on disk now" is how a second corruption hides a first.
  • withReadme(mutated, body) is the only way the README is touched. Its finally restores whatever the probe did - including throwing - and the throw still reaches the caller. Both existing probes (the lost clone URL, the dead badge) now go through it.
  • A durable marker for the interruptions no handler can catch. Before the README is touched, the same bytes are written to tmp/canonical-urls-restore/README.md.restore: one file, a JSON header line (v, target, bytes, sha256, pid, at) then the bytes. It lives under tmp/, which .gitignore already covers, so a marker that outlives a run cannot itself dirty the tree, and its extension is not one the checker scans. This is not hypothetical: scripts/run-tests.js SIGKILLs a suite that overruns its 300 s budget, and this suite is a slow one (~94 s, 13 check() calls).
  • Signals and crashes. SIGINT, SIGTERM, SIGHUP, uncaughtException, unhandledRejection and plain exit all salvage through one function before the process goes, then exit. A marker we could not write (read-only checkout) is reported as one line rather than pretending the hard-kill path is covered.
  • Self-heal at startup. A marker found at start is verified against its length and sha256, written back if the README differs, consumed if not, and reported in one line: [heal] README.md was left modified by an interrupted run (pid ..., ...); restored 10545 bytes from tmp/canonical-urls-restore/README.md.restore. A marker that was itself truncated by the kill that wrote it is discarded, not written back - healing from it would be a second dirtying dressed up as a repair.
  • The invariant, asserted. The last section fails loudly if README.md differs from the bytes captured at start, if a mutation is still open, if a marker survives, or if git status --porcelain -- README.md says anything at all.

Proof (assertions, not claims)

  • A check that throws mid-probe still puts the README back (4): the probe really had the file modified when it threw, the error still reached the caller, the captured bytes were restored, the marker was cleared.
  • A run killed mid-probe is healed by the next run (7): a mutation abandoned with its marker on disk - no finally, no exit hook, the SIGKILL case - is healed, in one line, restores the bytes captured before the kill, consumes the marker, and a second run finds nothing to heal.
  • A marker that cannot be trusted is thrown away (4): a half-written marker and a garbage marker both heal nothing and leave the README alone; the bad marker is removed so it cannot fail every run from here on.
  • The interruption hooks restore... (10): the handlers are registered, and the salvage path a signal runs is exercised in the state a signal would find it in and names the signal.
  • This run leaves the tree exactly as it found it (4): byte-for-byte, no open mutation, no marker, and git agrees.

Verified end to end

A real SIGKILL delivered into a live run while the marker was on disk reproduced the original defect exactly - M README.md plus the marker. The next node test/canonical-urls.test.js printed the single [heal] line, healed, and finished clean.

Gates

  • node test/canonical-urls.test.js - 88 passed, 0 failed (was 58)
  • node scripts/syntax-check.js - Syntax-checked 547 files: all OK
  • git status --porcelain - empty after the run
  • node scripts/run-tests.js --filter=canonical-urls - 88 assertions passed, 0 failed, OK - every suite passed.
## The defect, measured `test/canonical-urls.test.js` plants a dead badge into `README.md`, runs the checker, then restores the file. The restore was the **last statement of a `try` block**, so any interruption that skipped the statement skipped the restore: a killed process, a timed-out terminal, a ctrl-C. The tree was left with `M README.md`, and the next suite run reported **six failures that were one dirty file** - three tool calls and one wrong diagnosis before `git checkout -- .` turned 6 failures into `52 passed, 0 failed`. The runner now refuses to start on a dirty tree (#2468), so the *next* run cannot be lied to. This is the other half: the suite must not be able to leave the tree dirty in the first place, and must say so when the restore fails. ## What changed (`test/canonical-urls.test.js` only) Restoration is now a property of touching the file, not a statement after the probe: - **Captured once, restored from memory.** The original bytes are read once at start; every restore writes *those* bytes. A re-read of a file the probe already overwrote is not the original, and letting "restored" mean "whatever is on disk now" is how a second corruption hides a first. - **`withReadme(mutated, body)`** is the only way the README is touched. Its `finally` restores whatever the probe did - including throwing - and the throw still reaches the caller. Both existing probes (the lost clone URL, the dead badge) now go through it. - **A durable marker for the interruptions no handler can catch.** Before the README is touched, the same bytes are written to `tmp/canonical-urls-restore/README.md.restore`: one file, a JSON header line (`v`, `target`, `bytes`, `sha256`, `pid`, `at`) then the bytes. It lives under `tmp/`, which `.gitignore` already covers, so a marker that outlives a run cannot itself dirty the tree, and its extension is not one the checker scans. This is not hypothetical: `scripts/run-tests.js` **SIGKILLs** a suite that overruns its 300 s budget, and this suite is a slow one (~94 s, 13 `check()` calls). - **Signals and crashes.** `SIGINT`, `SIGTERM`, `SIGHUP`, `uncaughtException`, `unhandledRejection` and plain `exit` all salvage through one function before the process goes, then exit. A marker we could not write (read-only checkout) is reported as one line rather than pretending the hard-kill path is covered. - **Self-heal at startup.** A marker found at start is verified against its length and sha256, written back if the README differs, consumed if not, and reported in **one line**: `[heal] README.md was left modified by an interrupted run (pid ..., ...); restored 10545 bytes from tmp/canonical-urls-restore/README.md.restore`. A marker that was itself truncated by the kill that wrote it is **discarded**, not written back - healing from it would be a second dirtying dressed up as a repair. - **The invariant, asserted.** The last section fails loudly if `README.md` differs from the bytes captured at start, if a mutation is still open, if a marker survives, or if `git status --porcelain -- README.md` says anything at all. ## Proof (assertions, not claims) - `A check that throws mid-probe still puts the README back` (4): the probe really had the file modified when it threw, the error still reached the caller, the captured bytes were restored, the marker was cleared. - `A run killed mid-probe is healed by the next run` (7): a mutation abandoned with its marker on disk - no finally, no exit hook, the SIGKILL case - is healed, **in one line**, restores the bytes captured *before* the kill, consumes the marker, and a second run finds nothing to heal. - `A marker that cannot be trusted is thrown away` (4): a half-written marker and a garbage marker both heal nothing and leave the README alone; the bad marker is removed so it cannot fail every run from here on. - `The interruption hooks restore...` (10): the handlers are registered, and the salvage path a signal runs is exercised in the state a signal would find it in and names the signal. - `This run leaves the tree exactly as it found it` (4): byte-for-byte, no open mutation, no marker, and `git` agrees. ## Verified end to end A real `SIGKILL` delivered into a live run *while the marker was on disk* reproduced the original defect exactly - ` M README.md` plus the marker. The next `node test/canonical-urls.test.js` printed the single `[heal]` line, healed, and finished clean. ## Gates - `node test/canonical-urls.test.js` - **88 passed, 0 failed** (was 58) - `node scripts/syntax-check.js` - `Syntax-checked 547 files: all OK` - `git status --porcelain` - empty after the run - `node scripts/run-tests.js --filter=canonical-urls` - `88 assertions passed, 0 failed`, `OK - every suite passed.`
test(canonical-urls): the suite cannot leave the tree dirty, and says so when it cannot put it back
All checks were successful
CI / Typecheck (pull_request) Successful in 18s
PR rebase / Rebase onto main and arm auto-merge (pull_request) Successful in 9s
CI / Server smoke and Docker build (pull_request) Successful in 44s
CI / Test (Node 22) (pull_request) Successful in 50s
CI / Test (Node 24) (pull_request) Successful in 53s
CI / Arm auto-merge (pull_request) Successful in 6s
838977384b
Restoring the README was the last statement of a `try` block, so anything that skipped the
statement skipped the restore. An interrupted run - a killed process, a timed-out terminal, a
ctrl-C - left `M README.md` behind, and the next suite run reported six failures that were one
dirty file. The runner now refuses to start on a dirty tree (#2468); this is the other half.

Restoration is now a property of touching the file rather than a statement after the probe:

  * the original bytes are captured once at start and every restore writes those bytes, so
    "restored" cannot come to mean "whatever was on disk when the probe finished";
  * both README probes go through `withReadme`, whose `finally` restores even when the probe
    throws, and the throw still reaches the caller;
  * before the README is touched the same bytes go to `tmp/canonical-urls-restore/README.md.restore`
    (gitignored, and not an extension the checker scans), with a length and sha256 in its header so
    a marker truncated by the kill that wrote it is refused rather than written back;
  * SIGINT/SIGTERM/SIGHUP, uncaughtException, unhandledRejection and `exit` all salvage through the
    same function before the process goes - and `scripts/run-tests.js` SIGKILLs an overrunning suite,
    which is the case only the marker can survive;
  * a marker found at startup is healed, one line is printed saying so, and the marker is consumed;
  * the last section asserts README.md is byte for byte the file the run started with, that no
    mutation is open, that no marker survives, and that `git status --porcelain -- README.md` is
    empty.

Proved, not assumed: a probe that throws still restores (and its error still surfaces); a mutation
abandoned with its marker on disk - the SIGKILL case - is healed by the next `selfHeal()` in one
line; a half-written and a garbage marker are both discarded without touching the README; and the
salvage path a signal runs is exercised in the state a signal would find it in.

58 assertions before, 88 after; `node test/canonical-urls.test.js` green, tree clean after.
forgejo-actions scheduled this pull request to auto merge when all checks succeed 2026-11-23 23:22:32 +00:00
jaylouisw force-pushed test/canonical-restores from 838977384b
All checks were successful
CI / Typecheck (pull_request) Successful in 18s
PR rebase / Rebase onto main and arm auto-merge (pull_request) Successful in 9s
CI / Server smoke and Docker build (pull_request) Successful in 44s
CI / Test (Node 22) (pull_request) Successful in 50s
CI / Test (Node 24) (pull_request) Successful in 53s
CI / Arm auto-merge (pull_request) Successful in 6s
to 4ee570a83b
All checks were successful
CI / Server smoke and Docker build (pull_request) Successful in 41s
CI / Typecheck (pull_request) Successful in 15s
PR rebase / Rebase onto main and arm auto-merge (pull_request) Successful in 4s
CI / Test (Node 22) (pull_request) Successful in 47s
CI / Test (Node 24) (pull_request) Successful in 50s
CI / Arm auto-merge (pull_request) Successful in 3s
2026-11-23 23:37:36 +00:00
Compare
jaylouisw deleted branch test/canonical-restores 2026-11-23 23:42:49 +00:00
Sign in to join this conversation.
No description provided.