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,568 @@
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.WorkerPool = exports.KeyPool = exports.EvalCache = exports.JUDGE_MEMO_SCOPE = exports.CACHE_SCHEMA_VERSION = void 0;
7
+ exports.summarizeQaTokenUsage = summarizeQaTokenUsage;
8
+ exports.runWithKeyPoolRetries = runWithKeyPoolRetries;
9
+ exports.parseRetryAfterMs = parseRetryAfterMs;
10
+ exports.resolveWorkerConcurrency = resolveWorkerConcurrency;
11
+ exports.resolveCacheScope = resolveCacheScope;
12
+ exports.resolveEvalCachePath = resolveEvalCachePath;
13
+ exports.resolveJudgeKeys = resolveJudgeKeys;
14
+ const node_fs_1 = __importDefault(require("node:fs"));
15
+ const node_path_1 = __importDefault(require("node:path"));
16
+ const node_crypto_1 = __importDefault(require("node:crypto"));
17
+ const sqlite_1 = __importDefault(require("../db/sqlite"));
18
+ /**
19
+ * Content-addressed eval cache for LongMemEval QA runs.
20
+ *
21
+ * Architecture: full-request hashing → raw-response replay.
22
+ *
23
+ * Key = sha256(CACHE_SCHEMA_VERSION + providerNamespace + JSON.stringify(requestBody)).
24
+ * No .trim(), no selected-field hashing. Any prompt edit, temperature change,
25
+ * or model swap automatically invalidates the cache because the full request
26
+ * body changes.
27
+ *
28
+ * Stores raw model responses (not parsed verdicts). Parsing happens at read
29
+ * time so future parser improvements don't require re-running models.
30
+ *
31
+ * The cache is "frozen replay": it stores the exact bytes a model returned for
32
+ * a given request. It does NOT assume LLM calls are deterministic — even at
33
+ * temperature 0, provider drift can produce different outputs for identical
34
+ * prompts. The cache replays the first-seen draw. Authoritative promotion
35
+ * runs should use --no-cache for a cold full-500.
36
+ *
37
+ * Design follows the existing embedding cache (src/embeddings/caching.ts):
38
+ * SQLite + WAL + busy_timeout=10000 + INSERT OR IGNORE.
39
+ */
40
+ /**
41
+ * Bump this on any change to key computation or stored schema.
42
+ *
43
+ * v2 (2026-09-18): invalidated all v1 entries after the degraded shared-session
44
+ * incident.
45
+ *
46
+ * v3 (2026-09-19): adds an explicit cache scope to key computation. The QA
47
+ * runner passes the effective x-opencode-session as that scope, so a response
48
+ * produced by one routed backend session can never be replayed into another.
49
+ *
50
+ * v4 (2026-09-22 / integrity fix 2026-09-23): documents the campaign finding
51
+ * that cross-run replay needs a pinned cache scope + shared --cache-dir (and
52
+ * shared evidence cache for evidence arms) — NOT body rewriting. The
53
+ * path-variance diagnostic (scripts/eval/diagnose-evalcache-path-variance.ts)
54
+ * measured runner-assembly answer/judge bodies as ALREADY byte-identical across
55
+ * different per-case temp dirs (notePaths relativized; evidence tables emit
56
+ * item/action/amount/date only). A brief experiment that rewrote
57
+ * /tmp/renor-product-XXXXXX tokens inside the hashed JSON was rejected before
58
+ * merge: model-visible path-shaped strings must remain part of the key, or two
59
+ * different contexts can collide and replay the wrong answer.
60
+ *
61
+ * Key formula is unchanged from v3 aside from the version string: sha256 of
62
+ * CACHE_SCHEMA_VERSION \0 providerNamespace \0 cacheScope \0
63
+ * JSON.stringify(requestBody) with NO body mutation. Keeping the v4 label
64
+ * intentionally invalidates v3 rows: a brief pre-merge canonicalize experiment
65
+ * may have written experimental v4 keys on developer machines, and a clean
66
+ * version cut avoids mixed semantics (cheaper than subtle collisions between
67
+ * v3 and any experimental v4 rows). Scoring stays fail-closed on the schema
68
+ * recorded in the run manifest.
69
+ */
70
+ exports.CACHE_SCHEMA_VERSION = 'evalcache-v4';
71
+ /** Routing-free scope of the judge memo (see EvalCache.computeJudgeMemoKey). Bump with its key formula. */
72
+ exports.JUDGE_MEMO_SCOPE = 'judge-memo-v1';
73
+ function summarizeQaTokenUsage(records) {
74
+ return {
75
+ live: {
76
+ answerPrompt: records.reduce((n, c) => n + (!c.answer?.fromCache ? (c.answer?.usage?.promptTokens ?? 0) : 0), 0),
77
+ answerCompletion: records.reduce((n, c) => n + (!c.answer?.fromCache ? (c.answer?.usage?.completionTokens ?? 0) : 0), 0),
78
+ judgePrompt: records.reduce((n, c) => n + (!c.judge?.fromCache ? (c.judge?.usage?.promptTokens ?? 0) : 0), 0),
79
+ judgeCompletion: records.reduce((n, c) => n + (!c.judge?.fromCache ? (c.judge?.usage?.completionTokens ?? 0) : 0), 0),
80
+ },
81
+ avoided: {
82
+ answerPrompt: records.reduce((n, c) => n + (c.answer?.fromCache ? (c.answer?.usage?.promptTokens ?? 0) : 0), 0),
83
+ answerCompletion: records.reduce((n, c) => n + (c.answer?.fromCache ? (c.answer?.usage?.completionTokens ?? 0) : 0), 0),
84
+ judgePrompt: records.reduce((n, c) => n + (c.judge?.fromCache ? (c.judge?.usage?.promptTokens ?? 0) : 0), 0),
85
+ judgeCompletion: records.reduce((n, c) => n + (c.judge?.fromCache ? (c.judge?.usage?.completionTokens ?? 0) : 0), 0),
86
+ },
87
+ };
88
+ }
89
+ class EvalCache {
90
+ db;
91
+ stats = {
92
+ answerHits: 0, answerMisses: 0,
93
+ judgeHits: 0, judgeMisses: 0,
94
+ avoidedAnswerPromptTokens: 0, avoidedAnswerCompletionTokens: 0,
95
+ avoidedJudgePromptTokens: 0, avoidedJudgeCompletionTokens: 0,
96
+ liveAnswerPromptTokens: 0, liveAnswerCompletionTokens: 0,
97
+ liveJudgePromptTokens: 0, liveJudgeCompletionTokens: 0,
98
+ cacheLookupTotalMs: 0, cacheLookupCount: 0,
99
+ judgeMemoHits: 0, judgeMemoMisses: 0,
100
+ };
101
+ lookupStmt;
102
+ insertStmt;
103
+ constructor(dbPath) {
104
+ node_fs_1.default.mkdirSync(node_path_1.default.dirname(dbPath), { recursive: true });
105
+ this.db = new sqlite_1.default(dbPath);
106
+ this.db.pragma('journal_mode = WAL');
107
+ this.db.pragma('busy_timeout = 10000');
108
+ this.db.exec(`
109
+ CREATE TABLE IF NOT EXISTS eval_responses (
110
+ cache_key TEXT PRIMARY KEY,
111
+ cache_schema_version TEXT NOT NULL,
112
+ provider_namespace TEXT NOT NULL,
113
+ call_type TEXT NOT NULL,
114
+ question_id TEXT NOT NULL,
115
+ raw_content TEXT NOT NULL,
116
+ model_echo TEXT NOT NULL,
117
+ finish_reason TEXT NOT NULL,
118
+ prompt_tokens INTEGER NOT NULL DEFAULT 0,
119
+ completion_tokens INTEGER NOT NULL DEFAULT 0,
120
+ latency_ms INTEGER NOT NULL DEFAULT 0,
121
+ request_body_json TEXT NOT NULL,
122
+ created_at TEXT NOT NULL
123
+ )
124
+ `);
125
+ this.db.exec(`CREATE INDEX IF NOT EXISTS idx_eval_responses_qid ON eval_responses(question_id)`);
126
+ this.db.exec(`CREATE INDEX IF NOT EXISTS idx_eval_responses_type ON eval_responses(call_type)`);
127
+ this.lookupStmt = this.db.prepare('SELECT raw_content, model_echo, finish_reason, prompt_tokens, completion_tokens, latency_ms, provider_namespace FROM eval_responses WHERE cache_key = ?');
128
+ this.insertStmt = this.db.prepare(`INSERT OR IGNORE INTO eval_responses
129
+ (cache_key, cache_schema_version, provider_namespace, call_type, question_id,
130
+ raw_content, model_echo, finish_reason, prompt_tokens, completion_tokens,
131
+ latency_ms, request_body_json, created_at)
132
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
133
+ }
134
+ /**
135
+ * Compute a content-addressed cache key from the full API request plus an
136
+ * optional routing scope.
137
+ *
138
+ * Key = sha256(CACHE_SCHEMA_VERSION \0 providerNamespace \0 cacheScope
139
+ * \0 requestBodyJSON).
140
+ *
141
+ * The request body is hashed as-is (no path rewriting). Cross-run replay
142
+ * relies on pinned RENOR_QA_CACHE_SCOPE + shared --cache-dir, not on
143
+ * mutating model-visible text before the key (v4 integrity fix).
144
+ *
145
+ * The QA runner uses the effective x-opencode-session as cacheScope because
146
+ * that header has been empirically shown to affect backend routing/results.
147
+ * Callers that have no routing-sensitive context may leave the scope empty.
148
+ */
149
+ static computeKey(providerNamespace, requestBody, cacheScope = '') {
150
+ // Hash the raw request body. Never rewrite model-visible text — path-shaped
151
+ // tokens in context must produce distinct keys (integrity).
152
+ return node_crypto_1.default.createHash('sha256')
153
+ .update(`${exports.CACHE_SCHEMA_VERSION}\0${providerNamespace}\0${cacheScope}\0${JSON.stringify(requestBody)}`)
154
+ .digest('hex');
155
+ }
156
+ /**
157
+ * Key of the judge memo: one judge verdict per (question id, judge endpoint and model, and the whole judge
158
+ * request: system prompt, the question, the gold answer, the model answer, temperature and token cap).
159
+ * No routing scope: a verdict is a function of what the judge was asked, so a later run with a new routing
160
+ * session asks nothing new. The READER's answer is never keyed this way: it keeps the scope, so independent
161
+ * draws stay independent (only an identical answer text earns an identical verdict).
162
+ */
163
+ static computeJudgeMemoKey(providerNamespace, questionId, requestBody) {
164
+ return node_crypto_1.default.createHash('sha256')
165
+ .update(`${exports.CACHE_SCHEMA_VERSION}\0${providerNamespace}\0${exports.JUDGE_MEMO_SCOPE}\0${questionId}\0${JSON.stringify(requestBody)}`)
166
+ .digest('hex');
167
+ }
168
+ /** Look up a memoized judge verdict. A hit counts as a judge hit (and its avoided tokens); a miss is only counted as a memo miss. */
169
+ getJudgeMemo(memoKey) {
170
+ const row = this.lookupStmt.get(memoKey);
171
+ if (!row) {
172
+ this.stats.judgeMemoMisses++;
173
+ return null;
174
+ }
175
+ this.stats.judgeMemoHits++;
176
+ this.stats.judgeHits++;
177
+ this.stats.avoidedJudgePromptTokens += row.prompt_tokens;
178
+ this.stats.avoidedJudgeCompletionTokens += row.completion_tokens;
179
+ return {
180
+ rawContent: row.raw_content,
181
+ modelEcho: row.model_echo,
182
+ finishReason: row.finish_reason,
183
+ promptTokens: row.prompt_tokens,
184
+ completionTokens: row.completion_tokens,
185
+ latencyMs: row.latency_ms,
186
+ providerNamespace: row.provider_namespace,
187
+ };
188
+ }
189
+ /**
190
+ * Record a live judge verdict in the memo. Callers pass only verdicts that parsed: a response the parser
191
+ * refuses is retried by the next run, not replayed. Not a live call: put() already counted it.
192
+ */
193
+ putJudgeMemo(entry) {
194
+ return this.insertEntry(entry) > 0;
195
+ }
196
+ /**
197
+ * Look up a cached response. Returns null on miss.
198
+ */
199
+ get(cacheKey, callType) {
200
+ const started = Date.now();
201
+ const row = this.lookupStmt.get(cacheKey);
202
+ this.stats.cacheLookupTotalMs += Date.now() - started;
203
+ this.stats.cacheLookupCount++;
204
+ if (!row) {
205
+ if (callType === 'answer')
206
+ this.stats.answerMisses++;
207
+ else
208
+ this.stats.judgeMisses++;
209
+ return null;
210
+ }
211
+ if (callType === 'answer') {
212
+ this.stats.answerHits++;
213
+ this.stats.avoidedAnswerPromptTokens += row.prompt_tokens;
214
+ this.stats.avoidedAnswerCompletionTokens += row.completion_tokens;
215
+ }
216
+ else {
217
+ this.stats.judgeHits++;
218
+ this.stats.avoidedJudgePromptTokens += row.prompt_tokens;
219
+ this.stats.avoidedJudgeCompletionTokens += row.completion_tokens;
220
+ }
221
+ return {
222
+ rawContent: row.raw_content,
223
+ modelEcho: row.model_echo,
224
+ finishReason: row.finish_reason,
225
+ promptTokens: row.prompt_tokens,
226
+ completionTokens: row.completion_tokens,
227
+ latencyMs: row.latency_ms,
228
+ providerNamespace: row.provider_namespace,
229
+ };
230
+ }
231
+ insertEntry(params) {
232
+ const result = this.insertStmt.run(params.cacheKey, exports.CACHE_SCHEMA_VERSION, params.providerNamespace, params.callType, params.questionId, params.rawContent, params.modelEcho, params.finishReason, params.promptTokens, params.completionTokens, params.latencyMs, JSON.stringify(params.requestBody), new Date().toISOString());
233
+ return result.changes;
234
+ }
235
+ /**
236
+ * Store a live response in the cache. INSERT OR IGNORE preserves first-seen semantics.
237
+ */
238
+ put(params) {
239
+ this.insertEntry(params);
240
+ // Track live usage
241
+ if (params.callType === 'answer') {
242
+ this.stats.liveAnswerPromptTokens += params.promptTokens;
243
+ this.stats.liveAnswerCompletionTokens += params.completionTokens;
244
+ }
245
+ else {
246
+ this.stats.liveJudgePromptTokens += params.promptTokens;
247
+ this.stats.liveJudgeCompletionTokens += params.completionTokens;
248
+ }
249
+ }
250
+ /**
251
+ * Promote an already-observed response into another cache identity without
252
+ * counting it as a second live provider call. Returns true only when a new
253
+ * cache row was inserted.
254
+ */
255
+ promote(params) {
256
+ return this.insertEntry(params) > 0;
257
+ }
258
+ /**
259
+ * Invalidate recorded model calls. Filters combine (AND); an empty filter is
260
+ * refused unless `all` is explicit. Returns the number of rows removed.
261
+ */
262
+ invalidate(filter) {
263
+ const clauses = [];
264
+ const args = [];
265
+ if (filter.providerNamespace) {
266
+ clauses.push('provider_namespace = ?');
267
+ args.push(filter.providerNamespace);
268
+ }
269
+ if (filter.callType) {
270
+ clauses.push('call_type = ?');
271
+ args.push(filter.callType);
272
+ }
273
+ if (clauses.length === 0 && !filter.all)
274
+ throw new Error('EvalCache.invalidate: pass providerNamespace, callType, or all: true');
275
+ const where = clauses.length > 0 ? ` WHERE ${clauses.join(' AND ')}` : '';
276
+ return this.db.prepare(`DELETE FROM eval_responses${where}`).run(...args).changes;
277
+ }
278
+ /** Cached row count per provider namespace and call type. */
279
+ summary() {
280
+ return (this.db.prepare('SELECT provider_namespace AS providerNamespace, call_type AS callType, COUNT(*) AS rows FROM eval_responses GROUP BY provider_namespace, call_type ORDER BY provider_namespace, call_type').all());
281
+ }
282
+ /** Row count for diagnostics. */
283
+ rowCount() {
284
+ const row = this.db.prepare('SELECT COUNT(*) as cnt FROM eval_responses').get();
285
+ return row.cnt;
286
+ }
287
+ close() {
288
+ this.db.close();
289
+ }
290
+ }
291
+ exports.EvalCache = EvalCache;
292
+ class KeyPool {
293
+ slots;
294
+ maxPerKey;
295
+ nextIndex = 0;
296
+ waiters = [];
297
+ constructor(apiKeys, maxPerKey = 1) {
298
+ if (apiKeys.length === 0)
299
+ throw new Error('KeyPool requires at least one API key');
300
+ this.slots = apiKeys.map((key, i) => ({
301
+ key,
302
+ inFlight: 0,
303
+ disabled: false,
304
+ cooldownUntil: 0,
305
+ label: `key-${i + 1}`,
306
+ }));
307
+ this.maxPerKey = maxPerKey;
308
+ }
309
+ get size() { return this.slots.filter(s => !s.disabled).length; }
310
+ get totalInFlight() { return this.slots.reduce((n, s) => n + s.inFlight, 0); }
311
+ findAvailable(now) {
312
+ for (let i = 0; i < this.slots.length; i++) {
313
+ const idx = (this.nextIndex + i) % this.slots.length;
314
+ const slot = this.slots[idx];
315
+ if (!slot.disabled && slot.inFlight < this.maxPerKey && now >= slot.cooldownUntil) {
316
+ this.nextIndex = (idx + 1) % this.slots.length;
317
+ return slot;
318
+ }
319
+ }
320
+ return undefined;
321
+ }
322
+ lease(slot) {
323
+ slot.inFlight++;
324
+ let released = false;
325
+ return {
326
+ key: slot.key,
327
+ label: slot.label,
328
+ release: () => {
329
+ if (released)
330
+ return;
331
+ released = true;
332
+ slot.inFlight = Math.max(0, slot.inFlight - 1);
333
+ this.drainWaiters();
334
+ },
335
+ };
336
+ }
337
+ /**
338
+ * Acquire a key slot. Returns { key, label, release }.
339
+ * release() is idempotent and should still be called in a finally block.
340
+ */
341
+ async acquire() {
342
+ if (this.size === 0) {
343
+ throw new Error('All API keys have been permanently disabled (401/403)');
344
+ }
345
+ const slot = this.findAvailable(Date.now());
346
+ if (slot)
347
+ return this.lease(slot);
348
+ // All usable keys are busy or cooling down. Queue and immediately drain so
349
+ // cooldown-only waits get a wake-up timer even when no in-flight request
350
+ // remains to call release().
351
+ return new Promise((resolve, reject) => {
352
+ this.waiters.push({ resolve, reject });
353
+ this.drainWaiters();
354
+ });
355
+ }
356
+ rejectAllWaiters(error) {
357
+ while (this.waiters.length > 0) {
358
+ this.waiters.shift()?.reject(error);
359
+ }
360
+ }
361
+ drainWaiters() {
362
+ if (this.waiters.length === 0)
363
+ return;
364
+ if (this.size === 0) {
365
+ this.rejectAllWaiters(new Error('All API keys have been permanently disabled (401/403)'));
366
+ return;
367
+ }
368
+ const now = Date.now();
369
+ const slot = this.findAvailable(now);
370
+ if (slot) {
371
+ const waiter = this.waiters.shift();
372
+ if (waiter)
373
+ waiter.resolve(this.lease(slot));
374
+ if (this.waiters.length > 0)
375
+ queueMicrotask(() => this.drainWaiters());
376
+ return;
377
+ }
378
+ // All usable keys are still busy or in cooldown. Busy keys will wake us on
379
+ // release; cooldown-only waits need a timer for the nearest expiry.
380
+ const nearestCooldown = this.slots
381
+ .filter(s => !s.disabled && s.inFlight < this.maxPerKey && s.cooldownUntil > now)
382
+ .reduce((min, s) => Math.min(min, s.cooldownUntil), Infinity);
383
+ if (nearestCooldown < Infinity) {
384
+ setTimeout(() => this.drainWaiters(), Math.max(0, nearestCooldown - now) + 10);
385
+ }
386
+ }
387
+ /**
388
+ * Report an HTTP error for a key. Handles cooldown and permanent disable.
389
+ * Call this BEFORE release() if the error warrants special handling.
390
+ */
391
+ reportError(label, statusCode, retryAfterMs) {
392
+ const slot = this.slots.find(s => s.label === label);
393
+ if (!slot)
394
+ return;
395
+ if (statusCode === 401 || statusCode === 403) {
396
+ slot.disabled = true;
397
+ process.stderr.write(`[KeyPool] ${label} permanently disabled (HTTP ${statusCode})\n`);
398
+ if (this.slots.every(s => s.disabled)) {
399
+ const error = new Error('All API keys have been permanently disabled (401/403)');
400
+ this.rejectAllWaiters(error);
401
+ throw error;
402
+ }
403
+ }
404
+ else if (statusCode === 429) {
405
+ const cooldown = retryAfterMs !== undefined && Number.isFinite(retryAfterMs) && retryAfterMs >= 0
406
+ ? retryAfterMs
407
+ : 60_000;
408
+ slot.cooldownUntil = Date.now() + cooldown;
409
+ process.stderr.write(`[KeyPool] ${label} rate-limited, cooldown ${cooldown}ms\n`);
410
+ }
411
+ this.drainWaiters();
412
+ }
413
+ }
414
+ exports.KeyPool = KeyPool;
415
+ /**
416
+ * Run a pooled operation with a fresh key acquisition on every attempt.
417
+ * This is intentionally outside chatCall's internal retries so a 429/401 on
418
+ * one credential cannot pin all retries to the same bad key.
419
+ */
420
+ async function runWithKeyPoolRetries(pool, maxAttempts, fn, backoffMs = attempt => Math.min(1000 * 2 ** attempt, 8000)) {
421
+ if (!Number.isInteger(maxAttempts) || maxAttempts < 1) {
422
+ throw new Error('maxAttempts must be an integer >= 1');
423
+ }
424
+ let lastError;
425
+ for (let attempt = 1; attempt <= maxAttempts; attempt++) {
426
+ const lease = await pool.acquire();
427
+ try {
428
+ const value = await fn({ key: lease.key, label: lease.label }, attempt);
429
+ return { value, attempts: attempt };
430
+ }
431
+ catch (error) {
432
+ lastError = error;
433
+ }
434
+ finally {
435
+ lease.release();
436
+ }
437
+ if (attempt < maxAttempts) {
438
+ const delay = backoffMs(attempt);
439
+ if (delay > 0)
440
+ await new Promise(resolve => setTimeout(resolve, delay));
441
+ }
442
+ }
443
+ throw lastError instanceof Error
444
+ ? lastError
445
+ : new Error(`Pooled API failed after ${maxAttempts} attempts: ${String(lastError ?? 'unknown')}`);
446
+ }
447
+ /** Parse Retry-After as either delta-seconds or an HTTP date. */
448
+ function parseRetryAfterMs(value, nowMs = Date.now()) {
449
+ if (!value)
450
+ return undefined;
451
+ const seconds = Number(value);
452
+ if (Number.isFinite(seconds) && seconds >= 0)
453
+ return seconds * 1000;
454
+ const at = Date.parse(value);
455
+ if (!Number.isFinite(at))
456
+ return undefined;
457
+ return Math.max(0, at - nowMs);
458
+ }
459
+ /** Resolve runner concurrency, defaulting to usable judge-key capacity (capped at 3). */
460
+ function resolveWorkerConcurrency(configured, activeJudgeKeys, defaultCap = 3) {
461
+ if (!Number.isInteger(activeJudgeKeys) || activeJudgeKeys < 1) {
462
+ throw new Error('activeJudgeKeys must be an integer >= 1');
463
+ }
464
+ if (configured === undefined || configured.trim() === '') {
465
+ return Math.min(defaultCap, activeJudgeKeys);
466
+ }
467
+ const parsed = Number(configured);
468
+ if (!Number.isInteger(parsed) || parsed < 1) {
469
+ throw new Error(`--concurrency must be an integer >= 1 (got ${configured})`);
470
+ }
471
+ return parsed;
472
+ }
473
+ // ─── Bounded Worker Pool ───────────────────────────────────────────────────────
474
+ /**
475
+ * Simple bounded worker pool. Runs up to `concurrency` tasks at once.
476
+ * Unlike p-limit, this is zero-dependency and supports our needs.
477
+ */
478
+ class WorkerPool {
479
+ concurrency;
480
+ running = 0;
481
+ queue = [];
482
+ constructor(concurrency) {
483
+ if (concurrency < 1)
484
+ throw new Error('WorkerPool concurrency must be >= 1');
485
+ this.concurrency = concurrency;
486
+ }
487
+ /**
488
+ * Run a task when a slot is available.
489
+ * The returned promise resolves/rejects with the task's result.
490
+ */
491
+ async run(fn) {
492
+ if (this.running >= this.concurrency) {
493
+ await new Promise(resolve => this.queue.push(resolve));
494
+ }
495
+ this.running++;
496
+ try {
497
+ return await fn();
498
+ }
499
+ finally {
500
+ this.running--;
501
+ const next = this.queue.shift();
502
+ if (next)
503
+ next();
504
+ }
505
+ }
506
+ }
507
+ exports.WorkerPool = WorkerPool;
508
+ // ─── Cache Path Resolution ─────────────────────────────────────────────────────
509
+ /**
510
+ * Resolve the cache scope that becomes part of the eval-cache key.
511
+ *
512
+ * Default: the effective routing session (`x-opencode-session:<session>`). The
513
+ * session header affects backend routing, so it is part of cache identity and a
514
+ * degraded routed session cannot poison later healthy runs (2026-09-18 incident).
515
+ *
516
+ * Benchmark runs that deliberately want cross-run reuse pin a canonical scope
517
+ * via RENOR_QA_CACHE_SCOPE. The canonical registry is maintained by the
518
+ * benchmark-infrastructure work (PR #67); the routing session stays unique for
519
+ * health even when cache identity is shared.
520
+ */
521
+ function resolveCacheScope(args) {
522
+ const explicit = (args.explicit ?? '').trim();
523
+ return explicit ? `scope:${explicit}` : `x-opencode-session:${args.session}`;
524
+ }
525
+ /**
526
+ * Resolve the eval cache path from environment.
527
+ * RENOR_EVAL_CACHE=off → null (disabled).
528
+ * RENOR_EVAL_CACHE=on or unset → default path.
529
+ * RENOR_EVAL_CACHE=/path → explicit path.
530
+ */
531
+ function resolveEvalCachePath(env = process.env) {
532
+ const raw = env.RENOR_EVAL_CACHE;
533
+ if (raw === 'off' || raw === '0')
534
+ return null;
535
+ if (!raw || raw === 'on') {
536
+ return node_path_1.default.join(env.HOME ?? '.', '.local', 'share', 'renor', 'eval-cache.db');
537
+ }
538
+ return node_path_1.default.resolve(raw);
539
+ }
540
+ /**
541
+ * Parse judge API keys from environment.
542
+ * Supports JUDGE_API_KEY_1, JUDGE_API_KEY_2, ... or falls back to single key.
543
+ */
544
+ function resolveJudgeKeys(env, fallbackKeyEnv, prefixEnv) {
545
+ const keys = [];
546
+ const prefixes = [prefixEnv, fallbackKeyEnv, 'JUDGE_API_KEY', 'OLLAMA_MAX_KEY', 'OLLAMA_API_KEY'].filter(Boolean);
547
+ const seenPrefixes = new Set();
548
+ for (const prefix of prefixes) {
549
+ if (seenPrefixes.has(prefix))
550
+ continue;
551
+ seenPrefixes.add(prefix);
552
+ for (let i = 1; i <= 10; i++) {
553
+ const val = env[`${prefix}_${i}`];
554
+ if (val && val.trim())
555
+ keys.push(val.trim());
556
+ }
557
+ if (keys.length > 0)
558
+ return [...new Set(keys)];
559
+ }
560
+ // Check if prefixEnv or fallbackKeyEnv holds a comma-separated list or single key
561
+ const single = (prefixEnv && env[prefixEnv]) || env[fallbackKeyEnv];
562
+ if (single && single.trim()) {
563
+ const split = single.split(',').map(s => s.trim()).filter(Boolean);
564
+ if (split.length > 0)
565
+ return split;
566
+ }
567
+ return [];
568
+ }
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EvalHashEmbeddingProvider = void 0;
4
+ /**
5
+ * Deterministic lexical hash embeddings for eval infrastructure smoke tests.
6
+ *
7
+ * This provider intentionally has no semantic-quality claim. It exists so CI can
8
+ * execute indexing/vector-search/scoring paths without network/model downloads.
9
+ * Any manifest using it must be marked qualityEligible=false.
10
+ */
11
+ class EvalHashEmbeddingProvider {
12
+ dimensions = 256;
13
+ identity = { kind: 'eval-hash', model: 'lexical-hash-v1' };
14
+ vector(text) {
15
+ const out = new Array(this.dimensions).fill(0);
16
+ const words = text.toLowerCase().match(/[\p{L}\p{N}]+/gu) ?? [];
17
+ for (const word of words) {
18
+ let hash = 2166136261;
19
+ for (let i = 0; i < word.length; i++) {
20
+ hash ^= word.charCodeAt(i);
21
+ hash = Math.imul(hash, 16777619);
22
+ }
23
+ out[(hash >>> 0) % this.dimensions] += 1;
24
+ }
25
+ const norm = Math.sqrt(out.reduce((sum, value) => sum + value * value, 0)) || 1;
26
+ return out.map(value => value / norm);
27
+ }
28
+ async embed(texts) {
29
+ return texts.map(text => this.vector(text));
30
+ }
31
+ async embedQuery(text) {
32
+ return this.vector(text);
33
+ }
34
+ }
35
+ exports.EvalHashEmbeddingProvider = EvalHashEmbeddingProvider;