@speculoai/baton 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/README.md +459 -2
- package/README.zh-CN.md +460 -0
- package/dist/adapters/agy.js +62 -0
- package/dist/adapters/claude.js +79 -0
- package/dist/adapters/codex.js +105 -0
- package/dist/adapters/common.js +25 -0
- package/dist/adapters/cursor.js +96 -0
- package/dist/adapters/index.js +6 -0
- package/dist/cli.js +162 -0
- package/dist/contract.js +42 -0
- package/dist/git.js +201 -0
- package/dist/install.js +231 -0
- package/dist/mcp.js +90 -0
- package/dist/process.js +23 -0
- package/dist/redact.js +9 -0
- package/dist/scope.js +108 -0
- package/dist/store.js +105 -0
- package/dist/supervisor.js +293 -0
- package/dist/tail.js +64 -0
- package/dist/tasks.js +305 -0
- package/dist/types.js +1 -0
- package/dist/worktree.js +260 -0
- package/package.json +34 -4
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
# Baton
|
|
2
|
+
|
|
3
|
+
[English](README.md) | **简体中文**
|
|
4
|
+
|
|
5
|
+
> 本地 AI Agent 算力桥:**Any Agent as Host, Any Agent as Worker**。
|
|
6
|
+
> 当前使用的 CLI/IDE 作为主控(Host),把有边界的代码任务派给本机安装的其他 Agent CLI(Worker),直接消耗对方订阅里的额度。任务在独立进程里跑,状态落在磁盘上,跑多久都行,随时能看它在干什么。
|
|
7
|
+
|
|
8
|
+
可派工的目标:`claude` (Claude Code)、`codex` (Codex CLI)、`cursor` (Cursor Agent CLI)、`agy` (Antigravity)。
|
|
9
|
+
|
|
10
|
+
> **0.4.0 是破坏性升级**,见 [从 0.3.x 升级](#从-03x-升级)。0.3.0 的说明在 [从 0.2.x 升级](#从-02x-升级)。
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## 目录
|
|
15
|
+
|
|
16
|
+
- [核心工作原理](#核心工作原理)
|
|
17
|
+
- [安装指南 (Installation)](#安装指南-installation)
|
|
18
|
+
- [验证安装](#验证安装)
|
|
19
|
+
- [使用方法](#使用方法)
|
|
20
|
+
- [宿主内的技能](#宿主内的技能)
|
|
21
|
+
- [命令行](#命令行)
|
|
22
|
+
- [状态与存活判定](#状态与存活判定)
|
|
23
|
+
- [停下提问与续轮 (needs_input / reply)](#停下提问与续轮-needs_input--reply)
|
|
24
|
+
- [MCP 工具](#mcp-工具)
|
|
25
|
+
- [任务目录 ~/.baton](#任务目录-baton)
|
|
26
|
+
- [各平台底层调用规范](#各平台底层调用规范)
|
|
27
|
+
- [安全机制](#安全机制)
|
|
28
|
+
- [手工冒烟测试](#手工冒烟测试)
|
|
29
|
+
- [开发与单元测试](#开发与单元测试)
|
|
30
|
+
- [从 0.3.x 升级](#从-03x-升级)
|
|
31
|
+
- [从 0.2.x 升级](#从-02x-升级)
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## 核心工作原理
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
宿主 CLI ──shell──> baton start ──拉起脱离父进程的守护进程──> baton _supervise <id>
|
|
39
|
+
│ │ (立即返回 task_id) │ 持有子 CLI 进程组
|
|
40
|
+
│ ▼ │ 解析流式输出 → 统一事件
|
|
41
|
+
│ ~/.baton/tasks/<id>/ <──────── 唯一写者 ────────┘
|
|
42
|
+
│ ▲
|
|
43
|
+
└──shell/MCP──> baton status / wait / tail / reply / cancel / ls
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
- **任务不依附宿主**:守护进程独立运行。宿主会话退出、MCP 重启、终端关掉,任务照常跑完,之后仍能读到结果。
|
|
47
|
+
- **没有总时长上限**:默认只有空闲超时(连续 600 秒没有任何输出才停),慢模型跑几个小时也行。
|
|
48
|
+
- **过程可见**:各家 CLI 的流式输出被统一成事件(消息、工具调用、工具结果、错误),用 `baton tail -f` 实时看;`status` 直接告诉你它是在动、在安静思考,还是守护进程已经死了。
|
|
49
|
+
- **按轮交互**:子 CLI 停下来提问时,人或主 Agent 用 `baton reply` 在原会话里接着说,每一轮谁说了什么都落盘。
|
|
50
|
+
- **零额外 API Key**:复用本机各家 CLI 已登录的会话与订阅额度。
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## 安装指南 (Installation)
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
npx @speculoai/baton install
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
这条命令会把 `baton` 装成全局命令,并把插件装进你选中的 agent。`--only <agent>` 可重复(`claude`、`codex`、`cursor`、`agy`、`gemini`),`--force` 重装,`--dry-run` 只打印命令。以后更新:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
npx @speculoai/baton@latest update
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
下面各家的命令是同一套步骤,供手动安装。
|
|
67
|
+
|
|
68
|
+
### 前置要求
|
|
69
|
+
|
|
70
|
+
1. **Node.js** >= 20.17。
|
|
71
|
+
2. 本地已安装并登录至少一个受支持的 Agent CLI(`codex`、`claude`、`agy`、`cursor`)。
|
|
72
|
+
3. 通过插件市场安装不会把 `baton` 放进 PATH。运行 `/baton:setup`,或执行 `npm install -g @speculoai/baton`。
|
|
73
|
+
4. (可选)本地开发:
|
|
74
|
+
```bash
|
|
75
|
+
git clone https://github.com/SpeculoAI/baton.git
|
|
76
|
+
cd baton
|
|
77
|
+
npm install
|
|
78
|
+
npm run build
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
### Claude Code
|
|
84
|
+
|
|
85
|
+
Claude Code 原生支持通过插件市场进行扩展。
|
|
86
|
+
|
|
87
|
+
#### 远程一键安装(推荐)
|
|
88
|
+
|
|
89
|
+
在系统终端中运行:
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
claude plugin marketplace add https://github.com/SpeculoAI/baton
|
|
93
|
+
claude plugin install baton@speculoai
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
或者在已开启的 `claude` 交互终端内直接输入 Slash 命令:
|
|
97
|
+
|
|
98
|
+
```text
|
|
99
|
+
/plugin marketplace add https://github.com/SpeculoAI/baton
|
|
100
|
+
/plugin install baton@speculoai
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
#### 本地克隆安装
|
|
104
|
+
|
|
105
|
+
在克隆好的 `baton` 目录下执行:
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
claude plugin marketplace add .
|
|
109
|
+
claude plugin install baton@speculoai
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
### Antigravity (agy)
|
|
115
|
+
|
|
116
|
+
Antigravity 原生支持插件系统与技能自动加载。
|
|
117
|
+
|
|
118
|
+
#### 远程一键安装(推荐)
|
|
119
|
+
|
|
120
|
+
在终端中直接运行:
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
agy plugin install https://github.com/SpeculoAI/baton
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
#### 本地克隆安装
|
|
127
|
+
|
|
128
|
+
在克隆好的 `baton` 目录下执行:
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
agy plugin install .
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
> **更新**:重新执行一次相同的安装指令即可覆盖更新到最新版本。
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
### Codex CLI
|
|
139
|
+
|
|
140
|
+
Codex 命令行支持通过插件体系注册并接入 MCP。
|
|
141
|
+
|
|
142
|
+
#### 终端命令安装
|
|
143
|
+
|
|
144
|
+
在系统终端中运行:
|
|
145
|
+
|
|
146
|
+
```bash
|
|
147
|
+
codex plugin marketplace add https://github.com/SpeculoAI/baton
|
|
148
|
+
codex plugin add baton@speculoai
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
#### 交互式安装
|
|
152
|
+
|
|
153
|
+
在 `codex` 交互界面中输入:
|
|
154
|
+
|
|
155
|
+
```text
|
|
156
|
+
/plugins
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
搜索 `baton`,然后选择 `Install Plugin`。
|
|
160
|
+
|
|
161
|
+
#### 本地克隆安装
|
|
162
|
+
|
|
163
|
+
在克隆好的 `baton` 目录下执行:
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
codex plugin marketplace add .
|
|
167
|
+
codex plugin add baton@speculoai
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
### Codex App
|
|
173
|
+
|
|
174
|
+
适用于 Codex 桌面客户端:
|
|
175
|
+
|
|
176
|
+
1. 打开 Codex App,点击左侧边栏的 **Plugins**。
|
|
177
|
+
2. 搜索 `Baton`(或点击添加自定义市场源:`https://github.com/SpeculoAI/baton`)。
|
|
178
|
+
3. 点击 **Install** 确认安装。
|
|
179
|
+
|
|
180
|
+
---
|
|
181
|
+
|
|
182
|
+
### Cursor (Agent CLI 与 IDE)
|
|
183
|
+
|
|
184
|
+
#### Cursor Agent CLI 终端安装
|
|
185
|
+
|
|
186
|
+
Cursor 官方 Agent CLI 支持直接添加 Git 地址:
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
agent plugin marketplace add https://github.com/SpeculoAI/baton
|
|
190
|
+
agent plugin install baton@speculoai
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
或者在 Cursor Agent 聊天框中输入:
|
|
194
|
+
|
|
195
|
+
```text
|
|
196
|
+
/add-plugin baton
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
#### Cursor IDE (MCP 模式配置)
|
|
200
|
+
|
|
201
|
+
如果你主要使用 Cursor 编辑器窗口,可将其作为 MCP 服务器挂载:
|
|
202
|
+
|
|
203
|
+
在项目根目录 `.cursor/mcp.json`(或全局 Cursor 设置中的 MCP 配置)中添加:
|
|
204
|
+
|
|
205
|
+
```json
|
|
206
|
+
{
|
|
207
|
+
"mcpServers": {
|
|
208
|
+
"baton": {
|
|
209
|
+
"command": "baton",
|
|
210
|
+
"args": ["mcp"]
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
---
|
|
217
|
+
|
|
218
|
+
### Gemini CLI
|
|
219
|
+
|
|
220
|
+
Gemini CLI 采用扩展(Extensions)系统。这里把 baton 装进 Gemini CLI 当宿主用;暂不支持把任务派给 gemini。
|
|
221
|
+
|
|
222
|
+
#### 远程安装
|
|
223
|
+
|
|
224
|
+
在终端中执行:
|
|
225
|
+
|
|
226
|
+
```bash
|
|
227
|
+
gemini extensions install https://github.com/SpeculoAI/baton --consent
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
#### 本地链接模式(推荐用于本地开发)
|
|
231
|
+
|
|
232
|
+
在克隆好的 `baton` 目录下执行:
|
|
233
|
+
|
|
234
|
+
```bash
|
|
235
|
+
gemini extensions link .
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
#### 扩展更新
|
|
239
|
+
|
|
240
|
+
```bash
|
|
241
|
+
gemini extensions update baton
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
---
|
|
245
|
+
|
|
246
|
+
### 通用 MCP 客户端 (Claude Desktop / Windsurf 等)
|
|
247
|
+
|
|
248
|
+
本仓库提供标准的 MCP Stdio 协议服务,可以接入任何兼容 MCP 的客户端(例如 Claude Desktop、Windsurf、Zed、Cline、Roo Code)。
|
|
249
|
+
|
|
250
|
+
在宿主的 MCP 配置文件中添加:
|
|
251
|
+
|
|
252
|
+
```json
|
|
253
|
+
{
|
|
254
|
+
"mcpServers": {
|
|
255
|
+
"baton": {
|
|
256
|
+
"command": "baton",
|
|
257
|
+
"args": ["mcp"]
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
从 Dock 启动的应用常常拿不到 nvm 或 volta 的 PATH。如果终端里 `baton` 可用、MCP 却起不来,把 `command -v baton` 的绝对路径填进 `command`。
|
|
264
|
+
|
|
265
|
+
---
|
|
266
|
+
|
|
267
|
+
## 验证安装
|
|
268
|
+
|
|
269
|
+
```bash
|
|
270
|
+
baton agents
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
输出示例:
|
|
274
|
+
|
|
275
|
+
```json
|
|
276
|
+
{"agents":[{"id":"codex","installed":true,"bin":"/opt/homebrew/bin/codex","version":"codex-cli 0.167.0"},{"id":"claude","installed":true,"bin":"/Users/user/.local/bin/claude","version":"2.1.323 (Claude Code)"},{"id":"cursor","installed":true,"bin":"/Users/user/.local/bin/cursor-agent","version":"2026.11.11-043ef28"},{"id":"agy","installed":true,"bin":"/Users/user/.local/bin/agy","version":"1.5.6"}]}
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
只有 `installed: true` 的 Agent 才可以作为派工目标。
|
|
280
|
+
|
|
281
|
+
---
|
|
282
|
+
|
|
283
|
+
## 使用方法
|
|
284
|
+
|
|
285
|
+
### 宿主内的技能
|
|
286
|
+
|
|
287
|
+
安装插件后,宿主会加载三个技能:
|
|
288
|
+
|
|
289
|
+
- [skills/setup/SKILL.md](skills/setup/SKILL.md):`/baton:setup`,确认 `baton` 命令在 PATH 上
|
|
290
|
+
- [skills/baton-rescue/SKILL.md](skills/baton-rescue/SKILL.md):单跳派工,有界等待,转问或代答子 CLI 的问题
|
|
291
|
+
- [skills/baton-orchestrate/SKILL.md](skills/baton-orchestrate/SKILL.md):多单调度,持依赖图、逐单推进、审查、返工、换家、汇总(不绑定 Speckit / OpenSpec / agent-skills)
|
|
292
|
+
|
|
293
|
+
对宿主说"把这个重构 rescue 给 Codex 做"之类的话,宿主就会用 shell 调 `baton start`,再循环 `baton wait --for 50` 直到结束。技能只依赖 shell 命令,不要求宿主有后台子 Agent。
|
|
294
|
+
|
|
295
|
+
**路由规则**:用户指定了目标就用指定的;没指定时实现与排错默认派给 `codex`,当前会话本身是 Codex 时改派 `claude`;绝不派给自己。
|
|
296
|
+
|
|
297
|
+
### 命令行
|
|
298
|
+
|
|
299
|
+
所有命令都只输出一个 JSON 对象(`tail` 除外),出错时退出码非 0。`baton` 是 `@speculoai/baton` 提供的全局命令。
|
|
300
|
+
|
|
301
|
+
```bash
|
|
302
|
+
baton agents
|
|
303
|
+
baton start --target <codex|claude|cursor|agy> [--prompt <text> | stdin] [--file <p>...] [--effort low|medium|high|ultra]
|
|
304
|
+
[--model <id>] [--idle-timeout <秒>] [--max-duration <秒>] [--cwd <dir>]
|
|
305
|
+
[--scope <glob> ...] [--scope-on-violation warn|fail]
|
|
306
|
+
[--mode readonly|write] [--allow-weak]
|
|
307
|
+
baton status <id>
|
|
308
|
+
baton wait <id>... [--for <秒> | --until-done] # 默认 --for 50;--until-done 阻塞到任一任务离开 running
|
|
309
|
+
baton tail <id> [-f] [--raw] # 人读格式的事件;-f 跟到任务停下;--raw 输出 events.jsonl 原行
|
|
310
|
+
baton reply <id> [--prompt <text> | stdin] [--as host|human] # scope_violation / readonly_violation 后也可续轮
|
|
311
|
+
baton cancel <id>
|
|
312
|
+
baton ls [--all] # 默认只列当前目录的任务,按更新时间倒序
|
|
313
|
+
baton prune --older <天数> # 删除早于 N 天的非 running 任务目录(不级联删 worktree)
|
|
314
|
+
baton worktree add [--name <id>] [--branch <name>] [--base <ref>]
|
|
315
|
+
baton worktree ls
|
|
316
|
+
baton worktree rm <name-or-path> # 脏或未合并则拒绝;干净则 remove + 删本地分支
|
|
317
|
+
baton mcp # 启动 MCP 服务(stdio)
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
- `--effort` 映射到各家原生参数:Codex `-c model_reasoning_effort=...`;Claude/Agy `--effort`(`ultra` 映射为 `max`);Cursor 仅 effort 时 → `--model auto[effort=...]`。
|
|
321
|
+
- `--model <id>`(目前仅 Cursor):按任务指定模型,写入 meta,`reply` 延续。Cursor 规则:仅 model → `--model <id>`;model+effort → `--model '<id>[effort=...]'`;若 id 已含档位(如 `grok-4.7-xhigh`)或已有括号再带 `--effort` 则报错。不会出现两个 `--model`。`status` 含 `model`(请求)与 `model_argv`(传给 CLI 的参数);这不是 provider 确认的“实际模型”。
|
|
322
|
+
- `--idle-timeout` 默认 600:连续这么久没有任何输出字节才停掉。`--max-duration` 默认不限。两者都按单轮计时,`reply` 开新一轮时重新计时。
|
|
323
|
+
- **`--mode readonly|write`(默认 `write`):** 映射到各家只读参数(Codex `--sandbox read-only`、Claude `--permission-mode plan`、Cursor `--mode ask`、Agy `--mode plan`)。写入 meta,`reply` 延续。每轮结束后若有净变更,会把干净的 `success` 升为 `readonly_violation`。无映射的目标默认拒绝,需显式 `--allow-weak`。**兼容性:** 新增终态 `readonly_violation`。
|
|
324
|
+
- **Worktree 与任务解耦:** 先 `baton worktree add`,再 `baton start --cwd <path>`。默认目录 `.worktrees/`(会写入 `.gitignore`);可选 `.baton.json` 配置 `copy` / `symlink` / `post_create`。软链 `node_modules` 在分支依赖不一致时可能互相污染,必要时改用 `post_create` 独立安装。
|
|
325
|
+
- **`--scope`(可选):** 可重复,glob 相对任务 `cwd`。每轮结束后对照**任务起点净变更**(自 baseline HEAD 起的提交 ∪ 工作区 digest 差分)。`--scope-on-violation warn`(默认)保留原终态并写 `scope_violations` + `warning`;`fail` 会把干净的 `success` 升为 `scope_violation`。这是**防失误**,不是沙箱:忽略文件、仓外写入、共享 cwd 并发改动是已知盲区——建议与 worktree 叠用。带 `--scope` 时 cwd 必须是 git 仓库。**兼容性:** 新增终态 `scope_violation` 及可选 `scope_*` 字段。
|
|
326
|
+
|
|
327
|
+
### 状态与存活判定
|
|
328
|
+
|
|
329
|
+
| `status` | 含义 |
|
|
330
|
+
|---|---|
|
|
331
|
+
| `running` | 还在跑,额外带 `liveness` 和 `quiet_for_s` |
|
|
332
|
+
| `needs_input` | 子 CLI 停下来提问,`decision` 里有 `question` 和 `options` |
|
|
333
|
+
| `success` | 本轮完成,带 `summary` 和本轮的 `git_changes`(warn 策略下仍可能有 `scope_violations`) |
|
|
334
|
+
| `scope_violation` | `--scope-on-violation fail` 下越界;可 `reply` |
|
|
335
|
+
| `readonly_violation` | `--mode readonly` 但工作区有净变更;可 `reply` |
|
|
336
|
+
| `failed` / `timed_out` / `cancelled` | 终态,不能再 `reply` |
|
|
337
|
+
| `not_authenticated` / `quota_exceeded` | 对方 CLI 没登录 / 额度用尽 |
|
|
338
|
+
| `not_installed` | `start` 时就返回,目标不在 `PATH` 里 |
|
|
339
|
+
|
|
340
|
+
`running` 时的 `liveness`:
|
|
341
|
+
|
|
342
|
+
| `liveness` | 条件 |
|
|
343
|
+
|---|---|
|
|
344
|
+
| `active` | 60 秒内有输出 |
|
|
345
|
+
| `quiet` | 守护进程活着,但超过 60 秒没有输出(通常是在思考) |
|
|
346
|
+
| `orphaned` | 守护进程没了或心跳超过 30 秒未更新;下一次读会杀掉残留子进程并补写 `failed` |
|
|
347
|
+
|
|
348
|
+
### 停下提问与续轮 (needs_input / reply)
|
|
349
|
+
|
|
350
|
+
派工时 baton 会在 prompt 末尾附上输出约定:需要选择时用 `BATON_DECISION` 块提问,做完时用 `BATON_RESULT` 块总结。子 CLI 提问后任务进入 `needs_input`,进程正常退出。
|
|
351
|
+
|
|
352
|
+
```bash
|
|
353
|
+
baton reply <id> --as human --prompt "选 A"
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
- 在原会话上开下一轮。`--as human` 表示这是人说的话;主 Agent 依据用户先前指示代答时用默认的 `--as host`。
|
|
357
|
+
- `success` 之后也能 `reply` 追问。任务 `running` 时 `reply` 返回 `busy`。
|
|
358
|
+
- 每一轮的作者、输入和结果都记在 `turns/<n>.json`,事后可回看。
|
|
359
|
+
|
|
360
|
+
### MCP 工具
|
|
361
|
+
|
|
362
|
+
MCP 是同一套逻辑的薄壳,进程里不存任何任务状态,重启 MCP 不影响正在跑的任务:
|
|
363
|
+
|
|
364
|
+
| 工具 | 对应命令 |
|
|
365
|
+
|---|---|
|
|
366
|
+
| `list_agents` | `agents` |
|
|
367
|
+
| `delegate_task` | `start`(参数 `target_agent`、`prompt`、`files`、`reasoning_effort`、`idle_timeout_seconds`、`max_duration_seconds`、`cwd`) |
|
|
368
|
+
| `task_status` | `status` |
|
|
369
|
+
| `task_wait` | `wait`(参数 `task_ids`、`wait_seconds`,默认 50,上限 55;等待期间每 15 秒发一次进度通知) |
|
|
370
|
+
| `task_reply` | `reply`(参数 `task_id`、`prompt`、`as`) |
|
|
371
|
+
| `task_cancel` | `cancel` |
|
|
372
|
+
| `task_list` | `ls`(参数 `all`、`cwd`) |
|
|
373
|
+
|
|
374
|
+
---
|
|
375
|
+
|
|
376
|
+
## 任务目录 ~/.baton
|
|
377
|
+
|
|
378
|
+
根目录默认 `~/.baton`,可用 `BATON_HOME` 换位置。目录权限 `0700`,文件 `0600`(里面有 prompt 原文)。
|
|
379
|
+
|
|
380
|
+
```
|
|
381
|
+
~/.baton/tasks/<task_id>/
|
|
382
|
+
meta.json 当前状态(原子写)
|
|
383
|
+
events.jsonl 统一事件流
|
|
384
|
+
turns/<n>.json 第 n 轮:作者、输入、结束状态、摘要、git_changes
|
|
385
|
+
raw/<n>.stdout.log 第 n 轮子进程原始输出,不截断
|
|
386
|
+
raw/<n>.stderr.log
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
任务目录不会自动清理,用 `baton prune --older <天数>` 手动清。
|
|
390
|
+
|
|
391
|
+
---
|
|
392
|
+
|
|
393
|
+
## 各平台底层调用规范
|
|
394
|
+
|
|
395
|
+
| Agent | 首次调用 | 续会话 | 说明 |
|
|
396
|
+
| :--- | :--- | :--- | :--- |
|
|
397
|
+
| **Codex** | `codex exec --json --color never --approve-for-me --skip-git-repo-check --cd <cwd> -` | `codex exec resume --json <thread_id> -` | write 用 `--approve-for-me`(与 `--sandbox` 互斥);readonly 首轮用 `--sandbox read-only`;`exec resume` 无 sandbox 参数,也不接受 `--cd`、`--color` |
|
|
398
|
+
| **Claude Code** | `claude -p --output-format stream-json --verbose --permission-mode acceptEdits --permission-prompts none` | 追加 `--resume <session_id>` | readonly → `--permission-mode plan`;`stream-json` 在 `-p` 下必须配 `--verbose`;hook 事件不进事件流 |
|
|
399
|
+
| **Cursor** | `cursor-agent --print --force --trust --output-format stream-json --workspace <cwd>` | 追加 `--resume <session_id>` | readonly → `--mode ask`(去掉 `--force`,保留 `--trust`);优先找 `cursor-agent`,再找 `agent`,不调用 IDE 的 `cursor` |
|
|
400
|
+
| **Antigravity (agy)** | `agy --print <prompt> --mode accept-edits --output-format stream-json` | 追加 `--conversation <id>` | readonly → `--mode plan`;回复是逐字增量,完整文本取自最终 `result` |
|
|
401
|
+
|
|
402
|
+
解析器认不出的输出行一律记为 `raw` 事件,不会丢。各家的真实输出样本在 `test/fixtures/`。
|
|
403
|
+
|
|
404
|
+
---
|
|
405
|
+
|
|
406
|
+
## 安全机制
|
|
407
|
+
|
|
408
|
+
1. **防递归 (`BATON_DELEGATION=1`)**:子进程环境里带这个变量,下游再调用 baton 会被直接拒绝。
|
|
409
|
+
2. **进程组隔离**:子 CLI 独立成进程组,超时和 `cancel` 会杀掉整组,包括它拉起的孙进程。
|
|
410
|
+
3. **凭据脱敏**:写进事件和摘要的文本先脱敏再截断;原始日志完整保留在受 `0600` 保护的 `raw/` 下。
|
|
411
|
+
4. **id 校验**:所有传入的 task id 先校验是 UUID,再拼路径。
|
|
412
|
+
5. **`--scope` 不是安全边界**:只事后检查 git 可见的净变更。被 ignore 的路径、仓外写入、共享 cwd 并发、orphaned(检查未跑)是已知盲区。
|
|
413
|
+
|
|
414
|
+
---
|
|
415
|
+
|
|
416
|
+
## 手工冒烟测试
|
|
417
|
+
|
|
418
|
+
在一个独立的临时 Git 仓库里跑:
|
|
419
|
+
|
|
420
|
+
1. `baton agents`,确认目标 `installed: true`。
|
|
421
|
+
2. 发起一个会停下提问的任务:
|
|
422
|
+
```bash
|
|
423
|
+
baton start --target codex --effort low --prompt "不要改文件。先停下来问我:选 A 还是 B,用 BATON_DECISION 格式,options: A | B。"
|
|
424
|
+
```
|
|
425
|
+
3. 另开一个终端 `baton tail -f <id>`,能实时看到事件。
|
|
426
|
+
4. `baton wait <id> --for 50` 直到返回 `needs_input`,`decision` 里是 A/B。
|
|
427
|
+
5. `baton reply <id> --as human --prompt "A。回复你选的字母后用 BATON_RESULT 结束。"`,再 `wait` 到 `success`,确认 `session_id` 与第一轮相同。
|
|
428
|
+
|
|
429
|
+
---
|
|
430
|
+
|
|
431
|
+
## 开发与单元测试
|
|
432
|
+
|
|
433
|
+
```bash
|
|
434
|
+
npm test # tsc --noEmit + node --test,不消耗真实模型额度
|
|
435
|
+
npm run typecheck
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
集成测试用 `test/fake-cli/` 里的假 CLI 和临时 `BATON_HOME`,覆盖:父进程被杀后任务照常完成、空闲与总时长超时、取消杀整组、守护进程死亡后的孤儿判定、needs_input → reply 续会话、有界 wait、tail、ls/prune、MCP 重启后仍能读到任务。适配器测试用各家真实输出样本。
|
|
439
|
+
|
|
440
|
+
---
|
|
441
|
+
|
|
442
|
+
## 从 0.3.x 升级
|
|
443
|
+
|
|
444
|
+
- 用 `npx @speculoai/baton install` 安装,用 `npx @speculoai/baton@latest update` 更新。插件 id 是 `baton@speculoai`。
|
|
445
|
+
- MCP 运行 `baton`,参数是 `["mcp"]`。`/baton:setup` 安装全局包,不再往插件目录里执行 npm。
|
|
446
|
+
- Node 要求改为 >= 20.17。发布包由 `tsc` 构建,市场安装不再需要 TypeScript 源码。
|
|
447
|
+
|
|
448
|
+
## 从 0.2.x 升级
|
|
449
|
+
|
|
450
|
+
- **Node 最低 20.17。** 源码是 TypeScript。安装后使用 `baton` 命令。
|
|
451
|
+
- 手工 MCP 配置的 `command` 为 `baton`,`args` 为 `["mcp"]`。
|
|
452
|
+
- CLI 去掉 `delegate`,改为 `start` + `wait` + `reply`;`wait` 用位置参数传 id。
|
|
453
|
+
- MCP 去掉 `delegate_task(session_id)` 续派和 `task_wait_any`:续轮用 `task_reply`,多单等待用 `task_wait(task_ids)`。
|
|
454
|
+
- `needs_decision` 改名 `needs_input`;`timeout_seconds`(上限 1800)改为 `idle_timeout_seconds` + `max_duration_seconds`,默认没有总时长上限。
|
|
455
|
+
- 任务状态从系统临时目录移到 `~/.baton`。
|
|
456
|
+
- **暂不支持 gemini 作为派工目标**(本机鉴权问题,无法验证)。Gemini CLI 作为宿主安装 baton 扩展不受影响。
|
|
457
|
+
|
|
458
|
+
---
|
|
459
|
+
|
|
460
|
+
曾用名:**Agent-Plugin-Agent (APA)**。更早的安装串已失效,请使用 `baton@speculoai`。
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { redact } from "../redact.js";
|
|
2
|
+
import { authHit, which } from "./common.js";
|
|
3
|
+
const AUTH = ["not logged in", "please log in", "authentication required"];
|
|
4
|
+
function event(data) {
|
|
5
|
+
return [{ ts: new Date().toISOString(), turn: 0, ...data }];
|
|
6
|
+
}
|
|
7
|
+
function parse(line) {
|
|
8
|
+
try {
|
|
9
|
+
return JSON.parse(line);
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export const agy = {
|
|
16
|
+
id: "agy",
|
|
17
|
+
readonlyEnforcement: "agent-enforced",
|
|
18
|
+
resolve(env) {
|
|
19
|
+
return which(["agy"], env);
|
|
20
|
+
},
|
|
21
|
+
build({ bin, prompt, sessionId, reasoningEffort, mode }) {
|
|
22
|
+
const args = ["--print", prompt, "--mode", mode === "readonly" ? "plan" : "accept-edits", "--output-format", "stream-json"];
|
|
23
|
+
if (reasoningEffort)
|
|
24
|
+
args.push("--effort", reasoningEffort === "ultra" ? "max" : reasoningEffort);
|
|
25
|
+
if (sessionId)
|
|
26
|
+
args.push("--conversation", sessionId);
|
|
27
|
+
return { command: bin, args, stdin: null };
|
|
28
|
+
},
|
|
29
|
+
sessionIdFrom(line) {
|
|
30
|
+
const obj = parse(line);
|
|
31
|
+
return obj?.event === "init" && typeof obj.conversation_id === "string" ? obj.conversation_id : null;
|
|
32
|
+
},
|
|
33
|
+
parseLine(line) {
|
|
34
|
+
const obj = parse(line);
|
|
35
|
+
if (!obj || typeof obj !== "object" || typeof obj.event !== "string")
|
|
36
|
+
return null;
|
|
37
|
+
if (obj.event === "init")
|
|
38
|
+
return [];
|
|
39
|
+
if (obj.event === "step_update") {
|
|
40
|
+
const step = obj.step_update ?? {};
|
|
41
|
+
// agent_response 的 text_delta 是逐字增量;完整回复在 result.response 里出一次,
|
|
42
|
+
// 否则标记解析只会看到最后一个增量。
|
|
43
|
+
if (step.step_type !== "tool")
|
|
44
|
+
return [];
|
|
45
|
+
const name = String(step.tool_name ?? step.tool_info?.name ?? "tool");
|
|
46
|
+
const detail = redact(JSON.stringify(step.tool_info?.parameters ?? {})).slice(0, 200);
|
|
47
|
+
if (step.state === "ACTIVE")
|
|
48
|
+
return event({ type: "tool_call", name, detail });
|
|
49
|
+
return event({ type: "tool_result", name, ok: step.state === "DONE", detail });
|
|
50
|
+
}
|
|
51
|
+
if (obj.event === "result") {
|
|
52
|
+
const result = obj.result ?? {};
|
|
53
|
+
if (result.status !== "SUCCESS")
|
|
54
|
+
return event({ type: "error", text: redact(String(result.response || result.status || "agy 返回错误")) });
|
|
55
|
+
return typeof result.response === "string" && result.response ? event({ type: "message", text: result.response }) : [];
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
},
|
|
59
|
+
isAuthFailure(text) {
|
|
60
|
+
return authHit(text, AUTH);
|
|
61
|
+
},
|
|
62
|
+
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { redact } from "../redact.js";
|
|
2
|
+
import { authHit, which } from "./common.js";
|
|
3
|
+
const AUTH = ["not logged in", "please run /login", "invalid api key"];
|
|
4
|
+
function stamp(data) {
|
|
5
|
+
return { ts: new Date().toISOString(), turn: 0, ...data };
|
|
6
|
+
}
|
|
7
|
+
function parse(line) {
|
|
8
|
+
try {
|
|
9
|
+
return JSON.parse(line);
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function contentText(content) {
|
|
16
|
+
if (typeof content === "string")
|
|
17
|
+
return content;
|
|
18
|
+
if (!Array.isArray(content))
|
|
19
|
+
return "";
|
|
20
|
+
return content.map((part) => (typeof part?.text === "string" ? part.text : "")).join("");
|
|
21
|
+
}
|
|
22
|
+
export const claude = {
|
|
23
|
+
id: "claude",
|
|
24
|
+
readonlyEnforcement: "agent-enforced",
|
|
25
|
+
resolve(env) {
|
|
26
|
+
return which(["claude"], env);
|
|
27
|
+
},
|
|
28
|
+
build({ bin, prompt, sessionId, reasoningEffort, mode }) {
|
|
29
|
+
// stream-json 在 -p 下必须配 --verbose。
|
|
30
|
+
const permission = mode === "readonly" ? "plan" : "acceptEdits";
|
|
31
|
+
const args = ["-p", "--output-format", "stream-json", "--verbose", "--permission-mode", permission, "--permission-prompts", "none"];
|
|
32
|
+
if (reasoningEffort)
|
|
33
|
+
args.push("--effort", reasoningEffort === "ultra" ? "max" : reasoningEffort);
|
|
34
|
+
if (sessionId)
|
|
35
|
+
args.push("--resume", sessionId);
|
|
36
|
+
args.push(prompt);
|
|
37
|
+
return { command: bin, args, stdin: null };
|
|
38
|
+
},
|
|
39
|
+
sessionIdFrom(line) {
|
|
40
|
+
const obj = parse(line);
|
|
41
|
+
return obj?.type === "system" && obj.subtype === "init" && typeof obj.session_id === "string" ? obj.session_id : null;
|
|
42
|
+
},
|
|
43
|
+
parseLine(line) {
|
|
44
|
+
const obj = parse(line);
|
|
45
|
+
if (!obj || typeof obj !== "object" || typeof obj.type !== "string")
|
|
46
|
+
return null;
|
|
47
|
+
// system:init 由 sessionIdFrom 处理,hook_* 会带出宿主 hook 的输出,不进事件流。
|
|
48
|
+
if (obj.type === "system" || obj.type === "rate_limit_event")
|
|
49
|
+
return [];
|
|
50
|
+
const parts = Array.isArray(obj.message?.content) ? obj.message.content : [];
|
|
51
|
+
if (obj.type === "assistant") {
|
|
52
|
+
const events = [];
|
|
53
|
+
const text = parts.filter((p) => p?.type === "text" && typeof p.text === "string").map((p) => p.text).join("");
|
|
54
|
+
for (const p of parts) {
|
|
55
|
+
if (p?.type !== "tool_use")
|
|
56
|
+
continue;
|
|
57
|
+
const input = p.input ?? {};
|
|
58
|
+
const detail = redact(String(input.command ?? input.file_path ?? input.path ?? JSON.stringify(input))).slice(0, 200);
|
|
59
|
+
events.push(stamp({ type: "tool_call", name: String(p.name ?? "tool"), detail }));
|
|
60
|
+
}
|
|
61
|
+
if (text)
|
|
62
|
+
events.push(stamp({ type: "message", text }));
|
|
63
|
+
return events;
|
|
64
|
+
}
|
|
65
|
+
if (obj.type === "user") {
|
|
66
|
+
return parts
|
|
67
|
+
.filter((p) => p?.type === "tool_result")
|
|
68
|
+
.map((p) => stamp({ type: "tool_result", name: "tool", ok: p.is_error !== true, detail: redact(contentText(p.content)).slice(0, 200) }));
|
|
69
|
+
}
|
|
70
|
+
if (obj.type === "result") {
|
|
71
|
+
// 最终文本已经以 assistant 消息出过一次,这里只报错误。
|
|
72
|
+
return obj.is_error ? [stamp({ type: "error", text: redact(String(obj.result ?? obj.subtype ?? "claude 返回错误")) })] : [];
|
|
73
|
+
}
|
|
74
|
+
return null;
|
|
75
|
+
},
|
|
76
|
+
isAuthFailure(text) {
|
|
77
|
+
return authHit(text, AUTH);
|
|
78
|
+
},
|
|
79
|
+
};
|