@tangle-network/agent-runtime 0.198.2 → 0.200.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/dist/{activation-DFRTurvU.d.ts → activation-BPxs-Iu2.d.ts} +4 -2
  2. package/dist/{activation-IBEVN3VI.js → activation-Ct-BTTzF.js} +2 -2
  3. package/dist/{activation-IBEVN3VI.js.map → activation-Ct-BTTzF.js.map} +1 -1
  4. package/dist/agent.d.ts +1 -1
  5. package/dist/agent.js +2 -3
  6. package/dist/agent.js.map +1 -1
  7. package/dist/analyst-loop-DZw5QWT9.js +176 -0
  8. package/dist/analyst-loop-DZw5QWT9.js.map +1 -0
  9. package/dist/analyst-loop.d.ts +1 -11
  10. package/dist/analyst-loop.js +2 -2
  11. package/dist/{coordination-driver-NFvZ5ofi.js → coordination-driver-qdAwriPV.js} +8 -5
  12. package/dist/{coordination-driver-NFvZ5ofi.js.map → coordination-driver-qdAwriPV.js.map} +1 -1
  13. package/dist/{delegate-dN5yooEj.js → delegate-BWHG-zZW.js} +2 -2
  14. package/dist/{delegate-dN5yooEj.js.map → delegate-BWHG-zZW.js.map} +1 -1
  15. package/dist/durable.d.ts +2 -2
  16. package/dist/durable.js +2 -2
  17. package/dist/{graph-CgCVtMuz.js → graph-cCqKhLqz.js} +3 -3
  18. package/dist/{graph-CgCVtMuz.js.map → graph-cCqKhLqz.js.map} +1 -1
  19. package/dist/{improvement-cycle-Cfu6kDOs.js → improvement-cycle-yia1ST3c.js} +17 -7
  20. package/dist/improvement-cycle-yia1ST3c.js.map +1 -0
  21. package/dist/{index-CMTUgh-T.d.ts → index-Br191WbE.d.ts} +190 -142
  22. package/dist/index.d.ts +4 -4
  23. package/dist/index.js +20 -46
  24. package/dist/index.js.map +1 -1
  25. package/dist/intelligence.d.ts +13 -104
  26. package/dist/intelligence.js +7 -406
  27. package/dist/intelligence.js.map +1 -1
  28. package/dist/kernel.d.ts +3 -3
  29. package/dist/kernel.js +8 -9
  30. package/dist/{loop-runner-bin-CAf1OQot.d.ts → loop-runner-bin-BNRdsDOn.d.ts} +3 -3
  31. package/dist/{loop-runner-bin-WQniYJ8C.js → loop-runner-bin-jQ8hXO9J.js} +4 -4
  32. package/dist/{loop-runner-bin-WQniYJ8C.js.map → loop-runner-bin-jQ8hXO9J.js.map} +1 -1
  33. package/dist/loop-runner-bin.d.ts +1 -1
  34. package/dist/loop-runner-bin.js +1 -1
  35. package/dist/mcp/bin.js +3 -3
  36. package/dist/mcp/index.d.ts +2 -2
  37. package/dist/mcp/index.js +4 -4
  38. package/dist/{provision-supervisor-BxsIaJ35.js → provision-supervisor-BA2-GPth.js} +3 -3
  39. package/dist/{provision-supervisor-BxsIaJ35.js.map → provision-supervisor-BA2-GPth.js.map} +1 -1
  40. package/dist/{redact-DqfB7oB4.js → redact-h-oaw11Q.js} +13098 -11222
  41. package/dist/redact-h-oaw11Q.js.map +1 -0
  42. package/dist/{runtime-CHEtvaTY.js → runtime-DeqdBVeC.js} +8 -9
  43. package/dist/{runtime-CHEtvaTY.js.map → runtime-DeqdBVeC.js.map} +1 -1
  44. package/dist/{server-ccGua5tH.js → server-U_k9NUbk.js} +3 -3
  45. package/dist/{server-ccGua5tH.js.map → server-U_k9NUbk.js.map} +1 -1
  46. package/dist/{stream-agent-turn-CLOQr497.d.ts → stream-agent-turn-CJWthifS.d.ts} +42 -13
  47. package/dist/{structural-rollout-BmDuXyR9.js → structural-rollout-BcMmrpv4.js} +2 -3
  48. package/dist/{structural-rollout-BmDuXyR9.js.map → structural-rollout-BcMmrpv4.js.map} +1 -1
  49. package/dist/{supervise-DVt8-TI-.js → supervise-DmFO50N6.js} +497 -90
  50. package/dist/supervise-DmFO50N6.js.map +1 -0
  51. package/dist/testing.d.ts +2 -2
  52. package/dist/testing.js +12 -12
  53. package/dist/tui/index.d.ts +1 -1
  54. package/dist/tui/index.js +1 -1
  55. package/package.json +3 -3
  56. package/dist/analyst-loop-BknOQUW5.js +0 -546
  57. package/dist/analyst-loop-BknOQUW5.js.map +0 -1
  58. package/dist/improvement-cycle-Cfu6kDOs.js.map +0 -1
  59. package/dist/redact-DqfB7oB4.js.map +0 -1
  60. package/dist/sandbox-events-DbC2WKKS.js +0 -929
  61. package/dist/sandbox-events-DbC2WKKS.js.map +0 -1
  62. package/dist/supervise-DVt8-TI-.js.map +0 -1
package/dist/testing.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { An as RunGraphTestOptions, Ho as SuperviseTestOptions, Pn as runGraphWithTestBrain, Wo as superviseWithTestBrain, cr as driverAgent, sr as DriverAgentOptions, us as SupervisorAgentTestDeps, ys as supervisorAgentWithTestBrain } from "./index-CMTUgh-T.js";
2
- import { Tn as ToolLoopChat, wn as ToolLoopCallContext } from "./stream-agent-turn-CLOQr497.js";
1
+ import { An as RunGraphTestOptions, Bo as SuperviseTestOptions, Ho as superviseWithTestBrain, Pn as runGraphWithTestBrain, _s as supervisorAgentWithTestBrain, ar as DriverAgentOptions, cs as SupervisorAgentTestDeps, or as driverAgent } from "./index-Br191WbE.js";
2
+ import { Tn as ToolLoopChat, wn as ToolLoopCallContext } from "./stream-agent-turn-CJWthifS.js";
3
3
  import { AgentImprovementProposal, AgentProfile, AgentProfileImprovementMeasuredComparison, SandboxSizePreset } from "@tangle-network/agent-interface";
4
4
  //#region src/testing/index.d.ts
5
5
  /** A proposal produced by Runtime's opaque profile-improvement path. */
package/dist/testing.js CHANGED
@@ -1,14 +1,14 @@
1
- import { f as verifyAgentImprovementProposal } from "./improvement-cycle-Cfu6kDOs.js";
1
+ import { f as verifyAgentImprovementProposal } from "./improvement-cycle-yia1ST3c.js";
2
2
  import { o as canonicalCandidateDigest$1, u as immutableCandidateValue } from "./protected-redaction-wGo44k2K.js";
3
3
  import { M as parseExactAgentProfile, w as applyExactAgentProfileDiff } from "./prepare-CAO1yXov.js";
4
- import { p as supervisorAgentWithTestBrain, r as superviseWithTestBrain } from "./supervise-DVt8-TI-.js";
5
- import { r as driverAgent } from "./coordination-driver-NFvZ5ofi.js";
6
- import { i as runGraphWithTestBrain } from "./graph-CgCVtMuz.js";
4
+ import { p as supervisorAgentWithTestBrain, r as superviseWithTestBrain } from "./supervise-DmFO50N6.js";
5
+ import { r as driverAgent } from "./coordination-driver-qdAwriPV.js";
6
+ import { i as runGraphWithTestBrain } from "./graph-cCqKhLqz.js";
7
7
  import { SANDBOX_SIZE_PRESET_NAMES } from "@tangle-network/agent-interface";
8
8
  //#region src/testing/fixtures/agent-improvement-proposal.json
9
9
  var agent_improvement_proposal_default = {
10
10
  changedSurfaces: ["prompt"],
11
- digest: "sha256:324b80494638dd39e198ce560cc9a848189cc42eaa05c5e9b442c659e1951c06",
11
+ digest: "sha256:240186a31dc1452a779e85c4b174090e3f8fb84494ac87c5f82e0280598c2bb1",
12
12
  evaluation: {
13
13
  "decision": {
14
14
  "contributingChecks": [
@@ -4579,7 +4579,7 @@ var agent_improvement_proposal_default = {
4579
4579
  ],
4580
4580
  "metadata": {
4581
4581
  "fixture": "agent-improvement-proposal",
4582
- "runtimeVersion": "0.198.2"
4582
+ "runtimeVersion": "0.200.0"
4583
4583
  },
4584
4584
  "objectives": [
4585
4585
  {
@@ -4690,8 +4690,8 @@ var agent_improvement_proposal_default = {
4690
4690
  "baselineContentHash": "sha256:5c21ee53e513fc604cb09754e21c392b24a424da0ef37dbf8f1ee4a8a0b08f09",
4691
4691
  "candidateContentHash": "sha256:60fcbb1c728194bd51d7d19cb732d1c3f1881dce7e0a6266b41c8b98cfd65693",
4692
4692
  "kind": "agent-eval-loop",
4693
- "recordDigest": "sha256:84663d01050ad4f72231f88b54872145f4bd2582567c9707731de60589bd3cbd",
4694
- "runId": "agent-runtime-0.198.2-proposal-fixture",
4693
+ "recordDigest": "sha256:797845421140b7afdfaeef76018260dd0775bdc2f0acb51e4748f6f22233d699",
4694
+ "runId": "agent-runtime-0.200.0-proposal-fixture",
4695
4695
  "schema": "agent-candidate-experiment"
4696
4696
  }
4697
4697
  },
@@ -4714,13 +4714,13 @@ var agent_improvement_proposal_default = {
4714
4714
  }],
4715
4715
  kind: "agent-improvement-proposal",
4716
4716
  proposedAt: "2026-07-10T01:00:00.000Z",
4717
- runId: "agent-runtime-0.198.2-proposal-fixture"
4717
+ runId: "agent-runtime-0.200.0-proposal-fixture"
4718
4718
  };
4719
4719
  //#endregion
4720
4720
  //#region src/testing/fixtures/agent-profile-improvement-proposal.json
4721
4721
  var agent_profile_improvement_proposal_default = {
4722
4722
  changedSurfaces: ["prompt", "skills"],
4723
- digest: "sha256:1d0d2cf990325c020efaf98743e18c12c7335f461671b0273e25574d8c98e7dc",
4723
+ digest: "sha256:6e4a8ef4eab876ad23c1eac8e76e20f9f58506c25f64591b51d8e041414b8a3e",
4724
4724
  evaluation: {
4725
4725
  "decision": {
4726
4726
  "contributingChecks": [
@@ -6354,7 +6354,7 @@ var agent_profile_improvement_proposal_default = {
6354
6354
  ],
6355
6355
  "metadata": {
6356
6356
  "fixture": "agent-profile-improvement-proposal",
6357
- "runtimeVersion": "0.198.2"
6357
+ "runtimeVersion": "0.200.0"
6358
6358
  },
6359
6359
  "objectives": [
6360
6360
  {
@@ -6465,7 +6465,7 @@ var agent_profile_improvement_proposal_default = {
6465
6465
  "baselineContentHash": "sha256:21c495a37c418c10bde64fbaa188beddeed31f1f051ea60a6a6582a9ee0db704",
6466
6466
  "candidateContentHash": "sha256:103f77bc8481601eef1ad5fe6ba84a40dffabc3a44f421f8c8559121edab84e9",
6467
6467
  "kind": "agent-eval-loop",
6468
- "recordDigest": "sha256:e678b55393bec74768edba3fe3a5c170516db3968156287c2d0d39aba42334d7",
6468
+ "recordDigest": "sha256:5a209130c625a814b2f0498d629d9927d5d0658649f3852a551ada9a153e8df6",
6469
6469
  "runId": "profile-improvement-1",
6470
6470
  "schema": "agent-profile-improvement-experiment"
6471
6471
  }
@@ -1,4 +1,4 @@
1
- import { an as SupervisorCleanupReceipt, in as ProvisionedSupervisor, nn as ProvisionSupervisorConnection, on as provisionSupervisor, rn as ProvisionSupervisorRequest } from "../index-CMTUgh-T.js";
1
+ import { an as SupervisorCleanupReceipt, in as ProvisionedSupervisor, nn as ProvisionSupervisorConnection, on as provisionSupervisor, rn as ProvisionSupervisorRequest } from "../index-Br191WbE.js";
2
2
  //#region src/tui/top-app.d.ts
3
3
  /**
4
4
  * The interactive side of the supervisor-run TUI: a keypress/mouse loop over the frames
package/dist/tui/index.js CHANGED
@@ -1,3 +1,3 @@
1
- import { t as provisionSupervisor } from "../provision-supervisor-BxsIaJ35.js";
1
+ import { t as provisionSupervisor } from "../provision-supervisor-BA2-GPth.js";
2
2
  import { a as renderTopFrameWithLayout, i as renderTopFrame, n as runTopApp, r as loadTopSnapshot, t as renderTopOnce } from "../top-app-KgIFpD9H.js";
3
3
  export { loadTopSnapshot, provisionSupervisor, renderTopFrame, renderTopFrameWithLayout, renderTopOnce, runTopApp };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tangle-network/agent-runtime",
3
- "version": "0.198.2",
3
+ "version": "0.200.0",
4
4
  "description": "Shared task-lifecycle skeleton for agents: a recursive loop kernel for chat turns, one-shot tasks, and multi-attempt loops, with trace capture and eval-gated self-improvement. Domain behavior lives in adapters; scoring and ship-gates in @tangle-network/agent-eval.",
5
5
  "homepage": "https://github.com/tangle-network/agent-runtime#readme",
6
6
  "repository": {
@@ -99,7 +99,7 @@
99
99
  "@biomejs/biome": "^2.5.11",
100
100
  "@modelcontextprotocol/sdk": "1.30.0",
101
101
  "@tangle-network/agent-eval": ">=0.175.0 <0.176.0",
102
- "@tangle-network/agent-interface": "^2.3.0",
102
+ "@tangle-network/agent-interface": "^2.4.0",
103
103
  "@tangle-network/sandbox": ">=0.36.4 <0.38.0",
104
104
  "@types/node": "26.4.0",
105
105
  "@types/tar-stream": "3.1.4",
@@ -122,7 +122,7 @@
122
122
  "license": "MIT",
123
123
  "peerDependencies": {
124
124
  "@tangle-network/agent-eval": ">=0.175.0 <0.176.0",
125
- "@tangle-network/agent-interface": "^2.3.0",
125
+ "@tangle-network/agent-interface": "^2.4.0",
126
126
  "@tangle-network/sandbox": ">=0.36.4 <0.38.0"
127
127
  },
128
128
  "dependencies": {
@@ -1,546 +0,0 @@
1
- import { n as AnalystError } from "./errors-DodWX-cb.js";
2
- import { a as extractLlmCallEvent } from "./sandbox-events-DbC2WKKS.js";
3
- import { DEFAULT_TRACE_ANALYST_BUDGETS, TRACE_ANALYST_TRUNCATION_MARKER_PREFIX, diffFindings } from "@tangle-network/agent-eval";
4
- //#region src/analyst-loop/iterations-to-trace-store.ts
5
- /**
6
- *
7
- * The read seam that closes the autonomous loop: project a round's `Iteration[]`
8
- * (each carrying its raw `SandboxEvent[]`) into an in-memory `TraceAnalysisStore`, the
9
- * read interface the trace analysts query. `runAnalystLoop` has had zero consumers
10
- * because nothing turned a loop's iterations into a store — this is that bridge.
11
- *
12
- * One iteration → one trace. The iteration is the root AGENT span; each `SandboxEvent`
13
- * becomes a child span (LLM for llm_call events, TOOL for tool events, else SPAN), so an
14
- * analyst can walk a shot's trace, cluster its errors, and emit findings the driver
15
- * steers on. Projection is best-effort over the FLAT SandboxEvent shape (no per-event
16
- * lineage yet — that's the richer-trace gap); it never fabricates — an errored iteration
17
- * surfaces a real ERROR span carrying the real message.
18
- *
19
- * @experimental
20
- */
21
- const bytesOf = (v) => Buffer.byteLength(JSON.stringify(v) ?? "", "utf8");
22
- const iso = (ms) => new Date(ms).toISOString();
23
- /** Normalize volatile tokens out of a status message so semantically identical failures
24
- * collapse to one signature (digits, hex/uuids, paths, durations → placeholders). */
25
- function normalizeSignature(message) {
26
- return message.replace(/0x[0-9a-fA-F]+/g, "HEX").replace(/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/g, "UUID").replace(/(\/[\w.-]+){2,}/g, "PATH").replace(/\b\d+(\.\d+)?(ms|s|m|h)\b/g, "DUR").replace(/\b\d+\b/g, "#").replace(/\s+/g, " ").trim().slice(0, 200);
27
- }
28
- function spanKindFor(event, agentRunName) {
29
- const llm = extractLlmCallEvent(event, agentRunName);
30
- if (llm) return {
31
- kind: "LLM",
32
- model: llm.model ?? null,
33
- tool: null
34
- };
35
- const type = String(event?.type ?? "");
36
- if (/tool/i.test(type)) {
37
- const d = event?.data;
38
- return {
39
- kind: "TOOL",
40
- model: null,
41
- tool: typeof d?.name === "string" ? d.name : typeof d?.tool === "string" ? d.tool : type
42
- };
43
- }
44
- return {
45
- kind: "SPAN",
46
- model: null,
47
- tool: null
48
- };
49
- }
50
- function errorMessageOf(event) {
51
- const type = String(event?.type ?? "");
52
- const d = event?.data;
53
- if (/error|fail/i.test(type) || d?.error) {
54
- const m = d?.error ?? d?.message;
55
- return typeof m === "string" ? m : `${type} error`;
56
- }
57
- }
58
- /** Project one iteration → one trace: root AGENT span + a child span per event. */
59
- function projectIteration(iter) {
60
- const traceId = (iter.events.find((e) => (e?.data)?.sandboxId)?.data)?.sandboxId ?? `iter-${iter.index}`;
61
- const start = iso(iter.startedAt);
62
- const end = iso(iter.endedAt || iter.startedAt);
63
- const durationMs = Math.max(0, (iter.endedAt || iter.startedAt) - iter.startedAt);
64
- const rootId = `${traceId}:root`;
65
- const iterErrored = Boolean(iter.error) || iter.verdict?.valid === false;
66
- const spans = [{
67
- trace_id: traceId,
68
- span_id: rootId,
69
- parent_span_id: null,
70
- name: iter.agentRunName,
71
- kind: "AGENT",
72
- start_time: start,
73
- end_time: end,
74
- duration_ms: durationMs,
75
- status: iter.error ? "ERROR" : "OK",
76
- status_message: iter.error?.message,
77
- service_name: "agent-runtime",
78
- agent_name: iter.agentRunName,
79
- model_name: null,
80
- tool_name: null,
81
- attributes: {
82
- "iteration.index": iter.index,
83
- "verdict.valid": iter.verdict?.valid,
84
- "verdict.score": iter.verdict?.score,
85
- "output.preview": iter.output === void 0 ? void 0 : String(iter.output).slice(0, 2e3)
86
- }
87
- }];
88
- const models = /* @__PURE__ */ new Set();
89
- const tools = /* @__PURE__ */ new Set();
90
- iter.events.forEach((event, i) => {
91
- const { kind, model, tool } = spanKindFor(event, iter.agentRunName);
92
- if (model) models.add(model);
93
- if (tool) tools.add(tool);
94
- const errMsg = errorMessageOf(event);
95
- spans.push({
96
- trace_id: traceId,
97
- span_id: `${traceId}:e${i}`,
98
- parent_span_id: rootId,
99
- name: String(event?.type ?? "event"),
100
- kind,
101
- start_time: start,
102
- end_time: end,
103
- duration_ms: 0,
104
- status: errMsg ? "ERROR" : "OK",
105
- status_message: errMsg,
106
- service_name: "agent-runtime",
107
- agent_name: iter.agentRunName,
108
- model_name: model,
109
- tool_name: tool,
110
- attributes: event?.data ?? {}
111
- });
112
- });
113
- const hasErrors = spans.some((s) => s.status === "ERROR") || iterErrored;
114
- const summary = {
115
- trace_id: traceId,
116
- service_name: "agent-runtime",
117
- agent_name: iter.agentRunName,
118
- span_count: spans.length,
119
- has_errors: hasErrors,
120
- start_time: start,
121
- end_time: end,
122
- duration_ms: durationMs,
123
- raw_jsonl_bytes: bytesOf(spans),
124
- models: [...models],
125
- tools: [...tools]
126
- };
127
- return {
128
- summary,
129
- spans,
130
- rawBytes: summary.raw_jsonl_bytes
131
- };
132
- }
133
- function matchesFilters(t, f) {
134
- if (!f) return true;
135
- if (f.has_errors !== void 0 && t.summary.has_errors !== f.has_errors) return false;
136
- if (f.service_names?.length && !f.service_names.includes(t.summary.service_name ?? "")) return false;
137
- if (f.agent_names?.length && !f.agent_names.includes(t.summary.agent_name ?? "")) return false;
138
- if (f.model_names?.length && !f.model_names.some((m) => t.summary.models.includes(m))) return false;
139
- if (f.tool_names?.length && !f.tool_names.some((tn) => t.summary.tools.includes(tn))) return false;
140
- if (f.start_time_after && t.summary.start_time < f.start_time_after) return false;
141
- if (f.start_time_before && t.summary.start_time > f.start_time_before) return false;
142
- if (f.regex_pattern && !new RegExp(f.regex_pattern).test(JSON.stringify(t.spans))) return false;
143
- return true;
144
- }
145
- function capAttributes(attributes, perAttrCap) {
146
- let truncated = 0;
147
- const capped = {};
148
- for (const [k, v] of Object.entries(attributes)) {
149
- const s = typeof v === "string" ? v : JSON.stringify(v);
150
- if (typeof s === "string" && s.length > perAttrCap) {
151
- truncated += 1;
152
- capped[k] = `${TRACE_ANALYST_TRUNCATION_MARKER_PREFIX} ${s.length}b]${s.slice(0, perAttrCap)}`;
153
- } else capped[k] = v;
154
- }
155
- return {
156
- capped,
157
- truncated
158
- };
159
- }
160
- /**
161
- * Build an in-memory `TraceAnalysisStore` over a loop round's iterations. Fail-loud on an
162
- * empty round — there is nothing for an analyst to read, and a silent empty store would
163
- * mask a broken capture path.
164
- */
165
- function iterationsToTraceStore(iterations, budgets = DEFAULT_TRACE_ANALYST_BUDGETS) {
166
- if (iterations.length === 0) throw new AnalystError("iterationsToTraceStore: no iterations to analyze (empty round)");
167
- const traces = iterations.map((it) => projectIteration(it));
168
- const byId = new Map(traces.map((t) => [t.summary.trace_id, t]));
169
- const buildClusters = (set) => {
170
- const map = /* @__PURE__ */ new Map();
171
- for (const t of set) for (const s of t.spans) {
172
- if (s.status !== "ERROR" || !s.status_message) continue;
173
- const sig = normalizeSignature(s.status_message);
174
- const c = map.get(sig) ?? {
175
- signature: sig,
176
- status_message_sample: s.status_message,
177
- span_name: s.name,
178
- tool_name: s.tool_name,
179
- trace_count: 0,
180
- span_count: 0,
181
- prevalence: 0,
182
- exemplar_trace_ids: [],
183
- exemplar_span_ids: []
184
- };
185
- c.span_count += 1;
186
- if (!c.exemplar_trace_ids.includes(t.summary.trace_id) && c.exemplar_trace_ids.length < 10) {
187
- c.exemplar_trace_ids.push(t.summary.trace_id);
188
- c.trace_count += 1;
189
- }
190
- if (c.exemplar_span_ids.length < 10) c.exemplar_span_ids.push(s.span_id);
191
- map.set(sig, c);
192
- }
193
- const errorTraces = set.filter((t) => t.summary.has_errors).length || 1;
194
- return [...map.values()].map((c) => ({
195
- ...c,
196
- prevalence: c.trace_count / errorTraces
197
- })).sort((a, b) => b.trace_count - a.trace_count);
198
- };
199
- return {
200
- async hasTrace(trace_id) {
201
- return byId.has(trace_id);
202
- },
203
- async hasSpans(opts) {
204
- const t = byId.get(opts.trace_id);
205
- if (!t) return [];
206
- const present = new Set(t.spans.map((s) => s.span_id));
207
- return opts.span_ids.filter((id) => present.has(id));
208
- },
209
- async getOverview(filters) {
210
- const set = traces.filter((t) => matchesFilters(t, filters));
211
- const services = /* @__PURE__ */ new Set();
212
- const agents = /* @__PURE__ */ new Set();
213
- const models = /* @__PURE__ */ new Set();
214
- const tools = /* @__PURE__ */ new Set();
215
- let errorSpans = 0;
216
- for (const t of set) {
217
- if (t.summary.service_name) services.add(t.summary.service_name);
218
- if (t.summary.agent_name) agents.add(t.summary.agent_name);
219
- for (const m of t.summary.models) models.add(m);
220
- for (const tn of t.summary.tools) tools.add(tn);
221
- errorSpans += t.spans.filter((s) => s.status === "ERROR").length;
222
- }
223
- const times = set.map((t) => t.summary.start_time).sort();
224
- return {
225
- total_traces: set.length,
226
- raw_jsonl_bytes: set.reduce((n, t) => n + t.rawBytes, 0),
227
- services: [...services],
228
- agents: [...agents],
229
- models: [...models],
230
- tool_names: [...tools],
231
- sample_trace_ids: set.slice(0, 20).map((t) => t.summary.trace_id),
232
- errors: {
233
- trace_count: set.filter((t) => t.summary.has_errors).length,
234
- span_count: errorSpans
235
- },
236
- error_clusters: buildClusters(set),
237
- time_range: times.length ? {
238
- earliest: times[0],
239
- latest: times[times.length - 1]
240
- } : null
241
- };
242
- },
243
- async queryTraces(opts) {
244
- const set = traces.filter((t) => matchesFilters(t, opts.filters));
245
- const offset = opts.offset ?? 0;
246
- return {
247
- traces: set.slice(offset, offset + opts.limit).map((t) => t.summary),
248
- total: set.length,
249
- has_more: offset + opts.limit < set.length
250
- };
251
- },
252
- async countTraces(filters) {
253
- return traces.filter((t) => matchesFilters(t, filters)).length;
254
- },
255
- async viewTrace(opts) {
256
- const t = byId.get(opts.trace_id);
257
- if (!t) return {
258
- trace_id: opts.trace_id,
259
- spans: []
260
- };
261
- const cap = opts.per_attribute_byte_cap ?? budgets.perAttributeViewBudget;
262
- const projected = t.spans.map((s) => ({
263
- ...s,
264
- attributes: capAttributes(s.attributes, cap).capped
265
- }));
266
- if (bytesOf(projected) > budgets.perCallByteCeiling) {
267
- const names = /* @__PURE__ */ new Map();
268
- for (const s of t.spans) names.set(s.name, (names.get(s.name) ?? 0) + 1);
269
- return {
270
- trace_id: opts.trace_id,
271
- oversized: {
272
- span_count: t.spans.length,
273
- top_span_names: [...names.entries()].sort((a, b) => b[1] - a[1]).slice(0, 20),
274
- span_response_bytes_max: Math.max(...t.spans.map((s) => bytesOf(s))),
275
- error_span_count: t.spans.filter((s) => s.status === "ERROR").length
276
- }
277
- };
278
- }
279
- return {
280
- trace_id: opts.trace_id,
281
- spans: projected
282
- };
283
- },
284
- async viewSpans(opts) {
285
- const t = byId.get(opts.trace_id);
286
- const cap = opts.per_attribute_byte_cap ?? budgets.perAttributeSpanBudget;
287
- const want = new Set(opts.span_ids);
288
- const found = (t?.spans ?? []).filter((s) => want.has(s.span_id));
289
- let truncated = 0;
290
- let bytes = 0;
291
- const spans = [];
292
- const omitted = [];
293
- for (const s of found) {
294
- const { capped, truncated: n } = capAttributes(s.attributes, cap);
295
- const projected = {
296
- ...s,
297
- attributes: capped
298
- };
299
- const size = bytesOf(projected);
300
- if (spans.length > 0 && bytes + size > budgets.perCallByteCeiling) {
301
- omitted.push(s.span_id);
302
- continue;
303
- }
304
- truncated += n;
305
- bytes += size;
306
- spans.push(projected);
307
- }
308
- const foundIds = new Set(found.map((s) => s.span_id));
309
- return {
310
- trace_id: opts.trace_id,
311
- spans,
312
- missing_span_ids: opts.span_ids.filter((id) => !foundIds.has(id)),
313
- omitted_span_ids: omitted,
314
- has_more: omitted.length > 0,
315
- truncated_attribute_count: truncated
316
- };
317
- },
318
- async searchTrace(opts) {
319
- const t = byId.get(opts.trace_id);
320
- const max = opts.max_matches ?? 50;
321
- const hits = [];
322
- for (const s of t?.spans ?? []) for (const hit of searchSpanAttrs(s, opts.regex_pattern, budgets.perMatchTextBudget)) {
323
- if (hits.length >= max) break;
324
- hits.push(hit);
325
- }
326
- return {
327
- trace_id: opts.trace_id,
328
- hits,
329
- has_more: hits.length >= max
330
- };
331
- },
332
- async searchSpan(opts) {
333
- const t = byId.get(opts.trace_id);
334
- const max = opts.max_matches ?? 50;
335
- const span = (t?.spans ?? []).find((s) => s.span_id === opts.span_id);
336
- const all = span ? searchSpanAttrs(span, opts.regex_pattern, budgets.perMatchTextBudget) : [];
337
- const hits = all.slice(0, max);
338
- return {
339
- trace_id: opts.trace_id,
340
- span_id: opts.span_id,
341
- hits,
342
- has_more: all.length > hits.length
343
- };
344
- }
345
- };
346
- }
347
- function searchSpanAttrs(span, pattern, textCap) {
348
- const re = new RegExp(pattern, "g");
349
- const hits = [];
350
- for (const [k, v] of Object.entries(span.attributes)) {
351
- const text = typeof v === "string" ? v : JSON.stringify(v);
352
- if (typeof text !== "string") continue;
353
- re.lastIndex = 0;
354
- const m = re.exec(text);
355
- if (!m) continue;
356
- const at = m.index;
357
- hits.push({
358
- trace_id: span.trace_id,
359
- span_id: span.span_id,
360
- span_name: span.name,
361
- span_kind: span.kind,
362
- attribute_path: `attributes.${k}`,
363
- matched_text: m[0].slice(0, textCap),
364
- context_before: text.slice(Math.max(0, at - textCap / 2), at),
365
- context_after: text.slice(at + m[0].length, at + m[0].length + textCap / 2),
366
- match_offset: at
367
- });
368
- }
369
- return hits;
370
- }
371
- //#endregion
372
- //#region src/analyst-loop/run-analyst-loop.ts
373
- /** Analyze a run and apply accepted knowledge and agent-surface proposals. */
374
- async function runAnalystLoop(opts) {
375
- const log = opts.log ?? defaultLog;
376
- const strategy = opts.priorFindingsStrategy ?? "per-kind";
377
- const emit = makeEmitter(opts.onEvent);
378
- const startedAt = Date.now();
379
- const baselineRunId = resolveBaselineRunId(opts);
380
- const priorAll = baselineRunId ? opts.findingsStore?.loadRun(baselineRunId) ?? [] : [];
381
- log("baseline resolved", {
382
- baselineRunId,
383
- prior_findings: priorAll.length
384
- });
385
- await emit({
386
- type: "baseline-resolved",
387
- runId: opts.runId,
388
- baselineRunId,
389
- priorFindingCount: priorAll.length
390
- });
391
- const analystResult = await runRegistry(opts, buildPriorFindingsInput(priorAll, strategy, opts.registry.list()), emit);
392
- log("analyst run complete", {
393
- findings: analystResult.findings.length,
394
- cost_usd: analystResult.total_cost_usd,
395
- per_analyst: analystResult.per_analyst.map((s) => ({
396
- id: s.analyst_id,
397
- status: s.status,
398
- n: s.findings_count
399
- }))
400
- });
401
- if (opts.findingsStore && analystResult.findings.length > 0) {
402
- await opts.findingsStore.append(opts.runId, analystResult.findings);
403
- await emit({
404
- type: "findings-persisted",
405
- runId: opts.runId,
406
- count: analystResult.findings.length
407
- });
408
- }
409
- let diff = null;
410
- if (baselineRunId && analystResult.findings.length > 0) {
411
- diff = diffFindings(priorAll.map((f) => ({ ...f })), analystResult.findings.map((f) => ({
412
- ...f,
413
- run_id: opts.runId
414
- })));
415
- log("diff vs baseline", {
416
- appeared: diff.appeared.length,
417
- disappeared: diff.disappeared.length,
418
- persisted: diff.persisted.length,
419
- changed: diff.changed.length
420
- });
421
- await emit({
422
- type: "diff-computed",
423
- runId: opts.runId,
424
- baselineRunId,
425
- appeared: diff.appeared.length,
426
- disappeared: diff.disappeared.length,
427
- persisted: diff.persisted.length,
428
- changed: diff.changed.length
429
- });
430
- }
431
- let knowledge = null;
432
- if (opts.knowledgeProposalSource) knowledge = await runKnowledgeProposalSource(opts, analystResult.findings, log, emit);
433
- let improvement = null;
434
- if (opts.improvementProposalSource) improvement = await runImprovementProposalSource(opts, analystResult.findings, log, emit);
435
- const durationMs = Math.max(0, Date.now() - startedAt);
436
- await emit({
437
- type: "loop-completed",
438
- runId: opts.runId,
439
- durationMs
440
- });
441
- return {
442
- runId: opts.runId,
443
- baselineRunId,
444
- durationMs,
445
- analystResult,
446
- diff,
447
- knowledge,
448
- improvement
449
- };
450
- }
451
- function makeEmitter(onEvent) {
452
- if (!onEvent) return async () => {};
453
- return async (event) => {
454
- await onEvent(event);
455
- };
456
- }
457
- async function runRegistry(opts, priorFindings, emit) {
458
- const reg = opts.registry;
459
- const registryOptions = {
460
- ...priorFindings ? { priorFindings } : {},
461
- ...opts.chainFindings !== void 0 ? { chainFindings: opts.chainFindings } : {},
462
- ...opts.costLedger ? { costLedger: opts.costLedger } : {},
463
- ...opts.costPhase ? { costPhase: opts.costPhase } : {},
464
- ...opts.signal ? { signal: opts.signal } : {}
465
- };
466
- if (typeof reg.runStream === "function" && opts.onEvent) {
467
- let final = null;
468
- for await (const ev of reg.runStream(opts.runId, opts.inputs, registryOptions)) {
469
- await emit({
470
- type: "analyst",
471
- runId: opts.runId,
472
- event: ev
473
- });
474
- if (ev.type === "run-completed") final = ev.result;
475
- }
476
- if (!final) throw new Error("runAnalystLoop: registry.runStream ended without run-completed event");
477
- return final;
478
- }
479
- return opts.registry.run(opts.runId, opts.inputs, registryOptions);
480
- }
481
- function resolveBaselineRunId(opts) {
482
- if (opts.baselineRunId === null) return null;
483
- if (typeof opts.baselineRunId === "string") return opts.baselineRunId;
484
- if (!opts.findingsStore) return null;
485
- const all = opts.findingsStore.loadAll();
486
- let last = null;
487
- for (const row of all) {
488
- if (row.run_id === opts.runId) continue;
489
- last = row.run_id;
490
- }
491
- return last;
492
- }
493
- function buildPriorFindingsInput(prior, strategy, registry) {
494
- if (strategy === "none" || prior.length === 0) return void 0;
495
- const stripped = prior.map(({ run_id: _run_id, ...rest }) => rest);
496
- if (strategy === "wildcard") return { "*": stripped };
497
- return stripped;
498
- }
499
- async function runKnowledgeProposalSource(opts, findings, log, emit) {
500
- const batch = await opts.knowledgeProposalSource.proposeFromFindings(findings);
501
- log("knowledge.proposeFromFindings", {
502
- proposals: batch.proposals.length,
503
- skipped: batch.skipped,
504
- errors: batch.errors.length
505
- });
506
- await emit({
507
- type: "knowledge-proposed",
508
- runId: opts.runId,
509
- proposalCount: batch.proposals.length,
510
- skipped: batch.skipped,
511
- errors: batch.errors.length
512
- });
513
- return {
514
- proposals: batch.proposals,
515
- skipped: batch.skipped,
516
- errors: batch.errors
517
- };
518
- }
519
- async function runImprovementProposalSource(opts, findings, log, emit) {
520
- const batch = await opts.improvementProposalSource.proposeFromFindings(findings);
521
- log("improvement.proposeFromFindings", {
522
- edits: batch.edits.length,
523
- skipped: batch.skipped,
524
- errors: batch.errors.length
525
- });
526
- await emit({
527
- type: "improvement-proposed",
528
- runId: opts.runId,
529
- editCount: batch.edits.length,
530
- skipped: batch.skipped,
531
- errors: batch.errors.length
532
- });
533
- return {
534
- edits: batch.edits,
535
- skipped: batch.skipped,
536
- errors: batch.errors
537
- };
538
- }
539
- function defaultLog(msg, fields) {
540
- if (fields) console.log(`[analyst-loop] ${msg}`, fields);
541
- else console.log(`[analyst-loop] ${msg}`);
542
- }
543
- //#endregion
544
- export { iterationsToTraceStore as n, runAnalystLoop as t };
545
-
546
- //# sourceMappingURL=analyst-loop-BknOQUW5.js.map