quilltap 4.15.0-dev.109 → 4.15.0-dev.120

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -442,7 +442,7 @@ quilltap maintenance status --instance Friday --json
442
442
  quilltap maintenance run --instance Friday # Run the sweeps once (lock-gated; refuses while the server is up)
443
443
  ```
444
444
 
445
- `maintenance run` is a DB writer: it claims `<dataDir>/quilltap.lock` and **refuses while a running Quilltap server holds it** — stop the server first. Because it can only run with the server down, it performs the sweeps expressible as direct SQL/filesystem work: reaping finished background jobs (COMPLETED after 7 days, DEAD after 30, keyed off `completedAt`), closed terminal sessions older than 30 days plus their transcript files, and orphaned mount-index files. The **stale-chat asset collapse** (superseded story-backgrounds and wardrobe avatars) needs the server's file-storage machinery and runs only on the server's daily tick — `status` reports a stale-chat count so you can see the backlog. Retention windows mirror `lib/background-jobs/maintenance/retention-constants.ts`.
445
+ `maintenance run` is a DB writer: it claims `<dataDir>/quilltap.lock` and **refuses while a running Quilltap server holds it** — stop the server first. Because it can only run with the server down, it performs the sweeps expressible as direct SQL/filesystem work: reaping finished background jobs (COMPLETED after 7 days, DEAD after 30, keyed off `completedAt`), closed terminal sessions older than 30 days plus their transcript files, and orphaned mount-index files (each with its stored document or blob content). The **stale-chat asset collapse** (superseded story-backgrounds and wardrobe avatars) needs the server's file-storage machinery and runs only on the server's daily tick — `status` reports a stale-chat count so you can see the backlog. Retention windows mirror `lib/background-jobs/maintenance/retention-constants.ts`.
446
446
 
447
447
  ## Logs
448
448
 
@@ -0,0 +1,84 @@
1
+ /**
2
+ * `quilltap maintenance run`, step 3: the orphaned mount-index file sweep.
3
+ *
4
+ * A mount index whose tables came from the schema translator carries no
5
+ * foreign keys, so deleting a `doc_mount_files` row cascades to nothing. The
6
+ * sweep therefore deletes an orphan's document and blob payload itself, as the
7
+ * server's `gcOrphanedFileRow` does. A bare `DELETE FROM doc_mount_files`
8
+ * (what this step used to run) left that payload behind for good.
9
+ *
10
+ * @jest-environment node
11
+ */
12
+
13
+ 'use strict';
14
+
15
+ const path = require('path');
16
+ // The real driver, by absolute path: jest maps the bare specifier to a mock.
17
+ const Database = require(path.join(process.cwd(), 'node_modules', 'better-sqlite3'));
18
+ const { sweepOrphanedMountFiles } = require('../maintenance-commands');
19
+
20
+ // The tables as the schema translator creates them: no REFERENCES anywhere.
21
+ function createIndex({ withDocuments = true, withBlobs = true } = {}) {
22
+ const db = new Database(':memory:');
23
+ db.exec('CREATE TABLE doc_mount_files (id TEXT PRIMARY KEY, sha256 TEXT)');
24
+ db.exec('CREATE TABLE doc_mount_file_links (id TEXT PRIMARY KEY, fileId TEXT, mountPointId TEXT)');
25
+ if (withDocuments) db.exec('CREATE TABLE doc_mount_documents (id TEXT PRIMARY KEY, fileId TEXT, content TEXT)');
26
+ if (withBlobs) db.exec('CREATE TABLE doc_mount_blobs (id TEXT PRIMARY KEY, fileId TEXT, data BLOB)');
27
+ return db;
28
+ }
29
+
30
+ const ids = (db, table, column = 'id') =>
31
+ db.prepare(`SELECT ${column} AS v FROM ${table} ORDER BY v`).all().map((row) => row.v);
32
+
33
+ describe('sweepOrphanedMountFiles', () => {
34
+ it('deletes an orphaned file row together with its document and blob payload', () => {
35
+ const db = createIndex();
36
+ db.exec(`
37
+ INSERT INTO doc_mount_files VALUES ('linked', 'a'), ('orphan-doc', 'b'), ('orphan-blob', 'c');
38
+ INSERT INTO doc_mount_file_links VALUES ('link-1', 'linked', 'mp-1');
39
+ INSERT INTO doc_mount_documents VALUES ('d-linked', 'linked', 'kept'), ('d-orphan', 'orphan-doc', 'gone');
40
+ INSERT INTO doc_mount_blobs VALUES ('b-linked', 'linked', x'01'), ('b-orphan', 'orphan-blob', x'02');
41
+ `);
42
+
43
+ expect(sweepOrphanedMountFiles(db)).toBe(2);
44
+
45
+ expect(ids(db, 'doc_mount_files')).toEqual(['linked']);
46
+ expect(ids(db, 'doc_mount_documents')).toEqual(['d-linked']);
47
+ expect(ids(db, 'doc_mount_blobs')).toEqual(['b-linked']);
48
+ expect(ids(db, 'doc_mount_file_links')).toEqual(['link-1']);
49
+ });
50
+
51
+ it('leaves content alone while any link, in any store, still references it', () => {
52
+ const db = createIndex();
53
+ db.exec(`
54
+ INSERT INTO doc_mount_files VALUES ('shared', 'a');
55
+ INSERT INTO doc_mount_file_links VALUES ('link-1', 'shared', 'mp-1'), ('link-2', 'shared', 'mp-2');
56
+ INSERT INTO doc_mount_documents VALUES ('d-shared', 'shared', 'kept');
57
+ `);
58
+
59
+ expect(sweepOrphanedMountFiles(db)).toBe(0);
60
+ expect(ids(db, 'doc_mount_files')).toEqual(['shared']);
61
+ expect(ids(db, 'doc_mount_documents')).toEqual(['d-shared']);
62
+ });
63
+
64
+ it('copes with an index that never created a payload table', () => {
65
+ // Both payload tables are minted lazily; a document-only index has no
66
+ // `doc_mount_blobs`, and deleting from a missing table would throw.
67
+ const db = createIndex({ withBlobs: false });
68
+ db.exec(`
69
+ INSERT INTO doc_mount_files VALUES ('orphan', 'a');
70
+ INSERT INTO doc_mount_documents VALUES ('d-orphan', 'orphan', 'gone');
71
+ `);
72
+
73
+ expect(sweepOrphanedMountFiles(db)).toBe(1);
74
+ expect(ids(db, 'doc_mount_documents')).toEqual([]);
75
+
76
+ const bare = createIndex({ withDocuments: false, withBlobs: false });
77
+ bare.exec("INSERT INTO doc_mount_files VALUES ('orphan', 'a')");
78
+ expect(sweepOrphanedMountFiles(bare)).toBe(1);
79
+ });
80
+
81
+ it('does nothing on a database with no mount-index tables', () => {
82
+ expect(sweepOrphanedMountFiles(new Database(':memory:'))).toBe(0);
83
+ });
84
+ });
@@ -228,6 +228,39 @@ async function handleStatus(flags) {
228
228
  }
229
229
  }
230
230
 
231
+ /**
232
+ * Delete every `doc_mount_files` row that no link references, with its
233
+ * payload. Returns how many file rows went.
234
+ *
235
+ * The payload rows (`doc_mount_documents`, `doc_mount_blobs`) are deleted
236
+ * explicitly, before the file row: a mount index whose tables came from the
237
+ * schema translator carries no foreign keys, so nothing cascades and a bare
238
+ * delete of the file row would keep its payload forever. This MIRRORS the
239
+ * server's `gcOrphanedFileRow` (`lib/mount-index/orphan-store-reaper.ts`),
240
+ * which `sweepOrphanedFiles` runs for each orphan. Keep them in step. Both
241
+ * payload tables are created lazily, so each is checked for first.
242
+ *
243
+ * One transaction: a failure part-way leaves nothing half-collected.
244
+ */
245
+ function sweepOrphanedMountFiles(mountsDb) {
246
+ const tableExists = (name) =>
247
+ !!mountsDb.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?").get(name);
248
+ if (!tableExists('doc_mount_files') || !tableExists('doc_mount_file_links')) return 0;
249
+
250
+ const orphaned = 'SELECT id FROM doc_mount_files WHERE id NOT IN (SELECT DISTINCT fileId FROM doc_mount_file_links)';
251
+ const sweep = mountsDb.transaction(() => {
252
+ for (const payloadTable of ['doc_mount_documents', 'doc_mount_blobs']) {
253
+ if (tableExists(payloadTable)) {
254
+ mountsDb.prepare(`DELETE FROM ${payloadTable} WHERE fileId IN (${orphaned})`).run();
255
+ }
256
+ }
257
+ return mountsDb
258
+ .prepare('DELETE FROM doc_mount_files WHERE id NOT IN (SELECT DISTINCT fileId FROM doc_mount_file_links)')
259
+ .run().changes;
260
+ });
261
+ return sweep();
262
+ }
263
+
231
264
  async function handleRun(flags) {
232
265
  let resolved;
233
266
  let pepper;
@@ -321,12 +354,7 @@ async function handleRun(flags) {
321
354
  // be absent on older instances — skip gracefully.
322
355
  try {
323
356
  mountsDb = openMountIndexDb(resolved.dataDir, pepper, { readonly: false });
324
- summary.orphanedFiles = mountsDb
325
- .prepare(
326
- 'DELETE FROM doc_mount_files ' +
327
- 'WHERE id NOT IN (SELECT DISTINCT fileId FROM doc_mount_file_links)'
328
- )
329
- .run().changes;
357
+ summary.orphanedFiles = sweepOrphanedMountFiles(mountsDb);
330
358
  } catch {
331
359
  summary.orphanedFiles = 0;
332
360
  }
@@ -391,4 +419,5 @@ async function maintenanceCommand(args) {
391
419
 
392
420
  module.exports = {
393
421
  maintenanceCommand,
422
+ sweepOrphanedMountFiles,
394
423
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "quilltap",
3
- "version": "4.15.0-dev.109",
3
+ "version": "4.15.0-dev.120",
4
4
  "description": "Self-hosted AI workspace for writers, worldbuilders, and roleplayers. Run with npx quilltap.",
5
5
  "author": {
6
6
  "name": "Charles Sebold",