|
1 | 1 | export * as SessionPurge from "./purge" |
2 | 2 |
|
3 | | -import { and, asc, eq, lte, sql } from "drizzle-orm" |
| 3 | +import { and, asc, eq, lte, sql, inArray} from "drizzle-orm" |
4 | 4 | import { DateTime, Effect } from "effect" |
5 | 5 | import type { Database } from "../database/database" |
6 | 6 | import { EventSequenceTable, EventTable } from "../event/sql" |
7 | 7 | import type { FSUtil } from "../fs-util" |
8 | 8 | import { SessionLifecycle } from "./lifecycle" |
| 9 | +import { ProjectV2 } from "../project" |
9 | 10 | import { SessionSchema } from "./schema" |
10 | 11 | import { SessionTable, SessionTombstoneTable } from "./sql" |
11 | 12 |
|
12 | 13 | type DatabaseService = Database.Interface["db"] |
13 | 14 |
|
14 | 15 | export interface Claimed { |
| 16 | + /** The tombstone for the session that was asked for — the root of the purged subtree. */ |
15 | 17 | readonly tombstone: SessionLifecycle.Tombstone |
16 | | - /** Managed tool-output files owned by the purged session. Removed only after the commit. */ |
| 18 | + /** |
| 19 | + * Every tombstone written, root first then descendants. A child session ID is an ID some |
| 20 | + * client may still hold — an open tab, a recently-closed entry — so each purged child needs |
| 21 | + * its OWN tombstone. A parent-only tombstone would leave every child indistinguishable from |
| 22 | + * "not fetched yet", which is the exact confusion tombstones exist to remove, rebuilt one |
| 23 | + * level down. |
| 24 | + */ |
| 25 | + readonly tombstones: ReadonlyArray<SessionLifecycle.Tombstone> |
| 26 | + /** Managed tool-output files owned by the purged subtree. Removed only after the commit. */ |
17 | 27 | readonly objects: ReadonlyArray<string> |
18 | 28 | } |
19 | 29 |
|
@@ -71,47 +81,107 @@ export const claim = Effect.fn("SessionPurge.claim")(function* ( |
71 | 81 | .pipe(Effect.orDie) |
72 | 82 | if (!row) return undefined |
73 | 83 |
|
74 | | - const objects = yield* managedObjects(db, input.sessionID) |
75 | | - |
76 | | - // Durable events are keyed by aggregate, not by a foreign key, so they do not cascade. |
77 | | - // Inlined rather than calling EventV2.remove so the whole purge is one transaction: a |
78 | | - // crash between two transactions would leave events for a session that no longer exists. |
79 | | - yield* db.delete(EventSequenceTable).where(eq(EventSequenceTable.aggregate_id, input.sessionID)).run() |
80 | | - yield* db.delete(EventTable).where(eq(EventTable.aggregate_id, input.sessionID)).run() |
81 | | - // FTS5 virtual tables cannot declare a foreign key, so session_transcript_search cannot |
82 | | - // cascade like the ordinary relational child tables below -- same reasoning as the |
83 | | - // aggregate-keyed event tables just above, deleted explicitly for the same reason. |
84 | | - yield* db.run(sql`DELETE FROM session_transcript_search WHERE session_id = ${input.sessionID}`) |
85 | | - // Every child table declares `onDelete: "cascade"`, and `PRAGMA foreign_keys = ON` is set |
86 | | - // when the database opens. The purge inventory test is what keeps that true for tables |
87 | | - // added later. |
88 | | - yield* db.delete(SessionTable).where(eq(SessionTable.id, input.sessionID)).run() |
89 | | - |
90 | | - yield* db |
91 | | - .insert(SessionTombstoneTable) |
92 | | - .values({ |
93 | | - id: input.sessionID, |
94 | | - project_id: row.projectID, |
95 | | - time_purged: input.now, |
96 | | - last_lifecycle_revision: row.lifecycleRevision, |
97 | | - }) |
98 | | - .onConflictDoNothing() |
99 | | - .run() |
100 | | - |
101 | | - return { |
102 | | - tombstone: SessionLifecycle.Tombstone.make({ |
103 | | - id: input.sessionID, |
104 | | - projectID: row.projectID, |
105 | | - purgedAt: DateTime.makeUnsafe(input.now), |
106 | | - lastLifecycleRevision: row.lifecycleRevision, |
107 | | - }), |
108 | | - objects, |
109 | | - } satisfies Claimed |
| 84 | + // The whole subtree, deepest first. `parent_id` is a plain column with no foreign key, so |
| 85 | + // children do NOT cascade with their parent — purging only the root would leave live |
| 86 | + // orphans pointing at a tombstoned parent, which is worse than either outcome alone. |
| 87 | + const subtree = yield* descendants(db, input.sessionID) |
| 88 | + |
| 89 | + const objects: string[] = [] |
| 90 | + const tombstones: SessionLifecycle.Tombstone[] = [] |
| 91 | + |
| 92 | + for (const target of subtree) { |
| 93 | + objects.push(...(yield* managedObjects(db, target.id))) |
| 94 | + |
| 95 | + // Durable events are keyed by aggregate, not by a foreign key, so they do not cascade. |
| 96 | + // Inlined rather than calling EventV2.remove so the whole purge is one transaction: a |
| 97 | + // crash between two transactions would leave events for a session that no longer exists. |
| 98 | + yield* db.delete(EventSequenceTable).where(eq(EventSequenceTable.aggregate_id, target.id)).run() |
| 99 | + yield* db.delete(EventTable).where(eq(EventTable.aggregate_id, target.id)).run() |
| 100 | + // FTS5 virtual tables cannot declare a foreign key, so session_transcript_search cannot |
| 101 | + // cascade like the ordinary relational child tables below -- same reasoning as the |
| 102 | + // aggregate-keyed event tables just above, deleted explicitly for the same reason. |
| 103 | + yield* db.run(sql`DELETE FROM session_transcript_search WHERE session_id = ${target.id}`) |
| 104 | + // Every child table declares `onDelete: "cascade"`, and `PRAGMA foreign_keys = ON` is set |
| 105 | + // when the database opens. The purge inventory test is what keeps that true for tables |
| 106 | + // added later. |
| 107 | + yield* db.delete(SessionTable).where(eq(SessionTable.id, target.id)).run() |
| 108 | + |
| 109 | + yield* db |
| 110 | + .insert(SessionTombstoneTable) |
| 111 | + .values({ |
| 112 | + id: target.id, |
| 113 | + project_id: target.projectID, |
| 114 | + time_purged: input.now, |
| 115 | + last_lifecycle_revision: target.lifecycleRevision, |
| 116 | + }) |
| 117 | + .onConflictDoNothing() |
| 118 | + .run() |
| 119 | + |
| 120 | + tombstones.push( |
| 121 | + SessionLifecycle.Tombstone.make({ |
| 122 | + id: target.id, |
| 123 | + projectID: target.projectID, |
| 124 | + purgedAt: DateTime.makeUnsafe(input.now), |
| 125 | + lastLifecycleRevision: target.lifecycleRevision, |
| 126 | + }), |
| 127 | + ) |
| 128 | + } |
| 129 | + |
| 130 | + const root = tombstones.find((t) => t.id === input.sessionID) |
| 131 | + if (!root) return undefined |
| 132 | + return { tombstone: root, tombstones, objects } satisfies Claimed |
110 | 133 | }), |
111 | 134 | ) |
112 | 135 | .pipe(Effect.orDie) |
113 | 136 | }) |
114 | 137 |
|
| 138 | +/** |
| 139 | + * The session and everything beneath it, DEEPEST FIRST. |
| 140 | + * |
| 141 | + * Ordering matters for the reader, not for the database: the whole purge is one transaction, so a |
| 142 | + * crash at any point rolls the entire subtree back rather than leaving half a tree deleted. That |
| 143 | + * is the recovery answer — there is no partial state to recover FROM. Deepest-first simply keeps |
| 144 | + * the delete order matching the containment order, so anyone stepping through it sees children go |
| 145 | + * before the parent they belong to. |
| 146 | + */ |
| 147 | +const descendants = Effect.fn("SessionPurge.descendants")(function* ( |
| 148 | + db: DatabaseService, |
| 149 | + rootID: SessionSchema.ID, |
| 150 | +) { |
| 151 | + type Node = { id: SessionSchema.ID; projectID: ProjectV2.ID; lifecycleRevision: number } |
| 152 | + const ordered: Node[] = [] |
| 153 | + let frontier: SessionSchema.ID[] = [rootID] |
| 154 | + // Breadth-first down, then reversed: a session tree is shallow (GitHub-style sub-issue depth, |
| 155 | + // not a filesystem), so this is a handful of queries rather than a recursive CTE. |
| 156 | + while (frontier.length > 0) { |
| 157 | + const rows = yield* db |
| 158 | + .select({ |
| 159 | + id: SessionTable.id, |
| 160 | + projectID: SessionTable.project_id, |
| 161 | + lifecycleRevision: SessionTable.lifecycle_revision, |
| 162 | + }) |
| 163 | + .from(SessionTable) |
| 164 | + .where(inArray(SessionTable.id, frontier)) |
| 165 | + .all() |
| 166 | + .pipe(Effect.orDie) |
| 167 | + for (const r of rows) { |
| 168 | + ordered.push({ |
| 169 | + id: SessionSchema.ID.make(r.id), |
| 170 | + projectID: r.projectID, |
| 171 | + lifecycleRevision: r.lifecycleRevision, |
| 172 | + }) |
| 173 | + } |
| 174 | + const children = yield* db |
| 175 | + .select({ id: SessionTable.id }) |
| 176 | + .from(SessionTable) |
| 177 | + .where(inArray(SessionTable.parent_id, frontier)) |
| 178 | + .all() |
| 179 | + .pipe(Effect.orDie) |
| 180 | + frontier = children.map((c) => SessionSchema.ID.make(c.id)) |
| 181 | + } |
| 182 | + return ordered.reverse() |
| 183 | +}) |
| 184 | + |
115 | 185 | /** |
116 | 186 | * Managed tool-output files this session produced, read from its durable events rather than its |
117 | 187 | * projected messages: a reverted message is removed from the projection while the file it wrote |
|
0 commit comments