@tangle-network/agent-eval 0.27.2 → 0.28.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 (42) hide show
  1. package/dist/{baseline-4R5deP0N.d.ts → baseline-BwdCXUS8.d.ts} +1 -1
  2. package/dist/builder-eval/index.d.ts +3 -3
  3. package/dist/chunk-UW4NOOZI.js +1561 -0
  4. package/dist/chunk-UW4NOOZI.js.map +1 -0
  5. package/dist/{control-BT4qnXiS.d.ts → control-rJhEDdpy.d.ts} +4 -4
  6. package/dist/{control-runtime-BZ_lVLYW.d.ts → control-runtime-BRdQ0wrx.d.ts} +2 -2
  7. package/dist/control.d.ts +5 -5
  8. package/dist/{emitter-DP_cSSiw.d.ts → emitter-BqjeOvJh.d.ts} +1 -1
  9. package/dist/{failure-cluster-Cw65_5FY.d.ts → failure-cluster-D1NZKqYu.d.ts} +1 -1
  10. package/dist/{feedback-trajectory-D1aGKusy.d.ts → feedback-trajectory-j0nJFgC6.d.ts} +1 -1
  11. package/dist/governance/index.d.ts +2 -2
  12. package/dist/{index-BhLlu-qO.d.ts → index-Cgt3DKXr.d.ts} +1 -1
  13. package/dist/index.d.ts +1156 -335
  14. package/dist/index.js +1532 -489
  15. package/dist/index.js.map +1 -1
  16. package/dist/{integrity-DK2EBVZC.d.ts → integrity-BAxLGJ9I.d.ts} +2 -2
  17. package/dist/knowledge/index.d.ts +3 -3
  18. package/dist/meta-eval/index.d.ts +1 -1
  19. package/dist/{multi-layer-verifier-U-c8ge1k.d.ts → multi-layer-verifier-BNi4-8lR.d.ts} +1 -1
  20. package/dist/openapi.json +1 -1
  21. package/dist/optimization.d.ts +8 -8
  22. package/dist/pipelines/index.d.ts +6 -6
  23. package/dist/prm/index.d.ts +4 -4
  24. package/dist/{query-DODUYdPg.d.ts → query-BFDT0kX_.d.ts} +1 -1
  25. package/dist/{release-report-CCQqnK46.d.ts → release-report-PWhGlpfO.d.ts} +1 -1
  26. package/dist/replay-BX5Fm8en.d.ts +529 -0
  27. package/dist/reporting.d.ts +4 -4
  28. package/dist/{researcher-G81CWc0q.d.ts → researcher-ClDX3KZx.d.ts} +5 -5
  29. package/dist/rl.d.ts +8 -8
  30. package/dist/{rubric-D5tjHNJQ.d.ts → rubric-DgSqjqqj.d.ts} +2 -2
  31. package/dist/{store-Db2Bv8Cf.d.ts → store-BP5be6s7.d.ts} +1 -1
  32. package/dist/{summary-report-Dl4akLKX.d.ts → summary-report-jrSGb2xZ.d.ts} +1 -1
  33. package/dist/{test-graded-scenario-B2kWEdh9.d.ts → test-graded-scenario-BJ54PDan.d.ts} +2 -2
  34. package/dist/traces.d.ts +9 -311
  35. package/dist/traces.js +15 -986
  36. package/dist/traces.js.map +1 -1
  37. package/dist/{trajectory-CnoBo-JY.d.ts → trajectory-BFmveYZt.d.ts} +1 -1
  38. package/dist/wire/index.d.ts +4 -4
  39. package/package.json +1 -1
  40. package/dist/chunk-4U4BKCXK.js +0 -569
  41. package/dist/chunk-4U4BKCXK.js.map +0 -1
  42. package/dist/replay-D7z0J43-.d.ts +0 -225
@@ -1,569 +0,0 @@
1
- import {
2
- canonicalize,
3
- hashJson
4
- } from "./chunk-VSMTAMNK.js";
5
- import {
6
- ReplayError
7
- } from "./chunk-NG236HPC.js";
8
-
9
- // src/trace/store.ts
10
- var InMemoryTraceStore = class {
11
- runs = /* @__PURE__ */ new Map();
12
- allSpans = [];
13
- allEvents = [];
14
- allArtifacts = [];
15
- allBudget = [];
16
- async appendRun(run) {
17
- if (this.runs.has(run.runId)) throw new Error(`run ${run.runId} already exists`);
18
- this.runs.set(run.runId, { ...run });
19
- }
20
- async updateRun(runId, patch) {
21
- const existing = this.runs.get(runId);
22
- if (!existing) throw new Error(`run ${runId} not found`);
23
- this.runs.set(runId, { ...existing, ...patch });
24
- }
25
- async appendSpan(span) {
26
- this.allSpans.push({ ...span });
27
- }
28
- async updateSpan(spanId, patch) {
29
- const idx = this.allSpans.findIndex((s) => s.spanId === spanId);
30
- if (idx < 0) throw new Error(`span ${spanId} not found`);
31
- this.allSpans[idx] = { ...this.allSpans[idx], ...patch };
32
- }
33
- async appendEvent(event) {
34
- this.allEvents.push({ ...event });
35
- }
36
- async appendArtifact(artifact) {
37
- this.allArtifacts.push({ ...artifact });
38
- }
39
- async appendBudgetEntry(entry) {
40
- this.allBudget.push({ ...entry });
41
- }
42
- async getRun(runId) {
43
- const r = this.runs.get(runId);
44
- return r ? { ...r } : void 0;
45
- }
46
- async listRuns(filter = {}) {
47
- return [...this.runs.values()].filter((r) => matchesRun(r, filter));
48
- }
49
- async spans(filter = {}) {
50
- return this.allSpans.filter((s) => matchesSpan(s, filter)).map((s) => ({ ...s }));
51
- }
52
- async events(filter = {}) {
53
- return this.allEvents.filter((e) => matchesEvent(e, filter)).map((e) => ({ ...e }));
54
- }
55
- async budget(runId) {
56
- return this.allBudget.filter((b) => b.runId === runId).map((b) => ({ ...b }));
57
- }
58
- async artifacts(runId) {
59
- return this.allArtifacts.filter((a) => a.runId === runId).map((a) => ({ ...a }));
60
- }
61
- };
62
- function matchesRun(r, f) {
63
- if (f.scenarioId && r.scenarioId !== f.scenarioId) return false;
64
- if (f.variantId && r.variantId !== f.variantId) return false;
65
- if (f.status && r.status !== f.status) return false;
66
- if (f.since !== void 0 && r.startedAt < f.since) return false;
67
- if (f.until !== void 0 && r.startedAt > f.until) return false;
68
- if (f.tag && r.tags?.[f.tag.key] !== f.tag.value) return false;
69
- if (f.parentRunId && r.parentRunId !== f.parentRunId) return false;
70
- if (f.projectId && r.projectId !== f.projectId) return false;
71
- if (f.chatId && r.chatId !== f.chatId) return false;
72
- if (f.layer && r.layer !== f.layer) return false;
73
- return true;
74
- }
75
- function matchesSpan(s, f) {
76
- if (f.runId && s.runId !== f.runId) return false;
77
- if (f.parentSpanId && s.parentSpanId !== f.parentSpanId) return false;
78
- if (f.kind && s.kind !== f.kind) return false;
79
- if (f.name && s.name !== f.name) return false;
80
- if (f.toolName && (s.kind !== "tool" || s.toolName !== f.toolName)) return false;
81
- if (f.judgeId && (s.kind !== "judge" || s.judgeId !== f.judgeId)) return false;
82
- if (f.since !== void 0 && s.startedAt < f.since) return false;
83
- if (f.until !== void 0 && s.startedAt > f.until) return false;
84
- return true;
85
- }
86
- function matchesEvent(e, f) {
87
- if (f.runId && e.runId !== f.runId) return false;
88
- if (f.spanId && e.spanId !== f.spanId) return false;
89
- if (f.kind && e.kind !== f.kind) return false;
90
- if (f.since !== void 0 && e.timestamp < f.since) return false;
91
- if (f.until !== void 0 && e.timestamp > f.until) return false;
92
- return true;
93
- }
94
- var FileSystemTraceStore = class {
95
- dir;
96
- maxBytes;
97
- /** Lazy in-memory index for queries — populated on first read. */
98
- index;
99
- loaded = false;
100
- constructor(options) {
101
- this.dir = options.dir;
102
- this.maxBytes = options.maxBytes ?? 32 * 1024 * 1024;
103
- }
104
- async ensureDir() {
105
- const fs = await import("fs/promises");
106
- await fs.mkdir(this.dir, { recursive: true });
107
- }
108
- async append(name, record) {
109
- await this.ensureDir();
110
- const fs = await import("fs/promises");
111
- const path = await import("path");
112
- const active = path.join(this.dir, `${name}.ndjson`);
113
- try {
114
- const stat = await fs.stat(active);
115
- if (stat.size >= this.maxBytes) {
116
- const rolled = path.join(this.dir, `${name}.${Date.now()}.ndjson`);
117
- await fs.rename(active, rolled);
118
- }
119
- } catch {
120
- }
121
- await fs.appendFile(active, `${JSON.stringify(record)}
122
- `, "utf8");
123
- if (this.index && !record?._update) {
124
- void this.insertInto(name, record);
125
- }
126
- }
127
- async insertInto(name, record) {
128
- if (!this.index) return;
129
- switch (name) {
130
- case "runs":
131
- await this.index.appendRun(record);
132
- break;
133
- case "spans":
134
- await this.index.appendSpan(record);
135
- break;
136
- case "events":
137
- await this.index.appendEvent(record);
138
- break;
139
- case "artifacts":
140
- await this.index.appendArtifact(record);
141
- break;
142
- case "budget":
143
- await this.index.appendBudgetEntry(record);
144
- break;
145
- }
146
- }
147
- async load() {
148
- if (this.loaded && this.index) return this.index;
149
- const fs = await import("fs/promises");
150
- const path = await import("path");
151
- const store = new InMemoryTraceStore();
152
- try {
153
- const entries = await fs.readdir(this.dir);
154
- for (const file of entries) {
155
- if (!file.endsWith(".ndjson")) continue;
156
- const full = path.join(this.dir, file);
157
- const content = await fs.readFile(full, "utf8");
158
- const base = file.split(".")[0];
159
- for (const line of content.split("\n")) {
160
- if (!line.trim()) continue;
161
- const record = JSON.parse(line);
162
- if (base === "runs") {
163
- try {
164
- await store.appendRun(record);
165
- } catch {
166
- await store.updateRun(record.runId, record);
167
- }
168
- } else if (base === "spans") {
169
- if (record?._update) {
170
- try {
171
- await store.updateSpan(record.spanId, record);
172
- } catch {
173
- await store.appendSpan(record);
174
- }
175
- } else {
176
- await store.appendSpan(record);
177
- }
178
- } else if (base === "events") {
179
- await store.appendEvent(record);
180
- } else if (base === "artifacts") {
181
- await store.appendArtifact(record);
182
- } else if (base === "budget") {
183
- await store.appendBudgetEntry(record);
184
- }
185
- }
186
- }
187
- } catch {
188
- }
189
- this.index = store;
190
- this.loaded = true;
191
- return store;
192
- }
193
- async appendRun(run) {
194
- await this.append("runs", run);
195
- }
196
- async updateRun(runId, patch) {
197
- await this.append("runs", { runId, ...patch, _update: true });
198
- if (this.index) await this.index.updateRun(runId, patch);
199
- }
200
- async appendSpan(span) {
201
- await this.append("spans", span);
202
- }
203
- async updateSpan(spanId, patch) {
204
- await this.append("spans", { spanId, ...patch, _update: true });
205
- if (this.index) await this.index.updateSpan(spanId, patch);
206
- }
207
- async appendEvent(event) {
208
- await this.append("events", event);
209
- }
210
- async appendArtifact(artifact) {
211
- await this.append("artifacts", artifact);
212
- }
213
- async appendBudgetEntry(entry) {
214
- await this.append("budget", entry);
215
- }
216
- async getRun(runId) {
217
- return (await this.load()).getRun(runId);
218
- }
219
- async listRuns(filter) {
220
- return (await this.load()).listRuns(filter);
221
- }
222
- async spans(filter) {
223
- return (await this.load()).spans(filter);
224
- }
225
- async events(filter) {
226
- return (await this.load()).events(filter);
227
- }
228
- async budget(runId) {
229
- return (await this.load()).budget(runId);
230
- }
231
- async artifacts(runId) {
232
- return (await this.load()).artifacts(runId);
233
- }
234
- };
235
-
236
- // src/trace/otel.ts
237
- var OTEL_AGENT_EVAL_SCOPE = { name: "@tangle-network/agent-eval", version: "0.3.0" };
238
- async function exportRunAsOtlp(store, runId, resourceAttrs = {}) {
239
- const run = await store.getRun(runId);
240
- if (!run) throw new Error(`run ${runId} not found`);
241
- const spans = await store.spans({ runId });
242
- const events = await store.events({ runId });
243
- const eventsBySpan = /* @__PURE__ */ new Map();
244
- for (const e of events) {
245
- if (!e.spanId) continue;
246
- const arr = eventsBySpan.get(e.spanId) ?? [];
247
- arr.push(e);
248
- eventsBySpan.set(e.spanId, arr);
249
- }
250
- const traceId = runToTraceId(run);
251
- const otlpSpans = spans.map(
252
- (s) => spanToOtlp(s, traceId, eventsBySpan.get(s.spanId) ?? [])
253
- );
254
- return {
255
- resourceSpans: [
256
- {
257
- resource: {
258
- attributes: toAttributes({
259
- "service.name": "agent-eval",
260
- "run.id": run.runId,
261
- "run.scenario_id": run.scenarioId,
262
- "run.variant_id": run.variantId ?? "",
263
- "run.dataset_version": run.datasetVersion ?? "",
264
- "run.code_sha": run.codeSha ?? "",
265
- "run.model_fingerprint": run.modelFingerprint ?? "",
266
- ...resourceAttrs
267
- })
268
- },
269
- scopeSpans: [{ scope: OTEL_AGENT_EVAL_SCOPE, spans: otlpSpans }]
270
- }
271
- ]
272
- };
273
- }
274
- function spanToOtlp(span, traceId, events) {
275
- const endedAt = span.endedAt ?? span.startedAt;
276
- return {
277
- traceId,
278
- spanId: padSpanId(span.spanId),
279
- parentSpanId: span.parentSpanId ? padSpanId(span.parentSpanId) : void 0,
280
- name: span.name,
281
- kind: 1,
282
- // SPAN_KIND_INTERNAL
283
- startTimeUnixNano: msToNs(span.startedAt),
284
- endTimeUnixNano: msToNs(endedAt),
285
- attributes: toAttributes(flattenSpanAttributes(span)),
286
- events: events.map((e) => ({
287
- timeUnixNano: msToNs(e.timestamp),
288
- name: e.kind,
289
- attributes: toAttributes(flattenPayload(e.payload))
290
- })),
291
- status: span.status === "error" ? { code: 2, message: span.error } : { code: 1 }
292
- };
293
- }
294
- function flattenSpanAttributes(span) {
295
- const base = {
296
- "span.kind": span.kind
297
- };
298
- if (span.kind === "llm") {
299
- base["llm.model"] = span.model;
300
- if (span.inputTokens !== void 0) base["llm.input_tokens"] = span.inputTokens;
301
- if (span.outputTokens !== void 0) base["llm.output_tokens"] = span.outputTokens;
302
- if (span.costUsd !== void 0) base["llm.cost_usd"] = span.costUsd;
303
- if (span.finishReason) base["llm.finish_reason"] = span.finishReason;
304
- } else if (span.kind === "tool") {
305
- base["tool.name"] = span.toolName;
306
- if (span.latencyMs !== void 0) base["tool.latency_ms"] = span.latencyMs;
307
- } else if (span.kind === "retrieval") {
308
- base["retrieval.query"] = span.query;
309
- base["retrieval.hits"] = span.hits.length;
310
- } else if (span.kind === "judge") {
311
- base["judge.id"] = span.judgeId;
312
- base["judge.dimension"] = span.dimension;
313
- base["judge.score"] = span.score;
314
- base["judge.target_span_id"] = span.targetSpanId;
315
- } else if (span.kind === "sandbox") {
316
- if (span.image) base["sandbox.image"] = span.image;
317
- if (span.exitCode !== void 0) base["sandbox.exit_code"] = span.exitCode;
318
- if (span.testsPassed !== void 0) base["sandbox.tests_passed"] = span.testsPassed;
319
- if (span.testsTotal !== void 0) base["sandbox.tests_total"] = span.testsTotal;
320
- }
321
- if (span.attributes) {
322
- for (const [k, v] of Object.entries(span.attributes)) {
323
- if (typeof v === "string" || typeof v === "number" || typeof v === "boolean") base[k] = v;
324
- }
325
- }
326
- return base;
327
- }
328
- function flattenPayload(payload) {
329
- const out = {};
330
- for (const [k, v] of Object.entries(payload)) {
331
- if (typeof v === "string" || typeof v === "number" || typeof v === "boolean") out[k] = v;
332
- else out[k] = JSON.stringify(v);
333
- }
334
- return out;
335
- }
336
- function toAttributes(record) {
337
- return Object.entries(record).map(([key, value]) => ({
338
- key,
339
- value: typeof value === "number" ? Number.isInteger(value) ? { intValue: value.toString() } : { doubleValue: value } : typeof value === "boolean" ? { boolValue: value } : { stringValue: value }
340
- }));
341
- }
342
- function msToNs(ms) {
343
- return (BigInt(Math.floor(ms)) * 1000000n).toString();
344
- }
345
- function padSpanId(id) {
346
- const cleaned = id.replace(/-/g, "");
347
- return cleaned.slice(0, 16).padEnd(16, "0");
348
- }
349
- function runToTraceId(run) {
350
- const cleaned = run.runId.replace(/-/g, "");
351
- return cleaned.slice(0, 32).padEnd(32, "0");
352
- }
353
-
354
- // src/trace/redact.ts
355
- var DEFAULT_REDACTION_RULES = [
356
- { id: "email", pattern: /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/gi },
357
- { id: "ssn", pattern: /\b\d{3}-\d{2}-\d{4}\b/g },
358
- { id: "credit-card", pattern: /\b(?:\d[ -]*?){13,16}\b/g },
359
- { id: "phone-us", pattern: /\b(?:\+?1[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}\b/g },
360
- { id: "ipv4", pattern: /\b(?:\d{1,3}\.){3}\d{1,3}\b/g },
361
- { id: "aws-access-key", pattern: /\bAKIA[0-9A-Z]{16}\b/g },
362
- { id: "bearer", pattern: /\bBearer\s+[A-Za-z0-9._~+/=-]{10,}/gi },
363
- { id: "sk-key", pattern: /\bsk-[A-Za-z0-9_-]{10,}\b/g },
364
- {
365
- id: "private-key-block",
366
- pattern: /-----BEGIN (?:RSA |EC |OPENSSH |DSA )?PRIVATE KEY-----[\s\S]*?-----END[^-]*-----/g
367
- }
368
- ];
369
- var REDACTION_VERSION = "1.0.0";
370
- function redactString(input, rules = DEFAULT_REDACTION_RULES) {
371
- const byRule = {};
372
- let redactionCount = 0;
373
- let output = input;
374
- for (const rule of rules) {
375
- let hits = 0;
376
- output = output.replace(rule.pattern, () => {
377
- hits++;
378
- return rule.replacement ?? `[redacted:${rule.id}]`;
379
- });
380
- if (hits > 0) {
381
- byRule[rule.id] = hits;
382
- redactionCount += hits;
383
- }
384
- }
385
- return { output, report: { redactionCount, byRule } };
386
- }
387
- function redactValue(value, rules = DEFAULT_REDACTION_RULES, report = { redactionCount: 0, byRule: {} }) {
388
- if (typeof value === "string") {
389
- const { output, report: r } = redactString(value, rules);
390
- report.redactionCount += r.redactionCount;
391
- for (const [k, v] of Object.entries(r.byRule)) {
392
- report.byRule[k] = (report.byRule[k] ?? 0) + v;
393
- }
394
- return { value: output, report };
395
- }
396
- if (Array.isArray(value)) {
397
- return {
398
- value: value.map((v) => redactValue(v, rules, report).value),
399
- report
400
- };
401
- }
402
- if (value !== null && typeof value === "object") {
403
- const next = {};
404
- for (const [k, v] of Object.entries(value)) {
405
- next[k] = redactValue(v, rules, report).value;
406
- }
407
- return { value: next, report };
408
- }
409
- return { value, report };
410
- }
411
-
412
- // src/replay.ts
413
- var ReplayCacheMissError = class extends ReplayError {
414
- constructor(url, requestKey2, message) {
415
- super(message ?? `replay cache miss for ${url} (key=${requestKey2})`);
416
- this.url = url;
417
- this.requestKey = requestKey2;
418
- }
419
- url;
420
- requestKey;
421
- };
422
- var ReplayCache = class _ReplayCache {
423
- byKey = /* @__PURE__ */ new Map();
424
- orphans = 0;
425
- byProvider = {};
426
- byModel = {};
427
- /**
428
- * Build a cache from a sink's events. The sink must implement `list()`.
429
- * Filter by `runId` / `spanId` to scope to a specific replay.
430
- */
431
- static async fromSink(sink, filter = {}) {
432
- if (!sink.list) {
433
- throw new ReplayError("ReplayCache.fromSink: sink must implement list() to be replayable.");
434
- }
435
- const events = await sink.list(filter);
436
- return _ReplayCache.fromEvents(events);
437
- }
438
- /** Build a cache from an in-memory event list. */
439
- static async fromEvents(events) {
440
- const cache = new _ReplayCache();
441
- const groups = /* @__PURE__ */ new Map();
442
- for (const e of events) {
443
- const k = `${e.runId ?? ""}::${e.spanId ?? ""}::${e.attemptIndex}`;
444
- const g = groups.get(k) ?? {};
445
- if (e.direction === "request") g.req = e;
446
- else g.res = e;
447
- groups.set(k, g);
448
- }
449
- for (const g of groups.values()) {
450
- if (!g.req) continue;
451
- if (!g.res) {
452
- cache.orphans += 1;
453
- continue;
454
- }
455
- const key = await requestKey(g.req);
456
- cache.byKey.set(key, { request: g.req, response: g.res });
457
- cache.byProvider[g.req.provider] = (cache.byProvider[g.req.provider] ?? 0) + 1;
458
- cache.byModel[g.req.model] = (cache.byModel[g.req.model] ?? 0) + 1;
459
- }
460
- return cache;
461
- }
462
- /** Number of cacheable (request, response) pairs in the cache. */
463
- size() {
464
- return this.byKey.size;
465
- }
466
- stats() {
467
- return {
468
- total: this.byKey.size,
469
- byProvider: { ...this.byProvider },
470
- byModel: { ...this.byModel },
471
- orphanRequests: this.orphans
472
- };
473
- }
474
- /** Iterate every cached `(request, response)` pair in insertion order. */
475
- *entries() {
476
- for (const entry of this.byKey.values()) yield entry;
477
- }
478
- /**
479
- * Look up a cached response by hashing the (model, messages, temperature,
480
- * maxTokens, response_format) shape. Returns `undefined` on miss; the
481
- * caller decides whether to throw, fall back to the network, or skip.
482
- */
483
- async lookup(requestBody) {
484
- const key = await keyFromBody(requestBody);
485
- return this.byKey.get(key);
486
- }
487
- };
488
- function createReplayFetch(cache, opts = {}) {
489
- const onMiss = opts.onMiss ?? "throw";
490
- const fallback = opts.fallbackFetch ?? globalThis.fetch?.bind(globalThis);
491
- return (async (input, init) => {
492
- const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
493
- if (!/\/chat\/completions(?:[?#].*)?$/.test(url)) {
494
- if (!fallback)
495
- throw new ReplayError(
496
- `replay fetch: non-completions URL ${url} but no fallbackFetch configured`
497
- );
498
- return fallback(input, init);
499
- }
500
- let bodyParsed;
501
- if (init?.body && typeof init.body === "string") {
502
- try {
503
- bodyParsed = JSON.parse(init.body);
504
- } catch {
505
- }
506
- }
507
- const hit = bodyParsed === void 0 ? void 0 : await cache.lookup(bodyParsed);
508
- if (hit) {
509
- opts.onHit?.({ url, provider: hit.request.provider, model: hit.request.model });
510
- const status = hit.response.statusCode ?? 200;
511
- const headers = new Headers(
512
- Object.entries(hit.response.responseHeaders ?? { "Content-Type": "application/json" })
513
- );
514
- const bodyText = typeof hit.response.responseBody === "string" ? hit.response.responseBody : JSON.stringify(hit.response.responseBody ?? {});
515
- return new Response(bodyText, { status, headers });
516
- }
517
- opts.onMissNotify?.({ url, requestBody: bodyParsed });
518
- if (onMiss === "throw") {
519
- const key = bodyParsed === void 0 ? "<unparseable>" : await keyFromBody(bodyParsed);
520
- throw new ReplayCacheMissError(url, key);
521
- }
522
- if (onMiss === "fail-closed") {
523
- return new Response(JSON.stringify({ error: "replay_cache_miss" }), { status: 599 });
524
- }
525
- if (!fallback)
526
- throw new ReplayError("replay fetch: onMiss=fallback but no fallbackFetch configured");
527
- return fallback(input, init);
528
- });
529
- }
530
- async function* iterateRawCalls(sink, filter = {}) {
531
- if (!sink.list) {
532
- throw new ReplayError("iterateRawCalls: sink must implement list().");
533
- }
534
- const events = await sink.list(filter);
535
- const cache = await ReplayCache.fromEvents(events);
536
- for (const entry of cache.entries()) yield entry;
537
- }
538
- async function requestKey(event) {
539
- return keyFromBody(event.requestBody);
540
- }
541
- async function keyFromBody(body) {
542
- if (body == null || typeof body !== "object") return hashJson({ raw: String(body) });
543
- const b = body;
544
- const reduced = canonicalize({
545
- model: b.model ?? null,
546
- messages: b.messages ?? null,
547
- temperature: b.temperature ?? null,
548
- max_tokens: b.max_tokens ?? null,
549
- max_completion_tokens: b.max_completion_tokens ?? null,
550
- response_format: b.response_format ?? null
551
- });
552
- return hashJson(reduced);
553
- }
554
-
555
- export {
556
- InMemoryTraceStore,
557
- FileSystemTraceStore,
558
- OTEL_AGENT_EVAL_SCOPE,
559
- exportRunAsOtlp,
560
- DEFAULT_REDACTION_RULES,
561
- REDACTION_VERSION,
562
- redactString,
563
- redactValue,
564
- ReplayCacheMissError,
565
- ReplayCache,
566
- createReplayFetch,
567
- iterateRawCalls
568
- };
569
- //# sourceMappingURL=chunk-4U4BKCXK.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/trace/store.ts","../src/trace/otel.ts","../src/trace/redact.ts","../src/replay.ts"],"sourcesContent":["/**\n * TraceStore — persistence + query over the TraceSchema v1 corpus.\n *\n * Two implementations ship in the core:\n * - InMemoryTraceStore: dev + tests, fully in-process\n * - FileSystemTraceStore: NDJSON append-only files per entity, suitable\n * for long-running CI jobs; rolled over at 32MB\n *\n * Downstream adapters (DuckDB, Langfuse, R2 parquet) implement this same\n * interface — the rest of the framework is storage-agnostic.\n */\n\nimport type {\n Artifact,\n BudgetLedgerEntry,\n EventKind,\n Run,\n RunStatus,\n Span,\n SpanKind,\n TraceEvent,\n} from './schema'\n\nexport interface RunFilter {\n scenarioId?: string\n variantId?: string\n status?: RunStatus\n since?: number\n until?: number\n tag?: { key: string; value: string }\n parentRunId?: string\n projectId?: string\n chatId?: string\n layer?: import('./schema').RunLayer\n}\n\nexport interface SpanFilter {\n runId?: string\n parentSpanId?: string\n kind?: SpanKind\n name?: string\n toolName?: string\n judgeId?: string\n since?: number\n until?: number\n}\n\nexport interface EventFilter {\n runId?: string\n spanId?: string\n kind?: EventKind\n since?: number\n until?: number\n}\n\nexport interface TraceStore {\n appendRun(run: Run): Promise<void>\n updateRun(runId: string, patch: Partial<Run>): Promise<void>\n appendSpan(span: Span): Promise<void>\n updateSpan(spanId: string, patch: Partial<Span>): Promise<void>\n appendEvent(event: TraceEvent): Promise<void>\n appendArtifact(artifact: Artifact): Promise<void>\n appendBudgetEntry(entry: BudgetLedgerEntry): Promise<void>\n\n getRun(runId: string): Promise<Run | undefined>\n listRuns(filter?: RunFilter): Promise<Run[]>\n spans(filter?: SpanFilter): Promise<Span[]>\n events(filter?: EventFilter): Promise<TraceEvent[]>\n budget(runId: string): Promise<BudgetLedgerEntry[]>\n artifacts(runId: string): Promise<Artifact[]>\n}\n\n// ── In-memory ────────────────────────────────────────────────────────\n\nexport class InMemoryTraceStore implements TraceStore {\n private runs = new Map<string, Run>()\n private allSpans: Span[] = []\n private allEvents: TraceEvent[] = []\n private allArtifacts: Artifact[] = []\n private allBudget: BudgetLedgerEntry[] = []\n\n async appendRun(run: Run): Promise<void> {\n if (this.runs.has(run.runId)) throw new Error(`run ${run.runId} already exists`)\n this.runs.set(run.runId, { ...run })\n }\n\n async updateRun(runId: string, patch: Partial<Run>): Promise<void> {\n const existing = this.runs.get(runId)\n if (!existing) throw new Error(`run ${runId} not found`)\n this.runs.set(runId, { ...existing, ...patch })\n }\n\n async appendSpan(span: Span): Promise<void> {\n this.allSpans.push({ ...span })\n }\n\n async updateSpan(spanId: string, patch: Partial<Span>): Promise<void> {\n const idx = this.allSpans.findIndex((s) => s.spanId === spanId)\n if (idx < 0) throw new Error(`span ${spanId} not found`)\n this.allSpans[idx] = { ...this.allSpans[idx], ...patch } as Span\n }\n\n async appendEvent(event: TraceEvent): Promise<void> {\n this.allEvents.push({ ...event })\n }\n\n async appendArtifact(artifact: Artifact): Promise<void> {\n this.allArtifacts.push({ ...artifact })\n }\n\n async appendBudgetEntry(entry: BudgetLedgerEntry): Promise<void> {\n this.allBudget.push({ ...entry })\n }\n\n async getRun(runId: string): Promise<Run | undefined> {\n const r = this.runs.get(runId)\n return r ? { ...r } : undefined\n }\n\n async listRuns(filter: RunFilter = {}): Promise<Run[]> {\n return [...this.runs.values()].filter((r) => matchesRun(r, filter))\n }\n\n async spans(filter: SpanFilter = {}): Promise<Span[]> {\n return this.allSpans.filter((s) => matchesSpan(s, filter)).map((s) => ({ ...s }))\n }\n\n async events(filter: EventFilter = {}): Promise<TraceEvent[]> {\n return this.allEvents.filter((e) => matchesEvent(e, filter)).map((e) => ({ ...e }))\n }\n\n async budget(runId: string): Promise<BudgetLedgerEntry[]> {\n return this.allBudget.filter((b) => b.runId === runId).map((b) => ({ ...b }))\n }\n\n async artifacts(runId: string): Promise<Artifact[]> {\n return this.allArtifacts.filter((a) => a.runId === runId).map((a) => ({ ...a }))\n }\n}\n\nfunction matchesRun(r: Run, f: RunFilter): boolean {\n if (f.scenarioId && r.scenarioId !== f.scenarioId) return false\n if (f.variantId && r.variantId !== f.variantId) return false\n if (f.status && r.status !== f.status) return false\n if (f.since !== undefined && r.startedAt < f.since) return false\n if (f.until !== undefined && r.startedAt > f.until) return false\n if (f.tag && r.tags?.[f.tag.key] !== f.tag.value) return false\n if (f.parentRunId && r.parentRunId !== f.parentRunId) return false\n if (f.projectId && r.projectId !== f.projectId) return false\n if (f.chatId && r.chatId !== f.chatId) return false\n if (f.layer && r.layer !== f.layer) return false\n return true\n}\n\nfunction matchesSpan(s: Span, f: SpanFilter): boolean {\n if (f.runId && s.runId !== f.runId) return false\n if (f.parentSpanId && s.parentSpanId !== f.parentSpanId) return false\n if (f.kind && s.kind !== f.kind) return false\n if (f.name && s.name !== f.name) return false\n if (f.toolName && (s.kind !== 'tool' || s.toolName !== f.toolName)) return false\n if (f.judgeId && (s.kind !== 'judge' || s.judgeId !== f.judgeId)) return false\n if (f.since !== undefined && s.startedAt < f.since) return false\n if (f.until !== undefined && s.startedAt > f.until) return false\n return true\n}\n\nfunction matchesEvent(e: TraceEvent, f: EventFilter): boolean {\n if (f.runId && e.runId !== f.runId) return false\n if (f.spanId && e.spanId !== f.spanId) return false\n if (f.kind && e.kind !== f.kind) return false\n if (f.since !== undefined && e.timestamp < f.since) return false\n if (f.until !== undefined && e.timestamp > f.until) return false\n return true\n}\n\n// ── Filesystem (NDJSON append-only, one file per entity) ─────────────\n\nexport interface FileSystemTraceStoreOptions {\n dir: string\n /** Roll over NDJSON files when they exceed this size in bytes. Default 32 MB. */\n maxBytes?: number\n}\n\nexport class FileSystemTraceStore implements TraceStore {\n private dir: string\n private maxBytes: number\n /** Lazy in-memory index for queries — populated on first read. */\n private index?: InMemoryTraceStore\n private loaded = false\n\n constructor(options: FileSystemTraceStoreOptions) {\n this.dir = options.dir\n this.maxBytes = options.maxBytes ?? 32 * 1024 * 1024\n }\n\n private async ensureDir(): Promise<void> {\n const fs = await import('node:fs/promises')\n await fs.mkdir(this.dir, { recursive: true })\n }\n\n private async append(name: string, record: unknown): Promise<void> {\n await this.ensureDir()\n const fs = await import('node:fs/promises')\n const path = await import('node:path')\n const active = path.join(this.dir, `${name}.ndjson`)\n try {\n const stat = await fs.stat(active)\n if (stat.size >= this.maxBytes) {\n const rolled = path.join(this.dir, `${name}.${Date.now()}.ndjson`)\n await fs.rename(active, rolled)\n }\n } catch {\n /* file doesn't exist yet */\n }\n await fs.appendFile(active, `${JSON.stringify(record)}\\n`, 'utf8')\n // Mirror genuinely-new rows into the lazy index. Update rows (marked\n // with `_update: true` by updateRun/updateSpan) are applied by those\n // methods directly via the index's update* APIs — re-inserting them\n // here triggers a duplicate-id error once the first read populates\n // the index.\n if (this.index && !(record as { _update?: boolean })?._update) {\n void this.insertInto(name, record)\n }\n }\n\n private async insertInto(name: string, record: unknown): Promise<void> {\n if (!this.index) return\n switch (name) {\n case 'runs':\n await this.index.appendRun(record as Run)\n break\n case 'spans':\n await this.index.appendSpan(record as Span)\n break\n case 'events':\n await this.index.appendEvent(record as TraceEvent)\n break\n case 'artifacts':\n await this.index.appendArtifact(record as Artifact)\n break\n case 'budget':\n await this.index.appendBudgetEntry(record as BudgetLedgerEntry)\n break\n }\n }\n\n private async load(): Promise<InMemoryTraceStore> {\n if (this.loaded && this.index) return this.index\n const fs = await import('node:fs/promises')\n const path = await import('node:path')\n const store = new InMemoryTraceStore()\n try {\n const entries = await fs.readdir(this.dir)\n for (const file of entries) {\n if (!file.endsWith('.ndjson')) continue\n const full = path.join(this.dir, file)\n const content = await fs.readFile(full, 'utf8')\n const base = file.split('.')[0]\n for (const line of content.split('\\n')) {\n if (!line.trim()) continue\n const record = JSON.parse(line)\n if (base === 'runs') {\n // Allow re-loading without duplicate error\n try {\n await store.appendRun(record)\n } catch {\n await store.updateRun(record.runId, record)\n }\n } else if (base === 'spans') {\n // `updateSpan` appends an `_update: true` patch row instead of\n // rewriting the original span. On reload we must collapse those\n // patches onto the original span — otherwise a fresh\n // FileSystemTraceStore reading the same dir reports duplicate\n // spans (one full, one fragment with no runId/kind/name), which\n // breaks any downstream consumer that re-opens the store\n // cross-process (e.g. the OTLP converter).\n if (record?._update) {\n try {\n await store.updateSpan(record.spanId, record)\n } catch {\n // Patch row arrived before the original — should not happen\n // with locked append order, but fall through to append so we\n // don't lose data.\n await store.appendSpan(record)\n }\n } else {\n await store.appendSpan(record)\n }\n } else if (base === 'events') {\n await store.appendEvent(record)\n } else if (base === 'artifacts') {\n await store.appendArtifact(record)\n } else if (base === 'budget') {\n await store.appendBudgetEntry(record)\n }\n }\n }\n } catch {\n /* empty dir, first run */\n }\n this.index = store\n this.loaded = true\n return store\n }\n\n async appendRun(run: Run): Promise<void> {\n await this.append('runs', run)\n }\n async updateRun(runId: string, patch: Partial<Run>): Promise<void> {\n // NDJSON is append-only; record updates as new rows with the same runId —\n // readers collapse by last-write-wins on load.\n await this.append('runs', { runId, ...patch, _update: true })\n if (this.index) await this.index.updateRun(runId, patch)\n }\n async appendSpan(span: Span): Promise<void> {\n await this.append('spans', span)\n }\n async updateSpan(spanId: string, patch: Partial<Span>): Promise<void> {\n await this.append('spans', { spanId, ...patch, _update: true })\n if (this.index) await this.index.updateSpan(spanId, patch)\n }\n async appendEvent(event: TraceEvent): Promise<void> {\n await this.append('events', event)\n }\n async appendArtifact(artifact: Artifact): Promise<void> {\n await this.append('artifacts', artifact)\n }\n async appendBudgetEntry(entry: BudgetLedgerEntry): Promise<void> {\n await this.append('budget', entry)\n }\n\n async getRun(runId: string): Promise<Run | undefined> {\n return (await this.load()).getRun(runId)\n }\n async listRuns(filter?: RunFilter): Promise<Run[]> {\n return (await this.load()).listRuns(filter)\n }\n async spans(filter?: SpanFilter): Promise<Span[]> {\n return (await this.load()).spans(filter)\n }\n async events(filter?: EventFilter): Promise<TraceEvent[]> {\n return (await this.load()).events(filter)\n }\n async budget(runId: string): Promise<BudgetLedgerEntry[]> {\n return (await this.load()).budget(runId)\n }\n async artifacts(runId: string): Promise<Artifact[]> {\n return (await this.load()).artifacts(runId)\n }\n}\n","/**\n * OpenTelemetry JSON export — maps TraceSchema v1 to OTLP/JSON so\n * traces render natively in Jaeger / Honeycomb / Langfuse / Grafana.\n *\n * Wire format only. We do NOT depend on the @opentelemetry SDK — that\n * would drag in polyfills incompatible with Workers/Edge. Consumers\n * push the JSON to their collector of choice via HTTP.\n *\n * Reference: OTLP 1.3.2 (ResourceSpans / ScopeSpans / Span).\n */\n\nimport type { Run, Span, TraceEvent } from './schema'\nimport type { TraceStore } from './store'\n\nexport const OTEL_AGENT_EVAL_SCOPE = { name: '@tangle-network/agent-eval', version: '0.3.0' }\n\nexport interface OtlpSpan {\n traceId: string\n spanId: string\n parentSpanId?: string\n name: string\n kind: number\n startTimeUnixNano: string\n endTimeUnixNano: string\n attributes: Array<{\n key: string\n value: { stringValue?: string; intValue?: string; doubleValue?: number; boolValue?: boolean }\n }>\n events?: Array<{ timeUnixNano: string; name: string; attributes?: OtlpSpan['attributes'] }>\n status?: { code: number; message?: string }\n}\n\nexport interface OtlpResourceSpans {\n resource: { attributes: OtlpSpan['attributes'] }\n scopeSpans: Array<{ scope: typeof OTEL_AGENT_EVAL_SCOPE; spans: OtlpSpan[] }>\n}\n\nexport interface OtlpExport {\n resourceSpans: OtlpResourceSpans[]\n}\n\n/** Export a single run's spans + events in OTLP/JSON. */\nexport async function exportRunAsOtlp(\n store: TraceStore,\n runId: string,\n resourceAttrs: Record<string, string | number | boolean> = {},\n): Promise<OtlpExport> {\n const run = await store.getRun(runId)\n if (!run) throw new Error(`run ${runId} not found`)\n const spans = await store.spans({ runId })\n const events = await store.events({ runId })\n const eventsBySpan = new Map<string, TraceEvent[]>()\n for (const e of events) {\n if (!e.spanId) continue\n const arr = eventsBySpan.get(e.spanId) ?? []\n arr.push(e)\n eventsBySpan.set(e.spanId, arr)\n }\n const traceId = runToTraceId(run)\n const otlpSpans: OtlpSpan[] = spans.map((s) =>\n spanToOtlp(s, traceId, eventsBySpan.get(s.spanId) ?? []),\n )\n return {\n resourceSpans: [\n {\n resource: {\n attributes: toAttributes({\n 'service.name': 'agent-eval',\n 'run.id': run.runId,\n 'run.scenario_id': run.scenarioId,\n 'run.variant_id': run.variantId ?? '',\n 'run.dataset_version': run.datasetVersion ?? '',\n 'run.code_sha': run.codeSha ?? '',\n 'run.model_fingerprint': run.modelFingerprint ?? '',\n ...resourceAttrs,\n }),\n },\n scopeSpans: [{ scope: OTEL_AGENT_EVAL_SCOPE, spans: otlpSpans }],\n },\n ],\n }\n}\n\nfunction spanToOtlp(span: Span, traceId: string, events: TraceEvent[]): OtlpSpan {\n const endedAt = span.endedAt ?? span.startedAt\n return {\n traceId,\n spanId: padSpanId(span.spanId),\n parentSpanId: span.parentSpanId ? padSpanId(span.parentSpanId) : undefined,\n name: span.name,\n kind: 1, // SPAN_KIND_INTERNAL\n startTimeUnixNano: msToNs(span.startedAt),\n endTimeUnixNano: msToNs(endedAt),\n attributes: toAttributes(flattenSpanAttributes(span)),\n events: events.map((e) => ({\n timeUnixNano: msToNs(e.timestamp),\n name: e.kind,\n attributes: toAttributes(flattenPayload(e.payload)),\n })),\n status: span.status === 'error' ? { code: 2, message: span.error } : { code: 1 },\n }\n}\n\nfunction flattenSpanAttributes(span: Span): Record<string, string | number | boolean> {\n const base: Record<string, string | number | boolean> = {\n 'span.kind': span.kind,\n }\n if (span.kind === 'llm') {\n base['llm.model'] = span.model\n if (span.inputTokens !== undefined) base['llm.input_tokens'] = span.inputTokens\n if (span.outputTokens !== undefined) base['llm.output_tokens'] = span.outputTokens\n if (span.costUsd !== undefined) base['llm.cost_usd'] = span.costUsd\n if (span.finishReason) base['llm.finish_reason'] = span.finishReason\n } else if (span.kind === 'tool') {\n base['tool.name'] = span.toolName\n if (span.latencyMs !== undefined) base['tool.latency_ms'] = span.latencyMs\n } else if (span.kind === 'retrieval') {\n base['retrieval.query'] = span.query\n base['retrieval.hits'] = span.hits.length\n } else if (span.kind === 'judge') {\n base['judge.id'] = span.judgeId\n base['judge.dimension'] = span.dimension\n base['judge.score'] = span.score\n base['judge.target_span_id'] = span.targetSpanId\n } else if (span.kind === 'sandbox') {\n if (span.image) base['sandbox.image'] = span.image\n if (span.exitCode !== undefined) base['sandbox.exit_code'] = span.exitCode\n if (span.testsPassed !== undefined) base['sandbox.tests_passed'] = span.testsPassed\n if (span.testsTotal !== undefined) base['sandbox.tests_total'] = span.testsTotal\n }\n if (span.attributes) {\n for (const [k, v] of Object.entries(span.attributes)) {\n if (typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean') base[k] = v\n }\n }\n return base\n}\n\nfunction flattenPayload(\n payload: Record<string, unknown>,\n): Record<string, string | number | boolean> {\n const out: Record<string, string | number | boolean> = {}\n for (const [k, v] of Object.entries(payload)) {\n if (typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean') out[k] = v\n else out[k] = JSON.stringify(v)\n }\n return out\n}\n\nfunction toAttributes(record: Record<string, string | number | boolean>): OtlpSpan['attributes'] {\n return Object.entries(record).map(([key, value]) => ({\n key,\n value:\n typeof value === 'number'\n ? Number.isInteger(value)\n ? { intValue: value.toString() }\n : { doubleValue: value }\n : typeof value === 'boolean'\n ? { boolValue: value }\n : { stringValue: value },\n }))\n}\n\nfunction msToNs(ms: number): string {\n return (BigInt(Math.floor(ms)) * 1_000_000n).toString()\n}\n\nfunction padSpanId(id: string): string {\n // OTLP wants 16-hex spanIds. UUIDs are 32-hex; strip dashes and take first 16.\n const cleaned = id.replace(/-/g, '')\n return cleaned.slice(0, 16).padEnd(16, '0')\n}\n\nfunction runToTraceId(run: Run): string {\n // OTLP wants 32-hex traceIds. Use runId directly when it's 32-hex already,\n // else SHA-ish truncate.\n const cleaned = run.runId.replace(/-/g, '')\n return cleaned.slice(0, 32).padEnd(32, '0')\n}\n","/**\n * Redaction — remove PII / secrets from trace payloads before persist.\n *\n * Pre-persistence rules mean raw traces in storage are already scrubbed.\n * Unredacted variants (for debugging / post-mortems) live in a separate\n * storage layer with stricter access controls; this module only covers\n * the default scrub-then-persist path.\n *\n * Rules compose: pass an array of `RedactionRule`, each is applied in\n * order. Strings that match get replaced with a tagged sentinel so the\n * eval framework can count how many redactions happened per run\n * (surfaced via `redaction_applied` events).\n */\n\nexport interface RedactionRule {\n id: string\n pattern: RegExp\n /** Replacement — e.g. '[PII:email]'. Defaults to `[redacted:{id}]`. */\n replacement?: string\n}\n\nexport interface RedactionReport {\n redactionCount: number\n byRule: Record<string, number>\n}\n\n/** OWASP / common-sense defaults — extend per-domain. */\nexport const DEFAULT_REDACTION_RULES: RedactionRule[] = [\n { id: 'email', pattern: /\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,}\\b/gi },\n { id: 'ssn', pattern: /\\b\\d{3}-\\d{2}-\\d{4}\\b/g },\n { id: 'credit-card', pattern: /\\b(?:\\d[ -]*?){13,16}\\b/g },\n { id: 'phone-us', pattern: /\\b(?:\\+?1[-.\\s]?)?\\(?\\d{3}\\)?[-.\\s]?\\d{3}[-.\\s]?\\d{4}\\b/g },\n { id: 'ipv4', pattern: /\\b(?:\\d{1,3}\\.){3}\\d{1,3}\\b/g },\n { id: 'aws-access-key', pattern: /\\bAKIA[0-9A-Z]{16}\\b/g },\n { id: 'bearer', pattern: /\\bBearer\\s+[A-Za-z0-9._~+/=-]{10,}/gi },\n { id: 'sk-key', pattern: /\\bsk-[A-Za-z0-9_-]{10,}\\b/g },\n {\n id: 'private-key-block',\n pattern: /-----BEGIN (?:RSA |EC |OPENSSH |DSA )?PRIVATE KEY-----[\\s\\S]*?-----END[^-]*-----/g,\n },\n]\n\nexport const REDACTION_VERSION = '1.0.0'\n\n/**\n * Redact a single string. Returns the new string and a per-rule count of\n * how many substitutions fired.\n */\nexport function redactString(\n input: string,\n rules: RedactionRule[] = DEFAULT_REDACTION_RULES,\n): { output: string; report: RedactionReport } {\n const byRule: Record<string, number> = {}\n let redactionCount = 0\n let output = input\n for (const rule of rules) {\n let hits = 0\n output = output.replace(rule.pattern, () => {\n hits++\n return rule.replacement ?? `[redacted:${rule.id}]`\n })\n if (hits > 0) {\n byRule[rule.id] = hits\n redactionCount += hits\n }\n }\n return { output, report: { redactionCount, byRule } }\n}\n\n/**\n * Walk a JSON-ish value applying `redactString` to every string leaf.\n * Arrays and plain objects are recursed; other types pass through\n * untouched. Circular references throw — traces should be tree-shaped.\n */\nexport function redactValue(\n value: unknown,\n rules: RedactionRule[] = DEFAULT_REDACTION_RULES,\n report: RedactionReport = { redactionCount: 0, byRule: {} },\n): { value: unknown; report: RedactionReport } {\n if (typeof value === 'string') {\n const { output, report: r } = redactString(value, rules)\n report.redactionCount += r.redactionCount\n for (const [k, v] of Object.entries(r.byRule)) {\n report.byRule[k] = (report.byRule[k] ?? 0) + v\n }\n return { value: output, report }\n }\n if (Array.isArray(value)) {\n return {\n value: value.map((v) => redactValue(v, rules, report).value),\n report,\n }\n }\n if (value !== null && typeof value === 'object') {\n const next: Record<string, unknown> = {}\n for (const [k, v] of Object.entries(value)) {\n next[k] = redactValue(v, rules, report).value\n }\n return { value: next, report }\n }\n return { value, report }\n}\n","/**\n * Replay-from-raw-events — turn every captured campaign run into a\n * re-runnable artifact.\n *\n * `RawProviderSink` captures every provider HTTP envelope; `runEvalCampaign`\n * makes that capture the default. Together they make every past run a\n * complete fingerprint of what happened on the wire — enough to replay\n * the run without burning new LLM cost.\n *\n * Three use cases this primitive enables:\n *\n * 1. **Post-hoc judging** — apply a new judge / rubric / scoring callback\n * to last week's runs without re-calling any LLM. The cost of trying\n * a new rubric drops from \"another full sweep\" to a CPU-bound replay.\n * 2. **Determinism audits** — replay the same campaign and verify the\n * raw responses match byte-for-byte. Any drift is a non-determinism\n * bug (in the harness, the prompt builder, the sandbox, …).\n * 3. **Free judge calibration** — run two judges on identical responses\n * and measure inter-judge agreement without doubling LLM spend.\n *\n * The interface is deliberately fetch-shaped. Inject `createReplayFetch`\n * into `LlmClientOptions.fetch` and every `callLlm` transparently reads\n * from the cache instead of calling the network. No new code path through\n * the LLM client is needed; the cache hit is invisible to the runner.\n */\n\nimport { ReplayError } from './errors'\nimport { canonicalize, hashJson } from './pre-registration'\nimport type { RawProviderEvent, RawProviderSink } from './trace/raw-provider-sink'\n\nexport class ReplayCacheMissError extends ReplayError {\n constructor(\n public readonly url: string,\n public readonly requestKey: string,\n message?: string,\n ) {\n super(message ?? `replay cache miss for ${url} (key=${requestKey})`)\n }\n}\n\nexport interface ReplayCacheEntry {\n request: RawProviderEvent\n response: RawProviderEvent\n}\n\nexport interface ReplayCacheStats {\n total: number\n byProvider: Record<string, number>\n byModel: Record<string, number>\n /** Spans for which we have a request but no response (run aborted mid-call). */\n orphanRequests: number\n}\n\n/**\n * In-memory deterministic cache of (request → response) keyed on a stable\n * hash of the request body. Built from a `RawProviderSink` containing\n * paired `request` and `response` events from a previous run.\n *\n * The cache is the source of truth for replay; `createReplayFetch` is a\n * thin wrapper that reads from it.\n */\nexport class ReplayCache {\n private byKey = new Map<string, ReplayCacheEntry>()\n private orphans = 0\n private byProvider: Record<string, number> = {}\n private byModel: Record<string, number> = {}\n\n /**\n * Build a cache from a sink's events. The sink must implement `list()`.\n * Filter by `runId` / `spanId` to scope to a specific replay.\n */\n static async fromSink(\n sink: RawProviderSink,\n filter: { runId?: string; spanId?: string } = {},\n ): Promise<ReplayCache> {\n if (!sink.list) {\n throw new ReplayError('ReplayCache.fromSink: sink must implement list() to be replayable.')\n }\n const events = await sink.list(filter)\n return ReplayCache.fromEvents(events)\n }\n\n /** Build a cache from an in-memory event list. */\n static async fromEvents(events: RawProviderEvent[]): Promise<ReplayCache> {\n const cache = new ReplayCache()\n // Group by (runId, spanId, attemptIndex) so request/response/error pairs are matched.\n // A cell can have many spans; each retry attempt within a span is a separate group.\n type GroupKey = string\n const groups = new Map<GroupKey, { req?: RawProviderEvent; res?: RawProviderEvent }>()\n for (const e of events) {\n const k = `${e.runId ?? ''}::${e.spanId ?? ''}::${e.attemptIndex}`\n const g = groups.get(k) ?? {}\n if (e.direction === 'request') g.req = e\n else g.res = e\n groups.set(k, g)\n }\n for (const g of groups.values()) {\n if (!g.req) continue\n if (!g.res) {\n cache.orphans += 1\n continue\n }\n const key = await requestKey(g.req)\n cache.byKey.set(key, { request: g.req, response: g.res })\n cache.byProvider[g.req.provider] = (cache.byProvider[g.req.provider] ?? 0) + 1\n cache.byModel[g.req.model] = (cache.byModel[g.req.model] ?? 0) + 1\n }\n return cache\n }\n\n /** Number of cacheable (request, response) pairs in the cache. */\n size(): number {\n return this.byKey.size\n }\n\n stats(): ReplayCacheStats {\n return {\n total: this.byKey.size,\n byProvider: { ...this.byProvider },\n byModel: { ...this.byModel },\n orphanRequests: this.orphans,\n }\n }\n\n /** Iterate every cached `(request, response)` pair in insertion order. */\n *entries(): IterableIterator<ReplayCacheEntry> {\n for (const entry of this.byKey.values()) yield entry\n }\n\n /**\n * Look up a cached response by hashing the (model, messages, temperature,\n * maxTokens, response_format) shape. Returns `undefined` on miss; the\n * caller decides whether to throw, fall back to the network, or skip.\n */\n async lookup(requestBody: unknown): Promise<ReplayCacheEntry | undefined> {\n const key = await keyFromBody(requestBody)\n return this.byKey.get(key)\n }\n}\n\nexport interface ReplayFetchOptions {\n /**\n * Behaviour on cache miss. Default `'throw'`. `'fallback'` calls the\n * `fallbackFetch` (typically `globalThis.fetch`) so a partial replay can\n * still complete; `'fail-closed'` returns a synthetic 599 response so the\n * call site sees a non-retriable failure.\n */\n onMiss?: 'throw' | 'fallback' | 'fail-closed'\n fallbackFetch?: typeof fetch\n /** Optional callback fired once per replayed call (for telemetry / counters). */\n onHit?: (info: { url: string; provider: string; model: string }) => void\n /** Optional callback fired on cache miss before the `onMiss` policy applies. */\n onMissNotify?: (info: { url: string; requestBody: unknown }) => void\n}\n\n/**\n * Build a `fetch`-shaped function that serves cached responses out of a\n * `ReplayCache` for any URL ending in `/chat/completions`. Pass through\n * `LlmClientOptions.fetch` and `callLlm` becomes free.\n *\n * Non-`/chat/completions` URLs are passed straight to the fallback fetch\n * (default: `globalThis.fetch`). This matters because non-LLM HTTP work\n * (judge HTTP servers, sandbox callbacks) sometimes flows through the same\n * `fetch` and shouldn't be intercepted.\n */\nexport function createReplayFetch(cache: ReplayCache, opts: ReplayFetchOptions = {}): typeof fetch {\n const onMiss = opts.onMiss ?? 'throw'\n const fallback = opts.fallbackFetch ?? globalThis.fetch?.bind(globalThis)\n\n return (async (input: RequestInfo | URL, init?: RequestInit) => {\n const url =\n typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url\n if (!/\\/chat\\/completions(?:[?#].*)?$/.test(url)) {\n if (!fallback)\n throw new ReplayError(\n `replay fetch: non-completions URL ${url} but no fallbackFetch configured`,\n )\n return fallback(input as RequestInfo, init)\n }\n let bodyParsed: unknown\n if (init?.body && typeof init.body === 'string') {\n try {\n bodyParsed = JSON.parse(init.body)\n } catch {\n /* raw body, not JSON */\n }\n }\n const hit = bodyParsed === undefined ? undefined : await cache.lookup(bodyParsed)\n if (hit) {\n opts.onHit?.({ url, provider: hit.request.provider, model: hit.request.model })\n const status = hit.response.statusCode ?? 200\n const headers = new Headers(\n Object.entries(hit.response.responseHeaders ?? { 'Content-Type': 'application/json' }),\n )\n const bodyText =\n typeof hit.response.responseBody === 'string'\n ? hit.response.responseBody\n : JSON.stringify(hit.response.responseBody ?? {})\n return new Response(bodyText, { status, headers })\n }\n opts.onMissNotify?.({ url, requestBody: bodyParsed })\n if (onMiss === 'throw') {\n const key = bodyParsed === undefined ? '<unparseable>' : await keyFromBody(bodyParsed)\n throw new ReplayCacheMissError(url, key)\n }\n if (onMiss === 'fail-closed') {\n return new Response(JSON.stringify({ error: 'replay_cache_miss' }), { status: 599 })\n }\n if (!fallback)\n throw new ReplayError('replay fetch: onMiss=fallback but no fallbackFetch configured')\n return fallback(input as RequestInfo, init)\n }) as typeof fetch\n}\n\n/**\n * Convenience iterator over `(request, response)` pairs in a sink — for\n * post-hoc scoring that doesn't need a `fetch` shim. The judge or scorer\n * runs purely in-process over cached LLM outputs.\n */\nexport async function* iterateRawCalls(\n sink: RawProviderSink,\n filter: { runId?: string; spanId?: string } = {},\n): AsyncGenerator<ReplayCacheEntry> {\n if (!sink.list) {\n throw new ReplayError('iterateRawCalls: sink must implement list().')\n }\n const events = await sink.list(filter)\n const cache = await ReplayCache.fromEvents(events)\n for (const entry of cache.entries()) yield entry\n}\n\n// ── Hashing ──────────────────────────────────────────────────────────────\n\n/**\n * Canonical request key.\n *\n * `model + messages + temperature + max_tokens|max_completion_tokens +\n * response_format` are the dimensions that affect the response shape.\n * Other fields (timestamp headers, provider-specific metadata) are\n * intentionally excluded so a request hashes the same across re-runs.\n */\nasync function requestKey(event: RawProviderEvent): Promise<string> {\n return keyFromBody(event.requestBody)\n}\n\nasync function keyFromBody(body: unknown): Promise<string> {\n if (body == null || typeof body !== 'object') return hashJson({ raw: String(body) })\n const b = body as Record<string, unknown>\n const reduced = canonicalize({\n model: b.model ?? null,\n messages: b.messages ?? null,\n temperature: b.temperature ?? null,\n max_tokens: b.max_tokens ?? null,\n max_completion_tokens: b.max_completion_tokens ?? null,\n response_format: b.response_format ?? null,\n })\n return hashJson(reduced)\n}\n"],"mappings":";;;;;;;;;AA0EO,IAAM,qBAAN,MAA+C;AAAA,EAC5C,OAAO,oBAAI,IAAiB;AAAA,EAC5B,WAAmB,CAAC;AAAA,EACpB,YAA0B,CAAC;AAAA,EAC3B,eAA2B,CAAC;AAAA,EAC5B,YAAiC,CAAC;AAAA,EAE1C,MAAM,UAAU,KAAyB;AACvC,QAAI,KAAK,KAAK,IAAI,IAAI,KAAK,EAAG,OAAM,IAAI,MAAM,OAAO,IAAI,KAAK,iBAAiB;AAC/E,SAAK,KAAK,IAAI,IAAI,OAAO,EAAE,GAAG,IAAI,CAAC;AAAA,EACrC;AAAA,EAEA,MAAM,UAAU,OAAe,OAAoC;AACjE,UAAM,WAAW,KAAK,KAAK,IAAI,KAAK;AACpC,QAAI,CAAC,SAAU,OAAM,IAAI,MAAM,OAAO,KAAK,YAAY;AACvD,SAAK,KAAK,IAAI,OAAO,EAAE,GAAG,UAAU,GAAG,MAAM,C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