Repository navigation
fix(retention): deleting a thread root re-roots the chain it orphans - #1161
Merged
Merged
Conversation
TASK-043, @sprint-review 57204. `thread_root_id` is ON DELETE SET NULL, which
is right for the row pointed at and wrong for everything under it. Delete R
from R <- C <- G:
C : reply_to -> NULL, thread_root_id -> NULL correct — C is now a root
G : reply_to = C (ALIVE), thread_root_id -> NULL WRONG — root should be C
G is not a dangling edge, so the reply_to FK cannot help, and no write path
revisits it. It matches the un-rooted predicate permanently. That distinction
is why "zero dangling edges on the live instance" did not close this.
reRootOrphanedChains repairs it by applying the write path's own
COALESCE-of-parent derivation repeatedly, guarded so it only reads from a
parent that already knows its root — otherwise a pass copies one NULL onto
another and reports progress forever. Called from deleteOlderThan rather than
from the retention cron, so every caller gets it, and wrapped so a failed
repair never fails the delete: the rows are already gone, and an un-repaired
orphan costs an over-expand, which the reader treats as unknown and renders
expanded. Noisy, not destructive.
TWO CORRECTIONS TO HOW THIS GETS TESTED, and the second is the substantive one.
`UPDATE ... FROM` with a self-join is the natural single statement and pg-mem
rejects it ("Unknown alias"), which would push the repair's only coverage to
tier 1 — the same trap the backfill's recursive CTE fell into. A JOINed SELECT
plus keyed UPDATEs runs on both.
And PG-MEM DOES NOT FIRE ON DELETE SET NULL AT ALL. Measured minimally: three
rows, one delete, children still pointing at the deleted id. So @sprint-review's
"threadFollowByParticipation.test.js:252 already does DELETE FROM messages
under pg-mem, so FK actions are exercised at the unit tier" (57286) is not
true — a DELETE running is not its FK action firing. My first draft of the
unit suite had a test asserting the orphaning and it PASSED; it was not
evidence and it is gone.
So the split is now honest. The unit suite proves the repair against a state
it constructs by hand, and says out loud that the fixture is a claim about
what Postgres does. threading.retention.test.js proves at tier 1 that Postgres
actually produces that state, and that deleteOlderThan repairs it end to end —
including a control that an intact thread survives a delete that removes
nothing, since a repair that rewrote healthy rows would pass everything else.
My own #1141 guard caught this commit: the new comment quoted
COALESCE-of-parent verbatim, making three occurrences where it pins two,
which silently changes which one a first-match probe hits. Reworded rather
than the guard loosened.
296 threading unit tests green; the 4 tier-1 cases skip without
INTEGRATION_TEST and run in CI.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This was referenced Aug 23, 2026
lilyshen0722
added a commit
that referenced
this pull request
Sep 1, 2026
The "verify by sha, not by PR" rule is one step short of actionable: `gh pr view` returns two real shas for a merged PR, and because this repo squash-merges, `headRefOid` is never an ancestor of `main`. `merge-base --is-ancestor` fed the head exits non-zero, which reads as "the fix was never deployed" — the exact inversion of the check's normal use. Nothing errors, so a re-read does not catch it. Adds a rider under the existing bullet: `mergeCommit.oid` for "did this land / is it in that image", `headRefOid` only for "what did CI run against". Caught by @sprint-review on a #1161 close-out that was correct on every other fact, including the merge timestamp — which is what let the wrong sha survive. Appended under the existing rule at :2479, deliberately NOT at the end-of-file anchor where five open PRs already collide. Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
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-043, @sprint-review 57204.
thread_root_idisON DELETE SET NULL— right for the row pointed at, wrong for everything under it. Delete R fromR ← C ← G:G is not a dangling edge, so the
reply_toFK can't help and no write path revisits it. It matches the un-rooted predicate permanently. That distinction is why "zero dangling edges on the live instance" didn't close this.The repair
reRootOrphanedChainsapplies the write path's own COALESCE-of-parent derivation repeatedly, guarded so it only reads from a parent that already knows its root — otherwise a pass copies one NULL onto another and reports progress forever.Called from
deleteOlderThan, not the retention cron, so every caller gets it. Wrapped so a failed repair never fails the delete: the rows are already gone, and an un-repaired orphan costs an over-expand, which the reader treats as unknown and renders expanded. Noisy, not destructive.Two corrections to how this gets tested — the second is substantive
UPDATE … FROMwith a self-join is the natural single statement and pg-mem rejects it (Unknown alias), which would push the repair's only coverage to tier 1 — the same trap the backfill's recursive CTE fell into. A JOINed SELECT plus keyed UPDATEs runs on both.And pg-mem does not fire
ON DELETE SET NULLat all. Measured minimally — three rows, one delete, children still pointing at the deleted id:So @sprint-review's "threadFollowByParticipation.test.js:252 already does DELETE FROM messages under pg-mem, so FK actions are exercised at the unit tier" (57286) isn't true — a DELETE running is not its FK action firing. My first draft of the unit suite had a test asserting the orphaning and it passed. It wasn't evidence, and it's gone.
The honest split
retentionReRoot)threading.retention)deleteOlderThanrepairs it end to endTier 1 includes a control that an intact thread survives a delete removing nothing — a repair that rewrote healthy rows would pass everything else.
My own #1141 guard caught this commit: the new comment quoted COALESCE-of-parent verbatim, making three occurrences where it pins two, which silently changes which one a first-match probe hits. Reworded rather than loosening the guard.
296 threading unit tests green; the 4 tier-1 cases skip without
INTEGRATION_TESTand run in CI.🤖 Generated with Claude Code