11import { Effect , FileSystem , Option , Schedule , Schema } from "effect"
2- import { homedir } from "node:os"
3- import { join } from "node:path"
4- import type { DiscoverOptions , Endpoint , EnsureOptions , StopOptions } from "../service.js"
2+ import type { DiscoverOptions , EnsureOptions , StopOptions } from "../service.js"
53import {
64 contenderFailure ,
75 contenderFinished ,
@@ -11,8 +9,10 @@ import {
119import { defaultEnsureTiming , ensureTiming , type EnsureTiming } from "../service-timing.js"
1210import { matchesVersion } from "../service-version.js"
1311import { PtyHandoff } from "../pty-handoff.js"
12+ import { fallback , headers , type LocalService , probeResult , same } from "../service-probe.js"
1413
1514export * from "../service.js"
15+ export { headers }
1616/** Contents of the local service registration file. */
1717export type Info = import ( "../service.js" ) . Info
1818
@@ -40,9 +40,9 @@ export const incumbent = Effect.fn("service.incumbent")(function* (
4040 options : DiscoverOptions & { readonly url : string } ,
4141) {
4242 const info = yield * read ( options . file )
43- const found = info === undefined ? undefined : yield * probe ( { ... info , url : options . url } )
44- if ( found === undefined ) return undefined
45- if ( ! found . compatible ) return undefined
43+ if ( info === undefined ) return undefined
44+ const found = ( yield * Effect . promise ( ( ) => probeResult ( { ... info , url : options . url } ) ) ) . service
45+ if ( ! found ? .compatible ) return undefined
4646 if ( ! matchesVersion ( found . version , options ) ) return undefined
4747 return { endpoint : found . endpoint , state : found . state }
4848} )
@@ -177,17 +177,6 @@ export const stop = Effect.fn("service.stop")(function* (options: StopOptions =
177177 if ( info !== undefined ) yield * terminate ( info , options , ensureTiming ( options ) )
178178} )
179179
180- function fallback ( ) {
181- const state = process . env [ "XDG_STATE_HOME" ] ?? join ( homedir ( ) , ".local" , "state" )
182- return join ( state , "opencode" , "service.json" )
183- }
184-
185- /** Create HTTP authentication headers for a service endpoint. */
186- export function headers ( endpoint : Endpoint ) {
187- if ( endpoint . auth === undefined ) return undefined
188- return { authorization : "Basic " + btoa ( endpoint . auth . username + ":" + endpoint . auth . password ) }
189- }
190-
191180/** Schema for the local service registration file. */
192181export const Info = Schema . Struct ( {
193182 id : Schema . optional ( Schema . String ) ,
@@ -198,13 +187,6 @@ export const Info = Schema.Struct({
198187} )
199188
200189const decode = Schema . decodeUnknownEffect ( Schema . fromJsonString ( Info ) )
201- const decodeInfo = Schema . decodeUnknownOption (
202- Schema . Struct ( {
203- version : Schema . String ,
204- pid : Schema . Int . check ( Schema . isGreaterThanOrEqualTo ( 0 ) ) ,
205- } ) ,
206- )
207-
208190// A missing or corrupt file means no valid info; callers treat both
209191// the same (the registering server self-evicts, clients rediscover).
210192const read = Effect . fnUntraced ( function * ( file ?: string ) {
@@ -214,77 +196,10 @@ const read = Effect.fnUntraced(function* (file?: string) {
214196 return yield * decode ( text . value ) . pipe ( Effect . option , Effect . map ( Option . getOrUndefined ) )
215197} )
216198
217- type LocalService = {
218- readonly info : Info
219- readonly endpoint : Endpoint
220- readonly version ?: string
221- readonly state : "ready" | "waiting" | "failed"
222- readonly compatible : boolean
223- }
224-
225- const probe = Effect . fnUntraced ( function * ( info : Info ) {
226- return ( yield * probeResult ( info ) ) . service
227- } )
228-
229- const probeResult = Effect . fnUntraced ( function * ( info : Info , timeout = defaultEnsureTiming . requestTimeout ) {
230- const endpoint = {
231- url : info . url ,
232- auth :
233- info . password === undefined
234- ? undefined
235- : { type : "basic" as const , username : "opencode" , password : info . password } ,
236- } satisfies Endpoint
237- const signal = AbortSignal . timeout ( timeout )
238- const result = yield * Effect . promise ( ( ) =>
239- fetch ( new URL ( "/api/info" , info . url ) , { headers : headers ( endpoint ) , signal } )
240- . then ( async ( response ) => ( {
241- response,
242- body : response . status === 404 ? undefined : ( ( await response . json ( ) ) as unknown ) ,
243- } ) )
244- . then (
245- ( value ) => ( { value } ) ,
246- ( cause : unknown ) => ( { cause } ) ,
247- ) ,
248- )
249- if ( "cause" in result ) return { service : undefined , timedOut : signal . aborted }
250- const response = result . value . response
251- // A missing health endpoint identifies protocol incompatibility, not an older
252- // version. Only an unmet version requirement lets ensure replace this owner.
253- if ( response . status === 404 )
254- return {
255- service : {
256- info,
257- endpoint,
258- version : info . version ,
259- state : "ready" as const ,
260- compatible : false ,
261- } satisfies LocalService ,
262- timedOut : false ,
263- }
264- const body = result . value . body
265- const serverInfo = decodeInfo ( body )
266- if ( Option . isSome ( serverInfo ) ) {
267- if ( serverInfo . value . pid !== info . pid ) return { service : undefined , timedOut : false }
268- if ( info . version !== undefined && serverInfo . value . version !== info . version )
269- return { service : undefined , timedOut : false }
270- return {
271- service : {
272- info,
273- endpoint,
274- version : serverInfo . value . version ,
275- state : response . ok ? "ready" : response . status === 500 ? "failed" : "waiting" ,
276- compatible : true ,
277- } satisfies LocalService ,
278- timedOut : false ,
279- }
280- }
281- return { service : undefined , timedOut : false }
282- } )
283-
284199const registered = Effect . fnUntraced ( function * ( file ?: string , timeout ?: number ) {
285200 const info = yield * read ( file )
286201 if ( info === undefined ) return { info : undefined , service : undefined , timedOut : false }
287- return { info, ...( yield * probeResult ( info , timeout ) ) }
202+ return { info, ...( yield * Effect . promise ( ( ) => probeResult ( info , timeout ) ) ) }
288203} )
289204
290205// 50ms cadence bounded at ~5s, shared by stop escalation and each ensure
@@ -303,10 +218,6 @@ const stopped = Effect.fnUntraced(function* (pid: number) {
303218 return yield * Effect . fail ( new Error ( `Server process ${ pid } is still running` ) )
304219} )
305220
306- function same ( left : Info , right : Info ) {
307- return left . id === right . id && left . version === right . version && left . url === right . url && left . pid === right . pid
308- }
309-
310221const terminate = Effect . fnUntraced ( function * ( info : Info , options : { readonly file ?: string } , timing : EnsureTiming ) {
311222 const current = yield * read ( options . file )
312223 if ( current === undefined || ! same ( current , info ) ) return
0 commit comments