fix(cli): keep mini session waits alive (#50248)

Co-authored-by: Dax Raad <d@ironbay.co>
This commit is contained in:
Andrei Vince
2026-10-19 13:13:58 -04:00
committed by GitHub
Unverified
parent 86ac38ad56
commit b56e25379b
3 changed files with 74 additions and 0 deletions
+3
View File
@@ -140,6 +140,9 @@ export function createMiniConnection(input: MiniCommandInput["server"]) {
OpenCode.make({
baseUrl: endpoint.url,
headers: Service.headers(endpoint),
// Session waits can remain idle until background work finishes.
fetch: ((request: RequestInfo | URL, init?: RequestInit) =>
fetch(request, { ...init, timeout: false } as BunFetchRequestInit)) as typeof fetch,
})
const reconnect = input.reconnect
return {
+24
View File
@@ -0,0 +1,24 @@
import assert from "node:assert/strict"
import { ClientError, OpenCode } from "@opencode/client/promise"
import { createMiniConnection } from "../../src/mini"
const endpoint = { url: process.argv[2]! }
const connection = createMiniConnection({ endpoint, reconnect: async () => endpoint })
const replacement = await connection.reconnect?.(new AbortController().signal)
if (!replacement) throw new Error("Expected a replacement client")
const timeout = (error: unknown) =>
error instanceof ClientError &&
error.reason === "Transport" &&
error.cause instanceof Error &&
error.cause.name === "TimeoutError"
await Promise.all([
connection.sdk.session.wait({ sessionID: "ses_initial" }),
replacement.session.wait({ sessionID: "ses_reconnected" }),
assert.rejects(OpenCode.make({ baseUrl: endpoint.url }).session.wait({ sessionID: "ses_control" }), timeout),
assert.rejects(
connection.sdk.session.wait({ sessionID: "ses_cancelled" }, { signal: AbortSignal.timeout(100) }),
timeout,
),
])
+47
View File
@@ -0,0 +1,47 @@
import { expect, test } from "bun:test"
import path from "node:path"
import { isolatedEnv } from "./fixture/environment"
import { tmpdir } from "./fixture/tmpdir"
test("mini waits beyond the fetch idle timeout before and after reconnecting", async () => {
await using directory = await tmpdir()
const requests: string[] = []
const server = Bun.serve({
hostname: "127.0.0.1",
port: 0,
idleTimeout: 0,
async fetch(request) {
requests.push(new URL(request.url).pathname)
// Bun's socket timer sweeps every four seconds, so allow more than two sweeps.
await Bun.sleep(12_000)
return new Response(null, { status: 204 })
},
})
const child = Bun.spawn(
[process.execPath, path.join(import.meta.dir, "fixture/mini-wait.ts"), server.url.toString()],
{
cwd: path.join(import.meta.dir, ".."),
env: isolatedEnv(directory.path, { BUN_CONFIG_HTTP_IDLE_TIMEOUT: "1" }),
signal: AbortSignal.timeout(25_000),
stdout: "pipe",
stderr: "pipe",
},
)
try {
const [stdout, stderr, code] = await Promise.all([
new Response(child.stdout).text(),
new Response(child.stderr).text(),
child.exited,
])
expect(stderr).not.toContain("Transport: The operation timed out")
expect(stderr).not.toContain("TimeoutError")
expect({ stdout, code }).toEqual({ stdout: "", code: 0 })
expect(requests.filter((path) => path !== "/api/experimental/session/ses_cancelled/wait").toSorted()).toEqual(
["ses_initial", "ses_reconnected", "ses_control"].map((id) => `/api/experimental/session/${id}/wait`).sort(),
)
} finally {
child.kill()
await child.exited
await server.stop(true)
}
}, 30_000)