@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
package/src/serve.ts
ADDED
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* serve.ts
|
|
3
|
+
*
|
|
4
|
+
* ServeLifecycle — owns the `opencode serve` child process.
|
|
5
|
+
*
|
|
6
|
+
* Spec contract (v0.4.2 §1.1, §5, §6.1):
|
|
7
|
+
* - `Bun.spawn(["opencode", "serve", "--port", String(port), "--hostname", "127.0.0.1"], …)`
|
|
8
|
+
* - `OPENCODE_SERVER_PASSWORD` set in the child env to a 32-byte secret
|
|
9
|
+
* base64-encoded.
|
|
10
|
+
* - All subsequent HTTP calls include `Authorization: Basic <b64>` with
|
|
11
|
+
* username "opencode".
|
|
12
|
+
* - `--hostname 127.0.0.1` is hardcoded; not configurable.
|
|
13
|
+
* - `--dangerously-skip-permissions` is NOT in the default args. It is
|
|
14
|
+
* added only when `BIZAR_BACKGROUND_SKIP_PERMISSIONS=1`.
|
|
15
|
+
* - Health check: poll `GET /health` on the bound port with 100ms
|
|
16
|
+
* interval and 5s timeout.
|
|
17
|
+
* - Crash recovery: `proc.exited.then(...)` callback; on unexpected
|
|
18
|
+
* exit, notify a registered callback (caller marks instances failed).
|
|
19
|
+
* - Restart with exponential backoff (250ms, 500ms, 1s; max 3 retries).
|
|
20
|
+
*
|
|
21
|
+
* This file is the ONLY place in the plugin allowed to import
|
|
22
|
+
* `node:crypto` (NEW-H1, HIGH-24, NEW-H9). The forbidden-imports check
|
|
23
|
+
* (scripts/check-forbidden-imports.sh) enforces this exception.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { getRandomValues } from "node:crypto";
|
|
27
|
+
import type { Subprocess } from "bun";
|
|
28
|
+
|
|
29
|
+
// --- Logger interface -----------------------------------------------------
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Minimal Logger interface — matches the shape in `state.ts` / `logger.ts`.
|
|
33
|
+
*/
|
|
34
|
+
export interface Logger {
|
|
35
|
+
log(opts: { level: "debug" | "info" | "warn" | "error"; message: string }): void;
|
|
36
|
+
debug(message: string): void;
|
|
37
|
+
info(message: string): void;
|
|
38
|
+
warn(message: string): void;
|
|
39
|
+
error(message: string): void;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// --- Public surface -------------------------------------------------------
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Callback invoked when the serve child exits unexpectedly. The plugin
|
|
46
|
+
* uses this to mark all in-memory instances `failed` with
|
|
47
|
+
* `error: "serve child exited unexpectedly"`.
|
|
48
|
+
*/
|
|
49
|
+
export type OnUnexpectedExit = (exitCode: number | null) => void;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Result of a successful `start()`. The plugin stores these so the
|
|
53
|
+
* `HttpClient` can authenticate and the `EventStream` can subscribe.
|
|
54
|
+
*/
|
|
55
|
+
export interface ServeInfo {
|
|
56
|
+
pid: number;
|
|
57
|
+
port: number;
|
|
58
|
+
password: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Lifecycle owner for one `opencode serve` child process.
|
|
63
|
+
*
|
|
64
|
+
* Public surface (the interface contract for Thor's tests):
|
|
65
|
+
* - `start()` — spawn the child, wait for the listening line, health-check.
|
|
66
|
+
* - `stop()` — SIGTERM the child, wait 5s, then SIGKILL.
|
|
67
|
+
* - `healthCheck()` — poll `GET /health` once.
|
|
68
|
+
* - `pid`, `port`, `password`, `baseUrl` getters.
|
|
69
|
+
* - `onUnexpectedExit(cb)` — register a crash-recovery callback.
|
|
70
|
+
* - `tryRestart()` — exponential backoff restart (250/500/1000ms, 3 tries).
|
|
71
|
+
*
|
|
72
|
+
* The class is single-use: after `stop()`, the instance is dead and a
|
|
73
|
+
* new one must be constructed. `tryRestart()` reuses the same object.
|
|
74
|
+
*/
|
|
75
|
+
export class ServeLifecycle {
|
|
76
|
+
private _port: number | null = null;
|
|
77
|
+
private _pid: number | null = null;
|
|
78
|
+
private _password: string | null = null;
|
|
79
|
+
private _proc: Subprocess | null = null;
|
|
80
|
+
private _worktree: string;
|
|
81
|
+
private _logger: Logger;
|
|
82
|
+
private _unexpectedExitCb: OnUnexpectedExit | null = null;
|
|
83
|
+
private _intentionalShutdown = false;
|
|
84
|
+
private _exitedAttached = false;
|
|
85
|
+
private _initialPort: number;
|
|
86
|
+
|
|
87
|
+
constructor(opts: { port: number; worktree: string; logger: Logger }) {
|
|
88
|
+
this._initialPort = opts.port;
|
|
89
|
+
this._worktree = opts.worktree;
|
|
90
|
+
this._logger = opts.logger;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// --- Getters (per interface contract) -----------------------------------
|
|
94
|
+
|
|
95
|
+
get pid(): number | null {
|
|
96
|
+
return this._pid;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
get port(): number | null {
|
|
100
|
+
return this._port;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
get password(): string | null {
|
|
104
|
+
return this._password;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
get baseUrl(): string {
|
|
108
|
+
if (this._port === null) {
|
|
109
|
+
throw new Error("serve.ts: baseUrl accessed before start()");
|
|
110
|
+
}
|
|
111
|
+
return `http://127.0.0.1:${this._port}`;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
get worktree(): string {
|
|
115
|
+
return this._worktree;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// --- Startup ------------------------------------------------------------
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Spawn the serve child and wait for it to be ready.
|
|
122
|
+
*
|
|
123
|
+
* Sequence (spec §5.1):
|
|
124
|
+
* 1. Generate 32-byte secret (NEW-H9: `Buffer.from(...).toString("base64")`).
|
|
125
|
+
* 2. `Bun.spawn(["opencode", "serve", "--port", String(port), "--hostname", "127.0.0.1"], …)`.
|
|
126
|
+
* 3. Read stdout line-by-line until "opencode server listening on http://127.0.0.1:<port>".
|
|
127
|
+
* Parse the bound port (the OS may have remapped 0 → <random>).
|
|
128
|
+
* 4. Health-check: `GET /health` with 100ms interval, 5s timeout.
|
|
129
|
+
* 5. Attach `proc.exited` for crash recovery.
|
|
130
|
+
* 6. Return `{ pid, port, password }`.
|
|
131
|
+
*
|
|
132
|
+
* Errors:
|
|
133
|
+
* - ENOENT (opencode not on PATH) → log, set `pid = null`, throw.
|
|
134
|
+
* - EACCES → same.
|
|
135
|
+
* - Listening line not seen in 5s → log, set `pid = null`, throw.
|
|
136
|
+
* - Health check fails in 5s → log, kill child, throw.
|
|
137
|
+
*/
|
|
138
|
+
async start(): Promise<ServeInfo> {
|
|
139
|
+
if (this._proc !== null) {
|
|
140
|
+
throw new Error("serve.ts: start() called twice without stop()");
|
|
141
|
+
}
|
|
142
|
+
const initialPort = this._initialPort;
|
|
143
|
+
const password = generatePassword();
|
|
144
|
+
|
|
145
|
+
const skipPerms = process.env.BIZAR_BACKGROUND_SKIP_PERMISSIONS === "1";
|
|
146
|
+
const args = [
|
|
147
|
+
"opencode",
|
|
148
|
+
"serve",
|
|
149
|
+
"--port",
|
|
150
|
+
String(initialPort),
|
|
151
|
+
"--hostname",
|
|
152
|
+
"127.0.0.1",
|
|
153
|
+
];
|
|
154
|
+
if (skipPerms) args.push("--dangerously-skip-permissions");
|
|
155
|
+
|
|
156
|
+
let proc: Subprocess;
|
|
157
|
+
try {
|
|
158
|
+
proc = Bun.spawn(args, {
|
|
159
|
+
stdout: "pipe",
|
|
160
|
+
stderr: "pipe",
|
|
161
|
+
env: { ...process.env, OPENCODE_SERVER_PASSWORD: password },
|
|
162
|
+
});
|
|
163
|
+
} catch (err: unknown) {
|
|
164
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
165
|
+
const code = (err as NodeJS.ErrnoException).code;
|
|
166
|
+
if (code === "ENOENT") {
|
|
167
|
+
this._logger.error(
|
|
168
|
+
"bizar: opencode binary not found on PATH; background agents disabled",
|
|
169
|
+
);
|
|
170
|
+
} else if (code === "EACCES") {
|
|
171
|
+
this._logger.error(
|
|
172
|
+
`bizar: cannot execute opencode binary: ${msg}; background agents disabled`,
|
|
173
|
+
);
|
|
174
|
+
} else {
|
|
175
|
+
this._logger.error(`bizar: failed to spawn opencode serve: ${msg}`);
|
|
176
|
+
}
|
|
177
|
+
this._pid = null;
|
|
178
|
+
throw err;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
this._proc = proc;
|
|
182
|
+
this._password = password;
|
|
183
|
+
this._pid = proc.pid;
|
|
184
|
+
this._intentionalShutdown = false;
|
|
185
|
+
|
|
186
|
+
// Wait for the listening line on stdout.
|
|
187
|
+
let boundPort: number;
|
|
188
|
+
try {
|
|
189
|
+
boundPort = await waitForListeningLine(proc, this._logger, 5_000);
|
|
190
|
+
} catch (err: unknown) {
|
|
191
|
+
this._logger.error(
|
|
192
|
+
`bizar: opencode serve did not announce listening line: ${
|
|
193
|
+
err instanceof Error ? err.message : String(err)
|
|
194
|
+
}`,
|
|
195
|
+
);
|
|
196
|
+
this.cleanupOnStartFailure();
|
|
197
|
+
throw err;
|
|
198
|
+
}
|
|
199
|
+
this._port = boundPort;
|
|
200
|
+
|
|
201
|
+
// Health check loop.
|
|
202
|
+
const healthy = await pollHealthCheck(boundPort, password, 5_000);
|
|
203
|
+
if (!healthy) {
|
|
204
|
+
this._logger.error("bizar: opencode serve did not pass health check in 5s");
|
|
205
|
+
this.cleanupOnStartFailure();
|
|
206
|
+
throw new Error("opencode serve failed health check");
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
this.attachExitHandler();
|
|
210
|
+
this._logger.info(
|
|
211
|
+
`bizar: opencode serve ready on http://127.0.0.1:${boundPort} (pid=${proc.pid})`,
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
return { pid: proc.pid, port: boundPort, password };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// --- Stop ---------------------------------------------------------------
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Graceful stop: SIGTERM, wait up to 5s, then SIGKILL. Idempotent.
|
|
221
|
+
*/
|
|
222
|
+
async stop(): Promise<void> {
|
|
223
|
+
const proc = this._proc;
|
|
224
|
+
if (proc === null) return;
|
|
225
|
+
this._intentionalShutdown = true;
|
|
226
|
+
try {
|
|
227
|
+
proc.kill("SIGTERM");
|
|
228
|
+
} catch {
|
|
229
|
+
// already dead
|
|
230
|
+
}
|
|
231
|
+
try {
|
|
232
|
+
await withTimeout(proc.exited, 5_000);
|
|
233
|
+
} catch {
|
|
234
|
+
try {
|
|
235
|
+
proc.kill("SIGKILL");
|
|
236
|
+
} catch {
|
|
237
|
+
// ignore
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
this._proc = null;
|
|
241
|
+
this._pid = null;
|
|
242
|
+
this._port = null;
|
|
243
|
+
this._password = null;
|
|
244
|
+
this._exitedAttached = false;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// --- Health check -------------------------------------------------------
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Single `GET /health` call with a short timeout. Returns the boolean.
|
|
251
|
+
* Uses the global `fetch` directly (not `HttpClient`) to keep this
|
|
252
|
+
* method usable before `HttpClient` is constructed.
|
|
253
|
+
*/
|
|
254
|
+
async healthCheck(): Promise<boolean> {
|
|
255
|
+
if (this._port === null || this._password === null) return false;
|
|
256
|
+
return await pollHealthCheck(this._port, this._password, 2_000);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// --- Crash recovery -----------------------------------------------------
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Register a callback for unexpected exits. The callback is invoked
|
|
263
|
+
* exactly once per unexpected exit. The plugin uses this to mark
|
|
264
|
+
* all in-memory instances `failed` and clear the in-memory map.
|
|
265
|
+
*/
|
|
266
|
+
onUnexpectedExit(cb: OnUnexpectedExit): void {
|
|
267
|
+
this._unexpectedExitCb = cb;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Try to restart the serve child with exponential backoff
|
|
272
|
+
* (250ms, 500ms, 1s — max 3 retries). Returns the new `ServeInfo` or
|
|
273
|
+
* throws if all retries fail.
|
|
274
|
+
*/
|
|
275
|
+
async tryRestart(): Promise<ServeInfo> {
|
|
276
|
+
const delays = [250, 500, 1000];
|
|
277
|
+
let lastErr: unknown = null;
|
|
278
|
+
for (let i = 0; i < delays.length; i++) {
|
|
279
|
+
const delay = delays[i];
|
|
280
|
+
if (delay === undefined) continue;
|
|
281
|
+
await sleep(delay);
|
|
282
|
+
this._logger.warn(`bizar: retrying serve start (attempt ${i + 1}/3)…`);
|
|
283
|
+
// Reset internal state so start() is happy.
|
|
284
|
+
this._proc = null;
|
|
285
|
+
this._pid = null;
|
|
286
|
+
this._port = this._port ?? 0;
|
|
287
|
+
try {
|
|
288
|
+
return await this.start();
|
|
289
|
+
} catch (err: unknown) {
|
|
290
|
+
lastErr = err;
|
|
291
|
+
this._logger.warn(
|
|
292
|
+
`bizar: serve restart attempt ${i + 1}/3 failed: ${
|
|
293
|
+
err instanceof Error ? err.message : String(err)
|
|
294
|
+
}`,
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
throw lastErr instanceof Error
|
|
299
|
+
? lastErr
|
|
300
|
+
: new Error(`serve restart failed: ${String(lastErr)}`);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// --- Internal -----------------------------------------------------------
|
|
304
|
+
|
|
305
|
+
private attachExitHandler(): void {
|
|
306
|
+
const proc = this._proc;
|
|
307
|
+
if (proc === null || this._exitedAttached) return;
|
|
308
|
+
this._exitedAttached = true;
|
|
309
|
+
proc.exited
|
|
310
|
+
.then((exitCode) => {
|
|
311
|
+
if (this._intentionalShutdown) return;
|
|
312
|
+
this._pid = null;
|
|
313
|
+
this._port = null;
|
|
314
|
+
this._password = null;
|
|
315
|
+
this._proc = null;
|
|
316
|
+
this._exitedAttached = false;
|
|
317
|
+
const cb = this._unexpectedExitCb;
|
|
318
|
+
if (cb) {
|
|
319
|
+
try {
|
|
320
|
+
cb(exitCode);
|
|
321
|
+
} catch {
|
|
322
|
+
// callbacks must never throw across the boundary
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
})
|
|
326
|
+
.catch(() => {
|
|
327
|
+
// proc.exited only rejects if the proc was already awaited or
|
|
328
|
+
// never spawned; safe to ignore.
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
private cleanupOnStartFailure(): void {
|
|
333
|
+
const proc = this._proc;
|
|
334
|
+
if (proc !== null) {
|
|
335
|
+
try {
|
|
336
|
+
proc.kill("SIGKILL");
|
|
337
|
+
} catch {
|
|
338
|
+
// ignore
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
this._proc = null;
|
|
342
|
+
this._pid = null;
|
|
343
|
+
this._port = null;
|
|
344
|
+
this._password = null;
|
|
345
|
+
this._exitedAttached = false;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// --- Helpers --------------------------------------------------------------
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Generate a 32-byte secret, base64-encoded (NEW-H9).
|
|
353
|
+
*/
|
|
354
|
+
function generatePassword(): string {
|
|
355
|
+
const bytes = new Uint8Array(32);
|
|
356
|
+
getRandomValues(bytes);
|
|
357
|
+
return Buffer.from(bytes).toString("base64");
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Read stdout from `proc` line-by-line and resolve with the bound port
|
|
362
|
+
* when we see the "opencode server listening on http://127.0.0.1:<port>"
|
|
363
|
+
* line. The actual port may differ from the requested port (especially
|
|
364
|
+
* when port=0 for OS-assigned).
|
|
365
|
+
*
|
|
366
|
+
* Throws on timeout. The reader is released so the `ReadableStream` is
|
|
367
|
+
* cleaned up.
|
|
368
|
+
*/
|
|
369
|
+
async function waitForListeningLine(
|
|
370
|
+
proc: Subprocess,
|
|
371
|
+
logger: Logger,
|
|
372
|
+
timeoutMs: number,
|
|
373
|
+
): Promise<number> {
|
|
374
|
+
// `stdout` is `ReadableStream<Uint8Array>` when stdout is "pipe". We
|
|
375
|
+
// assert the type because Bun's type inference for the generic param
|
|
376
|
+
// is not always narrow enough.
|
|
377
|
+
const stdout = proc.stdout as ReadableStream<Uint8Array> | number | null | undefined;
|
|
378
|
+
if (stdout === null || stdout === undefined) {
|
|
379
|
+
throw new Error("opencode serve: no stdout pipe");
|
|
380
|
+
}
|
|
381
|
+
if (typeof stdout === "number") {
|
|
382
|
+
throw new Error(`opencode serve: stdout is fd=${stdout}; expected a stream`);
|
|
383
|
+
}
|
|
384
|
+
const reader = stdout.getReader();
|
|
385
|
+
const decoder = new TextDecoder("utf-8");
|
|
386
|
+
let buffer = "";
|
|
387
|
+
const portRegex = /opencode server listening on http:\/\/127\.0\.0\.1:(\d+)/;
|
|
388
|
+
try {
|
|
389
|
+
const deadline = Date.now() + timeoutMs;
|
|
390
|
+
while (true) {
|
|
391
|
+
const remaining = deadline - Date.now();
|
|
392
|
+
if (remaining <= 0) {
|
|
393
|
+
throw new Error(`listening line not seen within ${timeoutMs}ms`);
|
|
394
|
+
}
|
|
395
|
+
const readResult = await Promise.race([
|
|
396
|
+
reader.read(),
|
|
397
|
+
sleep(remaining).then(() => ({
|
|
398
|
+
done: true as const,
|
|
399
|
+
value: undefined as unknown as Uint8Array,
|
|
400
|
+
})),
|
|
401
|
+
]);
|
|
402
|
+
if (readResult.done) {
|
|
403
|
+
// Stream ended or timeout fired; check buffer one last time.
|
|
404
|
+
const m = buffer.match(portRegex);
|
|
405
|
+
if (m && m[1]) {
|
|
406
|
+
const port = parseInt(m[1], 10);
|
|
407
|
+
return port;
|
|
408
|
+
}
|
|
409
|
+
throw new Error(
|
|
410
|
+
`opencode serve exited before listening line. Last stdout: ${buffer.slice(-200)}`,
|
|
411
|
+
);
|
|
412
|
+
}
|
|
413
|
+
const chunk = decoder.decode(readResult.value as Uint8Array, { stream: true });
|
|
414
|
+
buffer += chunk;
|
|
415
|
+
const m = buffer.match(portRegex);
|
|
416
|
+
if (m && m[1]) {
|
|
417
|
+
const port = parseInt(m[1], 10);
|
|
418
|
+
logger.debug(`bizar: serve listening line observed (port=${port})`);
|
|
419
|
+
return port;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
} finally {
|
|
423
|
+
try {
|
|
424
|
+
reader.releaseLock();
|
|
425
|
+
} catch {
|
|
426
|
+
// ignore
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Poll `GET /health` with 100ms interval and `totalMs` total timeout.
|
|
433
|
+
* Returns true on first 2xx; false on timeout.
|
|
434
|
+
*/
|
|
435
|
+
async function pollHealthCheck(
|
|
436
|
+
port: number,
|
|
437
|
+
password: string,
|
|
438
|
+
totalMs: number,
|
|
439
|
+
): Promise<boolean> {
|
|
440
|
+
const deadline = Date.now() + totalMs;
|
|
441
|
+
const authHeader = `Basic ${btoa(`opencode:${password}`)}`;
|
|
442
|
+
while (Date.now() < deadline) {
|
|
443
|
+
const remaining = Math.max(50, deadline - Date.now());
|
|
444
|
+
const ac = new AbortController();
|
|
445
|
+
const timer = setTimeout(() => ac.abort(), remaining);
|
|
446
|
+
try {
|
|
447
|
+
const response = await fetch(`http://127.0.0.1:${port}/health`, {
|
|
448
|
+
method: "GET",
|
|
449
|
+
headers: { Authorization: authHeader },
|
|
450
|
+
signal: ac.signal,
|
|
451
|
+
});
|
|
452
|
+
if (response.ok) {
|
|
453
|
+
return true;
|
|
454
|
+
}
|
|
455
|
+
} catch {
|
|
456
|
+
// ignore — keep polling
|
|
457
|
+
} finally {
|
|
458
|
+
clearTimeout(timer);
|
|
459
|
+
}
|
|
460
|
+
await sleep(100);
|
|
461
|
+
}
|
|
462
|
+
return false;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
function sleep(ms: number): Promise<void> {
|
|
466
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
async function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> {
|
|
470
|
+
return await Promise.race([
|
|
471
|
+
promise,
|
|
472
|
+
sleep(ms).then(() => {
|
|
473
|
+
throw new Error(`timed out after ${ms}ms`);
|
|
474
|
+
}),
|
|
475
|
+
]);
|
|
476
|
+
}
|