opencode-swarm-plugin 0.26.1 → 0.27.2
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/.turbo/turbo-build.log +4 -4
- package/CHANGELOG.md +38 -0
- package/README.md +43 -46
- package/bin/swarm.ts +10 -54
- package/dist/compaction-hook.d.ts +57 -0
- package/dist/compaction-hook.d.ts.map +1 -0
- package/dist/hive.d.ts +741 -0
- package/dist/hive.d.ts.map +1 -0
- package/dist/index.d.ts +139 -23
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1353 -350
- package/dist/learning.d.ts +9 -9
- package/dist/plugin.js +1176 -350
- package/dist/schemas/cell-events.d.ts +1352 -0
- package/dist/schemas/{bead-events.d.ts.map → cell-events.d.ts.map} +1 -1
- package/dist/schemas/{bead.d.ts → cell.d.ts} +173 -29
- package/dist/schemas/cell.d.ts.map +1 -0
- package/dist/schemas/index.d.ts +11 -7
- package/dist/schemas/index.d.ts.map +1 -1
- package/dist/structured.d.ts +17 -7
- package/dist/structured.d.ts.map +1 -1
- package/dist/swarm-decompose.d.ts +5 -5
- package/dist/swarm-orchestrate.d.ts +16 -2
- package/dist/swarm-orchestrate.d.ts.map +1 -1
- package/dist/swarm-prompts.d.ts +9 -9
- package/dist/swarm-prompts.d.ts.map +1 -1
- package/dist/swarm-review.d.ts +210 -0
- package/dist/swarm-review.d.ts.map +1 -0
- package/dist/swarm-worktree.d.ts +185 -0
- package/dist/swarm-worktree.d.ts.map +1 -0
- package/dist/swarm.d.ts +7 -0
- package/dist/swarm.d.ts.map +1 -1
- package/dist/tool-availability.d.ts +3 -2
- package/dist/tool-availability.d.ts.map +1 -1
- package/docs/analysis-socratic-planner-pattern.md +1 -1
- package/docs/planning/ADR-007-swarm-enhancements-worktree-review.md +168 -0
- package/docs/testing/context-recovery-test.md +2 -2
- package/evals/README.md +2 -2
- package/evals/scorers/index.ts +7 -7
- package/examples/commands/swarm.md +21 -23
- package/examples/plugin-wrapper-template.ts +310 -44
- package/examples/skills/{beads-workflow → hive-workflow}/SKILL.md +40 -40
- package/examples/skills/swarm-coordination/SKILL.md +1 -1
- package/global-skills/swarm-coordination/SKILL.md +14 -14
- package/global-skills/swarm-coordination/references/coordinator-patterns.md +3 -3
- package/package.json +2 -2
- package/src/compaction-hook.ts +161 -0
- package/src/{beads.integration.test.ts → hive.integration.test.ts} +92 -80
- package/src/{beads.ts → hive.ts} +378 -219
- package/src/index.ts +57 -20
- package/src/learning.ts +9 -9
- package/src/output-guardrails.test.ts +4 -4
- package/src/output-guardrails.ts +9 -9
- package/src/planning-guardrails.test.ts +1 -1
- package/src/planning-guardrails.ts +1 -1
- package/src/schemas/{bead-events.test.ts → cell-events.test.ts} +83 -77
- package/src/schemas/cell-events.ts +807 -0
- package/src/schemas/{bead.ts → cell.ts} +95 -41
- package/src/schemas/evaluation.ts +1 -1
- package/src/schemas/index.ts +90 -18
- package/src/schemas/swarm-context.ts +2 -2
- package/src/structured.test.ts +15 -15
- package/src/structured.ts +18 -11
- package/src/swarm-decompose.ts +23 -23
- package/src/swarm-orchestrate.ts +135 -21
- package/src/swarm-prompts.ts +43 -43
- package/src/swarm-review.test.ts +702 -0
- package/src/swarm-review.ts +696 -0
- package/src/swarm-worktree.test.ts +501 -0
- package/src/swarm-worktree.ts +575 -0
- package/src/swarm.integration.test.ts +12 -12
- package/src/tool-availability.ts +36 -3
- package/dist/beads.d.ts +0 -386
- package/dist/beads.d.ts.map +0 -1
- package/dist/schemas/bead-events.d.ts +0 -698
- package/dist/schemas/bead.d.ts.map +0 -1
- package/src/schemas/bead-events.ts +0 -583
|
@@ -0,0 +1,575 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Swarm Worktree Isolation Module
|
|
3
|
+
*
|
|
4
|
+
* Provides git worktree-based isolation for parallel swarm workers.
|
|
5
|
+
* Each worker gets their own worktree at a shared start commit,
|
|
6
|
+
* preventing file conflicts without needing reservations.
|
|
7
|
+
*
|
|
8
|
+
* Key features:
|
|
9
|
+
* - Create worktrees at specific commits (swarm start point)
|
|
10
|
+
* - Cherry-pick commits back to main branch
|
|
11
|
+
* - Clean up worktrees on completion or abort
|
|
12
|
+
* - List active worktrees for a project
|
|
13
|
+
*
|
|
14
|
+
* Credit: Patterns inspired by https://github.com/nexxeln/opencode-config
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { tool } from "@opencode-ai/plugin";
|
|
18
|
+
import { z } from "zod";
|
|
19
|
+
import { join } from "node:path";
|
|
20
|
+
import { existsSync } from "node:fs";
|
|
21
|
+
|
|
22
|
+
// ============================================================================
|
|
23
|
+
// Types
|
|
24
|
+
// ============================================================================
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Worktree info returned by git worktree list
|
|
28
|
+
*/
|
|
29
|
+
export interface WorktreeInfo {
|
|
30
|
+
task_id: string;
|
|
31
|
+
path: string;
|
|
32
|
+
commit: string;
|
|
33
|
+
branch?: string;
|
|
34
|
+
created_at?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Result of worktree operations
|
|
39
|
+
*/
|
|
40
|
+
export interface WorktreeResult {
|
|
41
|
+
success: boolean;
|
|
42
|
+
worktree_path?: string;
|
|
43
|
+
task_id?: string;
|
|
44
|
+
error?: string;
|
|
45
|
+
created_at_commit?: string;
|
|
46
|
+
merged_commit?: string;
|
|
47
|
+
removed_path?: string;
|
|
48
|
+
removed_count?: number;
|
|
49
|
+
already_removed?: boolean;
|
|
50
|
+
conflicting_files?: string[];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// ============================================================================
|
|
54
|
+
// Constants
|
|
55
|
+
// ============================================================================
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Directory where worktrees are stored
|
|
59
|
+
*/
|
|
60
|
+
const WORKTREE_DIR = ".swarm/worktrees";
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Get the worktree path for a task
|
|
64
|
+
*/
|
|
65
|
+
function getWorktreePath(projectPath: string, taskId: string): string {
|
|
66
|
+
// Sanitize task ID for filesystem
|
|
67
|
+
const safeTaskId = taskId.replace(/[^a-zA-Z0-9.-]/g, "_");
|
|
68
|
+
return join(projectPath, WORKTREE_DIR, safeTaskId);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Parse task ID from worktree path
|
|
73
|
+
*/
|
|
74
|
+
function parseTaskIdFromPath(worktreePath: string): string | null {
|
|
75
|
+
const parts = worktreePath.split("/");
|
|
76
|
+
const worktreesIdx = parts.indexOf("worktrees");
|
|
77
|
+
if (worktreesIdx >= 0 && worktreesIdx < parts.length - 1) {
|
|
78
|
+
return parts[worktreesIdx + 1];
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// ============================================================================
|
|
84
|
+
// Helper Functions
|
|
85
|
+
// ============================================================================
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Check if a path is a git repository
|
|
89
|
+
*/
|
|
90
|
+
async function isGitRepo(path: string): Promise<boolean> {
|
|
91
|
+
const result = await Bun.$`git -C ${path} rev-parse --git-dir`
|
|
92
|
+
.quiet()
|
|
93
|
+
.nothrow();
|
|
94
|
+
return result.exitCode === 0;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Check if there are uncommitted changes
|
|
99
|
+
*/
|
|
100
|
+
async function hasUncommittedChanges(path: string): Promise<boolean> {
|
|
101
|
+
const result = await Bun.$`git -C ${path} status --porcelain`.quiet().nothrow();
|
|
102
|
+
if (result.exitCode !== 0) return true; // Assume dirty if can't check
|
|
103
|
+
return result.stdout.toString().trim().length > 0;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Get current HEAD commit
|
|
108
|
+
*/
|
|
109
|
+
async function getCurrentCommit(path: string): Promise<string | null> {
|
|
110
|
+
const result = await Bun.$`git -C ${path} rev-parse HEAD`.quiet().nothrow();
|
|
111
|
+
if (result.exitCode !== 0) return null;
|
|
112
|
+
return result.stdout.toString().trim();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Get commits in worktree since start_commit
|
|
117
|
+
*/
|
|
118
|
+
async function getWorktreeCommits(
|
|
119
|
+
worktreePath: string,
|
|
120
|
+
startCommit: string,
|
|
121
|
+
): Promise<string[]> {
|
|
122
|
+
const result =
|
|
123
|
+
await Bun.$`git -C ${worktreePath} log --format=%H ${startCommit}..HEAD`
|
|
124
|
+
.quiet()
|
|
125
|
+
.nothrow();
|
|
126
|
+
if (result.exitCode !== 0) return [];
|
|
127
|
+
return result.stdout
|
|
128
|
+
.toString()
|
|
129
|
+
.trim()
|
|
130
|
+
.split("\n")
|
|
131
|
+
.filter((c: string) => c.length > 0);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Ensure worktree directory exists
|
|
136
|
+
*/
|
|
137
|
+
async function ensureWorktreeDir(projectPath: string): Promise<void> {
|
|
138
|
+
const worktreeDir = join(projectPath, WORKTREE_DIR);
|
|
139
|
+
await Bun.$`mkdir -p ${worktreeDir}`.quiet().nothrow();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// ============================================================================
|
|
143
|
+
// Tool Definitions
|
|
144
|
+
// ============================================================================
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Create a git worktree for a task
|
|
148
|
+
*
|
|
149
|
+
* Creates an isolated worktree at the specified start commit.
|
|
150
|
+
* Workers operate in their worktree without affecting main branch.
|
|
151
|
+
*/
|
|
152
|
+
export const swarm_worktree_create = tool({
|
|
153
|
+
description:
|
|
154
|
+
"Create a git worktree for isolated task execution. Worker operates in worktree, not main branch.",
|
|
155
|
+
args: {
|
|
156
|
+
project_path: z.string().describe("Absolute path to project root"),
|
|
157
|
+
task_id: z.string().describe("Task/bead ID (e.g., bd-abc123.1)"),
|
|
158
|
+
start_commit: z
|
|
159
|
+
.string()
|
|
160
|
+
.describe("Commit SHA to create worktree at (swarm start point)"),
|
|
161
|
+
},
|
|
162
|
+
async execute(args): Promise<string> {
|
|
163
|
+
// Validate git repo
|
|
164
|
+
if (!(await isGitRepo(args.project_path))) {
|
|
165
|
+
const result: WorktreeResult = {
|
|
166
|
+
success: false,
|
|
167
|
+
error: `${args.project_path} is not a git repository`,
|
|
168
|
+
};
|
|
169
|
+
return JSON.stringify(result, null, 2);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Check if worktree already exists
|
|
173
|
+
const worktreePath = getWorktreePath(args.project_path, args.task_id);
|
|
174
|
+
const exists = existsSync(worktreePath);
|
|
175
|
+
if (exists) {
|
|
176
|
+
const result: WorktreeResult = {
|
|
177
|
+
success: false,
|
|
178
|
+
error: `Worktree already exists for task ${args.task_id}`,
|
|
179
|
+
worktree_path: worktreePath,
|
|
180
|
+
};
|
|
181
|
+
return JSON.stringify(result, null, 2);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Ensure worktree directory exists
|
|
185
|
+
await ensureWorktreeDir(args.project_path);
|
|
186
|
+
|
|
187
|
+
// Create worktree at start_commit with detached HEAD
|
|
188
|
+
// Using detached HEAD avoids branch conflicts between workers
|
|
189
|
+
const createResult =
|
|
190
|
+
await Bun.$`git -C ${args.project_path} worktree add --detach ${worktreePath} ${args.start_commit}`
|
|
191
|
+
.quiet()
|
|
192
|
+
.nothrow();
|
|
193
|
+
|
|
194
|
+
if (createResult.exitCode !== 0) {
|
|
195
|
+
const result: WorktreeResult = {
|
|
196
|
+
success: false,
|
|
197
|
+
error: `Failed to create worktree: ${createResult.stderr.toString()}`,
|
|
198
|
+
};
|
|
199
|
+
return JSON.stringify(result, null, 2);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const result: WorktreeResult = {
|
|
203
|
+
success: true,
|
|
204
|
+
worktree_path: worktreePath,
|
|
205
|
+
task_id: args.task_id,
|
|
206
|
+
created_at_commit: args.start_commit,
|
|
207
|
+
};
|
|
208
|
+
return JSON.stringify(result, null, 2);
|
|
209
|
+
},
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Merge (cherry-pick) commits from worktree back to main
|
|
214
|
+
*
|
|
215
|
+
* After worker completes, cherry-pick their commits to main branch.
|
|
216
|
+
* This integrates the isolated work back into the shared codebase.
|
|
217
|
+
*/
|
|
218
|
+
export const swarm_worktree_merge = tool({
|
|
219
|
+
description:
|
|
220
|
+
"Cherry-pick commits from worktree back to main branch. Call after worker completes.",
|
|
221
|
+
args: {
|
|
222
|
+
project_path: z.string().describe("Absolute path to project root"),
|
|
223
|
+
task_id: z.string().describe("Task/bead ID"),
|
|
224
|
+
start_commit: z
|
|
225
|
+
.string()
|
|
226
|
+
.optional()
|
|
227
|
+
.describe("Original start commit (to find new commits)"),
|
|
228
|
+
},
|
|
229
|
+
async execute(args): Promise<string> {
|
|
230
|
+
const worktreePath = getWorktreePath(args.project_path, args.task_id);
|
|
231
|
+
|
|
232
|
+
// Check worktree exists
|
|
233
|
+
const exists = existsSync(worktreePath);
|
|
234
|
+
if (!exists) {
|
|
235
|
+
const result: WorktreeResult = {
|
|
236
|
+
success: false,
|
|
237
|
+
error: `Worktree not found for task ${args.task_id}`,
|
|
238
|
+
};
|
|
239
|
+
return JSON.stringify(result, null, 2);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Get start commit if not provided (from worktree's initial commit)
|
|
243
|
+
let startCommit = args.start_commit;
|
|
244
|
+
if (!startCommit) {
|
|
245
|
+
// Try to get from worktree metadata or use merge-base
|
|
246
|
+
const mergeBaseResult =
|
|
247
|
+
await Bun.$`git -C ${args.project_path} merge-base HEAD ${worktreePath}`
|
|
248
|
+
.quiet()
|
|
249
|
+
.nothrow();
|
|
250
|
+
if (mergeBaseResult.exitCode === 0) {
|
|
251
|
+
startCommit = mergeBaseResult.stdout.toString().trim();
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (!startCommit) {
|
|
256
|
+
const result: WorktreeResult = {
|
|
257
|
+
success: false,
|
|
258
|
+
error: "Could not determine start commit for cherry-pick",
|
|
259
|
+
};
|
|
260
|
+
return JSON.stringify(result, null, 2);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Get commits in worktree since start
|
|
264
|
+
const commits = await getWorktreeCommits(worktreePath, startCommit);
|
|
265
|
+
|
|
266
|
+
if (commits.length === 0) {
|
|
267
|
+
const result: WorktreeResult = {
|
|
268
|
+
success: false,
|
|
269
|
+
error: `Worktree has no commits since ${startCommit.slice(0, 7)}`,
|
|
270
|
+
};
|
|
271
|
+
return JSON.stringify(result, null, 2);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Cherry-pick commits in order (oldest first)
|
|
275
|
+
const reversedCommits = commits.reverse();
|
|
276
|
+
let lastMergedCommit: string | null = null;
|
|
277
|
+
|
|
278
|
+
for (const commit of reversedCommits) {
|
|
279
|
+
const cherryResult =
|
|
280
|
+
await Bun.$`git -C ${args.project_path} cherry-pick ${commit}`
|
|
281
|
+
.quiet()
|
|
282
|
+
.nothrow();
|
|
283
|
+
|
|
284
|
+
if (cherryResult.exitCode !== 0) {
|
|
285
|
+
// Check if it's a conflict
|
|
286
|
+
const stderr = cherryResult.stderr.toString();
|
|
287
|
+
if (stderr.includes("conflict") || stderr.includes("CONFLICT")) {
|
|
288
|
+
// Get conflicting files
|
|
289
|
+
const statusResult =
|
|
290
|
+
await Bun.$`git -C ${args.project_path} status --porcelain`
|
|
291
|
+
.quiet()
|
|
292
|
+
.nothrow();
|
|
293
|
+
const conflictingFiles = statusResult.stdout
|
|
294
|
+
.toString()
|
|
295
|
+
.split("\n")
|
|
296
|
+
.filter((line: string) => line.startsWith("UU") || line.startsWith("AA"))
|
|
297
|
+
.map((line: string) => line.slice(3).trim());
|
|
298
|
+
|
|
299
|
+
// Abort the cherry-pick
|
|
300
|
+
await Bun.$`git -C ${args.project_path} cherry-pick --abort`
|
|
301
|
+
.quiet()
|
|
302
|
+
.nothrow();
|
|
303
|
+
|
|
304
|
+
const result: WorktreeResult = {
|
|
305
|
+
success: false,
|
|
306
|
+
error: `Merge conflict during cherry-pick of ${commit.slice(0, 7)}`,
|
|
307
|
+
conflicting_files: conflictingFiles,
|
|
308
|
+
};
|
|
309
|
+
return JSON.stringify(result, null, 2);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const result: WorktreeResult = {
|
|
313
|
+
success: false,
|
|
314
|
+
error: `Failed to cherry-pick ${commit.slice(0, 7)}: ${stderr}`,
|
|
315
|
+
};
|
|
316
|
+
return JSON.stringify(result, null, 2);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
lastMergedCommit = commit;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const result: WorktreeResult = {
|
|
323
|
+
success: true,
|
|
324
|
+
task_id: args.task_id,
|
|
325
|
+
merged_commit: lastMergedCommit || undefined,
|
|
326
|
+
};
|
|
327
|
+
return JSON.stringify(result, null, 2);
|
|
328
|
+
},
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Clean up a worktree
|
|
333
|
+
*
|
|
334
|
+
* Removes the worktree directory and git tracking.
|
|
335
|
+
* Call after merge or on abort.
|
|
336
|
+
*/
|
|
337
|
+
export const swarm_worktree_cleanup = tool({
|
|
338
|
+
description:
|
|
339
|
+
"Remove a worktree after completion or abort. Idempotent - safe to call multiple times.",
|
|
340
|
+
args: {
|
|
341
|
+
project_path: z.string().describe("Absolute path to project root"),
|
|
342
|
+
task_id: z.string().optional().describe("Task/bead ID to clean up"),
|
|
343
|
+
cleanup_all: z
|
|
344
|
+
.boolean()
|
|
345
|
+
.optional()
|
|
346
|
+
.describe("Remove all worktrees for this project"),
|
|
347
|
+
},
|
|
348
|
+
async execute(args): Promise<string> {
|
|
349
|
+
if (args.cleanup_all) {
|
|
350
|
+
// List and remove all worktrees
|
|
351
|
+
const listResult =
|
|
352
|
+
await Bun.$`git -C ${args.project_path} worktree list --porcelain`
|
|
353
|
+
.quiet()
|
|
354
|
+
.nothrow();
|
|
355
|
+
|
|
356
|
+
if (listResult.exitCode !== 0) {
|
|
357
|
+
const result: WorktreeResult = {
|
|
358
|
+
success: false,
|
|
359
|
+
error: `Failed to list worktrees: ${listResult.stderr.toString()}`,
|
|
360
|
+
};
|
|
361
|
+
return JSON.stringify(result, null, 2);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// Parse worktree list
|
|
365
|
+
const output = listResult.stdout.toString();
|
|
366
|
+
const worktreeDir = join(args.project_path, WORKTREE_DIR);
|
|
367
|
+
const worktrees = output
|
|
368
|
+
.split("\n\n")
|
|
369
|
+
.filter((block: string) => block.includes(worktreeDir))
|
|
370
|
+
.map((block: string) => {
|
|
371
|
+
const pathMatch = block.match(/^worktree (.+)$/m);
|
|
372
|
+
return pathMatch ? pathMatch[1] : null;
|
|
373
|
+
})
|
|
374
|
+
.filter((p: string | null): p is string => p !== null);
|
|
375
|
+
|
|
376
|
+
let removedCount = 0;
|
|
377
|
+
for (const wt of worktrees) {
|
|
378
|
+
const removeResult =
|
|
379
|
+
await Bun.$`git -C ${args.project_path} worktree remove --force ${wt}`
|
|
380
|
+
.quiet()
|
|
381
|
+
.nothrow();
|
|
382
|
+
if (removeResult.exitCode === 0) {
|
|
383
|
+
removedCount++;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const result: WorktreeResult = {
|
|
388
|
+
success: true,
|
|
389
|
+
removed_count: removedCount,
|
|
390
|
+
};
|
|
391
|
+
return JSON.stringify(result, null, 2);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
if (!args.task_id) {
|
|
395
|
+
const result: WorktreeResult = {
|
|
396
|
+
success: false,
|
|
397
|
+
error: "Either task_id or cleanup_all must be provided",
|
|
398
|
+
};
|
|
399
|
+
return JSON.stringify(result, null, 2);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const worktreePath = getWorktreePath(args.project_path, args.task_id);
|
|
403
|
+
|
|
404
|
+
// Check if worktree exists (use existsSync for directories)
|
|
405
|
+
const exists = existsSync(worktreePath);
|
|
406
|
+
if (!exists) {
|
|
407
|
+
// Idempotent - already removed
|
|
408
|
+
const result: WorktreeResult = {
|
|
409
|
+
success: true,
|
|
410
|
+
already_removed: true,
|
|
411
|
+
removed_path: worktreePath,
|
|
412
|
+
};
|
|
413
|
+
return JSON.stringify(result, null, 2);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// Remove worktree
|
|
417
|
+
const removeResult =
|
|
418
|
+
await Bun.$`git -C ${args.project_path} worktree remove --force ${worktreePath}`
|
|
419
|
+
.quiet()
|
|
420
|
+
.nothrow();
|
|
421
|
+
|
|
422
|
+
if (removeResult.exitCode !== 0) {
|
|
423
|
+
// Try manual cleanup if git worktree remove fails
|
|
424
|
+
await Bun.$`rm -rf ${worktreePath}`.quiet().nothrow();
|
|
425
|
+
await Bun.$`git -C ${args.project_path} worktree prune`
|
|
426
|
+
.quiet()
|
|
427
|
+
.nothrow();
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
const result: WorktreeResult = {
|
|
431
|
+
success: true,
|
|
432
|
+
removed_path: worktreePath,
|
|
433
|
+
task_id: args.task_id,
|
|
434
|
+
};
|
|
435
|
+
return JSON.stringify(result, null, 2);
|
|
436
|
+
},
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* List all worktrees for a project
|
|
441
|
+
*
|
|
442
|
+
* Returns info about active worktrees including task IDs and paths.
|
|
443
|
+
*/
|
|
444
|
+
export const swarm_worktree_list = tool({
|
|
445
|
+
description: "List all active worktrees for a project",
|
|
446
|
+
args: {
|
|
447
|
+
project_path: z.string().describe("Absolute path to project root"),
|
|
448
|
+
},
|
|
449
|
+
async execute(args): Promise<string> {
|
|
450
|
+
const listResult =
|
|
451
|
+
await Bun.$`git -C ${args.project_path} worktree list --porcelain`
|
|
452
|
+
.quiet()
|
|
453
|
+
.nothrow();
|
|
454
|
+
|
|
455
|
+
if (listResult.exitCode !== 0) {
|
|
456
|
+
return JSON.stringify(
|
|
457
|
+
{
|
|
458
|
+
worktrees: [],
|
|
459
|
+
count: 0,
|
|
460
|
+
error: `Failed to list worktrees: ${listResult.stderr.toString()}`,
|
|
461
|
+
},
|
|
462
|
+
null,
|
|
463
|
+
2,
|
|
464
|
+
);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// Parse worktree list
|
|
468
|
+
const output = listResult.stdout.toString();
|
|
469
|
+
const worktreeDir = join(args.project_path, WORKTREE_DIR);
|
|
470
|
+
|
|
471
|
+
const worktrees: WorktreeInfo[] = [];
|
|
472
|
+
|
|
473
|
+
// Split by double newline (each worktree block)
|
|
474
|
+
const blocks = output.split("\n\n").filter((b: string) => b.trim());
|
|
475
|
+
|
|
476
|
+
for (const block of blocks) {
|
|
477
|
+
const pathMatch = block.match(/^worktree (.+)$/m);
|
|
478
|
+
const commitMatch = block.match(/^HEAD ([a-f0-9]+)$/m);
|
|
479
|
+
const branchMatch = block.match(/^branch (.+)$/m);
|
|
480
|
+
|
|
481
|
+
if (pathMatch && pathMatch[1].includes(worktreeDir)) {
|
|
482
|
+
const path = pathMatch[1];
|
|
483
|
+
const taskId = parseTaskIdFromPath(path);
|
|
484
|
+
|
|
485
|
+
if (taskId) {
|
|
486
|
+
worktrees.push({
|
|
487
|
+
task_id: taskId,
|
|
488
|
+
path,
|
|
489
|
+
commit: commitMatch ? commitMatch[1] : "unknown",
|
|
490
|
+
branch: branchMatch ? branchMatch[1] : undefined,
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
return JSON.stringify(
|
|
497
|
+
{
|
|
498
|
+
worktrees,
|
|
499
|
+
count: worktrees.length,
|
|
500
|
+
},
|
|
501
|
+
null,
|
|
502
|
+
2,
|
|
503
|
+
);
|
|
504
|
+
},
|
|
505
|
+
});
|
|
506
|
+
|
|
507
|
+
// ============================================================================
|
|
508
|
+
// Isolation Mode Helpers
|
|
509
|
+
// ============================================================================
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* Check if worktree isolation can be used
|
|
513
|
+
*
|
|
514
|
+
* Worktree mode requires:
|
|
515
|
+
* - Clean working directory (no uncommitted changes)
|
|
516
|
+
* - Valid git repository
|
|
517
|
+
*/
|
|
518
|
+
export async function canUseWorktreeIsolation(
|
|
519
|
+
projectPath: string,
|
|
520
|
+
): Promise<{ canUse: boolean; reason?: string }> {
|
|
521
|
+
if (!(await isGitRepo(projectPath))) {
|
|
522
|
+
return { canUse: false, reason: "Not a git repository" };
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
if (await hasUncommittedChanges(projectPath)) {
|
|
526
|
+
return {
|
|
527
|
+
canUse: false,
|
|
528
|
+
reason: "Uncommitted changes exist - commit or stash first",
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
return { canUse: true };
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* Get the current commit for worktree start point
|
|
537
|
+
*/
|
|
538
|
+
export async function getStartCommit(
|
|
539
|
+
projectPath: string,
|
|
540
|
+
): Promise<string | null> {
|
|
541
|
+
return getCurrentCommit(projectPath);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Hard reset main branch to start commit (for abort)
|
|
546
|
+
*/
|
|
547
|
+
export async function resetToStartCommit(
|
|
548
|
+
projectPath: string,
|
|
549
|
+
startCommit: string,
|
|
550
|
+
): Promise<{ success: boolean; error?: string }> {
|
|
551
|
+
const result =
|
|
552
|
+
await Bun.$`git -C ${projectPath} reset --hard ${startCommit}`
|
|
553
|
+
.quiet()
|
|
554
|
+
.nothrow();
|
|
555
|
+
|
|
556
|
+
if (result.exitCode !== 0) {
|
|
557
|
+
return {
|
|
558
|
+
success: false,
|
|
559
|
+
error: `Failed to reset: ${result.stderr.toString()}`,
|
|
560
|
+
};
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
return { success: true };
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
// ============================================================================
|
|
567
|
+
// Exports
|
|
568
|
+
// ============================================================================
|
|
569
|
+
|
|
570
|
+
export const worktreeTools = {
|
|
571
|
+
swarm_worktree_create,
|
|
572
|
+
swarm_worktree_merge,
|
|
573
|
+
swarm_worktree_cleanup,
|
|
574
|
+
swarm_worktree_list,
|
|
575
|
+
};
|
|
@@ -85,7 +85,7 @@ describe("swarm_decompose", () => {
|
|
|
85
85
|
const parsed = JSON.parse(result);
|
|
86
86
|
|
|
87
87
|
expect(parsed).toHaveProperty("prompt");
|
|
88
|
-
expect(parsed).toHaveProperty("expected_schema", "
|
|
88
|
+
expect(parsed).toHaveProperty("expected_schema", "CellTree");
|
|
89
89
|
expect(parsed).toHaveProperty("schema_hint");
|
|
90
90
|
expect(parsed.prompt).toContain("Add user authentication with OAuth");
|
|
91
91
|
expect(parsed.prompt).toContain("2-3 independent subtasks");
|
|
@@ -335,7 +335,7 @@ describe("swarm_plan_prompt", () => {
|
|
|
335
335
|
);
|
|
336
336
|
const parsed = JSON.parse(result);
|
|
337
337
|
|
|
338
|
-
expect(parsed).toHaveProperty("expected_schema", "
|
|
338
|
+
expect(parsed).toHaveProperty("expected_schema", "CellTree");
|
|
339
339
|
expect(parsed).toHaveProperty("validation_note");
|
|
340
340
|
expect(parsed.validation_note).toContain("swarm_validate_decomposition");
|
|
341
341
|
expect(parsed).toHaveProperty("schema_hint");
|
|
@@ -401,8 +401,8 @@ describe("swarm_plan_prompt", () => {
|
|
|
401
401
|
});
|
|
402
402
|
|
|
403
403
|
describe("swarm_validate_decomposition", () => {
|
|
404
|
-
it("validates correct
|
|
405
|
-
const
|
|
404
|
+
it("validates correct CellTree", async () => {
|
|
405
|
+
const validCellTree = JSON.stringify({
|
|
406
406
|
epic: {
|
|
407
407
|
title: "Add OAuth",
|
|
408
408
|
description: "Implement OAuth authentication",
|
|
@@ -426,14 +426,14 @@ describe("swarm_validate_decomposition", () => {
|
|
|
426
426
|
});
|
|
427
427
|
|
|
428
428
|
const result = await swarm_validate_decomposition.execute(
|
|
429
|
-
{ response:
|
|
429
|
+
{ response: validCellTree },
|
|
430
430
|
mockContext,
|
|
431
431
|
);
|
|
432
432
|
|
|
433
433
|
const parsed = JSON.parse(result);
|
|
434
434
|
|
|
435
435
|
expect(parsed.valid).toBe(true);
|
|
436
|
-
expect(parsed.
|
|
436
|
+
expect(parsed.cell_tree).toBeDefined();
|
|
437
437
|
expect(parsed.stats).toEqual({
|
|
438
438
|
subtask_count: 2,
|
|
439
439
|
total_files: 3,
|
|
@@ -442,7 +442,7 @@ describe("swarm_validate_decomposition", () => {
|
|
|
442
442
|
});
|
|
443
443
|
|
|
444
444
|
it("rejects file conflicts", async () => {
|
|
445
|
-
const
|
|
445
|
+
const conflictingCellTree = JSON.stringify({
|
|
446
446
|
epic: {
|
|
447
447
|
title: "Conflicting files",
|
|
448
448
|
},
|
|
@@ -463,7 +463,7 @@ describe("swarm_validate_decomposition", () => {
|
|
|
463
463
|
});
|
|
464
464
|
|
|
465
465
|
const result = await swarm_validate_decomposition.execute(
|
|
466
|
-
{ response:
|
|
466
|
+
{ response: conflictingCellTree },
|
|
467
467
|
mockContext,
|
|
468
468
|
);
|
|
469
469
|
|
|
@@ -1342,8 +1342,8 @@ describe("Swarm Prompt V2 (with Swarm Mail/Beads)", () => {
|
|
|
1342
1342
|
// V2 prompt tells agents to USE beads
|
|
1343
1343
|
expect(SUBTASK_PROMPT_V2).toContain("{bead_id}");
|
|
1344
1344
|
expect(SUBTASK_PROMPT_V2).toContain("{epic_id}");
|
|
1345
|
-
expect(SUBTASK_PROMPT_V2).toContain("
|
|
1346
|
-
expect(SUBTASK_PROMPT_V2).toContain("
|
|
1345
|
+
expect(SUBTASK_PROMPT_V2).toContain("hive_update");
|
|
1346
|
+
expect(SUBTASK_PROMPT_V2).toContain("hive_create");
|
|
1347
1347
|
expect(SUBTASK_PROMPT_V2).toContain("swarm_complete");
|
|
1348
1348
|
});
|
|
1349
1349
|
|
|
@@ -1417,8 +1417,8 @@ describe("Swarm Prompt V2 (with Swarm Mail/Beads)", () => {
|
|
|
1417
1417
|
expect(lowerPrompt).not.toContain("coordinator will reserve");
|
|
1418
1418
|
});
|
|
1419
1419
|
|
|
1420
|
-
it("enforces swarm_complete over manual
|
|
1421
|
-
// Step 9: Use swarm_complete, not
|
|
1420
|
+
it("enforces swarm_complete over manual hive_close", () => {
|
|
1421
|
+
// Step 9: Use swarm_complete, not hive_close
|
|
1422
1422
|
expect(SUBTASK_PROMPT_V2).toContain("swarm_complete");
|
|
1423
1423
|
expect(SUBTASK_PROMPT_V2).toContain("DO NOT manually close the bead");
|
|
1424
1424
|
expect(SUBTASK_PROMPT_V2).toContain("Use swarm_complete");
|