Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions packages/cli/src/commands/handlers/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { run } from "@opencode/tui"
import { Commands } from "../commands"
import { Runtime } from "../../framework/runtime"
import { Config } from "../../config"
import { Context, Effect, FileSystem, Option, Queue, Schedule, Semaphore } from "effect"
import { Effect, FileSystem, Option, Queue, Schedule, Semaphore } from "effect"
import { ServerConnection } from "../../services/server-connection"
import { Updater } from "../../services/updater"
import { UpdatePreflight } from "../../services/update-preflight"
Expand Down Expand Up @@ -87,8 +87,6 @@ export default Runtime.handler(Commands, (input) =>
preflight.loading()
const config = yield* Config.Service
const npm = yield* Npm.Service
const fileSystem = yield* FileSystem.FileSystem
const runServicePromise = Effect.runPromiseWith(Context.make(FileSystem.FileSystem, fileSystem))
const context = yield* Effect.context<FileSystem.FileSystem>()
const runFork = Effect.runForkWith(context)
const runPromise = Effect.runPromiseWith(context)
Expand All @@ -106,8 +104,8 @@ export default Runtime.handler(Commands, (input) =>
local: requestedServer === undefined,
service: service
? {
reconnect: (signal) => runServicePromise(service.reconnect(), { signal }),
restart: () => runServicePromise(service.restart()),
reconnect: (signal) => runPromise(service.reconnect(), { signal }),
restart: () => runPromise(service.restart()),
}
: undefined,
},
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/src/commands/handlers/mini.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, Effect, FileSystem, Option } from "effect"
import { Effect, FileSystem, Option } from "effect"
import { Commands } from "../commands"
import { Runtime } from "../../framework/runtime"
import { ServerConnection } from "../../services/server-connection"
Expand All @@ -16,14 +16,14 @@ export default Runtime.handler(Commands.commands.mini, (input) =>
const config = yield* Config.Service
const global = yield* Global.Service
const resolved = resolve(yield* config.get(), { terminalSuspend: process.platform !== "win32" })
const fileSystem = yield* FileSystem.FileSystem
const runServicePromise = Effect.runPromiseWith(Context.make(FileSystem.FileSystem, fileSystem))
const context = yield* Effect.context<FileSystem.FileSystem>()
const runPromise = Effect.runPromiseWith(context)
const service = server.service
yield* Effect.promise(() =>
runMini({
server: {
endpoint: server.endpoint,
reconnect: service ? (signal) => runServicePromise(service.reconnect(), { signal }) : undefined,
reconnect: service ? (signal) => runPromise(service.reconnect(), { signal }) : undefined,
},
continue: input.continue,
session: Option.getOrUndefined(input.session),
Expand All @@ -36,7 +36,7 @@ export default Runtime.handler(Commands.commands.mini, (input) =>
demo: input.demo,
tuiConfig: resolved,
config: {
update: (update) => runServicePromise(config.update(update)),
update: (update) => runPromise(config.update(update)),
},
paths: { home: global.home, state: global.state, log: global.log },
}),
Expand Down
28 changes: 24 additions & 4 deletions packages/cli/src/services/server-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,30 @@ export const resolve = Effect.fn("cli.server-connection.resolve")(function* (arg
if (!endpoint) return { endpoint: yield* Standalone.start(), standalone: true } satisfies Resolved
return { endpoint, service: managedService(options) } satisfies Resolved
}
return {
endpoint: yield* resolveManaged({ ...options, onStart: args.onStart }, mismatch),
service: managedService(options),
} satisfies Resolved
// Service.ensure waits minutes for a registered service that stopped answering; say so instead of leaving the
// terminal blank. Once it starts a server, onStart reports progress and the update screen may own the terminal.
let started = false
const endpoint = yield* Effect.raceFirst(
resolveManaged(
{
...options,
onStart: (reason, previousVersion) => {
started = true
args.onStart?.(reason, previousVersion)
},
},
mismatch,
),
Effect.sleep("5 seconds").pipe(
Effect.andThen(
Effect.sync(() => {
if (!started) process.stderr.write("Waiting for the background server to respond...\n")
}),
),
Effect.andThen(Effect.never),
),
)
return { endpoint, service: managedService(options) } satisfies Resolved
})

function managedService(options: EnsureOptions) {
Expand Down
74 changes: 47 additions & 27 deletions packages/client/src/effect/service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { Effect, FileSystem, Option, Schedule, Schema } from "effect"
import { Clock, Effect, FileSystem, Option, Schedule, Schema } from "effect"
import type { DiscoverOptions, EnsureOptions, StopOptions } from "../service.js"
import { contenderPool, spawnServiceContender } from "../service-contender.js"
import { defaultEnsureTiming, ensureTiming, type EnsureTiming } from "../service-timing.js"
import { matchesVersion } from "../service-version.js"
import { PtyHandoff } from "../pty-handoff.js"
import { decide, fallback, headers, probeResult, same } from "../service-probe.js"
import {
decide,
fallback,
headers,
isUninterruptibleDiskSleep,
probeResult,
same,
unresponsiveOwner,
} from "../service-probe.js"

export * from "../service.js"
export { headers }
Expand Down Expand Up @@ -49,7 +57,8 @@ export const incumbent = Effect.fn("service.incumbent")(function* (
export const ensure = Effect.fn("service.ensure")(function* (options: EnsureOptions = {}) {
const timing = ensureTiming(options)
const pool = contenderPool(timing)
let timeouts: { readonly info: Info; readonly count: number } | undefined
const unresponsive = unresponsiveOwner(timing.unresponsiveTimeout)
let deadline = (yield* Clock.currentTimeMillis) + timing.promiseTimeout
let announced = false
const announce = (reason: "missing" | "version-mismatch", previousVersion?: string) =>
Effect.sync(() => {
Expand All @@ -68,25 +77,39 @@ export const ensure = Effect.fn("service.ensure")(function* (options: EnsureOpti
catch: (cause) => new Error("Failed to start server", { cause }),
})
})
let unanswered = false
const timedOut = () =>
pool.failure() ??
new Error(
unanswered
? "Timed out waiting for the background service to respond"
: "Timed out waiting for the background service to start",
)
const found = yield* Effect.gen(function* () {
const probedAt = yield* Clock.currentTimeMillis
// An owner still within its unresponsive budget holds the deadline open, so a long budget is not cut short.
if (probedAt >= deadline && !unresponsive.waiting()) return yield* Effect.fail(timedOut())
const registration = yield* registered(options.file, timing.requestTimeout)
const info = registration.info
const service = registration.service
if (registration.timedOut && info !== undefined) {
timeouts = {
info,
count: timeouts !== undefined && same(timeouts.info, info) ? timeouts.count + 1 : 1,
}
if (timeouts.count >= 3) {
yield* announce("missing")
yield* Effect.logWarning("Background service is unresponsive; recovery cannot preserve persistent terminals")
yield* Effect.tryPromise(() => PtyHandoff.clear(options.file ?? fallback()))
yield* terminate(info, options, timing)
pool.evict(info.pid)
pool.recruitNow()
timeouts = undefined
}
} else timeouts = undefined
// A server this call started can block its event loop while it boots, opening a large database on a slow disk.
// The startup deadline bounds it rather than the unresponsive budget, so that boot is never killed and repeated.
const owner = registration.timedOut && info !== undefined && !pool.spawned(info.pid) ? info : undefined
unanswered = owner !== undefined
const diskSleep = owner !== undefined && (yield* Effect.promise(() => isUninterruptibleDiskSleep(owner.pid)))
const expired = unresponsive.observe(owner, probedAt, yield* Clock.currentTimeMillis, diskSleep)
let evicted = false
if (expired !== undefined) {
yield* announce("missing")
yield* Effect.logWarning("Background service is unresponsive; recovery cannot preserve persistent terminals")
yield* Effect.tryPromise(() => PtyHandoff.clear(options.file ?? fallback()))
yield* terminate(expired, options, timing)
pool.evict(expired.pid)
pool.recruitNow()
unresponsive.forget()
deadline = (yield* Clock.currentTimeMillis) + timing.promiseTimeout
evicted = true
}
if (service !== undefined) {
pool.serviceAnswered()
const decision = decide(service, options)
Expand All @@ -107,22 +130,19 @@ export const ensure = Effect.fn("service.ensure")(function* (options: EnsureOpti

const failed = pool.reap()
if (failed !== undefined) return yield* Effect.fail(failed)
if (pool.shouldRecruit(info !== undefined)) {
// An owner that times out still holds the port; a contender would only add load until it is evicted.
if ((!registration.timedOut || evicted) && pool.shouldRecruit(info !== undefined)) {
yield* announce("missing")
pool.add(yield* spawnContender)
}
return Option.none()
}).pipe(
Effect.repeat({
until: Option.isSome,
// Probes run sequentially, so a slow probe stretches each iteration; bound the loop by wall clock
// like the Promise variant rather than by attempt count.
schedule: Schedule.spaced(timing.pollInterval).pipe(Schedule.upTo({ duration: timing.promiseTimeout })),
}),
// Probes run sequentially, so a slow probe stretches each iteration; the loop is bounded by the wall-clock
// deadline above rather than by attempt count, like the Promise variant.
Effect.repeat({ until: Option.isSome, schedule: Schedule.spaced(timing.pollInterval) }),
Effect.ensuring(Effect.sync(() => pool.releaseAll())),
)
if (Option.isNone(found))
return yield* Effect.fail(pool.failure() ?? new Error("Timed out waiting for the background service to start"))
if (Option.isNone(found)) return yield* Effect.fail(timedOut())
return found.value.endpoint
})

Expand Down
65 changes: 44 additions & 21 deletions packages/client/src/promise/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import { contenderPool, spawnServiceContender } from "../service-contender.js"
import { defaultEnsureTiming, ensureTiming, type EnsureTiming } from "../service-timing.js"
import { matchesVersion } from "../service-version.js"
import { PtyHandoff } from "../pty-handoff.js"
import { decide, fallback, headers, probeResult, same } from "../service-probe.js"
import {
decide,
fallback,
headers,
isUninterruptibleDiskSleep,
probeResult,
same,
unresponsiveOwner,
} from "../service-probe.js"

export * from "../service.js"
export { fallback, headers }
Expand All @@ -27,9 +35,9 @@ export async function discover(options: DiscoverOptions = {}) {
/** Ensure a healthy, compatible local service is running. */
export async function ensure(options: EnsureOptions = {}): Promise<Endpoint> {
const timing = ensureTiming(options)
const deadline = Date.now() + timing.promiseTimeout
let deadline = Date.now() + timing.promiseTimeout
const pool = contenderPool(timing)
let timeouts: { readonly info: Info; readonly count: number } | undefined
const unresponsive = unresponsiveOwner(timing.unresponsiveTimeout)
let announced = false

const announce = (reason: "missing" | "version-mismatch", previousVersion?: string) => {
Expand All @@ -48,25 +56,39 @@ export async function ensure(options: EnsureOptions = {}): Promise<Endpoint> {
}

try {
let unanswered = false
while (true) {
if (Date.now() >= deadline)
throw pool.failure() ?? new Error("Timed out waiting for the background service to start")
// An owner still within its unresponsive budget holds the deadline open, so a long budget is not cut short.
if (Date.now() >= deadline && !unresponsive.waiting())
throw (
pool.failure() ??
new Error(
unanswered
? "Timed out waiting for the background service to respond"
: "Timed out waiting for the background service to start",
)
)
const probedAt = Date.now()
const registration = await registered(options.file, timing.requestTimeout)
if (registration.timedOut && registration.info !== undefined) {
timeouts = {
info: registration.info,
count: timeouts !== undefined && same(timeouts.info, registration.info) ? timeouts.count + 1 : 1,
}
if (timeouts.count >= 3) {
announce("missing")
console.warn("Background service is unresponsive; recovery cannot preserve persistent terminals")
await PtyHandoff.clear(options.file ?? fallback())
await terminate(registration.info, options, timing)
pool.evict(registration.info.pid)
pool.recruitNow()
timeouts = undefined
}
} else timeouts = undefined
const info = registration.info
// A server this call started can block its event loop while it boots, opening a large database on a slow disk.
// The startup deadline bounds it rather than the unresponsive budget, so that boot is never killed and repeated.
const owner = registration.timedOut && info !== undefined && !pool.spawned(info.pid) ? info : undefined
unanswered = owner !== undefined
const diskSleep = owner !== undefined && (await isUninterruptibleDiskSleep(owner.pid))
const expired = unresponsive.observe(owner, probedAt, Date.now(), diskSleep)
let evicted = false
if (expired !== undefined) {
announce("missing")
console.warn("Background service is unresponsive; recovery cannot preserve persistent terminals")
await PtyHandoff.clear(options.file ?? fallback())
await terminate(expired, options, timing)
pool.evict(expired.pid)
pool.recruitNow()
unresponsive.forget()
deadline = Date.now() + timing.promiseTimeout
evicted = true
}

if (registration.service !== undefined) {
pool.serviceAnswered()
Expand All @@ -87,7 +109,8 @@ export async function ensure(options: EnsureOptions = {}): Promise<Endpoint> {
} else {
const failed = pool.reap()
if (failed !== undefined) throw failed
if (pool.shouldRecruit(registration.info !== undefined)) {
// An owner that times out still holds the port; a contender would only add load until it is evicted.
if ((!registration.timedOut || evicted) && pool.shouldRecruit(info !== undefined)) {
announce("missing")
pool.add(await spawnContender())
}
Expand Down
4 changes: 4 additions & 0 deletions packages/client/src/service-contender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ export function contenderPool(timing: EnsureTiming) {
failure = undefined
lastSpawn = 0
},
/** Whether this call started the process `pid`. */
spawned(pid: number) {
return [...contenders].some((item) => item.child.pid === pid)
},
/** Let the next attempt start without waiting a spawn delay. */
recruitNow() {
lastSpawn = Date.now() - spawnDelay
Expand Down
Loading
Loading