@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
package/src/plan-fs.ts ADDED
@@ -0,0 +1,323 @@
1
+ /**
2
+ * plan-fs.ts
3
+ *
4
+ * v0.5.0 — Plan filesystem operations.
5
+ *
6
+ * Pure file-I/O helpers for the on-disk plan layout used by the v2
7
+ * canvas:
8
+ *
9
+ * <worktree>/plans/<slug>/meta.json — status + bookkeeping
10
+ * <worktree>/plans/<slug>/plan.json — v2 canvas
11
+ *
12
+ * This module is the **only** place in the plugin that knows the
13
+ * layout. Both `src/commands-impl.ts` (slash-command side effects)
14
+ * and `src/tools/plan-action.ts` (the `bizar_plan_action` tool) build
15
+ * on these primitives. The CLI in `cli/plan.mjs` duplicates the same
16
+ * layout (language boundary: mjs vs ts); keep both copies in sync.
17
+ *
18
+ * Concurrency:
19
+ * - All writes go through a single async mutex so two concurrent
20
+ * `createPlan()` calls cannot both create the same slug, and so a
21
+ * `createPlan` racing with a `bizar_plan_action` write never
22
+ * produces a partial state on disk.
23
+ * - Writes are atomic: write to `<file>.tmp`, then `renameSync` to
24
+ * the final path. A crash mid-write leaves a `.tmp` orphan — the
25
+ * `rmSync` in the catch block is best-effort.
26
+ *
27
+ * Errors:
28
+ * - All functions return a discriminated result object (`{ ok: true, ... }`
29
+ * or `{ ok: false, error: ... }`) and NEVER throw. Callers can
30
+ * surface the error string to the user.
31
+ * - Slug validation: every public function validates the slug with
32
+ * `SLUG_REGEX` and returns `{ ok: false, error: ... }` on failure.
33
+ *
34
+ * [KEEP-IN-SYNC-WITH cli/plan.mjs] — the CLI in `cli/plan.mjs` mirrors
35
+ * the layout. If you change the canvas or meta shape, update both.
36
+ */
37
+
38
+ import {
39
+ existsSync,
40
+ mkdirSync,
41
+ readFileSync,
42
+ renameSync,
43
+ rmSync,
44
+ writeFileSync,
45
+ } from "node:fs";
46
+ import { join } from "node:path";
47
+
48
+ import type { Logger } from "./logger.js";
49
+
50
+ // --- Constants ------------------------------------------------------------
51
+
52
+ /** Same slug rule used everywhere in the project. */
53
+ const SLUG_REGEX = /^[a-z0-9][a-z0-9-]{0,63}$/;
54
+
55
+ /** Initial status of a freshly created plan. */
56
+ const DEFAULT_STATUS = "draft" as const;
57
+
58
+ // --- Types ----------------------------------------------------------------
59
+
60
+ /** Public result type for all plan-fs operations. */
61
+ export type PlanFsResult<T> =
62
+ | { ok: true; value: T }
63
+ | { ok: false; error: string };
64
+
65
+ /** A summary row used by `listPlans`. */
66
+ export interface PlanListEntry {
67
+ slug: string;
68
+ status: string;
69
+ lastEdited: string;
70
+ }
71
+
72
+ /** The shape stored in `meta.json`. */
73
+ export interface PlanMeta {
74
+ status: string;
75
+ lastEdited: string;
76
+ /** Anything else the viewer/agent has written into meta. */
77
+ [key: string]: unknown;
78
+ }
79
+
80
+ /** The shape stored in `plan.json` (v2 canvas). */
81
+ export interface PlanCanvas {
82
+ schemaVersion: 2;
83
+ title: string;
84
+ elements: unknown[];
85
+ connections: unknown[];
86
+ comments: unknown[];
87
+ viewport: { x: number; y: number; zoom: number };
88
+ lastEdited: string;
89
+ [key: string]: unknown;
90
+ }
91
+
92
+ /** Returned by `createPlan` on success. */
93
+ export interface CreatePlanSuccess {
94
+ meta: PlanMeta;
95
+ canvas: PlanCanvas;
96
+ }
97
+
98
+ // --- Module-level mutex ---------------------------------------------------
99
+
100
+ /** Per-process mutex. Serializes every plan-fs mutation. */
101
+ const locks: { chain: Promise<unknown> } = { chain: Promise.resolve() };
102
+
103
+ async function withLock<T>(fn: () => Promise<T>): Promise<T> {
104
+ const prev = locks.chain ?? Promise.resolve();
105
+ const next = prev.then(fn, fn);
106
+ locks.chain = next.catch(() => {});
107
+ return next;
108
+ }
109
+
110
+ // --- Helpers --------------------------------------------------------------
111
+
112
+ function planDir(worktree: string, slug: string): string {
113
+ return join(worktree, "plans", slug);
114
+ }
115
+
116
+ function metaPath(worktree: string, slug: string): string {
117
+ return join(planDir(worktree, slug), "meta.json");
118
+ }
119
+
120
+ function canvasPath(worktree: string, slug: string): string {
121
+ return join(planDir(worktree, slug), "plan.json");
122
+ }
123
+
124
+ function isValidSlug(slug: string): boolean {
125
+ return SLUG_REGEX.test(slug);
126
+ }
127
+
128
+ function readJson<T>(filePath: string): T | null {
129
+ if (!existsSync(filePath)) return null;
130
+ try {
131
+ const raw = readFileSync(filePath, "utf-8");
132
+ const parsed = JSON.parse(raw) as unknown;
133
+ if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
134
+ return null;
135
+ }
136
+ return parsed as T;
137
+ } catch {
138
+ return null;
139
+ }
140
+ }
141
+
142
+ function writeJsonAtomic(
143
+ filePath: string,
144
+ data: unknown,
145
+ logger: Logger,
146
+ ): { ok: true } | { ok: false; error: string } {
147
+ const tmp = `${filePath}.tmp`;
148
+ try {
149
+ mkdirSync(join(filePath, ".."), { recursive: true });
150
+ writeFileSync(tmp, JSON.stringify(data, null, 2), "utf-8");
151
+ renameSync(tmp, filePath);
152
+ return { ok: true };
153
+ } catch (err: unknown) {
154
+ const msg = err instanceof Error ? err.message : String(err);
155
+ logger.warn(`bizar: plan-fs: failed to write ${filePath}: ${msg}`);
156
+ try {
157
+ if (existsSync(tmp)) rmSync(tmp);
158
+ } catch {
159
+ // best-effort cleanup
160
+ }
161
+ return { ok: false, error: `Failed to write ${filePath}: ${msg}` };
162
+ }
163
+ }
164
+
165
+ function titleCaseFromSlug(slug: string): string {
166
+ return slug
167
+ .split(/[-_]/)
168
+ .map((w) => (w.length === 0 ? w : w[0]!.toUpperCase() + w.slice(1)))
169
+ .join(" ");
170
+ }
171
+
172
+ // --- Public API -----------------------------------------------------------
173
+
174
+ /**
175
+ * Create a new plan at `<worktree>/plans/<slug>/`. The directory
176
+ * contains:
177
+ *
178
+ * - `meta.json` — `{ status: "draft", lastEdited: <iso>, title }`
179
+ * - `plan.json` — minimal v2 canvas (empty elements / connections / comments)
180
+ *
181
+ * If a plan with the same slug already exists, returns
182
+ * `{ ok: false, error: "Plan already exists: <slug>" }` — we never
183
+ * clobber. Callers should explicitly `/plan delete` first if they
184
+ * want a fresh canvas.
185
+ *
186
+ * The `template` option is currently informational only — the v0.5.0
187
+ * MVP always scaffolds a blank canvas. Future versions may seed the
188
+ * canvas with template-specific elements.
189
+ */
190
+ export async function createPlan(
191
+ worktree: string,
192
+ slug: string,
193
+ opts: { template?: string | null; logger: Logger },
194
+ ): Promise<PlanFsResult<CreatePlanSuccess>> {
195
+ if (!isValidSlug(slug)) {
196
+ return {
197
+ ok: false,
198
+ error: `Invalid slug "${slug}". Must match ^[a-z0-9][a-z0-9-]{0,63}$.`,
199
+ };
200
+ }
201
+
202
+ return withLock(async () => {
203
+ const dir = planDir(worktree, slug);
204
+ if (existsSync(dir)) {
205
+ return {
206
+ ok: false,
207
+ error:
208
+ `Plan "${slug}" already exists at ${dir}. ` +
209
+ `Use /plan delete ${slug} first, or pick a new slug.`,
210
+ };
211
+ }
212
+
213
+ const now = new Date().toISOString();
214
+ const title = titleCaseFromSlug(slug);
215
+ const meta: PlanMeta = {
216
+ status: DEFAULT_STATUS,
217
+ lastEdited: now,
218
+ title,
219
+ };
220
+ const canvas: PlanCanvas = {
221
+ schemaVersion: 2,
222
+ title,
223
+ elements: [],
224
+ connections: [],
225
+ comments: [],
226
+ viewport: { x: 0, y: 0, zoom: 1 },
227
+ lastEdited: now,
228
+ };
229
+
230
+ // Ensure the directory exists before writing (writeJsonAtomic also
231
+ // mkdir's the parent, but doing it explicitly lets us return a
232
+ // clearer error).
233
+ try {
234
+ mkdirSync(dir, { recursive: true });
235
+ } catch (err: unknown) {
236
+ const msg = err instanceof Error ? err.message : String(err);
237
+ opts.logger.warn(`bizar: plan-fs: mkdir failed for ${dir}: ${msg}`);
238
+ return { ok: false, error: `Cannot create plan directory: ${dir} (${msg})` };
239
+ }
240
+
241
+ const metaRes = writeJsonAtomic(metaPath(worktree, slug), meta, opts.logger);
242
+ if (!metaRes.ok) return metaRes;
243
+ const canvasRes = writeJsonAtomic(canvasPath(worktree, slug), canvas, opts.logger);
244
+ if (!canvasRes.ok) {
245
+ // Roll back the partially-created directory so a retry has a
246
+ // clean slate. Best-effort.
247
+ try {
248
+ rmSync(dir, { recursive: true, force: true });
249
+ } catch {
250
+ // ignore
251
+ }
252
+ return canvasRes;
253
+ }
254
+
255
+ opts.logger.info(
256
+ `bizar: plan-fs: created plan "${slug}" at ${dir} (template=${opts.template ?? "blank"})`,
257
+ );
258
+ return { ok: true, value: { meta, canvas } };
259
+ });
260
+ }
261
+
262
+ /**
263
+ * List the plans in `<worktree>/plans/`. Returns an array sorted by
264
+ * slug. Each entry has `slug`, `status` (from `meta.json` if present,
265
+ * else `"unknown"`), and `lastEdited` (ISO timestamp or `""` if
266
+ * missing).
267
+ *
268
+ * Returns `[]` when the worktree has no `plans/` directory — that's
269
+ * the "no plans yet" case, not an error.
270
+ */
271
+ export async function listPlans(
272
+ worktree: string,
273
+ _logger: Logger,
274
+ ): Promise<PlanListEntry[]> {
275
+ const dir = join(worktree, "plans");
276
+ if (!existsSync(dir)) return [];
277
+
278
+ // Use sync readdir here — the function is called rarely (from
279
+ // /plan list) and the directory is small. Keeping the function
280
+ // async lets us swap in an async implementation later.
281
+ const { readdirSync, statSync } = await import("node:fs");
282
+ let entries: string[];
283
+ try {
284
+ entries = readdirSync(dir);
285
+ } catch {
286
+ return [];
287
+ }
288
+
289
+ const out: PlanListEntry[] = [];
290
+ for (const name of entries) {
291
+ const full = join(dir, name);
292
+ try {
293
+ const stat = statSync(full);
294
+ if (!stat.isDirectory()) continue;
295
+ } catch {
296
+ continue;
297
+ }
298
+ if (!isValidSlug(name)) continue; // skip non-slug dirs
299
+
300
+ const meta = readJson<PlanMeta>(metaPath(worktree, name));
301
+ out.push({
302
+ slug: name,
303
+ status: meta?.status ?? "unknown",
304
+ lastEdited:
305
+ typeof meta?.lastEdited === "string" ? meta.lastEdited : "",
306
+ });
307
+ }
308
+ out.sort((a, b) => a.slug.localeCompare(b.slug));
309
+ return out;
310
+ }
311
+
312
+ /**
313
+ * Read `meta.json` for a plan. Returns `null` if the plan or the
314
+ * meta file does not exist. The caller decides what "missing" means
315
+ * (the slash command treats it as a soft error).
316
+ */
317
+ export async function getPlanMeta(
318
+ worktree: string,
319
+ slug: string,
320
+ ): Promise<PlanMeta | null> {
321
+ if (!isValidSlug(slug)) return null;
322
+ return readJson<PlanMeta>(metaPath(worktree, slug));
323
+ }
package/src/report.ts ADDED
@@ -0,0 +1,178 @@
1
+ /**
2
+ * report.ts
3
+ *
4
+ * Per-session log writer. Appends metadata-only lines (no args) to
5
+ * ~/.cache/bizar/logs/<sessionId>.log with 10 MB rotation.
6
+ * Per §7.1, §7.6, §8.3, §10.1.
7
+ */
8
+
9
+ import { appendFileSync, renameSync, unlinkSync, existsSync, statSync, mkdirSync } from "node:fs";
10
+ import path from "node:path";
11
+ import os from "node:os";
12
+
13
+ /** Minimal Logger interface — same shape as state.ts */
14
+ export interface Logger {
15
+ log(opts: { level: "debug" | "info" | "warn" | "error"; message: string }): void;
16
+ }
17
+
18
+ function expandHome(p: string): string {
19
+ if (p.startsWith("~/") || p === "~") {
20
+ return path.join(os.homedir(), p.slice(1));
21
+ }
22
+ return p;
23
+ }
24
+
25
+ /**
26
+ * Log line format (per §7.1):
27
+ * 2026-06-17T14:30:01.123Z agent=thor tool=read fingerprint=ab12cd outcome=ok duration=45ms
28
+ */
29
+ function formatLine(
30
+ sessionId: string,
31
+ agent: string | null,
32
+ tool: string,
33
+ fingerprint: string,
34
+ outcome: "ok" | "error",
35
+ durationMs: number
36
+ ): string {
37
+ const ts = new Date().toISOString();
38
+ const agentPart = agent ? ` agent=${agent}` : "";
39
+ return `${ts}${agentPart} tool=${tool} fingerprint=${fingerprint} outcome=${outcome} duration=${durationMs}ms\n`;
40
+ }
41
+
42
+ /**
43
+ * Recursive mkdirSync with EACCES handling per §8.2.
44
+ */
45
+ function ensureDir(dir: string, logger: Logger): boolean {
46
+ try {
47
+ mkdirSync(dir, { recursive: true });
48
+ return true;
49
+ } catch (err: unknown) {
50
+ const code = (err as NodeJS.ErrnoException).code;
51
+ if (code === "EACCES" || code === "EROFS") {
52
+ logger.log({
53
+ level: "error",
54
+ message: `bizar: cannot create log directory ${dir}: ${String(err)}`,
55
+ });
56
+ return false;
57
+ }
58
+ throw err;
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Rotate log files when current file exceeds maxBytes:
64
+ * 1. Delete .3.log (if exists)
65
+ * 2. Rename .2.log → .3.log
66
+ * 3. Rename .1.log → .2.log
67
+ * 4. Rename <sessionId>.log → .1.log
68
+ *
69
+ * Each step in its own try/catch. Best-effort — never blocks the write.
70
+ * Per §8.3.
71
+ */
72
+ function rotateLog(logPath: string, logger: Logger): void {
73
+ const dir = path.dirname(logPath);
74
+
75
+ // Step 1: delete .3.log
76
+ const p3 = path.join(dir, ".3.log");
77
+ if (existsSync(p3)) {
78
+ try { unlinkSync(p3); } catch { /* non-fatal */ }
79
+ }
80
+
81
+ // Step 2: rename .2.log → .3.log
82
+ const p2 = path.join(dir, ".2.log");
83
+ if (existsSync(p2)) {
84
+ try { renameSync(p2, p3); } catch {
85
+ logger.log({ level: "warn", message: `bizar: log rotation .2→.3 failed` });
86
+ }
87
+ }
88
+
89
+ // Step 3: rename .1.log → .2.log
90
+ const p1 = path.join(dir, ".1.log");
91
+ if (existsSync(p1)) {
92
+ try { renameSync(p1, p2); } catch {
93
+ logger.log({ level: "warn", message: `bizar: log rotation .1→.2 failed` });
94
+ }
95
+ }
96
+
97
+ // Step 4: rename current → .1.log
98
+ if (existsSync(logPath)) {
99
+ try { renameSync(logPath, p1); } catch {
100
+ logger.log({ level: "warn", message: `bizar: log rotation current→.1 failed` });
101
+ }
102
+ }
103
+ }
104
+
105
+ export class LogWriter {
106
+ private logDir: string;
107
+ private maxBytes: number;
108
+ private logger: Logger;
109
+ private initialized = false;
110
+
111
+ constructor(logDir: string, maxBytes: number, logger: Logger) {
112
+ this.logDir = expandHome(logDir);
113
+ this.maxBytes = Math.max(1024, Math.floor(maxBytes));
114
+ this.logger = logger;
115
+ }
116
+
117
+ /**
118
+ * Lazily ensure the log directory exists.
119
+ */
120
+ private ensureLogDir(): boolean {
121
+ if (this.initialized) return true;
122
+ this.initialized = true;
123
+ return ensureDir(this.logDir, this.logger);
124
+ }
125
+
126
+ /**
127
+ * Append a metadata-only log line for a tool call.
128
+ *
129
+ * Per §7.1 / §7.6: NO args are written. Only:
130
+ * - ISO timestamp
131
+ * - session ID
132
+ * - tool name
133
+ * - fingerprint hash
134
+ * - outcome (ok | error)
135
+ * - duration in milliseconds
136
+ */
137
+ async write(event: {
138
+ sessionId: string;
139
+ agent: string | null;
140
+ tool: string;
141
+ fingerprint: string;
142
+ outcome: "ok" | "error";
143
+ durationMs: number;
144
+ }): Promise<void> {
145
+ if (!this.ensureLogDir()) return;
146
+
147
+ const logPath = path.join(this.logDir, `${event.sessionId}.log`);
148
+ const line = formatLine(
149
+ event.sessionId,
150
+ event.agent,
151
+ event.tool,
152
+ event.fingerprint,
153
+ event.outcome,
154
+ event.durationMs
155
+ );
156
+
157
+ // Check rotation threshold
158
+ try {
159
+ if (existsSync(logPath)) {
160
+ const stat = statSync(logPath);
161
+ if (stat.size + Buffer.byteLength(line, "utf8") > this.maxBytes) {
162
+ rotateLog(logPath, this.logger);
163
+ }
164
+ }
165
+ } catch {
166
+ // best-effort rotation check
167
+ }
168
+
169
+ try {
170
+ appendFileSync(logPath, line, "utf8");
171
+ } catch (err: unknown) {
172
+ this.logger.log({
173
+ level: "warn",
174
+ message: `bizar: failed to write to log ${logPath}: ${String(err)}`,
175
+ });
176
+ }
177
+ }
178
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * research-prompt.ts
3
+ *
4
+ * The intervention message injected into a background session that has been
5
+ * stuck in a thinking loop. The LLM sees this as a new user message and
6
+ * should respond by spawning Mimir or taking other concrete action.
7
+ *
8
+ * Security note: the message is static — it does not interpolate any
9
+ * user-supplied content, prompt, or agent output. The only variable is
10
+ * the duration. This matches the prompt-injection invariant from
11
+ * handoff.ts: the only dynamic content is a short number from our own
12
+ * state, never from agent or user sources.
13
+ *
14
+ * The duration is formatted as `Xm Ys` (or just `Ys` if under a minute)
15
+ * so the agent has concrete context for how long it has been looping.
16
+ * We do not include the instanceId, sessionId, prompt preview, or any
17
+ * other potentially-sensitive content — those would only widen the
18
+ * attack surface without helping the LLM make progress.
19
+ */
20
+ export function researchInterventionPrompt(durationMs: number): string {
21
+ const safeMs = Math.max(0, Math.floor(durationMs));
22
+ const minutes = Math.floor(safeMs / 60_000);
23
+ const seconds = Math.floor((safeMs % 60_000) / 1000);
24
+ const durationStr = minutes > 0 ? `${minutes}m ${seconds}s` : `${seconds}s`;
25
+ return `[SYSTEM REMINDER — Thinking Loop Detected]
26
+
27
+ You have been thinking for over ${durationStr} without calling any tools or producing any text output. This is a sign you are stuck in a thinking loop.
28
+
29
+ To make progress, take ONE of these actions NOW:
30
+ 1. Use the task tool to spawn a Mimir agent for focused research on the original topic.
31
+ 2. Use read/grep/glob to gather concrete information from the codebase.
32
+ 3. Use bash to execute commands that produce observable results.
33
+
34
+ Do NOT continue thinking without taking action. Make a tool call in your next turn.`;
35
+ }