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
@@ -1,238 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ProvenanceError = void 0;
4
- exports.normDedupeValue = normDedupeValue;
5
- exports.matchForm = matchForm;
6
- exports.splitWords = splitWords;
7
- exports.dedupeKeyFor = dedupeKeyFor;
8
- exports.insertStructuredRows = insertStructuredRows;
9
- exports.deleteStructuredRowsForSource = deleteStructuredRowsForSource;
10
- exports.deleteStructuredRowsForSourceKind = deleteStructuredRowsForSourceKind;
11
- exports.readCurrentRowsForNotes = readCurrentRowsForNotes;
12
- const orchestrator_1 = require("../retrieval/orchestrator");
13
- class ProvenanceError extends Error {
14
- }
15
- exports.ProvenanceError = ProvenanceError;
16
- /** NFKD → fold Latin diacritics → keep letters/numbers/marks from every script → lowercase → collapse.
17
- *
18
- * Letters and numbers are kept from EVERY script (`\p{L}`/`\p{N}`), never an
19
- * ASCII range. The previous `[^\x20-\x7E]` + `[^a-z0-9]` pair deleted every
20
- * non-Latin character, so two distinct memories written in Chinese, Japanese,
21
- * Cyrillic, Greek, Arabic, Hebrew or Devanagari both normalized to `''` and
22
- * shared one identity — and `decision-model` then called one `identical` to the
23
- * other, folding the candidate into the existing row's confirm_count instead of
24
- * storing it. Silent data loss.
25
- *
26
- * Combining marks are folded ONLY after a Latin base letter, which is what keeps
27
- * the existing accent folding (`Café` → `cafe`) without destroying the marks
28
- * that ARE letters in other scripts. A blanket `\p{M}` strip looks equivalent on
29
- * Latin but merges meaning-changing pairs in scripts this bug report names:
30
- * `が`/`か`, `ぱ`/`は`, `की`/`क`, `मिल`/`मील`, `น้ำ`/`นำ` (measured; pinned by test).
31
- * To go back to the blanket strip, delete only the lookbehind (making the two
32
- * mark clauses `\p{M}` in both places).
33
- *
34
- * `\p{L}` is `a-zA-Z` and `\p{N}` is `0-9` within ASCII, so ASCII identities are
35
- * unchanged. `\p{C}` (control, format, private-use, unassigned) is REMOVED rather
36
- * than treated as a separator, matching what the old `[^\x20-\x7E]` filter did to
37
- * those characters — so every ASCII input stays byte-identical, including one
38
- * carrying a control character. That preserves one pre-existing quirk: a control
39
- * character between two word characters glues them (`'a\u0000b'` → `ab`);
40
- * separating instead would move ASCII identities, so it is left out of scope.
41
- *
42
- * Values with no letters or numbers (punctuation, symbols, emoji) still normalize
43
- * to `''`, exactly as before. Dataset-free. */
44
- function normDedupeValue(value) {
45
- return value
46
- .normalize('NFKD')
47
- .replace(/(?<=\p{Script=Latin})\p{M}+/gu, '')
48
- .toLowerCase()
49
- .replace(/\p{C}+/gu, '')
50
- .replace(/[^\p{L}\p{N}\p{M}]+/gu, ' ')
51
- .trim();
52
- }
53
- /** The read side's word separator: the SAME class `normDedupeValue` uses above, so a query
54
- * is cut into words exactly as the text it must match is. `\p{L}`/`\p{N}`/`\p{M}` cover
55
- * every script; both are exactly `a-z`/`0-9`/nothing within ASCII, so an ASCII query is
56
- * cut into byte-identical words.
57
- *
58
- * Splitting on the ASCII range instead (`[^a-z0-9]`) deletes every non-ASCII letter, and a
59
- * word that is *entirely* non-ASCII is deleted whole rather than shortened: `não` became
60
- * `n`+`o`, `東京` became nothing, `東京都` became nothing. A read then lost the identity of
61
- * the very facts it was written to find — measured on this repo's read paths in
62
- * `tests/context/readSideUnicodeTokens.test.ts`: a `東京都` query scored every row 0, so
63
- * `大阪に通っている` (newer, irrelevant) outranked `東京都に住んでいる`, and the query
64
- * scoped to no row at all, returning the whole table.
65
- *
66
- * `numeric` additionally keeps `.` and `%` inside a word, so a measurement stays one word
67
- * ("3.5%", "42 Wh"). Both variants were the pre-existing ASCII classes plus `\p{L}\p{N}\p{M}`
68
- * widened from `a-z0-9`, so no ASCII word changes.
69
- *
70
- * Deliberately NOT NFKD-folded, unlike `normDedupeValue`: the read side matches its terms
71
- * with `includes()` against the RAW stored text (`rowSearchText` + `indexOf`), so folding
72
- * here would stop a term from matching the very row it names. Marks are kept as word
73
- * characters for the same reason the write side keeps them for non-Latin scripts. */
74
- const WORD_SEPARATOR = /[^\p{L}\p{N}\p{M}]+/u;
75
- const WORD_SEPARATOR_NUMERIC = /[^\p{L}\p{N}\p{M}.%]+/u;
76
- /** The one form the read side matches on: NFC, then lowercased. Applied to BOTH the query
77
- * and the text it is matched against, so a query matches a stored fact however either was
78
- * spelled. Stored payloads are never normalized (they are written as given), and NFC is
79
- * *not* the same string as NFD — macOS-authored text, pasted filenames and Obsidian on Mac
80
- * all produce NFD — so an NFC query term would otherwise miss an NFD fact (`café` vs
81
- * `café`) that a raw substring test cannot bridge. ASCII is NFC-stable, so this is
82
- * identity on every ASCII input. NOT NFKD and NOT accent-folded: the match is a raw
83
- * `includes()`, so folding a term would stop it matching the very row it names, which is
84
- * also why the write side's `normDedupeValue` folding cannot be reused here. */
85
- function matchForm(text) {
86
- return (text ?? '').normalize('NFC').toLowerCase();
87
- }
88
- /** Lowercased NFC words of `text` in order, repeats included — callers de-duplicate. A query
89
- * and the stored text it is matched against are both put in `matchForm`, so the cut cannot
90
- * differ by side. */
91
- function splitWords(text, numeric = false) {
92
- return matchForm(text).split(numeric ? WORD_SEPARATOR_NUMERIC : WORD_SEPARATOR);
93
- }
94
- /** dedupe_key per ADR §4: fact (value, object), event (when, event), pref (statement). */
95
- function dedupeKeyFor(row) {
96
- switch (row.rowKind) {
97
- case 'fact': {
98
- const p = row.payload;
99
- return `fact|${normDedupeValue(p.value)}|${normDedupeValue(p.object)}`;
100
- }
101
- case 'event': {
102
- const p = row.payload;
103
- return `event|${normDedupeValue(p.when)}|${normDedupeValue(p.event)}`;
104
- }
105
- case 'preference': {
106
- const p = row.payload;
107
- return `pref|${normDedupeValue(p.statement)}`;
108
- }
109
- }
110
- }
111
- function assertProvenance(row) {
112
- if (!row.sourcePath || !row.sourcePath.trim()) {
113
- throw new ProvenanceError(`structured row (${row.rowKind}) rejected: missing source_path`);
114
- }
115
- if (!Number.isInteger(row.sourceChunkId) || row.sourceChunkId <= 0) {
116
- throw new ProvenanceError(`structured row (${row.rowKind}) from ${row.sourcePath} rejected: missing source_chunk_id`);
117
- }
118
- }
119
- function parsePayload(_rowKind, raw) {
120
- return JSON.parse(raw);
121
- }
122
- /**
123
- * Insert rows with identity-keyed supersession (ADR-0003 §4.1). A new row
124
- * supersedes a current row only when they share (row_kind, scope, dedupe_key)
125
- * AND both content dates are known AND the new source is not older. When
126
- * either date is NULL (unknown), supersession never fires: both rows stay
127
- * current and the composer orders them deterministically.
128
- *
129
- * Same-identity double-insert from the SAME source cannot occur: migration
130
- * v11's partial UNIQUE index (current rows only) closes the check-then-act
131
- * race across `await model.decide`, and a lost race is re-checked here and
132
- * skipped (the winning writer's row already covers the identity) instead of
133
- * double-inserting. Cross-note coexistence (different source_path) is
134
- * preserved by the index's source_path column.
135
- */
136
- function insertStructuredRows(db, rows, opts = {}) {
137
- const insert = db.prepare(`
138
- INSERT INTO structured_rows
139
- (row_kind, source_path, source_hash, source_chunk_id, scope, extraction_version,
140
- valid_from, valid_to, payload_json, dedupe_key, created_at)
141
- VALUES (?, ?, ?, ?, ?, ?, ?, NULL, ?, ?, ?)
142
- `);
143
- // A content date is only as late as its file's write time. Rows stored before this bound existed are bounded the same
144
- // way wherever they are read, including here, so a stale future date cannot outrank a normal later update.
145
- const findCurrent = db.prepare(`
146
- SELECT r.id, r.valid_from, n.modified_at
147
- FROM structured_rows r LEFT JOIN notes n ON n.path = r.source_path
148
- WHERE r.row_kind = ? AND r.scope = ? AND r.dedupe_key = ? AND r.valid_to IS NULL
149
- `);
150
- const writeTime = db.prepare('SELECT modified_at FROM notes WHERE path = ?');
151
- const supersede = db.prepare('UPDATE structured_rows SET valid_to = ? WHERE id = ?');
152
- db.transaction(() => {
153
- for (const row of rows) {
154
- assertProvenance(row);
155
- const validFrom = (0, orchestrator_1.boundContentDate)(row.validFrom, writeTime.get(row.sourcePath)?.modified_at);
156
- const dedupeKey = dedupeKeyFor(row);
157
- const scope = row.scope ?? 'global';
158
- const now = new Date().toISOString();
159
- if (validFrom !== null && !opts.skipSupersession) {
160
- for (const existing of findCurrent.all(row.rowKind, scope, dedupeKey)) {
161
- // Supersede only on identity collision with known dates on both
162
- // sides; the newer (or same-date re-extraction) source wins.
163
- const existingFrom = (0, orchestrator_1.boundContentDate)(existing.valid_from, existing.modified_at);
164
- if (existingFrom !== null && validFrom >= existingFrom) {
165
- supersede.run(validFrom, existing.id);
166
- }
167
- }
168
- }
169
- try {
170
- insert.run(row.rowKind, row.sourcePath, row.sourceHash, row.sourceChunkId, scope, row.extractionVersion, validFrom, JSON.stringify(row.payload), dedupeKey, now);
171
- }
172
- catch (err) {
173
- const code = err?.code ?? '';
174
- const message = err instanceof Error ? err.message : String(err);
175
- if (code === 'SQLITE_CONSTRAINT_UNIQUE' || /UNIQUE constraint failed/.test(message)) {
176
- // Lost a same-identity insert race against a concurrent writer for
177
- // this source (migration v11 partial UNIQUE index). Re-check: a
178
- // current row already covers the identity from this source — skip
179
- // instead of double-inserting; anything else is a real error.
180
- const dupe = db
181
- .prepare(`SELECT id FROM structured_rows
182
- WHERE row_kind = ? AND scope = ? AND dedupe_key = ? AND source_path = ? AND valid_to IS NULL LIMIT 1`)
183
- .get(row.rowKind, scope, dedupeKey, row.sourcePath);
184
- if (dupe)
185
- continue;
186
- }
187
- throw err;
188
- }
189
- }
190
- })();
191
- }
192
- /** Edit path (ADR-0003 §4.2): re-index of a note deletes its rows; re-extraction repopulates. */
193
- function deleteStructuredRowsForSource(db, sourcePath) {
194
- db.prepare('DELETE FROM structured_rows WHERE source_path = ?').run(sourcePath);
195
- }
196
- /**
197
- * Same edit path, scoped to one lane.
198
- *
199
- * The whole-source delete removes every row kind for a note. With more than
200
- * one lane armed, the second lane's cleanup deleted the first lane's fresh
201
- * rows and the third deleted both. The cache key is per lane
202
- * (source_hash + extraction_version), so stale-row cleanup is too.
203
- */
204
- function deleteStructuredRowsForSourceKind(db, sourcePath, rowKind) {
205
- db.prepare('DELETE FROM structured_rows WHERE source_path = ? AND row_kind = ?')
206
- .run(sourcePath, rowKind);
207
- }
208
- /**
209
- * Current-only read joined to the live notes table: a row whose source_path no
210
- * longer resolves to a live note is never returned (provenance guard), even if
211
- * a connection without the FK pragma leaked orphans into the table.
212
- */
213
- function readCurrentRowsForNotes(db, notePaths) {
214
- if (notePaths.length === 0)
215
- return [];
216
- const placeholders = notePaths.map(() => '?').join(', ');
217
- const rows = db.prepare(`SELECT r.id, r.row_kind, r.source_path, r.source_hash, r.source_chunk_id,
218
- r.scope, r.extraction_version, r.valid_from, r.valid_to, r.payload_json, r.dedupe_key,
219
- r.confirm_count, n.modified_at
220
- FROM structured_rows r
221
- JOIN notes n ON n.path = r.source_path
222
- WHERE r.valid_to IS NULL AND r.source_path IN (${placeholders})
223
- ORDER BY r.id`).all(...notePaths);
224
- return rows.map((r) => ({
225
- id: r.id,
226
- rowKind: r.row_kind,
227
- sourcePath: r.source_path,
228
- sourceChunkId: r.source_chunk_id,
229
- sourceHash: r.source_hash,
230
- scope: r.scope,
231
- extractionVersion: r.extraction_version,
232
- validFrom: (0, orchestrator_1.boundContentDate)(r.valid_from ?? null, r.modified_at),
233
- validTo: r.valid_to ?? null,
234
- payload: parsePayload(r.row_kind, r.payload_json),
235
- dedupeKey: r.dedupe_key,
236
- confirmCount: r.confirm_count ?? 1,
237
- }));
238
- }
@@ -1,81 +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.buildFilename = buildFilename;
7
- exports.formatConversation = formatConversation;
8
- const crypto_1 = __importDefault(require("crypto"));
9
- // Возвращает YYYY-MM-DD из ISO-строки
10
- function isoToDate(iso) {
11
- return iso.substring(0, 10);
12
- }
13
- // Возвращает HH:mm из ISO-строки
14
- function isoToTime(iso) {
15
- return iso.substring(11, 16);
16
- }
17
- // A file name is at most 255 bytes; the date prefix and ".md" take about 22 of them, so the title gets this many UTF-8 bytes.
18
- const MAX_TITLE_BYTES = 120;
19
- // Убирает символы, недопустимые в именах файлов, и управляющие символы (NUL ломает путь); длинное название обрезается
20
- // и получает короткий хеш полного названия, чтобы разные разговоры не совпали по имени файла.
21
- function sanitizeTitle(title) {
22
- const clean = title.replace(/[/\\:*?"<>|\x00-\x1f\x7f]/g, "-").trim();
23
- if (Buffer.byteLength(clean, "utf8") <= MAX_TITLE_BYTES)
24
- return clean;
25
- let kept = "";
26
- for (const ch of clean) {
27
- if (Buffer.byteLength(kept + ch, "utf8") > MAX_TITLE_BYTES)
28
- break;
29
- kept += ch;
30
- }
31
- const digest = crypto_1.default.createHash("sha256").update(title).digest("hex").slice(0, 8);
32
- return `${kept.trimEnd()}-${digest}`;
33
- }
34
- function buildFilename(title, isoTime) {
35
- const date = isoToDate(isoTime);
36
- const time = isoToTime(isoTime).replace(":", "-");
37
- return `${date} ${time} - ${sanitizeTitle(title)}.md`;
38
- }
39
- function formatToolCall(tc) {
40
- if (tc.kind === "edit") {
41
- const diffLines = [
42
- ...tc.oldString.split("\n").map((l) => `> - ${l}`),
43
- ...tc.newString.split("\n").map((l) => `> + ${l}`),
44
- ].join("\n");
45
- return `> **Edit** \`${tc.filePath}\`\n> \`\`\`diff\n${diffLines}\n> \`\`\``;
46
- }
47
- if (tc.kind === "write") {
48
- return `> **Write** \`${tc.filePath}\``;
49
- }
50
- return `> **Bash** \`${tc.command}\``;
51
- }
52
- function formatTurn(turn) {
53
- const parts = [];
54
- if (turn.text) {
55
- const label = turn.role === "user" ? "User" : "Assistant";
56
- parts.push(`**${label}:** ${turn.text}`);
57
- }
58
- for (const tc of turn.toolCalls) {
59
- parts.push(formatToolCall(tc));
60
- }
61
- return parts.join("\n\n");
62
- }
63
- function formatConversation(conv) {
64
- const date = isoToDate(conv.timeStart);
65
- const timeStart = isoToTime(conv.timeStart);
66
- // Убираем переносы строк, чтобы не сломать YAML-фронтматтер
67
- const safeSessionId = conv.sessionId.replace(/[\r\n]/g, " ");
68
- const safeProject = conv.project.replace(/[\r\n]/g, " ");
69
- const frontmatter = [
70
- "---",
71
- "kind: conversation",
72
- `date: ${date}`,
73
- `time_start: ${timeStart}`,
74
- `session_id: ${safeSessionId}`,
75
- `project: ${safeProject}`,
76
- `title: ${JSON.stringify(conv.title)}`,
77
- "---",
78
- ].join("\n");
79
- const body = conv.turns.map(formatTurn).filter(Boolean).join("\n\n");
80
- return `${frontmatter}\n\n${body}\n`;
81
- }
@@ -1,193 +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.encodeProjectPath = encodeProjectPath;
7
- exports.resolveJSONLPath = resolveJSONLPath;
8
- exports.parseConversation = parseConversation;
9
- const promises_1 = __importDefault(require("fs/promises"));
10
- const path_1 = __importDefault(require("path"));
11
- const os_1 = __importDefault(require("os"));
12
- /**
13
- * Кодирует путь проекта в формат имени директории Claude Code:
14
- * слэши заменяются на дефисы (как в ~/.claude/projects/).
15
- */
16
- function encodeProjectPath(cwd) {
17
- return cwd.replace(/\//g, "-");
18
- }
19
- /**
20
- * Возвращает абсолютный путь к JSONL-файлу сессии в директории проектов Claude Code.
21
- */
22
- function resolveJSONLPath(sessionId, cwd) {
23
- const encoded = encodeProjectPath(cwd);
24
- const projects = path_1.default.join(os_1.default.homedir(), ".claude", "projects");
25
- const file = path_1.default.join(projects, encoded, `${sessionId}.jsonl`);
26
- // The session id comes from the hook payload and names a file: a separator or a `..` that leaves the projects
27
- // directory is refused, never read (a transcript of another file would otherwise be written to the vault).
28
- if (/[\\/]/.test(sessionId) || !file.startsWith(projects + path_1.default.sep)) {
29
- throw new Error("session_id must name a file in the Claude Code projects directory");
30
- }
31
- return file;
32
- }
33
- const isText = (v) => typeof v === "string" && v !== "";
34
- /** A message's content blocks: an array of objects, or a non-blank string as one text block. Anything else in the array is dropped. */
35
- function contentBlocks(raw) {
36
- if (Array.isArray(raw))
37
- return raw.filter((b) => b !== null && typeof b === "object");
38
- return typeof raw === "string" && raw.trim() ? [{ type: "text", text: raw }] : [];
39
- }
40
- /** The text of the text blocks, joined; a block whose text is not a string adds nothing. */
41
- function textOf(content) {
42
- return content
43
- .filter(b => b.type === "text" && typeof b.text === "string")
44
- .map(b => b.text)
45
- .join("\n")
46
- .trim();
47
- }
48
- /**
49
- * Определяет, является ли сообщение пользователя внутренним (системным).
50
- * Внутренние сообщения не включаются в итоговый разговор:
51
- * - только tool_result блоки (ответ инструмента, не пользователь)
52
- * - сообщения с тегами CLI (local-command, command-name)
53
- */
54
- function isInternalUserMessage(content) {
55
- if (content.length === 0)
56
- return true;
57
- // Только tool_result блоки — ответ инструмента, не настоящий пользователь
58
- if (content.every((b) => b.type === "tool_result"))
59
- return true;
60
- // Внутренние теги Claude Code CLI
61
- const first = content.find((b) => b.type === "text")?.text;
62
- const firstText = typeof first === "string" ? first : "";
63
- if (firstText.startsWith("<local-command") ||
64
- firstText.startsWith("<command-name>"))
65
- return true;
66
- return false;
67
- }
68
- /**
69
- * Ждёт появления записи ai-title в JSONL-файле с повторными попытками.
70
- * Claude Code генерирует заголовок асинхронно после завершения сессии,
71
- * поэтому Stop hook может сработать раньше, чем заголовок записан.
72
- *
73
- * @param jsonlPath - путь к .jsonl файлу
74
- * @param retries - максимальное количество попыток (каждая с задержкой 2с)
75
- */
76
- async function waitForAiTitle(jsonlPath, retries = 8, intervalMs = 2000) {
77
- for (let i = 0; i < retries; i++) {
78
- await new Promise((resolve) => setTimeout(resolve, intervalMs));
79
- let raw;
80
- try {
81
- raw = await promises_1.default.readFile(jsonlPath, "utf8");
82
- }
83
- catch {
84
- return null;
85
- }
86
- for (const line of raw.split("\n")) {
87
- try {
88
- const entry = JSON.parse(line);
89
- if (entry?.type === "ai-title" && typeof entry.aiTitle === "string" && entry.aiTitle) {
90
- return entry.aiTitle;
91
- }
92
- }
93
- catch {
94
- continue;
95
- }
96
- }
97
- }
98
- return null;
99
- }
100
- /**
101
- * Парсит JSONL-файл сессии Claude Code и возвращает структурированный разговор.
102
- *
103
- * @param jsonlPath - путь к .jsonl файлу сессии
104
- * @param sessionId - идентификатор сессии (используется как fallback заголовок)
105
- * @param cwd - рабочая директория проекта
106
- */
107
- async function parseConversation(jsonlPath, sessionId, cwd, options = {}) {
108
- const raw = await promises_1.default.readFile(jsonlPath, "utf8");
109
- const lines = raw.split("\n").filter((l) => l.trim());
110
- let title = sessionId;
111
- let timeStart = new Date().toISOString();
112
- let timeStartFound = false;
113
- const turns = [];
114
- for (const line of lines) {
115
- let parsed;
116
- try {
117
- parsed = JSON.parse(line);
118
- }
119
- catch {
120
- continue;
121
- }
122
- // A line of the file is any JSON value; only an object can be an entry.
123
- if (parsed === null || typeof parsed !== "object")
124
- continue;
125
- const entry = parsed;
126
- // Извлекаем заголовок сессии из специальной записи ai-title
127
- if (entry.type === "ai-title" && typeof entry.aiTitle === "string" && entry.aiTitle) {
128
- title = entry.aiTitle;
129
- continue;
130
- }
131
- // Время начала = первый dequeue из очереди операций
132
- if (!timeStartFound &&
133
- entry.type === "queue-operation" &&
134
- entry.operation === "dequeue" &&
135
- typeof entry.timestamp === "string") {
136
- timeStart = entry.timestamp;
137
- timeStartFound = true;
138
- continue;
139
- }
140
- // Пропускаем мета-сообщения (системные уведомления Claude Code)
141
- // и сайдчейн-записи (внутренние подразговоры оркестратора)
142
- if (entry.isMeta || entry.isSidechain)
143
- continue;
144
- if (entry.type === "user" && entry.message?.role === "user") {
145
- // content может быть строкой (короткие сессии) или массивом блоков
146
- const content = contentBlocks(entry.message.content);
147
- if (isInternalUserMessage(content))
148
- continue;
149
- const text = textOf(content);
150
- if (!text)
151
- continue;
152
- turns.push({ role: "user", text, toolCalls: [] });
153
- continue;
154
- }
155
- if (entry.type === "assistant" && entry.message?.role === "assistant") {
156
- const content = contentBlocks(entry.message.content);
157
- const text = textOf(content);
158
- const toolCalls = [];
159
- for (const block of content) {
160
- if (block.type !== "tool_use")
161
- continue;
162
- const input = (block.input ?? {});
163
- if (block.name === "Edit" && isText(input.file_path)) {
164
- toolCalls.push({
165
- kind: "edit",
166
- filePath: input.file_path,
167
- oldString: typeof input.old_string === "string" ? input.old_string : "",
168
- newString: typeof input.new_string === "string" ? input.new_string : "",
169
- });
170
- }
171
- else if (block.name === "Write" && isText(input.file_path)) {
172
- toolCalls.push({ kind: "write", filePath: input.file_path });
173
- }
174
- else if (block.name === "Bash" && isText(input.command)) {
175
- toolCalls.push({ kind: "bash", command: input.command });
176
- }
177
- }
178
- if (!text && toolCalls.length === 0)
179
- continue;
180
- turns.push({ role: "assistant", text, toolCalls });
181
- }
182
- }
183
- // Если заголовок не найден — ждём: Claude Code записывает ai-title асинхронно.
184
- // Ждём только если в JSONL есть queue-operation (признак полноценной сессии).
185
- // Короткие сессии без queue-operation ai-title не получают.
186
- const { maxRetries = 8, retryIntervalMs = 2000 } = options;
187
- if (title === sessionId && timeStartFound && maxRetries > 0) {
188
- const aiTitle = await waitForAiTitle(jsonlPath, maxRetries, retryIntervalMs);
189
- if (aiTitle)
190
- title = aiTitle;
191
- }
192
- return { sessionId, title, timeStart, project: cwd, turns };
193
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,42 +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.writeConversation = writeConversation;
7
- const path_1 = __importDefault(require("path"));
8
- const formatter_1 = require("./formatter");
9
- const secretGuard_1 = require("../extraction/secretGuard");
10
- const confinedWrite_1 = require("../memory/confinedWrite");
11
- /**
12
- * Записывает разговор в файл markdown в хранилище.
13
- * @param vaultPath - корневой путь хранилища
14
- * @param conversationsDir - относительный путь к папке разговоров (например, "Claude Code/Conversations")
15
- * @param conv - распарсенный разговор
16
- * @returns абсолютный путь к созданному файлу
17
- * @throws Error если обнаружена попытка обхода пути
18
- */
19
- async function writeConversation(vaultPath, conversationsDir, conv) {
20
- const filename = (0, formatter_1.buildFilename)(conv.title, conv.timeStart);
21
- const dir = path_1.default.join(vaultPath, conversationsDir);
22
- // Защита от path traversal через имя файла
23
- const filePath = path_1.default.join(dir, filename);
24
- const resolvedPath = path_1.default.resolve(filePath);
25
- const resolvedBase = path_1.default.resolve(dir);
26
- if (!resolvedPath.startsWith(resolvedBase + path_1.default.sep) &&
27
- resolvedPath !== resolvedBase) {
28
- throw new Error("Path traversal detected in conversation filename");
29
- }
30
- // RENOR_SECRET_GUARD (default ON): a transcript that looks like it holds a secret is not written, and the
31
- // directory is not created. The caller (the log-conversation hook) reports the refusal and exits cleanly.
32
- if ((0, secretGuard_1.secretGuardEnabled)()) {
33
- await (0, secretGuard_1.assertStorable)([
34
- { field: "conversation", text: (0, formatter_1.formatConversation)(conv) },
35
- { field: "conversation file name", text: filename },
36
- ]);
37
- }
38
- const content = (0, formatter_1.formatConversation)(conv);
39
- // A link, a hard link or a FIFO planted in the vault is refused, never followed (memory/confinedWrite.ts).
40
- await (0, confinedWrite_1.writeConfined)(vaultPath, path_1.default.relative(path_1.default.resolve(vaultPath), resolvedPath), content);
41
- return filePath;
42
- }