@cortexkit/pi-magic-context 0.49.25 → 0.49.27

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.
Files changed (39) hide show
  1. package/dist/auto-search-worker.js +4 -4
  2. package/dist/chunk-embedding-write-worker.js +8 -9
  3. package/dist/{compartment-chunk-embedding-g9a977b8.js → compartment-chunk-embedding-pkk5beqp.js} +3 -3
  4. package/dist/{embedding-backfill-qxzjy5v2.js → embedding-backfill-25fp3b8w.js} +7 -7
  5. package/dist/{index-0dr64gsy.js → index-0s14xaca.js} +2 -2
  6. package/dist/{index-n8e5e9fp.js → index-0x1c054v.js} +1 -1
  7. package/dist/{index-hxp6q5f8.js → index-48twbev2.js} +50 -36
  8. package/dist/{index-j3pz3a1b.js → index-4zemngt7.js} +1 -1
  9. package/dist/{index-d53veb5e.js → index-5d506f7y.js} +719 -23
  10. package/dist/{index-8hmqdav2.js → index-6981g1xv.js} +39 -8
  11. package/dist/{index-1g0r4req.js → index-bkcwcghx.js} +1308 -104
  12. package/dist/{index-8a2wvrda.js → index-bvpar0bg.js} +2 -5
  13. package/dist/{index-w1avzkdt.js → index-g026ztby.js} +1 -1
  14. package/dist/{index-cm9e52mb.js → index-kagezf2h.js} +18 -4
  15. package/dist/{index-wb2t932e.js → index-kmk3xpf9.js} +1 -1
  16. package/dist/{index-zn2xdbjy.js → index-sdsnb5b6.js} +8 -8
  17. package/dist/index-v0ht940m.js +488 -0
  18. package/dist/{index-cms6wfmd.js → index-vea2d9ka.js} +1 -1
  19. package/dist/{index-nxq2srg9.js → index-wjbqw6r1.js} +1 -1
  20. package/dist/{index-00c5a49f.js → index-xa90f88m.js} +3 -3
  21. package/dist/{index-fzfpq5bd.js → index-xw070axm.js} +3 -3
  22. package/dist/index.js +726 -688
  23. package/dist/{message-fts-rowid-map-1tb3qjtc.js → message-fts-rowid-map-3mbvdgn9.js} +2 -4
  24. package/dist/{message-index-bounded-yj7s7cwz.js → message-index-bounded-avbaa7wa.js} +19 -22
  25. package/dist/migration-worker.js +7 -8
  26. package/dist/{project-embedding-registry-9e82qsb6.js → project-embedding-registry-hc4ahjdt.js} +6 -6
  27. package/dist/raw-ordinal-warmup-worker.js +7 -30
  28. package/dist/{read-session-chunk-rndwjfrx.js → read-session-chunk-1rfprgc1.js} +6 -4
  29. package/dist/{read-session-raw-napqv76k.js → read-session-raw-g9b1zdn5.js} +3 -1
  30. package/dist/{search-archive-qa1sf8hc.js → search-archive-mpjeecwj.js} +5 -5
  31. package/dist/{search-2aa5kzyp.js → search-vkkfg27q.js} +13 -14
  32. package/dist/{storage-tags-r48vxthe.js → storage-tags-9qaasrky.js} +2 -2
  33. package/dist/subagent-entry.js +17 -18
  34. package/dist/tag-snapshot-worker-pi.js +9 -10
  35. package/package.json +1 -1
  36. package/dist/index-n4wyxcgr.js +0 -712
  37. package/dist/index-wrr9rp9a.js +0 -16
  38. package/dist/index-zmg1td73.js +0 -2100
  39. package/dist/message-index-fedjhja5.js +0 -58
@@ -1,2100 +0,0 @@
1
- import {
2
- getHarness2
3
- } from "./index-0h2mkab2.js";
4
- import {
5
- getMagicContextStorageDir
6
- } from "./index-fgs78jyd.js";
7
- import {
8
- isPidAlive,
9
- readProcessStartTime
10
- } from "./index-n4wyxcgr.js";
11
- import {
12
- BoundedSessionMap,
13
- resolveOpenCodeDbPath,
14
- assertOpenCodeStoreGeneration
15
- } from "./index-23t8ryqw.js";
16
- import {
17
- Database2,
18
- getSqliteDatabasePath2,
19
- prepareCachedStatement2,
20
- withSqliteBackgroundWriter2,
21
- withoutSqliteTransformPass2
22
- } from "./index-2qtx0q6h.js";
23
- import {
24
- removeSystemReminders,
25
- removeNestedBlocks,
26
- hasMeaningfulUserText2,
27
- extractTexts2
28
- } from "./index-my0dn413.js";
29
- import {
30
- recordMessageFtsRowid2,
31
- recordIndexedMessageTime2,
32
- messageFtsOrdinalRangeIsMapped2,
33
- getMessageFtsRowidMapBackfillProgress2,
34
- deleteUnmappedMessageFtsRows2
35
- } from "./index-8a2wvrda.js";
36
- import {
37
- deleteChunkEmbedBackoffForSession2
38
- } from "./index-wb2t932e.js";
39
- import {
40
- closeQuietly,
41
- TAG_TOMBSTONE_OWNER_PREFIX,
42
- tagTombstoneOwnerRange,
43
- tagTombstoneOwnerKey,
44
- tagRevisionKey,
45
- tagIdentityRevisionKey,
46
- deleteTagIdentityRevisions
47
- } from "./index-w1avzkdt.js";
48
- import {
49
- cleanUserText2
50
- } from "./index-cm9e52mb.js";
51
- import {
52
- sameProjectDirectory
53
- } from "./index-wrr9rp9a.js";
54
- import {
55
- logSlowWriteTransaction
56
- } from "./index-hqw0rt9k.js";
57
- import {
58
- ensurePrivateStorageDirectorySync,
59
- writeStorageFileSync,
60
- writeStorageFileAtomicSync,
61
- syncStorageDirectorySync,
62
- writeStorageFileDurableAtomicSync
63
- } from "./index-81nthk8j.js";
64
-
65
- // ../plugin/src/hooks/magic-context/lkg-recovery-fence.ts
66
- import { createHash as createHash2, randomUUID as randomUUID2 } from "node:crypto";
67
- import {
68
- lstatSync,
69
- opendirSync,
70
- readdirSync as readdirSync2,
71
- readFileSync as readFileSync2,
72
- rmdirSync,
73
- rmSync as rmSync2
74
- } from "node:fs";
75
- import { basename, dirname, join as join2 } from "node:path";
76
-
77
- // ../plugin/src/shared/storage-file-mutex.ts
78
- import { execFileSync } from "node:child_process";
79
- import { createHash, randomUUID } from "node:crypto";
80
- import { readdirSync, readFileSync, rmSync } from "node:fs";
81
- import { join } from "node:path";
82
- var ticketChecksum = (name, ticket) => createHash("sha256").update(JSON.stringify([name, ticket.choosing, ticket.number, ticket.birth])).digest("hex");
83
- function processBirth(pid) {
84
- if (process.platform === "linux") {
85
- try {
86
- const stat = readFileSync(`/proc/${pid}/stat`, "utf8");
87
- const ticks = stat.slice(stat.lastIndexOf(")") + 2).trim().split(/\s+/)[19];
88
- if (!ticks || !/^\d+$/.test(ticks))
89
- return null;
90
- return `${readFileSync("/proc/sys/kernel/random/boot_id", "utf8").trim()}:${ticks}`;
91
- } catch {
92
- return null;
93
- }
94
- }
95
- if (process.platform === "darwin") {
96
- try {
97
- return execFileSync("ps", ["-p", String(pid), "-o", "lstart="], {
98
- encoding: "utf8",
99
- timeout: 1000,
100
- windowsHide: true,
101
- env: { ...process.env, TZ: "UTC", LC_ALL: "C" },
102
- stdio: ["ignore", "pipe", "pipe"]
103
- }).trim() || null;
104
- } catch {
105
- return null;
106
- }
107
- }
108
- const start = readProcessStartTime(pid);
109
- return start === null ? null : String(start);
110
- }
111
- var pause = new Int32Array(new SharedArrayBuffer(4));
112
- function withStorageFileMutex(directory, work, timeoutMs = 1000) {
113
- ensurePrivateStorageDirectorySync(directory);
114
- const name = `${process.pid}-${randomUUID()}.lock`;
115
- const ownPath = join(directory, name);
116
- const birth = processBirth(process.pid);
117
- const ticket = { choosing: true, number: 0, birth, checksum: "" };
118
- const encodedTicket = () => JSON.stringify({ ...ticket, checksum: ticketChecksum(name, ticket) });
119
- for (let attempt = 0;; attempt++) {
120
- try {
121
- writeStorageFileSync(ownPath, encodedTicket(), { flag: "wx", forcePrivate: true });
122
- break;
123
- } catch (error) {
124
- if (error.code !== "ENOENT" || attempt >= 2)
125
- throw error;
126
- ensurePrivateStorageDirectorySync(directory);
127
- }
128
- }
129
- const deadline = performance.now() + timeoutMs;
130
- const peers = () => {
131
- const found = [];
132
- for (const other of readdirSync(directory)) {
133
- if (other === name || !/^\d+-[\da-f-]+\.lock$/.test(other))
134
- continue;
135
- const pid = Number(other.split("-")[0]);
136
- const otherPath = join(directory, other);
137
- let value = null;
138
- try {
139
- const parsed = JSON.parse(readFileSync(otherPath, "utf8"));
140
- if (typeof parsed.choosing === "boolean" && Number.isSafeInteger(parsed.number) && parsed.number >= 0 && (parsed.birth === null || typeof parsed.birth === "string") && parsed.checksum === ticketChecksum(other, parsed))
141
- value = parsed;
142
- } catch (error) {
143
- if (error.code === "ENOENT")
144
- continue;
145
- }
146
- const alive = isPidAlive(pid);
147
- const actualBirth = alive === "alive" && value?.birth ? processBirth(pid) : null;
148
- if (alive === "dead" || actualBirth !== null && value?.birth && actualBirth !== value.birth) {
149
- rmSync(otherPath, { force: true });
150
- continue;
151
- }
152
- found.push([other, value]);
153
- }
154
- return found;
155
- };
156
- try {
157
- ticket.number = 1 + Math.max(0, ...peers().map(([, value]) => value?.number ?? 0));
158
- ticket.choosing = false;
159
- writeStorageFileAtomicSync(ownPath, encodedTicket(), true);
160
- while (peers().some(([other, value]) => !value || value.choosing || value.number < ticket.number || value.number === ticket.number && other < name)) {
161
- if (performance.now() >= deadline)
162
- throw new Error("Storage safety publication lock is unavailable");
163
- Atomics.wait(pause, 0, 0, 10);
164
- }
165
- const result = work();
166
- if (result && typeof result.then === "function")
167
- throw new TypeError("Storage publication mutex requires synchronous work");
168
- return result;
169
- } finally {
170
- rmSync(ownPath, { force: true });
171
- }
172
- }
173
-
174
- // ../plugin/src/hooks/magic-context/lkg-recovery-fence.ts
175
- class LkgRecoveryFenceError extends Error {
176
- constructor(message) {
177
- super(message);
178
- this.name = "LkgRecoveryFenceError";
179
- }
180
- }
181
- var platform = process.platform;
182
- var observer;
183
- var knownFences = new BoundedSessionMap(1000);
184
- var ioObserver;
185
- var cacheKey = (sessionId) => `${getMagicContextStorageDir()}\x00${getHarness2()}\x00${sessionId}`;
186
- function remember(sessionId, value) {
187
- knownFences.set(cacheKey(sessionId), value);
188
- return value;
189
- }
190
- function known(sessionId) {
191
- const prior = knownFences.get(cacheKey(sessionId));
192
- if (prior)
193
- return prior;
194
- const record = read(sessionId);
195
- if (record)
196
- return remember(sessionId, { state: record.state, record });
197
- try {
198
- lstatSync(lkgRecoveryFencePath(sessionId));
199
- } catch (error) {
200
- if (error.code === "ENOENT")
201
- return remember(sessionId, { state: "clean", record: null });
202
- }
203
- return remember(sessionId, { state: "invalid", record: null });
204
- }
205
- function clearLkgFenceProcessState(sessionId) {
206
- if (sessionId)
207
- knownFences.delete(cacheKey(sessionId));
208
- else
209
- knownFences.clear();
210
- }
211
- var durableFencesSupported = () => platform !== "win32";
212
- var hash = (value) => createHash2("sha256").update(value).digest("hex");
213
- function lkgRecoveryFencePath(sessionId) {
214
- return join2(getMagicContextStorageDir(), "lkg-recovery-fences", `${hash(sessionId)}.json`);
215
- }
216
- var lockSweepDirectory;
217
- function closeLkgLockSweepDirectory() {
218
- lockSweepDirectory?.dir.closeSync();
219
- lockSweepDirectory = undefined;
220
- }
221
- function takeLkgRecoveryLockSweepPage(maxEntries = 200) {
222
- const path = join2(getMagicContextStorageDir(), "lkg-recovery-fences");
223
- if (lockSweepDirectory?.path !== path)
224
- closeLkgLockSweepDirectory();
225
- const names = [];
226
- try {
227
- lockSweepDirectory ??= { path, dir: opendirSync(path) };
228
- for (let i = 0;i < Math.min(200, Math.max(1, maxEntries)); i++) {
229
- const entry = lockSweepDirectory.dir.readSync();
230
- if (!entry) {
231
- closeLkgLockSweepDirectory();
232
- break;
233
- }
234
- if (entry.isDirectory() && /^[a-f0-9]{64}\.json\.locks$/.test(entry.name))
235
- names.push(entry.name);
236
- }
237
- } catch {
238
- closeLkgLockSweepDirectory();
239
- }
240
- return names;
241
- }
242
- function sweepEmptyLkgRecoveryLockDirectories(names, retainedSessionIds) {
243
- if (!durableFencesSupported())
244
- return 0;
245
- const retainedHashes = new Set(Array.from(retainedSessionIds, hash));
246
- const root = join2(getMagicContextStorageDir(), "lkg-recovery-fences");
247
- let removed = 0;
248
- for (const name of names.slice(0, 200)) {
249
- if (!/^[a-f0-9]{64}\.json\.locks$/.test(name) || retainedHashes.has(name.slice(0, 64)))
250
- continue;
251
- const path = join2(root, name);
252
- try {
253
- try {
254
- lstatSync(path.slice(0, -".locks".length));
255
- continue;
256
- } catch (error) {
257
- if (error.code !== "ENOENT")
258
- continue;
259
- }
260
- if (!lstatSync(path).isDirectory())
261
- continue;
262
- rmdirSync(path);
263
- removed++;
264
- } catch {}
265
- }
266
- return removed;
267
- }
268
- function metadata(slot) {
269
- return [
270
- slot.inputIdSeq,
271
- slot.inputContentDigests,
272
- slot.inputContentSignatures ?? null,
273
- slot.piOutputEntryIds ?? null,
274
- slot.lastInputMessageId,
275
- slot.modelKey,
276
- slot.providerKey
277
- ];
278
- }
279
- function representationKey(slot) {
280
- return createHash2("sha256").update(slot.jsonPrefix).update(JSON.stringify(metadata(slot))).digest("hex");
281
- }
282
- function lkgRecoverySlotKey(slot) {
283
- return hash(JSON.stringify([
284
- representationKey(slot),
285
- slot.capturedAt,
286
- slot.rowVersion ?? null,
287
- slot.captureSequence ?? null
288
- ]));
289
- }
290
- function payload(record) {
291
- return [
292
- record.version,
293
- record.sessionId,
294
- record.host,
295
- record.modelKey,
296
- record.providerKey,
297
- record.slotKey,
298
- record.servedDigest,
299
- record.revision,
300
- record.state
301
- ];
302
- }
303
- function publishRecord(record) {
304
- ioObserver?.("write");
305
- try {
306
- if (record.state === "clean")
307
- observer?.("clean");
308
- writeStorageFileDurableAtomicSync(lkgRecoveryFencePath(record.sessionId), JSON.stringify({ ...record, checksum: hash(JSON.stringify(payload(record))) }), () => observer?.("temp-synced"));
309
- if (record.state === "dirty")
310
- observer?.("dirty");
311
- remember(record.sessionId, {
312
- state: record.state,
313
- record: { ...record, checksum: hash(JSON.stringify(payload(record))) }
314
- });
315
- } catch (error) {
316
- const visible = read(record.sessionId);
317
- if (record.state === "clean" && visible?.state === "clean" && visible.revision === record.revision && visible.slotKey === record.slotKey) {
318
- const dirty = { ...record, state: "dirty" };
319
- writeStorageFileDurableAtomicSync(lkgRecoveryFencePath(record.sessionId), JSON.stringify({ ...dirty, checksum: hash(JSON.stringify(payload(dirty))) }));
320
- }
321
- remember(record.sessionId, { state: "invalid", record: null });
322
- throw error;
323
- }
324
- }
325
- function read(sessionId) {
326
- ioObserver?.("read");
327
- try {
328
- const path = lkgRecoveryFencePath(sessionId);
329
- const stat = lstatSync(path);
330
- const directory = lstatSync(dirname(path));
331
- if (!stat.isFile() || stat.size > 4096 || !directory.isDirectory() || directory.isSymbolicLink() || process.platform !== "win32" && ((stat.mode & 63) !== 0 || (directory.mode & 63) !== 0 || process.getuid && stat.uid !== process.getuid()))
332
- return null;
333
- const value = JSON.parse(readFileSync2(path, "utf8"));
334
- if (value.version !== 1 || value.sessionId !== sessionId || value.host !== getHarness2() || value.modelKey !== null && typeof value.modelKey !== "string" || value.providerKey !== null && typeof value.providerKey !== "string" || !/^[a-f0-9]{64}$/.test(value.slotKey) || !/^[a-f0-9]{64}$/.test(value.servedDigest) || typeof value.revision !== "string" || !["clean", "dirty"].includes(value.state) || value.checksum !== hash(JSON.stringify(payload(value))))
335
- return null;
336
- return value;
337
- } catch {
338
- return null;
339
- }
340
- }
341
- function publish(sessionId, slot, state, revision) {
342
- const record = {
343
- version: 1,
344
- sessionId,
345
- host: getHarness2(),
346
- modelKey: slot.modelKey,
347
- providerKey: slot.providerKey,
348
- slotKey: lkgRecoverySlotKey(slot),
349
- servedDigest: hash(slot.jsonPrefix),
350
- revision,
351
- state
352
- };
353
- publishRecord(record);
354
- }
355
- function locked(sessionId, work) {
356
- ioObserver?.("mutex");
357
- return withStorageFileMutex(`${lkgRecoveryFencePath(sessionId)}.locks`, () => {
358
- const path = lkgRecoveryFencePath(sessionId);
359
- for (const name of readdirSync2(dirname(path))) {
360
- if (name.startsWith(`${basename(path)}.`) && name.endsWith(".tmp"))
361
- rmSync2(join2(dirname(path), name), { force: true });
362
- }
363
- return work();
364
- });
365
- }
366
- function hasCleanLkgRecoveryFence(sessionId, _slot) {
367
- if (!durableFencesSupported())
368
- return false;
369
- const state = known(sessionId);
370
- return state.state === "clean";
371
- }
372
- function lkgFenceRequiresRebuild(sessionId, slot, processLocalProof) {
373
- return !processLocalProof && (durableFencesSupported() ? !hasCleanLkgRecoveryFence(sessionId, slot) : true);
374
- }
375
- function lkgRecoveryFenceUnclean(sessionId) {
376
- return durableFencesSupported() && known(sessionId).state !== "clean";
377
- }
378
- function ownsLkgDirtyFence(sessionId, revision) {
379
- if (!durableFencesSupported())
380
- return revision === "process-local";
381
- const record = known(sessionId).record;
382
- return revision !== undefined && record?.state === "dirty" && record.revision === revision;
383
- }
384
- function invalidateCleanLkgRecoveryFence(sessionId, load, ownRevision) {
385
- if (!durableFencesSupported())
386
- return "process-local";
387
- const prior = known(sessionId);
388
- if (prior.state === "dirty")
389
- return prior.record?.revision === ownRevision ? ownRevision : undefined;
390
- return locked(sessionId, () => {
391
- const slot = load() ?? {
392
- jsonPrefix: "",
393
- inputIdSeq: [],
394
- inputContentDigests: [],
395
- lastInputMessageId: "",
396
- modelKey: null,
397
- providerKey: null,
398
- capturedAt: 0
399
- };
400
- const record = read(sessionId);
401
- if (record?.state !== "dirty") {
402
- const revision = randomUUID2();
403
- publish(sessionId, slot, "dirty", revision);
404
- return revision;
405
- }
406
- return record?.state === "dirty" && record.revision === ownRevision ? ownRevision : undefined;
407
- });
408
- }
409
- function markLkgRecoveryDirty(args) {
410
- if (!durableFencesSupported()) {
411
- if (!args.processLocalProof)
412
- throw new LkgRecoveryFenceError("Restart replay requires a healthy rebuild on Windows");
413
- return "process-local";
414
- }
415
- const state = known(args.sessionId);
416
- if (state.state === "dirty" && args.processLocalProof && state.record && state.record.revision === args.ownDirtyRevision)
417
- return state.record.revision;
418
- if (state.state === "invalid")
419
- throw new LkgRecoveryFenceError("No clean durable recovery certificate");
420
- return locked(args.sessionId, () => {
421
- const slot = args.slot;
422
- const before = read(args.sessionId);
423
- if (before?.state === "dirty") {
424
- if (!args.processLocalProof || before.revision !== args.ownDirtyRevision)
425
- throw new LkgRecoveryFenceError(`Recovery superseded this process's last-good request (local=${args.processLocalProof}, owned=${before.revision === args.ownDirtyRevision})`);
426
- return before.revision;
427
- }
428
- if (before && before.state !== "clean")
429
- throw new LkgRecoveryFenceError("No clean durable recovery certificate");
430
- const revision = randomUUID2();
431
- publish(args.sessionId, slot, "dirty", revision);
432
- return revision;
433
- });
434
- }
435
- function commitLkgWithRecoveryFence(args) {
436
- if (!durableFencesSupported())
437
- return args.commit();
438
- const state = known(args.sessionId);
439
- if (state.state === "clean" && !args.requiredRevision && !args.retainDirtyRevision) {
440
- const committed = args.commit();
441
- if (committed) {
442
- observer?.("committed");
443
- remember(args.sessionId, { state: "clean", record: null });
444
- }
445
- return committed;
446
- }
447
- if (state.state === "invalid" && !args.rebuild)
448
- throw new LkgRecoveryFenceError("Unproven prior request requires a prefix rebuild");
449
- return locked(args.sessionId, () => {
450
- const before = read(args.sessionId);
451
- if (args.requiredRevision && before?.revision !== args.requiredRevision)
452
- throw new LkgRecoveryFenceError("A later publisher superseded the rollback snapshot");
453
- const ownsDirty = before?.state === "dirty" && (before.revision === args.retainDirtyRevision || before.revision === args.ownDirtyRevision);
454
- if (before?.state === "dirty" && !ownsDirty && !args.rebuild)
455
- throw new LkgRecoveryFenceError("A concurrent recovery requires a declared prefix rebuild");
456
- if (before?.state !== "dirty" && state.state === "invalid")
457
- publish(args.sessionId, args.load() ?? args.slot, "dirty", randomUUID2());
458
- if (!args.commit())
459
- return false;
460
- observer?.("committed");
461
- const saved = args.load();
462
- if (!saved || representationKey(saved) !== representationKey(args.slot))
463
- throw new LkgRecoveryFenceError("Last-good commit did not match its certificate");
464
- if (args.retainDirtyRevision || args.cleanDigest !== undefined && hash(saved.jsonPrefix) !== args.cleanDigest)
465
- return true;
466
- const dirty = read(args.sessionId);
467
- if (dirty?.state === "dirty")
468
- publish(args.sessionId, saved, "clean", dirty.revision);
469
- else
470
- remember(args.sessionId, { state: "clean", record: null });
471
- return true;
472
- });
473
- }
474
- function clearLkgRecoveryFence(sessionId, remove, shouldClear) {
475
- if (!durableFencesSupported())
476
- return remove?.();
477
- const path = lkgRecoveryFencePath(sessionId);
478
- const missing = (file) => {
479
- try {
480
- lstatSync(file);
481
- return false;
482
- } catch (error) {
483
- return error.code === "ENOENT";
484
- }
485
- };
486
- if (!remove && !shouldClear && missing(path) && missing(`${path}.locks`)) {
487
- clearLkgFenceProcessState(sessionId);
488
- return;
489
- }
490
- try {
491
- return locked(sessionId, () => {
492
- const result = remove?.();
493
- if (shouldClear && !shouldClear(result))
494
- return result;
495
- rmSync2(lkgRecoveryFencePath(sessionId), { force: true });
496
- syncStorageDirectorySync(dirname(lkgRecoveryFencePath(sessionId)));
497
- clearLkgFenceProcessState(sessionId);
498
- return result;
499
- });
500
- } finally {
501
- try {
502
- rmdirSync(`${path}.locks`);
503
- } catch {}
504
- }
505
- }
506
-
507
- // ../plugin/src/v2/store-reader.ts
508
- import { createHash as createHash3 } from "node:crypto";
509
- import { statSync } from "node:fs";
510
- import { hostname } from "node:os";
511
- import { resolve } from "node:path";
512
- function gaDatabasePath(dataHome, channel = "latest", env = process.env) {
513
- return resolveOpenCodeDbPath("v2", { dataHome, channel, env }).path;
514
- }
515
- var RAW_MESSAGE_TYPES = [
516
- "user",
517
- "synthetic",
518
- "assistant",
519
- "skill",
520
- "shell",
521
- "system"
522
- ];
523
- var RAW_MESSAGE_TYPE_PARAMETERS = RAW_MESSAGE_TYPES.map(() => "?").join(", ");
524
- var V2_MESSAGE_PAGE_SQL = `WITH bounds AS (
525
- SELECT
526
- CASE WHEN ? = 0 THEN -1 ELSE COALESCE((
527
- SELECT seq FROM session_message
528
- WHERE session_id = ? AND type IN (${RAW_MESSAGE_TYPE_PARAMETERS})
529
- ORDER BY seq ASC LIMIT 1 OFFSET ?
530
- ), (SELECT MAX(seq) FROM session_message WHERE session_id = ?), -1) END AS after_seq,
531
- COALESCE((
532
- SELECT seq FROM session_message
533
- WHERE session_id = ? AND type IN (${RAW_MESSAGE_TYPE_PARAMETERS})
534
- ORDER BY seq ASC LIMIT 1 OFFSET ?
535
- ), ?) AS watermark_seq
536
- )
537
- SELECT id, session_id, type, seq, time_created, data FROM session_message, bounds
538
- WHERE session_id = ?
539
- AND type IN (${RAW_MESSAGE_TYPE_PARAMETERS})
540
- AND seq > bounds.after_seq
541
- AND seq <= bounds.watermark_seq
542
- ORDER BY seq ASC LIMIT ?`;
543
- var V2_STORE_READER_DEBUG_COUNTER_KEY = "magic-context.v2.store-reader-debug";
544
- var debugSymbol = Symbol.for(V2_STORE_READER_DEBUG_COUNTER_KEY);
545
- var debugGlobal = globalThis;
546
- var QUERY_STATEMENTS = {
547
- messagePage: V2_MESSAGE_PAGE_SQL,
548
- messageCount: "SELECT COUNT(*) AS count FROM session_message WHERE session_id = ? AND type IN (?)",
549
- storedMessageCount: "SELECT COUNT(*) AS count FROM session_message WHERE session_id = ?",
550
- messageById: "SELECT id, session_id, type, seq, time_created, data FROM session_message WHERE session_id = ? AND id = ? AND type IN (?) LIMIT 1",
551
- messageExistsById: "SELECT 1 FROM session_message WHERE id = ? AND session_id = ? AND type IN (?) LIMIT 1",
552
- messageOrdinalById: `WITH target AS (
553
- SELECT seq FROM session_message WHERE session_id = ? AND id = ? AND type IN (?) LIMIT 1
554
- )
555
- SELECT (SELECT COUNT(*) FROM session_message WHERE session_id = ? AND type IN (?) AND seq <= target.seq) AS ordinal FROM target`,
556
- messageIdOrdinals: "SELECT id FROM session_message WHERE session_id = ? AND type IN (?) ORDER BY seq ASC LIMIT ? OFFSET ?",
557
- messageOrdinalPage: "SELECT id, type, seq, json_valid(data) AS valid FROM session_message WHERE session_id = ? AND seq > ? ORDER BY seq ASC, id ASC LIMIT ?",
558
- rawRowsThrough: "SELECT id, session_id, type, seq, time_created, data FROM session_message WHERE session_id = ? AND type IN (?) AND seq <= ? ORDER BY seq DESC LIMIT ?"
559
- };
560
- function debugCounters() {
561
- const counters = debugGlobal[debugSymbol] ??= {
562
- decodedRows: 0,
563
- operations: {},
564
- openReaders: 0,
565
- maxOpenReaders: 0,
566
- readersOpened: 0,
567
- readersClosed: 0
568
- };
569
- counters.openReaders ??= 0;
570
- counters.maxOpenReaders ??= 0;
571
- counters.readersOpened ??= 0;
572
- counters.readersClosed ??= 0;
573
- return counters;
574
- }
575
- function returnedRowCount(value) {
576
- if (Array.isArray(value))
577
- return value.length;
578
- if (value === null || value === undefined || value === false)
579
- return 0;
580
- return 1;
581
- }
582
- function trackDecodeOperation(name, operation) {
583
- const counters = debugCounters();
584
- const before = counters.decodedRows;
585
- const started = performance.now();
586
- let value;
587
- try {
588
- value = operation();
589
- return value;
590
- } finally {
591
- const decodedRows = counters.decodedRows - before;
592
- const current = counters.operations[name] ?? {
593
- calls: 0,
594
- decodedRows: 0,
595
- maxDecodedRows: 0
596
- };
597
- current.calls += 1;
598
- current.decodedRows += decodedRows;
599
- current.maxDecodedRows = Math.max(current.maxDecodedRows, decodedRows);
600
- counters.operations[name] = current;
601
- const statement = QUERY_STATEMENTS[name];
602
- if (counters.captureQueries && statement) {
603
- (counters.queries ??= []).push({
604
- operation: name,
605
- statement,
606
- rows: returnedRowCount(value),
607
- elapsedMs: performance.now() - started
608
- });
609
- }
610
- }
611
- }
612
- function decode(row) {
613
- debugCounters().decodedRows += 1;
614
- const data = JSON.parse(row.data);
615
- if (!data || typeof data !== "object" || Array.isArray(data)) {
616
- throw new Error(`Invalid session_message data at seq ${row.seq}`);
617
- }
618
- if (row.type === "idle" && !["succeeded", "failed", "interrupted"].includes(String(data.outcome))) {
619
- throw new Error(`Invalid idle outcome at seq ${row.seq}`);
620
- }
621
- return { ...row, data };
622
- }
623
- function connect(path, identity = "") {
624
- const stat = statSync(path);
625
- const acquiredIdentity = identity || `${stat.dev}:${stat.ino}:${stat.birthtimeMs}`;
626
- const db = new Database2(path, { readonly: true, fileMustExist: true });
627
- try {
628
- assertOpenCodeStoreGeneration(db, "v2", path);
629
- } catch (error) {
630
- db.close();
631
- throw error;
632
- }
633
- const counters = debugCounters();
634
- counters.openReaders += 1;
635
- counters.readersOpened += 1;
636
- counters.maxOpenReaders = Math.max(counters.maxOpenReaders, counters.openReaders);
637
- return { db, statements: new Map, identity: acquiredIdentity, leases: 0, retired: false };
638
- }
639
- function closeConnection(connection) {
640
- connection.statements.clear();
641
- try {
642
- connection.db.close();
643
- } finally {
644
- const counters = debugCounters();
645
- counters.openReaders = Math.max(0, counters.openReaders - 1);
646
- counters.readersClosed += 1;
647
- }
648
- }
649
- function messageTimesQuery(count) {
650
- return `SELECT id, COALESCE(time_created, json_extract(data, '$.time.created')) AS created,
651
- json_extract(data, '$.time.completed') AS completed
652
- FROM session_message WHERE +session_id = ? AND id IN (${Array.from({ length: count }, () => "?").join(",")})`;
653
- }
654
-
655
- class V2StoreReader {
656
- path;
657
- pool;
658
- db;
659
- connection;
660
- closed = false;
661
- constructor(path, pool) {
662
- this.path = path;
663
- this.pool = pool;
664
- this.connection = pool ? pool.acquire(path) : connect(path);
665
- this.db = this.connection.db;
666
- }
667
- hostIdentity() {
668
- return createHash3("sha256").update(JSON.stringify([hostname(), resolve(this.path), this.connection.identity])).digest("hex");
669
- }
670
- prepare(sql) {
671
- if (this.closed)
672
- throw new Error("Store reader is closed");
673
- const statements = this.connection.statements;
674
- let statement = statements.get(sql);
675
- if (!statement) {
676
- statement = this.db.prepare(sql);
677
- if (statements.size >= 128)
678
- statements.clear();
679
- statements.set(sql, statement);
680
- }
681
- return statement;
682
- }
683
- close() {
684
- if (this.closed)
685
- return;
686
- this.closed = true;
687
- if (this.pool)
688
- this.pool.release(this.connection);
689
- else
690
- closeConnection(this.connection);
691
- }
692
- page(sessionID, options = {}) {
693
- return trackDecodeOperation("page", () => {
694
- const limit = options.limit ?? 100;
695
- if (!Number.isSafeInteger(limit) || limit < 1 || limit > 1e4)
696
- throw new Error("Invalid page limit");
697
- const after = options.after ?? -1;
698
- if (!Number.isSafeInteger(after))
699
- throw new Error("Invalid seq cursor");
700
- if (options.through !== undefined && !Number.isSafeInteger(options.through))
701
- throw new Error("Invalid seq upper bound");
702
- const predicates = ["session_id = ?", "seq > ?"];
703
- const parameters = [sessionID, after];
704
- if (options.through !== undefined) {
705
- predicates.push("seq <= ?");
706
- parameters.push(options.through);
707
- }
708
- if (options.type) {
709
- predicates.push("type = ?");
710
- parameters.push(options.type);
711
- }
712
- const rows = this.prepare(`SELECT id, session_id, type, seq, time_created, data FROM session_message
713
- WHERE ${predicates.join(" AND ")}
714
- ORDER BY seq ASC LIMIT ?`).all(...parameters, limit).map(decode);
715
- return { rows, cursor: rows.at(-1)?.seq };
716
- });
717
- }
718
- messagePage(sessionID, afterOrdinal, limit, finalWatermark, after) {
719
- return trackDecodeOperation("messagePage", () => {
720
- if (!Number.isSafeInteger(afterOrdinal) || afterOrdinal < 0)
721
- throw new Error("Invalid raw-message ordinal cursor");
722
- if (!Number.isSafeInteger(limit) || limit < 1 || limit > 1e4)
723
- throw new Error("Invalid page limit");
724
- if (!Number.isSafeInteger(finalWatermark) || finalWatermark < 0)
725
- throw new Error("Invalid raw-message watermark");
726
- const pageSize = Math.min(limit, finalWatermark - afterOrdinal);
727
- if (pageSize <= 0)
728
- return [];
729
- if (after) {
730
- const anchor = this.prepare("SELECT seq FROM session_message WHERE session_id = ? AND id = ?").get(sessionID, after.id);
731
- if (anchor) {
732
- return this.prepare(`SELECT id, session_id, type, seq, time_created, data
733
- FROM session_message WHERE session_id = ? AND seq > ?
734
- AND type IN (${RAW_MESSAGE_TYPE_PARAMETERS}) ORDER BY seq ASC LIMIT ?`).all(sessionID, anchor.seq, ...RAW_MESSAGE_TYPES, pageSize).map(decode);
735
- }
736
- }
737
- const maximumSeq = Number.MAX_SAFE_INTEGER;
738
- const rows = this.prepare(V2_MESSAGE_PAGE_SQL).all(afterOrdinal, sessionID, ...RAW_MESSAGE_TYPES, Math.max(0, afterOrdinal - 1), sessionID, sessionID, ...RAW_MESSAGE_TYPES, finalWatermark - 1, maximumSeq, sessionID, ...RAW_MESSAGE_TYPES, pageSize);
739
- return rows.map(decode);
740
- });
741
- }
742
- messageCount(sessionID) {
743
- return trackDecodeOperation("messageCount", () => {
744
- const rawTypes = RAW_MESSAGE_TYPES.map(() => "?").join(", ");
745
- const row = this.prepare(`SELECT COUNT(*) AS count FROM session_message
746
- WHERE session_id = ? AND type IN (${rawTypes})`).get(sessionID, ...RAW_MESSAGE_TYPES);
747
- return typeof row?.count === "number" ? row.count : 0;
748
- });
749
- }
750
- messageCountAfterSequence(sessionID, after, through) {
751
- const rawTypes = RAW_MESSAGE_TYPES.map(() => "?").join(", ");
752
- const row = this.prepare(`SELECT COUNT(*) AS count FROM session_message
753
- WHERE session_id = ? AND seq > ? AND seq <= ? AND type IN (${rawTypes})`).get(sessionID, after, through, ...RAW_MESSAGE_TYPES);
754
- return typeof row?.count === "number" ? row.count : 0;
755
- }
756
- sessionDirectoryPage(afterSessionID, limit) {
757
- const rows = this.prepare(`SELECT id, COALESCE(directory, '') AS directory FROM session_v2
758
- WHERE id > ? ORDER BY id ASC LIMIT ?`).all(afterSessionID ?? "", limit);
759
- return rows.map((row) => ({ sessionId: row.id, directory: row.directory }));
760
- }
761
- latestMessageTime(sessionID) {
762
- const row = this.prepare("SELECT MAX(time_created) AS time FROM session_message WHERE session_id = ?").get(sessionID);
763
- return row?.time ?? undefined;
764
- }
765
- isSubagent(sessionID) {
766
- const row = this.prepare("SELECT parent_id FROM session_v2 WHERE id = ?").get(sessionID);
767
- if (!row)
768
- return null;
769
- return typeof row.parent_id === "string" && row.parent_id.length > 0;
770
- }
771
- sessionIds() {
772
- return this.prepare("SELECT id FROM session_v2").all().map((row) => row.id);
773
- }
774
- latestRootSessionInDirectory(directory) {
775
- const columns = new Set(this.prepare("PRAGMA table_info(session_v2)").all().map((column) => column.name));
776
- const archived = columns.has("time_archived") ? " AND time_archived IS NULL" : "";
777
- const order = columns.has("time_updated") ? "time_updated DESC, id DESC" : "id DESC";
778
- const page = this.prepare(`SELECT id, COALESCE(directory, '') AS directory FROM session_v2
779
- WHERE parent_id IS NULL${archived}
780
- AND COALESCE(json_extract(metadata, '$.magic_context'), '') <> 'hidden-run'
781
- ORDER BY ${order} LIMIT ? OFFSET ?`);
782
- const pageSize = 500;
783
- for (let offset = 0;; offset += pageSize) {
784
- const rows = page.all(pageSize, offset);
785
- const match = rows.find((row) => sameProjectDirectory(row.directory, directory));
786
- if (match)
787
- return match.id;
788
- if (rows.length < pageSize)
789
- return;
790
- }
791
- }
792
- rootSessionActivity(sessionIDs) {
793
- const roots = new Map;
794
- for (let offset = 0;offset < sessionIDs.length; offset += 500) {
795
- const chunk = sessionIDs.slice(offset, offset + 500);
796
- const rows = this.prepare(`SELECT id, (
797
- SELECT MAX(time_created) FROM session_message
798
- WHERE session_id = session_v2.id AND type = 'user'
799
- ) AS time FROM session_v2
800
- WHERE parent_id IS NULL
801
- AND COALESCE(json_extract(metadata, '$.magic_context'), '') <> 'hidden-run'
802
- AND id IN (${chunk.map(() => "?").join(",")})`).all(...chunk);
803
- for (const row of rows)
804
- if (row.time !== null)
805
- roots.set(row.id, row.time);
806
- }
807
- return roots;
808
- }
809
- oldestUserMessageTimesSince(sessionIDs, sinceMs) {
810
- const result = new Map;
811
- for (let offset = 0;offset < sessionIDs.length; offset += 500) {
812
- const chunk = sessionIDs.slice(offset, offset + 500);
813
- const rows = this.prepare(`SELECT session_id, MIN(time_created) AS time FROM session_message
814
- WHERE type = 'user' AND time_created > ? AND session_id IN (${chunk.map(() => "?").join(",")})
815
- GROUP BY session_id`).all(sinceMs, ...chunk);
816
- for (const row of rows)
817
- result.set(row.session_id, row.time);
818
- }
819
- return result;
820
- }
821
- retrospectiveUserPage(sessionID, options) {
822
- const { boundaryMs, before, limit, maxChars, truncationMarker } = options;
823
- if (!Number.isSafeInteger(limit) || limit < 1 || limit > 1e4)
824
- throw new Error("Invalid page limit");
825
- if (!Number.isSafeInteger(maxChars) || maxChars <= truncationMarker.length)
826
- throw new Error("Invalid text limit");
827
- const direction = before ? "DESC" : "ASC";
828
- const head = Math.floor((maxChars - truncationMarker.length) / 2);
829
- const tail = maxChars - truncationMarker.length - head;
830
- return this.prepare(`WITH users AS (
831
- SELECT session_id, seq, time_created, data FROM session_message
832
- WHERE session_id = ? AND type = 'user' AND time_created ${before ? "<=" : ">"}
833
- ?
834
- ORDER BY time_created ${direction}, seq ${direction} LIMIT ?
835
- ), texts AS (
836
- SELECT session_id, seq, time_created, CASE
837
- WHEN json_extract(data, '$.synthetic') = 1 OR json_extract(data, '$.ignored') = 1 THEN ''
838
- WHEN json_type(data, '$.text') = 'text' THEN json_extract(data, '$.text')
839
- ELSE COALESCE((SELECT group_concat(json_extract(value, '$.text'), char(10))
840
- FROM json_each(users.data, '$.content')
841
- WHERE json_extract(value, '$.type') IN ('text', 'input_text')
842
- AND COALESCE(json_extract(value, '$.synthetic'), 0) <> 1
843
- AND COALESCE(json_extract(value, '$.ignored'), 0) <> 1), '') END AS text
844
- FROM users WHERE json_valid(data)
845
- ) SELECT seq, time_created, CASE WHEN length(text) > ?
846
- THEN substr(text, 1, ?) || ? || substr(text, -?) ELSE text END AS text,
847
- EXISTS (
848
- SELECT 1 FROM session_message assistant_reply
849
- WHERE assistant_reply.session_id = texts.session_id
850
- AND assistant_reply.type = 'assistant'
851
- AND assistant_reply.seq < texts.seq
852
- AND EXISTS (
853
- SELECT 1 FROM session_message prior_user
854
- WHERE prior_user.session_id = texts.session_id
855
- AND prior_user.type = 'user'
856
- AND prior_user.seq < assistant_reply.seq
857
- )
858
- ) AS follows_assistant_reply
859
- FROM texts ORDER BY time_created ${direction}, seq ${direction}`).all(sessionID, boundaryMs, limit, maxChars, head, truncationMarker, tail);
860
- }
861
- storedRowsAfter(sessionID, after) {
862
- const row = this.prepare("SELECT COUNT(*) AS count FROM session_message WHERE session_id = ? AND seq > ?").get(sessionID, after);
863
- return row.count;
864
- }
865
- storedMessageCount(sessionID) {
866
- return trackDecodeOperation("storedMessageCount", () => {
867
- const row = this.prepare("SELECT COUNT(*) AS count FROM session_message WHERE session_id = ?").get(sessionID);
868
- return typeof row?.count === "number" ? row.count : 0;
869
- });
870
- }
871
- messageById(sessionID, id) {
872
- return trackDecodeOperation("messageById", () => {
873
- const rawTypes = RAW_MESSAGE_TYPES.map(() => "?").join(", ");
874
- const row = this.prepare(`SELECT id, session_id, type, seq, time_created, data FROM session_message
875
- WHERE session_id = ? AND id = ? AND type IN (${rawTypes}) LIMIT 1`).get(sessionID, id, ...RAW_MESSAGE_TYPES);
876
- return row ? decode(row) : null;
877
- });
878
- }
879
- messageTimes(sessionID, ids) {
880
- const times = new Map;
881
- for (let offset = 0;offset < ids.length; offset += 500) {
882
- const batch = ids.slice(offset, offset + 500);
883
- const rows = this.prepare(messageTimesQuery(batch.length)).all(sessionID, ...batch);
884
- for (const row of rows)
885
- times.set(row.id, {
886
- created: typeof row.created === "number" ? row.created : undefined,
887
- completed: typeof row.completed === "number" ? row.completed : undefined
888
- });
889
- }
890
- return times;
891
- }
892
- messageExistsById(sessionID, id) {
893
- return trackDecodeOperation("messageExistsById", () => {
894
- const rawTypes = RAW_MESSAGE_TYPES.map(() => "?").join(", ");
895
- const row = this.prepare(`SELECT 1 FROM session_message
896
- WHERE id = ? AND session_id = ? AND type IN (${rawTypes}) LIMIT 1`).get(id, sessionID, ...RAW_MESSAGE_TYPES);
897
- return row != null;
898
- });
899
- }
900
- messageOrdinalById(sessionID, id) {
901
- return trackDecodeOperation("messageOrdinalById", () => {
902
- const rawTypes = RAW_MESSAGE_TYPES.map(() => "?").join(", ");
903
- const row = this.prepare(`WITH target AS (
904
- SELECT seq FROM session_message
905
- WHERE session_id = ? AND id = ? AND type IN (${rawTypes})
906
- LIMIT 1
907
- )
908
- SELECT (
909
- SELECT COUNT(*) FROM session_message
910
- WHERE session_id = ?
911
- AND type IN (${rawTypes})
912
- AND seq <= target.seq
913
- ) AS ordinal
914
- FROM target`).get(sessionID, id, ...RAW_MESSAGE_TYPES, sessionID, ...RAW_MESSAGE_TYPES);
915
- return typeof row?.ordinal === "number" ? row.ordinal : null;
916
- });
917
- }
918
- messageIdOrdinals(sessionID, fromOrdinal, toOrdinal) {
919
- return trackDecodeOperation("messageIdOrdinals", () => {
920
- if (!Number.isSafeInteger(fromOrdinal) || fromOrdinal < 1)
921
- throw new Error("Invalid raw-message range start");
922
- if (!Number.isSafeInteger(toOrdinal) || toOrdinal < fromOrdinal)
923
- throw new Error("Invalid raw-message range end");
924
- const rawTypes = RAW_MESSAGE_TYPES.map(() => "?").join(", ");
925
- const rows = this.prepare(`SELECT id FROM session_message
926
- WHERE session_id = ? AND type IN (${rawTypes})
927
- ORDER BY seq ASC LIMIT ? OFFSET ?`).all(sessionID, ...RAW_MESSAGE_TYPES, toOrdinal - fromOrdinal + 1, fromOrdinal - 1);
928
- return new Map(rows.map((row, index) => [row.id, fromOrdinal + index]));
929
- });
930
- }
931
- messageOrdinalPage(sessionID, after, limit) {
932
- return trackDecodeOperation("messageOrdinalPage", () => {
933
- if (!Number.isSafeInteger(limit) || limit < 1 || limit > 1e4)
934
- throw new Error("Invalid page limit");
935
- const rows = after ? this.prepare(`SELECT id, type, seq, json_valid(data) AS valid
936
- FROM session_message
937
- WHERE session_id = ? AND (seq > ? OR (seq = ? AND id > ?))
938
- ORDER BY seq ASC, id ASC LIMIT ?`).all(sessionID, after.timeCreated, after.timeCreated, after.id, limit) : this.prepare(`SELECT id, type, seq, json_valid(data) AS valid
939
- FROM session_message
940
- WHERE session_id = ?
941
- ORDER BY seq ASC, id ASC LIMIT ?`).all(sessionID, limit);
942
- const rawTypes = new Set(RAW_MESSAGE_TYPES);
943
- return rows.map((row) => ({
944
- id: row.id,
945
- timeCreated: row.seq,
946
- contributesOrdinal: rawTypes.has(row.type),
947
- hasValidInfo: row.valid === 1
948
- }));
949
- });
950
- }
951
- range(sessionID, after, through) {
952
- return trackDecodeOperation("range", () => through <= after ? [] : this.all(sessionID, after, undefined, through));
953
- }
954
- spanRowStamps(sessionID, after, through) {
955
- if (!Number.isSafeInteger(after) || !Number.isSafeInteger(through))
956
- throw new Error("Invalid seq span");
957
- const rows = this.prepare(`SELECT seq, id, time_updated AS updated, length(CAST(data AS BLOB)) AS bytes
958
- FROM session_message WHERE session_id = ? AND seq > ? AND seq <= ?
959
- ORDER BY seq ASC`).all(sessionID, after, through);
960
- const stamps = new Map;
961
- for (const row of rows)
962
- stamps.set(row.seq, `${row.id}\x00${row.updated ?? ""}\x00${row.bytes ?? ""}`);
963
- return stamps;
964
- }
965
- rawRowsThrough(sessionID, throughSeq, limit) {
966
- return trackDecodeOperation("rawRowsThrough", () => {
967
- if (!Number.isSafeInteger(throughSeq))
968
- throw new Error("Invalid seq upper bound");
969
- if (!Number.isSafeInteger(limit) || limit < 1 || limit > 1e4)
970
- throw new Error("Invalid page limit");
971
- const rawTypes = RAW_MESSAGE_TYPES.map(() => "?").join(", ");
972
- return this.prepare(`SELECT id, session_id, type, seq, time_created, data FROM session_message
973
- WHERE session_id = ? AND type IN (${rawTypes}) AND seq <= ?
974
- ORDER BY seq DESC LIMIT ?`).all(sessionID, ...RAW_MESSAGE_TYPES, throughSeq, limit).map(decode);
975
- });
976
- }
977
- instructionRowsThrough(sessionID, throughSeq) {
978
- return trackDecodeOperation("instructionRowsThrough", () => {
979
- if (!Number.isSafeInteger(throughSeq))
980
- throw new Error("Invalid seq upper bound");
981
- return this.prepare(`SELECT id, session_id, type, seq, time_created, data FROM session_message
982
- WHERE session_id = ? AND type = 'system' AND seq <= ?
983
- ORDER BY seq ASC`).all(sessionID, throughSeq).map(decode);
984
- });
985
- }
986
- sequenceForId(sessionID, id) {
987
- if (!id)
988
- return;
989
- const row = this.prepare("SELECT seq FROM session_message WHERE session_id = ? AND id = ? LIMIT 1").get(sessionID, id);
990
- return typeof row?.seq === "number" ? row.seq : undefined;
991
- }
992
- syntheticMessageIDs(sessionID) {
993
- const rows = this.prepare("SELECT id FROM session_message WHERE session_id = ? AND type = 'synthetic'").all(sessionID);
994
- return new Set(rows.map((row) => row.id));
995
- }
996
- forkOrigin(sessionID) {
997
- const columns = new Set(this.prepare("PRAGMA table_info(session_v2)").all().map((column) => column.name));
998
- if (!columns.has("fork_session_id") || !columns.has("fork_boundary"))
999
- return null;
1000
- const row = this.prepare("SELECT fork_session_id, fork_boundary FROM session_v2 WHERE id = ?").get(sessionID);
1001
- if (!row?.fork_session_id || !row.fork_boundary)
1002
- return null;
1003
- try {
1004
- const boundary = JSON.parse(row.fork_boundary);
1005
- if (boundary.type !== "before" && boundary.type !== "through" || typeof boundary.messageID !== "string" || boundary.messageID.length === 0)
1006
- return null;
1007
- return {
1008
- parentSessionID: row.fork_session_id,
1009
- boundary: { type: boundary.type, messageID: boundary.messageID }
1010
- };
1011
- } catch {
1012
- return null;
1013
- }
1014
- }
1015
- sessionExists(sessionID) {
1016
- return this.prepare("SELECT 1 FROM session_v2 WHERE id = ?").get(sessionID) != null;
1017
- }
1018
- rowStampsThrough(sessionID, throughSeq) {
1019
- return this.prepare(`SELECT id, type, seq, time_created FROM session_message
1020
- WHERE session_id = ? AND seq <= ? ORDER BY seq ASC`).all(sessionID, throughSeq);
1021
- }
1022
- latestSequenceForIds(sessionID, ids) {
1023
- let latest = -1;
1024
- for (let offset = 0;offset < ids.length; offset += 500) {
1025
- const chunk = ids.slice(offset, offset + 500);
1026
- const row = this.prepare(`SELECT MAX(seq) AS seq FROM session_message
1027
- WHERE session_id = ? AND id IN (${chunk.map(() => "?").join(", ")})`).get(sessionID, ...chunk);
1028
- if (typeof row?.seq === "number")
1029
- latest = Math.max(latest, row.seq);
1030
- }
1031
- return latest;
1032
- }
1033
- compactionByStatus(sessionID, status) {
1034
- const row = this.prepare(`SELECT id, session_id, type, seq, time_created, data FROM session_message
1035
- WHERE session_id = ? AND type = 'compaction'
1036
- AND json_extract(data, '$.status') = ?
1037
- ORDER BY seq DESC LIMIT 1`).get(sessionID, status);
1038
- return row ? decode(row) : undefined;
1039
- }
1040
- latestCompaction(sessionID) {
1041
- return trackDecodeOperation("latestCompaction", () => this.compactionByStatus(sessionID, "completed"));
1042
- }
1043
- latestRunningCompaction(sessionID) {
1044
- return trackDecodeOperation("latestRunningCompaction", () => this.compactionByStatus(sessionID, "running"));
1045
- }
1046
- idleRows(sessionID, after = -1) {
1047
- return trackDecodeOperation("idleRows", () => this.all(sessionID, after, "idle"));
1048
- }
1049
- earliestSequence(sessionID) {
1050
- const row = this.prepare("SELECT MIN(seq) AS seq FROM session_message WHERE session_id = ?").get(sessionID);
1051
- return typeof row?.seq === "number" ? row.seq : undefined;
1052
- }
1053
- latestSequence(sessionID) {
1054
- const row = this.prepare("SELECT MAX(seq) AS seq FROM session_message WHERE session_id = ?").get(sessionID);
1055
- return typeof row?.seq === "number" ? row.seq : -1;
1056
- }
1057
- assistantSince(sessionID, afterSeq) {
1058
- return trackDecodeOperation("assistantSince", () => {
1059
- const rows = this.prepare(`SELECT id, session_id, type, seq, time_created, data FROM session_message
1060
- WHERE session_id = ? AND type = 'assistant' AND seq > ?
1061
- ORDER BY seq ASC`).all(sessionID, afterSeq);
1062
- return rows.map((row) => decode(row));
1063
- });
1064
- }
1065
- latestAssistant(sessionID) {
1066
- return trackDecodeOperation("latestAssistant", () => {
1067
- const row = this.prepare(`SELECT id, session_id, type, seq, time_created, data FROM session_message
1068
- WHERE session_id = ? AND type = 'assistant'
1069
- ORDER BY seq DESC LIMIT 1`).get(sessionID);
1070
- return row ? decode(row) : undefined;
1071
- });
1072
- }
1073
- latestIdle(sessionID) {
1074
- return trackDecodeOperation("latestIdle", () => {
1075
- const row = this.prepare(`SELECT id, session_id, type, seq, time_created, data FROM session_message
1076
- WHERE session_id = ? AND type = 'idle'
1077
- ORDER BY seq DESC LIMIT 1`).get(sessionID);
1078
- return row ? decode(row) : undefined;
1079
- });
1080
- }
1081
- window(sessionID) {
1082
- return trackDecodeOperation("window", () => this.db.transaction(() => {
1083
- const cut = this.latestCompaction(sessionID);
1084
- return this.all(sessionID, cut ? cut.seq - 1 : -1);
1085
- }).deferred());
1086
- }
1087
- history(sessionID) {
1088
- return trackDecodeOperation("history", () => this.all(sessionID, -1));
1089
- }
1090
- all(sessionID, after, type, through) {
1091
- const rows = [];
1092
- for (;; ) {
1093
- const page = this.page(sessionID, { after, through, type });
1094
- rows.push(...page.rows);
1095
- if (page.rows.length < 100 || page.cursor === undefined)
1096
- return rows;
1097
- after = page.cursor;
1098
- }
1099
- }
1100
- }
1101
-
1102
- // ../plugin/src/features/magic-context/session-activity.ts
1103
- var PREFIX = "retrospective_activity:";
1104
- var INTERVAL_MS = 15000;
1105
- var pending = new WeakMap;
1106
- function sessionActivityKey(sessionId) {
1107
- return `${PREFIX}${sessionId}`;
1108
- }
1109
- function deleteSessionActivity(db, sessionIds) {
1110
- if (sessionIds.length === 0)
1111
- return;
1112
- const sessions = pending.get(db);
1113
- for (const sessionId of sessionIds) {
1114
- const state = sessions?.get(sessionId);
1115
- if (state?.timer)
1116
- clearTimeout(state.timer);
1117
- sessions?.delete(sessionId);
1118
- }
1119
- const placeholders = sessionIds.map(() => "?").join(", ");
1120
- db.prepare(`DELETE FROM schema_migrations_meta WHERE key IN (${placeholders})`).run(...sessionIds.map(sessionActivityKey));
1121
- deleteTagIdentityRevisions(db, sessionIds);
1122
- }
1123
- function readSessionActivity(db, sessionId) {
1124
- const row = db.prepare("SELECT value FROM schema_migrations_meta WHERE key = ?").get(sessionActivityKey(sessionId));
1125
- const time = Number(row?.value);
1126
- return row && Number.isFinite(time) ? time : undefined;
1127
- }
1128
- function advanceSessionActivity(db, sessionId, time) {
1129
- if (!sessionId || !Number.isFinite(time) || time <= 0)
1130
- return;
1131
- db.prepare(`INSERT INTO schema_migrations_meta (key, value) VALUES (?, ?)
1132
- ON CONFLICT(key) DO UPDATE SET value = CAST(MAX(CAST(value AS INTEGER), CAST(excluded.value AS INTEGER)) AS TEXT)`).run(sessionActivityKey(sessionId), String(Math.floor(time)));
1133
- }
1134
- function observeSessionActivity(db, sessionId, time = Date.now()) {
1135
- if (!sessionId || !Number.isFinite(time) || time <= 0)
1136
- return;
1137
- let sessions = pending.get(db);
1138
- if (!sessions) {
1139
- sessions = new Map;
1140
- pending.set(db, sessions);
1141
- }
1142
- const current = sessions.get(sessionId);
1143
- if (!current || time - current.lastWrite >= INTERVAL_MS) {
1144
- if (current?.timer)
1145
- clearTimeout(current.timer);
1146
- advanceSessionActivity(db, sessionId, Math.max(time, current?.latest ?? 0));
1147
- sessions.set(sessionId, { lastWrite: time, latest: time });
1148
- return;
1149
- }
1150
- current.latest = Math.max(current.latest, time);
1151
- if (!current.timer) {
1152
- current.timer = setTimeout(() => {
1153
- current.timer = undefined;
1154
- try {
1155
- advanceSessionActivity(db, sessionId, current.latest);
1156
- current.lastWrite = Date.now();
1157
- } catch {
1158
- sessions.delete(sessionId);
1159
- }
1160
- }, Math.max(1, INTERVAL_MS - (time - current.lastWrite)));
1161
- current.timer.unref?.();
1162
- }
1163
- }
1164
- async function backfillSessionActivity(db, harness, latestTime) {
1165
- const completedKey = `retrospective_activity_backfill:${harness}:v1`;
1166
- if (db.prepare("SELECT 1 FROM schema_migrations_meta WHERE key = ?").get(completedKey))
1167
- return;
1168
- let cursor = "";
1169
- while (true) {
1170
- const rows = db.prepare(`SELECT session_id FROM session_projects
1171
- WHERE harness = ? AND session_id > ? ORDER BY session_id LIMIT 100`).all(harness, cursor);
1172
- if (rows.length === 0)
1173
- break;
1174
- for (const row of rows) {
1175
- const time = await latestTime(row.session_id);
1176
- if (time !== undefined)
1177
- advanceSessionActivity(db, row.session_id, time);
1178
- }
1179
- cursor = rows[rows.length - 1].session_id;
1180
- await new Promise((resolve) => setTimeout(resolve, 0));
1181
- }
1182
- db.prepare("INSERT OR IGNORE INTO schema_migrations_meta (key, value) VALUES (?, 'completed')").run(completedKey);
1183
- }
1184
-
1185
- // ../plugin/src/features/magic-context/storage-session-tables.ts
1186
- var SESSION_SCOPED_TABLES = [
1187
- { table: "pending_ops", harnessScoped: true },
1188
- { table: "source_contents", harnessScoped: true },
1189
- { table: "tool_owner_backfill_state" },
1190
- { table: "tags", harnessScoped: true },
1191
- { table: "session_meta", harnessScoped: true },
1192
- {
1193
- table: "temporal_decisions",
1194
- extraPredicate: "NOT EXISTS (SELECT 1 FROM session_meta AS remaining WHERE remaining.session_id = temporal_decisions.session_id)"
1195
- },
1196
- {
1197
- table: "session_replay_decisions",
1198
- extraPredicate: "NOT EXISTS (SELECT 1 FROM session_meta AS remaining WHERE remaining.session_id = session_replay_decisions.session_id)"
1199
- },
1200
- { table: "session_projects", harnessScoped: true },
1201
- { table: "compartment_chunk_embeddings", harnessScoped: true },
1202
- { table: "compartments", harnessScoped: true },
1203
- {
1204
- table: "compartment_history_versions",
1205
- extraPredicate: "NOT EXISTS (SELECT 1 FROM compartments AS remaining WHERE remaining.session_id = compartment_history_versions.session_id)"
1206
- },
1207
- { table: "compression_depth", harnessScoped: true },
1208
- { table: "session_facts", harnessScoped: true },
1209
- { table: "compartment_state_lease" },
1210
- { table: "notes", harnessScoped: true, extraPredicate: "type = 'session'" },
1211
- { table: "recomp_compartments", harnessScoped: true },
1212
- { table: "recomp_facts", harnessScoped: true },
1213
- { table: "user_memory_candidates" },
1214
- { table: "primer_candidates", harnessScoped: true },
1215
- { table: "m0_mutation_log" },
1216
- { table: "compartment_events", harnessScoped: true },
1217
- { table: "subagent_invocations", harnessScoped: true },
1218
- { table: "historian_runs", harnessScoped: true },
1219
- { table: "plugin_messages" },
1220
- { table: "transform_decisions", harnessScoped: true },
1221
- { table: "synapse_batch_ledger" },
1222
- { table: "embedding_measurement_corpus" },
1223
- { table: "pending_session_cleanup", harnessScoped: true },
1224
- { table: "message_history_fts" },
1225
- { table: "message_fts_rowid_map" },
1226
- { table: "message_history_source", harnessScoped: true },
1227
- { table: "message_history_index", harnessScoped: true },
1228
- { table: "lkg_slots" },
1229
- { table: "lkg_slot_chunks" }
1230
- ];
1231
- var cleanupStatements = new WeakMap;
1232
- function cleanupStatement(db, sql) {
1233
- let cache = cleanupStatements.get(db);
1234
- if (!cache) {
1235
- cache = new Map;
1236
- cleanupStatements.set(db, cache);
1237
- }
1238
- let statement = cache.get(sql);
1239
- if (!statement) {
1240
- if (cache.size >= 256)
1241
- cache.clear();
1242
- statement = prepareCachedStatement2(db, sql);
1243
- cache.set(sql, statement);
1244
- }
1245
- return statement;
1246
- }
1247
- function deleteSessionScopedRows(db, sessionIds, harness, options = {}) {
1248
- if (sessionIds.length === 0)
1249
- return 0;
1250
- let deletableSessionIds = [...sessionIds];
1251
- if (options.rustModuleCleanupAcknowledged !== true) {
1252
- const placeholders = sessionIds.map(() => "?").join(", ");
1253
- const harnessPredicate = harness === undefined ? "" : " AND harness = ?";
1254
- const protectedRows = cleanupStatement(db, `SELECT session_id
1255
- FROM pending_session_cleanup
1256
- WHERE session_id IN (${placeholders})
1257
- AND harness LIKE '%:rust'${harnessPredicate}`).all(...sessionIds, ...harness === undefined ? [] : [`${harness}:rust`]);
1258
- const protectedSessionIds = new Set(protectedRows.map((row) => row.session_id));
1259
- deletableSessionIds = sessionIds.filter((sessionId) => !protectedSessionIds.has(sessionId));
1260
- }
1261
- if (deletableSessionIds.length === 0)
1262
- return 0;
1263
- const placeholders = deletableSessionIds.map(() => "?").join(", ");
1264
- const harnessTables = SESSION_SCOPED_TABLES.filter((definition) => definition.harnessScoped);
1265
- if (options.preserveTagRevisionTombstones) {
1266
- const owners = cleanupStatement(db, harnessTables.map((definition) => `SELECT session_id, harness FROM ${definition.table} WHERE session_id IN (${placeholders})${definition.extraPredicate ? ` AND ${definition.extraPredicate}` : ""}`).join(" UNION ")).all(...harnessTables.flatMap(() => deletableSessionIds));
1267
- const record = cleanupStatement(db, "INSERT OR IGNORE INTO schema_migrations_meta (key, value) VALUES (?, ?)");
1268
- for (const owner of owners)
1269
- record.run(tagTombstoneOwnerKey(owner.session_id, owner.harness), owner.harness);
1270
- const fallback = cleanupStatement(db, `INSERT INTO schema_migrations_meta (key, value)
1271
- SELECT ?, ? WHERE NOT EXISTS (SELECT 1 FROM schema_migrations_meta WHERE key >= ? AND key < ?)`);
1272
- for (const id of deletableSessionIds)
1273
- fallback.run(tagTombstoneOwnerKey(id, getHarness2()), getHarness2(), ...tagTombstoneOwnerRange(id));
1274
- }
1275
- for (const sessionId of deletableSessionIds)
1276
- deleteChunkEmbedBackoffForSession2(db, sessionId);
1277
- for (const definition of SESSION_SCOPED_TABLES) {
1278
- if (definition.table === "message_history_fts") {
1279
- cleanupStatement(db, `DELETE FROM message_history_fts
1280
- WHERE rowid IN (
1281
- SELECT fts_rowid
1282
- FROM message_fts_rowid_map
1283
- WHERE session_id IN (${placeholders})
1284
- )`).run(...deletableSessionIds);
1285
- deleteUnmappedMessageFtsRows2(db, deletableSessionIds);
1286
- continue;
1287
- }
1288
- const predicates = [`session_id IN (${placeholders})`];
1289
- if (definition.extraPredicate)
1290
- predicates.push(definition.extraPredicate);
1291
- const bindHarness = harness !== undefined && definition.harnessScoped === true;
1292
- if (bindHarness)
1293
- predicates.push("harness = ?");
1294
- const statement = cleanupStatement(db, `DELETE FROM ${definition.table} WHERE ${predicates.join(" AND ")}`);
1295
- if (bindHarness)
1296
- statement.run(...deletableSessionIds, harness);
1297
- else
1298
- statement.run(...deletableSessionIds);
1299
- }
1300
- deleteSessionActivity(db, deletableSessionIds);
1301
- if (!options.preserveTagRevisionTombstones) {
1302
- const retireOwner = cleanupStatement(db, harness === undefined ? "DELETE FROM schema_migrations_meta WHERE key >= ? AND key < ?" : "DELETE FROM schema_migrations_meta WHERE key = ?");
1303
- const survivingRows = harnessTables.map((definition) => `EXISTS (SELECT 1 FROM ${definition.table} WHERE session_id = ?${definition.extraPredicate ? ` AND ${definition.extraPredicate}` : ""})`).join(" OR ");
1304
- const retire = cleanupStatement(db, `DELETE FROM schema_migrations_meta WHERE key IN (?, ?)
1305
- AND NOT (${survivingRows})
1306
- AND NOT EXISTS (SELECT 1 FROM schema_migrations_meta AS owners WHERE owners.key >= ? AND owners.key < ?)`);
1307
- for (const id of deletableSessionIds) {
1308
- if (harness === undefined)
1309
- retireOwner.run(...tagTombstoneOwnerRange(id));
1310
- else
1311
- retireOwner.run(tagTombstoneOwnerKey(id, harness));
1312
- retire.run(tagIdentityRevisionKey(id), tagRevisionKey(id), ...harnessTables.map(() => id), ...tagTombstoneOwnerRange(id));
1313
- }
1314
- }
1315
- if (!options.deferRecoveryFenceCleanup)
1316
- for (const sessionId of deletableSessionIds)
1317
- clearLkgRecoveryFence(sessionId);
1318
- return deletableSessionIds.length;
1319
- }
1320
- var SESSION_CLEANUP_SLICE_ROWS = 64;
1321
- function deleteSessionScopedRowsSlice(db, sessionId, harness) {
1322
- if (cleanupStatement(db, "SELECT 1 FROM pending_session_cleanup WHERE session_id = ? AND harness = ?").get(sessionId, `${harness}:rust`))
1323
- return { complete: true, deleted: 0 };
1324
- cleanupStatement(db, "INSERT OR IGNORE INTO schema_migrations_meta(key,value) VALUES (?,?)").run(tagTombstoneOwnerKey(sessionId, harness), harness);
1325
- let remaining = SESSION_CLEANUP_SLICE_ROWS;
1326
- const started = performance.now();
1327
- remaining -= deleteChunkEmbedBackoffForSession2(db, sessionId, remaining);
1328
- if (remaining <= 0)
1329
- return { complete: false, deleted: 0 };
1330
- for (const definition of SESSION_SCOPED_TABLES) {
1331
- if (definition.table === "message_history_fts") {
1332
- const rows = cleanupStatement(db, "SELECT fts_rowid AS id FROM message_fts_rowid_map WHERE session_id = ? LIMIT ?").all(sessionId, remaining);
1333
- const removeFts = cleanupStatement(db, "DELETE FROM message_history_fts WHERE rowid = ?");
1334
- const removeMap = cleanupStatement(db, "DELETE FROM message_fts_rowid_map WHERE session_id = ? AND fts_rowid = ?");
1335
- for (const row of rows) {
1336
- removeFts.run(row.id);
1337
- removeMap.run(sessionId, row.id);
1338
- }
1339
- remaining -= rows.length;
1340
- if (remaining > 0 && !getMessageFtsRowidMapBackfillProgress2(db).completed)
1341
- remaining -= cleanupStatement(db, `DELETE FROM message_history_fts WHERE rowid IN (
1342
- SELECT rowid FROM message_history_fts WHERE session_id = ? LIMIT ?
1343
- )`).run(sessionId, remaining).changes;
1344
- } else {
1345
- const predicates = ["session_id = ?"];
1346
- if (definition.extraPredicate)
1347
- predicates.push(definition.extraPredicate);
1348
- if (definition.harnessScoped)
1349
- predicates.push("harness = ?");
1350
- const args = [sessionId, ...definition.harnessScoped ? [harness] : [], remaining];
1351
- const withoutRowid = definition.table === "temporal_decisions" || definition.table === "session_replay_decisions";
1352
- const key = withoutRowid ? "message_id" : "rowid";
1353
- remaining -= cleanupStatement(db, `DELETE FROM ${definition.table}
1354
- WHERE ${withoutRowid ? "session_id = ? AND " : ""}${key} IN (
1355
- SELECT ${key} FROM ${definition.table} WHERE ${predicates.join(" AND ")} LIMIT ?
1356
- )`).run(...withoutRowid ? [sessionId] : [], ...args).changes;
1357
- }
1358
- if (remaining <= 0 || performance.now() - started >= 10)
1359
- return { complete: false, deleted: 0 };
1360
- }
1361
- return {
1362
- complete: true,
1363
- deleted: deleteSessionScopedRows(db, [sessionId], harness, {
1364
- deferRecoveryFenceCleanup: true
1365
- })
1366
- };
1367
- }
1368
-
1369
- // ../plugin/src/features/magic-context/compression-depth-storage.ts
1370
- var incrementDepthStatements = new WeakMap;
1371
- var totalDepthStatements = new WeakMap;
1372
- var maxDepthStatements = new WeakMap;
1373
- var clearDepthStatements = new WeakMap;
1374
- function getClearDepthStatement(db) {
1375
- let stmt = clearDepthStatements.get(db);
1376
- if (!stmt) {
1377
- stmt = db.prepare("DELETE FROM compression_depth WHERE session_id = ?");
1378
- clearDepthStatements.set(db, stmt);
1379
- }
1380
- return stmt;
1381
- }
1382
- function clearCompressionDepth(db, sessionId) {
1383
- getClearDepthStatement(db).run(sessionId);
1384
- }
1385
- function clearCompressionDepthRange(db, sessionId, startOrdinal, endOrdinal) {
1386
- if (endOrdinal < startOrdinal) {
1387
- return;
1388
- }
1389
- db.prepare("DELETE FROM compression_depth WHERE session_id = ? AND message_ordinal BETWEEN ? AND ?").run(sessionId, startOrdinal, endOrdinal);
1390
- }
1391
-
1392
- // ../plugin/src/features/magic-context/message-index.ts
1393
- import { createHash as createHash4 } from "node:crypto";
1394
-
1395
- // ../plugin/src/shared/search-text.ts
1396
- function removeTaskNotifications(text) {
1397
- return removeNestedBlocks(text, ["task", "task_cohort"]);
1398
- }
1399
- function cleanSearchText(text) {
1400
- return removeNestedBlocks(text, ["system-reminder", "task", "task_cohort"]).replace(/\s+/g, " ").trim();
1401
- }
1402
- var QUERY_STOP_WORDS = new Set("a an and are as at be been by can could did do does for from how i in is it me my of on or our please should that the their them there these they this to us was we were what when where which who why will with would you your".split(" "));
1403
- function terms(text) {
1404
- return [
1405
- ...new Set((text.toLowerCase().match(/[\p{L}\p{N}_]+/gu) ?? []).filter((term) => !QUERY_STOP_WORDS.has(term)))
1406
- ];
1407
- }
1408
- function keywordRelevance(query, content) {
1409
- const needles = terms(query);
1410
- if (needles.length === 0)
1411
- return 0;
1412
- const words = new Set(terms(cleanSearchText(content)));
1413
- return 0.8 * needles.filter((term) => words.has(term)).length / needles.length;
1414
- }
1415
- function matchedSearchSnippet(content, query, cap = 220) {
1416
- const text = cleanSearchText(content);
1417
- const needles = new Set(terms(query));
1418
- const matches = [...text.matchAll(/[\p{L}\p{N}_]+/gu)].filter((match) => needles.has(match[0].toLowerCase()));
1419
- if (matches.length === 0)
1420
- return "";
1421
- let bestStart = matches[0].index;
1422
- let bestCoverage = -1;
1423
- for (const match of matches) {
1424
- const start = Math.max(0, match.index - Math.floor(cap / 5));
1425
- const coverage = keywordRelevance(query, text.slice(start, start + cap));
1426
- if (coverage > bestCoverage) {
1427
- bestStart = start;
1428
- bestCoverage = coverage;
1429
- }
1430
- }
1431
- if (bestStart > 0) {
1432
- const boundary = text.indexOf(" ", bestStart);
1433
- if (boundary >= 0 && boundary < matches[matches.length - 1].index)
1434
- bestStart = boundary + 1;
1435
- }
1436
- const passage = text.slice(bestStart, bestStart + cap).trim();
1437
- return `${bestStart > 0 ? "…" : ""}${passage}${bestStart + cap < text.length ? "…" : ""}`;
1438
- }
1439
-
1440
- // ../plugin/src/features/magic-context/message-index.ts
1441
- var MESSAGE_HISTORY_ORPHAN_SWEEP_BATCH_SIZE = 200;
1442
- var MESSAGE_HISTORY_ORPHAN_SAFETY_AGE_MS = 24 * 60 * 60 * 1000;
1443
- var MESSAGE_HISTORY_ORPHAN_SWEEP_COOLDOWN_MS = 10 * 60 * 1000;
1444
- var MESSAGE_HISTORY_ORPHAN_UNAVAILABLE_REPROBE_MS = 24 * 60 * 60 * 1000;
1445
- var lastIndexedStatements = new WeakMap;
1446
- var insertMessageStatements = new WeakMap;
1447
- var upsertProgressStatements = new WeakMap;
1448
- var upsertDirtyFloorStatements = new WeakMap;
1449
- var deleteFtsStatements = new WeakMap;
1450
- var deleteFtsRangeStatements = new WeakMap;
1451
- var deleteIndexStatements = new WeakMap;
1452
- var countIndexedMessageStatements = new WeakMap;
1453
- var getMessageSourceStatements = new WeakMap;
1454
- var upsertMessageSourceStatements = new WeakMap;
1455
- var deleteMessageSourceStatements = new WeakMap;
1456
- var deleteMessageSourceRangeStatements = new WeakMap;
1457
- var deleteMessageFtsStatements = new WeakMap;
1458
- var deleteFtsMapStatements = new WeakMap;
1459
- var deleteFtsMapRangeStatements = new WeakMap;
1460
- var deleteMessageFtsMapStatements = new WeakMap;
1461
- var MESSAGE_INDEX_MAX_FTS_BYTES2 = 64 * 1024;
1462
- var skipStatements = new WeakMap;
1463
- function skipStatement(db, sql) {
1464
- const statements = skipStatements.get(db) ?? new Map;
1465
- skipStatements.set(db, statements);
1466
- let statement = statements.get(sql);
1467
- if (!statement) {
1468
- statement = db.prepare(sql);
1469
- statements.set(sql, statement);
1470
- }
1471
- return statement;
1472
- }
1473
- var messageIndexSkipKey2 = (sessionId, messageId) => `message_index_skip:${JSON.stringify([sessionId, messageId])}`;
1474
- function deleteMessageIndexSkips(db, sessionId, messageId) {
1475
- if (messageId !== undefined) {
1476
- skipStatement(db, "DELETE FROM schema_migrations_meta WHERE key=?").run(messageIndexSkipKey2(sessionId, messageId));
1477
- } else {
1478
- const prefix = `message_index_skip:${JSON.stringify([sessionId]).slice(0, -1)},`;
1479
- skipStatement(db, "DELETE FROM schema_migrations_meta WHERE substr(key,1,length(?))=?").run(prefix, prefix);
1480
- }
1481
- }
1482
- function normalizeIndexText(text) {
1483
- return text.replace(/\s+/g, " ").trim();
1484
- }
1485
- function getLastIndexedStatement(db) {
1486
- let stmt = lastIndexedStatements.get(db);
1487
- if (!stmt) {
1488
- stmt = db.prepare("SELECT last_indexed_ordinal, dirty_floor_ordinal FROM message_history_index WHERE session_id = ?");
1489
- lastIndexedStatements.set(db, stmt);
1490
- }
1491
- return stmt;
1492
- }
1493
- function getInsertMessageStatement(db) {
1494
- let stmt = insertMessageStatements.get(db);
1495
- if (!stmt) {
1496
- stmt = db.prepare("INSERT INTO message_history_fts (session_id, message_ordinal, message_id, role, content) VALUES (?, ?, ?, ?, ?)");
1497
- insertMessageStatements.set(db, stmt);
1498
- }
1499
- return stmt;
1500
- }
1501
- function getUpsertProgressStatement(db) {
1502
- let stmt = upsertProgressStatements.get(db);
1503
- if (!stmt) {
1504
- stmt = db.prepare("INSERT INTO message_history_index (session_id, last_indexed_ordinal, dirty_floor_ordinal, updated_at, harness) VALUES (?, ?, ?, ?, ?) ON CONFLICT(session_id) DO UPDATE SET last_indexed_ordinal = excluded.last_indexed_ordinal, dirty_floor_ordinal = excluded.dirty_floor_ordinal, updated_at = excluded.updated_at");
1505
- upsertProgressStatements.set(db, stmt);
1506
- }
1507
- return stmt;
1508
- }
1509
- function getUpsertDirtyFloorStatement(db) {
1510
- let stmt = upsertDirtyFloorStatements.get(db);
1511
- if (!stmt) {
1512
- stmt = db.prepare("INSERT INTO message_history_index (session_id, last_indexed_ordinal, dirty_floor_ordinal, updated_at, harness) VALUES (?, ?, ?, ?, ?) ON CONFLICT(session_id) DO UPDATE SET last_indexed_ordinal = MAX(message_history_index.last_indexed_ordinal, excluded.last_indexed_ordinal), dirty_floor_ordinal = CASE WHEN message_history_index.dirty_floor_ordinal <= 0 THEN excluded.dirty_floor_ordinal WHEN excluded.dirty_floor_ordinal <= 0 THEN message_history_index.dirty_floor_ordinal ELSE MIN(message_history_index.dirty_floor_ordinal, excluded.dirty_floor_ordinal) END, updated_at = excluded.updated_at");
1513
- upsertDirtyFloorStatements.set(db, stmt);
1514
- }
1515
- return stmt;
1516
- }
1517
- function getDeleteFtsStatement(db) {
1518
- let stmt = deleteFtsStatements.get(db);
1519
- if (!stmt) {
1520
- stmt = db.prepare(`DELETE FROM message_history_fts
1521
- WHERE rowid IN (
1522
- SELECT fts_rowid FROM message_fts_rowid_map WHERE session_id = ?
1523
- )`);
1524
- deleteFtsStatements.set(db, stmt);
1525
- }
1526
- return stmt;
1527
- }
1528
- function getDeleteFtsRangeStatement(db) {
1529
- let stmt = deleteFtsRangeStatements.get(db);
1530
- if (!stmt) {
1531
- stmt = db.prepare(`DELETE FROM message_history_fts
1532
- WHERE rowid IN (
1533
- SELECT fts_rowid
1534
- FROM message_fts_rowid_map
1535
- WHERE session_id = ? AND message_ordinal BETWEEN ? AND ?
1536
- )`);
1537
- deleteFtsRangeStatements.set(db, stmt);
1538
- }
1539
- return stmt;
1540
- }
1541
- function getDeleteIndexStatement(db) {
1542
- let stmt = deleteIndexStatements.get(db);
1543
- if (!stmt) {
1544
- stmt = db.prepare("DELETE FROM message_history_index WHERE session_id = ?");
1545
- deleteIndexStatements.set(db, stmt);
1546
- }
1547
- return stmt;
1548
- }
1549
- function getCountIndexedMessageStatement(db) {
1550
- let stmt = countIndexedMessageStatements.get(db);
1551
- if (!stmt) {
1552
- stmt = db.prepare(`SELECT COUNT(*) AS count
1553
- FROM message_history_source AS source
1554
- JOIN message_fts_rowid_map AS map
1555
- ON map.session_id = source.session_id
1556
- AND map.message_ordinal = source.message_ordinal
1557
- WHERE source.session_id = ? AND source.message_id = ?`);
1558
- countIndexedMessageStatements.set(db, stmt);
1559
- }
1560
- return stmt;
1561
- }
1562
- function getMessageSourceStatement(db) {
1563
- let stmt = getMessageSourceStatements.get(db);
1564
- if (!stmt) {
1565
- stmt = db.prepare("SELECT message_ordinal, source_version, normalized_content_hash, role FROM message_history_source WHERE session_id = ? AND message_id = ?");
1566
- getMessageSourceStatements.set(db, stmt);
1567
- }
1568
- return stmt;
1569
- }
1570
- function getUpsertMessageSourceStatement(db) {
1571
- let stmt = upsertMessageSourceStatements.get(db);
1572
- if (!stmt) {
1573
- stmt = db.prepare(`INSERT INTO message_history_source (
1574
- session_id, message_id, message_ordinal, source_version,
1575
- normalized_content_hash, role, harness, updated_at
1576
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
1577
- ON CONFLICT(session_id, message_id) DO UPDATE SET
1578
- message_ordinal = excluded.message_ordinal,
1579
- source_version = excluded.source_version,
1580
- normalized_content_hash = excluded.normalized_content_hash,
1581
- role = excluded.role,
1582
- harness = excluded.harness,
1583
- updated_at = excluded.updated_at`);
1584
- upsertMessageSourceStatements.set(db, stmt);
1585
- }
1586
- return stmt;
1587
- }
1588
- function getDeleteMessageSourceStatement(db) {
1589
- let stmt = deleteMessageSourceStatements.get(db);
1590
- if (!stmt) {
1591
- stmt = db.prepare("DELETE FROM message_history_source WHERE session_id = ?");
1592
- deleteMessageSourceStatements.set(db, stmt);
1593
- }
1594
- return stmt;
1595
- }
1596
- function getDeleteMessageSourceRangeStatement(db) {
1597
- let stmt = deleteMessageSourceRangeStatements.get(db);
1598
- if (!stmt) {
1599
- stmt = db.prepare("DELETE FROM message_history_source WHERE session_id = ? AND message_ordinal BETWEEN ? AND ?");
1600
- deleteMessageSourceRangeStatements.set(db, stmt);
1601
- }
1602
- return stmt;
1603
- }
1604
- function getDeleteMessageFtsStatement(db) {
1605
- let stmt = deleteMessageFtsStatements.get(db);
1606
- if (!stmt) {
1607
- stmt = db.prepare(`DELETE FROM message_history_fts
1608
- WHERE rowid IN (
1609
- SELECT map.fts_rowid
1610
- FROM message_history_source AS source
1611
- JOIN message_fts_rowid_map AS map
1612
- ON map.session_id = source.session_id
1613
- AND map.message_ordinal = source.message_ordinal
1614
- WHERE source.session_id = ? AND source.message_id = ?
1615
- )`);
1616
- deleteMessageFtsStatements.set(db, stmt);
1617
- }
1618
- return stmt;
1619
- }
1620
- function getDeleteFtsMapStatement(db) {
1621
- let stmt = deleteFtsMapStatements.get(db);
1622
- if (!stmt) {
1623
- stmt = db.prepare("DELETE FROM message_fts_rowid_map WHERE session_id = ?");
1624
- deleteFtsMapStatements.set(db, stmt);
1625
- }
1626
- return stmt;
1627
- }
1628
- function getDeleteFtsMapRangeStatement(db) {
1629
- let stmt = deleteFtsMapRangeStatements.get(db);
1630
- if (!stmt) {
1631
- stmt = db.prepare("DELETE FROM message_fts_rowid_map WHERE session_id = ? AND message_ordinal BETWEEN ? AND ?");
1632
- deleteFtsMapRangeStatements.set(db, stmt);
1633
- }
1634
- return stmt;
1635
- }
1636
- function getDeleteMessageFtsMapStatement(db) {
1637
- let stmt = deleteMessageFtsMapStatements.get(db);
1638
- if (!stmt) {
1639
- stmt = db.prepare(`DELETE FROM message_fts_rowid_map
1640
- WHERE session_id = ?
1641
- AND message_ordinal = (
1642
- SELECT message_ordinal
1643
- FROM message_history_source
1644
- WHERE session_id = ? AND message_id = ?
1645
- )`);
1646
- deleteMessageFtsMapStatements.set(db, stmt);
1647
- }
1648
- return stmt;
1649
- }
1650
- function insertMessageFtsRow(db, sessionId, messageOrdinal, messageId, role, content, messageTimeMs) {
1651
- const result = getInsertMessageStatement(db).run(sessionId, messageOrdinal, messageId, role, content);
1652
- recordMessageFtsRowid2(db, sessionId, messageOrdinal, result.lastInsertRowid, messageTimeMs ?? null);
1653
- }
1654
- function normalizeSourceVersion(version) {
1655
- if (typeof version === "number")
1656
- return `number:${version}`;
1657
- if (typeof version === "string")
1658
- return `string:${version}`;
1659
- return "null";
1660
- }
1661
- function toMessageIndexSource2(message) {
1662
- const content = getIndexableContent2(message.role, message.parts);
1663
- return {
1664
- id: message.id,
1665
- ordinal: message.ordinal,
1666
- role: message.role,
1667
- createdAt: message.createdAt,
1668
- sourceVersion: normalizeSourceVersion(message.version),
1669
- contentHash: createHash4("sha256").update(content).digest("hex"),
1670
- content
1671
- };
1672
- }
1673
- function getMessageSourceSnapshot(message) {
1674
- return toMessageIndexSource2(message);
1675
- }
1676
- function getMessageIndexSourceIdentity2(message) {
1677
- const source = getMessageSourceSnapshot(message);
1678
- return JSON.stringify([
1679
- source.ordinal,
1680
- source.sourceVersion,
1681
- source.contentHash,
1682
- source.role,
1683
- message.createdAt ?? null
1684
- ]);
1685
- }
1686
- function isMessageIndexSourceCurrent2(db, sessionId, message) {
1687
- const source = getMessageSourceSnapshot(message);
1688
- return isIndexSourceCurrent(db, sessionId, source);
1689
- }
1690
- function isIndexSourceCurrent(db, sessionId, source) {
1691
- const row = getMessageSourceStatement(db).get(sessionId, source.id);
1692
- return row?.message_ordinal === source.ordinal && row.source_version === source.sourceVersion && row.normalized_content_hash === source.contentHash && row.role === source.role;
1693
- }
1694
- function writeMessageSource(db, sessionId, source, now) {
1695
- getUpsertMessageSourceStatement(db).run(sessionId, source.id, source.ordinal, source.sourceVersion, source.contentHash, source.role, getHarness2(), now);
1696
- const bytes = Buffer.byteLength(source.content);
1697
- if (source.ftsSkippedBytes !== undefined || bytes > MESSAGE_INDEX_MAX_FTS_BYTES2) {
1698
- skipStatement(db, "INSERT INTO schema_migrations_meta(key,value) VALUES (?,?) ON CONFLICT(key) DO UPDATE SET value=excluded.value").run(messageIndexSkipKey2(sessionId, source.id), JSON.stringify({
1699
- reason: "oversized",
1700
- bytes: source.ftsSkippedBytes ?? bytes,
1701
- contentHash: source.contentHash,
1702
- ordinal: source.ordinal,
1703
- complete: true
1704
- }));
1705
- return "";
1706
- }
1707
- skipStatement(db, "DELETE FROM schema_migrations_meta WHERE key=?").run(messageIndexSkipKey2(sessionId, source.id));
1708
- return source.content;
1709
- }
1710
- function getLastIndexedOrdinal2(db, sessionId) {
1711
- const row = getLastIndexedStatement(db).get(sessionId);
1712
- return typeof row?.last_indexed_ordinal === "number" ? row.last_indexed_ordinal : 0;
1713
- }
1714
- function getIndexedMessageCorpusSize2(db, sessionId, maxOrdinal) {
1715
- const watermark = getLastIndexedOrdinal2(db, sessionId);
1716
- return maxOrdinal === null ? watermark : Math.min(watermark, Math.max(0, maxOrdinal));
1717
- }
1718
- function getDirtyIndexFloor(db, sessionId) {
1719
- const row = getLastIndexedStatement(db).get(sessionId);
1720
- return typeof row?.dirty_floor_ordinal === "number" && row.dirty_floor_ordinal > 0 ? row.dirty_floor_ordinal : null;
1721
- }
1722
- function markMessageIndexDirty2(db, sessionId, floorOrdinal) {
1723
- const dirtyFloor = Math.max(1, Math.floor(floorOrdinal));
1724
- getUpsertDirtyFloorStatement(db).run(sessionId, getLastIndexedOrdinal2(db, sessionId), dirtyFloor, Date.now(), getHarness2());
1725
- }
1726
- function isMessageAlreadyIndexed(db, sessionId, messageId) {
1727
- const row = getCountIndexedMessageStatement(db).get(sessionId, messageId);
1728
- return (typeof row?.count === "number" ? row.count : 0) > 0;
1729
- }
1730
- function setIndexProgress(db, sessionId, watermark, dirtyFloor, now) {
1731
- getUpsertProgressStatement(db).run(sessionId, Math.max(0, Math.floor(watermark)), dirtyFloor ?? 0, now, getHarness2());
1732
- }
1733
- function getMessageIndexReconciliationStartOrdinal2(db, sessionId) {
1734
- const watermark = getLastIndexedOrdinal2(db, sessionId);
1735
- const dirtyFloor = getDirtyIndexFloor(db, sessionId);
1736
- return dirtyFloor === null ? watermark : Math.min(watermark, dirtyFloor - 1);
1737
- }
1738
- function isMessageIndexReconciledThrough2(db, sessionId, finalWatermark) {
1739
- const dirtyFloor = getDirtyIndexFloor(db, sessionId);
1740
- return getLastIndexedOrdinal2(db, sessionId) >= finalWatermark && dirtyFloor === null;
1741
- }
1742
- function deleteIndexedMessage2(db, sessionId, messageId) {
1743
- deleteMessageIndexSkips(db, sessionId, messageId);
1744
- const row = getCountIndexedMessageStatement(db).get(sessionId, messageId);
1745
- const count = typeof row?.count === "number" ? row.count : 0;
1746
- clearIndexedMessages2(db, sessionId);
1747
- return count;
1748
- }
1749
- function clearIndexedMessagesInTransaction2(db, sessionId) {
1750
- deleteMessageIndexSkips(db, sessionId);
1751
- getDeleteFtsStatement(db).run(sessionId);
1752
- deleteUnmappedMessageFtsRows2(db, [sessionId]);
1753
- getDeleteFtsMapStatement(db).run(sessionId);
1754
- getDeleteMessageSourceStatement(db).run(sessionId);
1755
- getDeleteIndexStatement(db).run(sessionId);
1756
- clearCompressionDepth(db, sessionId);
1757
- }
1758
- function clearIndexedMessages2(db, sessionId) {
1759
- let transactionStartedAt = 0;
1760
- db.transaction(() => {
1761
- transactionStartedAt = performance.now();
1762
- clearIndexedMessagesInTransaction2(db, sessionId);
1763
- }).immediate();
1764
- logSlowWriteTransaction("message_index_clear", transactionStartedAt);
1765
- }
1766
- function getIndexableContent2(role, parts) {
1767
- if (role === "user") {
1768
- if (!hasMeaningfulUserText2(parts)) {
1769
- return "";
1770
- }
1771
- return extractTexts2(parts).map(removeTaskNotifications).map(cleanUserText2).map(normalizeIndexText).filter((text) => text.length > 0).join(" / ");
1772
- }
1773
- if (role === "assistant") {
1774
- return extractTexts2(parts).map(removeTaskNotifications).map(removeSystemReminders).map(normalizeIndexText).filter((text) => text.length > 0).join(" / ");
1775
- }
1776
- return "";
1777
- }
1778
- function indexSingleMessageInTransaction(db, sessionId, message, now, dirtyFloorBeforeAttempt, source) {
1779
- const currentWatermark = getLastIndexedOrdinal2(db, sessionId);
1780
- const dirtyFloor = getDirtyIndexFloor(db, sessionId);
1781
- if (message.ordinal <= currentWatermark) {
1782
- if (isIndexSourceCurrent(db, sessionId, source)) {
1783
- recordIndexedMessageTime2(db, sessionId, message.ordinal, message.createdAt);
1784
- return false;
1785
- }
1786
- if (!messageFtsOrdinalRangeIsMapped2(db, sessionId, message.ordinal, message.ordinal)) {
1787
- return false;
1788
- }
1789
- getDeleteMessageFtsStatement(db).run(sessionId, message.id);
1790
- getDeleteMessageFtsMapStatement(db).run(sessionId, sessionId, message.id);
1791
- const content = writeMessageSource(db, sessionId, source, now);
1792
- if (content.length > 0 && (message.role === "user" || message.role === "assistant")) {
1793
- insertMessageFtsRow(db, sessionId, message.ordinal, message.id, message.role, content, message.createdAt);
1794
- }
1795
- setIndexProgress(db, sessionId, currentWatermark, dirtyFloorBeforeAttempt === message.ordinal ? null : dirtyFloorBeforeAttempt, now);
1796
- return true;
1797
- }
1798
- if (message.ordinal !== currentWatermark + 1 || dirtyFloor !== null && dirtyFloor !== message.ordinal) {
1799
- return false;
1800
- }
1801
- const content = writeMessageSource(db, sessionId, source, now);
1802
- let inserted = false;
1803
- if (content.length > 0 && (message.role === "user" || message.role === "assistant") && !isMessageAlreadyIndexed(db, sessionId, message.id)) {
1804
- insertMessageFtsRow(db, sessionId, message.ordinal, message.id, message.role, content, message.createdAt);
1805
- inserted = true;
1806
- }
1807
- setIndexProgress(db, sessionId, message.ordinal, dirtyFloorBeforeAttempt === message.ordinal ? null : dirtyFloorBeforeAttempt, now);
1808
- return inserted;
1809
- }
1810
- function indexSingleMessage2(db, sessionId, message, shouldCommit = () => true, preparedSource) {
1811
- if (!shouldCommit())
1812
- return false;
1813
- const source = preparedSource ?? getMessageSourceSnapshot(message);
1814
- const currentWatermark = getLastIndexedOrdinal2(db, sessionId);
1815
- if (message.ordinal <= currentWatermark && isIndexSourceCurrent(db, sessionId, source)) {
1816
- recordIndexedMessageTime2(db, sessionId, message.ordinal, message.createdAt);
1817
- return false;
1818
- }
1819
- const dirtyFloorBeforeAttempt = getDirtyIndexFloor(db, sessionId);
1820
- markMessageIndexDirty2(db, sessionId, Math.min(message.ordinal, currentWatermark + 1));
1821
- db.exec("BEGIN IMMEDIATE");
1822
- const transactionStartedAt = performance.now();
1823
- let committed = false;
1824
- try {
1825
- const result = indexSingleMessageInTransaction(db, sessionId, message, Date.now(), dirtyFloorBeforeAttempt, source);
1826
- if (!shouldCommit())
1827
- return false;
1828
- db.exec("COMMIT");
1829
- committed = true;
1830
- logSlowWriteTransaction("message_index_incremental", transactionStartedAt);
1831
- return result;
1832
- } finally {
1833
- if (!committed) {
1834
- try {
1835
- db.exec("ROLLBACK");
1836
- } catch {}
1837
- }
1838
- }
1839
- }
1840
- function indexMessagesAfterOrdinal2(db, sessionId, messages, _lastIndexedOrdinal, finalWatermark = messages.length) {
1841
- return indexItemsAfterOrdinal(db, sessionId, messages, finalWatermark, toMessageIndexSource2);
1842
- }
1843
- function indexSourcesAfterOrdinal2(db, sessionId, sources, finalWatermark) {
1844
- return indexItemsAfterOrdinal(db, sessionId, sources, finalWatermark, (source) => source);
1845
- }
1846
- function indexItemsAfterOrdinal(db, sessionId, items, finalWatermark, toSource) {
1847
- const now = Date.now();
1848
- let inserted = 0;
1849
- const sources = new Map(items.map((item) => [item.ordinal, toSource(item)]));
1850
- db.exec("BEGIN IMMEDIATE");
1851
- const transactionStartedAt = performance.now();
1852
- let committed = false;
1853
- try {
1854
- const currentWatermark = getLastIndexedOrdinal2(db, sessionId);
1855
- const dirtyFloor = getDirtyIndexFloor(db, sessionId);
1856
- const effectiveWatermark = dirtyFloor === null ? currentWatermark : Math.min(currentWatermark, Math.max(0, dirtyFloor - 1));
1857
- if (dirtyFloor !== null && dirtyFloor <= currentWatermark && !messageFtsOrdinalRangeIsMapped2(db, sessionId, dirtyFloor, Math.min(currentWatermark, finalWatermark))) {
1858
- db.exec("COMMIT");
1859
- committed = true;
1860
- logSlowWriteTransaction("message_index_reconcile", transactionStartedAt);
1861
- return 0;
1862
- }
1863
- if (dirtyFloor !== null && dirtyFloor <= finalWatermark) {
1864
- getDeleteFtsRangeStatement(db).run(sessionId, dirtyFloor, finalWatermark);
1865
- getDeleteFtsMapRangeStatement(db).run(sessionId, dirtyFloor, finalWatermark);
1866
- getDeleteMessageSourceRangeStatement(db).run(sessionId, dirtyFloor, finalWatermark);
1867
- }
1868
- const itemsByOrdinal = new Map;
1869
- for (const item of items) {
1870
- if (item.ordinal > effectiveWatermark && item.ordinal <= finalWatermark) {
1871
- itemsByOrdinal.set(item.ordinal, item);
1872
- }
1873
- }
1874
- let coveredWatermark = effectiveWatermark;
1875
- while (coveredWatermark < finalWatermark && itemsByOrdinal.has(coveredWatermark + 1)) {
1876
- coveredWatermark += 1;
1877
- }
1878
- for (let ordinal = effectiveWatermark + 1;ordinal <= coveredWatermark; ordinal++) {
1879
- const item = itemsByOrdinal.get(ordinal);
1880
- if (!item)
1881
- continue;
1882
- const message = sources.get(ordinal);
1883
- if (!message)
1884
- continue;
1885
- const content = writeMessageSource(db, sessionId, message, now);
1886
- if (content.length === 0 || message.role !== "user" && message.role !== "assistant" || isMessageAlreadyIndexed(db, sessionId, message.id)) {
1887
- continue;
1888
- }
1889
- insertMessageFtsRow(db, sessionId, message.ordinal, message.id, message.role, content, message.createdAt);
1890
- inserted += 1;
1891
- }
1892
- const missingFloor = coveredWatermark < finalWatermark ? coveredWatermark + 1 : null;
1893
- const preservedFloor = dirtyFloor !== null && dirtyFloor > finalWatermark ? dirtyFloor : null;
1894
- const nextDirtyFloor = missingFloor === null ? preservedFloor : preservedFloor === null ? missingFloor : Math.min(missingFloor, preservedFloor);
1895
- setIndexProgress(db, sessionId, coveredWatermark, nextDirtyFloor, now);
1896
- db.exec("COMMIT");
1897
- committed = true;
1898
- logSlowWriteTransaction("message_index_reconcile", transactionStartedAt);
1899
- } finally {
1900
- if (!committed) {
1901
- try {
1902
- db.exec("ROLLBACK");
1903
- } catch {}
1904
- }
1905
- }
1906
- return inserted;
1907
- }
1908
- function harnessSupportsOpenCodeOrphanSweep(harness) {
1909
- return harness === "opencode" || harness === "opencode2";
1910
- }
1911
- function getMessageHistoryOrphanSweepState(db, harness) {
1912
- const state = db.prepare("SELECT cursor_session_id, last_swept_at FROM message_history_orphan_sweep WHERE harness = ?").get(harness) ?? {};
1913
- if (harness === "opencode2") {
1914
- if (!state.cursor_session_id?.startsWith("session_v2:"))
1915
- return {};
1916
- return { ...state, cursor_session_id: state.cursor_session_id.slice("session_v2:".length) };
1917
- }
1918
- return state;
1919
- }
1920
- function persistMessageHistoryOrphanSweepState(db, cursor, lastSweptAt, harness) {
1921
- db.prepare(`INSERT INTO message_history_orphan_sweep (harness, cursor_session_id, last_swept_at)
1922
- VALUES (?, ?, ?)
1923
- ON CONFLICT(harness) DO UPDATE SET
1924
- cursor_session_id = excluded.cursor_session_id,
1925
- last_swept_at = excluded.last_swept_at`).run(harness, harness === "opencode2" ? `session_v2:${cursor}` : cursor, lastSweptAt);
1926
- }
1927
- function getOpenCodeSessionScopedCandidateSourceSql(harness) {
1928
- const sources = SESSION_SCOPED_TABLES.filter((definition) => definition.harnessScoped === true).map((definition) => {
1929
- const predicates = ["session_id IS NOT NULL", `harness = '${harness}'`];
1930
- if (definition.extraPredicate)
1931
- predicates.push(definition.extraPredicate);
1932
- return `SELECT session_id FROM ${definition.table} WHERE ${predicates.join(" AND ")}`;
1933
- });
1934
- const jsonId = `SUBSTR(key, ${TAG_TOMBSTONE_OWNER_PREFIX.length + 1}, LENGTH(key) - ${TAG_TOMBSTONE_OWNER_PREFIX.length + harness.length + 1})`;
1935
- sources.push(`SELECT CASE WHEN json_valid(${jsonId}) THEN
1936
- CASE WHEN json_type(${jsonId}) = 'text' THEN json_extract(${jsonId}, '$') END END AS session_id
1937
- FROM schema_migrations_meta WHERE value = '${harness}'
1938
- AND key >= '${TAG_TOMBSTONE_OWNER_PREFIX}' AND key < '${TAG_TOMBSTONE_OWNER_PREFIX.slice(0, -1)};'
1939
- AND SUBSTR(key, -${harness.length + 1}) = ':${harness}'`);
1940
- return sources.join(`
1941
- UNION
1942
- `);
1943
- }
1944
- function* sweepOrphanedOpenCodeMessageIndexesInBackground(db, openReadableOpenCodeDb, options = {}) {
1945
- const now = options.now ?? Date.now();
1946
- const batchSize = Math.max(1, Math.floor(options.batchSize ?? MESSAGE_HISTORY_ORPHAN_SWEEP_BATCH_SIZE));
1947
- const safetyAgeMs = Math.max(0, options.safetyAgeMs ?? MESSAGE_HISTORY_ORPHAN_SAFETY_AGE_MS);
1948
- const cooldownMs = Math.max(0, options.cooldownMs ?? MESSAGE_HISTORY_ORPHAN_SWEEP_COOLDOWN_MS);
1949
- const unavailableReprobeMs = Math.max(cooldownMs, options.unavailableReprobeMs ?? MESSAGE_HISTORY_ORPHAN_UNAVAILABLE_REPROBE_MS);
1950
- const harness = getHarness2();
1951
- const state = getMessageHistoryOrphanSweepState(db, harness);
1952
- const cursor = typeof state.cursor_session_id === "string" ? state.cursor_session_id : "";
1953
- if (typeof state.last_swept_at === "number" && state.last_swept_at + cooldownMs > now) {
1954
- return { status: "cooldown", scanned: 0, deleted: 0, cursor };
1955
- }
1956
- if (!harnessSupportsOpenCodeOrphanSweep(harness)) {
1957
- persistMessageHistoryOrphanSweepState(db, cursor, now + unavailableReprobeMs - cooldownMs, harness);
1958
- return { status: "unavailable", scanned: 0, deleted: 0, cursor };
1959
- }
1960
- let openCodeDb = null;
1961
- try {
1962
- openCodeDb = openReadableOpenCodeDb();
1963
- } catch {
1964
- openCodeDb = null;
1965
- }
1966
- if (!openCodeDb) {
1967
- persistMessageHistoryOrphanSweepState(db, cursor, now + unavailableReprobeMs - cooldownMs, harness);
1968
- return { status: "source_unavailable", scanned: 0, deleted: 0, cursor };
1969
- }
1970
- let v2Reader;
1971
- try {
1972
- let sessionExists;
1973
- let liveIds;
1974
- if (harness === "opencode2") {
1975
- try {
1976
- const path = getSqliteDatabasePath2(openCodeDb);
1977
- if (!path)
1978
- throw new Error("OpenCode 2 source has no durable path");
1979
- v2Reader = new V2StoreReader(path);
1980
- liveIds = new Set(v2Reader.sessionIds());
1981
- if (liveIds.size === 0)
1982
- throw new Error("OpenCode 2 source has no sessions");
1983
- sessionExists = (id) => liveIds.has(id);
1984
- } catch {
1985
- persistMessageHistoryOrphanSweepState(db, cursor, now + unavailableReprobeMs - cooldownMs, harness);
1986
- return { status: "source_unavailable", scanned: 0, deleted: 0, cursor };
1987
- }
1988
- } else {
1989
- liveIds = new Set(openCodeDb.prepare("SELECT id FROM session").all().map((row) => row.id));
1990
- const query = openCodeDb.prepare("SELECT 1 FROM session WHERE id = ? LIMIT 1");
1991
- sessionExists = (id) => !!query.get(id);
1992
- }
1993
- if (options.fenceLockNames?.length) {
1994
- const retainedIds = new Set(liveIds);
1995
- const sources = SESSION_SCOPED_TABLES.map((def) => `SELECT session_id FROM ${def.table}${def.extraPredicate ? ` WHERE ${def.extraPredicate}` : ""}`);
1996
- for (const row of db.prepare(sources.join(" UNION ")).all()) {
1997
- if (typeof row.session_id === "string")
1998
- retainedIds.add(row.session_id);
1999
- }
2000
- for (const row of db.prepare("SELECT key FROM schema_migrations_meta WHERE key >= ? AND key < ?").all(TAG_TOMBSTONE_OWNER_PREFIX, "tag_revision_owner;")) {
2001
- try {
2002
- const id = JSON.parse(row.key.slice(TAG_TOMBSTONE_OWNER_PREFIX.length, row.key.lastIndexOf(":")));
2003
- if (typeof id === "string")
2004
- retainedIds.add(id);
2005
- } catch {}
2006
- }
2007
- sweepEmptyLkgRecoveryLockDirectories(options.fenceLockNames, retainedIds);
2008
- }
2009
- const cutoff = now - safetyAgeMs;
2010
- const candidateSourceSql = getOpenCodeSessionScopedCandidateSourceSql(harness);
2011
- const candidates = db.prepare(`SELECT session_id
2012
- FROM (${candidateSourceSql}) AS session_candidates
2013
- WHERE session_id > ?
2014
- AND NOT EXISTS (
2015
- SELECT 1
2016
- FROM message_history_index
2017
- WHERE message_history_index.session_id = session_candidates.session_id
2018
- AND message_history_index.harness = '${harness}'
2019
- AND message_history_index.updated_at > ?
2020
- )
2021
- ORDER BY session_id ASC
2022
- LIMIT ?`).all(cursor, cutoff, batchSize);
2023
- const missingSessionIds = candidates.filter((candidate) => !sessionExists(candidate.session_id)).map((candidate) => candidate.session_id);
2024
- const nextCursor = candidates.length < batchSize ? "" : candidates[candidates.length - 1]?.session_id ?? cursor;
2025
- const completedAt = candidates.length < batchSize ? now : null;
2026
- let deleted = 0;
2027
- let slices = 0;
2028
- const stillEligible = db.prepare(`SELECT 1 WHERE NOT EXISTS (
2029
- SELECT 1 FROM message_history_index
2030
- WHERE session_id = ? AND harness = '${harness}' AND updated_at > ?
2031
- )`);
2032
- for (const sessionId of missingSessionIds) {
2033
- let complete = false;
2034
- do {
2035
- let removed = 0;
2036
- db.exec("BEGIN IMMEDIATE");
2037
- const transactionStartedAt = performance.now();
2038
- let committed = false;
2039
- try {
2040
- const hostStillMissing = v2Reader ? !v2Reader.sessionExists(sessionId) : !sessionExists(sessionId);
2041
- if (hostStillMissing && stillEligible.get(sessionId, cutoff)) {
2042
- const slice = deleteSessionScopedRowsSlice(db, sessionId, harness);
2043
- removed = slice.deleted;
2044
- complete = slice.complete;
2045
- } else
2046
- complete = true;
2047
- db.exec("COMMIT");
2048
- committed = true;
2049
- logSlowWriteTransaction("message_index_orphan_sweep", transactionStartedAt);
2050
- } finally {
2051
- if (!committed) {
2052
- try {
2053
- db.exec("ROLLBACK");
2054
- } catch {}
2055
- }
2056
- }
2057
- deleted += removed;
2058
- if (removed)
2059
- clearLkgRecoveryFence(sessionId);
2060
- yield;
2061
- if (++slices >= Math.max(1, options.maxSlices ?? 512)) {
2062
- return {
2063
- status: "swept",
2064
- scanned: candidates.length,
2065
- deleted,
2066
- cursor,
2067
- partial: true
2068
- };
2069
- }
2070
- } while (!complete);
2071
- }
2072
- persistMessageHistoryOrphanSweepState(db, nextCursor, completedAt, harness);
2073
- return {
2074
- status: "swept",
2075
- scanned: candidates.length,
2076
- deleted,
2077
- cursor: nextCursor
2078
- };
2079
- } finally {
2080
- v2Reader?.close();
2081
- closeQuietly(openCodeDb);
2082
- }
2083
- }
2084
- async function sweepOrphanedOpenCodeMessageIndexesCooperatively2(db, openSource, options = {}) {
2085
- return withoutSqliteTransformPass2(() => withSqliteBackgroundWriter2(async () => {
2086
- const sweep = sweepOrphanedOpenCodeMessageIndexesInBackground(db, openSource, options);
2087
- try {
2088
- for (;; ) {
2089
- const step = sweep.next();
2090
- if (step.done)
2091
- return step.value;
2092
- await new Promise((resolve) => setTimeout(resolve, 0));
2093
- }
2094
- } finally {
2095
- sweep.return({ status: "source_unavailable", scanned: 0, deleted: 0, cursor: "" });
2096
- }
2097
- }));
2098
- }
2099
-
2100
- export { LkgRecoveryFenceError, clearLkgFenceProcessState, takeLkgRecoveryLockSweepPage, lkgFenceRequiresRebuild, lkgRecoveryFenceUnclean, ownsLkgDirtyFence, invalidateCleanLkgRecoveryFence, markLkgRecoveryDirty, commitLkgWithRecoveryFence, clearLkgRecoveryFence, gaDatabasePath, V2StoreReader, readSessionActivity, observeSessionActivity, backfillSessionActivity, deleteSessionScopedRows, clearCompressionDepth, clearCompressionDepthRange, cleanSearchText, keywordRelevance, matchedSearchSnippet, MESSAGE_INDEX_MAX_FTS_BYTES2, messageIndexSkipKey2, toMessageIndexSource2, getMessageIndexSourceIdentity2, isMessageIndexSourceCurrent2, getLastIndexedOrdinal2, getIndexedMessageCorpusSize2, markMessageIndexDirty2, getMessageIndexReconciliationStartOrdinal2, isMessageIndexReconciledThrough2, deleteIndexedMessage2, clearIndexedMessagesInTransaction2, clearIndexedMessages2, getIndexableContent2, indexSingleMessage2, indexMessagesAfterOrdinal2, indexSourcesAfterOrdinal2, sweepOrphanedOpenCodeMessageIndexesCooperatively2 };