@tangle-network/agent-runtime 0.18.0 → 0.20.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 +110 -0
- package/dist/agent.d.ts +1 -1
- package/dist/chunk-LPPM7EGS.js +1141 -0
- package/dist/chunk-LPPM7EGS.js.map +1 -0
- package/dist/chunk-RZAOYKCO.js +51 -0
- package/dist/chunk-RZAOYKCO.js.map +1 -0
- package/dist/chunk-VFUEE6DF.js +373 -0
- package/dist/chunk-VFUEE6DF.js.map +1 -0
- package/dist/chunk-XLWPTPRP.js +52 -0
- package/dist/chunk-XLWPTPRP.js.map +1 -0
- package/dist/chunk-Z5LKAYAS.js +248 -0
- package/dist/chunk-Z5LKAYAS.js.map +1 -0
- package/dist/index.d.ts +4 -137
- package/dist/index.js +8 -38
- package/dist/index.js.map +1 -1
- package/dist/loops.d.ts +153 -0
- package/dist/loops.js +19 -0
- package/dist/loops.js.map +1 -0
- package/dist/mcp/bin.d.ts +1 -0
- package/dist/mcp/bin.js +150 -0
- package/dist/mcp/bin.js.map +1 -0
- package/dist/mcp/index.d.ts +827 -0
- package/dist/mcp/index.js +74 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/profiles.d.ts +133 -0
- package/dist/profiles.js +14 -0
- package/dist/profiles.js.map +1 -0
- package/dist/runtime-run-B2j-hvBj.d.ts +137 -0
- package/dist/types-Bx-tArkc.d.ts +225 -0
- package/dist/{types-ByIhNRFk.d.ts → types-DvJIha6w.d.ts} +1 -1
- package/package.json +25 -1
package/dist/loops.d.ts
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
export { AgentProfile, CreateSandboxOptions, SandboxEvent, SandboxInstance } from '@tangle-network/sandbox';
|
|
2
|
+
import { D as DefaultVerdict, a as Driver, I as Iteration, A as AgentRunSpec, O as OutputAdapter, V as Validator, E as ExecCtx, L as LoopWinner, b as LoopResult } from './types-Bx-tArkc.js';
|
|
3
|
+
export { c as LoopDecisionPayload, d as LoopEndedPayload, e as LoopIterationEndedPayload, f as LoopIterationStartedPayload, g as LoopSandboxClient, h as LoopStartedPayload, i as LoopTraceEmitter, j as LoopTraceEvent, k as ValidationCtx } from './types-Bx-tArkc.js';
|
|
4
|
+
import './runtime-run-B2j-hvBj.js';
|
|
5
|
+
import './types-DvJIha6w.js';
|
|
6
|
+
import '@tangle-network/agent-eval';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @experimental
|
|
10
|
+
*
|
|
11
|
+
* FanoutVote driver — N parallel attempts in iteration 0, pick the highest-
|
|
12
|
+
* scoring valid output. No second iteration: the topology is "spawn N, score,
|
|
13
|
+
* pick winner". The kernel handles heterogeneous fanout via the
|
|
14
|
+
* `agentRuns: AgentRunSpec[]` form on `runLoop`.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
type FanoutVoteDecision = 'pick-winner' | 'fail';
|
|
18
|
+
/** @experimental */
|
|
19
|
+
interface FanoutVoteScored<Task, Output> {
|
|
20
|
+
task: Task;
|
|
21
|
+
output: Output;
|
|
22
|
+
verdict?: DefaultVerdict;
|
|
23
|
+
iterationIndex: number;
|
|
24
|
+
agentRunName: string;
|
|
25
|
+
}
|
|
26
|
+
/** @experimental */
|
|
27
|
+
interface CreateFanoutVoteDriverOptions<Task, Output> {
|
|
28
|
+
/** Number of parallel attempts. Must be >= 1. */
|
|
29
|
+
n: number;
|
|
30
|
+
/**
|
|
31
|
+
* Pick the winner from the scored set. Default: highest `verdict.score`
|
|
32
|
+
* among valid outputs (ties broken by smallest iteration index). When
|
|
33
|
+
* no valid outputs exist, returns `undefined` and `decide()` resolves
|
|
34
|
+
* to `'fail'`. The kernel still records winners structurally — this
|
|
35
|
+
* selector only feeds `decide()`'s pass/fail signal.
|
|
36
|
+
*/
|
|
37
|
+
selector?: (scored: FanoutVoteScored<Task, Output>[]) => FanoutVoteScored<Task, Output> | undefined;
|
|
38
|
+
/** Stable identifier surfaced in trace events. Default `'fanout-vote'`. */
|
|
39
|
+
name?: string;
|
|
40
|
+
}
|
|
41
|
+
/** @experimental */
|
|
42
|
+
declare function createFanoutVoteDriver<Task, Output>(options: CreateFanoutVoteDriverOptions<Task, Output>): Driver<Task, Output, FanoutVoteDecision>;
|
|
43
|
+
/**
|
|
44
|
+
* Test helper: surface the per-iteration scored view a custom `selector`
|
|
45
|
+
* would receive. Exposed so consumers writing a custom selector can test it
|
|
46
|
+
* standalone without driving the full kernel.
|
|
47
|
+
*
|
|
48
|
+
* @experimental
|
|
49
|
+
*/
|
|
50
|
+
declare function scoreFanoutVoteIterations<Task, Output>(iterations: ReadonlyArray<Iteration<Task, Output>>): FanoutVoteScored<Task, Output>[];
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @experimental
|
|
54
|
+
*
|
|
55
|
+
* Refine driver — single task per iteration, validator-gated.
|
|
56
|
+
*
|
|
57
|
+
* `plan` returns `[task]` (possibly transformed via `refineTask`) until the
|
|
58
|
+
* prior verdict is valid OR the local cap is hit, then `[]`.
|
|
59
|
+
* `decide` returns `'stop'` once the latest verdict is valid OR the cap is
|
|
60
|
+
* reached. The kernel's `maxIterations` is an orthogonal safety cap;
|
|
61
|
+
* whichever is lower wins.
|
|
62
|
+
*/
|
|
63
|
+
|
|
64
|
+
type RefineDecision = 'continue' | 'stop';
|
|
65
|
+
/** @experimental */
|
|
66
|
+
interface CreateRefineDriverOptions<Task> {
|
|
67
|
+
/** Hard cap on iterations. Default 5. */
|
|
68
|
+
maxIterations?: number;
|
|
69
|
+
/**
|
|
70
|
+
* Optional task transform applied each round based on the prior verdict.
|
|
71
|
+
* When omitted, the same task is replayed and the agent is expected to
|
|
72
|
+
* inspect the sandbox session state for prior attempts.
|
|
73
|
+
*/
|
|
74
|
+
refineTask?: (task: Task, prior: DefaultVerdict) => Task;
|
|
75
|
+
/** Stable identifier surfaced in trace events. Default `'refine'`. */
|
|
76
|
+
name?: string;
|
|
77
|
+
}
|
|
78
|
+
/** @experimental */
|
|
79
|
+
declare function createRefineDriver<Task, Output>(options?: CreateRefineDriverOptions<Task>): Driver<Task, Output, RefineDecision>;
|
|
80
|
+
/**
|
|
81
|
+
* Test helper: select the last-valid iteration (or the last attempt if
|
|
82
|
+
* none passed). Mirrors the kernel's default selector ordering for refine
|
|
83
|
+
* topologies — the most recent successful attempt wins.
|
|
84
|
+
*
|
|
85
|
+
* @experimental
|
|
86
|
+
*/
|
|
87
|
+
declare function refineWinnerIndex<Task, Output>(iterations: ReadonlyArray<Iteration<Task, Output>>): number | undefined;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @experimental
|
|
91
|
+
*
|
|
92
|
+
* `runLoop` — the topology-agnostic kernel built atop the sandbox SDK.
|
|
93
|
+
*
|
|
94
|
+
* Each iteration:
|
|
95
|
+
* 1. `driver.plan(task, history)` → N tasks (1 = refine, N = fanout, 0 = stop)
|
|
96
|
+
* 2. For each task (parallel, bounded by `maxConcurrency`):
|
|
97
|
+
* a. round-robin an `AgentRunSpec` from `agentRuns`
|
|
98
|
+
* b. `sandboxClient.create({ backend: { profile }, ...overrides })`
|
|
99
|
+
* c. iterate `box.streamPrompt(taskToPrompt(task))` and collect events
|
|
100
|
+
* 3. `output.parse(events)` → typed `Output`
|
|
101
|
+
* 4. `validator?.validate(output)` → `DefaultVerdict`
|
|
102
|
+
* 5. Append `Iteration` to history; emit `loop.iteration.ended`
|
|
103
|
+
* 6. `driver.decide(history)` → if terminal, return result + winner
|
|
104
|
+
*
|
|
105
|
+
* The kernel owns: iteration accounting, per-iteration timing, error
|
|
106
|
+
* capture, abort propagation, concurrency cap, cost aggregation, and trace
|
|
107
|
+
* emission. The kernel does NOT own: what the agent runs (sandbox SDK +
|
|
108
|
+
* profile), how outputs are decoded (output adapter), how outputs are
|
|
109
|
+
* scored (validator), or topology (driver).
|
|
110
|
+
*/
|
|
111
|
+
|
|
112
|
+
/** @experimental */
|
|
113
|
+
interface RunLoopOptions<Task, Output, Decision> {
|
|
114
|
+
driver: Driver<Task, Output, Decision>;
|
|
115
|
+
/**
|
|
116
|
+
* Single agent spec — every iteration uses this profile. Mutually
|
|
117
|
+
* exclusive with `agentRuns`.
|
|
118
|
+
*/
|
|
119
|
+
agentRun?: AgentRunSpec<Task>;
|
|
120
|
+
/**
|
|
121
|
+
* Multiple specs for heterogeneous fanout. The kernel round-robins
|
|
122
|
+
* through them when the driver plans N tasks. Mutually exclusive with
|
|
123
|
+
* `agentRun`.
|
|
124
|
+
*/
|
|
125
|
+
agentRuns?: AgentRunSpec<Task>[];
|
|
126
|
+
output: OutputAdapter<Output>;
|
|
127
|
+
validator?: Validator<Output>;
|
|
128
|
+
task: Task;
|
|
129
|
+
ctx: ExecCtx;
|
|
130
|
+
/** Default 10. Hard cap on total iterations across all `plan()` rounds. */
|
|
131
|
+
maxIterations?: number;
|
|
132
|
+
/** Default 4. In-flight worker cap within a single `plan()` batch. */
|
|
133
|
+
maxConcurrency?: number;
|
|
134
|
+
/**
|
|
135
|
+
* Pre-allocated id for trace correlation. Default = `loop-${random}`.
|
|
136
|
+
* Surfaces as `runId` on every emitted `LoopTraceEvent`.
|
|
137
|
+
*/
|
|
138
|
+
runId?: string;
|
|
139
|
+
/**
|
|
140
|
+
* Clock override; default `Date.now`. Deterministic tests pass a
|
|
141
|
+
* monotonic counter to stabilize iteration timing fields.
|
|
142
|
+
*/
|
|
143
|
+
now?: () => number;
|
|
144
|
+
/**
|
|
145
|
+
* Override the default winner selector (highest-valid-score, ties broken
|
|
146
|
+
* by earliest iteration).
|
|
147
|
+
*/
|
|
148
|
+
selectWinner?: (iterations: Iteration<Task, Output>[]) => LoopWinner<Task, Output> | undefined;
|
|
149
|
+
}
|
|
150
|
+
/** @experimental */
|
|
151
|
+
declare function runLoop<Task, Output, Decision>(options: RunLoopOptions<Task, Output, Decision>): Promise<LoopResult<Task, Output, Decision>>;
|
|
152
|
+
|
|
153
|
+
export { AgentRunSpec, type CreateFanoutVoteDriverOptions, type CreateRefineDriverOptions, DefaultVerdict, Driver, ExecCtx, type FanoutVoteDecision, type FanoutVoteScored, Iteration, LoopResult, LoopWinner, OutputAdapter, type RefineDecision, type RunLoopOptions, Validator, createFanoutVoteDriver, createRefineDriver, refineWinnerIndex, runLoop, scoreFanoutVoteIterations };
|
package/dist/loops.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createRefineDriver,
|
|
3
|
+
refineWinnerIndex,
|
|
4
|
+
runLoop
|
|
5
|
+
} from "./chunk-VFUEE6DF.js";
|
|
6
|
+
import {
|
|
7
|
+
createFanoutVoteDriver,
|
|
8
|
+
scoreFanoutVoteIterations
|
|
9
|
+
} from "./chunk-XLWPTPRP.js";
|
|
10
|
+
import "./chunk-RZAOYKCO.js";
|
|
11
|
+
import "./chunk-DGUM43GV.js";
|
|
12
|
+
export {
|
|
13
|
+
createFanoutVoteDriver,
|
|
14
|
+
createRefineDriver,
|
|
15
|
+
refineWinnerIndex,
|
|
16
|
+
runLoop,
|
|
17
|
+
scoreFanoutVoteIterations
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=loops.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
package/dist/mcp/bin.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
createDefaultCoderDelegate,
|
|
4
|
+
createMcpServer
|
|
5
|
+
} from "../chunk-LPPM7EGS.js";
|
|
6
|
+
import {
|
|
7
|
+
runLoop
|
|
8
|
+
} from "../chunk-VFUEE6DF.js";
|
|
9
|
+
import "../chunk-Z5LKAYAS.js";
|
|
10
|
+
import "../chunk-XLWPTPRP.js";
|
|
11
|
+
import "../chunk-RZAOYKCO.js";
|
|
12
|
+
import "../chunk-DGUM43GV.js";
|
|
13
|
+
|
|
14
|
+
// src/mcp/bin.ts
|
|
15
|
+
async function main() {
|
|
16
|
+
const fanoutHarnesses = parseHarnesses(process.env.MCP_CODER_FANOUT_HARNESSES);
|
|
17
|
+
const maxConcurrency = parseConcurrency(process.env.MCP_MAX_CONCURRENT_SANDBOXES);
|
|
18
|
+
const wantCoder = !process.env.MCP_DISABLE_CODER;
|
|
19
|
+
const wantResearcher = !process.env.MCP_DISABLE_RESEARCHER;
|
|
20
|
+
const needsSandbox = wantCoder || wantResearcher;
|
|
21
|
+
let sandboxClient;
|
|
22
|
+
if (needsSandbox) {
|
|
23
|
+
const apiKey = process.env.SANDBOX_API_KEY;
|
|
24
|
+
if (!apiKey && !process.env.AGENT_RUNTIME_MCP_ALLOW_NO_KEY) {
|
|
25
|
+
process.stderr.write(
|
|
26
|
+
"agent-runtime-mcp: SANDBOX_API_KEY is required (set AGENT_RUNTIME_MCP_ALLOW_NO_KEY=1 to run without it for diagnostics, or set MCP_DISABLE_CODER=1 MCP_DISABLE_RESEARCHER=1 to run the queue-only subset)\n"
|
|
27
|
+
);
|
|
28
|
+
process.exit(2);
|
|
29
|
+
}
|
|
30
|
+
sandboxClient = await loadSandboxClient(apiKey);
|
|
31
|
+
}
|
|
32
|
+
const coderDelegate = wantCoder && sandboxClient ? createDefaultCoderDelegate({
|
|
33
|
+
sandboxClient,
|
|
34
|
+
fanoutHarnesses,
|
|
35
|
+
maxConcurrency
|
|
36
|
+
}) : void 0;
|
|
37
|
+
const researcherDelegate = wantResearcher && sandboxClient ? await loadResearcherDelegate(sandboxClient, maxConcurrency) : void 0;
|
|
38
|
+
const server = createMcpServer({ coderDelegate, researcherDelegate });
|
|
39
|
+
process.on("SIGINT", () => {
|
|
40
|
+
server.stop();
|
|
41
|
+
process.exit(0);
|
|
42
|
+
});
|
|
43
|
+
process.on("SIGTERM", () => {
|
|
44
|
+
server.stop();
|
|
45
|
+
process.exit(0);
|
|
46
|
+
});
|
|
47
|
+
await server.serve();
|
|
48
|
+
}
|
|
49
|
+
async function loadSandboxClient(apiKey) {
|
|
50
|
+
const mod = await import("@tangle-network/sandbox").catch((err) => {
|
|
51
|
+
process.stderr.write(
|
|
52
|
+
`agent-runtime-mcp: failed to load @tangle-network/sandbox (${err.message}); install the peer dependency
|
|
53
|
+
`
|
|
54
|
+
);
|
|
55
|
+
process.exit(2);
|
|
56
|
+
});
|
|
57
|
+
const SandboxCtor = mod.Sandbox;
|
|
58
|
+
if (!SandboxCtor) {
|
|
59
|
+
process.stderr.write(
|
|
60
|
+
"agent-runtime-mcp: @tangle-network/sandbox does not export Sandbox; cannot construct client\n"
|
|
61
|
+
);
|
|
62
|
+
process.exit(2);
|
|
63
|
+
}
|
|
64
|
+
const baseUrl = process.env.SANDBOX_BASE_URL;
|
|
65
|
+
return new SandboxCtor({
|
|
66
|
+
apiKey,
|
|
67
|
+
...baseUrl ? { baseUrl } : {}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
async function loadResearcherDelegate(sandboxClient, maxConcurrency) {
|
|
71
|
+
const profilesSpecifier = "@tangle-network/agent-knowledge/profiles";
|
|
72
|
+
const mod = await import(profilesSpecifier).catch(() => void 0);
|
|
73
|
+
if (!mod) return void 0;
|
|
74
|
+
const fanoutFactory = mod.multiHarnessResearcherFanout;
|
|
75
|
+
const singleFactory = mod.researcherProfile;
|
|
76
|
+
if (!fanoutFactory || !singleFactory) return void 0;
|
|
77
|
+
return async (args, ctx) => {
|
|
78
|
+
const task = {
|
|
79
|
+
question: args.question,
|
|
80
|
+
knowledgeNamespace: args.namespace,
|
|
81
|
+
scope: args.scope,
|
|
82
|
+
sources: args.sources,
|
|
83
|
+
recencyWindow: args.config?.recencyWindow ? {
|
|
84
|
+
since: args.config.recencyWindow.since ? new Date(args.config.recencyWindow.since) : void 0,
|
|
85
|
+
until: args.config.recencyWindow.until ? new Date(args.config.recencyWindow.until) : void 0
|
|
86
|
+
} : void 0,
|
|
87
|
+
maxItems: args.config?.maxItems,
|
|
88
|
+
minConfidence: args.config?.minConfidence
|
|
89
|
+
};
|
|
90
|
+
const variants = Math.max(1, Math.trunc(args.variants ?? 1));
|
|
91
|
+
ctx.report({ iteration: 0, phase: "starting" });
|
|
92
|
+
if (variants <= 1) {
|
|
93
|
+
const preset = singleFactory({ task });
|
|
94
|
+
const result2 = await runLoop({
|
|
95
|
+
driver: {
|
|
96
|
+
name: "mcp-researcher-single",
|
|
97
|
+
async plan(t, history) {
|
|
98
|
+
return history.length === 0 ? [t] : [];
|
|
99
|
+
},
|
|
100
|
+
decide(history) {
|
|
101
|
+
return history.length > 0 ? "pick-winner" : "fail";
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
agentRun: preset.agentRunSpec,
|
|
105
|
+
output: preset.output,
|
|
106
|
+
validator: preset.validator,
|
|
107
|
+
task,
|
|
108
|
+
ctx: { sandboxClient, signal: ctx.signal },
|
|
109
|
+
maxIterations: 1,
|
|
110
|
+
maxConcurrency
|
|
111
|
+
});
|
|
112
|
+
const output2 = result2.winner?.output;
|
|
113
|
+
if (!output2) throw new Error("researcher delegate produced no winner");
|
|
114
|
+
ctx.report({ iteration: 1, phase: "completed" });
|
|
115
|
+
return output2;
|
|
116
|
+
}
|
|
117
|
+
const fanout = fanoutFactory({ task });
|
|
118
|
+
const result = await runLoop({
|
|
119
|
+
driver: fanout.driver,
|
|
120
|
+
agentRuns: fanout.agentRuns.slice(0, variants),
|
|
121
|
+
output: fanout.output,
|
|
122
|
+
validator: fanout.validator,
|
|
123
|
+
task,
|
|
124
|
+
ctx: { sandboxClient, signal: ctx.signal },
|
|
125
|
+
maxIterations: variants,
|
|
126
|
+
maxConcurrency: Math.min(maxConcurrency, variants)
|
|
127
|
+
});
|
|
128
|
+
const output = result.winner?.output;
|
|
129
|
+
if (!output) throw new Error("researcher delegate fanout produced no winner");
|
|
130
|
+
ctx.report({ iteration: result.iterations.length, phase: "completed" });
|
|
131
|
+
return output;
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
function parseHarnesses(raw) {
|
|
135
|
+
if (!raw) return void 0;
|
|
136
|
+
const list = raw.split(",").map((entry) => entry.trim()).filter(Boolean);
|
|
137
|
+
return list.length > 0 ? list : void 0;
|
|
138
|
+
}
|
|
139
|
+
function parseConcurrency(raw) {
|
|
140
|
+
if (!raw) return 4;
|
|
141
|
+
const n = Number(raw);
|
|
142
|
+
if (!Number.isFinite(n) || n < 1) return 4;
|
|
143
|
+
return Math.min(Math.trunc(n), 32);
|
|
144
|
+
}
|
|
145
|
+
main().catch((err) => {
|
|
146
|
+
process.stderr.write(`agent-runtime-mcp: ${err instanceof Error ? err.stack : String(err)}
|
|
147
|
+
`);
|
|
148
|
+
process.exit(1);
|
|
149
|
+
});
|
|
150
|
+
//# sourceMappingURL=bin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/mcp/bin.ts"],"sourcesContent":["#!/usr/bin/env node\n\n/**\n * @experimental\n *\n * `agent-runtime-mcp` — stdio MCP server entry point.\n *\n * Spins up a server with the default coder delegate (wired against the\n * real `@tangle-network/sandbox` client) and, when the optional\n * `@tangle-network/agent-knowledge` peer is installed, a researcher\n * delegate against `multiHarnessResearcherFanout`.\n *\n * Environment variables:\n * SANDBOX_API_KEY required — passed to `new Sandbox({ apiKey })`\n * SANDBOX_BASE_URL optional — sandbox-SDK base URL override\n * MCP_MAX_CONCURRENT_SANDBOXES default 4 — kernel maxConcurrency cap\n * MCP_CODER_FANOUT_HARNESSES comma-separated harness ids to use for variants > 1\n * MCP_DISABLE_CODER set to `1` to omit `delegate_code`\n * MCP_DISABLE_RESEARCHER set to `1` to omit `delegate_research` even when peer is present\n */\n\nimport type { LoopSandboxClient } from '../loops'\nimport { runLoop } from '../loops'\nimport { createDefaultCoderDelegate, type ResearcherDelegate } from './delegates'\nimport { createMcpServer } from './server'\nimport type { ResearchOutputShape } from './types'\n\nasync function main(): Promise<void> {\n const fanoutHarnesses = parseHarnesses(process.env.MCP_CODER_FANOUT_HARNESSES)\n const maxConcurrency = parseConcurrency(process.env.MCP_MAX_CONCURRENT_SANDBOXES)\n const wantCoder = !process.env.MCP_DISABLE_CODER\n const wantResearcher = !process.env.MCP_DISABLE_RESEARCHER\n\n // Skip the sandbox client load entirely when no profile delegate needs it —\n // the feedback + status + history tools are queue-bound and require no\n // sandbox. Useful for tooling that mounts the MCP server purely for\n // self-introspection.\n const needsSandbox = wantCoder || wantResearcher\n let sandboxClient: LoopSandboxClient | undefined\n if (needsSandbox) {\n const apiKey = process.env.SANDBOX_API_KEY\n if (!apiKey && !process.env.AGENT_RUNTIME_MCP_ALLOW_NO_KEY) {\n process.stderr.write(\n 'agent-runtime-mcp: SANDBOX_API_KEY is required (set AGENT_RUNTIME_MCP_ALLOW_NO_KEY=1 to run without it for diagnostics, or set MCP_DISABLE_CODER=1 MCP_DISABLE_RESEARCHER=1 to run the queue-only subset)\\n',\n )\n process.exit(2)\n }\n sandboxClient = await loadSandboxClient(apiKey)\n }\n\n const coderDelegate =\n wantCoder && sandboxClient\n ? createDefaultCoderDelegate({\n sandboxClient,\n fanoutHarnesses,\n maxConcurrency,\n })\n : undefined\n\n const researcherDelegate =\n wantResearcher && sandboxClient\n ? await loadResearcherDelegate(sandboxClient, maxConcurrency)\n : undefined\n\n const server = createMcpServer({ coderDelegate, researcherDelegate })\n\n process.on('SIGINT', () => {\n server.stop()\n process.exit(0)\n })\n process.on('SIGTERM', () => {\n server.stop()\n process.exit(0)\n })\n\n await server.serve()\n}\n\nasync function loadSandboxClient(apiKey: string | undefined): Promise<LoopSandboxClient> {\n // Dynamic import keeps the bin importable in environments that haven't\n // installed `@tangle-network/sandbox` yet (the runtime package lists it\n // as a peer dep, not a hard dep).\n const mod = await import('@tangle-network/sandbox').catch((err) => {\n process.stderr.write(\n `agent-runtime-mcp: failed to load @tangle-network/sandbox (${err.message}); install the peer dependency\\n`,\n )\n process.exit(2)\n })\n const SandboxCtor = (mod as { Sandbox?: new (config: unknown) => LoopSandboxClient }).Sandbox\n if (!SandboxCtor) {\n process.stderr.write(\n 'agent-runtime-mcp: @tangle-network/sandbox does not export Sandbox; cannot construct client\\n',\n )\n process.exit(2)\n }\n const baseUrl = process.env.SANDBOX_BASE_URL\n return new SandboxCtor({\n apiKey,\n ...(baseUrl ? { baseUrl } : {}),\n })\n}\n\ninterface ResearcherProfilePreset {\n agentRunSpec: Parameters<typeof runLoop>[0]['agentRun'] extends infer T ? NonNullable<T> : never\n output: Parameters<typeof runLoop>[0]['output']\n validator: Parameters<typeof runLoop>[0]['validator']\n}\n\ninterface ResearcherFanoutPreset {\n agentRuns: NonNullable<Parameters<typeof runLoop>[0]['agentRuns']>\n output: Parameters<typeof runLoop>[0]['output']\n validator: Parameters<typeof runLoop>[0]['validator']\n driver: Parameters<typeof runLoop>[0]['driver']\n}\n\nasync function loadResearcherDelegate(\n sandboxClient: LoopSandboxClient,\n maxConcurrency: number,\n): Promise<ResearcherDelegate | undefined> {\n // Optional peer — when `@tangle-network/agent-knowledge` isn't installed,\n // we silently omit the researcher tool from the advertisement. The\n // dynamic-import path is resolved at runtime; TypeScript cannot see the\n // peer, so we type the module structurally rather than via its own\n // declaration file.\n const profilesSpecifier = '@tangle-network/agent-knowledge/profiles'\n const mod = await import(profilesSpecifier).catch(() => undefined)\n if (!mod) return undefined\n type SingleFactory = (opts: { task: unknown }) => ResearcherProfilePreset\n type FanoutFactory = (opts: { task: unknown }) => ResearcherFanoutPreset\n const fanoutFactory = (mod as { multiHarnessResearcherFanout?: FanoutFactory })\n .multiHarnessResearcherFanout\n const singleFactory = (mod as { researcherProfile?: SingleFactory }).researcherProfile\n if (!fanoutFactory || !singleFactory) return undefined\n\n return async (args, ctx) => {\n const task = {\n question: args.question,\n knowledgeNamespace: args.namespace,\n scope: args.scope,\n sources: args.sources,\n recencyWindow: args.config?.recencyWindow\n ? {\n since: args.config.recencyWindow.since\n ? new Date(args.config.recencyWindow.since)\n : undefined,\n until: args.config.recencyWindow.until\n ? new Date(args.config.recencyWindow.until)\n : undefined,\n }\n : undefined,\n maxItems: args.config?.maxItems,\n minConfidence: args.config?.minConfidence,\n }\n const variants = Math.max(1, Math.trunc(args.variants ?? 1))\n ctx.report({ iteration: 0, phase: 'starting' })\n if (variants <= 1) {\n const preset = singleFactory({ task })\n const result = await runLoop({\n driver: {\n name: 'mcp-researcher-single',\n async plan(t, history) {\n return history.length === 0 ? [t] : []\n },\n decide(history) {\n return history.length > 0 ? 'pick-winner' : 'fail'\n },\n },\n agentRun: preset.agentRunSpec,\n output: preset.output,\n validator: preset.validator,\n task,\n ctx: { sandboxClient, signal: ctx.signal },\n maxIterations: 1,\n maxConcurrency,\n })\n const output = result.winner?.output\n if (!output) throw new Error('researcher delegate produced no winner')\n ctx.report({ iteration: 1, phase: 'completed' })\n return output as ResearchOutputShape\n }\n const fanout = fanoutFactory({ task })\n const result = await runLoop({\n driver: fanout.driver,\n agentRuns: fanout.agentRuns.slice(0, variants),\n output: fanout.output,\n validator: fanout.validator,\n task,\n ctx: { sandboxClient, signal: ctx.signal },\n maxIterations: variants,\n maxConcurrency: Math.min(maxConcurrency, variants),\n })\n const output = result.winner?.output\n if (!output) throw new Error('researcher delegate fanout produced no winner')\n ctx.report({ iteration: result.iterations.length, phase: 'completed' })\n return output as ResearchOutputShape\n }\n}\n\nfunction parseHarnesses(raw: string | undefined): string[] | undefined {\n if (!raw) return undefined\n const list = raw\n .split(',')\n .map((entry) => entry.trim())\n .filter(Boolean)\n return list.length > 0 ? list : undefined\n}\n\nfunction parseConcurrency(raw: string | undefined): number {\n if (!raw) return 4\n const n = Number(raw)\n if (!Number.isFinite(n) || n < 1) return 4\n return Math.min(Math.trunc(n), 32)\n}\n\nmain().catch((err) => {\n process.stderr.write(`agent-runtime-mcp: ${err instanceof Error ? err.stack : String(err)}\\n`)\n process.exit(1)\n})\n"],"mappings":";;;;;;;;;;;;;;AA2BA,eAAe,OAAsB;AACnC,QAAM,kBAAkB,eAAe,QAAQ,IAAI,0BAA0B;AAC7E,QAAM,iBAAiB,iBAAiB,QAAQ,IAAI,4BAA4B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