fix(server): deleted threads release their stored history #32378
Closedrealhasanshoaib(GitHub profile, opens in a new tab)wants to merge 8 commits into
main fromrefs/pull/32378/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -254,0 +315,21 @@315+ FROM inherited316+ JOIN orchestration_v2_projection_threads AS fork ON fork.thread_id = inherited.thread_id317+ WHERE json_extract(fork.payload_json, '$.forkedFrom.type') = 'run'318+ AND json_extract(fork.payload_json, '$.lineage.sideChat') IS NOT 1319+ )320+ SELECT thread_id FROM orchestration_v2_projection_threads321+ WHERE deleted_at IS NOT NULL AND deleted_at <= ${cutoff} AND thread_id > ${cursor}322+ AND thread_id NOT IN (SELECT thread_id FROM inherited WHERE thread_id IS NOT NULL)323+ ORDER BY thread_id ASC324+ LIMIT ${PAGE_SIZE}325+ `.pipe(NodeSqliteClient.readOnly);326+ const threadIds = threads.map((row) => row.thread_id);327+ yield* Effect.yieldNow;328+ if (threadIds.length > 0) {329+ for (const table of THREAD_CONTENT_TABLES) {330+ yield* deleteBatches(331+ (limit) => sql`332+ DELETE FROM ${sql(table)}333+ WHERE rowid IN (334+ SELECT rowid FROM ${sql(table)}335+ WHERE thread_id IN ${sql.in(threadIds)}
- @@ -385,0 +559,21 @@559+it.effect("purges a very large thread in bounded batches", () =>560+ Effect.gen(function* () {561+ const { maintenance, seed, remove } = yield* purgeFixture;562+ const sql = yield* SqlClient.SqlClient;563+ const big = yield* seed("big");564+ yield* sql`565+ INSERT INTO orchestration_v2_turn_item_positions (thread_id, turn_item_id, ordinal)566+ WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < 1200)567+ SELECT ${big.id}, 'turn-item:purge:filler:' || i, i + 100 FROM n568+ `;569+ yield* remove(big, yield* DateTime.now);570+ yield* TestClock.adjust("2 days");571+572+ let batches = 0;573+ const result = yield* maintenance.pruneEventStore(574+ Effect.sync(() => {575+ batches += 1;576+ }),577+ );578+ assert.isAtLeast(result.purgedProjectionRowCount, 1200);579+ assert.isAtLeast(batches, 3);
- @@ -254,0 +302,21 @@302+ if (count < ROW_BATCH_SIZE) return;303+ }304+ });305+ for (let cursor = ""; ;) {306+ const threads = yield* sql<{ readonly thread_id: string }>`307+ WITH RECURSIVE inherited(thread_id) AS (308+ SELECT json_extract(fork.payload_json, '$.forkedFrom.threadId')309+ FROM orchestration_v2_projection_threads AS fork310+ WHERE json_extract(fork.payload_json, '$.forkedFrom.type') = 'run'311+ AND json_extract(fork.payload_json, '$.lineage.sideChat') IS NOT 1312+ AND (fork.deleted_at IS NULL OR fork.deleted_at > ${cutoff})313+ UNION314+ SELECT json_extract(fork.payload_json, '$.forkedFrom.threadId')315+ FROM inherited316+ JOIN orchestration_v2_projection_threads AS fork ON fork.thread_id = inherited.thread_id317+ WHERE json_extract(fork.payload_json, '$.forkedFrom.type') = 'run'318+ AND json_extract(fork.payload_json, '$.lineage.sideChat') IS NOT 1319+ )320+ SELECT thread_id FROM orchestration_v2_projection_threads321+ WHERE deleted_at IS NOT NULL AND deleted_at <= ${cutoff} AND thread_id > ${cursor}322+ AND thread_id NOT IN (SELECT thread_id FROM inherited WHERE thread_id IS NOT NULL)
realhasanshoaib(GitHub profile, opens in a new tab) linked this to fix #17845
juliusmarminge added labelstakeover:in-progress
- @@ -252,3 +279,19 @@252279 return { deletedEventCount, deletedReceiptCount };253280 });254281282+ // A deleted thread keeps only its tombstone row once the event history it could be replayed283+ // from has been pruned. A thread that a surviving run-fork inherits history from, or that a284+ // surviving thread still has a pending merge-back from, is kept whole. Idempotent.285+ const purgeDeletedThreadContent = Effect.fnUntraced(function* (286+ now: DateTime.Utc,287+ betweenPages: Effect.Effect<void>,288+ ) {289+ const cutoff = DateTime.formatIso(DateTime.subtractDuration(now, RETAINED_EVENT_HISTORY));290+ let purged = 0;291+ // Each batch is one statement that commits on its own, so no transaction spans a yield.292+ const deleteBatches = Effect.fnUntraced(function* (293+ batch: (limit: number) => Effect.Effect<unknown, SqlError.SqlError>,294+ ) {295+ for (;;) {296+ yield* batch(ROW_BATCH_SIZE);297+ const changes = yield* sql<{ readonly count: number }>`SELECT changes() AS count`;@@ -254,0 +314,21 @@314+ SELECT transfer.source_thread_id315+ FROM orchestration_v2_projection_context_transfers AS transfer316+ JOIN orchestration_v2_projection_threads AS target317+ ON target.thread_id = transfer.target_thread_id318+ WHERE transfer.type = 'merge_back' AND transfer.status = 'pending'319+ AND (target.deleted_at IS NULL OR target.deleted_at > ${cutoff})320+ UNION321+ SELECT json_extract(fork.payload_json, '$.forkedFrom.threadId')322+ FROM inherited323+ JOIN orchestration_v2_projection_threads AS fork ON fork.thread_id = inherited.thread_id324+ WHERE json_extract(fork.payload_json, '$.forkedFrom.type') = 'run'325+ AND json_extract(fork.payload_json, '$.lineage.sideChat') IS NOT 1326+ )327+ SELECT thread_id FROM orchestration_v2_projection_threads328+ WHERE deleted_at IS NOT NULL AND deleted_at <= ${cutoff} AND thread_id > ${cursor}329+ AND thread_id NOT IN (SELECT thread_id FROM inherited WHERE thread_id IS NOT NULL)330+ ORDER BY thread_id ASC331+ LIMIT ${PAGE_SIZE}332+ `.pipe(NodeSqliteClient.readOnly);333+ const threadIds = threads.map((row) => row.thread_id);334+ yield* Effect.yieldNow;
juliusmarminge added labelstakeover:blocked
juliusmarminge removed labelstakeover:in-progress
juliusmarminge added labelstakeover:in-progress
juliusmarminge removed labelstakeover:blocked
Pusheddf31c1a
juliusmarminge added labelstakeover:blocked
juliusmarminge removed labelstakeover:in-progress
github-actions[bot](GitHub profile, opens in a new tab) added labelsneeds-rebase
shivamhwp(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Codex Review Summary: ✅ Completed
This comment shows the latest Codex review activity on this pull request.
533559fℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.