opencode-swarm-plugin 0.26.0 → 0.27.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/.turbo/turbo-build.log +4 -4
- package/CHANGELOG.md +37 -0
- package/README.md +43 -46
- package/bin/swarm.ts +8 -8
- 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 +1418 -387
- package/dist/learning.d.ts +9 -9
- package/dist/plugin.js +1240 -386
- 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/hive.ts +1017 -0
- 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 -383
- 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/beads.ts +0 -800
- package/src/schemas/bead-events.ts +0 -583
package/src/beads.ts
DELETED
|
@@ -1,800 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Beads Module - Type-safe wrappers using BeadsAdapter
|
|
3
|
-
*
|
|
4
|
-
* This module provides validated, type-safe operations for the beads
|
|
5
|
-
* issue tracker using the BeadsAdapter from swarm-mail.
|
|
6
|
-
*
|
|
7
|
-
* Key principles:
|
|
8
|
-
* - Use BeadsAdapter for all operations (no CLI commands)
|
|
9
|
-
* - Validate all inputs with Zod schemas
|
|
10
|
-
* - Throw typed errors on failure
|
|
11
|
-
* - Support atomic epic creation with rollback
|
|
12
|
-
*
|
|
13
|
-
* IMPORTANT: Call setBeadsWorkingDirectory() before using tools to ensure
|
|
14
|
-
* operations run in the correct project directory.
|
|
15
|
-
*/
|
|
16
|
-
import { tool } from "@opencode-ai/plugin";
|
|
17
|
-
import { z } from "zod";
|
|
18
|
-
import {
|
|
19
|
-
createBeadsAdapter,
|
|
20
|
-
FlushManager,
|
|
21
|
-
type BeadsAdapter,
|
|
22
|
-
type Bead as AdapterBead,
|
|
23
|
-
getSwarmMail,
|
|
24
|
-
} from "swarm-mail";
|
|
25
|
-
|
|
26
|
-
// ============================================================================
|
|
27
|
-
// Working Directory Configuration
|
|
28
|
-
// ============================================================================
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Module-level working directory for beads commands.
|
|
32
|
-
* Set this via setBeadsWorkingDirectory() before using tools.
|
|
33
|
-
* If not set, commands run in process.cwd() which may be wrong for plugins.
|
|
34
|
-
*/
|
|
35
|
-
let beadsWorkingDirectory: string | null = null;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Set the working directory for all beads commands.
|
|
39
|
-
* Call this from the plugin initialization with the project directory.
|
|
40
|
-
*
|
|
41
|
-
* @param directory - Absolute path to the project directory
|
|
42
|
-
*/
|
|
43
|
-
export function setBeadsWorkingDirectory(directory: string): void {
|
|
44
|
-
beadsWorkingDirectory = directory;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Get the current working directory for beads commands.
|
|
49
|
-
* Returns the configured directory or process.cwd() as fallback.
|
|
50
|
-
*/
|
|
51
|
-
export function getBeadsWorkingDirectory(): string {
|
|
52
|
-
return beadsWorkingDirectory || process.cwd();
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Run a git command in the correct working directory.
|
|
57
|
-
*/
|
|
58
|
-
async function runGitCommand(
|
|
59
|
-
args: string[],
|
|
60
|
-
): Promise<{ exitCode: number; stdout: string; stderr: string }> {
|
|
61
|
-
const cwd = getBeadsWorkingDirectory();
|
|
62
|
-
const proc = Bun.spawn(["git", ...args], {
|
|
63
|
-
cwd,
|
|
64
|
-
stdout: "pipe",
|
|
65
|
-
stderr: "pipe",
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
const [stdout, stderr] = await Promise.all([
|
|
69
|
-
new Response(proc.stdout).text(),
|
|
70
|
-
new Response(proc.stderr).text(),
|
|
71
|
-
]);
|
|
72
|
-
|
|
73
|
-
const exitCode = await proc.exited;
|
|
74
|
-
|
|
75
|
-
return { exitCode, stdout, stderr };
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
import {
|
|
79
|
-
BeadSchema,
|
|
80
|
-
BeadCreateArgsSchema,
|
|
81
|
-
BeadUpdateArgsSchema,
|
|
82
|
-
BeadCloseArgsSchema,
|
|
83
|
-
BeadQueryArgsSchema,
|
|
84
|
-
EpicCreateArgsSchema,
|
|
85
|
-
EpicCreateResultSchema,
|
|
86
|
-
type Bead,
|
|
87
|
-
type BeadCreateArgs,
|
|
88
|
-
type EpicCreateResult,
|
|
89
|
-
} from "./schemas";
|
|
90
|
-
import { createEvent, appendEvent } from "swarm-mail";
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Custom error for bead operations
|
|
94
|
-
*/
|
|
95
|
-
export class BeadError extends Error {
|
|
96
|
-
constructor(
|
|
97
|
-
message: string,
|
|
98
|
-
public readonly command: string,
|
|
99
|
-
public readonly exitCode?: number,
|
|
100
|
-
public readonly stderr?: string,
|
|
101
|
-
) {
|
|
102
|
-
super(message);
|
|
103
|
-
this.name = "BeadError";
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Custom error for validation failures
|
|
109
|
-
*/
|
|
110
|
-
export class BeadValidationError extends Error {
|
|
111
|
-
constructor(
|
|
112
|
-
message: string,
|
|
113
|
-
public readonly zodError: z.ZodError,
|
|
114
|
-
) {
|
|
115
|
-
super(message);
|
|
116
|
-
this.name = "BeadValidationError";
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// ============================================================================
|
|
121
|
-
// Adapter Singleton
|
|
122
|
-
// ============================================================================
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Lazy singleton for BeadsAdapter instances
|
|
126
|
-
* Maps projectKey -> BeadsAdapter
|
|
127
|
-
*/
|
|
128
|
-
const adapterCache = new Map<string, BeadsAdapter>();
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Get or create a BeadsAdapter instance for a project
|
|
132
|
-
* Exported for testing - allows tests to verify state directly
|
|
133
|
-
*/
|
|
134
|
-
export async function getBeadsAdapter(projectKey: string): Promise<BeadsAdapter> {
|
|
135
|
-
if (adapterCache.has(projectKey)) {
|
|
136
|
-
return adapterCache.get(projectKey)!;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
const swarmMail = await getSwarmMail(projectKey);
|
|
140
|
-
const db = await swarmMail.getDatabase();
|
|
141
|
-
const adapter = createBeadsAdapter(db, projectKey);
|
|
142
|
-
|
|
143
|
-
// Run migrations to ensure schema exists
|
|
144
|
-
await adapter.runMigrations();
|
|
145
|
-
|
|
146
|
-
adapterCache.set(projectKey, adapter);
|
|
147
|
-
return adapter;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Format adapter bead for output (map field names)
|
|
152
|
-
* Adapter uses: type, created_at/updated_at (timestamps)
|
|
153
|
-
* Schema expects: issue_type, created_at/updated_at (ISO strings)
|
|
154
|
-
*/
|
|
155
|
-
function formatBeadForOutput(adapterBead: AdapterBead): Record<string, unknown> {
|
|
156
|
-
return {
|
|
157
|
-
id: adapterBead.id,
|
|
158
|
-
title: adapterBead.title,
|
|
159
|
-
description: adapterBead.description || "",
|
|
160
|
-
status: adapterBead.status,
|
|
161
|
-
priority: adapterBead.priority,
|
|
162
|
-
issue_type: adapterBead.type, // Adapter: type → Schema: issue_type
|
|
163
|
-
created_at: new Date(adapterBead.created_at).toISOString(),
|
|
164
|
-
updated_at: new Date(adapterBead.updated_at).toISOString(),
|
|
165
|
-
closed_at: adapterBead.closed_at
|
|
166
|
-
? new Date(adapterBead.closed_at).toISOString()
|
|
167
|
-
: undefined,
|
|
168
|
-
parent_id: adapterBead.parent_id || undefined,
|
|
169
|
-
dependencies: [], // TODO: fetch from adapter if needed
|
|
170
|
-
metadata: {},
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// ============================================================================
|
|
175
|
-
// Tool Definitions
|
|
176
|
-
// ============================================================================
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Create a new bead with type-safe validation
|
|
180
|
-
*/
|
|
181
|
-
export const beads_create = tool({
|
|
182
|
-
description: "Create a new bead with type-safe validation",
|
|
183
|
-
args: {
|
|
184
|
-
title: tool.schema.string().describe("Bead title"),
|
|
185
|
-
type: tool.schema
|
|
186
|
-
.enum(["bug", "feature", "task", "epic", "chore"])
|
|
187
|
-
.optional()
|
|
188
|
-
.describe("Issue type (default: task)"),
|
|
189
|
-
priority: tool.schema
|
|
190
|
-
.number()
|
|
191
|
-
.min(0)
|
|
192
|
-
.max(3)
|
|
193
|
-
.optional()
|
|
194
|
-
.describe("Priority 0-3 (default: 2)"),
|
|
195
|
-
description: tool.schema.string().optional().describe("Bead description"),
|
|
196
|
-
parent_id: tool.schema
|
|
197
|
-
.string()
|
|
198
|
-
.optional()
|
|
199
|
-
.describe("Parent bead ID for epic children"),
|
|
200
|
-
},
|
|
201
|
-
async execute(args, ctx) {
|
|
202
|
-
const validated = BeadCreateArgsSchema.parse(args);
|
|
203
|
-
const projectKey = getBeadsWorkingDirectory();
|
|
204
|
-
const adapter = await getBeadsAdapter(projectKey);
|
|
205
|
-
|
|
206
|
-
try {
|
|
207
|
-
const bead = await adapter.createBead(projectKey, {
|
|
208
|
-
title: validated.title,
|
|
209
|
-
type: validated.type || "task",
|
|
210
|
-
priority: validated.priority ?? 2,
|
|
211
|
-
description: validated.description,
|
|
212
|
-
parent_id: validated.parent_id,
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
// Mark dirty for export
|
|
216
|
-
await adapter.markDirty(projectKey, bead.id);
|
|
217
|
-
|
|
218
|
-
const formatted = formatBeadForOutput(bead);
|
|
219
|
-
return JSON.stringify(formatted, null, 2);
|
|
220
|
-
} catch (error) {
|
|
221
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
222
|
-
throw new BeadError(
|
|
223
|
-
`Failed to create bead: ${message}`,
|
|
224
|
-
"beads_create",
|
|
225
|
-
);
|
|
226
|
-
}
|
|
227
|
-
},
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* Create an epic with subtasks in one atomic operation
|
|
232
|
-
*/
|
|
233
|
-
export const beads_create_epic = tool({
|
|
234
|
-
description: "Create epic with subtasks in one atomic operation",
|
|
235
|
-
args: {
|
|
236
|
-
epic_title: tool.schema.string().describe("Epic title"),
|
|
237
|
-
epic_description: tool.schema
|
|
238
|
-
.string()
|
|
239
|
-
.optional()
|
|
240
|
-
.describe("Epic description"),
|
|
241
|
-
epic_id: tool.schema
|
|
242
|
-
.string()
|
|
243
|
-
.optional()
|
|
244
|
-
.describe("Custom ID for the epic (e.g., 'phase-0')"),
|
|
245
|
-
subtasks: tool.schema
|
|
246
|
-
.array(
|
|
247
|
-
tool.schema.object({
|
|
248
|
-
title: tool.schema.string(),
|
|
249
|
-
priority: tool.schema.number().min(0).max(3).optional(),
|
|
250
|
-
files: tool.schema.array(tool.schema.string()).optional(),
|
|
251
|
-
id_suffix: tool.schema
|
|
252
|
-
.string()
|
|
253
|
-
.optional()
|
|
254
|
-
.describe(
|
|
255
|
-
"Custom ID suffix (e.g., 'e2e-test' becomes 'phase-0.e2e-test')",
|
|
256
|
-
),
|
|
257
|
-
}),
|
|
258
|
-
)
|
|
259
|
-
.describe("Subtasks to create under the epic"),
|
|
260
|
-
strategy: tool.schema
|
|
261
|
-
.enum(["file-based", "feature-based", "risk-based"])
|
|
262
|
-
.optional()
|
|
263
|
-
.describe("Decomposition strategy used (default: feature-based)"),
|
|
264
|
-
task: tool.schema
|
|
265
|
-
.string()
|
|
266
|
-
.optional()
|
|
267
|
-
.describe("Original task description that was decomposed"),
|
|
268
|
-
project_key: tool.schema
|
|
269
|
-
.string()
|
|
270
|
-
.optional()
|
|
271
|
-
.describe("Project path for event emission"),
|
|
272
|
-
recovery_context: tool.schema
|
|
273
|
-
.object({
|
|
274
|
-
shared_context: tool.schema.string().optional(),
|
|
275
|
-
skills_to_load: tool.schema.array(tool.schema.string()).optional(),
|
|
276
|
-
coordinator_notes: tool.schema.string().optional(),
|
|
277
|
-
})
|
|
278
|
-
.optional()
|
|
279
|
-
.describe("Recovery context from checkpoint compaction"),
|
|
280
|
-
},
|
|
281
|
-
async execute(args, ctx) {
|
|
282
|
-
const validated = EpicCreateArgsSchema.parse(args);
|
|
283
|
-
const projectKey = getBeadsWorkingDirectory();
|
|
284
|
-
const adapter = await getBeadsAdapter(projectKey);
|
|
285
|
-
const created: AdapterBead[] = [];
|
|
286
|
-
|
|
287
|
-
try {
|
|
288
|
-
// 1. Create epic
|
|
289
|
-
const epic = await adapter.createBead(projectKey, {
|
|
290
|
-
title: validated.epic_title,
|
|
291
|
-
type: "epic",
|
|
292
|
-
priority: 1,
|
|
293
|
-
description: validated.epic_description,
|
|
294
|
-
});
|
|
295
|
-
await adapter.markDirty(projectKey, epic.id);
|
|
296
|
-
created.push(epic);
|
|
297
|
-
|
|
298
|
-
// 2. Create subtasks
|
|
299
|
-
for (const subtask of validated.subtasks) {
|
|
300
|
-
const subtaskBead = await adapter.createBead(projectKey, {
|
|
301
|
-
title: subtask.title,
|
|
302
|
-
type: "task",
|
|
303
|
-
priority: subtask.priority ?? 2,
|
|
304
|
-
parent_id: epic.id,
|
|
305
|
-
});
|
|
306
|
-
await adapter.markDirty(projectKey, subtaskBead.id);
|
|
307
|
-
created.push(subtaskBead);
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
const result: EpicCreateResult = {
|
|
311
|
-
success: true,
|
|
312
|
-
epic: formatBeadForOutput(epic) as Bead,
|
|
313
|
-
subtasks: created.slice(1).map((b) => formatBeadForOutput(b) as Bead),
|
|
314
|
-
};
|
|
315
|
-
|
|
316
|
-
// Emit DecompositionGeneratedEvent for learning system
|
|
317
|
-
if (args.project_key) {
|
|
318
|
-
try {
|
|
319
|
-
const event = createEvent("decomposition_generated", {
|
|
320
|
-
project_key: args.project_key,
|
|
321
|
-
epic_id: epic.id,
|
|
322
|
-
task: args.task || validated.epic_title,
|
|
323
|
-
context: validated.epic_description,
|
|
324
|
-
strategy: args.strategy || "feature-based",
|
|
325
|
-
epic_title: validated.epic_title,
|
|
326
|
-
subtasks: validated.subtasks.map((st) => ({
|
|
327
|
-
title: st.title,
|
|
328
|
-
files: st.files || [],
|
|
329
|
-
priority: st.priority,
|
|
330
|
-
})),
|
|
331
|
-
recovery_context: args.recovery_context,
|
|
332
|
-
});
|
|
333
|
-
await appendEvent(event, args.project_key);
|
|
334
|
-
} catch (error) {
|
|
335
|
-
// Non-fatal - log and continue
|
|
336
|
-
console.warn(
|
|
337
|
-
"[beads_create_epic] Failed to emit DecompositionGeneratedEvent:",
|
|
338
|
-
error,
|
|
339
|
-
);
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
return JSON.stringify(result, null, 2);
|
|
344
|
-
} catch (error) {
|
|
345
|
-
// Partial failure - rollback via deleteBead
|
|
346
|
-
const rollbackErrors: string[] = [];
|
|
347
|
-
|
|
348
|
-
for (const bead of created) {
|
|
349
|
-
try {
|
|
350
|
-
await adapter.deleteBead(projectKey, bead.id, {
|
|
351
|
-
reason: "Rollback partial epic",
|
|
352
|
-
});
|
|
353
|
-
} catch (rollbackError) {
|
|
354
|
-
const errMsg =
|
|
355
|
-
rollbackError instanceof Error
|
|
356
|
-
? rollbackError.message
|
|
357
|
-
: String(rollbackError);
|
|
358
|
-
console.error(`Failed to rollback bead ${bead.id}:`, rollbackError);
|
|
359
|
-
rollbackErrors.push(`${bead.id}: ${errMsg}`);
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
364
|
-
let rollbackInfo = `\n\nRolled back ${created.length - rollbackErrors.length} bead(s)`;
|
|
365
|
-
|
|
366
|
-
if (rollbackErrors.length > 0) {
|
|
367
|
-
rollbackInfo += `\n\nRollback failures (${rollbackErrors.length}):\n${rollbackErrors.join("\n")}`;
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
throw new BeadError(
|
|
371
|
-
`Epic creation failed: ${errorMsg}${rollbackInfo}`,
|
|
372
|
-
"beads_create_epic",
|
|
373
|
-
1,
|
|
374
|
-
);
|
|
375
|
-
}
|
|
376
|
-
},
|
|
377
|
-
});
|
|
378
|
-
|
|
379
|
-
/**
|
|
380
|
-
* Query beads with filters
|
|
381
|
-
*/
|
|
382
|
-
export const beads_query = tool({
|
|
383
|
-
description: "Query beads with filters (replaces bd list, bd ready, bd wip)",
|
|
384
|
-
args: {
|
|
385
|
-
status: tool.schema
|
|
386
|
-
.enum(["open", "in_progress", "blocked", "closed"])
|
|
387
|
-
.optional()
|
|
388
|
-
.describe("Filter by status"),
|
|
389
|
-
type: tool.schema
|
|
390
|
-
.enum(["bug", "feature", "task", "epic", "chore"])
|
|
391
|
-
.optional()
|
|
392
|
-
.describe("Filter by type"),
|
|
393
|
-
ready: tool.schema
|
|
394
|
-
.boolean()
|
|
395
|
-
.optional()
|
|
396
|
-
.describe("Only show unblocked beads (uses bd ready)"),
|
|
397
|
-
limit: tool.schema
|
|
398
|
-
.number()
|
|
399
|
-
.optional()
|
|
400
|
-
.describe("Max results to return (default: 20)"),
|
|
401
|
-
},
|
|
402
|
-
async execute(args, ctx) {
|
|
403
|
-
const validated = BeadQueryArgsSchema.parse(args);
|
|
404
|
-
const projectKey = getBeadsWorkingDirectory();
|
|
405
|
-
const adapter = await getBeadsAdapter(projectKey);
|
|
406
|
-
|
|
407
|
-
try {
|
|
408
|
-
let beads: AdapterBead[];
|
|
409
|
-
|
|
410
|
-
if (validated.ready) {
|
|
411
|
-
const readyBead = await adapter.getNextReadyBead(projectKey);
|
|
412
|
-
beads = readyBead ? [readyBead] : [];
|
|
413
|
-
} else {
|
|
414
|
-
beads = await adapter.queryBeads(projectKey, {
|
|
415
|
-
status: validated.status,
|
|
416
|
-
type: validated.type,
|
|
417
|
-
limit: validated.limit || 20,
|
|
418
|
-
});
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
const formatted = beads.map((b) => formatBeadForOutput(b));
|
|
422
|
-
return JSON.stringify(formatted, null, 2);
|
|
423
|
-
} catch (error) {
|
|
424
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
425
|
-
throw new BeadError(
|
|
426
|
-
`Failed to query beads: ${message}`,
|
|
427
|
-
"beads_query",
|
|
428
|
-
);
|
|
429
|
-
}
|
|
430
|
-
},
|
|
431
|
-
});
|
|
432
|
-
|
|
433
|
-
/**
|
|
434
|
-
* Update a bead's status or description
|
|
435
|
-
*/
|
|
436
|
-
export const beads_update = tool({
|
|
437
|
-
description: "Update bead status/description",
|
|
438
|
-
args: {
|
|
439
|
-
id: tool.schema.string().describe("Bead ID"),
|
|
440
|
-
status: tool.schema
|
|
441
|
-
.enum(["open", "in_progress", "blocked", "closed"])
|
|
442
|
-
.optional()
|
|
443
|
-
.describe("New status"),
|
|
444
|
-
description: tool.schema.string().optional().describe("New description"),
|
|
445
|
-
priority: tool.schema
|
|
446
|
-
.number()
|
|
447
|
-
.min(0)
|
|
448
|
-
.max(3)
|
|
449
|
-
.optional()
|
|
450
|
-
.describe("New priority"),
|
|
451
|
-
},
|
|
452
|
-
async execute(args, ctx) {
|
|
453
|
-
const validated = BeadUpdateArgsSchema.parse(args);
|
|
454
|
-
const projectKey = getBeadsWorkingDirectory();
|
|
455
|
-
const adapter = await getBeadsAdapter(projectKey);
|
|
456
|
-
|
|
457
|
-
try {
|
|
458
|
-
let bead: AdapterBead;
|
|
459
|
-
|
|
460
|
-
// Status changes use changeBeadStatus, other fields use updateBead
|
|
461
|
-
if (validated.status) {
|
|
462
|
-
bead = await adapter.changeBeadStatus(
|
|
463
|
-
projectKey,
|
|
464
|
-
validated.id,
|
|
465
|
-
validated.status,
|
|
466
|
-
);
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
// Update other fields if provided
|
|
470
|
-
if (validated.description !== undefined || validated.priority !== undefined) {
|
|
471
|
-
bead = await adapter.updateBead(projectKey, validated.id, {
|
|
472
|
-
description: validated.description,
|
|
473
|
-
priority: validated.priority,
|
|
474
|
-
});
|
|
475
|
-
} else if (!validated.status) {
|
|
476
|
-
// No changes requested
|
|
477
|
-
const existingBead = await adapter.getBead(projectKey, validated.id);
|
|
478
|
-
if (!existingBead) {
|
|
479
|
-
throw new BeadError(
|
|
480
|
-
`Bead not found: ${validated.id}`,
|
|
481
|
-
"beads_update",
|
|
482
|
-
);
|
|
483
|
-
}
|
|
484
|
-
bead = existingBead;
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
await adapter.markDirty(projectKey, validated.id);
|
|
488
|
-
|
|
489
|
-
const formatted = formatBeadForOutput(bead!);
|
|
490
|
-
return JSON.stringify(formatted, null, 2);
|
|
491
|
-
} catch (error) {
|
|
492
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
493
|
-
throw new BeadError(
|
|
494
|
-
`Failed to update bead: ${message}`,
|
|
495
|
-
"beads_update",
|
|
496
|
-
);
|
|
497
|
-
}
|
|
498
|
-
},
|
|
499
|
-
});
|
|
500
|
-
|
|
501
|
-
/**
|
|
502
|
-
* Close a bead with reason
|
|
503
|
-
*/
|
|
504
|
-
export const beads_close = tool({
|
|
505
|
-
description: "Close a bead with reason",
|
|
506
|
-
args: {
|
|
507
|
-
id: tool.schema.string().describe("Bead ID"),
|
|
508
|
-
reason: tool.schema.string().describe("Completion reason"),
|
|
509
|
-
},
|
|
510
|
-
async execute(args, ctx) {
|
|
511
|
-
const validated = BeadCloseArgsSchema.parse(args);
|
|
512
|
-
const projectKey = getBeadsWorkingDirectory();
|
|
513
|
-
const adapter = await getBeadsAdapter(projectKey);
|
|
514
|
-
|
|
515
|
-
try {
|
|
516
|
-
const bead = await adapter.closeBead(
|
|
517
|
-
projectKey,
|
|
518
|
-
validated.id,
|
|
519
|
-
validated.reason,
|
|
520
|
-
);
|
|
521
|
-
|
|
522
|
-
await adapter.markDirty(projectKey, validated.id);
|
|
523
|
-
|
|
524
|
-
return `Closed ${bead.id}: ${validated.reason}`;
|
|
525
|
-
} catch (error) {
|
|
526
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
527
|
-
throw new BeadError(
|
|
528
|
-
`Failed to close bead: ${message}`,
|
|
529
|
-
"beads_close",
|
|
530
|
-
);
|
|
531
|
-
}
|
|
532
|
-
},
|
|
533
|
-
});
|
|
534
|
-
|
|
535
|
-
/**
|
|
536
|
-
* Mark a bead as in-progress
|
|
537
|
-
*/
|
|
538
|
-
export const beads_start = tool({
|
|
539
|
-
description:
|
|
540
|
-
"Mark a bead as in-progress (shortcut for update --status in_progress)",
|
|
541
|
-
args: {
|
|
542
|
-
id: tool.schema.string().describe("Bead ID"),
|
|
543
|
-
},
|
|
544
|
-
async execute(args, ctx) {
|
|
545
|
-
const projectKey = getBeadsWorkingDirectory();
|
|
546
|
-
const adapter = await getBeadsAdapter(projectKey);
|
|
547
|
-
|
|
548
|
-
try {
|
|
549
|
-
const bead = await adapter.changeBeadStatus(
|
|
550
|
-
projectKey,
|
|
551
|
-
args.id,
|
|
552
|
-
"in_progress",
|
|
553
|
-
);
|
|
554
|
-
|
|
555
|
-
await adapter.markDirty(projectKey, args.id);
|
|
556
|
-
|
|
557
|
-
return `Started: ${bead.id}`;
|
|
558
|
-
} catch (error) {
|
|
559
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
560
|
-
throw new BeadError(
|
|
561
|
-
`Failed to start bead: ${message}`,
|
|
562
|
-
"beads_start",
|
|
563
|
-
);
|
|
564
|
-
}
|
|
565
|
-
},
|
|
566
|
-
});
|
|
567
|
-
|
|
568
|
-
/**
|
|
569
|
-
* Get the next ready bead
|
|
570
|
-
*/
|
|
571
|
-
export const beads_ready = tool({
|
|
572
|
-
description: "Get the next ready bead (unblocked, highest priority)",
|
|
573
|
-
args: {},
|
|
574
|
-
async execute(args, ctx) {
|
|
575
|
-
const projectKey = getBeadsWorkingDirectory();
|
|
576
|
-
const adapter = await getBeadsAdapter(projectKey);
|
|
577
|
-
|
|
578
|
-
try {
|
|
579
|
-
const bead = await adapter.getNextReadyBead(projectKey);
|
|
580
|
-
|
|
581
|
-
if (!bead) {
|
|
582
|
-
return "No ready beads";
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
const formatted = formatBeadForOutput(bead);
|
|
586
|
-
return JSON.stringify(formatted, null, 2);
|
|
587
|
-
} catch (error) {
|
|
588
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
589
|
-
throw new BeadError(
|
|
590
|
-
`Failed to get ready beads: ${message}`,
|
|
591
|
-
"beads_ready",
|
|
592
|
-
);
|
|
593
|
-
}
|
|
594
|
-
},
|
|
595
|
-
});
|
|
596
|
-
|
|
597
|
-
/**
|
|
598
|
-
* Sync beads to git and push
|
|
599
|
-
*/
|
|
600
|
-
export const beads_sync = tool({
|
|
601
|
-
description: "Sync beads to git and push (MANDATORY at session end)",
|
|
602
|
-
args: {
|
|
603
|
-
auto_pull: tool.schema
|
|
604
|
-
.boolean()
|
|
605
|
-
.optional()
|
|
606
|
-
.describe("Pull before sync (default: true)"),
|
|
607
|
-
},
|
|
608
|
-
async execute(args, ctx) {
|
|
609
|
-
const autoPull = args.auto_pull ?? true;
|
|
610
|
-
const projectKey = getBeadsWorkingDirectory();
|
|
611
|
-
const adapter = await getBeadsAdapter(projectKey);
|
|
612
|
-
const TIMEOUT_MS = 30000; // 30 seconds
|
|
613
|
-
|
|
614
|
-
/**
|
|
615
|
-
* Helper to run a command with timeout
|
|
616
|
-
*/
|
|
617
|
-
const withTimeout = async <T>(
|
|
618
|
-
promise: Promise<T>,
|
|
619
|
-
timeoutMs: number,
|
|
620
|
-
operation: string,
|
|
621
|
-
): Promise<T> => {
|
|
622
|
-
let timeoutId: ReturnType<typeof setTimeout> | undefined;
|
|
623
|
-
|
|
624
|
-
const timeoutPromise = new Promise<never>((_, reject) => {
|
|
625
|
-
timeoutId = setTimeout(
|
|
626
|
-
() =>
|
|
627
|
-
reject(
|
|
628
|
-
new BeadError(
|
|
629
|
-
`Operation timed out after ${timeoutMs}ms`,
|
|
630
|
-
operation,
|
|
631
|
-
),
|
|
632
|
-
),
|
|
633
|
-
timeoutMs,
|
|
634
|
-
);
|
|
635
|
-
});
|
|
636
|
-
|
|
637
|
-
try {
|
|
638
|
-
return await Promise.race([promise, timeoutPromise]);
|
|
639
|
-
} finally {
|
|
640
|
-
if (timeoutId !== undefined) {
|
|
641
|
-
clearTimeout(timeoutId);
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
};
|
|
645
|
-
|
|
646
|
-
// 1. Flush beads to JSONL using FlushManager
|
|
647
|
-
const flushManager = new FlushManager({
|
|
648
|
-
adapter,
|
|
649
|
-
projectKey,
|
|
650
|
-
outputPath: `${projectKey}/.beads/issues.jsonl`,
|
|
651
|
-
});
|
|
652
|
-
|
|
653
|
-
const flushResult = await withTimeout(
|
|
654
|
-
flushManager.flush(),
|
|
655
|
-
TIMEOUT_MS,
|
|
656
|
-
"flush beads",
|
|
657
|
-
);
|
|
658
|
-
|
|
659
|
-
if (flushResult.beadsExported === 0) {
|
|
660
|
-
return "No beads to sync";
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
// 2. Check if there are changes to commit
|
|
664
|
-
const beadsStatusResult = await runGitCommand([
|
|
665
|
-
"status",
|
|
666
|
-
"--porcelain",
|
|
667
|
-
".beads/",
|
|
668
|
-
]);
|
|
669
|
-
const hasChanges = beadsStatusResult.stdout.trim() !== "";
|
|
670
|
-
|
|
671
|
-
if (hasChanges) {
|
|
672
|
-
// 3. Stage .beads changes
|
|
673
|
-
const addResult = await runGitCommand(["add", ".beads/"]);
|
|
674
|
-
if (addResult.exitCode !== 0) {
|
|
675
|
-
throw new BeadError(
|
|
676
|
-
`Failed to stage beads: ${addResult.stderr}`,
|
|
677
|
-
"git add .beads/",
|
|
678
|
-
addResult.exitCode,
|
|
679
|
-
);
|
|
680
|
-
}
|
|
681
|
-
|
|
682
|
-
// 4. Commit
|
|
683
|
-
const commitResult = await withTimeout(
|
|
684
|
-
runGitCommand(["commit", "-m", "chore: sync beads"]),
|
|
685
|
-
TIMEOUT_MS,
|
|
686
|
-
"git commit",
|
|
687
|
-
);
|
|
688
|
-
if (
|
|
689
|
-
commitResult.exitCode !== 0 &&
|
|
690
|
-
!commitResult.stdout.includes("nothing to commit")
|
|
691
|
-
) {
|
|
692
|
-
throw new BeadError(
|
|
693
|
-
`Failed to commit beads: ${commitResult.stderr}`,
|
|
694
|
-
"git commit",
|
|
695
|
-
commitResult.exitCode,
|
|
696
|
-
);
|
|
697
|
-
}
|
|
698
|
-
}
|
|
699
|
-
|
|
700
|
-
// 5. Pull if requested
|
|
701
|
-
if (autoPull) {
|
|
702
|
-
const pullResult = await withTimeout(
|
|
703
|
-
runGitCommand(["pull", "--rebase"]),
|
|
704
|
-
TIMEOUT_MS,
|
|
705
|
-
"git pull --rebase",
|
|
706
|
-
);
|
|
707
|
-
|
|
708
|
-
if (pullResult.exitCode !== 0) {
|
|
709
|
-
throw new BeadError(
|
|
710
|
-
`Failed to pull: ${pullResult.stderr}`,
|
|
711
|
-
"git pull --rebase",
|
|
712
|
-
pullResult.exitCode,
|
|
713
|
-
);
|
|
714
|
-
}
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
// 6. Push
|
|
718
|
-
const pushResult = await withTimeout(
|
|
719
|
-
runGitCommand(["push"]),
|
|
720
|
-
TIMEOUT_MS,
|
|
721
|
-
"git push",
|
|
722
|
-
);
|
|
723
|
-
if (pushResult.exitCode !== 0) {
|
|
724
|
-
throw new BeadError(
|
|
725
|
-
`Failed to push: ${pushResult.stderr}`,
|
|
726
|
-
"git push",
|
|
727
|
-
pushResult.exitCode,
|
|
728
|
-
);
|
|
729
|
-
}
|
|
730
|
-
|
|
731
|
-
return "Beads synced and pushed successfully";
|
|
732
|
-
},
|
|
733
|
-
});
|
|
734
|
-
|
|
735
|
-
/**
|
|
736
|
-
* Link a bead to an Agent Mail thread
|
|
737
|
-
*/
|
|
738
|
-
export const beads_link_thread = tool({
|
|
739
|
-
description: "Add metadata linking bead to Agent Mail thread",
|
|
740
|
-
args: {
|
|
741
|
-
bead_id: tool.schema.string().describe("Bead ID"),
|
|
742
|
-
thread_id: tool.schema.string().describe("Agent Mail thread ID"),
|
|
743
|
-
},
|
|
744
|
-
async execute(args, ctx) {
|
|
745
|
-
const projectKey = getBeadsWorkingDirectory();
|
|
746
|
-
const adapter = await getBeadsAdapter(projectKey);
|
|
747
|
-
|
|
748
|
-
try {
|
|
749
|
-
const bead = await adapter.getBead(projectKey, args.bead_id);
|
|
750
|
-
|
|
751
|
-
if (!bead) {
|
|
752
|
-
throw new BeadError(
|
|
753
|
-
`Bead not found: ${args.bead_id}`,
|
|
754
|
-
"beads_link_thread",
|
|
755
|
-
);
|
|
756
|
-
}
|
|
757
|
-
|
|
758
|
-
const existingDesc = bead.description || "";
|
|
759
|
-
const threadMarker = `[thread:${args.thread_id}]`;
|
|
760
|
-
|
|
761
|
-
if (existingDesc.includes(threadMarker)) {
|
|
762
|
-
return `Bead ${args.bead_id} already linked to thread ${args.thread_id}`;
|
|
763
|
-
}
|
|
764
|
-
|
|
765
|
-
const newDesc = existingDesc
|
|
766
|
-
? `${existingDesc}\n\n${threadMarker}`
|
|
767
|
-
: threadMarker;
|
|
768
|
-
|
|
769
|
-
await adapter.updateBead(projectKey, args.bead_id, {
|
|
770
|
-
description: newDesc,
|
|
771
|
-
});
|
|
772
|
-
|
|
773
|
-
await adapter.markDirty(projectKey, args.bead_id);
|
|
774
|
-
|
|
775
|
-
return `Linked bead ${args.bead_id} to thread ${args.thread_id}`;
|
|
776
|
-
} catch (error) {
|
|
777
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
778
|
-
throw new BeadError(
|
|
779
|
-
`Failed to link thread: ${message}`,
|
|
780
|
-
"beads_link_thread",
|
|
781
|
-
);
|
|
782
|
-
}
|
|
783
|
-
},
|
|
784
|
-
});
|
|
785
|
-
|
|
786
|
-
// ============================================================================
|
|
787
|
-
// Export all tools
|
|
788
|
-
// ============================================================================
|
|
789
|
-
|
|
790
|
-
export const beadsTools = {
|
|
791
|
-
beads_create: beads_create,
|
|
792
|
-
beads_create_epic: beads_create_epic,
|
|
793
|
-
beads_query: beads_query,
|
|
794
|
-
beads_update: beads_update,
|
|
795
|
-
beads_close: beads_close,
|
|
796
|
-
beads_start: beads_start,
|
|
797
|
-
beads_ready: beads_ready,
|
|
798
|
-
beads_sync: beads_sync,
|
|
799
|
-
beads_link_thread: beads_link_thread,
|
|
800
|
-
};
|