codex-supervisor-mcp 0.13.5 → 0.14.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/README.en.md +2 -2
- package/README.md +2 -2
- package/package.json +19 -63
- package/skills/codex-supervisor/SKILL.md +5 -4
- package/src/{acceptance.js → app/acceptance.js} +54 -68
- package/src/app/auto-cleanup.js +90 -0
- package/src/app/cancel.js +60 -0
- package/src/app/cleanup-facts.js +96 -0
- package/src/app/cleanup-plan.js +164 -0
- package/src/app/compose.js +7 -0
- package/src/{context-compiler.js → app/context-compiler.js} +7 -13
- package/src/app/dispatch.js +396 -0
- package/src/app/followup.js +98 -0
- package/src/app/land-chain.js +133 -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 +50 -0
- package/src/app/resume.js +181 -0
- package/src/app/run-state.js +26 -0
- package/src/app/settled.js +28 -0
- package/src/{side-chat.js → app/side-chat.js} +82 -265
- 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 +176 -0
- package/src/{acceptance-baseline.js → domain/acceptance-baseline.js} +2 -1
- 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/{overview-delta.js → domain/overview-delta.js} +7 -3
- package/src/domain/side-turn-view.js +73 -0
- package/src/{worker-preflight.js → domain/worker-preflight.js} +1 -1
- package/src/{cleanup-hint.js → infra/cleanup-hint.js} +7 -8
- package/src/infra/git.js +30 -0
- package/src/infra/process.js +59 -0
- package/src/{sandbox-roots.js → infra/sandbox-roots.js} +2 -1
- package/src/{skill-sync.js → infra/skill-sync.js} +1 -1
- package/src/infra/store/db.js +265 -0
- package/src/infra/store/events.js +146 -0
- package/src/infra/store/facts.js +338 -0
- package/src/infra/store/reconcile.js +172 -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 +337 -0
- package/src/{auto-update-worker.js → infra/update/auto-update-worker.js} +4 -4
- package/src/{auto-update.js → infra/update/auto-update.js} +3 -3
- package/src/{update-check.js → infra/update/update-check.js} +4 -4
- package/src/{workspace-kind.js → infra/workspace-kind.js} +5 -7
- package/src/{worktree.js → infra/worktree.js} +56 -41
- package/src/{acceptance-cli.js → interface/bin/acceptance-cli.js} +7 -3
- package/src/{cleanup.js → interface/bin/cleanup.js} +4 -3
- package/src/{monitor.jsx → interface/bin/monitor.jsx} +4 -1
- package/src/{setup.js → interface/bin/setup.js} +7 -6
- package/src/{stage.js → interface/bin/stage.js} +1 -1
- package/src/{web-server.js → interface/bin/web-server.js} +2 -2
- 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 +94 -0
- package/src/interface/mcp/tools/delivery.js +197 -0
- package/src/interface/mcp/tools/dispatch.js +267 -0
- package/src/interface/mcp/tools/facts.js +239 -0
- package/src/interface/mcp/tools/overview.js +202 -0
- package/src/interface/mcp/tools/poll.js +58 -0
- package/src/interface/mcp/tools/results.js +284 -0
- package/src/interface/mcp/tools/review.js +135 -0
- package/src/interface/mcp/tools/wait.js +145 -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 +223 -0
- package/src/interface/web/app.js +226 -0
- package/src/interface/web/views.js +224 -0
- package/src/mcp-server.js +3 -2223
- package/src/run-host/entry.js +23 -0
- package/src/run-host/finalize.js +236 -0
- package/src/run-host/launch.js +63 -0
- package/src/run-host/live-run.js +73 -0
- package/src/run-host/outcome-facts.js +61 -0
- package/src/run-host/streams.js +76 -0
- package/src/run-host/track.js +128 -0
- package/src/{runner-adapter.js → runners/codex/adapter.js} +6 -6
- package/src/runners/codex/chat-chunks.js +97 -0
- package/src/{event-parser.js → runners/codex/event-parser.js} +57 -55
- package/src/{goal-store.js → runners/codex/goal-store.js} +2 -2
- package/src/{mcp-config.js → runners/codex/mcp-config.js} +1 -1
- package/src/cleanup-plan.js +0 -158
- package/src/codex-runner.js +0 -1586
- package/src/landing.js +0 -507
- package/src/task-store.js +0 -1800
- package/src/web-app.js +0 -425
- /package/src/{failure-class.js → domain/failure-class.js} +0 -0
- /package/src/{owned-changes.js → domain/owned-changes.js} +0 -0
- /package/src/{prompt.js → domain/prompt.js} +0 -0
- /package/src/{status.js → domain/status.js} +0 -0
- /package/src/{thread-health.js → domain/thread-health.js} +0 -0
- /package/src/{truncate.js → domain/truncate.js} +0 -0
- /package/src/{worker-changes.js → domain/worker-changes.js} +0 -0
- /package/src/{worker-env.js → domain/worker-env.js} +0 -0
- /package/src/{worker-report.js → domain/worker-report.js} +0 -0
- /package/src/{worker-report.schema.json → domain/worker-report.schema.json} +0 -0
- /package/src/{bin-resolver.js → infra/bin-resolver.js} +0 -0
- /package/src/{external-output.js → infra/external-output.js} +0 -0
- /package/src/{handoff.js → infra/handoff.js} +0 -0
- /package/src/{paths.js → infra/paths.js} +0 -0
- /package/src/{stage-store.js → infra/stage-store.js} +0 -0
- /package/src/{tool-guard.js → runners/codex/tool-guard.js} +0 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { defaultBinDirs, findBinaryPath, resolveCommand, shimMessage } from "
|
|
1
|
+
import { runSync, spawnProcess } from "../../infra/process.js";
|
|
2
|
+
import { defaultBinDirs, findBinaryPath, resolveCommand, shimMessage } from "../../infra/bin-resolver.js";
|
|
3
3
|
import { applyCodexEvent } from "./event-parser.js";
|
|
4
4
|
|
|
5
5
|
const CODEX_NPM_ENTRY = { pkg: "@openai/codex", bin: "bin/codex.js" };
|
|
@@ -21,7 +21,7 @@ export class CodexRunnerAdapter {
|
|
|
21
21
|
capabilityPreflight(target = this.resolveTarget()) {
|
|
22
22
|
if (target.error) return { ok: false, error: target.error };
|
|
23
23
|
try {
|
|
24
|
-
const help =
|
|
24
|
+
const help = runSync(target.cmd, [...target.prefixArgs, "exec", "--help"], {
|
|
25
25
|
encoding: "utf8",
|
|
26
26
|
timeout: 15000,
|
|
27
27
|
windowsHide: true,
|
|
@@ -65,7 +65,7 @@ export class CodexRunnerAdapter {
|
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
start(target, args, options) {
|
|
68
|
-
return
|
|
68
|
+
return spawnProcess(target.cmd, [...target.prefixArgs, ...args], options);
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
parseEvent(task, event) {
|
|
@@ -75,7 +75,7 @@ export class CodexRunnerAdapter {
|
|
|
75
75
|
cancelChild(child) {
|
|
76
76
|
if (process.platform === "win32" && child.pid) {
|
|
77
77
|
try {
|
|
78
|
-
|
|
78
|
+
runSync("taskkill", ["/PID", String(child.pid), "/T", "/F"], { stdio: "ignore", timeout: 5000, windowsHide: true });
|
|
79
79
|
return;
|
|
80
80
|
} catch {
|
|
81
81
|
// fall through to the process handle
|
|
@@ -86,7 +86,7 @@ export class CodexRunnerAdapter {
|
|
|
86
86
|
|
|
87
87
|
cancelPid(pid) {
|
|
88
88
|
if (process.platform === "win32") {
|
|
89
|
-
|
|
89
|
+
runSync("taskkill", ["/PID", String(pid), "/T", "/F"], { stdio: "ignore", timeout: 5000, windowsHide: true });
|
|
90
90
|
return;
|
|
91
91
|
}
|
|
92
92
|
process.kill(pid, "SIGTERM");
|
|
@@ -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
|
+
}
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
// Invariant: status is the lifecycle and is only ever assigned queued /
|
|
4
4
|
// running / completed / failed / cancelled / lost. Progress details go to
|
|
5
5
|
// phase, which is only meaningful while the work is running.
|
|
6
|
-
import {
|
|
6
|
+
import { commandFailed } from "../../domain/command-outcome.js";
|
|
7
|
+
import { parseWorkerReport } from "../../domain/worker-report.js";
|
|
7
8
|
|
|
8
9
|
function getEventType(event) {
|
|
9
10
|
return event.type ?? event.event ?? event.msg?.type ?? "unknown";
|
|
@@ -84,6 +85,58 @@ function applyGoalEvent(next, event) {
|
|
|
84
85
|
return next;
|
|
85
86
|
}
|
|
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
|
+
|
|
87
140
|
export function applyCodexEvent(task, event) {
|
|
88
141
|
const now = new Date().toISOString();
|
|
89
142
|
const eventType = getEventType(event);
|
|
@@ -109,56 +162,11 @@ export function applyCodexEvent(task, event) {
|
|
|
109
162
|
return next;
|
|
110
163
|
}
|
|
111
164
|
|
|
112
|
-
if (ITEM_PHASE.has(itemType) && (eventType === "item.started" || eventType === "item.updated"))
|
|
113
|
-
next.status = "running";
|
|
114
|
-
next.phase = ITEM_PHASE.get(itemType);
|
|
115
|
-
if (itemType === "command_execution") {
|
|
116
|
-
next.current_command = inferCommand(item);
|
|
117
|
-
next.current_action = next.current_command ? `Running ${next.current_command}` : "Running command";
|
|
118
|
-
} else if (itemType === "file_change") {
|
|
119
|
-
next.current_action = "Editing files";
|
|
120
|
-
} else if (itemType !== "reasoning" && itemType !== "todo_list") {
|
|
121
|
-
next.current_action = `Processing ${itemType}`;
|
|
122
|
-
}
|
|
123
|
-
return next;
|
|
124
|
-
}
|
|
165
|
+
if (ITEM_PHASE.has(itemType) && (eventType === "item.started" || eventType === "item.updated")) return applyItemProgress(next, item, itemType);
|
|
125
166
|
|
|
126
167
|
if (eventType === "item.completed") {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
next.phase = "command";
|
|
130
|
-
next.current_command = null;
|
|
131
|
-
const exitCode = item?.exit_code ?? item?.exitCode ?? null;
|
|
132
|
-
const status = item?.status ?? "completed";
|
|
133
|
-
const entry = { command, exit_code: exitCode, status, completed_at: now };
|
|
134
|
-
// The tail of a failing command's output is the one piece of evidence
|
|
135
|
-
// a reviewer needs to tell "the build is broken" from "the network was
|
|
136
|
-
// down". Only failures keep it: the commands column is one row cell.
|
|
137
|
-
if (commandFailed(entry)) {
|
|
138
|
-
const tail = outputTail(item?.aggregated_output ?? item?.output ?? null);
|
|
139
|
-
if (tail) entry.output_tail = tail;
|
|
140
|
-
}
|
|
141
|
-
next.commands = [...(next.commands ?? []), entry];
|
|
142
|
-
return next;
|
|
143
|
-
}
|
|
144
|
-
if (itemType === "file_change") {
|
|
145
|
-
const files = inferChangedFiles(item);
|
|
146
|
-
next.phase = "editing";
|
|
147
|
-
next.changed_files = Array.from(new Set([...(next.changed_files ?? []), ...files]));
|
|
148
|
-
return next;
|
|
149
|
-
}
|
|
150
|
-
if (itemType === "agent_message") {
|
|
151
|
-
const message = inferAgentMessage(item);
|
|
152
|
-
next.phase = "reporting";
|
|
153
|
-
if (message) next.last_message = message;
|
|
154
|
-
return next;
|
|
155
|
-
}
|
|
156
|
-
if (itemType === "error") {
|
|
157
|
-
return appendNotice(next, item?.message ?? item?.text ?? null);
|
|
158
|
-
}
|
|
159
|
-
if (itemType === "reasoning" || itemType === "mcp_tool_call" || itemType === "todo_list") {
|
|
160
|
-
return next;
|
|
161
|
-
}
|
|
168
|
+
const completed = applyItemCompleted(next, item, itemType, now);
|
|
169
|
+
if (completed) return completed;
|
|
162
170
|
}
|
|
163
171
|
|
|
164
172
|
if (eventType === "turn.completed") {
|
|
@@ -224,12 +232,6 @@ function outputTail(output) {
|
|
|
224
232
|
|
|
225
233
|
// A sandbox-refused command can come back with no exit code at all, only a
|
|
226
234
|
// failed status, so both are failure.
|
|
227
|
-
export function commandFailed(entry) {
|
|
228
|
-
if (!entry) return false;
|
|
229
|
-
if (entry.status === "failed") return true;
|
|
230
|
-
return entry.exit_code !== null && entry.exit_code !== undefined && entry.exit_code !== 0;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
235
|
function clipText(value, max) {
|
|
234
236
|
const text = String(value ?? "");
|
|
235
237
|
return text.length > max ? `${text.slice(0, max)}…` : text;
|
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
// to the goal it recorded at dispatch time.
|
|
7
7
|
import { existsSync } from "node:fs";
|
|
8
8
|
import { DatabaseSync } from "node:sqlite";
|
|
9
|
-
import { goalsDbPath } from "
|
|
10
|
-
import { mapGoalStatus } from "
|
|
9
|
+
import { goalsDbPath } from "../../infra/paths.js";
|
|
10
|
+
import { mapGoalStatus } from "../../domain/status.js";
|
|
11
11
|
|
|
12
12
|
function toCamelCase(status) {
|
|
13
13
|
return String(status).replace(/_([a-z])/g, (_match, letter) => letter.toUpperCase());
|
package/src/cleanup-plan.js
DELETED
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
-
import { lstat, realpath } from "node:fs/promises";
|
|
3
|
-
import { execFileSync } from "node:child_process";
|
|
4
|
-
import { resolve } from "node:path";
|
|
5
|
-
import { ACTIVE_STATUSES } from "./status.js";
|
|
6
|
-
import { worktreesDir } from "./paths.js";
|
|
7
|
-
import { appendTaskEvent, getTask, isPidAlive, markCleanupPlanItemApplied, readTasks, updateWorkItemDelivery, runIdFor } from "./task-store.js";
|
|
8
|
-
|
|
9
|
-
export const CLEANUP_PLAN_TTL_MS = 15 * 60 * 1000;
|
|
10
|
-
|
|
11
|
-
function git(cwd, args) {
|
|
12
|
-
return execFileSync(process.env.GIT_BIN?.trim() || "git", ["--no-optional-locks", "-C", cwd, ...args], {
|
|
13
|
-
encoding: "utf8",
|
|
14
|
-
timeout: 15000,
|
|
15
|
-
stdio: ["ignore", "pipe", "pipe"]
|
|
16
|
-
}).trim();
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function hash(value) {
|
|
20
|
-
return createHash("sha256").update(value).digest("hex");
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
async function directoryBytes(path) {
|
|
24
|
-
try {
|
|
25
|
-
const output = execFileSync("du", ["-sk", path], { encoding: "utf8", timeout: 30000, stdio: ["ignore", "pipe", "ignore"] }).trim();
|
|
26
|
-
const kib = Number(output.split(/\s+/)[0]);
|
|
27
|
-
return Number.isFinite(kib) ? kib * 1024 : 0;
|
|
28
|
-
} catch {
|
|
29
|
-
return 0;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function remoteProof(task, { fetch = false } = {}) {
|
|
34
|
-
if (!task.push_remote || !task.push_branch || !task.push_remote_head) {
|
|
35
|
-
return { error: "remote delivery proof is incomplete; rerun verify_delivery_push" };
|
|
36
|
-
}
|
|
37
|
-
let remoteHead = task.push_remote_head;
|
|
38
|
-
if (fetch) {
|
|
39
|
-
try {
|
|
40
|
-
git(task.project_root, ["remote", "get-url", task.push_remote]);
|
|
41
|
-
git(task.project_root, ["fetch", "--no-tags", task.push_remote, `refs/heads/${task.push_branch}`]);
|
|
42
|
-
remoteHead = git(task.project_root, ["rev-parse", "FETCH_HEAD"]);
|
|
43
|
-
} catch (error) {
|
|
44
|
-
return { error: `remote delivery proof could not be refreshed: ${error.message.split("\n")[0]}` };
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
try {
|
|
48
|
-
git(task.project_root, ["merge-base", "--is-ancestor", task.landed_head, remoteHead]);
|
|
49
|
-
} catch {
|
|
50
|
-
return { error: "landed head is no longer reachable from the remote branch" };
|
|
51
|
-
}
|
|
52
|
-
return { remoteHead };
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
async function taskVerdict(task, tasks, { revalidateRemote = false } = {}) {
|
|
56
|
-
if (task.status !== "completed") return { verdict: "blocked", reasons: ["worker is unfinished, failed, cancelled or lost"] };
|
|
57
|
-
if (task.acceptance_results?.passed !== true) return { verdict: "blocked", reasons: ["acceptance is not explicitly passed"] };
|
|
58
|
-
if (task.pid && isPidAlive(task.pid)) return { verdict: "blocked", reasons: ["worker process is still active"] };
|
|
59
|
-
if (task.run_host_pid && isPidAlive(task.run_host_pid)) return { verdict: "blocked", reasons: ["run host process is still active"] };
|
|
60
|
-
if (task.acceptance_results?.status === "running" || (task.acceptance_results?.supervisor_pid && isPidAlive(task.acceptance_results.supervisor_pid))) return { verdict: "blocked", reasons: ["acceptance process is still active"] };
|
|
61
|
-
if (tasks.some((other) => ACTIVE_STATUSES.has(other.status) && (other.depends_on ?? []).includes(task.id))) return { verdict: "blocked", reasons: ["an active dependent still needs this work"] };
|
|
62
|
-
if (!task.worktree_path || !task.project_root) return { verdict: "review", reasons: ["no registered isolated worktree"] };
|
|
63
|
-
if (!task.landed_at || !task.landed_head) return { verdict: "blocked", reasons: ["work has not been landed"] };
|
|
64
|
-
if (!task.push_verified_at) return { verdict: "blocked", reasons: ["remote delivery has not been verified"] };
|
|
65
|
-
const delivery = remoteProof(task, { fetch: revalidateRemote });
|
|
66
|
-
if (delivery.error) return { verdict: "blocked", reasons: [delivery.error] };
|
|
67
|
-
try {
|
|
68
|
-
const path = resolve(task.worktree_path);
|
|
69
|
-
if (path !== resolve(worktreesDir, task.id)) return { verdict: "review", reasons: ["worktree is outside its managed task directory"] };
|
|
70
|
-
const info = await lstat(path);
|
|
71
|
-
if (info.isSymbolicLink()) return { verdict: "review", reasons: ["worktree is a symlink"] };
|
|
72
|
-
if (await realpath(path) !== await realpath(resolve(worktreesDir, task.id))) return { verdict: "review", reasons: ["worktree realpath is unexpected"] };
|
|
73
|
-
const branch = git(path, ["symbolic-ref", "--short", "HEAD"]);
|
|
74
|
-
if (branch !== `codex/${task.id}`) return { verdict: "review", reasons: ["worktree branch ownership is unexpected"] };
|
|
75
|
-
const registrations = git(task.project_root, ["worktree", "list", "--porcelain", "-z"]).split("\0").filter((line) => line.startsWith("worktree "));
|
|
76
|
-
let registered = false;
|
|
77
|
-
for (const line of registrations) if (await realpath(line.slice(9)) === await realpath(path)) registered = true;
|
|
78
|
-
if (!registered) return { verdict: "review", reasons: ["git worktree registration is missing"] };
|
|
79
|
-
if (git(path, ["status", "--porcelain", "--untracked-files=all"])) return { verdict: "blocked", reasons: ["worktree has uncommitted or untracked files"] };
|
|
80
|
-
if (git(path, ["ls-files", "--others", "--ignored", "--exclude-standard"])) return { verdict: "blocked", reasons: ["worktree has ignored artifacts"] };
|
|
81
|
-
if (task.attention_reason) return { verdict: "blocked", reasons: [`unresolved attention: ${task.attention_reason}`] };
|
|
82
|
-
if (task.run_evidence?.external_outputs?.length) return { verdict: "blocked", reasons: ["shared output evidence is retained"] };
|
|
83
|
-
return {
|
|
84
|
-
verdict: "safe",
|
|
85
|
-
reasons: ["acceptance passed", "landed", "remote delivery verified", "worktree clean", "no active dependent"],
|
|
86
|
-
fingerprint: hash(JSON.stringify({ path, branch, head: git(path, ["rev-parse", "HEAD"]), landed: task.landed_head, push: { remote: task.push_remote, branch: task.push_branch, head: delivery.remoteHead } }))
|
|
87
|
-
};
|
|
88
|
-
} catch (error) {
|
|
89
|
-
return { verdict: "review", reasons: [`cleanup proof failed: ${error.message.split("\n")[0]}`] };
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export async function createCleanupPlan({ taskIds = null, projectRoot = null, now = Date.now(), revalidateRemote = true } = {}) {
|
|
94
|
-
const tasks = await readTasks();
|
|
95
|
-
const selected = tasks.filter((task) => (!taskIds || taskIds.includes(task.id)) && (!projectRoot || resolve(task.project_root) === resolve(projectRoot)));
|
|
96
|
-
const items = [];
|
|
97
|
-
for (const task of selected) {
|
|
98
|
-
const result = await taskVerdict(task, tasks, { revalidateRemote });
|
|
99
|
-
const bytes = result.verdict === "safe" ? await directoryBytes(task.worktree_path) : 0;
|
|
100
|
-
items.push({ task_id: task.id, verdict: result.verdict, reasons: result.reasons, fingerprint: result.fingerprint ?? null, reclaimable_bytes: bytes, eligible_after: result.verdict === "safe" ? new Date(now).toISOString() : null });
|
|
101
|
-
}
|
|
102
|
-
const summary = {
|
|
103
|
-
safe: items.filter((item) => item.verdict === "safe").length,
|
|
104
|
-
review: items.filter((item) => item.verdict === "review").length,
|
|
105
|
-
blocked: items.filter((item) => item.verdict === "blocked").length,
|
|
106
|
-
reclaimable_bytes: items.reduce((sum, item) => sum + item.reclaimable_bytes, 0)
|
|
107
|
-
};
|
|
108
|
-
return {
|
|
109
|
-
id: randomUUID(),
|
|
110
|
-
project_root: projectRoot ? resolve(projectRoot) : null,
|
|
111
|
-
generated_at: new Date(now).toISOString(),
|
|
112
|
-
expires_at: new Date(now + CLEANUP_PLAN_TTL_MS).toISOString(),
|
|
113
|
-
policy: "explicit_apply_keep_evidence_and_branch",
|
|
114
|
-
summary,
|
|
115
|
-
items
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export async function removePlannedWorktree(task, fingerprint) {
|
|
120
|
-
const result = await taskVerdict(task, await readTasks(), { revalidateRemote: true });
|
|
121
|
-
if (result.verdict !== "safe") throw new Error(`worktree is no longer safe: ${result.reasons.join("; ")}`);
|
|
122
|
-
if (result.fingerprint !== fingerprint) throw new Error("worktree changed since cleanup plan; create a new plan");
|
|
123
|
-
git(task.project_root, ["worktree", "remove", task.worktree_path]);
|
|
124
|
-
try { git(task.project_root, ["worktree", "prune"]); } catch { /* best effort */ }
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export async function applyCleanupPlan({ plan, itemIds, actor = "main_thread" }) {
|
|
128
|
-
if (Date.parse(plan.expires_at) <= Date.now()) {
|
|
129
|
-
return { error: "cleanup_plan_expired", plan_id: plan.id, expires_at: plan.expires_at };
|
|
130
|
-
}
|
|
131
|
-
const selected = plan.items.filter((item) => itemIds.includes(item.id));
|
|
132
|
-
if (selected.length !== itemIds.length) return { error: "cleanup_plan_item_not_found", plan_id: plan.id };
|
|
133
|
-
const applied = [];
|
|
134
|
-
const skipped = [];
|
|
135
|
-
for (const item of selected) {
|
|
136
|
-
if (item.verdict !== "safe") {
|
|
137
|
-
skipped.push({ item_id: item.id, task_id: item.task_id, reason: "only SAFE plan items may be applied" });
|
|
138
|
-
continue;
|
|
139
|
-
}
|
|
140
|
-
const task = await getTask(item.task_id);
|
|
141
|
-
if (!task) {
|
|
142
|
-
skipped.push({ item_id: item.id, task_id: item.task_id, reason: "task no longer exists" });
|
|
143
|
-
continue;
|
|
144
|
-
}
|
|
145
|
-
try {
|
|
146
|
-
await removePlannedWorktree(task, item.fingerprint);
|
|
147
|
-
const now = await markCleanupPlanItemApplied(item.id, actor);
|
|
148
|
-
await updateWorkItemDelivery(task.id, "cleanup_removed");
|
|
149
|
-
await appendTaskEvent(task.id, { type: "supervisor.cleanup_removed", plan_id: plan.id, item_id: item.id, removed_at: now, retained: ["branch", "task", "events", "jsonl", "artifacts"] }, {
|
|
150
|
-
runId: runIdFor(task.id, task.run_count ?? 1), actor
|
|
151
|
-
});
|
|
152
|
-
applied.push({ item_id: item.id, task_id: task.id, worktree_path: task.worktree_path, removed_at: now });
|
|
153
|
-
} catch (error) {
|
|
154
|
-
skipped.push({ item_id: item.id, task_id: item.task_id, reason: error.message.split("\n")[0] });
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
return { plan_id: plan.id, applied, skipped, retained: ["branch", "task", "events", "jsonl", "artifacts"] };
|
|
158
|
-
}
|