renor-engine 1.2.0 → 1.4.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 (216) hide show
  1. package/README.md +34 -2
  2. package/THIRD-PARTY-NOTICES +2661 -0
  3. package/bin/renor-engine.cjs +38 -0
  4. package/dist/BUILD.json +6 -0
  5. package/dist/cli.js +1674 -516
  6. package/package.json +17 -80
  7. package/dist/.tsbuildinfo +0 -1
  8. package/dist/api/apiAuth.js +0 -43
  9. package/dist/api/server.js +0 -174
  10. package/dist/buildIdentity.js +0 -149
  11. package/dist/callPhases.js +0 -82
  12. package/dist/cli-extractors.js +0 -46
  13. package/dist/config.js +0 -65
  14. package/dist/context/composer.js +0 -320
  15. package/dist/context/readHonesty.js +0 -478
  16. package/dist/context/rowStore.js +0 -238
  17. package/dist/conversation/formatter.js +0 -81
  18. package/dist/conversation/parser.js +0 -193
  19. package/dist/conversation/types.js +0 -2
  20. package/dist/conversation/writer.js +0 -42
  21. package/dist/db/client.js +0 -149
  22. package/dist/db/handoffTable.js +0 -31
  23. package/dist/db/indexSalvage.js +0 -101
  24. package/dist/db/migrations.js +0 -670
  25. package/dist/db/readCache.js +0 -109
  26. package/dist/db/schema.js +0 -5
  27. package/dist/db/sqlite.js +0 -396
  28. package/dist/db/statements.js +0 -89
  29. package/dist/digests/digestCache.js +0 -146
  30. package/dist/digests/digests.js +0 -597
  31. package/dist/digests/worker.js +0 -350
  32. package/dist/embeddings/caching.js +0 -337
  33. package/dist/embeddings/coalescing.js +0 -177
  34. package/dist/embeddings/interactive.js +0 -103
  35. package/dist/embeddings/local.js +0 -70
  36. package/dist/embeddings/openai.js +0 -106
  37. package/dist/embeddings/rebuild.js +0 -166
  38. package/dist/embeddings/simdScan.js +0 -260
  39. package/dist/embeddings/space.js +0 -21
  40. package/dist/embeddings/types.js +0 -14
  41. package/dist/embeddings/vectorIndex.js +0 -674
  42. package/dist/embeddings/vertex.js +0 -334
  43. package/dist/eval/artifacts.js +0 -42
  44. package/dist/eval/cacheReport.js +0 -146
  45. package/dist/eval/comparison.js +0 -182
  46. package/dist/eval/context700-candidate.js +0 -145
  47. package/dist/eval/evalCache.js +0 -568
  48. package/dist/eval/hashEmbedding.js +0 -35
  49. package/dist/eval/indexSnapshots.js +0 -549
  50. package/dist/eval/longmemeval.js +0 -56
  51. package/dist/eval/metrics.js +0 -181
  52. package/dist/eval/pageDump.js +0 -67
  53. package/dist/eval/renorbenchBaseline.js +0 -486
  54. package/dist/eval/renorbenchDocuments.js +0 -134
  55. package/dist/eval/renorbenchProduct.js +0 -182
  56. package/dist/eval/runLedger.js +0 -279
  57. package/dist/eval/runManifest.js +0 -485
  58. package/dist/eval/scoring.js +0 -197
  59. package/dist/eval/textTokens.js +0 -17
  60. package/dist/evidence/attach.js +0 -264
  61. package/dist/evidence/background.js +0 -147
  62. package/dist/evidence/evidenceCache.js +0 -182
  63. package/dist/evidence/gate.js +0 -37
  64. package/dist/evidence/keyPool.js +0 -85
  65. package/dist/evidence/ollamaChat.js +0 -84
  66. package/dist/evidence/optin.js +0 -165
  67. package/dist/evidence/options.js +0 -151
  68. package/dist/evidence/secondPass.js +0 -294
  69. package/dist/extraction/classifyCapture.js +0 -34
  70. package/dist/extraction/decision-model.js +0 -174
  71. package/dist/extraction/eventExtractor.js +0 -113
  72. package/dist/extraction/extractionClient.js +0 -122
  73. package/dist/extraction/factExtractor.js +0 -46
  74. package/dist/extraction/factsWritePath.js +0 -179
  75. package/dist/extraction/jevDecisionModel.js +0 -307
  76. package/dist/extraction/preferenceExtractor.js +0 -150
  77. package/dist/extraction/secretGuard.js +0 -180
  78. package/dist/extraction/secretScanner.js +0 -665
  79. package/dist/extraction/typedFacts.js +0 -224
  80. package/dist/extraction/writePathAdjudication.js +0 -110
  81. package/dist/importing/apply.js +0 -672
  82. package/dist/importing/bundle-write.js +0 -80
  83. package/dist/importing/cli.js +0 -408
  84. package/dist/importing/existing.js +0 -51
  85. package/dist/importing/facts.js +0 -121
  86. package/dist/importing/forgetImported.js +0 -79
  87. package/dist/importing/hosted.js +0 -467
  88. package/dist/importing/journalLog.js +0 -96
  89. package/dist/importing/markdown.js +0 -98
  90. package/dist/importing/plan.js +0 -201
  91. package/dist/importing/secretScreen.js +0 -113
  92. package/dist/importing/serialize.js +0 -188
  93. package/dist/importing/source.js +0 -463
  94. package/dist/importing/types.js +0 -6
  95. package/dist/indexer/chunker.js +0 -319
  96. package/dist/indexer/dailyEntries.js +0 -63
  97. package/dist/indexer/hasher.js +0 -10
  98. package/dist/indexer/laneRuns.js +0 -48
  99. package/dist/indexer/o200k.js +0 -481
  100. package/dist/indexer/outsideRoot.js +0 -77
  101. package/dist/indexer/parser.js +0 -170
  102. package/dist/indexer/pipeline.js +0 -988
  103. package/dist/indexer/staleness.js +0 -201
  104. package/dist/indexer/tokenizer.js +0 -77
  105. package/dist/indexer/walker.js +0 -170
  106. package/dist/learning/cli.js +0 -113
  107. package/dist/learning/schema.js +0 -144
  108. package/dist/learning/settings.js +0 -45
  109. package/dist/learning/store.js +0 -267
  110. package/dist/learning/tap.js +0 -401
  111. package/dist/mcp/hive.js +0 -88
  112. package/dist/mcp/requestErrors.js +0 -47
  113. package/dist/mcp/server.js +0 -426
  114. package/dist/memory/agentLease.js +0 -43
  115. package/dist/memory/confinedWrite.js +0 -315
  116. package/dist/memory/controls.js +0 -73
  117. package/dist/memory/dailyNote.js +0 -265
  118. package/dist/memory/dreamer/adjudicate.js +0 -215
  119. package/dist/memory/dreamer/apply.js +0 -120
  120. package/dist/memory/dreamer/budget.js +0 -64
  121. package/dist/memory/dreamer/candidates.js +0 -149
  122. package/dist/memory/dreamer/collection.js +0 -104
  123. package/dist/memory/dreamer/dreamer.js +0 -352
  124. package/dist/memory/dreamer/review.js +0 -111
  125. package/dist/memory/durableAppend.js +0 -57
  126. package/dist/memory/factConflicts.js +0 -205
  127. package/dist/memory/factTerms.js +0 -321
  128. package/dist/memory/handoff.js +0 -363
  129. package/dist/memory/hiveNameHeader.cjs +0 -10
  130. package/dist/memory/inventory.js +0 -55
  131. package/dist/memory/invisibleText.js +0 -20
  132. package/dist/memory/lifecycle.js +0 -2014
  133. package/dist/memory/limits.js +0 -13
  134. package/dist/memory/requests.js +0 -183
  135. package/dist/memory/sourceName.js +0 -276
  136. package/dist/memory/statusFacts.js +0 -61
  137. package/dist/memory/whatsNew.js +0 -158
  138. package/dist/memory/writer.js +0 -176
  139. package/dist/memory/writerConflicts.js +0 -85
  140. package/dist/memorymap/memoryMap.js +0 -1594
  141. package/dist/memorymap/replacedJournal.js +0 -58
  142. package/dist/memorymap/replacements.js +0 -410
  143. package/dist/mvp/activity.js +0 -86
  144. package/dist/mvp/agentMemory.js +0 -1803
  145. package/dist/mvp/answerCheck.js +0 -211
  146. package/dist/mvp/auth.js +0 -531
  147. package/dist/mvp/cli.js +0 -86
  148. package/dist/mvp/client-store.js +0 -462
  149. package/dist/mvp/compileCache.js +0 -242
  150. package/dist/mvp/compileSeed.js +0 -33
  151. package/dist/mvp/config.js +0 -292
  152. package/dist/mvp/connectionStatus.js +0 -80
  153. package/dist/mvp/contextDistinct.js +0 -53
  154. package/dist/mvp/contextProfile.js +0 -87
  155. package/dist/mvp/contextRelevance.js +0 -413
  156. package/dist/mvp/directorySurface.js +0 -68
  157. package/dist/mvp/engine.js +0 -1078
  158. package/dist/mvp/exportAll.js +0 -393
  159. package/dist/mvp/factLanePack.js +0 -77
  160. package/dist/mvp/factMarkers.js +0 -65
  161. package/dist/mvp/factsLane.js +0 -203
  162. package/dist/mvp/hiddenSpans.js +0 -585
  163. package/dist/mvp/history.js +0 -414
  164. package/dist/mvp/hitDates.js +0 -31
  165. package/dist/mvp/hitFloor.js +0 -36
  166. package/dist/mvp/identity.js +0 -18
  167. package/dist/mvp/idle.js +0 -113
  168. package/dist/mvp/log.js +0 -118
  169. package/dist/mvp/memoryDir.js +0 -98
  170. package/dist/mvp/posthogMcp.js +0 -210
  171. package/dist/mvp/readScope.js +0 -104
  172. package/dist/mvp/readUsefulness.js +0 -222
  173. package/dist/mvp/sanitize.js +0 -48
  174. package/dist/mvp/schemaRejections.js +0 -91
  175. package/dist/mvp/scope.js +0 -127
  176. package/dist/mvp/server.js +0 -2712
  177. package/dist/mvp/serverCard.js +0 -38
  178. package/dist/mvp/storedSize.js +0 -139
  179. package/dist/mvp/telemetry.js +0 -474
  180. package/dist/net/poolFetch.js +0 -174
  181. package/dist/recall/actionRecall.js +0 -282
  182. package/dist/recall/autoRecall.js +0 -447
  183. package/dist/recall/searchRecall.js +0 -189
  184. package/dist/retrieval/adviceLines.js +0 -195
  185. package/dist/retrieval/adviceQuestion.js +0 -44
  186. package/dist/retrieval/conversationShape.js +0 -44
  187. package/dist/retrieval/dailyEntryTime.js +0 -55
  188. package/dist/retrieval/entityLookup.js +0 -162
  189. package/dist/retrieval/factsFirst.js +0 -188
  190. package/dist/retrieval/keyword.js +0 -84
  191. package/dist/retrieval/ledgerEventTime.js +0 -51
  192. package/dist/retrieval/ledgerLiveness.js +0 -83
  193. package/dist/retrieval/ledgerPresentation.js +0 -161
  194. package/dist/retrieval/noteIdentity.js +0 -57
  195. package/dist/retrieval/orchestrator.js +0 -1224
  196. package/dist/retrieval/plainWording.js +0 -236
  197. package/dist/retrieval/queryHelpers.js +0 -142
  198. package/dist/retrieval/questionSpan.js +0 -475
  199. package/dist/retrieval/rankingTrace.js +0 -184
  200. package/dist/retrieval/readerEvents.js +0 -690
  201. package/dist/retrieval/readerLayout.js +0 -422
  202. package/dist/retrieval/relativeTimeHints.js +0 -113
  203. package/dist/retrieval/semantic.js +0 -67
  204. package/dist/router/routerCache.js +0 -106
  205. package/dist/router/sessionRouter.js +0 -726
  206. package/dist/sharing/cli.js +0 -181
  207. package/dist/sharing/examples.js +0 -136
  208. package/dist/sharing/fields.js +0 -225
  209. package/dist/sharing/loop.js +0 -80
  210. package/dist/sharing/project.js +0 -327
  211. package/dist/sharing/send.js +0 -466
  212. package/dist/sharing/state.js +0 -222
  213. package/dist/sharing/wording.js +0 -48
  214. package/dist/toolRefusal.js +0 -120
  215. package/dist/watcher/watcher.js +0 -77
  216. /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
- }