@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,390 @@
1
+ /**
2
+ * wait-for-feedback.test.ts
3
+ *
4
+ * Tests for `waitForFeedback` in src/tools/wait-for-feedback.ts.
5
+ *
6
+ * We test the core function (extracted from the tool factory) directly
7
+ * with an injectable `sleep` and `now` so tests run instantly. The
8
+ * tool factory's wrapper is thin and adds no behavior.
9
+ *
10
+ * Groups:
11
+ * 1. timeout (no feedback → status: "timed_out")
12
+ * 2. immediate feedback (comments already present)
13
+ * 3. delayed feedback (comments added while polling)
14
+ * 4. approved status (meta.json)
15
+ * 5. rejected status (meta.json)
16
+ * 6. sinceTimestamp filter (only newer comments count)
17
+ * 7. invalid slug → error
18
+ * 8. missing plan → error
19
+ */
20
+
21
+ import { describe, test, expect, beforeAll, afterAll } from "bun:test";
22
+ import {
23
+ mkdtempSync,
24
+ rmSync,
25
+ mkdirSync,
26
+ writeFileSync,
27
+ } from "node:fs";
28
+ import { join } from "node:path";
29
+ import { tmpdir } from "node:os";
30
+
31
+ import { waitForFeedback } from "../../src/tools/wait-for-feedback.ts";
32
+
33
+ // ---------------------------------------------------------------------------
34
+ // Fixtures
35
+ // ---------------------------------------------------------------------------
36
+
37
+ class MockLogger {
38
+ messages: Array<{ level: string; message: string }> = [];
39
+ log(opts: { level: string; message: string }) { this.messages.push(opts); }
40
+ debug(m: string) { this.messages.push({ level: "debug", message: m }); }
41
+ info(m: string) { this.messages.push({ level: "info", message: m }); }
42
+ warn(m: string) { this.messages.push({ level: "warn", message: m }); }
43
+ error(m: string) { this.messages.push({ level: "error", message: m }); }
44
+ }
45
+
46
+ let worktree: string;
47
+
48
+ beforeAll(() => {
49
+ worktree = mkdtempSync(join(tmpdir(), "bizar-wait-feedback-test-"));
50
+ });
51
+
52
+ afterAll(() => {
53
+ if (worktree) rmSync(worktree, { recursive: true, force: true });
54
+ });
55
+
56
+ function planDir(slug: string) {
57
+ return join(worktree, "plans", slug);
58
+ }
59
+ function planJsonPath(slug: string) {
60
+ return join(planDir(slug), "plan.json");
61
+ }
62
+ function metaJsonPath(slug: string) {
63
+ return join(planDir(slug), "meta.json");
64
+ }
65
+ function seedCanvas(slug: string, canvas: unknown) {
66
+ mkdirSync(planDir(slug), { recursive: true });
67
+ writeFileSync(planJsonPath(slug), JSON.stringify(canvas), "utf-8");
68
+ }
69
+ function seedMeta(slug: string, meta: unknown) {
70
+ mkdirSync(planDir(slug), { recursive: true });
71
+ writeFileSync(metaJsonPath(slug), JSON.stringify(meta), "utf-8");
72
+ }
73
+
74
+ /** Make a sleep function that just bumps a fake clock by `ms` instead of
75
+ * actually waiting. Tests stay fast even with big timeout values. */
76
+ function fakeSleep(now: { t: number }) {
77
+ return (ms: number) => {
78
+ now.t += ms;
79
+ return Promise.resolve();
80
+ };
81
+ }
82
+
83
+ // ===========================================================================
84
+ // Group 1 — timeout
85
+ // ===========================================================================
86
+
87
+ describe("waitForFeedback — timeout", () => {
88
+ test("returns timed_out when no feedback arrives", async () => {
89
+ const slug = "timeout-no-feedback";
90
+ seedCanvas(slug, { schemaVersion: 2, comments: [] });
91
+ const now = { t: 1000 };
92
+ const r = await waitForFeedback(
93
+ {
94
+ worktree,
95
+ logger: new MockLogger(),
96
+ pollIntervalMs: 5,
97
+ sleep: fakeSleep(now),
98
+ now: () => now.t,
99
+ },
100
+ { planSlug: slug, timeoutMs: 50 },
101
+ );
102
+ expect(r.ok).toBe(true);
103
+ if (r.ok) {
104
+ expect(r.status).toBe("timed_out");
105
+ expect(r.newComments).toEqual([]);
106
+ expect(r.planStatus).toBe("draft");
107
+ }
108
+ });
109
+
110
+ test("clamps timeout to minimum 5000ms", async () => {
111
+ const slug = "timeout-clamp";
112
+ seedCanvas(slug, { schemaVersion: 2, comments: [] });
113
+ const now = { t: 0 };
114
+ const r = await waitForFeedback(
115
+ {
116
+ worktree,
117
+ logger: new MockLogger(),
118
+ pollIntervalMs: 5,
119
+ sleep: fakeSleep(now),
120
+ now: () => now.t,
121
+ },
122
+ { planSlug: slug, timeoutMs: 100 }, // way below min
123
+ );
124
+ expect(r.ok).toBe(true);
125
+ if (r.ok) {
126
+ // Waited exactly the clamped minimum (5000ms), no feedback → timed out
127
+ expect(r.status).toBe("timed_out");
128
+ expect(r.waitedMs).toBeGreaterThanOrEqual(5000);
129
+ }
130
+ });
131
+ });
132
+
133
+ // ===========================================================================
134
+ // Group 2 — immediate feedback (comments already present)
135
+ // ===========================================================================
136
+
137
+ describe("waitForFeedback — immediate feedback", () => {
138
+ test("returns feedback_received on first tick when comments exist", async () => {
139
+ const slug = "immediate-comments";
140
+ seedCanvas(slug, {
141
+ schemaVersion: 2,
142
+ comments: [
143
+ { id: "c_1", text: "Hello", created: "2026-06-18T10:00:00Z", author: "DrB0rk" },
144
+ ],
145
+ });
146
+ const now = { t: 0 };
147
+ const r = await waitForFeedback(
148
+ {
149
+ worktree,
150
+ logger: new MockLogger(),
151
+ pollIntervalMs: 5,
152
+ sleep: fakeSleep(now),
153
+ now: () => now.t,
154
+ },
155
+ { planSlug: slug, timeoutMs: 60_000 },
156
+ );
157
+ expect(r.ok).toBe(true);
158
+ if (r.ok) {
159
+ expect(r.status).toBe("feedback_received");
160
+ expect(r.newComments).toHaveLength(1);
161
+ expect(r.newComments[0]!.id).toBe("c_1");
162
+ }
163
+ });
164
+ });
165
+
166
+ // ===========================================================================
167
+ // Group 3 — delayed feedback (comments added while polling)
168
+ // ===========================================================================
169
+
170
+ describe("waitForFeedback — delayed feedback", () => {
171
+ test("returns feedback_received when comments appear during polling", async () => {
172
+ const slug = "delayed-comments";
173
+ seedCanvas(slug, { schemaVersion: 2, comments: [] });
174
+ const now = { t: 0 };
175
+
176
+ // Sleep that injects a comment after 2 ticks
177
+ let sleepCount = 0;
178
+ const sleep = (ms: number) => {
179
+ sleepCount++;
180
+ now.t += ms;
181
+ // After 2 sleeps (i.e. during the 3rd tick), add a comment.
182
+ if (sleepCount === 2) {
183
+ const canvas = {
184
+ schemaVersion: 2,
185
+ comments: [
186
+ { id: "c_late", text: "Late", created: "2026-06-18T12:00:00Z" },
187
+ ],
188
+ };
189
+ writeFileSync(planJsonPath(slug), JSON.stringify(canvas), "utf-8");
190
+ }
191
+ return Promise.resolve();
192
+ };
193
+
194
+ const r = await waitForFeedback(
195
+ {
196
+ worktree,
197
+ logger: new MockLogger(),
198
+ pollIntervalMs: 5,
199
+ sleep,
200
+ now: () => now.t,
201
+ },
202
+ { planSlug: slug, timeoutMs: 60_000 },
203
+ );
204
+ expect(r.ok).toBe(true);
205
+ if (r.ok) {
206
+ expect(r.status).toBe("feedback_received");
207
+ expect(r.newComments).toHaveLength(1);
208
+ expect(r.newComments[0]!.id).toBe("c_late");
209
+ }
210
+ });
211
+ });
212
+
213
+ // ===========================================================================
214
+ // Group 4 — approved status
215
+ // ===========================================================================
216
+
217
+ describe("waitForFeedback — approved", () => {
218
+ test("returns approved when meta.json status is approved (on first tick)", async () => {
219
+ const slug = "approved-first-tick";
220
+ seedCanvas(slug, { schemaVersion: 2, comments: [] });
221
+ seedMeta(slug, { status: "approved" });
222
+ const now = { t: 0 };
223
+ const r = await waitForFeedback(
224
+ {
225
+ worktree,
226
+ logger: new MockLogger(),
227
+ pollIntervalMs: 5,
228
+ sleep: fakeSleep(now),
229
+ now: () => now.t,
230
+ },
231
+ { planSlug: slug, timeoutMs: 60_000 },
232
+ );
233
+ expect(r.ok).toBe(true);
234
+ if (r.ok) {
235
+ expect(r.status).toBe("approved");
236
+ expect(r.planStatus).toBe("approved");
237
+ }
238
+ });
239
+
240
+ test("returns approved when meta.json becomes approved during polling", async () => {
241
+ const slug = "approved-during";
242
+ seedCanvas(slug, { schemaVersion: 2, comments: [] });
243
+ const now = { t: 0 };
244
+ let sleepCount = 0;
245
+ const sleep = (ms: number) => {
246
+ sleepCount++;
247
+ now.t += ms;
248
+ if (sleepCount === 2) {
249
+ writeFileSync(metaJsonPath(slug), JSON.stringify({ status: "approved" }));
250
+ }
251
+ return Promise.resolve();
252
+ };
253
+ const r = await waitForFeedback(
254
+ { worktree, logger: new MockLogger(), pollIntervalMs: 5, sleep, now: () => now.t },
255
+ { planSlug: slug, timeoutMs: 60_000 },
256
+ );
257
+ expect(r.ok).toBe(true);
258
+ if (r.ok) expect(r.status).toBe("approved");
259
+ });
260
+ });
261
+
262
+ // ===========================================================================
263
+ // Group 5 — rejected status
264
+ // ===========================================================================
265
+
266
+ describe("waitForFeedback — rejected", () => {
267
+ test("returns rejected when meta.json status is rejected", async () => {
268
+ const slug = "rejected-ok";
269
+ seedCanvas(slug, { schemaVersion: 2, comments: [] });
270
+ seedMeta(slug, { status: "rejected" });
271
+ const now = { t: 0 };
272
+ const r = await waitForFeedback(
273
+ {
274
+ worktree,
275
+ logger: new MockLogger(),
276
+ pollIntervalMs: 5,
277
+ sleep: fakeSleep(now),
278
+ now: () => now.t,
279
+ },
280
+ { planSlug: slug, timeoutMs: 60_000 },
281
+ );
282
+ expect(r.ok).toBe(true);
283
+ if (r.ok) {
284
+ expect(r.status).toBe("rejected");
285
+ expect(r.planStatus).toBe("rejected");
286
+ }
287
+ });
288
+ });
289
+
290
+ // ===========================================================================
291
+ // Group 6 — sinceTimestamp filter
292
+ // ===========================================================================
293
+
294
+ describe("waitForFeedback — sinceTimestamp filter", () => {
295
+ test("only counts comments strictly after sinceTimestamp", async () => {
296
+ const slug = "since-ts";
297
+ seedCanvas(slug, {
298
+ schemaVersion: 2,
299
+ comments: [
300
+ { id: "c_old", text: "Old", created: "2026-06-18T09:00:00Z" },
301
+ { id: "c_new", text: "New", created: "2026-06-18T11:00:00Z" },
302
+ ],
303
+ });
304
+ const now = { t: 0 };
305
+ const r = await waitForFeedback(
306
+ {
307
+ worktree,
308
+ logger: new MockLogger(),
309
+ pollIntervalMs: 5,
310
+ sleep: fakeSleep(now),
311
+ now: () => now.t,
312
+ },
313
+ {
314
+ planSlug: slug,
315
+ timeoutMs: 60_000,
316
+ sinceTimestamp: "2026-06-18T10:00:00Z",
317
+ },
318
+ );
319
+ expect(r.ok).toBe(true);
320
+ if (r.ok) {
321
+ expect(r.status).toBe("feedback_received");
322
+ expect(r.newComments).toHaveLength(1);
323
+ expect(r.newComments[0]!.id).toBe("c_new");
324
+ }
325
+ });
326
+
327
+ test("returns timed_out when all comments are older than sinceTimestamp", async () => {
328
+ const slug = "since-ts-no-new";
329
+ seedCanvas(slug, {
330
+ schemaVersion: 2,
331
+ comments: [
332
+ { id: "c_old", text: "Old", created: "2026-06-18T09:00:00Z" },
333
+ ],
334
+ });
335
+ const now = { t: 0 };
336
+ const r = await waitForFeedback(
337
+ {
338
+ worktree,
339
+ logger: new MockLogger(),
340
+ pollIntervalMs: 5,
341
+ sleep: fakeSleep(now),
342
+ now: () => now.t,
343
+ },
344
+ {
345
+ planSlug: slug,
346
+ timeoutMs: 60_000,
347
+ sinceTimestamp: "2026-06-18T12:00:00Z",
348
+ },
349
+ );
350
+ expect(r.ok).toBe(true);
351
+ if (r.ok) {
352
+ expect(r.status).toBe("timed_out");
353
+ expect(r.newComments).toEqual([]);
354
+ }
355
+ });
356
+ });
357
+
358
+ // ===========================================================================
359
+ // Group 7 — invalid slug
360
+ // ===========================================================================
361
+
362
+ describe("waitForFeedback — invalid slug", () => {
363
+ test("returns error result for invalid slug", async () => {
364
+ const r = await waitForFeedback(
365
+ { worktree, logger: new MockLogger(), pollIntervalMs: 5, sleep: fakeSleep({ t: 0 }), now: () => 0 },
366
+ { planSlug: "UPPERCASE", timeoutMs: 60_000 },
367
+ );
368
+ expect(r.ok).toBe(false);
369
+ if (!r.ok) {
370
+ expect(r.error).toMatch(/Invalid planSlug/);
371
+ }
372
+ });
373
+ });
374
+
375
+ // ===========================================================================
376
+ // Group 8 — missing plan
377
+ // ===========================================================================
378
+
379
+ describe("waitForFeedback — missing plan", () => {
380
+ test("returns error result when plan directory does not exist", async () => {
381
+ const r = await waitForFeedback(
382
+ { worktree, logger: new MockLogger(), pollIntervalMs: 5, sleep: fakeSleep({ t: 0 }), now: () => 0 },
383
+ { planSlug: "does-not-exist", timeoutMs: 60_000 },
384
+ );
385
+ expect(r.ok).toBe(false);
386
+ if (!r.ok) {
387
+ expect(r.error).toMatch(/Plan not found/);
388
+ }
389
+ });
390
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "ESNext",
5
+ "moduleResolution": "Bundler",
6
+ "lib": ["ES2022"],
7
+ "strict": true,
8
+ "noImplicitAny": true,
9
+ "strictNullChecks": true,
10
+ "noImplicitReturns": true,
11
+ "noFallthroughCasesInSwitch": true,
12
+ "noUncheckedIndexedAccess": true,
13
+ "declaration": true,
14
+ "declarationMap": true,
15
+ "sourceMap": true,
16
+ "esModuleInterop": true,
17
+ "allowSyntheticDefaultImports": true,
18
+ "forceConsistentCasingInFileNames": true,
19
+ "skipLibCheck": true,
20
+ "resolveJsonModule": true,
21
+ "isolatedModules": true,
22
+ "verbatimModuleSyntax": false,
23
+ "types": ["bun"],
24
+ "outDir": "./dist",
25
+ "rootDir": "."
26
+ },
27
+ "include": ["index.ts", "src/**/*.ts", "tests/**/*.ts"],
28
+ "exclude": ["node_modules", "dist"]
29
+ }