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,157 @@
|
|
|
1
|
+
import { runSync, spawnProcess } from "../../infra/process.js";
|
|
2
|
+
import { defaultBinDirs, findBinaryPath, resolveCommand, shimMessage } from "../../infra/bin-resolver.js";
|
|
3
|
+
import { workerReportSchemaPath } from "../../domain/worker-report.js";
|
|
4
|
+
import { chunksFor } from "./chat-chunks.js";
|
|
5
|
+
import { applyCodexEvent } from "./event-parser.js";
|
|
6
|
+
import { readNativeGoal, readNativeGoalState } from "./goal-store.js";
|
|
7
|
+
import { codexConfigText, disableMcpServerArgs, supervisorMcpDisableArgs } from "./mcp-config.js";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
|
|
10
|
+
const quoteCommand = (path) => process.platform === "win32" ? `"${path.replace(/"/g, '""')}"` : `'${path.replace(/'/g, "'\\''")}'`;
|
|
11
|
+
|
|
12
|
+
const CODEX_NPM_ENTRY = { pkg: "@openai/codex", bin: "bin/codex.js" };
|
|
13
|
+
|
|
14
|
+
export class CodexRunnerAdapter {
|
|
15
|
+
constructor({ env = process.env } = {}) {
|
|
16
|
+
this.env = env;
|
|
17
|
+
this.name = "codex";
|
|
18
|
+
this.capabilities = { os_sandbox: true, resume: true, side_chat: true, tool_guard: true, structured_report: true, native_goal: true };
|
|
19
|
+
// The read-only fork a side question runs in.
|
|
20
|
+
this.sideChat = {
|
|
21
|
+
forkArgs: (options) => this.forkSideArgs(options),
|
|
22
|
+
// Config overrides go before the thread id and the stdin marker.
|
|
23
|
+
withConfig: (args, configArgs) => [...args.slice(0, -2), ...configArgs, ...args.slice(-2)],
|
|
24
|
+
configText: codexConfigText,
|
|
25
|
+
disableMcpServerArgs,
|
|
26
|
+
chunksFor,
|
|
27
|
+
deleteArgs: (threadId) => ["delete", "--force", threadId]
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Adds what installs the guard hook, the report schema and the sandbox
|
|
32
|
+
// settings to the arguments of a start or a resume.
|
|
33
|
+
launchArgs({ args, record, outputSchema, writableRoots }) {
|
|
34
|
+
const guard = fileURLToPath(new URL("./tool-guard.js", import.meta.url));
|
|
35
|
+
const hookCommand = `${quoteCommand(process.execPath)} ${quoteCommand(guard)}`;
|
|
36
|
+
const configArgs = [...(outputSchema ? ["--output-schema", workerReportSchemaPath] : []), "--dangerously-bypass-hook-trust", "-c",
|
|
37
|
+
`hooks.PreToolUse=[{matcher="Bash|apply_patch|create_goal",hooks=[{type="command",command=${JSON.stringify(hookCommand)}}]}]`];
|
|
38
|
+
// A worker may use its explicitly configured business MCP servers, but it
|
|
39
|
+
// must not inherit this supervisor and create a second orchestration tree.
|
|
40
|
+
configArgs.push(...supervisorMcpDisableArgs());
|
|
41
|
+
configArgs.push("-c", `sandbox_workspace_write.network_access=${record.guard_policy?.networkAccess === true}`);
|
|
42
|
+
if (writableRoots.length) configArgs.push("-c", `sandbox_workspace_write.writable_roots=${JSON.stringify(writableRoots)}`);
|
|
43
|
+
// Before the prompt/session positional argument that ends exec and resume.
|
|
44
|
+
return [...args.slice(0, -1), ...configArgs, args.at(-1)];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Codex already prints the supervisor's event vocabulary.
|
|
48
|
+
toEvents(line) {
|
|
49
|
+
return [JSON.parse(line)];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
nativeGoal(threadId) {
|
|
53
|
+
return readNativeGoal(threadId);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
nativeGoalState(threadId) {
|
|
57
|
+
return readNativeGoalState(threadId);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Only a thread with no native goal, or one explicitly stored as complete,
|
|
61
|
+
// can safely resume with a structured output schema. Paused, blocked,
|
|
62
|
+
// usage-limited and budget-limited goals are not complete; Codex can still
|
|
63
|
+
// activate their goal turn and reject the incompatible schema.
|
|
64
|
+
resumeAllowsOutputSchema(threadId) {
|
|
65
|
+
const goalRead = readNativeGoalState(threadId);
|
|
66
|
+
return ["missing_goal", "missing_database"].includes(goalRead.state)
|
|
67
|
+
|| (goalRead.state === "available" && goalRead.goal?.storage_status === "complete");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
resolveTarget() {
|
|
71
|
+
const configured = this.env.CODEX_BIN?.trim();
|
|
72
|
+
const located = configured ? configured : (findBinaryPath("codex", { extraDirs: defaultBinDirs() }) ?? "codex");
|
|
73
|
+
const target = resolveCommand(located, { npmEntry: CODEX_NPM_ENTRY });
|
|
74
|
+
if (target.shim) return { error: shimMessage("codex", target.cmd, "CODEX_BIN") };
|
|
75
|
+
return { cmd: target.cmd, prefixArgs: target.prefixArgs };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
capabilityPreflight(target = this.resolveTarget()) {
|
|
79
|
+
if (target.error) return { ok: false, error: target.error };
|
|
80
|
+
try {
|
|
81
|
+
const help = runSync(target.cmd, [...target.prefixArgs, "exec", "--help"], {
|
|
82
|
+
encoding: "utf8",
|
|
83
|
+
timeout: 15000,
|
|
84
|
+
windowsHide: true,
|
|
85
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
86
|
+
});
|
|
87
|
+
if (!help.includes("--dangerously-bypass-hook-trust") || !help.includes("--output-schema")) {
|
|
88
|
+
return { ok: false, error: "Codex CLI lacks required per-run hooks/output-schema; install Codex 0.162 or newer before dispatch" };
|
|
89
|
+
}
|
|
90
|
+
return { ok: true, target };
|
|
91
|
+
} catch (error) {
|
|
92
|
+
return { ok: false, error: error.code === "ENOENT"
|
|
93
|
+
? `failed to spawn ${target.cmd} for Codex capability preflight: ${error.message}`
|
|
94
|
+
: `Codex capability preflight failed: ${error.message}` };
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
startArgs({ cwd, sandbox, model, reasoningEffort, skipGitRepoCheck }) {
|
|
99
|
+
const args = ["exec", "--json", "--cd", cwd, "--sandbox", sandbox];
|
|
100
|
+
if (skipGitRepoCheck) args.push("--skip-git-repo-check");
|
|
101
|
+
if (model) args.push("--model", model);
|
|
102
|
+
args.push("-c", `model_reasoning_effort="${reasoningEffort}"`, "-");
|
|
103
|
+
return args;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
resumeArgs(task) {
|
|
107
|
+
const args = ["exec", "resume", "--json", "-c", `sandbox_mode="${task.sandbox}"`];
|
|
108
|
+
if (task.model) args.push("--model", task.model);
|
|
109
|
+
args.push("-c", `model_reasoning_effort="${task.reasoning_effort ?? "high"}"`);
|
|
110
|
+
if (task.skip_git_repo_check) args.push("--skip-git-repo-check");
|
|
111
|
+
args.push(task.thread_id, "-");
|
|
112
|
+
return args;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
forkSideArgs({ forkThreadId, workerThreadId, model }) {
|
|
116
|
+
const args = forkThreadId
|
|
117
|
+
? ["exec", "resume", "--json", "--skip-git-repo-check", "-c", 'sandbox_mode="read-only"']
|
|
118
|
+
: ["exec", "fork", "--json", "--skip-git-repo-check", "-c", 'sandbox_mode="read-only"'];
|
|
119
|
+
if (model) args.push("-m", model);
|
|
120
|
+
args.push(forkThreadId ?? workerThreadId, "-");
|
|
121
|
+
return args;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
start(target, args, options) {
|
|
125
|
+
return spawnProcess(target.cmd, [...target.prefixArgs, ...args], options);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
parseEvent(task, event) {
|
|
129
|
+
return applyCodexEvent(task, event);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
cancelChild(child) {
|
|
133
|
+
if (process.platform === "win32" && child.pid) {
|
|
134
|
+
try {
|
|
135
|
+
runSync("taskkill", ["/PID", String(child.pid), "/T", "/F"], { stdio: "ignore", timeout: 5000, windowsHide: true });
|
|
136
|
+
return;
|
|
137
|
+
} catch {
|
|
138
|
+
// fall through to the process handle
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
child.kill("SIGTERM");
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
cancelPid(pid) {
|
|
145
|
+
if (process.platform === "win32") {
|
|
146
|
+
runSync("taskkill", ["/PID", String(pid), "/T", "/F"], { stdio: "ignore", timeout: 5000, windowsHide: true });
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
process.kill(pid, "SIGTERM");
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
ownsPid(pid, describeProcess) {
|
|
153
|
+
return describeProcess(pid).includes("codex");
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export const codexRunner = new CodexRunnerAdapter();
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// Codex events -> the UI message stream chunks the board's side chat renders.
|
|
2
|
+
|
|
3
|
+
const OUTPUT_CLIP = 4000;
|
|
4
|
+
|
|
5
|
+
function clip(text, max = OUTPUT_CLIP) {
|
|
6
|
+
if (typeof text !== "string") return text ?? null;
|
|
7
|
+
return text.length <= max ? text : `${text.slice(0, max)}\n…(${text.length - max} more characters)`;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function completedItemChunks(item, state) {
|
|
11
|
+
const out = [];
|
|
12
|
+
switch (item.type) {
|
|
13
|
+
case "reasoning": {
|
|
14
|
+
const id = `r-${item.id}`;
|
|
15
|
+
out.push({ type: "reasoning-start", id }, { type: "reasoning-delta", id, delta: item.text ?? "" }, { type: "reasoning-end", id });
|
|
16
|
+
break;
|
|
17
|
+
}
|
|
18
|
+
case "agent_message": {
|
|
19
|
+
const id = `t-${item.id}`;
|
|
20
|
+
out.push({ type: "text-start", id }, { type: "text-delta", id, delta: item.text ?? "" }, { type: "text-end", id });
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
case "command_execution":
|
|
24
|
+
if (!state.toolsSeen.has(item.id)) {
|
|
25
|
+
state.toolsSeen.add(item.id);
|
|
26
|
+
out.push({ type: "tool-input-available", toolCallId: item.id, toolName: "shell", input: { command: item.command ?? "" } });
|
|
27
|
+
}
|
|
28
|
+
out.push({
|
|
29
|
+
type: "tool-output-available",
|
|
30
|
+
toolCallId: item.id,
|
|
31
|
+
output: { exit_code: item.exit_code ?? null, status: item.status ?? null, output: clip(item.aggregated_output ?? "") }
|
|
32
|
+
});
|
|
33
|
+
break;
|
|
34
|
+
case "file_change":
|
|
35
|
+
out.push(
|
|
36
|
+
{ type: "tool-input-available", toolCallId: item.id, toolName: "edit", input: { changes: item.changes ?? [] } },
|
|
37
|
+
{ type: "tool-output-available", toolCallId: item.id, output: { status: item.status ?? "completed" } }
|
|
38
|
+
);
|
|
39
|
+
break;
|
|
40
|
+
case "web_search":
|
|
41
|
+
case "mcp_tool_call":
|
|
42
|
+
out.push(
|
|
43
|
+
{ type: "tool-input-available", toolCallId: item.id, toolName: item.type, input: { ...item, id: undefined, type: undefined } },
|
|
44
|
+
{ type: "tool-output-available", toolCallId: item.id, output: { status: item.status ?? "completed" } }
|
|
45
|
+
);
|
|
46
|
+
break;
|
|
47
|
+
case "error":
|
|
48
|
+
// Startup notices (unknown config keys, missing model metadata) come
|
|
49
|
+
// through as error items. They are not the answer failing.
|
|
50
|
+
out.push({ type: "data-notice", data: { message: item.message ?? item.text ?? "" } });
|
|
51
|
+
break;
|
|
52
|
+
default:
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function chunksFor(event, state) {
|
|
59
|
+
const item = event.item ?? {};
|
|
60
|
+
const out = [];
|
|
61
|
+
switch (event.type) {
|
|
62
|
+
case "thread.started":
|
|
63
|
+
if (event.thread_id) out.push({ type: "data-session", data: { fork_thread_id: event.thread_id } });
|
|
64
|
+
break;
|
|
65
|
+
case "turn.started":
|
|
66
|
+
out.push({ type: "start-step" });
|
|
67
|
+
break;
|
|
68
|
+
case "item.started":
|
|
69
|
+
case "item.updated":
|
|
70
|
+
if (item.type === "command_execution" && !state.toolsSeen.has(item.id)) {
|
|
71
|
+
state.toolsSeen.add(item.id);
|
|
72
|
+
out.push({ type: "tool-input-available", toolCallId: item.id, toolName: "shell", input: { command: item.command ?? "" } });
|
|
73
|
+
}
|
|
74
|
+
break;
|
|
75
|
+
case "item.completed":
|
|
76
|
+
out.push(...completedItemChunks(item, state));
|
|
77
|
+
break;
|
|
78
|
+
case "turn.completed":
|
|
79
|
+
out.push({ type: "finish-step" });
|
|
80
|
+
if (event.usage) {
|
|
81
|
+
out.push({ type: "data-usage", data: event.usage });
|
|
82
|
+
state.usage = event.usage;
|
|
83
|
+
}
|
|
84
|
+
state.completed = true;
|
|
85
|
+
break;
|
|
86
|
+
case "turn.failed":
|
|
87
|
+
out.push({ type: "error", errorText: event.error?.message ?? event.error ?? event.message ?? "Codex turn failed" });
|
|
88
|
+
state.failed = true;
|
|
89
|
+
break;
|
|
90
|
+
case "error":
|
|
91
|
+
out.push({ type: "data-notice", data: { message: event.message ?? event.error ?? "" } });
|
|
92
|
+
break;
|
|
93
|
+
default:
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
// Maps the Codex exec JSONL stream onto the work state machine.
|
|
2
|
+
//
|
|
3
|
+
// Invariant: status is the lifecycle and is only ever assigned queued /
|
|
4
|
+
// running / completed / failed / cancelled / lost. Progress details go to
|
|
5
|
+
// phase, which is only meaningful while the work is running.
|
|
6
|
+
import { commandFailed } from "../../domain/command-outcome.js";
|
|
7
|
+
import { parseWorkerReport } from "../../domain/worker-report.js";
|
|
8
|
+
|
|
9
|
+
function getEventType(event) {
|
|
10
|
+
return event.type ?? event.event ?? event.msg?.type ?? "unknown";
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function getItem(event) {
|
|
14
|
+
return event.item ?? event.msg?.item ?? event.data?.item ?? null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function inferCommand(item) {
|
|
18
|
+
return item?.command ?? item?.cmd ?? item?.arguments?.cmd ?? item?.text ?? null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function inferAgentMessage(item) {
|
|
22
|
+
const raw = item?.text ?? item?.message ?? item?.content ?? item?.output_text ?? null;
|
|
23
|
+
const parsed = parseWorkerReport(raw);
|
|
24
|
+
return parsed.valid ? parsed.report.result : raw;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// The stored event stream is raw Codex JSONL, so reading a worker's own report
|
|
28
|
+
// back out means walking `item.completed` events whose item is an
|
|
29
|
+
// agent_message. The live state machine and the on-demand result reader share
|
|
30
|
+
// this one extraction.
|
|
31
|
+
export function extractAgentMessage(event) {
|
|
32
|
+
const item = getItem(event);
|
|
33
|
+
const itemType = item?.type ?? item?.kind ?? item?.name ?? null;
|
|
34
|
+
if (itemType !== "agent_message") return null;
|
|
35
|
+
return inferAgentMessage(item);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Codex reports edits as {"changes":[{"path": "...", "kind": "add"}]}. The
|
|
39
|
+
// older flat shapes are kept for compatibility with other event producers.
|
|
40
|
+
function inferChangedFiles(item) {
|
|
41
|
+
const candidates = [item?.path, item?.file, item?.file_path, item?.filename].filter(Boolean);
|
|
42
|
+
if (Array.isArray(item?.changes)) {
|
|
43
|
+
for (const change of item.changes) {
|
|
44
|
+
const path = change?.path ?? change?.file;
|
|
45
|
+
if (path) candidates.push(path);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (Array.isArray(item?.files)) candidates.push(...item.files);
|
|
49
|
+
return candidates;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const ITEM_PHASE = new Map([
|
|
53
|
+
["command_execution", "command"],
|
|
54
|
+
["file_change", "editing"],
|
|
55
|
+
["agent_message", "reporting"],
|
|
56
|
+
["reasoning", "thinking"],
|
|
57
|
+
["todo_list", "thinking"],
|
|
58
|
+
["mcp_tool_call", "command"]
|
|
59
|
+
]);
|
|
60
|
+
|
|
61
|
+
// Codex emits informational items as `item.type === "error"` (config warnings,
|
|
62
|
+
// "long threads drift" heads-ups). They are not task failures, so they are
|
|
63
|
+
// recorded as notices instead of setting the error field.
|
|
64
|
+
function appendNotice(task, message) {
|
|
65
|
+
if (!message) return task;
|
|
66
|
+
const existing = task.notices ? task.notices.split("\n") : [];
|
|
67
|
+
if (existing.includes(message)) return task;
|
|
68
|
+
const combined = [...existing, message].join("\n");
|
|
69
|
+
return { ...task, notices: combined.length > 4000 ? combined.slice(-4000) : combined };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const GOAL_EVENT_TYPES = new Set(["thread.goal.updated", "thread_goal_updated", "goal.updated"]);
|
|
73
|
+
|
|
74
|
+
function applyGoalEvent(next, event) {
|
|
75
|
+
const goal = event.goal ?? event.msg?.goal ?? event.data?.goal ?? event;
|
|
76
|
+
if (!goal) return next;
|
|
77
|
+
if (goal.status) next.goal_status = goal.status;
|
|
78
|
+
if (goal.tokens_used ?? goal.tokensUsed) next.goal_tokens_used = goal.tokens_used ?? goal.tokensUsed;
|
|
79
|
+
if (goal.token_budget ?? goal.tokenBudget) next.goal_token_budget = goal.token_budget ?? goal.tokenBudget;
|
|
80
|
+
if (goal.time_used_seconds ?? goal.timeUsedSeconds) {
|
|
81
|
+
next.goal_time_used_seconds = goal.time_used_seconds ?? goal.timeUsedSeconds;
|
|
82
|
+
}
|
|
83
|
+
if (goal.objective) next.goal_objective = goal.objective;
|
|
84
|
+
next.goal_updated_at = new Date().toISOString();
|
|
85
|
+
return next;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function applyItemProgress(next, item, itemType) {
|
|
89
|
+
next.status = "running";
|
|
90
|
+
next.phase = ITEM_PHASE.get(itemType);
|
|
91
|
+
if (itemType === "command_execution") {
|
|
92
|
+
next.current_command = inferCommand(item);
|
|
93
|
+
next.current_action = next.current_command ? `Running ${next.current_command}` : "Running command";
|
|
94
|
+
} else if (itemType === "file_change") {
|
|
95
|
+
next.current_action = "Editing files";
|
|
96
|
+
} else if (itemType !== "reasoning" && itemType !== "todo_list") {
|
|
97
|
+
next.current_action = `Processing ${itemType}`;
|
|
98
|
+
}
|
|
99
|
+
return next;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function applyCommandCompleted(next, item, now) {
|
|
103
|
+
const command = inferCommand(item);
|
|
104
|
+
next.phase = "command";
|
|
105
|
+
next.current_command = null;
|
|
106
|
+
const exitCode = item?.exit_code ?? item?.exitCode ?? null;
|
|
107
|
+
const status = item?.status ?? "completed";
|
|
108
|
+
const entry = { command, exit_code: exitCode, status, completed_at: now };
|
|
109
|
+
// The tail of a failing command's output is the one piece of evidence
|
|
110
|
+
// a reviewer needs to tell "the build is broken" from "the network was
|
|
111
|
+
// down". Only failures keep it: the commands column is one row cell.
|
|
112
|
+
if (commandFailed(entry)) {
|
|
113
|
+
const tail = outputTail(item?.aggregated_output ?? item?.output ?? null);
|
|
114
|
+
if (tail) entry.output_tail = tail;
|
|
115
|
+
}
|
|
116
|
+
next.commands = [...(next.commands ?? []), entry];
|
|
117
|
+
return next;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// The row after a completed item, or null for an item type that changes nothing here.
|
|
121
|
+
function applyItemCompleted(next, item, itemType, now) {
|
|
122
|
+
if (itemType === "command_execution") return applyCommandCompleted(next, item, now);
|
|
123
|
+
if (itemType === "file_change") {
|
|
124
|
+
const files = inferChangedFiles(item);
|
|
125
|
+
next.phase = "editing";
|
|
126
|
+
next.changed_files = Array.from(new Set([...(next.changed_files ?? []), ...files]));
|
|
127
|
+
return next;
|
|
128
|
+
}
|
|
129
|
+
if (itemType === "agent_message") {
|
|
130
|
+
const message = inferAgentMessage(item);
|
|
131
|
+
next.phase = "reporting";
|
|
132
|
+
if (message) next.last_message = message;
|
|
133
|
+
return next;
|
|
134
|
+
}
|
|
135
|
+
if (itemType === "error") return appendNotice(next, item?.message ?? item?.text ?? null);
|
|
136
|
+
if (itemType === "reasoning" || itemType === "mcp_tool_call" || itemType === "todo_list") return next;
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function applyCodexEvent(task, event) {
|
|
141
|
+
const now = new Date().toISOString();
|
|
142
|
+
const eventType = getEventType(event);
|
|
143
|
+
const item = getItem(event);
|
|
144
|
+
const itemType = item?.type ?? item?.kind ?? item?.name ?? null;
|
|
145
|
+
let next = {
|
|
146
|
+
...task,
|
|
147
|
+
last_event_at: now,
|
|
148
|
+
last_event_type: eventType
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
if (eventType === "thread.started") {
|
|
152
|
+
next.status = "running";
|
|
153
|
+
next.phase = "starting";
|
|
154
|
+
const threadId = event.thread_id ?? event.threadId ?? event.msg?.thread_id;
|
|
155
|
+
if (threadId) next.thread_id = threadId;
|
|
156
|
+
return next;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (eventType === "turn.started") {
|
|
160
|
+
next.status = "running";
|
|
161
|
+
next.phase = "thinking";
|
|
162
|
+
return next;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (ITEM_PHASE.has(itemType) && (eventType === "item.started" || eventType === "item.updated")) return applyItemProgress(next, item, itemType);
|
|
166
|
+
|
|
167
|
+
if (eventType === "item.completed") {
|
|
168
|
+
const completed = applyItemCompleted(next, item, itemType, now);
|
|
169
|
+
if (completed) return completed;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (eventType === "turn.completed") {
|
|
173
|
+
next.status = "completed";
|
|
174
|
+
next.phase = null;
|
|
175
|
+
next.current_action = "Completed";
|
|
176
|
+
next.current_command = null;
|
|
177
|
+
next.completed_at = now;
|
|
178
|
+
return next;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (eventType === "turn.failed") {
|
|
182
|
+
next.status = "failed";
|
|
183
|
+
next.phase = null;
|
|
184
|
+
next.current_action = "Failed";
|
|
185
|
+
next.current_command = null;
|
|
186
|
+
next.completed_at = now;
|
|
187
|
+
// Codex writes the error as an object ({ message }). Stored as-is it
|
|
188
|
+
// cannot be bound to the TEXT column, the whole row update is dropped,
|
|
189
|
+
// and a worker that failed cleanly is later reported as lost.
|
|
190
|
+
next.error = errorText(event.error) ?? errorText(event.message) ?? errorText(event.msg?.error) ?? "Codex turn failed";
|
|
191
|
+
return next;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (eventType === "error") {
|
|
195
|
+
return appendNotice(next, errorText(event.message) ?? errorText(event.error) ?? "Codex reported an error event");
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (GOAL_EVENT_TYPES.has(eventType)) {
|
|
199
|
+
return applyGoalEvent(next, event);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return next;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// An error field as a string, whatever shape Codex gave it.
|
|
206
|
+
function errorText(value) {
|
|
207
|
+
if (value === null || value === undefined) return null;
|
|
208
|
+
if (typeof value === "string") return value || null;
|
|
209
|
+
if (typeof value === "object") {
|
|
210
|
+
const inner = value.message ?? value.error ?? value.msg ?? null;
|
|
211
|
+
if (typeof inner === "string" && inner) return inner;
|
|
212
|
+
try {
|
|
213
|
+
return JSON.stringify(value);
|
|
214
|
+
} catch {
|
|
215
|
+
return String(value);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return String(value);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const OUTPUT_TAIL_BYTES = 300;
|
|
222
|
+
|
|
223
|
+
function outputTail(output) {
|
|
224
|
+
if (typeof output !== "string" || !output) return null;
|
|
225
|
+
const buf = Buffer.from(output, "utf8");
|
|
226
|
+
if (buf.length <= OUTPUT_TAIL_BYTES) return output.trim() || null;
|
|
227
|
+
// Cut on a character boundary: drop bytes until the slice decodes cleanly.
|
|
228
|
+
let start = buf.length - OUTPUT_TAIL_BYTES;
|
|
229
|
+
while (start < buf.length && (buf[start] & 0xc0) === 0x80) start += 1;
|
|
230
|
+
return `…${buf.subarray(start).toString("utf8").trim()}`;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// A sandbox-refused command can come back with no exit code at all, only a
|
|
234
|
+
// failed status, so both are failure.
|
|
235
|
+
function clipText(value, max) {
|
|
236
|
+
const text = String(value ?? "");
|
|
237
|
+
return text.length > max ? `${text.slice(0, max)}…` : text;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// What the worker actually ran, condensed for a reviewer to hold its report
|
|
241
|
+
// against: a worker that says "tests pass" with commands_run 0, or that ends
|
|
242
|
+
// on a failing command and calls the work done, is caught here without
|
|
243
|
+
// reading the event stream. The commands list accumulates across resumes,
|
|
244
|
+
// so runs says how many turns it covers.
|
|
245
|
+
export function summarizeVerification(task, { limit = 10, clip = 160 } = {}) {
|
|
246
|
+
const commands = Array.isArray(task?.commands) ? task.commands : [];
|
|
247
|
+
const failed = commands.filter(commandFailed);
|
|
248
|
+
const last = commands[commands.length - 1] ?? null;
|
|
249
|
+
return {
|
|
250
|
+
runs: task?.run_count ?? 0,
|
|
251
|
+
commands_run: commands.length,
|
|
252
|
+
failed_count: failed.length,
|
|
253
|
+
last_exit_code: last ? (last.exit_code ?? null) : null,
|
|
254
|
+
last_command_failed: commandFailed(last),
|
|
255
|
+
failed: failed.slice(-limit).map((entry) => ({
|
|
256
|
+
command: clipText(entry.command, clip),
|
|
257
|
+
exit_code: entry.exit_code ?? null,
|
|
258
|
+
output_tail: entry.output_tail ?? null
|
|
259
|
+
})),
|
|
260
|
+
last: commands.slice(-limit).map((entry) => ({
|
|
261
|
+
command: clipText(entry.command, clip),
|
|
262
|
+
exit_code: entry.exit_code ?? null,
|
|
263
|
+
failed: commandFailed(entry)
|
|
264
|
+
}))
|
|
265
|
+
};
|
|
266
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// Read-only view of Codex's own thread goals.
|
|
2
|
+
//
|
|
3
|
+
// Codex stores goals in $CODEX_HOME/goals_1.sqlite (table thread_goals). The
|
|
4
|
+
// supervisor never writes there: Codex owns that file. When a worker thread
|
|
5
|
+
// has a native goal we surface it; when it does not, the supervisor falls back
|
|
6
|
+
// to the goal it recorded at dispatch time.
|
|
7
|
+
import { existsSync } from "node:fs";
|
|
8
|
+
import { DatabaseSync } from "node:sqlite";
|
|
9
|
+
import { goalsDbPath } from "../../infra/paths.js";
|
|
10
|
+
import { mapGoalStatus } from "../../domain/status.js";
|
|
11
|
+
|
|
12
|
+
function toCamelCase(status) {
|
|
13
|
+
return String(status).replace(/_([a-z])/g, (_match, letter) => letter.toUpperCase());
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function readNativeGoalState(threadId) {
|
|
17
|
+
if (!threadId) return { state: "no_thread", goal: null };
|
|
18
|
+
if (!existsSync(goalsDbPath)) return { state: "missing_database", goal: null };
|
|
19
|
+
let db;
|
|
20
|
+
try {
|
|
21
|
+
db = new DatabaseSync(goalsDbPath, { readOnly: true });
|
|
22
|
+
db.exec("PRAGMA busy_timeout=2000;");
|
|
23
|
+
const row = db.prepare(`
|
|
24
|
+
SELECT goal_id, objective, status, token_budget, tokens_used,
|
|
25
|
+
time_used_seconds, created_at_ms, updated_at_ms
|
|
26
|
+
FROM thread_goals
|
|
27
|
+
WHERE thread_id = ?
|
|
28
|
+
`).get(threadId);
|
|
29
|
+
if (!row) return { state: "missing_goal", goal: null };
|
|
30
|
+
return { state: "available", goal: {
|
|
31
|
+
objective: row.objective,
|
|
32
|
+
status: toCamelCase(row.status),
|
|
33
|
+
storage_status: row.status,
|
|
34
|
+
work_status: mapGoalStatus(row.status),
|
|
35
|
+
token_budget: row.token_budget ?? null,
|
|
36
|
+
tokens_used: row.tokens_used ?? 0,
|
|
37
|
+
time_used_seconds: row.time_used_seconds ?? 0,
|
|
38
|
+
created_at: row.created_at_ms ? new Date(row.created_at_ms).toISOString() : null,
|
|
39
|
+
updated_at: row.updated_at_ms ? new Date(row.updated_at_ms).toISOString() : null
|
|
40
|
+
} };
|
|
41
|
+
} catch (error) {
|
|
42
|
+
// A locked or half-migrated Codex database must never break a supervisor
|
|
43
|
+
// call; the supervisor goal is still returned by the caller.
|
|
44
|
+
return { state: /locked|busy/i.test(error.message) ? "locked" : "read_error", goal: null, error: error.message };
|
|
45
|
+
} finally {
|
|
46
|
+
try {
|
|
47
|
+
db?.close();
|
|
48
|
+
} catch {
|
|
49
|
+
/* already closed */
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function readNativeGoal(threadId) { return readNativeGoalState(threadId).goal; }
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { codexHome } from "../../infra/paths.js";
|
|
4
|
+
|
|
5
|
+
export function configuredMcpServers(configText) {
|
|
6
|
+
const names = new Set();
|
|
7
|
+
for (const line of String(configText ?? "").split("\n")) {
|
|
8
|
+
const match = line.match(/^\s*\[mcp_servers\.(?:"([^"]+)"|([^\].]+))\]\s*(?:#.*)?$/);
|
|
9
|
+
if (match) names.add(match[1] ?? match[2]);
|
|
10
|
+
}
|
|
11
|
+
return names;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// The key path is written bare: `-c mcp_servers.name.enabled=false`. A
|
|
15
|
+
// quoted name (`mcp_servers."name".enabled`) is taken by codex as a new
|
|
16
|
+
// server literally called "name", quotes included, with no transport, and
|
|
17
|
+
// the whole config then fails to load.
|
|
18
|
+
export function disableMcpServerArgs(configText, names = null) {
|
|
19
|
+
const configured = configuredMcpServers(configText);
|
|
20
|
+
const selected = names ? [...configured].filter((name) => names.has(name)) : [...configured];
|
|
21
|
+
const args = [];
|
|
22
|
+
for (const name of selected) {
|
|
23
|
+
if (!/^[A-Za-z0-9_-]+$/.test(name)) continue;
|
|
24
|
+
args.push("-c", `mcp_servers.${name}.enabled=false`);
|
|
25
|
+
}
|
|
26
|
+
return args;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function codexConfigText() {
|
|
30
|
+
try {
|
|
31
|
+
return readFileSync(join(codexHome, "config.toml"), "utf8");
|
|
32
|
+
} catch {
|
|
33
|
+
return "";
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function supervisorMcpDisableArgs() {
|
|
38
|
+
return disableMcpServerArgs(codexConfigText(), new Set(["agent-supervisor", "codex-supervisor"]));
|
|
39
|
+
}
|