quilltap 4.8.0-dev.186 → 4.8.0-dev.192

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/bin/quilltap.js CHANGED
@@ -717,6 +717,20 @@ Subcommands (high-level shortcuts; auto-pick the right database):
717
717
  Prompts/ and Scenarios/ folder counts, and any
718
718
  divergence between DB columns and vault content.
719
719
  (flags: --id <id|name> --diverged --blocked --limit N)
720
+ characters archives List archived characters and ARCHIVE bundles on
721
+ the shelf, loose bundles included. Read-only.
722
+ characters archive <name|id> --write [--port N]
723
+ Archive a character via the RUNNING server (it
724
+ holds the export pipeline and the passphrase).
725
+ characters rehydrate <name|id> --write [--port N]
726
+ Wake an archived character via the running server.
727
+ characters export <name|id> [--out <path>] [--port N]
728
+ Write a PLAINTEXT .qtap for a character. Archived:
729
+ decrypts the bundle offline (prompts for the
730
+ passphrase on protected instances) — the only way
731
+ to reach packed-away mail/photos/summaries without
732
+ rehydrating. Live: runs the server's export
733
+ pipeline (server must be up). Read-only.
720
734
  optimize [target...] Run maintenance (VACUUM + ANALYZE + PRAGMA optimize)
721
735
  on the named databases, or all of them if no
722
736
  target is given. Targets: main, llm-logs,
@@ -0,0 +1,216 @@
1
+ /**
2
+ * Bind planning for filesystem-backed document stores.
3
+ *
4
+ * The planner decides what a container is allowed to see, so its edges matter
5
+ * more than its happy path: a nested store that shadows its parent, a missing
6
+ * path that Docker would helpfully materialise as an empty directory, or a
7
+ * Windows host where the whole path-identical scheme cannot work.
8
+ *
9
+ * @jest-environment node
10
+ */
11
+
12
+ 'use strict';
13
+
14
+ const {
15
+ planStoreMounts,
16
+ toDockerArgs,
17
+ findMissingBinds,
18
+ normalisePath,
19
+ } = require('../docker-mounts');
20
+
21
+ /** Build a doc_mount_points-shaped row with sensible defaults. */
22
+ function store(overrides = {}) {
23
+ return {
24
+ id: 'id-' + (overrides.name || 'x'),
25
+ name: 'Store',
26
+ mountType: 'filesystem',
27
+ storeType: 'documents',
28
+ basePath: '/vaults/one',
29
+ enabled: 1,
30
+ ...overrides,
31
+ };
32
+ }
33
+
34
+ /** A path probe that treats the given list as the only existing directories. */
35
+ const existsIn = (paths) => (p) => paths.includes(p);
36
+
37
+ const linux = (rows, exists) => planStoreMounts(rows, { platform: 'linux', exists });
38
+ const macos = (rows, exists) => planStoreMounts(rows, { platform: 'darwin', exists });
39
+
40
+ describe('planStoreMounts', () => {
41
+ it('binds an enabled filesystem store at its own host path', () => {
42
+ const plan = linux([store({ name: 'Church', basePath: '/vaults/church' })], existsIn(['/vaults/church']));
43
+
44
+ expect(plan.binds).toEqual([
45
+ { hostPath: '/vaults/church', containerPath: '/vaults/church', stores: ['Church'] },
46
+ ]);
47
+ });
48
+
49
+ it('treats obsidian stores as filesystem-backed', () => {
50
+ const plan = linux(
51
+ [store({ name: 'Malory', mountType: 'obsidian', basePath: '/vaults/malory' })],
52
+ existsIn(['/vaults/malory'])
53
+ );
54
+
55
+ expect(plan.binds.map((b) => b.hostPath)).toEqual(['/vaults/malory']);
56
+ });
57
+
58
+ it('ignores database-backed and disabled stores', () => {
59
+ const plan = linux(
60
+ [
61
+ store({ name: 'Vault', mountType: 'database', basePath: '' }),
62
+ store({ name: 'Off', basePath: '/vaults/off', enabled: 0 }),
63
+ ],
64
+ existsIn(['/vaults/off'])
65
+ );
66
+
67
+ expect(plan.binds).toEqual([]);
68
+ });
69
+
70
+ it('emits one bind for several stores sharing a path', () => {
71
+ const plan = linux(
72
+ [
73
+ store({ name: 'Church', basePath: '/vaults/shared' }),
74
+ store({ name: 'Small Group', basePath: '/vaults/shared/' }),
75
+ ],
76
+ existsIn(['/vaults/shared'])
77
+ );
78
+
79
+ expect(plan.binds).toHaveLength(1);
80
+ expect(plan.binds[0].stores).toEqual(['Church', 'Small Group']);
81
+ });
82
+
83
+ it('drops a store nested inside another bound store', () => {
84
+ // Binding both would mount the child independently and shadow the
85
+ // parent's view of that subdirectory.
86
+ const plan = linux(
87
+ [
88
+ store({ name: 'Vault', basePath: '/vaults/obsidian' }),
89
+ store({ name: 'Notes', basePath: '/vaults/obsidian/notes' }),
90
+ ],
91
+ existsIn(['/vaults/obsidian', '/vaults/obsidian/notes'])
92
+ );
93
+
94
+ expect(plan.binds.map((b) => b.hostPath)).toEqual(['/vaults/obsidian']);
95
+ });
96
+
97
+ it('does not treat a sibling with a shared prefix as nested', () => {
98
+ const plan = linux(
99
+ [
100
+ store({ name: 'A', basePath: '/vaults/notes' }),
101
+ store({ name: 'B', basePath: '/vaults/notes-archive' }),
102
+ ],
103
+ existsIn(['/vaults/notes', '/vaults/notes-archive'])
104
+ );
105
+
106
+ expect(plan.binds.map((b) => b.hostPath)).toEqual(['/vaults/notes', '/vaults/notes-archive']);
107
+ });
108
+
109
+ it('skips a missing path rather than letting Docker fabricate it', () => {
110
+ const plan = linux([store({ name: 'Gone', basePath: '/vaults/gone' })], existsIn([]));
111
+
112
+ expect(plan.binds).toEqual([]);
113
+ expect(plan.skipped).toEqual([
114
+ { hostPath: '/vaults/gone', stores: ['Gone'], reason: 'missing' },
115
+ ]);
116
+ expect(plan.warnings.join(' ')).toContain('does not exist');
117
+ });
118
+
119
+ it('rejects a relative base path', () => {
120
+ const plan = linux([store({ name: 'Rel', basePath: 'relative/path' })], existsIn(['relative/path']));
121
+
122
+ expect(plan.binds).toEqual([]);
123
+ expect(plan.warnings.join(' ')).toContain('not absolute');
124
+ });
125
+
126
+ it('refuses path-identical binds on Windows', () => {
127
+ const plan = planStoreMounts([store({ basePath: 'C:\\Users\\me\\Vault' })], {
128
+ platform: 'win32',
129
+ exists: () => true,
130
+ });
131
+
132
+ expect(plan.unsupported).toBe(true);
133
+ expect(plan.binds).toEqual([]);
134
+ expect(plan.warnings.join(' ')).toContain('not supported on Windows');
135
+ });
136
+
137
+ it('warns about macOS paths outside Docker Desktop default shares', () => {
138
+ const plan = macos([store({ name: 'Odd', basePath: '/data/vault' })], existsIn(['/data/vault']));
139
+
140
+ expect(plan.binds).toHaveLength(1);
141
+ expect(plan.warnings.join(' ')).toContain('File sharing');
142
+ });
143
+
144
+ it('does not warn for macOS paths under a shared prefix', () => {
145
+ const plan = macos([store({ name: 'Home', basePath: '/Users/me/Vault' })], existsIn(['/Users/me/Vault']));
146
+
147
+ expect(plan.warnings.join(' ')).not.toContain('File sharing');
148
+ });
149
+
150
+ it('warns about host ownership on Linux', () => {
151
+ const plan = linux([store({ basePath: '/vaults/one' })], existsIn(['/vaults/one']));
152
+
153
+ expect(plan.warnings.join(' ')).toContain('--user');
154
+ });
155
+ });
156
+
157
+ describe('toDockerArgs', () => {
158
+ it('renders each bind as a -v pair', () => {
159
+ const plan = linux(
160
+ [
161
+ store({ name: 'A', basePath: '/vaults/a' }),
162
+ store({ name: 'B', basePath: '/vaults/b' }),
163
+ ],
164
+ existsIn(['/vaults/a', '/vaults/b'])
165
+ );
166
+
167
+ expect(toDockerArgs(plan)).toEqual([
168
+ '-v', '/vaults/a:/vaults/a',
169
+ '-v', '/vaults/b:/vaults/b',
170
+ ]);
171
+ });
172
+
173
+ it('keeps a path containing spaces in a single argument', () => {
174
+ // The argv is handed to execFileSync, so a space must not split the pair.
175
+ const plan = linux([store({ basePath: '/vaults/Local Obsidian' })], existsIn(['/vaults/Local Obsidian']));
176
+
177
+ expect(toDockerArgs(plan)).toEqual([
178
+ '-v', '/vaults/Local Obsidian:/vaults/Local Obsidian',
179
+ ]);
180
+ });
181
+ });
182
+
183
+ describe('findMissingBinds', () => {
184
+ const plan = () =>
185
+ linux(
186
+ [
187
+ store({ name: 'A', basePath: '/vaults/a' }),
188
+ store({ name: 'B', basePath: '/vaults/b' }),
189
+ ],
190
+ existsIn(['/vaults/a', '/vaults/b'])
191
+ );
192
+
193
+ it('reports binds a container was not created with', () => {
194
+ expect(findMissingBinds(plan(), ['/vaults/a']).map((b) => b.hostPath)).toEqual(['/vaults/b']);
195
+ });
196
+
197
+ it('reports nothing when every bind is present', () => {
198
+ expect(findMissingBinds(plan(), ['/vaults/a', '/vaults/b', '/data'])).toEqual([]);
199
+ });
200
+
201
+ it('ignores a trailing separator when comparing', () => {
202
+ expect(findMissingBinds(plan(), ['/vaults/a/', '/vaults/b/'])).toEqual([]);
203
+ });
204
+ });
205
+
206
+ describe('normalisePath', () => {
207
+ it.each([
208
+ ['/a/b/', '/a/b'],
209
+ ['/a//b', '/a/b'],
210
+ ['/a/./b', '/a/b'],
211
+ ['/a/c/../b', '/a/b'],
212
+ ['/', '/'],
213
+ ])('normalises %s to %s', (input, expected) => {
214
+ expect(normalisePath(input)).toBe(expected);
215
+ });
216
+ });
@@ -126,7 +126,7 @@ _quilltap_complete() {
126
126
  fi
127
127
  ;;
128
128
  docs)
129
- local docs_verbs="list show files ls dir tree read export scan write delete mkdir move copy link rmdir mvdir status find grep reindex embed"
129
+ local docs_verbs="list show files ls dir tree read export scan write delete mkdir move copy link rmdir mvdir status docker-mounts find grep reindex embed"
130
130
  local docs_flags="--mount --instance --data-dir --passphrase --port --json --help \
131
131
  --force --rendered --links --folder --type --ext --limit --max --context --top --threshold \
132
132
  --ignore-case -l --wait -R --recursive --sort -r --reverse --depth --max-nodes --long --semantic"
@@ -136,6 +136,7 @@ complete -c quilltap -n '__quilltap_using_subcommand docs' -f -a 'scan' -d 'Trig
136
136
  complete -c quilltap -n '__quilltap_using_subcommand docs' -f -a 'find' -d 'Substring search'
137
137
  complete -c quilltap -n '__quilltap_using_subcommand docs' -f -a 'grep' -d 'Pattern search in text'
138
138
  complete -c quilltap -n '__quilltap_using_subcommand docs' -f -a 'status' -d 'Per-mount status'
139
+ complete -c quilltap -n '__quilltap_using_subcommand docs' -f -a 'docker-mounts' -d 'Binds needed under Docker'
139
140
  complete -c quilltap -n '__quilltap_using_subcommand docs' -f -a 'reindex' -d 'Re-extract and chunk'
140
141
  complete -c quilltap -n '__quilltap_using_subcommand docs' -f -a 'embed' -d 'Enqueue embeddings'
141
142
  complete -c quilltap -n '__quilltap_using_subcommand docs' -f -a 'write' -d 'Write a file'
@@ -168,6 +168,7 @@ _quilltap_docs() {
168
168
  'find:Substring search'
169
169
  'grep:Pattern search inside text'
170
170
  'status:Per-mount status'
171
+ 'docker-mounts:Bind mounts needed under Docker'
171
172
  'reindex:Re-extract and re-chunk'
172
173
  'embed:Enqueue embedding jobs'
173
174
  'write:Write a file'
@@ -922,13 +922,26 @@ function inspectCharacterVault(row, mounts) {
922
922
  return status;
923
923
  }
924
924
 
925
- function cmdCharacters(args, ctx) {
925
+ async function cmdCharacters(args, ctx) {
926
926
  const { flags, positional } = parseSubArgs(args);
927
927
  const sub = positional[0] || 'status';
928
- if (sub !== 'status') {
929
- throw new Error(`Unknown characters subcommand: ${sub}. Try: status`);
928
+ switch (sub) {
929
+ case 'status':
930
+ return cmdCharactersStatus(flags, ctx);
931
+ case 'archives':
932
+ return cmdCharactersArchives(flags, ctx);
933
+ case 'archive':
934
+ return cmdCharactersArchiveVerb('archive', positional[1], flags, ctx);
935
+ case 'rehydrate':
936
+ return cmdCharactersArchiveVerb('rehydrate', positional[1], flags, ctx);
937
+ case 'export':
938
+ return cmdCharactersExport(positional[1], flags, ctx);
939
+ default:
940
+ throw new Error(`Unknown characters subcommand: ${sub}. Try: status, archives, archive, rehydrate, export`);
930
941
  }
942
+ }
931
943
 
944
+ function cmdCharactersStatus(flags, ctx) {
932
945
  const json = asBool(flags.json);
933
946
  const limit = asInt(flags.limit, 0);
934
947
  const onlyDiverged = asBool(flags.diverged);
@@ -1042,6 +1055,258 @@ function summarizeCharacterStatuses(all) {
1042
1055
  return { ok, diverged, missingFiles, noVault, empty, physIncomplete };
1043
1056
  }
1044
1057
 
1058
+ // ---------- characters: archive shelf ----------
1059
+
1060
+ const ARCHIVE_MAGIC = Buffer.from('QTAPARC1', 'ascii');
1061
+ const ARCHIVE_INTERNAL_PASSPHRASE = '__quilltap_no_passphrase__';
1062
+
1063
+ /**
1064
+ * List archived characters and the ARCHIVE bundle files on the shelf,
1065
+ * including loose bundles (files rows no character points at — the survivors
1066
+ * of a "keep archived bundles" wipe). Read-only.
1067
+ */
1068
+ function cmdCharactersArchives(flags, ctx) {
1069
+ const json = asBool(flags.json);
1070
+ const main = ctx.openMain();
1071
+ try {
1072
+ const cols = new Set(main.prepare('PRAGMA table_info(characters)').all().map(r => r.name));
1073
+ if (!cols.has('archivedAt')) {
1074
+ console.log('This database predates character archiving (no archivedAt column).');
1075
+ return;
1076
+ }
1077
+ const archived = main.prepare(
1078
+ 'SELECT id, name, archivedAt, archiveFileId FROM characters WHERE archivedAt IS NOT NULL ORDER BY archivedAt DESC'
1079
+ ).all();
1080
+ const bundles = main.prepare(
1081
+ "SELECT id, originalFilename, storageKey, size, createdAt FROM files WHERE category = 'ARCHIVE' ORDER BY createdAt DESC"
1082
+ ).all();
1083
+
1084
+ const referenced = new Set(archived.map(c => c.archiveFileId).filter(Boolean));
1085
+ const looseBundles = bundles.filter(b => !referenced.has(b.id));
1086
+
1087
+ if (json) {
1088
+ return printJson({ archivedCharacters: archived, bundles, looseBundles });
1089
+ }
1090
+
1091
+ if (archived.length === 0 && bundles.length === 0) {
1092
+ console.log('The archive shelf stands empty — no archived characters, no bundles.');
1093
+ return;
1094
+ }
1095
+
1096
+ if (archived.length > 0) {
1097
+ console.log(`Archived characters (${archived.length}):`);
1098
+ printTable(archived.map(c => ({
1099
+ id: c.id.slice(0, 8),
1100
+ name: truncate(c.name, 28),
1101
+ archivedAt: c.archivedAt,
1102
+ bundle: c.archiveFileId ? c.archiveFileId.slice(0, 8) : '(none — pre-bundle tombstone)',
1103
+ })));
1104
+ }
1105
+ if (bundles.length > 0) {
1106
+ console.log('');
1107
+ console.log(`Archive bundles (${bundles.length}${looseBundles.length > 0 ? `, ${looseBundles.length} loose` : ''}):`);
1108
+ printTable(bundles.map(b => ({
1109
+ id: b.id.slice(0, 8),
1110
+ file: truncate(b.originalFilename, 44),
1111
+ bytes: b.size,
1112
+ createdAt: b.createdAt,
1113
+ state: referenced.has(b.id) ? 'held by character' : 'loose (importable only)',
1114
+ })));
1115
+ }
1116
+ } finally {
1117
+ try { main.close(); } catch {}
1118
+ }
1119
+ }
1120
+
1121
+ /**
1122
+ * Archive or rehydrate a character through the RUNNING server's API. The
1123
+ * archive pipeline (export, encryption, prune) and the passphrase cache live
1124
+ * in the server process — the CLI cannot run them against the raw database —
1125
+ * so the server must be up, and it is the server that holds the instance
1126
+ * lock. `--write` is still required as the explicit opt-in to a write.
1127
+ */
1128
+ async function cmdCharactersArchiveVerb(verb, query, flags, ctx) {
1129
+ if (!query) {
1130
+ throw new Error(`Usage: characters ${verb} <name|id> --write [--port N]`);
1131
+ }
1132
+ if (!asBool(flags.write)) {
1133
+ throw new Error(`characters ${verb} changes data; add --write to proceed.`);
1134
+ }
1135
+ const port = asInt(flags.port, 3000);
1136
+
1137
+ const main = ctx.openMain();
1138
+ let character;
1139
+ try {
1140
+ character = resolveCharacter(main, String(query), ctx.openMounts);
1141
+ } finally {
1142
+ try { main.close(); } catch {}
1143
+ }
1144
+
1145
+ const url = `http://localhost:${port}/api/v1/characters/${encodeURIComponent(character.id)}?action=${verb}`;
1146
+ let res;
1147
+ try {
1148
+ res = await fetch(url, { method: 'POST' });
1149
+ } catch (err) {
1150
+ throw new Error(
1151
+ `Could not reach the Quilltap server at http://localhost:${port}: ${err.message}\n` +
1152
+ `The ${verb} operation runs inside the server (it needs the export pipeline and the ` +
1153
+ 'unlocked passphrase), so start the server first.'
1154
+ );
1155
+ }
1156
+ const body = await res.json().catch(() => ({}));
1157
+ if (!res.ok) {
1158
+ throw new Error(body.error || `${verb} failed with HTTP ${res.status}`);
1159
+ }
1160
+
1161
+ if (asBool(flags.json)) return printJson(body);
1162
+ if (verb === 'archive') {
1163
+ console.log(
1164
+ body.pruneComplete === false
1165
+ ? `${character.name} is archived, but the prune did not finish — run the same command again to complete it.`
1166
+ : `${character.name} rests in the archive. Bundle file: ${body.archiveFileId || '(none)'}.`
1167
+ );
1168
+ } else {
1169
+ const r = body.restored;
1170
+ console.log(
1171
+ r
1172
+ ? `${character.name} is awake again — ${r.memories} memories, ${r.documents} documents, ${r.blobs} blobs restored.`
1173
+ : `${character.name} is awake again.`
1174
+ );
1175
+ if (body.archiveBundleFileId) {
1176
+ console.log(`The archive bundle stays in the file library (file ${body.archiveBundleFileId}); delete it there if you no longer want the spare copy.`);
1177
+ }
1178
+ for (const w of body.warnings || []) console.log(`warning: ${w}`);
1179
+ }
1180
+ }
1181
+
1182
+ /** Parse + decrypt a QTAPARC1 bundle. Returns null on a wrong passphrase. */
1183
+ function tryDecryptArchiveBundle(data, passphrase) {
1184
+ const crypto = require('crypto');
1185
+ const headerLength = data.readUInt32BE(ARCHIVE_MAGIC.length);
1186
+ const bodyStart = ARCHIVE_MAGIC.length + 4 + headerLength;
1187
+ if (headerLength <= 0 || data.length < bodyStart + 16) {
1188
+ throw new Error('Archive bundle is truncated (bad header or missing auth tag).');
1189
+ }
1190
+ const header = JSON.parse(data.subarray(ARCHIVE_MAGIC.length + 4, bodyStart).toString('utf8'));
1191
+ const salt = Buffer.from(header.salt, 'hex');
1192
+ const iv = Buffer.from(header.iv, 'hex');
1193
+ const key = crypto.pbkdf2Sync(passphrase, new Uint8Array(salt), header.kdfIterations, 32, header.kdfDigest);
1194
+ const keyHash = crypto.createHash('sha256').update(new Uint8Array(key)).digest('hex');
1195
+ if (keyHash !== header.keyHash) return null;
1196
+
1197
+ const ciphertext = data.subarray(bodyStart, data.length - 16);
1198
+ const authTag = data.subarray(data.length - 16);
1199
+ const decipher = crypto.createDecipheriv(header.algorithm, new Uint8Array(key), new Uint8Array(iv));
1200
+ decipher.setAuthTag(new Uint8Array(authTag));
1201
+ return Buffer.concat([decipher.update(new Uint8Array(ciphertext)), decipher.final()]);
1202
+ }
1203
+
1204
+ /**
1205
+ * Export a character as a plaintext `.qtap` — the interchange escape hatch.
1206
+ *
1207
+ * Archived characters: decrypt their bundle straight off the disk (offline;
1208
+ * prompts for the passphrase on protected instances). This is the only way to
1209
+ * reach an archived character's packed-away material — mail, photographs,
1210
+ * summaries — without rehydrating. Live characters: proxy to the running
1211
+ * server's export pipeline. Read-only either way.
1212
+ */
1213
+ async function cmdCharactersExport(query, flags, ctx) {
1214
+ if (!query) {
1215
+ throw new Error('Usage: characters export <name|id> [--out <path>] [--port N]');
1216
+ }
1217
+
1218
+ const main = ctx.openMain();
1219
+ let character;
1220
+ let archiveFile = null;
1221
+ try {
1222
+ character = resolveCharacter(main, String(query), ctx.openMounts);
1223
+ const cols = new Set(main.prepare('PRAGMA table_info(characters)').all().map(r => r.name));
1224
+ if (cols.has('archivedAt')) {
1225
+ const row = main.prepare('SELECT archivedAt, archiveFileId FROM characters WHERE id = ?').get(character.id);
1226
+ if (row && row.archivedAt && row.archiveFileId) {
1227
+ archiveFile = main.prepare('SELECT id, storageKey, sha256 FROM files WHERE id = ?').get(row.archiveFileId);
1228
+ if (!archiveFile) {
1229
+ throw new Error(`${character.name} is archived but their bundle file row (${row.archiveFileId}) is missing.`);
1230
+ }
1231
+ } else if (row && row.archivedAt) {
1232
+ throw new Error(`${character.name} is a pre-bundle tombstone (no archive bundle exists to export).`);
1233
+ }
1234
+ }
1235
+ } finally {
1236
+ try { main.close(); } catch {}
1237
+ }
1238
+
1239
+ const safeName = String(character.name || character.id).replace(/[\\/:*?"<>|]/g, '_');
1240
+ const outPath = path.resolve(flags.out ? String(flags.out) : `${safeName}.qtap`);
1241
+
1242
+ let plaintext;
1243
+ if (archiveFile) {
1244
+ if (!archiveFile.storageKey) {
1245
+ throw new Error('The bundle row has no storage key; the file was never written.');
1246
+ }
1247
+ const bundlePath = path.join(ctx.dataDir, '..', 'files', archiveFile.storageKey);
1248
+ if (!fs.existsSync(bundlePath)) {
1249
+ throw new Error(`Bundle bytes not found on disk: ${bundlePath}`);
1250
+ }
1251
+ const data = fs.readFileSync(bundlePath);
1252
+
1253
+ if (!data.subarray(0, ARCHIVE_MAGIC.length).equals(ARCHIVE_MAGIC)) {
1254
+ // Pre-encryption plaintext bundle — pass it through untouched.
1255
+ plaintext = data;
1256
+ } else {
1257
+ plaintext = tryDecryptArchiveBundle(data, ARCHIVE_INTERNAL_PASSPHRASE);
1258
+ if (plaintext === null && process.env.QUILLTAP_DB_PASSPHRASE) {
1259
+ plaintext = tryDecryptArchiveBundle(data, process.env.QUILLTAP_DB_PASSPHRASE);
1260
+ }
1261
+ if (plaintext === null) {
1262
+ const { promptPassphrase } = require('./db-helpers');
1263
+ const pass = await promptPassphrase('Archive passphrase: ');
1264
+ if (pass) plaintext = tryDecryptArchiveBundle(data, pass);
1265
+ }
1266
+ if (plaintext === null) {
1267
+ throw new Error(
1268
+ 'That passphrase does not open this archive. If you changed your passphrase and this ' +
1269
+ 'bundle was reported left behind, it still wants the old one.'
1270
+ );
1271
+ }
1272
+ }
1273
+ } else {
1274
+ // Live character: the export pipeline lives in the server.
1275
+ const port = asInt(flags.port, 3000);
1276
+ const url = `http://localhost:${port}/api/v1/system/tools?action=export`;
1277
+ let res;
1278
+ try {
1279
+ res = await fetch(url, {
1280
+ method: 'POST',
1281
+ headers: { 'Content-Type': 'application/json' },
1282
+ body: JSON.stringify({
1283
+ type: 'characters',
1284
+ scope: 'selected',
1285
+ selectedIds: [character.id],
1286
+ includeMemories: true,
1287
+ }),
1288
+ });
1289
+ } catch (err) {
1290
+ throw new Error(
1291
+ `Could not reach the Quilltap server at http://localhost:${port}: ${err.message}\n` +
1292
+ 'Exporting a live character runs the server\'s export pipeline, so start the server first. ' +
1293
+ '(Archived characters export offline from their bundle.)'
1294
+ );
1295
+ }
1296
+ if (!res.ok) {
1297
+ const body = await res.json().catch(() => ({}));
1298
+ throw new Error(body.error || `Export failed with HTTP ${res.status}`);
1299
+ }
1300
+ plaintext = Buffer.from(await res.arrayBuffer());
1301
+ }
1302
+
1303
+ fs.writeFileSync(outPath, plaintext);
1304
+ console.log(`Wrote ${plaintext.length} bytes to ${outPath}`);
1305
+ if (archiveFile) {
1306
+ console.log('This is the decrypted archive bundle — a plaintext .qtap. Guard it accordingly.');
1307
+ }
1308
+ }
1309
+
1045
1310
  // ---------- verb: optimize ----------
1046
1311
 
1047
1312
  const OPTIMIZE_TARGETS = {
@@ -0,0 +1,201 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Docker Bind Planning for Filesystem Document Stores
5
+ *
6
+ * A container sees only the host paths handed to it at creation time. Database
7
+ * -backed stores live inside the data directory and ride along on the single
8
+ * bind every Quilltap container already has; filesystem and Obsidian stores
9
+ * point anywhere on the host and are, by default, simply absent inside the
10
+ * container — the store lists happily from the cached mount index while every
11
+ * read and write against the real bytes fails.
12
+ *
13
+ * This module turns "what stores does this instance have" into "what -v flags
14
+ * does the container need". It is deliberately pure: the caller supplies the
15
+ * store rows, the platform, and a path probe, so the planner can be tested
16
+ * without a database, a filesystem, or Docker.
17
+ *
18
+ * ## Why the binds are path-identical
19
+ *
20
+ * Each store is bound at its own host path (`-v /host/vault:/host/vault`) so
21
+ * the `basePath` recorded in the database resolves unchanged whether Quilltap
22
+ * runs natively or in a container. The alternative — mounting under some
23
+ * container-local prefix — would require a translation layer on every path in
24
+ * and out of the database, and would make the same instance directory
25
+ * unusable outside the container.
26
+ *
27
+ * Docker creates missing destination ancestors itself, as root and mode 0755,
28
+ * during mount setup — so binding `/Users/you/Vault` into an image that has no
29
+ * `/Users` works, and the unprivileged app user can still traverse in. Those
30
+ * ancestors are *not* writable by the app user, which is a feature: a store
31
+ * that was never bound stays structurally unwritable rather than quietly
32
+ * accumulating a fabricated directory tree.
33
+ *
34
+ * @module docker-mounts
35
+ */
36
+
37
+ const path = require('path');
38
+ const fs = require('fs');
39
+
40
+ /**
41
+ * Prefixes Docker Desktop for macOS shares with the VM out of the box. A bind
42
+ * whose source falls outside these is accepted by `docker run` but arrives in
43
+ * the container as an empty directory, which is a uniquely confusing failure —
44
+ * so it earns a warning rather than silence.
45
+ */
46
+ const MACOS_DEFAULT_SHARED_PREFIXES = ['/Users', '/Volumes', '/private', '/tmp', '/var/folders'];
47
+
48
+ /** Store types whose bytes live on the host filesystem rather than in the database. */
49
+ const FILESYSTEM_MOUNT_TYPES = new Set(['filesystem', 'obsidian']);
50
+
51
+ /**
52
+ * Normalise a base path for comparison: resolve `.`/`..`, collapse separators,
53
+ * and drop any trailing separator so `/a/b` and `/a/b/` are one path.
54
+ */
55
+ function normalisePath(basePath) {
56
+ const resolved = path.posix.normalize(String(basePath).trim());
57
+ if (resolved.length > 1 && resolved.endsWith('/')) {
58
+ return resolved.slice(0, -1);
59
+ }
60
+ return resolved;
61
+ }
62
+
63
+ /** True when `candidate` sits inside `ancestor` (and is not `ancestor` itself). */
64
+ function isDescendantOf(candidate, ancestor) {
65
+ return candidate.startsWith(ancestor.endsWith('/') ? ancestor : ancestor + '/');
66
+ }
67
+
68
+ /**
69
+ * Plan the bind mounts an instance's filesystem-backed stores require.
70
+ *
71
+ * @param {Array<object>} rows - doc_mount_points rows (id, name, mountType, basePath, enabled)
72
+ * @param {object} [options]
73
+ * @param {string} [options.platform] - process.platform value; defaults to the current host
74
+ * @param {(p: string) => boolean} [options.exists] - path probe, injectable for tests
75
+ * @returns {{binds: Array<object>, skipped: Array<object>, warnings: Array<string>, unsupported: boolean}}
76
+ */
77
+ function planStoreMounts(rows, options = {}) {
78
+ const platform = options.platform || process.platform;
79
+ const exists =
80
+ options.exists ||
81
+ ((p) => {
82
+ try {
83
+ return fs.statSync(p).isDirectory();
84
+ } catch {
85
+ return false;
86
+ }
87
+ });
88
+
89
+ const warnings = [];
90
+
91
+ // Windows host paths (C:\Users\…) have no in-container equivalent, so the
92
+ // path-identical scheme this module depends on cannot work there. Say so
93
+ // plainly rather than emitting binds that would silently misbehave.
94
+ if (platform === 'win32') {
95
+ return {
96
+ binds: [],
97
+ skipped: [],
98
+ warnings: [
99
+ 'Automatic store binds are not supported on Windows: container paths cannot mirror ' +
100
+ 'Windows host paths. Filesystem document stores must be bound manually.',
101
+ ],
102
+ unsupported: true,
103
+ };
104
+ }
105
+
106
+ const candidates = rows.filter(
107
+ (r) => FILESYSTEM_MOUNT_TYPES.has(r.mountType) && r.enabled && String(r.basePath || '').trim()
108
+ );
109
+
110
+ // Group stores by normalised path first — several stores commonly share one
111
+ // vault root, and they need exactly one bind between them.
112
+ const byPath = new Map();
113
+ for (const row of candidates) {
114
+ const normalised = normalisePath(row.basePath);
115
+ if (!path.posix.isAbsolute(normalised)) {
116
+ warnings.push(`Skipping store '${row.name}': base path '${row.basePath}' is not absolute.`);
117
+ continue;
118
+ }
119
+ if (!byPath.has(normalised)) {
120
+ byPath.set(normalised, []);
121
+ }
122
+ byPath.get(normalised).push(row.name);
123
+ }
124
+
125
+ const allPaths = [...byPath.keys()].sort();
126
+
127
+ const binds = [];
128
+ const skipped = [];
129
+
130
+ for (const hostPath of allPaths) {
131
+ const stores = byPath.get(hostPath);
132
+
133
+ // A path nested inside another selected path is already covered by that
134
+ // bind. Binding both is redundant, and Docker mounts them independently —
135
+ // which would shadow the parent's view of the child directory.
136
+ const ancestor = allPaths.find((other) => other !== hostPath && isDescendantOf(hostPath, other));
137
+ if (ancestor) {
138
+ continue;
139
+ }
140
+
141
+ if (!exists(hostPath)) {
142
+ // Never create the source. Docker would happily materialise a missing
143
+ // bind source as a root-owned empty directory, which presents an empty
144
+ // store as a healthy one — the exact failure this feature exists to end.
145
+ skipped.push({ hostPath, stores, reason: 'missing' });
146
+ continue;
147
+ }
148
+
149
+ if (platform === 'darwin' && !MACOS_DEFAULT_SHARED_PREFIXES.some((p) => hostPath === p || isDescendantOf(hostPath, p))) {
150
+ warnings.push(
151
+ `'${hostPath}' is outside Docker Desktop's default shared paths. Add it under ` +
152
+ 'Settings → Resources → File sharing, or the store will appear empty in the container.'
153
+ );
154
+ }
155
+
156
+ binds.push({ hostPath, containerPath: hostPath, stores });
157
+ }
158
+
159
+ if (platform === 'linux' && binds.length > 0) {
160
+ warnings.push(
161
+ 'On Linux, bind mounts preserve host ownership. If the container user cannot write to ' +
162
+ 'these paths, start the container with --user "$(id -u):$(id -g)".'
163
+ );
164
+ }
165
+
166
+ for (const entry of skipped) {
167
+ warnings.push(
168
+ `Skipping '${entry.hostPath}' (${entry.stores.join(', ')}): the path does not exist on this host.`
169
+ );
170
+ }
171
+
172
+ return { binds, skipped, warnings, unsupported: false };
173
+ }
174
+
175
+ /** Render a plan as `docker run` arguments. */
176
+ function toDockerArgs(plan) {
177
+ const args = [];
178
+ for (const bind of plan.binds) {
179
+ args.push('-v', `${bind.hostPath}:${bind.containerPath}`);
180
+ }
181
+ return args;
182
+ }
183
+
184
+ /**
185
+ * Compare a plan against the binds a container was actually created with.
186
+ * Returns the host paths a running container is missing, which is what tells
187
+ * an operator that a restart is owed.
188
+ */
189
+ function findMissingBinds(plan, existingSources) {
190
+ const existing = new Set(existingSources.map(normalisePath));
191
+ return plan.binds.filter((b) => !existing.has(b.hostPath));
192
+ }
193
+
194
+ module.exports = {
195
+ planStoreMounts,
196
+ toDockerArgs,
197
+ findMissingBinds,
198
+ normalisePath,
199
+ FILESYSTEM_MOUNT_TYPES,
200
+ MACOS_DEFAULT_SHARED_PREFIXES,
201
+ };
@@ -71,6 +71,8 @@ Read subcommands:
71
71
  grep [--mount <name|id|all>] [--ignore-case] [-l] [--max N] [--context N] <pattern>
72
72
  Substring search inside extracted text
73
73
  status [--mount <name|id>] [--top N] Per-mount extraction + embedding rollup
74
+ docker-mounts [--format args|json] Bind mounts this instance's filesystem
75
+ stores need to be visible in Docker
74
76
 
75
77
  Server-required subcommands (background-job queue lives in the running server):
76
78
  grep --semantic [--mount <name|id|all>] [--top N] [--threshold 0..1] <query>
@@ -210,6 +212,8 @@ function parseFlags(args) {
210
212
  threshold: -1,
211
213
  // base64 read/write flag
212
214
  base64: false,
215
+ // docker-mounts output shape: table (human), args (docker run flags), json
216
+ format: '',
213
217
  };
214
218
  const positional = [];
215
219
  let i = 0;
@@ -229,6 +233,7 @@ function parseFlags(args) {
229
233
  break;
230
234
  }
231
235
  case '--json': flags.json = true; break;
236
+ case '--format': flags.format = args[++i]; break;
232
237
  case '--uri': flags.uri = true; break;
233
238
  case '--rendered': flags.rendered = true; break;
234
239
  case '--folder': flags.folder = args[++i]; break;
@@ -487,6 +492,95 @@ async function handleList(flags) {
487
492
  }
488
493
  }
489
494
 
495
+ // ----------------------------------------------------------------------------
496
+ // docker-mounts
497
+ // ----------------------------------------------------------------------------
498
+
499
+ /**
500
+ * Report the bind mounts this instance's filesystem-backed stores need in
501
+ * order to be reachable from inside a container.
502
+ *
503
+ * `--format args` prints only the flags, one per line, so a start script can
504
+ * splice them into a `docker run` argv without parsing prose. Everything
505
+ * advisory goes to stderr for exactly that reason: stdout stays machine-clean
506
+ * even when there are warnings worth a human's attention.
507
+ */
508
+ async function handleDockerMounts(flags) {
509
+ const { planStoreMounts, toDockerArgs } = require('./docker-mounts');
510
+ const { db } = await openDb(flags);
511
+
512
+ let rows;
513
+ try {
514
+ rows = db.prepare(`
515
+ SELECT id, name, mountType, storeType, basePath, enabled
516
+ FROM doc_mount_points
517
+ WHERE mountType != 'database'
518
+ ORDER BY name COLLATE NOCASE
519
+ `).all();
520
+ } finally {
521
+ db.close();
522
+ }
523
+
524
+ const plan = planStoreMounts(rows);
525
+ const format = flags.format || (flags.json ? 'json' : 'table');
526
+
527
+ if (format === 'json') {
528
+ process.stdout.write(JSON.stringify(plan, null, 2) + '\n');
529
+ return;
530
+ }
531
+
532
+ if (format === 'args') {
533
+ for (const arg of toDockerArgs(plan)) {
534
+ process.stdout.write(arg + '\n');
535
+ }
536
+ for (const warning of plan.warnings) {
537
+ process.stderr.write(`warning: ${warning}\n`);
538
+ }
539
+ return;
540
+ }
541
+
542
+ if (plan.unsupported) {
543
+ for (const warning of plan.warnings) {
544
+ console.log(`${YELLOW}${warning}${RESET}`);
545
+ }
546
+ return;
547
+ }
548
+
549
+ if (plan.binds.length === 0 && plan.skipped.length === 0) {
550
+ console.log('(no filesystem-backed document stores — nothing to bind)');
551
+ return;
552
+ }
553
+
554
+ if (plan.binds.length > 0) {
555
+ console.log(`${BOLD}Bind mounts required:${RESET}`);
556
+ console.table(
557
+ plan.binds.map((b) => ({
558
+ 'host path': b.hostPath,
559
+ stores: b.stores.join(', '),
560
+ }))
561
+ );
562
+ }
563
+
564
+ if (plan.skipped.length > 0) {
565
+ console.log(`${BOLD}Skipped:${RESET}`);
566
+ console.table(
567
+ plan.skipped.map((s) => ({
568
+ 'host path': s.hostPath,
569
+ stores: s.stores.join(', '),
570
+ reason: s.reason,
571
+ }))
572
+ );
573
+ }
574
+
575
+ for (const warning of plan.warnings) {
576
+ console.log(`${YELLOW}warning:${RESET} ${warning}`);
577
+ }
578
+
579
+ console.log('');
580
+ console.log(`${DIM}Binds are applied when a container is created. Re-run the start script`);
581
+ console.log(`with --recreate to rebuild the container with these stores included.${RESET}`);
582
+ }
583
+
490
584
  // ----------------------------------------------------------------------------
491
585
  // show
492
586
  // ----------------------------------------------------------------------------
@@ -3033,6 +3127,9 @@ async function docsCommand(args) {
3033
3127
  case 'status':
3034
3128
  await handleStatus(flags);
3035
3129
  break;
3130
+ case 'docker-mounts':
3131
+ await handleDockerMounts(flags);
3132
+ break;
3036
3133
  case 'find':
3037
3134
  await handleFind(flags, positional);
3038
3135
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "quilltap",
3
- "version": "4.8.0-dev.186",
3
+ "version": "4.8.0-dev.192",
4
4
  "description": "Self-hosted AI workspace for writers, worldbuilders, and roleplayers. Run with npx quilltap.",
5
5
  "author": {
6
6
  "name": "Charles Sebold",