agenr 2.0.1 → 3.0.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.
@@ -1,1817 +1,7 @@
1
1
  #!/usr/bin/env node
2
- import {
3
- composeProcedureRecallText,
4
- computeProcedureRevisionHash,
5
- computeProcedureSourceHash
6
- } from "./chunk-ZYADFKX3.js";
7
- import {
8
- CLAIM_KEY_SOURCES,
9
- CLAIM_KEY_STATUSES,
10
- CLAIM_SUPPORT_MODES,
11
- ENTRY_TYPES,
12
- EXPIRY_LEVELS,
13
- composeEmbeddingText,
14
- createDatabase,
15
- createEmbeddingClient,
16
- createRecallAdapter,
17
- isRecord,
18
- normalizeProcedureDefinition,
19
- parseOptionalBoolean,
20
- parseOptionalIntegerInRange,
21
- parseOptionalTimestampString,
22
- parseOptionalTrimmedString,
23
- parseRequiredTrimmedString,
24
- projectClaimCentricRecallEntry,
25
- pushIssue,
26
- pushUnexpectedFields,
27
- readConfig,
28
- resolveEmbeddingApiKey,
29
- resolveEmbeddingModel,
30
- runUnifiedRecall
31
- } from "./chunk-Y2BC7RCE.js";
32
- import {
33
- recall
34
- } from "./chunk-MEHOGUZE.js";
35
-
36
- // src/internal-recall-eval-server.ts
37
- import process from "process";
38
-
39
- // src/adapters/api/internal-recall-eval-server.ts
40
- import { createServer } from "http";
41
-
42
- // src/app/evals/recall/collect-diagnostics.ts
43
- function createRecallEvalDiagnosticsCollector(request) {
44
- const diagnosticsRequested = wantsRecallEvalDiagnostics(request);
45
- const timingsRequested = request.options?.includeTimings === true;
46
- const observationEnabled = diagnosticsRequested || timingsRequested;
47
- const execution = {
48
- mode: "isolated-case",
49
- provisioning: "exact-fixture-seed",
50
- recallPath: request.recallPath ?? "core",
51
- memoryPoolCount: request.memoryPool.length,
52
- provisionedCount: 0,
53
- requestedDiagnostics: request.options?.includeDiagnostics === true,
54
- requestedCandidates: request.options?.includeCandidates === true
55
- };
56
- const stageTimings = {
57
- sandboxSetupMs: 0,
58
- fixtureProvisionMs: 0,
59
- recallMs: 0,
60
- queryEmbeddingMs: 0,
61
- vectorSearchMs: 0,
62
- lexicalSearchMs: 0,
63
- mergeCandidatesMs: 0,
64
- scoreCandidatesMs: 0,
65
- thresholdMs: 0,
66
- budgetMs: 0,
67
- hydrateEntriesMs: 0,
68
- shapeResultsMs: 0,
69
- recordRecallEventsMs: 0
70
- };
71
- const retrieval = {
72
- queryEmbeddingDimensions: 0,
73
- vectorSearchLimit: 0,
74
- lexicalSearchLimit: 0
75
- };
76
- const candidateCounts = {
77
- vectorRetrieved: 0,
78
- lexicalRetrieved: 0,
79
- merged: 0,
80
- thresholdQualified: 0,
81
- budgetAccepted: 0,
82
- finalRanked: 0,
83
- hydrated: 0,
84
- returned: 0,
85
- telemetryAttempted: 0
86
- };
87
- let provision;
88
- let ranking;
89
- let filtering;
90
- let claimKey;
91
- let degraded;
92
- let provisionObserved = false;
93
- let retrievalObserved = false;
94
- let traceObserved = false;
95
- const traceSink = {
96
- reportSummary(summary) {
97
- traceObserved = true;
98
- ranking = {
99
- limit: summary.ranking.limit,
100
- threshold: summary.ranking.threshold,
101
- budget: summary.ranking.budget,
102
- noResultReason: summary.ranking.noResultReason
103
- };
104
- filtering = {
105
- types: [...summary.filtering.types],
106
- tags: [...summary.filtering.tags],
107
- since: summary.filtering.since,
108
- until: summary.filtering.until,
109
- around: summary.filtering.around ? {
110
- source: summary.filtering.around.source,
111
- anchor: summary.filtering.around.anchor,
112
- radiusDays: summary.filtering.around.radiusDays
113
- } : void 0
114
- };
115
- candidateCounts.merged = summary.candidateCounts.merged;
116
- candidateCounts.thresholdQualified = summary.candidateCounts.thresholdQualified;
117
- candidateCounts.budgetAccepted = summary.candidateCounts.budgetAccepted;
118
- candidateCounts.finalRanked = summary.candidateCounts.finalRanked;
119
- candidateCounts.returned = summary.candidateCounts.returned;
120
- claimKey = {
121
- historicalBoosted: summary.claimKey.historicalBoosted,
122
- tentativeLineageSuppressed: summary.claimKey.tentativeLineageSuppressed,
123
- trustPenalized: summary.claimKey.trustPenalized,
124
- redundancyPenalized: summary.claimKey.redundancyPenalized
125
- };
126
- degraded = {
127
- active: summary.degraded.active,
128
- reasons: [...summary.degraded.reasons],
129
- lexicalOnly: summary.degraded.lexicalOnly,
130
- notices: [...summary.degraded.notices]
131
- };
132
- stageTimings.mergeCandidatesMs = summary.timings.mergeCandidatesMs;
133
- stageTimings.scoreCandidatesMs = summary.timings.scoreCandidatesMs;
134
- stageTimings.thresholdMs = summary.timings.thresholdMs;
135
- stageTimings.budgetMs = summary.timings.budgetMs;
136
- stageTimings.shapeResultsMs = summary.timings.shapeResultsMs;
137
- }
138
- };
139
- return {
140
- traceSink,
141
- isObservationEnabled() {
142
- return observationEnabled;
143
- },
144
- recordSandboxSetup(durationMs) {
145
- stageTimings.sandboxSetupMs = durationMs;
146
- },
147
- recordFixtureProvisionTiming(durationMs) {
148
- stageTimings.fixtureProvisionMs = durationMs;
149
- },
150
- recordProvision(result, durationMs) {
151
- provisionObserved = true;
152
- execution.provisionedCount = result.provisionedCount;
153
- stageTimings.fixtureProvisionMs = durationMs;
154
- provision = {
155
- requestedCount: request.memoryPool.length,
156
- provisionedCount: result.provisionedCount,
157
- providedIdCount: result.providedIdCount,
158
- generatedIdCount: result.generatedIdCount,
159
- retiredCount: result.retiredCount,
160
- supersededCount: result.supersededCount,
161
- createdAtDefaultedCount: result.createdAtDefaultedCount,
162
- updatedAtDefaultedCount: result.updatedAtDefaultedCount,
163
- seededEntries: result.seededEntries.map((entry) => ({
164
- id: entry.id,
165
- created_at: entry.created_at,
166
- updated_at: entry.updated_at,
167
- retired: entry.retired,
168
- superseded_by: entry.superseded_by,
169
- claim_key: entry.claim_key,
170
- claim_key_status: entry.claim_key_status,
171
- valid_from: entry.valid_from,
172
- valid_to: entry.valid_to
173
- }))
174
- };
175
- },
176
- recordRecall(durationMs) {
177
- stageTimings.recallMs = durationMs;
178
- },
179
- recordQueryEmbedding(params) {
180
- retrievalObserved = true;
181
- stageTimings.queryEmbeddingMs = params.durationMs;
182
- retrieval.queryEmbeddingDimensions = params.dimensions;
183
- },
184
- recordVectorSearch(params) {
185
- retrievalObserved = true;
186
- stageTimings.vectorSearchMs = params.durationMs;
187
- retrieval.vectorSearchLimit = params.limit;
188
- candidateCounts.vectorRetrieved = params.count;
189
- },
190
- recordLexicalSearch(params) {
191
- retrievalObserved = true;
192
- stageTimings.lexicalSearchMs = params.durationMs;
193
- retrieval.lexicalSearchLimit = params.limit;
194
- candidateCounts.lexicalRetrieved = params.count;
195
- },
196
- recordHydrateEntries(params) {
197
- retrievalObserved = true;
198
- stageTimings.hydrateEntriesMs = params.durationMs;
199
- candidateCounts.hydrated = params.count;
200
- },
201
- recordRecallTelemetry(params) {
202
- retrievalObserved = true;
203
- stageTimings.recordRecallEventsMs = params.durationMs;
204
- candidateCounts.telemetryAttempted = params.entryCount;
205
- },
206
- buildDiagnostics() {
207
- if (!diagnosticsRequested) {
208
- return void 0;
209
- }
210
- return {
211
- execution,
212
- provision: provisionObserved ? provision : void 0,
213
- retrieval: retrievalObserved ? retrieval : void 0,
214
- ranking: traceObserved ? ranking : void 0,
215
- filtering: traceObserved ? filtering : void 0,
216
- claimKey: traceObserved ? claimKey : void 0,
217
- degraded: traceObserved ? degraded : void 0,
218
- candidateCounts
219
- };
220
- },
221
- buildTimings(totalMs) {
222
- if (!timingsRequested) {
223
- return void 0;
224
- }
225
- return {
226
- totalMs,
227
- sandboxSetupMs: stageTimings.sandboxSetupMs,
228
- fixtureProvisionMs: stageTimings.fixtureProvisionMs,
229
- recallMs: stageTimings.recallMs,
230
- queryEmbeddingMs: stageTimings.queryEmbeddingMs,
231
- vectorSearchMs: stageTimings.vectorSearchMs,
232
- lexicalSearchMs: stageTimings.lexicalSearchMs,
233
- mergeCandidatesMs: stageTimings.mergeCandidatesMs,
234
- scoreCandidatesMs: stageTimings.scoreCandidatesMs,
235
- thresholdMs: stageTimings.thresholdMs,
236
- budgetMs: stageTimings.budgetMs,
237
- hydrateEntriesMs: stageTimings.hydrateEntriesMs,
238
- shapeResultsMs: stageTimings.shapeResultsMs,
239
- recordRecallEventsMs: stageTimings.recordRecallEventsMs
240
- };
241
- }
242
- };
243
- }
244
- function wantsRecallEvalDiagnostics(request) {
245
- return request.options?.includeDiagnostics === true || request.options?.includeCandidates === true;
246
- }
247
-
248
- // src/app/evals/recall/instrumented-recall-ports.ts
249
- function createInstrumentedRecallPorts(ports, observer) {
250
- return {
251
- async embed(text) {
252
- const startedAt = Date.now();
253
- try {
254
- const embedding = await ports.embed(text);
255
- observer.recordQueryEmbedding({
256
- durationMs: elapsedMs(startedAt),
257
- dimensions: embedding.length
258
- });
259
- return embedding;
260
- } catch (error) {
261
- observer.recordQueryEmbedding({
262
- durationMs: elapsedMs(startedAt),
263
- dimensions: 0
264
- });
265
- throw error;
266
- }
267
- },
268
- async vectorSearch(params) {
269
- const startedAt = Date.now();
270
- try {
271
- const results = await ports.vectorSearch(params);
272
- observer.recordVectorSearch({
273
- durationMs: elapsedMs(startedAt),
274
- count: results.length,
275
- limit: params.limit
276
- });
277
- return results;
278
- } catch (error) {
279
- observer.recordVectorSearch({
280
- durationMs: elapsedMs(startedAt),
281
- count: 0,
282
- limit: params.limit
283
- });
284
- throw error;
285
- }
286
- },
287
- async ftsSearch(params) {
288
- const startedAt = Date.now();
289
- try {
290
- const results = await ports.ftsSearch(params);
291
- observer.recordLexicalSearch({
292
- durationMs: elapsedMs(startedAt),
293
- count: results.length,
294
- limit: params.limit
295
- });
296
- return results;
297
- } catch (error) {
298
- observer.recordLexicalSearch({
299
- durationMs: elapsedMs(startedAt),
300
- count: 0,
301
- limit: params.limit
302
- });
303
- throw error;
304
- }
305
- },
306
- ...ports.fetchPredecessors ? {
307
- async fetchPredecessors(params) {
308
- return ports.fetchPredecessors(params);
309
- }
310
- } : {},
311
- async hydrateEntries(ids) {
312
- const startedAt = Date.now();
313
- try {
314
- const entries = await ports.hydrateEntries(ids);
315
- observer.recordHydrateEntries({
316
- durationMs: elapsedMs(startedAt),
317
- count: entries.length
318
- });
319
- return entries;
320
- } catch (error) {
321
- observer.recordHydrateEntries({
322
- durationMs: elapsedMs(startedAt),
323
- count: 0
324
- });
325
- throw error;
326
- }
327
- },
328
- async recordRecallEvents(params) {
329
- const startedAt = Date.now();
330
- try {
331
- await ports.recordRecallEvents(params);
332
- } finally {
333
- observer.recordRecallTelemetry({
334
- durationMs: elapsedMs(startedAt),
335
- entryCount: params.entryIds.length
336
- });
337
- }
338
- }
339
- };
340
- }
341
- function elapsedMs(startedAt) {
342
- return Math.max(0, Date.now() - startedAt);
343
- }
344
-
345
- // src/app/evals/recall/normalize-response.ts
346
- function buildRecallEvalSuccessResponse(params) {
347
- const entryResults = Array.isArray(params.results) ? params.results : params.results.entries;
348
- const projectedEntries = Array.isArray(params.results) ? entryResults.map((result) => projectClaimCentricRecallEntry(result, { asOf: params.request.recallRequest.asOf })) : params.results.projectedEntries;
349
- const metadata = buildMetadata(params.request, params.results, projectedEntries);
350
- return {
351
- status: "ok",
352
- caseId: params.request.caseId,
353
- result: {
354
- entries: entryResults.map((result, index) => ({
355
- id: result.entry.id,
356
- subject: result.entry.subject,
357
- content: result.entry.content,
358
- type: result.entry.type,
359
- importance: result.entry.importance,
360
- expiry: result.entry.expiry,
361
- tags: result.entry.tags,
362
- created_at: result.entry.created_at,
363
- score: result.score,
364
- scores: result.scores,
365
- claim: {
366
- familyKey: projectedEntries[index]?.familyKey ?? `entry:${result.entry.id}`,
367
- claimKey: projectedEntries[index]?.claimKey,
368
- slotPolicy: projectedEntries[index]?.slotPolicy ?? "exclusive",
369
- memoryState: projectedEntries[index]?.memoryState ?? "current",
370
- claimStatus: projectedEntries[index]?.claimStatus ?? "no_key",
371
- freshness: projectedEntries[index]?.freshness ?? {
372
- createdAt: result.entry.created_at,
373
- isCurrent: true,
374
- label: `created ${result.entry.created_at} | current state`
375
- },
376
- provenance: projectedEntries[index]?.provenance ?? {},
377
- whySurfaced: projectedEntries[index]?.whySurfaced ?? {
378
- summary: `ranked score ${result.score.toFixed(2)}`,
379
- reasons: []
380
- }
381
- }
382
- })),
383
- entryIds: entryResults.map((result) => result.entry.id)
384
- },
385
- metadata,
386
- diagnostics: params.diagnostics,
387
- timings: params.timings,
388
- sandbox: buildSandboxResult(params.sandbox)
389
- };
390
- }
391
- function buildRecallEvalErrorResponse(params) {
392
- return {
393
- status: "error",
394
- caseId: params.request.caseId,
395
- error: {
396
- code: params.code,
397
- message: params.message,
398
- details: params.details
399
- },
400
- diagnostics: params.diagnostics,
401
- timings: params.timings,
402
- sandbox: params.sandbox ? buildSandboxResult(params.sandbox) : void 0
403
- };
404
- }
405
- function buildSandboxResult(sandbox) {
406
- return {
407
- root: sandbox.root,
408
- dbPath: sandbox.dbPath,
409
- preserved: sandbox.preserved
410
- };
411
- }
412
- function buildMetadata(request, results, projectedEntries) {
413
- if (Array.isArray(results)) {
414
- return {
415
- path: request.recallPath ?? "core",
416
- claim: {
417
- projectedEntries: projectedEntries.map(buildProjectedEntryMetadata)
418
- }
419
- };
420
- }
421
- return {
422
- path: "unified",
423
- claim: {
424
- projectedEntries: projectedEntries.map(buildProjectedEntryMetadata),
425
- entryFamilies: results.entryFamilies.map(buildClaimFamilyMetadata),
426
- transitions: results.claimTransitions
427
- },
428
- unified: {
429
- routing: results.routing,
430
- timeWindow: results.timeWindow,
431
- asOf: results.asOf,
432
- procedure: results.procedure ? {
433
- id: results.procedure.id,
434
- procedureKey: results.procedure.procedure_key,
435
- title: results.procedure.title,
436
- goal: results.procedure.goal
437
- } : void 0,
438
- procedureCandidates: results.procedureCandidates.map((candidate) => ({
439
- id: candidate.procedure.id,
440
- procedureKey: candidate.procedure.procedure_key,
441
- title: candidate.procedure.title,
442
- score: candidate.score,
443
- lexicalScore: candidate.scores.lexical,
444
- vectorScore: candidate.scores.vector
445
- })),
446
- procedureNotices: results.procedureNotices,
447
- notices: results.notices,
448
- episodeCount: results.episodes.length
449
- }
450
- };
451
- }
452
- function buildProjectedEntryMetadata(entry) {
453
- return {
454
- entryId: entry.entryId,
455
- familyKey: entry.familyKey,
456
- claimKey: entry.claimKey,
457
- slotPolicy: entry.slotPolicy,
458
- memoryState: entry.memoryState,
459
- claimStatus: entry.claimStatus,
460
- freshness: entry.freshness,
461
- provenance: entry.provenance,
462
- whySurfaced: entry.whySurfaced
463
- };
464
- }
465
- function buildClaimFamilyMetadata(family) {
466
- return {
467
- familyKey: family.familyKey,
468
- claimKey: family.claimKey,
469
- slotPolicy: family.slotPolicy,
470
- subject: family.subject,
471
- primaryEntryId: family.primary.entryId,
472
- entries: family.entries.map((entry) => ({
473
- id: entry.entryId,
474
- memoryState: entry.memoryState,
475
- claimStatus: entry.claimStatus
476
- }))
477
- };
478
- }
479
-
480
- // src/app/evals/recall/provision-fixtures.ts
481
- import { createHash } from "crypto";
482
- var DEFAULT_IMPORTANCE = 6;
483
- var DEFAULT_EXPIRY = "permanent";
484
- var DEFAULT_QUALITY_SCORE = 0.5;
485
- async function provisionRecallEvalFixtures(params) {
486
- const preparedBatch = prepareFixtures(params.caseId, params.memoryPool, params.provisionedAt);
487
- if (preparedBatch.insertionOrder.length === 0) {
488
- return {
489
- provisionedCount: 0,
490
- providedIdCount: 0,
491
- generatedIdCount: 0,
492
- retiredCount: 0,
493
- supersededCount: 0,
494
- createdAtDefaultedCount: 0,
495
- updatedAtDefaultedCount: 0,
496
- seededEntries: []
497
- };
498
- }
499
- const embeddings = await params.embedding.embed(preparedBatch.insertionOrder.map((fixture) => fixture.embeddingText));
500
- if (embeddings.length !== preparedBatch.insertionOrder.length) {
501
- throw new Error(`Fixture embedding count mismatch: expected ${preparedBatch.insertionOrder.length}, received ${embeddings.length}.`);
502
- }
503
- await params.store.withTransaction(async (store) => {
504
- for (const [index, fixture] of preparedBatch.insertionOrder.entries()) {
505
- await store.insertEntry(fixture.entry, embeddings[index] ?? [], fixture.contentHash);
506
- }
507
- });
508
- return {
509
- provisionedCount: preparedBatch.insertionOrder.length,
510
- providedIdCount: preparedBatch.providedIdCount,
511
- generatedIdCount: preparedBatch.generatedIdCount,
512
- retiredCount: preparedBatch.retiredCount,
513
- supersededCount: preparedBatch.supersededCount,
514
- createdAtDefaultedCount: preparedBatch.createdAtDefaultedCount,
515
- updatedAtDefaultedCount: preparedBatch.updatedAtDefaultedCount,
516
- seededEntries: preparedBatch.seededEntries
517
- };
518
- }
519
- function prepareFixtures(caseId, fixtures, provisionedAt) {
520
- const resolvedIds = fixtures.map((fixture, index) => fixture.id ?? createFixtureId(caseId, index, fixture));
521
- const duplicateIds = findDuplicateIds(resolvedIds);
522
- if (duplicateIds.length > 0) {
523
- throw new Error(`Fixture IDs must be unique. Duplicate IDs: ${duplicateIds.join(", ")}.`);
524
- }
525
- const knownIds = new Set(resolvedIds);
526
- const prepared = fixtures.map((fixture, index) => {
527
- const supersededBy = fixture.superseded_by;
528
- if (supersededBy && !knownIds.has(supersededBy)) {
529
- throw new Error(`memoryPool[${index}].superseded_by references unknown fixture id "${supersededBy}".`);
530
- }
531
- const entry = buildEntry(fixture, resolvedIds[index] ?? "", provisionedAt);
532
- return {
533
- fixtureIndex: index,
534
- entry,
535
- contentHash: hashText(`${entry.type}
536
- ${entry.subject}
537
- ${entry.content}`),
538
- embeddingText: composeEmbeddingText(entry)
539
- };
540
- });
541
- return {
542
- insertionOrder: topologicallySortFixtures(prepared),
543
- providedIdCount: fixtures.filter((fixture) => fixture.id !== void 0).length,
544
- generatedIdCount: fixtures.filter((fixture) => fixture.id === void 0).length,
545
- retiredCount: prepared.filter((fixture) => fixture.entry.retired).length,
546
- supersededCount: prepared.filter((fixture) => fixture.entry.superseded_by !== void 0).length,
547
- createdAtDefaultedCount: fixtures.filter((fixture) => fixture.created_at === void 0).length,
548
- updatedAtDefaultedCount: fixtures.filter((fixture) => fixture.updated_at === void 0).length,
549
- seededEntries: prepared.map((fixture) => summarizePreparedFixture(fixture.entry))
550
- };
551
- }
552
- function buildEntry(fixture, id, provisionedAt) {
553
- const createdAt = fixture.created_at ?? provisionedAt;
554
- const updatedAt = fixture.updated_at ?? createdAt;
555
- return {
556
- id,
557
- type: fixture.type,
558
- subject: fixture.subject,
559
- content: fixture.content,
560
- importance: fixture.importance ?? DEFAULT_IMPORTANCE,
561
- expiry: fixture.expiry ?? DEFAULT_EXPIRY,
562
- tags: fixture.tags ?? [],
563
- source_file: fixture.source_file,
564
- source_context: fixture.source_context,
565
- quality_score: DEFAULT_QUALITY_SCORE,
566
- recall_count: 0,
567
- superseded_by: fixture.superseded_by,
568
- claim_key: fixture.claim_key,
569
- claim_key_status: fixture.claim_key_status,
570
- claim_key_source: fixture.claim_key_source,
571
- claim_support_source_kind: fixture.claim_support_source_kind,
572
- claim_support_locator: fixture.claim_support_locator,
573
- claim_support_observed_at: fixture.claim_support_observed_at,
574
- claim_support_mode: fixture.claim_support_mode,
575
- valid_from: fixture.valid_from,
576
- valid_to: fixture.valid_to,
577
- supersession_kind: fixture.supersession_kind,
578
- supersession_reason: fixture.supersession_reason,
579
- retired: fixture.retired ?? false,
580
- retired_at: fixture.retired_at,
581
- retired_reason: fixture.retired_reason,
582
- created_at: createdAt,
583
- updated_at: updatedAt
584
- };
585
- }
586
- function summarizePreparedFixture(entry) {
587
- return {
588
- id: entry.id,
589
- created_at: entry.created_at,
590
- updated_at: entry.updated_at,
591
- retired: entry.retired,
592
- superseded_by: entry.superseded_by,
593
- claim_key: entry.claim_key,
594
- claim_key_status: entry.claim_key_status,
595
- valid_from: entry.valid_from,
596
- valid_to: entry.valid_to
597
- };
598
- }
599
- function createFixtureId(caseId, index, fixture) {
600
- const digest = createHash("sha256").update(caseId).update(":").update(String(index)).update(":").update(fixture.type).update(":").update(fixture.subject).update(":").update(fixture.content).digest("hex");
601
- return `eval-${digest.slice(0, 24)}`;
602
- }
603
- function findDuplicateIds(ids) {
604
- const seen = /* @__PURE__ */ new Set();
605
- const duplicates = [];
606
- for (const id of ids) {
607
- if (seen.has(id)) {
608
- if (!duplicates.includes(id)) {
609
- duplicates.push(id);
610
- }
611
- continue;
612
- }
613
- seen.add(id);
614
- }
615
- return duplicates;
616
- }
617
- function topologicallySortFixtures(fixtures) {
618
- const indegree = new Map(fixtures.map((fixture) => [fixture.entry.id, 0]));
619
- const dependents = /* @__PURE__ */ new Map();
620
- for (const fixture of fixtures) {
621
- const successorId = fixture.entry.superseded_by;
622
- if (!successorId) {
623
- continue;
624
- }
625
- indegree.set(fixture.entry.id, (indegree.get(fixture.entry.id) ?? 0) + 1);
626
- const successorDependents = dependents.get(successorId) ?? [];
627
- successorDependents.push(fixture);
628
- dependents.set(successorId, successorDependents);
629
- }
630
- const ready = fixtures.filter((fixture) => (indegree.get(fixture.entry.id) ?? 0) === 0).sort((left, right) => left.fixtureIndex - right.fixtureIndex);
631
- const sorted = [];
632
- while (ready.length > 0) {
633
- const current = ready.shift();
634
- if (!current) {
635
- break;
636
- }
637
- sorted.push(current);
638
- const currentDependents = (dependents.get(current.entry.id) ?? []).sort((left, right) => left.fixtureIndex - right.fixtureIndex);
639
- for (const dependent of currentDependents) {
640
- const remaining = (indegree.get(dependent.entry.id) ?? 0) - 1;
641
- indegree.set(dependent.entry.id, remaining);
642
- if (remaining === 0) {
643
- ready.push(dependent);
644
- ready.sort((left, right) => left.fixtureIndex - right.fixtureIndex);
645
- }
646
- }
647
- }
648
- if (sorted.length !== fixtures.length) {
649
- const unresolved = fixtures.filter((fixture) => !sorted.includes(fixture)).map((fixture) => fixture.entry.id);
650
- throw new Error(`Fixture supersession metadata contains a cycle: ${unresolved.join(", ")}.`);
651
- }
652
- return sorted;
653
- }
654
- function hashText(value) {
655
- return createHash("sha256").update(value).digest("hex");
656
- }
657
-
658
- // src/app/evals/recall/provision-procedure-fixtures.ts
659
- import { createHash as createHash2 } from "crypto";
660
- async function provisionRecallEvalProcedureFixtures(params) {
661
- const procedures = prepareProcedures(params.caseId, params.procedurePool, params.provisionedAt);
662
- if (procedures.length === 0) {
663
- return {
664
- provisionedCount: 0
665
- };
666
- }
667
- await params.store.withTransaction(async (store) => {
668
- for (const procedure of procedures) {
669
- await store.insertProcedure(procedure);
670
- }
671
- });
672
- return {
673
- provisionedCount: procedures.length
674
- };
675
- }
676
- function prepareProcedures(caseId, fixtures, provisionedAt) {
677
- const resolvedIds = fixtures.map((fixture, index) => fixture.id ?? createFixtureId2(caseId, index, fixture));
678
- const duplicateIds = findDuplicateIds2(resolvedIds);
679
- if (duplicateIds.length > 0) {
680
- throw new Error(`Procedure fixture IDs must be unique. Duplicate IDs: ${duplicateIds.join(", ")}.`);
681
- }
682
- const knownIds = new Set(resolvedIds);
683
- return fixtures.map((fixture, index) => {
684
- if (fixture.superseded_by && !knownIds.has(fixture.superseded_by)) {
685
- throw new Error(`procedurePool[${index}].superseded_by references unknown fixture id "${fixture.superseded_by}".`);
686
- }
687
- const normalizedBody = normalizeProcedureDefinition(
688
- {
689
- procedure_key: fixture.procedure_key,
690
- title: fixture.title,
691
- goal: fixture.goal,
692
- when_to_use: fixture.when_to_use ?? [],
693
- when_not_to_use: fixture.when_not_to_use ?? [],
694
- prerequisites: fixture.prerequisites ?? [],
695
- steps: fixture.steps,
696
- verification: fixture.verification ?? [],
697
- failure_modes: fixture.failure_modes ?? [],
698
- sources: fixture.sources ?? [{ kind: "manual", label: "recall eval fixture" }]
699
- },
700
- `procedurePool[${index}]`
701
- );
702
- const createdAt = fixture.created_at ?? provisionedAt;
703
- const updatedAt = fixture.updated_at ?? createdAt;
704
- return {
705
- id: resolvedIds[index] ?? "",
706
- ...normalizedBody,
707
- source_file: fixture.source_file,
708
- recall_text: composeProcedureRecallText(normalizedBody),
709
- revision_hash: computeProcedureRevisionHash(normalizedBody),
710
- source_hash: computeProcedureSourceHash(JSON.stringify(normalizedBody)),
711
- retired: fixture.retired ?? false,
712
- retired_at: fixture.retired_at,
713
- retired_reason: fixture.retired_reason,
714
- superseded_by: fixture.superseded_by,
715
- created_at: createdAt,
716
- updated_at: updatedAt
717
- };
718
- });
719
- }
720
- function createFixtureId2(caseId, index, fixture) {
721
- const digest = createHash2("sha256").update(caseId).update(":").update(String(index)).update(":").update(fixture.procedure_key).update(":").update(fixture.title).update(":").update(fixture.goal).digest("hex");
722
- return `eval-procedure-${digest.slice(0, 24)}`;
723
- }
724
- function findDuplicateIds2(ids) {
725
- const seen = /* @__PURE__ */ new Set();
726
- const duplicates = [];
727
- for (const id of ids) {
728
- if (seen.has(id)) {
729
- if (!duplicates.includes(id)) {
730
- duplicates.push(id);
731
- }
732
- continue;
733
- }
734
- seen.add(id);
735
- }
736
- return duplicates;
737
- }
738
-
739
- // src/app/evals/recall/sandbox.ts
740
- import { mkdir, mkdtemp, rm } from "fs/promises";
741
- import { tmpdir } from "os";
742
- import path from "path";
743
-
744
- // src/adapters/db/eval-fixture-store.ts
745
- function createRecallEvalFixtureStore(database) {
746
- return {
747
- insertEntry: async (entry, embedding, contentHash) => database.insertEntry(entry, embedding, contentHash),
748
- insertProcedure: async (procedure) => database.upsertProcedure(procedure),
749
- withTransaction: async (fn) => database.withTransaction(async (transaction) => fn(createRecallEvalFixtureStore(transaction)))
750
- };
751
- }
752
-
753
- // src/app/evals/recall/sandbox.ts
754
- var SANDBOX_DB_FILENAME = "knowledge.db";
755
- var SANDBOX_DIR_PREFIX = "agenr-recall-eval-";
756
- async function setupRecallEvalSandbox(request) {
757
- const suppliedRoot = request?.root !== void 0;
758
- const preserved = request?.preserve === true;
759
- const root = suppliedRoot ? path.resolve(request.root ?? "") : await mkdtemp(path.join(tmpdir(), SANDBOX_DIR_PREFIX));
760
- let database;
761
- const dbPath = path.join(root, SANDBOX_DB_FILENAME);
762
- try {
763
- if (suppliedRoot) {
764
- await mkdir(root, { recursive: true });
765
- }
766
- await removeDatabaseFiles(dbPath);
767
- database = await createDatabase(dbPath);
768
- const openDatabase = database;
769
- return {
770
- root,
771
- dbPath,
772
- preserved,
773
- fixtureStore: createRecallEvalFixtureStore(openDatabase),
774
- episodeDatabase: openDatabase,
775
- procedureDatabase: openDatabase,
776
- createRecallPorts: (embedding) => createRecallAdapter(openDatabase, embedding),
777
- cleanup: async () => {
778
- await openDatabase.close().catch(() => void 0);
779
- if (preserved) {
780
- return;
781
- }
782
- if (suppliedRoot) {
783
- await removeDatabaseFiles(dbPath);
784
- return;
785
- }
786
- await rm(root, { recursive: true, force: true });
787
- }
788
- };
789
- } catch (error) {
790
- await database?.close().catch(() => void 0);
791
- if (!preserved) {
792
- if (suppliedRoot) {
793
- await removeDatabaseFiles(dbPath).catch(() => void 0);
794
- } else {
795
- await rm(root, { recursive: true, force: true }).catch(() => void 0);
796
- }
797
- }
798
- throw error;
799
- }
800
- }
801
- async function removeDatabaseFiles(dbPath) {
802
- await Promise.all([rm(dbPath, { force: true }), rm(`${dbPath}-wal`, { force: true }), rm(`${dbPath}-shm`, { force: true })]);
803
- }
804
-
805
- // src/app/evals/recall/run-recall-eval-case.ts
806
- async function runRecallEvalCase(request) {
807
- const startedAt = Date.now();
808
- const provisionedAt = new Date(startedAt).toISOString();
809
- const diagnostics = createRecallEvalDiagnosticsCollector(request);
810
- const recallPath = request.recallPath ?? "core";
811
- let sandbox;
812
- let sharedEmbeddingPort;
813
- let sharedEmbeddingError;
814
- const getEmbeddingSupport = () => {
815
- if (sharedEmbeddingPort) {
816
- return {
817
- available: true,
818
- port: sharedEmbeddingPort
819
- };
820
- }
821
- if (sharedEmbeddingError) {
822
- return {
823
- available: false,
824
- error: sharedEmbeddingError
825
- };
826
- }
827
- const config = readConfig();
828
- try {
829
- sharedEmbeddingPort = createEmbeddingClient(resolveEmbeddingApiKey(config), resolveEmbeddingModel(config));
830
- return {
831
- available: true,
832
- port: sharedEmbeddingPort
833
- };
834
- } catch (error) {
835
- sharedEmbeddingError = error instanceof Error ? error.message : String(error);
836
- return {
837
- available: false,
838
- error: sharedEmbeddingError
839
- };
840
- }
841
- };
842
- const getEmbeddingPort = () => {
843
- const support = getEmbeddingSupport();
844
- if (!support.port) {
845
- throw new Error(support.error ?? "Embeddings are unavailable.");
846
- }
847
- return support.port;
848
- };
849
- try {
850
- const sandboxStartedAt = Date.now();
851
- try {
852
- sandbox = await setupRecallEvalSandbox(request.sandbox);
853
- diagnostics.recordSandboxSetup(elapsedMs2(sandboxStartedAt));
854
- } catch (error) {
855
- diagnostics.recordSandboxSetup(elapsedMs2(sandboxStartedAt));
856
- return buildRecallEvalErrorResponse({
857
- request,
858
- code: "sandbox_setup_failed",
859
- message: "Failed to create isolated recall eval sandbox.",
860
- details: toErrorDetails(error),
861
- diagnostics: diagnostics.buildDiagnostics(),
862
- timings: diagnostics.buildTimings(elapsedMs2(startedAt))
863
- });
864
- }
865
- if (request.memoryPool.length > 0 || (request.procedurePool?.length ?? 0) > 0) {
866
- const provisionStartedAt = Date.now();
867
- try {
868
- let entryProvisionResult;
869
- if (request.memoryPool.length > 0) {
870
- entryProvisionResult = await provisionRecallEvalFixtures({
871
- caseId: request.caseId,
872
- memoryPool: request.memoryPool,
873
- store: sandbox.fixtureStore,
874
- embedding: getEmbeddingPort(),
875
- provisionedAt
876
- });
877
- }
878
- if ((request.procedurePool?.length ?? 0) > 0) {
879
- await provisionRecallEvalProcedureFixtures({
880
- caseId: request.caseId,
881
- procedurePool: request.procedurePool ?? [],
882
- store: sandbox.fixtureStore,
883
- provisionedAt
884
- });
885
- }
886
- if (entryProvisionResult) {
887
- diagnostics.recordProvision(entryProvisionResult, elapsedMs2(provisionStartedAt));
888
- } else {
889
- diagnostics.recordFixtureProvisionTiming(elapsedMs2(provisionStartedAt));
890
- }
891
- } catch (error) {
892
- diagnostics.recordFixtureProvisionTiming(elapsedMs2(provisionStartedAt));
893
- return buildRecallEvalErrorResponse({
894
- request,
895
- code: "fixture_provision_failed",
896
- message: "Failed to provision recall eval fixtures into isolated storage.",
897
- details: toErrorDetails(error),
898
- diagnostics: diagnostics.buildDiagnostics(),
899
- timings: diagnostics.buildTimings(elapsedMs2(startedAt)),
900
- sandbox
901
- });
902
- }
903
- }
904
- const recallStartedAt = Date.now();
905
- try {
906
- const embeddingSupport = getEmbeddingSupport();
907
- const recallEmbeddingPort = request.options?.faultInjection?.queryEmbeddingFailure === true ? createUnavailableEmbeddingPort("Injected recall eval query embedding failure.") : embeddingSupport.port ?? createUnavailableEmbeddingPort(embeddingSupport.error ?? "Embeddings are unavailable.");
908
- const basePorts = applyRecallEvalFaultInjection(sandbox.createRecallPorts(recallEmbeddingPort), request);
909
- const recallPorts = diagnostics.isObservationEnabled() ? createInstrumentedRecallPorts(basePorts, diagnostics) : basePorts;
910
- const slotPolicyConfig = request.unified?.memoryPolicy?.slotPolicies;
911
- const unifiedRecallOptions = {
912
- ...slotPolicyConfig ? { slotPolicyConfig } : {},
913
- ...diagnostics.isObservationEnabled() ? { trace: diagnostics.traceSink } : {}
914
- };
915
- const results = recallPath === "unified" ? await runUnifiedRecall(
916
- {
917
- text: request.recallRequest.text,
918
- ...request.unified?.mode ? { mode: request.unified.mode } : {},
919
- ...request.recallRequest.limit !== void 0 ? { limit: request.recallRequest.limit } : {},
920
- ...request.recallRequest.threshold !== void 0 ? { threshold: request.recallRequest.threshold } : {},
921
- ...request.recallRequest.types && request.recallRequest.types.length > 0 ? { types: request.recallRequest.types } : {},
922
- ...request.recallRequest.tags && request.recallRequest.tags.length > 0 ? { tags: request.recallRequest.tags } : {},
923
- ...request.recallRequest.asOf ? { asOf: request.recallRequest.asOf } : {},
924
- ...request.unified?.sessionKey ? { sessionKey: request.unified.sessionKey } : {}
925
- },
926
- {
927
- database: sandbox.episodeDatabase,
928
- procedures: sandbox.procedureDatabase,
929
- recall: recallPorts,
930
- embeddingAvailable: embeddingSupport.available,
931
- ...embeddingSupport.error ? { embeddingError: embeddingSupport.error } : {},
932
- ...slotPolicyConfig ? { claimSlotPolicyConfig: slotPolicyConfig } : {},
933
- ...embeddingSupport.available ? {
934
- embedQuery: async (text) => {
935
- const vectors = await recallEmbeddingPort.embed([text]);
936
- return vectors[0] ?? [];
937
- }
938
- } : {},
939
- ...Object.keys(unifiedRecallOptions).length > 0 ? { recallOptions: unifiedRecallOptions } : {}
940
- }
941
- ) : await recall(request.recallRequest, recallPorts, diagnostics.isObservationEnabled() ? { trace: diagnostics.traceSink } : void 0);
942
- diagnostics.recordRecall(elapsedMs2(recallStartedAt));
943
- return buildRecallEvalSuccessResponse({
944
- request,
945
- results,
946
- diagnostics: diagnostics.buildDiagnostics(),
947
- timings: diagnostics.buildTimings(elapsedMs2(startedAt)),
948
- sandbox
949
- });
950
- } catch (error) {
951
- diagnostics.recordRecall(elapsedMs2(recallStartedAt));
952
- return buildRecallEvalErrorResponse({
953
- request,
954
- code: "recall_execution_failed",
955
- message: "Failed to execute real recall against isolated eval state.",
956
- details: toErrorDetails(error),
957
- diagnostics: diagnostics.buildDiagnostics(),
958
- timings: diagnostics.buildTimings(elapsedMs2(startedAt)),
959
- sandbox
960
- });
961
- }
962
- } catch (error) {
963
- return buildRecallEvalErrorResponse({
964
- request,
965
- code: "internal_error",
966
- message: "Recall eval execution failed unexpectedly.",
967
- details: toErrorDetails(error),
968
- diagnostics: diagnostics.buildDiagnostics(),
969
- timings: diagnostics.buildTimings(elapsedMs2(startedAt)),
970
- sandbox
971
- });
972
- } finally {
973
- await sandbox?.cleanup().catch(() => void 0);
974
- }
975
- }
976
- function toErrorDetails(error) {
977
- if (error instanceof Error) {
978
- return {
979
- cause: error.message
980
- };
981
- }
982
- return {
983
- cause: String(error)
984
- };
985
- }
986
- function createUnavailableEmbeddingPort(message) {
987
- return {
988
- async embed() {
989
- throw new Error(message);
990
- }
991
- };
992
- }
993
- function applyRecallEvalFaultInjection(ports, request) {
994
- if (request.options?.faultInjection?.vectorSearchFailure !== true) {
995
- return ports;
996
- }
997
- return {
998
- async embed(text) {
999
- return ports.embed(text);
1000
- },
1001
- async vectorSearch() {
1002
- throw new Error("Injected recall eval vector search failure.");
1003
- },
1004
- async ftsSearch(params) {
1005
- return ports.ftsSearch(params);
1006
- },
1007
- ...ports.fetchPredecessors ? {
1008
- async fetchPredecessors(params) {
1009
- return ports.fetchPredecessors(params);
1010
- }
1011
- } : {},
1012
- async hydrateEntries(ids) {
1013
- return ports.hydrateEntries(ids);
1014
- },
1015
- async recordRecallEvents(params) {
1016
- return ports.recordRecallEvents(params);
1017
- }
1018
- };
1019
- }
1020
- function elapsedMs2(startedAt) {
1021
- return Math.max(0, Date.now() - startedAt);
1022
- }
1023
-
1024
- // src/adapters/api/validation/recall-eval-request.ts
1025
- var ROOT_REQUEST_KEYS = /* @__PURE__ */ new Set(["caseId", "description", "recallPath", "sandbox", "memoryPool", "recallRequest", "unified", "options"]);
1026
- var SANDBOX_REQUEST_KEYS = /* @__PURE__ */ new Set(["root", "preserve"]);
1027
- var FIXTURE_ENTRY_KEYS = /* @__PURE__ */ new Set([
1028
- "id",
1029
- "type",
1030
- "subject",
1031
- "content",
1032
- "importance",
1033
- "expiry",
1034
- "tags",
1035
- "source_file",
1036
- "source_context",
1037
- "created_at",
1038
- "updated_at",
1039
- "retired",
1040
- "retired_at",
1041
- "retired_reason",
1042
- "superseded_by",
1043
- "claim_key",
1044
- "claim_key_status",
1045
- "claim_key_source",
1046
- "claim_support_source_kind",
1047
- "claim_support_locator",
1048
- "claim_support_observed_at",
1049
- "claim_support_mode",
1050
- "valid_from",
1051
- "valid_to",
1052
- "supersession_kind",
1053
- "supersession_reason"
1054
- ]);
1055
- var RECALL_REQUEST_KEYS = /* @__PURE__ */ new Set([
1056
- "text",
1057
- "limit",
1058
- "threshold",
1059
- "budget",
1060
- "types",
1061
- "tags",
1062
- "since",
1063
- "until",
1064
- "around",
1065
- "aroundRadius",
1066
- "asOf",
1067
- "rankingProfile"
1068
- ]);
1069
- var UNIFIED_REQUEST_KEYS = /* @__PURE__ */ new Set(["mode", "sessionKey", "memoryPolicy"]);
1070
- var UNIFIED_MEMORY_POLICY_KEYS = /* @__PURE__ */ new Set(["slotPolicies"]);
1071
- var SLOT_POLICY_KEYS = /* @__PURE__ */ new Set(["attributeHeads"]);
1072
- var OPTIONS_KEYS = /* @__PURE__ */ new Set(["includeDiagnostics", "includeCandidates", "includeTimings", "faultInjection"]);
1073
- var FAULT_INJECTION_KEYS = /* @__PURE__ */ new Set(["queryEmbeddingFailure", "vectorSearchFailure"]);
1074
- var RECALL_PATHS = ["core", "unified"];
1075
- var RECALL_RANKING_PROFILES = ["historical_state"];
1076
- var UNIFIED_RECALL_MODES = ["auto", "entries", "episodes"];
1077
- var CLAIM_SLOT_POLICIES = ["exclusive", "multivalued"];
1078
- var RecallEvalRequestValidationError = class extends Error {
1079
- /** Parseable case identifier echoed for invalid request correlation when available. */
1080
- caseId;
1081
- /** Structured list of request validation issues. */
1082
- issues;
1083
- /**
1084
- * Creates a request validation error with stable issue details.
1085
- *
1086
- * @param issues - Structured validation issues collected during parsing.
1087
- * @param caseId - Parseable request case identifier when available.
1088
- */
1089
- constructor(issues, caseId) {
1090
- super("Invalid recall eval request.");
1091
- this.name = "RecallEvalRequestValidationError";
1092
- this.issues = issues;
1093
- this.caseId = caseId;
1094
- }
1095
- };
1096
- function parseRecallEvalCaseRequest(input) {
1097
- const caseId = extractParseableCaseId(input);
1098
- if (!isRecord(input)) {
1099
- throw new RecallEvalRequestValidationError(
1100
- [
1101
- {
1102
- path: "$",
1103
- message: "Request body must be a JSON object."
1104
- }
1105
- ],
1106
- caseId
1107
- );
1108
- }
1109
- const issues = [];
1110
- pushUnexpectedFields(input, ROOT_REQUEST_KEYS, "", issues);
1111
- const parsedCaseId = parseRequiredTrimmedString(input.caseId, "caseId", issues);
1112
- const description = parseOptionalTrimmedString(input.description, "description", issues);
1113
- const recallPath = parseOptionalRecallPath(input.recallPath, "recallPath", issues);
1114
- const sandbox = parseSandbox(input.sandbox, issues);
1115
- const memoryPool = parseMemoryPool(input.memoryPool, issues);
1116
- const recallRequest = parseRecallRequest(input.recallRequest, issues);
1117
- const unified = parseUnifiedRequest(input.unified, issues);
1118
- const options = parseOptions(input.options, issues);
1119
- validatePathSpecificRequest(recallPath, recallRequest, unified, issues);
1120
- if (issues.length > 0 || parsedCaseId === void 0 || memoryPool === void 0 || recallRequest === void 0) {
1121
- throw new RecallEvalRequestValidationError(issues, caseId);
1122
- }
1123
- return {
1124
- caseId: parsedCaseId,
1125
- description,
1126
- recallPath,
1127
- sandbox,
1128
- memoryPool,
1129
- recallRequest,
1130
- unified,
1131
- options
1132
- };
1133
- }
1134
- function mapRecallEvalCaseRequestDto(dto) {
1135
- return {
1136
- caseId: dto.caseId,
1137
- description: dto.description,
1138
- recallPath: dto.recallPath,
1139
- sandbox: mapSandboxRequestDto(dto.sandbox),
1140
- memoryPool: dto.memoryPool.map(mapFixtureEntryDto),
1141
- recallRequest: mapRecallRequestDto(dto.recallRequest),
1142
- unified: mapUnifiedRequestDto(dto.unified),
1143
- options: mapCaseOptionsDto(dto.options)
1144
- };
1145
- }
1146
- function extractParseableCaseId(value) {
1147
- if (!isRecord(value) || typeof value.caseId !== "string") {
1148
- return void 0;
1149
- }
1150
- const normalized = value.caseId.trim();
1151
- return normalized.length > 0 ? normalized : void 0;
1152
- }
1153
- function parseSandbox(value, issues) {
1154
- if (value === void 0) {
1155
- return void 0;
1156
- }
1157
- const sandbox = parseObject(value, "sandbox", issues);
1158
- if (sandbox === void 0) {
1159
- return void 0;
1160
- }
1161
- pushUnexpectedFields(sandbox, SANDBOX_REQUEST_KEYS, "sandbox", issues);
1162
- return {
1163
- root: parseOptionalTrimmedString(sandbox.root, "sandbox.root", issues),
1164
- preserve: parseOptionalBoolean(sandbox.preserve, "sandbox.preserve", issues)
1165
- };
1166
- }
1167
- function parseMemoryPool(value, issues) {
1168
- if (!Array.isArray(value)) {
1169
- pushIssue(issues, "memoryPool", "Expected an array of fixture entries.");
1170
- return void 0;
1171
- }
1172
- return value.flatMap((entry, index) => {
1173
- const parsed = parseFixtureEntry(entry, index, issues);
1174
- return parsed ? [parsed] : [];
1175
- });
1176
- }
1177
- function parseFixtureEntry(value, index, issues) {
1178
- const basePath = `memoryPool[${index}]`;
1179
- const fixture = parseObject(value, basePath, issues);
1180
- if (fixture === void 0) {
1181
- return void 0;
1182
- }
1183
- pushUnexpectedFields(fixture, FIXTURE_ENTRY_KEYS, basePath, issues);
1184
- const type = parseEntryType(fixture.type, `${basePath}.type`, issues);
1185
- const subject = parseRequiredTrimmedString(fixture.subject, `${basePath}.subject`, issues);
1186
- const content = parseRequiredTrimmedString(fixture.content, `${basePath}.content`, issues);
1187
- if (type === void 0 || subject === void 0 || content === void 0) {
1188
- return void 0;
1189
- }
1190
- return {
1191
- id: parseOptionalTrimmedString(fixture.id, `${basePath}.id`, issues),
1192
- type,
1193
- subject,
1194
- content,
1195
- importance: parseOptionalIntegerInRange(fixture.importance, `${basePath}.importance`, issues, {
1196
- min: 1,
1197
- max: 10
1198
- }),
1199
- expiry: parseOptionalExpiry(fixture.expiry, `${basePath}.expiry`, issues),
1200
- tags: parseOptionalStringArray(fixture.tags, `${basePath}.tags`, issues),
1201
- source_file: parseOptionalTrimmedString(fixture.source_file, `${basePath}.source_file`, issues),
1202
- source_context: parseOptionalTrimmedString(fixture.source_context, `${basePath}.source_context`, issues),
1203
- created_at: parseOptionalTimestampString(fixture.created_at, `${basePath}.created_at`, issues),
1204
- updated_at: parseOptionalTimestampString(fixture.updated_at, `${basePath}.updated_at`, issues),
1205
- retired: parseOptionalBoolean(fixture.retired, `${basePath}.retired`, issues),
1206
- retired_at: parseOptionalTimestampString(fixture.retired_at, `${basePath}.retired_at`, issues),
1207
- retired_reason: parseOptionalTrimmedString(fixture.retired_reason, `${basePath}.retired_reason`, issues),
1208
- superseded_by: parseOptionalTrimmedString(fixture.superseded_by, `${basePath}.superseded_by`, issues),
1209
- claim_key: parseOptionalTrimmedString(fixture.claim_key, `${basePath}.claim_key`, issues),
1210
- claim_key_status: parseOptionalClaimKeyStatus(fixture.claim_key_status, `${basePath}.claim_key_status`, issues),
1211
- claim_key_source: parseOptionalClaimKeySource(fixture.claim_key_source, `${basePath}.claim_key_source`, issues),
1212
- claim_support_source_kind: parseOptionalTrimmedString(fixture.claim_support_source_kind, `${basePath}.claim_support_source_kind`, issues),
1213
- claim_support_locator: parseOptionalTrimmedString(fixture.claim_support_locator, `${basePath}.claim_support_locator`, issues),
1214
- claim_support_observed_at: parseOptionalTimestampString(fixture.claim_support_observed_at, `${basePath}.claim_support_observed_at`, issues),
1215
- claim_support_mode: parseOptionalClaimSupportMode(fixture.claim_support_mode, `${basePath}.claim_support_mode`, issues),
1216
- valid_from: parseOptionalTimestampString(fixture.valid_from, `${basePath}.valid_from`, issues),
1217
- valid_to: parseOptionalTimestampString(fixture.valid_to, `${basePath}.valid_to`, issues),
1218
- supersession_kind: parseOptionalTrimmedString(fixture.supersession_kind, `${basePath}.supersession_kind`, issues),
1219
- supersession_reason: parseOptionalTrimmedString(fixture.supersession_reason, `${basePath}.supersession_reason`, issues)
1220
- };
1221
- }
1222
- function parseRecallRequest(value, issues) {
1223
- const recallRequest = parseObject(value, "recallRequest", issues);
1224
- if (recallRequest === void 0) {
1225
- return void 0;
1226
- }
1227
- pushUnexpectedFields(recallRequest, RECALL_REQUEST_KEYS, "recallRequest", issues);
1228
- const text = parseRequiredTrimmedString(recallRequest.text, "recallRequest.text", issues);
1229
- if (text === void 0) {
1230
- return void 0;
1231
- }
1232
- return {
1233
- text,
1234
- limit: parseOptionalIntegerInRange(recallRequest.limit, "recallRequest.limit", issues, {
1235
- min: 0
1236
- }),
1237
- threshold: parseOptionalThreshold(recallRequest.threshold, "recallRequest.threshold", issues),
1238
- budget: parseOptionalIntegerInRange(recallRequest.budget, "recallRequest.budget", issues, {
1239
- min: 0
1240
- }),
1241
- types: parseOptionalEntryTypeArray(recallRequest.types, "recallRequest.types", issues),
1242
- tags: parseOptionalStringArray(recallRequest.tags, "recallRequest.tags", issues),
1243
- since: parseOptionalTrimmedString(recallRequest.since, "recallRequest.since", issues),
1244
- until: parseOptionalTrimmedString(recallRequest.until, "recallRequest.until", issues),
1245
- around: parseOptionalTrimmedString(recallRequest.around, "recallRequest.around", issues),
1246
- aroundRadius: parseOptionalIntegerInRange(recallRequest.aroundRadius, "recallRequest.aroundRadius", issues, {
1247
- min: 1
1248
- }),
1249
- asOf: parseOptionalTrimmedString(recallRequest.asOf, "recallRequest.asOf", issues),
1250
- rankingProfile: parseOptionalRankingProfile(recallRequest.rankingProfile, "recallRequest.rankingProfile", issues)
1251
- };
1252
- }
1253
- function parseUnifiedRequest(value, issues) {
1254
- if (value === void 0) {
1255
- return void 0;
1256
- }
1257
- const unified = parseObject(value, "unified", issues);
1258
- if (unified === void 0) {
1259
- return void 0;
1260
- }
1261
- pushUnexpectedFields(unified, UNIFIED_REQUEST_KEYS, "unified", issues);
1262
- return {
1263
- mode: parseOptionalUnifiedRecallMode(unified.mode, "unified.mode", issues),
1264
- sessionKey: parseOptionalTrimmedString(unified.sessionKey, "unified.sessionKey", issues),
1265
- memoryPolicy: parseUnifiedMemoryPolicy(unified.memoryPolicy, issues)
1266
- };
1267
- }
1268
- function parseOptions(value, issues) {
1269
- if (value === void 0) {
1270
- return void 0;
1271
- }
1272
- const options = parseObject(value, "options", issues);
1273
- if (options === void 0) {
1274
- return void 0;
1275
- }
1276
- pushUnexpectedFields(options, OPTIONS_KEYS, "options", issues);
1277
- return {
1278
- includeDiagnostics: parseOptionalBoolean(options.includeDiagnostics, "options.includeDiagnostics", issues),
1279
- includeCandidates: parseOptionalBoolean(options.includeCandidates, "options.includeCandidates", issues),
1280
- includeTimings: parseOptionalBoolean(options.includeTimings, "options.includeTimings", issues),
1281
- faultInjection: parseFaultInjection(options.faultInjection, issues)
1282
- };
1283
- }
1284
- function parseFaultInjection(value, issues) {
1285
- if (value === void 0) {
1286
- return void 0;
1287
- }
1288
- const faultInjection = parseObject(value, "options.faultInjection", issues);
1289
- if (faultInjection === void 0) {
1290
- return void 0;
1291
- }
1292
- pushUnexpectedFields(faultInjection, FAULT_INJECTION_KEYS, "options.faultInjection", issues);
1293
- return {
1294
- queryEmbeddingFailure: parseOptionalBoolean(faultInjection.queryEmbeddingFailure, "options.faultInjection.queryEmbeddingFailure", issues),
1295
- vectorSearchFailure: parseOptionalBoolean(faultInjection.vectorSearchFailure, "options.faultInjection.vectorSearchFailure", issues)
1296
- };
1297
- }
1298
- function parseUnifiedMemoryPolicy(value, issues) {
1299
- if (value === void 0) {
1300
- return void 0;
1301
- }
1302
- const memoryPolicy = parseObject(value, "unified.memoryPolicy", issues);
1303
- if (memoryPolicy === void 0) {
1304
- return void 0;
1305
- }
1306
- pushUnexpectedFields(memoryPolicy, UNIFIED_MEMORY_POLICY_KEYS, "unified.memoryPolicy", issues);
1307
- return {
1308
- slotPolicies: parseClaimSlotPolicyConfig(memoryPolicy.slotPolicies, "unified.memoryPolicy.slotPolicies", issues)
1309
- };
1310
- }
1311
- function parseEntryType(value, path2, issues) {
1312
- if (typeof value !== "string" || !ENTRY_TYPES.includes(value)) {
1313
- pushIssue(issues, path2, `Expected one of: ${ENTRY_TYPES.join(", ")}.`);
1314
- return void 0;
1315
- }
1316
- return value;
1317
- }
1318
- function parseOptionalRecallPath(value, path2, issues) {
1319
- if (value === void 0) {
1320
- return void 0;
1321
- }
1322
- if (typeof value !== "string" || !RECALL_PATHS.includes(value)) {
1323
- pushIssue(issues, path2, `Expected one of: ${RECALL_PATHS.join(", ")}.`);
1324
- return void 0;
1325
- }
1326
- return value;
1327
- }
1328
- function parseOptionalUnifiedRecallMode(value, path2, issues) {
1329
- if (value === void 0) {
1330
- return void 0;
1331
- }
1332
- if (typeof value !== "string" || !UNIFIED_RECALL_MODES.includes(value)) {
1333
- pushIssue(issues, path2, `Expected one of: ${UNIFIED_RECALL_MODES.join(", ")}.`);
1334
- return void 0;
1335
- }
1336
- return value;
1337
- }
1338
- function parseOptionalRankingProfile(value, path2, issues) {
1339
- if (value === void 0) {
1340
- return void 0;
1341
- }
1342
- if (typeof value !== "string" || !RECALL_RANKING_PROFILES.includes(value)) {
1343
- pushIssue(issues, path2, `Expected one of: ${RECALL_RANKING_PROFILES.join(", ")}.`);
1344
- return void 0;
1345
- }
1346
- return value;
1347
- }
1348
- function parseOptionalExpiry(value, path2, issues) {
1349
- if (value === void 0) {
1350
- return void 0;
1351
- }
1352
- if (typeof value !== "string" || !EXPIRY_LEVELS.includes(value)) {
1353
- pushIssue(issues, path2, `Expected one of: ${EXPIRY_LEVELS.join(", ")}.`);
1354
- return void 0;
1355
- }
1356
- return value;
1357
- }
1358
- function parseOptionalClaimKeyStatus(value, path2, issues) {
1359
- if (value === void 0) {
1360
- return void 0;
1361
- }
1362
- if (typeof value !== "string" || !CLAIM_KEY_STATUSES.includes(value)) {
1363
- pushIssue(issues, path2, `Expected one of: ${CLAIM_KEY_STATUSES.join(", ")}.`);
1364
- return void 0;
1365
- }
1366
- return value;
1367
- }
1368
- function parseOptionalClaimKeySource(value, path2, issues) {
1369
- if (value === void 0) {
1370
- return void 0;
1371
- }
1372
- if (typeof value !== "string" || !CLAIM_KEY_SOURCES.includes(value)) {
1373
- pushIssue(issues, path2, `Expected one of: ${CLAIM_KEY_SOURCES.join(", ")}.`);
1374
- return void 0;
1375
- }
1376
- return value;
1377
- }
1378
- function parseOptionalClaimSupportMode(value, path2, issues) {
1379
- if (value === void 0) {
1380
- return void 0;
1381
- }
1382
- if (typeof value !== "string" || !CLAIM_SUPPORT_MODES.includes(value)) {
1383
- pushIssue(issues, path2, `Expected one of: ${CLAIM_SUPPORT_MODES.join(", ")}.`);
1384
- return void 0;
1385
- }
1386
- return value;
1387
- }
1388
- function parseOptionalStringArray(value, path2, issues) {
1389
- if (value === void 0) {
1390
- return void 0;
1391
- }
1392
- if (!Array.isArray(value) || value.some((item) => typeof item !== "string")) {
1393
- pushIssue(issues, path2, "Expected an array of strings.");
1394
- return void 0;
1395
- }
1396
- return value.map((item) => item.trim()).filter((item) => item.length > 0);
1397
- }
1398
- function parseOptionalEntryTypeArray(value, path2, issues) {
1399
- if (value === void 0) {
1400
- return void 0;
1401
- }
1402
- if (!Array.isArray(value)) {
1403
- pushIssue(issues, path2, "Expected an array.");
1404
- return void 0;
1405
- }
1406
- const parsed = [];
1407
- for (const [index, item] of value.entries()) {
1408
- const entryType = parseEntryType(item, `${path2}[${index}]`, issues);
1409
- if (entryType !== void 0) {
1410
- parsed.push(entryType);
1411
- }
1412
- }
1413
- return parsed;
1414
- }
1415
- function parseOptionalThreshold(value, path2, issues) {
1416
- if (value === void 0) {
1417
- return void 0;
1418
- }
1419
- if (typeof value !== "number" || Number.isNaN(value) || value < 0 || value > 1) {
1420
- pushIssue(issues, path2, "Expected a number from 0 to 1.");
1421
- return void 0;
1422
- }
1423
- return value;
1424
- }
1425
- function parseClaimSlotPolicyConfig(value, path2, issues) {
1426
- if (value === void 0) {
1427
- return void 0;
1428
- }
1429
- const config = parseObject(value, path2, issues);
1430
- if (config === void 0) {
1431
- return void 0;
1432
- }
1433
- pushUnexpectedFields(config, SLOT_POLICY_KEYS, path2, issues);
1434
- const attributeHeads = parseClaimSlotPolicyAttributeHeads(config.attributeHeads, `${path2}.attributeHeads`, issues);
1435
- return attributeHeads ? { attributeHeads } : void 0;
1436
- }
1437
- function parseClaimSlotPolicyAttributeHeads(value, path2, issues) {
1438
- if (value === void 0) {
1439
- return void 0;
1440
- }
1441
- if (!isRecord(value)) {
1442
- pushIssue(issues, path2, "Expected an object.");
1443
- return void 0;
1444
- }
1445
- const normalized = {};
1446
- for (const [rawKey, rawValue] of Object.entries(value)) {
1447
- const attributeHead = rawKey.trim().toLowerCase();
1448
- if (!/^[a-z0-9][a-z0-9_-]*$/.test(attributeHead)) {
1449
- pushIssue(issues, `${path2}.${rawKey}`, "Expected a canonical attribute-head label.");
1450
- continue;
1451
- }
1452
- if (typeof rawValue !== "string" || !CLAIM_SLOT_POLICIES.includes(rawValue)) {
1453
- pushIssue(issues, `${path2}.${attributeHead}`, `Expected one of: ${CLAIM_SLOT_POLICIES.join(", ")}.`);
1454
- continue;
1455
- }
1456
- normalized[attributeHead] = rawValue;
1457
- }
1458
- return Object.keys(normalized).length > 0 ? normalized : void 0;
1459
- }
1460
- function parseObject(value, path2, issues) {
1461
- if (!isRecord(value)) {
1462
- pushIssue(issues, path2, "Expected an object.");
1463
- return void 0;
1464
- }
1465
- return value;
1466
- }
1467
- function validatePathSpecificRequest(recallPath, recallRequest, unified, issues) {
1468
- const effectivePath = recallPath ?? "core";
1469
- if (effectivePath !== "unified") {
1470
- if (unified !== void 0) {
1471
- pushIssue(issues, "unified", 'The "unified" block is only allowed when recallPath is "unified".');
1472
- }
1473
- return;
1474
- }
1475
- if (recallRequest === void 0) {
1476
- return;
1477
- }
1478
- if (recallRequest.budget !== void 0) {
1479
- pushIssue(issues, "recallRequest.budget", 'This field is only supported when recallPath is "core".');
1480
- }
1481
- if (recallRequest.since !== void 0) {
1482
- pushIssue(issues, "recallRequest.since", 'This field is only supported when recallPath is "core".');
1483
- }
1484
- if (recallRequest.until !== void 0) {
1485
- pushIssue(issues, "recallRequest.until", 'This field is only supported when recallPath is "core".');
1486
- }
1487
- if (recallRequest.around !== void 0) {
1488
- pushIssue(issues, "recallRequest.around", 'This field is only supported when recallPath is "core".');
1489
- }
1490
- if (recallRequest.aroundRadius !== void 0) {
1491
- pushIssue(issues, "recallRequest.aroundRadius", 'This field is only supported when recallPath is "core".');
1492
- }
1493
- if (recallRequest.rankingProfile !== void 0) {
1494
- pushIssue(issues, "recallRequest.rankingProfile", 'This field is derived by unified recall and cannot be supplied when recallPath is "unified".');
1495
- }
1496
- }
1497
- function mapSandboxRequestDto(dto) {
1498
- if (dto === void 0) {
1499
- return void 0;
1500
- }
1501
- return {
1502
- root: dto.root,
1503
- preserve: dto.preserve
1504
- };
1505
- }
1506
- function mapFixtureEntryDto(dto) {
1507
- return {
1508
- id: dto.id,
1509
- type: dto.type,
1510
- subject: dto.subject,
1511
- content: dto.content,
1512
- importance: dto.importance,
1513
- expiry: dto.expiry,
1514
- tags: dto.tags,
1515
- source_file: dto.source_file,
1516
- source_context: dto.source_context,
1517
- created_at: dto.created_at,
1518
- updated_at: dto.updated_at,
1519
- retired: dto.retired,
1520
- retired_at: dto.retired_at,
1521
- retired_reason: dto.retired_reason,
1522
- superseded_by: dto.superseded_by,
1523
- claim_key: dto.claim_key,
1524
- claim_key_status: dto.claim_key_status,
1525
- claim_key_source: dto.claim_key_source,
1526
- claim_support_source_kind: dto.claim_support_source_kind,
1527
- claim_support_locator: dto.claim_support_locator,
1528
- claim_support_observed_at: dto.claim_support_observed_at,
1529
- claim_support_mode: dto.claim_support_mode,
1530
- valid_from: dto.valid_from,
1531
- valid_to: dto.valid_to,
1532
- supersession_kind: dto.supersession_kind,
1533
- supersession_reason: dto.supersession_reason
1534
- };
1535
- }
1536
- function mapRecallRequestDto(dto) {
1537
- return {
1538
- text: dto.text,
1539
- limit: dto.limit,
1540
- threshold: dto.threshold,
1541
- budget: dto.budget,
1542
- types: dto.types,
1543
- tags: dto.tags,
1544
- since: dto.since,
1545
- until: dto.until,
1546
- around: dto.around,
1547
- aroundRadius: dto.aroundRadius,
1548
- asOf: dto.asOf,
1549
- rankingProfile: dto.rankingProfile
1550
- };
1551
- }
1552
- function mapUnifiedRequestDto(dto) {
1553
- if (dto === void 0) {
1554
- return void 0;
1555
- }
1556
- return {
1557
- mode: dto.mode,
1558
- sessionKey: dto.sessionKey,
1559
- memoryPolicy: dto.memoryPolicy !== void 0 ? {
1560
- slotPolicies: dto.memoryPolicy.slotPolicies
1561
- } : void 0
1562
- };
1563
- }
1564
- function mapCaseOptionsDto(dto) {
1565
- if (dto === void 0) {
1566
- return void 0;
1567
- }
1568
- return {
1569
- includeDiagnostics: dto.includeDiagnostics,
1570
- includeCandidates: dto.includeCandidates,
1571
- includeTimings: dto.includeTimings,
1572
- faultInjection: dto.faultInjection
1573
- };
1574
- }
1575
-
1576
- // src/adapters/api/routes/internal-recall-eval.ts
1577
- var INTERNAL_RECALL_EVAL_ROUTE = {
1578
- method: "POST",
1579
- path: "/internal/evals/recall/run"
1580
- };
1581
- function createInternalRecallEvalRoute(runner = runRecallEvalCase) {
1582
- return {
1583
- ...INTERNAL_RECALL_EVAL_ROUTE,
1584
- handler: async (request) => {
1585
- let validatedRequest;
1586
- try {
1587
- validatedRequest = await parseValidatedRequest(request);
1588
- const result = await runner(validatedRequest);
1589
- return jsonResponse(result, 200);
1590
- } catch (error) {
1591
- if (error instanceof RecallEvalRequestValidationError) {
1592
- return jsonResponse(
1593
- {
1594
- status: "error",
1595
- caseId: error.caseId,
1596
- error: {
1597
- code: "invalid_request",
1598
- message: error.message,
1599
- details: error.issues
1600
- }
1601
- },
1602
- 400
1603
- );
1604
- }
1605
- return jsonResponse(
1606
- {
1607
- status: "error",
1608
- caseId: validatedRequest?.caseId,
1609
- error: {
1610
- code: "internal_error",
1611
- message: "Internal recall eval adapter error."
1612
- }
1613
- },
1614
- 500
1615
- );
1616
- }
1617
- }
1618
- };
1619
- }
1620
- var parseJsonBody = async (request) => {
1621
- try {
1622
- return await request.json();
1623
- } catch {
1624
- throw new RecallEvalRequestValidationError([
1625
- {
1626
- path: "$",
1627
- message: "Request body must be valid JSON."
1628
- }
1629
- ]);
1630
- }
1631
- };
1632
- var parseValidatedRequest = async (request) => {
1633
- const payload = await parseJsonBody(request);
1634
- const requestDto = parseRecallEvalCaseRequest(payload);
1635
- return mapRecallEvalCaseRequestDto(requestDto);
1636
- };
1637
- var jsonResponse = (body, status) => new Response(JSON.stringify(body), {
1638
- status,
1639
- headers: {
1640
- "content-type": "application/json; charset=utf-8"
1641
- }
1642
- });
1643
-
1644
- // src/adapters/api/internal-recall-eval-server.ts
1645
- var DEFAULT_INTERNAL_RECALL_EVAL_HOST = "127.0.0.1";
1646
- var DEFAULT_INTERNAL_RECALL_EVAL_PORT = 4010;
1647
- async function startInternalRecallEvalServer(options = {}) {
1648
- const host2 = options.host ?? DEFAULT_INTERNAL_RECALL_EVAL_HOST;
1649
- const port2 = options.port ?? DEFAULT_INTERNAL_RECALL_EVAL_PORT;
1650
- const route = createInternalRecallEvalRoute(options.runner);
1651
- const server2 = createServer((request, response) => {
1652
- void handleRequest(request, response, route, host2, port2).catch(() => {
1653
- if (response.headersSent !== true) {
1654
- writeTextResponse(response, 500, "Internal server error.\n");
1655
- return;
1656
- }
1657
- response.destroy();
1658
- });
1659
- });
1660
- await listen(server2, port2, host2);
1661
- const address = server2.address();
1662
- if (address === null || typeof address === "string") {
1663
- await closeServer(server2);
1664
- throw new Error("Internal recall eval server did not expose a TCP address.");
1665
- }
1666
- return {
1667
- host: host2,
1668
- port: address.port,
1669
- routePath: route.path,
1670
- baseUrl: `http://${formatHostForUrl(host2)}:${address.port}`,
1671
- close: async () => {
1672
- await closeServer(server2);
1673
- }
1674
- };
1675
- }
1676
- var handleRequest = async (request, response, route, fallbackHost, fallbackPort) => {
1677
- const requestUrl = new URL(request.url ?? "/", `http://${formatHostForUrl(fallbackHost)}:${request.socket.localPort ?? fallbackPort}`);
1678
- if (requestUrl.pathname !== route.path) {
1679
- writeTextResponse(response, 404, "Not found.\n");
1680
- return;
1681
- }
1682
- if (request.method !== route.method) {
1683
- response.statusCode = 405;
1684
- response.setHeader("allow", route.method);
1685
- response.end();
1686
- return;
1687
- }
1688
- const body = await readBody(request);
1689
- const routeRequest = new Request(requestUrl, {
1690
- method: route.method,
1691
- headers: toHeaders(request),
1692
- body: body.length > 0 ? body : void 0
1693
- });
1694
- const routeResponse = await route.handler(routeRequest);
1695
- await writeRouteResponse(response, routeResponse);
1696
- };
1697
- var readBody = async (request) => {
1698
- const chunks = [];
1699
- for await (const chunk of request) {
1700
- chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
1701
- }
1702
- return Buffer.concat(chunks);
1703
- };
1704
- var toHeaders = (request) => {
1705
- const headers = new Headers();
1706
- for (const [name, value] of Object.entries(request.headers)) {
1707
- if (value === void 0) {
1708
- continue;
1709
- }
1710
- if (Array.isArray(value)) {
1711
- for (const item of value) {
1712
- headers.append(name, item);
1713
- }
1714
- continue;
1715
- }
1716
- headers.set(name, value);
1717
- }
1718
- return headers;
1719
- };
1720
- var writeRouteResponse = async (response, routeResponse) => {
1721
- response.statusCode = routeResponse.status;
1722
- for (const [name, value] of routeResponse.headers) {
1723
- response.setHeader(name, value);
1724
- }
1725
- const body = Buffer.from(await routeResponse.arrayBuffer());
1726
- response.end(body);
1727
- };
1728
- var writeTextResponse = (response, status, body) => {
1729
- response.statusCode = status;
1730
- response.setHeader("content-type", "text/plain; charset=utf-8");
1731
- response.end(body);
1732
- };
1733
- var listen = async (server2, port2, host2) => {
1734
- await new Promise((resolve, reject) => {
1735
- const onError = (error) => {
1736
- server2.off("listening", onListening);
1737
- reject(error);
1738
- };
1739
- const onListening = () => {
1740
- server2.off("error", onError);
1741
- resolve();
1742
- };
1743
- server2.once("error", onError);
1744
- server2.once("listening", onListening);
1745
- server2.listen(port2, host2);
1746
- });
1747
- };
1748
- var closeServer = async (server2) => {
1749
- if (server2.listening !== true) {
1750
- return;
1751
- }
1752
- await new Promise((resolve, reject) => {
1753
- server2.close((error) => {
1754
- if (error) {
1755
- reject(error);
1756
- return;
1757
- }
1758
- resolve();
1759
- });
1760
- });
1761
- };
1762
- var formatHostForUrl = (host2) => {
1763
- if (host2.includes(":") && host2.startsWith("[") !== true) {
1764
- return `[${host2}]`;
1765
- }
1766
- return host2;
1767
- };
1768
-
1769
- // src/internal-recall-eval-server.ts
1770
- var HOST_ENV_NAME = "AGENR_INTERNAL_RECALL_EVAL_HOST";
1771
- var PORT_ENV_NAME = "AGENR_INTERNAL_RECALL_EVAL_PORT";
1772
- var host = resolveHost(process.env[HOST_ENV_NAME]);
1773
- var port = resolvePort(process.env[PORT_ENV_NAME]);
1774
- var server = await startInternalRecallEvalServer({ host, port });
1775
- console.log(`Internal recall eval dev server listening at ${server.baseUrl}${server.routePath}`);
1776
- console.log("Serving only the existing internal recall eval route for local agenr-evals runs.");
1777
- installSignalHandler("SIGINT");
1778
- installSignalHandler("SIGTERM");
1779
- function installSignalHandler(signal) {
1780
- process.once(signal, () => {
1781
- void shutdown(signal);
1782
- });
1783
- }
1784
- var shuttingDown = false;
1785
- async function shutdown(signal) {
1786
- if (shuttingDown === true) {
1787
- return;
1788
- }
1789
- shuttingDown = true;
1790
- console.log(`Received ${signal}. Shutting down internal recall eval dev server.`);
1791
- try {
1792
- await server.close();
1793
- } finally {
1794
- process.exit(0);
1795
- }
1796
- }
1797
- function resolveHost(value) {
1798
- const trimmed = value?.trim();
1799
- if (!trimmed) {
1800
- return DEFAULT_INTERNAL_RECALL_EVAL_HOST;
1801
- }
1802
- return trimmed;
1803
- }
1804
- function resolvePort(value) {
1805
- const trimmed = value?.trim();
1806
- if (!trimmed) {
1807
- return DEFAULT_INTERNAL_RECALL_EVAL_PORT;
1808
- }
1809
- if (/^\d+$/u.test(trimmed) !== true) {
1810
- throw new Error(`${PORT_ENV_NAME} must be an integer between 0 and 65535.`);
1811
- }
1812
- const parsed = Number.parseInt(trimmed, 10);
1813
- if (!Number.isInteger(parsed) || parsed < 0 || parsed > 65535) {
1814
- throw new Error(`${PORT_ENV_NAME} must be an integer between 0 and 65535.`);
1815
- }
1816
- return parsed;
1817
- }
2
+ import "./chunk-P5SB75FK.js";
3
+ import "./chunk-ZYADFKX3.js";
4
+ import "./chunk-GELCEVFA.js";
5
+ import "./chunk-575MUIW5.js";
6
+ import "./chunk-ELR2HSVC.js";
7
+ import "./chunk-5LADPJ4C.js";