codex-supervisor-mcp 0.0.0-stage → 0.4.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.md +260 -2
- package/package.json +69 -4
- package/skills/codex-supervisor/SKILL.md +114 -0
- package/src/cleanup.js +128 -0
- package/src/codex-runner.js +540 -0
- package/src/event-parser.js +176 -0
- package/src/goal-store.js +51 -0
- package/src/mcp-server.js +599 -0
- package/src/monitor.jsx +298 -0
- package/src/paths.js +22 -0
- package/src/prompt.js +39 -0
- package/src/setup.js +290 -0
- package/src/status.js +37 -0
- package/src/task-store.js +453 -0
- package/src/truncate.js +135 -0
- package/src/worktree.js +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 zriyo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,261 @@
|
|
|
1
|
-
#
|
|
1
|
+
# codex-supervisor-mcp
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
让一个主线程(Claude Code、Codex、任何 MCP 客户端)同时指挥多个 Codex worker 干活。
|
|
4
|
+
|
|
5
|
+
一个 worker 一个独立 Git worktree,一个 worker 一个原生 Codex goal,状态全部落盘。主线程负责派单、等待、读结果、断线重连、取消。
|
|
6
|
+
|
|
7
|
+
## 它解决什么
|
|
8
|
+
|
|
9
|
+
直接让多个 agent 同时改一个仓库,结果是互相覆盖、没人知道谁改了什么。这个 MCP 把「派单」和「记账」从模型上下文里拿出来,落到磁盘上:
|
|
10
|
+
|
|
11
|
+
| 问题 | 做法 |
|
|
12
|
+
|---|---|
|
|
13
|
+
| 多个 worker 互相踩文件 | 每个 worker 一个 Git worktree;`ownedPaths` 在派单前做冲突检测 |
|
|
14
|
+
| 主线程上下文塞不下 worker 的过程 | 事件流写 `data/runs/<taskId>.jsonl`;读事件有 `limit` / `maxChars` / `kinds` 三个闸门 |
|
|
15
|
+
| 主线程读一次就被顶爆上下文 | `get_orchestration_overview` 把全部 worker 压到 2k token 以内 |
|
|
16
|
+
| 进程重启后不知道谁还在跑 | `supervisor.sqlite` 里一行一个 work 快照,含 `thread_id` |
|
|
17
|
+
| 换个会话就接不上之前的 worker | `resume_codex_worker` 走 `codex exec resume <thread_id>`,接的是同一个 Codex 会话 |
|
|
18
|
+
| 分不清「跑失败了」和「进程没了」 | 状态机把 `failed` 和 `lost` 分开 |
|
|
19
|
+
|
|
20
|
+
## 架构
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
MCP client (Claude Code / Codex / ...)
|
|
24
|
+
│ stdio (MCP)
|
|
25
|
+
▼
|
|
26
|
+
codex-supervisor-mcp
|
|
27
|
+
│ spawn: codex exec --json
|
|
28
|
+
▼
|
|
29
|
+
Codex worker ──► data/runs/<taskId>.jsonl 原始 JSONL 事件流
|
|
30
|
+
──► supervisor.sqlite tasks + task_events
|
|
31
|
+
──► worktrees/<taskId>/ 独立 Git worktree
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## 安装
|
|
35
|
+
|
|
36
|
+
需要 Node.js >= 22 和 `codex` CLI 在 `PATH` 里。
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npm install -g codex-supervisor-mcp
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
全局安装会自动完成两件事(`postinstall`):
|
|
43
|
+
|
|
44
|
+
1. 把配套 skill 装进检测到的客户端目录:`~/.claude/skills/`、`~/.agents/skills/`、`~/.codex/skills/`。
|
|
45
|
+
2. 用客户端自带的 CLI 注册 MCP:`claude mcp add -s user` / `codex mcp add`。
|
|
46
|
+
|
|
47
|
+
目标 skill 已存在且内容不同时,先写一份 `SKILL.md.bak-<时间戳>` 再覆盖。任何一步失败都不会让安装失败。
|
|
48
|
+
|
|
49
|
+
重跑、修复、或只装某一个客户端:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
codex-supervisor-setup # 自动检测客户端
|
|
53
|
+
codex-supervisor-setup --target claude,codex
|
|
54
|
+
codex-supervisor-setup --skill-only # 只装 skill
|
|
55
|
+
codex-supervisor-setup --dry-run # 只打印计划
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
自动安装只在**全局安装**时发生。`npx`、`--ignore-scripts`、以及被别人当项目依赖装的场景都不会触发。要跳过用 `CODEX_SUPERVISOR_SKIP_SETUP=1`;要补装手动跑一次 `codex-supervisor-setup`。
|
|
59
|
+
|
|
60
|
+
### 手动配置到 Claude Code
|
|
61
|
+
|
|
62
|
+
```json
|
|
63
|
+
{
|
|
64
|
+
"mcpServers": {
|
|
65
|
+
"codex-supervisor": {
|
|
66
|
+
"command": "npx",
|
|
67
|
+
"args": ["-y", "codex-supervisor-mcp"]
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### 配置到 Codex
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
codex mcp add codex-supervisor -- npx -y codex-supervisor-mcp
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## 工具
|
|
80
|
+
|
|
81
|
+
13 个工具。
|
|
82
|
+
|
|
83
|
+
| 工具 | 入参 | 作用 |
|
|
84
|
+
|---|---|---|
|
|
85
|
+
| `create_codex_worker` | `task`, `cwd`, **`ownedPaths`**, **`goal`**, `session_id`, `dependsOn`, `sandbox`, `model`, `reasoningEffort`, `title`, `skipGitRepoCheck` | 起一个 worker |
|
|
86
|
+
| `create_codex_followup_worker` | `task_id`, `followup_prompt`, `session_id`, + 同上 | 起一个**新会话**,把老 worker 的 prompt、状态、近期事件拼进去 |
|
|
87
|
+
| `resume_codex_worker` | `task_id`, `prompt` | 接**同一个** Codex 会话继续跑 |
|
|
88
|
+
| `list_codex_workers` | `status`, `includeHistory`, `includeDetails` | 列出 worker,默认只看在跑的 |
|
|
89
|
+
| `get_orchestration_overview` | `status`, `limit` | 全部 worker 的紧凑状态表,默认给主线程用 |
|
|
90
|
+
| `wait_codex_workers` | `task_ids`, `mode`(any/all), `timeoutMinutes`, `includeEvents`, `eventLimit`, `eventMaxChars`, `eventKinds` | 阻塞等到终态,回摘要 |
|
|
91
|
+
| `get_codex_worker_status` | `task_id` | 单个 worker 的完整记录 |
|
|
92
|
+
| `get_codex_worker_events` | `task_id`, `limit`, `maxChars`, `kinds` | 读原始事件流 |
|
|
93
|
+
| `get_worker_goal` | `task_id` | 读 worker 的 goal(supervisor 侧 + Codex 原生) |
|
|
94
|
+
| `get_worker_summary` | `task_id` | 单个 worker 的一段话总结,主线程收尾时读这个 |
|
|
95
|
+
| `get_session_works` | `session_id` | 一个 session 派出去的全部 worker,按时间升序。主线程挂了之后靠它找回那批活 |
|
|
96
|
+
| `search_works` | `query`, `limit` | 在 title / goal / prompt / last_message 里做子串匹配,从新到旧 |
|
|
97
|
+
| `cancel_codex_worker` | `task_id` | 终止 worker,进程句柄不在本进程时按 pid 兜底 |
|
|
98
|
+
|
|
99
|
+
### 为什么 `ownedPaths` 和 `goal` 是必填
|
|
100
|
+
|
|
101
|
+
- `ownedPaths`:这个 worker 允许写的路径(相对 `cwd` 或绝对路径都行)。派单前和所有在跑的 worker 求交集,重叠就拒绝,返回冲突的 `task_id`,不会先建 worktree 再报错。
|
|
102
|
+
- `goal`:`{ objective, tokenBudget? }`。这是主线程做 L1 总结的锚点,也是判断 work 该不该结束的依据。
|
|
103
|
+
|
|
104
|
+
`dependsOn` 记的是 worker 之间的依赖边,落库,主线程查得到谁等谁。
|
|
105
|
+
|
|
106
|
+
### 读事件的三个闸门
|
|
107
|
+
|
|
108
|
+
`get_codex_worker_events` 的三个参数把读的成本压住:
|
|
109
|
+
|
|
110
|
+
| 参数 | 作用 |
|
|
111
|
+
|---|---|
|
|
112
|
+
| `limit` | 最多几条 |
|
|
113
|
+
| `maxChars` | 单条事件里超长的字符串从中间截断(保留头尾) |
|
|
114
|
+
| `kinds` | 只看指定的几类事件,先过滤再取 `limit` |
|
|
115
|
+
|
|
116
|
+
截断规则是照抄 Codex 自己的 `TruncationPolicy::Bytes`:中间截、保留头尾,前面加
|
|
117
|
+
|
|
118
|
+
```
|
|
119
|
+
Warning: truncated output (original token count: N)
|
|
120
|
+
Total output lines: M
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
`get_orchestration_overview` 的预算同样是硬保证:字段上限逐级收紧,直到整包 JSON 落在 7000 字节(约 1750 token)以内,返回值里的 `approx_tokens` 就是它自己的实际成本。
|
|
124
|
+
|
|
125
|
+
## work 状态机
|
|
126
|
+
|
|
127
|
+
`status` 是生命周期,一个 work 一个值,不会被进度覆盖:
|
|
128
|
+
|
|
129
|
+
| 值 | 含义 |
|
|
130
|
+
|---|---|
|
|
131
|
+
| `queued` | 已派单,未启动 |
|
|
132
|
+
| `running` | 运行中 |
|
|
133
|
+
| `completed` | 成功 |
|
|
134
|
+
| `failed` | 失败(非零退出码、`turn.failed`、spawn 失败) |
|
|
135
|
+
| `cancelled` | 被 `cancel_codex_worker` 中断 |
|
|
136
|
+
| `lost` | 被外部信号杀掉、或 MCP 进程消失,没写终止事件 |
|
|
137
|
+
|
|
138
|
+
`phase` 只在 `running` 时有值,到终态归 `null`:
|
|
139
|
+
|
|
140
|
+
```
|
|
141
|
+
starting → thinking → command → editing → reporting
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
`failed` 和 `lost` 分开是有用的:前者要看日志找原因,后者直接重跑。
|
|
145
|
+
|
|
146
|
+
### goal 和 work 的映射
|
|
147
|
+
|
|
148
|
+
Codex 原生 goal 有六个状态,映射到 work 状态:
|
|
149
|
+
|
|
150
|
+
| goal 状态 | work status | 说明 |
|
|
151
|
+
|---|---|---|
|
|
152
|
+
| `active` | `running` | 正常推进 |
|
|
153
|
+
| `paused` | `running` | 暂停,等人 |
|
|
154
|
+
| `blocked` | `running` | 卡住,需要主线程介入 |
|
|
155
|
+
| `usageLimited` / `usage_limited` | `failed` | 配额用尽 |
|
|
156
|
+
| `budgetLimited` / `budget_limited` | `failed` | 预算用尽 |
|
|
157
|
+
| `complete` | `completed` | 达成 |
|
|
158
|
+
|
|
159
|
+
`paused` 和 `blocked` 不是结束,是「需要人管」。`get_orchestration_overview` 会把这两个单独列进 `needs_attention`,不用靠超时猜。
|
|
160
|
+
|
|
161
|
+
## 状态存储
|
|
162
|
+
|
|
163
|
+
默认写在 `~/.codex-supervisor/`:
|
|
164
|
+
|
|
165
|
+
| 文件 | 内容 |
|
|
166
|
+
|---|---|
|
|
167
|
+
| `data/supervisor.sqlite` | `tasks`(一行一个 work)+ `task_events`(结构化事件,带 `seq`) |
|
|
168
|
+
| `data/runs/<taskId>.jsonl` | Codex `--json` 原始输出,逐行事件 |
|
|
169
|
+
| `worktrees/<taskId>/` | 该 worker 的独立 Git worktree |
|
|
170
|
+
|
|
171
|
+
老版本(0.1.x)的库会在第一次打开时自动迁移:`editing` / `command` / `command_completed` / `reporting` 这些原本塞在 `status` 里的过程值会被拆到 `phase`,`status` 归到 `running`,一行不丢。迁移在一个 `BEGIN IMMEDIATE` 事务里做,多个进程同时启动也只会有一个真的迁移。
|
|
172
|
+
|
|
173
|
+
`changed_files` 有两个来源:Codex 的 `file_change` 事件,以及 worker 结束后读自己 worktree 的 `git status --porcelain`。后者是兜底——worker 用 shell 命令(`printf > file`)改文件时不会产生 `file_change` 事件,只有 worktree 知道真相。
|
|
174
|
+
|
|
175
|
+
## 环境变量
|
|
176
|
+
|
|
177
|
+
| 变量 | 默认值 | 作用 |
|
|
178
|
+
|---|---|---|
|
|
179
|
+
| `SUPERVISOR_HOME` | `~/.codex-supervisor` | 状态根目录(sqlite / 事件流 / worktree 都在它下面) |
|
|
180
|
+
| `CODEX_HOME` | `~/.codex` | 只读,用来读 Codex 原生的 `goals_1.sqlite` |
|
|
181
|
+
| `CODEX_BIN` | `codex`(走 `PATH`) | Codex CLI 可执行文件路径 |
|
|
182
|
+
| `GIT_BIN` | `git`(走 `PATH`) | Git 可执行文件路径 |
|
|
183
|
+
|
|
184
|
+
如果 MCP 客户端启动时 `PATH` 里没有 `codex`(GUI 启动的应用常见),显式设置 `CODEX_BIN`:
|
|
185
|
+
|
|
186
|
+
```json
|
|
187
|
+
{
|
|
188
|
+
"mcpServers": {
|
|
189
|
+
"codex-supervisor": {
|
|
190
|
+
"command": "npx",
|
|
191
|
+
"args": ["-y", "codex-supervisor-mcp"],
|
|
192
|
+
"env": { "CODEX_BIN": "/usr/local/bin/codex" }
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## 已知限制
|
|
199
|
+
|
|
200
|
+
- **worker 的生命周期绑在 MCP 进程上**:`codex` 是 MCP 进程的子进程,MCP 被 kill 时 worker 会被留下。这些 worker 会在下一次 `list_codex_workers` / `get_orchestration_overview` 时被结算成 `lost`。真正的解法是常驻 daemon,见 Roadmap。
|
|
201
|
+
- **不往 Codex 原生 goal 写数据**:`goals_1.sqlite` 归 Codex 所有,本 MCP 只读。worker 起来后由它自己调 Codex 的 `create_goal` 建原生 goal,本 MCP 只负责在派单时把这段指令拼进 prompt。
|
|
202
|
+
- **原生 goal 依赖模型照做**:`codex exec` 不会自动建 goal,是派单时那段指令让 worker 建的。模型偶尔漏调,这时 `native_goal` 为 `null`,`get_worker_goal` 退回派单时记录的那份,不影响状态机。
|
|
203
|
+
- **`search_works` 是子串匹配,不是全文索引**:几百条 work 的规模下 `LIKE` 扫描足够快,也省掉一套索引的维护成本。上到几万条再谈别的。
|
|
204
|
+
- **跨进程取消按 pid**:进程句柄不在本进程时,会用落库的 pid 发 `SIGTERM`,并且要求 `ps` 里那个 pid 的命令行包含 `codex`,避免误杀被复用的 pid。
|
|
205
|
+
- **`create_codex_followup_worker` 和 `resume_codex_worker` 不是一回事**:前者开新会话、靠文本重述上下文;后者接同一个会话。要细节不丢就用后者。
|
|
206
|
+
- **`session_id` 由派单方自己传,本 MCP 不生成**:一批活传同一个值,`get_session_works` 才能把它们归到一起。不传就是 `NULL`,事后按 session 找不回来。
|
|
207
|
+
|
|
208
|
+
## Roadmap
|
|
209
|
+
|
|
210
|
+
1. ~~去个人化:`CODEX_BIN` / `SUPERVISOR_HOME` / `GIT_BIN` 环境变量~~ ✅
|
|
211
|
+
2. ~~work 状态机:`status` 和 `phase` 拆开,新增 `lost`~~ ✅
|
|
212
|
+
3. ~~工具面:`ownedPaths` / `goal` / `dependsOn`,新增 4 个工具,事件闸门~~ ✅
|
|
213
|
+
4. ~~跨会话续接:`thread_id` 落库 + `resume_codex_worker`~~ ✅
|
|
214
|
+
5. ~~并发稳定性:两个崩溃点、所有权冲突、多进程写库~~ ✅
|
|
215
|
+
6. ~~session 维度:`session_id` 落库 + `get_session_works` 断线重连~~
|
|
216
|
+
7. ~~按关键词找历史 work:`search_works`~~
|
|
217
|
+
8. ~~worker 原生 goal:派单时注入 `create_goal` 指令~~
|
|
218
|
+
9. 常驻 daemon:派单和进程生命周期从 MCP 进程里拿出来
|
|
219
|
+
10. React 实时看板
|
|
220
|
+
|
|
221
|
+
砍掉不做的(说明理由,免得以后又想起来):
|
|
222
|
+
|
|
223
|
+
| 想法 | 为什么不必要 |
|
|
224
|
+
|---|---|
|
|
225
|
+
| 向量检索 | `search_works` 的子串匹配在几百条规模下更快、零维护。两家的官方记忆系统(Codex `memories`、Claude auto-memory)也都是文件检索,不是向量 |
|
|
226
|
+
| `usage_count` 排序 | 实测 80 个 work 里只有 2 个被回头引用过,计数器全是 0/1,排序等于没排 |
|
|
227
|
+
| 复用 Codex 的 `usage_count` | 那个数统计的是"这篇记忆被引用了几次",跟"主线程查了几次"不是一回事 |
|
|
228
|
+
| 三层记忆索引 | 现在的数据量撑不起一层索引,`get_worker_summary` 已经够了 |
|
|
229
|
+
|
|
230
|
+
## 配合 skill 用
|
|
231
|
+
|
|
232
|
+
仓库里带了 `skills/codex-supervisor/SKILL.md`,装完之后可以放到主线程的 skill 目录,让它自己知道什么时候该派单:
|
|
233
|
+
|
|
234
|
+
```bash
|
|
235
|
+
# Claude Code
|
|
236
|
+
mkdir -p ~/.claude/skills/codex-supervisor
|
|
237
|
+
cp "$(npm root -g)/codex-supervisor-mcp/skills/codex-supervisor/SKILL.md" ~/.claude/skills/codex-supervisor/
|
|
238
|
+
|
|
239
|
+
# Codex / 其他读取 ~/.agents/skills 的客户端
|
|
240
|
+
mkdir -p ~/.agents/skills/codex-supervisor
|
|
241
|
+
cp "$(npm root -g)/codex-supervisor-mcp/skills/codex-supervisor/SKILL.md" ~/.agents/skills/codex-supervisor/
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
skill 里写了派单流程、读结果该按什么顺序、`failed` 和 `lost` 怎么区分、断线怎么接。
|
|
245
|
+
|
|
246
|
+
## 开发
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
npm install
|
|
250
|
+
npm test # 确定性测试:截断移植、状态机、v1→v2 迁移、13 项端到端回归
|
|
251
|
+
npm run test:real # 用真 codex CLI 跑端到端(含跨进程 resume、worktree 隔离)
|
|
252
|
+
npm run smoke # 基础冒烟(真 codex)
|
|
253
|
+
npm run smoke:mcp # MCP 协议冒烟
|
|
254
|
+
npm run monitor # Ink 终端看板
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
`npm test` 用 `src/test-fixtures/fake-codex.js` 回放固定的 Codex 事件流,跑得快且确定。`npm run test:real` 会真的调 `codex exec`,慢一些,但它才是「跟最新 Codex 兼容」的证据。
|
|
258
|
+
|
|
259
|
+
## License
|
|
260
|
+
|
|
261
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,71 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "codex-supervisor-mcp",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "MCP server that dispatches and supervises parallel Codex CLI workers, one isolated Git worktree each, with a persisted work state machine.",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/zriyox/codex-supervisor-mcp.git"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/zriyox/codex-supervisor-mcp#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/zriyox/codex-supervisor-mcp/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"mcp",
|
|
17
|
+
"model-context-protocol",
|
|
18
|
+
"codex",
|
|
19
|
+
"claude",
|
|
20
|
+
"orchestration",
|
|
21
|
+
"multi-agent",
|
|
22
|
+
"worktree",
|
|
23
|
+
"supervisor"
|
|
24
|
+
],
|
|
25
|
+
"bin": {
|
|
26
|
+
"codex-supervisor-mcp": "./src/mcp-server.js",
|
|
27
|
+
"codex-supervisor-setup": "./src/setup.js"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"src/mcp-server.js",
|
|
31
|
+
"src/setup.js",
|
|
32
|
+
"src/codex-runner.js",
|
|
33
|
+
"src/task-store.js",
|
|
34
|
+
"src/goal-store.js",
|
|
35
|
+
"src/event-parser.js",
|
|
36
|
+
"src/worktree.js",
|
|
37
|
+
"src/truncate.js",
|
|
38
|
+
"src/status.js",
|
|
39
|
+
"src/paths.js",
|
|
40
|
+
"src/prompt.js",
|
|
41
|
+
"src/cleanup.js",
|
|
42
|
+
"src/monitor.jsx",
|
|
43
|
+
"skills/",
|
|
44
|
+
"README.md",
|
|
45
|
+
"LICENSE"
|
|
46
|
+
],
|
|
47
|
+
"scripts": {
|
|
48
|
+
"postinstall": "node ./src/setup.js --auto --quiet",
|
|
49
|
+
"setup": "node ./src/setup.js",
|
|
50
|
+
"start": "node ./src/mcp-server.js",
|
|
51
|
+
"monitor": "tsx ./src/monitor.jsx",
|
|
52
|
+
"cleanup": "node ./src/cleanup.js",
|
|
53
|
+
"test": "node ./src/truncate-test.js && node ./src/status-test.js && node ./src/migration-test.js && node ./src/setup-test.js && node ./src/regression-test.js",
|
|
54
|
+
"test:real": "node ./src/real-codex-test.js",
|
|
55
|
+
"smoke": "node ./src/smoke-test.js",
|
|
56
|
+
"smoke:followup": "node ./src/followup-smoke-test.js",
|
|
57
|
+
"smoke:mcp": "node ./src/mcp-smoke-test.js",
|
|
58
|
+
"smoke:mcp-wait": "node ./src/mcp-wait-smoke-test.js"
|
|
59
|
+
},
|
|
60
|
+
"dependencies": {
|
|
61
|
+
"@modelcontextprotocol/sdk": "^1.21.0"
|
|
62
|
+
},
|
|
63
|
+
"optionalDependencies": {
|
|
64
|
+
"ink": "^7.0.2",
|
|
65
|
+
"react": "^19.2.6",
|
|
66
|
+
"tsx": "^4.21.0"
|
|
67
|
+
},
|
|
68
|
+
"engines": {
|
|
69
|
+
"node": ">=22"
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: codex-supervisor
|
|
3
|
+
description: 把 Codex CLI 当并行 worker 派活、盯进度、收结果。用户说"派 codex 干""并行跑""开几个 worker""这批活分几路""多 agent 做"时用;工作线程断了要接回来、要查以前派过什么活,也用这个。
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# codex-supervisor
|
|
7
|
+
|
|
8
|
+
主线程派活给 Codex worker,一个 worker 一个独立 Git worktree,状态全部落盘。
|
|
9
|
+
|
|
10
|
+
## 什么时候用
|
|
11
|
+
|
|
12
|
+
- 一批能拆开的活:改 5 个模块、写 6 篇文档、清点 3 个仓库
|
|
13
|
+
- 活偏大,不想让它占自己的上下文
|
|
14
|
+
- 跑到一半可能断,需要能接回来
|
|
15
|
+
|
|
16
|
+
**不用**:一个文件的小改、纯问答、需要来回确认的活。这些自己干更快。
|
|
17
|
+
|
|
18
|
+
## 一次派单
|
|
19
|
+
|
|
20
|
+
1. 拆活。每路写清楚:干什么、能写哪些文件、怎么算做完。
|
|
21
|
+
2. `create_codex_worker` 派出去。
|
|
22
|
+
3. 派完就 `wait_codex_workers` 等,别轮询。
|
|
23
|
+
4. 收结果走 `get_worker_summary`,别一上来读事件流。
|
|
24
|
+
|
|
25
|
+
`ownedPaths` 和 `goal` 必填,不是可选项。
|
|
26
|
+
|
|
27
|
+
| 参数 | 怎么填 |
|
|
28
|
+
|---|---|
|
|
29
|
+
| `task` | 这一路具体干什么。要能让对方直接开工,别写"优化一下" |
|
|
30
|
+
| `cwd` | 项目根目录。worker 在它的 worktree 里干活,不动你的工作区 |
|
|
31
|
+
| `ownedPaths` | 这个 worker 允许写的路径。和正在跑的 worker 重叠会被拒,返回冲突的 `task_id` |
|
|
32
|
+
| `goal.objective` | 一句话说清这路活的目标 |
|
|
33
|
+
| `session_id` | 同一批派单传同一个值。断线重连靠它 |
|
|
34
|
+
| `dependsOn` | 有先后依赖时填上游的 `task_id` |
|
|
35
|
+
|
|
36
|
+
一次派一批:
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
create_codex_worker × N (同一个 session_id)
|
|
40
|
+
wait_codex_workers (task_ids: [...], mode: "all")
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## 读结果别把上下文撑爆
|
|
44
|
+
|
|
45
|
+
按这个顺序读,能不动后面就别动:
|
|
46
|
+
|
|
47
|
+
| 想干什么 | 用哪个 | 代价 |
|
|
48
|
+
|---|---|---|
|
|
49
|
+
| 看全部 worker 现在什么状态 | `get_orchestration_overview` | ~1750 token,封顶 |
|
|
50
|
+
| 收一路活的结果 | `get_worker_summary` | 一段话 |
|
|
51
|
+
| 看某个 worker 干了什么 | `get_codex_worker_events` | 用 `limit` / `kinds` / `maxChars` 压 |
|
|
52
|
+
| 看某个 worker 的完整记录 | `get_codex_worker_status` | 中等,比 overview 一行细 |
|
|
53
|
+
| 看 goal 和 token 消耗 | `get_worker_goal` | 很小 |
|
|
54
|
+
|
|
55
|
+
`get_codex_worker_events` 的三个闸门:`kinds` 先过滤再取 `limit`;`maxChars` 把超长字符串中间截断;不知道有哪些 `kinds` 时先看返回里的 `available_kinds`。
|
|
56
|
+
|
|
57
|
+
**别做**:一次 `limit: 200` 拉全量事件。那是几万 token。
|
|
58
|
+
|
|
59
|
+
## 中断了怎么接
|
|
60
|
+
|
|
61
|
+
先分清是哪种断:
|
|
62
|
+
|
|
63
|
+
| 情况 | 用什么 | 效果 |
|
|
64
|
+
|---|---|---|
|
|
65
|
+
| 想接着**同一个** Codex 会话干 | `resume_codex_worker` | 上下文不丢,`thread_id` 不变 |
|
|
66
|
+
| 想开**新会话**,带着老活的摘要干 | `create_codex_followup_worker` | 新线程,靠文本重述 |
|
|
67
|
+
| 主线程自己挂了,要找回那批活 | `get_session_works` | 按 `session_id` 列全部 |
|
|
68
|
+
|
|
69
|
+
主线程重启后第一件事:`get_session_works({ session_id })`。一次拿到那批活的清单和状态,不用凭记忆猜。
|
|
70
|
+
|
|
71
|
+
`resume_codex_worker` 只有在原任务记了 `thread_id` 时能用。没记过就用 `create_codex_followup_worker`。
|
|
72
|
+
|
|
73
|
+
## 找以前的活
|
|
74
|
+
|
|
75
|
+
`search_works({ query })` 在 title、goal、prompt、last_message 里做子串匹配,从新到旧返回。
|
|
76
|
+
|
|
77
|
+
想按状态筛就用 `list_codex_workers({ status, includeHistory: true })`。
|
|
78
|
+
|
|
79
|
+
## 状态怎么读
|
|
80
|
+
|
|
81
|
+
`status` 是生命周期,一个 work 一个值:`queued` / `running` / `completed` / `failed` / `cancelled` / `lost`。
|
|
82
|
+
|
|
83
|
+
`phase` 只在跑着的时候有值:`starting` → `thinking` → `command` → `editing` → `reporting`。
|
|
84
|
+
|
|
85
|
+
**`failed` 和 `lost` 不是一回事**:
|
|
86
|
+
|
|
87
|
+
- `failed`:跑完了但没成。去看日志和 `error`。
|
|
88
|
+
- `lost`:进程被外部信号杀了,或者 MCP 进程自己没了。什么都没说。**直接重跑**,不用查日志。
|
|
89
|
+
|
|
90
|
+
`paused` 和 `blocked` 的 goal 不是结束,是等人管。`get_orchestration_overview` 会把它们列进 `needs_attention`。
|
|
91
|
+
|
|
92
|
+
## goal 是干嘛的
|
|
93
|
+
|
|
94
|
+
每个 worker 起手会自己调 Codex 的 `create_goal`,把 `goal.objective` 建成那个线程的原生 goal;干完调 `update_goal` 标 `complete`。所以:
|
|
95
|
+
|
|
96
|
+
- `get_worker_goal` 里的 `native_goal` 是 Codex 自己的记录,带真实 token 和时间消耗
|
|
97
|
+
- Go 到 Codex 界面上能直接看到这个 goal
|
|
98
|
+
- `resume_codex_worker` **不会**再建 goal(有未完成 goal 时 `create_goal` 会失败)
|
|
99
|
+
|
|
100
|
+
## 常见坑
|
|
101
|
+
|
|
102
|
+
- **worker 的命绑在 MCP 进程上**。MCP 被 kill,正在跑的 worker 会被留下,下次读状态时结算成 `lost`。
|
|
103
|
+
- **`ownedPaths` 冲突是好事**。说明两路活会踩同一个文件,这时候该改拆法,不是绕过检测。
|
|
104
|
+
- **worktree 只隔离工作区,不隔离端口和数据库**。多路活要是都会连同一个 dev server 或同一个库,得自己错开。
|
|
105
|
+
- **`cancel_codex_worker` 跨进程靠 pid**。要求那个 pid 的命令行里带 `codex`,防止误杀复用 pid 的进程。
|
|
106
|
+
|
|
107
|
+
## 收尾
|
|
108
|
+
|
|
109
|
+
一批活全部到终态后:
|
|
110
|
+
|
|
111
|
+
1. `get_orchestration_overview` 确认没有 `needs_attention`
|
|
112
|
+
2. 逐路 `get_worker_summary`
|
|
113
|
+
3. 失败的区分 `failed`(查原因)和 `lost`(重跑)
|
|
114
|
+
4. 改动在各自 worktree 的 `codex/<taskId>` 分支上,合并由主线程决定
|
package/src/cleanup.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { rm, stat } from "node:fs/promises";
|
|
3
|
+
import { basename, join } from "node:path";
|
|
4
|
+
import { deleteTasks, readTasks, vacuumStore } from "./task-store.js";
|
|
5
|
+
import { runsDir, worktreesDir } from "./paths.js";
|
|
6
|
+
import { TERMINAL_STATUSES } from "./status.js";
|
|
7
|
+
|
|
8
|
+
function parseArgs(argv) {
|
|
9
|
+
const options = {
|
|
10
|
+
days: 14,
|
|
11
|
+
dryRun: true,
|
|
12
|
+
includeFailed: true,
|
|
13
|
+
vacuum: false
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
17
|
+
const arg = argv[index];
|
|
18
|
+
if (arg === "--days") {
|
|
19
|
+
options.days = Number(argv[index + 1]);
|
|
20
|
+
index += 1;
|
|
21
|
+
} else if (arg === "--apply") {
|
|
22
|
+
options.dryRun = false;
|
|
23
|
+
} else if (arg === "--dry-run") {
|
|
24
|
+
options.dryRun = true;
|
|
25
|
+
} else if (arg === "--keep-failed") {
|
|
26
|
+
options.includeFailed = false;
|
|
27
|
+
} else if (arg === "--vacuum") {
|
|
28
|
+
options.vacuum = true;
|
|
29
|
+
} else if (arg === "--help" || arg === "-h") {
|
|
30
|
+
options.help = true;
|
|
31
|
+
} else {
|
|
32
|
+
throw new Error(`Unknown argument: ${arg}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!Number.isFinite(options.days) || options.days < 0) {
|
|
37
|
+
throw new Error("--days must be a non-negative number");
|
|
38
|
+
}
|
|
39
|
+
return options;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function printHelp() {
|
|
43
|
+
console.log(`Usage: npm run cleanup -- [options]
|
|
44
|
+
|
|
45
|
+
Options:
|
|
46
|
+
--dry-run Show what would be deleted. Default.
|
|
47
|
+
--apply Actually delete matching rows, JSONL logs, and worktrees.
|
|
48
|
+
--days <n> Keep tasks newer than n days. Default: 14.
|
|
49
|
+
--keep-failed Preserve failed/cancelled tasks even when older than --days.
|
|
50
|
+
--vacuum Vacuum SQLite after deleting rows.
|
|
51
|
+
-h, --help Show help.
|
|
52
|
+
`);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function isTerminal(task) {
|
|
56
|
+
return TERMINAL_STATUSES.has(task.status);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function isDeleteCandidate(task, cutoffMs, includeFailed) {
|
|
60
|
+
if (!isTerminal(task)) return false;
|
|
61
|
+
if (!includeFailed && ["failed", "cancelled", "lost"].includes(task.status)) return false;
|
|
62
|
+
const completedAt = Date.parse(task.completed_at ?? task.updated_at ?? task.created_at);
|
|
63
|
+
return Number.isFinite(completedAt) && completedAt < cutoffMs;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function safeDeleteFile(path, dryRun) {
|
|
67
|
+
try {
|
|
68
|
+
await stat(path);
|
|
69
|
+
if (!dryRun) await rm(path, { force: true });
|
|
70
|
+
return true;
|
|
71
|
+
} catch {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function safeDeleteDir(path, dryRun) {
|
|
77
|
+
try {
|
|
78
|
+
await stat(path);
|
|
79
|
+
if (!dryRun) await rm(path, { recursive: true, force: true });
|
|
80
|
+
return true;
|
|
81
|
+
} catch {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const options = parseArgs(process.argv.slice(2));
|
|
87
|
+
if (options.help) {
|
|
88
|
+
printHelp();
|
|
89
|
+
process.exit(0);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const cutoffMs = Date.now() - options.days * 24 * 60 * 60 * 1000;
|
|
93
|
+
const tasks = await readTasks();
|
|
94
|
+
const candidates = tasks.filter((task) => isDeleteCandidate(task, cutoffMs, options.includeFailed));
|
|
95
|
+
const taskIds = candidates.map((task) => task.id);
|
|
96
|
+
|
|
97
|
+
const deleted = {
|
|
98
|
+
tasks: taskIds.length,
|
|
99
|
+
jsonl: 0,
|
|
100
|
+
worktrees: 0
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
for (const task of candidates) {
|
|
104
|
+
const logPath = task.run_log ?? join(runsDir, `${task.id}.jsonl`);
|
|
105
|
+
if (await safeDeleteFile(logPath, options.dryRun)) deleted.jsonl += 1;
|
|
106
|
+
if (task.worktree_path && basename(task.worktree_path) === task.id) {
|
|
107
|
+
if (await safeDeleteDir(task.worktree_path, options.dryRun)) deleted.worktrees += 1;
|
|
108
|
+
} else {
|
|
109
|
+
const fallbackWorktree = join(worktreesDir, task.id);
|
|
110
|
+
if (await safeDeleteDir(fallbackWorktree, options.dryRun)) deleted.worktrees += 1;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (!options.dryRun && taskIds.length > 0) {
|
|
115
|
+
await deleteTasks(taskIds);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (!options.dryRun && options.vacuum) {
|
|
119
|
+
await vacuumStore();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
console.log(JSON.stringify({
|
|
123
|
+
dry_run: options.dryRun,
|
|
124
|
+
keep_days: options.days,
|
|
125
|
+
cutoff: new Date(cutoffMs).toISOString(),
|
|
126
|
+
candidates: taskIds,
|
|
127
|
+
deleted
|
|
128
|
+
}, null, 2));
|