renor-engine 1.1.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 (193) hide show
  1. package/README.md +27 -2
  2. package/THIRD-PARTY-NOTICES +2661 -0
  3. package/dist/BUILD.json +3 -3
  4. package/dist/cli.js +1656 -490
  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 -548
  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/preferenceExtractor.js +0 -150
  75. package/dist/extraction/secretGuard.js +0 -176
  76. package/dist/extraction/secretScanner.js +0 -665
  77. package/dist/extraction/typedFacts.js +0 -224
  78. package/dist/extraction/writePathAdjudication.js +0 -110
  79. package/dist/importing/apply.js +0 -672
  80. package/dist/importing/bundle-write.js +0 -80
  81. package/dist/importing/cli.js +0 -408
  82. package/dist/importing/existing.js +0 -51
  83. package/dist/importing/facts.js +0 -121
  84. package/dist/importing/forgetImported.js +0 -79
  85. package/dist/importing/hosted.js +0 -467
  86. package/dist/importing/journalLog.js +0 -96
  87. package/dist/importing/markdown.js +0 -98
  88. package/dist/importing/plan.js +0 -201
  89. package/dist/importing/secretScreen.js +0 -113
  90. package/dist/importing/serialize.js +0 -188
  91. package/dist/importing/source.js +0 -463
  92. package/dist/importing/types.js +0 -6
  93. package/dist/indexer/chunker.js +0 -319
  94. package/dist/indexer/dailyEntries.js +0 -63
  95. package/dist/indexer/hasher.js +0 -10
  96. package/dist/indexer/laneRuns.js +0 -48
  97. package/dist/indexer/o200k.js +0 -481
  98. package/dist/indexer/outsideRoot.js +0 -77
  99. package/dist/indexer/parser.js +0 -170
  100. package/dist/indexer/pipeline.js +0 -988
  101. package/dist/indexer/staleness.js +0 -201
  102. package/dist/indexer/tokenizer.js +0 -77
  103. package/dist/indexer/walker.js +0 -170
  104. package/dist/mcp/hive.js +0 -88
  105. package/dist/mcp/requestErrors.js +0 -47
  106. package/dist/mcp/server.js +0 -426
  107. package/dist/memory/agentLease.js +0 -43
  108. package/dist/memory/confinedWrite.js +0 -315
  109. package/dist/memory/controls.js +0 -73
  110. package/dist/memory/dailyNote.js +0 -265
  111. package/dist/memory/durableAppend.js +0 -57
  112. package/dist/memory/factConflicts.js +0 -203
  113. package/dist/memory/factTerms.js +0 -321
  114. package/dist/memory/handoff.js +0 -363
  115. package/dist/memory/hiveNameHeader.cjs +0 -10
  116. package/dist/memory/inventory.js +0 -55
  117. package/dist/memory/invisibleText.js +0 -20
  118. package/dist/memory/lifecycle.js +0 -1991
  119. package/dist/memory/limits.js +0 -13
  120. package/dist/memory/requests.js +0 -183
  121. package/dist/memory/sourceName.js +0 -276
  122. package/dist/memory/statusFacts.js +0 -61
  123. package/dist/memory/whatsNew.js +0 -158
  124. package/dist/memory/writer.js +0 -175
  125. package/dist/memory/writerConflicts.js +0 -85
  126. package/dist/memorymap/memoryMap.js +0 -1594
  127. package/dist/memorymap/replacedJournal.js +0 -58
  128. package/dist/memorymap/replacements.js +0 -410
  129. package/dist/mvp/activity.js +0 -86
  130. package/dist/mvp/agentMemory.js +0 -1800
  131. package/dist/mvp/answerCheck.js +0 -211
  132. package/dist/mvp/auth.js +0 -531
  133. package/dist/mvp/cli.js +0 -75
  134. package/dist/mvp/client-store.js +0 -462
  135. package/dist/mvp/compileCache.js +0 -242
  136. package/dist/mvp/compileSeed.js +0 -33
  137. package/dist/mvp/config.js +0 -288
  138. package/dist/mvp/connectionStatus.js +0 -80
  139. package/dist/mvp/contextDistinct.js +0 -53
  140. package/dist/mvp/contextProfile.js +0 -87
  141. package/dist/mvp/contextRelevance.js +0 -413
  142. package/dist/mvp/directorySurface.js +0 -68
  143. package/dist/mvp/engine.js +0 -1078
  144. package/dist/mvp/exportAll.js +0 -374
  145. package/dist/mvp/factLanePack.js +0 -77
  146. package/dist/mvp/factMarkers.js +0 -65
  147. package/dist/mvp/factsLane.js +0 -203
  148. package/dist/mvp/hiddenSpans.js +0 -585
  149. package/dist/mvp/history.js +0 -414
  150. package/dist/mvp/hitDates.js +0 -31
  151. package/dist/mvp/hitFloor.js +0 -36
  152. package/dist/mvp/identity.js +0 -18
  153. package/dist/mvp/idle.js +0 -113
  154. package/dist/mvp/log.js +0 -118
  155. package/dist/mvp/memoryDir.js +0 -98
  156. package/dist/mvp/posthogMcp.js +0 -210
  157. package/dist/mvp/readScope.js +0 -104
  158. package/dist/mvp/readUsefulness.js +0 -222
  159. package/dist/mvp/sanitize.js +0 -48
  160. package/dist/mvp/schemaRejections.js +0 -91
  161. package/dist/mvp/scope.js +0 -127
  162. package/dist/mvp/server.js +0 -2604
  163. package/dist/mvp/serverCard.js +0 -38
  164. package/dist/mvp/storedSize.js +0 -139
  165. package/dist/mvp/telemetry.js +0 -474
  166. package/dist/net/poolFetch.js +0 -174
  167. package/dist/recall/actionRecall.js +0 -282
  168. package/dist/recall/autoRecall.js +0 -447
  169. package/dist/recall/searchRecall.js +0 -189
  170. package/dist/retrieval/adviceLines.js +0 -195
  171. package/dist/retrieval/adviceQuestion.js +0 -44
  172. package/dist/retrieval/conversationShape.js +0 -44
  173. package/dist/retrieval/dailyEntryTime.js +0 -55
  174. package/dist/retrieval/entityLookup.js +0 -162
  175. package/dist/retrieval/factsFirst.js +0 -188
  176. package/dist/retrieval/keyword.js +0 -84
  177. package/dist/retrieval/ledgerEventTime.js +0 -51
  178. package/dist/retrieval/ledgerLiveness.js +0 -83
  179. package/dist/retrieval/ledgerPresentation.js +0 -161
  180. package/dist/retrieval/noteIdentity.js +0 -57
  181. package/dist/retrieval/orchestrator.js +0 -1171
  182. package/dist/retrieval/plainWording.js +0 -236
  183. package/dist/retrieval/queryHelpers.js +0 -142
  184. package/dist/retrieval/questionSpan.js +0 -475
  185. package/dist/retrieval/readerEvents.js +0 -690
  186. package/dist/retrieval/readerLayout.js +0 -422
  187. package/dist/retrieval/relativeTimeHints.js +0 -113
  188. package/dist/retrieval/semantic.js +0 -67
  189. package/dist/router/routerCache.js +0 -106
  190. package/dist/router/sessionRouter.js +0 -726
  191. package/dist/toolRefusal.js +0 -120
  192. package/dist/watcher/watcher.js +0 -77
  193. /package/dist/{indexer/o200k.json → o200k.json} +0 -0
@@ -1,334 +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.VertexEmbeddingProvider = void 0;
7
- const node_fs_1 = __importDefault(require("node:fs"));
8
- const node_crypto_1 = __importDefault(require("node:crypto"));
9
- const callPhases_1 = require("../callPhases");
10
- const types_1 = require("./types");
11
- /**
12
- * How a token is kept fresh. Google's lives an hour. A call does not use one with a twelfth of its life left (5 of the 60
13
- * minutes, the margin it always kept), and the token is renewed in the background when a sixth is left (10 minutes), so the
14
- * calls keep finding one in hand and none of them waits for the exchange. Shares, not minutes, so a token of any lifetime is
15
- * treated alike.
16
- */
17
- const REQUEST_MARGIN_SHARE = 1 / 12;
18
- const RENEW_AHEAD_SHARE = 1 / 6;
19
- /**
20
- * A token that lives less than this is not renewed in the background: the calls exchange for one when they need it. Google's
21
- * lives an hour; a service that hands out tokens of seconds would otherwise be asked for one every few seconds for good.
22
- */
23
- const MIN_RENEWED_LIFETIME_MS = 60_000;
24
- /** A renewal that failed is tried again after this, for as long as the token in hand can still be used. */
25
- const RENEW_RETRY_MS = 60_000;
26
- /** The exchange is one small request, and every call that needs a token waits for it: a stalled one must not hold them all. */
27
- const TOKEN_EXCHANGE_TIMEOUT_MS = 30_000;
28
- /** The longest wait setTimeout takes (2^31 - 1 ms); a longer one fires at once. */
29
- const MAX_TIMER_MS = 2 ** 31 - 1;
30
- /** The process's own, looked up at each use, so a global clock someone else has replaced is the one it reads. */
31
- const processClock = {
32
- now: () => Date.now(),
33
- setTimeout: (callback, ms) => setTimeout(callback, ms),
34
- clearTimeout: timer => clearTimeout(timer),
35
- };
36
- /** Quota (429) and transient (500/503) answers are retried: 8 attempts, waiting 10 s, 20 s ... up to 120 s between them. */
37
- const DEFAULT_MAX_ATTEMPTS = 8;
38
- const defaultRetryWaitMs = (attempt) => Math.min(120_000, 5_000 * 2 ** attempt) + Math.random() * 2_000;
39
- /** The endpoint is built from the location; a test points it at a local stub instead. */
40
- function endpointBase(baseUrl, location) {
41
- if (baseUrl === undefined)
42
- return `https://${location}-aiplatform.googleapis.com`;
43
- const url = new URL(baseUrl);
44
- const loopback = url.hostname === '127.0.0.1' || url.hostname === 'localhost' || url.hostname === '[::1]';
45
- if (url.protocol !== 'https:' && !(url.protocol === 'http:' && loopback))
46
- throw new Error('Vertex baseUrl must be https, or http on loopback');
47
- return url.origin;
48
- }
49
- /** Wait `ms`, or until `signal` aborts, which ends the call that is waiting. */
50
- function sleepUnlessAborted(ms, signal) {
51
- return new Promise((resolve, reject) => {
52
- if (signal?.aborted)
53
- return reject(signal.reason);
54
- const onAbort = () => {
55
- clearTimeout(timer);
56
- reject(signal.reason);
57
- };
58
- const timer = setTimeout(() => {
59
- signal?.removeEventListener('abort', onAbort);
60
- resolve();
61
- }, ms);
62
- signal?.addEventListener('abort', onAbort, { once: true });
63
- });
64
- }
65
- function b64url(input) {
66
- return Buffer.from(input).toString('base64url');
67
- }
68
- /** `work`, or the signal's reason as soon as it aborts: the caller stops waiting and `work` goes on for the others. */
69
- function untilAborted(work, signal) {
70
- return new Promise((resolve, reject) => {
71
- const onAbort = () => reject(signal.reason);
72
- signal.addEventListener('abort', onAbort, { once: true });
73
- work.then(resolve, reject).finally(() => signal.removeEventListener('abort', onAbort));
74
- });
75
- }
76
- class VertexEmbeddingProvider {
77
- dimensions;
78
- kind = 'vertex';
79
- model;
80
- identity;
81
- project;
82
- location;
83
- key;
84
- maxInstancesPerRequest;
85
- maxInputTokens;
86
- base;
87
- maxAttempts;
88
- retryWaitMs;
89
- /** Where its requests go: the global `fetch`, looked up at each call, unless the caller gave it another (see `request`). */
90
- request;
91
- tokenClock;
92
- accessToken = null;
93
- /** Epoch ms until which a call may use the token in hand (REQUEST_MARGIN_SHARE), and when it is renewed in the background. */
94
- tokenUsableUntil = 0;
95
- tokenRenewAt = 0;
96
- /** The exchange in flight: a caller that needs a token while one is being fetched waits for it instead of making another. */
97
- exchange = null;
98
- renewTimer;
99
- usage;
100
- constructor(options) {
101
- const keyFile = options?.keyFile
102
- ?? process.env.RENOR_EVAL_VERTEX_KEY_FILE
103
- ?? process.env.GOOGLE_APPLICATION_CREDENTIALS
104
- ?? '';
105
- if (!keyFile || !node_fs_1.default.existsSync(keyFile)) {
106
- throw new Error('Vertex provider requires a service-account key file (GOOGLE_APPLICATION_CREDENTIALS or RENOR_EVAL_VERTEX_KEY_FILE)');
107
- }
108
- const raw = JSON.parse(node_fs_1.default.readFileSync(keyFile, 'utf8'));
109
- if (!raw.client_email || !raw.private_key || !raw.token_uri) {
110
- throw new Error('Service-account key file must contain client_email, private_key, and token_uri');
111
- }
112
- this.key = raw;
113
- this.project = options?.project ?? process.env.RENOR_EVAL_VERTEX_PROJECT ?? '';
114
- if (!/^[a-z0-9-]+$/.test(this.project))
115
- throw new Error('RENOR_EVAL_VERTEX_PROJECT is required');
116
- this.location = options?.location ?? process.env.RENOR_EVAL_VERTEX_LOCATION ?? '';
117
- if (!this.location)
118
- throw new Error('RENOR_EVAL_VERTEX_LOCATION is required');
119
- if (!/^[a-z0-9-]+$/.test(this.location))
120
- throw new Error('Invalid Vertex location');
121
- this.model = options?.model ?? process.env.RENOR_EVAL_MODEL ?? 'gemini-embedding-001';
122
- this.identity = { kind: this.kind, model: this.model };
123
- const dimensions = options?.dimensions ?? Number(process.env.RENOR_EVAL_EMBEDDING_DIMS ?? 768);
124
- if (!Number.isInteger(dimensions) || ![768, 1536, 3072].includes(dimensions)) {
125
- throw new Error('gemini-embedding-001 dimensions must be 768, 1536, or 3072');
126
- }
127
- this.dimensions = dimensions;
128
- this.maxInstancesPerRequest = options?.maxInstancesPerRequest ?? 32;
129
- const maxInputTokens = options?.maxInputTokens
130
- ?? Number(process.env.RENOR_EVAL_MAX_EMBED_TOKENS ?? 80_000_000);
131
- if (!Number.isInteger(maxInputTokens) || maxInputTokens < 1_000) {
132
- throw new Error('RENOR_EVAL_MAX_EMBED_TOKENS must be a positive integer >= 1000');
133
- }
134
- this.maxInputTokens = maxInputTokens;
135
- this.base = endpointBase(options?.baseUrl, this.location);
136
- this.maxAttempts = options?.retry?.maxAttempts ?? DEFAULT_MAX_ATTEMPTS;
137
- this.retryWaitMs = options?.retry?.waitMs ?? defaultRetryWaitMs;
138
- this.request = options?.request ?? ((input, init) => globalThis.fetch(input, init));
139
- this.tokenClock = options?.tokenClock ?? processClock;
140
- this.usage = { requests: 0, inputTokens: 0, maxInputTokens };
141
- }
142
- /**
143
- * The token a call uses: the one in hand while it is usable, otherwise the exchange (one at a time, shared by every caller
144
- * waiting for it). A caller that gives up (its signal aborts) stops waiting; the exchange goes on and the next call finds
145
- * its token. The background renewal (armRenewal) keeps this from happening on a call at all while the provider is in use.
146
- */
147
- getAccessToken(signal) {
148
- if (this.accessToken && this.tokenClock.now() < this.tokenUsableUntil)
149
- return Promise.resolve(this.accessToken);
150
- if (signal?.aborted)
151
- return Promise.reject(signal.reason); // as before: a call already stopped starts no exchange
152
- return signal ? untilAborted(this.fetchToken(), signal) : this.fetchToken();
153
- }
154
- fetchToken() {
155
- this.exchange ??= this.exchangeToken().finally(() => { this.exchange = null; });
156
- return this.exchange;
157
- }
158
- /**
159
- * The token is renewed at RENEW_AHEAD_SHARE of its life left, whether or not a call is waiting for it, and the renewal
160
- * arms the next one. A timer that cannot wait that long (setTimeout's limit) wakes early and waits again. The timer is
161
- * unref'd: it never keeps a process alive. A provider that is no longer used goes on renewing while its process lives,
162
- * one small request about every 50 minutes.
163
- */
164
- armRenewal(at, lifetimeMs) {
165
- this.tokenClock.clearTimeout(this.renewTimer);
166
- if (!Number.isFinite(at) || !(lifetimeMs >= MIN_RENEWED_LIFETIME_MS))
167
- return;
168
- this.tokenRenewAt = at;
169
- this.waitToRenew(at - this.tokenClock.now());
170
- }
171
- waitToRenew(ms) {
172
- this.renewTimer = this.tokenClock.setTimeout(() => void this.renew(), Math.min(Math.max(0, ms), MAX_TIMER_MS));
173
- this.renewTimer.unref();
174
- }
175
- async renew() {
176
- const early = this.tokenRenewAt - this.tokenClock.now();
177
- if (early > 0)
178
- return this.waitToRenew(early);
179
- try {
180
- await this.fetchToken();
181
- }
182
- catch {
183
- // A call exchanges for a token when it must, and reports why if it cannot. Meanwhile, try again while the token in
184
- // hand can still be used, so a service that was away for a moment is not found out by a call.
185
- if (this.tokenClock.now() < this.tokenUsableUntil)
186
- this.waitToRenew(RENEW_RETRY_MS);
187
- }
188
- }
189
- /** OAuth2 JWT-bearer grant: SA key -> short-lived access token. */
190
- async exchangeToken() {
191
- const now = Math.floor(Date.now() / 1000); // the assertion is dated by the real clock, which is what Google reads
192
- const header = b64url(JSON.stringify({ alg: 'RS256', typ: 'JWT' }));
193
- const claims = b64url(JSON.stringify({
194
- iss: this.key.client_email,
195
- scope: 'https://www.googleapis.com/auth/cloud-platform',
196
- aud: this.key.token_uri,
197
- iat: now,
198
- exp: now + 3600,
199
- }));
200
- const signature = b64url(node_crypto_1.default.createSign('RSA-SHA256')
201
- .update(`${header}.${claims}`)
202
- .sign(this.key.private_key));
203
- const response = await this.request(this.key.token_uri, {
204
- method: 'POST',
205
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
206
- body: new URLSearchParams({
207
- grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
208
- assertion: `${header}.${claims}.${signature}`,
209
- }).toString(),
210
- // Not a caller's signal: the exchange is shared, and one caller giving up must not end it for the others.
211
- signal: AbortSignal.timeout(TOKEN_EXCHANGE_TIMEOUT_MS),
212
- });
213
- if (!response.ok)
214
- throw new Error(`Vertex token exchange failed: ${response.status}`);
215
- const data = await response.json();
216
- const lifetimeMs = data.expires_in * 1000;
217
- const issuedAt = this.tokenClock.now();
218
- this.accessToken = data.access_token;
219
- this.tokenUsableUntil = issuedAt + lifetimeMs * (1 - REQUEST_MARGIN_SHARE);
220
- this.armRenewal(issuedAt + lifetimeMs * (1 - RENEW_AHEAD_SHARE), lifetimeMs);
221
- return this.accessToken;
222
- }
223
- /**
224
- * `call` says how long this one may take (see EmbedOptions): `deadline` is the epoch ms by which it gives up, and a
225
- * retry whose wait would end past it is not waited for, so a refusing service is reported at once rather than at the
226
- * deadline. Without one the retries run their whole course.
227
- */
228
- async predict(contents, call) {
229
- const waitedForToken = performance.now();
230
- const token = await this.getAccessToken(call.signal);
231
- (0, callPhases_1.recordPhase)('token', performance.now() - waitedForToken);
232
- const url = `${this.base}/v1/projects/${this.project}/locations/${this.location}/publishers/google/models/${this.model}:predict`;
233
- const body = JSON.stringify({
234
- instances: contents.map(({ text, taskType }) => ({
235
- content: text,
236
- task_type: taskType,
237
- })),
238
- parameters: { outputDimensionality: this.dimensions, autoTruncate: true },
239
- });
240
- // Quota (429) and transient (500/503) errors are expected on new projects:
241
- // back off with jitter instead of failing the run. Retries cost nothing —
242
- // 429 responses are not billed.
243
- const maxAttempts = this.maxAttempts;
244
- let attempt = 0;
245
- let response;
246
- // The request itself, as the log's `call` phase has it: from sending to the last byte read, less the time spent opening a
247
- // connection for it (which the pool reports as `connect`).
248
- let sentAt = 0;
249
- let connectedBefore = 0;
250
- const spent = () => performance.now() - sentAt - ((0, callPhases_1.recordedPhase)('connect') - connectedBefore);
251
- for (;;) {
252
- sentAt = performance.now();
253
- connectedBefore = (0, callPhases_1.recordedPhase)('connect');
254
- response = await this.request(url, {
255
- method: 'POST',
256
- headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
257
- body,
258
- signal: call.signal,
259
- });
260
- if (response.ok || ![429, 500, 503].includes(response.status))
261
- break;
262
- (0, callPhases_1.recordPhase)('call', spent());
263
- attempt += 1;
264
- if (attempt >= maxAttempts) {
265
- throw new types_1.EmbeddingUnavailableError(`Vertex predict failed after ${attempt} attempts (HTTP ${response.status})`);
266
- }
267
- const waitMs = this.retryWaitMs(attempt);
268
- if (Date.now() + waitMs > call.deadline) {
269
- throw new types_1.EmbeddingUnavailableError(`Vertex predict refused (HTTP ${response.status}); the retry is later than this call may wait`);
270
- }
271
- process.stderr.write(`[vertex] HTTP ${response.status} on ${contents.length} inputs; retry ${attempt}/${maxAttempts - 1} in ${Math.round(waitMs / 1000)}s\n`);
272
- await sleepUnlessAborted(waitMs, call.signal);
273
- }
274
- if (!response.ok) {
275
- const bodyText = await response.text();
276
- (0, callPhases_1.recordPhase)('call', spent());
277
- // The status travels with the error (the message is unchanged): a 400 names an input the service refuses.
278
- throw Object.assign(new Error(`Vertex predict failed (${response.status}): ${bodyText.slice(0, 400)}`), { status: response.status });
279
- }
280
- const data = await response.json();
281
- (0, callPhases_1.recordPhase)('call', spent());
282
- const predictions = data.predictions ?? [];
283
- if (predictions.length !== contents.length
284
- || predictions.some(p => !Array.isArray(p.embeddings?.values) || p.embeddings.values.length !== this.dimensions)) {
285
- throw new Error('Vertex predict returned malformed or incomplete embeddings');
286
- }
287
- this.usage.requests += 1;
288
- for (const p of predictions)
289
- this.usage.inputTokens += p.embeddings?.statistics?.token_count ?? 0;
290
- if (this.usage.inputTokens > this.maxInputTokens) {
291
- throw new Error(`Embedding cost guard tripped: ${this.usage.inputTokens} input tokens exceeds the ${this.maxInputTokens} cap (set RENOR_EVAL_MAX_EMBED_TOKENS to raise it)`);
292
- }
293
- return predictions.map(p => p.embeddings.values);
294
- }
295
- async embedAll(texts, taskType, options) {
296
- if (texts.length === 0)
297
- return [];
298
- const call = {
299
- deadline: options?.budgetMs === undefined ? Infinity : Date.now() + options.budgetMs,
300
- signal: options?.signal,
301
- };
302
- const out = [];
303
- const started = performance.now();
304
- try {
305
- for (let i = 0; i < texts.length; i += this.maxInstancesPerRequest) {
306
- const batch = texts.slice(i, i + this.maxInstancesPerRequest).map(text => ({ text, taskType }));
307
- out.push(...await this.predict(batch, call));
308
- }
309
- }
310
- finally {
311
- (0, callPhases_1.recordPhase)('embed', performance.now() - started);
312
- }
313
- return out;
314
- }
315
- async embed(texts, options) {
316
- return this.embedAll(texts, 'RETRIEVAL_DOCUMENT', options);
317
- }
318
- async embedQuery(text, options) {
319
- const [vector] = await this.embedAll([text], 'RETRIEVAL_QUERY', options);
320
- return vector;
321
- }
322
- describe() {
323
- // Credentials and project IDs never leave the process through logs.
324
- return {
325
- kind: 'vertex',
326
- model: this.model,
327
- dimensions: this.dimensions,
328
- location: this.location,
329
- taskTypes: { document: 'RETRIEVAL_DOCUMENT', query: 'RETRIEVAL_QUERY' },
330
- usage: { ...this.usage },
331
- };
332
- }
333
- }
334
- exports.VertexEmbeddingProvider = VertexEmbeddingProvider;
@@ -1,42 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.sha256 = sha256;
7
- exports.claimDirectory = claimDirectory;
8
- exports.assertRegularTree = assertRegularTree;
9
- exports.safeEndpoint = safeEndpoint;
10
- const node_fs_1 = __importDefault(require("node:fs"));
11
- const node_path_1 = __importDefault(require("node:path"));
12
- const node_crypto_1 = __importDefault(require("node:crypto"));
13
- /**
14
- * Measurement-side helpers shared by the eval runners. Evaluator provenance
15
- * (source hash, git state) lives in src/eval/runManifest.ts — the single
16
- * authority — so only artifact-safety utilities live here.
17
- */
18
- function sha256(data) {
19
- return node_crypto_1.default.createHash('sha256').update(data).digest('hex');
20
- }
21
- /** Exclusive claim: never overwrite another worker's run, even after failure. */
22
- function claimDirectory(directory) {
23
- node_fs_1.default.mkdirSync(node_path_1.default.dirname(directory), { recursive: true });
24
- node_fs_1.default.mkdirSync(directory, { mode: 0o700 });
25
- }
26
- /** Fixtures must consist only of regular files/directories. No external links. */
27
- function assertRegularTree(root) {
28
- const stat = node_fs_1.default.lstatSync(root);
29
- if (stat.isSymbolicLink() || (!stat.isDirectory() && !stat.isFile())) {
30
- throw new Error(`Non-regular fixture entry: ${root}`);
31
- }
32
- if (stat.isDirectory())
33
- for (const child of node_fs_1.default.readdirSync(root))
34
- assertRegularTree(node_path_1.default.join(root, child));
35
- }
36
- function safeEndpoint(value) {
37
- const url = new URL(value);
38
- if (!['https:', 'http:'].includes(url.protocol) || url.username || url.password || url.search || url.hash) {
39
- throw new Error('Embedding base URL must be HTTP(S) without credentials, query, or fragment');
40
- }
41
- return value;
42
- }
@@ -1,146 +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.PER_RUN_EVIDENCE_BLOCKER = exports.UNCACHED_MODEL_BLOCKER = exports.UNCACHED_EMBEDDING_BLOCKER = exports.UNPINNED_SCOPE_BLOCKER = void 0;
7
- exports.cacheHitRate = cacheHitRate;
8
- exports.buildCacheReport = buildCacheReport;
9
- exports.formatCacheReport = formatCacheReport;
10
- exports.emitCacheReport = emitCacheReport;
11
- exports.readProxyCounters = readProxyCounters;
12
- exports.proxyWindow = proxyWindow;
13
- const node_fs_1 = __importDefault(require("node:fs"));
14
- const node_path_1 = __importDefault(require("node:path"));
15
- function cacheHitRate(counts) {
16
- const lookups = counts.hits + counts.misses;
17
- return lookups === 0 ? null : counts.hits / lookups;
18
- }
19
- const rate = (counts) => ({ hits: counts.hits, misses: counts.misses, hitRate: cacheHitRate(counts) });
20
- exports.UNPINNED_SCOPE_BLOCKER = "answer/judge keys are scoped to this process's routing session; pin RENOR_QA_CACHE_SCOPE (and share --cache-dir) to replay across runs";
21
- exports.UNCACHED_EMBEDDING_BLOCKER = 'embedding calls ran with the cache off (RENOR_EVAL_EMBED_CACHE=off): turn it on to replay them';
22
- exports.UNCACHED_MODEL_BLOCKER = 'answer/judge calls ran with the eval cache off (--no-cache or RENOR_EVAL_CACHE=off): turn it on to replay them';
23
- exports.PER_RUN_EVIDENCE_BLOCKER = "the evidence cache lives in this run's output directory; set RENOR_EVIDENCE_CACHE to a shared path to replay across runs";
24
- function buildCacheReport(input) {
25
- const report = { liveProviderCalls: 0, healthProbeCalls: input.healthProbes ?? 0, repeatRunBlockers: [] };
26
- const uncached = {};
27
- if (input.embedding) {
28
- const { sent } = input.embedding;
29
- const calls = sent ? sent.calls : input.embedding.misses;
30
- report.liveProviderCalls += calls;
31
- if (sent)
32
- report.embeddingSent = { texts: sent.texts, calls: sent.calls };
33
- if (input.embedding.cached === false) {
34
- if (calls > 0) {
35
- uncached.embedding = calls;
36
- report.repeatRunBlockers.push(exports.UNCACHED_EMBEDDING_BLOCKER);
37
- }
38
- }
39
- else {
40
- report.embedding = rate(input.embedding);
41
- if (!sent)
42
- report.embeddingCallsUpperBound = true;
43
- }
44
- }
45
- if (input.evidence) {
46
- report.evidence = rate(input.evidence);
47
- report.liveProviderCalls += input.evidence.misses;
48
- if (!input.evidenceCacheShared && input.evidence.hits + input.evidence.misses > 0)
49
- report.repeatRunBlockers.push(exports.PER_RUN_EVIDENCE_BLOCKER);
50
- }
51
- if (input.evalCache) {
52
- const { answerHits, answerMisses, judgeHits, judgeMisses } = input.evalCache;
53
- report.answer = rate({ hits: answerHits, misses: answerMisses });
54
- report.judge = rate({ hits: judgeHits, misses: judgeMisses });
55
- if ((input.evalCache.judgeMemoHits ?? 0) + (input.evalCache.judgeMemoMisses ?? 0) > 0) {
56
- report.judgeMemo = rate({ hits: input.evalCache.judgeMemoHits ?? 0, misses: input.evalCache.judgeMemoMisses ?? 0 });
57
- }
58
- if (!input.pinnedScope && answerHits + answerMisses + judgeHits + judgeMisses > 0)
59
- report.repeatRunBlockers.push(exports.UNPINNED_SCOPE_BLOCKER);
60
- }
61
- if (input.indexSnapshots)
62
- report.indexSnapshots = input.indexSnapshots;
63
- const modelCalls = input.liveModelCalls ?? (input.evalCache ? input.evalCache.answerMisses + input.evalCache.judgeMisses : 0);
64
- report.liveProviderCalls += modelCalls;
65
- if (!input.evalCache && modelCalls > 0) {
66
- uncached.model = modelCalls;
67
- report.repeatRunBlockers.push(exports.UNCACHED_MODEL_BLOCKER);
68
- }
69
- if (Object.keys(uncached).length > 0)
70
- report.uncached = uncached;
71
- return report;
72
- }
73
- const part = (label, r) => r ? `${label} ${r.hits}/${r.hits + r.misses} (${r.hitRate === null ? 'n/a' : `${(r.hitRate * 100).toFixed(1)}%`})` : null;
74
- function formatCacheReport(report) {
75
- const count = (n, noun) => `${n} ${noun}${n === 1 ? '' : 's'}`;
76
- const sent = report.embeddingSent
77
- ? `, ${count(report.embeddingSent.texts, 'text')} in ${count(report.embeddingSent.calls, 'call')}`
78
- : report.embeddingCallsUpperBound ? ', calls counted as lookup misses (upper bound)' : '';
79
- const layers = [
80
- report.embedding ? `${part('embedding', report.embedding)}${sent}` : null,
81
- report.uncached?.embedding ? `embedding uncached${sent}` : null,
82
- part('evidence', report.evidence),
83
- part('answer', report.answer),
84
- part('judge', report.judge),
85
- part('judge memo', report.judgeMemo),
86
- report.indexSnapshots ? `index snapshots restored ${report.indexSnapshots.restored}/${report.indexSnapshots.restored + report.indexSnapshots.built + report.indexSnapshots.coldBusy} (${(report.indexSnapshots.savedIndexMs / 1000).toFixed(0)} s of index time not spent)` : null,
87
- report.uncached?.model ? `answer/judge uncached ${report.uncached.model}` : null,
88
- ].filter(Boolean);
89
- const probes = report.healthProbeCalls > 0 ? ` (+${report.healthProbeCalls} health probes, never cached)` : '';
90
- const lines = [`[cache] ${layers.length > 0 ? layers.join(' | ') : 'no caches in use'} | live provider calls ${report.liveProviderCalls}${probes}`];
91
- for (const blocker of report.repeatRunBlockers)
92
- lines.push(`[cache] a repeat run will miss: ${blocker}`);
93
- return lines.join('\n');
94
- }
95
- /** Print the report to stderr and, given an output dir, keep it beside the run as cache-report.json. */
96
- function emitCacheReport(report, outputDir) {
97
- process.stderr.write(`${formatCacheReport(report)}\n`);
98
- if (outputDir)
99
- node_fs_1.default.writeFileSync(node_path_1.default.join(outputDir, 'cache-report.json'), JSON.stringify(report, null, 2) + '\n', 'utf8');
100
- }
101
- /** Counters from an embed-proxy stats file, or null when it is absent or unreadable. */
102
- function readProxyCounters(statsPath) {
103
- if (!statsPath || !node_fs_1.default.existsSync(statsPath))
104
- return null;
105
- try {
106
- const raw = JSON.parse(node_fs_1.default.readFileSync(statsPath, 'utf8'));
107
- if (typeof raw.cacheHits !== 'number' || typeof raw.cacheMisses !== 'number')
108
- return null;
109
- const counters = { startedAt: raw.startedAt, cacheHits: raw.cacheHits, cacheMisses: raw.cacheMisses };
110
- if (typeof raw.upstreamTexts === 'number' && typeof raw.upstreamCalls === 'number') {
111
- counters.upstreamTexts = raw.upstreamTexts;
112
- counters.upstreamCalls = raw.upstreamCalls;
113
- }
114
- return counters;
115
- }
116
- catch {
117
- return null;
118
- }
119
- }
120
- /**
121
- * The embed-proxy is a long-lived service shared by every run (and shard), and
122
- * its stats file holds lifetime counters, so one run's embedding lookups (and what
123
- * the proxy sent upstream for them) are the change in those counters between its
124
- * start and its end. That window also counts any other client of the same proxy.
125
- * Unknown (undefined) when the proxy restarted in between, because its counters
126
- * were reset.
127
- */
128
- function proxyWindow(before, after) {
129
- if (!after)
130
- return undefined;
131
- if (before && before.startedAt !== after.startedAt)
132
- return undefined;
133
- const window = {
134
- hits: after.cacheHits - (before?.cacheHits ?? 0),
135
- misses: after.cacheMisses - (before?.cacheMisses ?? 0),
136
- };
137
- // The sent figures are known only when the proxy counted them on both ends of the window; otherwise unknown, never zero.
138
- if (after.upstreamTexts !== undefined && after.upstreamCalls !== undefined) {
139
- if (!before)
140
- window.sent = { texts: after.upstreamTexts, calls: after.upstreamCalls };
141
- else if (before.upstreamTexts !== undefined && before.upstreamCalls !== undefined) {
142
- window.sent = { texts: after.upstreamTexts - before.upstreamTexts, calls: after.upstreamCalls - before.upstreamCalls };
143
- }
144
- }
145
- return window;
146
- }