1- import { ServerStatus } from "@opencode/protocol/groups/server"
21import { Effect , FileSystem , Option , Schedule , Schema } from "effect"
32import { homedir } from "node:os"
43import { join } from "node:path"
@@ -31,6 +30,7 @@ export type Info = import("../service.js").Info
3130export 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
173174const 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
192199const 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 }
0 commit comments