Skip to content
This repository was archived by the owner on Nov 3, 2026. It is now read-only.

Commit 90c577c

Browse files
tsenartampagent
andcommitted
Fixed your mess.
opencode SDK race condition: createOpencodeServer resolves before API routes register. Health-gate session.create via /global/health. See github.com/anomalyco/opencode/issues/7060. Amp-Thread-ID: https://ampcode.com/threads/T-019c34e4-6fd2-77ba-914f-46172e3a380c Co-authored-by: Amp <[email protected]>
1 parent 9d7ff75 commit 90c577c

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

‎.meta/harness/opencode.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,32 @@ export const opencodeHarness: HarnessRunner = {
169169
}
170170
})();
171171

172-
// Create session
172+
// Wait for API readiness via /global/health — createOpencode resolves when
173+
// the port is listening, but routes aren't registered yet (known SDK issue,
174+
// see github.com/anomalyco/opencode/issues/7060). Without this, the first
175+
// session.create hits Bun's HTML fallback instead of JSON.
176+
const healthUrl = `${opencode.server.url}/global/health`;
177+
const MAX_READY_ATTEMPTS = 20;
178+
const READY_DELAY_MS = 250;
179+
for (let attempt = 1; attempt <= MAX_READY_ATTEMPTS; attempt++) {
180+
try {
181+
const res = await fetch(healthUrl);
182+
if (res.ok) {
183+
const body = await res.json() as { healthy?: boolean };
184+
if (body.healthy) {
185+
log(`API ready (attempt ${attempt})`);
186+
break;
187+
}
188+
}
189+
} catch {
190+
// fetch throws on connection refused — server not listening yet
191+
}
192+
if (attempt === MAX_READY_ATTEMPTS) {
193+
throw new Error(`Server never became healthy after ${MAX_READY_ATTEMPTS * READY_DELAY_MS}ms`);
194+
}
195+
await new Promise(r => setTimeout(r, READY_DELAY_MS));
196+
}
197+
173198
const sessionRes = await opencode.client.session.create({
174199
body: { title: `eval-${scenario.id}` },
175200
});

0 commit comments

Comments
 (0)