Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions apps/app/scripts/_util.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export async function spawnOpencodeServe({

const child = spawn("opencode", args, {
cwd,
stdio: ["ignore", "ignore", "pipe"],
stdio: ["ignore", "pipe", "pipe"],
env: {
...process.env,
// Make it explicit we're a non-TUI client.
Expand All @@ -69,13 +69,20 @@ export async function spawnOpencodeServe({

const baseUrl = `http://${hostname}:${port}`;

// If the process dies early, surface stderr.
// If the process dies early or never becomes healthy, surface its output so
// CI failures are diagnosable instead of showing an empty stderr.
let stderr = "";
child.stderr.setEncoding("utf8");
child.stderr.on("data", (chunk) => {
stderr += chunk;
});

let stdout = "";
child.stdout.setEncoding("utf8");
child.stdout.on("data", (chunk) => {
stdout += chunk;
});

async function waitForExit(ms) {
return Promise.race([
once(child, "exit").then(() => true),
Expand Down Expand Up @@ -115,16 +122,31 @@ export async function spawnOpencodeServe({
getStderr() {
return stderr;
},
getStdout() {
return stdout;
},
getExitInfo() {
return { exitCode: child.exitCode, signalCode: child.signalCode };
},
};
}

export async function waitForHealthy(client, { timeoutMs = 10_000, pollMs = 250 } = {}) {
export async function waitForHealthy(
client,
{ timeoutMs = 30_000, pollMs = 250, requestTimeoutMs = 5_000 } = {},
) {
const start = Date.now();
let lastError;

while (Date.now() - start < timeoutMs) {
try {
const health = await client.global.health();
// Bound each individual request: a single fetch to a port that is not
// yet accepting connections can otherwise block for the OS-level connect
// timeout (tens of seconds on macOS), blowing past `timeoutMs` because
// the loop can only re-check the deadline between awaits.
const health = await client.global.health({
signal: AbortSignal.timeout(requestTimeoutMs),
});
assert.equal(health.healthy, true);
assert.ok(typeof health.version === "string");
return health;
Expand Down
2 changes: 2 additions & 0 deletions apps/app/scripts/browser-entry.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ try {
results.ok = false;
results.error = message;
results.stderr = opencode?.getStderr?.() ?? "";
results.stdout = opencode?.getStdout?.() ?? "";
results.serverExit = opencode?.getExitInfo?.() ?? null;
console.error(JSON.stringify(results, null, 2));
process.exitCode = 1;
} finally {
Expand Down
2 changes: 2 additions & 0 deletions apps/app/scripts/e2e.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ try {
results.ok = false;
results.error = message;
results.stderr = server.getStderr();
results.stdout = server.getStdout?.() ?? "";
results.serverExit = server.getExitInfo?.() ?? null;
console.error(JSON.stringify(results, null, 2));
process.exitCode = 1;
} finally {
Expand Down
10 changes: 9 additions & 1 deletion apps/app/scripts/fs-engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,15 @@ try {
);
} catch (e) {
const message = e instanceof Error ? e.message : String(e);
console.error(JSON.stringify({ ok: false, error: message, stderr: server.getStderr() }));
console.error(
JSON.stringify({
ok: false,
error: message,
stderr: server.getStderr(),
stdout: server.getStdout?.() ?? "",
serverExit: server.getExitInfo?.() ?? null,
}),
);
process.exitCode = 1;
} finally {
await server.close();
Expand Down
2 changes: 2 additions & 0 deletions apps/app/scripts/session-switch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ try {
results.ok = false;
results.error = message;
results.stderr = server.getStderr();
results.stdout = server.getStdout?.() ?? "";
results.serverExit = server.getExitInfo?.() ?? null;
console.error(JSON.stringify(results, null, 2));
process.exitCode = 1;
} finally {
Expand Down
Loading