mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
554 lines
20 KiB
TypeScript
554 lines
20 KiB
TypeScript
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 { makeGlobalNode } from "@opencode/util/effect/app-node"
|
|
import { Database } from "./database/database.js"
|
|
import { JobList } from "./job/list.js"
|
|
import { type Recovery, RecoverySchema } from "./job/recovery.js"
|
|
import { JobTable } from "./job/sql.js"
|
|
import { SessionMessage } from "./session/message.js"
|
|
import { SessionSchema } from "./session/schema.js"
|
|
|
|
export type { Recovery }
|
|
|
|
const Background = Schema.Struct({
|
|
id: Schema.String,
|
|
notificationID: SessionMessage.ID,
|
|
recovery: RecoverySchema,
|
|
status: Schema.Literals(["running", "completed", "error", "cancelled"]),
|
|
output: Schema.optionalKey(Schema.String),
|
|
error: Schema.optionalKey(Schema.String),
|
|
})
|
|
|
|
export type Background = typeof Background.Type
|
|
export type Status = Background["status"]
|
|
|
|
const decodeBackground = Schema.decodeUnknownResult(Background)
|
|
const COMPLETED_LIMIT = 25
|
|
|
|
export type Info = {
|
|
id: string
|
|
type: string
|
|
title?: string
|
|
status: Status
|
|
started_at: number
|
|
completed_at?: number
|
|
output?: string
|
|
error?: string
|
|
metadata?: Record<string, unknown>
|
|
notificationID?: SessionMessage.ID
|
|
}
|
|
|
|
type Active = {
|
|
info: Info
|
|
done: Deferred.Deferred<Info>
|
|
backgrounded: Deferred.Deferred<Info>
|
|
scope: Scope.Closeable
|
|
blockingSessions: Map<SessionSchema.ID, number>
|
|
isBackgrounded: boolean
|
|
consumed: boolean
|
|
recovery?: Recovery
|
|
}
|
|
|
|
type State = {
|
|
jobs: SynchronizedRef.SynchronizedRef<Map<string, Active>>
|
|
scope: Scope.Scope
|
|
}
|
|
|
|
type FinishResult = {
|
|
info?: Info
|
|
done?: Deferred.Deferred<Info>
|
|
scope?: Scope.Closeable
|
|
generation?: Scope.Closeable
|
|
}
|
|
|
|
type BackgroundResult = {
|
|
info?: Info
|
|
backgrounded?: Deferred.Deferred<Info>
|
|
}
|
|
|
|
type StartResult = { info: Info } | { info: Info; scope: Scope.Closeable }
|
|
|
|
type BlockWait = {
|
|
done: Deferred.Deferred<Info>
|
|
backgrounded: Deferred.Deferred<Info>
|
|
generation: Scope.Closeable
|
|
}
|
|
|
|
type BlockStart =
|
|
| { type: "missing" }
|
|
| { type: "finished"; info: Info; generation: Scope.Closeable }
|
|
| { type: "backgrounded"; info: Info }
|
|
| { type: "wait"; wait: BlockWait }
|
|
|
|
export type StartInput = {
|
|
id: string
|
|
type: string
|
|
title?: string
|
|
metadata?: Record<string, unknown>
|
|
recovery?: Recovery
|
|
notificationID?: SessionMessage.ID
|
|
run: Effect.Effect<string, unknown>
|
|
}
|
|
|
|
export type WaitInput = {
|
|
id: string
|
|
timeout?: number
|
|
}
|
|
|
|
export type WaitResult = {
|
|
info?: Info
|
|
timedOut: boolean
|
|
}
|
|
|
|
export type BlockInput = {
|
|
id: string
|
|
sessionID: SessionSchema.ID
|
|
}
|
|
|
|
export type BlockResult = { type: "finished"; info: Info } | { type: "backgrounded"; info: Info }
|
|
|
|
export type BackgroundAllInput = {
|
|
sessionID: SessionSchema.ID
|
|
type?: string
|
|
}
|
|
|
|
export interface Interface {
|
|
readonly get: (id: string) => Effect.Effect<Info | undefined>
|
|
readonly start: (input: StartInput) => Effect.Effect<Info>
|
|
readonly wait: (input: WaitInput) => Effect.Effect<WaitResult>
|
|
readonly block: (input: BlockInput) => Effect.Effect<BlockResult | undefined>
|
|
readonly background: (id: string) => Effect.Effect<Info | undefined>
|
|
readonly backgroundAll: (input: BackgroundAllInput) => Effect.Effect<Info[]>
|
|
readonly cancel: (id: string) => Effect.Effect<Info | undefined>
|
|
readonly cancelSession: (sessionID: SessionSchema.ID) => Effect.Effect<Info[]>
|
|
readonly pendingBackground: Effect.Effect<readonly Background[]>
|
|
readonly runningBackground: Effect.Effect<readonly Recovery[]>
|
|
readonly backgroundJobs: Effect.Effect<readonly JobList.Item[]>
|
|
readonly completeBackground: (notificationID: SessionMessage.ID) => Effect.Effect<void>
|
|
}
|
|
|
|
export class Service extends Context.Service<Service, Interface>()("@opencode/Job") {}
|
|
|
|
function snapshot(job: Active): Info {
|
|
return {
|
|
...job.info,
|
|
...(job.info.metadata ? { metadata: { ...job.info.metadata } } : {}),
|
|
}
|
|
}
|
|
|
|
function ownerSessionID(recovery: Recovery): SessionSchema.ID {
|
|
return recovery.kind === "subagent" ? recovery.parentSessionID : recovery.sessionID
|
|
}
|
|
|
|
function recoveryTitle(job: Active, recovery: Recovery): string | null {
|
|
if (job.info.title !== undefined) return job.info.title
|
|
if (recovery.kind === "shell") return recovery.command
|
|
if (recovery.kind === "subagent") return recovery.description
|
|
return recovery.path
|
|
}
|
|
|
|
function matchesSession(job: Active, sessionID: SessionSchema.ID) {
|
|
if (job.info.metadata?.sessionID === sessionID) return true
|
|
if (job.blockingSessions.has(sessionID)) return true
|
|
if (!job.recovery) return false
|
|
if (job.recovery.kind === "subagent")
|
|
return job.recovery.parentSessionID === sessionID || job.recovery.childSessionID === sessionID
|
|
return job.recovery.sessionID === sessionID
|
|
}
|
|
|
|
function errorText(cause: Cause.Cause<unknown>) {
|
|
const render = (error: Error): string => {
|
|
const message = error.message || error.name || "Unknown error"
|
|
if (!(error.cause instanceof Error)) return message
|
|
const detail = render(error.cause)
|
|
return detail === message || detail.startsWith(`${message}\n`) ? detail : `${message}\nCaused by: ${detail}`
|
|
}
|
|
return Cause.prettyErrors(cause).map(render).join("\n") || "Unknown error"
|
|
}
|
|
|
|
function incrementSession(input: Map<SessionSchema.ID, number>, sessionID: SessionSchema.ID) {
|
|
return new Map(input).set(sessionID, (input.get(sessionID) ?? 0) + 1)
|
|
}
|
|
|
|
function decrementSession(input: Map<SessionSchema.ID, number>, sessionID: SessionSchema.ID) {
|
|
const count = input.get(sessionID)
|
|
if (count === undefined) return input
|
|
const next = new Map(input)
|
|
if (count <= 1) next.delete(sessionID)
|
|
else next.set(sessionID, count - 1)
|
|
return next
|
|
}
|
|
|
|
/**
|
|
* Makes one scoped, process-local registry backed by the durable SQLite `job` table for
|
|
* backgrounded work until its completion notification is admitted.
|
|
*/
|
|
export const make = Effect.gen(function* () {
|
|
const db = (yield* Database.Service).db
|
|
const state: State = {
|
|
jobs: yield* SynchronizedRef.make(new Map()),
|
|
scope: yield* Scope.Scope,
|
|
}
|
|
|
|
const consume = (id: string, generation: Scope.Closeable) =>
|
|
SynchronizedRef.update(state.jobs, (jobs) => {
|
|
const job = jobs.get(id)
|
|
if (!job || job.scope !== generation || job.info.status === "running" || job.consumed) return jobs
|
|
const next = new Map(jobs)
|
|
// Order history by first consumption, not by start time or subsequent reads.
|
|
next.delete(id)
|
|
next.set(id, { ...job, consumed: true })
|
|
const completed = [...next].filter(([, job]) => job.consumed && !job.info.notificationID)
|
|
for (const [id] of completed.slice(0, -COMPLETED_LIMIT)) next.delete(id)
|
|
return next
|
|
})
|
|
|
|
const persistBackground = Effect.fnUntraced(function* (job: Active) {
|
|
if (!job.recovery || !job.info.notificationID) return
|
|
const now = yield* Clock.currentTimeMillis
|
|
const values = {
|
|
notification_id: job.info.notificationID,
|
|
id: job.info.id,
|
|
session_id: ownerSessionID(job.recovery),
|
|
child_session_id: job.recovery.kind === "subagent" ? job.recovery.childSessionID : null,
|
|
kind: job.recovery.kind,
|
|
title: recoveryTitle(job, job.recovery),
|
|
status: job.info.status,
|
|
output: job.info.output ?? null,
|
|
error: job.info.error ?? null,
|
|
recovery: job.recovery,
|
|
time_created: job.info.started_at,
|
|
time_updated: now,
|
|
}
|
|
yield* db
|
|
.insert(JobTable)
|
|
.values(values)
|
|
.onConflictDoUpdate({
|
|
target: JobTable.notification_id,
|
|
set: {
|
|
status: values.status,
|
|
output: values.output,
|
|
error: values.error,
|
|
recovery: values.recovery,
|
|
time_updated: now,
|
|
},
|
|
})
|
|
.run()
|
|
.pipe(Effect.orDie)
|
|
})
|
|
|
|
const settle = Effect.fnUntraced(function* (id: string, scope: Scope.Closeable, exit: Exit.Exit<string, unknown>) {
|
|
const completed_at = yield* Clock.currentTimeMillis
|
|
const result = yield* SynchronizedRef.modifyEffect(
|
|
state.jobs,
|
|
Effect.fnUntraced(function* (jobs): Effect.fn.Return<readonly [FinishResult, Map<string, Active>]> {
|
|
const job = jobs.get(id)
|
|
if (!job) return [{}, jobs]
|
|
if (job.scope !== scope) return [{}, jobs]
|
|
if (job.info.status !== "running") return [{ info: snapshot(job) }, jobs]
|
|
const status: Exclude<Status, "running"> = Exit.isSuccess(exit)
|
|
? "completed"
|
|
: Cause.hasInterruptsOnly(exit.cause)
|
|
? "cancelled"
|
|
: "error"
|
|
const next = {
|
|
...job,
|
|
blockingSessions: new Map<SessionSchema.ID, number>(),
|
|
info: {
|
|
...job.info,
|
|
status,
|
|
completed_at,
|
|
...(Exit.isSuccess(exit) ? { output: exit.value } : {}),
|
|
...(Exit.isFailure(exit) ? { error: errorText(exit.cause) } : {}),
|
|
},
|
|
}
|
|
if (status !== "cancelled") yield* persistBackground(next)
|
|
return [{ info: snapshot(next), done: job.done, scope: job.scope }, new Map(jobs).set(id, next)]
|
|
}),
|
|
)
|
|
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 }))
|
|
}
|
|
return result.info
|
|
})
|
|
|
|
const get: Interface["get"] = Effect.fn("Job.get")(function* (id) {
|
|
const job = (yield* SynchronizedRef.get(state.jobs)).get(id)
|
|
if (!job) return undefined
|
|
return snapshot(job)
|
|
})
|
|
|
|
const start: Interface["start"] = Effect.fnUntraced(function* (input) {
|
|
return yield* Effect.uninterruptibleMask((restore) =>
|
|
Effect.gen(function* () {
|
|
const started_at = yield* Clock.currentTimeMillis
|
|
const done = yield* Deferred.make<Info>()
|
|
const backgrounded = yield* Deferred.make<Info>()
|
|
const result = yield* SynchronizedRef.modifyEffect(
|
|
state.jobs,
|
|
Effect.fnUntraced(function* (jobs): Effect.fn.Return<readonly [StartResult, Map<string, Active>]> {
|
|
const existing = jobs.get(input.id)
|
|
if (existing?.info.status === "running") {
|
|
return [{ info: snapshot(existing) }, jobs]
|
|
}
|
|
const scope = yield* Scope.fork(state.scope, "parallel")
|
|
const job = {
|
|
info: {
|
|
id: input.id,
|
|
type: input.type,
|
|
title: input.title,
|
|
status: "running" as const,
|
|
started_at,
|
|
metadata: input.metadata,
|
|
...(input.notificationID ? { notificationID: input.notificationID } : {}),
|
|
},
|
|
done,
|
|
backgrounded,
|
|
scope,
|
|
blockingSessions: new Map<SessionSchema.ID, number>(),
|
|
isBackgrounded: false,
|
|
consumed: false,
|
|
recovery: input.recovery,
|
|
}
|
|
return [{ info: snapshot(job), scope }, new Map(jobs).set(input.id, job)]
|
|
}),
|
|
)
|
|
if ("scope" in result)
|
|
yield* restore(input.run).pipe(
|
|
Effect.exit,
|
|
Effect.flatMap((exit) => settle(input.id, result.scope, exit)),
|
|
Effect.asVoid,
|
|
Effect.forkIn(result.scope, { startImmediately: true }),
|
|
)
|
|
return result.info
|
|
}),
|
|
)
|
|
})
|
|
|
|
const wait: Interface["wait"] = Effect.fn("Job.wait")(function* (input) {
|
|
const job = (yield* SynchronizedRef.get(state.jobs)).get(input.id)
|
|
if (!job) return { timedOut: false }
|
|
return yield* Effect.gen(function* () {
|
|
if (job.info.status !== "running") return { info: snapshot(job), timedOut: false }
|
|
if (input.timeout === undefined) return { info: yield* Deferred.await(job.done), timedOut: false }
|
|
if (input.timeout <= 0) return { info: snapshot(job), timedOut: true }
|
|
const info = yield* Deferred.await(job.done).pipe(Effect.timeoutOption(input.timeout))
|
|
if (info._tag === "Some") return { info: info.value, timedOut: false }
|
|
return { info: snapshot(job), timedOut: true }
|
|
}).pipe(
|
|
// Recoverable wait -> background is a supported handoff, even after failure.
|
|
Effect.tap((result) =>
|
|
result.info?.status === "running" || job.recovery ? Effect.void : consume(input.id, job.scope),
|
|
),
|
|
)
|
|
})
|
|
|
|
const removeBlock = Effect.fnUntraced(function* (input: BlockInput) {
|
|
yield* SynchronizedRef.update(state.jobs, (jobs) => {
|
|
const job = jobs.get(input.id)
|
|
if (!job || job.info.status !== "running" || job.isBackgrounded) return jobs
|
|
return new Map(jobs).set(input.id, {
|
|
...job,
|
|
blockingSessions: decrementSession(job.blockingSessions, input.sessionID),
|
|
})
|
|
})
|
|
})
|
|
|
|
const block: Interface["block"] = Effect.fnUntraced(function* (input) {
|
|
const result = yield* SynchronizedRef.modify(state.jobs, (jobs): readonly [BlockStart, Map<string, Active>] => {
|
|
const job = jobs.get(input.id)
|
|
if (!job) return [{ type: "missing" }, jobs]
|
|
if (job.info.status !== "running") return [{ type: "finished", info: snapshot(job), generation: job.scope }, jobs]
|
|
if (job.isBackgrounded) return [{ type: "backgrounded", info: snapshot(job) }, jobs]
|
|
return [
|
|
{ type: "wait", wait: { done: job.done, backgrounded: job.backgrounded, generation: job.scope } },
|
|
new Map(jobs).set(input.id, {
|
|
...job,
|
|
blockingSessions: incrementSession(job.blockingSessions, input.sessionID),
|
|
}),
|
|
]
|
|
})
|
|
if (result.type === "missing") return undefined
|
|
if (result.type === "finished") {
|
|
yield* consume(input.id, result.generation)
|
|
return { type: "finished", info: result.info }
|
|
}
|
|
if (result.type === "backgrounded") return { type: "backgrounded", info: result.info }
|
|
return yield* Effect.raceFirst(
|
|
Deferred.await(result.wait.done).pipe(Effect.map((info) => ({ type: "finished" as const, info }))),
|
|
Deferred.await(result.wait.backgrounded).pipe(Effect.map((info) => ({ type: "backgrounded" as const, info }))),
|
|
).pipe(
|
|
Effect.tap((outcome) => (outcome.type === "finished" ? consume(input.id, result.wait.generation) : Effect.void)),
|
|
Effect.ensuring(removeBlock(input)),
|
|
)
|
|
})
|
|
|
|
const markBackground = Effect.fnUntraced(function* (job: Active) {
|
|
const next = {
|
|
...job,
|
|
isBackgrounded: true,
|
|
blockingSessions: new Map<SessionSchema.ID, number>(),
|
|
info: {
|
|
...job.info,
|
|
...(job.recovery ? { notificationID: job.info.notificationID ?? SessionMessage.ID.create() } : {}),
|
|
},
|
|
}
|
|
yield* persistBackground(next)
|
|
return next
|
|
})
|
|
|
|
const background: Interface["background"] = Effect.fn("Job.background")(function* (id) {
|
|
const result = yield* SynchronizedRef.modifyEffect(
|
|
state.jobs,
|
|
Effect.fnUntraced(function* (jobs): Effect.fn.Return<readonly [BackgroundResult, Map<string, Active>]> {
|
|
const job = jobs.get(id)
|
|
// Recoverable work may finish before the caller backgrounds it.
|
|
if (!job || (job.info.status !== "running" && !job.recovery)) return [{}, jobs]
|
|
if (job.isBackgrounded) return [{ info: snapshot(job) }, jobs]
|
|
const next = yield* markBackground(job)
|
|
return [{ info: snapshot(next), backgrounded: job.backgrounded }, new Map(jobs).set(id, next)]
|
|
}),
|
|
)
|
|
if (result.info && result.backgrounded) yield* Deferred.succeed(result.backgrounded, result.info)
|
|
return result.info
|
|
})
|
|
|
|
const backgroundAll: Interface["backgroundAll"] = Effect.fn("Job.backgroundAll")(function* (input) {
|
|
const result = yield* SynchronizedRef.modifyEffect(
|
|
state.jobs,
|
|
Effect.fnUntraced(function* (jobs): Effect.fn.Return<
|
|
readonly [Required<BackgroundResult>[], Map<string, Active>]
|
|
> {
|
|
const results: Required<BackgroundResult>[] = []
|
|
const next = new Map(jobs)
|
|
for (const [id, job] of jobs) {
|
|
if (job.info.status !== "running") continue
|
|
if (job.isBackgrounded) continue
|
|
if (input.type !== undefined && job.info.type !== input.type) continue
|
|
if (!job.blockingSessions.has(input.sessionID)) continue
|
|
const updated = yield* markBackground(job)
|
|
results.push({ info: snapshot(updated), backgrounded: job.backgrounded })
|
|
next.set(id, updated)
|
|
}
|
|
return [results, next]
|
|
}),
|
|
)
|
|
yield* Effect.forEach(result, (item) => Deferred.succeed(item.backgrounded, item.info), { discard: true })
|
|
return result.map((item) => item.info)
|
|
})
|
|
|
|
const cancelInternal = Effect.fnUntraced(function* (id: string, persist = true) {
|
|
const completed_at = yield* Clock.currentTimeMillis
|
|
const result = yield* SynchronizedRef.modifyEffect(
|
|
state.jobs,
|
|
Effect.fnUntraced(function* (jobs): Effect.fn.Return<readonly [FinishResult, Map<string, Active>]> {
|
|
const job = jobs.get(id)
|
|
if (!job) return [{}, jobs]
|
|
if (job.info.status !== "running") return [{ info: snapshot(job), generation: job.scope }, jobs]
|
|
const next = {
|
|
...job,
|
|
blockingSessions: new Map<SessionSchema.ID, number>(),
|
|
info: {
|
|
...job.info,
|
|
status: "cancelled" as const,
|
|
completed_at,
|
|
},
|
|
}
|
|
if (persist) yield* persistBackground(next)
|
|
return [
|
|
{ info: snapshot(next), done: job.done, scope: job.scope, generation: job.scope },
|
|
new Map(jobs).set(id, next),
|
|
]
|
|
}),
|
|
)
|
|
if (result.info && result.done) yield* Deferred.succeed(result.done, result.info)
|
|
if (result.scope) yield* Scope.close(result.scope, Exit.void)
|
|
if (result.generation) yield* consume(id, result.generation)
|
|
return result.info
|
|
})
|
|
|
|
const cancel: Interface["cancel"] = Effect.fn("Job.cancel")((id) => cancelInternal(id, true))
|
|
|
|
const cancelSession: Interface["cancelSession"] = Effect.fn("Job.cancelSession")(function* (sessionID) {
|
|
const ids = [...(yield* SynchronizedRef.get(state.jobs)).values()]
|
|
.filter((job) => job.info.status === "running" && matchesSession(job, sessionID))
|
|
.map((job) => job.info.id)
|
|
const cancelled = yield* Effect.forEach(ids, (id) => cancelInternal(id, false))
|
|
yield* db
|
|
.delete(JobTable)
|
|
.where(or(eq(JobTable.session_id, sessionID), eq(JobTable.child_session_id, sessionID)))
|
|
.run()
|
|
.pipe(Effect.orDie)
|
|
yield* SynchronizedRef.update(state.jobs, (jobs) => {
|
|
const next = new Map(jobs)
|
|
for (const [id, job] of jobs) {
|
|
if (matchesSession(job, sessionID)) next.delete(id)
|
|
}
|
|
return next
|
|
})
|
|
return cancelled.filter((info): info is Info => info !== undefined)
|
|
})
|
|
|
|
const pendingBackground: Interface["pendingBackground"] = Effect.gen(function* () {
|
|
const rows = yield* db
|
|
.select()
|
|
.from(JobTable)
|
|
.orderBy(asc(JobTable.time_created), asc(JobTable.notification_id))
|
|
.all()
|
|
.pipe(Effect.orDie)
|
|
return Array.filterMap(rows, (row) =>
|
|
decodeBackground({
|
|
id: row.id,
|
|
notificationID: row.notification_id,
|
|
recovery: row.recovery,
|
|
status: row.status,
|
|
...(row.output !== null ? { output: row.output } : {}),
|
|
...(row.error !== null ? { error: row.error } : {}),
|
|
}),
|
|
)
|
|
}).pipe(Effect.withSpan("Job.pendingBackground"))
|
|
|
|
const runningBackground: Interface["runningBackground"] = SynchronizedRef.get(state.jobs).pipe(
|
|
Effect.map((jobs) =>
|
|
[...jobs.values()].flatMap((job) =>
|
|
job.isBackgrounded && job.info.status === "running" && job.recovery ? [job.recovery] : [],
|
|
),
|
|
),
|
|
Effect.withSpan("Job.runningBackground"),
|
|
)
|
|
|
|
const completeBackground: Interface["completeBackground"] = Effect.fn("Job.completeBackground")((notificationID) =>
|
|
SynchronizedRef.updateEffect(state.jobs, (jobs) =>
|
|
Effect.gen(function* () {
|
|
yield* db.delete(JobTable).where(eq(JobTable.notification_id, notificationID)).run().pipe(Effect.orDie)
|
|
const entry = [...jobs].find(([, job]) => job.info.notificationID === notificationID)
|
|
if (!entry || entry[1].info.status === "running") return jobs
|
|
const next = new Map(jobs)
|
|
next.delete(entry[0])
|
|
return next
|
|
}),
|
|
),
|
|
)
|
|
|
|
return Service.of({
|
|
get,
|
|
start,
|
|
wait,
|
|
block,
|
|
background,
|
|
backgroundAll,
|
|
cancel,
|
|
cancelSession,
|
|
pendingBackground,
|
|
runningBackground,
|
|
backgroundJobs: JobList.read(db, state.jobs),
|
|
completeBackground,
|
|
})
|
|
})
|
|
|
|
const layer = Layer.effect(Service, make)
|
|
|
|
export const node = makeGlobalNode({ service: Service, layer, deps: [Database.node] })
|