Repository navigation
fix(threading): the backfill records the first rooted reply as the cutoff, not the newest un-rooted one (TASK-046) - #1148
Conversation
…toff, not the newest un-rooted one (TASK-046) The 22:28Z dry run on the live pod recorded cutoff 12:21:11Z — MAX(created_at) over un-rooted edges, which is poisoned by retention orphans (a descendant of a deleted root keeps a live parent and loses its root, so a recent orphan sets the boundary late). A hardcoded deploy instant is no better: it is wrong on every self-hosted instance that ships derivation on a different day, as the script's own header already said. The boundary that survives both: MIN(created_at) over messages that have a reply edge AND a root — the first reply derivation-on-write ever wrote, exact to within one reply, per instance, orphan-immune. The newest un-rooted reply remains only as a fallback when no rooted reply exists, and only behind --derivation-live; otherwise --apply refuses with exit 3, because without a rooted reply there is no evidence derivation is deployed and ON CONFLICT DO NOTHING would freeze a wrong boundary. The ledger row now records which source produced the value (cutoffSource). Also removes the header sentence the ruling rejected — a MISSING ledger row is unknown and expands; a NULL cutoff in a written row is knowledge. Tests: the three pinning suites flipped to the new contract (36 passing); tsc:check clean. Ruling text: docs/design/threading-surface-ruling.md (#1147). Co-Authored-By: Claude Fable 5 <[email protected]>
…action sprint-review (TASK-046 note): losing the INSERT after the UPDATE leaves every chain rooted and no cutoff recorded, and 'the backfill always writes the ledger row' is the only remaining guarantee between a partial run and a permanently unknown cutoff. BEGIN before the UPDATE, COMMIT after the INSERT, ROLLBACK on error, on a dedicated client. Pinned by ordering test. Co-Authored-By: Claude Fable 5 <[email protected]>
…ures a recorded boundary sprint-review (57397): after the backfill every pre-threading reply carries a root, so MIN over rooted replies returns the oldest reply ever written — a plausible wrong answer. A run that finds the ledger row reports it and stops before the population count and before CUTOFF_SQL; the recorded value is the only true boundary. Pinned by ordering test. Co-Authored-By: Claude Fable 5 <[email protected]>
…ck cutoff shows its cause sprint-review (57398): on an instance with no reply written since derivation deployed, the primary MIN is NULL and the gated fallback yields the newest un-rooted reply. The dry run now prints how many derivation-written replies exist, so an operator sees why the fallback fired before deciding on --derivation-live. Co-Authored-By: Claude Fable 5 <[email protected]>
lilyshen0722
left a comment
There was a problem hiding this comment.
Gate: approve on content. This closes all three of the hazards I raised against the --apply, and closes them properly rather than narrowly. (CI is BLOCKED as I write; the verdict below is on the code.)
1. Ordering — the read precedes the UPDATE. CUTOFF_SQL at :316, UPDATE messages m at :347. That mattered more with MIN than it did with MAX: after the backfill, pre-threading replies satisfy "both fields non-null," so MIN over that predicate would return the oldest reply ever written. The old query became useless post-UPDATE; this one would have become plausibly wrong, which is the worse failure. Preserved correctly.
2. Primary-vs-fallback is visible and gated. The query returns from_rooted alongside the value, the dry run prints which source produced it — first derivation-written reply versus newest un-rooted reply — FALLBACK, needs --derivation-live — and --apply refuses on the fallback path without the flag. That answers the concern I raised at 57398 better than the row count I asked for: on this instance the primary may well be empty (no reply edge has been written since 12:21:11Z), which would have silently recorded exactly the value the press is being held to avoid. Now it can't happen quietly.
3. The transaction exists, and it is a real one. pool.connect() at :336, BEGIN at :338, the UPDATE and the ledger INSERT both inside, COMMIT at :380, ROLLBACK in catch, client.release() in finally. Using a checked-out client rather than the pool is the part that makes it correct — BEGIN on a pool can land on a different connection from the statements it is meant to wrap, which would have produced a transaction that looked present and wasn't.
The comment above it states the stake accurately: since the un-rooted probe was retired, "the backfill always writes the ledger row" is the only remaining guarantee between a partial run and a permanently unknown cutoff. "Aspiration becomes property here" is the right description of what changed.
One observation, not blocking
The still_null invariant check at :388 runs outside the transaction, on pool rather than client. That is correct — it is a post-hoc audit of committed state, and running it inside would report on an uncommitted snapshot. Worth a half-line saying so, because the next reader sees pool.query immediately after a client block and cannot tell deliberate from overlooked. Everything else in that function now uses the client.
Not verified: I did not run the script or the suite, and CI has not reported. Specifically unverified: whether the primary query returns anything on the live instance — that is what the dry run answers, and it is the one number that decides whether --apply can proceed without --derivation-live.
#1149) (#1224) * test(backfill): the two cutoff guards pin reachability, not text order (#1149) Both tests describe control-flow properties of backfill-thread-root-id.ts and assert them with SCRIPT.indexOf position comparisons. @sprint-review mutated each at c0da084 and both stayed green on a script carrying the regression the test names, because a text-position comparison cannot see a `return`. - "the UPDATE and the ledger INSERT are one transaction": a bare `return` between the UPDATE and the INSERT keeps all four anchors in place and makes the ledger write unreachable. Now asserts the slice between them contains no return/throw/process.exit. - "a run that finds the ledger row reports it and never re-measures": the property is that it STOPS, which rests on one `return`. Deleting that line leaves both indices unchanged and the script re-measures — the exact failure #1148 removed. Now asserts the reported-and-stopped block contains a return. Negative control, each mutant applied in isolation and reverted: A reddens exactly the transaction test, B reddens exactly the ledger test. 34/34 green unmutated. #1170 did not retire these — it was additive (+111 -0) and covers CUTOFF_SQL's semantics, which is orthogonal to statement reachability. Co-Authored-By: Claude Opus 5 <[email protected]> * test(backfill): execute the ledger-first guard instead of scanning for a token @sprint-review re-checked this PR's own guards rather than taking the corroboration, and found both are still source scans: :89-92 and :101-107 are pure indexOf/lastIndexOf over SCRIPT. The token slices this PR added do catch the two named mutants, but they are a proxy for reachability, not reachability — blind to any mutant that removes a path without inserting a keyword, and false-red-prone if anyone extracts a helper between the anchors. Exports main(injectedPool?) so the ledger-first half can be run. The require.main === module gate is untouched, so importing still executes nothing — the property the comment at the bottom of that file protects. An injected pool also owns its own lifecycle: no env check, no Pool construction, no pool.end(). The instrument is a counting pool, because "stopped" is not observable from the database: ON CONFLICT DO NOTHING means a re-measuring run leaves the ledger row byte-identical. Which queries issue is what differs, so that is what the test records. Four cases including a CONTROL with no ledger row, without which "issues one query" is equally consistent with a main() that returns under every condition. Two mutations, both restored: delete the early return -> executing 2 red, text guard 1 red if (ledger && never) -> executing 3 red, text guard ALL GREEN The second is the justification. Every string stays in place, so no scan over the source can see it. Residue recorded in place: the transaction half stays a text guard, because reaching the APPLY path needs the argv flags and a seeded messages population. Named rather than left to be rediscovered. 38 tests green across both suites. Co-Authored-By: Claude Opus 5 <[email protected]> * docs: cite by symbol — three line pointers, and one rotted inside its own PR @sprint-review caught that the early return this PR's guard is written about is at :221, not the :209 the comment claims. The export of main() in this same branch is what moved it: a pointer that rotted inside the branch that wrote it, in the commit whose whole subject is that pointer. Swept the siblings rather than fixing the one (rule 15's third rider) and found two more, both real targets, both adrift: nativeRuntimeService.ts:512 cites ":697" for a quoted comment that lives at :740 — 43 lines off. I nearly filed it as a phantom quote; grepping the quoted TEXT found it immediately, which is the argument for the fix. agentEventService.lifecycle cites ":650" for the requeue (it is :646, .test.js:289 inside the call) and ":698" for the pending delete (it is :715; :698 is a comment line). All three now cite the symbol or the quoted text. A line number in a comment is a claim about a file's LENGTH, which is the one property every commit is entitled to change — and it fails silently, because a wrong pointer lands on plausible code and nobody follows it far enough to notice. 56 tests green across the three affected suites. Co-Authored-By: Claude Opus 5 <[email protected]> * test(backfill): execute the atomicity property at Tier 1 @sprint-review measured the residue I had only predicted: gating the ledger INSERT on a never-true condition leaves BEGIN/UPDATE/INSERT/COMMIT/ROLLBACK all present and in order, both unit suites green at 38/38, and the run leaves every chain rooted with no cutoff recorded — verbatim the harm the transaction guard's own comment names. The guard was blind to the one failure it was written for. Not fixable at Tier 0: pg-mem rejects WITH RECURSIVE, which threading.derivation.test.js already records. So the atomicity property needs a real server too. Two directions, because "one transaction" is two claims. FORWARD: a successful run leaves both the rooted rows and the ledger row. BACKWARD: a run whose INSERT throws leaves NEITHER. Forward alone passes against a script with no transaction at all. BACKWARD asserts process.exitCode, not a rejection — main()'s outer catch logs and sets the exit code, so a failed backfill surfaces as non-zero exit, never a thrown promise. That is the contract an operator observes. One fixture bug found and fixed in the writing, worth the comment it got: patching client.query on a POOLED pg client outlives the test, because the pool hands the same object out again. It broke the next two cases as a missing ledger row — a plausible product bug, not a broken fixture. The patch is now undone on release. Mutation-checked across both tiers: the gated INSERT reddens 3/3 at Tier 1 and 0/38 at Tier 0. Co-Authored-By: Claude Opus 5 <[email protected]> * test(backfill): the residue leads with the measurement, not the class @sprint-review: "blind to a class" reads softer than the truth. The measured statement is that this test is blind to THE defect it was written for — gate the ledger INSERT on a never-true condition and every anchor holds, the file reports 38/38, and the run leaves every chain rooted with no cutoff recorded, word for word the harm named four lines above. The generalisation is the weaker sentence and now goes second. Also records that the anchors are sound rather than leaving `lastIndexOf` looking like a smell: `UPDATE messages m` is unique, and lastIndexOf is the correct pick for the INSERT because the zero-eligible-edges branch carries a second `INSERT INTO migration_records` that indexOf would grab. The limit is the instrument, not the anchors — which is the reason the property moved to a tier that runs the code, not a reason to distrust this guard. Guard stays as-is otherwise, per review. 38/38. Co-Authored-By: Claude Opus 5 <[email protected]> * test(threading): record the mutant that separates BACKWARD from FORWARD Co-Authored-By: Claude Opus 5 <[email protected]> --------- Co-authored-by: Claude Opus 5 <[email protected]>
TASK-046 (pod 57374 → 57395). The 22:28Z dry run on the live pod recorded
cutoff 2026-08-22T12:21:11Z—MAX(created_at)over un-rooted edges. Two things wrong with that source, and one with the obvious replacement:thread_root_id; a recent orphan sets the boundary days late.Boundary that survives both:
MIN(created_at)over messages with a reply edge AND a root — the first reply derivation-on-write ever wrote. Exact to within one reply, per instance, orphan-immune. The un-rootedMAXremains only as a fallback when no rooted reply exists, and only behind--derivation-live; otherwise--applyrefuses (exit 3), because without a rooted reply there is no evidence derivation is deployed andON CONFLICT DO NOTHINGwould make a wrong boundary permanent. The ledger row recordscutoffSource.Also removes the header sentence the ruling rejected ("never as unknown"): a MISSING row is unknown and expands; a NULL cutoff in a written row is knowledge.
Proof:
threadingCutoffRecord,migrationScriptIsInert,unguardedScriptImporters— 36/36 after flipping the pins to the new contract;tsc:checkclean. (eslintparse errors on.tsscripts are pre-existing — CI lints--ext .js.)Operational: dry-run again on the live pod before Sam's
--apply; expect 245 rows / 172 threads and a cutoff just after2026-08-22T16:01:52Z(first derivation-written reply), not 12:21:11Z. Ruling text:docs/design/threading-surface-ruling.md(#1147).🤖 Generated with Claude Code