@tangle-network/agent-eval 0.20.12 → 0.21.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.
- package/CHANGELOG.md +76 -0
- package/README.md +39 -1
- package/dist/{chunk-75MCTH7P.js → chunk-3GN6U53I.js} +198 -3
- package/dist/chunk-3GN6U53I.js.map +1 -0
- package/dist/chunk-3IX6QTB7.js +1349 -0
- package/dist/chunk-3IX6QTB7.js.map +1 -0
- package/dist/{chunk-PKCVBYTQ.js → chunk-5IIQKMD5.js} +38 -2
- package/dist/chunk-5IIQKMD5.js.map +1 -0
- package/dist/{chunk-MCMV7DUL.js → chunk-ARZ6BEV6.js} +2 -2
- package/dist/{chunk-HKYRWNHV.js → chunk-HRZELXCR.js} +2 -2
- package/dist/{chunk-ODFINDLQ.js → chunk-KRR4VMH7.js} +11 -1
- package/dist/chunk-KRR4VMH7.js.map +1 -0
- package/dist/chunk-SNUHRBDL.js +154 -0
- package/dist/chunk-SNUHRBDL.js.map +1 -0
- package/dist/{chunk-KWUAAIHR.js → chunk-WOK2RTWG.js} +157 -1
- package/dist/chunk-WOK2RTWG.js.map +1 -0
- package/dist/{chunk-HNJLMAJ2.js → chunk-WOPGKVN4.js} +2 -2
- package/dist/cli.js +3 -2
- package/dist/cli.js.map +1 -1
- package/dist/{control-C8NKbF3w.d.ts → control-cxwMOAsy.d.ts} +3 -2
- package/dist/control.d.ts +4 -3
- package/dist/control.js +2 -2
- package/dist/emitter-B2XqDKFU.d.ts +121 -0
- package/dist/{feedback-trajectory-BGQ_ANCN.d.ts → feedback-trajectory-CB0A32o3.d.ts} +2 -1
- package/dist/index.d.ts +71 -83
- package/dist/index.js +48 -60
- package/dist/index.js.map +1 -1
- package/dist/openapi.json +1 -1
- package/dist/optimization.d.ts +3 -2
- package/dist/optimization.js +2 -2
- package/dist/reporting-Da2ihlcM.d.ts +672 -0
- package/dist/reporting.d.ts +5 -426
- package/dist/reporting.js +6 -2
- package/dist/{emitter-BYO2nSDA.d.ts → store-u47QaJ9G.d.ts} +1 -91
- package/dist/traces.d.ts +259 -3
- package/dist/traces.js +24 -4
- package/dist/wire/index.js +3 -2
- package/docs/research-report-methodology.md +155 -0
- package/package.json +10 -12
- package/dist/chunk-75MCTH7P.js.map +0 -1
- package/dist/chunk-IKFVX537.js +0 -717
- package/dist/chunk-IKFVX537.js.map +0 -1
- package/dist/chunk-KWUAAIHR.js.map +0 -1
- package/dist/chunk-ODFINDLQ.js.map +0 -1
- package/dist/chunk-PKCVBYTQ.js.map +0 -1
- /package/dist/{chunk-MCMV7DUL.js.map → chunk-ARZ6BEV6.js.map} +0 -0
- /package/dist/{chunk-HKYRWNHV.js.map → chunk-HRZELXCR.js.map} +0 -0
- /package/dist/{chunk-HNJLMAJ2.js.map → chunk-WOPGKVN4.js.map} +0 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// src/trace/raw-provider-sink.ts
|
|
2
|
+
import { promises as fs } from "fs";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
var REDACTED_HEADER_NAMES = /* @__PURE__ */ new Set([
|
|
5
|
+
"authorization",
|
|
6
|
+
"x-api-key",
|
|
7
|
+
"x-auth-token",
|
|
8
|
+
"cookie",
|
|
9
|
+
"set-cookie",
|
|
10
|
+
"proxy-authorization"
|
|
11
|
+
]);
|
|
12
|
+
var REDACTED_BODY_KEY = /^(api[_-]?key|bearer|password|secret|token|access[_-]?token|refresh[_-]?token)$/i;
|
|
13
|
+
function defaultProviderRedactor(event) {
|
|
14
|
+
const redactedFields = [...event.redactedFields ?? []];
|
|
15
|
+
const requestHeaders = redactHeaders(event.requestHeaders, "request", redactedFields);
|
|
16
|
+
const responseHeaders = redactHeaders(event.responseHeaders, "response", redactedFields);
|
|
17
|
+
const requestBody = redactBody(event.requestBody, "requestBody", redactedFields);
|
|
18
|
+
const responseBody = redactBody(event.responseBody, "responseBody", redactedFields);
|
|
19
|
+
return { ...event, requestHeaders, responseHeaders, requestBody, responseBody, redactedFields };
|
|
20
|
+
}
|
|
21
|
+
function redactHeaders(headers, prefix, redactedFields) {
|
|
22
|
+
if (!headers) return headers;
|
|
23
|
+
const out = {};
|
|
24
|
+
for (const [k, v] of Object.entries(headers)) {
|
|
25
|
+
if (REDACTED_HEADER_NAMES.has(k.toLowerCase())) {
|
|
26
|
+
redactedFields.push(`${prefix}Headers.${k}`);
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
out[k] = v;
|
|
30
|
+
}
|
|
31
|
+
return out;
|
|
32
|
+
}
|
|
33
|
+
function redactBody(value, pathStr, redactedFields) {
|
|
34
|
+
if (value == null) return value;
|
|
35
|
+
if (Array.isArray(value)) return value.map((v, i) => redactBody(v, `${pathStr}[${i}]`, redactedFields));
|
|
36
|
+
if (typeof value === "object") {
|
|
37
|
+
const out = {};
|
|
38
|
+
for (const [k, v] of Object.entries(value)) {
|
|
39
|
+
if (REDACTED_BODY_KEY.test(k)) {
|
|
40
|
+
redactedFields.push(`${pathStr}.${k}`);
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
out[k] = redactBody(v, `${pathStr}.${k}`, redactedFields);
|
|
44
|
+
}
|
|
45
|
+
return out;
|
|
46
|
+
}
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
var InMemoryRawProviderSink = class {
|
|
50
|
+
events = [];
|
|
51
|
+
redactor;
|
|
52
|
+
constructor(opts = {}) {
|
|
53
|
+
this.redactor = opts.redactor ?? defaultProviderRedactor;
|
|
54
|
+
}
|
|
55
|
+
async record(event) {
|
|
56
|
+
this.events.push(this.redactor({ ...event, redactedFields: event.redactedFields ?? [] }));
|
|
57
|
+
}
|
|
58
|
+
async list(filter = {}) {
|
|
59
|
+
return this.events.filter(
|
|
60
|
+
(e) => (filter.runId === void 0 || e.runId === filter.runId) && (filter.spanId === void 0 || e.spanId === filter.spanId) && (filter.direction === void 0 || e.direction === filter.direction) && (filter.attemptIndex === void 0 || e.attemptIndex === filter.attemptIndex)
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
size() {
|
|
64
|
+
return this.events.length;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
var NoopRawProviderSink = class {
|
|
68
|
+
async record() {
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
var FileSystemRawProviderSink = class {
|
|
72
|
+
dir;
|
|
73
|
+
fileName;
|
|
74
|
+
rollAtBytes;
|
|
75
|
+
redactor;
|
|
76
|
+
bytesWritten = 0;
|
|
77
|
+
rollIndex = 0;
|
|
78
|
+
initPromise = null;
|
|
79
|
+
constructor(opts) {
|
|
80
|
+
this.dir = opts.dir;
|
|
81
|
+
this.fileName = opts.fileName ?? "raw-provider-events.ndjson";
|
|
82
|
+
this.rollAtBytes = opts.rollAtBytes ?? 32 * 1024 * 1024;
|
|
83
|
+
this.redactor = opts.redactor ?? defaultProviderRedactor;
|
|
84
|
+
}
|
|
85
|
+
async ensureInit() {
|
|
86
|
+
if (!this.initPromise) {
|
|
87
|
+
this.initPromise = fs.mkdir(this.dir, { recursive: true }).then(() => void 0);
|
|
88
|
+
}
|
|
89
|
+
await this.initPromise;
|
|
90
|
+
}
|
|
91
|
+
currentPath() {
|
|
92
|
+
if (this.rollIndex === 0) return path.join(this.dir, this.fileName);
|
|
93
|
+
return path.join(this.dir, `${this.fileName}.${this.rollIndex}`);
|
|
94
|
+
}
|
|
95
|
+
async record(event) {
|
|
96
|
+
await this.ensureInit();
|
|
97
|
+
const redacted = this.redactor({ ...event, redactedFields: event.redactedFields ?? [] });
|
|
98
|
+
const line = JSON.stringify(redacted) + "\n";
|
|
99
|
+
if (this.bytesWritten + line.length > this.rollAtBytes && this.bytesWritten > 0) {
|
|
100
|
+
this.rollIndex += 1;
|
|
101
|
+
this.bytesWritten = 0;
|
|
102
|
+
}
|
|
103
|
+
await fs.appendFile(this.currentPath(), line, "utf8");
|
|
104
|
+
this.bytesWritten += line.length;
|
|
105
|
+
}
|
|
106
|
+
async list(filter = {}) {
|
|
107
|
+
await this.ensureInit();
|
|
108
|
+
const out = [];
|
|
109
|
+
for (let i = 0; i <= this.rollIndex; i++) {
|
|
110
|
+
const file = i === 0 ? path.join(this.dir, this.fileName) : path.join(this.dir, `${this.fileName}.${i}`);
|
|
111
|
+
let body;
|
|
112
|
+
try {
|
|
113
|
+
body = await fs.readFile(file, "utf8");
|
|
114
|
+
} catch (err) {
|
|
115
|
+
if (err.code === "ENOENT") continue;
|
|
116
|
+
throw err;
|
|
117
|
+
}
|
|
118
|
+
for (const line of body.split("\n")) {
|
|
119
|
+
if (!line) continue;
|
|
120
|
+
const event = JSON.parse(line);
|
|
121
|
+
if (filter.runId !== void 0 && event.runId !== filter.runId) continue;
|
|
122
|
+
if (filter.spanId !== void 0 && event.spanId !== filter.spanId) continue;
|
|
123
|
+
if (filter.direction !== void 0 && event.direction !== filter.direction) continue;
|
|
124
|
+
if (filter.attemptIndex !== void 0 && event.attemptIndex !== filter.attemptIndex) continue;
|
|
125
|
+
out.push(event);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return out;
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
function providerFromBaseUrl(baseUrl) {
|
|
132
|
+
const lower = baseUrl.toLowerCase();
|
|
133
|
+
if (lower.includes("api.openai.com")) return "openai";
|
|
134
|
+
if (lower.includes("api.anthropic.com")) return "anthropic";
|
|
135
|
+
if (lower.includes("generativelanguage.googleapis.com")) return "google";
|
|
136
|
+
if (lower.includes("api.together.ai") || lower.includes("api.together.xyz")) return "together";
|
|
137
|
+
if (lower.includes("api.deepseek.com")) return "deepseek";
|
|
138
|
+
if (lower.includes("router.tangle.tools")) return "tangle-router";
|
|
139
|
+
if (lower.includes("api.litellm") || lower.includes("litellm")) return "litellm";
|
|
140
|
+
try {
|
|
141
|
+
return new URL(baseUrl).host;
|
|
142
|
+
} catch {
|
|
143
|
+
return baseUrl;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export {
|
|
148
|
+
defaultProviderRedactor,
|
|
149
|
+
InMemoryRawProviderSink,
|
|
150
|
+
NoopRawProviderSink,
|
|
151
|
+
FileSystemRawProviderSink,
|
|
152
|
+
providerFromBaseUrl
|
|
153
|
+
};
|
|
154
|
+
//# sourceMappingURL=chunk-SNUHRBDL.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/trace/raw-provider-sink.ts"],"sourcesContent":["/**\n * RawProviderSink — first-class persistence for the actual HTTP-level\n * request/response bodies of every LLM provider call.\n *\n * Why this is a separate sink from the structured `LlmSpan`:\n *\n * - `LlmSpan` records the *intent* — model name, messages, output text,\n * usage. It's what dashboards read; it's NOT enough for forensics.\n * - When a downstream consumer reports \"the verifier used the wrong route\"\n * or \"tokens look right but reasoning was missing,\" the only way to\n * answer is the raw HTTP body. Span fields can lie (a proxy can echo\n * a different `model` value than what actually answered); the raw\n * response is ground truth.\n *\n * Default behaviour: opt-in. Pass `rawSink` to `LlmClientOptions` (or the\n * matrix runner / BuilderSession sets it up automatically) and every\n * request, response, and error is recorded — including retries, with the\n * attempt index attached so a flaky call's full event chain is recoverable.\n *\n * Redaction is enforced at sink time. The default redactor strips\n * `Authorization`, `X-Api-Key`, `X-Auth-Token`, `Cookie` headers and any\n * payload field whose key matches `apiKey | api_key | bearer | password |\n * secret | token` (case-insensitive). Override via the sink constructor or\n * the per-call `redactor`. The `redactedFields` array on the persisted\n * event lets a reviewer see what was stripped without exposing the values.\n */\n\nimport { promises as fs } from 'node:fs'\nimport * as path from 'node:path'\n\nexport type RawProviderDirection = 'request' | 'response' | 'error'\n\nexport interface RawProviderEvent {\n /** Stable id. Generated by the sink if omitted. */\n eventId: string\n /** Trace context populated by `LlmClient` when the call is wrapped in a span. */\n runId?: string\n spanId?: string\n /**\n * Logical provider name. Free-form so callers can use whatever id matches\n * their topology (`'openai'`, `'anthropic'`, `'tangle-router'`, …). When\n * omitted, derived from `baseUrl` in `LlmClientOptions`.\n */\n provider: string\n model: string\n /** Endpoint path, e.g. `'/v1/chat/completions'`. */\n endpoint: string\n /** Base URL used for the call (already-normalised — no trailing slash). */\n baseUrl: string\n /** 0-indexed retry attempt. The first attempt is 0; a retried call gets 1, 2, … */\n attemptIndex: number\n direction: RawProviderDirection\n /** Unix ms. */\n timestamp: number\n /** Wall-clock duration of the call leg. Set on `response` and `error` events; null on `request`. */\n durationMs?: number\n statusCode?: number\n requestHeaders?: Record<string, string>\n requestBody?: unknown\n responseHeaders?: Record<string, string>\n responseBody?: unknown\n /** Set on `direction: 'error'` events. */\n errorMessage?: string\n /** Field paths the redactor stripped from this event ('header:Authorization', 'body.apiKey', …). */\n redactedFields: string[]\n}\n\nexport interface RawProviderSinkFilter {\n runId?: string\n spanId?: string\n direction?: RawProviderDirection\n attemptIndex?: number\n}\n\nexport interface RawProviderSink {\n record(event: RawProviderEvent): Promise<void>\n /** Optional listing — implementations that durably persist (file, db) should support this. */\n list?(filter?: RawProviderSinkFilter): Promise<RawProviderEvent[]>\n /** Optional teardown for backed implementations. */\n close?(): Promise<void>\n}\n\nexport type ProviderRedactor = (event: RawProviderEvent) => RawProviderEvent\n\nconst REDACTED_HEADER_NAMES = new Set([\n 'authorization',\n 'x-api-key',\n 'x-auth-token',\n 'cookie',\n 'set-cookie',\n 'proxy-authorization',\n])\n\nconst REDACTED_BODY_KEY = /^(api[_-]?key|bearer|password|secret|token|access[_-]?token|refresh[_-]?token)$/i\n\n/**\n * Default redactor — strips well-known auth headers and any body field whose\n * key matches the credential pattern. Records every redacted path on\n * `event.redactedFields` so a downstream reviewer can see what was removed.\n */\nexport function defaultProviderRedactor(event: RawProviderEvent): RawProviderEvent {\n const redactedFields: string[] = [...(event.redactedFields ?? [])]\n const requestHeaders = redactHeaders(event.requestHeaders, 'request', redactedFields)\n const responseHeaders = redactHeaders(event.responseHeaders, 'response', redactedFields)\n const requestBody = redactBody(event.requestBody, 'requestBody', redactedFields)\n const responseBody = redactBody(event.responseBody, 'responseBody', redactedFields)\n return { ...event, requestHeaders, responseHeaders, requestBody, responseBody, redactedFields }\n}\n\nfunction redactHeaders(\n headers: Record<string, string> | undefined,\n prefix: 'request' | 'response',\n redactedFields: string[],\n): Record<string, string> | undefined {\n if (!headers) return headers\n const out: Record<string, string> = {}\n for (const [k, v] of Object.entries(headers)) {\n if (REDACTED_HEADER_NAMES.has(k.toLowerCase())) {\n redactedFields.push(`${prefix}Headers.${k}`)\n continue\n }\n out[k] = v\n }\n return out\n}\n\nfunction redactBody(\n value: unknown,\n pathStr: string,\n redactedFields: string[],\n): unknown {\n if (value == null) return value\n if (Array.isArray(value)) return value.map((v, i) => redactBody(v, `${pathStr}[${i}]`, redactedFields))\n if (typeof value === 'object') {\n const out: Record<string, unknown> = {}\n for (const [k, v] of Object.entries(value as Record<string, unknown>)) {\n if (REDACTED_BODY_KEY.test(k)) {\n redactedFields.push(`${pathStr}.${k}`)\n continue\n }\n out[k] = redactBody(v, `${pathStr}.${k}`, redactedFields)\n }\n return out\n }\n return value\n}\n\n// ── In-memory ────────────────────────────────────────────────────────────\n\nexport interface InMemoryRawProviderSinkOptions {\n redactor?: ProviderRedactor\n}\n\nexport class InMemoryRawProviderSink implements RawProviderSink {\n private events: RawProviderEvent[] = []\n private redactor: ProviderRedactor\n\n constructor(opts: InMemoryRawProviderSinkOptions = {}) {\n this.redactor = opts.redactor ?? defaultProviderRedactor\n }\n\n async record(event: RawProviderEvent): Promise<void> {\n this.events.push(this.redactor({ ...event, redactedFields: event.redactedFields ?? [] }))\n }\n\n async list(filter: RawProviderSinkFilter = {}): Promise<RawProviderEvent[]> {\n return this.events.filter((e) =>\n (filter.runId === undefined || e.runId === filter.runId) &&\n (filter.spanId === undefined || e.spanId === filter.spanId) &&\n (filter.direction === undefined || e.direction === filter.direction) &&\n (filter.attemptIndex === undefined || e.attemptIndex === filter.attemptIndex),\n )\n }\n\n size(): number { return this.events.length }\n}\n\nexport class NoopRawProviderSink implements RawProviderSink {\n async record(): Promise<void> { /* no-op */ }\n}\n\n// ── Filesystem (NDJSON) ──────────────────────────────────────────────────\n\nexport interface FileSystemRawProviderSinkOptions {\n /** Directory the NDJSON file is written into. Created if missing. */\n dir: string\n /** File name; default `'raw-provider-events.ndjson'`. */\n fileName?: string\n /** Bytes after which the writer rolls over to a new file (default 32 MiB). */\n rollAtBytes?: number\n redactor?: ProviderRedactor\n}\n\nexport class FileSystemRawProviderSink implements RawProviderSink {\n private dir: string\n private fileName: string\n private rollAtBytes: number\n private redactor: ProviderRedactor\n private bytesWritten = 0\n private rollIndex = 0\n private initPromise: Promise<void> | null = null\n\n constructor(opts: FileSystemRawProviderSinkOptions) {\n this.dir = opts.dir\n this.fileName = opts.fileName ?? 'raw-provider-events.ndjson'\n this.rollAtBytes = opts.rollAtBytes ?? 32 * 1024 * 1024\n this.redactor = opts.redactor ?? defaultProviderRedactor\n }\n\n private async ensureInit(): Promise<void> {\n if (!this.initPromise) {\n this.initPromise = fs.mkdir(this.dir, { recursive: true }).then(() => undefined)\n }\n await this.initPromise\n }\n\n private currentPath(): string {\n if (this.rollIndex === 0) return path.join(this.dir, this.fileName)\n return path.join(this.dir, `${this.fileName}.${this.rollIndex}`)\n }\n\n async record(event: RawProviderEvent): Promise<void> {\n await this.ensureInit()\n const redacted = this.redactor({ ...event, redactedFields: event.redactedFields ?? [] })\n const line = JSON.stringify(redacted) + '\\n'\n if (this.bytesWritten + line.length > this.rollAtBytes && this.bytesWritten > 0) {\n this.rollIndex += 1\n this.bytesWritten = 0\n }\n await fs.appendFile(this.currentPath(), line, 'utf8')\n this.bytesWritten += line.length\n }\n\n async list(filter: RawProviderSinkFilter = {}): Promise<RawProviderEvent[]> {\n await this.ensureInit()\n const out: RawProviderEvent[] = []\n for (let i = 0; i <= this.rollIndex; i++) {\n const file = i === 0\n ? path.join(this.dir, this.fileName)\n : path.join(this.dir, `${this.fileName}.${i}`)\n let body: string\n try {\n body = await fs.readFile(file, 'utf8')\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === 'ENOENT') continue\n throw err\n }\n for (const line of body.split('\\n')) {\n if (!line) continue\n const event = JSON.parse(line) as RawProviderEvent\n if (filter.runId !== undefined && event.runId !== filter.runId) continue\n if (filter.spanId !== undefined && event.spanId !== filter.spanId) continue\n if (filter.direction !== undefined && event.direction !== filter.direction) continue\n if (filter.attemptIndex !== undefined && event.attemptIndex !== filter.attemptIndex) continue\n out.push(event)\n }\n }\n return out\n }\n}\n\n// ── Helpers ──────────────────────────────────────────────────────────────\n\n/**\n * Best-effort provider id from a base URL. Falls back to the URL host when\n * none of the well-known patterns match.\n */\nexport function providerFromBaseUrl(baseUrl: string): string {\n const lower = baseUrl.toLowerCase()\n if (lower.includes('api.openai.com')) return 'openai'\n if (lower.includes('api.anthropic.com')) return 'anthropic'\n if (lower.includes('generativelanguage.googleapis.com')) return 'google'\n if (lower.includes('api.together.ai') || lower.includes('api.together.xyz')) return 'together'\n if (lower.includes('api.deepseek.com')) return 'deepseek'\n if (lower.includes('router.tangle.tools')) return 'tangle-router'\n if (lower.includes('api.litellm') || lower.includes('litellm')) return 'litellm'\n try {\n return new URL(baseUrl).host\n } catch {\n return baseUrl\n }\n}\n"],"mappings":";AA2BA,SAAS,YAAY,UAAU;AAC/B,YAAY,UAAU;AAwDtB,IAAM,wBAAwB,oBAAI,IAAI;AAAA,EACpC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAED,IAAM,oBAAoB;AAOnB,SAAS,wBAAwB,OAA2C;AACjF,QAAM,iBAA2B,CAAC,GAAI,MAAM,kBAAkB,CAAC,CAAE;AACjE,QAAM,iBAAiB,cAAc,MAAM,gBAAgB,WAAW,cAAc;AACpF,QAAM,kBAAkB,cAAc,MAAM,iBAAiB,YAAY,cAAc;AACvF,QAAM,cAAc,WAAW,MAAM,aAAa,eAAe,cAAc;AAC/E,QAAM,eAAe,WAAW,MAAM,cAAc,gBAAgB,cAAc;AAClF,SAAO,EAAE,GAAG,OAAO,gBAAgB,iBAA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AAkB,EAAG,QAAO;AACpF,MAAI,MAAM,SAAS,kBAAkB,EAAG,QAAO;AAC/C,MAAI,MAAM,SAAS,qBAAqB,EAAG,QAAO;AAClD,MAAI,MAAM,SAAS,aAAa,KAAK,MAAM,SAAS,SAAS,EAAG,QAAO;AACvE,MAAI;AACF,WAAO,IAAI,IAAI,OAAO,EAAE;AAAA,EAC1B,QAAQ;AACN,WAAO;AAAA,EACT;AACF;","names":[]}
|
|
@@ -497,6 +497,121 @@ function runToTraceId(run) {
|
|
|
497
497
|
return cleaned.slice(0, 32).padEnd(32, "0");
|
|
498
498
|
}
|
|
499
499
|
|
|
500
|
+
// src/trace/integrity.ts
|
|
501
|
+
var RunIntegrityError = class extends Error {
|
|
502
|
+
constructor(report) {
|
|
503
|
+
super(
|
|
504
|
+
`Run ${report.runId} failed integrity check: ${report.issues.map((i) => i.code).join(", ")}`
|
|
505
|
+
);
|
|
506
|
+
this.report = report;
|
|
507
|
+
this.name = "RunIntegrityError";
|
|
508
|
+
}
|
|
509
|
+
report;
|
|
510
|
+
};
|
|
511
|
+
async function assertRunCaptured(store, runId, expectations = {}) {
|
|
512
|
+
const issues = [];
|
|
513
|
+
const run = await store.getRun(runId);
|
|
514
|
+
if (!run) {
|
|
515
|
+
return {
|
|
516
|
+
ok: false,
|
|
517
|
+
runId,
|
|
518
|
+
llmSpanCount: 0,
|
|
519
|
+
judgeSpanCount: 0,
|
|
520
|
+
toolSpanCount: 0,
|
|
521
|
+
rawProviderEventCount: 0,
|
|
522
|
+
rawSpanCoverage: { covered: 0, total: 0 },
|
|
523
|
+
issues: [{ code: "no_run", message: `Run ${runId} not found in store.` }]
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
const spans = await store.spans({ runId });
|
|
527
|
+
const llmSpans2 = spans.filter((s) => s.kind === "llm");
|
|
528
|
+
const judgeSpans2 = spans.filter((s) => s.kind === "judge");
|
|
529
|
+
const toolSpans2 = spans.filter((s) => s.kind === "tool");
|
|
530
|
+
const llmMin = expectations.llmSpansMin ?? 0;
|
|
531
|
+
const judgeMin = expectations.judgeSpansMin ?? 0;
|
|
532
|
+
const toolMin = expectations.toolSpansMin ?? 0;
|
|
533
|
+
if (llmSpans2.length < llmMin) {
|
|
534
|
+
issues.push({
|
|
535
|
+
code: "missing_llm_spans",
|
|
536
|
+
message: `Expected \u2265 ${llmMin} LLM spans, found ${llmSpans2.length}.`,
|
|
537
|
+
detail: { expected: llmMin, found: llmSpans2.length }
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
if (judgeSpans2.length < judgeMin) {
|
|
541
|
+
issues.push({
|
|
542
|
+
code: "missing_judge_spans",
|
|
543
|
+
message: `Expected \u2265 ${judgeMin} judge spans, found ${judgeSpans2.length}.`,
|
|
544
|
+
detail: { expected: judgeMin, found: judgeSpans2.length }
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
if (toolSpans2.length < toolMin) {
|
|
548
|
+
issues.push({
|
|
549
|
+
code: "missing_tool_spans",
|
|
550
|
+
message: `Expected \u2265 ${toolMin} tool spans, found ${toolSpans2.length}.`,
|
|
551
|
+
detail: { expected: toolMin, found: toolSpans2.length }
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
let rawEventCount = 0;
|
|
555
|
+
let coverage = { covered: 0, total: llmSpans2.length };
|
|
556
|
+
if (expectations.rawSink) {
|
|
557
|
+
if (!expectations.rawSink.list) {
|
|
558
|
+
issues.push({
|
|
559
|
+
code: "no_raw_sink",
|
|
560
|
+
message: "Provided rawSink does not implement list(); cannot verify capture."
|
|
561
|
+
});
|
|
562
|
+
} else {
|
|
563
|
+
const events = await expectations.rawSink.list({ runId });
|
|
564
|
+
rawEventCount = events.length;
|
|
565
|
+
const rawMin = expectations.rawProviderEventsMin ?? 1;
|
|
566
|
+
if (rawEventCount < rawMin) {
|
|
567
|
+
issues.push({
|
|
568
|
+
code: "missing_raw_events",
|
|
569
|
+
message: `Expected \u2265 ${rawMin} raw provider events, found ${rawEventCount}.`,
|
|
570
|
+
detail: { expected: rawMin, found: rawEventCount }
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
if (expectations.requireRawCoverageOfLlmSpans) {
|
|
574
|
+
const requestEventsBySpan = new Set(
|
|
575
|
+
events.filter((e) => e.direction === "request" && e.spanId).map((e) => e.spanId)
|
|
576
|
+
);
|
|
577
|
+
const orphaned = llmSpans2.filter((s) => !requestEventsBySpan.has(s.spanId));
|
|
578
|
+
coverage = { covered: llmSpans2.length - orphaned.length, total: llmSpans2.length };
|
|
579
|
+
if (orphaned.length > 0) {
|
|
580
|
+
issues.push({
|
|
581
|
+
code: "orphan_llm_span",
|
|
582
|
+
message: `${orphaned.length} LLM span(s) have no matching raw provider request event.`,
|
|
583
|
+
detail: { orphanedSpanIds: orphaned.map((s) => s.spanId) }
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
} else if (expectations.requireRawCoverageOfLlmSpans || expectations.rawProviderEventsMin) {
|
|
589
|
+
issues.push({
|
|
590
|
+
code: "no_raw_sink",
|
|
591
|
+
message: "Raw coverage required but no rawSink supplied to the integrity check."
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
if (expectations.requireOutcome && (run.outcome === void 0 || run.outcome === null)) {
|
|
595
|
+
issues.push({
|
|
596
|
+
code: "missing_outcome",
|
|
597
|
+
message: `Run ${runId} has no outcome recorded.`
|
|
598
|
+
});
|
|
599
|
+
}
|
|
600
|
+
return {
|
|
601
|
+
ok: issues.length === 0,
|
|
602
|
+
runId,
|
|
603
|
+
llmSpanCount: llmSpans2.length,
|
|
604
|
+
judgeSpanCount: judgeSpans2.length,
|
|
605
|
+
toolSpanCount: toolSpans2.length,
|
|
606
|
+
rawProviderEventCount: rawEventCount,
|
|
607
|
+
rawSpanCoverage: coverage,
|
|
608
|
+
issues
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
function throwIfRunIncomplete(report) {
|
|
612
|
+
if (!report.ok) throw new RunIntegrityError(report);
|
|
613
|
+
}
|
|
614
|
+
|
|
500
615
|
// src/trace-analyst/types.ts
|
|
501
616
|
var DEFAULT_TRACE_ANALYST_BUDGETS = {
|
|
502
617
|
perCallByteCeiling: 15e4,
|
|
@@ -1468,6 +1583,43 @@ function normalizeRecordArray(value) {
|
|
|
1468
1583
|
return value.map((item) => item && typeof item === "object" ? { ...item } : { value: item });
|
|
1469
1584
|
}
|
|
1470
1585
|
|
|
1586
|
+
// src/trace-analyst/hook.ts
|
|
1587
|
+
var DEFAULT_QUESTION = "Summarise what happened in this run. Surface any failure modes, surprising findings, or evidence that the run's verdict is wrong.";
|
|
1588
|
+
function traceAnalystOnRunComplete(opts) {
|
|
1589
|
+
return async (ctx) => {
|
|
1590
|
+
if (opts.shouldRun && !opts.shouldRun(ctx)) return;
|
|
1591
|
+
const source = opts.analyze.source;
|
|
1592
|
+
if (source === void 0) {
|
|
1593
|
+
await ctx.store.appendEvent({
|
|
1594
|
+
eventId: `analyst-skip-${ctx.runId}`,
|
|
1595
|
+
runId: ctx.runId,
|
|
1596
|
+
kind: "log",
|
|
1597
|
+
timestamp: Date.now(),
|
|
1598
|
+
payload: { source: "trace_analyst_hook", reason: "no source configured" }
|
|
1599
|
+
});
|
|
1600
|
+
return;
|
|
1601
|
+
}
|
|
1602
|
+
const result = await analyzeTraces(
|
|
1603
|
+
{ question: opts.question ?? DEFAULT_QUESTION },
|
|
1604
|
+
{ ...opts.analyze, source }
|
|
1605
|
+
);
|
|
1606
|
+
if (opts.save) await opts.save(result, ctx);
|
|
1607
|
+
if (opts.gateOn && !opts.gateOn(result, ctx)) {
|
|
1608
|
+
await ctx.store.appendEvent({
|
|
1609
|
+
eventId: `analyst-gate-${ctx.runId}`,
|
|
1610
|
+
runId: ctx.runId,
|
|
1611
|
+
kind: "log",
|
|
1612
|
+
timestamp: Date.now(),
|
|
1613
|
+
payload: {
|
|
1614
|
+
source: "trace_analyst_hook",
|
|
1615
|
+
reason: "analyst_gate_failed",
|
|
1616
|
+
findings: result.findings
|
|
1617
|
+
}
|
|
1618
|
+
});
|
|
1619
|
+
}
|
|
1620
|
+
};
|
|
1621
|
+
}
|
|
1622
|
+
|
|
1471
1623
|
// src/trace-analyst/insights.ts
|
|
1472
1624
|
var DOMAIN_STOP_WORDS = /* @__PURE__ */ new Set([
|
|
1473
1625
|
"and",
|
|
@@ -1739,6 +1891,9 @@ export {
|
|
|
1739
1891
|
redactValue,
|
|
1740
1892
|
OTEL_AGENT_EVAL_SCOPE,
|
|
1741
1893
|
exportRunAsOtlp,
|
|
1894
|
+
RunIntegrityError,
|
|
1895
|
+
assertRunCaptured,
|
|
1896
|
+
throwIfRunIncomplete,
|
|
1742
1897
|
DEFAULT_TRACE_ANALYST_BUDGETS,
|
|
1743
1898
|
TRACE_ANALYST_TRUNCATION_MARKER_PREFIX,
|
|
1744
1899
|
OtlpFileTraceStore,
|
|
@@ -1751,6 +1906,7 @@ export {
|
|
|
1751
1906
|
buildTraceAnalystTools,
|
|
1752
1907
|
traceAnalystFunctionGroup,
|
|
1753
1908
|
analyzeTraces,
|
|
1909
|
+
traceAnalystOnRunComplete,
|
|
1754
1910
|
tokenizeDomainWords,
|
|
1755
1911
|
inferDomainKeywords,
|
|
1756
1912
|
domainEvidencePattern,
|
|
@@ -1761,4 +1917,4 @@ export {
|
|
|
1761
1917
|
defaultTraceInsightPanel,
|
|
1762
1918
|
buildTraceInsightPrompt
|
|
1763
1919
|
};
|
|
1764
|
-
//# sourceMappingURL=chunk-
|
|
1920
|
+
//# sourceMappingURL=chunk-WOK2RTWG.js.map
|