agent-supervisor-mcp 0.0.0-stage → 2.0.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/LICENSE +21 -0
- package/README.en.md +208 -0
- package/README.md +206 -2
- package/package.json +76 -4
- package/skills/agent-supervisor/SKILL.md +139 -0
- package/src/app/acceptance.js +263 -0
- package/src/app/auto-cleanup.js +91 -0
- package/src/app/cancel.js +57 -0
- package/src/app/cleanup-facts.js +101 -0
- package/src/app/cleanup-plan.js +166 -0
- package/src/app/compose.js +17 -0
- package/src/app/context-compiler.js +83 -0
- package/src/app/dispatch.js +398 -0
- package/src/app/followup.js +104 -0
- package/src/app/land-chain.js +134 -0
- package/src/app/landing.js +357 -0
- package/src/app/ownership.js +57 -0
- package/src/app/rebase.js +216 -0
- package/src/app/restore-worktree.js +51 -0
- package/src/app/resume.js +179 -0
- package/src/app/run-state.js +26 -0
- package/src/app/settled.js +28 -0
- package/src/app/side-chat.js +370 -0
- package/src/app/sse.js +20 -0
- package/src/app/start-run.js +77 -0
- package/src/app/verify-push.js +114 -0
- package/src/app/worktree-setup.js +218 -0
- package/src/domain/acceptance-baseline.js +65 -0
- package/src/domain/acceptance-spec.js +30 -0
- package/src/domain/cleanup-policy.js +44 -0
- package/src/domain/command-outcome.js +6 -0
- package/src/domain/failure-class.js +63 -0
- package/src/domain/owned-changes.js +27 -0
- package/src/domain/prompt.js +161 -0
- package/src/domain/side-turn-view.js +73 -0
- package/src/domain/status.js +50 -0
- package/src/domain/thread-health.js +33 -0
- package/src/domain/truncate.js +148 -0
- package/src/domain/worker-branch.js +6 -0
- package/src/domain/worker-changes.js +58 -0
- package/src/domain/worker-env.js +14 -0
- package/src/domain/worker-preflight.js +167 -0
- package/src/domain/worker-report.js +85 -0
- package/src/domain/worker-report.schema.json +10 -0
- package/src/infra/bin-resolver.js +287 -0
- package/src/infra/cleanup-hint.js +29 -0
- package/src/infra/crash-guard.js +44 -0
- package/src/infra/external-output.js +29 -0
- package/src/infra/git.js +61 -0
- package/src/infra/handoff.js +38 -0
- package/src/infra/paths.js +34 -0
- package/src/infra/process.js +59 -0
- package/src/infra/sandbox-roots.js +106 -0
- package/src/infra/skill-sync.js +109 -0
- package/src/infra/stage-store.js +39 -0
- package/src/infra/store/db.js +265 -0
- package/src/infra/store/event-body.js +82 -0
- package/src/infra/store/events.js +149 -0
- package/src/infra/store/facts.js +338 -0
- package/src/infra/store/reconcile.js +173 -0
- package/src/infra/store/records.js +171 -0
- package/src/infra/store/runner-readers.js +16 -0
- package/src/infra/store/schema.js +306 -0
- package/src/infra/store/sessions.js +138 -0
- package/src/infra/store/tasks.js +351 -0
- package/src/infra/update/auto-update-worker.js +186 -0
- package/src/infra/update/auto-update.js +143 -0
- package/src/infra/update/update-check.js +246 -0
- package/src/infra/workspace-kind.js +165 -0
- package/src/infra/worktree.js +361 -0
- package/src/interface/bin/acceptance-cli.js +62 -0
- package/src/interface/bin/cleanup.js +81 -0
- package/src/interface/bin/monitor.jsx +301 -0
- package/src/interface/bin/setup.js +342 -0
- package/src/interface/bin/stage.js +17 -0
- package/src/interface/bin/web-server.js +53 -0
- package/src/interface/mcp/handlers/delivery.js +165 -0
- package/src/interface/mcp/handlers/dispatch.js +236 -0
- package/src/interface/mcp/handlers/facts.js +204 -0
- package/src/interface/mcp/handlers/overview.js +106 -0
- package/src/interface/mcp/handlers/results.js +262 -0
- package/src/interface/mcp/handlers/review.js +115 -0
- package/src/interface/mcp/handlers/wait.js +141 -0
- package/src/interface/mcp/meta.js +7 -0
- package/src/interface/mcp/result.js +34 -0
- package/src/interface/mcp/schemas.js +24 -0
- package/src/interface/mcp/server.js +64 -0
- package/src/interface/mcp/tools/deliver.js +48 -0
- package/src/interface/mcp/tools/dispatch.js +90 -0
- package/src/interface/mcp/tools/optional.js +57 -0
- package/src/interface/mcp/tools/read.js +115 -0
- package/src/interface/mcp/tools/run.js +57 -0
- package/src/interface/mcp/tools/shapes.js +67 -0
- package/src/interface/mcp/tools/track.js +84 -0
- package/src/interface/mcp/update-state.js +34 -0
- package/src/interface/mcp/views/overview.js +183 -0
- package/src/interface/mcp/views/task.js +224 -0
- package/src/interface/web/app.js +226 -0
- package/src/interface/web/views.js +227 -0
- package/src/mcp-server.js +4 -0
- package/src/run-host/entry.js +26 -0
- package/src/run-host/finalize.js +243 -0
- package/src/run-host/launch.js +49 -0
- package/src/run-host/live-run.js +76 -0
- package/src/run-host/outcome-facts.js +61 -0
- package/src/run-host/streams.js +102 -0
- package/src/run-host/track.js +132 -0
- package/src/runners/claude/adapter.js +118 -0
- package/src/runners/claude/events.js +100 -0
- package/src/runners/claude/tool-guard.js +48 -0
- package/src/runners/codex/adapter.js +157 -0
- package/src/runners/codex/chat-chunks.js +97 -0
- package/src/runners/codex/event-parser.js +266 -0
- package/src/runners/codex/goal-store.js +54 -0
- package/src/runners/codex/mcp-config.js +39 -0
- package/src/runners/codex/tool-guard.js +184 -0
- package/src/runners/contract-check.js +82 -0
- package/src/runners/contract.js +53 -0
- package/src/runners/registry.js +45 -0
- package/web/dist/assets/index-D-A2xlpx.js +154 -0
- package/web/dist/assets/index-f8esISCp.css +2 -0
- package/web/dist/index.html +15 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { realpathSync, existsSync } from "node:fs";
|
|
3
|
+
import { resolve, dirname, relative, isAbsolute } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
function canonical(path) {
|
|
7
|
+
const suffix = [];
|
|
8
|
+
let parent = resolve(path);
|
|
9
|
+
while (!existsSync(parent)) {
|
|
10
|
+
const next = dirname(parent);
|
|
11
|
+
if (next === parent) throw new Error("path has no existing ancestor");
|
|
12
|
+
suffix.unshift(relative(next, parent)); parent = next;
|
|
13
|
+
}
|
|
14
|
+
return resolve(realpathSync(parent), ...suffix);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function inside(path, owner) {
|
|
18
|
+
const part = relative(owner, path);
|
|
19
|
+
return !isAbsolute(part) && part !== ".." && !part.startsWith(`..${process.platform === "win32" ? "\\" : "/"}`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const PATCH_PATH = /^\*\*\* (?:Add File|Update File|Delete File|Move to): (.+)$/gm;
|
|
23
|
+
|
|
24
|
+
function patchPaths(patchText) {
|
|
25
|
+
return [...patchText.matchAll(PATCH_PATH)].map((match) => match[1]);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Codex 0.162 runs a patch through the shell tool when the model has no
|
|
29
|
+
// separate apply_patch tool: the call arrives as
|
|
30
|
+
//
|
|
31
|
+
// apply_patch <<'EOF'
|
|
32
|
+
// *** Begin Patch
|
|
33
|
+
// ...
|
|
34
|
+
// *** End Patch
|
|
35
|
+
// EOF
|
|
36
|
+
//
|
|
37
|
+
// Without this branch the guard denies it as an undeclared shell command and a
|
|
38
|
+
// worker on such a model can never edit a file. Only a quoted heredoc counts:
|
|
39
|
+
// an unquoted one would expand $() and backticks inside the body before the
|
|
40
|
+
// patch runs. Nothing may follow the closing delimiter, so an "EOF && rm -rf x"
|
|
41
|
+
// tail never qualifies.
|
|
42
|
+
const PATCH_HEREDOC = /^\s*(?:\S*\/)?apply_patch\s+<<-?\s*(['"])([A-Za-z_][A-Za-z0-9_]*)\1\s*\r?\n([\s\S]*?)\r?\n\2\s*$/;
|
|
43
|
+
|
|
44
|
+
function patchFromHeredoc(command) {
|
|
45
|
+
const match = command.match(PATCH_HEREDOC);
|
|
46
|
+
if (!match) return null;
|
|
47
|
+
const body = match[3];
|
|
48
|
+
return body.includes("*** Begin Patch") && body.includes("*** End Patch") ? body : null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// The one place a patch's targets are judged, shared by the native tool and the
|
|
52
|
+
// shell form so both enforce the same ownership.
|
|
53
|
+
function judgePatch(paths, { event, policy, readOnly, deny }) {
|
|
54
|
+
if (readOnly) return deny("read-only worker: the sandbox denies every write, including the temp directory; report the change instead of editing");
|
|
55
|
+
if (!paths.length) return deny("patch has no recognized file paths");
|
|
56
|
+
try {
|
|
57
|
+
// With no owned_paths the worker may still keep scratch notes, but only
|
|
58
|
+
// inside its own worktree, which is never landed.
|
|
59
|
+
const owners = policy.ownedPaths.length ? policy.ownedPaths.map(canonical) : [canonical(policy.worktree ?? policy.cwd)];
|
|
60
|
+
for (const path of paths) {
|
|
61
|
+
const target = canonical(resolve(event.cwd ?? policy.cwd, path));
|
|
62
|
+
if (!owners.some((owner) => inside(target, owner))) return deny(policy.ownedPaths.length
|
|
63
|
+
? "outside owned_paths: " + path
|
|
64
|
+
: "outside this worker's own worktree: " + path);
|
|
65
|
+
}
|
|
66
|
+
} catch (error) { return deny("cannot prove patch ownership: " + error.message); }
|
|
67
|
+
return { allow: true, reason: policy.ownedPaths.length ? "owned patch" : "scratch patch in the worker's own worktree" };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// A writing worker may still look around between edits. These are the safe
|
|
71
|
+
// reads; anything that can write (sed -i, find -delete/-exec, jq -i, tee,
|
|
72
|
+
// redirects) stays out. The guard is not a filesystem sandbox, so a miss only
|
|
73
|
+
// costs an unowned file inside the worker's own worktree, which land refuses.
|
|
74
|
+
const SIMPLE_READ = /^(?:pwd|ls|cat|head|tail|rg|grep|wc|sort|uniq|cut|tr|file|stat|du|df|date|which|basename|dirname|realpath|jq|sed|find|tree|nl|od|strings|shasum|sha256sum|cksum|awk)(?:\s|$)/;
|
|
75
|
+
const VERSION_PROBE = /^(?:go|node|npm|npx|java|javac|mvn|gradle|python3?|pip3?|rustc|cargo|dotnet|php|ruby|deno|bun|tsc|cmake|make|git)\s+(?:-v|--version|version|-h|--help)(?:\s|$)/;
|
|
76
|
+
const GIT_READ = /^git\s+(?:status|diff|show|log|rev-parse|rev-list|grep|branch|remote|tag|describe|ls-files|blame|shortlog|cat-file|name-rev|for-each-ref|version|config\s+--get|worktree\s+list)(?:\s|$)/;
|
|
77
|
+
|
|
78
|
+
// Shell syntax that can chain, redirect or execute. A bare $VAR is allowed:
|
|
79
|
+
// an expansion is not re-parsed as syntax, which is what lets awk programs
|
|
80
|
+
// such as {print $1} through, while $(...) and ${...} stay out.
|
|
81
|
+
const SHELL_SYNTAX = /[;&<>()`\n\r]|\$\(|\$\{/;
|
|
82
|
+
|
|
83
|
+
function readOnlySegment(segment) {
|
|
84
|
+
const text = segment.trim();
|
|
85
|
+
if (!text) return false;
|
|
86
|
+
if (/(?:^|\s)--(?:pre|ext-diff|output|exec)(?:=|\s|$)/.test(text)) return false;
|
|
87
|
+
if (/^git(?:\s|$)/.test(text)) return GIT_READ.test(text);
|
|
88
|
+
if (/^sed(?:\s|$)/.test(text)) return !/(?:^|\s)-i(?:\s|$)/.test(text);
|
|
89
|
+
if (/^find(?:\s|$)/.test(text)) return !/(?:^|\s)-(?:exec|execdir|ok|okdir|delete|fprint|fprintf|fls)(?:\s|$)/.test(text);
|
|
90
|
+
if (/^jq(?:\s|$)/.test(text)) return !/(?:^|\s)(?:-i|--in-place)(?:\s|$)/.test(text);
|
|
91
|
+
if (/^go(?:\s|$)/.test(text)) return /^go\s+(?:env|version)(?:\s|$)/.test(text);
|
|
92
|
+
return SIMPLE_READ.test(text) || VERSION_PROBE.test(text);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// A pipe only reads if every segment reads: "rg x | head" is fine, while
|
|
96
|
+
// "cat f | tee out" or "rg x | sh" is not. Any other metacharacter refuses too.
|
|
97
|
+
function isReadCommand(command) {
|
|
98
|
+
const text = command.trim();
|
|
99
|
+
if (SHELL_SYNTAX.test(text)) return false;
|
|
100
|
+
return text.split("|").every((segment) => readOnlySegment(segment));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
export function evaluateToolUse(event, policy) {
|
|
105
|
+
const deny = (reason) => ({ allow: false, reason });
|
|
106
|
+
if (!policy || !Array.isArray(policy.ownedPaths) || !policy.cwd) return deny("missing supervisor guard policy");
|
|
107
|
+
// Who needs the shell allowlist? It exists to stop a writing worker from
|
|
108
|
+
// going around owned_paths. Two shapes have no landable write boundary left:
|
|
109
|
+
// the read-only sandbox denies every file write, temp directory included, and
|
|
110
|
+
// a workspace-write worker with no owned_paths can only write its own throwaway
|
|
111
|
+
// worktree, its temp dir and the OS cache dir while land refuses everything it
|
|
112
|
+
// produced. Both are what a research worker looks like, and the allowlist would
|
|
113
|
+
// only stop it from running curl, gh or python.
|
|
114
|
+
// danger-full-access keeps the allowlist: there the sandbox is gone.
|
|
115
|
+
const landsNothing = policy.sandbox === "read-only"
|
|
116
|
+
|| (policy.sandbox === "workspace-write" && policy.ownedPaths.length === 0);
|
|
117
|
+
const readOnly = policy.sandbox === "read-only";
|
|
118
|
+
const tool = event.tool_name;
|
|
119
|
+
const input = event.tool_input;
|
|
120
|
+
const command = typeof input === "string" ? input : input?.command;
|
|
121
|
+
if (tool === "create_goal") {
|
|
122
|
+
const configured = policy.goalTokenBudget ?? null;
|
|
123
|
+
const supplied = input && typeof input === "object" && Object.hasOwn(input, "token_budget")
|
|
124
|
+
? input.token_budget
|
|
125
|
+
: undefined;
|
|
126
|
+
if (configured === null) {
|
|
127
|
+
if (supplied !== undefined) {
|
|
128
|
+
return deny("the dispatcher did not set a goal token budget; omit token_budget entirely (0, 1, and null are not unlimited)");
|
|
129
|
+
}
|
|
130
|
+
return { allow: true, reason: "goal created without a token budget" };
|
|
131
|
+
}
|
|
132
|
+
if (supplied !== configured) {
|
|
133
|
+
return deny(`the dispatcher set goal token_budget to ${configured}; create_goal must include exactly that value`);
|
|
134
|
+
}
|
|
135
|
+
return { allow: true, reason: `goal uses the dispatcher's token budget ${configured}` };
|
|
136
|
+
}
|
|
137
|
+
if (tool === "Bash") {
|
|
138
|
+
if (typeof command !== "string") return deny("invalid shell input");
|
|
139
|
+
const normalized = command.replace(/\\\r?\n/g, "").replace(/\s+/g, " ").trim();
|
|
140
|
+
for (const forbidden of policy.forbid ?? []) {
|
|
141
|
+
if (normalized.toLowerCase().includes(forbidden.toLowerCase())) return deny("forbidden command: " + forbidden);
|
|
142
|
+
}
|
|
143
|
+
// The patch is judged before the shapes below: a read-only worker gets a
|
|
144
|
+
// clearer refusal than the OS one, and a no-land worker still cannot patch
|
|
145
|
+
// outside its own worktree.
|
|
146
|
+
const shellPatch = patchFromHeredoc(command);
|
|
147
|
+
if (shellPatch) return judgePatch(patchPaths(shellPatch), { event, policy, readOnly, deny });
|
|
148
|
+
// Reading is never exclusive: no branch below looks at another worker's
|
|
149
|
+
// owned_paths, so two workers may read the same file at the same time.
|
|
150
|
+
if (landsNothing) return { allow: true, reason: readOnly
|
|
151
|
+
? "read-only sandbox: every write is denied by the OS, so shell commands need no allowlist"
|
|
152
|
+
: "no landable path: writes stay in this worker's own worktree" };
|
|
153
|
+
// Validation lines are a dispatcher's explicit executable contract, just
|
|
154
|
+
// like acceptance. They can have side effects and are never inferred from the
|
|
155
|
+
// model's own report. Arbitrary shell programs are not path sandboxes.
|
|
156
|
+
if ((policy.allowedCommands ?? []).some((entry) => entry.trim() === command.trim())) return { allow: true, reason: "declared validation command" };
|
|
157
|
+
// A declaration ending in * is a prefix pattern, so a declared
|
|
158
|
+
// "go test ./internal/*" covers "go test ./internal/service -run TestX".
|
|
159
|
+
// Only a single command with no metacharacters: a prefix must not smuggle
|
|
160
|
+
// in a second command behind a semicolon.
|
|
161
|
+
if (!SHELL_SYNTAX.test(command)) {
|
|
162
|
+
const prefixes = (policy.allowedCommands ?? []).map((entry) => entry.trim()).filter((entry) => entry.endsWith("*")).map((entry) => entry.slice(0, -1));
|
|
163
|
+
if (prefixes.some((prefix) => prefix && command.trim().startsWith(prefix))) return { allow: true, reason: "declared command prefix" };
|
|
164
|
+
}
|
|
165
|
+
if (isReadCommand(command)) return { allow: true, reason: "read command" };
|
|
166
|
+
return deny("undeclared shell command; edit with apply_patch (the tool, or apply_patch <<'EOF' ... EOF in the shell) or declare the command");
|
|
167
|
+
}
|
|
168
|
+
if (tool === "apply_patch") {
|
|
169
|
+
if (typeof command !== "string" || !command.startsWith("*** Begin Patch")) return deny("invalid patch input");
|
|
170
|
+
return judgePatch(patchPaths(command), { event, policy, readOnly, deny });
|
|
171
|
+
}
|
|
172
|
+
return { allow: true, reason: "tool is outside the Bash/apply_patch policy" };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
|
176
|
+
let input = "";
|
|
177
|
+
for await (const chunk of process.stdin) input += chunk;
|
|
178
|
+
let decision;
|
|
179
|
+
try { decision = evaluateToolUse(JSON.parse(input), JSON.parse(process.env.AGENT_SUPERVISOR_GUARD_POLICY ?? "null")); }
|
|
180
|
+
catch (error) { decision = { allow: false, reason: "invalid supervisor guard input: " + error.message }; }
|
|
181
|
+
process.stdout.write(JSON.stringify({ hookSpecificOutput: { hookEventName: "PreToolUse",
|
|
182
|
+
permissionDecision: decision.allow ? "allow" : "deny", permissionDecisionReason: decision.reason } }) + "\n");
|
|
183
|
+
}
|
|
184
|
+
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// The checks every runner has to pass, built in or not. It needs no test
|
|
2
|
+
// framework: give it the runner and a transcript (lines the CLI printed for
|
|
3
|
+
// one finished run) and it returns the problems it found, none when the
|
|
4
|
+
// runner is sound. docs/runner.md shows it inside a test.
|
|
5
|
+
import { assertRunner, CAPABILITIES } from "./contract.js";
|
|
6
|
+
|
|
7
|
+
const EVENT_TYPES = new Set(["thread.started", "turn.started", "turn.completed", "turn.failed", "item.started", "item.updated", "item.completed", "error"]);
|
|
8
|
+
const isStrings = (value) => Array.isArray(value) && value.every((entry) => typeof entry === "string");
|
|
9
|
+
|
|
10
|
+
function checkArgs(runner, problems) {
|
|
11
|
+
const start = runner.startArgs({ cwd: process.cwd(), sandbox: "workspace-write", model: null, reasoningEffort: "high", skipGitRepoCheck: true });
|
|
12
|
+
if (!isStrings(start)) problems.push("startArgs() must return an array of strings");
|
|
13
|
+
const record = { id: "contract-1", cwd: process.cwd(), sandbox: "workspace-write", guard_policy: {} };
|
|
14
|
+
for (const outputSchema of [true, false]) {
|
|
15
|
+
const launch = runner.launchArgs({ args: isStrings(start) ? start : [], record, outputSchema, writableRoots: [] });
|
|
16
|
+
if (!isStrings(launch)) problems.push(`launchArgs({ outputSchema: ${outputSchema} }) must return an array of strings`);
|
|
17
|
+
}
|
|
18
|
+
if (runner.capabilities.resume) {
|
|
19
|
+
const resume = runner.resumeArgs({ ...record, thread_id: "thread-1", model: null, reasoning_effort: "high" });
|
|
20
|
+
if (!isStrings(resume)) problems.push("resumeArgs() must return an array of strings");
|
|
21
|
+
else if (!resume.includes("thread-1")) problems.push("resumeArgs() must pass the stored thread_id to the CLI");
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function checkTranscript(runner, transcript, problems) {
|
|
26
|
+
const events = [];
|
|
27
|
+
for (const line of transcript) {
|
|
28
|
+
let produced;
|
|
29
|
+
try {
|
|
30
|
+
produced = runner.toEvents(line);
|
|
31
|
+
} catch (error) {
|
|
32
|
+
problems.push(`toEvents() threw on a transcript line: ${error.message}`);
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (!Array.isArray(produced)) problems.push("toEvents() must return an array");
|
|
36
|
+
else events.push(...produced);
|
|
37
|
+
}
|
|
38
|
+
for (const event of events) {
|
|
39
|
+
if (!EVENT_TYPES.has(event?.type)) problems.push(`toEvents() produced an event outside the vocabulary: ${JSON.stringify(event).slice(0, 120)}`);
|
|
40
|
+
}
|
|
41
|
+
const started = events.find((event) => event.type === "thread.started");
|
|
42
|
+
if (!started?.thread_id) problems.push("the transcript must yield thread.started with a thread_id");
|
|
43
|
+
if (!["turn.completed", "turn.failed"].includes(events.at(-1)?.type)) problems.push("the transcript must end in turn.completed or turn.failed");
|
|
44
|
+
if (!events.some((event) => event.type === "item.completed" && event.item?.type === "agent_message" && typeof event.item.text === "string")) {
|
|
45
|
+
problems.push("the transcript must yield the worker's final message as an agent_message item");
|
|
46
|
+
}
|
|
47
|
+
let task = { id: "contract-1", status: "queued", commands: [], changed_files: [] };
|
|
48
|
+
try {
|
|
49
|
+
for (const event of events) task = runner.parseEvent(task, event);
|
|
50
|
+
if (started?.thread_id && task.thread_id !== started.thread_id) problems.push("parseEvent() must record the thread_id");
|
|
51
|
+
if (events.at(-1)?.type === "turn.completed" && task.status !== "completed") problems.push("parseEvent() must end a completed turn in status completed");
|
|
52
|
+
} catch (error) {
|
|
53
|
+
problems.push(`parseEvent() threw: ${error.message}`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function checkRunner(runner, { transcript = [] } = {}) {
|
|
58
|
+
try {
|
|
59
|
+
assertRunner(runner);
|
|
60
|
+
} catch (error) {
|
|
61
|
+
return [error.message];
|
|
62
|
+
}
|
|
63
|
+
const problems = [];
|
|
64
|
+
try {
|
|
65
|
+
checkArgs(runner, problems);
|
|
66
|
+
} catch (error) {
|
|
67
|
+
problems.push(`building arguments threw: ${error.message}`);
|
|
68
|
+
}
|
|
69
|
+
let threw = false;
|
|
70
|
+
try {
|
|
71
|
+
runner.toEvents("this is not what the CLI prints {");
|
|
72
|
+
} catch {
|
|
73
|
+
threw = true;
|
|
74
|
+
}
|
|
75
|
+
// A line that is not an event is reported by the core; swallowing it would hide a broken CLI.
|
|
76
|
+
if (!threw) problems.push("toEvents() must throw on a line it cannot read");
|
|
77
|
+
if (transcript.length > 0) checkTranscript(runner, transcript, problems);
|
|
78
|
+
else problems.push("give a transcript: the lines the CLI printed for one finished run");
|
|
79
|
+
return problems;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export { CAPABILITIES };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// What a runner is. A runner is the only code that knows one CLI: its
|
|
2
|
+
// arguments, its output, how it is stopped. The core asks for these members
|
|
3
|
+
// and nothing else, and reads `capabilities` before using an optional one.
|
|
4
|
+
//
|
|
5
|
+
// Events. Whatever the CLI prints, `toEvents(line)` returns events in the
|
|
6
|
+
// supervisor's vocabulary, which is what is stored and what every reader
|
|
7
|
+
// understands:
|
|
8
|
+
//
|
|
9
|
+
// { type: "thread.started", thread_id }
|
|
10
|
+
// { type: "turn.started" }
|
|
11
|
+
// { type: "item.started" | "item.completed", item: { id, type, ... } }
|
|
12
|
+
// item.type "agent_message" { text }
|
|
13
|
+
// item.type "command_execution" { command, aggregated_output, exit_code, status }
|
|
14
|
+
// item.type "file_change" { changes: [{ path, kind }] }
|
|
15
|
+
// { type: "turn.completed", usage? }
|
|
16
|
+
// { type: "turn.failed", error: { message } }
|
|
17
|
+
//
|
|
18
|
+
// docs/runner.md walks through writing one.
|
|
19
|
+
export const CAPABILITIES = ["os_sandbox", "resume", "side_chat", "tool_guard", "structured_report", "native_goal"];
|
|
20
|
+
|
|
21
|
+
const REQUIRED = ["resolveTarget", "capabilityPreflight", "startArgs", "launchArgs", "toEvents", "parseEvent", "cancelChild", "cancelPid", "ownsPid"];
|
|
22
|
+
// Members a runner must have once it declares the capability.
|
|
23
|
+
const BY_CAPABILITY = { resume: ["resumeArgs"], side_chat: ["sideChat", "start"], native_goal: ["nativeGoal", "nativeGoalState"] };
|
|
24
|
+
|
|
25
|
+
// Throws with every problem at once, so a runner author fixes them in one pass.
|
|
26
|
+
export function assertRunner(runner, source = "runner") {
|
|
27
|
+
const problems = [];
|
|
28
|
+
if (!runner || typeof runner !== "object") throw new Error(`${source} does not export a runner object`);
|
|
29
|
+
if (typeof runner.name !== "string" || !/^[a-z][a-z0-9-]{0,31}$/.test(runner.name)) problems.push("name must be a short lowercase identifier");
|
|
30
|
+
const capabilities = runner.capabilities ?? {};
|
|
31
|
+
for (const key of CAPABILITIES) if (typeof capabilities[key] !== "boolean") problems.push(`capabilities.${key} must be true or false`);
|
|
32
|
+
for (const key of Object.keys(capabilities)) if (!CAPABILITIES.includes(key)) problems.push(`capabilities.${key} is not a known capability`);
|
|
33
|
+
for (const member of REQUIRED) if (typeof runner[member] !== "function") problems.push(`${member}() is missing`);
|
|
34
|
+
for (const [capability, members] of Object.entries(BY_CAPABILITY)) {
|
|
35
|
+
if (capabilities[capability] !== true) continue;
|
|
36
|
+
for (const member of members) if (runner[member] == null) problems.push(`${member} is required by capabilities.${capability}`);
|
|
37
|
+
}
|
|
38
|
+
if (problems.length > 0) throw new Error(`${source} is not a valid runner: ${problems.join("; ")}`);
|
|
39
|
+
return runner;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// The error a caller gets for asking a runner for something it cannot do.
|
|
43
|
+
export function unsupported(runner, capability, what) {
|
|
44
|
+
return Object.assign(new Error(`${what} is not supported by the ${runner.name} runner (no ${capability})`), { code: "unsupported", capability, runner: runner.name });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Without an OS sandbox nothing but the worktree confines what a worker
|
|
48
|
+
// writes, so such a runner gets a worktree or a read-only run and nothing else.
|
|
49
|
+
export function assertWorkspaceAllowed(runnerName, confined, workspaceKind) {
|
|
50
|
+
if (confined !== false || ["git_worktree", "read_only_in_place"].includes(workspaceKind)) return;
|
|
51
|
+
throw Object.assign(new Error(`the ${runnerName} runner has no OS sandbox, so it can only run in a git_worktree or read_only_in_place workspace, not ${workspaceKind}`),
|
|
52
|
+
{ code: "unsupported", capability: "os_sandbox", runner: runnerName });
|
|
53
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// Finds a runner by name. The two built in are always there; any other is a
|
|
2
|
+
// package named agent-supervisor-runner-<name>, or a file named by
|
|
3
|
+
// AGENT_SUPERVISOR_RUNNER_<NAME>, loaded the first time it is asked for.
|
|
4
|
+
import { assertRunner } from "./contract.js";
|
|
5
|
+
import { claudeRunner } from "./claude/adapter.js";
|
|
6
|
+
import { codexRunner } from "./codex/adapter.js";
|
|
7
|
+
import { pathToFileURL } from "node:url";
|
|
8
|
+
|
|
9
|
+
export const DEFAULT_RUNNER = "codex";
|
|
10
|
+
const runners = new Map([[codexRunner.name, assertRunner(codexRunner, "codex")], [claudeRunner.name, assertRunner(claudeRunner, "claude")]]);
|
|
11
|
+
|
|
12
|
+
export const runnerNames = () => [...runners.keys()];
|
|
13
|
+
|
|
14
|
+
// For code that already has a task: the runner was loaded when it was dispatched
|
|
15
|
+
// in this process, or is loaded by loadRunner at the entry that reads the task.
|
|
16
|
+
export function getRunner(name = DEFAULT_RUNNER) {
|
|
17
|
+
const runner = runners.get(name ?? DEFAULT_RUNNER);
|
|
18
|
+
if (!runner) throw Object.assign(new Error(`runner "${name}" is not loaded`), { code: "runner_not_loaded" });
|
|
19
|
+
return runner;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const runnerOf = (task) => getRunner(task?.worker ?? DEFAULT_RUNNER);
|
|
23
|
+
|
|
24
|
+
function externalSource(name) {
|
|
25
|
+
const fromEnv = process.env[`AGENT_SUPERVISOR_RUNNER_${name.toUpperCase().replace(/-/g, "_")}`]?.trim();
|
|
26
|
+
return fromEnv ? { specifier: pathToFileURL(fromEnv).href, label: fromEnv } : { specifier: `agent-supervisor-runner-${name}`, label: `agent-supervisor-runner-${name}` };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function loadRunner(name = DEFAULT_RUNNER) {
|
|
30
|
+
const wanted = name ?? DEFAULT_RUNNER;
|
|
31
|
+
if (runners.has(wanted)) return runners.get(wanted);
|
|
32
|
+
if (!/^[a-z][a-z0-9-]{0,31}$/.test(wanted)) throw Object.assign(new Error(`"${wanted}" is not a runner name`), { code: "runner_unknown" });
|
|
33
|
+
const { specifier, label } = externalSource(wanted);
|
|
34
|
+
let loaded;
|
|
35
|
+
try {
|
|
36
|
+
loaded = await import(specifier);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
throw Object.assign(new Error(`runner "${wanted}" is not available: could not load ${label} (${error.message.split("\n")[0]}). Built in: ${runnerNames().join(", ")}`),
|
|
39
|
+
{ code: "runner_unknown" });
|
|
40
|
+
}
|
|
41
|
+
const runner = assertRunner(loaded.runner ?? loaded.default, label);
|
|
42
|
+
if (runner.name !== wanted) throw Object.assign(new Error(`${label} exports a runner named "${runner.name}", not "${wanted}"`), { code: "runner_unknown" });
|
|
43
|
+
runners.set(wanted, runner);
|
|
44
|
+
return runner;
|
|
45
|
+
}
|