codex-supervisor-mcp 1.1.0 → 1.2.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.
@@ -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
+ }