@tangle-network/agent-runtime 0.38.0 → 0.40.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +265 -69
- package/dist/chunk-3WQJRSUJ.js +201 -0
- package/dist/chunk-3WQJRSUJ.js.map +1 -0
- package/dist/{chunk-M65QJD35.js → chunk-6HI3QUJD.js} +5 -3
- package/dist/{chunk-M65QJD35.js.map → chunk-6HI3QUJD.js.map} +1 -1
- package/dist/{chunk-Z523NPJK.js → chunk-7ZECSZ3C.js} +2 -59
- package/dist/chunk-7ZECSZ3C.js.map +1 -0
- package/dist/chunk-FNMGYYSS.js +60 -0
- package/dist/chunk-FNMGYYSS.js.map +1 -0
- package/dist/{chunk-V6GURW4W.js → chunk-HSX6PFZR.js} +1 -209
- package/dist/chunk-HSX6PFZR.js.map +1 -0
- package/dist/{chunk-7JBDJQLO.js → chunk-OISRXLWI.js} +8 -3
- package/dist/chunk-OISRXLWI.js.map +1 -0
- package/dist/chunk-VFKBIZTY.js +212 -0
- package/dist/chunk-VFKBIZTY.js.map +1 -0
- package/dist/{dynamic-DeOPeeAw.d.ts → dynamic-BT9Ji3jE.d.ts} +3 -1
- package/dist/improvement.d.ts +1 -1
- package/dist/index.d.ts +10 -147
- package/dist/index.js +23 -99
- package/dist/index.js.map +1 -1
- package/dist/{otel-export-CNmeg_7B.d.ts → kb-gate-C4tho31v.d.ts} +2 -191
- package/dist/loop-runner-bin-C1MuoT8c.d.ts +192 -0
- package/dist/loop-runner-bin.d.ts +12 -0
- package/dist/loop-runner-bin.js +19 -0
- package/dist/loop-runner-bin.js.map +1 -0
- package/dist/loops.d.ts +4 -4
- package/dist/loops.js +1 -1
- package/dist/mcp/bin.js +4 -3
- package/dist/mcp/bin.js.map +1 -1
- package/dist/mcp/index.d.ts +5 -3
- package/dist/mcp/index.js +12 -8
- package/dist/mcp/index.js.map +1 -1
- package/dist/{optimize-prompt-cmH9wZdH.d.ts → optimize-prompt-D-urF2wW.d.ts} +1 -1
- package/dist/otel-export-xgf4J6bo.d.ts +191 -0
- package/dist/profiles.d.ts +1 -1
- package/dist/{types-CmkQl8qE.d.ts → types-CNs7_1R3.d.ts} +8 -1
- package/package.json +3 -2
- package/dist/chunk-7JBDJQLO.js.map +0 -1
- package/dist/chunk-V6GURW4W.js.map +0 -1
- package/dist/chunk-Z523NPJK.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,14 +1,38 @@
|
|
|
1
1
|
# @tangle-network/agent-runtime
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The task-lifecycle substrate for domain agents. It owns the **chat-turn engine**, the **driven-loop kernel** (refine / fanout-vote / agent-authored *dynamic* topologies), **delegated loops** (build-in-a-loop, valid-only research, review, audit, self-improve), **identity-gated prompt optimization**, **OpenTelemetry GenAI tracing**, knowledge readiness, sanitized telemetry, and the declarative `defineAgent` manifest — and delegates domain behavior (models, tools, KB) to adapters. Long-running execution durability lives in [`@tangle-network/sandbox`](https://www.npmjs.com/package/@tangle-network/sandbox); evals + gates in [`@tangle-network/agent-eval`](https://www.npmjs.com/package/@tangle-network/agent-eval).
|
|
4
4
|
|
|
5
5
|
```bash
|
|
6
6
|
pnpm add @tangle-network/agent-runtime @tangle-network/agent-eval @tangle-network/sandbox
|
|
7
7
|
```
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
---
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
## Contents
|
|
12
|
+
|
|
13
|
+
- [Getting started](#getting-started) — the 20-line production chat turn
|
|
14
|
+
- [Which entry point do I reach for?](#which-entry-point-do-i-reach-for)
|
|
15
|
+
- [Capabilities](#capabilities)
|
|
16
|
+
- [1. Chat turns — `handleChatTurn`](#1-chat-turns--handlechatturn)
|
|
17
|
+
- [2. Driven loops + topology drivers](#2-driven-loops--topology-drivers)
|
|
18
|
+
- [3. Agent-authored topology — `createDynamicDriver`](#3-agent-authored-topology--createdynamicdriver)
|
|
19
|
+
- [4. Delegated loop-runner — `runDelegatedLoop`](#4-delegated-loop-runner--rundelegatedloop)
|
|
20
|
+
- [5. Reliable build-in-a-loop — the coder delegate](#5-reliable-build-in-a-loop--the-coder-delegate)
|
|
21
|
+
- [6. Valid-only research — `createKbGate`](#6-valid-only-research--createkbgate)
|
|
22
|
+
- [7. Identity-gated prompt optimization — `optimizePrompt`](#7-identity-gated-prompt-optimization--optimizeprompt)
|
|
23
|
+
- [8. OpenTelemetry GenAI topology tracing](#8-opentelemetry-genai-topology-tracing)
|
|
24
|
+
- [9. MCP delegation server — `agent-runtime-mcp`](#9-mcp-delegation-server--agent-runtime-mcp)
|
|
25
|
+
- [Defaults](#defaults)
|
|
26
|
+
- [Composition with the stack](#composition-with-the-stack)
|
|
27
|
+
- [Subpath exports](#subpath-exports)
|
|
28
|
+
- [Adoption skill](#adoption-skill)
|
|
29
|
+
- [Stability · Tests · Docs](#stability--tests--docs)
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## Getting started
|
|
34
|
+
|
|
35
|
+
Every product agent is a `handleChatTurn` call inside a route. This is what gtm / creative / legal / tax all run in production:
|
|
12
36
|
|
|
13
37
|
```ts
|
|
14
38
|
import { handleChatTurn } from '@tangle-network/agent-runtime'
|
|
@@ -33,106 +57,278 @@ export async function POST({ request, env, ctx }: { request: Request; env: Env;
|
|
|
33
57
|
}
|
|
34
58
|
```
|
|
35
59
|
|
|
36
|
-
That's the centerpiece. Everything
|
|
60
|
+
That's the centerpiece. Everything below is *"when one chat turn isn't enough"* — multi-shot loops, delegation, optimization, and the telemetry that makes them auditable.
|
|
61
|
+
|
|
62
|
+
---
|
|
37
63
|
|
|
38
64
|
## Which entry point do I reach for?
|
|
39
65
|
|
|
66
|
+
| You want to… | Reach for | Subpath |
|
|
67
|
+
|---|---|---|
|
|
68
|
+
| Run a production chat turn (90% of products) | `handleChatTurn` | root |
|
|
69
|
+
| Declare an agent (profile + surfaces + adapters) | `defineAgent` | `/agent` |
|
|
70
|
+
| One-shot task with verification + eval | `runAgentTask` | root |
|
|
71
|
+
| Multi-shot loop (refine / fanout-vote) | `runLoop` + a driver | `/loops` |
|
|
72
|
+
| Let the **agent choose** the loop shape per round | `createDynamicDriver` + `createSandboxPlanner` | `/loops` |
|
|
73
|
+
| Delegate a disciplined loop by mode (code/research/…) | `runDelegatedLoop` / `agent-runtime-loop` | root |
|
|
74
|
+
| Build code reliably (reviewed, gated) | `createDefaultCoderDelegate` | `/mcp` |
|
|
75
|
+
| Grow a KB with only grounded facts | `createKbGate` | `/mcp` |
|
|
76
|
+
| Improve a prompt safely (identity-gated) | `optimizePrompt` | `/improvement` |
|
|
77
|
+
| Ship loop traces to a GenAI viewer | `buildLoopOtelSpans` + `createOtelExporter` | root |
|
|
78
|
+
| Expose delegation as MCP tools to a sandbox agent | `createMcpServer` / `agent-runtime-mcp` | `/mcp` |
|
|
79
|
+
| Mutate surfaces from trace findings | `runAnalystLoop` | `/analyst-loop` |
|
|
80
|
+
| Persist a run + cost ledger | `startRuntimeRun` | root |
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## Capabilities
|
|
85
|
+
|
|
86
|
+
### 1. Chat turns — `handleChatTurn`
|
|
87
|
+
|
|
88
|
+
The production turn envelope: frames a producer with the `session.run.*` NDJSON protocol, the persist → post-process → trace-flush hook order, and a stable execution id for client-retry replay. See [Getting started](#getting-started) and [`examples/chat-handler/`](./examples/chat-handler/).
|
|
89
|
+
|
|
90
|
+
### 2. Driven loops + topology drivers
|
|
91
|
+
|
|
92
|
+
`runLoop` is a topology-agnostic kernel: each iteration spawns a sandbox on an `AgentRunSpec`, decodes the output, validates it, and asks a **driver** what to do next. The driver owns topology; the validator owns scoring; the kernel owns iteration accounting, concurrency, cost/token aggregation, and trace emission.
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import { runLoop, createFanoutVoteDriver } from '@tangle-network/agent-runtime/loops'
|
|
96
|
+
|
|
97
|
+
const result = await runLoop({
|
|
98
|
+
driver: createFanoutVoteDriver({ n: 3 }), // 3 parallel attempts, pick the best valid one
|
|
99
|
+
agentRuns: [claudeSpec, codexSpec, glmSpec], // heterogeneous: one harness per branch
|
|
100
|
+
output, // events → typed Output
|
|
101
|
+
validator, // Output → { valid, score }
|
|
102
|
+
task,
|
|
103
|
+
ctx: { sandboxClient: sandbox },
|
|
104
|
+
})
|
|
105
|
+
result.winner // highest-scoring valid attempt
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Shipped drivers (`/loops/drivers`): **`createRefineDriver`** (single task, iterate until valid) and **`createFanoutVoteDriver`** (N parallel, vote). See [`examples/coder-loop/`](./examples/coder-loop/) and [`examples/researcher-loop/`](./examples/researcher-loop/).
|
|
109
|
+
|
|
110
|
+
### 3. Agent-authored topology — `createDynamicDriver`
|
|
111
|
+
|
|
112
|
+
The third driver lets the **agent author the loop topology at runtime** — refine, fan out, or stop, decided per round by an injected planner. Topology is orthogonal to harness: the planner never names a backend; the kernel's `agentRuns` round-robin decides which harness runs each branch.
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
import { runLoop, createDynamicDriver, createSandboxPlanner } from '@tangle-network/agent-runtime/loops'
|
|
116
|
+
|
|
117
|
+
const planner = createSandboxPlanner({
|
|
118
|
+
client: sandbox,
|
|
119
|
+
profile: { name: 'planner', metadata: { backendType: 'claude-code' } }, // cheap model is fine
|
|
120
|
+
decodeTask: (raw) => raw as Task,
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
const result = await runLoop({
|
|
124
|
+
driver: createDynamicDriver({ planner, maxIterations: 8 }),
|
|
125
|
+
agentRuns: [claudeSpec, codexSpec], // the planner can fan a single round across both
|
|
126
|
+
output, validator, task,
|
|
127
|
+
ctx: { sandboxClient: sandbox },
|
|
128
|
+
})
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
The planner emits one `TopologyMove` per round (`refine` | `fanout` | `stop`) with a rationale; a malformed move throws `PlannerError` (the loop never runs a topology nobody chose).
|
|
132
|
+
|
|
133
|
+
### 4. Delegated loop-runner — `runDelegatedLoop`
|
|
134
|
+
|
|
135
|
+
One configured entrypoint a worker agent (or a scheduled routine) calls to run a disciplined loop in a chosen **mode**, over the hardened engines below. Fail-loud on an unwired mode; a thrown engine is captured as `{ ok: false }` so unattended runs *record* rather than crash.
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
import {
|
|
139
|
+
runDelegatedLoop, coderLoopRunner, researchLoopRunner, type DelegatedLoopRegistry,
|
|
140
|
+
} from '@tangle-network/agent-runtime'
|
|
141
|
+
|
|
142
|
+
const registry: DelegatedLoopRegistry = {
|
|
143
|
+
code: coderLoopRunner({
|
|
144
|
+
sandboxClient,
|
|
145
|
+
args: { goal: 'fix the flaky retry test', repoRoot: '/repo' },
|
|
146
|
+
reviewer, // optional adversarial gate
|
|
147
|
+
winnerSelection: 'smallest-diff',
|
|
148
|
+
}),
|
|
149
|
+
research: researchLoopRunner({ research, gate: { selfArtifactKinds: ['spec'] }, maxRounds: 3 }),
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const result = await runDelegatedLoop('code', registry)
|
|
153
|
+
// → { mode: 'code', ok: true, output: CoderOutput, durationMs }
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Modes: `code` · `review` · `research` · `audit` · `self-improve` · `dynamic` — each with a default factory (`coderLoopRunner`, `reviewLoopRunner`, `researchLoopRunner`, `dynamicLoopRunner`, `selfImproveLoopRunner`, `auditLoopRunner`).
|
|
157
|
+
|
|
158
|
+
**Schedulable**: the `agent-runtime-loop` bin runs it from a cron/routine. The config module wires the registry (with full env/creds access):
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
agent-runtime-loop --mode research --config ./loops.config.js
|
|
162
|
+
# exits 0 (ok) · 1 (recorded failure) · 2 (usage/config error); prints the result as JSON
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
// loops.config.js — default-exports a DelegatedLoopRegistry (or a factory)
|
|
167
|
+
import { researchLoopRunner } from '@tangle-network/agent-runtime'
|
|
168
|
+
export default { research: researchLoopRunner({ research: myResearchEngine, maxRounds: 3 }) }
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### 5. Reliable build-in-a-loop — the coder delegate
|
|
172
|
+
|
|
173
|
+
`createDefaultCoderDelegate` drives a coder loop with **default-on safety gates** so it never ships junk:
|
|
174
|
+
|
|
175
|
+
- **no-op rejection** — an empty patch can't "pass" trivially,
|
|
176
|
+
- **secret-path floor** — always-on, independent of `forbiddenPaths` (`.env`, keys, wallets, …),
|
|
177
|
+
- optional **`reviewer`** gate — a candidate must pass tests/typecheck **and** be approved to win,
|
|
178
|
+
- **`winnerSelection`** — `highest-score` (default) · `smallest-diff` · `highest-readiness` · `first-approved`.
|
|
179
|
+
|
|
180
|
+
```ts
|
|
181
|
+
import { createDefaultCoderDelegate } from '@tangle-network/agent-runtime/mcp'
|
|
182
|
+
|
|
183
|
+
const coder = createDefaultCoderDelegate({
|
|
184
|
+
sandboxClient,
|
|
185
|
+
fanoutHarnesses: ['claude-code', 'codex'],
|
|
186
|
+
reviewer: async (output, task) => ({ approved: output.testResult.passed, recommendation: 'ship', readiness: 0.9 }),
|
|
187
|
+
winnerSelection: 'highest-readiness',
|
|
188
|
+
})
|
|
189
|
+
const out = await coder({ goal: 'add a retry with backoff', repoRoot: '/repo', variants: 2 }, ctx)
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
See [`examples/coder-loop/`](./examples/coder-loop/) and [`examples/agent-into-reviewer/`](./examples/agent-into-reviewer/).
|
|
193
|
+
|
|
194
|
+
### 6. Valid-only research — `createKbGate`
|
|
195
|
+
|
|
196
|
+
A fail-closed gate so a knowledge base grows with **only grounded facts**. The always-on floor: a fact's `verbatimPassage` must literally appear in its `sourceText` (anti-hallucination), the asserted value must be in the passage, and citations can't point at self-generated artifacts (laundering). Plug in your own judges; verdict-only (remediation is yours).
|
|
197
|
+
|
|
198
|
+
```ts
|
|
199
|
+
import { createKbGate } from '@tangle-network/agent-runtime/mcp'
|
|
200
|
+
|
|
201
|
+
const gate = createKbGate({ selfArtifactKinds: ['spec', 'cad_params'] })
|
|
202
|
+
const verdict = await gate({
|
|
203
|
+
claim: 'revenue was $1.2B in 2025',
|
|
204
|
+
value: 1_200_000_000,
|
|
205
|
+
verbatimPassage: 'total revenue was $1,200,000,000 for the fiscal year',
|
|
206
|
+
sourceText: rawSource,
|
|
207
|
+
})
|
|
208
|
+
if (verdict.accepted) writeToKb(fact)
|
|
209
|
+
else console.warn('vetoed by', verdict.vetoedBy, verdict.reason)
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
`researchLoopRunner` (mode `research`) wraps this with a correct-on-veto remediation loop: research → gate → re-research the vetoed gaps up to `maxRounds`, then **return** the unverified ones (escalate, never silently drop).
|
|
213
|
+
|
|
214
|
+
### 7. Identity-gated prompt optimization — `optimizePrompt`
|
|
215
|
+
|
|
216
|
+
Optimize any text prompt over agent-eval's `runImprovementLoop`, **identity-gated by construction**: it runs evals, proposes candidates (default `gepaDriver`), and the held-out gate compares candidate vs baseline. `result.prompt` is the **baseline unless the gate decided `ship`** — so registering a prompt for optimization can never regress it.
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
import { optimizePrompt } from '@tangle-network/agent-runtime/improvement'
|
|
220
|
+
|
|
221
|
+
const { prompt, improved, delta } = await optimizePrompt({
|
|
222
|
+
baselinePrompt: CURRENT_SYSTEM_PROMPT,
|
|
223
|
+
runWithPrompt: (candidate, scenario, ctx) => runYourThing(candidate, scenario),
|
|
224
|
+
scenarios, holdoutScenarios, judges, runDir,
|
|
225
|
+
reflection: { llm, model: 'claude-sonnet-4-6' },
|
|
226
|
+
})
|
|
227
|
+
// assign `prompt` unconditionally — it's the safe one
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
See [`examples/self-improving-loop/`](./examples/self-improving-loop/).
|
|
231
|
+
|
|
232
|
+
### 8. OpenTelemetry GenAI topology tracing
|
|
233
|
+
|
|
234
|
+
`runLoop` emits a structured event stream; `buildLoopOtelSpans` turns it into a **nested, real-duration span tree** that any GenAI trace viewer (Phoenix, Langfuse, Grafana Tempo, Tangle Intelligence) renders natively. Attributes follow the current GenAI semantic conventions (`gen_ai.operation.name`, `gen_ai.agent.name`, `gen_ai.usage.input_tokens/output_tokens`) plus a `tangle.loop.*` extension for the topology (move kind/rationale, edge lineage, verdict, placement, cost).
|
|
235
|
+
|
|
236
|
+
```ts
|
|
237
|
+
import { buildLoopOtelSpans, createOtelExporter } from '@tangle-network/agent-runtime'
|
|
238
|
+
|
|
239
|
+
const exporter = createOtelExporter() // reads OTEL_EXPORTER_OTLP_ENDPOINT
|
|
240
|
+
for (const span of buildLoopOtelSpans(loopEvents, traceId)) exporter?.exportSpan(span)
|
|
241
|
+
await exporter?.flush()
|
|
40
242
|
```
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
243
|
+
|
|
244
|
+
The shape: `loop → loop.round (move + rationale) → loop.iteration (agent, usage, verdict, cost, parent edge)`. See [`examples/with-intelligence-export/`](./examples/with-intelligence-export/).
|
|
245
|
+
|
|
246
|
+
### 9. MCP delegation server — `agent-runtime-mcp`
|
|
247
|
+
|
|
248
|
+
Expose the five delegation tools (`delegate_code`, `delegate_research`, `delegate_feedback`, `delegation_status`, `delegation_history`) to a sandbox coding-harness agent — mount the canonical server, don't fork delegation logic.
|
|
249
|
+
|
|
250
|
+
```ts
|
|
251
|
+
import { createMcpServer, createDefaultCoderDelegate } from '@tangle-network/agent-runtime/mcp'
|
|
252
|
+
|
|
253
|
+
const server = createMcpServer({
|
|
254
|
+
coderDelegate: createDefaultCoderDelegate({ sandboxClient }),
|
|
255
|
+
researcherDelegate, // wire your KB-backed researcher
|
|
256
|
+
})
|
|
52
257
|
```
|
|
53
258
|
|
|
259
|
+
Or mount the `agent-runtime-mcp` stdio bin on a production `AgentProfile.mcp`. See [`examples/mcp-delegation/`](./examples/mcp-delegation/) and [`examples/fleet-delegation/`](./examples/fleet-delegation/).
|
|
260
|
+
|
|
261
|
+
---
|
|
262
|
+
|
|
54
263
|
## Defaults
|
|
55
264
|
|
|
56
265
|
When nothing is specified:
|
|
57
266
|
|
|
58
267
|
| Knob | Default | Override |
|
|
59
268
|
|---|---|---|
|
|
60
|
-
| Backend model | `gpt-4o-mini` (
|
|
61
|
-
| Backend provider | `openai-compat` when `TANGLE_API_KEY
|
|
269
|
+
| Backend model | `gpt-4o-mini` (via `createOpenAICompatibleBackend`) | `model` option / `MODEL_NAME` env |
|
|
270
|
+
| Backend provider | `openai-compat` when `TANGLE_API_KEY`, else `openai` if `OPENAI_API_KEY` | `MODEL_PROVIDER` env |
|
|
62
271
|
| Router base URL | `https://router.tangle.tools/v1` | `TANGLE_ROUTER_BASE_URL` env |
|
|
63
272
|
| Sandbox base URL | `https://sandbox.tangle.tools` | `SANDBOX_API_URL` env |
|
|
64
|
-
| Loop iteration cap | 8 | `runLoop({ maxIterations })` |
|
|
65
|
-
| Driver | none — required
|
|
66
|
-
|
|
|
273
|
+
| Loop iteration cap | 10 (`runLoop`); dynamic driver 8 | `runLoop({ maxIterations })` |
|
|
274
|
+
| Driver | none — required by `runLoop` | `createRefineDriver` / `createFanoutVoteDriver` / `createDynamicDriver` |
|
|
275
|
+
| Winner selection (coder delegate) | `highest-score` | `winnerSelection` option |
|
|
276
|
+
| KB gate min passage | 12 chars | `createKbGate({ minPassageChars })` |
|
|
277
|
+
| `optimizePrompt` gate | `heldOutGate` | `defaultProductionGate` for red-team hardening |
|
|
67
278
|
| OTEL export | off | set `OTEL_EXPORTER_OTLP_ENDPOINT` |
|
|
68
|
-
|
|
|
69
|
-
|
|
70
|
-
## Composition with the rest of the stack
|
|
71
|
-
|
|
72
|
-
```
|
|
73
|
-
agent-runtime ──── handleChatTurn (chat turn lifecycle)
|
|
74
|
-
defineAgent (declarative manifest)
|
|
75
|
-
runLoop (multi-shot kernel)
|
|
76
|
-
createMcpServer (delegation tools server)
|
|
77
|
-
OTEL export (trace pipeline)
|
|
279
|
+
| Loop-runner mode failure | recorded as `{ ok: false }` | `runDelegatedLoop` never crashes on a thrown engine |
|
|
78
280
|
|
|
79
|
-
|
|
80
|
-
(consumes agent-runtime traces, scores, gates promotion)
|
|
281
|
+
---
|
|
81
282
|
|
|
82
|
-
|
|
83
|
-
(analyst-loop produces these; runtime consumes them)
|
|
283
|
+
## Composition with the stack
|
|
84
284
|
|
|
85
|
-
sandbox ──── AgentProfile (substrate type), Sandbox.create, exportTraceBundle
|
|
86
|
-
(provides the harness execution surface)
|
|
87
285
|
```
|
|
286
|
+
agent-runtime ── handleChatTurn · runLoop + drivers · runDelegatedLoop · createMcpServer
|
|
287
|
+
optimizePrompt · createKbGate · buildLoopOtelSpans · defineAgent
|
|
88
288
|
|
|
89
|
-
|
|
289
|
+
agent-eval ── runEvalCampaign · runImprovementLoop (gepaDriver) · heldOutGate · runAgentMatrix
|
|
290
|
+
(consumes runtime traces, scores, gates promotion)
|
|
90
291
|
|
|
91
|
-
|
|
292
|
+
agent-knowledge ─ proposeKnowledgeWrites / applyKnowledgeWriteBlocks
|
|
293
|
+
(analyst-loop produces these; runtime + createKbGate consume them)
|
|
92
294
|
|
|
93
|
-
|
|
295
|
+
sandbox ── AgentProfile · Sandbox.create · streamPrompt · exportTraceBundle
|
|
296
|
+
(the harness execution surface every loop runs on)
|
|
297
|
+
```
|
|
94
298
|
|
|
95
|
-
|
|
96
|
-
- [`chat-handler/`](./examples/chat-handler/) — `handleChatTurn`, the production centerpiece
|
|
299
|
+
---
|
|
97
300
|
|
|
98
|
-
|
|
99
|
-
- [`with-knowledge-readiness/`](./examples/with-knowledge-readiness/) — `requiredKnowledge` + `decideKnowledgeReadiness`
|
|
100
|
-
- [`sanitized-telemetry-streaming/`](./examples/sanitized-telemetry-streaming/) — `createRuntimeStreamEventCollector` + redaction
|
|
101
|
-
- [`runtime-run/`](./examples/runtime-run/) — `startRuntimeRun` + cost ledger persistence
|
|
301
|
+
## Subpath exports
|
|
102
302
|
|
|
103
|
-
|
|
104
|
-
|
|
303
|
+
| Import | Owns |
|
|
304
|
+
|---|---|
|
|
305
|
+
| `@tangle-network/agent-runtime` | chat turns, delegated loop-runner, OTEL export, errors, model resolution |
|
|
306
|
+
| `…/agent` | `defineAgent` + surfaces / outcome adapters |
|
|
307
|
+
| `…/loops` | `runLoop` kernel + `refine` / `fanout-vote` / **`dynamic`** drivers + `loopDispatch` |
|
|
308
|
+
| `…/profiles` | `coderProfile`, `researcherProfile` presets |
|
|
309
|
+
| `…/mcp` | `createMcpServer`, `createDefaultCoderDelegate`, **`createKbGate`**, `agent-runtime-mcp` bin |
|
|
310
|
+
| `…/improvement` | **`optimizePrompt`** (text) + `improvementDriver` (code/worktree) |
|
|
311
|
+
| `…/analyst-loop` | `runAnalystLoop` — analyst registry driver |
|
|
312
|
+
| `…/platform` | cross-site SSO + integrations hub |
|
|
105
313
|
|
|
106
|
-
|
|
107
|
-
- [`coder-loop/`](./examples/coder-loop/) — `coderProfile` + `runLoop` + `FanoutVote`
|
|
108
|
-
- [`researcher-loop/`](./examples/researcher-loop/) — `researcherProfile` + `runLoop` (peer dep: `@tangle-network/agent-knowledge`)
|
|
109
|
-
- [`fleet-delegation/`](./examples/fleet-delegation/) — `TANGLE_FLEET_ID` + `createFleetWorkspaceExecutor`
|
|
314
|
+
Bins: `agent-runtime-mcp` (delegation MCP server) · `agent-runtime-loop` (schedulable delegated loop-runner).
|
|
110
315
|
|
|
111
|
-
|
|
316
|
+
---
|
|
112
317
|
|
|
113
|
-
|
|
318
|
+
## Adoption skill
|
|
114
319
|
|
|
115
|
-
|
|
320
|
+
This package ships a **self-contained adoption skill** at [`skills/agent-runtime-adoption/SKILL.md`](./skills/agent-runtime-adoption/SKILL.md) — driven loops, topology drivers, the `loopDispatch` campaign bridge, MCP delegation, and identity-gated `optimizePrompt`. It needs only this package + `@tangle-network/agent-eval`, so external consumers need nothing private. For the full self-improving pipeline (trace sink → analyst loop → scorecard → production loop → CI), see the `agent-eval-adoption` / `agent-stack-adoption` skills.
|
|
116
321
|
|
|
117
|
-
|
|
118
|
-
|---|---|
|
|
119
|
-
| `agent-runtime` | Task lifecycle, adapters, backends, chat-turn engine, model resolution, trace bridge, `defineAgent` |
|
|
120
|
-
| `agent-runtime/platform` | Cross-site SSO + integrations hub |
|
|
121
|
-
| `agent-runtime/agent` | `defineAgent` + surfaces / outcome adapters |
|
|
122
|
-
| `agent-runtime/analyst-loop` | `runAnalystLoop` — analyst registry driver |
|
|
123
|
-
| `agent-runtime/loops` | `runLoop` kernel + `Refine` / `FanoutVote` drivers |
|
|
124
|
-
| `agent-runtime/profiles` | `coderProfile`, `researcherProfile` presets |
|
|
125
|
-
| `agent-runtime/mcp` | `createMcpServer` + `agent-runtime-mcp` bin (5 delegation tools) |
|
|
126
|
-
| `agent-eval` | Evals, judges, scorecards, RL bridge, release evidence, matrix |
|
|
127
|
-
| `agent-knowledge` | Evidence, claims, wiki pages, retrieval |
|
|
128
|
-
| `sandbox` | `AgentProfile`, `Sandbox.create`, `streamPrompt`, `exportTraceBundle` |
|
|
322
|
+
---
|
|
129
323
|
|
|
130
|
-
|
|
324
|
+
## Stability · Tests · Docs
|
|
131
325
|
|
|
132
|
-
|
|
326
|
+
Every public export is annotated `@stable` or `@experimental`. `@stable` exports don't change shape inside a minor; `@experimental` ones may and require a deliberate consumer bump.
|
|
133
327
|
|
|
134
328
|
```bash
|
|
135
|
-
pnpm test #
|
|
329
|
+
pnpm test # full suite across the kernel, drivers, MCP, delegate hardening, kb-gate, loop-runner, backends
|
|
136
330
|
pnpm typecheck
|
|
137
331
|
pnpm build
|
|
138
332
|
```
|
|
333
|
+
|
|
334
|
+
Deeper docs: [`docs/concepts.md`](./docs/concepts.md) (mental model) · [`docs/agent-bus-protocol.md`](./docs/agent-bus-protocol.md) (cross-gateway header contract) · [`docs/conversation-economics.md`](./docs/conversation-economics.md) (who pays — `authSource`) · [`docs/durability-adapters.md`](./docs/durability-adapters.md) (SQL-backed `ConversationJournal`).
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import {
|
|
2
|
+
runAnalystLoop
|
|
3
|
+
} from "./chunk-XBUG326M.js";
|
|
4
|
+
import {
|
|
5
|
+
optimizePrompt
|
|
6
|
+
} from "./chunk-VOX6Z3II.js";
|
|
7
|
+
import {
|
|
8
|
+
createKbGate
|
|
9
|
+
} from "./chunk-FNMGYYSS.js";
|
|
10
|
+
import {
|
|
11
|
+
createDefaultCoderDelegate
|
|
12
|
+
} from "./chunk-VFKBIZTY.js";
|
|
13
|
+
import {
|
|
14
|
+
createDynamicDriver,
|
|
15
|
+
runLoop
|
|
16
|
+
} from "./chunk-OISRXLWI.js";
|
|
17
|
+
import {
|
|
18
|
+
ConfigError
|
|
19
|
+
} from "./chunk-SQSCRJ7U.js";
|
|
20
|
+
|
|
21
|
+
// src/loop-runner.ts
|
|
22
|
+
var DELEGATED_LOOP_MODES = [
|
|
23
|
+
"code",
|
|
24
|
+
"review",
|
|
25
|
+
"research",
|
|
26
|
+
"audit",
|
|
27
|
+
"self-improve",
|
|
28
|
+
"dynamic"
|
|
29
|
+
];
|
|
30
|
+
function isDelegatedLoopMode(value) {
|
|
31
|
+
return typeof value === "string" && DELEGATED_LOOP_MODES.includes(value);
|
|
32
|
+
}
|
|
33
|
+
async function runDelegatedLoop(mode, registry, options = {}) {
|
|
34
|
+
const runner = registry[mode];
|
|
35
|
+
if (!runner) {
|
|
36
|
+
throw new ConfigError(
|
|
37
|
+
`runDelegatedLoop: no runner registered for mode '${mode}' (registered: ${Object.keys(registry).join(", ") || "none"})`
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
const now = options.now ?? Date.now;
|
|
41
|
+
const signal = options.signal ?? new AbortController().signal;
|
|
42
|
+
const start = now();
|
|
43
|
+
try {
|
|
44
|
+
const output = await runner(signal);
|
|
45
|
+
return { mode, ok: true, output, durationMs: now() - start };
|
|
46
|
+
} catch (err) {
|
|
47
|
+
return {
|
|
48
|
+
mode,
|
|
49
|
+
ok: false,
|
|
50
|
+
error: err instanceof Error ? err.message : String(err),
|
|
51
|
+
durationMs: now() - start
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function coderLoopRunner(options) {
|
|
56
|
+
const delegate = createDefaultCoderDelegate({
|
|
57
|
+
sandboxClient: options.sandboxClient,
|
|
58
|
+
...options.reviewer ? { reviewer: options.reviewer } : {},
|
|
59
|
+
...options.winnerSelection ? { winnerSelection: options.winnerSelection } : {},
|
|
60
|
+
...options.fanoutHarnesses ? { fanoutHarnesses: options.fanoutHarnesses } : {}
|
|
61
|
+
});
|
|
62
|
+
return async (signal) => {
|
|
63
|
+
const ctx = { signal, report: () => {
|
|
64
|
+
} };
|
|
65
|
+
return delegate(options.args, ctx);
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function reviewLoopRunner(options) {
|
|
69
|
+
return coderLoopRunner(options);
|
|
70
|
+
}
|
|
71
|
+
function dynamicLoopRunner(o) {
|
|
72
|
+
return async (signal) => runLoop({
|
|
73
|
+
driver: createDynamicDriver({
|
|
74
|
+
planner: o.planner,
|
|
75
|
+
...o.maxIterations !== void 0 ? { maxIterations: o.maxIterations } : {},
|
|
76
|
+
...o.maxFanout !== void 0 ? { maxFanout: o.maxFanout } : {}
|
|
77
|
+
}),
|
|
78
|
+
...o.agentRun ? { agentRun: o.agentRun } : {},
|
|
79
|
+
...o.agentRuns ? { agentRuns: o.agentRuns } : {},
|
|
80
|
+
output: o.output,
|
|
81
|
+
...o.validator ? { validator: o.validator } : {},
|
|
82
|
+
task: o.task,
|
|
83
|
+
ctx: { sandboxClient: o.sandboxClient, signal },
|
|
84
|
+
...o.maxIterations !== void 0 ? { maxIterations: o.maxIterations } : {}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
function researchLoopRunner(o) {
|
|
88
|
+
const gate = createKbGate(o.gate);
|
|
89
|
+
const maxRounds = Math.max(1, Math.trunc(o.maxRounds ?? 1));
|
|
90
|
+
return async (signal) => {
|
|
91
|
+
const accepted = [];
|
|
92
|
+
let vetoed = [];
|
|
93
|
+
let rounds = 0;
|
|
94
|
+
for (let round = 0; round < maxRounds; round += 1) {
|
|
95
|
+
if (signal.aborted) break;
|
|
96
|
+
rounds += 1;
|
|
97
|
+
const candidates = await o.research(round, vetoed);
|
|
98
|
+
if (candidates.length === 0) break;
|
|
99
|
+
vetoed = [];
|
|
100
|
+
for (const c of candidates) {
|
|
101
|
+
const v = await gate(c);
|
|
102
|
+
if (v.accepted) accepted.push(c);
|
|
103
|
+
else vetoed.push({ candidate: c, vetoedBy: v.vetoedBy, reason: v.reason });
|
|
104
|
+
}
|
|
105
|
+
if (vetoed.length === 0) break;
|
|
106
|
+
}
|
|
107
|
+
return { accepted, vetoed, rounds };
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function selfImproveLoopRunner(options) {
|
|
111
|
+
return async () => optimizePrompt(options);
|
|
112
|
+
}
|
|
113
|
+
function auditLoopRunner(options) {
|
|
114
|
+
return async () => runAnalystLoop(options);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// src/loop-runner-bin.ts
|
|
118
|
+
async function runLoopRunnerCli(args) {
|
|
119
|
+
if (!isDelegatedLoopMode(args.mode)) {
|
|
120
|
+
return {
|
|
121
|
+
exitCode: 2,
|
|
122
|
+
error: `unknown mode '${args.mode}' (expected one of: ${DELEGATED_LOOP_MODES.join(", ")})`
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
let registry;
|
|
126
|
+
try {
|
|
127
|
+
registry = await args.loadRegistry();
|
|
128
|
+
} catch (err) {
|
|
129
|
+
return { exitCode: 2, error: `failed to load registry: ${errMsg(err)}` };
|
|
130
|
+
}
|
|
131
|
+
if (!registry[args.mode]) {
|
|
132
|
+
return {
|
|
133
|
+
exitCode: 2,
|
|
134
|
+
error: `config registers no runner for mode '${args.mode}' (registered: ${Object.keys(registry).join(", ") || "none"})`
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
const result = await runDelegatedLoop(args.mode, registry, {
|
|
138
|
+
...args.now ? { now: args.now } : {}
|
|
139
|
+
});
|
|
140
|
+
return { exitCode: result.ok ? 0 : 1, result };
|
|
141
|
+
}
|
|
142
|
+
function parseLoopRunnerArgv(argv) {
|
|
143
|
+
const out = {};
|
|
144
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
145
|
+
const a = argv[i];
|
|
146
|
+
if (a === "--mode") out.mode = argv[++i];
|
|
147
|
+
else if (a === "--config") out.config = argv[++i];
|
|
148
|
+
else if (a?.startsWith("--mode=")) out.mode = a.slice("--mode=".length);
|
|
149
|
+
else if (a?.startsWith("--config=")) out.config = a.slice("--config=".length);
|
|
150
|
+
}
|
|
151
|
+
return out;
|
|
152
|
+
}
|
|
153
|
+
function resolveRegistry(mod) {
|
|
154
|
+
const def = mod?.default ?? mod;
|
|
155
|
+
const value = typeof def === "function" ? def() : def;
|
|
156
|
+
return value;
|
|
157
|
+
}
|
|
158
|
+
function errMsg(err) {
|
|
159
|
+
return err instanceof Error ? err.message : String(err);
|
|
160
|
+
}
|
|
161
|
+
async function main() {
|
|
162
|
+
const { mode, config } = parseLoopRunnerArgv(process.argv.slice(2));
|
|
163
|
+
if (!mode || !config) {
|
|
164
|
+
process.stderr.write(
|
|
165
|
+
`usage: agent-runtime-loop --mode <mode> --config <module>
|
|
166
|
+
modes: ${DELEGATED_LOOP_MODES.join(" | ")}
|
|
167
|
+
config: a JS/TS module default-exporting a DelegatedLoopRegistry (or a factory)
|
|
168
|
+
`
|
|
169
|
+
);
|
|
170
|
+
process.exit(2);
|
|
171
|
+
}
|
|
172
|
+
const { pathToFileURL } = await import("url");
|
|
173
|
+
const { resolve } = await import("path");
|
|
174
|
+
const cli = await runLoopRunnerCli({
|
|
175
|
+
mode,
|
|
176
|
+
loadRegistry: async () => resolveRegistry(await import(pathToFileURL(resolve(config)).href))
|
|
177
|
+
});
|
|
178
|
+
process.stdout.write(`${JSON.stringify(cli.result ?? { error: cli.error }, null, 2)}
|
|
179
|
+
`);
|
|
180
|
+
if (cli.error) process.stderr.write(`${cli.error}
|
|
181
|
+
`);
|
|
182
|
+
process.exit(cli.exitCode);
|
|
183
|
+
}
|
|
184
|
+
if (process.argv[1] && /loop-runner-bin\.(js|ts|mjs)$/.test(process.argv[1])) {
|
|
185
|
+
void main();
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export {
|
|
189
|
+
DELEGATED_LOOP_MODES,
|
|
190
|
+
isDelegatedLoopMode,
|
|
191
|
+
runDelegatedLoop,
|
|
192
|
+
coderLoopRunner,
|
|
193
|
+
reviewLoopRunner,
|
|
194
|
+
dynamicLoopRunner,
|
|
195
|
+
researchLoopRunner,
|
|
196
|
+
selfImproveLoopRunner,
|
|
197
|
+
auditLoopRunner,
|
|
198
|
+
runLoopRunnerCli,
|
|
199
|
+
parseLoopRunnerArgv
|
|
200
|
+
};
|
|
201
|
+
//# sourceMappingURL=chunk-3WQJRSUJ.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/loop-runner.ts","../src/loop-runner-bin.ts"],"sourcesContent":["/**\n * @experimental\n *\n * `runDelegatedLoop` — the configured delegated loop-runner.\n *\n * One typed entrypoint a worker agent (or a scheduled routine) calls to run a\n * disciplined loop in a chosen MODE, over agent-runtime's hardened engines:\n *\n * code → build-in-a-loop via the coder delegate (no-op + secret floor,\n * optional reviewer gate, winner-selection)\n * review → code mode with a REQUIRED reviewer (the gate is the point)\n * research → research-in-a-loop with valid-only KB growth (createKbGate)\n * audit → analyze trace/run data → findings (runAnalystLoop, caller-wired)\n * self-improve → identity-gated prompt optimization (optimizePrompt, caller-wired)\n * dynamic → agent-authored topology (runLoop + createDynamicDriver)\n *\n * It is intentionally a thin façade: the value is that EVERY product reuses the\n * one hardened engine instead of forking delegation logic. The dispatcher owns\n * mode routing, timing, fail-loud on an unregistered mode, and a uniform result\n * shape; each mode's engine is a pre-configured runner in the registry (build it\n * with the factories below, or inject your own / a stub).\n */\n\nimport type { Scenario } from '@tangle-network/agent-eval/campaign'\nimport { runAnalystLoop } from './analyst-loop'\nimport type { RunAnalystLoopOpts, RunAnalystLoopResult } from './analyst-loop/types'\nimport { ConfigError } from './errors'\nimport {\n type OptimizePromptOptions,\n type OptimizePromptResult,\n optimizePrompt,\n} from './improvement/optimize-prompt'\nimport {\n type AgentRunSpec,\n createDynamicDriver,\n type DynamicDecision,\n type LoopResult,\n type LoopSandboxClient,\n type OutputAdapter,\n runLoop,\n type TopologyPlanner,\n type Validator,\n} from './loops'\nimport {\n type CoderReviewer,\n type CoderWinnerSelection,\n createDefaultCoderDelegate,\n type DelegateRunCtx,\n} from './mcp/delegates'\nimport { type CreateKbGateOptions, createKbGate, type FactCandidate } from './mcp/kb-gate'\nimport type { DelegateCodeArgs } from './mcp/types'\nimport type { CoderOutput } from './profiles/coder'\n\n/** @experimental Every delegated-loop mode, for validation + CLI surfaces. */\nexport const DELEGATED_LOOP_MODES = [\n 'code',\n 'review',\n 'research',\n 'audit',\n 'self-improve',\n 'dynamic',\n] as const\n\n/** @experimental */\nexport type DelegatedLoopMode = (typeof DELEGATED_LOOP_MODES)[number]\n\n/** @experimental Type guard for an untrusted mode string (CLI / config input). */\nexport function isDelegatedLoopMode(value: unknown): value is DelegatedLoopMode {\n return typeof value === 'string' && (DELEGATED_LOOP_MODES as readonly string[]).includes(value)\n}\n\n/** @experimental A pre-configured loop for one mode. Returns the mode's raw\n * output; the dispatcher wraps it in a {@link DelegatedLoopResult}. */\nexport type DelegatedLoopRunner<T = unknown> = (signal: AbortSignal) => Promise<T>\n\n/** @experimental Mode → configured runner. Partial: only register the modes a\n * given product/routine actually uses. */\nexport type DelegatedLoopRegistry = Partial<Record<DelegatedLoopMode, DelegatedLoopRunner>>\n\n/** @experimental Uniform result — never throws from a registered runner; a\n * thrown engine becomes `{ ok: false, error }` so a routine can record + move on. */\nexport interface DelegatedLoopResult<T = unknown> {\n mode: DelegatedLoopMode\n ok: boolean\n output?: T\n error?: string\n durationMs: number\n}\n\n/** @experimental */\nexport interface RunDelegatedLoopOptions {\n signal?: AbortSignal\n /** Clock override for deterministic tests. */\n now?: () => number\n}\n\n/**\n * @experimental\n *\n * Dispatch a configured loop by mode. Fails loud (throws `ConfigError`) when no\n * runner is registered for the mode — a routine pointed at an unwired mode is a\n * config bug, not a silent no-op. A runner that throws is captured as\n * `{ ok: false }` so unattended runs record the failure rather than crash.\n */\nexport async function runDelegatedLoop<T = unknown>(\n mode: DelegatedLoopMode,\n registry: DelegatedLoopRegistry,\n options: RunDelegatedLoopOptions = {},\n): Promise<DelegatedLoopResult<T>> {\n const runner = registry[mode] as DelegatedLoopRunner<T> | undefined\n if (!runner) {\n throw new ConfigError(\n `runDelegatedLoop: no runner registered for mode '${mode}' (registered: ${\n Object.keys(registry).join(', ') || 'none'\n })`,\n )\n }\n const now = options.now ?? Date.now\n const signal = options.signal ?? new AbortController().signal\n const start = now()\n try {\n const output = await runner(signal)\n return { mode, ok: true, output, durationMs: now() - start }\n } catch (err) {\n return {\n mode,\n ok: false,\n error: err instanceof Error ? err.message : String(err),\n durationMs: now() - start,\n }\n }\n}\n\n/** @experimental Options for the default `code`/`review` runner. */\nexport interface CoderLoopRunnerOptions {\n sandboxClient: LoopSandboxClient\n /** What to build — the delegate args (goal, repoRoot, variants, config, …). */\n args: DelegateCodeArgs\n /** Adversarial reviewer. REQUIRED for `review` mode (see `reviewLoopRunner`). */\n reviewer?: CoderReviewer\n /** Winner-selection strategy. Default `highest-score`. */\n winnerSelection?: CoderWinnerSelection\n /** Harnesses for `variants > 1` fanout. */\n fanoutHarnesses?: string[]\n}\n\n/** @experimental Build a `code`-mode runner over the hardened coder delegate. */\nexport function coderLoopRunner(options: CoderLoopRunnerOptions): DelegatedLoopRunner<CoderOutput> {\n const delegate = createDefaultCoderDelegate({\n sandboxClient: options.sandboxClient,\n ...(options.reviewer ? { reviewer: options.reviewer } : {}),\n ...(options.winnerSelection ? { winnerSelection: options.winnerSelection } : {}),\n ...(options.fanoutHarnesses ? { fanoutHarnesses: options.fanoutHarnesses } : {}),\n })\n return async (signal) => {\n const ctx: DelegateRunCtx = { signal, report: () => {} }\n return delegate(options.args, ctx)\n }\n}\n\n/**\n * @experimental\n *\n * `review` mode = `code` with a REQUIRED reviewer. The gate is the whole point,\n * so the type forces a reviewer (a \"review loop\" with no reviewer is a code loop).\n */\nexport function reviewLoopRunner(\n options: CoderLoopRunnerOptions & { reviewer: CoderReviewer },\n): DelegatedLoopRunner<CoderOutput> {\n return coderLoopRunner(options)\n}\n\n/** @experimental Options for the default `dynamic` runner. */\nexport interface DynamicLoopRunnerOptions<Task, Output> {\n sandboxClient: LoopSandboxClient\n /** The agent-authored topology planner (e.g. `createSandboxPlanner(...)`). */\n planner: TopologyPlanner<Task, Output>\n task: Task\n output: OutputAdapter<Output>\n validator?: Validator<Output>\n /** Exactly one of `agentRun` / `agentRuns` (runLoop validates). */\n agentRun?: AgentRunSpec<Task>\n agentRuns?: AgentRunSpec<Task>[]\n maxIterations?: number\n maxFanout?: number\n}\n\n/** @experimental `dynamic` mode — agent-authored topology over `runLoop`. */\nexport function dynamicLoopRunner<Task, Output>(\n o: DynamicLoopRunnerOptions<Task, Output>,\n): DelegatedLoopRunner<LoopResult<Task, Output, DynamicDecision>> {\n return async (signal) =>\n runLoop<Task, Output, DynamicDecision>({\n driver: createDynamicDriver<Task, Output>({\n planner: o.planner,\n ...(o.maxIterations !== undefined ? { maxIterations: o.maxIterations } : {}),\n ...(o.maxFanout !== undefined ? { maxFanout: o.maxFanout } : {}),\n }),\n ...(o.agentRun ? { agentRun: o.agentRun } : {}),\n ...(o.agentRuns ? { agentRuns: o.agentRuns } : {}),\n output: o.output,\n ...(o.validator ? { validator: o.validator } : {}),\n task: o.task,\n ctx: { sandboxClient: o.sandboxClient, signal },\n ...(o.maxIterations !== undefined ? { maxIterations: o.maxIterations } : {}),\n })\n}\n\n/** @experimental A fact rejected at the KB gate — surfaced, never dropped. */\nexport interface VetoedFact {\n candidate: FactCandidate\n vetoedBy?: string\n reason?: string\n}\n\n/** @experimental */\nexport interface ResearchLoopResult {\n /** Facts that passed the fail-closed gate — safe to write to the KB. */\n accepted: FactCandidate[]\n /** Facts the gate vetoed in the final round — escalate, do not silently drop. */\n vetoed: VetoedFact[]\n /** Research rounds actually run. */\n rounds: number\n}\n\n/** @experimental Options for the default `research` runner. */\nexport interface ResearchLoopRunnerOptions {\n /**\n * The research engine (the consumer's web/doc searcher + extractor). Called\n * each round with the prior round's vetoes so it can re-research the gaps.\n * Returns fact candidates carrying their grounding (`verbatimPassage` +\n * `sourceText`).\n */\n research: (round: number, vetoed: VetoedFact[]) => Promise<FactCandidate[]>\n /** Gate config (extra judges, self-artifact kinds, …). The floor is always on. */\n gate?: CreateKbGateOptions\n /** Max research rounds (correct-on-veto remediation). Default 1. */\n maxRounds?: number\n}\n\n/**\n * @experimental `research` mode — research-in-a-loop with valid-only KB growth.\n *\n * Each round: research → gate every candidate (fail-closed; passage MUST be in\n * the source) → accept the clean ones → re-research the vetoed ones next round,\n * up to `maxRounds`. Vetoed facts in the final round are RETURNED (escalate,\n * never silently dropped) so the caller audits vs retries.\n */\nexport function researchLoopRunner(\n o: ResearchLoopRunnerOptions,\n): DelegatedLoopRunner<ResearchLoopResult> {\n const gate = createKbGate(o.gate)\n const maxRounds = Math.max(1, Math.trunc(o.maxRounds ?? 1))\n return async (signal) => {\n const accepted: FactCandidate[] = []\n let vetoed: VetoedFact[] = []\n let rounds = 0\n for (let round = 0; round < maxRounds; round += 1) {\n if (signal.aborted) break\n rounds += 1\n const candidates = await o.research(round, vetoed)\n if (candidates.length === 0) break\n vetoed = []\n for (const c of candidates) {\n const v = await gate(c)\n if (v.accepted) accepted.push(c)\n else vetoed.push({ candidate: c, vetoedBy: v.vetoedBy, reason: v.reason })\n }\n if (vetoed.length === 0) break\n }\n return { accepted, vetoed, rounds }\n }\n}\n\n/** @experimental `self-improve` mode — identity-gated prompt optimization. */\nexport function selfImproveLoopRunner<TScenario extends Scenario, TArtifact>(\n options: OptimizePromptOptions<TScenario, TArtifact>,\n): DelegatedLoopRunner<OptimizePromptResult<TArtifact, TScenario>> {\n return async () => optimizePrompt<TScenario, TArtifact>(options)\n}\n\n/** @experimental `audit` mode — analyst loop over captured trace/run data. */\nexport function auditLoopRunner<TProposal = unknown, TEdit = unknown>(\n options: RunAnalystLoopOpts,\n): DelegatedLoopRunner<RunAnalystLoopResult<TProposal, TEdit>> {\n return async () => runAnalystLoop<TProposal, TEdit>(options)\n}\n","#!/usr/bin/env node\n/**\n * @experimental\n *\n * `agent-runtime-loop` — the schedulable entrypoint for the configured\n * delegated loop-runner. A cron job / routine / Makefile target invokes:\n *\n * agent-runtime-loop --mode research --config ./loops.config.js\n *\n * The config module wires the registry (with full access to env / creds —\n * which is why the deps live there, not in this generic bin). It must default-\n * export a `DelegatedLoopRegistry`, or a `() => DelegatedLoopRegistry | Promise<…>`.\n * The bin runs the selected mode, prints the `DelegatedLoopResult` as JSON, and\n * exits 0 on `ok`, 1 on a recorded failure, 2 on a usage/config error.\n */\n\nimport {\n DELEGATED_LOOP_MODES,\n type DelegatedLoopMode,\n type DelegatedLoopRegistry,\n type DelegatedLoopResult,\n isDelegatedLoopMode,\n runDelegatedLoop,\n} from './loop-runner'\n\n/** @experimental Parsed CLI invocation. */\nexport interface LoopRunnerCliArgs {\n mode: string\n /** Loads the registry — the bin wires this from `--config`; tests inject a stub. */\n loadRegistry: () => Promise<DelegatedLoopRegistry> | DelegatedLoopRegistry\n now?: () => number\n}\n\n/** @experimental */\nexport interface LoopRunnerCliResult {\n exitCode: number\n result?: DelegatedLoopResult\n error?: string\n}\n\n/**\n * @experimental\n *\n * Pure CLI core (no process / argv / IO) so it's unit-testable: validate the\n * mode, load the registry, dispatch, map to an exit code (0 ok / 1 failed /\n * 2 usage). Exported for embedding in custom runners + tests.\n */\nexport async function runLoopRunnerCli(args: LoopRunnerCliArgs): Promise<LoopRunnerCliResult> {\n if (!isDelegatedLoopMode(args.mode)) {\n return {\n exitCode: 2,\n error: `unknown mode '${args.mode}' (expected one of: ${DELEGATED_LOOP_MODES.join(', ')})`,\n }\n }\n let registry: DelegatedLoopRegistry\n try {\n registry = await args.loadRegistry()\n } catch (err) {\n return { exitCode: 2, error: `failed to load registry: ${errMsg(err)}` }\n }\n if (!registry[args.mode]) {\n return {\n exitCode: 2,\n error: `config registers no runner for mode '${args.mode}' (registered: ${\n Object.keys(registry).join(', ') || 'none'\n })`,\n }\n }\n // runDelegatedLoop throws only on a missing runner (guarded above); a failing\n // engine is captured as { ok: false } → exit 1, not a crash.\n const result = await runDelegatedLoop(args.mode as DelegatedLoopMode, registry, {\n ...(args.now ? { now: args.now } : {}),\n })\n return { exitCode: result.ok ? 0 : 1, result }\n}\n\n/** Parse `--mode X --config Y` from an argv tail (`process.argv.slice(2)`). */\nexport function parseLoopRunnerArgv(argv: string[]): { mode?: string; config?: string } {\n const out: { mode?: string; config?: string } = {}\n for (let i = 0; i < argv.length; i += 1) {\n const a = argv[i]\n if (a === '--mode') out.mode = argv[++i]\n else if (a === '--config') out.config = argv[++i]\n else if (a?.startsWith('--mode=')) out.mode = a.slice('--mode='.length)\n else if (a?.startsWith('--config=')) out.config = a.slice('--config='.length)\n }\n return out\n}\n\n/** Normalize a config module's default export → a registry. */\nfunction resolveRegistry(mod: unknown): DelegatedLoopRegistry {\n const def = (mod as { default?: unknown })?.default ?? mod\n const value = typeof def === 'function' ? (def as () => unknown)() : def\n return value as DelegatedLoopRegistry\n}\n\nfunction errMsg(err: unknown): string {\n return err instanceof Error ? err.message : String(err)\n}\n\n/** The argv → IO → exit shell. Kept thin; logic lives in `runLoopRunnerCli`. */\nasync function main(): Promise<void> {\n const { mode, config } = parseLoopRunnerArgv(process.argv.slice(2))\n if (!mode || !config) {\n process.stderr.write(\n 'usage: agent-runtime-loop --mode <mode> --config <module>\\n' +\n ` modes: ${DELEGATED_LOOP_MODES.join(' | ')}\\n` +\n ' config: a JS/TS module default-exporting a DelegatedLoopRegistry (or a factory)\\n',\n )\n process.exit(2)\n }\n const { pathToFileURL } = await import('node:url')\n const { resolve } = await import('node:path')\n const cli = await runLoopRunnerCli({\n mode,\n loadRegistry: async () => resolveRegistry(await import(pathToFileURL(resolve(config)).href)),\n })\n process.stdout.write(`${JSON.stringify(cli.result ?? { error: cli.error }, null, 2)}\\n`)\n if (cli.error) process.stderr.write(`${cli.error}\\n`)\n process.exit(cli.exitCode)\n}\n\n// Run only when executed as the bin (not when imported for the testable core).\nif (process.argv[1] && /loop-runner-bin\\.(js|ts|mjs)$/.test(process.argv[1])) {\n void main()\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAsDO,IAAM,uBAAuB;AAAA,EAClC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAMO,SAAS,oBAAoB,OAA4C;AAC9E,SAAO,OAAO,UAAU,YAAa,qBAA2C,SAAS,KAAK;AAChG;AAmCA,eAAsB,iBACpB,MACA,UACA,UAAmC,CAAC,GACH;AACjC,QAAM,SAAS,SAAS,IAAI;AAC5B,MAAI,CAAC,QAAQ;AACX,UAAM,IAAI;AAAA,MACR,oDAAoD,IAAI,kBACtD,OAAO,KAAK,QAAQ,EAAE,KAAK,IAAI,KAAK,MACtC;AAAA,IACF;AAAA,EACF;AACA,QAAM,MAAM,QAAQ,OAAO,KAAK;AAChC,QAAM,SAAS,QAAQ,UAAU,IAAI,gBAAgB,EAAE;AACvD,QAAM,QAAQ,IAAI;AAClB,MAAI;AACF,UAAM,SAAS,MAAM,OAAO,MAAM;AAClC,WAAO,EAAE,MAAM,IAAI,MAAM,QAAQ,YAAY,IAAI,IAAI,MAAM;AAAA,EAC7D,SAAS,KAAK;AACZ,WAAO;AAAA,MACL;AAAA,MACA,IAAI;AAAA,MACJ,OAAO,eAAe,QAAQ,I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