@tangle-network/agent-eval 0.72.0 → 0.72.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 (69) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/dist/adapters/http.d.ts +1 -1
  3. package/dist/adapters/langchain.d.ts +1 -1
  4. package/dist/adapters/otel.d.ts +3 -2
  5. package/dist/agent-profile-DYRboYWu.d.ts +364 -0
  6. package/dist/analyst/index.d.ts +221 -0
  7. package/dist/analyst/index.js +371 -0
  8. package/dist/analyst/index.js.map +1 -0
  9. package/dist/analyst-t7zZS3TV.d.ts +88 -0
  10. package/dist/campaign/index.d.ts +485 -9
  11. package/dist/campaign/index.js +597 -22
  12. package/dist/campaign/index.js.map +1 -1
  13. package/dist/chunk-7W4SM7FD.js +1075 -0
  14. package/dist/chunk-7W4SM7FD.js.map +1 -0
  15. package/dist/{chunk-AIWHLG7J.js → chunk-GJJNJVIR.js} +11 -11
  16. package/dist/chunk-JHA3ZGSO.js +1496 -0
  17. package/dist/chunk-JHA3ZGSO.js.map +1 -0
  18. package/dist/{chunk-4QJN7RDX.js → chunk-JYE3WOTE.js} +55 -7
  19. package/dist/{chunk-4QJN7RDX.js.map → chunk-JYE3WOTE.js.map} +1 -1
  20. package/dist/chunk-LB2UOI5F.js +412 -0
  21. package/dist/chunk-LB2UOI5F.js.map +1 -0
  22. package/dist/{chunk-ODGETRTM.js → chunk-VUINJM5M.js} +234 -1415
  23. package/dist/chunk-VUINJM5M.js.map +1 -0
  24. package/dist/chunk-WYIHD6EB.js +1044 -0
  25. package/dist/chunk-WYIHD6EB.js.map +1 -0
  26. package/dist/{chunk-UD6EF73X.js → chunk-XPILG2CA.js} +119 -2
  27. package/dist/chunk-XPILG2CA.js.map +1 -0
  28. package/dist/contract/index.d.ts +17 -13
  29. package/dist/contract/index.js +13 -7
  30. package/dist/contract/index.js.map +1 -1
  31. package/dist/{control-DxvZeV5X.d.ts → control-BgA6BYTm.d.ts} +1 -1
  32. package/dist/control.d.ts +2 -2
  33. package/dist/{feedback-trajectory-8hKC5EOb.d.ts → feedback-trajectory-B3rErRsh.d.ts} +1 -1
  34. package/dist/harness-optimizer-EnEnQPsr.d.ts +106 -0
  35. package/dist/hosted/index.d.ts +223 -2
  36. package/dist/index.d.ts +49 -1323
  37. package/dist/index.js +353 -2496
  38. package/dist/index.js.map +1 -1
  39. package/dist/{index-BGBrVS24.d.ts → insight-report-Df3lxYXM.d.ts} +1 -221
  40. package/dist/kind-factory-DW9XWPvM.d.ts +172 -0
  41. package/dist/multi-layer-verifier-DlWCXuxL.d.ts +141 -0
  42. package/dist/openapi.json +1 -1
  43. package/dist/pareto-E-pembql.d.ts +81 -0
  44. package/dist/{provenance-C69gLUXH.d.ts → provenance-B-TFszPW.d.ts} +131 -4
  45. package/dist/redact-B40YG2M_.d.ts +45 -0
  46. package/dist/registry-DuVYiTvw.d.ts +128 -0
  47. package/dist/{researcher-WJvIpX3L.d.ts → researcher-C_KJyIGg.d.ts} +1 -141
  48. package/dist/rl.d.ts +4 -3
  49. package/dist/rl.js +4 -4
  50. package/dist/run-critic-BAIjX99r.d.ts +56 -0
  51. package/dist/{run-improvement-loop-Bzamo6GB.d.ts → run-improvement-loop-BqYH2vCR.d.ts} +25 -1
  52. package/dist/semantic-concept-judge-CV9Wlx4t.d.ts +650 -0
  53. package/dist/{store-jzKpMl16.d.ts → store-GmBE2pZZ.d.ts} +1 -1
  54. package/dist/traces.d.ts +371 -308
  55. package/dist/traces.js +43 -18
  56. package/dist/{types-CnmZ2bkP.d.ts → types-Bba0vl1V.d.ts} +1 -1
  57. package/dist/{registry-BGKyX6bw.d.ts → types-CRD68aH7.d.ts} +3 -128
  58. package/dist/wire/index.d.ts +1 -1
  59. package/dist/workflow/index.d.ts +494 -0
  60. package/dist/workflow/index.js +2177 -0
  61. package/dist/workflow/index.js.map +1 -0
  62. package/docs/design/self-improvement-roadmap.md +106 -0
  63. package/package.json +36 -12
  64. package/dist/agent-profile-DzcPHR1Z.d.ts +0 -114
  65. package/dist/chunk-ODGETRTM.js.map +0 -1
  66. package/dist/chunk-SL55X4VN.js +0 -186
  67. package/dist/chunk-SL55X4VN.js.map +0 -1
  68. package/dist/chunk-UD6EF73X.js.map +0 -1
  69. /package/dist/{chunk-AIWHLG7J.js.map → chunk-GJJNJVIR.js.map} +0 -0
@@ -0,0 +1,1075 @@
1
+ import {
2
+ AnalystRegistry,
3
+ DEFAULT_TRACE_ANALYST_KINDS,
4
+ computeFindingId,
5
+ createTraceAnalystKind,
6
+ makeFinding
7
+ } from "./chunk-WYIHD6EB.js";
8
+ import {
9
+ LlmClient,
10
+ callLlmJson
11
+ } from "./chunk-IHDHUN2X.js";
12
+ import {
13
+ NotFoundError
14
+ } from "./chunk-3BFEG2F6.js";
15
+
16
+ // src/analyst/ax-service.ts
17
+ import { ai } from "@ax-llm/ax";
18
+ function createAnalystAi(config) {
19
+ return ai({
20
+ name: config.provider ?? "openai",
21
+ apiKey: config.apiKey,
22
+ apiURL: config.baseUrl,
23
+ config: { model: config.model }
24
+ });
25
+ }
26
+
27
+ // src/trace-analyst/behavioral-metrics.ts
28
+ var INPUT_GROWTH_FACTOR = 3;
29
+ var MIN_TOOL_CALLS = 3;
30
+ var VERIFY_RE = /verif|eval|inspect|check|assert|validat|review|confirm/i;
31
+ function num(v) {
32
+ return typeof v === "number" && Number.isFinite(v) ? v : null;
33
+ }
34
+ function inputTokensOf(s) {
35
+ return num(s.attributes["llm.input_tokens"]) ?? num(s.attributes["llm.usage.input_tokens"]);
36
+ }
37
+ function outputTokensOf(s) {
38
+ return num(s.attributes["llm.output_tokens"]) ?? num(s.attributes["llm.usage.output_tokens"]);
39
+ }
40
+ function stepOf(s) {
41
+ return num(s.attributes.step);
42
+ }
43
+ function toolNameOf(s) {
44
+ if (s.tool_name) return s.tool_name;
45
+ const t = s.attributes["tool.name"];
46
+ return typeof t === "string" && t.length > 0 ? t : null;
47
+ }
48
+ function computeTraceMetrics(spans) {
49
+ const ordered = [...spans].sort((a, b) => {
50
+ const sa = stepOf(a);
51
+ const sb = stepOf(b);
52
+ if (sa !== null && sb !== null && sa !== sb) return sa - sb;
53
+ return a.start_time.localeCompare(b.start_time);
54
+ });
55
+ const inputTokenTrajectory = [];
56
+ const outputTokenTrajectory = [];
57
+ const toolHistogram = {};
58
+ let hasSelfVerification = false;
59
+ for (const s of ordered) {
60
+ const inT = inputTokensOf(s);
61
+ if (inT !== null) inputTokenTrajectory.push(inT);
62
+ const outT = outputTokensOf(s);
63
+ if (outT !== null) outputTokenTrajectory.push(outT);
64
+ const tool = toolNameOf(s);
65
+ if (tool) {
66
+ toolHistogram[tool] = (toolHistogram[tool] ?? 0) + 1;
67
+ if (VERIFY_RE.test(tool)) hasSelfVerification = true;
68
+ }
69
+ }
70
+ const totalToolCalls = Object.values(toolHistogram).reduce((a, b) => a + b, 0);
71
+ const distinctTools = Object.keys(toolHistogram).length;
72
+ const toolDiversityRatio = totalToolCalls === 0 ? 1 : distinctTools / totalToolCalls;
73
+ const signals = [];
74
+ if (inputTokenTrajectory.length >= 3) {
75
+ const first = inputTokenTrajectory[0];
76
+ const last = inputTokenTrajectory[inputTokenTrajectory.length - 1];
77
+ const growth = first > 0 ? last / first : 0;
78
+ if (last > first && growth >= INPUT_GROWTH_FACTOR) {
79
+ signals.push({
80
+ code: "monotonic-input-growth",
81
+ severity: "high",
82
+ detail: `LLM input tokens grew ${growth.toFixed(1)}x (${first}\u2192${last}) across ${inputTokenTrajectory.length} calls \u2014 full history re-sent each step with no compression.`,
83
+ evidence: {
84
+ first,
85
+ last,
86
+ growth_x: Number(growth.toFixed(2)),
87
+ calls: inputTokenTrajectory.length
88
+ }
89
+ });
90
+ }
91
+ }
92
+ if (outputTokenTrajectory.length >= 3) {
93
+ const first = outputTokenTrajectory[0];
94
+ const last = outputTokenTrajectory[outputTokenTrajectory.length - 1];
95
+ if (last < first) {
96
+ signals.push({
97
+ code: "output-length-decay",
98
+ severity: "medium",
99
+ detail: `LLM output tokens shrank ${first}\u2192${last} over ${outputTokenTrajectory.length} calls \u2014 less planning/reasoning per step as context grows.`,
100
+ evidence: { first, last, calls: outputTokenTrajectory.length }
101
+ });
102
+ }
103
+ }
104
+ if (totalToolCalls >= MIN_TOOL_CALLS && distinctTools === 1) {
105
+ const only = Object.keys(toolHistogram)[0];
106
+ signals.push({
107
+ code: "single-tool-dependency",
108
+ severity: "medium",
109
+ detail: `All ${totalToolCalls} tool calls are \`${only}\` \u2014 no tool diversity and no fallback path.`,
110
+ evidence: { tool: only, calls: totalToolCalls, distinct_tools: 1 }
111
+ });
112
+ }
113
+ if (totalToolCalls >= MIN_TOOL_CALLS && !hasSelfVerification) {
114
+ signals.push({
115
+ code: "no-self-verification",
116
+ severity: "medium",
117
+ detail: `${totalToolCalls} tool calls and none verify/inspect/check state \u2014 the agent never validates its own actions.`,
118
+ evidence: { tool_calls: totalToolCalls, verification_calls: 0 }
119
+ });
120
+ }
121
+ return {
122
+ llmCallCount: inputTokenTrajectory.length,
123
+ inputTokenTrajectory,
124
+ outputTokenTrajectory,
125
+ toolHistogram,
126
+ totalToolCalls,
127
+ distinctTools,
128
+ toolDiversityRatio,
129
+ hasSelfVerification,
130
+ signals
131
+ };
132
+ }
133
+
134
+ // src/analyst/behavioral-analyst.ts
135
+ var RECOMMENDED_ACTION = {
136
+ "monotonic-input-growth": "Add a context-budget instruction: once prior context exceeds a threshold, summarize earlier steps into a short status line instead of re-sending full history.",
137
+ "output-length-decay": "Require a minimum planning/reasoning budget per step so late steps do not degrade into terse, error-prone commands.",
138
+ "single-tool-dependency": "Direct the agent to use the full toolset (verify / inspect / alternate actions), not a single execute call, and to plan a fallback when a call returns an unexpected result.",
139
+ "no-self-verification": "After every state-mutating action, verify the result (eval / inspect / assert) before proceeding."
140
+ };
141
+ var ANALYST_ID = "efficiency-behavioral";
142
+ function deriveEfficiencyFindings(metrics, opts = {}) {
143
+ const analystId = opts.analystId ?? ANALYST_ID;
144
+ return metrics.signals.map(
145
+ (sig) => makeFinding({
146
+ analyst_id: analystId,
147
+ area: "efficiency",
148
+ subject: sig.code,
149
+ // kebab — passes the cluster grammar; stable key for diffFindings
150
+ claim: sig.detail,
151
+ severity: sig.severity,
152
+ // Deterministic arithmetic over spans, not a model judgment → certain.
153
+ confidence: 1,
154
+ evidence_refs: [
155
+ {
156
+ kind: "metric",
157
+ uri: `metric://efficiency/${sig.code}`,
158
+ excerpt: JSON.stringify(sig.evidence)
159
+ }
160
+ ],
161
+ recommended_action: RECOMMENDED_ACTION[sig.code],
162
+ metadata: { deterministic: true, evidence: sig.evidence },
163
+ ...opts.producedAt ? { produced_at: opts.producedAt } : {}
164
+ })
165
+ );
166
+ }
167
+ function behavioralAnalyst() {
168
+ return {
169
+ id: ANALYST_ID,
170
+ description: "Deterministic behavioral/efficiency findings over OTLP spans \u2014 token-growth, output-decay, tool-monoculture, missing self-verification. Zero LLM; model-agnostic by construction.",
171
+ inputKind: "trace-store",
172
+ cost: { kind: "deterministic" },
173
+ version: "1.0.0",
174
+ async analyze(store) {
175
+ const overview = await store.getOverview();
176
+ const spans = [];
177
+ for (const traceId of overview.sample_trace_ids) {
178
+ const viewed = await store.viewTrace({ trace_id: traceId });
179
+ if (viewed.spans) spans.push(...viewed.spans);
180
+ }
181
+ return deriveEfficiencyFindings(computeTraceMetrics(spans));
182
+ }
183
+ };
184
+ }
185
+
186
+ // src/analyst/default-registry.ts
187
+ function buildDefaultAnalystRegistry(opts = {}) {
188
+ const registry = new AnalystRegistry(opts.registry);
189
+ if (opts.includeBehavioral !== false) {
190
+ registry.register(behavioralAnalyst());
191
+ }
192
+ if (opts.ai) {
193
+ const kinds = opts.kinds ?? DEFAULT_TRACE_ANALYST_KINDS;
194
+ for (const spec of kinds) {
195
+ registry.register(createTraceAnalystKind(spec, { ai: opts.ai, model: opts.model }));
196
+ }
197
+ }
198
+ return registry;
199
+ }
200
+
201
+ // src/concurrency.ts
202
+ var Mutex = class {
203
+ locked = false;
204
+ waiters = [];
205
+ async acquire() {
206
+ if (!this.locked) {
207
+ this.locked = true;
208
+ return () => this.release();
209
+ }
210
+ return new Promise((resolve) => {
211
+ this.waiters.push(() => {
212
+ resolve(() => this.release());
213
+ });
214
+ });
215
+ }
216
+ release() {
217
+ const next = this.waiters.shift();
218
+ if (next) {
219
+ next();
220
+ } else {
221
+ this.locked = false;
222
+ }
223
+ }
224
+ async runExclusive(fn) {
225
+ const release = await this.acquire();
226
+ try {
227
+ return await fn();
228
+ } finally {
229
+ release();
230
+ }
231
+ }
232
+ /** True iff someone holds the lock right now. Diagnostics only. */
233
+ get isLocked() {
234
+ return this.locked;
235
+ }
236
+ /** Pending waiter count. Diagnostics only. */
237
+ get pending() {
238
+ return this.waiters.length;
239
+ }
240
+ };
241
+
242
+ // src/locked-jsonl-appender.ts
243
+ import { appendFileSync, existsSync, mkdirSync } from "fs";
244
+ import { dirname } from "path";
245
+ var mutexes = /* @__PURE__ */ new Map();
246
+ function getMutex(path) {
247
+ let m = mutexes.get(path);
248
+ if (!m) {
249
+ m = new Mutex();
250
+ mutexes.set(path, m);
251
+ }
252
+ return m;
253
+ }
254
+ var LockedJsonlAppender = class {
255
+ constructor(path) {
256
+ this.path = path;
257
+ this.mutex = getMutex(path);
258
+ if (!existsSync(dirname(path))) {
259
+ mkdirSync(dirname(path), { recursive: true });
260
+ }
261
+ }
262
+ path;
263
+ mutex;
264
+ async append(entry) {
265
+ const line = `${JSON.stringify(entry)}
266
+ `;
267
+ await this.mutex.runExclusive(() => {
268
+ appendFileSync(this.path, line);
269
+ });
270
+ }
271
+ };
272
+ function resetLockedAppendersForTesting() {
273
+ mutexes.clear();
274
+ }
275
+
276
+ // src/analyst/findings-store.ts
277
+ import { existsSync as existsSync2, readFileSync } from "fs";
278
+ var FindingsStore = class {
279
+ constructor(path) {
280
+ this.path = path;
281
+ this.appender = new LockedJsonlAppender(path);
282
+ }
283
+ path;
284
+ appender;
285
+ async append(runId, findings) {
286
+ for (const f of findings) {
287
+ const row = { ...f, run_id: runId };
288
+ await this.appender.append(row);
289
+ }
290
+ }
291
+ /** Load every persisted finding. Discards malformed trailing lines silently. */
292
+ loadAll() {
293
+ if (!existsSync2(this.path)) return [];
294
+ const raw = readFileSync(this.path, "utf8");
295
+ if (!raw) return [];
296
+ const out = [];
297
+ for (const line of raw.split("\n")) {
298
+ if (!line) continue;
299
+ try {
300
+ out.push(JSON.parse(line));
301
+ } catch {
302
+ }
303
+ }
304
+ return out;
305
+ }
306
+ /** Filter to a single run. */
307
+ loadRun(runId) {
308
+ return this.loadAll().filter((r) => r.run_id === runId);
309
+ }
310
+ };
311
+ function defaultIsMaterial(a, b) {
312
+ if (a.severity !== b.severity) return true;
313
+ if (Math.abs((a.confidence ?? 0) - (b.confidence ?? 0)) > 0.05) return true;
314
+ if (a.evidence_refs.length !== b.evidence_refs.length) return true;
315
+ return false;
316
+ }
317
+ function diffFindings(previous, current, policy = {}) {
318
+ const isMaterial = policy.isMaterial ?? defaultIsMaterial;
319
+ const prevById = new Map(previous.map((f) => [f.finding_id, f]));
320
+ const curById = new Map(current.map((f) => [f.finding_id, f]));
321
+ const appeared = [];
322
+ const disappeared = [];
323
+ const persisted = [];
324
+ const changed = [];
325
+ for (const [id, cur] of curById) {
326
+ const prev = prevById.get(id);
327
+ if (!prev) {
328
+ appeared.push(cur);
329
+ continue;
330
+ }
331
+ if (isMaterial(prev, cur)) {
332
+ changed.push({ previous: prev, current: cur });
333
+ } else {
334
+ persisted.push(cur);
335
+ }
336
+ }
337
+ for (const [id, prev] of prevById) {
338
+ if (!curById.has(id)) disappeared.push(prev);
339
+ }
340
+ return { appeared, disappeared, persisted, changed };
341
+ }
342
+
343
+ // src/analyst/kinds/skill-usage.ts
344
+ import { existsSync as existsSync3, readdirSync, readFileSync as readFileSync2, statSync } from "fs";
345
+ import { join } from "path";
346
+ var BLOAT_LINE_THRESHOLD = 300;
347
+ var TANGLE_PRIVATE_RE = /\b(cli-bridge|tangletools|ops-board|drew-gtr-pro|@tangle-network\/|~\/company|tangle\.tools|gtm-agent)\b|\bkimi\b|\btcloud\b/gi;
348
+ var TRIGGER_RE = /triggers?\s*[:-]/i;
349
+ function listSkillDirs(root) {
350
+ if (!existsSync3(root)) return [];
351
+ const out = [];
352
+ for (const entry of readdirSync(root, { withFileTypes: true })) {
353
+ if (!entry.isDirectory() && !entry.isSymbolicLink()) continue;
354
+ const skillMd = join(root, entry.name, "SKILL.md");
355
+ if (existsSync3(skillMd)) out.push({ name: entry.name, path: skillMd });
356
+ }
357
+ return out;
358
+ }
359
+ function walkJsonl(dir, cap) {
360
+ if (!existsSync3(dir)) return [];
361
+ const files = [];
362
+ const stack = [dir];
363
+ while (stack.length) {
364
+ const cur = stack.pop();
365
+ let entries;
366
+ try {
367
+ entries = readdirSync(cur, { withFileTypes: true });
368
+ } catch {
369
+ continue;
370
+ }
371
+ for (const e of entries) {
372
+ const full = join(cur, e.name);
373
+ if (e.isDirectory()) stack.push(full);
374
+ else if (e.name.endsWith(".jsonl")) {
375
+ files.push(full);
376
+ if (cap > 0 && files.length >= cap) return files;
377
+ }
378
+ }
379
+ }
380
+ return files;
381
+ }
382
+ function frontmatterDescription(body) {
383
+ const fm = /^---\n([\s\S]*?)\n---/.exec(body);
384
+ const block = fm?.[1] ?? "";
385
+ const m = /description:\s*(.+)/i.exec(block);
386
+ return m?.[1] ?? "";
387
+ }
388
+ function countArtifacts(roots, name, aliases) {
389
+ let n = 0;
390
+ for (const root of roots) {
391
+ const candidates = [join(root, ".evolve", name), ...aliases.map((a) => join(root, a))];
392
+ for (const dir of candidates) {
393
+ if (!existsSync3(dir)) continue;
394
+ try {
395
+ if (statSync(dir).isDirectory()) n += readdirSync(dir).length;
396
+ else n += 1;
397
+ } catch {
398
+ }
399
+ }
400
+ }
401
+ return n;
402
+ }
403
+ function buildSkillUsageReport(config) {
404
+ const skills = config.skillRoots.flatMap(
405
+ ({ root, kind }) => listSkillDirs(root).map((s) => ({ ...s, kind }))
406
+ );
407
+ const names = skills.map((s) => s.name);
408
+ const direct = new Map(names.map((n) => [n, 0]));
409
+ const slash = new Map(names.map((n) => [n, 0]));
410
+ const skillRe = /"skill"\s*:\s*"([a-z0-9_:-]+)"/g;
411
+ const cmdRe = /<command-name>\/?([a-z0-9_:-]+)<\/command-name>/g;
412
+ let transcripts = 0;
413
+ for (const dir of config.transcriptDirs) {
414
+ for (const file of walkJsonl(dir, config.maxTranscriptsPerDir ?? 0)) {
415
+ transcripts += 1;
416
+ let data;
417
+ try {
418
+ data = readFileSync2(file, "utf8");
419
+ } catch {
420
+ continue;
421
+ }
422
+ for (const m of data.matchAll(skillRe)) {
423
+ const g = m[1];
424
+ if (!g) continue;
425
+ const n = g.split(":").pop() ?? g;
426
+ const prev = direct.get(n);
427
+ if (prev !== void 0) direct.set(n, prev + 1);
428
+ }
429
+ for (const m of data.matchAll(cmdRe)) {
430
+ const g = m[1];
431
+ if (g === void 0) continue;
432
+ const prev = slash.get(g);
433
+ if (prev !== void 0) slash.set(g, prev + 1);
434
+ }
435
+ }
436
+ }
437
+ const bodies = /* @__PURE__ */ new Map();
438
+ for (const s of skills) {
439
+ try {
440
+ bodies.set(s.name, readFileSync2(s.path, "utf8"));
441
+ } catch {
442
+ bodies.set(s.name, "");
443
+ }
444
+ }
445
+ const inbound = new Map(names.map((n) => [n, 0]));
446
+ for (const target of names) {
447
+ const ref = new RegExp(`/${target}\\b|\\[\\[${target}\\]\\]`);
448
+ for (const s of skills) {
449
+ if (s.name === target) continue;
450
+ if (ref.test(bodies.get(s.name) ?? "")) inbound.set(target, inbound.get(target) + 1);
451
+ }
452
+ }
453
+ const records = skills.map((s) => {
454
+ const body = bodies.get(s.name) ?? "";
455
+ const dir = s.path.replace(/\/SKILL\.md$/, "");
456
+ return {
457
+ name: s.name,
458
+ kind: s.kind,
459
+ path: s.path,
460
+ lines: body ? body.split("\n").length : 0,
461
+ directInvocations: direct.get(s.name) ?? 0,
462
+ slashInvocations: slash.get(s.name) ?? 0,
463
+ inboundRefs: inbound.get(s.name) ?? 0,
464
+ artifactCount: countArtifacts(
465
+ config.artifactRoots ?? [],
466
+ s.name,
467
+ config.artifactAliases?.[s.name] ?? []
468
+ ),
469
+ tanglePrivateRefs: (body.match(TANGLE_PRIVATE_RE) ?? []).length,
470
+ hasReferencesDir: existsSync3(join(dir, "references")),
471
+ hasEvalsDir: existsSync3(join(dir, "evals")),
472
+ logsRuns: body.includes("skill-runs.jsonl"),
473
+ hasTriggerPhrases: TRIGGER_RE.test(frontmatterDescription(body) || body.slice(0, 600))
474
+ };
475
+ });
476
+ return { generatedFromTraces: transcripts, records };
477
+ }
478
+ var ANALYST_ID2 = "skill-usage";
479
+ function finding(area, subject, claim, severity, confidence, producedAt, recommended, evidenceUri, rationale) {
480
+ return {
481
+ schema_version: "1.0.0",
482
+ finding_id: computeFindingId({ analyst_id: ANALYST_ID2, area, subject, claim }),
483
+ analyst_id: ANALYST_ID2,
484
+ produced_at: producedAt,
485
+ severity,
486
+ area,
487
+ claim,
488
+ rationale,
489
+ evidence_refs: [{ kind: "artifact", uri: evidenceUri }],
490
+ recommended_action: recommended,
491
+ confidence,
492
+ subject
493
+ };
494
+ }
495
+ function emitSkillUsageFindings(report, producedAt) {
496
+ const out = [];
497
+ for (const r of report.records) {
498
+ const directTotal = r.directInvocations + r.slashInvocations;
499
+ const trueUsage = directTotal + r.inboundRefs + r.artifactCount;
500
+ if (trueUsage === 0) {
501
+ out.push(
502
+ finding(
503
+ "skill-usage",
504
+ r.name,
505
+ `Skill '${r.name}' has zero usage across all signals (direct, slash, inbound-refs, artifacts)`,
506
+ "high",
507
+ 0.6,
508
+ producedAt,
509
+ "Confirm the skill covers a real recurring job; if not, deprecate. Zero true usage is the only deterministic deprecation candidate.",
510
+ r.path,
511
+ "No Skill-tool call, no slash invocation, no sibling dispatches to it, and no on-disk artifacts."
512
+ )
513
+ );
514
+ } else if (directTotal === 0 && r.inboundRefs + r.artifactCount > 0) {
515
+ out.push(
516
+ finding(
517
+ "skill-usage",
518
+ r.name,
519
+ `Skill '${r.name}' shows 0 direct invocations but is used via orchestration/artifacts (inbound=${r.inboundRefs}, artifacts=${r.artifactCount})`,
520
+ "info",
521
+ 0.8,
522
+ producedAt,
523
+ "Do NOT treat as unused \u2014 usage is real but logged under parent skills or on disk. Strengthen direct-invocation discovery only if direct use is desired.",
524
+ r.path,
525
+ "The Skill-tool counter undercounts orchestrated/chained leaf skills."
526
+ )
527
+ );
528
+ }
529
+ if (directTotal <= 2 && !r.hasTriggerPhrases) {
530
+ out.push(
531
+ finding(
532
+ "discoverability",
533
+ r.name,
534
+ `Skill '${r.name}' is rarely invoked directly and its description has no explicit trigger phrases`,
535
+ "medium",
536
+ 0.7,
537
+ producedAt,
538
+ "Add a `Triggers:` clause with verbatim user phrases to the frontmatter description so the model auto-invokes it.",
539
+ r.path
540
+ )
541
+ );
542
+ }
543
+ if (r.kind === "public" && r.tanglePrivateRefs > 0) {
544
+ out.push(
545
+ finding(
546
+ "safety",
547
+ r.name,
548
+ `Public skill '${r.name}' carries ${r.tanglePrivateRefs} Tangle-private reference(s)`,
549
+ "high",
550
+ 0.75,
551
+ producedAt,
552
+ "Sanitize incidental internal refs (cli-bridge/kimi/tcloud/~company/private repos) or relocate to a private repo. Verify @tangle-network/* refs are to PUBLISHED packages before treating as a leak.",
553
+ r.path
554
+ )
555
+ );
556
+ }
557
+ if (r.lines > BLOAT_LINE_THRESHOLD && !r.hasReferencesDir) {
558
+ out.push(
559
+ finding(
560
+ "maintainability",
561
+ r.name,
562
+ `Skill '${r.name}' is ${r.lines} lines with no references/ split (progressive disclosure)`,
563
+ "medium",
564
+ 0.8,
565
+ producedAt,
566
+ `Split detail into references/ loaded on demand; keep SKILL.md a short overview. ${r.lines} lines load into every session's context budget.`,
567
+ r.path
568
+ )
569
+ );
570
+ }
571
+ if (!r.hasEvalsDir) {
572
+ out.push(
573
+ finding(
574
+ "data-quality",
575
+ r.name,
576
+ `Skill '${r.name}' ships no evals/`,
577
+ "low",
578
+ 0.6,
579
+ producedAt,
580
+ "Add evals/evals.json with >=3 scenarios proving the skill beats baseline; gives regression coverage.",
581
+ r.path
582
+ )
583
+ );
584
+ }
585
+ if (!r.logsRuns) {
586
+ out.push(
587
+ finding(
588
+ "observability",
589
+ r.name,
590
+ `Skill '${r.name}' never appends to .evolve/skill-runs.jsonl`,
591
+ "low",
592
+ 0.55,
593
+ producedAt,
594
+ "Append one run line to .evolve/skill-runs.jsonl on completion, or declare it a non-logging leaf, so the self-improvement loop can see it ran.",
595
+ r.path
596
+ )
597
+ );
598
+ }
599
+ }
600
+ return out;
601
+ }
602
+ var SkillUsageAnalyst = class {
603
+ id = ANALYST_ID2;
604
+ description = "Deterministic multi-signal skill-usage analysis: flags dead skills, measurement-invisible (orchestrated) usage, discovery gaps, public-repo leaks, bloat, missing evals, and missing run-logging.";
605
+ inputKind = "custom";
606
+ cost = { kind: "deterministic", est_usd_per_run: 0 };
607
+ version = "1.0.0";
608
+ async analyze(input, ctx) {
609
+ const producedAt = ctx.tags?.producedAt ?? (/* @__PURE__ */ new Date()).toISOString();
610
+ ctx.log?.(
611
+ `skill-usage: ${input.records.length} skills over ${input.generatedFromTraces} transcripts`
612
+ );
613
+ return emitSkillUsageFindings(input, producedAt);
614
+ }
615
+ };
616
+ var SKILL_USAGE_ANALYST = new SkillUsageAnalyst();
617
+
618
+ // src/run-score.ts
619
+ var DEFAULT_RUN_SCORE_WEIGHTS = {
620
+ success: 4,
621
+ goalProgress: 2,
622
+ repoGroundedness: 1.5,
623
+ driftPenalty: -1.5,
624
+ toolUseQuality: 1,
625
+ patchQuality: 1.25,
626
+ testReality: 1.5,
627
+ finalGate: 3,
628
+ reviewerBlockers: -2,
629
+ costUsd: -0.2,
630
+ wallSeconds: -0.1
631
+ };
632
+ function aggregateRunScore(score, weights = {}) {
633
+ const w = { ...DEFAULT_RUN_SCORE_WEIGHTS, ...weights };
634
+ return w.success * clamp01(score.success) + w.goalProgress * clamp01(score.goalProgress) + w.repoGroundedness * clamp01(score.repoGroundedness) + w.driftPenalty * clamp01(score.driftPenalty) + w.toolUseQuality * clamp01(score.toolUseQuality) + w.patchQuality * clamp01(score.patchQuality) + w.testReality * clamp01(score.testReality) + w.finalGate * clamp01(score.finalGate) + w.reviewerBlockers * clamp01(score.reviewerBlockers) + w.costUsd * Math.max(0, finiteOrZero(score.costUsd)) + w.wallSeconds * Math.max(0, finiteOrZero(score.wallSeconds) / 60);
635
+ }
636
+ function clamp01(value) {
637
+ if (!Number.isFinite(value)) return 0;
638
+ return Math.max(0, Math.min(1, value));
639
+ }
640
+ function finiteOrZero(value) {
641
+ return Number.isFinite(value) ? value : 0;
642
+ }
643
+
644
+ // src/run-critic.ts
645
+ var DEFAULT_DRIFT_PATTERNS = [
646
+ /https?:\/\//i,
647
+ /\btitle:\s/i,
648
+ /\bsummary:\s/i,
649
+ /\burl:\s/i,
650
+ /\bnpm package usage\b/i,
651
+ /\bnews\b/i
652
+ ];
653
+ var RunCritic = class {
654
+ weights;
655
+ driftPatterns;
656
+ constructor(options = {}) {
657
+ this.weights = options.weights;
658
+ this.driftPatterns = options.driftPatterns ?? DEFAULT_DRIFT_PATTERNS;
659
+ }
660
+ async score(store, runId) {
661
+ const run = await store.getRun(runId);
662
+ if (!run) throw new NotFoundError(`run ${runId} not found`);
663
+ const [spans, events, artifacts, budget] = await Promise.all([
664
+ store.spans({ runId }),
665
+ store.events({ runId }),
666
+ store.artifacts(runId),
667
+ store.budget(runId)
668
+ ]);
669
+ return this.scoreTrace({ run, spans, events, artifacts, budget });
670
+ }
671
+ scoreTrace(trace) {
672
+ const notes = [];
673
+ const llmSpans = trace.spans.filter(
674
+ (s) => s.kind === "llm"
675
+ );
676
+ const toolSpans = trace.spans.filter(
677
+ (s) => s.kind === "tool"
678
+ );
679
+ const judgeSpans = trace.spans.filter(
680
+ (s) => s.kind === "judge"
681
+ );
682
+ const sandboxSpans = trace.spans.filter(
683
+ (s) => s.kind === "sandbox"
684
+ );
685
+ const finalGateSpans = judgeSpans.filter(
686
+ (span) => span.dimension === "final_gate" || span.attributes?.finalGate === true
687
+ );
688
+ const success = trace.run.outcome?.pass === true ? 1 : trace.run.status === "completed" ? 0.5 : 0;
689
+ if (!success) notes.push("run did not complete with pass=true");
690
+ const judgeAverage = judgeSpans.length ? judgeSpans.reduce((sum, span) => sum + normalizeJudgeScore(span.score), 0) / judgeSpans.length : void 0;
691
+ const outcomeScore = typeof trace.run.outcome?.score === "number" ? clamp01(
692
+ trace.run.outcome.score > 1 ? trace.run.outcome.score / 100 : trace.run.outcome.score
693
+ ) : void 0;
694
+ const goalProgress = outcomeScore ?? judgeAverage ?? success;
695
+ const successfulTools = toolSpans.filter((span) => span.status !== "error").length;
696
+ const toolUseQuality = toolSpans.length === 0 ? 0 : successfulTools / toolSpans.length;
697
+ if (toolSpans.length === 0) notes.push("no tool spans recorded");
698
+ const patchEvidence = trace.artifacts.length + toolSpans.filter((span) => /write|edit|patch|apply/i.test(span.toolName)).length;
699
+ const patchQuality = patchEvidence > 0 ? clamp01(patchEvidence / 4) : 0;
700
+ if (!patchQuality) notes.push("no artifact or edit evidence recorded");
701
+ const sandboxTests = sandboxSpans.filter(
702
+ (span) => typeof span.testsTotal === "number" && span.testsTotal > 0
703
+ );
704
+ const testReality = sandboxTests.length ? sandboxTests.reduce(
705
+ (sum, span) => sum + (span.testsPassed ?? 0) / Math.max(1, span.testsTotal ?? 1),
706
+ 0
707
+ ) / sandboxTests.length : toolSpans.some(
708
+ (span) => /\btest|vitest|pytest|jest|build|tsc\b/i.test(JSON.stringify(span.args))
709
+ ) ? 0.4 : 0;
710
+ if (!testReality) notes.push("no real test/build evidence recorded");
711
+ const blockerSpans = judgeSpans.filter((span) => isBlockingJudge(span));
712
+ const finalGateBlockers = finalGateSpans.filter((span) => isBlockingJudge(span));
713
+ const finalGate = finalGateSpans.length ? finalGateBlockers.length ? 0 : 1 : success;
714
+ if (finalGateBlockers.length)
715
+ notes.push(`final gate blocked by ${finalGateBlockers.length} reviewer(s)`);
716
+ else if (!finalGateSpans.length) notes.push("no final gate judgment recorded");
717
+ const reviewerBlockers = judgeSpans.length ? blockerSpans.length / judgeSpans.length : 0;
718
+ if (reviewerBlockers) notes.push(`detected ${blockerSpans.length} blocking reviewer signal(s)`);
719
+ const positiveGroundingSignals = patchEvidence + sandboxSpans.length + llmSpans.filter((span) => looksRepoGrounded(span.output ?? "")).length;
720
+ const driftSignals = llmSpans.filter((span) => this.isDrift(span.output ?? "")).length + trace.events.filter((event) => this.isDrift(JSON.stringify(event.payload))).length;
721
+ const repoGroundedness = positiveGroundingSignals + driftSignals === 0 ? 0 : positiveGroundingSignals / (positiveGroundingSignals + driftSignals);
722
+ const driftPenalty = positiveGroundingSignals + driftSignals === 0 ? 0 : driftSignals / (positiveGroundingSignals + driftSignals);
723
+ if (driftSignals > 0) notes.push(`detected ${driftSignals} drift signal(s)`);
724
+ const costUsd = trace.budget.length ? Math.max(
725
+ ...trace.budget.filter((entry) => entry.dimension === "usd").map((entry) => entry.consumed),
726
+ 0
727
+ ) : llmSpans.reduce((sum, span) => sum + (span.costUsd ?? 0), 0);
728
+ const wallSeconds = trace.run.endedAt && trace.run.startedAt ? Math.max(0, (trace.run.endedAt - trace.run.startedAt) / 1e3) : 0;
729
+ return {
730
+ success,
731
+ goalProgress,
732
+ repoGroundedness,
733
+ driftPenalty,
734
+ toolUseQuality,
735
+ patchQuality,
736
+ testReality,
737
+ finalGate,
738
+ reviewerBlockers,
739
+ costUsd,
740
+ wallSeconds,
741
+ notes
742
+ };
743
+ }
744
+ rank(score) {
745
+ return aggregateRunScore(score, this.weights);
746
+ }
747
+ isDrift(text) {
748
+ return this.driftPatterns.some((pattern) => pattern.test(text));
749
+ }
750
+ };
751
+ function normalizeJudgeScore(score) {
752
+ return score > 1 ? clamp01(score / 10) : clamp01(score);
753
+ }
754
+ function looksRepoGrounded(text) {
755
+ return /(?:src\/|tests?\/|package\.json|tsconfig|\.ts\b|\.tsx\b|git status|pnpm |npm |vitest|pytest|jest)/i.test(
756
+ text
757
+ );
758
+ }
759
+ function isBlockingJudge(span) {
760
+ return span.attributes?.blocking === true || span.attributes?.verdict === "BLOCKING" || positiveNumber(span.attributes?.blockingFindings) || positiveNumber(span.attributes?.highFindings) || span.score <= 2;
761
+ }
762
+ function positiveNumber(value) {
763
+ return typeof value === "number" && value > 0;
764
+ }
765
+
766
+ // src/semantic-concept-judge.ts
767
+ var DEFAULT_COMPLEXITY_WEIGHTS = {
768
+ render: 1,
769
+ integrate: 2,
770
+ compute: 2.5
771
+ };
772
+ var SEMANTIC_CONCEPT_JUDGE_VERSION = "semantic-concept-judge-v1-2026-04-24";
773
+ var DEFAULT_MAX_SOURCE = 45e3;
774
+ var DEFAULT_MAX_HTML = 3e4;
775
+ var DEFAULT_MAX_PER_FILE = 2e4;
776
+ var DEFAULT_TIMEOUT = 18e4;
777
+ var DEFAULT_MODEL = "claude-sonnet-4-6";
778
+ var SEMANTIC_SCHEMA = {
779
+ type: "object",
780
+ additionalProperties: false,
781
+ required: ["summary", "concepts"],
782
+ properties: {
783
+ summary: { type: "string", minLength: 20, maxLength: 600 },
784
+ concepts: {
785
+ type: "array",
786
+ minItems: 1,
787
+ items: {
788
+ type: "object",
789
+ additionalProperties: false,
790
+ required: ["concept", "present", "score", "evidence", "severity"],
791
+ properties: {
792
+ concept: { type: "string", minLength: 1, maxLength: 120 },
793
+ present: { type: "boolean" },
794
+ score: { type: "number", minimum: 0, maximum: 10 },
795
+ evidence: { type: "string", minLength: 5, maxLength: 400 },
796
+ severity: { type: "string", enum: ["critical", "major", "minor", "info"] }
797
+ }
798
+ }
799
+ }
800
+ }
801
+ };
802
+ function truncate(body, cap, label) {
803
+ if (body.length <= cap) return body;
804
+ return `${body.slice(0, cap)}
805
+ \u2026 [truncated ${body.length - cap} chars of ${label}]`;
806
+ }
807
+ function buildPrompt(input, opts) {
808
+ const sourceBlob = input.sourceFiles.filter((f) => f.content.length <= opts.maxPerFileChars).map((f) => `--- FILE: ${f.path} ---
809
+ ${f.content}`).join("\n\n");
810
+ const html = input.servedHtml ?? "";
811
+ return `You are a strict code-review judge evaluating whether an agent's 0-to-1 build actually implements the features the user asked for.
812
+
813
+ You MUST distinguish:
814
+ (a) WORKING code that implements the concept (rendered UI, wired handler, real API call),
815
+ (b) KEYWORD-PRESENT stub (comments mentioning the concept, variable names, TODOs),
816
+ (c) ABSENT (concept nowhere).
817
+
818
+ A comment like "// TODO: add mint button" is NOT present \u2014 score 2-3. Only count a concept as present if there is real functional code: a rendered component, a call handler wired to state or a network call, a computed value actually used.
819
+
820
+ USER REQUEST (what the agent was asked to build):
821
+ ${input.userRequest}
822
+
823
+ ${input.artifactLabel ? `ARTIFACT METADATA:
824
+ name: ${input.artifactLabel}
825
+ description: ${input.artifactDescription ?? ""}
826
+
827
+ ` : ""}EXPECTED CONCEPTS (each must be graded independently):
828
+ ${input.expectedConcepts.map(
829
+ (c, i) => ` ${i + 1}. "${c.name}"${c.keywords?.length ? ` \u2014 hints: [${c.keywords.slice(0, 6).join(" | ")}]` : ""}`
830
+ ).join("\n")}
831
+
832
+ ${html ? `SERVED HTML (what the preview returns when hit):
833
+ ${truncate(html, opts.maxHtmlChars, "HTML")}
834
+
835
+ ` : ""}SOURCE FILES (the agent's workdir):
836
+ ${truncate(sourceBlob, opts.maxSourceChars, "source")}
837
+
838
+ For EACH concept, return:
839
+ - concept: the concept name as given (match exactly)
840
+ - present: boolean \u2014 does a working implementation exist?
841
+ - score: 0-10 \u2014 10 = production-ready; 7 = functional but thin; 4 = partial/stubbed; 2 = keyword-only comment; 0 = absent
842
+ - evidence: cite "<file>:<line>" or "served-html:<selector>" pointing at the strongest supporting code. If the concept is absent or stubbed, explain what's missing.
843
+ - severity:
844
+ "info" when present: true AND score >= 7
845
+ "minor" when present: true AND 4 <= score < 7
846
+ "major" when present: false OR score < 4
847
+ "critical" when the concept is not only absent but a core user flow depends on it
848
+
849
+ Also produce a "summary" (one sentence, 20-600 chars): overall verdict on whether this is a shippable implementation of the user request vs a keyword-dense placeholder.
850
+
851
+ BE SKEPTICAL. Keyword matching already passed \u2014 your job is to catch what keyword matching misses. If the agent shipped a working build, say so. If it shipped a stub, say so. Don't grade on effort.
852
+
853
+ Return STRICT JSON. No prose outside the JSON.`;
854
+ }
855
+ async function runSemanticConceptJudge(input, options = {}) {
856
+ const start = Date.now();
857
+ const totalCount = input.expectedConcepts.length;
858
+ if (totalCount === 0) {
859
+ return {
860
+ kind: "semantic-concept",
861
+ version: SEMANTIC_CONCEPT_JUDGE_VERSION,
862
+ score: 0,
863
+ presentCount: 0,
864
+ totalCount: 0,
865
+ findings: [],
866
+ summary: "no expected concepts declared",
867
+ durationMs: 0,
868
+ costUsd: null,
869
+ available: false,
870
+ error: "no expected concepts declared"
871
+ };
872
+ }
873
+ const opts = {
874
+ model: options.model ?? DEFAULT_MODEL,
875
+ timeoutMs: options.timeoutMs ?? DEFAULT_TIMEOUT,
876
+ maxSourceChars: options.maxSourceChars ?? DEFAULT_MAX_SOURCE,
877
+ maxPerFileChars: options.maxPerFileChars ?? DEFAULT_MAX_PER_FILE,
878
+ maxHtmlChars: options.maxHtmlChars ?? DEFAULT_MAX_HTML,
879
+ llm: options.llm ?? {},
880
+ weightConcepts: options.weightConcepts ?? "mean",
881
+ complexityWeights: { ...DEFAULT_COMPLEXITY_WEIGHTS, ...options.complexityWeights ?? {} }
882
+ };
883
+ const weightForConcept = (spec) => {
884
+ if (opts.weightConcepts === "mean") return 1;
885
+ if (spec.weight != null) return spec.weight;
886
+ if (opts.weightConcepts === "complexity") {
887
+ return opts.complexityWeights[spec.complexity ?? "render"] ?? 1;
888
+ }
889
+ return 1;
890
+ };
891
+ const weightByName = new Map(
892
+ input.expectedConcepts.map((c) => [c.name, weightForConcept(c)])
893
+ );
894
+ try {
895
+ const { value, result } = await callLlmJson(
896
+ {
897
+ model: opts.model,
898
+ messages: [
899
+ {
900
+ role: "system",
901
+ content: "You are a strict code-review judge. Return strict JSON only. No prose outside the JSON. A keyword in a comment is NOT a working implementation."
902
+ },
903
+ { role: "user", content: buildPrompt(input, opts) }
904
+ ],
905
+ jsonSchema: { name: "semantic_concept_judge", schema: SEMANTIC_SCHEMA },
906
+ temperature: 0,
907
+ timeoutMs: opts.timeoutMs
908
+ },
909
+ opts.llm
910
+ );
911
+ if (!value?.concepts || !Array.isArray(value.concepts)) {
912
+ throw new Error('judge returned malformed response \u2014 expected array under "concepts"');
913
+ }
914
+ const findings = value.concepts.map((c) => ({
915
+ concept: String(c.concept),
916
+ present: Boolean(c.present),
917
+ score: Math.max(0, Math.min(10, Number(c.score ?? 0))),
918
+ evidence: String(c.evidence ?? ""),
919
+ severity: ["critical", "major", "minor", "info"].includes(c.severity) ? c.severity : "info"
920
+ }));
921
+ const presentCount = findings.filter((f) => f.present && f.score >= 7).length;
922
+ let weightSum = 0;
923
+ let weightedScoreSum = 0;
924
+ for (const f of findings) {
925
+ const w = weightByName.get(f.concept) ?? 1;
926
+ weightSum += w;
927
+ weightedScoreSum += w * f.score;
928
+ }
929
+ const scoreAvg = weightSum > 0 ? weightedScoreSum / weightSum : findings.reduce((a, f) => a + f.score, 0) / Math.max(1, findings.length);
930
+ return {
931
+ kind: "semantic-concept",
932
+ version: SEMANTIC_CONCEPT_JUDGE_VERSION,
933
+ score: Number((scoreAvg / 10).toFixed(3)),
934
+ presentCount,
935
+ totalCount,
936
+ findings,
937
+ summary: String(value.summary ?? ""),
938
+ durationMs: Date.now() - start,
939
+ costUsd: result.costUsd ?? null,
940
+ available: true
941
+ };
942
+ } catch (err) {
943
+ return {
944
+ kind: "semantic-concept",
945
+ version: SEMANTIC_CONCEPT_JUDGE_VERSION,
946
+ score: 0,
947
+ presentCount: 0,
948
+ totalCount,
949
+ findings: [],
950
+ summary: "",
951
+ durationMs: Date.now() - start,
952
+ costUsd: null,
953
+ available: false,
954
+ error: err instanceof Error ? err.message : String(err)
955
+ };
956
+ }
957
+ }
958
+ function createSemanticConceptJudge(options = {}) {
959
+ return (input) => runSemanticConceptJudge(input, options);
960
+ }
961
+
962
+ // src/analyst/chat-client.ts
963
+ function createChatClient(opts) {
964
+ switch (opts.transport) {
965
+ case "router":
966
+ return wrapLlmClient(
967
+ opts.transport,
968
+ opts.defaultModel,
969
+ new LlmClient({
970
+ baseUrl: opts.baseUrl ?? "https://router.tangle.tools/v1",
971
+ apiKey: opts.apiKey
972
+ })
973
+ );
974
+ case "cli-bridge":
975
+ return wrapLlmClient(
976
+ opts.transport,
977
+ opts.defaultModel,
978
+ new LlmClient({
979
+ baseUrl: opts.baseUrl ?? "http://127.0.0.1:3344/v1",
980
+ apiKey: opts.bearer ?? ""
981
+ })
982
+ );
983
+ case "direct-provider":
984
+ return wrapLlmClient(
985
+ opts.transport,
986
+ opts.defaultModel,
987
+ new LlmClient({
988
+ baseUrl: opts.baseUrl,
989
+ apiKey: opts.apiKey
990
+ })
991
+ );
992
+ case "sandbox-sdk":
993
+ return {
994
+ transport: "sandbox-sdk",
995
+ defaultModel: opts.defaultModel,
996
+ chat: async (req, callOpts) => opts.chat(resolveModel(req, opts.defaultModel), callOpts)
997
+ };
998
+ case "mock":
999
+ return {
1000
+ transport: "mock",
1001
+ defaultModel: opts.defaultModel,
1002
+ chat: async (req, callOpts) => opts.handler(resolveModel(req, opts.defaultModel), callOpts)
1003
+ };
1004
+ }
1005
+ }
1006
+ function wrapLlmClient(transport, defaultModel, inner) {
1007
+ return {
1008
+ transport,
1009
+ defaultModel,
1010
+ chat: async (req, callOpts) => {
1011
+ const resolved = resolveModel(req, defaultModel);
1012
+ const call = inner.call({
1013
+ model: resolved.model,
1014
+ messages: req.messages,
1015
+ jsonMode: req.jsonMode,
1016
+ jsonSchema: req.jsonSchema,
1017
+ temperature: req.temperature,
1018
+ maxTokens: req.maxTokens,
1019
+ timeoutMs: req.timeoutMs
1020
+ });
1021
+ if (!callOpts?.signal) return await call;
1022
+ return await Promise.race([call, abortAsRejection(callOpts.signal)]);
1023
+ }
1024
+ };
1025
+ }
1026
+ function abortAsRejection(signal) {
1027
+ if (signal.aborted) return Promise.reject(toAbortError(signal));
1028
+ return new Promise((_, reject) => {
1029
+ signal.addEventListener("abort", () => reject(toAbortError(signal)), { once: true });
1030
+ });
1031
+ }
1032
+ function toAbortError(signal) {
1033
+ const reason = signal.reason;
1034
+ if (reason instanceof Error) return reason;
1035
+ const e = new Error("ChatClient.chat: aborted");
1036
+ e.name = "AbortError";
1037
+ return e;
1038
+ }
1039
+ function resolveModel(req, defaultModel) {
1040
+ if (req.model) return req;
1041
+ if (!defaultModel) {
1042
+ throw new Error(
1043
+ "ChatClient.chat: no model on request and no defaultModel on the client. Either pass req.model or bind defaultModel at createChatClient()."
1044
+ );
1045
+ }
1046
+ return { ...req, model: defaultModel };
1047
+ }
1048
+
1049
+ export {
1050
+ createAnalystAi,
1051
+ computeTraceMetrics,
1052
+ deriveEfficiencyFindings,
1053
+ behavioralAnalyst,
1054
+ buildDefaultAnalystRegistry,
1055
+ Mutex,
1056
+ LockedJsonlAppender,
1057
+ resetLockedAppendersForTesting,
1058
+ FindingsStore,
1059
+ defaultIsMaterial,
1060
+ diffFindings,
1061
+ buildSkillUsageReport,
1062
+ emitSkillUsageFindings,
1063
+ SkillUsageAnalyst,
1064
+ SKILL_USAGE_ANALYST,
1065
+ DEFAULT_RUN_SCORE_WEIGHTS,
1066
+ aggregateRunScore,
1067
+ clamp01,
1068
+ RunCritic,
1069
+ DEFAULT_COMPLEXITY_WEIGHTS,
1070
+ SEMANTIC_CONCEPT_JUDGE_VERSION,
1071
+ runSemanticConceptJudge,
1072
+ createSemanticConceptJudge,
1073
+ createChatClient
1074
+ };
1075
+ //# sourceMappingURL=chunk-7W4SM7FD.js.map