diff --git a/pm-stack/projector/src/projector.ts b/pm-stack/projector/src/projector.ts index 9cc29acbe8..7fb512c7a9 100644 --- a/pm-stack/projector/src/projector.ts +++ b/pm-stack/projector/src/projector.ts @@ -5,6 +5,7 @@ import type { upsertItem, upsertComment, markCommentDeleted, + deleteItem, recordPush, ItemRow, CommentRow, @@ -17,6 +18,7 @@ export interface ProjectorStore { upsertItem(i: Omit): Promise; upsertComment(c: Omit): Promise; markCommentDeleted(c: Omit): Promise; + deleteItem(ref: string): Promise; recordPush(p: { deliveryId: string; repo: string; @@ -227,7 +229,20 @@ export async function applyEvent( switch (event) { case "issues": { const issue = payload.issue; - if (issue) await applyIssue(issue); + if (issue && payload.action === "deleted") { + // A deleted issue must leave the read model, not be re-upserted + // by its own deletion event (#4210). + const ref = itemRef(repo, issue.number); + await store.deleteItem(ref); + addRef(ref); + hot(`issue:${issue.id ?? issue.number}`, { + ref, + actor: senderLogin, + sourceUrl: iso(issue.html_url), + sourceTimestamp: iso(issue.updated_at) ?? iso(issue.created_at), + }); + wrote = true; + } else if (issue) await applyIssue(issue); break; } case "pull_request": { diff --git a/pm-stack/projector/src/server.ts b/pm-stack/projector/src/server.ts index 57d855328a..01a62cab0e 100644 --- a/pm-stack/projector/src/server.ts +++ b/pm-stack/projector/src/server.ts @@ -60,6 +60,7 @@ function libsqlStore(db: Client): ProjectorStore { upsertItem: (i) => store.upsertItem(db, i), upsertComment: (c) => store.upsertComment(db, c), markCommentDeleted: (c) => store.markCommentDeleted(db, c), + deleteItem: (ref) => store.deleteItem(db, ref), recordPush: (p) => store.recordPush(db, p), appendHotEvents: (events) => store.appendHotEvents(db, events), }; diff --git a/pm-stack/projector/src/store.ts b/pm-stack/projector/src/store.ts index 0b6cdbd978..91e140a410 100644 --- a/pm-stack/projector/src/store.ts +++ b/pm-stack/projector/src/store.ts @@ -362,6 +362,27 @@ export async function deleteLink(db: Client, subject: string, ref: string, relat }); } +/** + * Remove an item and everything keyed off it, for a GitHub `issues` + * `deleted` event (#4210). Links and comments are projection data with + * no consumer independent of the item -- every read joins through it -- + * so they go with the item row instead of becoming orphans (triage + * decision on #4210), with two refinements from review: + * - A GitHub item's outbound links are keyed by the namespaced subject + * `gh:` (schema.sql), not the bare ref; both spellings are + * removed so no writer's rows survive. + * - Comment tombstones (deleted = 1) are RETAINED: they are the + * absorbing guard that rejects stale comment replays (see + * upsertComment), and erasing one would let a delayed create/edit + * resurrect a deleted comment. Live comments are removed; the + * tombstone rows are already excluded from default reads. + */ +export async function deleteItem(db: Client, ref: string): Promise { + await db.execute({ sql: `DELETE FROM pmp_items WHERE ref = ?`, args: [ref] }); + await db.execute({ sql: `DELETE FROM pmp_comments WHERE ref = ? AND deleted = 0`, args: [ref] }); + await db.execute({ sql: `DELETE FROM pmp_links WHERE subject IN (?, ?)`, args: [`gh:${ref}`, ref] }); +} + export async function getLinks(db: Client, opts: { subject?: string; ref?: string } = {}): Promise { const clauses: string[] = []; const args: string[] = []; diff --git a/pm-stack/projector/test/hot.test.ts b/pm-stack/projector/test/hot.test.ts index 8764a97733..28476cd109 100644 --- a/pm-stack/projector/test/hot.test.ts +++ b/pm-stack/projector/test/hot.test.ts @@ -17,6 +17,7 @@ import { upsertItem as storeUpsertItem, upsertComment as storeUpsertComment, markCommentDeleted as storeMarkCommentDeleted, + deleteItem as storeDeleteItem, recordPush as storeRecordPush, type HotEventInput, } from "../src/store"; @@ -44,6 +45,7 @@ function libsqlStore(dbx: Client): ProjectorStore { upsertItem: (i) => storeUpsertItem(dbx, i), upsertComment: (c) => storeUpsertComment(dbx, c), markCommentDeleted: (c) => storeMarkCommentDeleted(dbx, c), + deleteItem: (ref) => storeDeleteItem(dbx, ref), recordPush: (p) => storeRecordPush(dbx, p), appendHotEvents: (events) => appendHotEvents(dbx, events), }; diff --git a/pm-stack/projector/test/projector.test.ts b/pm-stack/projector/test/projector.test.ts index afab45657e..f0668662f3 100644 --- a/pm-stack/projector/test/projector.test.ts +++ b/pm-stack/projector/test/projector.test.ts @@ -6,6 +6,7 @@ import { upsertItem as storeUpsertItem, upsertComment as storeUpsertComment, markCommentDeleted as storeMarkCommentDeleted, + deleteItem as storeDeleteItem, recordPush as storeRecordPush, getItems, } from "../src/store"; @@ -19,6 +20,7 @@ function fakeStore() { const comments = new Map>(); const pushes: any[] = []; const deleted: string[] = []; + const deletedItems: string[] = []; const store: ProjectorStore = { async upsertItem(i) { const prev = items.get(i.ref); @@ -34,11 +36,18 @@ function fakeStore() { deleted.push(c.id); comments.set(c.id, { ...c, deleted: 1 }); }, + async deleteItem(ref) { + deletedItems.push(ref); + items.delete(ref); + for (const [id, c] of comments) { + if (c.ref === ref && c.deleted === 0) comments.delete(id); + } + }, async recordPush(p) { pushes.push(p); }, }; - return { store, items, comments, pushes, deleted }; + return { store, items, comments, pushes, deleted, deletedItems }; } describe("projector: repo allow-list", () => { @@ -75,6 +84,32 @@ describe("projector: issues", () => { expect(JSON.parse(items.get("developing-today/code#100")!.labels)).toEqual(["bug", "status:backlog"]); }); + test("deleted issue is removed, not re-upserted (#4210)", async () => { + const { store, items, deletedItems } = fakeStore(); + const issue = { + number: 102, + title: "Spam", + state: "open", + html_url: "https://github.com/developing-today/code/issues/102", + url: "https://api.github.com/repos/developing-today/code/issues/102", + user: { login: "dezren39" }, + created_at: "2026-10-25T00:00:00Z", + updated_at: "2026-10-25T00:00:00Z", + }; + const repo = { full_name: "developing-today/code" }; + await applyEvent(store, "issues", { action: "opened", repository: repo, issue }); + expect(items.has("developing-today/code#102")).toBe(true); + const r = await applyEvent(store, "issues", { + action: "deleted", + repository: repo, + issue: { ...issue, updated_at: "2026-10-25T01:00:00Z" }, + }); + expect(r.wrote).toBe(true); + expect(r.refs).toEqual(["developing-today/code#102"]); + expect(deletedItems).toEqual(["developing-today/code#102"]); + expect(items.has("developing-today/code#102")).toBe(false); + }); + test("issue with pull_request field is classified as a PR", async () => { const { store, items } = fakeStore(); const payload = { @@ -391,6 +426,7 @@ describe("projector: issue-shaped payloads never reset merge fields (#4158)", () upsertItem: (i) => storeUpsertItem(db, i), upsertComment: (c) => storeUpsertComment(db, c), markCommentDeleted: (c) => storeMarkCommentDeleted(db, c), + deleteItem: (ref) => storeDeleteItem(db, ref), recordPush: (p) => storeRecordPush(db, p), }; await applyEvent(store, "pull_request", mergedPrEvent); diff --git a/pm-stack/projector/test/store.test.ts b/pm-stack/projector/test/store.test.ts index 7c50c61fc5..eda0a63bef 100644 --- a/pm-stack/projector/test/store.test.ts +++ b/pm-stack/projector/test/store.test.ts @@ -8,6 +8,7 @@ import { upsertItem, upsertComment, markCommentDeleted, + deleteItem, upsertLink, getItems, getComments, @@ -120,6 +121,26 @@ describe("store: items", () => { }); }); +describe("store: deleteItem (#4210)", () => { + test("removes the item row, its live comments, and its subject links; tombstones survive", async () => { + const ref = "developing-today/code#4210"; + await upsertItem(db, item({ ref, number: 4210 })); + await upsertComment(db, comment({ id: "c-4210", ref })); + await markCommentDeleted(db, comment({ id: "c-4210-gone", ref })); + await upsertLink(db, { subject: `gh:${ref}`, ref: "developing-today/code#1", relation: "tracks" }); + await deleteItem(db, ref); + expect(await getItems(db, [ref])).toHaveLength(0); + // The live comment is gone; the tombstone is retained so a stale + // replay of it stays absorbed (Codex review on PR #6775). + const remaining = await getComments(db, [ref], { includeDeleted: true }); + expect(remaining.map((c) => c.id)).toEqual(["c-4210-gone"]); + expect(remaining[0].deleted).toBe(1); + expect(await getLinks(db, { subject: `gh:${ref}` })).toHaveLength(0); + // The linked-to item itself is untouched: only the subject side goes. + expect(await getItems(db, ["developing-today/code#1"])).toHaveLength(1); + }); +}); + describe("store: comments + links + subject view", () => { test("comments upsert and read, newest first", async () => { const base: Omit = {