@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/dist/worktree.js
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { randomBytes } from "node:crypto";
|
|
4
|
+
import { spawnSync } from "node:child_process";
|
|
5
|
+
const META_NAME = ".baton-worktree.json";
|
|
6
|
+
function git(cwd, args) {
|
|
7
|
+
const ran = spawnSync("git", args, { cwd, encoding: "utf8" });
|
|
8
|
+
return { status: ran.status ?? 1, stdout: ran.stdout || "", stderr: ran.stderr || "" };
|
|
9
|
+
}
|
|
10
|
+
function strings(value) {
|
|
11
|
+
return Array.isArray(value) ? value.filter((item) => typeof item === "string") : [];
|
|
12
|
+
}
|
|
13
|
+
export function loadWorktreeConfig(repo) {
|
|
14
|
+
const file = path.join(repo, ".baton.json");
|
|
15
|
+
if (!fs.existsSync(file))
|
|
16
|
+
return { copy: [], symlink: [], post_create: [] };
|
|
17
|
+
let raw;
|
|
18
|
+
try {
|
|
19
|
+
raw = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
throw new Error(`.baton.json 无效:${error.message}`);
|
|
23
|
+
}
|
|
24
|
+
const section = raw && typeof raw === "object" && "worktree" in raw
|
|
25
|
+
? raw.worktree
|
|
26
|
+
: undefined;
|
|
27
|
+
if (!section || typeof section !== "object")
|
|
28
|
+
return { copy: [], symlink: [], post_create: [] };
|
|
29
|
+
return {
|
|
30
|
+
dir: typeof section.dir === "string" ? section.dir : undefined,
|
|
31
|
+
base: typeof section.base === "string" ? section.base : undefined,
|
|
32
|
+
copy: strings(section.copy),
|
|
33
|
+
symlink: strings(section.symlink),
|
|
34
|
+
post_create: strings(section.post_create),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export function resolveWorktreeRoot(repo, cfg) {
|
|
38
|
+
if (cfg.dir)
|
|
39
|
+
return path.resolve(repo, cfg.dir);
|
|
40
|
+
const hidden = path.join(repo, ".worktrees");
|
|
41
|
+
const plain = path.join(repo, "worktrees");
|
|
42
|
+
if (fs.existsSync(hidden))
|
|
43
|
+
return hidden;
|
|
44
|
+
if (fs.existsSync(plain))
|
|
45
|
+
return plain;
|
|
46
|
+
return hidden;
|
|
47
|
+
}
|
|
48
|
+
export function ensureIgnored(repo, worktreeRoot) {
|
|
49
|
+
const rel = path.relative(repo, worktreeRoot) || ".";
|
|
50
|
+
if (rel.startsWith("..") || path.isAbsolute(rel)) {
|
|
51
|
+
throw new Error("worktree 根目录必须在仓库内。");
|
|
52
|
+
}
|
|
53
|
+
const check = git(repo, ["check-ignore", "-q", rel]);
|
|
54
|
+
if (check.status === 0)
|
|
55
|
+
return false;
|
|
56
|
+
const ignorePath = path.join(repo, ".gitignore");
|
|
57
|
+
const entry = rel.endsWith("/") ? rel : `${rel}/`;
|
|
58
|
+
const existing = fs.existsSync(ignorePath) ? fs.readFileSync(ignorePath, "utf8") : "";
|
|
59
|
+
const prefix = existing && !existing.endsWith("\n") ? "\n" : "";
|
|
60
|
+
fs.writeFileSync(ignorePath, `${existing}${prefix}${entry}\n`);
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
export function assertNotNestedWorktree(cwd) {
|
|
64
|
+
const dir = git(cwd, ["rev-parse", "--git-dir"]);
|
|
65
|
+
const common = git(cwd, ["rev-parse", "--git-common-dir"]);
|
|
66
|
+
if (dir.status !== 0 || common.status !== 0)
|
|
67
|
+
throw new Error("当前目录不是 git 仓库。");
|
|
68
|
+
const gitDir = path.resolve(cwd, dir.stdout.trim());
|
|
69
|
+
const gitCommon = path.resolve(cwd, common.stdout.trim());
|
|
70
|
+
if (gitDir === gitCommon)
|
|
71
|
+
return;
|
|
72
|
+
const superproject = git(cwd, ["rev-parse", "--show-superproject-working-tree"]);
|
|
73
|
+
if (superproject.status === 0 && superproject.stdout.trim())
|
|
74
|
+
return;
|
|
75
|
+
throw new Error("已在 linked worktree 中,请回到主仓再 add(禁止嵌套)。");
|
|
76
|
+
}
|
|
77
|
+
export function resolveRepoRoot(cwd) {
|
|
78
|
+
const ran = git(cwd, ["rev-parse", "--show-toplevel"]);
|
|
79
|
+
if (ran.status !== 0)
|
|
80
|
+
throw new Error("当前目录不是 git 仓库。");
|
|
81
|
+
return ran.stdout.trim();
|
|
82
|
+
}
|
|
83
|
+
function defaultBase(repo, configured) {
|
|
84
|
+
if (configured)
|
|
85
|
+
return configured;
|
|
86
|
+
for (const candidate of ["main", "master"]) {
|
|
87
|
+
const check = git(repo, ["rev-parse", "--verify", candidate]);
|
|
88
|
+
if (check.status === 0)
|
|
89
|
+
return candidate;
|
|
90
|
+
}
|
|
91
|
+
const head = git(repo, ["rev-parse", "--abbrev-ref", "HEAD"]);
|
|
92
|
+
if (head.status === 0 && head.stdout.trim() && head.stdout.trim() !== "HEAD")
|
|
93
|
+
return head.stdout.trim();
|
|
94
|
+
throw new Error("无法探测 base 分支,请在 .baton.json 或 --base 指定。");
|
|
95
|
+
}
|
|
96
|
+
function writeMeta(worktreePath, meta) {
|
|
97
|
+
fs.writeFileSync(path.join(worktreePath, META_NAME), `${JSON.stringify(meta, null, 2)}\n`);
|
|
98
|
+
}
|
|
99
|
+
function readMeta(worktreePath) {
|
|
100
|
+
const file = path.join(worktreePath, META_NAME);
|
|
101
|
+
if (!fs.existsSync(file))
|
|
102
|
+
return null;
|
|
103
|
+
try {
|
|
104
|
+
return JSON.parse(fs.readFileSync(file, "utf8"));
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
export function addWorktree(cwd, options = {}) {
|
|
111
|
+
assertNotNestedWorktree(cwd);
|
|
112
|
+
const repo = resolveRepoRoot(cwd);
|
|
113
|
+
const cfg = loadWorktreeConfig(repo);
|
|
114
|
+
const root = resolveWorktreeRoot(repo, cfg);
|
|
115
|
+
const gitignore_updated = ensureIgnored(repo, root);
|
|
116
|
+
fs.mkdirSync(root, { recursive: true });
|
|
117
|
+
const name = options.name || randomBytes(4).toString("hex");
|
|
118
|
+
if (!/^[A-Za-z0-9._-]+$/.test(name))
|
|
119
|
+
throw new Error("worktree name 含非法字符。");
|
|
120
|
+
const worktreePath = path.join(root, name);
|
|
121
|
+
if (fs.existsSync(worktreePath))
|
|
122
|
+
throw new Error(`worktree 已存在:${worktreePath}`);
|
|
123
|
+
const branch = options.branch || `baton/${name}`;
|
|
124
|
+
const base = defaultBase(repo, options.base ?? cfg.base);
|
|
125
|
+
const created = git(repo, ["worktree", "add", "-b", branch, worktreePath, base]);
|
|
126
|
+
if (created.status !== 0) {
|
|
127
|
+
throw new Error(`git worktree add 失败:${(created.stderr || created.stdout).trim()}`);
|
|
128
|
+
}
|
|
129
|
+
const meta = { name, branch, base, created_at: new Date().toISOString() };
|
|
130
|
+
writeMeta(worktreePath, meta);
|
|
131
|
+
try {
|
|
132
|
+
prepareWorktree(repo, worktreePath, cfg);
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
cleanupWorktree(repo, worktreePath, branch);
|
|
136
|
+
throw error;
|
|
137
|
+
}
|
|
138
|
+
return { status: "ok", path: worktreePath, branch, base, name, gitignore_updated };
|
|
139
|
+
}
|
|
140
|
+
export function listWorktrees(cwd) {
|
|
141
|
+
const repo = resolveRepoRoot(cwd);
|
|
142
|
+
const cfg = loadWorktreeConfig(repo);
|
|
143
|
+
const root = resolveWorktreeRoot(repo, cfg);
|
|
144
|
+
if (!fs.existsSync(root))
|
|
145
|
+
return [];
|
|
146
|
+
const items = [];
|
|
147
|
+
for (const entry of fs.readdirSync(root, { withFileTypes: true })) {
|
|
148
|
+
if (!entry.isDirectory())
|
|
149
|
+
continue;
|
|
150
|
+
const worktreePath = path.join(root, entry.name);
|
|
151
|
+
const meta = readMeta(worktreePath);
|
|
152
|
+
if (!meta)
|
|
153
|
+
continue;
|
|
154
|
+
items.push({ name: meta.name, path: worktreePath, branch: meta.branch, base: meta.base });
|
|
155
|
+
}
|
|
156
|
+
return items.sort((a, b) => a.name.localeCompare(b.name));
|
|
157
|
+
}
|
|
158
|
+
/** Minimal gitignore-style match for basename patterns (`*` / `?`). */
|
|
159
|
+
export function matchBasename(name, pattern) {
|
|
160
|
+
const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*").replace(/\?/g, ".");
|
|
161
|
+
return new RegExp(`^${escaped}$`).test(name);
|
|
162
|
+
}
|
|
163
|
+
function prepareWorktree(repo, worktreePath, cfg) {
|
|
164
|
+
const names = fs.readdirSync(repo);
|
|
165
|
+
for (const pattern of cfg.copy) {
|
|
166
|
+
for (const name of names) {
|
|
167
|
+
if (!matchBasename(name, pattern))
|
|
168
|
+
continue;
|
|
169
|
+
const src = path.join(repo, name);
|
|
170
|
+
const dest = path.join(worktreePath, name);
|
|
171
|
+
const stat = fs.lstatSync(src);
|
|
172
|
+
if (stat.isDirectory())
|
|
173
|
+
fs.cpSync(src, dest, { recursive: true });
|
|
174
|
+
else
|
|
175
|
+
fs.copyFileSync(src, dest);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
for (const pattern of cfg.symlink) {
|
|
179
|
+
for (const name of names) {
|
|
180
|
+
if (!matchBasename(name, pattern))
|
|
181
|
+
continue;
|
|
182
|
+
const src = path.join(repo, name);
|
|
183
|
+
const dest = path.join(worktreePath, name);
|
|
184
|
+
if (!fs.existsSync(src))
|
|
185
|
+
continue;
|
|
186
|
+
if (fs.existsSync(dest))
|
|
187
|
+
fs.rmSync(dest, { recursive: true, force: true });
|
|
188
|
+
fs.symlinkSync(src, dest);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
for (const command of cfg.post_create) {
|
|
192
|
+
const ran = spawnSync(command, {
|
|
193
|
+
cwd: worktreePath,
|
|
194
|
+
shell: true,
|
|
195
|
+
encoding: "utf8",
|
|
196
|
+
env: { ...process.env, BATON_MAIN_REPO: repo, BATON_WORKTREE: worktreePath },
|
|
197
|
+
timeout: 600_000,
|
|
198
|
+
});
|
|
199
|
+
if (ran.status !== 0) {
|
|
200
|
+
const detail = (ran.stderr || ran.stdout || "").trim() || `exit ${ran.status}`;
|
|
201
|
+
throw new Error(`post_create 失败(${command}):${detail}`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
function cleanupWorktree(repo, worktreePath, branch) {
|
|
206
|
+
git(repo, ["worktree", "remove", "--force", worktreePath]);
|
|
207
|
+
git(repo, ["branch", "-D", branch]);
|
|
208
|
+
git(repo, ["worktree", "prune"]);
|
|
209
|
+
}
|
|
210
|
+
function resolveManagedPath(cwd, nameOrPath) {
|
|
211
|
+
const repo = resolveRepoRoot(cwd);
|
|
212
|
+
const cfg = loadWorktreeConfig(repo);
|
|
213
|
+
const root = resolveWorktreeRoot(repo, cfg);
|
|
214
|
+
const candidate = path.isAbsolute(nameOrPath) ? path.resolve(nameOrPath) : path.resolve(cwd, nameOrPath);
|
|
215
|
+
const byName = path.join(root, nameOrPath);
|
|
216
|
+
const worktreePath = fs.existsSync(path.join(candidate, META_NAME))
|
|
217
|
+
? candidate
|
|
218
|
+
: fs.existsSync(path.join(byName, META_NAME))
|
|
219
|
+
? byName
|
|
220
|
+
: candidate;
|
|
221
|
+
const rel = path.relative(root, worktreePath);
|
|
222
|
+
if (!rel || rel.startsWith("..") || path.isAbsolute(rel)) {
|
|
223
|
+
throw new Error("只能删除 worktree 根目录下由 baton 管理的树。");
|
|
224
|
+
}
|
|
225
|
+
const meta = readMeta(worktreePath);
|
|
226
|
+
if (!meta)
|
|
227
|
+
throw new Error(`不是 baton 管理的 worktree:${worktreePath}`);
|
|
228
|
+
return { repo, root, worktreePath, meta };
|
|
229
|
+
}
|
|
230
|
+
function isDirty(worktreePath) {
|
|
231
|
+
// Ignore untracked (copy/.baton-worktree.json); only block on tracked edits.
|
|
232
|
+
const status = git(worktreePath, ["status", "--porcelain", "--untracked-files=no"]);
|
|
233
|
+
return status.status === 0 && status.stdout.trim().length > 0;
|
|
234
|
+
}
|
|
235
|
+
function hasUnmergedCommits(repo, branch, base) {
|
|
236
|
+
const ran = git(repo, ["rev-list", "--count", `${base}..${branch}`]);
|
|
237
|
+
if (ran.status !== 0)
|
|
238
|
+
return true;
|
|
239
|
+
return Number(ran.stdout.trim()) > 0;
|
|
240
|
+
}
|
|
241
|
+
export function removeWorktree(cwd, nameOrPath) {
|
|
242
|
+
const { repo, worktreePath, meta } = resolveManagedPath(cwd, nameOrPath);
|
|
243
|
+
if (isDirty(worktreePath)) {
|
|
244
|
+
throw new Error(`worktree 有未提交改动,拒绝删除:${worktreePath}`);
|
|
245
|
+
}
|
|
246
|
+
if (hasUnmergedCommits(repo, meta.branch, meta.base)) {
|
|
247
|
+
throw new Error(`分支 ${meta.branch} 相对 ${meta.base} 仍有未合并提交,拒绝删除。`);
|
|
248
|
+
}
|
|
249
|
+
// ponytail: --force after our tracked-dirty + unmerged checks; untracked copy/meta are expected
|
|
250
|
+
const removed = git(repo, ["worktree", "remove", "--force", worktreePath]);
|
|
251
|
+
if (removed.status !== 0) {
|
|
252
|
+
throw new Error(`git worktree remove 失败:${(removed.stderr || removed.stdout).trim()}`);
|
|
253
|
+
}
|
|
254
|
+
const deleted = git(repo, ["branch", "-d", meta.branch]);
|
|
255
|
+
if (deleted.status !== 0) {
|
|
256
|
+
throw new Error(`删除分支失败:${(deleted.stderr || deleted.stdout).trim()}`);
|
|
257
|
+
}
|
|
258
|
+
git(repo, ["worktree", "prune"]);
|
|
259
|
+
return { status: "ok", path: worktreePath, branch: meta.branch, name: meta.name };
|
|
260
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@speculoai/baton",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "把一块有边界的工作下放到另一家本地 Agent CLI,需要决定时用同一会话继续。",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/SpeculoAI/baton.git"
|
|
9
|
+
},
|
|
10
|
+
"bin": {
|
|
11
|
+
"baton": "dist/cli.js"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"start": "node src/cli.ts",
|
|
18
|
+
"build": "tsc -p tsconfig.build.json",
|
|
19
|
+
"prepack": "npm run build",
|
|
20
|
+
"test": "tsc --noEmit && node --test \"test/**/*.test.ts\"",
|
|
21
|
+
"typecheck": "tsc --noEmit"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@modelcontextprotocol/sdk": "^1.20.2"
|
|
25
|
+
},
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=20.17"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/node": "^24.19.2",
|
|
34
|
+
"typescript": "^7.0.2"
|
|
35
|
+
}
|
|
36
|
+
}
|