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,182 +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.EvidenceCache = exports.EVIDENCE_FAILURE_TTL_MS = exports.EVIDENCE_SCHEMA_VERSION = void 0;
7
- exports.dropEvidenceForStems = dropEvidenceForStems;
8
- const node_fs_1 = __importDefault(require("node:fs"));
9
- const node_crypto_1 = __importDefault(require("node:crypto"));
10
- const node_path_1 = __importDefault(require("node:path"));
11
- const sqlite_1 = __importDefault(require("../db/sqlite"));
12
- /**
13
- * Content-addressed cache for the H10 second-pass evidence extraction.
14
- *
15
- * Key = sha256(EVIDENCE_SCHEMA_VERSION \0 model \0 prompt), where prompt is the
16
- * exact rendered extractor prompt; a `bounds:` segment precedes the prompt only
17
- * when RENOR_EVIDENCE_MAX_TOKENS or RENOR_EVIDENCE_REASONING_EFFORT is set. Any
18
- * question, session-text, selection, or prompt-template change invalidates only
19
- * the affected entries. The same question over unchanged session content
20
- * replays for zero model cost.
21
- *
22
- * Stores parsed items (the extractor's JSON must parse before an entry is
23
- * written — fail-fast extraction never caches a malformed response). A failed
24
- * extraction is remembered apart from the tables, under the same key, for
25
- * EVIDENCE_FAILURE_TTL_MS (table evidence_failures).
26
- *
27
- * Design follows the eval cache (src/eval/evalCache.ts): SQLite + WAL +
28
- * busy_timeout=10000 + INSERT OR IGNORE.
29
- */
30
- exports.EVIDENCE_SCHEMA_VERSION = 'evidence-v1';
31
- /**
32
- * How long a failed extraction is remembered: 24 hours.
33
- *
34
- * Measured 2026-10-03 (GLM-5.3-Flash on Nebius, dev100): 39% of extraction
35
- * spend went to replies that reasoned to the provider's 8,192-token cap and
36
- * returned no content. Only tables were cached, so every repeat of such a
37
- * search paid for its three attempts again, and waited minutes for them.
38
- *
39
- * Only a reply is remembered: the model answered the last attempt, and its
40
- * answer is not a table (secondPass.ts EvidenceReplyError). An error before any
41
- * reply (a 429, a 5xx, a timeout, a refused request, no connection) says
42
- * nothing about the request and is never remembered: the next search asks
43
- * again. The key is the table's (model, bounds, exact prompt), so anything that
44
- * could change the outcome by itself (other notes, another question, model or
45
- * bound) is a new key and is asked at once. What a day gives up is luck, the
46
- * same request answered better next time, for at most one paid failure per
47
- * request per day. DELETE FROM evidence_failures forgets them all at once.
48
- */
49
- exports.EVIDENCE_FAILURE_TTL_MS = 24 * 60 * 60 * 1000;
50
- class EvidenceCache {
51
- db;
52
- lookupStmt;
53
- insertStmt;
54
- touchStmt;
55
- failureLookupStmt;
56
- failureTouchStmt;
57
- failureSweepStmt;
58
- failurePutStmt;
59
- constructor(dbPath) {
60
- node_fs_1.default.mkdirSync(node_path_1.default.dirname(dbPath), { recursive: true });
61
- this.db = new sqlite_1.default(dbPath);
62
- this.db.pragma('journal_mode = WAL');
63
- this.db.pragma('busy_timeout = 10000');
64
- this.db.exec(`
65
- CREATE TABLE IF NOT EXISTS evidence_cache (
66
- cache_key TEXT PRIMARY KEY,
67
- schema_version TEXT NOT NULL,
68
- model TEXT NOT NULL,
69
- question TEXT NOT NULL,
70
- sessions_json TEXT NOT NULL,
71
- items_json TEXT NOT NULL,
72
- prompt_tokens INTEGER NOT NULL DEFAULT 0,
73
- completion_tokens INTEGER NOT NULL DEFAULT 0,
74
- created_at TEXT NOT NULL,
75
- last_used_at TEXT,
76
- hits INTEGER NOT NULL DEFAULT 0
77
- )
78
- `);
79
- // Cache lookups are content-addressed by the cache_key primary key. Older
80
- // evidence-v1 databases created a secondary question index that no runtime
81
- // query uses; remove it so inserts do not maintain a redundant B-tree.
82
- this.db.exec('DROP INDEX IF EXISTS idx_evidence_cache_question');
83
- this.db.exec(`
84
- CREATE TABLE IF NOT EXISTS evidence_failures (
85
- cache_key TEXT PRIMARY KEY,
86
- model TEXT NOT NULL,
87
- error TEXT NOT NULL,
88
- failed_at_ms INTEGER NOT NULL,
89
- hits INTEGER NOT NULL DEFAULT 0
90
- )
91
- `);
92
- this.lookupStmt = this.db.prepare('SELECT items_json, sessions_json, model, question, prompt_tokens, completion_tokens, created_at, hits FROM evidence_cache WHERE cache_key = ?');
93
- this.insertStmt = this.db.prepare(`INSERT OR IGNORE INTO evidence_cache
94
- (cache_key, schema_version, model, question, sessions_json, items_json,
95
- prompt_tokens, completion_tokens, created_at, last_used_at, hits)
96
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0)`);
97
- this.touchStmt = this.db.prepare('UPDATE evidence_cache SET hits = hits + 1, last_used_at = ? WHERE cache_key = ?');
98
- this.failureLookupStmt = this.db.prepare('SELECT model, error, failed_at_ms FROM evidence_failures WHERE cache_key = ? AND failed_at_ms > ?');
99
- this.failureTouchStmt = this.db.prepare('UPDATE evidence_failures SET hits = hits + 1 WHERE cache_key = ?');
100
- this.failureSweepStmt = this.db.prepare('DELETE FROM evidence_failures WHERE failed_at_ms <= ?');
101
- this.failurePutStmt = this.db.prepare('INSERT OR REPLACE INTO evidence_failures (cache_key, model, error, failed_at_ms, hits) VALUES (?, ?, ?, ?, 0)');
102
- }
103
- /**
104
- * SHA-256 key over schema version, model, any request bounds, and the exact
105
- * rendered prompt. With no bound set it is the legacy key, so every existing
106
- * entry still replays; a bound adds one segment, so a bounded arm and an
107
- * unbounded arm never share an entry.
108
- */
109
- static computeKey(model, prompt, bounds = {}) {
110
- const bound = [
111
- ...(bounds.maxTokens !== undefined ? [`max_tokens=${bounds.maxTokens}`] : []),
112
- ...(bounds.reasoningEffort !== undefined ? [`reasoning_effort=${bounds.reasoningEffort}`] : []),
113
- ].join(';');
114
- return node_crypto_1.default.createHash('sha256')
115
- .update(`${exports.EVIDENCE_SCHEMA_VERSION}\0${model}\0${bound ? `bounds:${bound}\0` : ''}${prompt}`)
116
- .digest('hex');
117
- }
118
- /** Lookup. Returns null on miss. Touches hit counters on a hit. */
119
- get(cacheKey) {
120
- const row = this.lookupStmt.get(cacheKey);
121
- if (!row)
122
- return null;
123
- this.touchStmt.run(new Date().toISOString(), cacheKey);
124
- return {
125
- items: JSON.parse(row.items_json),
126
- sessions: JSON.parse(row.sessions_json),
127
- model: row.model,
128
- question: row.question,
129
- promptTokens: row.prompt_tokens,
130
- completionTokens: row.completion_tokens,
131
- createdAt: row.created_at,
132
- };
133
- }
134
- /** Store a freshly extracted, already-parsed entry. Existing keys are never overwritten. */
135
- put(cacheKey, entry) {
136
- this.insertStmt.run(cacheKey, exports.EVIDENCE_SCHEMA_VERSION, entry.model, entry.question, JSON.stringify(entry.sessions), JSON.stringify(entry.items), entry.promptTokens, entry.completionTokens, entry.createdAt, entry.createdAt);
137
- }
138
- /** The failure remembered under `cacheKey` less than EVIDENCE_FAILURE_TTL_MS ago, or null. Touches its hit counter. */
139
- getFailure(cacheKey) {
140
- const row = this.failureLookupStmt.get(cacheKey, Date.now() - exports.EVIDENCE_FAILURE_TTL_MS);
141
- if (!row)
142
- return null;
143
- this.failureTouchStmt.run(cacheKey);
144
- return { model: row.model, error: row.error, failedAt: row.failed_at_ms };
145
- }
146
- /** Remember a failed extraction, replacing any earlier one under the key; expired failures are dropped. */
147
- putFailure(cacheKey, entry) {
148
- this.failureSweepStmt.run(entry.failedAt - exports.EVIDENCE_FAILURE_TTL_MS);
149
- this.failurePutStmt.run(cacheKey, entry.model, entry.error, entry.failedAt);
150
- }
151
- stats() {
152
- const row = this.db.prepare('SELECT COUNT(*) as entries, COALESCE(SUM(hits), 0) as hits, COALESCE(SUM(prompt_tokens), 0) as promptTokens, COALESCE(SUM(completion_tokens), 0) as completionTokens FROM evidence_cache').get();
153
- return row;
154
- }
155
- close() {
156
- this.db.close();
157
- }
158
- }
159
- exports.EvidenceCache = EvidenceCache;
160
- /**
161
- * Drop every cached extraction that read one of `stems` (note paths): a permanent deletion takes the text these rows
162
- * extracted from a deleted memory (memorymap/memoryMap.ts, purgeRetired). Nothing to do when the cache file or its table
163
- * does not exist. The cache is keyed by the exact prompt, so a dropped row is only asked again when a later search reads
164
- * the same files.
165
- */
166
- function dropEvidenceForStems(cachePath, stems) {
167
- if (stems.length === 0 || !node_fs_1.default.existsSync(cachePath))
168
- return;
169
- const db = new sqlite_1.default(cachePath);
170
- try {
171
- db.pragma('busy_timeout = 10000');
172
- if (!db.prepare(`SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'evidence_cache'`).get())
173
- return;
174
- const drop = db.prepare(`DELETE FROM evidence_cache WHERE json_valid(sessions_json) AND EXISTS (
175
- SELECT 1 FROM json_each(sessions_json) s WHERE json_extract(s.value, '$.stem') = ?)`);
176
- db.transaction(() => { for (const stem of stems)
177
- drop.run(stem); })();
178
- }
179
- finally {
180
- db.close();
181
- }
182
- }
@@ -1,37 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EVIDENCE_INTENT = void 0;
4
- exports.resolveEvidenceMode = resolveEvidenceMode;
5
- exports.hasEvidenceIntent = hasEvidenceIntent;
6
- exports.evidenceGate = evidenceGate;
7
- function resolveEvidenceMode(env = process.env) {
8
- const raw = (env.RENOR_EVIDENCE_MODE ?? '').trim().toLowerCase();
9
- if (raw === '')
10
- return 'off';
11
- if (raw === 'off' || raw === 'false' || raw === '0' || raw === 'no')
12
- return 'off';
13
- if (raw === 'always' || raw === 'on' || raw === '1' || raw === 'yes' || raw === 'all')
14
- return 'always';
15
- if (raw === 'auto')
16
- return 'auto';
17
- throw new Error(`Invalid RENOR_EVIDENCE_MODE "${raw}": must be off | auto | always`);
18
- }
19
- /**
20
- * Gold-blind aggregation/enumeration intent. Copied from the eval runner's
21
- * AGGREGATE_INTENT and widened with count/list cues; never fixture-specific.
22
- */
23
- exports.EVIDENCE_INTENT = /\b(how many|how much|in total|total|altogether|combined|all of the|all the|count|number of|list|enumerate|every)\b/i;
24
- function hasEvidenceIntent(query) {
25
- return exports.EVIDENCE_INTENT.test(query);
26
- }
27
- function evidenceGate(mode, query, force = false) {
28
- if (mode === 'off')
29
- return { run: false, reason: 'mode-off' };
30
- if (force)
31
- return { run: true, reason: 'forced' };
32
- if (mode === 'always')
33
- return { run: true, reason: 'mode-always' };
34
- if (hasEvidenceIntent(query))
35
- return { run: true, reason: 'intent' };
36
- return { run: false, reason: 'intent' };
37
- }
@@ -1,85 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OLLAMA_KEY_ENV_NAMES = void 0;
4
- exports.splitKeyList = splitKeyList;
5
- exports.collectOllamaKeys = collectOllamaKeys;
6
- exports.isOllamaCloudEndpoint = isOllamaCloudEndpoint;
7
- exports.ollamaKeysForEndpoint = ollamaKeysForEndpoint;
8
- const ollamaChat_1 = require("./ollamaChat");
9
- /**
10
- * ONE source of truth for the Ollama Cloud key pool.
11
- *
12
- * WHY THIS FILE EXISTS
13
- * Two modules read the same env namespace and they disagreed. The write-path
14
- * extractor (`extraction/extractionClient.ts`) resolved SIX names; the
15
- * evidence second pass (`evidence/options.ts`) resolved THREE, and the three
16
- * it named were the first three of the other list. On 2026-09-30 that drift
17
- * cost a 500-case product run: `OLLAMA_CLOUD_KEY_1..3` had all hit their
18
- * MONTHLY ceiling (429) while `OLLAMA_CLOUD_KEY_4`, `OLLAMA_MAX_KEY` and
19
- * `OLLAMA_API_KEY` were healthy, so the evidence extractor exhausted its pool
20
- * and — because `attachSearchEvidence` is deliberately fail-open — the last 77
21
- * of 500 cases were answered with the evidence layer silently absent. The run
22
- * manifest reported full coverage and zero `evid*` keys: a run that lost its
23
- * evidence layer for 15% of cases was byte-indistinguishable from one where it
24
- * held. See issue #222.
25
- *
26
- * The defect was not a missing key. It was two hand-maintained copies of one
27
- * list. Keys are read by env-var NAME; values are never logged here.
28
- */
29
- exports.OLLAMA_KEY_ENV_NAMES = [
30
- 'OLLAMA_CLOUD_KEY_1',
31
- 'OLLAMA_CLOUD_KEY_2',
32
- 'OLLAMA_CLOUD_KEY_3',
33
- 'OLLAMA_CLOUD_KEY_4',
34
- 'OLLAMA_MAX_KEY',
35
- 'OLLAMA_API_KEY',
36
- ];
37
- /** Comma-separated env value -> trimmed, non-empty keys. */
38
- function splitKeyList(value) {
39
- return (value ?? '').split(',').map(key => key.trim()).filter(Boolean);
40
- }
41
- /**
42
- * Collect every configured Ollama key, in the fixed name order above, together
43
- * with the names that supplied them. `names` is safe to report; `keys` is not.
44
- */
45
- function collectOllamaKeys(env = process.env) {
46
- const keys = [];
47
- const names = [];
48
- for (const name of exports.OLLAMA_KEY_ENV_NAMES) {
49
- const found = splitKeyList(env[name]);
50
- if (found.length > 0) {
51
- keys.push(...found);
52
- names.push(name);
53
- }
54
- }
55
- return { keys, names };
56
- }
57
- /**
58
- * Whether `baseUrl` is Ollama Cloud, the only issuer these keys belong to:
59
- * https on ollama.com or a subdomain of it. Blank means the transport default,
60
- * which is Ollama Cloud. A look-alike host, plain http or an unparseable URL is
61
- * not.
62
- */
63
- function isOllamaCloudEndpoint(baseUrl) {
64
- let url;
65
- try {
66
- url = new URL(baseUrl?.trim() || ollamaChat_1.DEFAULT_CHAT_BASE_URL);
67
- }
68
- catch {
69
- return false;
70
- }
71
- const host = url.hostname.toLowerCase();
72
- return url.protocol === 'https:' && (host === 'ollama.com' || host.endsWith('.ollama.com'));
73
- }
74
- /**
75
- * The Ollama pool for a client that will call `baseUrl`: every key when that
76
- * is Ollama Cloud, none otherwise.
77
- *
78
- * The evidence client used to append this pool to whatever endpoint
79
- * RENOR_EVIDENCE_BASE_URL named, so pointing the extractor at another provider
80
- * sent every leftover OLLAMA_* key there too: a credential leak, and a 401 on
81
- * each search whose turn in the rotation fell on an Ollama key.
82
- */
83
- function ollamaKeysForEndpoint(baseUrl, env = process.env) {
84
- return isOllamaCloudEndpoint(baseUrl) ? collectOllamaKeys(env) : { keys: [], names: [] };
85
- }
@@ -1,84 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DEFAULT_CHAT_BASE_URL = void 0;
4
- exports.createOllamaChat = createOllamaChat;
5
- /** The endpoint called when no base URL is given: Ollama Cloud. */
6
- exports.DEFAULT_CHAT_BASE_URL = 'https://ollama.com/v1';
7
- function createOllamaChat(opts) {
8
- const baseUrl = (opts.baseUrl ?? exports.DEFAULT_CHAT_BASE_URL).replace(/\/+$/, '');
9
- const keys = opts.keys.filter(k => k && k.trim());
10
- if (keys.length === 0)
11
- throw new Error('createOllamaChat requires at least one API key');
12
- const timeoutMs = opts.timeoutMs ?? 180_000;
13
- const maxRetries = opts.maxRetries ?? 3;
14
- const backoffMs = opts.backoffMs ?? 1000;
15
- let counter = 0;
16
- return async (request) => {
17
- const body = JSON.stringify({
18
- model: request.model,
19
- messages: request.messages,
20
- temperature: request.temperature,
21
- // Only when the caller asked for JSON mode. Evidence calls omit it.
22
- ...(request.response_format ? { response_format: request.response_format } : {}),
23
- // Only when the caller set a bound, so an unbounded request is the same bytes: the session router always sets
24
- // a ceiling, the evidence pass only when the operator set one (RENOR_EVIDENCE_MAX_TOKENS /
25
- // RENOR_EVIDENCE_REASONING_EFFORT), the extractors never.
26
- ...(request.max_tokens !== undefined ? { max_tokens: request.max_tokens } : {}),
27
- ...(request.reasoning_effort !== undefined ? { reasoning_effort: request.reasoning_effort } : {}),
28
- });
29
- let lastError;
30
- for (let attempt = 0; attempt <= maxRetries; attempt++) {
31
- opts.beforeAttempt?.();
32
- let retryable = true;
33
- const key = keys[counter++ % keys.length];
34
- const started = Date.now();
35
- const controller = new AbortController();
36
- const timer = setTimeout(() => controller.abort(), timeoutMs);
37
- try {
38
- const resp = await fetch(`${baseUrl}/chat/completions`, {
39
- method: 'POST',
40
- headers: { Authorization: `Bearer ${key}`, 'Content-Type': 'application/json', ...opts.headers },
41
- body,
42
- signal: controller.signal,
43
- });
44
- if (resp.status === 429 || resp.status >= 500) {
45
- lastError = new Error(`extractor provider returned ${resp.status}`);
46
- await resp.text().catch(() => '');
47
- throw lastError;
48
- }
49
- if (!resp.ok) {
50
- // 4xx other than 429 is the provider rejecting this request.
51
- // Retrying it only spends the key pool and the backoff.
52
- retryable = false;
53
- lastError = new Error(`extractor provider returned ${resp.status}: ${(await resp.text().catch(() => '')).slice(0, 200)}`);
54
- throw lastError;
55
- }
56
- const data = await resp.json();
57
- const content = data.choices?.[0]?.message?.content;
58
- const finishReason = data.choices?.[0]?.finish_reason;
59
- if (typeof content !== 'string') {
60
- // `length` here is a reasoning model that spent its whole budget thinking.
61
- throw new Error(`extractor provider response missing choices[0].message.content${typeof finishReason === 'string' ? ` (finish_reason=${finishReason})` : ''}`);
62
- }
63
- return {
64
- content,
65
- promptTokens: data.usage?.prompt_tokens,
66
- completionTokens: data.usage?.completion_tokens,
67
- totalTokens: data.usage?.total_tokens,
68
- latencyMs: Date.now() - started,
69
- ...(typeof finishReason === 'string' ? { finishReason } : {}),
70
- };
71
- }
72
- catch (err) {
73
- lastError = err instanceof Error ? err : new Error(String(err));
74
- if (!retryable || attempt >= maxRetries)
75
- break;
76
- await new Promise(resolve => setTimeout(resolve, 2 ** attempt * backoffMs));
77
- }
78
- finally {
79
- clearTimeout(timer);
80
- }
81
- }
82
- throw lastError ?? new Error('extractor call failed');
83
- };
84
- }
@@ -1,165 +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.EvidenceDailyBudget = exports.EvidenceCapError = exports.EVIDENCE_DAILY_CAP_MAX = exports.EVIDENCE_DAILY_CAP_DEFAULT = exports.EVIDENCE_OPTIN_VALUE = void 0;
7
- exports.resolveEvidenceOptin = resolveEvidenceOptin;
8
- exports.readOptinKey = readOptinKey;
9
- exports.optinEndpointAllowed = optinEndpointAllowed;
10
- exports.utcDay = utcDay;
11
- exports.dailyBudgetGate = dailyBudgetGate;
12
- const node_fs_1 = __importDefault(require("node:fs"));
13
- const client_1 = require("../db/client");
14
- const keyPool_1 = require("./keyPool");
15
- /**
16
- * The evidence extractor as a per-workspace opt-in (owner decision, 13 Oct 2026).
17
- *
18
- * A workspace runs the extractor on Ollama Cloud only when its own env file says so:
19
- *
20
- * RENOR_EVIDENCE_OPTIN=ollama the switch; unset or blank is off, and then nothing here applies
21
- * RENOR_EVIDENCE_KEY_FILE=<path> the fleet key, by its path (a file, so the env file never holds the secret)
22
- * RENOR_EVIDENCE_DAILY_CAP=<n> HTTP requests to the model per UTC day for this workspace (default 50)
23
- *
24
- * With the switch on, the key comes from the key file and nowhere else (no OLLAMA_* name, no
25
- * RENOR_EVIDENCE_API_KEY), the endpoint is Ollama Cloud and the model is gemma4:31b; a base URL naming another
26
- * issuer is refused rather than sent the Ollama key. The operator turns it on and off with
27
- * scripts/alpha/set-tenant-evidence.sh. Values are never logged: only the key file's path and the reason a
28
- * setting was refused.
29
- */
30
- exports.EVIDENCE_OPTIN_VALUE = 'ollama';
31
- /**
32
- * HTTP requests to the model a workspace may make per UTC day (a retry of a 429 or 5xx is one more). Sized for the shared account, not for the user: gemma4:31b
33
- * reads about 9,300 tokens and writes about 190 per call (provider-terms report, 2026-10-03), so 50 calls are
34
- * about 0.47M tokens, about 0.07 USD at list price, and 350 calls a week. Ollama publishes its limits as
35
- * usage, not as calls, and does not state the weekly figure, so this stays well under any plausible share and
36
- * is a setting (RENOR_EVIDENCE_DAILY_CAP, set per workspace by the ops command). A search answered from the
37
- * cache costs none of it.
38
- */
39
- exports.EVIDENCE_DAILY_CAP_DEFAULT = 50;
40
- exports.EVIDENCE_DAILY_CAP_MAX = 10_000;
41
- /** null when the workspace has not opted in. A value that does not parse throws, like an invalid RENOR_EVIDENCE_MODE. */
42
- function resolveEvidenceOptin(env = process.env) {
43
- const raw = (env.RENOR_EVIDENCE_OPTIN ?? '').trim().toLowerCase();
44
- if (raw === '')
45
- return null;
46
- if (raw !== exports.EVIDENCE_OPTIN_VALUE) {
47
- throw new Error(`Invalid RENOR_EVIDENCE_OPTIN "${raw}": must be ${exports.EVIDENCE_OPTIN_VALUE} (or unset)`);
48
- }
49
- const keyFile = (env.RENOR_EVIDENCE_KEY_FILE ?? '').trim();
50
- const capText = (env.RENOR_EVIDENCE_DAILY_CAP ?? '').trim();
51
- let dailyCap = exports.EVIDENCE_DAILY_CAP_DEFAULT;
52
- if (capText !== '') {
53
- const value = Number(capText);
54
- if (!/^[0-9]+$/.test(capText) || !Number.isSafeInteger(value) || value > exports.EVIDENCE_DAILY_CAP_MAX) {
55
- throw new Error(`Invalid RENOR_EVIDENCE_DAILY_CAP "${capText}": must be a whole number from 0 to ${exports.EVIDENCE_DAILY_CAP_MAX}`);
56
- }
57
- dailyCap = value;
58
- }
59
- return { keyFile, dailyCap };
60
- }
61
- /**
62
- * The key in the key file: one line, trimmed. Null (with one stderr line naming the path, never the content) when
63
- * the setting names no file, the file is not a regular file, or it holds nothing usable.
64
- */
65
- function readOptinKey(keyFile) {
66
- if (!keyFile.startsWith('/')) {
67
- process.stderr.write('[evidence] opt-in is on but RENOR_EVIDENCE_KEY_FILE is not an absolute path: the extractor stays off\n');
68
- return null;
69
- }
70
- try {
71
- if (!node_fs_1.default.statSync(keyFile).isFile())
72
- throw new Error('not a regular file');
73
- const key = node_fs_1.default.readFileSync(keyFile, 'utf8').trim();
74
- if (key === '' || /\s/.test(key))
75
- throw new Error('empty or not a single token');
76
- return key;
77
- }
78
- catch (err) {
79
- process.stderr.write(`[evidence] opt-in is on but the key file ${keyFile} cannot be used (${err instanceof Error ? err.message.replace(keyFile, 'it') : 'unreadable'}): the extractor stays off\n`);
80
- return null;
81
- }
82
- }
83
- /** Opt-in sends the Ollama key, so it only ever goes to Ollama Cloud. */
84
- function optinEndpointAllowed(baseUrl) {
85
- if ((0, keyPool_1.isOllamaCloudEndpoint)(baseUrl))
86
- return true;
87
- process.stderr.write('[evidence] opt-in is on but RENOR_EVIDENCE_BASE_URL is not Ollama Cloud: the extractor stays off\n');
88
- return false;
89
- }
90
- /** The workspace used its allowance for today (or its allowance could not be read, which is treated the same). */
91
- class EvidenceCapError extends Error {
92
- constructor(message = 'the daily allowance of evidence-model calls is used') {
93
- super(message);
94
- this.name = 'EvidenceCapError';
95
- }
96
- }
97
- exports.EvidenceCapError = EvidenceCapError;
98
- const DAY_MS = 24 * 60 * 60 * 1000;
99
- const KEEP_DAYS = 14;
100
- function utcDay(now) {
101
- return new Date(now).toISOString().slice(0, 10);
102
- }
103
- /**
104
- * Model calls counted per UTC day in the workspace's own evidence-cache file, so a restart does not hand back
105
- * the day's allowance. `take` reserves one call before each HTTP attempt and is atomic across processes (one write
106
- * transaction), so a concurrent search can never overshoot by more than a call already in flight and refused.
107
- */
108
- class EvidenceDailyBudget {
109
- limit;
110
- now;
111
- db;
112
- takeStmt;
113
- usedStmt;
114
- sweepStmt;
115
- constructor(dbPath, limit, now = Date.now) {
116
- this.limit = limit;
117
- this.now = now;
118
- this.db = (0, client_1.openDb)(dbPath);
119
- this.db.pragma('journal_mode = WAL');
120
- this.db.pragma('busy_timeout = 10000');
121
- this.db.exec('CREATE TABLE IF NOT EXISTS evidence_daily_calls (day TEXT PRIMARY KEY, calls INTEGER NOT NULL)');
122
- this.takeStmt = this.db.prepare(`INSERT INTO evidence_daily_calls (day, calls) VALUES (?, 1)
123
- ON CONFLICT(day) DO UPDATE SET calls = calls + 1 WHERE calls < ?`);
124
- this.usedStmt = this.db.prepare('SELECT calls FROM evidence_daily_calls WHERE day = ?');
125
- this.sweepStmt = this.db.prepare('DELETE FROM evidence_daily_calls WHERE day < ?');
126
- }
127
- /** Reserve one call for today; false when the day's allowance is spent. */
128
- take() {
129
- const now = this.now();
130
- const day = utcDay(now);
131
- if (this.limit <= 0)
132
- return false;
133
- return this.db.transaction(() => {
134
- this.sweepStmt.run(utcDay(now - KEEP_DAYS * DAY_MS));
135
- return this.takeStmt.run(day, this.limit).changes > 0;
136
- }).immediate();
137
- }
138
- used() {
139
- return this.usedStmt.get(utcDay(this.now()))?.calls ?? 0;
140
- }
141
- }
142
- exports.EvidenceDailyBudget = EvidenceDailyBudget;
143
- /**
144
- * A `beforeAttempt` for createOllamaChat that reserves one call per HTTP attempt, retries included, and throws
145
- * EvidenceCapError when none is left. A budget that cannot be opened refuses every attempt: a shared provider
146
- * limit is not worth risking on a counter that is not there.
147
- */
148
- function dailyBudgetGate(open) {
149
- let budget;
150
- return () => {
151
- if (budget === undefined) {
152
- try {
153
- budget = open();
154
- }
155
- catch (err) {
156
- process.stderr.write(`[evidence] the daily call counter cannot be opened (${err instanceof Error ? err.message : String(err)}): the extractor stays off\n`);
157
- budget = null;
158
- }
159
- }
160
- if (!budget)
161
- throw new EvidenceCapError('the daily call counter is unavailable');
162
- if (!budget.take())
163
- throw new EvidenceCapError();
164
- };
165
- }