@martian-engineering/lossless-claw 0.8.0 → 0.8.1

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 (52) hide show
  1. package/README.md +8 -0
  2. package/dist/index.js +19240 -0
  3. package/docs/configuration.md +15 -5
  4. package/openclaw.plugin.json +27 -3
  5. package/package.json +7 -6
  6. package/skills/lossless-claw/references/config.md +37 -0
  7. package/index.ts +0 -2
  8. package/src/assembler.ts +0 -1196
  9. package/src/compaction.ts +0 -1753
  10. package/src/db/config.ts +0 -345
  11. package/src/db/connection.ts +0 -151
  12. package/src/db/features.ts +0 -61
  13. package/src/db/migration.ts +0 -868
  14. package/src/engine.ts +0 -4486
  15. package/src/estimate-tokens.ts +0 -80
  16. package/src/expansion-auth.ts +0 -365
  17. package/src/expansion-policy.ts +0 -303
  18. package/src/expansion.ts +0 -383
  19. package/src/integrity.ts +0 -600
  20. package/src/large-files.ts +0 -546
  21. package/src/lcm-log.ts +0 -37
  22. package/src/openclaw-bridge.ts +0 -22
  23. package/src/plugin/index.ts +0 -2037
  24. package/src/plugin/lcm-command.ts +0 -1040
  25. package/src/plugin/lcm-doctor-apply.ts +0 -540
  26. package/src/plugin/lcm-doctor-cleaners.ts +0 -655
  27. package/src/plugin/lcm-doctor-shared.ts +0 -210
  28. package/src/plugin/shared-init.ts +0 -59
  29. package/src/prune.ts +0 -391
  30. package/src/retrieval.ts +0 -360
  31. package/src/session-patterns.ts +0 -23
  32. package/src/startup-banner-log.ts +0 -49
  33. package/src/store/compaction-telemetry-store.ts +0 -156
  34. package/src/store/conversation-store.ts +0 -929
  35. package/src/store/fts5-sanitize.ts +0 -50
  36. package/src/store/full-text-fallback.ts +0 -83
  37. package/src/store/full-text-sort.ts +0 -21
  38. package/src/store/index.ts +0 -39
  39. package/src/store/parse-utc-timestamp.ts +0 -25
  40. package/src/store/summary-store.ts +0 -1519
  41. package/src/summarize.ts +0 -1508
  42. package/src/tools/common.ts +0 -53
  43. package/src/tools/lcm-conversation-scope.ts +0 -127
  44. package/src/tools/lcm-describe-tool.ts +0 -245
  45. package/src/tools/lcm-expand-query-tool.ts +0 -1235
  46. package/src/tools/lcm-expand-tool.delegation.ts +0 -580
  47. package/src/tools/lcm-expand-tool.ts +0 -453
  48. package/src/tools/lcm-expansion-recursion-guard.ts +0 -373
  49. package/src/tools/lcm-grep-tool.ts +0 -228
  50. package/src/transaction-mutex.ts +0 -136
  51. package/src/transcript-repair.ts +0 -301
  52. package/src/types.ts +0 -165
@@ -1,210 +0,0 @@
1
- import type { DatabaseSync } from "node:sqlite";
2
-
3
- export const FALLBACK_SUMMARY_MARKER = "[LCM fallback summary; truncated for context management]";
4
- export const TRUNCATED_SUMMARY_PREFIX = "[Truncated from ";
5
- export const TRUNCATED_SUMMARY_WINDOW = 40;
6
- export const FALLBACK_SUMMARY_WINDOW = 80;
7
-
8
- export type DoctorMarkerKind = "old" | "new" | "fallback";
9
-
10
- export type DoctorSummaryCandidate = {
11
- conversationId: number;
12
- summaryId: string;
13
- markerKind: DoctorMarkerKind;
14
- };
15
-
16
- export type DoctorConversationCounts = {
17
- total: number;
18
- old: number;
19
- truncated: number;
20
- fallback: number;
21
- };
22
-
23
- export type DoctorSummaryStats = {
24
- candidates: DoctorSummaryCandidate[];
25
- total: number;
26
- old: number;
27
- truncated: number;
28
- fallback: number;
29
- byConversation: Map<number, DoctorConversationCounts>;
30
- };
31
-
32
- export type DoctorTargetRecord = {
33
- conversationId: number;
34
- summaryId: string;
35
- kind: string;
36
- depth: number;
37
- tokenCount: number;
38
- content: string;
39
- createdAt: string;
40
- childCount: number;
41
- markerKind: DoctorMarkerKind;
42
- };
43
-
44
- type DoctorTargetRow = {
45
- conversation_id: number;
46
- summary_id: string;
47
- kind: string;
48
- depth: number;
49
- token_count: number;
50
- content: string;
51
- created_at: string;
52
- child_count: number | null;
53
- };
54
-
55
- /**
56
- * Detect broken summary markers that doctor should flag or repair.
57
- */
58
- export function detectDoctorMarker(content: string): DoctorMarkerKind | null {
59
- if (content.startsWith(FALLBACK_SUMMARY_MARKER)) {
60
- return "old";
61
- }
62
-
63
- const truncatedIndex = content.indexOf(TRUNCATED_SUMMARY_PREFIX);
64
- if (truncatedIndex >= 0 && content.length - truncatedIndex < TRUNCATED_SUMMARY_WINDOW) {
65
- return "new";
66
- }
67
-
68
- const fallbackIndex = content.indexOf(FALLBACK_SUMMARY_MARKER);
69
- if (fallbackIndex >= 0 && content.length - fallbackIndex < FALLBACK_SUMMARY_WINDOW) {
70
- return "fallback";
71
- }
72
-
73
- return null;
74
- }
75
-
76
- /**
77
- * Load doctor targets for one conversation or the whole DB.
78
- */
79
- export function loadDoctorTargets(
80
- db: DatabaseSync,
81
- conversationId?: number,
82
- ): DoctorTargetRecord[] {
83
- const statement = conversationId === undefined
84
- ? db.prepare(
85
- `SELECT
86
- s.conversation_id,
87
- s.summary_id,
88
- s.kind,
89
- COALESCE(s.depth, 0) AS depth,
90
- COALESCE(s.token_count, 0) AS token_count,
91
- COALESCE(s.content, '') AS content,
92
- COALESCE(s.created_at, '') AS created_at,
93
- COALESCE(spc.child_count, 0) AS child_count
94
- FROM summaries s
95
- LEFT JOIN (
96
- SELECT summary_id, COUNT(*) AS child_count
97
- FROM summary_parents
98
- GROUP BY summary_id
99
- ) spc ON spc.summary_id = s.summary_id
100
- WHERE INSTR(COALESCE(s.content, ''), ?) > 0
101
- OR INSTR(COALESCE(s.content, ''), ?) > 0
102
- ORDER BY s.conversation_id ASC, COALESCE(s.depth, 0) ASC, s.created_at ASC, s.summary_id ASC`,
103
- )
104
- : db.prepare(
105
- `SELECT
106
- s.conversation_id,
107
- s.summary_id,
108
- s.kind,
109
- COALESCE(s.depth, 0) AS depth,
110
- COALESCE(s.token_count, 0) AS token_count,
111
- COALESCE(s.content, '') AS content,
112
- COALESCE(s.created_at, '') AS created_at,
113
- COALESCE(spc.child_count, 0) AS child_count
114
- FROM summaries s
115
- LEFT JOIN (
116
- SELECT summary_id, COUNT(*) AS child_count
117
- FROM summary_parents
118
- GROUP BY summary_id
119
- ) spc ON spc.summary_id = s.summary_id
120
- WHERE s.conversation_id = ?
121
- AND (
122
- INSTR(COALESCE(s.content, ''), ?) > 0
123
- OR INSTR(COALESCE(s.content, ''), ?) > 0
124
- )
125
- ORDER BY COALESCE(s.depth, 0) ASC, s.created_at ASC, s.summary_id ASC`,
126
- );
127
-
128
- const rows = (conversationId === undefined
129
- ? statement.all(FALLBACK_SUMMARY_MARKER, TRUNCATED_SUMMARY_PREFIX)
130
- : statement.all(conversationId, FALLBACK_SUMMARY_MARKER, TRUNCATED_SUMMARY_PREFIX)) as DoctorTargetRow[];
131
-
132
- const targets: DoctorTargetRecord[] = [];
133
- for (const row of rows) {
134
- const markerKind = detectDoctorMarker(row.content);
135
- if (!markerKind) {
136
- continue;
137
- }
138
- targets.push({
139
- conversationId: row.conversation_id,
140
- summaryId: row.summary_id,
141
- kind: row.kind,
142
- depth: Math.max(0, Math.floor(row.depth ?? 0)),
143
- tokenCount: Math.max(0, Math.floor(row.token_count ?? 0)),
144
- content: row.content,
145
- createdAt: row.created_at,
146
- childCount:
147
- typeof row.child_count === "number" && Number.isFinite(row.child_count)
148
- ? Math.max(0, Math.floor(row.child_count))
149
- : 0,
150
- markerKind,
151
- });
152
- }
153
- return targets;
154
- }
155
-
156
- /**
157
- * Aggregate doctor counts from target rows.
158
- */
159
- export function getDoctorSummaryStats(
160
- db: DatabaseSync,
161
- conversationId?: number,
162
- ): DoctorSummaryStats {
163
- const targets = loadDoctorTargets(db, conversationId);
164
- const candidates: DoctorSummaryCandidate[] = [];
165
- const byConversation = new Map<number, DoctorConversationCounts>();
166
- let old = 0;
167
- let truncated = 0;
168
- let fallback = 0;
169
-
170
- for (const target of targets) {
171
- const current = byConversation.get(target.conversationId) ?? {
172
- total: 0,
173
- old: 0,
174
- truncated: 0,
175
- fallback: 0,
176
- };
177
- current.total += 1;
178
-
179
- switch (target.markerKind) {
180
- case "old":
181
- old += 1;
182
- current.old += 1;
183
- break;
184
- case "new":
185
- truncated += 1;
186
- current.truncated += 1;
187
- break;
188
- case "fallback":
189
- fallback += 1;
190
- current.fallback += 1;
191
- break;
192
- }
193
-
194
- byConversation.set(target.conversationId, current);
195
- candidates.push({
196
- conversationId: target.conversationId,
197
- summaryId: target.summaryId,
198
- markerKind: target.markerKind,
199
- });
200
- }
201
-
202
- return {
203
- candidates,
204
- total: candidates.length,
205
- old,
206
- truncated,
207
- fallback,
208
- byConversation,
209
- };
210
- }
@@ -1,59 +0,0 @@
1
- /**
2
- * Process-global singleton state for LCM plugin initialization.
3
- *
4
- * OpenClaw v2026.4.5+ calls plugin register() per-agent-context (main,
5
- * subagents, cron lanes). Without sharing, each call opens a new DB
6
- * connection and runs migrations — causing lock storms on large databases.
7
- *
8
- * Uses the same globalThis + Symbol.for() pattern as startup-banner-log.ts
9
- * to ensure one DB connection and engine per database path per process.
10
- *
11
- * The shared state stores the waitForEngine/waitForDatabase closures from
12
- * the first register() call. These closures close over the local init
13
- * variables (database, lcm, initPromise, etc.) so all subsequent callers
14
- * share the same deferred init chain without stale-reference issues.
15
- */
16
- import type { DatabaseSync } from "node:sqlite";
17
- import type { LcmContextEngine } from "../engine.js";
18
-
19
- export type SharedLcmInit = {
20
- /** Whether gateway_stop has been called. */
21
- stopped: boolean;
22
- /** Sync accessor — returns the engine if already initialized, null otherwise. */
23
- getCachedEngine: () => LcmContextEngine | null;
24
- /** Async accessor for the initialized engine (waits for deferred init). */
25
- waitForEngine: () => Promise<LcmContextEngine>;
26
- /** Async accessor for the initialized DB handle (waits for deferred init). */
27
- waitForDatabase: () => Promise<DatabaseSync>;
28
- };
29
-
30
- const SHARED_KEY = Symbol.for(
31
- "@martian-engineering/lossless-claw/shared-init",
32
- );
33
-
34
- function getStore(): Map<string, SharedLcmInit> {
35
- const g = globalThis as typeof globalThis & {
36
- [key: symbol]: Map<string, SharedLcmInit> | undefined;
37
- };
38
- if (!g[SHARED_KEY]) {
39
- g[SHARED_KEY] = new Map();
40
- }
41
- return g[SHARED_KEY]!;
42
- }
43
-
44
- export function getSharedInit(dbPath: string): SharedLcmInit | undefined {
45
- return getStore().get(dbPath);
46
- }
47
-
48
- export function setSharedInit(dbPath: string, init: SharedLcmInit): void {
49
- getStore().set(dbPath, init);
50
- }
51
-
52
- export function removeSharedInit(dbPath: string): void {
53
- getStore().delete(dbPath);
54
- }
55
-
56
- /** Clear all shared init state. Intended for tests only. */
57
- export function clearAllSharedInit(): void {
58
- getStore().clear();
59
- }
package/src/prune.ts DELETED
@@ -1,391 +0,0 @@
1
- /**
2
- * Conversation pruning for data retention.
3
- *
4
- * Identifies and deletes conversations where ALL messages are older than a
5
- * given threshold. Relies on ON DELETE CASCADE foreign keys in the schema
6
- * to clean up messages, summaries, context_items, and other dependent rows.
7
- */
8
- import type { DatabaseSync } from "node:sqlite";
9
-
10
- // ── Duration parsing ────────────────────────────────────────────────────────
11
-
12
- const DURATION_RE = /^(\d+)\s*(d|day|days|w|week|weeks|m|month|months|y|year|years)$/i;
13
-
14
- const UNIT_TO_DAYS: Record<string, number> = {
15
- d: 1,
16
- day: 1,
17
- days: 1,
18
- w: 7,
19
- week: 7,
20
- weeks: 7,
21
- m: 30,
22
- month: 30,
23
- months: 30,
24
- y: 365,
25
- year: 365,
26
- years: 365,
27
- };
28
-
29
- /**
30
- * Parse a human-friendly duration string (e.g. "90d", "3m", "1y") into
31
- * a number of days. Returns `null` when the input is not recognized.
32
- */
33
- export function parseDuration(input: string): number | null {
34
- const trimmed = input.trim().toLowerCase();
35
- const match = DURATION_RE.exec(trimmed);
36
- if (!match) {
37
- return null;
38
- }
39
- const amount = Number(match[1]);
40
- const unit = match[2]!.toLowerCase();
41
- const multiplier = UNIT_TO_DAYS[unit];
42
- if (multiplier == null || !Number.isFinite(amount) || amount <= 0) {
43
- return null;
44
- }
45
- return amount * multiplier;
46
- }
47
-
48
- // ── Prune types ─────────────────────────────────────────────────────────────
49
-
50
- export type PruneCandidate = {
51
- conversationId: number;
52
- sessionKey: string | null;
53
- messageCount: number;
54
- summaryCount: number;
55
- latestMessageAt: string;
56
- createdAt: string;
57
- };
58
-
59
- export type PruneResult = {
60
- /** Conversations that matched the age threshold. */
61
- candidates: PruneCandidate[];
62
- /** Number of conversations actually deleted (0 in dry-run mode). */
63
- deleted: number;
64
- /** Whether VACUUM was executed after deletion. */
65
- vacuumed: boolean;
66
- /** The cutoff date used (ISO-8601 UTC string). */
67
- cutoffDate: string;
68
- };
69
-
70
- export type PruneOptions = {
71
- /** Duration string, e.g. "90d", "30d", "1y". */
72
- before: string;
73
- /** When true, actually delete. Default is dry-run (false). */
74
- confirm?: boolean;
75
- /** Maximum conversations to delete per write transaction. Default 100. */
76
- batchSize?: number;
77
- /** Maximum delete batches to run before returning. Default unlimited. */
78
- maxBatches?: number;
79
- /** When true, run VACUUM after deletion. Default false. */
80
- vacuum?: boolean;
81
- /** Override "now" for testing. ISO-8601 UTC string. */
82
- now?: string;
83
- };
84
-
85
- // ── Core prune logic ────────────────────────────────────────────────────────
86
-
87
- type PruneCandidateRow = {
88
- conversation_id: number;
89
- session_key: string | null;
90
- message_count: number;
91
- summary_count: number;
92
- latest_message_at: string;
93
- created_at: string;
94
- };
95
-
96
- const SELECT_PRUNE_CANDIDATES_SQL = `SELECT
97
- c.conversation_id,
98
- c.session_key,
99
- COALESCE(msg_stats.message_count, 0) AS message_count,
100
- COALESCE(sum_stats.summary_count, 0) AS summary_count,
101
- COALESCE(msg_stats.latest_message_at, c.created_at) AS latest_message_at,
102
- c.created_at
103
- FROM conversations c
104
- LEFT JOIN (
105
- SELECT conversation_id,
106
- COUNT(*) AS message_count,
107
- MAX(created_at) AS latest_message_at
108
- FROM messages
109
- GROUP BY conversation_id
110
- ) msg_stats ON msg_stats.conversation_id = c.conversation_id
111
- LEFT JOIN (
112
- SELECT conversation_id,
113
- COUNT(*) AS summary_count
114
- FROM summaries
115
- GROUP BY conversation_id
116
- ) sum_stats ON sum_stats.conversation_id = c.conversation_id
117
- WHERE julianday(COALESCE(msg_stats.latest_message_at, c.created_at)) < julianday(?)
118
- ORDER BY julianday(COALESCE(msg_stats.latest_message_at, c.created_at)) ASC,
119
- c.conversation_id ASC`;
120
-
121
- /**
122
- * Compute the UTC cutoff date by subtracting `days` from `now`.
123
- */
124
- function computeCutoffDate(days: number, now?: string): string {
125
- const base = now ? new Date(now) : new Date();
126
- base.setUTCDate(base.getUTCDate() - days);
127
- return base.toISOString();
128
- }
129
-
130
- /**
131
- * Normalize prune batch size to a small positive integer.
132
- */
133
- function resolveBatchSize(batchSize?: number): number {
134
- if (batchSize == null) {
135
- return 100;
136
- }
137
- if (!Number.isFinite(batchSize) || batchSize <= 0) {
138
- throw new Error(`Invalid batch size "${batchSize}". Expected a positive integer.`);
139
- }
140
- return Math.floor(batchSize);
141
- }
142
-
143
- /**
144
- * Normalize the optional batch cap for confirm-mode pruning.
145
- */
146
- function resolveMaxBatches(maxBatches?: number): number | null {
147
- if (maxBatches == null) {
148
- return null;
149
- }
150
- if (!Number.isFinite(maxBatches) || maxBatches <= 0) {
151
- throw new Error(`Invalid max batches "${maxBatches}". Expected a positive integer.`);
152
- }
153
- return Math.floor(maxBatches);
154
- }
155
-
156
- /**
157
- * Load prune candidates using SQLite date math so mixed timestamp formats are
158
- * compared chronologically instead of lexically.
159
- */
160
- function loadPruneCandidates(
161
- db: DatabaseSync,
162
- cutoffDate: string,
163
- limit?: number,
164
- ): PruneCandidate[] {
165
- const sql = limit == null ? SELECT_PRUNE_CANDIDATES_SQL : `${SELECT_PRUNE_CANDIDATES_SQL}\n LIMIT ?`;
166
- const rows = (
167
- limit == null
168
- ? db.prepare(sql).all(cutoffDate)
169
- : db.prepare(sql).all(cutoffDate, limit)
170
- ) as PruneCandidateRow[];
171
- return rows.map((row) => ({
172
- conversationId: row.conversation_id,
173
- sessionKey: row.session_key,
174
- messageCount: row.message_count,
175
- summaryCount: row.summary_count,
176
- latestMessageAt: row.latest_message_at,
177
- createdAt: row.created_at,
178
- }));
179
- }
180
-
181
- /**
182
- * Detect whether an optional SQLite table exists.
183
- */
184
- function hasTable(db: DatabaseSync, tableName: string): boolean {
185
- const row = db
186
- .prepare(`SELECT 1 AS found FROM sqlite_master WHERE type = 'table' AND name = ? LIMIT 1`)
187
- .get(tableName) as { found: number } | undefined;
188
- return row?.found === 1;
189
- }
190
-
191
- /**
192
- * Create temp tables containing the conversations, summaries, and messages
193
- * selected for pruning so dependent deletes can use simple indexed lookups.
194
- */
195
- function stageCandidateConversationIds(
196
- db: DatabaseSync,
197
- candidates: PruneCandidate[],
198
- ): void {
199
- db.exec(`DROP TABLE IF EXISTS temp.prune_candidate_ids`);
200
- db.exec(`DROP TABLE IF EXISTS temp.prune_candidate_summary_ids`);
201
- db.exec(`DROP TABLE IF EXISTS temp.prune_candidate_message_ids`);
202
- db.exec(`CREATE TEMP TABLE prune_candidate_ids (conversation_id INTEGER PRIMARY KEY)`);
203
- db.exec(`CREATE TEMP TABLE prune_candidate_summary_ids (summary_id TEXT PRIMARY KEY)`);
204
- db.exec(`CREATE TEMP TABLE prune_candidate_message_ids (message_id INTEGER PRIMARY KEY)`);
205
- const insertStmt = db.prepare(
206
- `INSERT INTO temp.prune_candidate_ids (conversation_id) VALUES (?)`,
207
- );
208
- for (const candidate of candidates) {
209
- insertStmt.run(candidate.conversationId);
210
- }
211
- db.exec(`
212
- INSERT INTO temp.prune_candidate_summary_ids (summary_id)
213
- SELECT s.summary_id
214
- FROM summaries s
215
- JOIN temp.prune_candidate_ids p ON p.conversation_id = s.conversation_id
216
- `);
217
- db.exec(`
218
- INSERT INTO temp.prune_candidate_message_ids (message_id)
219
- SELECT m.message_id
220
- FROM messages m
221
- JOIN temp.prune_candidate_ids p ON p.conversation_id = m.conversation_id
222
- `);
223
- }
224
-
225
- /**
226
- * Remove the temp candidate table.
227
- */
228
- function dropCandidateConversationIds(db: DatabaseSync): void {
229
- db.exec(`DROP TABLE IF EXISTS temp.prune_candidate_message_ids`);
230
- db.exec(`DROP TABLE IF EXISTS temp.prune_candidate_summary_ids`);
231
- db.exec(`DROP TABLE IF EXISTS temp.prune_candidate_ids`);
232
- }
233
-
234
- /**
235
- * Delete candidate conversations and return the number of rows removed.
236
- */
237
- function deleteCandidates(db: DatabaseSync, candidates: PruneCandidate[]): number {
238
- if (candidates.length === 0) {
239
- return 0;
240
- }
241
-
242
- const tableOptions = {
243
- hasMessagesFts: hasTable(db, "messages_fts"),
244
- hasSummariesFts: hasTable(db, "summaries_fts"),
245
- hasSummariesFtsCjk: hasTable(db, "summaries_fts_cjk"),
246
- };
247
-
248
- stageCandidateConversationIds(db, candidates);
249
- try {
250
- db.prepare(
251
- `DELETE FROM summary_messages
252
- WHERE summary_id IN (SELECT summary_id FROM temp.prune_candidate_summary_ids)`,
253
- ).run();
254
-
255
- db.prepare(
256
- `DELETE FROM summary_messages
257
- WHERE message_id IN (SELECT message_id FROM temp.prune_candidate_message_ids)`,
258
- ).run();
259
-
260
- db.prepare(
261
- `DELETE FROM summary_parents
262
- WHERE summary_id IN (SELECT summary_id FROM temp.prune_candidate_summary_ids)`,
263
- ).run();
264
-
265
- db.prepare(
266
- `DELETE FROM summary_parents
267
- WHERE parent_summary_id IN (SELECT summary_id FROM temp.prune_candidate_summary_ids)`,
268
- ).run();
269
-
270
- db.prepare(
271
- `DELETE FROM context_items
272
- WHERE message_id IN (SELECT message_id FROM temp.prune_candidate_message_ids)`,
273
- ).run();
274
-
275
- db.prepare(
276
- `DELETE FROM context_items
277
- WHERE summary_id IN (SELECT summary_id FROM temp.prune_candidate_summary_ids)`,
278
- ).run();
279
-
280
- db.prepare(
281
- `DELETE FROM context_items
282
- WHERE conversation_id IN (SELECT conversation_id FROM temp.prune_candidate_ids)`,
283
- ).run();
284
-
285
- if (tableOptions.hasMessagesFts) {
286
- db.prepare(
287
- `DELETE FROM messages_fts
288
- WHERE rowid IN (SELECT message_id FROM temp.prune_candidate_message_ids)`,
289
- ).run();
290
- }
291
-
292
- if (tableOptions.hasSummariesFts) {
293
- db.prepare(
294
- `DELETE FROM summaries_fts
295
- WHERE summary_id IN (SELECT summary_id FROM temp.prune_candidate_summary_ids)`,
296
- ).run();
297
- }
298
-
299
- if (tableOptions.hasSummariesFtsCjk) {
300
- db.prepare(
301
- `DELETE FROM summaries_fts_cjk
302
- WHERE summary_id IN (SELECT summary_id FROM temp.prune_candidate_summary_ids)`,
303
- ).run();
304
- }
305
-
306
- return Number(
307
- db
308
- .prepare(
309
- `DELETE FROM conversations
310
- WHERE conversation_id IN (SELECT conversation_id FROM temp.prune_candidate_ids)`,
311
- )
312
- .run().changes ?? 0,
313
- );
314
- } finally {
315
- dropCandidateConversationIds(db);
316
- }
317
- }
318
-
319
- /**
320
- * Prune old conversations from the LCM database.
321
- *
322
- * In dry-run mode (default), returns the list of conversations that would be
323
- * deleted without modifying the database. With `confirm: true`, deletes them
324
- * and relies on ON DELETE CASCADE for cleanup of child rows.
325
- */
326
- export function pruneConversations(
327
- db: DatabaseSync,
328
- options: PruneOptions,
329
- ): PruneResult {
330
- const days = parseDuration(options.before);
331
- if (days == null) {
332
- throw new Error(
333
- `Invalid duration "${options.before}". Expected a value like "90d", "30d", "3m", or "1y".`,
334
- );
335
- }
336
-
337
- const cutoffDate = computeCutoffDate(days, options.now);
338
- const batchSize = resolveBatchSize(options.batchSize);
339
- const maxBatches = resolveMaxBatches(options.maxBatches);
340
-
341
- let deleted = 0;
342
- let vacuumed = false;
343
- let candidates: PruneCandidate[];
344
-
345
- if (!options.confirm) {
346
- candidates = loadPruneCandidates(db, cutoffDate);
347
- } else {
348
- candidates = [];
349
- let batchesRun = 0;
350
- while (true) {
351
- let batchCount = 0;
352
- db.exec("BEGIN IMMEDIATE");
353
- try {
354
- const batch = loadPruneCandidates(db, cutoffDate, batchSize);
355
- batchCount = batch.length;
356
- if (batch.length === 0) {
357
- db.exec("COMMIT");
358
- break;
359
- }
360
- deleted += deleteCandidates(db, batch);
361
- candidates.push(...batch);
362
- db.exec("COMMIT");
363
- } catch (error) {
364
- db.exec("ROLLBACK");
365
- throw error;
366
- }
367
- if (batchCount < batchSize) {
368
- break;
369
- }
370
- batchesRun += 1;
371
- if (maxBatches != null && batchesRun >= maxBatches) {
372
- break;
373
- }
374
- }
375
- }
376
-
377
- if (options.vacuum && deleted > 0) {
378
- db.exec("VACUUM");
379
- // VACUUM in WAL mode can leave the reclaimed pages in the WAL file until
380
- // a checkpoint folds them back into the main database.
381
- db.exec("PRAGMA wal_checkpoint(TRUNCATE)");
382
- vacuumed = true;
383
- }
384
-
385
- return {
386
- candidates,
387
- deleted,
388
- vacuumed,
389
- cutoffDate,
390
- };
391
- }