Skip to content

fix(projector): item deletion marker absorbs stale replays (#6782) - #6792

Merged
dezren39 merged 3 commits into
mainfrom
queue/fallback-6782-item-deletion-marker
Nov 7, 2026
Merged

dezren39 merged 3 commits into
mainfrom
queue/fallback-6782-item-deletion-marker

Conversation

@dezren39

@dezren39 dezren39 commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Fixes #6782 (follow-up of #4210; Codex finding 4500146162 on PR #6775).

Problem

PR #6775 made an issues deleted event remove the item row via deleteItem, but left no item-level deletion marker. A previously unprocessed issues/issue_comment delivery arriving after the deletion reinserted the item through upsertItem (and applyComment's item upsert); an in-flight reconcileStream response could do the same. The comment tombstones retained by #6775 do not protect the containing item.

Change

  • schema.sql: new pmp_item_deletions table — ref PK, source_updated_at (the deleted item's updated_at at the delete event), fetched_at.
  • store.ts:
    • deleteItem(db, ref, sourceUpdatedAt?) persists/refreshes the marker after removing the row (monotonic: a later delete refreshes it, an older one never moves it back; write time is the fallback timestamp for direct callers).
    • upsertItem is absorbing: a write whose source_updated_at is at or before the marker is refused. A strictly newer write clears the marker and proceeds — the decided rule for genuinely newer events (GitHub is the source of truth; issue numbers are never reused, so a newer source timestamp means the item demonstrably exists again).
    • getItemDeletion(db, ref) read helper.
  • projector.ts: the issues/deleted handler passes the payload timestamp to deleteItem. applyIssue, applyComment, pull_request, pull_request_review, and pull_request_review_comment are absorbed whole when a marker covers the incoming item's source timestamp (no item resurrection, no orphan comment/review rows), via the new optional ProjectorStore.itemDeletion read — optional so lightweight test stores keep working; the real store's upsertItem guard is the backstop for every other caller.
  • server.ts: libsqlStore binds itemDeletion and forwards the delete timestamp.
  • reconcile: reconcileStream writes items only through store.upsertItem, so the store guard covers in-flight reconcile responses with no reconcile-specific code.

Tests

  • Store level: marker persists even when the item was never upserted; stale creates at/before the marker are absorbed; a genuinely newer upsert recreates the item and clears the marker; marker refresh is monotonic.
  • Projector level (marker-aware fake store): opened → deleted → stale opened replay leaves the item absent; stale issue_comment replay writes neither the item nor the comment; a newer event recreates the item and clears the marker.
  • Full projector suite: 104 pass / 0 fail (bun test in pm-stack/projector). tsc --noEmit shows no new errors (identical pre-existing count before/after).

Filed from a fallback-mode queue shift (lane code-u; queue backend down, code#6619).


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

Follow-up of #4210 (Codex finding 4500146162 on PR #6775): deleteItem removed the item row but left no marker, so a delayed issues/issue_comment delivery or an in-flight reconcile response could reinsert the deleted issue via upsertItem.

schema: new pmp_item_deletions table (ref PK, source_updated_at, fetched_at). store: deleteItem persists/refreshes the marker monotonically (payload source timestamp, write time as fallback); upsertItem refuses writes at or before the marker and clears it for a strictly newer source timestamp (GitHub stays source of truth; numbers are never reused). projector: issues/deleted passes the payload timestamp; applyIssue/applyComment/pull_request/review/review_comment sub-events are absorbed whole when a marker covers them, via the optional ProjectorStore.itemDeletion read (real store binds getItemDeletion). reconcileStream writes go through store.upsertItem, so the store guard covers it.

Tests: store level (marker persists without a prior upsert, absorption at/before marker, newer upsert recreates + clears, marker monotonicity) and projector level with a marker-aware fake store (stale opened replay, stale comment replay writes neither item nor comment, newer event recreates). Full projector suite: 104 pass / 0 fail.
@dezren39
dezren39 requested a review from a team as a code owner November 7, 2026 05:05
@coderabbitai

coderabbitai Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 15 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: cc0ad510-0ce0-4f60-b6fd-3ecc9c6ffdca
Commits

Reviewing files that changed from the base of the PR and between 9640974 and 7eacaf8.

Files selected for processing: 6

  • pm-stack/projector/schema.sql
  • pm-stack/projector/src/projector.ts
  • pm-stack/projector/src/server.ts
  • pm-stack/projector/src/store.ts
  • pm-stack/projector/test/projector.test.ts
  • pm-stack/projector/test/store.test.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-07T05:31:54.570568Z 7eacaf8 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8d5aaa476f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread pm-stack/projector/src/projector.ts Outdated
if (!issue) return;
await store.upsertItem(fromIssueOrPr(repo, issue, issueIsPr(issue) ? "pr" : "issue"));
const itemRow = fromIssueOrPr(repo, issue, issueIsPr(issue) ? "pr" : "issue");
if (await deletionBlocks(itemRow.ref, itemRow.source_updated_at)) return;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve comment tombstones when the parent deletion blocks writes

If a delayed issue_comment deletion arrives after its parent item was deleted, this early return skips markCommentDeleted, losing the permanent comment tombstone. An in-flight issue_comments reconcile response can subsequently recreate that deleted comment through reconcile.ts's direct upsertComment call, which does not consult item deletion markers, and /comments?refs=... exposes it even though the parent remains absent. Previously, processing the comment deletion persisted a tombstone that blocked that replay. Continue recording comment-deletion tombstones while suppressing parent-item and live-comment writes; the review-comment deletion branch has the same issue.

Useful? React with 👍 / 👎.

…writes (#6782)

Codex P2 on PR #6792: the deletionBlocks early return in applyComment (and the same shape in the pull_request_review_comment branch) skipped markCommentDeleted for delayed comment deletions, losing the permanent comment tombstone. Reconcile writes comments through a direct upsertComment that consults comment tombstones, not item markers, so the deleted comment could be resurrected while its parent stayed absent. Both branches now still record the tombstone while suppressing the parent-item and live-comment writes. Two projector tests cover the issue_comment and review_comment deletion paths. Suite: 106 pass / 0 fail.
@developing-today

Copy link
Copy Markdown
Contributor

Codex finding disposition (lane code-w, shepherding #6782) — replying here because GitHub rejected both API reply forms on the bot's review thread (422 abuse validation on base).

The P2 finding ("Preserve comment tombstones when the parent deletion blocks writes", projector.ts:230) is valid and fixed in a7ee1b9 on this branch:

  • applyComment: when the item deletion marker blocks the event, an issue_comment deletion now still calls markCommentDeleted (tombstone recorded, wrote=true) while the parent-item upsert and live-comment writes stay suppressed.
  • pull_request_review_comment: restructured the same way — the row is built before the block check, and a blocked deleted action records the tombstone instead of returning early.
  • Tests: two new projector tests ("a comment deletion after the parent deletion still records its tombstone", "a review-comment deletion after the parent deletion still records its tombstone"). Full projector suite: 106 pass / 0 fail (104 before + 2 new), run with bun test in pm-stack/projector on the laptop.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a7ee1b99f8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +145 to +149
describe("store: item deletion marker (#6782)", () => {
const T0 = "2026-10-25T00:00:00Z";
const T1 = "2026-10-25T01:00:00Z";
const T2 = "2026-10-25T02:00:00Z";
const T3 = "2026-10-25T03:00:00Z";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add generated coverage for deletion-marker transitions

The new deletion-marker state machine is covered only by fixed example sequences using four timestamps; neither this suite nor the projector cases generate operation sequences to check stale absorption, marker monotonicity, and newer-event recovery across replay orderings. Add property-based or model-generated coverage for these invariants. The root AGENTS.md explicitly requires property tests for meaningful state-machine invariants, so example-only coverage does not satisfy the repository's testing doctrine.

AGENTS.md reference: AGENTS.md:L41-L41

Useful? React with 👍 / 👎.

Comment thread pm-stack/projector/src/store.ts Outdated
Comment on lines +131 to +134
const marker = await getItemDeletion(db, i.ref);
if (marker) {
if (marker.source_updated_at >= i.source_updated_at) return;
await db.execute({ sql: `DELETE FROM pmp_item_deletions WHERE ref = ?`, args: [i.ref] });

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Make the marker check and item write atomic

When reconciliation and a deletion webhook overlap, an upsert at source time T2 can read an existing marker T1, then deleteItem can refresh it to T3 before this unconditional DELETE executes. The upsert then removes the newer T3 marker and recreates the stale item, permanently losing its deletion protection. The production handlers in server.ts allow reconciliation and webhook processing concurrently without serialization. Make the marker comparison/removal and item writes atomic relative to deleteItem, rather than removing whichever marker exists based on an earlier read.

Useful? React with 👍 / 👎.

…odel coverage (#6782)

Codex round 2 on PR #6792. P2 (store.ts): upsertItem read the marker, then unconditionally deleted it - a deleteItem landing in between could refresh the marker, and the upsert removed the newer marker and recreated the stale item. upsertItem and deleteItem now each run as one write batch (single transaction): the marker clear only removes markers strictly older than the incoming event, and both item writes are guarded by NOT EXISTS over whatever marker survives the clear, so a mid-flight refresh blocks the writes instead of being discarded. P1 (testing doctrine): the marker state machine is now also covered by a model-based generated test - a fixed-seed PRNG drives 40 interleaved upsert/delete sequences across three refs and four timestamps, checking the real store against a two-field contract model (monotonic marker; newest landed upsert) after every operation. Suite: 107 pass / 0 fail, 2727 expect() calls.
@developing-today

Copy link
Copy Markdown
Contributor

Codex round-2 disposition (lane code-w, shepherding #6782) — posted via the developing.today App because the user token is secondary-rate-limited tonight; thread replies to bot comments were also rejected by GitHub (422), so dispositions live here, per finding:

  1. P2 (round 1) tombstones — fixed in a7ee1b9 (previous disposition comment 6702739217).
  2. P2 store.ts:134 — marker check/write atomicity — valid, fixed in 7eacaf8. upsertItem and deleteItem now each run as a single write batch (one transaction). In upsertItem the marker clear is conditional (source_updated_at < the incoming event — it can only remove a marker this event legitimately beats), and both item writes are guarded by NOT EXISTS over whatever marker survives the clear, so a marker refreshed mid-flight blocks the writes rather than being removed by a stale read. deleteItem's marker write + row removals land in the same transaction, so a racing upsert decides against the pre- or post-delete state, never a torn mix.
  3. P1 store.test.ts:149 — generated coverage for marker transitions — valid, fixed in 7eacaf8. Added a model-based generated test: a fixed-seed mulberry32 PRNG drives 40 interleaved upsert/delete sequences (3 refs × 4 timestamps, 4–12 ops each) and, after every operation, compares the real store against a two-field contract model per ref (monotonic marker = max delete ts; item = newest landed upsert ts; absorbed upserts change nothing). The model is the named oracle; the absorption invariant (item never coexists with a marker at/after it) is checked implicitly on every step.

Full projector suite after both fixes: 107 pass / 0 fail, 2727 expect() calls (bun test in pm-stack/projector, laptop).

@dezren39
dezren39 merged commit 7c06447 into main Nov 7, 2026
4 checks passed
@dezren39
dezren39 deleted the queue/fallback-6782-item-deletion-marker branch November 7, 2026 05:33
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.

projector(items): persist an item deletion marker so stale replays cannot resurrect a deleted issue (follow-up of #4210)

1 participant