refactor(client): rename service start to ensure

This commit is contained in:
Dax Raad 2026-07-15 13:34:32 -04:00
commit ec8ee60e0b
17 changed files with 79 additions and 64 deletions

View file

@ -6,7 +6,7 @@ import { join } from "node:path"
import type {
DiscoverOptions,
Endpoint,
StartOptions,
EnsureOptions,
StopOptions,
} from "../service.js"
@ -28,7 +28,7 @@ type Contender = {
}
// Read-only lookup: registration file plus health check and version gate.
// Never spawns; escalation to start() is the caller's policy.
// Never spawns; escalation to ensure() is the caller's policy.
/** Discover a healthy, compatible local service without starting one. */
export const discover = Effect.fn("service.discover")(function* (options: DiscoverOptions = {}) {
return (yield* discoverLocal(options))?.endpoint
@ -45,7 +45,7 @@ const discoverLocal = Effect.fnUntraced(function* (options: DiscoverOptions) {
// version-mismatched one, and otherwise spawns small contenders until a server
// becomes discoverable. A contender is never killed merely for slow startup.
/** Ensure a healthy, compatible local service is running. */
export const start = Effect.fn("service.start")(function* (options: StartOptions = {}) {
export const ensure = Effect.fn("service.ensure")(function* (options: EnsureOptions = {}) {
const contenders = new Set<Contender>()
let announced = false
let lastSpawn = 0
@ -219,7 +219,7 @@ const find = Effect.fnUntraced(function* (options: { readonly file?: string }) {
return (yield* registered(options.file, true)).service
})
// 50ms cadence bounded at ~5s, shared by stop escalation and each start
// 50ms cadence bounded at ~5s, shared by stop escalation and each ensure
// discovery window.
const poll = Schedule.spaced("50 millis").pipe(Schedule.both(Schedule.recurs(100)))
@ -280,4 +280,4 @@ const requestStop = Effect.fnUntraced(function* (service: LocalService) {
})
/** Effect-based local service lifecycle operations. */
export const Service = { discover, start, stop, headers, Info }
export const Service = { discover, ensure, stop, headers, Info }

View file

@ -6,7 +6,7 @@ import type {
DiscoverOptions,
Endpoint,
Info,
StartOptions,
EnsureOptions,
StopOptions,
} from "../service.js"
import type { ServiceHealth, ServiceStopResponse } from "./generated/types.js"
@ -37,7 +37,7 @@ async function discoverLocal(options: DiscoverOptions) {
}
/** Ensure a healthy, compatible local service is running. */
export async function start(options: StartOptions = {}): Promise<Endpoint> {
export async function ensure(options: EnsureOptions = {}): Promise<Endpoint> {
const contenders = new Set<Contender>()
let announced = false
let lastSpawn = 0
@ -249,4 +249,4 @@ function delay(milliseconds: number) {
}
/** Promise-based local service lifecycle operations. */
export const Service = { discover, start, stop, headers }
export const Service = { discover, ensure, stop, headers }

View file

@ -21,15 +21,15 @@ export type DiscoverOptions = {
readonly version?: string
}
/** Reason a new service process must be started. */
export type StartReason = "missing" | "version-mismatch"
/** Reason ensuring the service requires a new process. */
export type EnsureReason = "missing" | "version-mismatch"
/** Options used to ensure the local OpenCode service is running. */
export type StartOptions = DiscoverOptions & {
export type EnsureOptions = DiscoverOptions & {
/** Service command and arguments. Defaults to `opencode serve --service`. */
readonly command?: ReadonlyArray<string>
/** Called once before spawning a new service process. */
readonly onStart?: (reason: StartReason, previousVersion?: string) => void
readonly onStart?: (reason: EnsureReason, previousVersion?: string) => void
}
/** Options used to stop the local OpenCode service. */