mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 17:01:37 +03:00
fix(client): preserve service on protocol-only reconnect mismatch (#50825)
This commit is contained in:
committed by
GitHub
Unverified
parent
eaf80d99e5
commit
3e77d5c170
@@ -95,7 +95,14 @@ export const ensure = Effect.fn("service.ensure")(function* (options: EnsureOpti
|
||||
} else timeouts = undefined
|
||||
if (service !== undefined) {
|
||||
spawnDelay = timing.spawnDelay
|
||||
const compatible = service.compatible && matchesVersion(service.version, options)
|
||||
const versionMatches = matchesVersion(service.version, options)
|
||||
const compatible = service.compatible && versionMatches
|
||||
if (!service.compatible && versionMatches)
|
||||
return yield* Effect.fail(
|
||||
new Error(
|
||||
"Background service uses an incompatible health protocol. Update this client or explicitly restart the service.",
|
||||
),
|
||||
)
|
||||
if (compatible && service.state === "ready") {
|
||||
yield* Effect.tryPromise(() => PtyHandoff.complete(options.file ?? fallback(), service.info))
|
||||
return Option.some(service)
|
||||
@@ -205,10 +212,7 @@ const probe = Effect.fnUntraced(function* (info: Info) {
|
||||
return (yield* probeResult(info)).service
|
||||
})
|
||||
|
||||
const probeResult = Effect.fnUntraced(function* (
|
||||
info: Info,
|
||||
timeout = defaultEnsureTiming.requestTimeout,
|
||||
) {
|
||||
const probeResult = Effect.fnUntraced(function* (info: Info, timeout = defaultEnsureTiming.requestTimeout) {
|
||||
const endpoint = {
|
||||
url: info.url,
|
||||
auth:
|
||||
@@ -230,8 +234,8 @@ const probeResult = Effect.fnUntraced(function* (
|
||||
)
|
||||
if ("cause" in result) return { service: undefined, timedOut: signal.aborted }
|
||||
const response = result.value.response
|
||||
// The previous V2 service exposes /api/status instead. Its authenticated 404 is enough
|
||||
// to recognize the registered daemon as incompatible and route it through replacement.
|
||||
// A missing health endpoint identifies protocol incompatibility, not an older
|
||||
// version. Only an unmet version requirement lets ensure replace this owner.
|
||||
if (response.status === 404)
|
||||
return {
|
||||
service: {
|
||||
|
||||
@@ -76,7 +76,12 @@ export async function ensure(options: EnsureOptions = {}): Promise<Endpoint> {
|
||||
if (registration.service !== undefined) {
|
||||
spawnDelay = timing.spawnDelay
|
||||
const service = registration.service
|
||||
const compatible = service.compatible && matchesVersion(service.version, options)
|
||||
const versionMatches = matchesVersion(service.version, options)
|
||||
const compatible = service.compatible && versionMatches
|
||||
if (!service.compatible && versionMatches)
|
||||
throw new Error(
|
||||
"Background service uses an incompatible health protocol. Update this client or explicitly restart the service.",
|
||||
)
|
||||
if (compatible && service.state === "ready") {
|
||||
await PtyHandoff.complete(options.file ?? fallback(), service.info)
|
||||
return service.endpoint
|
||||
@@ -177,8 +182,8 @@ async function probeResult(info: Info, timeout = defaultEnsureTiming.requestTime
|
||||
)
|
||||
if ("cause" in result) return { service: undefined, timedOut: signal.aborted }
|
||||
const response = result.value.response
|
||||
// The previous V2 service exposes /api/status instead. Its authenticated 404 is enough
|
||||
// to recognize the registered daemon as incompatible and route it through replacement.
|
||||
// A missing health endpoint identifies protocol incompatibility, not an older
|
||||
// version. Only an unmet version requirement lets ensure replace this owner.
|
||||
if (response.status === 404)
|
||||
return {
|
||||
service: {
|
||||
|
||||
@@ -44,8 +44,12 @@ const handoff = {
|
||||
}
|
||||
const server = Bun.serve({
|
||||
port: 0,
|
||||
async fetch(request) {
|
||||
async fetch(request): Promise<Response> {
|
||||
const pathname = new URL(request.url).pathname
|
||||
if (mode === "protocol" && pathname === "/api/experimental/persistent-pty/handoff") {
|
||||
await writeFile(registration + ".handoff-request", "")
|
||||
return Response.json({ handoff: null })
|
||||
}
|
||||
if (pathname === "/api/experimental/persistent-pty/handoff" && mode === "handoff") {
|
||||
if (request.method !== "POST" || request.headers.get("authorization") !== "Basic " + btoa("opencode:private"))
|
||||
return new Response(null, { status: 401 })
|
||||
@@ -55,6 +59,8 @@ const server = Bun.serve({
|
||||
if (pathname === "/api/experimental/persistent-pty/handoff" && mode === "handoff-broken")
|
||||
return new Response(null, { status: 500 })
|
||||
if (pathname !== "/api/info") return new Response(null, { status: 404 })
|
||||
if (mode === "protocol" && (await Bun.file(registration + ".missing-health").exists()))
|
||||
return new Response(null, { status: 404 })
|
||||
requests += 1
|
||||
if (mode === "starting") await writeFile(registration + ".status-request", "")
|
||||
if (mode === "hanging") {
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
import { NodeFileSystem } from "@effect/platform-node"
|
||||
import { expect, test } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { Service } from "../src/effect/service"
|
||||
import { ensure, stop } from "../src/promise/service"
|
||||
import type { EnsureOptions, EnsureReason, StopOptions } from "../src/service"
|
||||
import { serviceFixture } from "./fixture/service-fixture"
|
||||
import { accelerate } from "./fixture/service-timing"
|
||||
|
||||
const effect = accelerate(Service.ensure)
|
||||
const clients = [
|
||||
{ name: "Promise", ensure: accelerate(ensure), stop },
|
||||
{
|
||||
name: "Effect",
|
||||
ensure: (options: EnsureOptions) => Effect.runPromise(effect(options).pipe(Effect.provide(NodeFileSystem.layer))),
|
||||
stop: (options: StopOptions) => Effect.runPromise(Service.stop(options).pipe(Effect.provide(NodeFileSystem.layer))),
|
||||
},
|
||||
]
|
||||
|
||||
for (const client of clients) {
|
||||
for (const policy of [
|
||||
{ name: "no version requirement", version: undefined },
|
||||
{ name: "matching exact version", version: "test" },
|
||||
{ name: "accepting version predicate", version: (version: string) => version === "test" },
|
||||
]) {
|
||||
test(`${client.name} reconnect preserves a protocol-incompatible owner with ${policy.name}`, async () => {
|
||||
await using fixture = await serviceFixture()
|
||||
const owner = fixture.spawn("protocol")
|
||||
await fixture.waitForFile()
|
||||
const original = await Bun.file(fixture.registration).json()
|
||||
const starts: EnsureReason[] = []
|
||||
const options = {
|
||||
file: fixture.registration,
|
||||
version: policy.version,
|
||||
command: fixture.command("record-start"),
|
||||
onStart: (reason: EnsureReason) => starts.push(reason),
|
||||
}
|
||||
expect((await client.ensure(options)).url).toBe(original.url)
|
||||
|
||||
// The same process remains alive. Only its health endpoint becomes unavailable.
|
||||
await Bun.write(fixture.registration + ".missing-health", "")
|
||||
const error = await client.ensure(options).catch((error: unknown) => error)
|
||||
expect(await Bun.file(fixture.registration + ".signal").exists()).toBe(false)
|
||||
expect(await Bun.file(fixture.registration + ".handoff-request").exists()).toBe(false)
|
||||
expect(await Bun.file(fixture.registration + ".started").exists()).toBe(false)
|
||||
expect(starts).toEqual([])
|
||||
expect(owner.exitCode).toBe(null)
|
||||
expect(await Bun.file(fixture.registration).json()).toEqual(original)
|
||||
expect(error).toBeInstanceOf(Error)
|
||||
if (!(error instanceof Error)) throw error
|
||||
expect(error.message).toContain("incompatible health protocol")
|
||||
|
||||
await Bun.file(fixture.registration + ".missing-health").delete()
|
||||
expect((await client.ensure(options)).url).toBe(original.url)
|
||||
expect(owner.exitCode).toBe(null)
|
||||
})
|
||||
}
|
||||
|
||||
test(`${client.name} can explicitly replace a protocol-incompatible owner with a nonmatching version`, async () => {
|
||||
await using fixture = await serviceFixture()
|
||||
const owner = fixture.spawn("protocol")
|
||||
await fixture.waitForFile()
|
||||
await Bun.write(fixture.registration + ".missing-health", "")
|
||||
const starts: EnsureReason[] = []
|
||||
const endpoint = await client.ensure({
|
||||
file: fixture.registration,
|
||||
version: "2.1.0-next.1",
|
||||
command: fixture.command("compatible"),
|
||||
onStart: (reason) => starts.push(reason),
|
||||
})
|
||||
const replacement = await Bun.file(fixture.registration).json()
|
||||
fixture.track(replacement.pid)
|
||||
|
||||
expect(await owner.exited).toBe(0)
|
||||
expect(await Bun.file(fixture.registration + ".signal").text()).toBe("SIGTERM")
|
||||
expect(await Bun.file(fixture.registration + ".handoff-request").exists()).toBe(true)
|
||||
expect(starts).toEqual(["version-mismatch"])
|
||||
expect(replacement.pid).not.toBe(owner.pid)
|
||||
expect(replacement.version).toBe("2.1.0-next.1")
|
||||
expect(endpoint.url).toBe(replacement.url)
|
||||
})
|
||||
|
||||
test(`${client.name} can explicitly stop a protocol-incompatible owner`, async () => {
|
||||
await using fixture = await serviceFixture()
|
||||
const owner = fixture.spawn("protocol")
|
||||
await fixture.waitForFile()
|
||||
await Bun.write(fixture.registration + ".missing-health", "")
|
||||
|
||||
await client.stop({ file: fixture.registration })
|
||||
expect(await owner.exited).toBe(0)
|
||||
expect(await Bun.file(fixture.registration + ".signal").text()).toBe("SIGTERM")
|
||||
expect(await Bun.file(fixture.registration).exists()).toBe(false)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user