renor-engine 1.2.0 → 1.3.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 (215) hide show
  1. package/README.md +21 -1
  2. package/THIRD-PARTY-NOTICES +2661 -0
  3. package/dist/BUILD.json +6 -0
  4. package/dist/cli.js +1656 -516
  5. package/package.json +16 -80
  6. package/dist/.tsbuildinfo +0 -1
  7. package/dist/api/apiAuth.js +0 -43
  8. package/dist/api/server.js +0 -174
  9. package/dist/buildIdentity.js +0 -149
  10. package/dist/callPhases.js +0 -82
  11. package/dist/cli-extractors.js +0 -46
  12. package/dist/config.js +0 -65
  13. package/dist/context/composer.js +0 -320
  14. package/dist/context/readHonesty.js +0 -478
  15. package/dist/context/rowStore.js +0 -238
  16. package/dist/conversation/formatter.js +0 -81
  17. package/dist/conversation/parser.js +0 -193
  18. package/dist/conversation/types.js +0 -2
  19. package/dist/conversation/writer.js +0 -42
  20. package/dist/db/client.js +0 -149
  21. package/dist/db/handoffTable.js +0 -31
  22. package/dist/db/indexSalvage.js +0 -101
  23. package/dist/db/migrations.js +0 -670
  24. package/dist/db/readCache.js +0 -109
  25. package/dist/db/schema.js +0 -5
  26. package/dist/db/sqlite.js +0 -396
  27. package/dist/db/statements.js +0 -89
  28. package/dist/digests/digestCache.js +0 -146
  29. package/dist/digests/digests.js +0 -597
  30. package/dist/digests/worker.js +0 -350
  31. package/dist/embeddings/caching.js +0 -337
  32. package/dist/embeddings/coalescing.js +0 -177
  33. package/dist/embeddings/interactive.js +0 -103
  34. package/dist/embeddings/local.js +0 -70
  35. package/dist/embeddings/openai.js +0 -106
  36. package/dist/embeddings/rebuild.js +0 -166
  37. package/dist/embeddings/simdScan.js +0 -260
  38. package/dist/embeddings/space.js +0 -21
  39. package/dist/embeddings/types.js +0 -14
  40. package/dist/embeddings/vectorIndex.js +0 -674
  41. package/dist/embeddings/vertex.js +0 -334
  42. package/dist/eval/artifacts.js +0 -42
  43. package/dist/eval/cacheReport.js +0 -146
  44. package/dist/eval/comparison.js +0 -182
  45. package/dist/eval/context700-candidate.js +0 -145
  46. package/dist/eval/evalCache.js +0 -568
  47. package/dist/eval/hashEmbedding.js +0 -35
  48. package/dist/eval/indexSnapshots.js +0 -549
  49. package/dist/eval/longmemeval.js +0 -56
  50. package/dist/eval/metrics.js +0 -181
  51. package/dist/eval/pageDump.js +0 -67
  52. package/dist/eval/renorbenchBaseline.js +0 -486
  53. package/dist/eval/renorbenchDocuments.js +0 -134
  54. package/dist/eval/renorbenchProduct.js +0 -182
  55. package/dist/eval/runLedger.js +0 -279
  56. package/dist/eval/runManifest.js +0 -485
  57. package/dist/eval/scoring.js +0 -197
  58. package/dist/eval/textTokens.js +0 -17
  59. package/dist/evidence/attach.js +0 -264
  60. package/dist/evidence/background.js +0 -147
  61. package/dist/evidence/evidenceCache.js +0 -182
  62. package/dist/evidence/gate.js +0 -37
  63. package/dist/evidence/keyPool.js +0 -85
  64. package/dist/evidence/ollamaChat.js +0 -84
  65. package/dist/evidence/optin.js +0 -165
  66. package/dist/evidence/options.js +0 -151
  67. package/dist/evidence/secondPass.js +0 -294
  68. package/dist/extraction/classifyCapture.js +0 -34
  69. package/dist/extraction/decision-model.js +0 -174
  70. package/dist/extraction/eventExtractor.js +0 -113
  71. package/dist/extraction/extractionClient.js +0 -122
  72. package/dist/extraction/factExtractor.js +0 -46
  73. package/dist/extraction/factsWritePath.js +0 -179
  74. package/dist/extraction/jevDecisionModel.js +0 -307
  75. package/dist/extraction/preferenceExtractor.js +0 -150
  76. package/dist/extraction/secretGuard.js +0 -180
  77. package/dist/extraction/secretScanner.js +0 -665
  78. package/dist/extraction/typedFacts.js +0 -224
  79. package/dist/extraction/writePathAdjudication.js +0 -110
  80. package/dist/importing/apply.js +0 -672
  81. package/dist/importing/bundle-write.js +0 -80
  82. package/dist/importing/cli.js +0 -408
  83. package/dist/importing/existing.js +0 -51
  84. package/dist/importing/facts.js +0 -121
  85. package/dist/importing/forgetImported.js +0 -79
  86. package/dist/importing/hosted.js +0 -467
  87. package/dist/importing/journalLog.js +0 -96
  88. package/dist/importing/markdown.js +0 -98
  89. package/dist/importing/plan.js +0 -201
  90. package/dist/importing/secretScreen.js +0 -113
  91. package/dist/importing/serialize.js +0 -188
  92. package/dist/importing/source.js +0 -463
  93. package/dist/importing/types.js +0 -6
  94. package/dist/indexer/chunker.js +0 -319
  95. package/dist/indexer/dailyEntries.js +0 -63
  96. package/dist/indexer/hasher.js +0 -10
  97. package/dist/indexer/laneRuns.js +0 -48
  98. package/dist/indexer/o200k.js +0 -481
  99. package/dist/indexer/outsideRoot.js +0 -77
  100. package/dist/indexer/parser.js +0 -170
  101. package/dist/indexer/pipeline.js +0 -988
  102. package/dist/indexer/staleness.js +0 -201
  103. package/dist/indexer/tokenizer.js +0 -77
  104. package/dist/indexer/walker.js +0 -170
  105. package/dist/learning/cli.js +0 -113
  106. package/dist/learning/schema.js +0 -144
  107. package/dist/learning/settings.js +0 -45
  108. package/dist/learning/store.js +0 -267
  109. package/dist/learning/tap.js +0 -401
  110. package/dist/mcp/hive.js +0 -88
  111. package/dist/mcp/requestErrors.js +0 -47
  112. package/dist/mcp/server.js +0 -426
  113. package/dist/memory/agentLease.js +0 -43
  114. package/dist/memory/confinedWrite.js +0 -315
  115. package/dist/memory/controls.js +0 -73
  116. package/dist/memory/dailyNote.js +0 -265
  117. package/dist/memory/dreamer/adjudicate.js +0 -215
  118. package/dist/memory/dreamer/apply.js +0 -120
  119. package/dist/memory/dreamer/budget.js +0 -64
  120. package/dist/memory/dreamer/candidates.js +0 -149
  121. package/dist/memory/dreamer/collection.js +0 -104
  122. package/dist/memory/dreamer/dreamer.js +0 -352
  123. package/dist/memory/dreamer/review.js +0 -111
  124. package/dist/memory/durableAppend.js +0 -57
  125. package/dist/memory/factConflicts.js +0 -205
  126. package/dist/memory/factTerms.js +0 -321
  127. package/dist/memory/handoff.js +0 -363
  128. package/dist/memory/hiveNameHeader.cjs +0 -10
  129. package/dist/memory/inventory.js +0 -55
  130. package/dist/memory/invisibleText.js +0 -20
  131. package/dist/memory/lifecycle.js +0 -2014
  132. package/dist/memory/limits.js +0 -13
  133. package/dist/memory/requests.js +0 -183
  134. package/dist/memory/sourceName.js +0 -276
  135. package/dist/memory/statusFacts.js +0 -61
  136. package/dist/memory/whatsNew.js +0 -158
  137. package/dist/memory/writer.js +0 -176
  138. package/dist/memory/writerConflicts.js +0 -85
  139. package/dist/memorymap/memoryMap.js +0 -1594
  140. package/dist/memorymap/replacedJournal.js +0 -58
  141. package/dist/memorymap/replacements.js +0 -410
  142. package/dist/mvp/activity.js +0 -86
  143. package/dist/mvp/agentMemory.js +0 -1803
  144. package/dist/mvp/answerCheck.js +0 -211
  145. package/dist/mvp/auth.js +0 -531
  146. package/dist/mvp/cli.js +0 -86
  147. package/dist/mvp/client-store.js +0 -462
  148. package/dist/mvp/compileCache.js +0 -242
  149. package/dist/mvp/compileSeed.js +0 -33
  150. package/dist/mvp/config.js +0 -292
  151. package/dist/mvp/connectionStatus.js +0 -80
  152. package/dist/mvp/contextDistinct.js +0 -53
  153. package/dist/mvp/contextProfile.js +0 -87
  154. package/dist/mvp/contextRelevance.js +0 -413
  155. package/dist/mvp/directorySurface.js +0 -68
  156. package/dist/mvp/engine.js +0 -1078
  157. package/dist/mvp/exportAll.js +0 -393
  158. package/dist/mvp/factLanePack.js +0 -77
  159. package/dist/mvp/factMarkers.js +0 -65
  160. package/dist/mvp/factsLane.js +0 -203
  161. package/dist/mvp/hiddenSpans.js +0 -585
  162. package/dist/mvp/history.js +0 -414
  163. package/dist/mvp/hitDates.js +0 -31
  164. package/dist/mvp/hitFloor.js +0 -36
  165. package/dist/mvp/identity.js +0 -18
  166. package/dist/mvp/idle.js +0 -113
  167. package/dist/mvp/log.js +0 -118
  168. package/dist/mvp/memoryDir.js +0 -98
  169. package/dist/mvp/posthogMcp.js +0 -210
  170. package/dist/mvp/readScope.js +0 -104
  171. package/dist/mvp/readUsefulness.js +0 -222
  172. package/dist/mvp/sanitize.js +0 -48
  173. package/dist/mvp/schemaRejections.js +0 -91
  174. package/dist/mvp/scope.js +0 -127
  175. package/dist/mvp/server.js +0 -2712
  176. package/dist/mvp/serverCard.js +0 -38
  177. package/dist/mvp/storedSize.js +0 -139
  178. package/dist/mvp/telemetry.js +0 -474
  179. package/dist/net/poolFetch.js +0 -174
  180. package/dist/recall/actionRecall.js +0 -282
  181. package/dist/recall/autoRecall.js +0 -447
  182. package/dist/recall/searchRecall.js +0 -189
  183. package/dist/retrieval/adviceLines.js +0 -195
  184. package/dist/retrieval/adviceQuestion.js +0 -44
  185. package/dist/retrieval/conversationShape.js +0 -44
  186. package/dist/retrieval/dailyEntryTime.js +0 -55
  187. package/dist/retrieval/entityLookup.js +0 -162
  188. package/dist/retrieval/factsFirst.js +0 -188
  189. package/dist/retrieval/keyword.js +0 -84
  190. package/dist/retrieval/ledgerEventTime.js +0 -51
  191. package/dist/retrieval/ledgerLiveness.js +0 -83
  192. package/dist/retrieval/ledgerPresentation.js +0 -161
  193. package/dist/retrieval/noteIdentity.js +0 -57
  194. package/dist/retrieval/orchestrator.js +0 -1224
  195. package/dist/retrieval/plainWording.js +0 -236
  196. package/dist/retrieval/queryHelpers.js +0 -142
  197. package/dist/retrieval/questionSpan.js +0 -475
  198. package/dist/retrieval/rankingTrace.js +0 -184
  199. package/dist/retrieval/readerEvents.js +0 -690
  200. package/dist/retrieval/readerLayout.js +0 -422
  201. package/dist/retrieval/relativeTimeHints.js +0 -113
  202. package/dist/retrieval/semantic.js +0 -67
  203. package/dist/router/routerCache.js +0 -106
  204. package/dist/router/sessionRouter.js +0 -726
  205. package/dist/sharing/cli.js +0 -181
  206. package/dist/sharing/examples.js +0 -136
  207. package/dist/sharing/fields.js +0 -225
  208. package/dist/sharing/loop.js +0 -80
  209. package/dist/sharing/project.js +0 -327
  210. package/dist/sharing/send.js +0 -466
  211. package/dist/sharing/state.js +0 -222
  212. package/dist/sharing/wording.js +0 -48
  213. package/dist/toolRefusal.js +0 -120
  214. package/dist/watcher/watcher.js +0 -77
  215. /package/dist/{indexer/o200k.json → o200k.json} +0 -0
@@ -1,426 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.HANDOFF_TOOL = exports.TOOLS = void 0;
4
- exports.createMcpTools = createMcpTools;
5
- exports.utf8Lines = utf8Lines;
6
- exports.registerMcpHandlers = registerMcpHandlers;
7
- exports.startMcpServer = startMcpServer;
8
- const index_js_1 = require("@modelcontextprotocol/sdk/server/index.js");
9
- const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
10
- const types_js_1 = require("@modelcontextprotocol/sdk/types.js");
11
- const stream_1 = require("stream");
12
- const orchestrator_1 = require("../retrieval/orchestrator");
13
- const entityLookup_1 = require("../retrieval/entityLookup");
14
- const writer_1 = require("../memory/writer");
15
- const lifecycle_1 = require("../memory/lifecycle");
16
- const pipeline_1 = require("../indexer/pipeline");
17
- const queryHelpers_1 = require("../retrieval/queryHelpers");
18
- const keyword_1 = require("../retrieval/keyword");
19
- const engine_1 = require("../mvp/engine");
20
- const noteIdentity_1 = require("../retrieval/noteIdentity");
21
- const ledgerPresentation_1 = require("../retrieval/ledgerPresentation");
22
- const options_1 = require("../evidence/options");
23
- const attach_1 = require("../evidence/attach");
24
- const composer_1 = require("../context/composer");
25
- const hive_1 = require("./hive");
26
- const sourceName_1 = require("../memory/sourceName");
27
- const requestErrors_1 = require("./requestErrors");
28
- const sanitize_1 = require("../mvp/sanitize");
29
- const factsFirst_1 = require("../retrieval/factsFirst");
30
- const handoff_1 = require("../memory/handoff");
31
- const buildIdentity_1 = require("../buildIdentity");
32
- function createMcpTools(db, vectorIndex, provider, config, evidence = { mode: 'off', model: '', maxSessions: 12, charCap: 6000 }) {
33
- // Startup configuration, captured once: tool arguments and later env changes cannot rename a connection.
34
- // The hive name is the header form. Empty, unnamed, or a second live holder publishes no hive tools
35
- // (a duplicate name throws here, so `renor-engine mcp` fails at startup instead of joining the other process).
36
- const rawConnectionName = process.env.RENOR_CONNECTION_NAME?.trim();
37
- const identity = rawConnectionName ? (0, sourceName_1.hiveIdentity)(rawConnectionName) : null;
38
- const hive = identity ? (0, hive_1.createStdioHiveTools)(db, identity) : {};
39
- // Lazy extractor: built on first use so mode=off and key-less deployments
40
- // never open the cache file or construct a chat client.
41
- let extractor;
42
- const getExtractor = () => {
43
- if (extractor !== undefined)
44
- return extractor;
45
- extractor = (0, options_1.createEvidenceExtractor)(evidence);
46
- return extractor;
47
- };
48
- // The ledger is the engine's own record, not a memory: no read tool returns its header or the markup of its records.
49
- const vault = { memoryDir: config.vaultPath, ledgerRelPath: config.memoryLedger ?? lifecycle_1.DEFAULT_LEDGER_REL };
50
- const isLedger = (0, ledgerPresentation_1.ledgerNoteMatcher)(vault);
51
- // A save writes a file (the day's note, or the ledger) and nothing else, so search and keyword would not find it until
52
- // `renor index` ran again. Index the file just written, cut the way `renor index` cuts it, as the hosted save does.
53
- // The memory is already saved, so a failed refresh is said in the reply instead of failing the save.
54
- const indexSaved = async (file) => {
55
- try {
56
- const maxTokens = config.chunkMaxTokens ?? 400;
57
- await (0, pipeline_1.indexFile)(db, file, {
58
- embeddingProvider: provider,
59
- vectorIndex,
60
- // The defaults of loadConfig (CHUNK_MAX_TOKENS, CHUNK_OVERLAP_TOKENS): the chunker's own default overlap is 0.
61
- // A default overlap stays below a maximum the caller named, as loadConfig requires of the pair.
62
- chunkOptions: { maxTokens, overlapTokens: config.chunkOverlapTokens ?? Math.min(50, maxTokens - 1) },
63
- });
64
- return null;
65
- }
66
- catch (err) {
67
- const detail = err instanceof Error ? err.message : String(err);
68
- return { warning: 'memory-saved-but-index-refresh-failed', detail: detail.slice(0, 300) };
69
- }
70
- };
71
- // The handoff note (memory/handoff.ts), only with RENOR_HANDOFF_NOTE on. Its writer is this connection's startup name;
72
- // its expiry sweep runs for the life of the process (the timer never keeps it alive).
73
- if ((0, handoff_1.handoffNoteEnabled)())
74
- (0, handoff_1.startHandoffSweep)(db);
75
- const handoff = (0, handoff_1.handoffNoteEnabled)()
76
- ? { async [handoff_1.HANDOFF_TOOL_NAME](args) {
77
- return (0, handoff_1.runHandoffTool)(db, args ?? {}, identity ?? 'stdio');
78
- } }
79
- : {};
80
- return {
81
- ...hive,
82
- ...handoff,
83
- // Canonical tool names. Clients that prefix with the server name render
84
- // these as renor_search, renor_keyword, renor_facts, etc.
85
- async search(args) {
86
- const topK = (0, queryHelpers_1.pickTopK)(args, 20, 100);
87
- const result = await (0, orchestrator_1.retrieveContext)(db, vectorIndex, provider, args.query, topK, config.priorityPaths ?? [], {}, { isLedgerNote: isLedger });
88
- // Unlike the owner-name shortcuts in the shared retrieval orchestrator, the self-hosted stdio read must surface
89
- // a just-saved fact about any subject before its ledger prose is indexed. This is the same bounded current-fact
90
- // term-index lane the hosted read uses; it excludes superseded facts.
91
- // Merged by score with the note hits, not prepended: a fact that answers the query ranks by how well it does,
92
- // and a strong note match still comes first.
93
- const currentFacts = (0, factsFirst_1.matchingCurrentFactHits)(db, args.query, Math.min(3, topK));
94
- const factTexts = new Set(currentFacts.map(hit => hit.text));
95
- const hits = [...currentFacts, ...result.hits.filter(hit => !factTexts.has(hit.text))]
96
- .sort((a, b) => b.score - a.score)
97
- .slice(0, topK);
98
- const enabled = (0, attach_1.searchEvidenceEnabled)();
99
- const attached = await (0, attach_1.attachSearchEvidence)({
100
- query: args.query,
101
- hits,
102
- vaultPath: config.vaultPath,
103
- options: evidence,
104
- extractor: enabled ? getExtractor() : null,
105
- enabled,
106
- });
107
- // The evidence pass above read the sources as stored; what the caller is shown is what the user said.
108
- const shown = (0, ledgerPresentation_1.presentLedgerEvents)(attached.hits, isLedger);
109
- // C0 shared composer (ADR-0003 §5): opt-in via RENOR_STRUCTURED_CONTEXT
110
- // (default OFF — plain hits array, unchanged contract). When on, the
111
- // composed bounded context ships alongside the hits.
112
- if ((0, composer_1.structuredContextEnabled)()) {
113
- const excerptHits = shown.filter((h) => h.notePath !== attach_1.EVIDENCE_HIT_PATH);
114
- return {
115
- hits: shown,
116
- composedContext: (0, composer_1.composeQueryContextFromDb)(db, {
117
- query: args.query,
118
- hits: excerptHits,
119
- evidence: attached.evidence,
120
- }),
121
- };
122
- }
123
- return shown;
124
- },
125
- /**
126
- * Product path for the H10-v4 second pass: retrieve, then list every
127
- * distinct item/event/amount relevant to the question, with dates.
128
- * Content-addressed: an identical question over unchanged sessions
129
- * replays from cache with zero model calls.
130
- */
131
- async evidence(args) {
132
- const topK = (0, queryHelpers_1.pickTopK)(args, 20, 100);
133
- const hits = (await (0, orchestrator_1.retrieveContext)(db, vectorIndex, provider, args.query, topK, config.priorityPaths ?? [], {}, { isLedgerNote: isLedger })).hits;
134
- const attachment = await (0, attach_1.collectEvidence)({
135
- query: args.query,
136
- hits,
137
- vaultPath: config.vaultPath,
138
- options: evidence,
139
- extractor: getExtractor(),
140
- force: args.force === true,
141
- });
142
- return { query: args.query, hits: (0, ledgerPresentation_1.presentLedgerEvents)(hits, isLedger), evidence: attachment };
143
- },
144
- async keyword(args) {
145
- const topK = (0, queryHelpers_1.pickTopK)(args, 20, 50);
146
- const match = (0, queryHelpers_1.quoteFtsQuery)(args.query);
147
- if (!match)
148
- return [];
149
- try {
150
- // Rows of the ledger may come off the page (its header, an event the words are not in). They are judged before
151
- // the page is cut, from a read that goes on until the page is full or the index has no more rows: records of the
152
- // ledger never take a place from a note that says the word, however many of them rank above it. A search that
153
- // turns nothing away is the one read of topK rows it always was.
154
- const fts = (0, queryHelpers_1.shownPage)(rows => (0, keyword_1.keywordSearch)(db, match, rows), rows => (0, ledgerPresentation_1.keywordLedgerHits)(rows.map(r => ({ ...r, score: Math.abs(r.score), reason: 'fts' })), isLedger, args.query), topK);
155
- // The term may also name a note (task id, slug, file name, frontmatter id): add
156
- // it after the untouched prose page — reason says it came from note identity.
157
- const named = (0, noteIdentity_1.selectNoteIdentityHits)(db, args.query, noteIdentity_1.NOTE_IDENTITY_EXTRA, new Set(fts.map(r => r.notePath)), (_notePath, opening) => !(0, ledgerPresentation_1.isLedgerPreamble)(opening, isLedger));
158
- // Judge stored paths first; expose only vault-relative paths on the wire,
159
- // using the same presentation as `renor search --json`.
160
- return (0, ledgerPresentation_1.presentLedgerEvents)([...fts, ...named], isLedger)
161
- .map(hit => ({ ...hit, notePath: (0, engine_1.clientPath)(config.vaultPath, hit.notePath) }));
162
- }
163
- catch (err) {
164
- const message = err instanceof Error ? err.message : String(err);
165
- return { ok: false, error: 'fts-query', detail: message.slice(0, 200), results: [] };
166
- }
167
- },
168
- async entity(args) {
169
- return (0, entityLookup_1.lookupEntity)(db, args.name);
170
- },
171
- async facts(args) {
172
- const entityName = (0, queryHelpers_1.pickEntityName)(args);
173
- if (!entityName)
174
- return [];
175
- return (0, lifecycle_1.factsFor)(db, entityName, { asOf: args.asOf, includeHistory: args.includeHistory });
176
- },
177
- /** The principal durable-memory verb: ledger-first, supersedes the current fact for the same subject+predicate. Returns a receipt. */
178
- async remember_fact(args) {
179
- const ledgerRel = config.memoryLedger ?? lifecycle_1.DEFAULT_LEDGER_REL;
180
- const receipt = await (0, lifecycle_1.remember)(db, config.vaultPath, ledgerRel, { ...args, source: identity ?? undefined });
181
- // Nothing was written when the fact was already current, as the hosted save skips its refresh.
182
- if (receipt.action === 'nochange')
183
- return receipt;
184
- const failed = await indexSaved((0, lifecycle_1.resolveLedgerPath)(config.vaultPath, ledgerRel));
185
- return failed ? { ...receipt, ...failed } : receipt;
186
- },
187
- async status(_args) {
188
- const counts = {
189
- notes: db.prepare('SELECT COUNT(*) as c FROM notes').get().c,
190
- chunks: db.prepare('SELECT COUNT(*) as c FROM chunks').get().c,
191
- entities: db.prepare('SELECT COUNT(*) as c FROM entities').get().c,
192
- facts: db.prepare('SELECT COUNT(*) as c FROM facts').get().c,
193
- relations: db.prepare('SELECT COUNT(*) as c FROM relations').get().c,
194
- };
195
- return counts;
196
- },
197
- async note(args) {
198
- const date = args.date ?? new Date().toISOString().substring(0, 10);
199
- const source = identity ?? 'mcp';
200
- const filePath = await (0, writer_1.appendDailyMemory)(config.vaultPath, date, { text: args.text, source }, config.memoryDir);
201
- const failed = await indexSaved(filePath);
202
- return { ok: true, path: filePath, ...failed };
203
- },
204
- };
205
- }
206
- // Exported so tests/mcp/tool-surfaces.test.ts can compare the published list
207
- // against the surface map in docs/architecture/MCP-TOOL-SURFACES.md.
208
- exports.TOOLS = [
209
- {
210
- name: 'search',
211
- description: 'DEFAULT tool for questions about the user or the vault (who/what/where/current project, preferences, goals). Prepends current lifecycle facts when they match; then semantic chunks. Each hit may include session_date — render excerpts as "[Session date: <session_date>]\n<text>". Do not rewrite stored chunk text. When RENOR_SEARCH_EVIDENCE is on, search also runs the H10-v4 evidence second pass (full source notes, 12-note/6k cap, cached) and prepends a dated item table as the first hit. Do not use keyword search first for identity questions. Write durable facts with remember_fact.',
212
- inputSchema: {
213
- type: 'object',
214
- properties: {
215
- query: { type: 'string', description: 'Natural language search query' },
216
- topK: { type: 'number', description: 'Number of results (1-100, default 20)' },
217
- },
218
- required: ['query'],
219
- },
220
- },
221
- {
222
- name: 'evidence',
223
- description: 'Question-conditioned evidence extraction. Retrieves like search and then lists every distinct item/event/amount relevant to the question, with dates. Best for how-many / how-much / count / list / aggregate questions about the user\'s history. It never computes totals — count or sum the items yourself and prefer items with dates matching the question window. Identical questions over unchanged history replay from a content-addressed cache at zero model cost. Gated by RENOR_EVIDENCE_MODE (off | auto | always); call with force: true to run even when auto declines.',
224
- inputSchema: {
225
- type: 'object',
226
- properties: {
227
- query: { type: 'string', description: 'Natural language question about the user\'s history' },
228
- topK: { type: 'number', description: 'Number of retrieval hits to return (1-100, default 20)' },
229
- force: { type: 'boolean', description: 'Run the second pass even when auto gating declines (ignored when mode is off)' },
230
- },
231
- required: ['query'],
232
- },
233
- },
234
- {
235
- name: 'keyword',
236
- description: 'Exact keyword/BM25 search only: the query is matched as one exact phrase, and OR, AND, NEAR and * are plain words, not operators. Use search for identity and natural-language questions. Hyphens are quoted server-side. When the term appears in no chunk text, it is matched against note paths and frontmatter too, so a task id, slug or file name finds the note that carries it (reason: "note-identity").',
237
- inputSchema: {
238
- type: 'object',
239
- properties: {
240
- query: { type: 'string', description: 'Words to find, matched as one exact phrase: OR, AND, NEAR and * are plain words, not operators' },
241
- topK: { type: 'number', description: 'Number of results (1-50, default 20)' },
242
- },
243
- required: ['query'],
244
- },
245
- },
246
- {
247
- name: 'entity',
248
- description: 'Look up an entity (person, project, concept) by name or alias. Returns entity details or null.',
249
- inputSchema: {
250
- type: 'object',
251
- properties: { name: { type: 'string', description: 'Entity name or alias to look up' } },
252
- required: ['name'],
253
- },
254
- },
255
- {
256
- name: 'remember_fact',
257
- description: 'Durable memory write: stores (or supersedes) a fact about a subject. Ledger-first (survives index rebuild), temporal (closes the previous current fact for that subject+predicate). Returns a receipt. Use for "remember that…" statements. Confidence is not representable in the ledger.',
258
- inputSchema: {
259
- type: 'object',
260
- // Every lifecycle field is a single-line STRING (the ledger framing rejects newlines and
261
- // control characters), so the descriptions say so explicitly: without them a model that
262
- // reads `object` as "the value" happily sends a list or a struct and gets back an opaque
263
- // type error instead of a stored fact.
264
- properties: {
265
- subject: { type: 'string', description: 'Who or what the fact is about, e.g. "Renor". A single string, not a nested object.' },
266
- predicate: { type: 'string', description: 'The relation or attribute being asserted, e.g. "works_at". A short single-line string.' },
267
- object: { type: 'string', description: 'The current value, e.g. "Memory Company". A single string: pass a list or a struct as ONE joined string, never as JSON or an array.' },
268
- source: { type: 'string', description: 'Ignored. The author is this connection\'s header-form name. When the connection has no usable name, the engine default (agent) is stored. Never a file path or document name.' },
269
- },
270
- required: ['subject', 'predicate', 'object'],
271
- },
272
- },
273
- {
274
- name: 'facts',
275
- description: 'All current lifecycle facts for an entity (name / entityName / subject; aliases). Prefer search for a natural-language question. asOf / includeHistory for temporal reads.',
276
- inputSchema: {
277
- type: 'object',
278
- properties: {
279
- entityName: { type: 'string', description: 'Entity name (also accepts name / subject)' },
280
- name: { type: 'string', description: 'Alias of entityName' },
281
- asOf: { type: 'string', description: 'ISO timestamp — facts valid at that time' },
282
- includeHistory: { type: 'boolean', description: 'include superseded facts' },
283
- },
284
- },
285
- },
286
- {
287
- name: 'status',
288
- description: 'Get vault index statistics: count of notes, chunks, entities, facts, and relations.',
289
- inputSchema: { type: 'object', properties: {} },
290
- },
291
- {
292
- name: 'note',
293
- description: 'Store a memory entry in the daily notes. Appends text to the daily note for the given date.',
294
- inputSchema: {
295
- type: 'object',
296
- properties: {
297
- text: { type: 'string', description: 'Text to remember' },
298
- date: { type: 'string', description: 'Date in YYYY-MM-DD format (defaults to today)' },
299
- source: { type: 'string', description: 'Ignored. The author is this connection\'s header-form name, or mcp when the connection has no usable name. Never a file path or document name.' },
300
- },
301
- required: ['text'],
302
- },
303
- },
304
- ];
305
- /** Listed beside TOOLS only when the server has the handler, i.e. RENOR_HANDOFF_NOTE was on at startup. */
306
- exports.HANDOFF_TOOL = {
307
- name: handoff_1.HANDOFF_TOOL_NAME,
308
- description: handoff_1.HANDOFF_TOOL_DESCRIPTION,
309
- inputSchema: {
310
- type: 'object',
311
- properties: {
312
- action: { type: 'string', enum: ['read', 'write'], description: handoff_1.HANDOFF_ARG_DESCRIPTIONS.action },
313
- repo: { type: 'string', description: handoff_1.HANDOFF_ARG_DESCRIPTIONS.repo },
314
- branch: { type: 'string', description: handoff_1.HANDOFF_ARG_DESCRIPTIONS.branch },
315
- note: { type: 'string', description: handoff_1.HANDOFF_ARG_DESCRIPTIONS.note },
316
- },
317
- required: ['action', 'repo', 'branch'],
318
- },
319
- };
320
- /**
321
- * The stdin of the stdio server as lines the MCP transport can read, with a line that is not valid UTF-8 kept out.
322
- *
323
- * The transport decodes what it reads leniently, so an invalid byte would reach a tool as U+FFFD and be stored as
324
- * that. Each newline-terminated line is decoded strictly here instead; a line that fails is not passed on, and
325
- * `onInvalid` is told, so the caller can answer it. A frame is only ever checked whole: a multi-byte character split
326
- * across two reads is decoded once its line is complete.
327
- */
328
- function utf8Lines(source, onInvalid) {
329
- const out = new stream_1.PassThrough();
330
- const decoder = new TextDecoder('utf-8', { fatal: true });
331
- let pending = Buffer.alloc(0);
332
- const pass = (line) => {
333
- try {
334
- decoder.decode(line);
335
- }
336
- catch {
337
- onInvalid();
338
- return;
339
- }
340
- out.write(line);
341
- };
342
- source.on('data', (chunk) => {
343
- pending = pending.length === 0 ? chunk : Buffer.concat([pending, chunk]);
344
- let newline = pending.indexOf(0x0a);
345
- while (newline !== -1) {
346
- pass(pending.subarray(0, newline + 1));
347
- pending = pending.subarray(newline + 1);
348
- newline = pending.indexOf(0x0a);
349
- }
350
- });
351
- source.on('end', () => {
352
- if (pending.length > 0)
353
- pass(pending);
354
- out.end();
355
- });
356
- source.on('error', err => out.destroy(err));
357
- return out;
358
- }
359
- /**
360
- * Wire TOOLS into a low-level MCP Server's `tools/list` and `tools/call` handlers.
361
- *
362
- * Extracted from startMcpServer so a test can drive the REAL protocol path — the same handlers
363
- * a stdio or gateway client reaches — instead of calling createMcpTools().remember_fact()
364
- * directly. A write-path regression asserted only on the internal function would pass even if
365
- * the published schema or the error envelope the client actually sees were wrong.
366
- */
367
- function registerMcpHandlers(server, tools) {
368
- // Looked up by the name the client sent: an argument shape is the tool's own to check.
369
- const byName = tools;
370
- const hive = hive_1.HIVE_TOOLS.filter(tool => typeof byName[tool.name] === 'function');
371
- const handoff = typeof byName[handoff_1.HANDOFF_TOOL_NAME] === 'function' ? [exports.HANDOFF_TOOL] : [];
372
- server.setRequestHandler(types_js_1.ListToolsRequestSchema, async () => ({ tools: [...exports.TOOLS, ...hive, ...handoff] }));
373
- server.setRequestHandler(types_js_1.CallToolRequestSchema, async (request) => {
374
- const { name, arguments: args } = request.params;
375
- const handler = byName[name];
376
- if (!handler) {
377
- if (hive_1.HIVE_TOOLS.some(tool => tool.name === name)) {
378
- return { content: [{ type: 'text', text: `${name} is refused: this connection has no name. Set RENOR_CONNECTION_NAME to this agent's name before starting renor-engine mcp, then reconnect.` }], isError: true };
379
- }
380
- return { content: [{ type: 'text', text: `Unknown tool: ${name}` }], isError: true };
381
- }
382
- try {
383
- const result = await handler(args ?? {});
384
- // One line, as the hosted tools answer: indentation is tokens an agent pays for and reads nothing from.
385
- // Escaped as the hosted server escapes its replies (mvp/server.ts): stored text is untrusted, and a raw bidi
386
- // override or zero-width character would reach the agent's transcript unrendered.
387
- return { content: [{ type: 'text', text: (0, sanitize_1.escapeInvisibleControls)(JSON.stringify(result)) }] };
388
- }
389
- catch (err) {
390
- return { content: [{ type: 'text', text: (0, sanitize_1.escapeInvisibleControls)(`Error: ${err instanceof Error ? err.message : String(err)}`) }], isError: true };
391
- }
392
- });
393
- }
394
- async function startMcpServer(db, vectorIndex, provider, config) {
395
- const server = new index_js_1.Server({ name: 'renor', version: buildIdentity_1.ENGINE_VERSION }, { capabilities: { tools: {} } });
396
- const evidence = (0, options_1.resolveEvidenceToolOptions)(config);
397
- const tools = createMcpTools(db, vectorIndex, provider, config, evidence);
398
- registerMcpHandlers(server, tools);
399
- (0, requestErrors_1.mapInvalidParams)(server);
400
- // A frame that is not UTF-8 is not run: the transport would decode it as U+FFFD and a tool would store that.
401
- // utf8Lines answers such a frame itself, so the transport below only ever reads frames that passed the check.
402
- const stdin = utf8Lines(process.stdin, () => {
403
- process.stdout.write(JSON.stringify({
404
- jsonrpc: '2.0', id: null, error: { code: -32700, message: 'Parse error: the frame is not valid UTF-8; nothing was run' },
405
- }) + '\n');
406
- });
407
- const transport = new stdio_js_1.StdioServerTransport(stdin);
408
- // The SDK reports a frame it cannot read to onerror and sends nothing back, so the client that sent it waits
409
- // forever. The request id is unreadable, so the reply carries id null: -32700 when the line is not JSON, -32600
410
- // when it is JSON but not one request (a batch, an invalid id, a bad envelope). Nothing else is written to stdout.
411
- transport.onerror = (error) => {
412
- const code = error instanceof SyntaxError ? types_js_1.ErrorCode.ParseError
413
- : (0, requestErrors_1.isZodError)(error) ? types_js_1.ErrorCode.InvalidRequest
414
- : null;
415
- if (code === null)
416
- return;
417
- const reply = {
418
- jsonrpc: '2.0',
419
- id: null,
420
- error: { code, message: code === types_js_1.ErrorCode.ParseError ? 'Parse error' : 'Invalid Request' },
421
- };
422
- // The SDK's response type does not allow a null id, which the spec does for these two errors.
423
- transport.send(reply).catch(() => { });
424
- };
425
- await server.connect(transport);
426
- }
@@ -1,43 +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.claimAgentLease = claimAgentLease;
7
- exports.newLeaseHolder = newLeaseHolder;
8
- const crypto_1 = __importDefault(require("crypto"));
9
- const sourceName_1 = require("./sourceName");
10
- /**
11
- * One live stdio process may hold a hive name. The lease is a row in the workspace database, so a second
12
- * process sharing that file sees it. The holder is `${pid}:${nonce}`. A dead pid is stolen; a live pid is not.
13
- * Closing the MCP client does not release it: the process is still that name until it exits.
14
- */
15
- function claimAgentLease(db, agent, holder, now = new Date()) {
16
- const identity = (0, sourceName_1.hiveIdentity)(agent);
17
- if (!identity) {
18
- throw new Error('a hive lease needs a name that contains a letter or a digit, other than unnamed');
19
- }
20
- const existing = db.prepare('SELECT holder FROM agent_leases WHERE agent = ?').get(identity);
21
- if (existing && existing.holder !== holder && pidAlive(existing.holder)) {
22
- throw new Error(`another live connection is named ${JSON.stringify(identity)}; set RENOR_CONNECTION_NAME to a different name, or stop the other renor-engine mcp process, then reconnect`);
23
- }
24
- db.prepare(`INSERT INTO agent_leases (agent, holder, seen_at) VALUES (?, ?, ?)
25
- ON CONFLICT(agent) DO UPDATE SET holder = excluded.holder, seen_at = excluded.seen_at`).run(identity, holder, now.toISOString());
26
- }
27
- /** A new holder for this process. Two calls are two connections, even in one process. */
28
- function newLeaseHolder() {
29
- return `${process.pid}:${crypto_1.default.randomBytes(8).toString('hex')}`;
30
- }
31
- function pidAlive(holder) {
32
- const pid = Number(holder.split(':')[0]);
33
- // pid 0 is the process group. Never signal it.
34
- if (!Number.isInteger(pid) || pid <= 0)
35
- return false;
36
- try {
37
- process.kill(pid, 0);
38
- return true;
39
- }
40
- catch (err) {
41
- return err.code === 'EPERM';
42
- }
43
- }