GeezerVison is showing github.com — fix(client): surface managed startup stderr (#41793) · anomalyco/opencode@9322f5d
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/client/src/effect/service.ts +9-28Lines changed: 9 additions & 28 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { ServiceStatus } from "@opencode-ai/protocol/groups/health" | 2 | 2 | import { Effect, FileSystem, Option, Schedule, Schema } from "effect" | 3 | | -import { spawn, type ChildProcess } from "node:child_process" | 4 | 3 | import { homedir } from "node:os" | 5 | 4 | import { join } from "node:path" | 6 | 5 | import type { DiscoverOptions, Endpoint, EnsureOptions, StopOptions } from "../service.js" | | 6 | +import { | | 7 | + contenderFailure, | | 8 | + contenderFinished, | | 9 | + type ServiceContender, | | 10 | + spawnServiceContender, | | 11 | +} from "../service-contender.js" | 7 | 12 | import { defaultEnsureTiming, ensureTiming, type EnsureTiming } from "../service-timing.js" | 8 | 13 |
| 9 | 14 | export * from "../service.js" | @@ -18,11 +23,6 @@ export type Info = import("../service.js").Info | 18 | 23 | // is all a client needs to connect. The daemon's own configuration (port, | 19 | 24 | // persisted password) is CLI-owned and never read here. | 20 | 25 |
| 21 | | -type Contender = { | 22 | | - readonly child: ChildProcess | 23 | | - readonly error: () => Error | undefined | 24 | | -} | 25 | | - | 26 | 26 | // Read-only lookup: registration file plus health check and version gate. | 27 | 27 | // Never spawns; escalation to ensure() is the caller's policy. | 28 | 28 | /** Discover a healthy, compatible local service without starting one. */ | @@ -54,7 +54,7 @@ const discoverLocal = Effect.fnUntraced(function* (options: DiscoverOptions) { | 54 | 54 | /** Ensure a healthy, compatible local service is running. */ | 55 | 55 | export const ensure = Effect.fn("service.ensure")(function* (options: EnsureOptions = {}) { | 56 | 56 | const timing = ensureTiming(options) | 57 | | - const contenders = new Set<Contender>() | | 57 | + const contenders = new Set<ServiceContender>() | 58 | 58 | let timeouts: { readonly info: Info; readonly count: number } | undefined | 59 | 59 | let announced = false | 60 | 60 | let lastSpawn = 0 | @@ -70,13 +70,7 @@ export const ensure = Effect.fn("service.ensure")(function* (options: EnsureOpti | 70 | 70 | if (command === undefined) return yield* Effect.fail(new Error("Missing service command")) | 71 | 71 | return yield* Effect.try({ | 72 | 72 | try: () => { | 73 | | - const child = spawn(command, args, { detached: true, stdio: "ignore" }) | 74 | | - let error: Error | undefined | 75 | | - child.once("error", (cause) => { | 76 | | - error = new Error("Failed to start server", { cause }) | 77 | | - }) | 78 | | - child.unref() | 79 | | - return { child, error: () => error } | | 73 | + return spawnServiceContender(command, args) | 80 | 74 | }, | 81 | 75 | catch: (cause) => new Error("Failed to start server", { cause }), | 82 | 76 | }) | @@ -129,26 +123,13 @@ export const ensure = Effect.fn("service.ensure")(function* (options: EnsureOpti | 129 | 123 | until: Option.isSome, | 130 | 124 | schedule: Schedule.max([Schedule.spaced(timing.pollInterval), Schedule.recurs(timing.attempts)]), | 131 | 125 | }), | | 126 | + Effect.ensuring(Effect.sync(() => contenders.forEach((contender) => contender.release()))), | 132 | 127 | ) | 133 | 128 | if (Option.isNone(found)) | 134 | 129 | return yield* Effect.fail(new Error("Timed out waiting for the background service to start")) | 135 | 130 | return found.value.endpoint | 136 | 131 | }) | 137 | 132 |
| 138 | | -function contenderFailure(contender: Contender) { | 139 | | - const error = contender.error() | 140 | | - if (error !== undefined) return error | 141 | | - if (contender.child.exitCode !== null && contender.child.exitCode !== 0) | 142 | | - return new Error(`Server process exited with code ${contender.child.exitCode}`) | 143 | | - if (contender.child.signalCode !== null) | 144 | | - return new Error(`Server process terminated by ${contender.child.signalCode}`) | 145 | | - return undefined | 146 | | -} | 147 | | - | 148 | | -function contenderFinished(contender: Contender) { | 149 | | - return contender.error() !== undefined || contender.child.exitCode !== null || contender.child.signalCode !== null | 150 | | -} | 151 | | - | 152 | 133 | /** Stop the registered local service. */ | 153 | 134 | export const stop = Effect.fn("service.stop")(function* (options: StopOptions = {}) { | 154 | 135 | const existing = yield* find(options) | |
Collapse file Copy file name to clipboardExpand all lines: packages/client/src/promise/service.ts +53-69Lines changed: 53 additions & 69 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { readFile } from "node:fs/promises" | 2 | | -import { spawn, type ChildProcess } from "node:child_process" | 3 | 2 | import { homedir } from "node:os" | 4 | 3 | import { join } from "node:path" | 5 | 4 | import type { DiscoverOptions, Endpoint, Info, EnsureOptions, StopOptions } from "../service.js" | | 5 | +import { | | 6 | + contenderFailure, | | 7 | + contenderFinished, | | 8 | + type ServiceContender, | | 9 | + spawnServiceContender, | | 10 | +} from "../service-contender.js" | 6 | 11 | import { defaultEnsureTiming, ensureTiming, type EnsureTiming } from "../service-timing.js" | 7 | 12 | import type { ServiceHealth, ServiceStopResponse } from "./generated/types.js" | 8 | 13 |
| @@ -14,11 +19,6 @@ export * from "../service.js" | 14 | 19 | // intentionally implemented with Node APIs so Promise clients do not need | 15 | 20 | // Effect or @effect/platform-node at runtime. | 16 | 21 |
| 17 | | -type Contender = { | 18 | | - readonly child: ChildProcess | 19 | | - readonly error: () => Error | undefined | 20 | | -} | 21 | | - | 22 | 22 | /** Discover a healthy, compatible local service without starting one. */ | 23 | 23 | export async function discover(options: DiscoverOptions = {}) { | 24 | 24 | return (await discoverLocal(options))?.endpoint | @@ -35,7 +35,7 @@ async function discoverLocal(options: DiscoverOptions) { | 35 | 35 | export async function ensure(options: EnsureOptions = {}): Promise<Endpoint> { | 36 | 36 | const timing = ensureTiming(options) | 37 | 37 | const deadline = Date.now() + timing.promiseTimeout | 38 | | - const contenders = new Set<Contender>() | | 38 | + const contenders = new Set<ServiceContender>() | 39 | 39 | let timeouts: { readonly info: Info; readonly count: number } | undefined | 40 | 40 | let announced = false | 41 | 41 | let lastSpawn = 0 | @@ -50,79 +50,63 @@ export async function ensure(options: EnsureOptions = {}): Promise<Endpoint> { | 50 | 50 | const [command, ...args] = options.command ?? ["opencode", "serve", "--service"] | 51 | 51 | if (command === undefined) throw new Error("Missing service command") | 52 | 52 | try { | 53 | | - const child = spawn(command, args, { detached: true, stdio: "ignore" }) | 54 | | - let error: Error | undefined | 55 | | - child.once("error", (cause) => { | 56 | | - error = new Error("Failed to start server", { cause }) | 57 | | - }) | 58 | | - child.unref() | 59 | | - return { child, error: () => error } | | 53 | + return spawnServiceContender(command, args) | 60 | 54 | } catch (cause) { | 61 | 55 | throw new Error("Failed to start server", { cause }) | 62 | 56 | } | 63 | 57 | } | 64 | 58 |
| 65 | | - while (true) { | 66 | | - if (Date.now() >= deadline) throw new Error("Timed out waiting for the background service to start") | 67 | | - const registration = await registered(options.file, true, timing.requestTimeout) | 68 | | - if (registration.timedOut && registration.info !== undefined) { | 69 | | - timeouts = { | 70 | | - info: registration.info, | 71 | | - count: timeouts !== undefined && same(timeouts.info, registration.info) ? timeouts.count + 1 : 1, | 72 | | - } | 73 | | - if (timeouts.count >= 3) { | 74 | | - announce("missing") | 75 | | - await evict(registration.info, options, timing) | 76 | | - timeouts = undefined | 77 | | - lastSpawn = Date.now() - spawnDelay | 78 | | - } | 79 | | - } else timeouts = undefined | | 59 | + try { | | 60 | + while (true) { | | 61 | + if (Date.now() >= deadline) throw new Error("Timed out waiting for the background service to start") | | 62 | + const registration = await registered(options.file, true, timing.requestTimeout) | | 63 | + if (registration.timedOut && registration.info !== undefined) { | | 64 | + timeouts = { | | 65 | + info: registration.info, | | 66 | + count: timeouts !== undefined && same(timeouts.info, registration.info) ? timeouts.count + 1 : 1, | | 67 | + } | | 68 | + if (timeouts.count >= 3) { | | 69 | + announce("missing") | | 70 | + await evict(registration.info, options, timing) | | 71 | + timeouts = undefined | | 72 | + lastSpawn = Date.now() - spawnDelay | | 73 | + } | | 74 | + } else timeouts = undefined | 80 | 75 |
| 81 | | - if (registration.service !== undefined) { | 82 | | - spawnDelay = timing.spawnDelay | 83 | | - const service = registration.service | 84 | | - const compatible = !service.legacy && (options.version === undefined || service.version === options.version) | 85 | | - if (compatible && service.state === "ready") return service.endpoint | 86 | | - if (compatible && service.state === "failed") throw new Error("Background service failed to start") | 87 | | - if (!compatible) { | 88 | | - announce("version-mismatch", service.version) | 89 | | - await kill(service, options, timing).catch(() => undefined) | 90 | | - lastSpawn = 0 | 91 | | - } | 92 | | - } else { | 93 | | - if (lastSpawn === 0 && registration.info !== undefined) lastSpawn = Date.now() | 94 | | - const finished = [...contenders].filter(contenderFinished) | 95 | | - const failure = finished.map(contenderFailure).find((error) => error !== undefined) | 96 | | - if (finished.some((item) => item.child.exitCode === 0)) { | 97 | | - spawnDelay = Math.min(spawnDelay * 2, timing.maxSpawnDelay) | 98 | | - } | 99 | | - finished.forEach((item) => contenders.delete(item)) | 100 | | - if (failure !== undefined && contenders.size === 0) throw failure | 101 | | - // Keep one candidate plus one lock probe so a pre-lock stall cannot block recovery. | 102 | | - if (contenders.size < 2 && Date.now() - lastSpawn >= spawnDelay) { | 103 | | - announce("missing") | 104 | | - contenders.add(spawnContender()) | 105 | | - lastSpawn = Date.now() | | 76 | + if (registration.service !== undefined) { | | 77 | + spawnDelay = timing.spawnDelay | | 78 | + const service = registration.service | | 79 | + const compatible = !service.legacy && (options.version === undefined || service.version === options.version) | | 80 | + if (compatible && service.state === "ready") return service.endpoint | | 81 | + if (compatible && service.state === "failed") throw new Error("Background service failed to start") | | 82 | + if (!compatible) { | | 83 | + announce("version-mismatch", service.version) | | 84 | + await kill(service, options, timing).catch(() => undefined) | | 85 | + lastSpawn = 0 | | 86 | + } | | 87 | + } else { | | 88 | + if (lastSpawn === 0 && registration.info !== undefined) lastSpawn = Date.now() | | 89 | + const finished = [...contenders].filter(contenderFinished) | | 90 | + const failure = finished.map(contenderFailure).find((error) => error !== undefined) | | 91 | + if (finished.some((item) => item.child.exitCode === 0)) { | | 92 | + spawnDelay = Math.min(spawnDelay * 2, timing.maxSpawnDelay) | | 93 | + } | | 94 | + finished.forEach((item) => contenders.delete(item)) | | 95 | + if (failure !== undefined && contenders.size === 0) throw failure | | 96 | + // Keep one candidate plus one lock probe so a pre-lock stall cannot block recovery. | | 97 | + if (contenders.size < 2 && Date.now() - lastSpawn >= spawnDelay) { | | 98 | + announce("missing") | | 99 | + contenders.add(spawnContender()) | | 100 | + lastSpawn = Date.now() | | 101 | + } | 106 | 102 | } | | 103 | + await delay(timing.pollInterval) | 107 | 104 | } | 108 | | - await delay(timing.pollInterval) | | 105 | + } finally { | | 106 | + contenders.forEach((contender) => contender.release()) | 109 | 107 | } | 110 | 108 | } | 111 | 109 |
| 112 | | -function contenderFailure(contender: Contender) { | 113 | | - const error = contender.error() | 114 | | - if (error !== undefined) return error | 115 | | - if (contender.child.exitCode !== null && contender.child.exitCode !== 0) | 116 | | - return new Error(`Server process exited with code ${contender.child.exitCode}`) | 117 | | - if (contender.child.signalCode !== null) | 118 | | - return new Error(`Server process terminated by ${contender.child.signalCode}`) | 119 | | - return undefined | 120 | | -} | 121 | | - | 122 | | -function contenderFinished(contender: Contender) { | 123 | | - return contender.error() !== undefined || contender.child.exitCode !== null || contender.child.signalCode !== null | 124 | | -} | 125 | | - | 126 | 110 | /** Stop the registered local service. */ | 127 | 111 | export async function stop(options: StopOptions = {}) { | 128 | 112 | const existing = await find(options) | |
Collapse file Copy file name to clipboard +63Lines changed: 63 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { spawn, type ChildProcess } from "node:child_process" | | 2 | + | | 3 | +export type ServiceContender = { | | 4 | + readonly child: ChildProcess | | 5 | + readonly error: () => Error | undefined | | 6 | + readonly closed: () => boolean | | 7 | + readonly stderr: () => string | | 8 | + readonly release: () => void | | 9 | +} | | 10 | + | | 11 | +const stderrLimit = 8 * 1024 | | 12 | + | | 13 | +export function spawnServiceContender(command: string, args: ReadonlyArray<string>): ServiceContender { | | 14 | + const child = spawn(command, args, { detached: true, stdio: ["ignore", "ignore", "pipe"] }) | | 15 | + let error: Error | undefined | | 16 | + let closed = false | | 17 | + let stderr = Buffer.alloc(0) | | 18 | + const onStderr = (chunk: Buffer) => { | | 19 | + const tail = chunk.subarray(-stderrLimit) | | 20 | + stderr = | | 21 | + tail.length === stderrLimit | | 22 | + ? Buffer.from(tail) | | 23 | + : Buffer.concat([stderr.subarray(-(stderrLimit - tail.length)), tail]) | | 24 | + } | | 25 | + child.stderr?.on("data", onStderr) | | 26 | + if (child.stderr !== null && "unref" in child.stderr && typeof child.stderr.unref === "function") child.stderr.unref() | | 27 | + child.once("error", (cause) => { | | 28 | + error = new Error("Failed to start server", { cause }) | | 29 | + }) | | 30 | + child.once("close", () => { | | 31 | + closed = true | | 32 | + }) | | 33 | + child.unref() | | 34 | + return { | | 35 | + child, | | 36 | + error: () => error, | | 37 | + closed: () => closed, | | 38 | + stderr: () => stderr.toString("utf8").trim(), | | 39 | + release: () => { | | 40 | + child.stderr?.off("data", onStderr) | | 41 | + child.stderr?.resume() | | 42 | + stderr = Buffer.alloc(0) | | 43 | + }, | | 44 | + } | | 45 | +} | | 46 | + | | 47 | +export function contenderFailure(contender: ServiceContender) { | | 48 | + const error = contender.error() | | 49 | + if (error !== undefined) return error | | 50 | + if (contender.child.exitCode !== null && contender.child.exitCode !== 0) | | 51 | + return startupError(`Server process exited with code ${contender.child.exitCode}`, contender.stderr()) | | 52 | + if (contender.child.signalCode !== null) | | 53 | + return startupError(`Server process terminated by ${contender.child.signalCode}`, contender.stderr()) | | 54 | + return undefined | | 55 | +} | | 56 | + | | 57 | +export function contenderFinished(contender: ServiceContender) { | | 58 | + return contender.error() !== undefined || contender.closed() | | 59 | +} | | 60 | + | | 61 | +function startupError(message: string, stderr: string) { | | 62 | + return new Error(stderr ? `${message}\n${stderr}` : message) | | 63 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/client/test/fixture/service.ts +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,6 +3,10 @@ import { appendFile, rename, writeFile } from "node:fs/promises" | 3 | 3 | const [registration, mode, delay] = process.argv.slice(2) | 4 | 4 | if (registration === undefined || mode === undefined) throw new Error("Missing service fixture arguments") | 5 | 5 | if (mode === "failed") process.exit(1) | | 6 | +if (mode === "stderr-failed") { | | 7 | + process.stderr.write("x".repeat(16_384) + "\nactionable startup failure\n") | | 8 | + process.exit(1) | | 9 | +} | 6 | 10 | if (mode === "record-start") { | 7 | 11 | await writeFile(registration + ".started", "") | 8 | 12 | process.exit(1) | |
Collapse file Copy file name to clipboardExpand all lines: packages/client/test/promise-service.test.ts +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -72,6 +72,21 @@ test("reports a failed registered service", async () => { | 72 | 72 | ) | 73 | 73 | }) | 74 | 74 |
| | 75 | +test("reports a bounded contender stderr tail with native promises", async () => { | | 76 | + const directory = await temp() | | 77 | + const registration = join(directory, "service.json") | | 78 | + const error = await Service.ensure({ | | 79 | + file: registration, | | 80 | + version: "test", | | 81 | + command: [process.execPath, fixture, registration, "stderr-failed"], | | 82 | + }).catch((error: unknown) => error) | | 83 | + | | 84 | + expect(error).toBeInstanceOf(Error) | | 85 | + if (!(error instanceof Error)) throw error | | 86 | + expect(error.message).toContain("actionable startup failure") | | 87 | + expect(error.message.length).toBeLessThan(9_000) | | 88 | +}, 10_000) | | 89 | + | 75 | 90 | test("evicts an unresponsive registered service before starting its replacement", async () => { | 76 | 91 | const directory = await temp() | 77 | 92 | const registration = join(directory, "service.json") | |
Collapse file Copy file name to clipboardExpand all lines: packages/client/test/service.test.ts +17Lines changed: 17 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -201,6 +201,23 @@ test("reports a contender that fails to start", async () => { | 201 | 201 | ).rejects.toThrow("Server process exited with code 1") | 202 | 202 | }) | 203 | 203 |
| | 204 | +test("reports a bounded contender stderr tail", async () => { | | 205 | + const directory = await temp() | | 206 | + const registration = join(directory, "service.json") | | 207 | + const error = await run( | | 208 | + Service.ensure({ | | 209 | + file: registration, | | 210 | + version: "test", | | 211 | + command: [process.execPath, fixture, registration, "stderr-failed"], | | 212 | + }), | | 213 | + ).catch((error: unknown) => error) | | 214 | + | | 215 | + expect(error).toBeInstanceOf(Error) | | 216 | + if (!(error instanceof Error)) throw error | | 217 | + expect(error.message).toContain("actionable startup failure") | | 218 | + expect(error.message.length).toBeLessThan(9_000) | | 219 | +}, 10_000) | | 220 | + | 204 | 221 | test("reports a contender terminated by a signal", async () => { | 205 | 222 | const directory = await temp() | 206 | 223 | const registration = join(directory, "service.json") | |
|