@polderlabs/bizar-plugin 0.6.2 → 0.8.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.
@@ -0,0 +1,362 @@
1
+ /**
2
+ * plugins/bizar/src/opencode-runner.ts
3
+ *
4
+ * v0.8.0 — Process-based background agent runner.
5
+ *
6
+ * Replaces the v0.4–v0.7 `opencode serve` HTTP path. The HTTP API is
7
+ * passive — see the opencode docs:
8
+ * "When you run opencode it starts a TUI and a server. Where the
9
+ * TUI is the client that talks to the server."
10
+ *
11
+ * `opencode run <prompt>` is the active path: it spawns the agent
12
+ * loop in-process, drives it to completion, and exits. This module
13
+ * gives the plugin a clean way to spawn one `opencode run` per
14
+ * background agent, capture its output to the LogWriter's log file,
15
+ * track the PID for status/kill, and extract the opencode session
16
+ * id from the structured log stream.
17
+ *
18
+ * ─────────────────────────────────────────────────────────────────
19
+ * Why this exists (v0.7.0-alpha.1 → v0.8.0)
20
+ * ─────────────────────────────────────────────────────────────────
21
+ * Pre-v0.8.0, the plugin POSTed to `/api/session/{id}/prompt` on the
22
+ * opencode serve child. The server admitted the prompt
23
+ * (`session.next.prompt.admitted` event) but no agent loop processed
24
+ * it. The result: a session record was created, a tmux pane was
25
+ * spawned, and nothing happened. The user saw "spawns but does
26
+ * nothing" and was right.
27
+ *
28
+ * This module fixes that by spawning `opencode run` directly.
29
+ *
30
+ * ─────────────────────────────────────────────────────────────────
31
+ * Wire format
32
+ * ─────────────────────────────────────────────────────────────────
33
+ * `opencode run` writes structured logs to stderr, one log per line:
34
+ *
35
+ * timestamp=2026-06-24T01:25:13.537Z level=INFO run=<uuid> message="creating instance" directory=/tmp
36
+ * timestamp=2026-06-24T01:25:16.555Z level=INFO run=<uuid> message=created id=ses_… title="…" agent=…
37
+ * timestamp=2026-06-24T01:25:16.951Z level=INFO run=<uuid> message=loop session.id=ses_… step=0
38
+ * timestamp=2026-06-24T01:25:21.253Z level=INFO run=<uuid> message="exiting loop" session.id=ses_…
39
+ *
40
+ * The `message=created id=ses_<id>` line is how we recover the
41
+ * sessionId (the agent generated it; we don't pre-allocate). We
42
+ * parse the line with `message=created id=(ses_[A-Za-z0-9_]+)`.
43
+ *
44
+ * Both stdout and stderr are piped to `logPath` so the operator's
45
+ * tmux pane (and the dashboard's log viewer) can watch the agent
46
+ * work in real time. We prefix each line with the stream name
47
+ * ([stdout] / [stderr]) for human readability; downstream parsers
48
+ * can split on the first `] `.
49
+ */
50
+ import { createWriteStream, mkdirSync, existsSync } from "node:fs";
51
+ import { dirname } from "node:path";
52
+ import type { Subprocess } from "bun";
53
+
54
+ export type AgentState = "starting" | "running" | "done" | "failed" | "killed";
55
+
56
+ export interface AgentStatus {
57
+ state: AgentState;
58
+ sessionId?: string;
59
+ processId: number;
60
+ startedAt: number;
61
+ endedAt?: number;
62
+ exitCode?: number;
63
+ error?: string;
64
+ }
65
+
66
+ export interface SpawnAgentOptions {
67
+ /** The prompt to send. */
68
+ prompt: string;
69
+ /** Agent name (mimir, thor, …) — used as the session title prefix. */
70
+ agent: string;
71
+ /** Optional model override in "providerID/modelID" format. */
72
+ model?: { providerID: string; modelID: string };
73
+ /** Working directory for the opencode run. */
74
+ worktree: string;
75
+ /** Absolute path to the log file (the LogWriter's output path). */
76
+ logPath: string;
77
+ /** Optional session title (defaults to `bgr:<agent>`). */
78
+ title?: string;
79
+ /** Extra env vars to pass through. */
80
+ env?: Record<string, string>;
81
+ /** How long to wait for the sessionId to appear in stderr (ms). */
82
+ sessionIdTimeoutMs?: number;
83
+ }
84
+
85
+ export interface SpawnAgentResult {
86
+ ok: boolean;
87
+ sessionId?: string;
88
+ processId?: number;
89
+ error?: string;
90
+ }
91
+
92
+ type ExitCallback = (status: AgentStatus) => void;
93
+
94
+ interface AgentRecord {
95
+ status: AgentStatus;
96
+ proc: Subprocess;
97
+ onExit: ExitCallback[];
98
+ // Resolvers for the spawn promise. Consumed when the sessionId
99
+ // arrives (or the process exits before that).
100
+ spawnResolvers: Array<(result: SpawnAgentResult) => void>;
101
+ }
102
+
103
+ // Module-level registry of running agents.
104
+ const agents = new Map<number, AgentRecord>();
105
+
106
+ // --- Public API -----------------------------------------------------------
107
+
108
+ /**
109
+ * Spawn a single `opencode run` process. The promise resolves once
110
+ * the opencode child has reported its session id in the structured
111
+ * log stream (or once the process exits before that — typically
112
+ * because of an early error like a missing API key).
113
+ *
114
+ * @param opts
115
+ * @returns Promise resolving with `{ ok, sessionId?, processId? }` or `{ ok: false, error }`
116
+ */
117
+ export async function spawnAgent(opts: SpawnAgentOptions): Promise<SpawnAgentResult> {
118
+ // 1. Pre-flight: log dir must exist so the stream can open.
119
+ const logDir = dirname(opts.logPath);
120
+ if (!existsSync(logDir)) {
121
+ try {
122
+ mkdirSync(logDir, { recursive: true });
123
+ } catch (err: unknown) {
124
+ return {
125
+ ok: false,
126
+ error: `cannot create log dir ${logDir}: ${err instanceof Error ? err.message : String(err)}`,
127
+ };
128
+ }
129
+ }
130
+
131
+ // 2. Build argv. Note: opencode run takes the prompt as a positional
132
+ // arg. `--dir` sets the worktree. `--print-logs` ensures the
133
+ // structured log stream goes to stderr.
134
+ const args: string[] = [
135
+ "opencode",
136
+ "run",
137
+ "--dir", opts.worktree,
138
+ "--print-logs",
139
+ "--log-level", "INFO",
140
+ "--title", opts.title || `bgr:${opts.agent}:${Date.now()}`,
141
+ ];
142
+ if (opts.model) {
143
+ args.push("--model", `${opts.model.providerID}/${opts.model.modelID}`);
144
+ }
145
+ // `--` separates flags from positional so a prompt starting with
146
+ // `-` is treated as a message.
147
+ args.push("--", opts.prompt);
148
+
149
+ // 3. Spawn the process.
150
+ let proc: Subprocess;
151
+ try {
152
+ proc = Bun.spawn(args, {
153
+ stdout: "pipe",
154
+ stderr: "pipe",
155
+ env: { ...process.env, ...(opts.env || {}) },
156
+ });
157
+ } catch (err: unknown) {
158
+ return {
159
+ ok: false,
160
+ error: `failed to spawn opencode run: ${err instanceof Error ? err.message : String(err)}`,
161
+ };
162
+ }
163
+
164
+ // 4. Track the agent.
165
+ const rec: AgentRecord = {
166
+ status: {
167
+ state: "starting",
168
+ processId: proc.pid,
169
+ startedAt: Date.now(),
170
+ },
171
+ proc,
172
+ onExit: [],
173
+ spawnResolvers: [],
174
+ };
175
+ agents.set(proc.pid, rec);
176
+
177
+ // 5. Pipe stdout+stderr to the log file. Both go to the same file;
178
+ // lines are prefixed [stdout] / [stderr] for readability.
179
+ const logStream = createWriteStream(opts.logPath, { flags: "a" });
180
+ const decoder = new TextDecoder("utf-8");
181
+ const sessionIdRegex = /message=created id=(ses_[A-Za-z0-9_]+)/;
182
+ let sessionId: string | undefined;
183
+
184
+ const resolveSpawnIfReady = (forceError?: string) => {
185
+ const resolvers = rec.spawnResolvers.splice(0);
186
+ if (resolvers.length === 0) return;
187
+ if (forceError) {
188
+ for (const r of resolvers) {
189
+ r({ ok: false, processId: proc.pid, error: forceError });
190
+ }
191
+ return;
192
+ }
193
+ if (sessionId) {
194
+ rec.status.state = "running";
195
+ for (const r of resolvers) {
196
+ r({ ok: true, sessionId, processId: proc.pid });
197
+ }
198
+ }
199
+ };
200
+
201
+ const readStream = async (
202
+ reader: ReadableStreamDefaultReader<Uint8Array>,
203
+ label: "stderr" | "stdout",
204
+ ): Promise<void> => {
205
+ let buf = "";
206
+ try {
207
+ while (true) {
208
+ const { done, value } = await reader.read();
209
+ if (done) break;
210
+ const text = decoder.decode(value, { stream: true });
211
+ logStream.write(`[${label}] ${text}`);
212
+ if (label === "stderr" && !sessionId) {
213
+ buf += text;
214
+ const m = buf.match(sessionIdRegex);
215
+ if (m && m[1]) {
216
+ sessionId = m[1];
217
+ rec.status.sessionId = sessionId;
218
+ resolveSpawnIfReady();
219
+ }
220
+ }
221
+ }
222
+ } finally {
223
+ try { reader.releaseLock(); } catch { /* ignore */ }
224
+ }
225
+ };
226
+
227
+ // 6. Stream readers + exit handler — install BEFORE returning the
228
+ // promise so a fast-exiting process still produces a clean
229
+ // resolution.
230
+ const stderrReader = (proc.stderr as ReadableStream<Uint8Array>).getReader();
231
+ const stdoutReader = (proc.stdout as ReadableStream<Uint8Array>).getReader();
232
+ void readStream(stderrReader, "stderr");
233
+ void readStream(stdoutReader, "stdout");
234
+
235
+ proc.exited
236
+ .then((exitCode: number | null) => {
237
+ const code = exitCode ?? -1;
238
+ rec.status.exitCode = exitCode ?? undefined;
239
+ rec.status.endedAt = Date.now();
240
+ if (rec.status.state !== "killed") {
241
+ rec.status.state = code === 0 ? "done" : "failed";
242
+ if (code !== 0 && !rec.status.error) {
243
+ rec.status.error = `opencode run exited with code ${code}`;
244
+ }
245
+ }
246
+ // Resolve any unresolved spawn promises.
247
+ if (!sessionId) {
248
+ resolveSpawnIfReady(rec.status.error || "opencode run exited before reporting session id");
249
+ }
250
+ // Fire subscribers.
251
+ const cbs = rec.onExit.splice(0);
252
+ for (const cb of cbs) {
253
+ try { cb({ ...rec.status }); } catch { /* ignore */ }
254
+ }
255
+ // Best-effort flush + close of the log stream.
256
+ try { logStream.end(); } catch { /* ignore */ }
257
+ })
258
+ .catch(() => {
259
+ // proc.exited only rejects if the proc was already awaited or
260
+ // never spawned; safe to ignore.
261
+ });
262
+
263
+ // 7. Race the spawn promise against a timeout. The default is 10s
264
+ // which is plenty for opencode to print the "created" line on
265
+ // a warm host (<500ms in practice).
266
+ const sessionIdTimeoutMs = opts.sessionIdTimeoutMs ?? 10_000;
267
+ return new Promise<SpawnAgentResult>((resolve) => {
268
+ rec.spawnResolvers.push(resolve);
269
+ setTimeout(() => {
270
+ if (!sessionId) {
271
+ resolveSpawnIfReady(
272
+ `opencode run did not report a session id within ${sessionIdTimeoutMs}ms`,
273
+ );
274
+ }
275
+ }, sessionIdTimeoutMs);
276
+ });
277
+ }
278
+
279
+ /**
280
+ * Snapshot the current status of an agent. Returns null if the
281
+ * processId is not tracked (e.g. the plugin restarted and lost its
282
+ * in-memory map).
283
+ */
284
+ export function getStatus(processId: number): AgentStatus | null {
285
+ const rec = agents.get(processId);
286
+ if (!rec) return null;
287
+ return { ...rec.status };
288
+ }
289
+
290
+ /**
291
+ * Subscribe to the exit event. The callback fires exactly once when
292
+ * the process exits (whether naturally or via signal). Multiple
293
+ * subscribers are allowed; all of them fire in registration order.
294
+ *
295
+ * @returns unsubscribe function
296
+ */
297
+ export function onExit(processId: number, cb: ExitCallback): () => void {
298
+ const rec = agents.get(processId);
299
+ if (!rec) return () => undefined;
300
+ rec.onExit.push(cb);
301
+ // If the agent has already exited, fire the callback immediately
302
+ // (next microtask) so the caller doesn't have to special-case
303
+ // pre-exited agents.
304
+ if (rec.status.endedAt !== undefined) {
305
+ queueMicrotask(() => {
306
+ try { cb({ ...rec.status }); } catch { /* ignore */ }
307
+ });
308
+ }
309
+ return () => {
310
+ const i = rec.onExit.indexOf(cb);
311
+ if (i >= 0) rec.onExit.splice(i, 1);
312
+ };
313
+ }
314
+
315
+ /**
316
+ * Kill the agent. Sends the requested signal (default SIGTERM),
317
+ * waits up to 5s, then SIGKILL. Idempotent.
318
+ */
319
+ export function killAgent(
320
+ processId: number,
321
+ signal: "SIGTERM" | "SIGKILL" = "SIGTERM",
322
+ ): { ok: boolean; error?: string } {
323
+ const rec = agents.get(processId);
324
+ if (!rec) {
325
+ return { ok: true, error: "no such process tracked" };
326
+ }
327
+ if (rec.status.endedAt !== undefined) {
328
+ return { ok: true, error: "process already exited" };
329
+ }
330
+ rec.status.state = "killed";
331
+ try {
332
+ rec.proc.kill(signal);
333
+ } catch (err: unknown) {
334
+ return {
335
+ ok: false,
336
+ error: `kill(${signal}) failed: ${err instanceof Error ? err.message : String(err)}`,
337
+ };
338
+ }
339
+ // Schedule a forced kill after 5s in case SIGTERM is ignored.
340
+ setTimeout(() => {
341
+ if (rec.status.endedAt === undefined) {
342
+ try { rec.proc.kill("SIGKILL"); } catch { /* ignore */ }
343
+ }
344
+ }, 5_000);
345
+ return { ok: true };
346
+ }
347
+
348
+ /**
349
+ * List every tracked agent. Used by the plugin's startup to reconcile
350
+ * state and by tests.
351
+ */
352
+ export function list(): AgentStatus[] {
353
+ return Array.from(agents.values()).map((r) => ({ ...r.status }));
354
+ }
355
+
356
+ /**
357
+ * For tests: clear the in-memory registry. Does NOT kill running
358
+ * processes — callers must `killAgent()` first if they want that.
359
+ */
360
+ export function _resetForTests(): void {
361
+ agents.clear();
362
+ }