renor-engine 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (216) hide show
  1. package/README.md +34 -2
  2. package/THIRD-PARTY-NOTICES +2661 -0
  3. package/bin/renor-engine.cjs +38 -0
  4. package/dist/BUILD.json +6 -0
  5. package/dist/cli.js +1674 -516
  6. package/package.json +17 -80
  7. package/dist/.tsbuildinfo +0 -1
  8. package/dist/api/apiAuth.js +0 -43
  9. package/dist/api/server.js +0 -174
  10. package/dist/buildIdentity.js +0 -149
  11. package/dist/callPhases.js +0 -82
  12. package/dist/cli-extractors.js +0 -46
  13. package/dist/config.js +0 -65
  14. package/dist/context/composer.js +0 -320
  15. package/dist/context/readHonesty.js +0 -478
  16. package/dist/context/rowStore.js +0 -238
  17. package/dist/conversation/formatter.js +0 -81
  18. package/dist/conversation/parser.js +0 -193
  19. package/dist/conversation/types.js +0 -2
  20. package/dist/conversation/writer.js +0 -42
  21. package/dist/db/client.js +0 -149
  22. package/dist/db/handoffTable.js +0 -31
  23. package/dist/db/indexSalvage.js +0 -101
  24. package/dist/db/migrations.js +0 -670
  25. package/dist/db/readCache.js +0 -109
  26. package/dist/db/schema.js +0 -5
  27. package/dist/db/sqlite.js +0 -396
  28. package/dist/db/statements.js +0 -89
  29. package/dist/digests/digestCache.js +0 -146
  30. package/dist/digests/digests.js +0 -597
  31. package/dist/digests/worker.js +0 -350
  32. package/dist/embeddings/caching.js +0 -337
  33. package/dist/embeddings/coalescing.js +0 -177
  34. package/dist/embeddings/interactive.js +0 -103
  35. package/dist/embeddings/local.js +0 -70
  36. package/dist/embeddings/openai.js +0 -106
  37. package/dist/embeddings/rebuild.js +0 -166
  38. package/dist/embeddings/simdScan.js +0 -260
  39. package/dist/embeddings/space.js +0 -21
  40. package/dist/embeddings/types.js +0 -14
  41. package/dist/embeddings/vectorIndex.js +0 -674
  42. package/dist/embeddings/vertex.js +0 -334
  43. package/dist/eval/artifacts.js +0 -42
  44. package/dist/eval/cacheReport.js +0 -146
  45. package/dist/eval/comparison.js +0 -182
  46. package/dist/eval/context700-candidate.js +0 -145
  47. package/dist/eval/evalCache.js +0 -568
  48. package/dist/eval/hashEmbedding.js +0 -35
  49. package/dist/eval/indexSnapshots.js +0 -549
  50. package/dist/eval/longmemeval.js +0 -56
  51. package/dist/eval/metrics.js +0 -181
  52. package/dist/eval/pageDump.js +0 -67
  53. package/dist/eval/renorbenchBaseline.js +0 -486
  54. package/dist/eval/renorbenchDocuments.js +0 -134
  55. package/dist/eval/renorbenchProduct.js +0 -182
  56. package/dist/eval/runLedger.js +0 -279
  57. package/dist/eval/runManifest.js +0 -485
  58. package/dist/eval/scoring.js +0 -197
  59. package/dist/eval/textTokens.js +0 -17
  60. package/dist/evidence/attach.js +0 -264
  61. package/dist/evidence/background.js +0 -147
  62. package/dist/evidence/evidenceCache.js +0 -182
  63. package/dist/evidence/gate.js +0 -37
  64. package/dist/evidence/keyPool.js +0 -85
  65. package/dist/evidence/ollamaChat.js +0 -84
  66. package/dist/evidence/optin.js +0 -165
  67. package/dist/evidence/options.js +0 -151
  68. package/dist/evidence/secondPass.js +0 -294
  69. package/dist/extraction/classifyCapture.js +0 -34
  70. package/dist/extraction/decision-model.js +0 -174
  71. package/dist/extraction/eventExtractor.js +0 -113
  72. package/dist/extraction/extractionClient.js +0 -122
  73. package/dist/extraction/factExtractor.js +0 -46
  74. package/dist/extraction/factsWritePath.js +0 -179
  75. package/dist/extraction/jevDecisionModel.js +0 -307
  76. package/dist/extraction/preferenceExtractor.js +0 -150
  77. package/dist/extraction/secretGuard.js +0 -180
  78. package/dist/extraction/secretScanner.js +0 -665
  79. package/dist/extraction/typedFacts.js +0 -224
  80. package/dist/extraction/writePathAdjudication.js +0 -110
  81. package/dist/importing/apply.js +0 -672
  82. package/dist/importing/bundle-write.js +0 -80
  83. package/dist/importing/cli.js +0 -408
  84. package/dist/importing/existing.js +0 -51
  85. package/dist/importing/facts.js +0 -121
  86. package/dist/importing/forgetImported.js +0 -79
  87. package/dist/importing/hosted.js +0 -467
  88. package/dist/importing/journalLog.js +0 -96
  89. package/dist/importing/markdown.js +0 -98
  90. package/dist/importing/plan.js +0 -201
  91. package/dist/importing/secretScreen.js +0 -113
  92. package/dist/importing/serialize.js +0 -188
  93. package/dist/importing/source.js +0 -463
  94. package/dist/importing/types.js +0 -6
  95. package/dist/indexer/chunker.js +0 -319
  96. package/dist/indexer/dailyEntries.js +0 -63
  97. package/dist/indexer/hasher.js +0 -10
  98. package/dist/indexer/laneRuns.js +0 -48
  99. package/dist/indexer/o200k.js +0 -481
  100. package/dist/indexer/outsideRoot.js +0 -77
  101. package/dist/indexer/parser.js +0 -170
  102. package/dist/indexer/pipeline.js +0 -988
  103. package/dist/indexer/staleness.js +0 -201
  104. package/dist/indexer/tokenizer.js +0 -77
  105. package/dist/indexer/walker.js +0 -170
  106. package/dist/learning/cli.js +0 -113
  107. package/dist/learning/schema.js +0 -144
  108. package/dist/learning/settings.js +0 -45
  109. package/dist/learning/store.js +0 -267
  110. package/dist/learning/tap.js +0 -401
  111. package/dist/mcp/hive.js +0 -88
  112. package/dist/mcp/requestErrors.js +0 -47
  113. package/dist/mcp/server.js +0 -426
  114. package/dist/memory/agentLease.js +0 -43
  115. package/dist/memory/confinedWrite.js +0 -315
  116. package/dist/memory/controls.js +0 -73
  117. package/dist/memory/dailyNote.js +0 -265
  118. package/dist/memory/dreamer/adjudicate.js +0 -215
  119. package/dist/memory/dreamer/apply.js +0 -120
  120. package/dist/memory/dreamer/budget.js +0 -64
  121. package/dist/memory/dreamer/candidates.js +0 -149
  122. package/dist/memory/dreamer/collection.js +0 -104
  123. package/dist/memory/dreamer/dreamer.js +0 -352
  124. package/dist/memory/dreamer/review.js +0 -111
  125. package/dist/memory/durableAppend.js +0 -57
  126. package/dist/memory/factConflicts.js +0 -205
  127. package/dist/memory/factTerms.js +0 -321
  128. package/dist/memory/handoff.js +0 -363
  129. package/dist/memory/hiveNameHeader.cjs +0 -10
  130. package/dist/memory/inventory.js +0 -55
  131. package/dist/memory/invisibleText.js +0 -20
  132. package/dist/memory/lifecycle.js +0 -2014
  133. package/dist/memory/limits.js +0 -13
  134. package/dist/memory/requests.js +0 -183
  135. package/dist/memory/sourceName.js +0 -276
  136. package/dist/memory/statusFacts.js +0 -61
  137. package/dist/memory/whatsNew.js +0 -158
  138. package/dist/memory/writer.js +0 -176
  139. package/dist/memory/writerConflicts.js +0 -85
  140. package/dist/memorymap/memoryMap.js +0 -1594
  141. package/dist/memorymap/replacedJournal.js +0 -58
  142. package/dist/memorymap/replacements.js +0 -410
  143. package/dist/mvp/activity.js +0 -86
  144. package/dist/mvp/agentMemory.js +0 -1803
  145. package/dist/mvp/answerCheck.js +0 -211
  146. package/dist/mvp/auth.js +0 -531
  147. package/dist/mvp/cli.js +0 -86
  148. package/dist/mvp/client-store.js +0 -462
  149. package/dist/mvp/compileCache.js +0 -242
  150. package/dist/mvp/compileSeed.js +0 -33
  151. package/dist/mvp/config.js +0 -292
  152. package/dist/mvp/connectionStatus.js +0 -80
  153. package/dist/mvp/contextDistinct.js +0 -53
  154. package/dist/mvp/contextProfile.js +0 -87
  155. package/dist/mvp/contextRelevance.js +0 -413
  156. package/dist/mvp/directorySurface.js +0 -68
  157. package/dist/mvp/engine.js +0 -1078
  158. package/dist/mvp/exportAll.js +0 -393
  159. package/dist/mvp/factLanePack.js +0 -77
  160. package/dist/mvp/factMarkers.js +0 -65
  161. package/dist/mvp/factsLane.js +0 -203
  162. package/dist/mvp/hiddenSpans.js +0 -585
  163. package/dist/mvp/history.js +0 -414
  164. package/dist/mvp/hitDates.js +0 -31
  165. package/dist/mvp/hitFloor.js +0 -36
  166. package/dist/mvp/identity.js +0 -18
  167. package/dist/mvp/idle.js +0 -113
  168. package/dist/mvp/log.js +0 -118
  169. package/dist/mvp/memoryDir.js +0 -98
  170. package/dist/mvp/posthogMcp.js +0 -210
  171. package/dist/mvp/readScope.js +0 -104
  172. package/dist/mvp/readUsefulness.js +0 -222
  173. package/dist/mvp/sanitize.js +0 -48
  174. package/dist/mvp/schemaRejections.js +0 -91
  175. package/dist/mvp/scope.js +0 -127
  176. package/dist/mvp/server.js +0 -2712
  177. package/dist/mvp/serverCard.js +0 -38
  178. package/dist/mvp/storedSize.js +0 -139
  179. package/dist/mvp/telemetry.js +0 -474
  180. package/dist/net/poolFetch.js +0 -174
  181. package/dist/recall/actionRecall.js +0 -282
  182. package/dist/recall/autoRecall.js +0 -447
  183. package/dist/recall/searchRecall.js +0 -189
  184. package/dist/retrieval/adviceLines.js +0 -195
  185. package/dist/retrieval/adviceQuestion.js +0 -44
  186. package/dist/retrieval/conversationShape.js +0 -44
  187. package/dist/retrieval/dailyEntryTime.js +0 -55
  188. package/dist/retrieval/entityLookup.js +0 -162
  189. package/dist/retrieval/factsFirst.js +0 -188
  190. package/dist/retrieval/keyword.js +0 -84
  191. package/dist/retrieval/ledgerEventTime.js +0 -51
  192. package/dist/retrieval/ledgerLiveness.js +0 -83
  193. package/dist/retrieval/ledgerPresentation.js +0 -161
  194. package/dist/retrieval/noteIdentity.js +0 -57
  195. package/dist/retrieval/orchestrator.js +0 -1224
  196. package/dist/retrieval/plainWording.js +0 -236
  197. package/dist/retrieval/queryHelpers.js +0 -142
  198. package/dist/retrieval/questionSpan.js +0 -475
  199. package/dist/retrieval/rankingTrace.js +0 -184
  200. package/dist/retrieval/readerEvents.js +0 -690
  201. package/dist/retrieval/readerLayout.js +0 -422
  202. package/dist/retrieval/relativeTimeHints.js +0 -113
  203. package/dist/retrieval/semantic.js +0 -67
  204. package/dist/router/routerCache.js +0 -106
  205. package/dist/router/sessionRouter.js +0 -726
  206. package/dist/sharing/cli.js +0 -181
  207. package/dist/sharing/examples.js +0 -136
  208. package/dist/sharing/fields.js +0 -225
  209. package/dist/sharing/loop.js +0 -80
  210. package/dist/sharing/project.js +0 -327
  211. package/dist/sharing/send.js +0 -466
  212. package/dist/sharing/state.js +0 -222
  213. package/dist/sharing/wording.js +0 -48
  214. package/dist/toolRefusal.js +0 -120
  215. package/dist/watcher/watcher.js +0 -77
  216. /package/dist/{indexer/o200k.json → o200k.json} +0 -0
package/dist/db/client.js DELETED
@@ -1,149 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.openDb = openDb;
7
- exports.openIndexDb = openIndexDb;
8
- exports.openReadOnlyDb = openReadOnlyDb;
9
- const sqlite_1 = __importDefault(require("./sqlite"));
10
- const fs_1 = __importDefault(require("fs"));
11
- const path_1 = __importDefault(require("path"));
12
- const migrations_1 = require("./migrations");
13
- const indexSalvage_1 = require("./indexSalvage");
14
- const log_1 = require("../mvp/log");
15
- const log = (0, log_1.createLogger)('engine');
16
- /**
17
- * Map up to this much of the database file into memory for reads (`PRAGMA mmap_size`). SQLite maps only
18
- * what exists, so a small database is untouched; the ceiling is address space, not memory.
19
- *
20
- * Why: a sqlite-vec nearest-neighbour query reads every vector chunk, each a multi-megabyte BLOB, and
21
- * SQLite reads a BLOB's overflow pages straight from the file, one pread() per 4 KB page, bypassing its
22
- * page cache (so a bigger cache_size does not help). On a 10,000-note workspace that was ~60% kernel time
23
- * per query. Reading through the mapping turns those syscalls into copies and cut the scan's CPU by about
24
- * a third (scripts/bench/mcp-latency.ts). Only reads change: writes still go through write(), and answers
25
- * are identical.
26
- *
27
- * The one thing a mapped read cannot survive is the file shrinking under it. The engine never VACUUMs or
28
- * truncates its database, and the data-deletion runbook stops the service before touching the files.
29
- * Not applied to `openReadOnlyDb`, which inspects or imports from files this process does not own.
30
- */
31
- const MMAP_SIZE_BYTES = 1024 * 1024 * 1024;
32
- function openDb(dbPath) {
33
- const onDisk = dbPath !== ':memory:' && dbPath !== '';
34
- if (onDisk) {
35
- // The index holds every remembered statement in plain text. A new one is created owner-only whatever the
36
- // umask; SQLite gives its -wal and -shm files the main file's mode. An existing file keeps its mode.
37
- fs_1.default.mkdirSync(path_1.default.dirname(dbPath), { recursive: true, mode: 0o700 });
38
- fs_1.default.closeSync(fs_1.default.openSync(dbPath, fs_1.default.constants.O_CREAT | fs_1.default.constants.O_WRONLY, 0o600));
39
- }
40
- const db = new sqlite_1.default(dbPath);
41
- try {
42
- db.pragma('journal_mode = WAL');
43
- db.pragma('foreign_keys = ON');
44
- db.pragma(`mmap_size = ${MMAP_SIZE_BYTES}`);
45
- }
46
- catch (err) {
47
- // A file that is not a database fails on its first pragma; do not leave its handle open for the caller that never gets it.
48
- db.close();
49
- throw err;
50
- }
51
- return db;
52
- }
53
- /**
54
- * Opens the derived index, and rebuilds it when it is damaged. The index is rebuilt from the ledger and the Markdown
55
- * (the CLI banner says so), so a damaged `index.db` is not a reason to refuse to start: it is moved aside, with its
56
- * `-wal` and `-shm`, to `index.db.corrupt-<timestamp>` (kept for forensics) and the caller gets a fresh index, exactly
57
- * as for a missing one. Only corruption is moved aside: an open that fails for another reason (permissions, I/O) throws.
58
- * The move needs the ledger to rebuild from, so with no ledger a damaged index throws rather than being set aside.
59
- * The ledger itself is never touched here.
60
- */
61
- function openIndexDb(dbPath, ledgerPath) {
62
- let damage;
63
- try {
64
- const db = openDb(dbPath);
65
- let verdict;
66
- try {
67
- verdict = db.pragma('quick_check', { simple: true });
68
- }
69
- catch (err) {
70
- db.close();
71
- throw err;
72
- }
73
- if (verdict === 'ok')
74
- return db;
75
- db.close();
76
- damage = String(verdict);
77
- }
78
- catch (err) {
79
- if (!isCorruptionError(err))
80
- throw err;
81
- damage = err.message;
82
- }
83
- if (!fs_1.default.existsSync(ledgerPath)) {
84
- throw new Error(`index ${dbPath} is damaged (${damage}) and there is no ledger to rebuild it from; it was not moved aside. Restore the ledger, or move the index aside by hand.`);
85
- }
86
- // Read what the damaged file still gives before it is moved: the pending requests and cursors are not in the ledger.
87
- const salvage = readDamagedIndex(dbPath);
88
- const aside = setIndexAside(dbPath);
89
- const db = openDb(dbPath);
90
- (0, migrations_1.runMigrations)(db);
91
- const { restored, rejected } = (0, indexSalvage_1.restoreIndexTables)(db, salvage);
92
- log.error('index_rebuilt_from_ledger', { index: path_1.default.basename(dbPath), damage, movedTo: path_1.default.basename(aside), salvage: (0, indexSalvage_1.describeSalvage)(salvage, restored, rejected) });
93
- return db;
94
- }
95
- /** The rows the ledger does not hold, read from the damaged file, read-only (openReadOnlyDb: the file is not written). */
96
- function readDamagedIndex(dbPath) {
97
- let db;
98
- try {
99
- db = openReadOnlyDb(dbPath);
100
- }
101
- catch (err) {
102
- return (0, indexSalvage_1.unreadableSalvage)(err instanceof Error ? err.message : String(err));
103
- }
104
- try {
105
- return (0, indexSalvage_1.salvageIndexTables)(db);
106
- }
107
- finally {
108
- db.close();
109
- }
110
- }
111
- /** A SQLite error that says the file is damaged, as opposed to unreadable for some other reason. */
112
- function isCorruptionError(err) {
113
- const code = err.code;
114
- return typeof code === 'string' && (code === 'SQLITE_NOTADB' || code === 'SQLITE_CORRUPT' || code.startsWith('SQLITE_CORRUPT_'));
115
- }
116
- /**
117
- * Renames the index's side files first and the main file last: if this is interrupted, the damaged main file is still
118
- * there to be found again on the next start, and no WAL is ever left beside a fresh database it does not belong to.
119
- */
120
- function setIndexAside(dbPath) {
121
- const stamp = new Date().toISOString().replace(/[:.]/g, '-');
122
- let aside = `${dbPath}.corrupt-${stamp}`;
123
- for (let n = 2; ['', '-wal', '-shm'].some(side => fs_1.default.existsSync(aside + side)); n++)
124
- aside = `${dbPath}.corrupt-${stamp}-${n}`;
125
- for (const side of ['-wal', '-shm', '']) {
126
- if (fs_1.default.existsSync(dbPath + side))
127
- fs_1.default.renameSync(dbPath + side, aside + side);
128
- }
129
- return aside;
130
- }
131
- /**
132
- * Read-only open of a SQLite database by path — a product index being inspected, or a
133
- * source index an import reads from.
134
- *
135
- * Read-only is enforced by SQLite itself rather than by convention, so a read can never
136
- * write to the store it is reading. That matters for the WAL side files too: a
137
- * write-capable open of a WAL database can create `-wal`/`-shm` and checkpoint back into
138
- * the main file. `journal_mode` is deliberately NOT set here, because setting it is a
139
- * write. Product code must reach product SQLite through this module
140
- * (`tests/eval/product-path-contract.test.ts` guards the `openDb` boundary).
141
- */
142
- function openReadOnlyDb(dbPath) {
143
- if (!fs_1.default.existsSync(dbPath)) {
144
- throw new Error(`database not found: ${dbPath}`);
145
- }
146
- const db = new sqlite_1.default(dbPath, { readonly: true, fileMustExist: true });
147
- db.pragma('foreign_keys = ON');
148
- return db;
149
- }
@@ -1,31 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.HANDOFF_TABLE = void 0;
4
- exports.ensureHandoffTable = ensureHandoffTable;
5
- exports.hasHandoffTable = hasHandoffTable;
6
- /**
7
- * The handoff note's tables (memory/handoff.ts): the notes, and the one-row high-water mark of the clock they expire
8
- * by. They are not a migration: a workspace with RENOR_HANDOFF_NOTE off never gets them, so its schema and its bytes
9
- * stay those of a workspace without this code. They are created on the first write, read or expiry sweep, all of which
10
- * run only with the flag on, and by index salvage only when the damaged index had the notes.
11
- */
12
- exports.HANDOFF_TABLE = 'handoff_notes';
13
- const DDL = `CREATE TABLE IF NOT EXISTS handoff_notes (
14
- repo TEXT NOT NULL,
15
- branch TEXT NOT NULL,
16
- note TEXT NOT NULL,
17
- writer TEXT NOT NULL,
18
- written_at TEXT NOT NULL,
19
- expires_at TEXT NOT NULL,
20
- PRIMARY KEY (repo, branch)
21
- );
22
- CREATE TABLE IF NOT EXISTS handoff_clock (
23
- id INTEGER PRIMARY KEY CHECK (id = 1),
24
- high_water INTEGER NOT NULL
25
- )`;
26
- function ensureHandoffTable(db) {
27
- db.exec(DDL);
28
- }
29
- function hasHandoffTable(db) {
30
- return db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?").get(exports.HANDOFF_TABLE) !== undefined;
31
- }
@@ -1,101 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.INDEX_ONLY_TABLES = void 0;
4
- exports.unreadableSalvage = unreadableSalvage;
5
- exports.salvageIndexTables = salvageIndexTables;
6
- exports.restoreIndexTables = restoreIndexTables;
7
- exports.describeSalvage = describeSalvage;
8
- const handoffTable_1 = require("./handoffTable");
9
- /**
10
- * The index is rebuilt from the ledger, but a few of its tables hold state that no ledger event rebuilds: the pending
11
- * agent requests, the what's-new cursors, and the connection names the workspace has seen (known_agents). When the
12
- * index is damaged, those rows are read out of the damaged file, read-only, and written into the rebuilt one.
13
- */
14
- exports.INDEX_ONLY_TABLES = ['agent_requests', 'agent_cursors', 'known_agents'];
15
- /** What a damaged index gives when it cannot be opened at all: every table is unreadable, for the same reason. */
16
- function unreadableSalvage(reason) {
17
- const salvage = { rows: {}, unreadable: {} };
18
- for (const table of exports.INDEX_ONLY_TABLES) {
19
- salvage.rows[table] = [];
20
- salvage.unreadable[table] = reason;
21
- }
22
- return salvage;
23
- }
24
- /** Reads what an open (read-only) damaged index still gives. Never throws: a table that does not read says why. */
25
- function salvageIndexTables(db) {
26
- const salvage = { rows: {}, unreadable: {} };
27
- for (const table of tablesIn(db)) {
28
- const rows = [];
29
- salvage.rows[table] = rows;
30
- try {
31
- for (const row of db.prepare(`SELECT rowid AS __rowid, * FROM "${table}"`).iterate()) {
32
- rows.push(row);
33
- }
34
- }
35
- catch (err) {
36
- // An index from before this table existed has nothing to salvage here; that is not a loss.
37
- if (!/no such table/.test(errorText(err)))
38
- salvage.unreadable[table] = errorText(err);
39
- }
40
- }
41
- return salvage;
42
- }
43
- /** Writes the salvaged rows into a freshly migrated index. A row that will not go in is counted, never skipped silently. */
44
- function restoreIndexTables(db, salvage) {
45
- const restored = {};
46
- const rejected = {};
47
- // The handoff table is created on first use (db/handoffTable.ts), so the rebuilt index has it only when it is owed rows.
48
- if ((salvage.rows[handoffTable_1.HANDOFF_TABLE]?.length ?? 0) > 0)
49
- (0, handoffTable_1.ensureHandoffTable)(db);
50
- for (const table of tablesOf(salvage)) {
51
- restored[table] = 0;
52
- rejected[table] = 0;
53
- for (const row of salvage.rows[table] ?? []) {
54
- const { __rowid, ...values } = row;
55
- const cols = ['rowid', ...Object.keys(values)];
56
- const sql = `INSERT OR IGNORE INTO "${table}" (${cols.map(c => `"${c}"`).join(', ')}) VALUES (${cols.map(() => '?').join(', ')})`;
57
- try {
58
- const info = db.prepare(sql).run(__rowid, ...Object.keys(values).map(c => values[c]));
59
- restored[table] += info.changes;
60
- }
61
- catch {
62
- rejected[table] += 1;
63
- }
64
- }
65
- }
66
- return { restored, rejected };
67
- }
68
- /** One sentence for the log: what came back from the damaged index, and what did not. */
69
- function describeSalvage(salvage, restored, rejected) {
70
- const kept = tablesOf(salvage).map(t => `${t} ${restored[t] ?? 0}`).join(', ');
71
- const lost = tablesOf(salvage)
72
- .map(t => {
73
- const parts = [];
74
- if (rejected[t])
75
- parts.push(`${rejected[t]} row(s) could not be written`);
76
- if (salvage.unreadable[t])
77
- parts.push(`the damaged file could not be read (${salvage.unreadable[t]}), so rows past the damage are gone and not counted`);
78
- return parts.length ? `${t}: ${parts.join('; ')}` : '';
79
- })
80
- .filter(Boolean);
81
- return `Kept from the damaged index: ${kept}.${lost.length ? ` NOT RECOVERED: ${lost.join('. ')}.` : ''}`;
82
- }
83
- function errorText(err) {
84
- return err instanceof Error ? err.message : String(err);
85
- }
86
- /**
87
- * The tables to salvage from a damaged index: INDEX_ONLY_TABLES, then the handoff notes when that index has their table.
88
- * A workspace with the handoff note off never has it, so its salvage, and the log sentence, are what they were without it.
89
- */
90
- function tablesIn(db) {
91
- let handoff = false;
92
- try {
93
- handoff = (0, handoffTable_1.hasHandoffTable)(db);
94
- }
95
- catch { /* an unreadable schema: the other tables say why */ }
96
- return handoff ? [...exports.INDEX_ONLY_TABLES, handoffTable_1.HANDOFF_TABLE] : [...exports.INDEX_ONLY_TABLES];
97
- }
98
- /** The tables a salvage carries: INDEX_ONLY_TABLES, then the handoff notes when they were salvaged. */
99
- function tablesOf(salvage) {
100
- return handoffTable_1.HANDOFF_TABLE in salvage.rows ? [...exports.INDEX_ONLY_TABLES, handoffTable_1.HANDOFF_TABLE] : [...exports.INDEX_ONLY_TABLES];
101
- }