@tangle-network/agent-eval 0.120.2 → 0.120.3

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 (171) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/analyst/index.d.ts +3111 -0
  3. package/dist/analyst/index.js +403 -0
  4. package/dist/analyst/index.js.map +1 -0
  5. package/dist/authenticity/index.d.ts +161 -0
  6. package/dist/authenticity/index.js +215 -0
  7. package/dist/authenticity/index.js.map +1 -0
  8. package/dist/belief-state/index.d.ts +1301 -0
  9. package/dist/belief-state/index.js +2152 -0
  10. package/dist/belief-state/index.js.map +1 -0
  11. package/dist/benchmarks/index.d.ts +974 -0
  12. package/dist/benchmarks/index.js +60 -0
  13. package/dist/benchmarks/index.js.map +1 -0
  14. package/dist/builder-eval/index.d.ts +695 -0
  15. package/dist/builder-eval/index.js +366 -0
  16. package/dist/builder-eval/index.js.map +1 -0
  17. package/dist/campaign/index.d.ts +7454 -0
  18. package/dist/campaign/index.js +272 -0
  19. package/dist/campaign/index.js.map +1 -0
  20. package/dist/chunk-3CDFMEMO.js +3878 -0
  21. package/dist/chunk-3CDFMEMO.js.map +1 -0
  22. package/dist/chunk-3RF76KTD.js +84 -0
  23. package/dist/chunk-3RF76KTD.js.map +1 -0
  24. package/dist/chunk-3XH4Y2SS.js +750 -0
  25. package/dist/chunk-3XH4Y2SS.js.map +1 -0
  26. package/dist/chunk-3YYRZDON.js +45 -0
  27. package/dist/chunk-3YYRZDON.js.map +1 -0
  28. package/dist/chunk-5BYTIDZ7.js +550 -0
  29. package/dist/chunk-5BYTIDZ7.js.map +1 -0
  30. package/dist/chunk-5CVUPHJ4.js +2668 -0
  31. package/dist/chunk-5CVUPHJ4.js.map +1 -0
  32. package/dist/chunk-ARU2PZFM.js +312 -0
  33. package/dist/chunk-ARU2PZFM.js.map +1 -0
  34. package/dist/chunk-BOD4O7OF.js +40 -0
  35. package/dist/chunk-BOD4O7OF.js.map +1 -0
  36. package/dist/chunk-CVJP5TMD.js +766 -0
  37. package/dist/chunk-CVJP5TMD.js.map +1 -0
  38. package/dist/chunk-DPZAEKA6.js +880 -0
  39. package/dist/chunk-DPZAEKA6.js.map +1 -0
  40. package/dist/chunk-DTJ6QUQB.js +131 -0
  41. package/dist/chunk-DTJ6QUQB.js.map +1 -0
  42. package/dist/chunk-GGE4NNQT.js +65 -0
  43. package/dist/chunk-GGE4NNQT.js.map +1 -0
  44. package/dist/chunk-H5UD2323.js +286 -0
  45. package/dist/chunk-H5UD2323.js.map +1 -0
  46. package/dist/chunk-HHWE3POT.js +94 -0
  47. package/dist/chunk-HHWE3POT.js.map +1 -0
  48. package/dist/chunk-HKUCJ437.js +787 -0
  49. package/dist/chunk-HKUCJ437.js.map +1 -0
  50. package/dist/chunk-JHCHEVET.js +274 -0
  51. package/dist/chunk-JHCHEVET.js.map +1 -0
  52. package/dist/chunk-K4DBDHLK.js +158 -0
  53. package/dist/chunk-K4DBDHLK.js.map +1 -0
  54. package/dist/chunk-K6N6XJJX.js +306 -0
  55. package/dist/chunk-K6N6XJJX.js.map +1 -0
  56. package/dist/chunk-MA6HLL3S.js +65 -0
  57. package/dist/chunk-MA6HLL3S.js.map +1 -0
  58. package/dist/chunk-MAZ26DC7.js +99 -0
  59. package/dist/chunk-MAZ26DC7.js.map +1 -0
  60. package/dist/chunk-MOXWMGPC.js +577 -0
  61. package/dist/chunk-MOXWMGPC.js.map +1 -0
  62. package/dist/chunk-NJC7U437.js +626 -0
  63. package/dist/chunk-NJC7U437.js.map +1 -0
  64. package/dist/chunk-NMN4WGSJ.js +1030 -0
  65. package/dist/chunk-NMN4WGSJ.js.map +1 -0
  66. package/dist/chunk-NPCTHQIO.js +91 -0
  67. package/dist/chunk-NPCTHQIO.js.map +1 -0
  68. package/dist/chunk-ONWEPEDO.js +57 -0
  69. package/dist/chunk-ONWEPEDO.js.map +1 -0
  70. package/dist/chunk-OYZAPX5G.js +1526 -0
  71. package/dist/chunk-OYZAPX5G.js.map +1 -0
  72. package/dist/chunk-P5MGQ2FY.js +7958 -0
  73. package/dist/chunk-P5MGQ2FY.js.map +1 -0
  74. package/dist/chunk-PC4UYEBM.js +166 -0
  75. package/dist/chunk-PC4UYEBM.js.map +1 -0
  76. package/dist/chunk-PJQFMIOX.js +1182 -0
  77. package/dist/chunk-PJQFMIOX.js.map +1 -0
  78. package/dist/chunk-PXD6ZFNY.js +1107 -0
  79. package/dist/chunk-PXD6ZFNY.js.map +1 -0
  80. package/dist/chunk-PXE2VKMX.js +140 -0
  81. package/dist/chunk-PXE2VKMX.js.map +1 -0
  82. package/dist/chunk-PZ5AY32C.js +10 -0
  83. package/dist/chunk-PZ5AY32C.js.map +1 -0
  84. package/dist/chunk-QBRSJK47.js +622 -0
  85. package/dist/chunk-QBRSJK47.js.map +1 -0
  86. package/dist/chunk-S3UZOQ5Y.js +328 -0
  87. package/dist/chunk-S3UZOQ5Y.js.map +1 -0
  88. package/dist/chunk-SQQED7ZH.js +998 -0
  89. package/dist/chunk-SQQED7ZH.js.map +1 -0
  90. package/dist/chunk-SYV364BL.js +1266 -0
  91. package/dist/chunk-SYV364BL.js.map +1 -0
  92. package/dist/chunk-T4SQEITX.js +95 -0
  93. package/dist/chunk-T4SQEITX.js.map +1 -0
  94. package/dist/chunk-TT4KNT67.js +124 -0
  95. package/dist/chunk-TT4KNT67.js.map +1 -0
  96. package/dist/chunk-U5CHZ5M3.js +357 -0
  97. package/dist/chunk-U5CHZ5M3.js.map +1 -0
  98. package/dist/chunk-ULOKLHIQ.js +1937 -0
  99. package/dist/chunk-ULOKLHIQ.js.map +1 -0
  100. package/dist/chunk-VI2UW6B6.js +162 -0
  101. package/dist/chunk-VI2UW6B6.js.map +1 -0
  102. package/dist/chunk-VQMK5FMP.js +247 -0
  103. package/dist/chunk-VQMK5FMP.js.map +1 -0
  104. package/dist/chunk-VSMTAMNK.js +53 -0
  105. package/dist/chunk-VSMTAMNK.js.map +1 -0
  106. package/dist/chunk-VZSRQ272.js +149 -0
  107. package/dist/chunk-VZSRQ272.js.map +1 -0
  108. package/dist/chunk-WW2A73HW.js +159 -0
  109. package/dist/chunk-WW2A73HW.js.map +1 -0
  110. package/dist/chunk-X4UCIOTZ.js +136 -0
  111. package/dist/chunk-X4UCIOTZ.js.map +1 -0
  112. package/dist/chunk-XJYR7XFV.js +317 -0
  113. package/dist/chunk-XJYR7XFV.js.map +1 -0
  114. package/dist/chunk-ZET2UAYW.js +89 -0
  115. package/dist/chunk-ZET2UAYW.js.map +1 -0
  116. package/dist/chunk-ZMXDQ4K7.js +870 -0
  117. package/dist/chunk-ZMXDQ4K7.js.map +1 -0
  118. package/dist/chunk-ZZUXHH3R.js +99 -0
  119. package/dist/chunk-ZZUXHH3R.js.map +1 -0
  120. package/dist/cli.d.ts +1 -0
  121. package/dist/cli.js +112 -0
  122. package/dist/cli.js.map +1 -0
  123. package/dist/contract/index.d.ts +4972 -0
  124. package/dist/contract/index.js +1654 -0
  125. package/dist/contract/index.js.map +1 -0
  126. package/dist/control.d.ts +1013 -0
  127. package/dist/control.js +34 -0
  128. package/dist/control.js.map +1 -0
  129. package/dist/fuzz.d.ts +759 -0
  130. package/dist/fuzz.js +714 -0
  131. package/dist/fuzz.js.map +1 -0
  132. package/dist/hosted/index.d.ts +730 -0
  133. package/dist/hosted/index.js +14 -0
  134. package/dist/hosted/index.js.map +1 -0
  135. package/dist/index.d.ts +16780 -0
  136. package/dist/index.js +12168 -0
  137. package/dist/index.js.map +1 -0
  138. package/dist/matrix/index.d.ts +155 -0
  139. package/dist/matrix/index.js +8 -0
  140. package/dist/matrix/index.js.map +1 -0
  141. package/dist/meta-eval/index.d.ts +1030 -0
  142. package/dist/meta-eval/index.js +417 -0
  143. package/dist/meta-eval/index.js.map +1 -0
  144. package/dist/multishot/index.d.ts +579 -0
  145. package/dist/multishot/index.js +589 -0
  146. package/dist/multishot/index.js.map +1 -0
  147. package/dist/openapi.json +992 -0
  148. package/dist/pipelines/index.d.ts +567 -0
  149. package/dist/pipelines/index.js +515 -0
  150. package/dist/pipelines/index.js.map +1 -0
  151. package/dist/reporting.d.ts +1277 -0
  152. package/dist/reporting.js +48 -0
  153. package/dist/reporting.js.map +1 -0
  154. package/dist/rl.d.ts +4092 -0
  155. package/dist/rl.js +1724 -0
  156. package/dist/rl.js.map +1 -0
  157. package/dist/run-campaign-75RTPVV5.js +14 -0
  158. package/dist/run-campaign-75RTPVV5.js.map +1 -0
  159. package/dist/storyboard/index.d.ts +279 -0
  160. package/dist/storyboard/index.js +767 -0
  161. package/dist/storyboard/index.js.map +1 -0
  162. package/dist/trace-attributes.d.ts +52 -0
  163. package/dist/trace-attributes.js +62 -0
  164. package/dist/trace-attributes.js.map +1 -0
  165. package/dist/traces.d.ts +2343 -0
  166. package/dist/traces.js +249 -0
  167. package/dist/traces.js.map +1 -0
  168. package/dist/wire/index.d.ts +1252 -0
  169. package/dist/wire/index.js +81 -0
  170. package/dist/wire/index.js.map +1 -0
  171. package/package.json +1 -1
@@ -0,0 +1,317 @@
1
+ import {
2
+ hashJson
3
+ } from "./chunk-VSMTAMNK.js";
4
+ import {
5
+ ValidationError
6
+ } from "./chunk-ONWEPEDO.js";
7
+
8
+ // src/agent-profile-cell.ts
9
+ var AgentProfileCellValidationError = class extends ValidationError {
10
+ path;
11
+ constructor(message, path = "") {
12
+ super(path ? `${message} (at ${path})` : message);
13
+ this.path = path;
14
+ }
15
+ };
16
+ var SHA256_HEX = /^[0-9a-f]{64}$/;
17
+ var CELL_ID = /^agent-profile-cell:sha256:[0-9a-f]{64}$/;
18
+ async function buildAgentProfileCell(input) {
19
+ const material = await normalizeAgentProfileCellInput(input);
20
+ const cellId = `agent-profile-cell:sha256:${await hashJson(material)}`;
21
+ return { ...material, cellId };
22
+ }
23
+ function agentProfileCellHashMaterial(cell) {
24
+ const { cellId: _cellId, ...material } = cell;
25
+ void _cellId;
26
+ return normalizeAgentProfileCell(material);
27
+ }
28
+ async function verifyAgentProfileCell(cell) {
29
+ validateAgentProfileCell(cell);
30
+ return cell.cellId === `agent-profile-cell:sha256:${await hashJson(agentProfileCellHashMaterial(cell))}`;
31
+ }
32
+ function validateAgentProfileCell(input) {
33
+ if (input === null || typeof input !== "object") {
34
+ throw new AgentProfileCellValidationError("expected object");
35
+ }
36
+ const obj = input;
37
+ expectLiteral(obj.schemaVersion, "agent-profile-cell/v1", "schemaVersion");
38
+ if (typeof obj.cellId !== "string" || !CELL_ID.test(obj.cellId)) {
39
+ throw new AgentProfileCellValidationError(
40
+ "cellId must match agent-profile-cell:sha256:<64 lowercase hex chars>",
41
+ "cellId"
42
+ );
43
+ }
44
+ expectString(obj.profileId, "profileId");
45
+ validateSource(obj.sourceProfile, "sourceProfile");
46
+ if (obj.harness !== void 0) validateHarness(obj.harness, "harness");
47
+ if (obj.model !== void 0) expectString(obj.model, "model");
48
+ if (obj.promptHash !== void 0) expectString(obj.promptHash, "promptHash");
49
+ if (obj.dimensions !== void 0) validateDimensions(obj.dimensions, "dimensions");
50
+ return input;
51
+ }
52
+ function requireAgentProfileCell(record) {
53
+ if (!record.agentProfile) {
54
+ throw new AgentProfileCellValidationError(
55
+ `run "${record.runId}" is missing agentProfile; profile-cell grouping requires explicit profile identity`,
56
+ "agentProfile"
57
+ );
58
+ }
59
+ return validateAgentProfileCell(record.agentProfile);
60
+ }
61
+ function agentProfileCellKey(record) {
62
+ return requireAgentProfileCell(record).cellId;
63
+ }
64
+ async function assertRunAgentProfileCell(record) {
65
+ const profile = requireAgentProfileCell(record);
66
+ if (!await verifyAgentProfileCell(profile)) {
67
+ throw new AgentProfileCellValidationError(
68
+ `run "${record.runId}" has an agentProfile.cellId that does not match its content`,
69
+ "agentProfile.cellId"
70
+ );
71
+ }
72
+ if (profile.model !== void 0 && profile.model !== record.model) {
73
+ throw new AgentProfileCellValidationError(
74
+ `run "${record.runId}" agentProfile.model "${profile.model}" does not match model "${record.model}"`,
75
+ "agentProfile.model"
76
+ );
77
+ }
78
+ if (profile.promptHash !== void 0 && profile.promptHash !== record.promptHash) {
79
+ throw new AgentProfileCellValidationError(
80
+ `run "${record.runId}" agentProfile.promptHash "${profile.promptHash}" does not match promptHash "${record.promptHash}"`,
81
+ "agentProfile.promptHash"
82
+ );
83
+ }
84
+ return profile;
85
+ }
86
+ function groupRunsByAgentProfileCell(records) {
87
+ const groups = /* @__PURE__ */ new Map();
88
+ for (const record of records) {
89
+ const key = agentProfileCellKey(record);
90
+ const bucket = groups.get(key);
91
+ if (bucket) bucket.push(record);
92
+ else groups.set(key, [record]);
93
+ }
94
+ return groups;
95
+ }
96
+ async function normalizeAgentProfileCellInput(input) {
97
+ return normalizeAgentProfileCell({
98
+ schemaVersion: "agent-profile-cell/v1",
99
+ profileId: input.profileId,
100
+ sourceProfile: await normalizeSourceInput(input.sourceProfile),
101
+ harness: input.harness,
102
+ model: input.model,
103
+ promptHash: input.promptHash,
104
+ dimensions: input.dimensions
105
+ });
106
+ }
107
+ function normalizeAgentProfileCell(input) {
108
+ return compactObject({
109
+ schemaVersion: "agent-profile-cell/v1",
110
+ profileId: requireNonEmpty(input.profileId, "profileId"),
111
+ sourceProfile: normalizeSource(input.sourceProfile),
112
+ harness: input.harness ? normalizeHarness(input.harness, "harness") : void 0,
113
+ model: optionalNonEmpty(input.model, "model"),
114
+ promptHash: optionalNonEmpty(input.promptHash, "promptHash"),
115
+ dimensions: input.dimensions ? nonEmptyRecord(normalizeDimensions(input.dimensions)) : void 0
116
+ });
117
+ }
118
+ async function normalizeSourceInput(input) {
119
+ const kind = requireNonEmpty(input.kind, "sourceProfile.kind");
120
+ if (input.hash !== void 0 && input.profile !== void 0) {
121
+ throw new AgentProfileCellValidationError(
122
+ "sourceProfile must provide either hash or profile, not both",
123
+ "sourceProfile"
124
+ );
125
+ }
126
+ if (input.hash !== void 0) {
127
+ return { kind, hash: requireSha256Hex(input.hash, "sourceProfile.hash") };
128
+ }
129
+ if (input.profile === void 0) {
130
+ throw new AgentProfileCellValidationError(
131
+ "sourceProfile must provide hash or profile",
132
+ "sourceProfile"
133
+ );
134
+ }
135
+ assertJson(input.profile, "sourceProfile.profile");
136
+ return { kind, hash: await hashJson(input.profile) };
137
+ }
138
+ function normalizeSource(input) {
139
+ return {
140
+ kind: requireNonEmpty(input.kind, "sourceProfile.kind"),
141
+ hash: requireSha256Hex(input.hash, "sourceProfile.hash")
142
+ };
143
+ }
144
+ function normalizeHarness(input, path) {
145
+ return compactObject({
146
+ id: requireNonEmpty(input.id, `${path}.id`),
147
+ version: optionalNonEmpty(input.version, `${path}.version`),
148
+ hash: optionalNonEmpty(input.hash, `${path}.hash`)
149
+ });
150
+ }
151
+ function normalizeDimensions(input) {
152
+ const out = {};
153
+ for (const key of Object.keys(input).sort()) {
154
+ const value = input[key];
155
+ requireNonEmpty(key, "dimensions.<key>");
156
+ if (value !== null && typeof value !== "string" && typeof value !== "number" && typeof value !== "boolean") {
157
+ throw new AgentProfileCellValidationError(
158
+ "expected primitive dimension value",
159
+ `dimensions.${key}`
160
+ );
161
+ }
162
+ if (typeof value === "number" && !Number.isFinite(value)) {
163
+ throw new AgentProfileCellValidationError("expected finite number", `dimensions.${key}`);
164
+ }
165
+ out[key] = value;
166
+ }
167
+ return out;
168
+ }
169
+ function compactObject(input) {
170
+ const out = {};
171
+ for (const [key, value] of Object.entries(input)) {
172
+ if (value !== void 0) out[key] = value;
173
+ }
174
+ return out;
175
+ }
176
+ function nonEmptyRecord(input) {
177
+ return Object.keys(input).length > 0 ? input : void 0;
178
+ }
179
+ function validateSource(value, path) {
180
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
181
+ throw new AgentProfileCellValidationError("expected object", path);
182
+ }
183
+ const rec = value;
184
+ expectString(rec.kind, `${path}.kind`);
185
+ requireSha256Hex(rec.hash, `${path}.hash`);
186
+ }
187
+ function validateHarness(value, path) {
188
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
189
+ throw new AgentProfileCellValidationError("expected object", path);
190
+ }
191
+ const rec = value;
192
+ expectString(rec.id, `${path}.id`);
193
+ if (rec.version !== void 0) expectString(rec.version, `${path}.version`);
194
+ if (rec.hash !== void 0) expectString(rec.hash, `${path}.hash`);
195
+ }
196
+ function validateDimensions(value, path) {
197
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
198
+ throw new AgentProfileCellValidationError("expected object", path);
199
+ }
200
+ normalizeDimensions(value);
201
+ }
202
+ function assertJson(value, path) {
203
+ if (value === null) return;
204
+ const type = typeof value;
205
+ if (type === "string" || type === "boolean") return;
206
+ if (type === "number") {
207
+ if (!Number.isFinite(value)) {
208
+ throw new AgentProfileCellValidationError("expected finite number", path);
209
+ }
210
+ return;
211
+ }
212
+ if (Array.isArray(value)) {
213
+ value.forEach((item, index) => {
214
+ assertJson(item, `${path}[${index}]`);
215
+ });
216
+ return;
217
+ }
218
+ if (type === "object") {
219
+ for (const [key, nested] of Object.entries(value)) {
220
+ requireNonEmpty(key, `${path}.<key>`);
221
+ assertJson(nested, `${path}.${key}`);
222
+ }
223
+ return;
224
+ }
225
+ throw new AgentProfileCellValidationError("expected JSON-compatible value", path);
226
+ }
227
+ function expectLiteral(value, expected, path) {
228
+ if (value !== expected) {
229
+ throw new AgentProfileCellValidationError(`expected ${expected}`, path);
230
+ }
231
+ }
232
+ function expectString(value, path) {
233
+ if (typeof value !== "string" || value.length === 0) {
234
+ throw new AgentProfileCellValidationError("expected non-empty string", path);
235
+ }
236
+ }
237
+ function requireNonEmpty(value, path) {
238
+ if (typeof value !== "string" || value.length === 0) {
239
+ throw new AgentProfileCellValidationError("expected non-empty string", path);
240
+ }
241
+ return value;
242
+ }
243
+ function optionalNonEmpty(value, path) {
244
+ if (value === void 0) return void 0;
245
+ return requireNonEmpty(value, path);
246
+ }
247
+ function requireSha256Hex(value, path) {
248
+ if (typeof value !== "string" || !SHA256_HEX.test(value)) {
249
+ throw new AgentProfileCellValidationError("expected 64 lowercase sha256 hex chars", path);
250
+ }
251
+ return value;
252
+ }
253
+ var AGENT_PROFILE_KINDS = {
254
+ /** A profile declared via `defineAgentProfile(...)` from
255
+ * `@tangle-network/agent-interface`. The default kind for router-backed
256
+ * and sandbox-backed products. */
257
+ AGENT_INTERFACE_PROFILE: "agent-interface-profile"
258
+ };
259
+ function toAgentProfileJson(value) {
260
+ let serialized;
261
+ try {
262
+ serialized = JSON.stringify(value);
263
+ } catch (err) {
264
+ throw new AgentProfileCellValidationError(
265
+ `agent profile must be JSON-serializable: ${err instanceof Error ? err.message : String(err)}`,
266
+ "sourceProfile.profile"
267
+ );
268
+ }
269
+ if (serialized === void 0) {
270
+ throw new AgentProfileCellValidationError(
271
+ "agent profile must be JSON-serializable (got undefined after JSON.stringify)",
272
+ "sourceProfile.profile"
273
+ );
274
+ }
275
+ return JSON.parse(serialized);
276
+ }
277
+ async function buildAgentInterfaceProfileCell(profile, input) {
278
+ if (!profile || typeof profile !== "object") {
279
+ throw new AgentProfileCellValidationError("AgentProfile must be an object", "profile");
280
+ }
281
+ if (typeof profile.name !== "string" || profile.name.length === 0) {
282
+ throw new AgentProfileCellValidationError(
283
+ "AgentProfile must have a non-empty `name`",
284
+ "profile.name"
285
+ );
286
+ }
287
+ if (typeof profile.version !== "string" || profile.version.length === 0) {
288
+ throw new AgentProfileCellValidationError(
289
+ "AgentProfile must have a non-empty `version`",
290
+ "profile.version"
291
+ );
292
+ }
293
+ return buildAgentProfileCell({
294
+ ...input,
295
+ profileId: `${profile.name}@${profile.version}`,
296
+ sourceProfile: {
297
+ kind: AGENT_PROFILE_KINDS.AGENT_INTERFACE_PROFILE,
298
+ profile: toAgentProfileJson(profile)
299
+ }
300
+ });
301
+ }
302
+
303
+ export {
304
+ AgentProfileCellValidationError,
305
+ buildAgentProfileCell,
306
+ agentProfileCellHashMaterial,
307
+ verifyAgentProfileCell,
308
+ validateAgentProfileCell,
309
+ requireAgentProfileCell,
310
+ agentProfileCellKey,
311
+ assertRunAgentProfileCell,
312
+ groupRunsByAgentProfileCell,
313
+ AGENT_PROFILE_KINDS,
314
+ toAgentProfileJson,
315
+ buildAgentInterfaceProfileCell
316
+ };
317
+ //# sourceMappingURL=chunk-XJYR7XFV.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/agent-profile-cell.ts"],"sourcesContent":["import type { AgentProfile } from '@tangle-network/agent-interface'\nimport { ValidationError } from './errors'\nimport { hashJson } from './pre-registration'\n\nexport type AgentProfileCellSchemaVersion = 'agent-profile-cell/v1'\n\nexport type AgentProfileJson =\n | string\n | number\n | boolean\n | null\n | AgentProfileJson[]\n | { [key: string]: AgentProfileJson }\n\nexport type AgentProfileDimensionValue = string | number | boolean | null\n\nexport interface AgentProfileSource {\n /** Runtime/profile contract being fingerprinted, e.g. `agent-interface-profile`. */\n kind: string\n /** sha256 over the canonical source profile object. */\n hash: string\n}\n\nexport interface AgentProfileSourceInput {\n kind: string\n /** Precomputed sha256 for callers that already sign their profile artifact. */\n hash?: string\n /** Full canonical runtime profile; hashed and then discarded from the cell. */\n profile?: AgentProfileJson\n}\n\nexport interface AgentProfileHarness {\n id: string\n version?: string\n hash?: string\n}\n\nexport interface AgentProfileCellInput {\n profileId: string\n sourceProfile: AgentProfileSourceInput\n harness?: AgentProfileHarness\n model?: string\n promptHash?: string\n dimensions?: Record<string, AgentProfileDimensionValue>\n}\n\nexport interface AgentProfileCell {\n schemaVersion: AgentProfileCellSchemaVersion\n cellId: string\n profileId: string\n sourceProfile: AgentProfileSource\n harness?: AgentProfileHarness\n model?: string\n promptHash?: string\n dimensions?: Record<string, AgentProfileDimensionValue>\n}\n\nexport class AgentProfileCellValidationError extends ValidationError {\n readonly path: string\n constructor(message: string, path = '') {\n super(path ? `${message} (at ${path})` : message)\n this.path = path\n }\n}\n\nconst SHA256_HEX = /^[0-9a-f]{64}$/\nconst CELL_ID = /^agent-profile-cell:sha256:[0-9a-f]{64}$/\n\nexport async function buildAgentProfileCell(\n input: AgentProfileCellInput,\n): Promise<AgentProfileCell> {\n const material = await normalizeAgentProfileCellInput(input)\n const cellId = `agent-profile-cell:sha256:${await hashJson(material)}`\n return { ...material, cellId }\n}\n\nexport function agentProfileCellHashMaterial(\n cell: AgentProfileCell,\n): Omit<AgentProfileCell, 'cellId'> {\n const { cellId: _cellId, ...material } = cell\n void _cellId\n return normalizeAgentProfileCell(material)\n}\n\n/**\n * Verify an `AgentProfileCell`'s `cellId` matches the sha256 of its hash-material fields, confirming the record has not been tampered with.\n */\nexport async function verifyAgentProfileCell(cell: AgentProfileCell): Promise<boolean> {\n validateAgentProfileCell(cell)\n return (\n cell.cellId ===\n `agent-profile-cell:sha256:${await hashJson(agentProfileCellHashMaterial(cell))}`\n )\n}\n\nexport function validateAgentProfileCell(input: unknown): AgentProfileCell {\n if (input === null || typeof input !== 'object') {\n throw new AgentProfileCellValidationError('expected object')\n }\n const obj = input as Record<string, unknown>\n expectLiteral(obj.schemaVersion, 'agent-profile-cell/v1', 'schemaVersion')\n if (typeof obj.cellId !== 'string' || !CELL_ID.test(obj.cellId)) {\n throw new AgentProfileCellValidationError(\n 'cellId must match agent-profile-cell:sha256:<64 lowercase hex chars>',\n 'cellId',\n )\n }\n expectString(obj.profileId, 'profileId')\n validateSource(obj.sourceProfile, 'sourceProfile')\n if (obj.harness !== undefined) validateHarness(obj.harness, 'harness')\n if (obj.model !== undefined) expectString(obj.model, 'model')\n if (obj.promptHash !== undefined) expectString(obj.promptHash, 'promptHash')\n if (obj.dimensions !== undefined) validateDimensions(obj.dimensions, 'dimensions')\n return input as AgentProfileCell\n}\n\nexport function requireAgentProfileCell(record: {\n runId: string\n agentProfile?: AgentProfileCell\n}): AgentProfileCell {\n if (!record.agentProfile) {\n throw new AgentProfileCellValidationError(\n `run \"${record.runId}\" is missing agentProfile; profile-cell grouping requires explicit profile identity`,\n 'agentProfile',\n )\n }\n return validateAgentProfileCell(record.agentProfile)\n}\n\nexport function agentProfileCellKey(record: {\n runId: string\n agentProfile?: AgentProfileCell\n}): string {\n return requireAgentProfileCell(record).cellId\n}\n\nexport async function assertRunAgentProfileCell(record: {\n runId: string\n model: string\n promptHash: string\n agentProfile?: AgentProfileCell\n}): Promise<AgentProfileCell> {\n const profile = requireAgentProfileCell(record)\n if (!(await verifyAgentProfileCell(profile))) {\n throw new AgentProfileCellValidationError(\n `run \"${record.runId}\" has an agentProfile.cellId that does not match its content`,\n 'agentProfile.cellId',\n )\n }\n if (profile.model !== undefined && profile.model !== record.model) {\n throw new AgentProfileCellValidationError(\n `run \"${record.runId}\" agentProfile.model \"${profile.model}\" does not match model \"${record.model}\"`,\n 'agentProfile.model',\n )\n }\n if (profile.promptHash !== undefined && profile.promptHash !== record.promptHash) {\n throw new AgentProfileCellValidationError(\n `run \"${record.runId}\" agentProfile.promptHash \"${profile.promptHash}\" does not match promptHash \"${record.promptHash}\"`,\n 'agentProfile.promptHash',\n )\n }\n return profile\n}\n\nexport function groupRunsByAgentProfileCell<\n T extends { runId: string; agentProfile?: AgentProfileCell },\n>(records: readonly T[]): Map<string, T[]> {\n const groups = new Map<string, T[]>()\n for (const record of records) {\n const key = agentProfileCellKey(record)\n const bucket = groups.get(key)\n if (bucket) bucket.push(record)\n else groups.set(key, [record])\n }\n return groups\n}\n\nasync function normalizeAgentProfileCellInput(\n input: AgentProfileCellInput,\n): Promise<Omit<AgentProfileCell, 'cellId'>> {\n return normalizeAgentProfileCell({\n schemaVersion: 'agent-profile-cell/v1',\n profileId: input.profileId,\n sourceProfile: await normalizeSourceInput(input.sourceProfile),\n harness: input.harness,\n model: input.model,\n promptHash: input.promptHash,\n dimensions: input.dimensions,\n })\n}\n\nfunction normalizeAgentProfileCell(\n input: Omit<AgentProfileCell, 'cellId'>,\n): Omit<AgentProfileCell, 'cellId'> {\n return compactObject({\n schemaVersion: 'agent-profile-cell/v1' as const,\n profileId: requireNonEmpty(input.profileId, 'profileId'),\n sourceProfile: normalizeSource(input.sourceProfile),\n harness: input.harness ? normalizeHarness(input.harness, 'harness') : undefined,\n model: optionalNonEmpty(input.model, 'model'),\n promptHash: optionalNonEmpty(input.promptHash, 'promptHash'),\n dimensions: input.dimensions\n ? nonEmptyRecord(normalizeDimensions(input.dimensions))\n : undefined,\n })\n}\n\nasync function normalizeSourceInput(input: AgentProfileSourceInput): Promise<AgentProfileSource> {\n const kind = requireNonEmpty(input.kind, 'sourceProfile.kind')\n if (input.hash !== undefined && input.profile !== undefined) {\n throw new AgentProfileCellValidationError(\n 'sourceProfile must provide either hash or profile, not both',\n 'sourceProfile',\n )\n }\n if (input.hash !== undefined) {\n return { kind, hash: requireSha256Hex(input.hash, 'sourceProfile.hash') }\n }\n if (input.profile === undefined) {\n throw new AgentProfileCellValidationError(\n 'sourceProfile must provide hash or profile',\n 'sourceProfile',\n )\n }\n assertJson(input.profile, 'sourceProfile.profile')\n return { kind, hash: await hashJson(input.profile) }\n}\n\nfunction normalizeSource(input: AgentProfileSource): AgentProfileSource {\n return {\n kind: requireNonEmpty(input.kind, 'sourceProfile.kind'),\n hash: requireSha256Hex(input.hash, 'sourceProfile.hash'),\n }\n}\n\nfunction normalizeHarness(input: AgentProfileHarness, path: string): AgentProfileHarness {\n return compactObject({\n id: requireNonEmpty(input.id, `${path}.id`),\n version: optionalNonEmpty(input.version, `${path}.version`),\n hash: optionalNonEmpty(input.hash, `${path}.hash`),\n })\n}\n\nfunction normalizeDimensions(\n input: Record<string, AgentProfileDimensionValue>,\n): Record<string, AgentProfileDimensionValue> {\n const out: Record<string, AgentProfileDimensionValue> = {}\n for (const key of Object.keys(input).sort()) {\n const value = input[key]\n requireNonEmpty(key, 'dimensions.<key>')\n if (\n value !== null &&\n typeof value !== 'string' &&\n typeof value !== 'number' &&\n typeof value !== 'boolean'\n ) {\n throw new AgentProfileCellValidationError(\n 'expected primitive dimension value',\n `dimensions.${key}`,\n )\n }\n if (typeof value === 'number' && !Number.isFinite(value)) {\n throw new AgentProfileCellValidationError('expected finite number', `dimensions.${key}`)\n }\n out[key] = value\n }\n return out\n}\n\nfunction compactObject<T extends Record<string, unknown>>(input: T): T {\n const out: Record<string, unknown> = {}\n for (const [key, value] of Object.entries(input)) {\n if (value !== undefined) out[key] = value\n }\n return out as T\n}\n\nfunction nonEmptyRecord<T extends Record<string, unknown>>(input: T): T | undefined {\n return Object.keys(input).length > 0 ? input : undefined\n}\n\nfunction validateSource(value: unknown, path: string): void {\n if (value === null || typeof value !== 'object' || Array.isArray(value)) {\n throw new AgentProfileCellValidationError('expected object', path)\n }\n const rec = value as Record<string, unknown>\n expectString(rec.kind, `${path}.kind`)\n requireSha256Hex(rec.hash, `${path}.hash`)\n}\n\nfunction validateHarness(value: unknown, path: string): void {\n if (value === null || typeof value !== 'object' || Array.isArray(value)) {\n throw new AgentProfileCellValidationError('expected object', path)\n }\n const rec = value as Record<string, unknown>\n expectString(rec.id, `${path}.id`)\n if (rec.version !== undefined) expectString(rec.version, `${path}.version`)\n if (rec.hash !== undefined) expectString(rec.hash, `${path}.hash`)\n}\n\nfunction validateDimensions(value: unknown, path: string): void {\n if (value === null || typeof value !== 'object' || Array.isArray(value)) {\n throw new AgentProfileCellValidationError('expected object', path)\n }\n normalizeDimensions(value as Record<string, AgentProfileDimensionValue>)\n}\n\nfunction assertJson(value: AgentProfileJson, path: string): void {\n if (value === null) return\n const type = typeof value\n if (type === 'string' || type === 'boolean') return\n if (type === 'number') {\n if (!Number.isFinite(value)) {\n throw new AgentProfileCellValidationError('expected finite number', path)\n }\n return\n }\n if (Array.isArray(value)) {\n value.forEach((item, index) => {\n assertJson(item, `${path}[${index}]`)\n })\n return\n }\n if (type === 'object') {\n for (const [key, nested] of Object.entries(value)) {\n requireNonEmpty(key, `${path}.<key>`)\n assertJson(nested, `${path}.${key}`)\n }\n return\n }\n throw new AgentProfileCellValidationError('expected JSON-compatible value', path)\n}\n\nfunction expectLiteral(value: unknown, expected: string, path: string): void {\n if (value !== expected) {\n throw new AgentProfileCellValidationError(`expected ${expected}`, path)\n }\n}\n\nfunction expectString(value: unknown, path: string): void {\n if (typeof value !== 'string' || value.length === 0) {\n throw new AgentProfileCellValidationError('expected non-empty string', path)\n }\n}\n\nfunction requireNonEmpty(value: string, path: string): string {\n if (typeof value !== 'string' || value.length === 0) {\n throw new AgentProfileCellValidationError('expected non-empty string', path)\n }\n return value\n}\n\nfunction optionalNonEmpty(value: string | undefined, path: string): string | undefined {\n if (value === undefined) return undefined\n return requireNonEmpty(value, path)\n}\n\nfunction requireSha256Hex(value: unknown, path: string): string {\n if (typeof value !== 'string' || !SHA256_HEX.test(value)) {\n throw new AgentProfileCellValidationError('expected 64 lowercase sha256 hex chars', path)\n }\n return value\n}\n\n// ── Consumer helpers ─────────────────────────────────────────────────\n//\n// Boilerplate every product consuming `buildAgentProfileCell` used to duplicate:\n//\n// 1. A `JSON.parse(JSON.stringify(value))` helper that canonicalizes an\n// arbitrary `@tangle-network/agent-interface` `AgentProfile` into the recursive\n// `AgentProfileJson` shape, with a fail-loud error when the profile\n// is not JSON-serializable.\n//\n// 2. The magic string `'agent-interface-profile'` for `sourceProfile.kind`.\n//\n// Both belong here so the cross-product cell join (same canonical profile\n// hashes to the same `sourceProfile.hash` across products) is enforced by\n// the type system, not by every consumer remembering to do it right.\n// See blueprint-agent issue tangle-network/agent-eval#82.\n\n/** Canonical `sourceProfile.kind` values. Two products fingerprinting the\n * same canonical profile MUST use the same kind for their cells to share\n * `sourceProfile.hash`. Extend rather than create new strings — adding a\n * new kind is a deliberate cross-product schema change. */\nexport const AGENT_PROFILE_KINDS = {\n /** A profile declared via `defineAgentProfile(...)` from\n * `@tangle-network/agent-interface`. The default kind for router-backed\n * and sandbox-backed products. */\n AGENT_INTERFACE_PROFILE: 'agent-interface-profile',\n} as const\n\nexport type AgentProfileKind = (typeof AGENT_PROFILE_KINDS)[keyof typeof AGENT_PROFILE_KINDS]\n\n/** Canonicalize an arbitrary value into `AgentProfileJson` by JSON\n * round-trip. Throws when the value contains anything not representable\n * as JSON (functions, BigInt, cycles) — non-portable profiles fail loud\n * rather than silently dropping fields. */\nexport function toAgentProfileJson(value: unknown): AgentProfileJson {\n let serialized: string | undefined\n try {\n serialized = JSON.stringify(value)\n } catch (err) {\n throw new AgentProfileCellValidationError(\n `agent profile must be JSON-serializable: ${err instanceof Error ? err.message : String(err)}`,\n 'sourceProfile.profile',\n )\n }\n if (serialized === undefined) {\n throw new AgentProfileCellValidationError(\n 'agent profile must be JSON-serializable (got undefined after JSON.stringify)',\n 'sourceProfile.profile',\n )\n }\n return JSON.parse(serialized) as AgentProfileJson\n}\n\n/** Canonical AgentProfile shape required when deriving a stable cell id. */\nexport type AgentInterfaceProfileLike = AgentProfile & { name: string; version: string }\n\n/** Higher-level helper that hard-codes the canonical\n * `agent-interface-profile` kind plus the JSON canonicalization. Equivalent\n * to calling `buildAgentProfileCell` with `profileId = \\`${name}@${version}\\``\n * and `sourceProfile = { kind: AGENT_INTERFACE_PROFILE, profile: <round-tripped> }`.\n *\n * Use this from any product consuming an agent-interface `AgentProfile`; the\n * manual `buildAgentProfileCell` call is reserved for advanced cases\n * (custom kinds, pre-computed source hashes, alternate profileId\n * conventions). */\nexport async function buildAgentInterfaceProfileCell(\n profile: AgentInterfaceProfileLike,\n input: Omit<AgentProfileCellInput, 'profileId' | 'sourceProfile'>,\n): Promise<AgentProfileCell> {\n if (!profile || typeof profile !== 'object') {\n throw new AgentProfileCellValidationError('AgentProfile must be an object', 'profile')\n }\n if (typeof profile.name !== 'string' || profile.name.length === 0) {\n throw new AgentProfileCellValidationError(\n 'AgentProfile must have a non-empty `name`',\n 'profile.name',\n )\n }\n if (typeof profile.version !== 'string' || profile.version.length === 0) {\n throw new AgentProfileCellValidationError(\n 'AgentProfile must have a non-empty `version`',\n 'profile.version',\n )\n }\n return buildAgentProfileCell({\n ...input,\n profileId: `${profile.name}@${profile.version}`,\n sourceProfile: {\n kind: AGENT_PROFILE_KINDS.AGENT_INTERFACE_PROFILE,\n profile: toAgentProfileJson(profile),\n },\n })\n}\n"],"mappings":";;;;;;;;AAyDO,IAAM,kCAAN,cAA8C,gBAAgB;AAAA,EAC1D;AAAA,EACT,YAAY,SAAiB,OAAO,IAAI;AACtC,UAAM,OAAO,GAAG,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MACR;AAAA,MACA;AAAA,IACF;AAAA,EACF;AACA,SAAO,sBAAsB;AAAA,IAC3B,GAAG;AAAA,IACH,WAAW,GAAG,QAAQ,IAAI,IAAI,QAAQ,OAAO;AAAA,IAC7C,eAAe;AAAA,MACb,MAAM,oBAAoB;AAAA,MAC1B,SAAS,mBAAmB,OAAO;AAAA,IACrC;AAAA,EACF,CAAC;AACH;","names":[]}
@@ -0,0 +1,89 @@
1
+ import {
2
+ isJudgeSpan,
3
+ isLlmSpan,
4
+ isToolSpan
5
+ } from "./chunk-MA6HLL3S.js";
6
+
7
+ // src/trace/query.ts
8
+ async function runsForScenario(store, scenarioId) {
9
+ return store.listRuns({ scenarioId });
10
+ }
11
+ async function llmSpans(store, runId) {
12
+ const spans = await store.spans({ runId, kind: "llm" });
13
+ return spans.filter(isLlmSpan);
14
+ }
15
+ async function toolSpans(store, runId, toolName) {
16
+ const spans = await store.spans({ runId, kind: "tool", toolName });
17
+ return spans.filter(isToolSpan);
18
+ }
19
+ async function judgeSpans(store, runId) {
20
+ const spans = await store.spans({ runId, kind: "judge" });
21
+ return spans.filter(isJudgeSpan);
22
+ }
23
+ function groupBy(items, key) {
24
+ const map = /* @__PURE__ */ new Map();
25
+ for (const item of items) {
26
+ const k = key(item);
27
+ let bucket = map.get(k);
28
+ if (!bucket) {
29
+ bucket = [];
30
+ map.set(k, bucket);
31
+ }
32
+ bucket.push(item);
33
+ }
34
+ return map;
35
+ }
36
+ function argHash(args) {
37
+ return stableStringify(args);
38
+ }
39
+ function hasCapturedToolArgs(span) {
40
+ return span.argsCaptured !== false;
41
+ }
42
+ function stableStringify(value) {
43
+ if (value === void 0) return "undefined";
44
+ if (value === null || typeof value !== "object") return JSON.stringify(value) ?? "undefined";
45
+ if (Array.isArray(value)) return `[${value.map(stableStringify).join(",")}]`;
46
+ const obj = value;
47
+ const keys = Object.keys(obj).filter((k) => obj[k] !== void 0).sort();
48
+ const parts = keys.map((k) => `${JSON.stringify(k)}:${stableStringify(obj[k])}`);
49
+ return `{${parts.join(",")}}`;
50
+ }
51
+ function aggregateLlm(spans) {
52
+ return spans.reduce(
53
+ (acc, s) => ({
54
+ inputTokens: acc.inputTokens + (s.inputTokens ?? 0),
55
+ outputTokens: acc.outputTokens + (s.outputTokens ?? 0),
56
+ cachedTokens: acc.cachedTokens + (s.cachedTokens ?? 0),
57
+ cacheWriteTokens: acc.cacheWriteTokens + (s.cacheWriteTokens ?? 0),
58
+ reasoningTokens: acc.reasoningTokens + (s.reasoningTokens ?? 0),
59
+ costUsd: acc.costUsd + (s.costUsd ?? 0)
60
+ }),
61
+ {
62
+ inputTokens: 0,
63
+ outputTokens: 0,
64
+ cachedTokens: 0,
65
+ cacheWriteTokens: 0,
66
+ reasoningTokens: 0,
67
+ costUsd: 0
68
+ }
69
+ );
70
+ }
71
+ function runFailureClass(run) {
72
+ if (run.outcome?.failureClass) return run.outcome.failureClass;
73
+ if (run.status === "completed" && run.outcome?.pass !== false) return "success";
74
+ if (run.status === "aborted") return "budget_exceeded";
75
+ return "unknown";
76
+ }
77
+
78
+ export {
79
+ runsForScenario,
80
+ llmSpans,
81
+ toolSpans,
82
+ judgeSpans,
83
+ groupBy,
84
+ argHash,
85
+ hasCapturedToolArgs,
86
+ aggregateLlm,
87
+ runFailureClass
88
+ };
89
+ //# sourceMappingURL=chunk-ZET2UAYW.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/trace/query.ts"],"sourcesContent":["/**\n * Typed query helpers over TraceStore.\n *\n * Not a full SQL engine — a minimal, composable set of operators that\n * cover the canned-pipeline use cases. For ad-hoc analytics, persist to\n * NDJSON and point DuckDB at it; the schema is stable so external SQL\n * tooling works out of the box.\n */\n\nimport type { FailureClass, JudgeSpan, LlmSpan, Run, ToolSpan } from './schema'\nimport { isJudgeSpan, isLlmSpan, isToolSpan } from './schema'\nimport type { TraceStore } from './store'\n\nexport async function runsForScenario(store: TraceStore, scenarioId: string): Promise<Run[]> {\n return store.listRuns({ scenarioId })\n}\n\nexport async function llmSpans(store: TraceStore, runId?: string): Promise<LlmSpan[]> {\n const spans = await store.spans({ runId, kind: 'llm' })\n return spans.filter(isLlmSpan)\n}\n\nexport async function toolSpans(\n store: TraceStore,\n runId?: string,\n toolName?: string,\n): Promise<ToolSpan[]> {\n const spans = await store.spans({ runId, kind: 'tool', toolName })\n return spans.filter(isToolSpan)\n}\n\n/** Query judge-kind spans from the trace store, optionally scoped to a single run. */\nexport async function judgeSpans(store: TraceStore, runId?: string): Promise<JudgeSpan[]> {\n const spans = await store.spans({ runId, kind: 'judge' })\n return spans.filter(isJudgeSpan)\n}\n\n/** Group spans by any key selector. */\nexport function groupBy<T, K extends string | number>(items: T[], key: (t: T) => K): Map<K, T[]> {\n const map = new Map<K, T[]>()\n for (const item of items) {\n const k = key(item)\n let bucket = map.get(k)\n if (!bucket) {\n bucket = []\n map.set(k, bucket)\n }\n bucket.push(item)\n }\n return map\n}\n\n/** Hash tool arguments to an orderless-key-stable string for de-duplication. */\nexport function argHash(args: unknown): string {\n return stableStringify(args)\n}\n\n/** Whether argument-based comparisons are valid for this tool call. */\nexport function hasCapturedToolArgs(span: ToolSpan): boolean {\n return span.argsCaptured !== false\n}\n\nfunction stableStringify(value: unknown): string {\n // Must ALWAYS return a string: JSON.stringify(undefined) — and stringify of\n // functions/symbols — yields the JS value `undefined`, which would make\n // argHash return a non-string and silently break the de-dup keys behind\n // stuck-loop and failure-cluster.\n if (value === undefined) return 'undefined'\n if (value === null || typeof value !== 'object') return JSON.stringify(value) ?? 'undefined'\n if (Array.isArray(value)) return `[${value.map(stableStringify).join(',')}]`\n // Drop undefined-valued keys to match JSON.stringify semantics, so\n // `{a:1}` and `{a:1,b:undefined}` hash identically.\n const obj = value as Record<string, unknown>\n const keys = Object.keys(obj)\n .filter((k) => obj[k] !== undefined)\n .sort()\n const parts = keys.map((k) => `${JSON.stringify(k)}:${stableStringify(obj[k])}`)\n return `{${parts.join(',')}}`\n}\n\n/** Sum an LLM-span array into aggregate token + cost. */\nexport function aggregateLlm(spans: LlmSpan[]): {\n inputTokens: number\n outputTokens: number\n cachedTokens: number\n cacheWriteTokens: number\n reasoningTokens: number\n costUsd: number\n} {\n return spans.reduce(\n (acc, s) => ({\n inputTokens: acc.inputTokens + (s.inputTokens ?? 0),\n outputTokens: acc.outputTokens + (s.outputTokens ?? 0),\n cachedTokens: acc.cachedTokens + (s.cachedTokens ?? 0),\n cacheWriteTokens: acc.cacheWriteTokens + (s.cacheWriteTokens ?? 0),\n reasoningTokens: acc.reasoningTokens + (s.reasoningTokens ?? 0),\n costUsd: acc.costUsd + (s.costUsd ?? 0),\n }),\n {\n inputTokens: 0,\n outputTokens: 0,\n cachedTokens: 0,\n cacheWriteTokens: 0,\n reasoningTokens: 0,\n costUsd: 0,\n },\n )\n}\n\n/** Pick the outcome's failure class when present, else derive 'success' from run status. */\nexport function runFailureClass(run: Run): FailureClass {\n if (run.outcome?.failureClass) return run.outcome.failureClass\n if (run.status === 'completed' && run.outcome?.pass !== false) return 'success'\n if (run.status === 'aborted') return 'budget_exceeded'\n return 'unknown'\n}\n"],"mappings":";;;;;;;AAaA,eAAsB,gBAAgB,OAAmB,YAAoC;AAC3F,SAAO,MAAM,SAAS,EAAE,WAAW,CAAC;AACtC;AAEA,eAAsB,SAAS,OAAmB,OAAoC;AACpF,QAAM,QAAQ,MAAM,MAAM,MAAM,EAAE,OAAO,MAAM,MAAM,CAAC;AACtD,SAAO,MAAM,OAAO,SAAS;AAC/B;AAEA,eAAsB,UACpB,OACA,OACA,UACqB;AACrB,QAAM,QAAQ,MAAM,MAAM,MAAM,EAAE,OAAO,MAAM,QAAQ,SAAS,CAAC;AACjE,SAAO,MAAM,OAAO,UAAU;AAChC;AAGA,eAAsB,WAAW,OAAmB,OAAsC;AACxF,QAAM,QAAQ,MAAM,MAAM,MAAM,EAAE,OAAO,MAAM,QAAQ,CAAC;AACxD,SAAO,MAAM,OAAO,WAAW;AACjC;AAGO,SAAS,QAAsC,OAAY,KAA+B;AAC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