renor-engine 0.0.0-stage → 1.1.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 (194) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +75 -0
  3. package/README.md +105 -2
  4. package/dist/.tsbuildinfo +1 -0
  5. package/dist/BUILD.json +6 -0
  6. package/dist/api/apiAuth.js +43 -0
  7. package/dist/api/server.js +174 -0
  8. package/dist/buildIdentity.js +149 -0
  9. package/dist/callPhases.js +82 -0
  10. package/dist/cli-extractors.js +46 -0
  11. package/dist/cli.js +500 -0
  12. package/dist/config.js +65 -0
  13. package/dist/context/composer.js +320 -0
  14. package/dist/context/readHonesty.js +478 -0
  15. package/dist/context/rowStore.js +238 -0
  16. package/dist/conversation/formatter.js +81 -0
  17. package/dist/conversation/parser.js +193 -0
  18. package/dist/conversation/types.js +2 -0
  19. package/dist/conversation/writer.js +42 -0
  20. package/dist/db/client.js +149 -0
  21. package/dist/db/handoffTable.js +31 -0
  22. package/dist/db/indexSalvage.js +101 -0
  23. package/dist/db/migrations.js +670 -0
  24. package/dist/db/readCache.js +109 -0
  25. package/dist/db/schema.js +5 -0
  26. package/dist/db/sqlite.js +396 -0
  27. package/dist/db/statements.js +89 -0
  28. package/dist/digests/digestCache.js +146 -0
  29. package/dist/digests/digests.js +597 -0
  30. package/dist/digests/worker.js +350 -0
  31. package/dist/embeddings/caching.js +337 -0
  32. package/dist/embeddings/coalescing.js +177 -0
  33. package/dist/embeddings/interactive.js +103 -0
  34. package/dist/embeddings/local.js +70 -0
  35. package/dist/embeddings/openai.js +106 -0
  36. package/dist/embeddings/rebuild.js +166 -0
  37. package/dist/embeddings/simdScan.js +260 -0
  38. package/dist/embeddings/space.js +21 -0
  39. package/dist/embeddings/types.js +14 -0
  40. package/dist/embeddings/vectorIndex.js +674 -0
  41. package/dist/embeddings/vertex.js +334 -0
  42. package/dist/eval/artifacts.js +42 -0
  43. package/dist/eval/cacheReport.js +146 -0
  44. package/dist/eval/comparison.js +182 -0
  45. package/dist/eval/context700-candidate.js +145 -0
  46. package/dist/eval/evalCache.js +568 -0
  47. package/dist/eval/hashEmbedding.js +35 -0
  48. package/dist/eval/indexSnapshots.js +548 -0
  49. package/dist/eval/longmemeval.js +56 -0
  50. package/dist/eval/metrics.js +181 -0
  51. package/dist/eval/pageDump.js +67 -0
  52. package/dist/eval/renorbenchBaseline.js +486 -0
  53. package/dist/eval/renorbenchDocuments.js +134 -0
  54. package/dist/eval/renorbenchProduct.js +182 -0
  55. package/dist/eval/runLedger.js +279 -0
  56. package/dist/eval/runManifest.js +485 -0
  57. package/dist/eval/scoring.js +197 -0
  58. package/dist/eval/textTokens.js +17 -0
  59. package/dist/evidence/attach.js +264 -0
  60. package/dist/evidence/background.js +147 -0
  61. package/dist/evidence/evidenceCache.js +182 -0
  62. package/dist/evidence/gate.js +37 -0
  63. package/dist/evidence/keyPool.js +85 -0
  64. package/dist/evidence/ollamaChat.js +84 -0
  65. package/dist/evidence/optin.js +165 -0
  66. package/dist/evidence/options.js +151 -0
  67. package/dist/evidence/secondPass.js +294 -0
  68. package/dist/extraction/classifyCapture.js +34 -0
  69. package/dist/extraction/decision-model.js +174 -0
  70. package/dist/extraction/eventExtractor.js +113 -0
  71. package/dist/extraction/extractionClient.js +122 -0
  72. package/dist/extraction/factExtractor.js +46 -0
  73. package/dist/extraction/factsWritePath.js +179 -0
  74. package/dist/extraction/preferenceExtractor.js +150 -0
  75. package/dist/extraction/secretGuard.js +176 -0
  76. package/dist/extraction/secretScanner.js +665 -0
  77. package/dist/extraction/typedFacts.js +224 -0
  78. package/dist/extraction/writePathAdjudication.js +110 -0
  79. package/dist/importing/apply.js +672 -0
  80. package/dist/importing/bundle-write.js +80 -0
  81. package/dist/importing/cli.js +408 -0
  82. package/dist/importing/existing.js +51 -0
  83. package/dist/importing/facts.js +121 -0
  84. package/dist/importing/forgetImported.js +79 -0
  85. package/dist/importing/hosted.js +467 -0
  86. package/dist/importing/journalLog.js +96 -0
  87. package/dist/importing/markdown.js +98 -0
  88. package/dist/importing/plan.js +201 -0
  89. package/dist/importing/secretScreen.js +113 -0
  90. package/dist/importing/serialize.js +188 -0
  91. package/dist/importing/source.js +463 -0
  92. package/dist/importing/types.js +6 -0
  93. package/dist/indexer/chunker.js +319 -0
  94. package/dist/indexer/dailyEntries.js +63 -0
  95. package/dist/indexer/hasher.js +10 -0
  96. package/dist/indexer/laneRuns.js +48 -0
  97. package/dist/indexer/o200k.js +481 -0
  98. package/dist/indexer/o200k.json +8 -0
  99. package/dist/indexer/outsideRoot.js +77 -0
  100. package/dist/indexer/parser.js +170 -0
  101. package/dist/indexer/pipeline.js +988 -0
  102. package/dist/indexer/staleness.js +201 -0
  103. package/dist/indexer/tokenizer.js +77 -0
  104. package/dist/indexer/walker.js +170 -0
  105. package/dist/mcp/hive.js +88 -0
  106. package/dist/mcp/requestErrors.js +47 -0
  107. package/dist/mcp/server.js +426 -0
  108. package/dist/memory/agentLease.js +43 -0
  109. package/dist/memory/confinedWrite.js +315 -0
  110. package/dist/memory/controls.js +73 -0
  111. package/dist/memory/dailyNote.js +265 -0
  112. package/dist/memory/durableAppend.js +57 -0
  113. package/dist/memory/factConflicts.js +203 -0
  114. package/dist/memory/factTerms.js +321 -0
  115. package/dist/memory/handoff.js +363 -0
  116. package/dist/memory/hiveNameHeader.cjs +10 -0
  117. package/dist/memory/inventory.js +55 -0
  118. package/dist/memory/invisibleText.js +20 -0
  119. package/dist/memory/lifecycle.js +1991 -0
  120. package/dist/memory/limits.js +13 -0
  121. package/dist/memory/requests.js +183 -0
  122. package/dist/memory/sourceName.js +276 -0
  123. package/dist/memory/statusFacts.js +61 -0
  124. package/dist/memory/whatsNew.js +158 -0
  125. package/dist/memory/writer.js +175 -0
  126. package/dist/memory/writerConflicts.js +85 -0
  127. package/dist/memorymap/memoryMap.js +1594 -0
  128. package/dist/memorymap/replacedJournal.js +58 -0
  129. package/dist/memorymap/replacements.js +410 -0
  130. package/dist/mvp/activity.js +86 -0
  131. package/dist/mvp/agentMemory.js +1800 -0
  132. package/dist/mvp/answerCheck.js +211 -0
  133. package/dist/mvp/auth.js +531 -0
  134. package/dist/mvp/cli.js +75 -0
  135. package/dist/mvp/client-store.js +462 -0
  136. package/dist/mvp/compileCache.js +242 -0
  137. package/dist/mvp/compileSeed.js +33 -0
  138. package/dist/mvp/config.js +288 -0
  139. package/dist/mvp/connectionStatus.js +80 -0
  140. package/dist/mvp/contextDistinct.js +53 -0
  141. package/dist/mvp/contextProfile.js +87 -0
  142. package/dist/mvp/contextRelevance.js +413 -0
  143. package/dist/mvp/directorySurface.js +68 -0
  144. package/dist/mvp/engine.js +1078 -0
  145. package/dist/mvp/exportAll.js +374 -0
  146. package/dist/mvp/factLanePack.js +77 -0
  147. package/dist/mvp/factMarkers.js +65 -0
  148. package/dist/mvp/factsLane.js +203 -0
  149. package/dist/mvp/hiddenSpans.js +585 -0
  150. package/dist/mvp/history.js +414 -0
  151. package/dist/mvp/hitDates.js +31 -0
  152. package/dist/mvp/hitFloor.js +36 -0
  153. package/dist/mvp/identity.js +18 -0
  154. package/dist/mvp/idle.js +113 -0
  155. package/dist/mvp/log.js +118 -0
  156. package/dist/mvp/memoryDir.js +98 -0
  157. package/dist/mvp/posthogMcp.js +210 -0
  158. package/dist/mvp/readScope.js +104 -0
  159. package/dist/mvp/readUsefulness.js +222 -0
  160. package/dist/mvp/sanitize.js +48 -0
  161. package/dist/mvp/schemaRejections.js +91 -0
  162. package/dist/mvp/scope.js +127 -0
  163. package/dist/mvp/server.js +2604 -0
  164. package/dist/mvp/serverCard.js +38 -0
  165. package/dist/mvp/storedSize.js +139 -0
  166. package/dist/mvp/telemetry.js +474 -0
  167. package/dist/net/poolFetch.js +174 -0
  168. package/dist/recall/actionRecall.js +282 -0
  169. package/dist/recall/autoRecall.js +447 -0
  170. package/dist/recall/searchRecall.js +189 -0
  171. package/dist/retrieval/adviceLines.js +195 -0
  172. package/dist/retrieval/adviceQuestion.js +44 -0
  173. package/dist/retrieval/conversationShape.js +44 -0
  174. package/dist/retrieval/dailyEntryTime.js +55 -0
  175. package/dist/retrieval/entityLookup.js +162 -0
  176. package/dist/retrieval/factsFirst.js +188 -0
  177. package/dist/retrieval/keyword.js +84 -0
  178. package/dist/retrieval/ledgerEventTime.js +51 -0
  179. package/dist/retrieval/ledgerLiveness.js +83 -0
  180. package/dist/retrieval/ledgerPresentation.js +161 -0
  181. package/dist/retrieval/noteIdentity.js +57 -0
  182. package/dist/retrieval/orchestrator.js +1171 -0
  183. package/dist/retrieval/plainWording.js +236 -0
  184. package/dist/retrieval/queryHelpers.js +142 -0
  185. package/dist/retrieval/questionSpan.js +475 -0
  186. package/dist/retrieval/readerEvents.js +690 -0
  187. package/dist/retrieval/readerLayout.js +422 -0
  188. package/dist/retrieval/relativeTimeHints.js +113 -0
  189. package/dist/retrieval/semantic.js +67 -0
  190. package/dist/router/routerCache.js +106 -0
  191. package/dist/router/sessionRouter.js +726 -0
  192. package/dist/toolRefusal.js +120 -0
  193. package/dist/watcher/watcher.js +77 -0
  194. package/package.json +118 -4
@@ -0,0 +1,151 @@
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.resolveEvidenceBounds = resolveEvidenceBounds;
7
+ exports.resolveEvidenceProvider = resolveEvidenceProvider;
8
+ exports.evidenceCachePath = evidenceCachePath;
9
+ exports.resolveEvidenceChat = resolveEvidenceChat;
10
+ exports.resolveEvidenceToolOptions = resolveEvidenceToolOptions;
11
+ exports.createEvidenceExtractor = createEvidenceExtractor;
12
+ const node_path_1 = __importDefault(require("node:path"));
13
+ const ollamaChat_1 = require("./ollamaChat");
14
+ const keyPool_1 = require("./keyPool");
15
+ const evidenceCache_1 = require("./evidenceCache");
16
+ const optin_1 = require("./optin");
17
+ const gate_1 = require("./gate");
18
+ const secondPass_1 = require("./secondPass");
19
+ /**
20
+ * RENOR_EVIDENCE_MAX_TOKENS (a positive integer) and
21
+ * RENOR_EVIDENCE_REASONING_EFFORT (none | minimal | low | medium | high).
22
+ * Unset or blank means no bound, which is the shipped request. A value that
23
+ * does not parse throws, like an invalid RENOR_EVIDENCE_MODE: an operator who
24
+ * set a bound must not get an unbounded extractor because of a typo.
25
+ */
26
+ function resolveEvidenceBounds(env = process.env) {
27
+ const bounds = {};
28
+ const maxTokens = (env.RENOR_EVIDENCE_MAX_TOKENS ?? '').trim();
29
+ if (maxTokens !== '') {
30
+ const value = Number(maxTokens);
31
+ if (!/^[1-9][0-9]*$/.test(maxTokens) || !Number.isSafeInteger(value)) {
32
+ throw new Error(`Invalid RENOR_EVIDENCE_MAX_TOKENS "${maxTokens}": must be a positive integer`);
33
+ }
34
+ bounds.maxTokens = value;
35
+ }
36
+ const effort = (env.RENOR_EVIDENCE_REASONING_EFFORT ?? '').trim().toLowerCase();
37
+ if (effort !== '') {
38
+ if (!secondPass_1.EVIDENCE_REASONING_EFFORTS.includes(effort)) {
39
+ throw new Error(`Invalid RENOR_EVIDENCE_REASONING_EFFORT "${effort}": must be ${secondPass_1.EVIDENCE_REASONING_EFFORTS.join(' | ')}`);
40
+ }
41
+ bounds.reasoningEffort = effort;
42
+ }
43
+ return bounds;
44
+ }
45
+ /**
46
+ * The OpenAI-compatible provider the query-time model passes call: RENOR_EVIDENCE_BASE_URL (unset or blank: the
47
+ * transport's Ollama Cloud default) and the key pool. Shared by the evidence pass and the session router
48
+ * (src/router/sessionRouter.ts), so the two can never disagree about which keys exist, and the URL returned is the
49
+ * one the Ollama keys were judged against. Values are never logged.
50
+ */
51
+ function resolveEvidenceProvider(env = process.env) {
52
+ // Blank is the default endpoint (Ollama Cloud), like an unset variable.
53
+ const baseUrl = env.RENOR_EVIDENCE_BASE_URL?.trim() || undefined;
54
+ const keys = [
55
+ ...(env.RENOR_EVIDENCE_API_KEYS ?? '').split(','),
56
+ env.RENOR_EVIDENCE_API_KEY ?? '',
57
+ // The Ollama pool is collected by the shared helper, never re-listed here:
58
+ // this module used to name `OLLAMA_CLOUD_KEY_1..3` while the write-path
59
+ // extractor named six, so with the first three keys at their monthly
60
+ // ceiling the evidence layer vanished for 77 of 500 cases with no signal in
61
+ // the manifest (issue #222). One list, two readers. And only for an Ollama
62
+ // Cloud endpoint: another provider never receives an Ollama key.
63
+ ...(0, keyPool_1.ollamaKeysForEndpoint)(baseUrl, env).keys,
64
+ ].map(k => k.trim()).filter(Boolean);
65
+ return { baseUrl, keys };
66
+ }
67
+ /** Where the evidence pass caches, and where an opted-in workspace's daily allowance is counted: RENOR_EVIDENCE_CACHE, else beside the index. */
68
+ function evidenceCachePath(config, env = process.env) {
69
+ return env.RENOR_EVIDENCE_CACHE?.trim() || node_path_1.default.join(node_path_1.default.dirname(config.dbPath), 'evidence-cache.db');
70
+ }
71
+ /**
72
+ * The evidence pass's model and transport. A workspace that opted in (RENOR_EVIDENCE_OPTIN, see optin.ts) reaches Ollama
73
+ * Cloud with the key file's key alone, on gemma4:31b, and spends its daily allowance per HTTP attempt; otherwise the
74
+ * configured endpoint and keys (resolveEvidenceProvider) with RENOR_EVIDENCE_MODEL. No chat when no key is configured.
75
+ * `transport` bounds each call; unset, the transport's own defaults. Another reader of notes at query time (the answer
76
+ * check, mvp/answerCheck.ts) builds its chat here, so it sends them only where the evidence pass would.
77
+ */
78
+ function resolveEvidenceChat(config, env = process.env, transport = {}) {
79
+ const optin = (0, optin_1.resolveEvidenceOptin)(env);
80
+ const { baseUrl, keys } = resolveEvidenceProvider(env);
81
+ const bounds = {
82
+ ...(transport.timeoutMs !== undefined ? { timeoutMs: transport.timeoutMs } : {}),
83
+ ...(transport.maxRetries !== undefined ? { maxRetries: transport.maxRetries } : {}),
84
+ };
85
+ let chat;
86
+ if (optin) {
87
+ const optinKey = (0, optin_1.optinEndpointAllowed)(baseUrl) ? (0, optin_1.readOptinKey)(optin.keyFile) : null;
88
+ if (optinKey) {
89
+ const dailyCap = optin.dailyCap;
90
+ const cachePath = evidenceCachePath(config, env);
91
+ // The allowance is spent per HTTP attempt (a retried 429 or 5xx is another request to the provider), so it
92
+ // sits inside the transport rather than around it.
93
+ chat = (0, ollamaChat_1.createOllamaChat)({
94
+ keys: [optinKey],
95
+ beforeAttempt: (0, optin_1.dailyBudgetGate)(() => new optin_1.EvidenceDailyBudget(cachePath, dailyCap)),
96
+ ...bounds,
97
+ });
98
+ }
99
+ }
100
+ else if (keys.length > 0) {
101
+ chat = (0, ollamaChat_1.createOllamaChat)({ baseUrl, keys, ...bounds });
102
+ }
103
+ // The opt-in is the gemma4:31b decision: a stray RENOR_EVIDENCE_MODEL does not change what the privacy page names.
104
+ const model = optin ? secondPass_1.EVIDENCE_MODEL_DEFAULT : env.RENOR_EVIDENCE_MODEL?.trim() || secondPass_1.EVIDENCE_MODEL_DEFAULT;
105
+ return { model, ...(chat ? { chat } : {}), optin };
106
+ }
107
+ function resolveEvidenceToolOptions(config, env = process.env) {
108
+ const mode = (0, gate_1.resolveEvidenceMode)(env);
109
+ const cachePath = evidenceCachePath(config, env);
110
+ const bounds = resolveEvidenceBounds(env);
111
+ // The model, the transport and the opt-in (key file, endpoint, gemma4:31b, daily allowance): resolveEvidenceChat.
112
+ const { model, chat, optin } = resolveEvidenceChat(config, env);
113
+ // Coverage knob (RENOR_EVIDENCE_SESSIONS). Default 20 matches the
114
+ // promotion-grade full-500 arm. One model call per uncached search,
115
+ // regardless of session count; explicit values remain bounded.
116
+ // A value that floors below 1 is treated as UNSET, not as "0 sessions". A
117
+ // fractional setting is finite and positive, so a bare `> 0` test let it
118
+ // through and `Math.floor` turned it into 0 — and 0 is not a silent no-op:
119
+ // `loadEvidenceSessionsFromVault` rejects it (`maxSessions must be > 0`),
120
+ // which the `evidence` MCP tool surfaces as a thrown error and the search path
121
+ // swallows as `extractor-error`. Floor first, then require >= 1.
122
+ const requested = Number(env.RENOR_EVIDENCE_SESSIONS ?? '');
123
+ const requestedSessions = Number.isFinite(requested) ? Math.floor(requested) : Number.NaN;
124
+ const maxSessions = Number.isFinite(requestedSessions) && requestedSessions >= 1
125
+ ? Math.min(requestedSessions, 50)
126
+ : secondPass_1.MAX_EVIDENCE_SESSIONS;
127
+ return {
128
+ mode,
129
+ model,
130
+ maxSessions,
131
+ charCap: secondPass_1.EVIDENCE_SESSION_CHAR_CAP,
132
+ cachePath,
133
+ ...(chat ? { chat } : {}),
134
+ ...(optin ? { dailyCap: optin.dailyCap } : {}),
135
+ ...(bounds.maxTokens !== undefined || bounds.reasoningEffort !== undefined ? { bounds } : {}),
136
+ };
137
+ }
138
+ function createEvidenceExtractor(evidence) {
139
+ if (!evidence.chat)
140
+ return null;
141
+ let cache = null;
142
+ if (evidence.cachePath) {
143
+ try {
144
+ cache = new evidenceCache_1.EvidenceCache(evidence.cachePath);
145
+ }
146
+ catch (err) {
147
+ process.stderr.write(`[evidence] cache unavailable: ${err instanceof Error ? err.message : String(err)}\n`);
148
+ }
149
+ }
150
+ return new secondPass_1.EvidenceExtractor({ chat: evidence.chat, cache, model: evidence.model, bounds: evidence.bounds });
151
+ }
@@ -0,0 +1,294 @@
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.EvidenceExtractor = exports.EvidenceReplyError = exports.EVIDENCE_REASONING_EFFORTS = exports.EVIDENCE_SESSION_CHAR_CAP = exports.MAX_EVIDENCE_SESSIONS = exports.EVIDENCE_MODEL_DEFAULT = exports.EVIDENCE_PROMPT_TEMPLATE = void 0;
7
+ exports.loadEvidenceSessionsFromVault = loadEvidenceSessionsFromVault;
8
+ exports.renderEvidencePrompt = renderEvidencePrompt;
9
+ exports.parseEvidenceItems = parseEvidenceItems;
10
+ const promises_1 = __importDefault(require("node:fs/promises"));
11
+ const node_path_1 = __importDefault(require("node:path"));
12
+ const parser_1 = require("../indexer/parser");
13
+ const evidenceCache_1 = require("./evidenceCache");
14
+ /**
15
+ * H10-v4 second-pass, question-conditioned evidence extraction (product path).
16
+ *
17
+ * Frozen mechanism (champion = 453/500 = 90.6%, docs/evals/results/
18
+ * H10-V4-FULL500-20260919.md): given a question and the sessions retrieval
19
+ * already selected, the extractor (gemma4:31b) lists every distinct relevant
20
+ * item/event/amount with dates. It never computes totals and never sees gold.
21
+ *
22
+ * Productization adds:
23
+ * - content-addressed caching (exact rendered prompt -> parsed items, and,
24
+ * for a day, a reply that was not a table: EVIDENCE_FAILURE_TTL_MS);
25
+ * - session selection matching the promoted arm (20 sessions, 6,000 chars/session);
26
+ * - gating (off | auto | always), default off (src/evidence/gate.ts).
27
+ */
28
+ exports.EVIDENCE_PROMPT_TEMPLATE = `You extract evidence items from a user's dated conversation history.
29
+
30
+ QUESTION: {question}
31
+
32
+ Below are the sessions (with dates) that a memory system retrieved for this question.
33
+
34
+ TASK: List EVERY distinct item, event, or amount mentioned in the sessions that could be
35
+ relevant to answering the question. Rules:
36
+ - One entry per distinct item (e.g., each product, each trip, each device, each course, each amount).
37
+ - "action" = what happened (bought / visited / completed / raised / inherited / ...).
38
+ - "amount" = numeric value + unit when stated (e.g., "3 days", "$120", "100 points"); else "".
39
+ - "date" = the session/mention date when available; else "".
40
+ - Do NOT compute any totals. Do NOT merge different items. Include every candidate even if uncertain.
41
+ - Include items the question might count, even when the sessions mention them in passing.
42
+
43
+ SESSIONS:
44
+ {sessions}
45
+
46
+ Respond with ONLY minified JSON, no markdown:
47
+ {{"items":[{{"item":"...","action":"...","amount":"...","date":"..."}}]}}`;
48
+ exports.EVIDENCE_MODEL_DEFAULT = 'gemma4:31b';
49
+ exports.MAX_EVIDENCE_SESSIONS = 20;
50
+ exports.EVIDENCE_SESSION_CHAR_CAP = 6000;
51
+ /** The `reasoning_effort` values OpenAI-compatible providers share. Which ones a model accepts is the provider's call. */
52
+ exports.EVIDENCE_REASONING_EFFORTS = ['none', 'minimal', 'low', 'medium', 'high'];
53
+ /**
54
+ * Walk ranked hits as session identities, then hydrate each selected session
55
+ * from the complete source note. Retrieved chunk text is never extractor input.
56
+ * Unreadable/virtual/out-of-vault paths are skipped and later hits backfill.
57
+ * The first maxSessions unique notes by encounter rank are selected.
58
+ */
59
+ async function loadEvidenceSessionsFromVault(hits, vaultPath, opts = {}) {
60
+ const maxSessions = opts.maxSessions ?? exports.MAX_EVIDENCE_SESSIONS;
61
+ const charCap = opts.charCap ?? exports.EVIDENCE_SESSION_CHAR_CAP;
62
+ if (maxSessions <= 0)
63
+ throw new Error('maxSessions must be > 0');
64
+ if (charCap <= 0)
65
+ throw new Error('charCap must be > 0');
66
+ const root = node_path_1.default.resolve(vaultPath);
67
+ const seen = new Set();
68
+ const sessions = [];
69
+ for (const hit of hits) {
70
+ if (!hit.notePath || seen.has(hit.notePath))
71
+ continue;
72
+ seen.add(hit.notePath);
73
+ const candidate = node_path_1.default.isAbsolute(hit.notePath)
74
+ ? node_path_1.default.resolve(hit.notePath)
75
+ : node_path_1.default.resolve(root, hit.notePath);
76
+ const rel = node_path_1.default.relative(root, candidate);
77
+ if (rel === '..' || rel.startsWith(`..${node_path_1.default.sep}`) || node_path_1.default.isAbsolute(rel))
78
+ continue;
79
+ let raw;
80
+ try {
81
+ raw = await promises_1.default.readFile(candidate, 'utf8');
82
+ }
83
+ catch {
84
+ continue;
85
+ }
86
+ const parsedBody = (0, parser_1.parseNote)(candidate, raw).body;
87
+ const body = opts.transformBody ? opts.transformBody(parsedBody) : parsedBody;
88
+ if (!body)
89
+ continue;
90
+ const text = body.length > charCap ? `${body.slice(0, charCap)} …[truncated]` : body;
91
+ sessions.push({ stem: hit.notePath, date: hit.session_date, text });
92
+ if (sessions.length >= maxSessions)
93
+ break;
94
+ }
95
+ return sessions;
96
+ }
97
+ function sessionBlock(session) {
98
+ const date = session.date ?? '';
99
+ return `--- Session dated ${date} ---\n${session.text}`;
100
+ }
101
+ /** Exact prompt the extractor receives; the cache keys on this string. */
102
+ function renderEvidencePrompt(query, sessions) {
103
+ return exports.EVIDENCE_PROMPT_TEMPLATE
104
+ .replace('{question}', query)
105
+ .replace('{sessions}', sessions.map(sessionBlock).join('\n\n'));
106
+ }
107
+ /**
108
+ * The extractor answered, and its answer is not a table: it did not parse, or it stopped at the token limit. This is
109
+ * the one failure a search remembers (EVIDENCE_FAILURE_TTL_MS); an error before any answer is not. The message is the
110
+ * parse error's, so a caller that reads messages reads what it read before.
111
+ */
112
+ class EvidenceReplyError extends Error {
113
+ }
114
+ exports.EvidenceReplyError = EvidenceReplyError;
115
+ /**
116
+ * Fail-fast parse: malformed items JSON throws; it is never cached.
117
+ *
118
+ * A reply the strict parse rejects is repaired for each malformed shape extractors were seen to write (2026-10-03
119
+ * benchmark runs; tests/fixtures/evidence-replies.json), and the first repair whose result is, whole, a JSON object
120
+ * with an items list gives the table. A well-formed object is found inside surrounding text, as it always was; a
121
+ * repaired one is not, because a sentence that only quotes malformed JSON (a note's copy of the prompt's example) is no
122
+ * table. A repair changes structure only, never a character inside a string, so no value is guessed. Any other reply
123
+ * still throws the strict parse's own error.
124
+ */
125
+ function parseEvidenceItems(raw) {
126
+ const s = raw.trim().replace(/^```(?:json)?|```$/gm, '').trim();
127
+ try {
128
+ return parseItemsObject(s);
129
+ }
130
+ catch (err) {
131
+ for (const repaired of repairedReplies(s)) {
132
+ try {
133
+ return itemsFromPayload(JSON.parse(repaired));
134
+ }
135
+ catch {
136
+ // Not this shape.
137
+ }
138
+ }
139
+ throw err;
140
+ }
141
+ }
142
+ /** A JSON string (or the one the reply ends inside), a run of `{` or a run of `}`: a brace inside a string is text. */
143
+ const STRING_OR_BRACE_RUN = /"(?:[^"\\]|\\[\s\S])*(?:"|$)|\{+|\}+/g;
144
+ function* repairedReplies(s) {
145
+ // The rows without the object around them: [{"item":...},...].
146
+ if (s.startsWith('['))
147
+ yield `{"items":${s}}`;
148
+ // The prompt's own example has doubled braces ({{"items":[{{...}}]}}), and models copy them, on every object or on
149
+ // the outer one only: a run of 2n braces outside strings stands for n.
150
+ const halved = s.replace(STRING_OR_BRACE_RUN, run => (run[0] === '"' || run.length % 2 === 1 ? run : run.slice(run.length / 2)));
151
+ if (halved !== s)
152
+ yield halved;
153
+ // A reply cut off inside the list (at the token limit): the rows before the cut, then the list and object closed.
154
+ // The cut row is dropped, never completed.
155
+ let end = -1;
156
+ for (const run of s.matchAll(STRING_OR_BRACE_RUN))
157
+ if (run[0][0] === '}')
158
+ end = run.index + run[0].length;
159
+ if (end > 0)
160
+ yield `${s.slice(0, end)}]}`;
161
+ }
162
+ function parseItemsObject(s) {
163
+ const m = s.match(/\{[\s\S]*\}/);
164
+ if (!m)
165
+ throw new Error('extractor response did not contain a JSON object');
166
+ let payload;
167
+ try {
168
+ payload = JSON.parse(m[0]);
169
+ }
170
+ catch (err) {
171
+ const detail = err instanceof Error ? err.message : String(err);
172
+ throw new Error(`extractor returned malformed JSON: ${detail}`);
173
+ }
174
+ return itemsFromPayload(payload);
175
+ }
176
+ function itemsFromPayload(payload) {
177
+ const items = payload.items;
178
+ if (!Array.isArray(items))
179
+ throw new Error('extractor JSON must contain an items list');
180
+ return items.map((rawItem, index) => {
181
+ if (!rawItem || typeof rawItem !== 'object' || Array.isArray(rawItem)) {
182
+ throw new Error(`extractor item ${index} must be an object`);
183
+ }
184
+ const it = rawItem;
185
+ const item = typeof it.item === 'string' ? it.item.trim() : '';
186
+ if (!item)
187
+ throw new Error(`extractor item ${index} must contain a non-empty item string`);
188
+ return {
189
+ item,
190
+ ...(it.action !== undefined ? { action: String(it.action) } : {}),
191
+ ...(it.amount !== undefined ? { amount: String(it.amount) } : {}),
192
+ ...(it.date !== undefined ? { date: String(it.date) } : {}),
193
+ };
194
+ });
195
+ }
196
+ /**
197
+ * Runs (or replays) the second pass. A cache hit performs no model call.
198
+ */
199
+ class EvidenceExtractor {
200
+ chat;
201
+ cache;
202
+ model;
203
+ bounds;
204
+ constructor(opts) {
205
+ this.chat = opts.chat;
206
+ this.cache = opts.cache ?? null;
207
+ this.model = opts.model ?? exports.EVIDENCE_MODEL_DEFAULT;
208
+ this.bounds = opts.bounds ?? {};
209
+ }
210
+ /** The exact prompt the extractor receives for `query` over `sessions`, and the key its table is cached under. */
211
+ promptAndKey(query, sessions) {
212
+ const prompt = renderEvidencePrompt(query, sessions);
213
+ // A bound changes what the provider returns, so it is part of the cache
214
+ // identity: a bounded arm never replays an unbounded arm's table.
215
+ return { prompt, key: evidenceCache_1.EvidenceCache.computeKey(this.model, prompt, this.bounds) };
216
+ }
217
+ /** The key extract() caches `query` over `sessions` under, computed the one way extract() computes it. */
218
+ cacheKey(query, sessions) {
219
+ return this.promptAndKey(query, sessions).key;
220
+ }
221
+ /** The failure remembered under `key` (EVIDENCE_FAILURE_TTL_MS), or null. Never calls the model. */
222
+ replayFailure(key) {
223
+ return this.cache?.getFailure(key) ?? null;
224
+ }
225
+ /**
226
+ * Remembers that the extraction under `key` failed with `error`, so the same request does not pay for it again.
227
+ * A failure the cache cannot store is still reported, as it always was; only the next search pays again.
228
+ */
229
+ rememberFailure(key, error) {
230
+ try {
231
+ this.cache?.putFailure(key, { model: this.model, error, failedAt: Date.now() });
232
+ }
233
+ catch (err) {
234
+ process.stderr.write(`[evidence] could not remember a failed extraction: ${err instanceof Error ? err.message : String(err)}\n`);
235
+ }
236
+ }
237
+ /** The extraction cached under `key`, or null. Never calls the model. */
238
+ replay(key) {
239
+ const cached = this.cache?.get(key) ?? null;
240
+ if (!cached)
241
+ return null;
242
+ return {
243
+ items: cached.items,
244
+ sessions: cached.sessions,
245
+ fromCache: true,
246
+ model: cached.model,
247
+ usage: { promptTokens: cached.promptTokens, completionTokens: cached.completionTokens },
248
+ latencyMs: 0,
249
+ };
250
+ }
251
+ async extract(query, sessions) {
252
+ if (sessions.length === 0)
253
+ throw new Error('evidence extraction requires at least one session');
254
+ const { prompt, key } = this.promptAndKey(query, sessions);
255
+ const cached = this.replay(key);
256
+ if (cached)
257
+ return cached;
258
+ const res = await this.chat({
259
+ model: this.model,
260
+ messages: [{ role: 'user', content: prompt }],
261
+ temperature: 0,
262
+ ...(this.bounds.maxTokens !== undefined ? { max_tokens: this.bounds.maxTokens } : {}),
263
+ ...(this.bounds.reasoningEffort !== undefined ? { reasoning_effort: this.bounds.reasoningEffort } : {}),
264
+ });
265
+ let items;
266
+ try {
267
+ items = parseEvidenceItems(res.content);
268
+ }
269
+ catch (err) {
270
+ const detail = err instanceof Error ? err.message : String(err);
271
+ // Same parse failure (same retry policy), but a reply cut at a token
272
+ // limit says so rather than reading as malformed by the model.
273
+ throw new EvidenceReplyError(res.finishReason === 'length'
274
+ ? `extractor reply stopped at the token limit (finish_reason=length): ${detail}`
275
+ : detail);
276
+ }
277
+ const sessionMeta = sessions.map(s => ({ stem: s.stem, ...(s.date ? { date: s.date } : {}), chars: s.text.length }));
278
+ const usage = {
279
+ promptTokens: res.promptTokens ?? Math.ceil(prompt.length / 4),
280
+ completionTokens: res.completionTokens ?? Math.ceil(res.content.length / 4),
281
+ };
282
+ this.cache?.put(key, {
283
+ items,
284
+ sessions: sessionMeta,
285
+ model: this.model,
286
+ question: query,
287
+ promptTokens: usage.promptTokens,
288
+ completionTokens: usage.completionTokens,
289
+ createdAt: new Date().toISOString(),
290
+ });
291
+ return { items, sessions: sessionMeta, fromCache: false, model: this.model, usage, latencyMs: res.latencyMs };
292
+ }
293
+ }
294
+ exports.EvidenceExtractor = EvidenceExtractor;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.classifyCaptureEnabled = classifyCaptureEnabled;
4
+ exports.planCapture = planCapture;
5
+ /**
6
+ * What to do with a saved note, from the local-rules classifier only.
7
+ *
8
+ * RENOR_CLASSIFY_CAPTURE defaults OFF. Off means the live save path is unchanged: the note
9
+ * stays a raw note and nothing is classified. There is no model call, no new provider, and
10
+ * no API key. The local rules cannot honestly name a subject, predicate and object, so even
11
+ * when the flag is on this module only returns a draft. It does not write a fact.
12
+ */
13
+ const decision_model_1 = require("./decision-model");
14
+ const DURABLE = new Set(['decision', 'preference', 'fact', 'result']);
15
+ function classifyCaptureEnabled(env = process.env) {
16
+ const raw = (env.RENOR_CLASSIFY_CAPTURE ?? '').trim().toLowerCase();
17
+ return raw === '1' || raw === 'true' || raw === 'yes' || raw === 'on';
18
+ }
19
+ /** A draft only. `author` is the connection name plus "-auto", and nothing is written. */
20
+ function planCapture(text, author, env = process.env) {
21
+ if (!classifyCaptureEnabled(env))
22
+ return { disposition: 'off' };
23
+ const kind = (0, decision_model_1.classifyNote)(text);
24
+ if (kind === 'sensitive')
25
+ return { disposition: 'drop', kind };
26
+ if (!DURABLE.has(kind))
27
+ return { disposition: 'raw', kind: kind === 'request' ? 'request' : 'noise' };
28
+ return {
29
+ disposition: 'structured',
30
+ kind: kind,
31
+ statement: text.trim(),
32
+ author: `${author}-auto`,
33
+ };
34
+ }
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+ /**
3
+ * DecisionModel adapter (Lane Jev write-path plan §3): the single seam for
4
+ * extraction-time typed decisions — capture-gate classification and dedupe
5
+ * adjudication. The ledger never depends on a vendor: the default TODAY is the
6
+ * deterministic local-rules implementation (`LocalRulesDecisionModel`, zero
7
+ * model calls, zero cost); a model-backed adapter (gemma4:31b, jev, ...) plugs
8
+ * in later by implementing this interface. No vendor is hardcoded anywhere in
9
+ * the write path.
10
+ *
11
+ * Contract shape (docs/evals/LANE-JEV-WRITE-PATH.md §3):
12
+ * decide(task, input) -> { decision: TypedEnum, confidence, latencyMs, costUSDCents }
13
+ *
14
+ * Privacy mode is part of the interface: the local implementation never sends
15
+ * text off-host, and costUSDCents is 0 for every local decision.
16
+ */
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.LocalRulesDecisionModel = exports.STATIVE_CUE_RE = exports.NUMERIC_FACT_RE = void 0;
19
+ exports.classifyNote = classifyNote;
20
+ exports.defaultDecisionModel = defaultDecisionModel;
21
+ const secretScanner_1 = require("./secretScanner");
22
+ const rowStore_1 = require("../context/rowStore");
23
+ /**
24
+ * Lexical identity normalization for the dedupe verdict, shared with the
25
+ * rowStore dedupe key — this must be the SAME function the key is built from.
26
+ * It used to be a byte-copy of `normDedupeValue`, which is how it drifted into
27
+ * deleting every non-Latin character independently of the key pipeline; the two
28
+ * are now one import so a key and its adjudication verdict cannot disagree.
29
+ */
30
+ const norm = rowStore_1.normDedupeValue;
31
+ /**
32
+ * v1 numeric-fact capture cues: a number (with optional unit) anchored to a
33
+ * stative or eventive predicate. Conservative and gold-blind — the rules are
34
+ * the current-baseline classification, not a quality claim (the Lane Jev
35
+ * adapter is the seam where a stronger classifier plugs in). Exported so the
36
+ * capture-gate rules and the candidate extractor share one definition.
37
+ */
38
+ exports.NUMERIC_FACT_RE = /\b(\d+:\d{2}(?::\d{2})?|\d+(?:[.,]\d+)?)\s*(km|kg|mi|miles?|minutes?|mins?|hours?|hrs?|days?|weeks?|years?|%|percent|dollars?|usd|\$|€|£|reps?|lbs?|pounds?)?\b/i;
39
+ exports.STATIVE_CUE_RE = /\b(is|are|was|were|has|have|had|weighs?|costs?|paid|earned|runs?|ran|completed|finished|measures?|lasted|took|scored|deadline|birthday|anniversary|scheduled|due)\b/i;
40
+ /**
41
+ * Local rules DecisionModel — the default TODAY. Deterministic (same input →
42
+ * same decision), zero network, zero cost. Capture gate: a span is offered to
43
+ * fact extraction only when it carries a numeric value anchored by a cue
44
+ * verb/phrase; everything else gates to `none` in v1 (events/preferences are
45
+ * C3/C4 lanes). Dedupe: exact normalized (value, object) equality is
46
+ * `identical`; same object with a changed value is `updated`; otherwise
47
+ * `distinct`. Supersede is identity-keyed only — never blanket newest-wins
48
+ * (N18 KILL).
49
+ *
50
+ * Harmonization: the same adjudication semantics generalize per lane on the
51
+ * lane's own payload identity — event: same event + same `when` is
52
+ * `identical`, same event with a changed `when` is `updated`; preference:
53
+ * same normalized statement is `identical` (statement IS the identity),
54
+ * anything else `distinct`. The rowKind default keeps the v1 fact-lane
55
+ * callers (and their tests) byte-identical.
56
+ */
57
+ const DECISION_RE = /\b(decided|decision|we will|we'll|going with|chose|chosen|the call is|agreed to)\b/i;
58
+ const PREFERENCE_RE = /\b(prefer|preference|rather|always use|never use|don't use|do not use|instead of)\b/i;
59
+ const RESULT_RE = /\b(passed|failed|shipped|the result|finished|deployed|merged)\b/i;
60
+ const REQUEST_RE = /\b(please|can you|could you|would you|need you to|let me know|ask [a-z0-9_-]+ to)\b/i;
61
+ const NOISE_LEAD_RE = /^(ok|okay|thanks|thank you|lol|hey|on it|looking|got it|sure|yep|nope)\b/i;
62
+ const NOISE_CHATTER_RE = /\b(thanks|thank you|lol)\b/i;
63
+ const COPULA_RE = /\b(is|are|was|were)\b/i;
64
+ /**
65
+ * Cue order is fixed: a decision, preference or result is named before a question mark can
66
+ * steal it, and a numeric fact (the existing capture gate) is named before a broad "is".
67
+ * Anything else is noise. Sensitive text is refused before any of those.
68
+ */
69
+ function classifyNote(text) {
70
+ const trimmed = text.trim();
71
+ if (!trimmed)
72
+ return 'noise';
73
+ if ((0, secretScanner_1.scanSecrets)(trimmed))
74
+ return 'sensitive';
75
+ if (DECISION_RE.test(trimmed))
76
+ return 'decision';
77
+ if (PREFERENCE_RE.test(trimmed))
78
+ return 'preference';
79
+ if (RESULT_RE.test(trimmed))
80
+ return 'result';
81
+ if (LocalRulesDecisionModel.captureGate(trimmed) === 'fact')
82
+ return 'fact';
83
+ if (REQUEST_RE.test(trimmed) || /\?\s*$/.test(trimmed))
84
+ return 'request';
85
+ if (NOISE_LEAD_RE.test(trimmed) || NOISE_CHATTER_RE.test(trimmed))
86
+ return 'noise';
87
+ const words = trimmed.split(/\s+/).length;
88
+ if (COPULA_RE.test(trimmed) && words >= 4)
89
+ return 'fact';
90
+ return 'noise';
91
+ }
92
+ class LocalRulesDecisionModel {
93
+ async decide(request) {
94
+ const start = Date.now();
95
+ let result;
96
+ if (request.task === 'capture_gate') {
97
+ result = {
98
+ task: 'capture_gate',
99
+ decision: LocalRulesDecisionModel.captureGate(request.text),
100
+ confidence: 1,
101
+ latencyMs: 0,
102
+ costUSDCents: 0,
103
+ };
104
+ }
105
+ else if (request.task === 'classify_capture') {
106
+ result = {
107
+ task: 'classify_capture',
108
+ decision: LocalRulesDecisionModel.classifyNote(request.text),
109
+ confidence: 1,
110
+ latencyMs: 0,
111
+ costUSDCents: 0,
112
+ };
113
+ }
114
+ else {
115
+ result = {
116
+ task: 'dedupe_adjudication',
117
+ decision: LocalRulesDecisionModel.adjudicate(request.candidate, request.existing, request.rowKind ?? 'fact'),
118
+ confidence: 1,
119
+ latencyMs: 0,
120
+ costUSDCents: 0,
121
+ };
122
+ }
123
+ // latencyMs is reported (measured wall time), never simulated.
124
+ const latencyMs = Date.now() - start;
125
+ return { ...result, latencyMs };
126
+ }
127
+ static captureGate(text) {
128
+ if (!text || !text.trim())
129
+ return 'none';
130
+ const hasValue = exports.NUMERIC_FACT_RE.test(text);
131
+ const hasCue = exports.STATIVE_CUE_RE.test(text);
132
+ if (hasValue && hasCue)
133
+ return 'fact';
134
+ return 'none';
135
+ }
136
+ /**
137
+ * Local rules for a saved note. Same constraints as the rest of this class: deterministic,
138
+ * no network, no key, cost 0. This is not a model. It does not invent a subject, predicate
139
+ * or object, and nothing here writes a memory.
140
+ */
141
+ static classifyNote(text) {
142
+ return classifyNote(text);
143
+ }
144
+ static adjudicate(candidate, existing, rowKind = 'fact') {
145
+ if (!existing)
146
+ return 'distinct';
147
+ if (rowKind === 'event') {
148
+ const c = candidate;
149
+ const e = existing;
150
+ if (norm(c.event) !== norm(e.event))
151
+ return 'distinct';
152
+ return norm(c.when) === norm(e.when) ? 'identical' : 'updated';
153
+ }
154
+ if (rowKind === 'preference') {
155
+ const c = candidate;
156
+ const e = existing;
157
+ return norm(c.statement) === norm(e.statement) ? 'identical' : 'distinct';
158
+ }
159
+ const c = candidate;
160
+ const e = existing;
161
+ const sameObject = norm(c.object) === norm(e.object);
162
+ const sameValue = norm(c.value) === norm(e.value);
163
+ if (sameObject && sameValue)
164
+ return 'identical';
165
+ if (sameObject && !sameValue)
166
+ return 'updated';
167
+ return 'distinct';
168
+ }
169
+ }
170
+ exports.LocalRulesDecisionModel = LocalRulesDecisionModel;
171
+ /** The write path's default decision point — local rules today, vendor later. */
172
+ function defaultDecisionModel() {
173
+ return new LocalRulesDecisionModel();
174
+ }