feat(core): resume suspended sessions after service restart (#36105)

This commit is contained in:
Kit Langton 2026-07-09 13:19:15 -04:00 committed by GitHub
commit 6524dfc818
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 820 additions and 299 deletions

View file

@ -6,6 +6,7 @@ import { Context, Effect, Layer, Option } from "effect"
import { HttpClient, HttpClientRequest, HttpMiddleware, HttpRouter, HttpServer } from "effect/unstable/http"
import { HttpApi, HttpApiClient } from "effect/unstable/httpapi"
import { createServer } from "node:http"
import { SessionRestart } from "@opencode-ai/core/session/execution/restart"
import { ServerAuth } from "./auth"
import { createRoutes } from "./routes"
import { ServerInfo } from "./server-info"
@ -14,6 +15,7 @@ export type Options = {
readonly hostname: string
readonly port: Option.Option<number>
readonly password: string
readonly restartContinuity?: boolean
}
const ReadinessApi = HttpApi.make("readiness").add(HealthGroup)
@ -38,30 +40,49 @@ export const start = Effect.fn("ServerProcess.start")(function* (options: Option
})
function listen(options: Options) {
if (Option.isSome(options.port)) return bind(options.hostname, options.port.value, options.password)
if (Option.isSome(options.port)) return bind(options, options.port.value)
const next = (port: number): ReturnType<typeof bind> =>
bind(options.hostname, port, options.password).pipe(
Effect.catch((error) => (port === 65_535 ? Effect.fail(error) : next(port + 1))),
)
bind(options, port).pipe(Effect.catch((error) => (port === 65_535 ? Effect.fail(error) : next(port + 1))))
return next(4096)
}
function bind(hostname: string, port: number, password: string) {
function bind(options: Options, port: number) {
const server = createServer()
return Layer.build(
createRoutes(password, () => {
createRoutes(options.password, () => {
const address = server.address()
if (address === null || typeof address === "string") return []
const host = address.family === "IPv6" ? `[${address.address}]` : address.address
return ServerInfo.connectionURLs(`http://${host}:${address.port}`, hostname)
return ServerInfo.connectionURLs(`http://${host}:${address.port}`, options.hostname)
}).pipe(
Layer.flatMap((context) =>
HttpServer.serve(Context.get(context, HttpRouter.HttpRouter).asHttpEffect(), HttpMiddleware.logger),
),
Layer.provideMerge(NodeHttpServer.layer(() => server, { port, host: hostname })),
Layer.flatMap((context) => {
const serve = HttpServer.serve(
Context.get(context, HttpRouter.HttpRouter).asHttpEffect(),
HttpMiddleware.logger,
)
if (!options.restartContinuity) return serve
const restart = Context.get(context, SessionRestart.Service)
return Layer.merge(serve, restartContinuity(restart))
}),
Layer.provideMerge(NodeHttpServer.layer(() => server, { port, host: options.hostname })),
),
).pipe(
Effect.tap(() => Effect.addFinalizer(() => Effect.sync(() => server.closeAllConnections()))),
Effect.map((context) => Context.get(context, HttpServer.HttpServer).address),
)
}
/**
* The managed server owns restart continuity: it resumes Sessions the previous server suspended and
* suspends its own active Sessions on graceful shutdown. Suspension runs while the drains are still
* alive: connections close first, this finalizer runs next, and Session execution teardown follows.
*/
function restartContinuity(restart: SessionRestart.Interface) {
return Layer.effectDiscard(
Effect.gen(function* () {
yield* Effect.forkScoped(restart.resumeSuspendedSessions)
// Registered after the fork so suspension observes still-running resumed drains during teardown.
yield* Effect.addFinalizer(() => restart.suspendActiveSessions)
}),
)
}