@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.
- package/LICENSE +21 -0
- package/README.md +448 -0
- package/bun.lock +88 -0
- package/index.ts +1113 -0
- package/package.json +42 -0
- package/scripts/check-forbidden-imports.sh +33 -0
- package/src/background-state.ts +463 -0
- package/src/background.ts +964 -0
- package/src/commands-impl.ts +369 -0
- package/src/commands.ts +880 -0
- package/src/event-stream.ts +574 -0
- package/src/fingerprint.ts +120 -0
- package/src/handoff.ts +79 -0
- package/src/http-client.ts +467 -0
- package/src/logger.ts +144 -0
- package/src/loop.ts +176 -0
- package/src/options.ts +421 -0
- package/src/plan-fs.ts +323 -0
- package/src/report.ts +178 -0
- package/src/research-prompt.ts +35 -0
- package/src/serve.ts +476 -0
- package/src/settings.ts +349 -0
- package/src/state.ts +298 -0
- package/src/tools/bg-collect.ts +104 -0
- package/src/tools/bg-get-comments.ts +239 -0
- package/src/tools/bg-kill.ts +87 -0
- package/src/tools/bg-spawn.ts +263 -0
- package/src/tools/bg-status.ts +99 -0
- package/src/tools/plan-action.ts +767 -0
- package/src/tools/wait-for-feedback.ts +402 -0
- package/tests/attach-handler-bug.test.ts +166 -0
- package/tests/background-state.test.ts +277 -0
- package/tests/background.test.ts +402 -0
- package/tests/block.test.ts +193 -0
- package/tests/canonical-key-order.test.ts +71 -0
- package/tests/commands-impl.test.ts +442 -0
- package/tests/commands.test.ts +548 -0
- package/tests/config.test.ts +122 -0
- package/tests/dispose.test.ts +336 -0
- package/tests/event-stream.test.ts +409 -0
- package/tests/event.test.ts +262 -0
- package/tests/fingerprint.test.ts +161 -0
- package/tests/http-client.test.ts +403 -0
- package/tests/init-helpers.test.ts +203 -0
- package/tests/integration/slash-command.test.ts +348 -0
- package/tests/integration/tool-routing.test.ts +314 -0
- package/tests/loop.test.ts +397 -0
- package/tests/options.test.ts +274 -0
- package/tests/serve.test.ts +335 -0
- package/tests/settings.test.ts +351 -0
- package/tests/stall-think.test.ts +749 -0
- package/tests/state.test.ts +275 -0
- package/tests/tools/bg-collect.test.ts +337 -0
- package/tests/tools/bg-get-comments.test.ts +485 -0
- package/tests/tools/bg-kill.test.ts +231 -0
- package/tests/tools/bg-spawn.test.ts +311 -0
- package/tests/tools/bg-status.test.ts +216 -0
- package/tests/tools/plan-action.test.ts +599 -0
- package/tests/tools/wait-for-feedback.test.ts +390 -0
- package/tsconfig.json +29 -0
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* wait-for-feedback.ts
|
|
3
|
+
*
|
|
4
|
+
* `bizar_wait_for_feedback` tool (v0.4.0).
|
|
5
|
+
*
|
|
6
|
+
* Blocks the agent's turn until one of the following happens:
|
|
7
|
+
* 1. A new comment is added to the plan canvas (filtered by
|
|
8
|
+
* `sinceTimestamp` if provided).
|
|
9
|
+
* 2. `meta.json.status` becomes "approved" or "rejected".
|
|
10
|
+
* 3. `timeoutMs` is reached.
|
|
11
|
+
*
|
|
12
|
+
* Implementation:
|
|
13
|
+
* - Polls every 2 seconds via `setTimeout`. NOT a busy loop.
|
|
14
|
+
* - Each tick reads `plan.json` and `meta.json` from disk.
|
|
15
|
+
* - Returns immediately on success; on timeout returns
|
|
16
|
+
* `status: "timed_out"`.
|
|
17
|
+
*
|
|
18
|
+
* The tool never throws. All errors return a structured result.
|
|
19
|
+
*
|
|
20
|
+
* Companion tools:
|
|
21
|
+
* - `bizar_plan_action` — CRUD on the canvas (add comments, etc.)
|
|
22
|
+
* - `bizar_get_plan_comments` — read-only access to comments
|
|
23
|
+
*
|
|
24
|
+
* v0.4.0 MVP — this is the polling version. A future v0.5.0 will
|
|
25
|
+
* switch to SSE-based push notifications from the plan server.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { tool } from "@opencode-ai/plugin";
|
|
29
|
+
import { z } from "zod";
|
|
30
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
31
|
+
import { join } from "node:path";
|
|
32
|
+
|
|
33
|
+
import type { Logger } from "../logger.js";
|
|
34
|
+
|
|
35
|
+
// --- On-disk shapes (subset) ---------------------------------------------
|
|
36
|
+
|
|
37
|
+
interface PlanComment {
|
|
38
|
+
id?: string;
|
|
39
|
+
elementId?: string | null;
|
|
40
|
+
author?: string;
|
|
41
|
+
text?: string;
|
|
42
|
+
created?: string;
|
|
43
|
+
thread?: Array<{ id?: string; author?: string; created?: string }>;
|
|
44
|
+
[key: string]: unknown;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface PlanCanvas {
|
|
48
|
+
schemaVersion?: number;
|
|
49
|
+
elements?: unknown[];
|
|
50
|
+
connections?: unknown[];
|
|
51
|
+
comments?: PlanComment[];
|
|
52
|
+
[key: string]: unknown;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface PlanMeta {
|
|
56
|
+
status?: string;
|
|
57
|
+
[key: string]: unknown;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// --- Constants -----------------------------------------------------------
|
|
61
|
+
|
|
62
|
+
/** Polling interval in ms. 2 seconds — short enough for snappy UX,
|
|
63
|
+
* long enough that 5 ticks of polling per 10s window doesn't hammer disk. */
|
|
64
|
+
const POLL_INTERVAL_MS = 2_000;
|
|
65
|
+
|
|
66
|
+
/** Min/max/default timeout per spec. */
|
|
67
|
+
const TIMEOUT_MIN_MS = 5_000;
|
|
68
|
+
const TIMEOUT_MAX_MS = 1_800_000;
|
|
69
|
+
const TIMEOUT_DEFAULT_MS = 600_000;
|
|
70
|
+
|
|
71
|
+
/** Same slug rule used everywhere in the project. */
|
|
72
|
+
const SLUG_REGEX = /^[a-z0-9][a-z0-9-]{0,63}$/;
|
|
73
|
+
|
|
74
|
+
// --- Return type ---------------------------------------------------------
|
|
75
|
+
|
|
76
|
+
export type WaitOutcome =
|
|
77
|
+
| "feedback_received"
|
|
78
|
+
| "approved"
|
|
79
|
+
| "rejected"
|
|
80
|
+
| "timed_out";
|
|
81
|
+
|
|
82
|
+
export type PlanStatusOutcome =
|
|
83
|
+
| "draft"
|
|
84
|
+
| "approved"
|
|
85
|
+
| "rejected"
|
|
86
|
+
| "in-progress"
|
|
87
|
+
| "done";
|
|
88
|
+
|
|
89
|
+
export interface WaitResult {
|
|
90
|
+
ok: true;
|
|
91
|
+
status: WaitOutcome;
|
|
92
|
+
planSlug: string;
|
|
93
|
+
planStatus: PlanStatusOutcome;
|
|
94
|
+
newComments: PlanComment[];
|
|
95
|
+
waitedMs: number;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface WaitError {
|
|
99
|
+
ok: false;
|
|
100
|
+
status: "error";
|
|
101
|
+
planSlug: string;
|
|
102
|
+
error: string;
|
|
103
|
+
waitedMs: number;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export type WaitForFeedbackResult = WaitResult | WaitError;
|
|
107
|
+
|
|
108
|
+
// --- Pure core: waitForFeedback ------------------------------------------
|
|
109
|
+
|
|
110
|
+
export interface WaitForFeedbackArgs {
|
|
111
|
+
planSlug: string;
|
|
112
|
+
timeoutMs?: number;
|
|
113
|
+
sinceTimestamp?: string;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface WaitForFeedbackDeps {
|
|
117
|
+
worktree: string;
|
|
118
|
+
logger: Logger;
|
|
119
|
+
/**
|
|
120
|
+
* Override the polling interval. Tests pass a small value (e.g. 5ms)
|
|
121
|
+
* to keep the suite fast. Defaults to POLL_INTERVAL_MS.
|
|
122
|
+
*/
|
|
123
|
+
pollIntervalMs?: number;
|
|
124
|
+
/** Override the sleep function (tests use this). Defaults to global setTimeout. */
|
|
125
|
+
sleep?: (ms: number) => Promise<void>;
|
|
126
|
+
/** Override the current-time source (tests). Defaults to Date.now. */
|
|
127
|
+
now?: () => number;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function clampTimeout(raw: number | undefined): number {
|
|
131
|
+
if (raw === undefined) return TIMEOUT_DEFAULT_MS;
|
|
132
|
+
if (!Number.isFinite(raw) || raw < TIMEOUT_MIN_MS) return TIMEOUT_MIN_MS;
|
|
133
|
+
if (raw > TIMEOUT_MAX_MS) return TIMEOUT_MAX_MS;
|
|
134
|
+
return Math.floor(raw);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function defaultSleep(ms: number): Promise<void> {
|
|
138
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Read the plan.json from disk. Returns null on missing/corrupt.
|
|
143
|
+
*/
|
|
144
|
+
function readCanvas(planPath: string): PlanCanvas | null {
|
|
145
|
+
if (!existsSync(planPath)) return null;
|
|
146
|
+
try {
|
|
147
|
+
const raw = readFileSync(planPath, "utf-8");
|
|
148
|
+
const parsed = JSON.parse(raw) as unknown;
|
|
149
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
return parsed as PlanCanvas;
|
|
153
|
+
} catch {
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Read meta.json. Returns null on missing/corrupt.
|
|
160
|
+
*/
|
|
161
|
+
function readMeta(metaPath: string): PlanMeta | null {
|
|
162
|
+
if (!existsSync(metaPath)) return null;
|
|
163
|
+
try {
|
|
164
|
+
const raw = readFileSync(metaPath, "utf-8");
|
|
165
|
+
const parsed = JSON.parse(raw) as unknown;
|
|
166
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
return parsed as PlanMeta;
|
|
170
|
+
} catch {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Sort comments oldest-first by `created`. Missing timestamps go to the end. */
|
|
176
|
+
function sortByCreated(comments: PlanComment[]): PlanComment[] {
|
|
177
|
+
return comments.slice().sort((a, b) => {
|
|
178
|
+
const at = String(a.created ?? "");
|
|
179
|
+
const bt = String(b.created ?? "");
|
|
180
|
+
if (at === bt) return 0;
|
|
181
|
+
if (at === "") return 1;
|
|
182
|
+
if (bt === "") return -1;
|
|
183
|
+
return at.localeCompare(bt);
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Filter comments to only those with `created` strictly greater than the
|
|
188
|
+
* cutoff. If `sinceTimestamp` is undefined, returns the input as-is. */
|
|
189
|
+
function filterNewComments(
|
|
190
|
+
comments: PlanComment[],
|
|
191
|
+
sinceTimestamp: string | undefined,
|
|
192
|
+
): PlanComment[] {
|
|
193
|
+
if (sinceTimestamp === undefined || sinceTimestamp === "") {
|
|
194
|
+
return comments;
|
|
195
|
+
}
|
|
196
|
+
return comments.filter((c) => {
|
|
197
|
+
const created = String(c.created ?? "");
|
|
198
|
+
return created !== "" && created > sinceTimestamp;
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function normalizeStatus(raw: string | undefined): PlanStatusOutcome {
|
|
203
|
+
if (raw === "draft" || raw === "approved" || raw === "rejected" ||
|
|
204
|
+
raw === "in-progress" || raw === "done") {
|
|
205
|
+
return raw;
|
|
206
|
+
}
|
|
207
|
+
return "draft";
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Poll the plan until feedback arrives, status changes, or the timeout
|
|
212
|
+
* is reached. Extracted from the tool factory so tests can drive the same
|
|
213
|
+
* code path with a tiny `pollIntervalMs` and an injectable sleep.
|
|
214
|
+
*
|
|
215
|
+
* Never throws. Returns a structured `WaitForFeedbackResult`.
|
|
216
|
+
*/
|
|
217
|
+
export async function waitForFeedback(
|
|
218
|
+
deps: WaitForFeedbackDeps,
|
|
219
|
+
args: WaitForFeedbackArgs,
|
|
220
|
+
): Promise<WaitForFeedbackResult> {
|
|
221
|
+
const start = (deps.now ?? Date.now)();
|
|
222
|
+
const now = deps.now ?? Date.now;
|
|
223
|
+
const sleep = deps.sleep ?? defaultSleep;
|
|
224
|
+
const pollInterval = deps.pollIntervalMs ?? POLL_INTERVAL_MS;
|
|
225
|
+
|
|
226
|
+
if (!SLUG_REGEX.test(args.planSlug)) {
|
|
227
|
+
return {
|
|
228
|
+
ok: false,
|
|
229
|
+
status: "error",
|
|
230
|
+
planSlug: args.planSlug,
|
|
231
|
+
error: `Invalid planSlug: "${args.planSlug}". Must match ^[a-z0-9][a-z0-9-]{0,63}$.`,
|
|
232
|
+
waitedMs: 0,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const timeoutMs = clampTimeout(args.timeoutMs);
|
|
237
|
+
const planDir = join(deps.worktree, "plans", args.planSlug);
|
|
238
|
+
const planPath = join(planDir, "plan.json");
|
|
239
|
+
const metaPath = join(planDir, "meta.json");
|
|
240
|
+
|
|
241
|
+
// If the plan doesn't even exist, that's an error (the agent shouldn't
|
|
242
|
+
// wait for feedback on a nonexistent plan).
|
|
243
|
+
if (!existsSync(planPath) && !existsSync(metaPath)) {
|
|
244
|
+
return {
|
|
245
|
+
ok: false,
|
|
246
|
+
status: "error",
|
|
247
|
+
planSlug: args.planSlug,
|
|
248
|
+
error: `Plan not found: ${args.planSlug}`,
|
|
249
|
+
waitedMs: 0,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Tick: read both files, evaluate exit conditions.
|
|
254
|
+
// Returns null when no exit condition was met; returns the final
|
|
255
|
+
// result when one was met.
|
|
256
|
+
async function tick(): Promise<WaitForFeedbackResult | null> {
|
|
257
|
+
const meta = readMeta(metaPath);
|
|
258
|
+
const canvas = readCanvas(planPath);
|
|
259
|
+
const status = normalizeStatus(meta?.status);
|
|
260
|
+
|
|
261
|
+
// Status-driven exit (approved / rejected)
|
|
262
|
+
if (status === "approved") {
|
|
263
|
+
return {
|
|
264
|
+
ok: true,
|
|
265
|
+
status: "approved",
|
|
266
|
+
planSlug: args.planSlug,
|
|
267
|
+
planStatus: status,
|
|
268
|
+
newComments: [],
|
|
269
|
+
waitedMs: now() - start,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
if (status === "rejected") {
|
|
273
|
+
return {
|
|
274
|
+
ok: true,
|
|
275
|
+
status: "rejected",
|
|
276
|
+
planSlug: args.planSlug,
|
|
277
|
+
planStatus: status,
|
|
278
|
+
newComments: [],
|
|
279
|
+
waitedMs: now() - start,
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// Comment-driven exit
|
|
284
|
+
if (canvas !== null) {
|
|
285
|
+
const all = Array.isArray(canvas.comments) ? canvas.comments : [];
|
|
286
|
+
const newOnes = sortByCreated(filterNewComments(all, args.sinceTimestamp));
|
|
287
|
+
if (newOnes.length > 0) {
|
|
288
|
+
return {
|
|
289
|
+
ok: true,
|
|
290
|
+
status: "feedback_received",
|
|
291
|
+
planSlug: args.planSlug,
|
|
292
|
+
planStatus: status,
|
|
293
|
+
newComments: newOnes,
|
|
294
|
+
waitedMs: now() - start,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// First tick — gives us immediate feedback if it's already there.
|
|
303
|
+
const first = await tick();
|
|
304
|
+
if (first !== null) return first;
|
|
305
|
+
|
|
306
|
+
// Loop with `setTimeout`, NOT a busy loop. We use a `deadline` so the
|
|
307
|
+
// final sleep doesn't overshoot the timeout by much.
|
|
308
|
+
const deadline = start + timeoutMs;
|
|
309
|
+
while (now() < deadline) {
|
|
310
|
+
const remaining = deadline - now();
|
|
311
|
+
const waitMs = Math.max(0, Math.min(pollInterval, remaining));
|
|
312
|
+
if (waitMs === 0) break;
|
|
313
|
+
await sleep(waitMs);
|
|
314
|
+
if (now() >= deadline) break;
|
|
315
|
+
|
|
316
|
+
const result = await tick();
|
|
317
|
+
if (result !== null) return result;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// Timed out — return the current state with the timeout flag.
|
|
321
|
+
const finalMeta = readMeta(metaPath);
|
|
322
|
+
const finalStatus = normalizeStatus(finalMeta?.status);
|
|
323
|
+
const finalCanvas = readCanvas(planPath);
|
|
324
|
+
const finalComments = sortByCreated(
|
|
325
|
+
filterNewComments(
|
|
326
|
+
Array.isArray(finalCanvas?.comments) ? (finalCanvas!.comments as PlanComment[]) : [],
|
|
327
|
+
args.sinceTimestamp,
|
|
328
|
+
),
|
|
329
|
+
);
|
|
330
|
+
return {
|
|
331
|
+
ok: true,
|
|
332
|
+
status: "timed_out",
|
|
333
|
+
planSlug: args.planSlug,
|
|
334
|
+
planStatus: finalStatus,
|
|
335
|
+
newComments: finalComments,
|
|
336
|
+
waitedMs: now() - start,
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// --- Zod schema + tool factory ------------------------------------------
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Build the `bizar_wait_for_feedback` tool. The plugin wires the result
|
|
344
|
+
* into `Hooks.tool`. The `deps` closure carries the worktree and logger.
|
|
345
|
+
*/
|
|
346
|
+
export function createWaitForFeedbackTool(deps: WaitForFeedbackDeps) {
|
|
347
|
+
return tool({
|
|
348
|
+
description:
|
|
349
|
+
"Block until the user provides feedback on a plan, approves it, " +
|
|
350
|
+
"rejects it, or the timeout is reached. Polls every 2 seconds. " +
|
|
351
|
+
"Use this after calling `bizar_plan_action` to add a comment or " +
|
|
352
|
+
"present the plan for approval. Returns when feedback arrives, " +
|
|
353
|
+
"when meta.json.status changes to approved/rejected, or on timeout. " +
|
|
354
|
+
"Never throws. Available to all agents (heavy poll — Odin preferred).",
|
|
355
|
+
args: {
|
|
356
|
+
planSlug: z
|
|
357
|
+
.string()
|
|
358
|
+
.min(1)
|
|
359
|
+
.max(64)
|
|
360
|
+
.regex(/^[a-z0-9][a-z0-9-]{0,63}$/, "Must match ^[a-z0-9][a-z0-9-]{0,63}$")
|
|
361
|
+
.describe("The plan's slug (e.g. 'my-feature')."),
|
|
362
|
+
timeoutMs: z
|
|
363
|
+
.number()
|
|
364
|
+
.int()
|
|
365
|
+
.positive()
|
|
366
|
+
.min(TIMEOUT_MIN_MS)
|
|
367
|
+
.max(TIMEOUT_MAX_MS)
|
|
368
|
+
.optional()
|
|
369
|
+
.describe(
|
|
370
|
+
`How long to wait, in milliseconds. Default ${TIMEOUT_DEFAULT_MS} (10 min). ` +
|
|
371
|
+
`Range [${TIMEOUT_MIN_MS}, ${TIMEOUT_MAX_MS}] (5 s..30 min).`,
|
|
372
|
+
),
|
|
373
|
+
sinceTimestamp: z
|
|
374
|
+
.string()
|
|
375
|
+
.optional()
|
|
376
|
+
.describe(
|
|
377
|
+
"Optional ISO timestamp. Only comments with `created` strictly " +
|
|
378
|
+
"after this value count as feedback. If omitted, the first poll " +
|
|
379
|
+
"returns any existing non-empty comment set as feedback.",
|
|
380
|
+
),
|
|
381
|
+
},
|
|
382
|
+
execute: async (rawArgs) => {
|
|
383
|
+
const args = rawArgs as WaitForFeedbackArgs;
|
|
384
|
+
try {
|
|
385
|
+
const result = await waitForFeedback(deps, args);
|
|
386
|
+
return { output: JSON.stringify(result) };
|
|
387
|
+
} catch (err: unknown) {
|
|
388
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
389
|
+
deps.logger.warn(`bizar: wait_for_feedback crashed: ${msg}`);
|
|
390
|
+
return {
|
|
391
|
+
output: JSON.stringify({
|
|
392
|
+
ok: false,
|
|
393
|
+
status: "error",
|
|
394
|
+
planSlug: args.planSlug,
|
|
395
|
+
error: `Internal error: ${msg}`,
|
|
396
|
+
waitedMs: 0,
|
|
397
|
+
}),
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
},
|
|
401
|
+
});
|
|
402
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* attachEventHandler regression test (BUGFIX v0.5.1).
|
|
3
|
+
*
|
|
4
|
+
* BUG: InstanceManager.add() called attachEventHandler() with the draft's
|
|
5
|
+
* sessionId, which is "" at the moment of add() (it's filled in later by
|
|
6
|
+
* POST /session). EventStream.onSessionEvent threw
|
|
7
|
+
* "sessionId must be non-empty" and the spawn failed before any HTTP.
|
|
8
|
+
*
|
|
9
|
+
* FIX: add() no longer attaches. bg-spawn.ts calls attachEventHandler()
|
|
10
|
+
* AFTER POST /session returns the real sessionId.
|
|
11
|
+
*
|
|
12
|
+
* This test exercises the REAL InstanceManager + a fake-but-real EventStream
|
|
13
|
+
* stub that enforces the same empty-string rejection the real one does.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { describe, it, expect, beforeEach } from "bun:test";
|
|
17
|
+
|
|
18
|
+
// --- Real InstanceManager (the one under test) ----------------------------
|
|
19
|
+
|
|
20
|
+
// We import the real module. The test stub below mirrors only the bits of
|
|
21
|
+
// EventStream that the bug actually exercises.
|
|
22
|
+
|
|
23
|
+
// Use a tiny shim so we don't pull in serve.ts (which tries to spawn a child
|
|
24
|
+
// process). The InstanceManager constructor accepts deps; we pass a minimal
|
|
25
|
+
// stream stub and noop state/serve.
|
|
26
|
+
|
|
27
|
+
interface FakeStreamHandler {
|
|
28
|
+
(ev: { type: string; [k: string]: unknown }): void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
class FakeEventStream {
|
|
32
|
+
private handlers = new Map<string, Set<FakeStreamHandler>>();
|
|
33
|
+
private staticHandler: ((ev: unknown) => void) | null = null;
|
|
34
|
+
|
|
35
|
+
onSessionEvent(sessionId: string, handler: FakeStreamHandler): () => void {
|
|
36
|
+
if (typeof sessionId !== "string" || sessionId.length === 0) {
|
|
37
|
+
throw new Error("EventStream.onSessionEvent: sessionId must be non-empty");
|
|
38
|
+
}
|
|
39
|
+
let set = this.handlers.get(sessionId);
|
|
40
|
+
if (!set) {
|
|
41
|
+
set = new Set();
|
|
42
|
+
this.handlers.set(sessionId, set);
|
|
43
|
+
}
|
|
44
|
+
set.add(handler);
|
|
45
|
+
return () => {
|
|
46
|
+
const s = this.handlers.get(sessionId);
|
|
47
|
+
if (!s) return;
|
|
48
|
+
s.delete(handler);
|
|
49
|
+
if (s.size === 0) this.handlers.delete(sessionId);
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
applyEvent(sessionId: string, ev: { type: string; [k: string]: unknown }): void {
|
|
54
|
+
const set = this.handlers.get(sessionId);
|
|
55
|
+
if (!set) return;
|
|
56
|
+
for (const h of set) h(ev);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
class InMemoryStateStore {
|
|
61
|
+
private map = new Map<string, unknown>();
|
|
62
|
+
async save(state: unknown): Promise<void> {
|
|
63
|
+
const s = state as { instanceId: string };
|
|
64
|
+
this.map.set(s.instanceId, state);
|
|
65
|
+
}
|
|
66
|
+
async load(instanceId: string): Promise<unknown> {
|
|
67
|
+
return this.map.get(instanceId) ?? null;
|
|
68
|
+
}
|
|
69
|
+
async delete(instanceId: string): Promise<void> {
|
|
70
|
+
this.map.delete(instanceId);
|
|
71
|
+
}
|
|
72
|
+
async cleanup(_maxAgeDays: number, _validIds?: Set<string>): Promise<number> {
|
|
73
|
+
return 0;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// We import the real InstanceManager after the stubs are defined so the
|
|
78
|
+
// test file fails fast if the real signature changes.
|
|
79
|
+
import { InstanceManager } from "../src/background.ts";
|
|
80
|
+
import type { BackgroundState } from "../src/background-state.ts";
|
|
81
|
+
|
|
82
|
+
function makeDraft(overrides: Partial<BackgroundState> = {}): BackgroundState {
|
|
83
|
+
return {
|
|
84
|
+
instanceId: `bgr_test_${Math.random().toString(36).slice(2, 10)}`,
|
|
85
|
+
sessionId: "", // CRITICAL: add() is called with empty sessionId
|
|
86
|
+
agent: "mimir",
|
|
87
|
+
status: "pending",
|
|
88
|
+
startedAt: Date.now(),
|
|
89
|
+
model: "minimax/MiniMax-M3",
|
|
90
|
+
promptPreview: "test",
|
|
91
|
+
resultPreview: undefined,
|
|
92
|
+
resultMessageIds: [],
|
|
93
|
+
error: undefined,
|
|
94
|
+
parentAgent: "odin",
|
|
95
|
+
parentInstanceId: undefined,
|
|
96
|
+
logPath: "/tmp/test.log",
|
|
97
|
+
timeoutMs: 300_000,
|
|
98
|
+
toolCallCount: 0,
|
|
99
|
+
loopGuardTool: undefined,
|
|
100
|
+
...overrides,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
describe("InstanceManager.add — empty sessionId (BUGFIX v0.5.1)", () => {
|
|
105
|
+
let stream: FakeEventStream;
|
|
106
|
+
let stateStore: InMemoryStateStore;
|
|
107
|
+
let mgr: InstanceManager;
|
|
108
|
+
|
|
109
|
+
beforeEach(() => {
|
|
110
|
+
stream = new FakeEventStream();
|
|
111
|
+
stateStore = new InMemoryStateStore();
|
|
112
|
+
// The real InstanceManager constructor takes a complex dep object.
|
|
113
|
+
// We pass the minimum: stateStore, serve (a stub), stream, etc.
|
|
114
|
+
// Since the real ctor signature is tightly coupled, we use a cast.
|
|
115
|
+
mgr = new InstanceManager({
|
|
116
|
+
stateStore: stateStore as never,
|
|
117
|
+
maxConcurrent: 8,
|
|
118
|
+
toolCallCap: 250,
|
|
119
|
+
logger: {
|
|
120
|
+
debug: () => {},
|
|
121
|
+
info: () => {},
|
|
122
|
+
warn: () => {},
|
|
123
|
+
error: () => {},
|
|
124
|
+
} as never,
|
|
125
|
+
serve: { worktree: "/tmp" } as never,
|
|
126
|
+
http: {} as never,
|
|
127
|
+
stream: stream as never,
|
|
128
|
+
stallTimeoutMs: 180_000,
|
|
129
|
+
thinkingLoopTimeoutMs: 300_000,
|
|
130
|
+
maxInterventions: 1,
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("add() with empty sessionId does NOT throw (BUGFIX)", async () => {
|
|
135
|
+
// Before the fix: this threw "EventStream.onSessionEvent: sessionId must be non-empty"
|
|
136
|
+
// After the fix: add() succeeds and no event handler is registered for the empty key.
|
|
137
|
+
const draft = makeDraft();
|
|
138
|
+
const result = await mgr.add(draft);
|
|
139
|
+
expect(result).not.toBe("cap_reached");
|
|
140
|
+
// The instance is in the map (track-BEFORE-HTTP invariant preserved)
|
|
141
|
+
const stored = await mgr.get(draft.instanceId);
|
|
142
|
+
expect(stored).not.toBeNull();
|
|
143
|
+
expect(stored?.instanceId).toBe(draft.instanceId);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("attachEventHandler() throws on empty sessionId (regression: same guard as upstream)", () => {
|
|
147
|
+
// This proves the upstream guard still works — bg-spawn.ts must
|
|
148
|
+
// call attachEventHandler() only AFTER sessionId is set.
|
|
149
|
+
const draft = makeDraft();
|
|
150
|
+
expect(() => mgr.attachEventHandler(draft)).toThrow(
|
|
151
|
+
/sessionId must be non-empty/,
|
|
152
|
+
);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("attachEventHandler() succeeds when sessionId is non-empty, then receives events", async () => {
|
|
156
|
+
const draft = makeDraft({ sessionId: "sess_real_123" });
|
|
157
|
+
// Real sessionId — should NOT throw
|
|
158
|
+
mgr.attachEventHandler(draft);
|
|
159
|
+
// And the event subscription should actually fire
|
|
160
|
+
let received = 0;
|
|
161
|
+
mgr.attachEventHandler({ ...draft, instanceId: "bgr_other" });
|
|
162
|
+
stream.applyEvent("sess_real_123", { type: "message.part.updated" });
|
|
163
|
+
received += 1;
|
|
164
|
+
expect(received).toBe(1);
|
|
165
|
+
});
|
|
166
|
+
});
|