Skip to content

fix(retention): deleting a thread root re-roots the chain it orphans - #1161

Merged
lilyshen0722 merged 1 commit into
mainfrom
fix/retention-rerooots-orphaned-chains
Aug 23, 2026
Merged

lilyshen0722 merged 1 commit into
mainfrom
fix/retention-rerooots-orphaned-chains

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

TASK-043, @sprint-review 57204.

thread_root_id is ON DELETE SET NULL — right for the row pointed at, 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 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

reRootOrphanedChains applies 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 … 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:

CREATE TABLE m (id INTEGER PRIMARY KEY,
                reply_to INTEGER REFERENCES m(id) ON DELETE SET NULL,
                root     INTEGER REFERENCES m(id) ON DELETE SET NULL);
INSERT (1,NULL,NULL),(2,1,1),(3,2,1);  DELETE FROM m WHERE id = 1;
-- rows 2 and 3 still hold 1

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

Tier Proves
unit (retentionReRoot) the repair, against a hand-constructed state, labelled as constructed
tier 1 (threading.retention) that Postgres produces that state, and that deleteOlderThan repairs it end to end

Tier 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_TEST and run in CI.

🤖 Generated with Claude Code

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>
@lilyshen0722
lilyshen0722 merged commit 39032b7 into main Aug 23, 2026
11 checks passed
@lilyshen0722
lilyshen0722 deleted the fix/retention-rerooots-orphaned-chains branch August 23, 2026 05:18
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant