@tangle-network/agent-runtime 0.8.0 → 0.11.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.
@@ -0,0 +1,262 @@
1
+ import "./chunk-DGUM43GV.js";
2
+
3
+ // src/analyst-loop/run-analyst-loop.ts
4
+ import { diffFindings } from "@tangle-network/agent-eval";
5
+ async function runAnalystLoop(opts) {
6
+ const log = opts.log ?? defaultLog;
7
+ const strategy = opts.priorFindingsStrategy ?? "per-kind";
8
+ const emit = makeEmitter(opts.onEvent);
9
+ const startedAt = Date.now();
10
+ const baselineRunId = resolveBaselineRunId(opts);
11
+ const priorAll = baselineRunId ? opts.findingsStore?.loadRun(baselineRunId) ?? [] : [];
12
+ log("baseline resolved", { baselineRunId, prior_findings: priorAll.length });
13
+ await emit({
14
+ type: "baseline-resolved",
15
+ runId: opts.runId,
16
+ baselineRunId,
17
+ priorFindingCount: priorAll.length
18
+ });
19
+ const priorFindings = buildPriorFindingsInput(priorAll, strategy, opts.registry.list());
20
+ const analystResult = await runRegistry(opts, priorFindings, emit);
21
+ log("analyst run complete", {
22
+ findings: analystResult.findings.length,
23
+ cost_usd: analystResult.total_cost_usd,
24
+ per_analyst: analystResult.per_analyst.map((s) => ({
25
+ id: s.analyst_id,
26
+ status: s.status,
27
+ n: s.findings_count
28
+ }))
29
+ });
30
+ if (opts.findingsStore && analystResult.findings.length > 0) {
31
+ await opts.findingsStore.append(opts.runId, analystResult.findings);
32
+ await emit({
33
+ type: "findings-persisted",
34
+ runId: opts.runId,
35
+ count: analystResult.findings.length
36
+ });
37
+ }
38
+ let diff = null;
39
+ if (baselineRunId && analystResult.findings.length > 0) {
40
+ diff = diffFindings(
41
+ priorAll.map((f) => ({ ...f })),
42
+ analystResult.findings.map((f) => ({ ...f, run_id: opts.runId }))
43
+ );
44
+ log("diff vs baseline", {
45
+ appeared: diff.appeared.length,
46
+ disappeared: diff.disappeared.length,
47
+ persisted: diff.persisted.length,
48
+ changed: diff.changed.length
49
+ });
50
+ await emit({
51
+ type: "diff-computed",
52
+ runId: opts.runId,
53
+ baselineRunId,
54
+ appeared: diff.appeared.length,
55
+ disappeared: diff.disappeared.length,
56
+ persisted: diff.persisted.length,
57
+ changed: diff.changed.length
58
+ });
59
+ }
60
+ let knowledge = null;
61
+ if (opts.knowledgeAdapter) {
62
+ knowledge = await runKnowledgeAdapter(opts, analystResult.findings, log, emit);
63
+ }
64
+ let improvement = null;
65
+ if (opts.improvementAdapter) {
66
+ improvement = await runImprovementAdapter(opts, analystResult.findings, log, emit);
67
+ }
68
+ await emit({
69
+ type: "loop-completed",
70
+ runId: opts.runId,
71
+ durationMs: Date.now() - startedAt
72
+ });
73
+ return {
74
+ runId: opts.runId,
75
+ baselineRunId,
76
+ analystResult,
77
+ diff,
78
+ knowledge,
79
+ improvement
80
+ };
81
+ }
82
+ function makeEmitter(onEvent) {
83
+ if (!onEvent) return async () => {
84
+ };
85
+ return async (event) => {
86
+ await onEvent(event);
87
+ };
88
+ }
89
+ async function runRegistry(opts, priorFindings, emit) {
90
+ const reg = opts.registry;
91
+ if (typeof reg.runStream === "function" && opts.onEvent) {
92
+ let final = null;
93
+ for await (const ev of reg.runStream(opts.runId, opts.inputs, { priorFindings })) {
94
+ await emit({ type: "analyst", runId: opts.runId, event: ev });
95
+ if (ev.type === "run-completed") final = ev.result;
96
+ }
97
+ if (!final) {
98
+ throw new Error("runAnalystLoop: registry.runStream ended without run-completed event");
99
+ }
100
+ return final;
101
+ }
102
+ return opts.registry.run(opts.runId, opts.inputs, { priorFindings });
103
+ }
104
+ function resolveBaselineRunId(opts) {
105
+ if (opts.baselineRunId === null) return null;
106
+ if (typeof opts.baselineRunId === "string") return opts.baselineRunId;
107
+ if (!opts.findingsStore) return null;
108
+ const all = opts.findingsStore.loadAll();
109
+ let last = null;
110
+ for (const row of all) {
111
+ if (row.run_id === opts.runId) continue;
112
+ last = row.run_id;
113
+ }
114
+ return last;
115
+ }
116
+ function buildPriorFindingsInput(prior, strategy, registry) {
117
+ if (strategy === "none" || prior.length === 0) return void 0;
118
+ const stripped = prior.map(({ run_id: _run_id, ...rest }) => rest);
119
+ if (strategy === "wildcard") {
120
+ return { "*": stripped };
121
+ }
122
+ void registry;
123
+ return stripped;
124
+ }
125
+ async function runKnowledgeAdapter(opts, findings, log, emit) {
126
+ const adapter = opts.knowledgeAdapter;
127
+ const batch = await adapter.proposeFromFindings(findings);
128
+ log("knowledge.proposeFromFindings", {
129
+ proposals: batch.proposals.length,
130
+ skipped: batch.skipped,
131
+ errors: batch.errors.length
132
+ });
133
+ await emit({
134
+ type: "knowledge-proposed",
135
+ runId: opts.runId,
136
+ proposalCount: batch.proposals.length,
137
+ skipped: batch.skipped,
138
+ errors: batch.errors.length
139
+ });
140
+ const auto = opts.autoApply?.knowledge ?? false;
141
+ const threshold = opts.autoApply?.knowledgeConfidenceThreshold ?? 0.85;
142
+ if (!auto || !adapter.apply) {
143
+ await emit({
144
+ type: "knowledge-applied",
145
+ runId: opts.runId,
146
+ writtenCount: 0,
147
+ withheldForReview: batch.proposals.length
148
+ });
149
+ return {
150
+ proposals: batch.proposals,
151
+ applied: [],
152
+ skipped: batch.skipped,
153
+ errors: batch.errors,
154
+ withheld_for_review: batch.proposals.length
155
+ };
156
+ }
157
+ const findingsById = new Map(findings.map((f) => [f.finding_id, f]));
158
+ const safe = [];
159
+ let withheld = 0;
160
+ for (const p of batch.proposals) {
161
+ const src = p.sourceFindingId ? findingsById.get(p.sourceFindingId) : void 0;
162
+ if (!src) {
163
+ withheld += 1;
164
+ continue;
165
+ }
166
+ if (src.confidence < threshold) {
167
+ withheld += 1;
168
+ continue;
169
+ }
170
+ safe.push(p);
171
+ }
172
+ const result = await adapter.apply(safe);
173
+ log("knowledge.apply", {
174
+ applied: result.written.length,
175
+ withheld_for_review: withheld,
176
+ warnings: result.warnings.length
177
+ });
178
+ await emit({
179
+ type: "knowledge-applied",
180
+ runId: opts.runId,
181
+ writtenCount: result.written.length,
182
+ withheldForReview: withheld
183
+ });
184
+ return {
185
+ proposals: batch.proposals,
186
+ applied: result.written,
187
+ skipped: batch.skipped,
188
+ errors: batch.errors,
189
+ withheld_for_review: withheld
190
+ };
191
+ }
192
+ async function runImprovementAdapter(opts, findings, log, emit) {
193
+ const adapter = opts.improvementAdapter;
194
+ const batch = await adapter.proposeFromFindings(findings);
195
+ log("improvement.proposeFromFindings", {
196
+ edits: batch.edits.length,
197
+ skipped: batch.skipped,
198
+ errors: batch.errors.length
199
+ });
200
+ await emit({
201
+ type: "improvement-proposed",
202
+ runId: opts.runId,
203
+ editCount: batch.edits.length,
204
+ skipped: batch.skipped,
205
+ errors: batch.errors.length
206
+ });
207
+ const auto = opts.autoApply?.improvement ?? false;
208
+ const threshold = opts.autoApply?.improvementConfidenceThreshold ?? 0.9;
209
+ if (!auto || !adapter.apply) {
210
+ await emit({
211
+ type: "improvement-applied",
212
+ runId: opts.runId,
213
+ appliedCount: 0,
214
+ withheldForReview: batch.edits.length
215
+ });
216
+ return {
217
+ edits: batch.edits,
218
+ applied: [],
219
+ skipped: batch.skipped,
220
+ errors: batch.errors,
221
+ withheld_for_review: batch.edits.length
222
+ };
223
+ }
224
+ const findingsById = new Map(findings.map((f) => [f.finding_id, f]));
225
+ const safe = [];
226
+ let withheld = 0;
227
+ for (const e of batch.edits) {
228
+ const src = e.sourceFindingId ? findingsById.get(e.sourceFindingId) : void 0;
229
+ if (!src || src.confidence < threshold) {
230
+ withheld += 1;
231
+ continue;
232
+ }
233
+ safe.push(e);
234
+ }
235
+ const result = await adapter.apply(safe);
236
+ log("improvement.apply", {
237
+ applied: result.applied.length,
238
+ withheld_for_review: withheld,
239
+ warnings: result.warnings.length
240
+ });
241
+ await emit({
242
+ type: "improvement-applied",
243
+ runId: opts.runId,
244
+ appliedCount: result.applied.length,
245
+ withheldForReview: withheld
246
+ });
247
+ return {
248
+ edits: batch.edits,
249
+ applied: result.applied,
250
+ skipped: batch.skipped,
251
+ errors: batch.errors,
252
+ withheld_for_review: withheld
253
+ };
254
+ }
255
+ function defaultLog(msg, fields) {
256
+ if (fields) console.log(`[analyst-loop] ${msg}`, fields);
257
+ else console.log(`[analyst-loop] ${msg}`);
258
+ }
259
+ export {
260
+ runAnalystLoop
261
+ };
262
+ //# sourceMappingURL=analyst-loop.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/analyst-loop/run-analyst-loop.ts"],"sourcesContent":["/**\n * `runAnalystLoop` — the one call agent apps reach for to close the\n * recursive-self-improvement loop.\n *\n * 1. Load baseline findings (last run, or the slice the caller specifies)\n * 2. Run the analyst registry with priorFindings injected\n * 3. Persist the new run's findings to the ledger\n * 4. Diff the new run against the baseline\n * 5. Hand the findings to the knowledge adapter → proposals (and\n * optionally apply them) → wiki edits\n * 6. Hand the findings to the improvement adapter → prompt / tool /\n * scaffolding edits (review-only by default)\n * 7. Return a single report the consumer renders / persists / acts on.\n *\n * Adapters are optional: the loop works as a \"run + diff + report\"\n * primitive when no adapters are wired; it closes end-to-end when\n * both adapters are wired.\n */\n\nimport type { AnalystFinding, AnalystRunResult, FindingsDiff } from '@tangle-network/agent-eval'\nimport { diffFindings } from '@tangle-network/agent-eval'\n\nimport type {\n AnalystLoopEvent,\n AnalystRegistryStreamingLike,\n ImprovementReport,\n KnowledgeReport,\n RunAnalystLoopOpts,\n RunAnalystLoopResult,\n} from './types'\n\nexport async function runAnalystLoop<TProposal = unknown, TEdit = unknown>(\n opts: RunAnalystLoopOpts,\n): Promise<RunAnalystLoopResult<TProposal, TEdit>> {\n const log = opts.log ?? defaultLog\n const strategy = opts.priorFindingsStrategy ?? 'per-kind'\n const emit = makeEmitter(opts.onEvent)\n const startedAt = Date.now()\n\n // 1. Resolve baseline + load prior findings.\n const baselineRunId = resolveBaselineRunId(opts)\n const priorAll: ReadonlyArray<AnalystFinding & { run_id: string }> = baselineRunId\n ? (opts.findingsStore?.loadRun(baselineRunId) ?? [])\n : []\n log('baseline resolved', { baselineRunId, prior_findings: priorAll.length })\n await emit({\n type: 'baseline-resolved',\n runId: opts.runId,\n baselineRunId,\n priorFindingCount: priorAll.length,\n })\n\n // 2. Run the registry. Strategy controls how analysts see priors.\n // When the registry exposes runStream, forward each event verbatim\n // so subscribers see per-analyst progress in real time.\n const priorFindings = buildPriorFindingsInput(priorAll, strategy, opts.registry.list())\n const analystResult = await runRegistry(opts, priorFindings, emit)\n log('analyst run complete', {\n findings: analystResult.findings.length,\n cost_usd: analystResult.total_cost_usd,\n per_analyst: analystResult.per_analyst.map((s) => ({\n id: s.analyst_id,\n status: s.status,\n n: s.findings_count,\n })),\n })\n\n // 3. Persist the new run before any side-effecting adapter runs so\n // the ledger is the source of truth even if an adapter throws.\n if (opts.findingsStore && analystResult.findings.length > 0) {\n await opts.findingsStore.append(opts.runId, analystResult.findings)\n await emit({\n type: 'findings-persisted',\n runId: opts.runId,\n count: analystResult.findings.length,\n })\n }\n\n // 4. Diff vs baseline.\n let diff: FindingsDiff | null = null\n if (baselineRunId && analystResult.findings.length > 0) {\n diff = diffFindings(\n priorAll.map((f) => ({ ...f })),\n analystResult.findings.map((f) => ({ ...f, run_id: opts.runId })),\n )\n log('diff vs baseline', {\n appeared: diff.appeared.length,\n disappeared: diff.disappeared.length,\n persisted: diff.persisted.length,\n changed: diff.changed.length,\n })\n await emit({\n type: 'diff-computed',\n runId: opts.runId,\n baselineRunId,\n appeared: diff.appeared.length,\n disappeared: diff.disappeared.length,\n persisted: diff.persisted.length,\n changed: diff.changed.length,\n })\n }\n\n // 5. Knowledge adapter — proposals + optional auto-apply.\n let knowledge: KnowledgeReport<TProposal> | null = null\n if (opts.knowledgeAdapter) {\n knowledge = await runKnowledgeAdapter(opts, analystResult.findings, log, emit)\n }\n\n // 6. Improvement adapter — prompt / tool / scaffolding edits.\n let improvement: ImprovementReport<TEdit> | null = null\n if (opts.improvementAdapter) {\n improvement = await runImprovementAdapter(opts, analystResult.findings, log, emit)\n }\n\n await emit({\n type: 'loop-completed',\n runId: opts.runId,\n durationMs: Date.now() - startedAt,\n })\n\n return {\n runId: opts.runId,\n baselineRunId,\n analystResult,\n diff,\n knowledge,\n improvement,\n }\n}\n\ntype Emitter = (event: AnalystLoopEvent) => Promise<void>\n\nfunction makeEmitter(onEvent: RunAnalystLoopOpts['onEvent']): Emitter {\n if (!onEvent) return async () => {}\n return async (event) => {\n await onEvent(event)\n }\n}\n\nasync function runRegistry(\n opts: RunAnalystLoopOpts,\n priorFindings: ReturnType<typeof buildPriorFindingsInput>,\n emit: Emitter,\n): Promise<AnalystRunResult> {\n const reg = opts.registry as AnalystRegistryStreamingLike\n if (typeof reg.runStream === 'function' && opts.onEvent) {\n let final: AnalystRunResult | null = null\n for await (const ev of reg.runStream(opts.runId, opts.inputs, { priorFindings })) {\n await emit({ type: 'analyst', runId: opts.runId, event: ev })\n if (ev.type === 'run-completed') final = ev.result\n }\n if (!final) {\n throw new Error('runAnalystLoop: registry.runStream ended without run-completed event')\n }\n return final\n }\n return opts.registry.run(opts.runId, opts.inputs, { priorFindings })\n}\n\nfunction resolveBaselineRunId(opts: RunAnalystLoopOpts): string | null {\n if (opts.baselineRunId === null) return null\n if (typeof opts.baselineRunId === 'string') return opts.baselineRunId\n if (!opts.findingsStore) return null\n const all = opts.findingsStore.loadAll()\n let last: string | null = null\n for (const row of all) {\n if (row.run_id === opts.runId) continue\n last = row.run_id\n }\n return last\n}\n\nfunction buildPriorFindingsInput(\n prior: ReadonlyArray<AnalystFinding & { run_id: string }>,\n strategy: 'per-kind' | 'wildcard' | 'none',\n registry: ReadonlyArray<{ id: string }>,\n): ReadonlyArray<AnalystFinding> | Record<string, ReadonlyArray<AnalystFinding>> | undefined {\n if (strategy === 'none' || prior.length === 0) return undefined\n const stripped = prior.map(({ run_id: _run_id, ...rest }) => rest as AnalystFinding)\n if (strategy === 'wildcard') {\n return { '*': stripped }\n }\n void registry\n return stripped\n}\n\nasync function runKnowledgeAdapter<TProposal>(\n opts: RunAnalystLoopOpts,\n findings: ReadonlyArray<AnalystFinding>,\n log: NonNullable<RunAnalystLoopOpts['log']>,\n emit: Emitter,\n): Promise<KnowledgeReport<TProposal>> {\n const adapter = opts.knowledgeAdapter!\n const batch = await adapter.proposeFromFindings(findings)\n log('knowledge.proposeFromFindings', {\n proposals: batch.proposals.length,\n skipped: batch.skipped,\n errors: batch.errors.length,\n })\n await emit({\n type: 'knowledge-proposed',\n runId: opts.runId,\n proposalCount: batch.proposals.length,\n skipped: batch.skipped,\n errors: batch.errors.length,\n })\n\n const auto = opts.autoApply?.knowledge ?? false\n const threshold = opts.autoApply?.knowledgeConfidenceThreshold ?? 0.85\n\n if (!auto || !adapter.apply) {\n await emit({\n type: 'knowledge-applied',\n runId: opts.runId,\n writtenCount: 0,\n withheldForReview: batch.proposals.length,\n })\n return {\n proposals: batch.proposals as TProposal[],\n applied: [],\n skipped: batch.skipped,\n errors: batch.errors,\n withheld_for_review: batch.proposals.length,\n }\n }\n\n const findingsById = new Map(findings.map((f) => [f.finding_id, f]))\n const safe: TProposal[] = []\n let withheld = 0\n for (const p of batch.proposals as Array<TProposal & { sourceFindingId?: string }>) {\n const src = p.sourceFindingId ? findingsById.get(p.sourceFindingId) : undefined\n if (!src) {\n withheld += 1\n continue\n }\n if (src.confidence < threshold) {\n withheld += 1\n continue\n }\n safe.push(p)\n }\n const result = await adapter.apply(safe)\n log('knowledge.apply', {\n applied: result.written.length,\n withheld_for_review: withheld,\n warnings: result.warnings.length,\n })\n await emit({\n type: 'knowledge-applied',\n runId: opts.runId,\n writtenCount: result.written.length,\n withheldForReview: withheld,\n })\n return {\n proposals: batch.proposals as TProposal[],\n applied: result.written,\n skipped: batch.skipped,\n errors: batch.errors,\n withheld_for_review: withheld,\n }\n}\n\nasync function runImprovementAdapter<TEdit>(\n opts: RunAnalystLoopOpts,\n findings: ReadonlyArray<AnalystFinding>,\n log: NonNullable<RunAnalystLoopOpts['log']>,\n emit: Emitter,\n): Promise<ImprovementReport<TEdit>> {\n const adapter = opts.improvementAdapter!\n const batch = await adapter.proposeFromFindings(findings)\n log('improvement.proposeFromFindings', {\n edits: batch.edits.length,\n skipped: batch.skipped,\n errors: batch.errors.length,\n })\n await emit({\n type: 'improvement-proposed',\n runId: opts.runId,\n editCount: batch.edits.length,\n skipped: batch.skipped,\n errors: batch.errors.length,\n })\n\n const auto = opts.autoApply?.improvement ?? false\n const threshold = opts.autoApply?.improvementConfidenceThreshold ?? 0.9\n\n if (!auto || !adapter.apply) {\n await emit({\n type: 'improvement-applied',\n runId: opts.runId,\n appliedCount: 0,\n withheldForReview: batch.edits.length,\n })\n return {\n edits: batch.edits as TEdit[],\n applied: [],\n skipped: batch.skipped,\n errors: batch.errors,\n withheld_for_review: batch.edits.length,\n }\n }\n\n const findingsById = new Map(findings.map((f) => [f.finding_id, f]))\n const safe: TEdit[] = []\n let withheld = 0\n for (const e of batch.edits as Array<TEdit & { sourceFindingId?: string }>) {\n const src = e.sourceFindingId ? findingsById.get(e.sourceFindingId) : undefined\n if (!src || src.confidence < threshold) {\n withheld += 1\n continue\n }\n safe.push(e)\n }\n const result = await adapter.apply(safe)\n log('improvement.apply', {\n applied: result.applied.length,\n withheld_for_review: withheld,\n warnings: result.warnings.length,\n })\n await emit({\n type: 'improvement-applied',\n runId: opts.runId,\n appliedCount: result.applied.length,\n withheldForReview: withheld,\n })\n return {\n edits: batch.edits as TEdit[],\n applied: result.applied,\n skipped: batch.skipped,\n errors: batch.errors,\n withheld_for_review: withheld,\n }\n}\n\nfunction defaultLog(msg: string, fields?: Record<string, unknown>): void {\n if (fields) console.log(`[analyst-loop] ${msg}`, fields)\n else console.log(`[analyst-loop] ${msg}`)\n}\n"],"mappings":";;;AAoBA,SAAS,oBAAoB;AAW7B,eAAsB,eACpB,MACiD;AACjD,QAAM,MAAM,KAAK,OAAO;AACxB,QAAM,WAAW,KAAK,yBAAyB;AAC/C,QAAM,OAAO,YAAY,KAAK,OAAO;AACrC,QAAM,YAAY,KAAK,IAAI;AAG3B,QAAM,gBAAgB,qBAAqB,IAAI;AAC/C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AAAA,IACb,SAAS,OAAO;AAAA,IAChB,SAAS,MAAM;AAAA,IACf,QAAQ,MAAM;AAAA,IACd,qBAAqB;AAAA,EACvB;AACF;AAEA,SAAS,WAAW,KAAa,QAAwC;AACvE,MAAI,OAAQ,SAAQ,IAAI,kBAAkB,GAAG,IAAI,MAAM;AAAA,MAClD,SAAQ,IAAI,kBAAkB,GAAG,EAAE;AAC1C;","names":[]}
@@ -0,0 +1,11 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
8
+ export {
9
+ __require
10
+ };
11
+ //# sourceMappingURL=chunk-DGUM43GV.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { ControlEvalResult, KnowledgeRequirement, ControlBudget, KnowledgeReadinessReport, ControlStep, ControlDecision, UserQuestion, DataAcquisitionPlan, ControlRunResult, RunRecord, TraceStore, AgentEvalError, TraceEvent } from '@tangle-network/agent-eval';
2
2
  export { AgentEvalError, AgentEvalErrorCode, CaptureIntegrityError, ConfigError, ControlBudget, ControlDecision, ControlEvalResult, ControlRunResult, ControlStep, DataAcquisitionPlan, JudgeError, KnowledgeReadinessReport, KnowledgeRequirement, NotFoundError, ReplayError, RunRecord, UserQuestion, ValidationError, VerificationError } from '@tangle-network/agent-eval';
3
+ import { AgentProfilePrompt, AgentProfileResources, AgentSubagentProfile, AgentProfile, SandboxInstance } from '@tangle-network/sandbox';
3
4
 
4
5
  /**
5
6
  * @stable
@@ -427,6 +428,105 @@ declare function createOpenAICompatibleBackend<TInput extends AgentBackendInput
427
428
  fetchImpl?: typeof fetch;
428
429
  }): AgentExecutionBackend<TInput>;
429
430
 
431
+ /**
432
+ * Send an AgentProfile to a sandbox runtime for one chat turn. Composes
433
+ * the per-turn overlay (user message, prior history, knowledge flags)
434
+ * via `mergeAgentProfiles`, POSTs the full profile to the runtime's
435
+ * `/runtime/agents/run/stream` endpoint, and yields `RuntimeStreamEvent`s
436
+ * from the SSE response. The full profile — subagents, MCP servers,
437
+ * permissions, file mounts — reaches the sandbox by construction.
438
+ */
439
+
440
+ /** A message in the chat turn's prior context. Provider-neutral. */
441
+ interface ChatTurnMessage {
442
+ role: 'user' | 'assistant' | 'system';
443
+ content: string;
444
+ /** Optional metadata the consumer wants threaded through (timestamps, msg id, etc). */
445
+ metadata?: Record<string, unknown>;
446
+ }
447
+ /**
448
+ * Per-turn profile overlay. The caller's profile is the durable contract;
449
+ * the overlay carries volatile per-turn context (workspace facts, dynamic-
450
+ * advisor instructions, RAG hits) that the runtime should see but that
451
+ * doesn't belong in the canonical profile. Composed via `mergeAgentProfiles`.
452
+ */
453
+ interface ChatTurnOverlay {
454
+ /** Volatile prompt additions: dynamic-advisor context, intake gate output, RAG citations. */
455
+ promptOverlay?: AgentProfilePrompt;
456
+ /** Per-turn additional file mounts (vault docs, recent uploads). */
457
+ resourcesOverlay?: AgentProfileResources;
458
+ /** Override or augment the subagent map for this turn (rare). */
459
+ subagentsOverlay?: Record<string, AgentSubagentProfile>;
460
+ /** Free-form metadata threaded into the runtime call. */
461
+ metadata?: Record<string, unknown>;
462
+ }
463
+ /**
464
+ * Sandbox runtime contract — the subset of `SandboxInstance` `runChatTurn`
465
+ * actually invokes. Defined as an interface so consumers can inject a
466
+ * stub in tests without standing up a real sandbox.
467
+ */
468
+ interface ChatTurnSandbox {
469
+ /** Sandbox identifier used in the runtime URL. */
470
+ readonly id: string;
471
+ /** Base URL of the sandbox runtime, e.g. `https://sandbox.tangle.tools/v1/sandboxes/&lt;id&gt;`. */
472
+ readonly runtimeUrl: string;
473
+ /** Optional bearer for the runtime call (sidecar auth). */
474
+ readonly authHeader?: {
475
+ name: string;
476
+ value: string;
477
+ };
478
+ }
479
+ interface RunChatTurnOptions {
480
+ /** Canonical agent profile — the durable contract. */
481
+ profile: AgentProfile;
482
+ /** Volatile per-turn additions. */
483
+ overlay?: ChatTurnOverlay;
484
+ /** Current user message. */
485
+ message: string;
486
+ /** Prior conversation. */
487
+ priorMessages: readonly ChatTurnMessage[];
488
+ /** Sandbox the turn should run inside. */
489
+ sandbox: ChatTurnSandbox;
490
+ /** Override model for this turn (otherwise profile.model.default is used). */
491
+ modelOverride?: string;
492
+ /** AbortSignal for cancellation (timeouts, client disconnect, etc). */
493
+ signal?: AbortSignal;
494
+ /** Override fetch (for tests). */
495
+ fetch?: typeof fetch;
496
+ }
497
+ /**
498
+ * Compose the per-turn profile via `mergeAgentProfiles(profile, overlay)`,
499
+ * POST the FULL composed profile to the sandbox's runtime streaming endpoint,
500
+ * and yield parsed `RuntimeStreamEvent`s. The runtime resolves prompt,
501
+ * subagents, MCP servers, and permissions from the profile — callers do not
502
+ * hand-craft a `backend.profile` shape.
503
+ *
504
+ * for await (const event of runChatTurn({ profile, overlay, message, priorMessages, sandbox })) {
505
+ * // event.type === 'message' / 'tool_call' / 'task_complete' / etc.
506
+ * }
507
+ */
508
+ declare function runChatTurn(options: RunChatTurnOptions): AsyncIterable<RuntimeStreamEvent>;
509
+ /**
510
+ * Compose the per-turn AgentProfile. Public-and-pure so consumers can
511
+ * preview/log/persist the composed shape without firing a runtime call.
512
+ */
513
+ declare function composeTurnProfile(base: AgentProfile, overlay: ChatTurnOverlay): AgentProfile;
514
+ declare class ChatTurnError extends Error {
515
+ readonly status?: number | undefined;
516
+ constructor(message: string, status?: number | undefined);
517
+ }
518
+ /**
519
+ * Convenience: build a `ChatTurnSandbox` from a full `SandboxInstance`. Most
520
+ * callers already have one of these from `client.create()`. The runtime URL
521
+ * is derived from the instance's transport configuration.
522
+ */
523
+ declare function sandboxAsChatTurnTarget(instance: SandboxInstance, opts?: {
524
+ authHeader?: {
525
+ name: string;
526
+ value: string;
527
+ };
528
+ }): ChatTurnSandbox;
529
+
430
530
  /**
431
531
  * @stable
432
532
  *
@@ -492,6 +592,124 @@ declare class RuntimeRunStateError extends AgentEvalError {
492
592
  });
493
593
  }
494
594
 
595
+ /**
596
+ * Classify a user message against a profile's subagent map. Routes are
597
+ * declared on each `AgentSubagentProfile.metadata.matchers` (keywords +
598
+ * regex patterns); `classifyIntent` scores every subagent against the
599
+ * message and returns a typed result. Pure scoring — no LLM call. Caller
600
+ * decides whether the score is high enough to dispatch the subagent.
601
+ */
602
+
603
+ /** Matcher shape attached to `subagent.metadata.matchers`. */
604
+ interface SubagentMatcher {
605
+ /** Literal keywords (case-insensitive substring match). +1 per match unless `weight` overrides. */
606
+ keywords?: readonly string[];
607
+ /** Regex patterns (case-insensitive). +`weight` per match (default 1.5). */
608
+ patterns?: readonly (string | RegExp)[];
609
+ /** Per-rule weight override. */
610
+ weight?: number;
611
+ /** Minimum score for this subagent to be considered "matched". Default 1. */
612
+ minScore?: number;
613
+ }
614
+ interface ClassifyIntentResult {
615
+ /** Best-scoring subagent id; null if no subagent crossed `minScore`. */
616
+ id: string | null;
617
+ /** Best subagent's `AgentSubagentProfile`; null if no match. */
618
+ subagent: AgentSubagentProfile | null;
619
+ /** Score of the chosen subagent. */
620
+ score: number;
621
+ /** All subagent scores keyed by id — useful for telemetry / debugging. */
622
+ scores: Record<string, number>;
623
+ /** The matchers checked, for debugging "why didn't X route?" */
624
+ evaluated: Record<string, {
625
+ keywordHits: number;
626
+ patternHits: number;
627
+ minScore: number;
628
+ }>;
629
+ }
630
+ interface ClassifyIntentOptions {
631
+ /** Override the global default `minScore` (used when a subagent omits its own). */
632
+ defaultMinScore?: number;
633
+ /** Subagent ids to skip (e.g. scaffold-only subagents with `metadata.status === 'scaffold'`). */
634
+ skip?: readonly string[];
635
+ /** Only consider subagents whose metadata predicate returns true (e.g. `m =&gt; m.status === 'full'`). */
636
+ filter?: (subagent: AgentSubagentProfile, id: string) => boolean;
637
+ }
638
+ /**
639
+ * Pure intent classifier.
640
+ *
641
+ * For each subagent in `profile.subagents`: read `metadata.matchers`,
642
+ * score keyword + pattern hits against the user message, return the
643
+ * highest-scoring subagent (or null if none crossed `minScore`). No LLM
644
+ * call, no side effects. The runtime decides what to do with the result.
645
+ */
646
+ declare function classifyIntent(profile: AgentProfile, message: string, opts?: ClassifyIntentOptions): ClassifyIntentResult;
647
+
648
+ /**
649
+ * Validate an AgentProfile against canonical conformance rules: tool keys
650
+ * must map to an MCP server entry (not be shell capabilities masquerading
651
+ * as tools), subagents marked `metadata.status: 'scaffold'` must not be
652
+ * dispatchable, system prompts must be substantive. Pure — no I/O — so
653
+ * callers run it in a unit test or at module load to fail-fast on
654
+ * misconfiguration.
655
+ */
656
+
657
+ interface ConformanceIssue {
658
+ severity: 'error' | 'warn';
659
+ /** Stable kebab-case code, useful for CI matching. */
660
+ code: string;
661
+ /** Profile path the issue is anchored to, e.g. `tools.bash` or `subagents.foo`. */
662
+ path: string;
663
+ /** Human-readable failure reason. */
664
+ message: string;
665
+ }
666
+ interface ConformanceResult {
667
+ valid: boolean;
668
+ errors: ConformanceIssue[];
669
+ warnings: ConformanceIssue[];
670
+ }
671
+ interface ConformanceOptions {
672
+ /**
673
+ * Shell-style capabilities that may appear in `permissions` but NEVER in
674
+ * `tools`. Declaring these as tools is decorative — the runtime doesn't
675
+ * dispatch them as MCP servers. Default covers the observed offenders.
676
+ */
677
+ knownShellCapabilities?: readonly string[];
678
+ /**
679
+ * If true, treat `subagents[id].metadata.status === 'scaffold'` as an
680
+ * error rather than a warning. Default false — scaffolds are honest
681
+ * stubs as long as the router skips them.
682
+ */
683
+ strictNoScaffolds?: boolean;
684
+ /**
685
+ * Names that may appear in `tools` without a corresponding MCP server or
686
+ * resource mount. Default empty — kills decorative-tool anti-pattern.
687
+ */
688
+ toolsAllowedWithoutMcp?: readonly string[];
689
+ /**
690
+ * Minimum length of `prompt.systemPrompt`. Profiles below this are
691
+ * likely incomplete. Default 800 chars (rough lower bound for the
692
+ * partner-tier prompts the product agents target).
693
+ */
694
+ minSystemPromptChars?: number;
695
+ }
696
+ /**
697
+ * Run all conformance checks against a profile. Designed to be called from
698
+ * a unit test:
699
+ *
700
+ * it('legalAgentProfile passes conformance', () => {
701
+ * const result = assertProfileConformance(legalAgentProfile, {
702
+ * toolsAllowedWithoutMcp: ['agent-knowledge:query'], // in-process function
703
+ * })
704
+ * expect(result.valid).toBe(true)
705
+ * expect(result.errors).toEqual([])
706
+ * })
707
+ *
708
+ * Issues carry `path` + `code` so tests can pinpoint failures rather than
709
+ * collapse to a generic "validation failed."
710
+ */
711
+ declare function assertProfileConformance(profile: AgentProfile, opts?: ConformanceOptions): ConformanceResult;
712
+
495
713
  /**
496
714
  * @stable
497
715
  *
@@ -534,34 +752,27 @@ declare function runAgentTaskStream<TInput extends AgentBackendInput = AgentBack
534
752
  /**
535
753
  * @stable
536
754
  *
537
- * Canonical production-run lifecycle. ONE abstraction for "the agent did a
538
- * thing on behalf of a customer; record what it did, what it cost, and how it
539
- * ended." Consumer agents (legal, tax, gtm, creative, agent-builder) reach for
540
- * `startRuntimeRun` instead of inventing their own `agentRuns`-row helpers.
755
+ * Production-run lifecycle: record what the agent did on behalf of a customer,
756
+ * what it cost, and how it ended.
541
757
  *
542
758
  * Three concerns live in this module:
543
759
  *
544
760
  * 1. **Lifecycle state machine** — `running` -> `completed | failed | cancelled`,
545
- * enforced by `RuntimeRunStateError`. Completion is idempotent (a second
546
- * `complete()` call with the same status is a no-op so retries / cleanup
547
- * paths don't double-fire side effects). A different terminal status is a
548
- * state error.
761
+ * enforced by `RuntimeRunStateError`. Completion is idempotent for the same
762
+ * status (a second `complete()` call is a no-op so retries / cleanup paths
763
+ * don't double-fire side effects). A different terminal status is a state
764
+ * error.
549
765
  *
550
766
  * 2. **Cost ledger** — every `llm_call` event the handle observes contributes
551
767
  * `tokensIn`, `tokensOut`, `costUsd`, and bumps `llmCalls`. Wall time is
552
768
  * measured from `startRuntimeRun()` to `complete()`. Surface via
553
- * `handle.cost()` for "cost per customer task" dashboards.
769
+ * `handle.cost()` for cost-per-task dashboards.
554
770
  *
555
771
  * 3. **Persistence adapter** — `RuntimeRunPersistenceAdapter` is the seam
556
772
  * consumers plug in to write a `RuntimeRunRow` to their D1 / postgres /
557
773
  * KV store. The adapter receives a sanitized row shape; no telemetry
558
774
  * payload bytes flow through it unless the consumer opts in via
559
775
  * `RuntimeRunOptions.telemetryEvents`.
560
- *
561
- * The pattern replaces legal-agent's bespoke `completeProductionAgentRun` /
562
- * `persistRuntimeRun` pair from `eval-evidence.ts` + `api.chat.ts`. Both are
563
- * marked `@deprecated` in this release; consumers ditch them on their own
564
- * version bumps.
565
776
  */
566
777
 
567
778
  /** @stable */
@@ -770,16 +981,12 @@ declare function createRuntimeEventCollector<TState = unknown, TAction = unknown
770
981
  /**
771
982
  * @stable
772
983
  *
773
- * Streaming-event counterpart of `createRuntimeEventCollector`. Use this with
774
- * `runAgentTaskStream` — pass each yielded event through `onEvent` and read
775
- * the sanitized copies off `events`. The same `RuntimeTelemetryOptions`
776
- * redaction flags apply.
777
- *
778
- * Stream and non-stream events have different field shapes (timestamps,
779
- * sessions, text/tool deltas) so this is a sibling factory rather than an
780
- * overload of `createRuntimeEventCollector`; the unified-union alternative
781
- * was rejected because dispatching on `type` alone would silently misroute
782
- * events whose `type` literals overlap (`task_start`, `readiness_end`, etc.).
984
+ * Streaming-event counterpart of `createRuntimeEventCollector`. Pass each
985
+ * event yielded by `runAgentTaskStream` through `onEvent` and read the
986
+ * sanitized copies off `events`; the same `RuntimeTelemetryOptions` redaction
987
+ * flags apply. Kept distinct from `createRuntimeEventCollector` because the
988
+ * stream and non-stream event shapes overlap on `type` literals — dispatching
989
+ * on `type` alone would misroute events.
783
990
  */
784
991
  declare function createRuntimeStreamEventCollector(options?: RuntimeTelemetryOptions): RuntimeStreamEventCollector;
785
992
 
@@ -829,17 +1036,10 @@ declare function runtimeStreamServerSentEvent(event: RuntimeStreamEvent, options
829
1036
  /**
830
1037
  * @stable
831
1038
  *
832
- * Bridge from runtime stream events to the agent-eval trace schema.
833
- *
834
- * Before this module, consumers (legal-agent's chat.ts, gtm-agent's runtime
835
- * route) hand-rolled an adapter from `RuntimeStreamEvent` -> `TraceEvent` per
836
- * repo. The mapping is mechanical and the destination schema is owned by
837
- * agent-eval, so the adapter belongs in runtime, not in N consumer repos.
1039
+ * One-way bridge from `RuntimeStreamEvent` to agent-eval's `TraceEvent`.
838
1040
  *
839
- * The bridge is intentionally one-way (runtime -> agent-eval). The reverse
840
- * mapping is degenerate (agent-eval events have no session / task affinity)
841
- * and would invite consumers to round-trip through agent-eval, defeating the
842
- * point of the runtime-specific shape.
1041
+ * The reverse direction is intentionally unsupported agent-eval events
1042
+ * have no session / task affinity, so round-tripping would erase information.
843
1043
  */
844
1044
 
845
1045
  /** @stable */
@@ -890,4 +1090,4 @@ declare function createTraceBridge(options: TraceBridgeOptions): TraceBridge;
890
1090
  */
891
1091
  declare function toAgentEvalTrace(event: RuntimeStreamEvent, options: TraceBridgeOptions): TraceEvent | undefined;
892
1092
 
893
- export { type AgentAdapter, type AgentBackendContext, type AgentBackendInput, type AgentExecutionBackend, type AgentKnowledgeProvider, type AgentRuntimeEvent, type AgentRuntimeEventSink, type AgentTaskContext, type AgentTaskRunResult, type AgentTaskRunSummary, type AgentTaskSpec, type AgentTaskStatus, BackendTransportError, InMemoryRuntimeSessionStore, type KnowledgeReadinessDecision, type RunAgentTaskOptions, type RunAgentTaskStreamOptions, type RuntimeEventCollector, type RuntimeRunCompleteInput, type RuntimeRunCost, type RuntimeRunHandle, type RuntimeRunOptions, type RuntimeRunPersistenceAdapter, type RuntimeRunRow, RuntimeRunStateError, type RuntimeRunStatus, type RuntimeSession, type RuntimeSessionStore, type RuntimeStreamEvent, type RuntimeStreamEventCollector, type RuntimeStreamEventSink, type RuntimeStreamEventSummary, type RuntimeTelemetryOptions, type SanitizedKnowledgeReadinessReport, type SanitizedKnowledgeRequirement, type ServerSentEventOptions, SessionMismatchError, type TraceBridge, type TraceBridgeOptions, createIterableBackend, createOpenAICompatibleBackend, createRuntimeEventCollector, createRuntimeStreamEventCollector, createSandboxPromptBackend, createTraceBridge, decideKnowledgeReadiness, encodeServerSentEvent, readinessServerSentEvent, runAgentTask, runAgentTaskStream, runtimeStreamServerSentEvent, sanitizeAgentRuntimeEvent, sanitizeKnowledgeReadinessReport, sanitizeRuntimeStreamEvent, startRuntimeRun, summarizeAgentTaskRun, toAgentEvalTrace };
1093
+ export { type AgentAdapter, type AgentBackendContext, type AgentBackendInput, type AgentExecutionBackend, type AgentKnowledgeProvider, type AgentRuntimeEvent, type AgentRuntimeEventSink, type AgentTaskContext, type AgentTaskRunResult, type AgentTaskRunSummary, type AgentTaskSpec, type AgentTaskStatus, BackendTransportError, ChatTurnError, type ChatTurnMessage, type ChatTurnOverlay, type ChatTurnSandbox, type ClassifyIntentOptions, type ClassifyIntentResult, type ConformanceIssue, type ConformanceOptions, type ConformanceResult, InMemoryRuntimeSessionStore, type KnowledgeReadinessDecision, type RunAgentTaskOptions, type RunAgentTaskStreamOptions, type RunChatTurnOptions, type RuntimeEventCollector, type RuntimeRunCompleteInput, type RuntimeRunCost, type RuntimeRunHandle, type RuntimeRunOptions, type RuntimeRunPersistenceAdapter, type RuntimeRunRow, RuntimeRunStateError, type RuntimeRunStatus, type RuntimeSession, type RuntimeSessionStore, type RuntimeStreamEvent, type RuntimeStreamEventCollector, type RuntimeStreamEventSink, type RuntimeStreamEventSummary, type RuntimeTelemetryOptions, type SanitizedKnowledgeReadinessReport, type SanitizedKnowledgeRequirement, type ServerSentEventOptions, SessionMismatchError, type SubagentMatcher, type TraceBridge, type TraceBridgeOptions, assertProfileConformance, classifyIntent, composeTurnProfile, createIterableBackend, createOpenAICompatibleBackend, createRuntimeEventCollector, createRuntimeStreamEventCollector, createSandboxPromptBackend, createTraceBridge, decideKnowledgeReadiness, encodeServerSentEvent, readinessServerSentEvent, runAgentTask, runAgentTaskStream, runChatTurn, runtimeStreamServerSentEvent, sandboxAsChatTurnTarget, sanitizeAgentRuntimeEvent, sanitizeKnowledgeReadinessReport, sanitizeRuntimeStreamEvent, startRuntimeRun, summarizeAgentTaskRun, toAgentEvalTrace };