@polderlabs/bizar-plugin 0.5.4

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.
Files changed (60) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +448 -0
  3. package/bun.lock +88 -0
  4. package/index.ts +1113 -0
  5. package/package.json +42 -0
  6. package/scripts/check-forbidden-imports.sh +33 -0
  7. package/src/background-state.ts +463 -0
  8. package/src/background.ts +964 -0
  9. package/src/commands-impl.ts +369 -0
  10. package/src/commands.ts +880 -0
  11. package/src/event-stream.ts +574 -0
  12. package/src/fingerprint.ts +120 -0
  13. package/src/handoff.ts +79 -0
  14. package/src/http-client.ts +467 -0
  15. package/src/logger.ts +144 -0
  16. package/src/loop.ts +176 -0
  17. package/src/options.ts +421 -0
  18. package/src/plan-fs.ts +323 -0
  19. package/src/report.ts +178 -0
  20. package/src/research-prompt.ts +35 -0
  21. package/src/serve.ts +476 -0
  22. package/src/settings.ts +349 -0
  23. package/src/state.ts +298 -0
  24. package/src/tools/bg-collect.ts +104 -0
  25. package/src/tools/bg-get-comments.ts +239 -0
  26. package/src/tools/bg-kill.ts +87 -0
  27. package/src/tools/bg-spawn.ts +263 -0
  28. package/src/tools/bg-status.ts +99 -0
  29. package/src/tools/plan-action.ts +767 -0
  30. package/src/tools/wait-for-feedback.ts +402 -0
  31. package/tests/attach-handler-bug.test.ts +166 -0
  32. package/tests/background-state.test.ts +277 -0
  33. package/tests/background.test.ts +402 -0
  34. package/tests/block.test.ts +193 -0
  35. package/tests/canonical-key-order.test.ts +71 -0
  36. package/tests/commands-impl.test.ts +442 -0
  37. package/tests/commands.test.ts +548 -0
  38. package/tests/config.test.ts +122 -0
  39. package/tests/dispose.test.ts +336 -0
  40. package/tests/event-stream.test.ts +409 -0
  41. package/tests/event.test.ts +262 -0
  42. package/tests/fingerprint.test.ts +161 -0
  43. package/tests/http-client.test.ts +403 -0
  44. package/tests/init-helpers.test.ts +203 -0
  45. package/tests/integration/slash-command.test.ts +348 -0
  46. package/tests/integration/tool-routing.test.ts +314 -0
  47. package/tests/loop.test.ts +397 -0
  48. package/tests/options.test.ts +274 -0
  49. package/tests/serve.test.ts +335 -0
  50. package/tests/settings.test.ts +351 -0
  51. package/tests/stall-think.test.ts +749 -0
  52. package/tests/state.test.ts +275 -0
  53. package/tests/tools/bg-collect.test.ts +337 -0
  54. package/tests/tools/bg-get-comments.test.ts +485 -0
  55. package/tests/tools/bg-kill.test.ts +231 -0
  56. package/tests/tools/bg-spawn.test.ts +311 -0
  57. package/tests/tools/bg-status.test.ts +216 -0
  58. package/tests/tools/plan-action.test.ts +599 -0
  59. package/tests/tools/wait-for-feedback.test.ts +390 -0
  60. package/tsconfig.json +29 -0
@@ -0,0 +1,767 @@
1
+ /**
2
+ * plan-action.ts
3
+ *
4
+ * `bizar_plan_action` tool (v0.4.0).
5
+ *
6
+ * CRUD on the v2 canvas (`plans/<slug>/plan.json`) and on the plan
7
+ * metadata (`plans/<slug>/meta.json`). Pure file I/O — does not require
8
+ * the `opencode serve` child, so it works in any environment, even
9
+ * when background agents are disabled.
10
+ *
11
+ * Actions:
12
+ * - `get_canvas` — return the full plan.json
13
+ * - `add_element` — push a new element (id generated if absent)
14
+ * - `update_element` — patch an existing element
15
+ * - `delete_element` — remove an element AND its connections
16
+ * - `add_connection` — push a new connection (id generated)
17
+ * - `delete_connection` — remove a connection
18
+ * - `add_comment` — push a new comment (id generated)
19
+ * - `reply_to_comment` — append to an existing comment's thread
20
+ * - `set_status` — update meta.json `status`
21
+ *
22
+ * Errors:
23
+ * - Invalid slug → `{ error: "Invalid planSlug ..." }`
24
+ * - Missing plan.json (for canvas-touching actions) → `{ error: "Plan not found: ..." }`
25
+ * - Corrupt plan.json → `{ error: "Failed to read plan.json: ..." }`
26
+ * - Missing element/connection/comment → `{ error: "Element not found: ..." }`
27
+ *
28
+ * The tool NEVER throws. All errors are returned as JSON.
29
+ *
30
+ * Concurrency:
31
+ * - All writes go through a per-store async mutex (see `withLock`).
32
+ * - Writes are atomic via `writeFileSync(tmp) + renameSync(tmp, final)`.
33
+ *
34
+ * Read-only counterpart: `bizar_get_plan_comments` (see bg-get-comments.ts).
35
+ */
36
+
37
+ import { tool } from "@opencode-ai/plugin";
38
+ import { z } from "zod";
39
+ import {
40
+ existsSync,
41
+ readFileSync,
42
+ writeFileSync,
43
+ renameSync,
44
+ unlinkSync,
45
+ mkdirSync,
46
+ } from "node:fs";
47
+ import { join } from "node:path";
48
+
49
+ import type { Logger } from "../logger.js";
50
+
51
+ // --- On-disk shapes (subset of v2 canvas) ---------------------------------
52
+
53
+ /** A canvas element as stored in plan.json. We accept arbitrary fields
54
+ * beyond the documented subset so the agent can add new shapes later. */
55
+ export interface PlanElement {
56
+ id?: string;
57
+ type?: string;
58
+ x?: number;
59
+ y?: number;
60
+ width?: number;
61
+ height?: number;
62
+ title?: string;
63
+ content?: string;
64
+ [key: string]: unknown;
65
+ }
66
+
67
+ export interface PlanConnection {
68
+ id?: string;
69
+ from?: string;
70
+ to?: string;
71
+ fromElementId?: string;
72
+ toElementId?: string;
73
+ label?: string;
74
+ [key: string]: unknown;
75
+ }
76
+
77
+ export interface PlanCommentReply {
78
+ id?: string;
79
+ author?: string;
80
+ text?: string;
81
+ created?: string;
82
+ [key: string]: unknown;
83
+ }
84
+
85
+ export interface PlanComment {
86
+ id?: string;
87
+ x?: number;
88
+ y?: number;
89
+ elementId?: string | null;
90
+ author?: string;
91
+ text?: string;
92
+ created?: string;
93
+ thread?: PlanCommentReply[];
94
+ [key: string]: unknown;
95
+ }
96
+
97
+ export interface PlanCanvas {
98
+ schemaVersion?: number;
99
+ title?: string;
100
+ elements?: PlanElement[];
101
+ connections?: PlanConnection[];
102
+ comments?: PlanComment[];
103
+ viewport?: { x?: number; y?: number; zoom?: number };
104
+ [key: string]: unknown;
105
+ }
106
+
107
+ export interface PlanMeta {
108
+ status?: string;
109
+ [key: string]: unknown;
110
+ }
111
+
112
+ // --- Tool factory types ---------------------------------------------------
113
+
114
+ export interface PlanActionDeps {
115
+ /** Project root; `plans/<slug>/plan.json` lives here. */
116
+ worktree: string;
117
+ logger: Logger;
118
+ }
119
+
120
+ /** Per-plan status (matches cli/plan.mjs's meta.json). */
121
+ export const PLAN_STATUSES = [
122
+ "draft",
123
+ "approved",
124
+ "rejected",
125
+ "in-progress",
126
+ "done",
127
+ ] as const;
128
+ export type PlanStatus = (typeof PLAN_STATUSES)[number];
129
+
130
+ export interface PlanActionOk {
131
+ ok: true;
132
+ action: string;
133
+ planSlug: string;
134
+ [key: string]: unknown;
135
+ }
136
+
137
+ export interface PlanActionErr {
138
+ ok: false;
139
+ action: string;
140
+ planSlug: string;
141
+ error: string;
142
+ }
143
+
144
+ export type PlanActionResult = PlanActionOk | PlanActionErr;
145
+
146
+ // --- Slug validation ------------------------------------------------------
147
+
148
+ /** Same slug rule used everywhere in the project. */
149
+ const SLUG_REGEX = /^[a-z0-9][a-z0-9-]{0,63}$/;
150
+ function isValidSlug(slug: string): boolean {
151
+ return SLUG_REGEX.test(slug);
152
+ }
153
+
154
+ // --- ID generators --------------------------------------------------------
155
+
156
+ function makeElementId(): string {
157
+ return "el_" + Date.now().toString(36) + Math.random().toString(36).slice(2, 8);
158
+ }
159
+ function makeConnectionId(): string {
160
+ return "conn_" + Date.now().toString(36) + Math.random().toString(36).slice(2, 8);
161
+ }
162
+ function makeCommentId(): string {
163
+ return "c_" + Date.now().toString(36) + Math.random().toString(36).slice(2, 8);
164
+ }
165
+ function makeReplyId(): string {
166
+ return "r_" + Date.now().toString(36) + Math.random().toString(36).slice(2, 8);
167
+ }
168
+
169
+ // --- File I/O helpers (mirrors state.ts) ---------------------------------
170
+
171
+ /** Per-plan mutex — serialize concurrent writes to the same plan. */
172
+ async function withLock<T>(
173
+ locks: Map<string, Promise<unknown>>,
174
+ planSlug: string,
175
+ fn: () => Promise<T>,
176
+ ): Promise<T> {
177
+ const prev = locks.get(planSlug) ?? Promise.resolve();
178
+ const next = prev.then(fn, fn);
179
+ locks.set(planSlug, next.catch(() => {}));
180
+ return next;
181
+ }
182
+
183
+ function readCanvas(planPath: string): PlanCanvas | null {
184
+ if (!existsSync(planPath)) return null;
185
+ try {
186
+ const raw = readFileSync(planPath, "utf-8");
187
+ const parsed = JSON.parse(raw) as unknown;
188
+ if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
189
+ return null;
190
+ }
191
+ return parsed as PlanCanvas;
192
+ } catch {
193
+ return null;
194
+ }
195
+ }
196
+
197
+ function readMeta(metaPath: string): PlanMeta | null {
198
+ if (!existsSync(metaPath)) return null;
199
+ try {
200
+ const raw = readFileSync(metaPath, "utf-8");
201
+ const parsed = JSON.parse(raw) as unknown;
202
+ if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
203
+ return null;
204
+ }
205
+ return parsed as PlanMeta;
206
+ } catch {
207
+ return null;
208
+ }
209
+ }
210
+
211
+ function writeJsonAtomic(filePath: string, data: unknown, logger: Logger): void {
212
+ const tmp = `${filePath}.tmp`;
213
+ try {
214
+ writeFileSync(tmp, JSON.stringify(data, null, 2), "utf-8");
215
+ renameSync(tmp, filePath);
216
+ } catch (err: unknown) {
217
+ logger.warn(`bizar: failed to write ${filePath}: ${String(err)}`);
218
+ try {
219
+ if (existsSync(tmp)) unlinkSync(tmp);
220
+ } catch {
221
+ // non-fatal
222
+ }
223
+ }
224
+ }
225
+
226
+ function ensurePlanDir(planDir: string): boolean {
227
+ try {
228
+ mkdirSync(planDir, { recursive: true });
229
+ return true;
230
+ } catch {
231
+ return false;
232
+ }
233
+ }
234
+
235
+ // --- Pure core: planAction (extracted for testing) ------------------------
236
+
237
+ export interface PlanActionArgs {
238
+ action: string;
239
+ planSlug: string;
240
+ element?: PlanElement;
241
+ elementId?: string;
242
+ connection?: PlanConnection;
243
+ connectionId?: string;
244
+ comment?: PlanComment;
245
+ commentId?: string;
246
+ reply?: PlanCommentReply;
247
+ status?: PlanStatus;
248
+ }
249
+
250
+ /**
251
+ * Execute a plan action against the on-disk files. Returns a structured
252
+ * result; never throws.
253
+ *
254
+ * Extracted from the tool factory so tests can drive the same code path
255
+ * without needing the opencode tool framework.
256
+ */
257
+ export function planAction(
258
+ worktree: string,
259
+ logger: Logger,
260
+ locks: Map<string, Promise<unknown>>,
261
+ args: PlanActionArgs,
262
+ ): Promise<PlanActionResult> {
263
+ if (!isValidSlug(args.planSlug)) {
264
+ return Promise.resolve({
265
+ ok: false,
266
+ action: args.action,
267
+ planSlug: args.planSlug,
268
+ error: `Invalid planSlug: "${args.planSlug}". Must match ^[a-z0-9][a-z0-9-]{0,63}$.`,
269
+ });
270
+ }
271
+
272
+ return withLock(locks, args.planSlug, async (): Promise<PlanActionResult> => {
273
+ const planDir = join(worktree, "plans", args.planSlug);
274
+ const planPath = join(planDir, "plan.json");
275
+ const metaPath = join(planDir, "meta.json");
276
+
277
+ // `get_canvas` is the only read that doesn't require an existing plan
278
+ if (args.action === "get_canvas") {
279
+ if (!existsSync(planPath)) {
280
+ return {
281
+ ok: false,
282
+ action: args.action,
283
+ planSlug: args.planSlug,
284
+ error: `Plan not found: ${args.planSlug}`,
285
+ };
286
+ }
287
+ const canvas = readCanvas(planPath);
288
+ if (canvas === null) {
289
+ return {
290
+ ok: false,
291
+ action: args.action,
292
+ planSlug: args.planSlug,
293
+ error: `Plan not found or corrupt: ${args.planSlug}`,
294
+ };
295
+ }
296
+ return {
297
+ ok: true,
298
+ action: args.action,
299
+ planSlug: args.planSlug,
300
+ canvas,
301
+ };
302
+ }
303
+
304
+ // All other actions require a writable plan dir. If the directory
305
+ // doesn't exist, create it (so the agent can create + populate in
306
+ // one workflow). If creation fails, return an error.
307
+ if (!existsSync(planDir)) {
308
+ if (!ensurePlanDir(planDir)) {
309
+ return {
310
+ ok: false,
311
+ action: args.action,
312
+ planSlug: args.planSlug,
313
+ error: `Cannot create plan directory: ${planDir}`,
314
+ };
315
+ }
316
+ }
317
+
318
+ switch (args.action) {
319
+ case "add_element":
320
+ return doAddElement(planPath, logger, args);
321
+ case "update_element":
322
+ return doUpdateElement(planPath, logger, args);
323
+ case "delete_element":
324
+ return doDeleteElement(planPath, logger, args);
325
+ case "add_connection":
326
+ return doAddConnection(planPath, logger, args);
327
+ case "delete_connection":
328
+ return doDeleteConnection(planPath, logger, args);
329
+ case "add_comment":
330
+ return doAddComment(planPath, logger, args);
331
+ case "reply_to_comment":
332
+ return doReplyToComment(planPath, logger, args);
333
+ case "set_status":
334
+ return doSetStatus(planPath, metaPath, logger, args);
335
+ default:
336
+ return {
337
+ ok: false,
338
+ action: args.action,
339
+ planSlug: args.planSlug,
340
+ error: `Unknown action: "${args.action}"`,
341
+ };
342
+ }
343
+ });
344
+ }
345
+
346
+ // --- Action handlers ------------------------------------------------------
347
+
348
+ type CanvasResult =
349
+ | { ok: true; canvas: PlanCanvas }
350
+ | { ok: false; error: string };
351
+
352
+ function ensureCanvasForWrite(planPath: string, logger: Logger): CanvasResult {
353
+ if (existsSync(planPath)) {
354
+ const canvas = readCanvas(planPath);
355
+ if (canvas === null) {
356
+ // Corrupt file — refuse to overwrite
357
+ logger.warn(`bizar: cannot mutate corrupt plan.json at ${planPath}`);
358
+ return { ok: false, error: `plan.json is corrupt or not a v2 canvas object` };
359
+ }
360
+ // Backfill defaults
361
+ if (!Array.isArray(canvas.elements)) canvas.elements = [];
362
+ if (!Array.isArray(canvas.connections)) canvas.connections = [];
363
+ if (!Array.isArray(canvas.comments)) canvas.comments = [];
364
+ if (!canvas.viewport || typeof canvas.viewport !== "object") {
365
+ canvas.viewport = { x: 0, y: 0, zoom: 1 };
366
+ }
367
+ if (canvas.schemaVersion !== 2) canvas.schemaVersion = 2;
368
+ return { ok: true, canvas };
369
+ }
370
+ // New plan — create a minimal v2 canvas
371
+ const fresh: PlanCanvas = {
372
+ schemaVersion: 2,
373
+ title: "Untitled plan",
374
+ elements: [],
375
+ connections: [],
376
+ comments: [],
377
+ viewport: { x: 0, y: 0, zoom: 1 },
378
+ };
379
+ return { ok: true, canvas: fresh };
380
+ }
381
+
382
+ function doAddElement(
383
+ planPath: string,
384
+ logger: Logger,
385
+ args: PlanActionArgs,
386
+ ): PlanActionResult {
387
+ if (!args.element) {
388
+ return errMissingArg(args, "element");
389
+ }
390
+ const ensured = ensureCanvasForWrite(planPath, logger);
391
+ if (!ensured.ok) {
392
+ return { ok: false, action: args.action, planSlug: args.planSlug, error: ensured.error };
393
+ }
394
+ const canvas = ensured.canvas;
395
+ const element: PlanElement = { ...args.element };
396
+ if (!element.id) element.id = makeElementId();
397
+ // Default position/size if missing
398
+ if (typeof element.x !== "number") element.x = 80;
399
+ if (typeof element.y !== "number") element.y = 80;
400
+ if (typeof element.width !== "number") element.width = 240;
401
+ if (typeof element.height !== "number") element.height = 160;
402
+ if (!element.type) element.type = "text";
403
+ canvas.elements!.push(element);
404
+ writeJsonAtomic(planPath, canvas, logger);
405
+ return {
406
+ ok: true,
407
+ action: args.action,
408
+ planSlug: args.planSlug,
409
+ elementId: element.id,
410
+ };
411
+ }
412
+
413
+ function doUpdateElement(
414
+ planPath: string,
415
+ logger: Logger,
416
+ args: PlanActionArgs,
417
+ ): PlanActionResult {
418
+ if (!args.elementId) return errMissingArg(args, "elementId");
419
+ const ensured = ensureCanvasForWrite(planPath, logger);
420
+ if (!ensured.ok) {
421
+ return { ok: false, action: args.action, planSlug: args.planSlug, error: ensured.error };
422
+ }
423
+ const canvas = ensured.canvas;
424
+ const idx = canvas.elements!.findIndex((e) => e.id === args.elementId);
425
+ if (idx === -1) {
426
+ return {
427
+ ok: false,
428
+ action: args.action,
429
+ planSlug: args.planSlug,
430
+ error: `Element not found: ${args.elementId}`,
431
+ };
432
+ }
433
+ // Patch — only update fields present in args.element
434
+ if (args.element) {
435
+ for (const [k, v] of Object.entries(args.element)) {
436
+ // Never let the agent rewrite the id (use delete+add to change it)
437
+ if (k === "id") continue;
438
+ (canvas.elements![idx] as Record<string, unknown>)[k] = v;
439
+ }
440
+ }
441
+ writeJsonAtomic(planPath, canvas, logger);
442
+ return {
443
+ ok: true,
444
+ action: args.action,
445
+ planSlug: args.planSlug,
446
+ elementId: args.elementId,
447
+ };
448
+ }
449
+
450
+ function doDeleteElement(
451
+ planPath: string,
452
+ logger: Logger,
453
+ args: PlanActionArgs,
454
+ ): PlanActionResult {
455
+ if (!args.elementId) return errMissingArg(args, "elementId");
456
+ const ensured = ensureCanvasForWrite(planPath, logger);
457
+ if (!ensured.ok) {
458
+ return { ok: false, action: args.action, planSlug: args.planSlug, error: ensured.error };
459
+ }
460
+ const canvas = ensured.canvas;
461
+ const before = canvas.elements!.length;
462
+ canvas.elements = canvas.elements!.filter((e) => e.id !== args.elementId);
463
+ const removed = before - canvas.elements.length;
464
+
465
+ // Cascade: remove connections touching this element
466
+ const connsBefore = canvas.connections!.length;
467
+ canvas.connections = canvas.connections!.filter((c) => {
468
+ return (
469
+ c.fromElementId !== args.elementId &&
470
+ c.toElementId !== args.elementId &&
471
+ // Also handle the `from`/`to` shorthand used by some schemas.
472
+ c.from !== args.elementId &&
473
+ c.to !== args.elementId
474
+ );
475
+ });
476
+ const removedConns = connsBefore - canvas.connections.length;
477
+
478
+ // Cascade: remove comments pinned to this element (keep canvas-pinned)
479
+ const commentsBefore = canvas.comments!.length;
480
+ canvas.comments = canvas.comments!.filter((c) => c.elementId !== args.elementId);
481
+ const removedComments = commentsBefore - canvas.comments.length;
482
+
483
+ writeJsonAtomic(planPath, canvas, logger);
484
+ return {
485
+ ok: true,
486
+ action: args.action,
487
+ planSlug: args.planSlug,
488
+ elementId: args.elementId,
489
+ removed,
490
+ removedConnections: removedConns,
491
+ removedComments,
492
+ };
493
+ }
494
+
495
+ function doAddConnection(
496
+ planPath: string,
497
+ logger: Logger,
498
+ args: PlanActionArgs,
499
+ ): PlanActionResult {
500
+ if (!args.connection) return errMissingArg(args, "connection");
501
+ const ensured = ensureCanvasForWrite(planPath, logger);
502
+ if (!ensured.ok) {
503
+ return { ok: false, action: args.action, planSlug: args.planSlug, error: ensured.error };
504
+ }
505
+ const canvas = ensured.canvas;
506
+ const connection: PlanConnection = { ...args.connection };
507
+ if (!connection.id) connection.id = makeConnectionId();
508
+ // Normalize: accept either {from,to} or {fromElementId,toElementId}
509
+ if (connection.from && !connection.fromElementId) connection.fromElementId = connection.from;
510
+ if (connection.to && !connection.toElementId) connection.toElementId = connection.to;
511
+ canvas.connections!.push(connection);
512
+ writeJsonAtomic(planPath, canvas, logger);
513
+ return {
514
+ ok: true,
515
+ action: args.action,
516
+ planSlug: args.planSlug,
517
+ connectionId: connection.id,
518
+ };
519
+ }
520
+
521
+ function doDeleteConnection(
522
+ planPath: string,
523
+ logger: Logger,
524
+ args: PlanActionArgs,
525
+ ): PlanActionResult {
526
+ if (!args.connectionId) return errMissingArg(args, "connectionId");
527
+ const ensured = ensureCanvasForWrite(planPath, logger);
528
+ if (!ensured.ok) {
529
+ return { ok: false, action: args.action, planSlug: args.planSlug, error: ensured.error };
530
+ }
531
+ const canvas = ensured.canvas;
532
+ const before = canvas.connections!.length;
533
+ canvas.connections = canvas.connections!.filter((c) => c.id !== args.connectionId);
534
+ const removed = before - canvas.connections.length;
535
+ if (removed === 0) {
536
+ return {
537
+ ok: false,
538
+ action: args.action,
539
+ planSlug: args.planSlug,
540
+ error: `Connection not found: ${args.connectionId}`,
541
+ };
542
+ }
543
+ writeJsonAtomic(planPath, canvas, logger);
544
+ return {
545
+ ok: true,
546
+ action: args.action,
547
+ planSlug: args.planSlug,
548
+ connectionId: args.connectionId,
549
+ removed,
550
+ };
551
+ }
552
+
553
+ function doAddComment(
554
+ planPath: string,
555
+ logger: Logger,
556
+ args: PlanActionArgs,
557
+ ): PlanActionResult {
558
+ if (!args.comment) return errMissingArg(args, "comment");
559
+ const ensured = ensureCanvasForWrite(planPath, logger);
560
+ if (!ensured.ok) {
561
+ return { ok: false, action: args.action, planSlug: args.planSlug, error: ensured.error };
562
+ }
563
+ const canvas = ensured.canvas;
564
+ const comment: PlanComment = { ...args.comment };
565
+ if (!comment.id) comment.id = makeCommentId();
566
+ if (!comment.created) comment.created = new Date().toISOString();
567
+ if (!comment.thread) comment.thread = [];
568
+ canvas.comments!.push(comment);
569
+ writeJsonAtomic(planPath, canvas, logger);
570
+ return {
571
+ ok: true,
572
+ action: args.action,
573
+ planSlug: args.planSlug,
574
+ commentId: comment.id,
575
+ };
576
+ }
577
+
578
+ function doReplyToComment(
579
+ planPath: string,
580
+ logger: Logger,
581
+ args: PlanActionArgs,
582
+ ): PlanActionResult {
583
+ if (!args.commentId) return errMissingArg(args, "commentId");
584
+ if (!args.reply) return errMissingArg(args, "reply");
585
+ const ensured = ensureCanvasForWrite(planPath, logger);
586
+ if (!ensured.ok) {
587
+ return { ok: false, action: args.action, planSlug: args.planSlug, error: ensured.error };
588
+ }
589
+ const canvas = ensured.canvas;
590
+ const target = canvas.comments!.find((c) => c.id === args.commentId);
591
+ if (!target) {
592
+ return {
593
+ ok: false,
594
+ action: args.action,
595
+ planSlug: args.planSlug,
596
+ error: `Comment not found: ${args.commentId}`,
597
+ };
598
+ }
599
+ if (!Array.isArray(target.thread)) target.thread = [];
600
+ const reply: PlanCommentReply = { ...args.reply };
601
+ if (!reply.id) reply.id = makeReplyId();
602
+ if (!reply.created) reply.created = new Date().toISOString();
603
+ target.thread.push(reply);
604
+ writeJsonAtomic(planPath, canvas, logger);
605
+ return {
606
+ ok: true,
607
+ action: args.action,
608
+ planSlug: args.planSlug,
609
+ commentId: args.commentId,
610
+ replyId: reply.id,
611
+ };
612
+ }
613
+
614
+ function doSetStatus(
615
+ planPath: string,
616
+ metaPath: string,
617
+ logger: Logger,
618
+ args: PlanActionArgs,
619
+ ): PlanActionResult {
620
+ if (!args.status) return errMissingArg(args, "status");
621
+ if (!(PLAN_STATUSES as readonly string[]).includes(args.status)) {
622
+ return {
623
+ ok: false,
624
+ action: args.action,
625
+ planSlug: args.planSlug,
626
+ error: `Invalid status: ${args.status}. Must be one of: ${PLAN_STATUSES.join(", ")}.`,
627
+ };
628
+ }
629
+ const meta = (existsSync(metaPath) ? readMeta(metaPath) : null) ?? {};
630
+ meta.status = args.status;
631
+ meta.lastEdited = new Date().toISOString();
632
+ writeJsonAtomic(metaPath, meta, logger);
633
+
634
+ // Also touch plan.json (if it exists) so the viewer reloads.
635
+ if (existsSync(planPath)) {
636
+ const canvas = readCanvas(planPath);
637
+ if (canvas !== null) {
638
+ (canvas as Record<string, unknown>).lastEdited = meta.lastEdited;
639
+ writeJsonAtomic(planPath, canvas, logger);
640
+ }
641
+ }
642
+ return {
643
+ ok: true,
644
+ action: args.action,
645
+ planSlug: args.planSlug,
646
+ status: args.status,
647
+ };
648
+ }
649
+
650
+ function errMissingArg(args: PlanActionArgs, name: string): PlanActionResult {
651
+ return {
652
+ ok: false,
653
+ action: args.action,
654
+ planSlug: args.planSlug,
655
+ error: `Missing required argument: "${name}"`,
656
+ };
657
+ }
658
+
659
+ // --- Zod schema for the tool framework ------------------------------------
660
+
661
+ const elementSchema = z
662
+ .object({
663
+ id: z.string().optional(),
664
+ type: z.string().optional(),
665
+ x: z.number().optional(),
666
+ y: z.number().optional(),
667
+ width: z.number().optional(),
668
+ height: z.number().optional(),
669
+ title: z.string().optional(),
670
+ content: z.string().optional(),
671
+ })
672
+ .passthrough();
673
+
674
+ const connectionSchema = z
675
+ .object({
676
+ id: z.string().optional(),
677
+ from: z.string().optional(),
678
+ to: z.string().optional(),
679
+ fromElementId: z.string().optional(),
680
+ toElementId: z.string().optional(),
681
+ label: z.string().optional(),
682
+ })
683
+ .passthrough();
684
+
685
+ const commentSchema = z
686
+ .object({
687
+ id: z.string().optional(),
688
+ x: z.number().optional(),
689
+ y: z.number().optional(),
690
+ elementId: z.string().nullable().optional(),
691
+ author: z.string().optional(),
692
+ text: z.string().optional(),
693
+ created: z.string().optional(),
694
+ })
695
+ .passthrough();
696
+
697
+ const replySchema = z
698
+ .object({
699
+ id: z.string().optional(),
700
+ author: z.string().optional(),
701
+ text: z.string().optional(),
702
+ created: z.string().optional(),
703
+ })
704
+ .passthrough();
705
+
706
+ // --- Tool factory ---------------------------------------------------------
707
+
708
+ /**
709
+ * Build the `bizar_plan_action` tool. The plugin wires the result into
710
+ * `Hooks.tool`. The `deps` closure carries the worktree, logger, and a
711
+ * shared per-plan mutex map (one mutex map per plugin instance).
712
+ */
713
+ export function createPlanActionTool(deps: PlanActionDeps) {
714
+ const locks = new Map<string, Promise<unknown>>();
715
+ return tool({
716
+ description:
717
+ "CRUD on a Bizar Plan canvas. Use this to add elements, update " +
718
+ "their content, post comments, reply to comments, and set the " +
719
+ "plan's status. Pure file I/O — does not require any background " +
720
+ "agent or local server. Available to all agents.",
721
+ args: {
722
+ action: z.enum([
723
+ "get_canvas",
724
+ "add_element",
725
+ "update_element",
726
+ "delete_element",
727
+ "add_connection",
728
+ "delete_connection",
729
+ "add_comment",
730
+ "reply_to_comment",
731
+ "set_status",
732
+ ]),
733
+ planSlug: z
734
+ .string()
735
+ .min(1)
736
+ .max(64)
737
+ .regex(/^[a-z0-9][a-z0-9-]{0,63}$/, "Must match ^[a-z0-9][a-z0-9-]{0,63}$")
738
+ .describe("The plan's slug (e.g. 'my-feature')."),
739
+ element: elementSchema.optional(),
740
+ elementId: z.string().optional(),
741
+ connection: connectionSchema.optional(),
742
+ connectionId: z.string().optional(),
743
+ comment: commentSchema.optional(),
744
+ commentId: z.string().optional(),
745
+ reply: replySchema.optional(),
746
+ status: z.enum(PLAN_STATUSES).optional(),
747
+ },
748
+ execute: async (rawArgs) => {
749
+ const args = rawArgs as PlanActionArgs;
750
+ try {
751
+ const result = await planAction(deps.worktree, deps.logger, locks, args);
752
+ return { output: JSON.stringify(result) };
753
+ } catch (err: unknown) {
754
+ const msg = err instanceof Error ? err.message : String(err);
755
+ deps.logger.warn(`bizar: plan_action(${args.action}) crashed: ${msg}`);
756
+ return {
757
+ output: JSON.stringify({
758
+ ok: false,
759
+ action: args.action,
760
+ planSlug: args.planSlug,
761
+ error: `Internal error: ${msg}`,
762
+ }),
763
+ };
764
+ }
765
+ },
766
+ });
767
+ }