mirror-mind 0.39.5 → 0.40.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/hooks/log-assistant-reply.sh +3 -2
- package/.claude/hooks/log-session-end.sh +3 -2
- package/.claude/hooks/log-user-prompt.sh +3 -2
- package/.claude/hooks/mirror-inject.sh +3 -2
- package/.claude/hooks/session-start.sh +3 -2
- package/docs/releases/index.md +2 -0
- package/docs/releases/v0.40.0-next.1.md +103 -0
- package/docs/releases/v0.40.0.md +158 -0
- package/lib/desktop.js +8 -0
- package/package.json +1 -1
- package/plugins/mirror-mind/.claude-plugin/plugin.json +1 -1
- package/plugins/mirror-mind/agy/pre-invocation.sh +3 -2
- package/plugins/mirror-mind/agy/session-start.sh +3 -2
- package/plugins/mirror-mind/agy/stop.sh +3 -2
- package/plugins/mirror-mind/hooks/log-assistant-reply.sh +3 -2
- package/plugins/mirror-mind/hooks/log-session-end.sh +3 -2
- package/plugins/mirror-mind/hooks/log-user-prompt.sh +3 -2
- package/plugins/mirror-mind/hooks/mirror-inject.sh +3 -2
- package/plugins/mirror-mind/hooks/session-start.sh +3 -2
- package/plugins/mirror-mind/plugin.json +1 -1
- package/ts/src/conversation/logger.ts +30 -30
- package/ts/src/conversation/transcriptBackfill.ts +68 -61
- package/ts/src/guards/claudePlugin.ts +3 -2
- package/ts/src/guards/retiredSurfaces.ts +9 -4
- package/ts/src/journey/journeyCalls.ts +9 -2
- package/ts/src/journey/journeyDeletion.ts +602 -0
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
// Delete a Journey with its records — for a host that shows what will go first.
|
|
2
|
+
//
|
|
3
|
+
// `deleteJourney` (CV24.DS8) takes only an empty leaf: `removeJourney` refuses a
|
|
4
|
+
// Journey with any associated record, so a Journey no longer needed has to be
|
|
5
|
+
// emptied by hand. Mirror Desktop's RS028 asked for one act instead, and its
|
|
6
|
+
// Navigator decided what each record does (2026-11-05/06). Those decisions are
|
|
7
|
+
// the four rules below, and the principle under them is transparency rather than
|
|
8
|
+
// refusal: the host shows everything that will change, and the act refuses only
|
|
9
|
+
// what it cannot do safely.
|
|
10
|
+
//
|
|
11
|
+
// 1. A record that is the Journey's own is deleted: its row, its journey path,
|
|
12
|
+
// a `journey:<id>` scene orientation, its identity integrations,
|
|
13
|
+
// conversations, memories, tasks, attachments, runtime sessions, explorer
|
|
14
|
+
// and refinement stories, change requests and refinement cursor.
|
|
15
|
+
// 2. What belongs to a deleted conversation or memory goes with it: messages,
|
|
16
|
+
// summary embeddings, provider calls and memory access log entries.
|
|
17
|
+
// Discarding ONE conversation keeps its provider calls
|
|
18
|
+
// (`conversationDeletion.ts`: "a provider call still happened"); deleting a
|
|
19
|
+
// Journey takes them, because their prompts are the Journey's own words and
|
|
20
|
+
// the person asked for the Journey to be gone.
|
|
21
|
+
// 3. Another Journey's record that only points at something deleted is kept,
|
|
22
|
+
// its link cleared. Memories are filed under the Journey they are about, not
|
|
23
|
+
// the conversation they came from: in one long-lived home, 956 memories
|
|
24
|
+
// came from a conversation of a different Journey. Deleting them with the
|
|
25
|
+
// conversation would delete other Journeys' history.
|
|
26
|
+
// 4. An extension row that names the Journey -- in a column called exactly
|
|
27
|
+
// `journey_id` or `journey` of an `ext_*` table -- is deleted, and the
|
|
28
|
+
// preview names the extension that owns the table. The core does not know
|
|
29
|
+
// what the row means; it does not need to, because the person sees it named
|
|
30
|
+
// before confirming. A table without such a column is never searched.
|
|
31
|
+
//
|
|
32
|
+
// One friction is kept: a Journey with child Journeys is refused, naming them,
|
|
33
|
+
// so the act never cascades silently through the tree.
|
|
34
|
+
//
|
|
35
|
+
// Two calls. `previewJourneyDeletion` reads, and answers every count with a
|
|
36
|
+
// digest. `deleteJourneyWithRecords` takes that digest back and refuses with
|
|
37
|
+
// `journey_changed` when the home no longer matches it, so the act deletes what
|
|
38
|
+
// the person was shown and nothing else. Under one immediate transaction it
|
|
39
|
+
// re-reads the preview, takes a backup with a name never reused
|
|
40
|
+
// (`backups/pre-journey-deletion-<id>-<UTC stamp>-<random>.db`, 0600) and
|
|
41
|
+
// verifies that the backup reads as the same preview, and only then deletes.
|
|
42
|
+
// The front door's fixed-name pre-write snapshot is replaced by the next write,
|
|
43
|
+
// a turn's append included, so it cannot be the undo for a deletion. The core
|
|
44
|
+
// never deletes the named backup.
|
|
45
|
+
|
|
46
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
47
|
+
import { chmodSync, mkdirSync, readFileSync, rmSync, statSync } from "node:fs";
|
|
48
|
+
import { dirname, join } from "node:path";
|
|
49
|
+
import { deleteConversation } from "#conversation/conversationDeletion.ts";
|
|
50
|
+
import { DatabaseNotCurrentError } from "#conversation/hostCalls.ts";
|
|
51
|
+
import {
|
|
52
|
+
type Database,
|
|
53
|
+
openDatabaseReadOnly,
|
|
54
|
+
prepareArchiveSnapshot,
|
|
55
|
+
snapshotDatabaseTo,
|
|
56
|
+
type WritableDatabase,
|
|
57
|
+
withTransaction,
|
|
58
|
+
} from "#db/database.ts";
|
|
59
|
+
import { inspectMigrationState } from "#db/migrationReport.ts";
|
|
60
|
+
import { openLiveWriteDatabase } from "#frontDoor/liveBackup.ts";
|
|
61
|
+
import { JourneyCallRejectedError, type JourneyCallResult } from "#journey/journeyCalls.ts";
|
|
62
|
+
import { buildJourneyTree, type JourneyTree, type JourneyTreeRow } from "#journey/journeyTree.ts";
|
|
63
|
+
|
|
64
|
+
/** Rule 1: the Journey's own records, deleted with it. */
|
|
65
|
+
export interface JourneyOwnRecords {
|
|
66
|
+
journey_paths: number;
|
|
67
|
+
scene_orientations: number;
|
|
68
|
+
identity_integrations: number;
|
|
69
|
+
conversations: number;
|
|
70
|
+
memories: number;
|
|
71
|
+
tasks: number;
|
|
72
|
+
attachments: number;
|
|
73
|
+
runtime_sessions: number;
|
|
74
|
+
explorer_stories: number;
|
|
75
|
+
refinement_stories: number;
|
|
76
|
+
change_requests: number;
|
|
77
|
+
refinement_cursors: number;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Rule 2: what belongs to the deleted conversations and memories, deleted with them. */
|
|
81
|
+
export interface JourneyOwnedContents {
|
|
82
|
+
messages: number;
|
|
83
|
+
conversation_embeddings: number;
|
|
84
|
+
llm_calls: number;
|
|
85
|
+
memory_access_log: number;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Rule 3: other Journeys' rows that point at a deleted row, kept with the link cleared. */
|
|
89
|
+
export interface JourneyDetachedRecords {
|
|
90
|
+
memories: number;
|
|
91
|
+
runtime_sessions: number;
|
|
92
|
+
identity_integrations: number;
|
|
93
|
+
change_requests: number;
|
|
94
|
+
refinement_cursors: number;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Rule 4: one extension table's rows that name the Journey. */
|
|
98
|
+
export interface JourneyExtensionRows {
|
|
99
|
+
/** The installed extension whose `ext_<id>_` prefix the table carries, or null when none does. */
|
|
100
|
+
extension: string | null;
|
|
101
|
+
table: string;
|
|
102
|
+
rows: number;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface JourneyDeletionPreview {
|
|
106
|
+
journeyId: string;
|
|
107
|
+
/** Child Journeys, by key. Any child refuses the deletion. */
|
|
108
|
+
children: string[];
|
|
109
|
+
records: JourneyOwnRecords;
|
|
110
|
+
contents: JourneyOwnedContents;
|
|
111
|
+
detached: JourneyDetachedRecords;
|
|
112
|
+
/** Only tables with at least one row naming the Journey, ordered by table. */
|
|
113
|
+
extensions: JourneyExtensionRows[];
|
|
114
|
+
/** SHA-256 over everything above and the Journey's row. The deletion refuses any other. */
|
|
115
|
+
digest: string;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export interface JourneyDeletionBackup {
|
|
119
|
+
path: string;
|
|
120
|
+
sha256: string;
|
|
121
|
+
sizeBytes: number;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export interface JourneyDeletionReceipt extends JourneyCallResult {
|
|
125
|
+
/** The preview the deletion acted on: the one the person confirmed. */
|
|
126
|
+
deleted: JourneyDeletionPreview;
|
|
127
|
+
backup: JourneyDeletionBackup;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* The closed inventory, read by `journeyDeletion.test.ts` against the schema:
|
|
132
|
+
* a core table that names a Journey, or that references a row the deletion
|
|
133
|
+
* removes, without a decision here fails that test.
|
|
134
|
+
*/
|
|
135
|
+
export const JOURNEY_DELETION_INVENTORY = {
|
|
136
|
+
/** Core tables that name a Journey, and how. */
|
|
137
|
+
journeyColumns: {
|
|
138
|
+
identity: "key, in the journey and journey_path layers; journey:<id> in scene_orientation",
|
|
139
|
+
identity_integrations: "key, in the journey layer",
|
|
140
|
+
conversations: "journey",
|
|
141
|
+
memories: "journey",
|
|
142
|
+
tasks: "journey",
|
|
143
|
+
attachments: "journey_id",
|
|
144
|
+
runtime_sessions: "journey",
|
|
145
|
+
exploratory_stories: "journey",
|
|
146
|
+
builder_refinement_stories: "journey",
|
|
147
|
+
builder_change_requests: "journey",
|
|
148
|
+
builder_refinement_cursors: "journey",
|
|
149
|
+
},
|
|
150
|
+
/**
|
|
151
|
+
* Core tables that name a Journey and are deliberately left alone:
|
|
152
|
+
* `journey_mutation_receipts` is the administration's audit of what was done
|
|
153
|
+
* to the tree, a deletion included.
|
|
154
|
+
*/
|
|
155
|
+
kept: ["journey_mutation_receipts"],
|
|
156
|
+
/** Tables whose rows the deletion removes, so references into them need a decision. */
|
|
157
|
+
deletedTables: [
|
|
158
|
+
"conversations",
|
|
159
|
+
"memories",
|
|
160
|
+
"builder_refinement_stories",
|
|
161
|
+
"builder_change_requests",
|
|
162
|
+
"identity",
|
|
163
|
+
"identity_integrations",
|
|
164
|
+
],
|
|
165
|
+
references: {
|
|
166
|
+
/** Deleted with the row they reference (rule 2). */
|
|
167
|
+
deleted: [
|
|
168
|
+
"messages.conversation_id",
|
|
169
|
+
"conversation_embeddings.conversation_id",
|
|
170
|
+
"llm_calls.conversation_id",
|
|
171
|
+
"memory_access_log.memory_id",
|
|
172
|
+
],
|
|
173
|
+
/** Kept, the reference cleared, when the row is another Journey's (rule 3). */
|
|
174
|
+
unlinked: [
|
|
175
|
+
"memories.conversation_id",
|
|
176
|
+
"runtime_sessions.conversation_id",
|
|
177
|
+
"identity_integrations.conversation_id",
|
|
178
|
+
"identity_integrations.journal_id",
|
|
179
|
+
"builder_change_requests.refinement_story_id",
|
|
180
|
+
"builder_refinement_cursors.active_refinement_story_id",
|
|
181
|
+
"builder_refinement_cursors.active_change_request_id",
|
|
182
|
+
],
|
|
183
|
+
},
|
|
184
|
+
} as const;
|
|
185
|
+
|
|
186
|
+
const EXTENSION_JOURNEY_COLUMNS = ["journey_id", "journey"] as const;
|
|
187
|
+
|
|
188
|
+
function reject(
|
|
189
|
+
reason: ConstructorParameters<typeof JourneyCallRejectedError>[0],
|
|
190
|
+
populated?: string[],
|
|
191
|
+
): never {
|
|
192
|
+
throw new JourneyCallRejectedError(reason, populated);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function text(value: unknown): string {
|
|
196
|
+
if (typeof value !== "string" || value.length === 0) reject("invalid_request");
|
|
197
|
+
return value;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function number(db: Database, sql: string, ...params: string[]): number {
|
|
201
|
+
const value = db.prepare(sql).get(...params)?.n;
|
|
202
|
+
if (typeof value === "number") return value;
|
|
203
|
+
if (typeof value === "bigint") return Number(value);
|
|
204
|
+
throw new Error("journey deletion count did not return a number");
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function ids(db: Database, sql: string, journeyId: string): string[] {
|
|
208
|
+
return db
|
|
209
|
+
.prepare(sql)
|
|
210
|
+
.all(journeyId)
|
|
211
|
+
.map((row) => String(row.id));
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** The keys of the rows rule 2 and rule 3 follow from, as JSON arrays for `json_each`. */
|
|
215
|
+
interface DeletedKeys {
|
|
216
|
+
conversations: string;
|
|
217
|
+
memories: string;
|
|
218
|
+
stories: string;
|
|
219
|
+
changeRequests: string;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function deletedKeys(db: Database, journeyId: string): DeletedKeys {
|
|
223
|
+
return {
|
|
224
|
+
conversations: JSON.stringify(
|
|
225
|
+
ids(db, "SELECT id FROM conversations WHERE journey = ?", journeyId),
|
|
226
|
+
),
|
|
227
|
+
memories: JSON.stringify(ids(db, "SELECT id FROM memories WHERE journey = ?", journeyId)),
|
|
228
|
+
stories: JSON.stringify(
|
|
229
|
+
ids(db, "SELECT id FROM builder_refinement_stories WHERE journey = ?", journeyId),
|
|
230
|
+
),
|
|
231
|
+
changeRequests: JSON.stringify(
|
|
232
|
+
ids(db, "SELECT id FROM builder_change_requests WHERE journey = ?", journeyId),
|
|
233
|
+
),
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const IN = (param: string) => `IN (SELECT value FROM json_each(${param}))`;
|
|
238
|
+
|
|
239
|
+
/** `ext_*` tables with a Journey column, each with its owning extension when one is installed. */
|
|
240
|
+
function extensionTables(
|
|
241
|
+
db: Database,
|
|
242
|
+
): { table: string; columns: string[]; extension: string | null }[] {
|
|
243
|
+
const installed = db
|
|
244
|
+
.prepare("SELECT DISTINCT extension_id AS id FROM _ext_migrations")
|
|
245
|
+
.all()
|
|
246
|
+
.map((row) => String(row.id));
|
|
247
|
+
const owners = installed
|
|
248
|
+
.map((id) => ({ id, prefix: `ext_${id.replaceAll("-", "_")}_` }))
|
|
249
|
+
.sort((a, b) => b.prefix.length - a.prefix.length);
|
|
250
|
+
return db
|
|
251
|
+
.prepare(
|
|
252
|
+
"SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE 'ext\\_%' ESCAPE '\\' ORDER BY name",
|
|
253
|
+
)
|
|
254
|
+
.all()
|
|
255
|
+
.map((row) => String(row.name))
|
|
256
|
+
.map((table) => {
|
|
257
|
+
const present = new Set(
|
|
258
|
+
db
|
|
259
|
+
.prepare(`PRAGMA table_info("${table.replaceAll('"', '""')}")`)
|
|
260
|
+
.all()
|
|
261
|
+
.map((row) => String(row.name)),
|
|
262
|
+
);
|
|
263
|
+
return {
|
|
264
|
+
table,
|
|
265
|
+
columns: EXTENSION_JOURNEY_COLUMNS.filter((column) => present.has(column)),
|
|
266
|
+
extension: owners.find((owner) => table.startsWith(owner.prefix))?.id ?? null,
|
|
267
|
+
};
|
|
268
|
+
})
|
|
269
|
+
.filter((entry) => entry.columns.length > 0);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function extensionWhere(columns: readonly string[]): string {
|
|
273
|
+
return columns.map((column) => `"${column}" = ?`).join(" OR ");
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function journeyRow(db: Database, journeyId: string): { updatedAt: string } | undefined {
|
|
277
|
+
const row = db
|
|
278
|
+
.prepare("SELECT updated_at FROM identity WHERE layer = 'journey' AND key = ?")
|
|
279
|
+
.get(journeyId);
|
|
280
|
+
return row === undefined ? undefined : { updatedAt: String(row.updated_at) };
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** Read everything the deletion would change. Shared by the preview, the act and the backup check. */
|
|
284
|
+
function readPreview(db: Database, journeyId: string): JourneyDeletionPreview {
|
|
285
|
+
const row = journeyRow(db, journeyId);
|
|
286
|
+
if (!row) reject("unknown_journey");
|
|
287
|
+
const keys = deletedKeys(db, journeyId);
|
|
288
|
+
const j = journeyId;
|
|
289
|
+
const children = db
|
|
290
|
+
.prepare(
|
|
291
|
+
`SELECT key FROM identity
|
|
292
|
+
WHERE layer = 'journey'
|
|
293
|
+
AND CASE WHEN json_valid(metadata) THEN json_extract(metadata, '$.parent_journey') END = ?
|
|
294
|
+
ORDER BY key`,
|
|
295
|
+
)
|
|
296
|
+
.all(j)
|
|
297
|
+
.map((child) => String(child.key));
|
|
298
|
+
const records: JourneyOwnRecords = {
|
|
299
|
+
journey_paths: number(
|
|
300
|
+
db,
|
|
301
|
+
"SELECT COUNT(*) AS n FROM identity WHERE layer = 'journey_path' AND key = ?",
|
|
302
|
+
j,
|
|
303
|
+
),
|
|
304
|
+
scene_orientations: number(
|
|
305
|
+
db,
|
|
306
|
+
"SELECT COUNT(*) AS n FROM identity WHERE layer = 'scene_orientation' AND key = 'journey:' || ?",
|
|
307
|
+
j,
|
|
308
|
+
),
|
|
309
|
+
identity_integrations: number(
|
|
310
|
+
db,
|
|
311
|
+
"SELECT COUNT(*) AS n FROM identity_integrations WHERE layer = 'journey' AND key = ?",
|
|
312
|
+
j,
|
|
313
|
+
),
|
|
314
|
+
conversations: number(db, "SELECT COUNT(*) AS n FROM conversations WHERE journey = ?", j),
|
|
315
|
+
memories: number(db, "SELECT COUNT(*) AS n FROM memories WHERE journey = ?", j),
|
|
316
|
+
tasks: number(db, "SELECT COUNT(*) AS n FROM tasks WHERE journey = ?", j),
|
|
317
|
+
attachments: number(db, "SELECT COUNT(*) AS n FROM attachments WHERE journey_id = ?", j),
|
|
318
|
+
runtime_sessions: number(db, "SELECT COUNT(*) AS n FROM runtime_sessions WHERE journey = ?", j),
|
|
319
|
+
explorer_stories: number(
|
|
320
|
+
db,
|
|
321
|
+
"SELECT COUNT(*) AS n FROM exploratory_stories WHERE journey = ?",
|
|
322
|
+
j,
|
|
323
|
+
),
|
|
324
|
+
refinement_stories: number(
|
|
325
|
+
db,
|
|
326
|
+
"SELECT COUNT(*) AS n FROM builder_refinement_stories WHERE journey = ?",
|
|
327
|
+
j,
|
|
328
|
+
),
|
|
329
|
+
change_requests: number(
|
|
330
|
+
db,
|
|
331
|
+
"SELECT COUNT(*) AS n FROM builder_change_requests WHERE journey = ?",
|
|
332
|
+
j,
|
|
333
|
+
),
|
|
334
|
+
refinement_cursors: number(
|
|
335
|
+
db,
|
|
336
|
+
"SELECT COUNT(*) AS n FROM builder_refinement_cursors WHERE journey = ?",
|
|
337
|
+
j,
|
|
338
|
+
),
|
|
339
|
+
};
|
|
340
|
+
const contents: JourneyOwnedContents = {
|
|
341
|
+
messages: number(
|
|
342
|
+
db,
|
|
343
|
+
`SELECT COUNT(*) AS n FROM messages WHERE conversation_id ${IN("?")}`,
|
|
344
|
+
keys.conversations,
|
|
345
|
+
),
|
|
346
|
+
conversation_embeddings: number(
|
|
347
|
+
db,
|
|
348
|
+
`SELECT COUNT(*) AS n FROM conversation_embeddings WHERE conversation_id ${IN("?")}`,
|
|
349
|
+
keys.conversations,
|
|
350
|
+
),
|
|
351
|
+
llm_calls: number(
|
|
352
|
+
db,
|
|
353
|
+
`SELECT COUNT(*) AS n FROM llm_calls WHERE conversation_id ${IN("?")}`,
|
|
354
|
+
keys.conversations,
|
|
355
|
+
),
|
|
356
|
+
memory_access_log: number(
|
|
357
|
+
db,
|
|
358
|
+
`SELECT COUNT(*) AS n FROM memory_access_log WHERE memory_id ${IN("?")}`,
|
|
359
|
+
keys.memories,
|
|
360
|
+
),
|
|
361
|
+
};
|
|
362
|
+
const detached: JourneyDetachedRecords = {
|
|
363
|
+
memories: number(
|
|
364
|
+
db,
|
|
365
|
+
`SELECT COUNT(*) AS n FROM memories WHERE conversation_id ${IN("?")} AND journey IS NOT ?`,
|
|
366
|
+
keys.conversations,
|
|
367
|
+
j,
|
|
368
|
+
),
|
|
369
|
+
runtime_sessions: number(
|
|
370
|
+
db,
|
|
371
|
+
`SELECT COUNT(*) AS n FROM runtime_sessions WHERE conversation_id ${IN("?")} AND journey IS NOT ?`,
|
|
372
|
+
keys.conversations,
|
|
373
|
+
j,
|
|
374
|
+
),
|
|
375
|
+
identity_integrations: number(
|
|
376
|
+
db,
|
|
377
|
+
`SELECT COUNT(*) AS n FROM identity_integrations
|
|
378
|
+
WHERE (conversation_id ${IN("?")} OR journal_id ${IN("?")})
|
|
379
|
+
AND NOT (layer = 'journey' AND key = ?)`,
|
|
380
|
+
keys.conversations,
|
|
381
|
+
keys.memories,
|
|
382
|
+
j,
|
|
383
|
+
),
|
|
384
|
+
change_requests: number(
|
|
385
|
+
db,
|
|
386
|
+
`SELECT COUNT(*) AS n FROM builder_change_requests WHERE refinement_story_id ${IN("?")} AND journey IS NOT ?`,
|
|
387
|
+
keys.stories,
|
|
388
|
+
j,
|
|
389
|
+
),
|
|
390
|
+
refinement_cursors: number(
|
|
391
|
+
db,
|
|
392
|
+
`SELECT COUNT(*) AS n FROM builder_refinement_cursors
|
|
393
|
+
WHERE (active_refinement_story_id ${IN("?")} OR active_change_request_id ${IN("?")})
|
|
394
|
+
AND journey IS NOT ?`,
|
|
395
|
+
keys.stories,
|
|
396
|
+
keys.changeRequests,
|
|
397
|
+
j,
|
|
398
|
+
),
|
|
399
|
+
};
|
|
400
|
+
const extensions = extensionTables(db)
|
|
401
|
+
.map(({ table, columns, extension }) => ({
|
|
402
|
+
extension,
|
|
403
|
+
table,
|
|
404
|
+
rows: number(
|
|
405
|
+
db,
|
|
406
|
+
`SELECT COUNT(*) AS n FROM "${table}" WHERE ${extensionWhere(columns)}`,
|
|
407
|
+
...columns.map(() => j),
|
|
408
|
+
),
|
|
409
|
+
}))
|
|
410
|
+
.filter((entry) => entry.rows > 0);
|
|
411
|
+
const body = {
|
|
412
|
+
journeyId,
|
|
413
|
+
updatedAt: row.updatedAt,
|
|
414
|
+
children,
|
|
415
|
+
records,
|
|
416
|
+
contents,
|
|
417
|
+
detached,
|
|
418
|
+
extensions,
|
|
419
|
+
};
|
|
420
|
+
const digest = createHash("sha256").update(JSON.stringify(body)).digest("hex");
|
|
421
|
+
return { journeyId, children, records, contents, detached, extensions, digest };
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
function readTree(db: Database): JourneyTree {
|
|
425
|
+
return buildJourneyTree(
|
|
426
|
+
db
|
|
427
|
+
.prepare(
|
|
428
|
+
"SELECT key, content, updated_at, metadata FROM identity WHERE layer = 'journey' ORDER BY key",
|
|
429
|
+
)
|
|
430
|
+
.all() as unknown as JourneyTreeRow[],
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
function requireCurrent(dbPath: string): void {
|
|
435
|
+
const report = inspectMigrationState(dbPath);
|
|
436
|
+
if (report.state !== "current") throw new DatabaseNotCurrentError(report);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/** What deleting `journeyId` would change, without changing anything. */
|
|
440
|
+
export function previewJourneyDeletion(
|
|
441
|
+
dbPath: string,
|
|
442
|
+
request: { journeyId: string },
|
|
443
|
+
): JourneyDeletionPreview {
|
|
444
|
+
const journeyId = text(request?.journeyId);
|
|
445
|
+
requireCurrent(dbPath);
|
|
446
|
+
const db = openDatabaseReadOnly(dbPath);
|
|
447
|
+
try {
|
|
448
|
+
return readPreview(db, journeyId);
|
|
449
|
+
} finally {
|
|
450
|
+
db.close();
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
function stamp(date: Date): string {
|
|
455
|
+
return date
|
|
456
|
+
.toISOString()
|
|
457
|
+
.replace(/[-:]/g, "")
|
|
458
|
+
.replace(/\.\d{3}Z$/, "Z");
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
function removeBackup(path: string): void {
|
|
462
|
+
for (const file of [path, `${path}-wal`, `${path}-shm`]) rmSync(file, { force: true });
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* Take the named backup and verify that it reads as `expected`: the home the
|
|
467
|
+
* person was shown. Runs while the caller holds the write lock and has written
|
|
468
|
+
* nothing, so the snapshot is the state the deletion is about to change; only a
|
|
469
|
+
* defective snapshot can read differently. Exported so that check has a test.
|
|
470
|
+
*/
|
|
471
|
+
export function verifiedJourneyDeletionBackup(
|
|
472
|
+
dbPath: string,
|
|
473
|
+
expected: JourneyDeletionPreview,
|
|
474
|
+
): JourneyDeletionBackup {
|
|
475
|
+
const dir = join(dirname(dbPath), "backups");
|
|
476
|
+
const path = join(
|
|
477
|
+
dir,
|
|
478
|
+
`pre-journey-deletion-${expected.journeyId}-${stamp(new Date())}-${randomBytes(6).toString("hex")}.db`,
|
|
479
|
+
);
|
|
480
|
+
const fail = (): never => {
|
|
481
|
+
removeBackup(path);
|
|
482
|
+
return reject("backup_failed");
|
|
483
|
+
};
|
|
484
|
+
try {
|
|
485
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
486
|
+
snapshotDatabaseTo(dbPath, path);
|
|
487
|
+
chmodSync(path, 0o600);
|
|
488
|
+
} catch {
|
|
489
|
+
return fail();
|
|
490
|
+
}
|
|
491
|
+
if (!prepareArchiveSnapshot(path).ok) return fail();
|
|
492
|
+
let image: JourneyDeletionPreview | undefined;
|
|
493
|
+
const db = openDatabaseReadOnly(path);
|
|
494
|
+
try {
|
|
495
|
+
image = readPreview(db, expected.journeyId);
|
|
496
|
+
} catch {
|
|
497
|
+
image = undefined;
|
|
498
|
+
} finally {
|
|
499
|
+
db.close();
|
|
500
|
+
}
|
|
501
|
+
// The read-only open leaves empty sidecars beside a WAL file; this file is
|
|
502
|
+
// private to this call, so they are removed to leave one file.
|
|
503
|
+
rmSync(`${path}-wal`, { force: true });
|
|
504
|
+
rmSync(`${path}-shm`, { force: true });
|
|
505
|
+
if (image?.digest !== expected.digest) return fail();
|
|
506
|
+
return {
|
|
507
|
+
path,
|
|
508
|
+
sha256: createHash("sha256").update(readFileSync(path)).digest("hex"),
|
|
509
|
+
sizeBytes: statSync(path).size,
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/** Rules 1 to 4, in an order every foreign key accepts. The caller holds the transaction. */
|
|
514
|
+
function deleteRecords(db: WritableDatabase, preview: JourneyDeletionPreview): void {
|
|
515
|
+
const j = preview.journeyId;
|
|
516
|
+
const keys = deletedKeys(db, j);
|
|
517
|
+
const run = (sql: string, ...params: string[]) => db.prepare(sql).run(...params);
|
|
518
|
+
|
|
519
|
+
// Rule 2 before rule 1, for what the rows to delete still hold.
|
|
520
|
+
run(`DELETE FROM llm_calls WHERE conversation_id ${IN("?")}`, keys.conversations);
|
|
521
|
+
run(`DELETE FROM memory_access_log WHERE memory_id ${IN("?")}`, keys.memories);
|
|
522
|
+
run("DELETE FROM identity_integrations WHERE layer = 'journey' AND key = ?", j);
|
|
523
|
+
// Rule 3: an integration of another Journey keeps its place in the identity.
|
|
524
|
+
run(
|
|
525
|
+
`UPDATE identity_integrations SET journal_id = NULL WHERE journal_id ${IN("?")}`,
|
|
526
|
+
keys.memories,
|
|
527
|
+
);
|
|
528
|
+
run("DELETE FROM runtime_sessions WHERE journey = ?", j);
|
|
529
|
+
run("DELETE FROM memories WHERE journey = ?", j);
|
|
530
|
+
// Messages and embeddings go; what still points at the conversation is another Journey's, unlinked.
|
|
531
|
+
for (const conversationId of JSON.parse(keys.conversations) as string[]) {
|
|
532
|
+
deleteConversation(db, conversationId);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
run("DELETE FROM builder_refinement_cursors WHERE journey = ?", j);
|
|
536
|
+
run(
|
|
537
|
+
`UPDATE builder_refinement_cursors SET active_refinement_story_id = NULL WHERE active_refinement_story_id ${IN("?")}`,
|
|
538
|
+
keys.stories,
|
|
539
|
+
);
|
|
540
|
+
run(
|
|
541
|
+
`UPDATE builder_refinement_cursors SET active_change_request_id = NULL WHERE active_change_request_id ${IN("?")}`,
|
|
542
|
+
keys.changeRequests,
|
|
543
|
+
);
|
|
544
|
+
run("DELETE FROM builder_change_requests WHERE journey = ?", j);
|
|
545
|
+
run(
|
|
546
|
+
`UPDATE builder_change_requests SET refinement_story_id = NULL WHERE refinement_story_id ${IN("?")}`,
|
|
547
|
+
keys.stories,
|
|
548
|
+
);
|
|
549
|
+
run("DELETE FROM builder_refinement_stories WHERE journey = ?", j);
|
|
550
|
+
run("DELETE FROM exploratory_stories WHERE journey = ?", j);
|
|
551
|
+
run("DELETE FROM tasks WHERE journey = ?", j);
|
|
552
|
+
run("DELETE FROM attachments WHERE journey_id = ?", j);
|
|
553
|
+
|
|
554
|
+
run("DELETE FROM identity WHERE layer = 'journey_path' AND key = ?", j);
|
|
555
|
+
run("DELETE FROM identity WHERE layer = 'scene_orientation' AND key = 'journey:' || ?", j);
|
|
556
|
+
run("DELETE FROM identity WHERE layer = 'journey' AND key = ?", j);
|
|
557
|
+
|
|
558
|
+
// Rule 4. A row another row still holds is the extension's to release, not the core's.
|
|
559
|
+
for (const { table, columns } of extensionTables(db)) {
|
|
560
|
+
try {
|
|
561
|
+
run(`DELETE FROM "${table}" WHERE ${extensionWhere(columns)}`, ...columns.map(() => j));
|
|
562
|
+
} catch (error) {
|
|
563
|
+
if (error instanceof Error && /FOREIGN KEY constraint failed/.test(error.message)) {
|
|
564
|
+
reject("extension_rows_held", [table]);
|
|
565
|
+
}
|
|
566
|
+
throw error;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/** Delete `journeyId` and its records, as `previewJourneyDeletion` showed them, behind a named backup. */
|
|
572
|
+
export function deleteJourneyWithRecords(
|
|
573
|
+
dbPath: string,
|
|
574
|
+
request: { journeyId: string; expectedDigest: string },
|
|
575
|
+
): JourneyDeletionReceipt {
|
|
576
|
+
const journeyId = text(request?.journeyId);
|
|
577
|
+
const expectedDigest = text(request?.expectedDigest);
|
|
578
|
+
requireCurrent(dbPath);
|
|
579
|
+
const db = openLiveWriteDatabase(dbPath);
|
|
580
|
+
let backup: JourneyDeletionBackup | undefined;
|
|
581
|
+
let committed = false;
|
|
582
|
+
try {
|
|
583
|
+
const deleted = withTransaction(db, () => {
|
|
584
|
+
const preview = readPreview(db, journeyId);
|
|
585
|
+
if (preview.digest !== expectedDigest) reject("journey_changed");
|
|
586
|
+
if (preview.children.length > 0) reject("journey_has_children", preview.children);
|
|
587
|
+
backup = verifiedJourneyDeletionBackup(dbPath, preview);
|
|
588
|
+
deleteRecords(db, preview);
|
|
589
|
+
return preview;
|
|
590
|
+
});
|
|
591
|
+
committed = true;
|
|
592
|
+
if (!backup) throw new Error("journey deletion committed without a backup");
|
|
593
|
+
return { journeyId, tree: readTree(db), deleted, backup };
|
|
594
|
+
} catch (error) {
|
|
595
|
+
// Nothing was deleted: a backup of an unchanged home is only a large file. Once the
|
|
596
|
+
// deletion has committed, the backup is the only way back, whatever fails after it.
|
|
597
|
+
if (backup && !committed) removeBackup(backup.path);
|
|
598
|
+
throw error;
|
|
599
|
+
} finally {
|
|
600
|
+
db.close();
|
|
601
|
+
}
|
|
602
|
+
}
|