@tangle-network/agent-runtime 0.22.0 → 0.23.1

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 CHANGED
@@ -213,6 +213,33 @@ Or run the ready-made bin:
213
213
  TANGLE_API_KEY=sk_sandbox_... agent-runtime-mcp
214
214
  ```
215
215
 
216
+ ### Surfacing the tools through `createOpenAICompatibleBackend`
217
+
218
+ Sandbox callers discover MCP tools through the runtime mount. Callers that
219
+ route through the OpenAI-compat backend (tcloud, OpenRouter, cli-bridge,
220
+ OpenAI direct) must hand the model an explicit `tools[]` array — the
221
+ backend does not auto-discover. `mcpToolsForRuntimeMcp()` returns the
222
+ canonical projection so the model can call any of the 5 delegation tools
223
+ through the OpenAI-compat path:
224
+
225
+ ```ts
226
+ import {
227
+ createOpenAICompatibleBackend,
228
+ mcpToolsForRuntimeMcp,
229
+ } from '@tangle-network/agent-runtime'
230
+
231
+ const backend = createOpenAICompatibleBackend({
232
+ apiKey,
233
+ baseUrl,
234
+ model,
235
+ tools: mcpToolsForRuntimeMcp(),
236
+ })
237
+ ```
238
+
239
+ Use `mcpToolsForRuntimeMcpSubset(['delegate_research', 'delegation_status'])`
240
+ when you want a curated subset (e.g. read-only research without the coder
241
+ queue).
242
+
216
243
  The bin auto-wires the coder delegate and, when
217
244
  `@tangle-network/agent-knowledge` is installed as a peer, the researcher
218
245
  delegate. Environment knobs:
@@ -0,0 +1,427 @@
1
+ import {
2
+ DELEGATE_CODE_DESCRIPTION,
3
+ DELEGATE_CODE_INPUT_SCHEMA,
4
+ DELEGATE_CODE_TOOL_NAME,
5
+ DELEGATE_FEEDBACK_DESCRIPTION,
6
+ DELEGATE_FEEDBACK_INPUT_SCHEMA,
7
+ DELEGATE_FEEDBACK_TOOL_NAME,
8
+ DELEGATE_RESEARCH_DESCRIPTION,
9
+ DELEGATE_RESEARCH_INPUT_SCHEMA,
10
+ DELEGATE_RESEARCH_TOOL_NAME,
11
+ DELEGATION_HISTORY_DESCRIPTION,
12
+ DELEGATION_HISTORY_INPUT_SCHEMA,
13
+ DELEGATION_HISTORY_TOOL_NAME,
14
+ DELEGATION_STATUS_DESCRIPTION,
15
+ DELEGATION_STATUS_INPUT_SCHEMA,
16
+ DELEGATION_STATUS_TOOL_NAME,
17
+ DelegationTaskQueue,
18
+ InMemoryFeedbackStore,
19
+ createDelegateCodeHandler,
20
+ createDelegateFeedbackHandler,
21
+ createDelegateResearchHandler,
22
+ createDelegationHistoryHandler,
23
+ createDelegationStatusHandler
24
+ } from "./chunk-UNQM6XQO.js";
25
+ import {
26
+ runLoop
27
+ } from "./chunk-TZ53F7M7.js";
28
+ import {
29
+ coderProfile,
30
+ multiHarnessCoderFanout
31
+ } from "./chunk-CBQVID7G.js";
32
+
33
+ // src/mcp/executor.ts
34
+ function createSiblingSandboxExecutor(options) {
35
+ const underlying = options.client;
36
+ const client = {
37
+ create(opts) {
38
+ return underlying.create(opts);
39
+ },
40
+ describePlacement(box) {
41
+ return { kind: "sibling", sandboxId: readId(box) };
42
+ }
43
+ };
44
+ return {
45
+ client,
46
+ describe() {
47
+ return "sibling-sandbox (each delegation = fresh sandbox via client.create)";
48
+ }
49
+ };
50
+ }
51
+ function createFleetWorkspaceExecutor(options) {
52
+ const fleet = options.fleet;
53
+ const exclude = new Set(options.excludeMachineIds ?? []);
54
+ let callIndex = 0;
55
+ const placementBySandboxId = /* @__PURE__ */ new Map();
56
+ const client = {
57
+ async create() {
58
+ const ids = fleet.ids.filter((id) => !exclude.has(id));
59
+ if (ids.length === 0) {
60
+ throw new Error(
61
+ `agent-runtime: fleet ${fleet.fleetId} has no eligible worker machines (ids=[${fleet.ids.join(",")}], excluded=[${[...exclude].join(",")}])`
62
+ );
63
+ }
64
+ const selector = options.selectMachine;
65
+ const machineId = selector ? selector({ callIndex, ids }) : ids[callIndex % ids.length];
66
+ callIndex += 1;
67
+ if (typeof machineId !== "string" || machineId.length === 0) {
68
+ throw new Error("agent-runtime: fleet executor selectMachine returned an empty machine id");
69
+ }
70
+ const box = await fleet.sandbox(machineId);
71
+ const sandboxId = readId(box);
72
+ if (sandboxId) placementBySandboxId.set(sandboxId, { machineId });
73
+ return box;
74
+ },
75
+ describePlacement(box) {
76
+ const sandboxId = readId(box);
77
+ const recorded = sandboxId ? placementBySandboxId.get(sandboxId) : void 0;
78
+ return {
79
+ kind: "fleet",
80
+ sandboxId,
81
+ fleetId: fleet.fleetId,
82
+ machineId: recorded?.machineId
83
+ };
84
+ }
85
+ };
86
+ return {
87
+ client,
88
+ describe() {
89
+ const excluded = exclude.size > 0 ? ` (excluded=[${[...exclude].join(",")}])` : "";
90
+ return `fleet-workspace (fleetId=${fleet.fleetId}, machines=[${fleet.ids.join(",")}]${excluded})`;
91
+ }
92
+ };
93
+ }
94
+ function readId(box) {
95
+ const raw = box.id;
96
+ return typeof raw === "string" && raw.length > 0 ? raw : void 0;
97
+ }
98
+
99
+ // src/mcp/bin-helpers.ts
100
+ async function detectExecutor(args) {
101
+ const env = args.env ?? process.env;
102
+ const fleetId = parseFleetId(env.TANGLE_FLEET_ID);
103
+ if (!fleetId) {
104
+ return createSiblingSandboxExecutor({ client: args.sandboxClient });
105
+ }
106
+ const resolveFleet = args.resolveFleet ?? defaultResolveFleet;
107
+ const fleet = await resolveFleet(args.sandboxClient, fleetId);
108
+ const excludeMachineIds = parseList(env.TANGLE_FLEET_EXCLUDE_MACHINES);
109
+ return createFleetWorkspaceExecutor({
110
+ fleet,
111
+ excludeMachineIds
112
+ });
113
+ }
114
+ async function defaultResolveFleet(sandboxClient, fleetId) {
115
+ const fleets = sandboxClient.fleets;
116
+ if (!fleets || typeof fleets.get !== "function") {
117
+ throw new Error(
118
+ "agent-runtime-mcp: the configured sandbox client does not expose `.fleets.get`; upgrade @tangle-network/sandbox to >= 0.2.1 or unset TANGLE_FLEET_ID."
119
+ );
120
+ }
121
+ const raw = await fleets.get(fleetId);
122
+ if (!raw || typeof raw !== "object") {
123
+ throw new Error(`agent-runtime-mcp: fleets.get(${fleetId}) returned no handle`);
124
+ }
125
+ const handle = raw;
126
+ if (typeof handle.fleetId !== "string" || !Array.isArray(handle.ids)) {
127
+ throw new Error(
128
+ `agent-runtime-mcp: fleet handle for ${fleetId} is missing fleetId/ids \u2014 incompatible sandbox SDK shape`
129
+ );
130
+ }
131
+ if (typeof handle.sandbox !== "function") {
132
+ throw new Error(
133
+ `agent-runtime-mcp: fleet handle for ${fleetId} is missing sandbox(machineId) \u2014 incompatible sandbox SDK shape`
134
+ );
135
+ }
136
+ return handle;
137
+ }
138
+ function parseFleetId(raw) {
139
+ if (typeof raw !== "string") return void 0;
140
+ const trimmed = raw.trim();
141
+ return trimmed.length > 0 ? trimmed : void 0;
142
+ }
143
+ function parseList(raw) {
144
+ if (!raw) return void 0;
145
+ const list = raw.split(",").map((entry) => entry.trim()).filter(Boolean);
146
+ return list.length > 0 ? list : void 0;
147
+ }
148
+
149
+ // src/mcp/delegates.ts
150
+ function createDefaultCoderDelegate(options) {
151
+ const executor = resolveExecutor(options);
152
+ const sandboxClient = executor.client;
153
+ const fanoutHarnesses = options.fanoutHarnesses;
154
+ const maxConcurrency = options.maxConcurrency ?? 4;
155
+ return async (args, ctx) => {
156
+ const task = {
157
+ goal: buildCoderGoal(args),
158
+ repoRoot: args.repoRoot,
159
+ testCmd: args.config?.testCmd,
160
+ typecheckCmd: args.config?.typecheckCmd,
161
+ forbiddenPaths: args.config?.forbiddenPaths,
162
+ maxDiffLines: args.config?.maxDiffLines
163
+ };
164
+ const variants = Math.max(1, Math.trunc(args.variants ?? 1));
165
+ ctx.report({ iteration: 0, phase: "starting" });
166
+ if (variants <= 1) {
167
+ const { agentRunSpec, output, validator } = coderProfile({ task });
168
+ const result2 = await runLoop({
169
+ driver: singleShotDriver,
170
+ agentRun: agentRunSpec,
171
+ output,
172
+ validator,
173
+ task,
174
+ ctx: { sandboxClient, signal: ctx.signal },
175
+ maxIterations: 1,
176
+ maxConcurrency
177
+ });
178
+ const winner2 = result2.winner;
179
+ if (!winner2) {
180
+ throw new Error("coder delegate produced no winner");
181
+ }
182
+ ctx.report({ iteration: 1, phase: "completed" });
183
+ return winner2.output;
184
+ }
185
+ const fanout = multiHarnessCoderFanout(
186
+ fanoutHarnesses && fanoutHarnesses.length > 0 ? { harnesses: fanoutHarnesses.slice(0, variants) } : { harnesses: void 0 }
187
+ );
188
+ const agentRuns = fanout.agentRuns.slice(0, variants);
189
+ const result = await runLoop({
190
+ driver: fanout.driver,
191
+ agentRuns,
192
+ output: fanout.output,
193
+ validator: fanout.validator,
194
+ task,
195
+ ctx: { sandboxClient, signal: ctx.signal },
196
+ maxIterations: variants,
197
+ maxConcurrency: Math.min(maxConcurrency, variants)
198
+ });
199
+ const winner = result.winner;
200
+ if (!winner) {
201
+ throw new Error("coder delegate fanout produced no winner");
202
+ }
203
+ ctx.report({ iteration: agentRuns.length, phase: "completed" });
204
+ return winner.output;
205
+ };
206
+ }
207
+ function buildCoderGoal(args) {
208
+ if (!args.contextHint) return args.goal;
209
+ return [args.goal, "", "## Context", args.contextHint].join("\n");
210
+ }
211
+ function resolveExecutor(options) {
212
+ if (options.executor && options.sandboxClient) {
213
+ throw new Error("createDefaultCoderDelegate: pass exactly one of `executor` or `sandboxClient`");
214
+ }
215
+ if (options.executor) return options.executor;
216
+ if (options.sandboxClient) {
217
+ return createSiblingSandboxExecutor({ client: options.sandboxClient });
218
+ }
219
+ throw new Error("createDefaultCoderDelegate: `executor` or `sandboxClient` is required");
220
+ }
221
+ var singleShotDriver = {
222
+ name: "mcp-single-shot",
223
+ async plan(task, history) {
224
+ return history.length === 0 ? [task] : [];
225
+ },
226
+ decide(history) {
227
+ return history.length > 0 ? "pick-winner" : "fail";
228
+ }
229
+ };
230
+
231
+ // src/mcp/server.ts
232
+ import { createInterface } from "readline";
233
+ import { Readable, Writable } from "stream";
234
+ var PROTOCOL_VERSION = "2024-11-05";
235
+ var DEFAULT_SERVER_NAME = "agent-runtime-mcp";
236
+ var DEFAULT_SERVER_VERSION = "0.22.0";
237
+ function createMcpServer(options = {}) {
238
+ const queue = options.queue ?? new DelegationTaskQueue();
239
+ const feedbackStore = options.feedbackStore ?? new InMemoryFeedbackStore();
240
+ const serverName = options.serverName ?? DEFAULT_SERVER_NAME;
241
+ const serverVersion = options.serverVersion ?? DEFAULT_SERVER_VERSION;
242
+ const tools = /* @__PURE__ */ new Map();
243
+ if (options.coderDelegate) {
244
+ tools.set(DELEGATE_CODE_TOOL_NAME, {
245
+ name: DELEGATE_CODE_TOOL_NAME,
246
+ description: DELEGATE_CODE_DESCRIPTION,
247
+ inputSchema: DELEGATE_CODE_INPUT_SCHEMA,
248
+ handler: createDelegateCodeHandler({ queue, delegate: options.coderDelegate })
249
+ });
250
+ }
251
+ if (options.researcherDelegate) {
252
+ tools.set(DELEGATE_RESEARCH_TOOL_NAME, {
253
+ name: DELEGATE_RESEARCH_TOOL_NAME,
254
+ description: DELEGATE_RESEARCH_DESCRIPTION,
255
+ inputSchema: DELEGATE_RESEARCH_INPUT_SCHEMA,
256
+ handler: createDelegateResearchHandler({ queue, delegate: options.researcherDelegate })
257
+ });
258
+ }
259
+ tools.set(DELEGATE_FEEDBACK_TOOL_NAME, {
260
+ name: DELEGATE_FEEDBACK_TOOL_NAME,
261
+ description: DELEGATE_FEEDBACK_DESCRIPTION,
262
+ inputSchema: DELEGATE_FEEDBACK_INPUT_SCHEMA,
263
+ handler: createDelegateFeedbackHandler({ queue, store: feedbackStore })
264
+ });
265
+ tools.set(DELEGATION_STATUS_TOOL_NAME, {
266
+ name: DELEGATION_STATUS_TOOL_NAME,
267
+ description: DELEGATION_STATUS_DESCRIPTION,
268
+ inputSchema: DELEGATION_STATUS_INPUT_SCHEMA,
269
+ handler: createDelegationStatusHandler({ queue })
270
+ });
271
+ tools.set(DELEGATION_HISTORY_TOOL_NAME, {
272
+ name: DELEGATION_HISTORY_TOOL_NAME,
273
+ description: DELEGATION_HISTORY_DESCRIPTION,
274
+ inputSchema: DELEGATION_HISTORY_INPUT_SCHEMA,
275
+ handler: createDelegationHistoryHandler({ queue })
276
+ });
277
+ let stopped = false;
278
+ let activeReadline;
279
+ async function handle(message) {
280
+ if (stopped) {
281
+ return rpcError(message.id ?? null, -32099, "server stopped");
282
+ }
283
+ if (message.method === "initialize") {
284
+ return rpcResult(message.id ?? null, {
285
+ protocolVersion: PROTOCOL_VERSION,
286
+ capabilities: { tools: {} },
287
+ serverInfo: { name: serverName, version: serverVersion }
288
+ });
289
+ }
290
+ if (message.method === "notifications/initialized") {
291
+ return null;
292
+ }
293
+ if (message.method === "tools/list") {
294
+ return rpcResult(message.id ?? null, {
295
+ tools: [...tools.values()].map((tool) => ({
296
+ name: tool.name,
297
+ description: tool.description,
298
+ inputSchema: tool.inputSchema
299
+ }))
300
+ });
301
+ }
302
+ if (message.method === "tools/call") {
303
+ const params = message.params ?? {};
304
+ const name = typeof params.name === "string" ? params.name : "";
305
+ const tool = tools.get(name);
306
+ if (!tool) {
307
+ return rpcError(message.id ?? null, -32601, `unknown tool: ${name}`);
308
+ }
309
+ try {
310
+ const output = await tool.handler(params.arguments ?? {});
311
+ return rpcResult(message.id ?? null, {
312
+ content: [{ type: "text", text: JSON.stringify(output) }],
313
+ structuredContent: output,
314
+ isError: false
315
+ });
316
+ } catch (err) {
317
+ const reason = err instanceof Error ? err.message : String(err);
318
+ const code = err instanceof TypeError || err instanceof RangeError ? -32602 : -32e3;
319
+ return rpcError(message.id ?? null, code, reason);
320
+ }
321
+ }
322
+ if (message.id === void 0 || message.id === null) return null;
323
+ return rpcError(message.id, -32601, `unknown method: ${message.method}`);
324
+ }
325
+ async function serve(transport) {
326
+ const input = transport?.input ?? process.stdin;
327
+ const output = transport?.output ?? process.stdout;
328
+ const rl = createInterface({ input, crlfDelay: Number.POSITIVE_INFINITY });
329
+ activeReadline = rl;
330
+ return new Promise((resolve, reject) => {
331
+ rl.on("line", (line) => {
332
+ const trimmed = line.trim();
333
+ if (!trimmed) return;
334
+ let parsed;
335
+ try {
336
+ parsed = JSON.parse(trimmed);
337
+ } catch (err) {
338
+ writeResponse(output, rpcError(null, -32700, `parse error: ${err.message}`));
339
+ return;
340
+ }
341
+ if (!parsed || parsed.jsonrpc !== "2.0" || typeof parsed.method !== "string") {
342
+ writeResponse(output, rpcError(parsed?.id ?? null, -32600, "invalid request"));
343
+ return;
344
+ }
345
+ void handle(parsed).then((response) => {
346
+ if (response) writeResponse(output, response);
347
+ });
348
+ });
349
+ rl.on("close", () => resolve());
350
+ rl.on("error", (err) => reject(err));
351
+ if (stopped) {
352
+ rl.close();
353
+ resolve();
354
+ }
355
+ });
356
+ }
357
+ function stop() {
358
+ stopped = true;
359
+ activeReadline?.close();
360
+ activeReadline = void 0;
361
+ }
362
+ return {
363
+ tools,
364
+ queue,
365
+ feedbackStore,
366
+ handle,
367
+ serve,
368
+ stop
369
+ };
370
+ }
371
+ function rpcResult(id, result) {
372
+ return { jsonrpc: "2.0", id, result };
373
+ }
374
+ function rpcError(id, code, message, data) {
375
+ return {
376
+ jsonrpc: "2.0",
377
+ id,
378
+ error: data === void 0 ? { code, message } : { code, message, data }
379
+ };
380
+ }
381
+ function writeResponse(output, response) {
382
+ output.write(`${JSON.stringify(response)}
383
+ `);
384
+ }
385
+ function createInProcessTransport() {
386
+ const responses = [];
387
+ const input = new Readable({ read() {
388
+ } });
389
+ const output = new Writable({
390
+ write(chunk, _enc, cb) {
391
+ const text = chunk.toString("utf8");
392
+ for (const line of text.split("\n")) {
393
+ const trimmed = line.trim();
394
+ if (!trimmed) continue;
395
+ try {
396
+ responses.push(JSON.parse(trimmed));
397
+ } catch {
398
+ }
399
+ }
400
+ cb();
401
+ }
402
+ });
403
+ return {
404
+ transport: { input, output },
405
+ clientWrite(line) {
406
+ input.push(`${line}
407
+ `);
408
+ },
409
+ clientClose() {
410
+ input.push(null);
411
+ },
412
+ async readServer() {
413
+ for (let i = 0; i < 5; i += 1) await new Promise((r) => setImmediate(r));
414
+ return [...responses];
415
+ }
416
+ };
417
+ }
418
+
419
+ export {
420
+ createSiblingSandboxExecutor,
421
+ createFleetWorkspaceExecutor,
422
+ detectExecutor,
423
+ createDefaultCoderDelegate,
424
+ createMcpServer,
425
+ createInProcessTransport
426
+ };
427
+ //# sourceMappingURL=chunk-IQHYOJU3.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/mcp/executor.ts","../src/mcp/bin-helpers.ts","../src/mcp/delegates.ts","../src/mcp/server.ts"],"sourcesContent":["/**\n * @experimental\n *\n * Delegation executors — the layer between MCP delegates and the sandbox\n * substrate. Each executor exposes a {@link LoopSandboxClient} the kernel\n * consumes plus a placement tag so the trace pipeline can correlate workers\n * with their physical placement.\n *\n * Two implementations ship in-box:\n *\n * - {@link createSiblingSandboxExecutor} — every delegation spawns a fresh\n * sandbox sibling to the caller. Default when the MCP server runs as a\n * standalone CLI mounted outside a fleet.\n *\n * - {@link createFleetWorkspaceExecutor} — delegations dispatch onto machines\n * in the caller's existing fleet so worker diffs land directly on the\n * caller's filesystem (the fleet's shared workspace). Selected when the\n * parent sandbox passes `TANGLE_FLEET_ID` into the MCP server's env.\n */\n\nimport type { CreateSandboxOptions, SandboxInstance } from '@tangle-network/sandbox'\nimport type { LoopSandboxClient, LoopSandboxPlacement } from '../loops'\n\n/** @experimental */\nexport interface DelegationExecutor {\n /** Sandbox client the kernel calls. Returned with `describePlacement` set. */\n readonly client: LoopSandboxClient\n /** Best-effort one-liner used in stderr boot logs and diagnostics. */\n describe(): string\n}\n\n/** @experimental */\nexport interface SiblingSandboxExecutorOptions {\n client: LoopSandboxClient\n}\n\n/**\n * Wrap a raw sandbox SDK client so the kernel emits\n * `loop.iteration.dispatch` events with `{ placement: 'sibling', sandboxId }`.\n *\n * The returned client `.create()` delegates to the underlying client; the\n * only added behavior is a `describePlacement` tag the kernel reads.\n *\n * @experimental\n */\nexport function createSiblingSandboxExecutor(\n options: SiblingSandboxExecutorOptions,\n): DelegationExecutor {\n const underlying = options.client\n const client: LoopSandboxClient = {\n create(opts?: CreateSandboxOptions): Promise<SandboxInstance> {\n return underlying.create(opts)\n },\n describePlacement(box: SandboxInstance): LoopSandboxPlacement {\n return { kind: 'sibling', sandboxId: readId(box) }\n },\n }\n return {\n client,\n describe(): string {\n return 'sibling-sandbox (each delegation = fresh sandbox via client.create)'\n },\n }\n}\n\n/**\n * Minimal `SandboxFleet` surface the fleet executor calls. Declared\n * structurally so tests can pass an in-memory stub without instantiating the\n * sandbox SDK.\n *\n * @experimental\n */\nexport interface FleetHandle {\n readonly fleetId: string\n /** Machine ids in dispatch-eligible order. The executor round-robins. */\n readonly ids: ReadonlyArray<string>\n /** Resolve a machine id to its `SandboxInstance` — that machine is mounted\n * on the fleet's shared workspace, so any diff the worker writes lands on\n * every other fleet machine's filesystem too. */\n sandbox(machineId: string): Promise<SandboxInstance>\n}\n\n/** @experimental */\nexport interface FleetWorkspaceExecutorOptions {\n fleet: FleetHandle\n /**\n * Override the machine-selection policy. Default = round-robin across\n * `fleet.ids`, skipping the optional `excludeMachineIds` set (typically the\n * coordinator machine the MCP server is running on).\n */\n selectMachine?: (call: { callIndex: number; ids: ReadonlyArray<string> }) => string\n /**\n * Machine ids to skip during default round-robin. Set to the caller's own\n * machineId so workers don't compete with the orchestrator on the same VM.\n */\n excludeMachineIds?: ReadonlyArray<string>\n}\n\n/**\n * Build an executor that resolves each delegated iteration to an existing\n * machine in `fleet`. The fleet's shared-workspace policy means the worker\n * machine sees the caller's filesystem — diffs land in-place with no\n * cross-sandbox copy step.\n *\n * @experimental\n */\nexport function createFleetWorkspaceExecutor(\n options: FleetWorkspaceExecutorOptions,\n): DelegationExecutor {\n const fleet = options.fleet\n const exclude = new Set(options.excludeMachineIds ?? [])\n let callIndex = 0\n // machineId-by-sandboxId, populated as we resolve machines so\n // `describePlacement` can recover the assignment from the SandboxInstance\n // the kernel hands back.\n const placementBySandboxId = new Map<string, { machineId: string }>()\n\n const client: LoopSandboxClient = {\n async create(): Promise<SandboxInstance> {\n const ids = fleet.ids.filter((id) => !exclude.has(id))\n if (ids.length === 0) {\n throw new Error(\n `agent-runtime: fleet ${fleet.fleetId} has no eligible worker machines (ids=[${fleet.ids.join(',')}], excluded=[${[...exclude].join(',')}])`,\n )\n }\n const selector = options.selectMachine\n const machineId = selector ? selector({ callIndex, ids }) : ids[callIndex % ids.length]\n callIndex += 1\n if (typeof machineId !== 'string' || machineId.length === 0) {\n throw new Error('agent-runtime: fleet executor selectMachine returned an empty machine id')\n }\n const box = await fleet.sandbox(machineId)\n const sandboxId = readId(box)\n if (sandboxId) placementBySandboxId.set(sandboxId, { machineId })\n return box\n },\n describePlacement(box: SandboxInstance): LoopSandboxPlacement {\n const sandboxId = readId(box)\n const recorded = sandboxId ? placementBySandboxId.get(sandboxId) : undefined\n return {\n kind: 'fleet',\n sandboxId,\n fleetId: fleet.fleetId,\n machineId: recorded?.machineId,\n }\n },\n }\n\n return {\n client,\n describe(): string {\n const excluded = exclude.size > 0 ? ` (excluded=[${[...exclude].join(',')}])` : ''\n return `fleet-workspace (fleetId=${fleet.fleetId}, machines=[${fleet.ids.join(',')}]${excluded})`\n },\n }\n}\n\nfunction readId(box: SandboxInstance): string | undefined {\n const raw = (box as unknown as { id?: unknown }).id\n return typeof raw === 'string' && raw.length > 0 ? raw : undefined\n}\n","/**\n * @experimental\n *\n * Helpers extracted from `bin.ts` so the env-detection + executor-selection\n * logic is unit-testable without spawning a subprocess. The bin imports from\n * here; tests import from here directly.\n */\n\nimport type { LoopSandboxClient } from '../loops'\nimport {\n createFleetWorkspaceExecutor,\n createSiblingSandboxExecutor,\n type DelegationExecutor,\n type FleetHandle,\n} from './executor'\n\n/** @experimental */\nexport interface DetectExecutorArgs {\n sandboxClient: LoopSandboxClient\n /** Raw env (defaults to `process.env`). Pass an explicit map for tests. */\n env?: Record<string, string | undefined>\n /**\n * Override how a fleet handle is resolved from the client + fleet id. The\n * default reads `client.fleets.get(fleetId)` and validates the returned\n * shape against the structural `FleetHandle` contract.\n */\n resolveFleet?: (client: LoopSandboxClient, fleetId: string) => Promise<FleetHandle>\n}\n\n/**\n * Pick the right executor for an MCP server invocation based on env vars.\n *\n * - `TANGLE_FLEET_ID` set → fleet-workspace placement; resolves the handle\n * via `sandboxClient.fleets.get(...)`.\n * - Otherwise → sibling-sandbox placement; each delegation creates a fresh\n * sandbox via `sandboxClient.create(...)`.\n *\n * Fails loud (throws) when fleet mode is requested but the SDK shape is\n * incompatible — the operator chose fleet semantics, silently degrading to\n * sibling mode would lie about workspace topology.\n *\n * @experimental\n */\nexport async function detectExecutor(args: DetectExecutorArgs): Promise<DelegationExecutor> {\n const env = args.env ?? process.env\n const fleetId = parseFleetId(env.TANGLE_FLEET_ID)\n if (!fleetId) {\n return createSiblingSandboxExecutor({ client: args.sandboxClient })\n }\n const resolveFleet = args.resolveFleet ?? defaultResolveFleet\n const fleet = await resolveFleet(args.sandboxClient, fleetId)\n const excludeMachineIds = parseList(env.TANGLE_FLEET_EXCLUDE_MACHINES)\n return createFleetWorkspaceExecutor({\n fleet,\n excludeMachineIds,\n })\n}\n\ninterface FleetsApi {\n get(fleetId: string): Promise<unknown>\n}\n\nasync function defaultResolveFleet(\n sandboxClient: LoopSandboxClient,\n fleetId: string,\n): Promise<FleetHandle> {\n const fleets = (sandboxClient as unknown as { fleets?: FleetsApi }).fleets\n if (!fleets || typeof fleets.get !== 'function') {\n throw new Error(\n 'agent-runtime-mcp: the configured sandbox client does not expose `.fleets.get`; upgrade @tangle-network/sandbox to >= 0.2.1 or unset TANGLE_FLEET_ID.',\n )\n }\n const raw = await fleets.get(fleetId)\n if (!raw || typeof raw !== 'object') {\n throw new Error(`agent-runtime-mcp: fleets.get(${fleetId}) returned no handle`)\n }\n const handle = raw as Partial<FleetHandle>\n if (typeof handle.fleetId !== 'string' || !Array.isArray(handle.ids)) {\n throw new Error(\n `agent-runtime-mcp: fleet handle for ${fleetId} is missing fleetId/ids — incompatible sandbox SDK shape`,\n )\n }\n if (typeof handle.sandbox !== 'function') {\n throw new Error(\n `agent-runtime-mcp: fleet handle for ${fleetId} is missing sandbox(machineId) — incompatible sandbox SDK shape`,\n )\n }\n return handle as FleetHandle\n}\n\nfunction parseFleetId(raw: string | undefined): string | undefined {\n if (typeof raw !== 'string') return undefined\n const trimmed = raw.trim()\n return trimmed.length > 0 ? trimmed : undefined\n}\n\nfunction parseList(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","/**\n * @experimental\n *\n * Delegate factories — the layer between MCP tool handlers and the\n * underlying `runLoop` runners.\n *\n * The MCP server is profile-agnostic: it owns the task queue + feedback\n * store + transport. Each `*Delegate` is the closure that the queue\n * invokes when a task runs. Consumers can override either delegate to\n * inject custom drivers, mocks, fleet-aware dispatchers, etc.\n *\n * The default coder delegate is wired here because we own\n * `coderProfile` / `multiHarnessCoderFanout`. The default researcher\n * delegate is **not** wired in this file — `agent-knowledge` cannot be\n * imported from `agent-runtime` without inducing a cycle. Consumers\n * pass `researcherDelegate` explicitly when constructing the server.\n */\n\nimport type { LoopSandboxClient } from '../loops'\nimport { runLoop } from '../loops'\nimport { coderProfile, multiHarnessCoderFanout } from '../profiles/coder'\nimport { createSiblingSandboxExecutor, type DelegationExecutor } from './executor'\nimport type {\n CoderTask,\n DelegateCodeArgs,\n DelegateResearchArgs,\n DelegationProgress,\n ResearchOutputShape,\n} from './types'\n\n/** @experimental */\nexport interface DelegateRunCtx {\n signal: AbortSignal\n report(progress: DelegationProgress): void\n}\n\n/** @experimental */\nexport type CoderDelegate = (\n args: DelegateCodeArgs,\n ctx: DelegateRunCtx,\n) => Promise<import('../profiles/coder').CoderOutput>\n\n/** @experimental */\nexport type ResearcherDelegate = (\n args: DelegateResearchArgs,\n ctx: DelegateRunCtx,\n) => Promise<ResearchOutputShape>\n\n/** @experimental */\nexport interface CreateDefaultCoderDelegateOptions {\n /**\n * Execution placement. Pass a {@link DelegationExecutor} (sibling or fleet)\n * to control where worker iterations land. `sandboxClient` is a\n * convenience shorthand that wraps the client in a sibling executor — pass\n * one or the other, not both.\n */\n executor?: DelegationExecutor\n /**\n * Convenience shorthand for sibling placement. Equivalent to\n * `executor: createSiblingSandboxExecutor({ client: sandboxClient })`.\n */\n sandboxClient?: LoopSandboxClient\n /** Default `['claude-code', 'codex', 'opencode/zai-coding-plan/glm-5.1']` when variants > 1. */\n fanoutHarnesses?: string[]\n /** Hard cap on the kernel's per-batch concurrency. Default 4. */\n maxConcurrency?: number\n}\n\n/**\n * Build a coder delegate that drives `runLoop` against the project's\n * sandbox client + coder profile. When `args.variants > 1` it switches\n * to the multi-harness fanout topology.\n *\n * @experimental\n */\nexport function createDefaultCoderDelegate(\n options: CreateDefaultCoderDelegateOptions,\n): CoderDelegate {\n const executor = resolveExecutor(options)\n const sandboxClient = executor.client\n const fanoutHarnesses = options.fanoutHarnesses\n const maxConcurrency = options.maxConcurrency ?? 4\n return async (args, ctx) => {\n const task: CoderTask = {\n goal: buildCoderGoal(args),\n repoRoot: args.repoRoot,\n testCmd: args.config?.testCmd,\n typecheckCmd: args.config?.typecheckCmd,\n forbiddenPaths: args.config?.forbiddenPaths,\n maxDiffLines: args.config?.maxDiffLines,\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 { agentRunSpec, output, validator } = coderProfile({ task })\n const result = await runLoop({\n driver: singleShotDriver,\n agentRun: agentRunSpec,\n output,\n validator,\n task,\n ctx: { sandboxClient, signal: ctx.signal },\n maxIterations: 1,\n maxConcurrency,\n })\n const winner = result.winner\n if (!winner) {\n throw new Error('coder delegate produced no winner')\n }\n ctx.report({ iteration: 1, phase: 'completed' })\n return winner.output\n }\n const fanout = multiHarnessCoderFanout(\n fanoutHarnesses && fanoutHarnesses.length > 0\n ? { harnesses: fanoutHarnesses.slice(0, variants) }\n : { harnesses: undefined },\n )\n const agentRuns = fanout.agentRuns.slice(0, variants)\n const result = await runLoop({\n driver: fanout.driver,\n agentRuns,\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 winner = result.winner\n if (!winner) {\n throw new Error('coder delegate fanout produced no winner')\n }\n ctx.report({ iteration: agentRuns.length, phase: 'completed' })\n return winner.output\n }\n}\n\nfunction buildCoderGoal(args: DelegateCodeArgs): string {\n if (!args.contextHint) return args.goal\n return [args.goal, '', '## Context', args.contextHint].join('\\n')\n}\n\nfunction resolveExecutor(options: CreateDefaultCoderDelegateOptions): DelegationExecutor {\n if (options.executor && options.sandboxClient) {\n throw new Error('createDefaultCoderDelegate: pass exactly one of `executor` or `sandboxClient`')\n }\n if (options.executor) return options.executor\n if (options.sandboxClient) {\n return createSiblingSandboxExecutor({ client: options.sandboxClient })\n }\n throw new Error('createDefaultCoderDelegate: `executor` or `sandboxClient` is required')\n}\n\n/**\n * Single-shot driver — plan one task on iteration 0, stop after one\n * iteration. Used by the coder delegate when `variants <= 1`. Keeps the\n * runLoop kernel-level accounting (timing, cost, trace emission) while\n * skipping fanout/refine topology overhead.\n */\nconst singleShotDriver = {\n name: 'mcp-single-shot',\n async plan<Task>(task: Task, history: ReadonlyArray<unknown>): Promise<Task[]> {\n return history.length === 0 ? [task] : []\n },\n decide(history: ReadonlyArray<unknown>): 'pick-winner' | 'fail' {\n return history.length > 0 ? 'pick-winner' : 'fail'\n },\n}\n","/**\n * @experimental\n *\n * Stdio JSON-RPC MCP server exposing the 5 delegation tools to sandbox\n * coding-harness agents (claude-code, codex, opencode, ...).\n *\n * The server is transport-bound but topology-free: tool execution is\n * delegated to handler functions composed from a queue, a feedback\n * store, and per-profile run delegates. Consumers wire those at\n * construction time. The `agent-runtime-mcp` bin spins up a default\n * configuration for the common case (real sandbox client + coder).\n *\n * Wire protocol: line-delimited JSON-RPC 2.0 over stdio. Each line is\n * one request; each response is one line. `tools/list` and `tools/call`\n * mirror the MCP 2024-11-05 spec; we do not pull in\n * `@modelcontextprotocol/sdk` to keep the dependency footprint zero.\n */\n\nimport { createInterface, type Interface as ReadlineInterface } from 'node:readline'\nimport { Readable, Writable } from 'node:stream'\nimport type { CoderDelegate, ResearcherDelegate } from './delegates'\nimport { type FeedbackStore, InMemoryFeedbackStore } from './feedback-store'\nimport { DelegationTaskQueue } from './task-queue'\nimport {\n createDelegateCodeHandler,\n DELEGATE_CODE_DESCRIPTION,\n DELEGATE_CODE_INPUT_SCHEMA,\n DELEGATE_CODE_TOOL_NAME,\n} from './tools/delegate-code'\nimport {\n createDelegateFeedbackHandler,\n DELEGATE_FEEDBACK_DESCRIPTION,\n DELEGATE_FEEDBACK_INPUT_SCHEMA,\n DELEGATE_FEEDBACK_TOOL_NAME,\n} from './tools/delegate-feedback'\nimport {\n createDelegateResearchHandler,\n DELEGATE_RESEARCH_DESCRIPTION,\n DELEGATE_RESEARCH_INPUT_SCHEMA,\n DELEGATE_RESEARCH_TOOL_NAME,\n} from './tools/delegate-research'\nimport {\n createDelegationHistoryHandler,\n DELEGATION_HISTORY_DESCRIPTION,\n DELEGATION_HISTORY_INPUT_SCHEMA,\n DELEGATION_HISTORY_TOOL_NAME,\n} from './tools/delegation-history'\nimport {\n createDelegationStatusHandler,\n DELEGATION_STATUS_DESCRIPTION,\n DELEGATION_STATUS_INPUT_SCHEMA,\n DELEGATION_STATUS_TOOL_NAME,\n} from './tools/delegation-status'\n\n/** @experimental */\nexport interface McpServerOptions {\n /** Required to enable delegate_code. */\n coderDelegate?: CoderDelegate\n /**\n * Required to enable delegate_research. The substrate cannot ship a\n * default — wire one that closes over your `runLoop` + a\n * researcher profile (typically `@tangle-network/agent-knowledge`'s\n * `researcherProfile` / `multiHarnessResearcherFanout`).\n */\n researcherDelegate?: ResearcherDelegate\n /** Override the default in-memory feedback store. */\n feedbackStore?: FeedbackStore\n /** Override the default in-memory task queue. */\n queue?: DelegationTaskQueue\n /** Server display name surfaced via `initialize`. Default `'agent-runtime-mcp'`. */\n serverName?: string\n /** Server version surfaced via `initialize`. Default = the package version baked at build time. */\n serverVersion?: string\n}\n\n/** @experimental */\nexport interface McpToolDescriptor {\n name: string\n description: string\n inputSchema: Record<string, unknown>\n handler: (raw: unknown) => Promise<unknown>\n}\n\n/** @experimental */\nexport interface McpServer {\n /** Tools currently registered (depend on which delegates were wired). */\n readonly tools: ReadonlyMap<string, McpToolDescriptor>\n /** The underlying queue — exposed so tests can introspect it. */\n readonly queue: DelegationTaskQueue\n /** The feedback store — exposed for the same reason. */\n readonly feedbackStore: FeedbackStore\n /** Handle a single parsed JSON-RPC message. Returns the response object (or `null` for notifications). */\n handle(message: JsonRpcMessage): Promise<JsonRpcResponse | null>\n /** Drive the server on a stdio-shaped transport until `stop()` is called. */\n serve(transport?: McpTransport): Promise<void>\n /** Stop a `serve` call. Subsequent requests are rejected. */\n stop(): void\n}\n\n/** @experimental */\nexport interface McpTransport {\n input: NodeJS.ReadableStream\n output: NodeJS.WritableStream\n}\n\n/** @experimental */\nexport interface JsonRpcMessage {\n jsonrpc: '2.0'\n id?: number | string | null\n method: string\n params?: unknown\n}\n\n/** @experimental */\nexport interface JsonRpcResponse {\n jsonrpc: '2.0'\n id: number | string | null\n result?: unknown\n error?: { code: number; message: string; data?: unknown }\n}\n\nconst PROTOCOL_VERSION = '2024-11-05'\nconst DEFAULT_SERVER_NAME = 'agent-runtime-mcp'\nconst DEFAULT_SERVER_VERSION = '0.22.0'\n\n/** @experimental */\nexport function createMcpServer(options: McpServerOptions = {}): McpServer {\n const queue = options.queue ?? new DelegationTaskQueue()\n const feedbackStore = options.feedbackStore ?? new InMemoryFeedbackStore()\n const serverName = options.serverName ?? DEFAULT_SERVER_NAME\n const serverVersion = options.serverVersion ?? DEFAULT_SERVER_VERSION\n\n const tools = new Map<string, McpToolDescriptor>()\n\n if (options.coderDelegate) {\n tools.set(DELEGATE_CODE_TOOL_NAME, {\n name: DELEGATE_CODE_TOOL_NAME,\n description: DELEGATE_CODE_DESCRIPTION,\n inputSchema: DELEGATE_CODE_INPUT_SCHEMA as unknown as Record<string, unknown>,\n handler: createDelegateCodeHandler({ queue, delegate: options.coderDelegate }),\n })\n }\n if (options.researcherDelegate) {\n tools.set(DELEGATE_RESEARCH_TOOL_NAME, {\n name: DELEGATE_RESEARCH_TOOL_NAME,\n description: DELEGATE_RESEARCH_DESCRIPTION,\n inputSchema: DELEGATE_RESEARCH_INPUT_SCHEMA as unknown as Record<string, unknown>,\n handler: createDelegateResearchHandler({ queue, delegate: options.researcherDelegate }),\n })\n }\n tools.set(DELEGATE_FEEDBACK_TOOL_NAME, {\n name: DELEGATE_FEEDBACK_TOOL_NAME,\n description: DELEGATE_FEEDBACK_DESCRIPTION,\n inputSchema: DELEGATE_FEEDBACK_INPUT_SCHEMA as unknown as Record<string, unknown>,\n handler: createDelegateFeedbackHandler({ queue, store: feedbackStore }),\n })\n tools.set(DELEGATION_STATUS_TOOL_NAME, {\n name: DELEGATION_STATUS_TOOL_NAME,\n description: DELEGATION_STATUS_DESCRIPTION,\n inputSchema: DELEGATION_STATUS_INPUT_SCHEMA as unknown as Record<string, unknown>,\n handler: createDelegationStatusHandler({ queue }),\n })\n tools.set(DELEGATION_HISTORY_TOOL_NAME, {\n name: DELEGATION_HISTORY_TOOL_NAME,\n description: DELEGATION_HISTORY_DESCRIPTION,\n inputSchema: DELEGATION_HISTORY_INPUT_SCHEMA as unknown as Record<string, unknown>,\n handler: createDelegationHistoryHandler({ queue }),\n })\n\n let stopped = false\n let activeReadline: ReadlineInterface | undefined\n\n async function handle(message: JsonRpcMessage): Promise<JsonRpcResponse | null> {\n if (stopped) {\n return rpcError(message.id ?? null, -32099, 'server stopped')\n }\n if (message.method === 'initialize') {\n return rpcResult(message.id ?? null, {\n protocolVersion: PROTOCOL_VERSION,\n capabilities: { tools: {} },\n serverInfo: { name: serverName, version: serverVersion },\n })\n }\n if (message.method === 'notifications/initialized') {\n // MCP clients send this after the handshake; it has no id and expects\n // no response.\n return null\n }\n if (message.method === 'tools/list') {\n return rpcResult(message.id ?? null, {\n tools: [...tools.values()].map((tool) => ({\n name: tool.name,\n description: tool.description,\n inputSchema: tool.inputSchema,\n })),\n })\n }\n if (message.method === 'tools/call') {\n const params = (message.params ?? {}) as { name?: unknown; arguments?: unknown }\n const name = typeof params.name === 'string' ? params.name : ''\n const tool = tools.get(name)\n if (!tool) {\n return rpcError(message.id ?? null, -32601, `unknown tool: ${name}`)\n }\n try {\n const output = await tool.handler(params.arguments ?? {})\n return rpcResult(message.id ?? null, {\n content: [{ type: 'text', text: JSON.stringify(output) }],\n structuredContent: output,\n isError: false,\n })\n } catch (err) {\n const reason = err instanceof Error ? err.message : String(err)\n const code = err instanceof TypeError || err instanceof RangeError ? -32602 : -32000\n return rpcError(message.id ?? null, code, reason)\n }\n }\n if (message.id === undefined || message.id === null) return null\n return rpcError(message.id, -32601, `unknown method: ${message.method}`)\n }\n\n async function serve(transport?: McpTransport): Promise<void> {\n const input = transport?.input ?? process.stdin\n const output = transport?.output ?? process.stdout\n const rl = createInterface({ input, crlfDelay: Number.POSITIVE_INFINITY })\n activeReadline = rl\n return new Promise<void>((resolve, reject) => {\n rl.on('line', (line) => {\n const trimmed = line.trim()\n if (!trimmed) return\n let parsed: JsonRpcMessage | undefined\n try {\n parsed = JSON.parse(trimmed) as JsonRpcMessage\n } catch (err) {\n writeResponse(output, rpcError(null, -32700, `parse error: ${(err as Error).message}`))\n return\n }\n if (!parsed || parsed.jsonrpc !== '2.0' || typeof parsed.method !== 'string') {\n writeResponse(output, rpcError(parsed?.id ?? null, -32600, 'invalid request'))\n return\n }\n void handle(parsed).then((response) => {\n if (response) writeResponse(output, response)\n })\n })\n rl.on('close', () => resolve())\n rl.on('error', (err) => reject(err))\n if (stopped) {\n rl.close()\n resolve()\n }\n })\n }\n\n function stop(): void {\n stopped = true\n activeReadline?.close()\n activeReadline = undefined\n }\n\n return {\n tools,\n queue,\n feedbackStore,\n handle,\n serve,\n stop,\n }\n}\n\nfunction rpcResult(id: number | string | null, result: unknown): JsonRpcResponse {\n return { jsonrpc: '2.0', id, result }\n}\n\nfunction rpcError(\n id: number | string | null,\n code: number,\n message: string,\n data?: unknown,\n): JsonRpcResponse {\n return {\n jsonrpc: '2.0',\n id,\n error: data === undefined ? { code, message } : { code, message, data },\n }\n}\n\nfunction writeResponse(output: NodeJS.WritableStream, response: JsonRpcResponse): void {\n output.write(`${JSON.stringify(response)}\\n`)\n}\n\n/**\n * In-process pair of `Readable` + `Writable` streams suitable for driving\n * `server.serve(...)` from a test. Returns the agent-side stream (the\n * client writes to it) and the server-side stream (the test reads from it).\n *\n * @experimental\n */\nexport function createInProcessTransport(): {\n transport: McpTransport\n clientWrite(line: string): void\n clientClose(): void\n readServer(): Promise<JsonRpcResponse[]>\n} {\n const responses: JsonRpcResponse[] = []\n const input = new Readable({ read() {} })\n const output = new Writable({\n write(chunk, _enc, cb) {\n const text = chunk.toString('utf8')\n for (const line of text.split('\\n')) {\n const trimmed = line.trim()\n if (!trimmed) continue\n try {\n responses.push(JSON.parse(trimmed) as JsonRpcResponse)\n } catch {\n // Non-JSON output should never appear; drop it silently in the\n // test transport rather than crashing.\n }\n }\n cb()\n },\n })\n return {\n transport: { input, output },\n clientWrite(line: string) {\n input.push(`${line}\\n`)\n },\n clientClose() {\n input.push(null)\n },\n async readServer() {\n // Yield to the event loop a few times so async handlers drain.\n for (let i = 0; i < 5; i += 1) await new Promise((r) => setImmediate(r))\n return [...responses]\n },\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CO,SAAS,6BACd,SACoB;AACpB,QAAM,aAAa,QAAQ;AAC3B,QAAM,SAA4B;AAAA,IAChC,OAAO,MAAuD;AAC5D,aAAO,WAAW,OAAO,IAAI;AAAA,IAC/B;AAAA,IACA,kBAAkB,KAA4C;AAC5D,aAAO,EAAE,MAAM,WAAW,WAAW,OAAO,GAAG,EAAE;AAAA,IACnD;AAAA,EACF;AACA,SAAO;AAAA,IACL;AAAA,IACA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