codex-supervisor-mcp 0.5.8 → 0.5.9
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 +5 -2
- package/README.md +5 -2
- package/package.json +2 -1
- package/src/web-app.js +318 -0
- package/src/web-server.js +39 -292
package/README.en.md
CHANGED
|
@@ -82,7 +82,9 @@ npx -p codex-supervisor-mcp codex-supervisor-web
|
|
|
82
82
|
|
|
83
83
|
Open `http://127.0.0.1:7877`. The left column lists sessions with the title given at dispatch and how many workers are still running; a session opens into its worker ledger; a worker opens a drawer with its full report, change list, commands, event stream, task text and token usage. It refreshes every 2.5 seconds while something runs, every 8 seconds otherwise.
|
|
84
84
|
|
|
85
|
-
`
|
|
85
|
+
Which store it opens: `SUPERVISOR_HOME` from the environment if set; otherwise the nearest `.mcp.json` above the current directory, if its `codex-supervisor` entry sets `SUPERVISOR_HOME`; otherwise the default `~/.codex-supervisor`. The startup line says which one and where it came from, so running it inside the project directory is enough.
|
|
86
|
+
|
|
87
|
+
`SUPERVISOR_WEB_PORT` changes the port. A taken port (usually a board that is already running) is reported in one line and the process exits. The board reads. It does not dispatch or cancel.
|
|
86
88
|
|
|
87
89
|
## What the board shows
|
|
88
90
|
|
|
@@ -284,7 +286,8 @@ rm -rf ~/.codex-supervisor
|
|
|
284
286
|
| `Cannot find module 'node:sqlite'` | Node older than 22.13.0 | Upgrade Node |
|
|
285
287
|
| Worker stuck in `lost` | The MCP process was killed and the worker orphaned | `resume_codex_worker`, or dispatch again |
|
|
286
288
|
| `ownedPaths overlap with active worker(s)` | Two workers claimed the same paths | Split differently, or cancel the one holding them |
|
|
287
|
-
| The board is empty | The board and the MCP use different `SUPERVISOR_HOME`s | Start
|
|
289
|
+
| The board is empty | The board and the MCP use different `SUPERVISOR_HOME`s | Start it inside a project with a `.mcp.json`, or pass the same `SUPERVISOR_HOME`; the second startup line names the store in use |
|
|
290
|
+
| `port 7877 ... is already in use` | A board is already running | Open `http://127.0.0.1:7877`, or start a second one with `SUPERVISOR_WEB_PORT=8080` |
|
|
288
291
|
|
|
289
292
|
## Known limitations
|
|
290
293
|
|
package/README.md
CHANGED
|
@@ -82,7 +82,9 @@ npx -p codex-supervisor-mcp codex-supervisor-web
|
|
|
82
82
|
|
|
83
83
|
打开 `http://127.0.0.1:7877`。左栏按 session 列,显示派单时写的标题和还有几路在跑;点进去是这个 session 的 worker 台账;再点一路,右边抽屉是它的完整汇报、改动清单、命令、事件流、任务书和 token 消耗。有 worker 在跑时 2.5 秒刷一次,静止时 8 秒。
|
|
84
84
|
|
|
85
|
-
|
|
85
|
+
它读哪个状态目录:环境里有 `SUPERVISOR_HOME` 就用它;没有就从当前目录往上找 `.mcp.json`,里面 `codex-supervisor` 配了 `SUPERVISOR_HOME` 就用那个;都没有才用默认的 `~/.codex-supervisor`。启动时会打印用的是哪个、从哪来的。在项目目录里敲一下就对上了,不用再传路径。
|
|
86
|
+
|
|
87
|
+
端口用 `SUPERVISOR_WEB_PORT` 改。端口被占(多半是已经有一个看板在跑)会直接说明并退出。看板只读,不派单也不取消。
|
|
86
88
|
|
|
87
89
|
## 看板里有什么
|
|
88
90
|
|
|
@@ -284,7 +286,8 @@ rm -rf ~/.codex-supervisor
|
|
|
284
286
|
| `Cannot find module 'node:sqlite'` | Node 低于 22.13.0 | 升 Node |
|
|
285
287
|
| worker 卡在 `lost` | MCP 进程被杀,worker 成孤儿 | `resume_codex_worker` 接回,或重派 |
|
|
286
288
|
| `ownedPaths overlap with active worker(s)` | 两路认领了同一片路径 | 改拆法,或先取消占着的那路 |
|
|
287
|
-
| 看板打开是空的 | 看板和 MCP 的 `SUPERVISOR_HOME` 不是同一个 |
|
|
289
|
+
| 看板打开是空的 | 看板和 MCP 的 `SUPERVISOR_HOME` 不是同一个 | 在配了 `.mcp.json` 的项目目录里起,或者显式传同一个 `SUPERVISOR_HOME`;启动日志第二行写了它用的哪个 |
|
|
290
|
+
| `port 7877 ... is already in use` | 已经有一个看板在跑 | 直接开 `http://127.0.0.1:7877`,或 `SUPERVISOR_WEB_PORT=8080` 再起一个 |
|
|
288
291
|
|
|
289
292
|
## 已知限制
|
|
290
293
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "codex-supervisor-mcp",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "MCP server that dispatches and supervises parallel Codex CLI workers, one isolated Git worktree each, with a persisted work state machine.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -43,6 +43,7 @@
|
|
|
43
43
|
"src/cleanup.js",
|
|
44
44
|
"src/monitor.jsx",
|
|
45
45
|
"src/web-server.js",
|
|
46
|
+
"src/web-app.js",
|
|
46
47
|
"src/update-check.js",
|
|
47
48
|
"web/dist/",
|
|
48
49
|
"skills/",
|
package/src/web-app.js
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
// The board's HTTP server: a thin read-only view over the supervisor store.
|
|
2
|
+
// Which sessions exist, what each one is doing right now, what every Codex
|
|
3
|
+
// thread under a session has done. It reads the same SQLite file the MCP
|
|
4
|
+
// server writes and never writes to it, apart from the same orphan
|
|
5
|
+
// reconcile every read does. web-server.js is the entry that decides which
|
|
6
|
+
// store to open and then calls startWebServer().
|
|
7
|
+
import { createServer } from "node:http";
|
|
8
|
+
import { createReadStream, existsSync, statSync } from "node:fs";
|
|
9
|
+
import { extname, join, normalize, resolve } from "node:path";
|
|
10
|
+
import { fileURLToPath } from "node:url";
|
|
11
|
+
import {
|
|
12
|
+
getTask,
|
|
13
|
+
listTaskEventKinds,
|
|
14
|
+
readAgentMessages,
|
|
15
|
+
readSessions,
|
|
16
|
+
readTaskEvents,
|
|
17
|
+
readTasks,
|
|
18
|
+
reconcileDetachedActiveTasks
|
|
19
|
+
} from "./task-store.js";
|
|
20
|
+
import { readNativeGoal } from "./goal-store.js";
|
|
21
|
+
import { readTaskChanges } from "./worktree.js";
|
|
22
|
+
import { ACTIVE_STATUSES } from "./status.js";
|
|
23
|
+
import { supervisorRoot } from "./paths.js";
|
|
24
|
+
import { checkForUpdate } from "./update-check.js";
|
|
25
|
+
|
|
26
|
+
const here = fileURLToPath(new URL(".", import.meta.url));
|
|
27
|
+
const distDir = resolve(here, "..", "web", "dist");
|
|
28
|
+
const port = Number(process.env.SUPERVISOR_WEB_PORT ?? 7877);
|
|
29
|
+
const host = process.env.SUPERVISOR_WEB_HOST ?? "127.0.0.1";
|
|
30
|
+
const NO_SESSION = "none";
|
|
31
|
+
|
|
32
|
+
const MIME = {
|
|
33
|
+
".html": "text/html; charset=utf-8",
|
|
34
|
+
".js": "text/javascript; charset=utf-8",
|
|
35
|
+
".css": "text/css; charset=utf-8",
|
|
36
|
+
".json": "application/json; charset=utf-8",
|
|
37
|
+
".svg": "image/svg+xml",
|
|
38
|
+
".woff2": "font/woff2",
|
|
39
|
+
".woff": "font/woff",
|
|
40
|
+
".png": "image/png",
|
|
41
|
+
".ico": "image/x-icon"
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
function clip(value, maxChars) {
|
|
45
|
+
if (value === null || value === undefined) return null;
|
|
46
|
+
const text = String(value);
|
|
47
|
+
return text.length <= maxChars ? text : `${text.slice(0, maxChars)}…`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function durationMs(task) {
|
|
51
|
+
const start = Date.parse(task.started_at ?? task.created_at ?? "");
|
|
52
|
+
if (Number.isNaN(start)) return null;
|
|
53
|
+
const end = Date.parse(task.completed_at ?? "");
|
|
54
|
+
const stop = Number.isNaN(end) ? Date.now() : end;
|
|
55
|
+
return Math.max(0, stop - start);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// The row every list shows. No prompt, no raw events: those are per-worker
|
|
59
|
+
// reads, so a session with forty threads stays one cheap request.
|
|
60
|
+
function workerRow(task) {
|
|
61
|
+
return {
|
|
62
|
+
id: task.id,
|
|
63
|
+
title: task.title,
|
|
64
|
+
status: task.status,
|
|
65
|
+
phase: task.phase ?? null,
|
|
66
|
+
session_id: task.session_id ?? null,
|
|
67
|
+
thread_id: task.thread_id ?? null,
|
|
68
|
+
goal: task.goal_objective ?? null,
|
|
69
|
+
goal_status: task.goal_status ?? null,
|
|
70
|
+
model: task.model ?? null,
|
|
71
|
+
sandbox: task.sandbox,
|
|
72
|
+
project_root: task.project_root,
|
|
73
|
+
worktree_path: task.worktree_path ?? null,
|
|
74
|
+
branch: task.worktree_path ? `codex/${task.id}` : null,
|
|
75
|
+
base_commit: task.base_commit ?? null,
|
|
76
|
+
owned_paths: task.owned_paths ?? [],
|
|
77
|
+
depends_on: task.depends_on ?? [],
|
|
78
|
+
followup_of: task.followup_of ?? null,
|
|
79
|
+
resumed_from: task.resumed_from ?? null,
|
|
80
|
+
run_count: task.run_count ?? 0,
|
|
81
|
+
current_action: clip(task.current_action, 300),
|
|
82
|
+
last_message: clip(task.last_message, 400),
|
|
83
|
+
changed_file_count: liveChangeCount(task),
|
|
84
|
+
command_count: (task.commands ?? []).length,
|
|
85
|
+
error: task.error ?? null,
|
|
86
|
+
exit_code: task.exit_code ?? null,
|
|
87
|
+
pid: task.pid ?? null,
|
|
88
|
+
created_at: task.created_at || null,
|
|
89
|
+
started_at: task.started_at || null,
|
|
90
|
+
updated_at: task.updated_at || null,
|
|
91
|
+
completed_at: task.completed_at || null,
|
|
92
|
+
duration_ms: durationMs(task)
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Changed-file counts for the list. The stored changed_files on a row only
|
|
97
|
+
// knows about file_change events; the truth is the worktree diff, which costs
|
|
98
|
+
// two git calls per worker. Terminal rows never change again, so their count
|
|
99
|
+
// is memoised on (id, updated_at); running rows are re-read every time.
|
|
100
|
+
const changeCountCache = new Map();
|
|
101
|
+
function liveChangeCount(task) {
|
|
102
|
+
const key = `${task.id}:${task.updated_at}:${task.status}`;
|
|
103
|
+
const cached = changeCountCache.get(key);
|
|
104
|
+
if (cached !== undefined && !ACTIVE_STATUSES.has(task.status)) return cached;
|
|
105
|
+
const count = readTaskChanges(task).length;
|
|
106
|
+
changeCountCache.set(key, count);
|
|
107
|
+
if (changeCountCache.size > 2000) changeCountCache.delete(changeCountCache.keys().next().value);
|
|
108
|
+
return count;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function sessionKey(task) {
|
|
112
|
+
return task.session_id || NO_SESSION;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function latest(values) {
|
|
116
|
+
return values.filter(Boolean).sort().at(-1) ?? null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// What a session is about. The title the main thread recorded wins; without
|
|
120
|
+
// one, the goals of its workers are the next best description.
|
|
121
|
+
function aboutSession(meta, tasks) {
|
|
122
|
+
if (meta?.title) return { title: meta.title, note: meta.note ?? null, derived: false };
|
|
123
|
+
const goals = Array.from(new Set(tasks.map((task) => task.goal_objective).filter(Boolean)));
|
|
124
|
+
const summary = goals.slice(0, 3).map((goal) => clip(goal, 60)).join(";");
|
|
125
|
+
return { title: summary || null, note: null, derived: true };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function sessionRow(key, tasks, meta = null) {
|
|
129
|
+
const counts = {};
|
|
130
|
+
for (const task of tasks) counts[task.status] = (counts[task.status] ?? 0) + 1;
|
|
131
|
+
const active = tasks.filter((task) => ACTIVE_STATUSES.has(task.status));
|
|
132
|
+
const byActivity = [...tasks].sort((a, b) => String(b.updated_at ?? "").localeCompare(String(a.updated_at ?? "")));
|
|
133
|
+
const lastReport = byActivity.find((task) => task.last_message);
|
|
134
|
+
const about = aboutSession(meta, tasks);
|
|
135
|
+
return {
|
|
136
|
+
id: key,
|
|
137
|
+
unsessioned: key === NO_SESSION,
|
|
138
|
+
title: about.title,
|
|
139
|
+
note: about.note,
|
|
140
|
+
title_derived: about.derived,
|
|
141
|
+
worker_count: tasks.length,
|
|
142
|
+
active_count: active.length,
|
|
143
|
+
counts,
|
|
144
|
+
project_roots: Array.from(new Set(tasks.map((task) => task.project_root).filter(Boolean))),
|
|
145
|
+
first_created_at: tasks.map((task) => task.created_at).filter(Boolean).sort()[0] ?? null,
|
|
146
|
+
last_activity_at: latest(tasks.map((task) => task.updated_at)),
|
|
147
|
+
// What the session is doing right now: one line per running thread.
|
|
148
|
+
now: active.map((task) => ({
|
|
149
|
+
id: task.id,
|
|
150
|
+
title: task.title,
|
|
151
|
+
phase: task.phase ?? null,
|
|
152
|
+
current_action: clip(task.current_action, 160)
|
|
153
|
+
})),
|
|
154
|
+
last_report: lastReport
|
|
155
|
+
? { id: lastReport.id, title: lastReport.title, text: clip(lastReport.last_message, 240), at: lastReport.updated_at }
|
|
156
|
+
: null
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async function overview() {
|
|
161
|
+
await reconcileDetachedActiveTasks();
|
|
162
|
+
const tasks = await readTasks();
|
|
163
|
+
const metas = new Map((await readSessions()).map((row) => [row.id, row]));
|
|
164
|
+
const groups = new Map();
|
|
165
|
+
for (const task of tasks) {
|
|
166
|
+
const key = sessionKey(task);
|
|
167
|
+
if (!groups.has(key)) groups.set(key, []);
|
|
168
|
+
groups.get(key).push(task);
|
|
169
|
+
}
|
|
170
|
+
const sessions = Array.from(groups, ([key, group]) => sessionRow(key, group, metas.get(key) ?? null)).sort((a, b) => {
|
|
171
|
+
if (a.active_count !== b.active_count) return b.active_count - a.active_count;
|
|
172
|
+
return String(b.last_activity_at ?? "").localeCompare(String(a.last_activity_at ?? ""));
|
|
173
|
+
});
|
|
174
|
+
return {
|
|
175
|
+
generated_at: new Date().toISOString(),
|
|
176
|
+
store: supervisorRoot,
|
|
177
|
+
total_workers: tasks.length,
|
|
178
|
+
active_workers: tasks.filter((task) => ACTIVE_STATUSES.has(task.status)).length,
|
|
179
|
+
sessions
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async function session(key) {
|
|
184
|
+
await reconcileDetachedActiveTasks();
|
|
185
|
+
const tasks = (await readTasks()).filter((task) => sessionKey(task) === key);
|
|
186
|
+
if (tasks.length === 0) return null;
|
|
187
|
+
const meta = (await readSessions()).find((row) => row.id === key) ?? null;
|
|
188
|
+
return {
|
|
189
|
+
...sessionRow(key, tasks, meta),
|
|
190
|
+
workers: tasks.map(workerRow)
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function worker(taskId) {
|
|
195
|
+
const task = await getTask(taskId);
|
|
196
|
+
if (!task) return null;
|
|
197
|
+
const reports = await readAgentMessages(taskId, 10);
|
|
198
|
+
// Token spend comes from Codex's own turn.completed usage blocks; one per
|
|
199
|
+
// run, so a resumed worker sums its runs.
|
|
200
|
+
const usage = { input_tokens: 0, cached_input_tokens: 0, output_tokens: 0, reasoning_output_tokens: 0, turns: 0 };
|
|
201
|
+
for (const event of await readTaskEvents(taskId, 50, ["turn.completed"])) {
|
|
202
|
+
const block = event?.usage ?? {};
|
|
203
|
+
usage.turns += 1;
|
|
204
|
+
for (const key of ["input_tokens", "cached_input_tokens", "output_tokens", "reasoning_output_tokens"]) {
|
|
205
|
+
usage[key] += Number(block[key] ?? 0) || 0;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
const commands = (task.commands ?? []).slice(-40).map((entry) => ({
|
|
209
|
+
...entry,
|
|
210
|
+
command: clip(entry.command, 600)
|
|
211
|
+
}));
|
|
212
|
+
return {
|
|
213
|
+
...workerRow(task),
|
|
214
|
+
prompt: task.prompt ?? null,
|
|
215
|
+
changed_files: readTaskChanges(task),
|
|
216
|
+
commands,
|
|
217
|
+
reports: reports.length > 0 ? reports : task.last_message ? [task.last_message] : [],
|
|
218
|
+
native_goal: readNativeGoal(task.thread_id),
|
|
219
|
+
usage,
|
|
220
|
+
notices: task.notices ?? null,
|
|
221
|
+
run_log: task.run_log ?? null,
|
|
222
|
+
event_kinds: await listTaskEventKinds(taskId)
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
async function events(taskId, url) {
|
|
227
|
+
const limit = Math.min(1000, Math.max(1, Number(url.searchParams.get("limit") ?? 150)));
|
|
228
|
+
const kinds = url.searchParams.get("kinds")?.split(",").filter(Boolean) ?? null;
|
|
229
|
+
return { task_id: taskId, events: await readTaskEvents(taskId, limit, kinds) };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function json(res, status, body) {
|
|
233
|
+
const text = JSON.stringify(body);
|
|
234
|
+
res.writeHead(status, {
|
|
235
|
+
"content-type": "application/json; charset=utf-8",
|
|
236
|
+
"cache-control": "no-store",
|
|
237
|
+
"content-length": Buffer.byteLength(text)
|
|
238
|
+
});
|
|
239
|
+
res.end(text);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function serveStatic(res, pathname) {
|
|
243
|
+
if (!existsSync(distDir)) {
|
|
244
|
+
res.writeHead(503, { "content-type": "text/plain; charset=utf-8" });
|
|
245
|
+
res.end("web/dist is missing. Run `npm run web:build` first.\n");
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
const safe = normalize(pathname).replace(/^(\.\.[/\\])+/, "");
|
|
249
|
+
let file = join(distDir, safe);
|
|
250
|
+
if (!file.startsWith(distDir)) {
|
|
251
|
+
res.writeHead(403);
|
|
252
|
+
res.end();
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
if (!existsSync(file) || statSync(file).isDirectory()) file = join(distDir, "index.html");
|
|
256
|
+
const type = MIME[extname(file)] ?? "application/octet-stream";
|
|
257
|
+
res.writeHead(200, {
|
|
258
|
+
"content-type": type,
|
|
259
|
+
"cache-control": file.endsWith("index.html") ? "no-store" : "public, max-age=31536000, immutable"
|
|
260
|
+
});
|
|
261
|
+
createReadStream(file).pipe(res);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
async function route(req, res) {
|
|
265
|
+
const url = new URL(req.url ?? "/", `http://${host}`);
|
|
266
|
+
const parts = url.pathname.split("/").filter(Boolean);
|
|
267
|
+
if (parts[0] !== "api") return serveStatic(res, url.pathname);
|
|
268
|
+
if (req.method !== "GET") return json(res, 405, { error: "method_not_allowed" });
|
|
269
|
+
|
|
270
|
+
if (parts[1] === "overview" && parts.length === 2) return json(res, 200, await overview());
|
|
271
|
+
if (parts[1] === "version" && parts.length === 2) {
|
|
272
|
+
return json(res, 200, await checkForUpdate({ force: url.searchParams.get("force") === "1" }));
|
|
273
|
+
}
|
|
274
|
+
if (parts[1] === "sessions" && parts.length === 3) {
|
|
275
|
+
const body = await session(decodeURIComponent(parts[2]));
|
|
276
|
+
return body ? json(res, 200, body) : json(res, 404, { error: "session_not_found" });
|
|
277
|
+
}
|
|
278
|
+
if (parts[1] === "workers" && parts.length === 3) {
|
|
279
|
+
const body = await worker(decodeURIComponent(parts[2]));
|
|
280
|
+
return body ? json(res, 200, body) : json(res, 404, { error: "task_not_found" });
|
|
281
|
+
}
|
|
282
|
+
if (parts[1] === "workers" && parts.length === 4 && parts[3] === "events") {
|
|
283
|
+
const taskId = decodeURIComponent(parts[2]);
|
|
284
|
+
if (!(await getTask(taskId))) return json(res, 404, { error: "task_not_found" });
|
|
285
|
+
return json(res, 200, await events(taskId, url));
|
|
286
|
+
}
|
|
287
|
+
return json(res, 404, { error: "not_found" });
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export function startWebServer({ storeOrigin = "default" } = {}) {
|
|
291
|
+
const server = createServer((req, res) => {
|
|
292
|
+
route(req, res).catch((error) => {
|
|
293
|
+
process.stderr.write(`[codex-supervisor-web] ${error.stack ?? error.message}\n`);
|
|
294
|
+
if (!res.headersSent) json(res, 500, { error: "internal", message: error.message });
|
|
295
|
+
else res.end();
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
// A taken port is the one error a person will actually hit, usually because
|
|
300
|
+
// a board is already running. Say that, not a stack trace.
|
|
301
|
+
server.on("error", (error) => {
|
|
302
|
+
if (error.code === "EADDRINUSE") {
|
|
303
|
+
process.stderr.write(
|
|
304
|
+
`codex-supervisor-web: port ${port} on ${host} is already in use.\n` +
|
|
305
|
+
` A board may already be running at http://${host}:${port}\n` +
|
|
306
|
+
` To run a second one, pick another port: SUPERVISOR_WEB_PORT=8080 codex-supervisor-web\n`
|
|
307
|
+
);
|
|
308
|
+
process.exit(1);
|
|
309
|
+
}
|
|
310
|
+
process.stderr.write(`codex-supervisor-web: ${error.message}\n`);
|
|
311
|
+
process.exit(1);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
server.listen(port, host, () => {
|
|
315
|
+
process.stdout.write(`codex-supervisor web view on http://${host}:${port}\n store: ${supervisorRoot} (${storeOrigin})\n`);
|
|
316
|
+
});
|
|
317
|
+
return server;
|
|
318
|
+
}
|
package/src/web-server.js
CHANGED
|
@@ -1,302 +1,49 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
// session has done. It reads the same SQLite file the MCP server writes and
|
|
5
|
-
// never writes to it, apart from the same orphan reconcile every read does.
|
|
2
|
+
// Entry for `codex-supervisor-web`. Decides which store to open, then starts
|
|
3
|
+
// the board (web-app.js).
|
|
6
4
|
//
|
|
7
|
-
// codex-supervisor-web
|
|
5
|
+
// codex-supervisor-web # http://127.0.0.1:7877
|
|
8
6
|
// SUPERVISOR_WEB_PORT=8080 codex-supervisor-web
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const port = Number(process.env.SUPERVISOR_WEB_PORT ?? 7877);
|
|
31
|
-
const host = process.env.SUPERVISOR_WEB_HOST ?? "127.0.0.1";
|
|
32
|
-
const NO_SESSION = "none";
|
|
33
|
-
|
|
34
|
-
const MIME = {
|
|
35
|
-
".html": "text/html; charset=utf-8",
|
|
36
|
-
".js": "text/javascript; charset=utf-8",
|
|
37
|
-
".css": "text/css; charset=utf-8",
|
|
38
|
-
".json": "application/json; charset=utf-8",
|
|
39
|
-
".svg": "image/svg+xml",
|
|
40
|
-
".woff2": "font/woff2",
|
|
41
|
-
".woff": "font/woff",
|
|
42
|
-
".png": "image/png",
|
|
43
|
-
".ico": "image/x-icon"
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
function clip(value, maxChars) {
|
|
47
|
-
if (value === null || value === undefined) return null;
|
|
48
|
-
const text = String(value);
|
|
49
|
-
return text.length <= maxChars ? text : `${text.slice(0, maxChars)}…`;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function durationMs(task) {
|
|
53
|
-
const start = Date.parse(task.started_at ?? task.created_at ?? "");
|
|
54
|
-
if (Number.isNaN(start)) return null;
|
|
55
|
-
const end = Date.parse(task.completed_at ?? "");
|
|
56
|
-
const stop = Number.isNaN(end) ? Date.now() : end;
|
|
57
|
-
return Math.max(0, stop - start);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// The row every list shows. No prompt, no raw events: those are per-worker
|
|
61
|
-
// reads, so a session with forty threads stays one cheap request.
|
|
62
|
-
function workerRow(task) {
|
|
63
|
-
return {
|
|
64
|
-
id: task.id,
|
|
65
|
-
title: task.title,
|
|
66
|
-
status: task.status,
|
|
67
|
-
phase: task.phase ?? null,
|
|
68
|
-
session_id: task.session_id ?? null,
|
|
69
|
-
thread_id: task.thread_id ?? null,
|
|
70
|
-
goal: task.goal_objective ?? null,
|
|
71
|
-
goal_status: task.goal_status ?? null,
|
|
72
|
-
model: task.model ?? null,
|
|
73
|
-
sandbox: task.sandbox,
|
|
74
|
-
project_root: task.project_root,
|
|
75
|
-
worktree_path: task.worktree_path ?? null,
|
|
76
|
-
branch: task.worktree_path ? `codex/${task.id}` : null,
|
|
77
|
-
base_commit: task.base_commit ?? null,
|
|
78
|
-
owned_paths: task.owned_paths ?? [],
|
|
79
|
-
depends_on: task.depends_on ?? [],
|
|
80
|
-
followup_of: task.followup_of ?? null,
|
|
81
|
-
resumed_from: task.resumed_from ?? null,
|
|
82
|
-
run_count: task.run_count ?? 0,
|
|
83
|
-
current_action: clip(task.current_action, 300),
|
|
84
|
-
last_message: clip(task.last_message, 400),
|
|
85
|
-
changed_file_count: liveChangeCount(task),
|
|
86
|
-
command_count: (task.commands ?? []).length,
|
|
87
|
-
error: task.error ?? null,
|
|
88
|
-
exit_code: task.exit_code ?? null,
|
|
89
|
-
pid: task.pid ?? null,
|
|
90
|
-
created_at: task.created_at || null,
|
|
91
|
-
started_at: task.started_at || null,
|
|
92
|
-
updated_at: task.updated_at || null,
|
|
93
|
-
completed_at: task.completed_at || null,
|
|
94
|
-
duration_ms: durationMs(task)
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// Changed-file counts for the list. The stored changed_files on a row only
|
|
99
|
-
// knows about file_change events; the truth is the worktree diff, which costs
|
|
100
|
-
// two git calls per worker. Terminal rows never change again, so their count
|
|
101
|
-
// is memoised on (id, updated_at); running rows are re-read every time.
|
|
102
|
-
const changeCountCache = new Map();
|
|
103
|
-
function liveChangeCount(task) {
|
|
104
|
-
const key = `${task.id}:${task.updated_at}:${task.status}`;
|
|
105
|
-
const cached = changeCountCache.get(key);
|
|
106
|
-
if (cached !== undefined && !ACTIVE_STATUSES.has(task.status)) return cached;
|
|
107
|
-
const count = readTaskChanges(task).length;
|
|
108
|
-
changeCountCache.set(key, count);
|
|
109
|
-
if (changeCountCache.size > 2000) changeCountCache.delete(changeCountCache.keys().next().value);
|
|
110
|
-
return count;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function sessionKey(task) {
|
|
114
|
-
return task.session_id || NO_SESSION;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function latest(values) {
|
|
118
|
-
return values.filter(Boolean).sort().at(-1) ?? null;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// What a session is about. The title the main thread recorded wins; without
|
|
122
|
-
// one, the goals of its workers are the next best description.
|
|
123
|
-
function aboutSession(meta, tasks) {
|
|
124
|
-
if (meta?.title) return { title: meta.title, note: meta.note ?? null, derived: false };
|
|
125
|
-
const goals = Array.from(new Set(tasks.map((task) => task.goal_objective).filter(Boolean)));
|
|
126
|
-
const summary = goals.slice(0, 3).map((goal) => clip(goal, 60)).join(";");
|
|
127
|
-
return { title: summary || null, note: null, derived: true };
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function sessionRow(key, tasks, meta = null) {
|
|
131
|
-
const counts = {};
|
|
132
|
-
for (const task of tasks) counts[task.status] = (counts[task.status] ?? 0) + 1;
|
|
133
|
-
const active = tasks.filter((task) => ACTIVE_STATUSES.has(task.status));
|
|
134
|
-
const byActivity = [...tasks].sort((a, b) => String(b.updated_at ?? "").localeCompare(String(a.updated_at ?? "")));
|
|
135
|
-
const lastReport = byActivity.find((task) => task.last_message);
|
|
136
|
-
const about = aboutSession(meta, tasks);
|
|
137
|
-
return {
|
|
138
|
-
id: key,
|
|
139
|
-
unsessioned: key === NO_SESSION,
|
|
140
|
-
title: about.title,
|
|
141
|
-
note: about.note,
|
|
142
|
-
title_derived: about.derived,
|
|
143
|
-
worker_count: tasks.length,
|
|
144
|
-
active_count: active.length,
|
|
145
|
-
counts,
|
|
146
|
-
project_roots: Array.from(new Set(tasks.map((task) => task.project_root).filter(Boolean))),
|
|
147
|
-
first_created_at: tasks.map((task) => task.created_at).filter(Boolean).sort()[0] ?? null,
|
|
148
|
-
last_activity_at: latest(tasks.map((task) => task.updated_at)),
|
|
149
|
-
// What the session is doing right now: one line per running thread.
|
|
150
|
-
now: active.map((task) => ({
|
|
151
|
-
id: task.id,
|
|
152
|
-
title: task.title,
|
|
153
|
-
phase: task.phase ?? null,
|
|
154
|
-
current_action: clip(task.current_action, 160)
|
|
155
|
-
})),
|
|
156
|
-
last_report: lastReport
|
|
157
|
-
? { id: lastReport.id, title: lastReport.title, text: clip(lastReport.last_message, 240), at: lastReport.updated_at }
|
|
158
|
-
: null
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
async function overview() {
|
|
163
|
-
await reconcileDetachedActiveTasks();
|
|
164
|
-
const tasks = await readTasks();
|
|
165
|
-
const metas = new Map((await readSessions()).map((row) => [row.id, row]));
|
|
166
|
-
const groups = new Map();
|
|
167
|
-
for (const task of tasks) {
|
|
168
|
-
const key = sessionKey(task);
|
|
169
|
-
if (!groups.has(key)) groups.set(key, []);
|
|
170
|
-
groups.get(key).push(task);
|
|
171
|
-
}
|
|
172
|
-
const sessions = Array.from(groups, ([key, group]) => sessionRow(key, group, metas.get(key) ?? null)).sort((a, b) => {
|
|
173
|
-
if (a.active_count !== b.active_count) return b.active_count - a.active_count;
|
|
174
|
-
return String(b.last_activity_at ?? "").localeCompare(String(a.last_activity_at ?? ""));
|
|
175
|
-
});
|
|
176
|
-
return {
|
|
177
|
-
generated_at: new Date().toISOString(),
|
|
178
|
-
store: supervisorRoot,
|
|
179
|
-
total_workers: tasks.length,
|
|
180
|
-
active_workers: tasks.filter((task) => ACTIVE_STATUSES.has(task.status)).length,
|
|
181
|
-
sessions
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
async function session(key) {
|
|
186
|
-
await reconcileDetachedActiveTasks();
|
|
187
|
-
const tasks = (await readTasks()).filter((task) => sessionKey(task) === key);
|
|
188
|
-
if (tasks.length === 0) return null;
|
|
189
|
-
const meta = (await readSessions()).find((row) => row.id === key) ?? null;
|
|
190
|
-
return {
|
|
191
|
-
...sessionRow(key, tasks, meta),
|
|
192
|
-
workers: tasks.map(workerRow)
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
async function worker(taskId) {
|
|
197
|
-
const task = await getTask(taskId);
|
|
198
|
-
if (!task) return null;
|
|
199
|
-
const reports = await readAgentMessages(taskId, 10);
|
|
200
|
-
// Token spend comes from Codex's own turn.completed usage blocks; one per
|
|
201
|
-
// run, so a resumed worker sums its runs.
|
|
202
|
-
const usage = { input_tokens: 0, cached_input_tokens: 0, output_tokens: 0, reasoning_output_tokens: 0, turns: 0 };
|
|
203
|
-
for (const event of await readTaskEvents(taskId, 50, ["turn.completed"])) {
|
|
204
|
-
const block = event?.usage ?? {};
|
|
205
|
-
usage.turns += 1;
|
|
206
|
-
for (const key of ["input_tokens", "cached_input_tokens", "output_tokens", "reasoning_output_tokens"]) {
|
|
207
|
-
usage[key] += Number(block[key] ?? 0) || 0;
|
|
7
|
+
// SUPERVISOR_HOME=/path/to/state codex-supervisor-web
|
|
8
|
+
//
|
|
9
|
+
// The store has to be the one the MCP server writes. Without SUPERVISOR_HOME
|
|
10
|
+
// in the environment, the nearest .mcp.json above the current directory is
|
|
11
|
+
// read: a project that configures codex-supervisor with its own
|
|
12
|
+
// SUPERVISOR_HOME gets a board on that store without retyping the path.
|
|
13
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
14
|
+
import { dirname, join, resolve } from "node:path";
|
|
15
|
+
|
|
16
|
+
export function storeFromMcpConfig(startDir) {
|
|
17
|
+
let dir = resolve(startDir);
|
|
18
|
+
for (;;) {
|
|
19
|
+
const file = join(dir, ".mcp.json");
|
|
20
|
+
if (existsSync(file)) {
|
|
21
|
+
try {
|
|
22
|
+
const config = JSON.parse(readFileSync(file, "utf8"));
|
|
23
|
+
const home = config?.mcpServers?.["codex-supervisor"]?.env?.SUPERVISOR_HOME;
|
|
24
|
+
if (typeof home === "string" && home.trim()) return { home: resolve(dir, home.trim()), file };
|
|
25
|
+
} catch {
|
|
26
|
+
// an unreadable .mcp.json is somebody else's problem; keep walking
|
|
27
|
+
}
|
|
208
28
|
}
|
|
29
|
+
const parent = dirname(dir);
|
|
30
|
+
if (parent === dir) return null;
|
|
31
|
+
dir = parent;
|
|
209
32
|
}
|
|
210
|
-
const commands = (task.commands ?? []).slice(-40).map((entry) => ({
|
|
211
|
-
...entry,
|
|
212
|
-
command: clip(entry.command, 600)
|
|
213
|
-
}));
|
|
214
|
-
return {
|
|
215
|
-
...workerRow(task),
|
|
216
|
-
prompt: task.prompt ?? null,
|
|
217
|
-
changed_files: readTaskChanges(task),
|
|
218
|
-
commands,
|
|
219
|
-
reports: reports.length > 0 ? reports : task.last_message ? [task.last_message] : [],
|
|
220
|
-
native_goal: readNativeGoal(task.thread_id),
|
|
221
|
-
usage,
|
|
222
|
-
notices: task.notices ?? null,
|
|
223
|
-
run_log: task.run_log ?? null,
|
|
224
|
-
event_kinds: await listTaskEventKinds(taskId)
|
|
225
|
-
};
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
async function events(taskId, url) {
|
|
229
|
-
const limit = Math.min(1000, Math.max(1, Number(url.searchParams.get("limit") ?? 150)));
|
|
230
|
-
const kinds = url.searchParams.get("kinds")?.split(",").filter(Boolean) ?? null;
|
|
231
|
-
return { task_id: taskId, events: await readTaskEvents(taskId, limit, kinds) };
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
function json(res, status, body) {
|
|
235
|
-
const text = JSON.stringify(body);
|
|
236
|
-
res.writeHead(status, {
|
|
237
|
-
"content-type": "application/json; charset=utf-8",
|
|
238
|
-
"cache-control": "no-store",
|
|
239
|
-
"content-length": Buffer.byteLength(text)
|
|
240
|
-
});
|
|
241
|
-
res.end(text);
|
|
242
33
|
}
|
|
243
34
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
35
|
+
let storeOrigin = "SUPERVISOR_HOME";
|
|
36
|
+
if (!process.env.SUPERVISOR_HOME?.trim()) {
|
|
37
|
+
const found = storeFromMcpConfig(process.cwd());
|
|
38
|
+
if (found) {
|
|
39
|
+
process.env.SUPERVISOR_HOME = found.home;
|
|
40
|
+
storeOrigin = `from ${found.file}`;
|
|
41
|
+
} else {
|
|
42
|
+
storeOrigin = "default, no SUPERVISOR_HOME and no .mcp.json above the current directory";
|
|
249
43
|
}
|
|
250
|
-
const safe = normalize(pathname).replace(/^(\.\.[/\\])+/, "");
|
|
251
|
-
let file = join(distDir, safe);
|
|
252
|
-
if (!file.startsWith(distDir)) {
|
|
253
|
-
res.writeHead(403);
|
|
254
|
-
res.end();
|
|
255
|
-
return;
|
|
256
|
-
}
|
|
257
|
-
if (!existsSync(file) || statSync(file).isDirectory()) file = join(distDir, "index.html");
|
|
258
|
-
const type = MIME[extname(file)] ?? "application/octet-stream";
|
|
259
|
-
res.writeHead(200, {
|
|
260
|
-
"content-type": type,
|
|
261
|
-
"cache-control": file.endsWith("index.html") ? "no-store" : "public, max-age=31536000, immutable"
|
|
262
|
-
});
|
|
263
|
-
createReadStream(file).pipe(res);
|
|
264
44
|
}
|
|
265
45
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
if (req.method !== "GET") return json(res, 405, { error: "method_not_allowed" });
|
|
271
|
-
|
|
272
|
-
if (parts[1] === "overview" && parts.length === 2) return json(res, 200, await overview());
|
|
273
|
-
if (parts[1] === "version" && parts.length === 2) {
|
|
274
|
-
return json(res, 200, await checkForUpdate({ force: url.searchParams.get("force") === "1" }));
|
|
275
|
-
}
|
|
276
|
-
if (parts[1] === "sessions" && parts.length === 3) {
|
|
277
|
-
const body = await session(decodeURIComponent(parts[2]));
|
|
278
|
-
return body ? json(res, 200, body) : json(res, 404, { error: "session_not_found" });
|
|
279
|
-
}
|
|
280
|
-
if (parts[1] === "workers" && parts.length === 3) {
|
|
281
|
-
const body = await worker(decodeURIComponent(parts[2]));
|
|
282
|
-
return body ? json(res, 200, body) : json(res, 404, { error: "task_not_found" });
|
|
283
|
-
}
|
|
284
|
-
if (parts[1] === "workers" && parts.length === 4 && parts[3] === "events") {
|
|
285
|
-
const taskId = decodeURIComponent(parts[2]);
|
|
286
|
-
if (!(await getTask(taskId))) return json(res, 404, { error: "task_not_found" });
|
|
287
|
-
return json(res, 200, await events(taskId, url));
|
|
288
|
-
}
|
|
289
|
-
return json(res, 404, { error: "not_found" });
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
const server = createServer((req, res) => {
|
|
293
|
-
route(req, res).catch((error) => {
|
|
294
|
-
process.stderr.write(`[codex-supervisor-web] ${error.stack ?? error.message}\n`);
|
|
295
|
-
if (!res.headersSent) json(res, 500, { error: "internal", message: error.message });
|
|
296
|
-
else res.end();
|
|
297
|
-
});
|
|
298
|
-
});
|
|
299
|
-
|
|
300
|
-
server.listen(port, host, () => {
|
|
301
|
-
process.stdout.write(`codex-supervisor web view on http://${host}:${port} (store: ${supervisorRoot})\n`);
|
|
302
|
-
});
|
|
46
|
+
// paths.js reads SUPERVISOR_HOME when it is first imported, so the import
|
|
47
|
+
// has to come after the detection above.
|
|
48
|
+
const { startWebServer } = await import("./web-app.js");
|
|
49
|
+
startWebServer({ storeOrigin });
|