Skip to content

Commit 82f7134

Browse files
committed
fix(client): replace servers missing status endpoint
1 parent 763cac8 commit 82f7134

2 files changed

Lines changed: 58 additions & 12 deletions

File tree

‎packages/client/src/effect/service.ts‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { ServerStatus } from "@opencode/protocol/groups/server"
21
import { Effect, FileSystem, Option, Schedule, Schema } from "effect"
32
import { homedir } from "node:os"
43
import { join } from "node:path"
@@ -31,6 +30,7 @@ export type Info = import("../service.js").Info
3130
export const discover = Effect.fn("service.discover")(function* (options: DiscoverOptions = {}) {
3231
const found = (yield* registered(options.file)).service
3332
if (found?.state !== "ready") return undefined
33+
if (!found.compatible) return undefined
3434
if (!matchesVersion(found.version, options)) return undefined
3535
return found.endpoint
3636
})
@@ -42,6 +42,7 @@ export const incumbent = Effect.fn("service.incumbent")(function* (
4242
const info = yield* read(options.file)
4343
const found = info === undefined ? undefined : yield* probe({ ...info, url: options.url })
4444
if (found === undefined) return undefined
45+
if (!found.compatible) return undefined
4546
if (!matchesVersion(found.version, options)) return undefined
4647
return { endpoint: found.endpoint, state: found.state }
4748
})
@@ -94,7 +95,7 @@ export const ensure = Effect.fn("service.ensure")(function* (options: EnsureOpti
9495
} else timeouts = undefined
9596
if (service !== undefined) {
9697
spawnDelay = timing.spawnDelay
97-
const compatible = matchesVersion(service.version, options)
98+
const compatible = service.compatible && matchesVersion(service.version, options)
9899
if (compatible && service.state === "ready") {
99100
yield* Effect.tryPromise(() => PtyHandoff.complete(options.file ?? fallback(), service.info))
100101
return Option.some(service)
@@ -171,7 +172,12 @@ export const Info = Schema.Struct({
171172
})
172173

173174
const decode = Schema.decodeUnknownEffect(Schema.fromJsonString(Info))
174-
const decodeStatus = Schema.decodeUnknownOption(ServerStatus)
175+
const decodeStatus = Schema.decodeUnknownOption(
176+
Schema.Struct({
177+
version: Schema.String,
178+
pid: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
179+
}),
180+
)
175181

176182
// A missing or corrupt file means no valid info; callers treat both
177183
// the same (the registering server self-evicts, clients rediscover).
@@ -187,6 +193,7 @@ type LocalService = {
187193
readonly endpoint: Endpoint
188194
readonly version?: string
189195
readonly state: "ready" | "waiting" | "failed"
196+
readonly compatible: boolean
190197
}
191198

192199
const probe = Effect.fnUntraced(function* (info: Info) {
@@ -207,14 +214,30 @@ const probeResult = Effect.fnUntraced(function* (
207214
const signal = AbortSignal.timeout(timeout)
208215
const result = yield* Effect.promise(() =>
209216
fetch(new URL("/api/status", info.url), { headers: headers(endpoint), signal })
210-
.then(async (response) => ({ response, body: (await response.json()) as unknown }))
217+
.then(async (response) => ({
218+
response,
219+
body: response.status === 404 ? undefined : ((await response.json()) as unknown),
220+
}))
211221
.then(
212222
(value) => ({ value }),
213223
(cause: unknown) => ({ cause }),
214224
),
215225
)
216226
if ("cause" in result) return { service: undefined, timedOut: signal.aborted }
217227
const response = result.value.response
228+
// The previous V2 service exposes /api/health instead. Its authenticated 404 is enough
229+
// to recognize the registered daemon as incompatible and route it through replacement.
230+
if (response.status === 404)
231+
return {
232+
service: {
233+
info,
234+
endpoint,
235+
version: info.version,
236+
state: "ready" as const,
237+
compatible: false,
238+
} satisfies LocalService,
239+
timedOut: false,
240+
}
218241
const body = result.value.body
219242
const status = decodeStatus(body)
220243
if (Option.isSome(status)) {
@@ -227,6 +250,7 @@ const probeResult = Effect.fnUntraced(function* (
227250
endpoint,
228251
version: status.value.version,
229252
state: response.ok ? "ready" : response.status === 500 ? "failed" : "waiting",
253+
compatible: true,
230254
} satisfies LocalService,
231255
timedOut: false,
232256
}

‎packages/client/src/promise/service.ts‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
import { defaultEnsureTiming, ensureTiming, type EnsureTiming } from "../service-timing.js"
1212
import { matchesVersion } from "../service-version.js"
1313
import { PtyHandoff } from "../pty-handoff.js"
14-
import type { ServerStatus } from "./generated/types.js"
1514

1615
export * from "../service.js"
1716

@@ -25,6 +24,7 @@ export * from "../service.js"
2524
export async function discover(options: DiscoverOptions = {}) {
2625
const found = (await registered(options.file)).service
2726
if (found?.state !== "ready") return undefined
27+
if (!found.compatible) return undefined
2828
if (!matchesVersion(found.version, options)) return undefined
2929
return found.endpoint
3030
}
@@ -76,7 +76,7 @@ export async function ensure(options: EnsureOptions = {}): Promise<Endpoint> {
7676
if (registration.service !== undefined) {
7777
spawnDelay = timing.spawnDelay
7878
const service = registration.service
79-
const compatible = matchesVersion(service.version, options)
79+
const compatible = service.compatible && matchesVersion(service.version, options)
8080
if (compatible && service.state === "ready") {
8181
await PtyHandoff.complete(options.file ?? fallback(), service.info)
8282
return service.endpoint
@@ -151,6 +151,7 @@ type LocalService = {
151151
readonly endpoint: Endpoint
152152
readonly version?: string
153153
readonly state: "ready" | "waiting" | "failed"
154+
readonly compatible: boolean
154155
}
155156

156157
async function probeResult(info: Info, timeout = defaultEnsureTiming.requestTimeout) {
@@ -165,31 +166,52 @@ async function probeResult(info: Info, timeout = defaultEnsureTiming.requestTime
165166
const result = await fetch(new URL("/api/status", info.url), { headers: headers(endpoint), signal })
166167
.then(async (response) => ({
167168
response,
168-
body: (await response.json()) as ServerStatus,
169+
body: response.status === 404 ? undefined : ((await response.json()) as unknown),
169170
}))
170171
.then(
171172
(value) => ({ value }),
172173
(cause: unknown) => ({ cause }),
173174
)
174175
if ("cause" in result) return { service: undefined, timedOut: signal.aborted }
175176
const response = result.value.response
176-
const body = result.value.body
177-
if (body !== undefined && "version" in body && "pid" in body) {
178-
if (body.pid !== info.pid) return { service: undefined, timedOut: false }
179-
if (info.version !== undefined && body.version !== info.version) return { service: undefined, timedOut: false }
177+
// The previous V2 service exposes /api/health instead. Its authenticated 404 is enough
178+
// to recognize the registered daemon as incompatible and route it through replacement.
179+
if (response.status === 404)
180180
return {
181181
service: {
182182
info,
183183
endpoint,
184-
version: body.version,
184+
version: info.version,
185+
state: "ready" as const,
186+
compatible: false,
187+
} satisfies LocalService,
188+
timedOut: false,
189+
}
190+
const status = decodeStatus(result.value.body)
191+
if (status !== undefined) {
192+
if (status.pid !== info.pid) return { service: undefined, timedOut: false }
193+
if (info.version !== undefined && status.version !== info.version) return { service: undefined, timedOut: false }
194+
return {
195+
service: {
196+
info,
197+
endpoint,
198+
version: status.version,
185199
state: response.ok ? "ready" : response.status === 500 ? "failed" : "waiting",
200+
compatible: true,
186201
} satisfies LocalService,
187202
timedOut: false,
188203
}
189204
}
190205
return { service: undefined, timedOut: false }
191206
}
192207

208+
function decodeStatus(input: unknown) {
209+
if (typeof input !== "object" || input === null) return
210+
if (!("version" in input) || typeof input.version !== "string") return
211+
if (!("pid" in input) || typeof input.pid !== "number" || !Number.isInteger(input.pid) || input.pid < 0) return
212+
return { version: input.version, pid: input.pid }
213+
}
214+
193215
async function registered(file?: string, timeout?: number) {
194216
const info = await read(file)
195217
if (info === undefined) return { info: undefined, service: undefined, timedOut: false }

0 commit comments

Comments
 (0)