@mandujs/mcp 0.19.6 → 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/package.json +4 -3
- package/src/tools/ai-brief.ts +443 -0
- package/src/tools/deploy-preview.ts +316 -0
- package/src/tools/index.ts +14 -0
- package/src/tools/loop-close.ts +175 -0
- package/src/tools/run-tests.ts +424 -0
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP tool — `mandu.deploy.preview`
|
|
3
|
+
*
|
|
4
|
+
* Invokes `mandu deploy --target=<target> --dry-run` in a child process
|
|
5
|
+
* and parses the stdout into a structured artifact list.
|
|
6
|
+
*
|
|
7
|
+
* Safety:
|
|
8
|
+
* • `--dry-run` is *always* passed. The tool cannot be coerced into
|
|
9
|
+
* triggering a real deployment.
|
|
10
|
+
* • Target is validated against the known adapter list; unknown values
|
|
11
|
+
* fail fast without spawning a process.
|
|
12
|
+
* • The child has a 5-minute ceiling; it's killed if it overruns.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { Tool } from "@modelcontextprotocol/sdk/types.js";
|
|
16
|
+
import { spawn } from "bun";
|
|
17
|
+
import path from "path";
|
|
18
|
+
|
|
19
|
+
// Mirror the adapter list from `packages/cli/src/commands/deploy/types.ts`.
|
|
20
|
+
// Keep this in sync manually — imports from @mandujs/cli would introduce a
|
|
21
|
+
// cross-package coupling for a single string-union.
|
|
22
|
+
const DEPLOY_TARGETS = [
|
|
23
|
+
"docker",
|
|
24
|
+
"fly",
|
|
25
|
+
"vercel",
|
|
26
|
+
"railway",
|
|
27
|
+
"netlify",
|
|
28
|
+
"cf-pages",
|
|
29
|
+
"docker-compose",
|
|
30
|
+
] as const;
|
|
31
|
+
type DeployTarget = (typeof DEPLOY_TARGETS)[number];
|
|
32
|
+
const VALID_TARGET_SET = new Set<string>(DEPLOY_TARGETS);
|
|
33
|
+
|
|
34
|
+
interface DeployPreviewInput {
|
|
35
|
+
target?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface ArtifactEntry {
|
|
39
|
+
path: string;
|
|
40
|
+
preserved: boolean;
|
|
41
|
+
description?: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface DeployPreviewResult {
|
|
45
|
+
target: DeployTarget;
|
|
46
|
+
mode: "dry-run";
|
|
47
|
+
artifact_list: ArtifactEntry[];
|
|
48
|
+
warnings: string[];
|
|
49
|
+
/** Text diff extracted from the preview output, if present. */
|
|
50
|
+
diff?: string;
|
|
51
|
+
exit_code: number;
|
|
52
|
+
/** Trailing 2000 chars of stdout for diagnostic context. */
|
|
53
|
+
stdout_tail?: string;
|
|
54
|
+
/** Trailing 2000 chars of stderr for diagnostic context. */
|
|
55
|
+
stderr_tail?: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
59
|
+
// Input validation
|
|
60
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
61
|
+
|
|
62
|
+
const COMMAND_TIMEOUT_MS = 5 * 60_000;
|
|
63
|
+
|
|
64
|
+
function validateInput(raw: Record<string, unknown>): {
|
|
65
|
+
ok: true;
|
|
66
|
+
target: DeployTarget;
|
|
67
|
+
} | { ok: false; error: string; field: string; hint: string } {
|
|
68
|
+
const target = raw.target;
|
|
69
|
+
if (typeof target !== "string" || target.length === 0) {
|
|
70
|
+
return {
|
|
71
|
+
ok: false,
|
|
72
|
+
error: "'target' is required",
|
|
73
|
+
field: "target",
|
|
74
|
+
hint: `Pass one of: ${DEPLOY_TARGETS.join(", ")}`,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
if (!VALID_TARGET_SET.has(target)) {
|
|
78
|
+
return {
|
|
79
|
+
ok: false,
|
|
80
|
+
error: `Unknown deploy target: ${target}`,
|
|
81
|
+
field: "target",
|
|
82
|
+
hint: `Supported: ${DEPLOY_TARGETS.join(", ")}`,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
return { ok: true, target: target as DeployTarget };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
89
|
+
// Output parsing
|
|
90
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Parse the CLI's artifact-rendering block:
|
|
94
|
+
*
|
|
95
|
+
* 📦 Adapter prepare: docker
|
|
96
|
+
* + .mandu/deploy/docker/Dockerfile — production image
|
|
97
|
+
* • .mandu/deploy/docker/.dockerignore
|
|
98
|
+
* + .mandu/deploy/docker/entrypoint.sh
|
|
99
|
+
*
|
|
100
|
+
* Returns `preserved=true` for `•` (existing) and `false` for `+` (new).
|
|
101
|
+
*/
|
|
102
|
+
export function parseDeployPreviewOutput(raw: string): {
|
|
103
|
+
artifacts: ArtifactEntry[];
|
|
104
|
+
warnings: string[];
|
|
105
|
+
diff?: string;
|
|
106
|
+
} {
|
|
107
|
+
const lines = raw.split(/\r?\n/);
|
|
108
|
+
const artifacts: ArtifactEntry[] = [];
|
|
109
|
+
const warnings: string[] = [];
|
|
110
|
+
const diffLines: string[] = [];
|
|
111
|
+
|
|
112
|
+
let inDiff = false;
|
|
113
|
+
|
|
114
|
+
for (const rawLine of lines) {
|
|
115
|
+
const line = rawLine.replace(/\r$/, "");
|
|
116
|
+
const trimmed = line.trim();
|
|
117
|
+
|
|
118
|
+
// Diff mode gates the other regexes: inside a fenced block we don't
|
|
119
|
+
// re-interpret `+` / `•` as artifact markers.
|
|
120
|
+
if (inDiff) {
|
|
121
|
+
if (/^```$/.test(trimmed)) {
|
|
122
|
+
inDiff = false;
|
|
123
|
+
} else {
|
|
124
|
+
diffLines.push(line);
|
|
125
|
+
}
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (!trimmed) continue;
|
|
130
|
+
|
|
131
|
+
// Opening of a diff block: triple-backtick (optionally language-tagged)
|
|
132
|
+
// or an unfenced `diff --git` header.
|
|
133
|
+
if (/^```\S*$/.test(trimmed)) {
|
|
134
|
+
inDiff = true;
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (/^diff\s+--git\s/.test(trimmed)) {
|
|
138
|
+
inDiff = true;
|
|
139
|
+
diffLines.push(trimmed);
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Artifact: prefix `+` or `•` after leading whitespace.
|
|
144
|
+
const artifactMatch = /^([+•])\s+(.+)$/.exec(trimmed);
|
|
145
|
+
if (artifactMatch) {
|
|
146
|
+
const marker = artifactMatch[1];
|
|
147
|
+
const rest = artifactMatch[2];
|
|
148
|
+
const emDashIdx = rest.indexOf(" — ");
|
|
149
|
+
const p = emDashIdx >= 0 ? rest.slice(0, emDashIdx).trim() : rest.trim();
|
|
150
|
+
const desc = emDashIdx >= 0 ? rest.slice(emDashIdx + 3).trim() : undefined;
|
|
151
|
+
artifacts.push({
|
|
152
|
+
path: p,
|
|
153
|
+
preserved: marker === "•",
|
|
154
|
+
...(desc ? { description: desc } : {}),
|
|
155
|
+
});
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Warning: lines that start with ⚠️ or "Warning:"
|
|
160
|
+
if (/^\s*(?:⚠️|warning:)/i.test(trimmed)) {
|
|
161
|
+
warnings.push(trimmed.replace(/^⚠️\s*/u, "").replace(/^warning:\s*/i, "").trim());
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const result: { artifacts: ArtifactEntry[]; warnings: string[]; diff?: string } = {
|
|
167
|
+
artifacts,
|
|
168
|
+
warnings,
|
|
169
|
+
};
|
|
170
|
+
if (diffLines.length > 0) {
|
|
171
|
+
result.diff = diffLines.join("\n").trim();
|
|
172
|
+
}
|
|
173
|
+
return result;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
177
|
+
// Child process invocation
|
|
178
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
179
|
+
|
|
180
|
+
function tailString(s: string, max = 2000): string {
|
|
181
|
+
if (s.length <= max) return s;
|
|
182
|
+
return s.slice(-max);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async function resolveManduCommand(projectRoot: string): Promise<string[]> {
|
|
186
|
+
const localBin = path.join(projectRoot, "node_modules", ".bin", "mandu");
|
|
187
|
+
try {
|
|
188
|
+
const f = Bun.file(localBin);
|
|
189
|
+
if (await f.exists()) {
|
|
190
|
+
return ["bun", "run", localBin];
|
|
191
|
+
}
|
|
192
|
+
} catch {}
|
|
193
|
+
|
|
194
|
+
const monorepoCli = path.resolve(projectRoot, "packages", "cli", "src", "main.ts");
|
|
195
|
+
try {
|
|
196
|
+
const f = Bun.file(monorepoCli);
|
|
197
|
+
if (await f.exists()) {
|
|
198
|
+
return ["bun", "run", monorepoCli];
|
|
199
|
+
}
|
|
200
|
+
} catch {}
|
|
201
|
+
|
|
202
|
+
return ["mandu"];
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async function runProcess(
|
|
206
|
+
cmd: string[],
|
|
207
|
+
cwd: string,
|
|
208
|
+
timeoutMs: number,
|
|
209
|
+
): Promise<{ stdout: string; stderr: string; exitCode: number; timedOut: boolean }> {
|
|
210
|
+
const proc = spawn(cmd, {
|
|
211
|
+
cwd,
|
|
212
|
+
stdout: "pipe",
|
|
213
|
+
stderr: "pipe",
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
let timedOut = false;
|
|
217
|
+
const handle = setTimeout(() => {
|
|
218
|
+
timedOut = true;
|
|
219
|
+
try {
|
|
220
|
+
proc.kill();
|
|
221
|
+
} catch {}
|
|
222
|
+
}, timeoutMs);
|
|
223
|
+
|
|
224
|
+
try {
|
|
225
|
+
const [stdout, stderr, exitCode] = await Promise.all([
|
|
226
|
+
new Response(proc.stdout).text(),
|
|
227
|
+
new Response(proc.stderr).text(),
|
|
228
|
+
proc.exited,
|
|
229
|
+
]);
|
|
230
|
+
return { stdout, stderr, exitCode: exitCode ?? 1, timedOut };
|
|
231
|
+
} finally {
|
|
232
|
+
clearTimeout(handle);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
237
|
+
// Handler
|
|
238
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
239
|
+
|
|
240
|
+
async function deployPreview(
|
|
241
|
+
projectRoot: string,
|
|
242
|
+
input: DeployPreviewInput,
|
|
243
|
+
): Promise<DeployPreviewResult | { error: string; field?: string; hint?: string }> {
|
|
244
|
+
const validated = validateInput(input as Record<string, unknown>);
|
|
245
|
+
if (!validated.ok) {
|
|
246
|
+
return {
|
|
247
|
+
error: validated.error,
|
|
248
|
+
field: validated.field,
|
|
249
|
+
hint: validated.hint,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const { target } = validated;
|
|
254
|
+
const base = await resolveManduCommand(projectRoot);
|
|
255
|
+
const cmd = [...base, "deploy", `--target=${target}`, "--dry-run"];
|
|
256
|
+
|
|
257
|
+
let proc: { stdout: string; stderr: string; exitCode: number; timedOut: boolean };
|
|
258
|
+
try {
|
|
259
|
+
proc = await runProcess(cmd, projectRoot, COMMAND_TIMEOUT_MS);
|
|
260
|
+
} catch (err) {
|
|
261
|
+
return {
|
|
262
|
+
error: `Failed to spawn deploy preview: ${err instanceof Error ? err.message : String(err)}`,
|
|
263
|
+
hint: "Verify that @mandujs/cli is installed and accessible",
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const parsed = parseDeployPreviewOutput(`${proc.stdout}\n${proc.stderr}`);
|
|
268
|
+
|
|
269
|
+
const result: DeployPreviewResult = {
|
|
270
|
+
target,
|
|
271
|
+
mode: "dry-run",
|
|
272
|
+
artifact_list: parsed.artifacts,
|
|
273
|
+
warnings: parsed.warnings,
|
|
274
|
+
exit_code: proc.exitCode,
|
|
275
|
+
stdout_tail: tailString(proc.stdout),
|
|
276
|
+
stderr_tail: tailString(proc.stderr),
|
|
277
|
+
};
|
|
278
|
+
if (parsed.diff) result.diff = parsed.diff;
|
|
279
|
+
if (proc.timedOut) result.warnings.push("deploy preview timed out");
|
|
280
|
+
return result;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
284
|
+
// MCP tool definition + handler map
|
|
285
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
286
|
+
|
|
287
|
+
export const deployPreviewToolDefinitions: Tool[] = [
|
|
288
|
+
{
|
|
289
|
+
name: "mandu.deploy.preview",
|
|
290
|
+
description:
|
|
291
|
+
"Preview a deployment by running `mandu deploy --target=<t> --dry-run`. Returns the structured artifact list, parsed warnings, and any diff the adapter emits. Always dry-run — this tool cannot trigger a real deployment.",
|
|
292
|
+
annotations: {
|
|
293
|
+
readOnlyHint: true,
|
|
294
|
+
},
|
|
295
|
+
inputSchema: {
|
|
296
|
+
type: "object",
|
|
297
|
+
properties: {
|
|
298
|
+
target: {
|
|
299
|
+
type: "string",
|
|
300
|
+
enum: [...DEPLOY_TARGETS],
|
|
301
|
+
description:
|
|
302
|
+
"Deployment adapter target. Each adapter controls the artifact set — see `mandu deploy --help` for details.",
|
|
303
|
+
},
|
|
304
|
+
},
|
|
305
|
+
required: ["target"],
|
|
306
|
+
},
|
|
307
|
+
},
|
|
308
|
+
];
|
|
309
|
+
|
|
310
|
+
export function deployPreviewTools(projectRoot: string) {
|
|
311
|
+
const handlers: Record<string, (args: Record<string, unknown>) => Promise<unknown>> = {
|
|
312
|
+
"mandu.deploy.preview": async (args) =>
|
|
313
|
+
deployPreview(projectRoot, args as DeployPreviewInput),
|
|
314
|
+
};
|
|
315
|
+
return handlers;
|
|
316
|
+
}
|
package/src/tools/index.ts
CHANGED
|
@@ -32,6 +32,11 @@ export { resourceTools, resourceToolDefinitions } from "./resource.js";
|
|
|
32
32
|
export { componentTools, componentToolDefinitions } from "./component.js";
|
|
33
33
|
export { kitchenTools, kitchenToolDefinitions } from "./kitchen.js";
|
|
34
34
|
export { compositeTools, compositeToolDefinitions } from "./composite.js";
|
|
35
|
+
// Phase 14.3 — AI/agent loop-closure tool suite
|
|
36
|
+
export { runTestsTools, runTestsToolDefinitions } from "./run-tests.js";
|
|
37
|
+
export { deployPreviewTools, deployPreviewToolDefinitions } from "./deploy-preview.js";
|
|
38
|
+
export { aiBriefTools, aiBriefToolDefinitions } from "./ai-brief.js";
|
|
39
|
+
export { loopCloseTools, loopCloseToolDefinitions } from "./loop-close.js";
|
|
35
40
|
|
|
36
41
|
// 도구 모듈 import (등록용)
|
|
37
42
|
import { specTools, specToolDefinitions } from "./spec.js";
|
|
@@ -54,6 +59,10 @@ import { resourceTools, resourceToolDefinitions } from "./resource.js";
|
|
|
54
59
|
import { componentTools, componentToolDefinitions } from "./component.js";
|
|
55
60
|
import { kitchenTools, kitchenToolDefinitions } from "./kitchen.js";
|
|
56
61
|
import { compositeTools, compositeToolDefinitions } from "./composite.js";
|
|
62
|
+
import { runTestsTools, runTestsToolDefinitions } from "./run-tests.js";
|
|
63
|
+
import { deployPreviewTools, deployPreviewToolDefinitions } from "./deploy-preview.js";
|
|
64
|
+
import { aiBriefTools, aiBriefToolDefinitions } from "./ai-brief.js";
|
|
65
|
+
import { loopCloseTools, loopCloseToolDefinitions } from "./loop-close.js";
|
|
57
66
|
|
|
58
67
|
/**
|
|
59
68
|
* 도구 모듈 정보
|
|
@@ -94,6 +103,11 @@ const TOOL_MODULES: ToolModule[] = [
|
|
|
94
103
|
{ category: "component", definitions: componentToolDefinitions, handlers: componentTools },
|
|
95
104
|
{ category: "kitchen", definitions: kitchenToolDefinitions, handlers: kitchenTools },
|
|
96
105
|
{ category: "composite", definitions: compositeToolDefinitions, handlers: compositeTools },
|
|
106
|
+
// Phase 14.3 — AI/agent loop-closure suite
|
|
107
|
+
{ category: "run-tests", definitions: runTestsToolDefinitions, handlers: runTestsTools },
|
|
108
|
+
{ category: "deploy-preview", definitions: deployPreviewToolDefinitions, handlers: deployPreviewTools },
|
|
109
|
+
{ category: "ai-brief", definitions: aiBriefToolDefinitions, handlers: aiBriefTools },
|
|
110
|
+
{ category: "loop-close", definitions: loopCloseToolDefinitions, handlers: loopCloseTools },
|
|
97
111
|
];
|
|
98
112
|
|
|
99
113
|
/**
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP tool — `mandu.loop.close`
|
|
3
|
+
*
|
|
4
|
+
* Thin, safe adapter over `@mandujs/skills/loop-closure`. Given agent
|
|
5
|
+
* output (stdout, stderr, exitCode), runs the built-in loop-closure
|
|
6
|
+
* detectors and returns a structured follow-up prompt:
|
|
7
|
+
*
|
|
8
|
+
* {
|
|
9
|
+
* stallReason: string,
|
|
10
|
+
* nextPrompt: string,
|
|
11
|
+
* evidence: Array<{ kind, file?, line?, snippet, label? }>
|
|
12
|
+
* }
|
|
13
|
+
*
|
|
14
|
+
* SAFETY INVARIANTS — enforced by design:
|
|
15
|
+
* - `closeLoop()` is pure: no I/O, no spawn, no file writes.
|
|
16
|
+
* - This wrapper adds only input validation + field shaping.
|
|
17
|
+
* - The `nextPrompt` is ADVISORY text. It is never auto-executed.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import type { Tool } from "@modelcontextprotocol/sdk/types.js";
|
|
21
|
+
import { closeLoop, listDetectorIds } from "@mandujs/skills/loop-closure";
|
|
22
|
+
|
|
23
|
+
interface LoopCloseInput {
|
|
24
|
+
stdout?: unknown;
|
|
25
|
+
stderr?: unknown;
|
|
26
|
+
exitCode?: unknown;
|
|
27
|
+
detectors?: unknown;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function validateInput(raw: Record<string, unknown>): {
|
|
31
|
+
ok: true;
|
|
32
|
+
value: {
|
|
33
|
+
stdout: string;
|
|
34
|
+
stderr: string;
|
|
35
|
+
exitCode: number;
|
|
36
|
+
detectors?: string[];
|
|
37
|
+
};
|
|
38
|
+
} | { ok: false; error: string; field: string; hint: string } {
|
|
39
|
+
const stdout = raw.stdout ?? "";
|
|
40
|
+
if (typeof stdout !== "string") {
|
|
41
|
+
return {
|
|
42
|
+
ok: false,
|
|
43
|
+
error: "'stdout' must be a string",
|
|
44
|
+
field: "stdout",
|
|
45
|
+
hint: "Omit or pass '' for no stdout",
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const stderr = raw.stderr ?? "";
|
|
50
|
+
if (typeof stderr !== "string") {
|
|
51
|
+
return {
|
|
52
|
+
ok: false,
|
|
53
|
+
error: "'stderr' must be a string",
|
|
54
|
+
field: "stderr",
|
|
55
|
+
hint: "Omit or pass '' for no stderr",
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
let exitCode = 0;
|
|
60
|
+
if (raw.exitCode !== undefined) {
|
|
61
|
+
if (typeof raw.exitCode !== "number" || !Number.isFinite(raw.exitCode)) {
|
|
62
|
+
return {
|
|
63
|
+
ok: false,
|
|
64
|
+
error: "'exitCode' must be a finite number",
|
|
65
|
+
field: "exitCode",
|
|
66
|
+
hint: "Pass the child-process exit code, typically 0 (success) or non-zero (failure)",
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
exitCode = Math.trunc(raw.exitCode);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
let detectors: string[] | undefined;
|
|
73
|
+
if (raw.detectors !== undefined) {
|
|
74
|
+
if (!Array.isArray(raw.detectors) ||
|
|
75
|
+
!raw.detectors.every((d) => typeof d === "string")) {
|
|
76
|
+
return {
|
|
77
|
+
ok: false,
|
|
78
|
+
error: "'detectors' must be an array of detector IDs",
|
|
79
|
+
field: "detectors",
|
|
80
|
+
hint: `Valid IDs: ${listDetectorIds().join(", ")}`,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
detectors = raw.detectors as string[];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
ok: true,
|
|
88
|
+
value: { stdout, stderr, exitCode, ...(detectors ? { detectors } : {}) },
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
93
|
+
// Handler
|
|
94
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
95
|
+
|
|
96
|
+
async function runLoopClose(
|
|
97
|
+
input: LoopCloseInput,
|
|
98
|
+
): Promise<
|
|
99
|
+
| {
|
|
100
|
+
stallReason: string;
|
|
101
|
+
nextPrompt: string;
|
|
102
|
+
evidence: Array<{
|
|
103
|
+
kind: string;
|
|
104
|
+
file?: string;
|
|
105
|
+
line?: number;
|
|
106
|
+
snippet: string;
|
|
107
|
+
label?: string;
|
|
108
|
+
}>;
|
|
109
|
+
detectors_run: string[];
|
|
110
|
+
}
|
|
111
|
+
| { error: string; field?: string; hint?: string }
|
|
112
|
+
> {
|
|
113
|
+
const validated = validateInput(input as Record<string, unknown>);
|
|
114
|
+
if (!validated.ok) {
|
|
115
|
+
return {
|
|
116
|
+
error: validated.error,
|
|
117
|
+
field: validated.field,
|
|
118
|
+
hint: validated.hint,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const report = closeLoop(validated.value);
|
|
123
|
+
return {
|
|
124
|
+
stallReason: report.stallReason,
|
|
125
|
+
nextPrompt: report.nextPrompt,
|
|
126
|
+
evidence: report.evidence,
|
|
127
|
+
detectors_run: validated.value.detectors ?? listDetectorIds(),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
132
|
+
// Tool definition + handler map
|
|
133
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
134
|
+
|
|
135
|
+
export const loopCloseToolDefinitions: Tool[] = [
|
|
136
|
+
{
|
|
137
|
+
name: "mandu.loop.close",
|
|
138
|
+
description:
|
|
139
|
+
"Analyze agent output (stdout, stderr, exitCode) and return a structured `nextPrompt` that names the primary stall pattern, explains how to address it, and lists supporting evidence. This tool is pure — it never writes files, spawns processes, or auto-executes anything. The returned prompt is advisory text only.",
|
|
140
|
+
annotations: {
|
|
141
|
+
readOnlyHint: true,
|
|
142
|
+
},
|
|
143
|
+
inputSchema: {
|
|
144
|
+
type: "object",
|
|
145
|
+
properties: {
|
|
146
|
+
stdout: {
|
|
147
|
+
type: "string",
|
|
148
|
+
description: "Captured stdout from the most recent command/run.",
|
|
149
|
+
},
|
|
150
|
+
stderr: {
|
|
151
|
+
type: "string",
|
|
152
|
+
description: "Captured stderr from the most recent command/run.",
|
|
153
|
+
},
|
|
154
|
+
exitCode: {
|
|
155
|
+
type: "number",
|
|
156
|
+
description: "Child-process exit code. Defaults to 0 when omitted.",
|
|
157
|
+
},
|
|
158
|
+
detectors: {
|
|
159
|
+
type: "array",
|
|
160
|
+
items: { type: "string" },
|
|
161
|
+
description:
|
|
162
|
+
"Optional detector-ID allow-list. Omit to run the full built-in set.",
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
required: [],
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
];
|
|
169
|
+
|
|
170
|
+
export function loopCloseTools(_projectRoot: string) {
|
|
171
|
+
const handlers: Record<string, (args: Record<string, unknown>) => Promise<unknown>> = {
|
|
172
|
+
"mandu.loop.close": async (args) => runLoopClose(args as LoopCloseInput),
|
|
173
|
+
};
|
|
174
|
+
return handlers;
|
|
175
|
+
}
|