mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
fix(core): leave jobs unsettled when teardown interrupts them
This commit is contained in:
parent
3a486ee46b
commit
90f7884c74
2 changed files with 102 additions and 2 deletions
|
|
@ -1,9 +1,23 @@
|
|||
export * as Job from "./job.js"
|
||||
|
||||
import { asc, eq, or } from "drizzle-orm"
|
||||
import { Array, Cause, Clock, Context, Deferred, Effect, Exit, Layer, Schema, Scope, SynchronizedRef } from "effect"
|
||||
import {
|
||||
Array,
|
||||
Cause,
|
||||
Clock,
|
||||
Context,
|
||||
Deferred,
|
||||
Effect,
|
||||
Exit,
|
||||
Layer,
|
||||
Option,
|
||||
Schema,
|
||||
Scope,
|
||||
SynchronizedRef,
|
||||
} from "effect"
|
||||
import { makeGlobalNode } from "@opencode/util/effect/app-node"
|
||||
import { Database } from "./database/database.js"
|
||||
import { Detaching } from "./detaching.js"
|
||||
import { JobList } from "./job/list.js"
|
||||
import { type Recovery, RecoverySchema } from "./job/recovery.js"
|
||||
import { JobTable } from "./job/sql.js"
|
||||
|
|
@ -59,6 +73,8 @@ type State = {
|
|||
type FinishResult = {
|
||||
info?: Info
|
||||
done?: Deferred.Deferred<Info>
|
||||
// Teardown interrupted the work: `done` is interrupted instead of settled.
|
||||
interrupted?: boolean
|
||||
scope?: Scope.Closeable
|
||||
generation?: Scope.Closeable
|
||||
}
|
||||
|
|
@ -187,6 +203,8 @@ function decrementSession(input: Map<SessionSchema.ID, number>, sessionID: Sessi
|
|||
*/
|
||||
export const make = Effect.gen(function* () {
|
||||
const db = (yield* Database.Service).db
|
||||
// Optional so registries built outside the application graph (tests, tools) need not provide it.
|
||||
const detaching = Option.getOrUndefined(yield* Effect.serviceOption(Detaching.Service))
|
||||
const state: State = {
|
||||
jobs: yield* SynchronizedRef.make(new Map()),
|
||||
scope: yield* Scope.Scope,
|
||||
|
|
@ -248,6 +266,15 @@ export const make = Effect.gen(function* () {
|
|||
if (!job) return [{}, jobs]
|
||||
if (job.scope !== scope) return [{}, jobs]
|
||||
if (job.info.status !== "running") return [{ info: snapshot(job) }, jobs]
|
||||
// Teardown (this registry closing, or a planned shutdown) is not a terminal outcome: leave the job
|
||||
// and its durable row running for the next server, and interrupt rather than settle its observers.
|
||||
// A user cancel has already settled the job as cancelled before interrupting its work.
|
||||
if (
|
||||
Exit.isFailure(exit) &&
|
||||
Cause.hasInterruptsOnly(exit.cause) &&
|
||||
(scope.state._tag === "Closed" || detaching?.isDetaching())
|
||||
)
|
||||
return [{ done: job.done, scope: job.scope, interrupted: true }, jobs]
|
||||
const status: Exclude<Status, "running"> = Exit.isSuccess(exit)
|
||||
? "completed"
|
||||
: Cause.hasInterruptsOnly(exit.cause)
|
||||
|
|
@ -268,6 +295,7 @@ export const make = Effect.gen(function* () {
|
|||
return [{ info: snapshot(next), done: job.done, scope: job.scope }, new Map(jobs).set(id, next)]
|
||||
}),
|
||||
)
|
||||
if (result.interrupted && result.done) yield* Deferred.interrupt(result.done)
|
||||
if (result.info && result.done) yield* Deferred.succeed(result.done, result.info)
|
||||
if (result.scope) {
|
||||
yield* Scope.close(result.scope, Exit.void).pipe(Effect.forkIn(state.scope, { startImmediately: true }))
|
||||
|
|
@ -295,6 +323,8 @@ export const make = Effect.gen(function* () {
|
|||
return [{ info: snapshot(existing) }, jobs]
|
||||
}
|
||||
const scope = yield* Scope.fork(state.scope, "parallel")
|
||||
// Closing the registry interrupts the work without settling it; interrupt its observers too.
|
||||
yield* Scope.addFinalizer(scope, Deferred.interrupt(done))
|
||||
const job = {
|
||||
info: {
|
||||
id: input.id,
|
||||
|
|
@ -551,4 +581,4 @@ export const make = Effect.gen(function* () {
|
|||
|
||||
const layer = Layer.effect(Service, make)
|
||||
|
||||
export const node = makeGlobalNode({ service: Service, layer, deps: [Database.node] })
|
||||
export const node = makeGlobalNode({ service: Service, layer, deps: [Database.node, Detaching.node] })
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { describe, expect } from "bun:test"
|
||||
import { Database } from "@opencode/core/database/database"
|
||||
import { Detaching } from "@opencode/core/detaching"
|
||||
import jobMigration from "@opencode/core/database/migration/20261030054400_job"
|
||||
import { Job } from "@opencode/core/job"
|
||||
import { JobTable } from "@opencode/core/job/sql"
|
||||
|
|
@ -457,6 +458,75 @@ describe("Job", () => {
|
|||
}),
|
||||
)
|
||||
|
||||
it.live("interrupts observers without settling a background job when its registry is torn down", () =>
|
||||
Effect.gen(function* () {
|
||||
const scope = yield* Scope.make()
|
||||
const jobs = yield* Job.make.pipe(Scope.provide(scope))
|
||||
const job = yield* jobs.start({
|
||||
id: "shell_teardown",
|
||||
type: "shell",
|
||||
recovery: {
|
||||
kind: "shell",
|
||||
sessionID: SessionSchema.ID.make("ses_teardown"),
|
||||
shellID: "shell_teardown",
|
||||
command: "sleep 60",
|
||||
},
|
||||
run: Effect.never,
|
||||
})
|
||||
const background = yield* jobs.background(job.id)
|
||||
const observer = yield* jobs.wait({ id: job.id }).pipe(Effect.forkChild({ startImmediately: true }))
|
||||
|
||||
yield* Scope.close(scope, Exit.void)
|
||||
|
||||
const observed = yield* Fiber.await(observer).pipe(Effect.timeout("1 second"))
|
||||
expect(Exit.isFailure(observed) && Cause.hasInterruptsOnly(observed.cause)).toBe(true)
|
||||
const current = yield* Job.make
|
||||
expect(yield* current.pendingBackground).toMatchObject([
|
||||
{ id: job.id, notificationID: background?.notificationID, status: "running" },
|
||||
])
|
||||
if (background?.notificationID) yield* current.completeBackground(background.notificationID)
|
||||
}),
|
||||
)
|
||||
|
||||
it.live("leaves interrupted work unsettled while detaching, but still cancels on request", () =>
|
||||
Effect.gen(function* () {
|
||||
const detaching = yield* Detaching.make
|
||||
const jobs = yield* Job.make.pipe(Effect.provideService(Detaching.Service, detaching))
|
||||
const recovery = (id: string) => ({
|
||||
kind: "shell" as const,
|
||||
sessionID: SessionSchema.ID.make("ses_detaching"),
|
||||
shellID: id,
|
||||
command: "sleep 60",
|
||||
})
|
||||
const interrupt = yield* Deferred.make<void>()
|
||||
const detached = yield* jobs.start({
|
||||
id: "shell_detached",
|
||||
type: "shell",
|
||||
recovery: recovery("shell_detached"),
|
||||
run: Deferred.await(interrupt).pipe(Effect.andThen(Effect.interrupt)),
|
||||
})
|
||||
const cancelled = yield* jobs.start({
|
||||
id: "shell_cancelled",
|
||||
type: "shell",
|
||||
recovery: recovery("shell_cancelled"),
|
||||
run: Effect.never,
|
||||
})
|
||||
const background = yield* jobs.background(detached.id)
|
||||
const observer = yield* jobs.wait({ id: detached.id }).pipe(Effect.forkChild({ startImmediately: true }))
|
||||
|
||||
yield* detaching.handoffPrepared
|
||||
expect(yield* detaching.begin).toBe(true)
|
||||
yield* Deferred.succeed(interrupt, undefined)
|
||||
|
||||
const observed = yield* Fiber.await(observer).pipe(Effect.timeout("1 second"))
|
||||
expect(Exit.isFailure(observed) && Cause.hasInterruptsOnly(observed.cause)).toBe(true)
|
||||
expect(yield* jobs.pendingBackground).toMatchObject([{ id: detached.id, status: "running" }])
|
||||
expect(yield* jobs.cancel(cancelled.id)).toMatchObject({ status: "cancelled" })
|
||||
expect((yield* jobs.wait({ id: cancelled.id })).info?.status).toBe("cancelled")
|
||||
if (background?.notificationID) yield* jobs.completeBackground(background.notificationID)
|
||||
}),
|
||||
)
|
||||
|
||||
it.live("cancels running session jobs and removes unacknowledged SQLite job rows on cancelSession", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* Job.Service
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue