refactor(core): replace background job service (#34559)
This commit is contained in:
parent
6846542115
commit
461a1c3ab4
24 changed files with 593 additions and 673 deletions
|
|
@ -48,7 +48,7 @@ import { ShareNext } from "@/share/share-next"
|
|||
import { SessionShare } from "@/share/session"
|
||||
import { Npm } from "@opencode-ai/core/npm"
|
||||
import { memoMap } from "@opencode-ai/core/effect/memo-map"
|
||||
import { BackgroundJob } from "@/background/job"
|
||||
import { Job } from "@/job"
|
||||
import { RuntimeFlags } from "@/effect/runtime-flags"
|
||||
import { EventV2Bridge } from "@/event-v2-bridge"
|
||||
|
||||
|
|
@ -74,7 +74,7 @@ export const AppLayer = Layer.mergeAll(
|
|||
Todo.defaultLayer,
|
||||
Session.defaultLayer,
|
||||
SessionStatus.defaultLayer,
|
||||
BackgroundJob.defaultLayer,
|
||||
Job.defaultLayer,
|
||||
RuntimeFlags.defaultLayer,
|
||||
EventV2Bridge.defaultLayer,
|
||||
SessionRunState.defaultLayer,
|
||||
|
|
|
|||
|
|
@ -1,32 +1,34 @@
|
|||
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
|
||||
import { BackgroundJob as CoreBackgroundJob } from "@opencode-ai/core/background-job"
|
||||
import { Service, make } from "@opencode-ai/core/job"
|
||||
import { InstanceState } from "@/effect/instance-state"
|
||||
import { Effect, Layer } from "effect"
|
||||
|
||||
export {
|
||||
Service,
|
||||
type ExtendInput,
|
||||
type BackgroundAllInput,
|
||||
type BlockInput,
|
||||
type BlockResult,
|
||||
type Info,
|
||||
type Interface,
|
||||
type StartInput,
|
||||
type Status,
|
||||
type WaitInput,
|
||||
type WaitResult,
|
||||
} from "@opencode-ai/core/background-job"
|
||||
} from "@opencode-ai/core/job"
|
||||
|
||||
/** Keeps the legacy service instance-scoped while sharing the core registry engine. */
|
||||
export const layer = Layer.effect(
|
||||
CoreBackgroundJob.Service,
|
||||
Service,
|
||||
Effect.gen(function* () {
|
||||
const state = yield* InstanceState.make(() => CoreBackgroundJob.make)
|
||||
return CoreBackgroundJob.Service.of({
|
||||
const state = yield* InstanceState.make(() => make)
|
||||
return Service.of({
|
||||
list: () => InstanceState.useEffect(state, (jobs) => jobs.list()),
|
||||
get: (id) => InstanceState.useEffect(state, (jobs) => jobs.get(id)),
|
||||
start: (input) => InstanceState.useEffect(state, (jobs) => jobs.start(input)),
|
||||
extend: (input) => InstanceState.useEffect(state, (jobs) => jobs.extend(input)),
|
||||
wait: (input) => InstanceState.useEffect(state, (jobs) => jobs.wait(input)),
|
||||
waitForPromotion: (id) => InstanceState.useEffect(state, (jobs) => jobs.waitForPromotion(id)),
|
||||
promote: (id) => InstanceState.useEffect(state, (jobs) => jobs.promote(id)),
|
||||
block: (input) => InstanceState.useEffect(state, (jobs) => jobs.block(input)),
|
||||
background: (id) => InstanceState.useEffect(state, (jobs) => jobs.background(id)),
|
||||
backgroundAll: (input) => InstanceState.useEffect(state, (jobs) => jobs.backgroundAll(input)),
|
||||
cancel: (id) => InstanceState.useEffect(state, (jobs) => jobs.cancel(id)),
|
||||
})
|
||||
}),
|
||||
|
|
@ -34,6 +36,6 @@ export const layer = Layer.effect(
|
|||
|
||||
export const defaultLayer = layer
|
||||
|
||||
export const node = LayerNode.make({ service: CoreBackgroundJob.Service, layer, deps: [] })
|
||||
export const node = LayerNode.make({ service: Service, layer, deps: [] })
|
||||
|
||||
export * as BackgroundJob from "./job"
|
||||
export * as Job from "./job"
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
import { Account } from "@/account/account"
|
||||
import { Agent } from "@/agent/agent"
|
||||
import { BackgroundJob } from "@/background/job"
|
||||
import { Job } from "@/job"
|
||||
import { Config } from "@/config/config"
|
||||
import { InstanceState } from "@/effect/instance-state"
|
||||
import { RuntimeFlags } from "@/effect/runtime-flags"
|
||||
|
|
@ -33,7 +33,7 @@ export const experimentalHandlers = HttpApiBuilder.group(InstanceHttpApi, "exper
|
|||
const registry = yield* ToolRegistry.Service
|
||||
const worktreeSvc = yield* Worktree.Service
|
||||
const sessions = yield* Session.Service
|
||||
const background = yield* BackgroundJob.Service
|
||||
const jobs = yield* Job.Service
|
||||
const flags = yield* RuntimeFlags.Service
|
||||
|
||||
const capabilities = Effect.fn("ExperimentalHttpApi.capabilities")(function* () {
|
||||
|
|
@ -159,15 +159,7 @@ export const experimentalHandlers = HttpApiBuilder.group(InstanceHttpApi, "exper
|
|||
params: { sessionID: SessionID }
|
||||
}) {
|
||||
if (!flags.experimentalBackgroundSubagents) return false
|
||||
const jobs = (yield* background.list()).filter(
|
||||
(job) =>
|
||||
job.type === "task" &&
|
||||
job.status === "running" &&
|
||||
job.metadata?.parentSessionId === ctx.params.sessionID &&
|
||||
job.metadata.background !== true,
|
||||
)
|
||||
const promoted = yield* Effect.forEach(jobs, (job) => background.promote(job.id), { concurrency: "unbounded" })
|
||||
return promoted.some((job) => job !== undefined)
|
||||
return (yield* jobs.backgroundAll({ sessionID: ctx.params.sessionID, type: "task" })).length > 0
|
||||
})
|
||||
|
||||
const resource = Effect.fn("ExperimentalHttpApi.resource")(function* () {
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import * as Observability from "@opencode-ai/core/observability"
|
|||
import { Account } from "@/account/account"
|
||||
import { Agent } from "@/agent/agent"
|
||||
import { Auth } from "@/auth"
|
||||
import { BackgroundJob } from "@/background/job"
|
||||
import { Job } from "@/job"
|
||||
import { Command } from "@/command"
|
||||
import { Config } from "@/config/config"
|
||||
import { Workspace } from "@/control-plane/workspace"
|
||||
|
|
@ -233,7 +233,7 @@ const app = LayerNode.group([
|
|||
Session.node,
|
||||
SessionProjector.node,
|
||||
SessionStatus.node,
|
||||
BackgroundJob.node,
|
||||
Job.node,
|
||||
RuntimeFlags.node,
|
||||
EventV2Bridge.node,
|
||||
SessionRunState.node,
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import { LayerNode } from "@opencode-ai/core/effect/layer-node"
|
|||
import { InstanceState } from "@/effect/instance-state"
|
||||
import { SessionV1 } from "@opencode-ai/core/v1/session"
|
||||
import { Runner } from "@/effect/runner"
|
||||
import { BackgroundJob } from "@/background/job"
|
||||
import { Job } from "@/job"
|
||||
import { Effect, Latch, Layer, Scope, Context } from "effect"
|
||||
import { Session } from "./session"
|
||||
import { SessionID } from "./schema"
|
||||
|
|
@ -29,7 +29,7 @@ export class Service extends Context.Service<Service, Interface>()("@opencode/Se
|
|||
export const layer = Layer.effect(
|
||||
Service,
|
||||
Effect.gen(function* () {
|
||||
const background = yield* BackgroundJob.Service
|
||||
const jobs = yield* Job.Service
|
||||
const status = yield* SessionStatus.Service
|
||||
|
||||
const state = yield* InstanceState.make(
|
||||
|
|
@ -75,7 +75,7 @@ export const layer = Layer.effect(
|
|||
})
|
||||
|
||||
const cancel = Effect.fn("SessionRunState.cancel")(function* (sessionID: SessionID) {
|
||||
yield* cancelBackgroundJobs(background, sessionID)
|
||||
yield* cancelJobs(jobs, sessionID)
|
||||
const data = yield* InstanceState.get(state)
|
||||
const existing = data.runners.get(sessionID)
|
||||
if (!existing) {
|
||||
|
|
@ -108,31 +108,25 @@ export const layer = Layer.effect(
|
|||
}),
|
||||
)
|
||||
|
||||
export const defaultLayer = layer.pipe(
|
||||
Layer.provide(BackgroundJob.defaultLayer),
|
||||
Layer.provide(SessionStatus.defaultLayer),
|
||||
)
|
||||
export const defaultLayer = layer.pipe(Layer.provide(Job.defaultLayer), Layer.provide(SessionStatus.defaultLayer))
|
||||
|
||||
const cancelBackgroundJobs = Effect.fn("SessionRunState.cancelBackgroundJobs")(function* (
|
||||
background: BackgroundJob.Interface,
|
||||
sessionID: SessionID,
|
||||
) {
|
||||
const jobs = yield* background.list()
|
||||
const cancelJobs = Effect.fn("SessionRunState.cancelJobs")(function* (jobs: Job.Interface, sessionID: SessionID) {
|
||||
const running = yield* jobs.list()
|
||||
const pending = new Set<string>([sessionID])
|
||||
const cancelled = new Set<string>()
|
||||
const matches = (job: BackgroundJob.Info) => {
|
||||
const matches = (job: Job.Info) => {
|
||||
if (job.status !== "running") return false
|
||||
if (cancelled.has(job.id)) return false
|
||||
if (pending.has(job.id)) return true
|
||||
if (typeof job.metadata?.sessionId === "string" && pending.has(job.metadata.sessionId)) return true
|
||||
return typeof job.metadata?.parentSessionId === "string" && pending.has(job.metadata.parentSessionId)
|
||||
}
|
||||
let batch = jobs.filter(matches)
|
||||
let batch = running.filter(matches)
|
||||
while (batch.length > 0) {
|
||||
yield* Effect.forEach(
|
||||
batch,
|
||||
(job) =>
|
||||
background.cancel(job.id).pipe(
|
||||
jobs.cancel(job.id).pipe(
|
||||
Effect.tap(() =>
|
||||
Effect.sync(() => {
|
||||
cancelled.add(job.id)
|
||||
|
|
@ -143,7 +137,7 @@ const cancelBackgroundJobs = Effect.fn("SessionRunState.cancelBackgroundJobs")(f
|
|||
),
|
||||
{ concurrency: "unbounded", discard: true },
|
||||
)
|
||||
batch = jobs.filter(matches)
|
||||
batch = running.filter(matches)
|
||||
}
|
||||
})
|
||||
|
||||
|
|
@ -151,6 +145,6 @@ function busyError(sessionID: SessionID) {
|
|||
return new Session.BusyError({ sessionID })
|
||||
}
|
||||
|
||||
export const node = LayerNode.make({ service: Service, layer: layer, deps: [BackgroundJob.node, SessionStatus.node] })
|
||||
export const node = LayerNode.make({ service: Service, layer: layer, deps: [Job.node, SessionStatus.node] })
|
||||
|
||||
export * as SessionRunState from "./run-state"
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import { Slug } from "@opencode-ai/core/util/slug"
|
|||
import { SessionV1 } from "@opencode-ai/core/v1/session"
|
||||
import { serviceUse } from "@opencode-ai/core/effect/service-use"
|
||||
import path from "path"
|
||||
import { BackgroundJob } from "@/background/job"
|
||||
import { Job } from "@/job"
|
||||
import { Decimal } from "decimal.js"
|
||||
import type { ProviderMetadata, Usage } from "@opencode-ai/llm"
|
||||
import { InstallationVersion } from "@opencode-ai/core/installation/version"
|
||||
|
|
@ -491,13 +491,13 @@ export type Patch = Omit<Partial<Info>, "time" | "share" | "summary" | "revert"
|
|||
export const layer: Layer.Layer<
|
||||
Service,
|
||||
never,
|
||||
BackgroundJob.Service | RuntimeFlags.Service | Database.Service | EventV2Bridge.Service
|
||||
Job.Service | RuntimeFlags.Service | Database.Service | EventV2Bridge.Service
|
||||
> = Layer.effect(
|
||||
Service,
|
||||
Effect.gen(function* () {
|
||||
const { db } = yield* Database.Service
|
||||
const database = yield* Database.Service
|
||||
const background = yield* BackgroundJob.Service
|
||||
const jobs = yield* Job.Service
|
||||
const events = yield* EventV2Bridge.Service
|
||||
const flags = yield* RuntimeFlags.Service
|
||||
|
||||
|
|
@ -618,7 +618,7 @@ export const layer: Layer.Layer<
|
|||
Effect.catchCause(() => Effect.succeed(false)),
|
||||
)
|
||||
|
||||
if (hasInstance) yield* cancelBackgroundJobs(background, sessionID)
|
||||
if (hasInstance) yield* cancelJobs(jobs, sessionID)
|
||||
const kids = yield* children(sessionID)
|
||||
for (const child of kids) {
|
||||
yield* remove(child.id)
|
||||
|
|
@ -941,7 +941,7 @@ export const layer: Layer.Layer<
|
|||
)
|
||||
|
||||
export const defaultLayer = layer.pipe(
|
||||
Layer.provide(BackgroundJob.defaultLayer),
|
||||
Layer.provide(Job.defaultLayer),
|
||||
Layer.provide(Database.defaultLayer),
|
||||
Layer.provide(EventV2Bridge.defaultLayer),
|
||||
Layer.provide(
|
||||
|
|
@ -953,19 +953,16 @@ export const defaultLayer = layer.pipe(
|
|||
Layer.provide(RuntimeFlags.defaultLayer),
|
||||
)
|
||||
|
||||
const cancelBackgroundJobs = Effect.fn("Session.cancelBackgroundJobs")(function* (
|
||||
background: BackgroundJob.Interface,
|
||||
sessionID: SessionID,
|
||||
) {
|
||||
const jobs = yield* background.list()
|
||||
const cancelJobs = Effect.fn("Session.cancelJobs")(function* (jobs: Job.Interface, sessionID: SessionID) {
|
||||
const running = yield* jobs.list()
|
||||
yield* Effect.forEach(
|
||||
jobs.filter((job) => {
|
||||
running.filter((job) => {
|
||||
if (job.status !== "running") return false
|
||||
if (job.id === sessionID) return true
|
||||
if (job.metadata?.sessionId === sessionID) return true
|
||||
return job.metadata?.parentSessionId === sessionID
|
||||
}),
|
||||
(job) => background.cancel(job.id),
|
||||
(job) => jobs.cancel(job.id),
|
||||
{ concurrency: "unbounded", discard: true },
|
||||
)
|
||||
})
|
||||
|
|
@ -1098,7 +1095,7 @@ export function* listGlobal(input?: {
|
|||
export const node = LayerNode.make({
|
||||
service: Service,
|
||||
layer: layer,
|
||||
deps: [BackgroundJob.node, RuntimeFlags.node, Database.node, EventV2Bridge.node],
|
||||
deps: [Job.node, RuntimeFlags.node, Database.node, EventV2Bridge.node],
|
||||
})
|
||||
|
||||
export * as Session from "./session"
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ import { EventV2Bridge } from "@/event-v2-bridge"
|
|||
import { Agent } from "../agent/agent"
|
||||
import { Skill } from "../skill"
|
||||
import { Permission } from "@/permission"
|
||||
import { BackgroundJob } from "@/background/job"
|
||||
import { Job } from "@/job"
|
||||
import { RuntimeFlags } from "@/effect/runtime-flags"
|
||||
import { ProviderV2 } from "@opencode-ai/core/provider"
|
||||
import { ModelV2 } from "@opencode-ai/core/model"
|
||||
|
|
@ -325,7 +325,7 @@ export const defaultLayer = Layer.suspend(() =>
|
|||
Layer.provide(Skill.defaultLayer),
|
||||
Layer.provide(Agent.defaultLayer),
|
||||
Layer.provide(Session.defaultLayer),
|
||||
Layer.provide(BackgroundJob.defaultLayer),
|
||||
Layer.provide(Job.defaultLayer),
|
||||
Layer.provide(Provider.defaultLayer),
|
||||
Layer.provide(LSP.defaultLayer),
|
||||
Layer.provide(Instruction.defaultLayer),
|
||||
|
|
@ -426,7 +426,7 @@ export const node = LayerNode.make({
|
|||
Agent.node,
|
||||
Skill.node,
|
||||
Session.node,
|
||||
BackgroundJob.node,
|
||||
Job.node,
|
||||
Provider.node,
|
||||
LSP.node,
|
||||
Instruction.node,
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import * as Tool from "./tool"
|
|||
import DESCRIPTION from "./task.txt"
|
||||
import { ToolJsonSchema } from "./json-schema"
|
||||
import { SessionV1 } from "@opencode-ai/core/v1/session"
|
||||
import { BackgroundJob } from "@/background/job"
|
||||
import { Job } from "@/job"
|
||||
import { Session } from "@/session/session"
|
||||
import { SessionID, MessageID } from "../session/schema"
|
||||
import { MessageV2 } from "../session/message-v2"
|
||||
|
|
@ -33,11 +33,11 @@ const BACKGROUND_STARTED = [
|
|||
"DO NOT sleep, poll for progress, ask the task for status, or duplicate this task's work — avoid working with the same files or topics it is using.",
|
||||
"Work on non-overlapping tasks, or briefly tell the user what you launched and end your response.",
|
||||
].join("\n")
|
||||
const BACKGROUND_UPDATED = [
|
||||
"Additional context sent to the running background task.",
|
||||
const BACKGROUND_ALREADY_RUNNING = [
|
||||
"The task is already working in the background.",
|
||||
"The task is still working in the background. You will be notified automatically when it finishes.",
|
||||
"DO NOT sleep, poll for progress, ask the task for status, or duplicate this task's work — avoid working with the same files or topics it is using.",
|
||||
"Work on non-overlapping tasks, or briefly tell the user what you sent and end your response.",
|
||||
"Work on non-overlapping tasks, or briefly tell the user it is still running and end your response.",
|
||||
].join("\n")
|
||||
|
||||
const BaseParameterFields = {
|
||||
|
|
@ -82,7 +82,7 @@ export const TaskTool = Tool.define(
|
|||
id,
|
||||
Effect.gen(function* () {
|
||||
const agent = yield* Agent.Service
|
||||
const background = yield* BackgroundJob.Service
|
||||
const jobs = yield* Job.Service
|
||||
const config = yield* Config.Service
|
||||
const sessions = yield* Session.Service
|
||||
const scope = yield* Scope.Scope
|
||||
|
|
@ -229,7 +229,7 @@ export const TaskTool = Tool.define(
|
|||
})
|
||||
|
||||
const notify = Effect.fn("TaskTool.notifyBackgroundResult")(function* (jobID: string) {
|
||||
yield* background.wait({ id: jobID }).pipe(
|
||||
yield* jobs.wait({ id: jobID }).pipe(
|
||||
Effect.flatMap((result) => {
|
||||
if (result.info?.status === "completed") return inject("completed", result.info.output ?? "")
|
||||
if (result.info?.status === "error") return inject("error", result.info.error ?? "")
|
||||
|
|
@ -239,7 +239,8 @@ export const TaskTool = Tool.define(
|
|||
)
|
||||
})
|
||||
|
||||
if (yield* background.extend({ id: nextSession.id, run: runTask() })) {
|
||||
const existing = yield* jobs.get(nextSession.id)
|
||||
if (existing?.status === "running") {
|
||||
return {
|
||||
title: params.description,
|
||||
metadata: {
|
||||
|
|
@ -250,24 +251,17 @@ export const TaskTool = Tool.define(
|
|||
output: renderOutput({
|
||||
sessionID: nextSession.id,
|
||||
state: "running",
|
||||
summary: "Background task updated",
|
||||
text: BACKGROUND_UPDATED,
|
||||
summary: "Background task already running",
|
||||
text: BACKGROUND_ALREADY_RUNNING,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
const info = yield* background.start({
|
||||
const info = yield* jobs.start({
|
||||
id: nextSession.id,
|
||||
type: id,
|
||||
title: params.description,
|
||||
metadata,
|
||||
onPromote: Effect.all([
|
||||
ctx.metadata({
|
||||
title: params.description,
|
||||
metadata: { ...metadata, background: true, jobId: nextSession.id },
|
||||
}),
|
||||
notify(nextSession.id),
|
||||
]),
|
||||
run: runTask().pipe(Effect.onInterrupt(() => ops.cancel(nextSession.id))),
|
||||
})
|
||||
|
||||
|
|
@ -289,6 +283,7 @@ export const TaskTool = Tool.define(
|
|||
}
|
||||
|
||||
if (runInBackground) {
|
||||
yield* jobs.background(info.id)
|
||||
yield* notify(info.id)
|
||||
return backgroundResult()
|
||||
}
|
||||
|
|
@ -306,23 +301,27 @@ export const TaskTool = Tool.define(
|
|||
}),
|
||||
() =>
|
||||
Effect.gen(function* () {
|
||||
const result = yield* Effect.raceFirst(
|
||||
background.wait({ id: nextSession.id }).pipe(Effect.map((waited) => waited.info)),
|
||||
background.waitForPromotion(nextSession.id),
|
||||
)
|
||||
if (result?.metadata?.background === true) return backgroundResult()
|
||||
if (result?.status === "error") return yield* Effect.fail(new Error(result.error ?? "Task failed"))
|
||||
if (result?.status === "cancelled") return yield* Effect.fail(new Error("Task cancelled"))
|
||||
const result = yield* jobs.block({ id: nextSession.id, sessionID: ctx.sessionID })
|
||||
if (result?.type === "backgrounded") {
|
||||
yield* ctx.metadata({
|
||||
title: params.description,
|
||||
metadata: { ...metadata, background: true, jobId: nextSession.id },
|
||||
})
|
||||
yield* notify(nextSession.id)
|
||||
return backgroundResult()
|
||||
}
|
||||
if (result?.info.status === "error")
|
||||
return yield* Effect.fail(new Error(result.info.error ?? "Task failed"))
|
||||
if (result?.info.status === "cancelled") return yield* Effect.fail(new Error("Task cancelled"))
|
||||
return {
|
||||
title: params.description,
|
||||
metadata,
|
||||
output: renderOutput({ sessionID: nextSession.id, state: "completed", text: result?.output ?? "" }),
|
||||
output: renderOutput({ sessionID: nextSession.id, state: "completed", text: result?.info.output ?? "" }),
|
||||
}
|
||||
}),
|
||||
(_, exit) =>
|
||||
Effect.gen(function* () {
|
||||
if (Exit.hasInterrupts(exit))
|
||||
yield* Effect.all([cancel, background.cancel(nextSession.id)], { discard: true })
|
||||
if (Exit.hasInterrupts(exit)) yield* Effect.all([cancel, jobs.cancel(nextSession.id)], { discard: true })
|
||||
}).pipe(
|
||||
Effect.ensuring(
|
||||
Effect.sync(() => {
|
||||
|
|
|
|||
|
|
@ -172,7 +172,7 @@ Wait on a **published readiness signal**, not wall-clock time. Available afforda
|
|||
- `awaitWithTimeout(effect, message, duration?)` from `test/lib/effect.ts` — wrap any effect with `Effect.timeoutOrElse` and a custom error message.
|
||||
- `llm.wait(n)` from `test/lib/llm-server.ts` — wait until the mock LLM has received `n` HTTP calls.
|
||||
- `SessionStatus.Service` `.get(sessionID)` — observable per-session state (`{ type: "busy" | "idle" | ... }`).
|
||||
- `BackgroundJob.wait({ id, timeout })` from `src/background/job.ts` — wait for a background job to complete.
|
||||
- `Job.wait({ id, timeout })` from `src/job.ts` — wait for a job to complete.
|
||||
- Bus subscriptions — fork `Stream.runForEach(bus.subscribe(Event), ...)` and open a `Latch` inside the callback to signal first-event readiness.
|
||||
- `Deferred.await(deferred).pipe(Effect.timeoutOrElse(...))` for one-shot signals.
|
||||
|
||||
|
|
|
|||
|
|
@ -1,243 +0,0 @@
|
|||
import { describe, expect } from "bun:test"
|
||||
import { Deferred, Effect } from "effect"
|
||||
import { BackgroundJob } from "@/background/job"
|
||||
import { testEffect } from "../lib/effect"
|
||||
|
||||
const it = testEffect(BackgroundJob.defaultLayer)
|
||||
|
||||
describe("background.job", () => {
|
||||
it.instance("tracks started jobs through completion", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* BackgroundJob.Service
|
||||
const latch = yield* Deferred.make<void>()
|
||||
const job = yield* jobs.start({
|
||||
type: "test",
|
||||
title: "test job",
|
||||
run: Deferred.await(latch).pipe(Effect.as("done")),
|
||||
})
|
||||
|
||||
expect(job.id.startsWith("job_")).toBe(true)
|
||||
expect(job.status).toBe("running")
|
||||
expect(job.title).toBe("test job")
|
||||
|
||||
yield* Deferred.succeed(latch, undefined)
|
||||
const done = yield* jobs.wait({ id: job.id })
|
||||
|
||||
expect(done.timedOut).toBe(false)
|
||||
expect(done.info?.status).toBe("completed")
|
||||
expect(done.info?.output).toBe("done")
|
||||
expect((yield* jobs.list()).map((item) => item.id)).toEqual([job.id])
|
||||
}),
|
||||
)
|
||||
|
||||
it.instance("returns a running snapshot when wait times out", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* BackgroundJob.Service
|
||||
const job = yield* jobs.start({
|
||||
type: "test",
|
||||
run: Effect.never,
|
||||
})
|
||||
|
||||
const result = yield* jobs.wait({ id: job.id, timeout: 1 })
|
||||
|
||||
expect(result.timedOut).toBe(true)
|
||||
expect(result.info?.status).toBe("running")
|
||||
}),
|
||||
)
|
||||
|
||||
it.instance("deduplicates concurrent starts for a running id", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* BackgroundJob.Service
|
||||
const started = yield* Deferred.make<void>()
|
||||
const id = "job_test"
|
||||
const [first, second] = yield* Effect.all(
|
||||
[
|
||||
jobs.start({
|
||||
id,
|
||||
type: "test",
|
||||
run: Deferred.succeed(started, undefined).pipe(Effect.andThen(Effect.never)),
|
||||
}),
|
||||
jobs.start({
|
||||
id,
|
||||
type: "test",
|
||||
run: Effect.fail(new Error("duplicate started")),
|
||||
}),
|
||||
],
|
||||
{ concurrency: "unbounded" },
|
||||
)
|
||||
|
||||
yield* Deferred.await(started)
|
||||
|
||||
expect(first.id).toBe(id)
|
||||
expect(second.id).toBe(id)
|
||||
expect(first.status).toBe("running")
|
||||
expect(second.status).toBe("running")
|
||||
expect((yield* jobs.list()).map((item) => item.id)).toEqual([id])
|
||||
|
||||
yield* jobs.cancel(id)
|
||||
}),
|
||||
)
|
||||
|
||||
it.instance("waits for extensions before completing a running job", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* BackgroundJob.Service
|
||||
const first = yield* Deferred.make<void>()
|
||||
const second = yield* Deferred.make<void>()
|
||||
const job = yield* jobs.start({
|
||||
type: "test",
|
||||
run: Deferred.await(first).pipe(Effect.as("first")),
|
||||
})
|
||||
|
||||
expect(yield* jobs.extend({ id: job.id, run: Deferred.await(second).pipe(Effect.as("second")) })).toBe(true)
|
||||
yield* Deferred.succeed(first, undefined)
|
||||
expect((yield* jobs.get(job.id))?.status).toBe("running")
|
||||
|
||||
yield* Deferred.succeed(second, undefined)
|
||||
const done = yield* jobs.wait({ id: job.id })
|
||||
expect(done.info?.status).toBe("completed")
|
||||
expect(done.info?.output).toBe("second")
|
||||
}),
|
||||
)
|
||||
|
||||
it.instance("runs extensions after earlier work completes", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* BackgroundJob.Service
|
||||
const first = yield* Deferred.make<void>()
|
||||
const order: string[] = []
|
||||
const job = yield* jobs.start({
|
||||
type: "test",
|
||||
run: Effect.sync(() => order.push("start")).pipe(Effect.andThen(Deferred.await(first)), Effect.as("first")),
|
||||
})
|
||||
|
||||
expect(
|
||||
yield* jobs.extend({
|
||||
id: job.id,
|
||||
run: Effect.sync(() => order.push("extend")).pipe(Effect.as("second")),
|
||||
}),
|
||||
).toBe(true)
|
||||
yield* Effect.yieldNow
|
||||
expect(order).toEqual(["start"])
|
||||
|
||||
yield* Deferred.succeed(first, undefined)
|
||||
expect((yield* jobs.wait({ id: job.id })).info?.output).toBe("second")
|
||||
expect(order).toEqual(["start", "extend"])
|
||||
}),
|
||||
)
|
||||
|
||||
it.instance("rejects extensions after a job completes", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* BackgroundJob.Service
|
||||
const job = yield* jobs.start({ type: "test", run: Effect.succeed("done") })
|
||||
yield* jobs.wait({ id: job.id })
|
||||
|
||||
expect(yield* jobs.extend({ id: job.id, run: Effect.succeed("late") })).toBe(false)
|
||||
expect((yield* jobs.get(job.id))?.output).toBe("done")
|
||||
}),
|
||||
)
|
||||
|
||||
it.instance("records failed jobs", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* BackgroundJob.Service
|
||||
const job = yield* jobs.start({
|
||||
type: "test",
|
||||
run: Effect.fail(new Error("boom")),
|
||||
})
|
||||
|
||||
const result = yield* jobs.wait({ id: job.id })
|
||||
|
||||
expect(result.info?.status).toBe("error")
|
||||
expect(result.info?.error).toBe("boom")
|
||||
}),
|
||||
)
|
||||
|
||||
it.instance("ignores stale settlements after restarting a failed job", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* BackgroundJob.Service
|
||||
const fail = yield* Deferred.make<void>()
|
||||
const interrupted = yield* Deferred.make<void>()
|
||||
const release = yield* Deferred.make<void>()
|
||||
const id = "job_test"
|
||||
yield* jobs.start({
|
||||
id,
|
||||
type: "test",
|
||||
run: Deferred.await(fail).pipe(Effect.andThen(Effect.fail(new Error("boom")))),
|
||||
})
|
||||
yield* jobs.extend({
|
||||
id,
|
||||
run: Effect.never.pipe(
|
||||
Effect.ensuring(Deferred.succeed(interrupted, undefined).pipe(Effect.andThen(Deferred.await(release)))),
|
||||
),
|
||||
})
|
||||
|
||||
yield* Deferred.succeed(fail, undefined)
|
||||
expect((yield* jobs.wait({ id })).info?.status).toBe("error")
|
||||
yield* Deferred.await(interrupted)
|
||||
yield* jobs.start({ id, type: "test", run: Effect.never })
|
||||
|
||||
yield* Deferred.succeed(release, undefined)
|
||||
yield* Effect.yieldNow
|
||||
expect((yield* jobs.get(id))?.status).toBe("running")
|
||||
yield* jobs.cancel(id)
|
||||
}),
|
||||
)
|
||||
|
||||
it.instance("can cancel running jobs", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* BackgroundJob.Service
|
||||
const interrupted = yield* Deferred.make<void>()
|
||||
const job = yield* jobs.start({
|
||||
type: "test",
|
||||
run: Effect.never.pipe(Effect.ensuring(Deferred.succeed(interrupted, undefined))),
|
||||
})
|
||||
yield* jobs.extend({
|
||||
id: job.id,
|
||||
run: Effect.never,
|
||||
})
|
||||
|
||||
const cancelled = yield* jobs.cancel(job.id)
|
||||
|
||||
expect(cancelled?.status).toBe("cancelled")
|
||||
yield* Deferred.await(interrupted).pipe(Effect.timeout("1 second"))
|
||||
expect((yield* jobs.get(job.id))?.status).toBe("cancelled")
|
||||
}),
|
||||
)
|
||||
|
||||
it.instance("promotes running jobs without interrupting them", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* BackgroundJob.Service
|
||||
const latch = yield* Deferred.make<void>()
|
||||
const promoted = yield* Deferred.make<void>()
|
||||
const job = yield* jobs.start({
|
||||
type: "test",
|
||||
metadata: { parentSessionId: "parent" },
|
||||
onPromote: Deferred.succeed(promoted, undefined).pipe(Effect.asVoid),
|
||||
run: Deferred.await(latch).pipe(Effect.as("done")),
|
||||
})
|
||||
|
||||
const info = yield* jobs.promote(job.id)
|
||||
|
||||
expect(info?.status).toBe("running")
|
||||
expect(info?.metadata?.background).toBe(true)
|
||||
yield* Deferred.await(promoted)
|
||||
expect((yield* jobs.get(job.id))?.status).toBe("running")
|
||||
|
||||
yield* Deferred.succeed(latch, undefined)
|
||||
expect((yield* jobs.wait({ id: job.id })).info?.output).toBe("done")
|
||||
}),
|
||||
)
|
||||
|
||||
it.instance("returns immutable snapshots", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* BackgroundJob.Service
|
||||
const job = yield* jobs.start({
|
||||
type: "test",
|
||||
metadata: { value: "initial" },
|
||||
run: Effect.succeed("done"),
|
||||
})
|
||||
|
||||
if (job.metadata) job.metadata.value = "changed"
|
||||
|
||||
expect((yield* jobs.get(job.id))?.metadata?.value).toBe("initial")
|
||||
}),
|
||||
)
|
||||
})
|
||||
131
packages/opencode/test/job.test.ts
Normal file
131
packages/opencode/test/job.test.ts
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
import { describe, expect } from "bun:test"
|
||||
import { Deferred, Effect, Fiber } from "effect"
|
||||
import { Job } from "@/job"
|
||||
import { SessionSchema } from "@opencode-ai/core/session/schema"
|
||||
import { testEffect } from "./lib/effect"
|
||||
|
||||
const it = testEffect(Job.defaultLayer)
|
||||
|
||||
describe("job", () => {
|
||||
it.instance("tracks started jobs through completion", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* Job.Service
|
||||
const latch = yield* Deferred.make<void>()
|
||||
const job = yield* jobs.start({
|
||||
type: "test",
|
||||
title: "test job",
|
||||
run: Deferred.await(latch).pipe(Effect.as("done")),
|
||||
})
|
||||
|
||||
expect(job.id.startsWith("job_")).toBe(true)
|
||||
expect(job.status).toBe("running")
|
||||
expect(job.title).toBe("test job")
|
||||
|
||||
yield* Deferred.succeed(latch, undefined)
|
||||
const done = yield* jobs.wait({ id: job.id })
|
||||
|
||||
expect(done.timedOut).toBe(false)
|
||||
expect(done.info?.status).toBe("completed")
|
||||
expect(done.info?.output).toBe("done")
|
||||
expect((yield* jobs.list()).map((item) => item.id)).toEqual([job.id])
|
||||
}),
|
||||
)
|
||||
|
||||
it.instance("returns a running snapshot when wait times out", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* Job.Service
|
||||
const job = yield* jobs.start({ type: "test", run: Effect.never })
|
||||
|
||||
const result = yield* jobs.wait({ id: job.id, timeout: 1 })
|
||||
|
||||
expect(result.timedOut).toBe(true)
|
||||
expect(result.info?.status).toBe("running")
|
||||
}),
|
||||
)
|
||||
|
||||
it.instance("deduplicates concurrent starts for a running id", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* Job.Service
|
||||
const started = yield* Deferred.make<void>()
|
||||
const id = "job_test"
|
||||
const [first, second] = yield* Effect.all(
|
||||
[
|
||||
jobs.start({
|
||||
id,
|
||||
type: "test",
|
||||
run: Deferred.succeed(started, undefined).pipe(Effect.andThen(Effect.never)),
|
||||
}),
|
||||
jobs.start({ id, type: "test", run: Effect.fail(new Error("duplicate started")) }),
|
||||
],
|
||||
{ concurrency: "unbounded" },
|
||||
)
|
||||
|
||||
yield* Deferred.await(started)
|
||||
|
||||
expect(first.id).toBe(id)
|
||||
expect(second.id).toBe(id)
|
||||
expect(first.status).toBe("running")
|
||||
expect(second.status).toBe("running")
|
||||
expect((yield* jobs.list()).map((item) => item.id)).toEqual([id])
|
||||
|
||||
yield* jobs.cancel(id)
|
||||
}),
|
||||
)
|
||||
|
||||
it.instance("records failed jobs", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* Job.Service
|
||||
const job = yield* jobs.start({ type: "test", run: Effect.fail(new Error("boom")) })
|
||||
|
||||
const result = yield* jobs.wait({ id: job.id })
|
||||
|
||||
expect(result.info?.status).toBe("error")
|
||||
expect(result.info?.error).toBe("boom")
|
||||
}),
|
||||
)
|
||||
|
||||
it.instance("can cancel running jobs", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* Job.Service
|
||||
const interrupted = yield* Deferred.make<void>()
|
||||
const job = yield* jobs.start({
|
||||
type: "test",
|
||||
run: Effect.never.pipe(Effect.ensuring(Deferred.succeed(interrupted, undefined))),
|
||||
})
|
||||
|
||||
const cancelled = yield* jobs.cancel(job.id)
|
||||
|
||||
expect(cancelled?.status).toBe("cancelled")
|
||||
yield* Deferred.await(interrupted).pipe(Effect.timeout("1 second"))
|
||||
expect((yield* jobs.get(job.id))?.status).toBe("cancelled")
|
||||
}),
|
||||
)
|
||||
|
||||
it.instance("releases blocking waits when backgrounded", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* Job.Service
|
||||
const latch = yield* Deferred.make<void>()
|
||||
const job = yield* jobs.start({ type: "test", run: Deferred.await(latch).pipe(Effect.as("done")) })
|
||||
const waiting = yield* jobs
|
||||
.block({ id: job.id, sessionID: SessionSchema.ID.make("ses_parent") })
|
||||
.pipe(Effect.forkChild)
|
||||
|
||||
expect(yield* jobs.background(job.id)).toMatchObject({ id: job.id, status: "running" })
|
||||
expect(yield* Fiber.join(waiting)).toMatchObject({ type: "backgrounded", info: { id: job.id } })
|
||||
|
||||
yield* Deferred.succeed(latch, undefined)
|
||||
expect((yield* jobs.wait({ id: job.id })).info?.output).toBe("done")
|
||||
}),
|
||||
)
|
||||
|
||||
it.instance("returns immutable snapshots", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* Job.Service
|
||||
const job = yield* jobs.start({ type: "test", metadata: { value: "initial" }, run: Effect.succeed("done") })
|
||||
|
||||
if (job.metadata) job.metadata.value = "changed"
|
||||
|
||||
expect((yield* jobs.get(job.id))?.metadata?.value).toBe("initial")
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
|
@ -12,7 +12,7 @@ import { testEffect } from "../lib/effect"
|
|||
import { EventV2Bridge } from "@/event-v2-bridge"
|
||||
import { Storage } from "@/storage/storage"
|
||||
import { RuntimeFlags } from "@/effect/runtime-flags"
|
||||
import { BackgroundJob } from "@/background/job"
|
||||
import { Job } from "@/job"
|
||||
|
||||
const layer = (experimentalWorkspaces: boolean) =>
|
||||
Layer.mergeAll(
|
||||
|
|
@ -24,7 +24,7 @@ const layer = (experimentalWorkspaces: boolean) =>
|
|||
Layer.provide(EventV2Bridge.defaultLayer),
|
||||
Layer.provide(SessionProjector.defaultLayer),
|
||||
Layer.provide(RuntimeFlags.layer({ experimentalWorkspaces })),
|
||||
Layer.provide(BackgroundJob.defaultLayer),
|
||||
Layer.provide(Job.defaultLayer),
|
||||
),
|
||||
)
|
||||
const it = testEffect(layer(false))
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ import path from "path"
|
|||
import { fileURLToPath } from "url"
|
||||
import { NamedError } from "@opencode-ai/core/util/error"
|
||||
import { Agent as AgentSvc } from "../../src/agent/agent"
|
||||
import { BackgroundJob } from "@/background/job"
|
||||
import { Job } from "@/job"
|
||||
import { Command } from "../../src/command"
|
||||
import { Config } from "@/config/config"
|
||||
import { LSP } from "@/lsp/lsp"
|
||||
|
|
@ -183,7 +183,7 @@ function makePrompt(input?: { mcpInstructions?: MCP.ServerInstructions[]; proces
|
|||
lsp,
|
||||
makeMcp(input?.mcpInstructions),
|
||||
FSUtil.defaultLayer,
|
||||
BackgroundJob.defaultLayer,
|
||||
Job.defaultLayer,
|
||||
status,
|
||||
Database.defaultLayer,
|
||||
EventV2Bridge.defaultLayer,
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ import { provideInstance, testInstanceStoreLayer, tmpdirScoped } from "../fixtur
|
|||
import { testEffect } from "../lib/effect"
|
||||
import { Storage } from "@/storage/storage"
|
||||
import { RuntimeFlags } from "@/effect/runtime-flags"
|
||||
import { BackgroundJob } from "@/background/job"
|
||||
import { Job } from "@/job"
|
||||
import { EventV2Bridge } from "@/event-v2-bridge"
|
||||
import { GlobalBus } from "@/bus/global"
|
||||
|
||||
|
|
@ -24,7 +24,7 @@ const it = testEffect(
|
|||
Layer.provideMerge(EventV2Bridge.defaultLayer),
|
||||
Layer.provide(SessionProjector.defaultLayer),
|
||||
Layer.provide(RuntimeFlags.layer({ experimentalWorkspaces: false })),
|
||||
Layer.provide(BackgroundJob.defaultLayer),
|
||||
Layer.provide(Job.defaultLayer),
|
||||
),
|
||||
CrossSpawnSpawner.defaultLayer,
|
||||
testInstanceStoreLayer,
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import { SessionV1 } from "@opencode-ai/core/v1/session"
|
|||
import { Database } from "@opencode-ai/core/database/database"
|
||||
import { Deferred, Effect, Exit, Fiber, Layer } from "effect"
|
||||
import { Agent } from "../../src/agent/agent"
|
||||
import { BackgroundJob } from "@/background/job"
|
||||
import { Job } from "@/job"
|
||||
import { EventV2Bridge } from "@/event-v2-bridge"
|
||||
import { Config } from "@/config/config"
|
||||
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
|
||||
|
|
@ -19,7 +19,7 @@ import { Truncate } from "@/tool/truncate"
|
|||
import { ToolRegistry } from "@/tool/registry"
|
||||
import { RuntimeFlags } from "@/effect/runtime-flags"
|
||||
import { disposeAllInstances } from "../fixture/fixture"
|
||||
import { testEffect } from "../lib/effect"
|
||||
import { pollWithTimeout, testEffect } from "../lib/effect"
|
||||
import { ProviderV2 } from "@opencode-ai/core/provider"
|
||||
import { ModelV2 } from "@opencode-ai/core/model"
|
||||
|
||||
|
|
@ -35,7 +35,7 @@ const ref = {
|
|||
const layer = (flags: Partial<RuntimeFlags.Info> = {}) =>
|
||||
Layer.mergeAll(
|
||||
Agent.defaultLayer,
|
||||
BackgroundJob.defaultLayer,
|
||||
Job.defaultLayer,
|
||||
EventV2Bridge.defaultLayer,
|
||||
Config.defaultLayer,
|
||||
CrossSpawnSpawner.defaultLayer,
|
||||
|
|
@ -480,9 +480,9 @@ describe("tool.task", () => {
|
|||
}),
|
||||
)
|
||||
|
||||
it.instance("promotes a running foreground task without restarting it", () =>
|
||||
it.instance("backgrounds a running foreground task without restarting it", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* BackgroundJob.Service
|
||||
const jobs = yield* Job.Service
|
||||
const { chat, assistant } = yield* seed()
|
||||
const tool = yield* TaskTool
|
||||
const def = yield* tool.init()
|
||||
|
|
@ -531,7 +531,12 @@ describe("tool.task", () => {
|
|||
expect(job).toBeDefined()
|
||||
if (!job) throw new Error("task job not found")
|
||||
expect(job.metadata?.parentSessionId).toBe(chat.id)
|
||||
yield* jobs.promote(job.id)
|
||||
yield* pollWithTimeout(
|
||||
jobs
|
||||
.backgroundAll({ sessionID: chat.id, type: "task" })
|
||||
.pipe(Effect.map((backgrounded) => (backgrounded.length > 0 ? backgrounded : undefined))),
|
||||
"task never blocked the parent session",
|
||||
)
|
||||
|
||||
const result = yield* Fiber.join(fiber)
|
||||
expect(result.metadata.background).toBe(true)
|
||||
|
|
@ -548,7 +553,7 @@ describe("tool.task", () => {
|
|||
|
||||
background.instance("execute launches background tasks without waiting for completion", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* BackgroundJob.Service
|
||||
const jobs = yield* Job.Service
|
||||
const { chat, assistant } = yield* seed()
|
||||
const tool = yield* TaskTool
|
||||
const def = yield* tool.init()
|
||||
|
|
@ -584,15 +589,13 @@ describe("tool.task", () => {
|
|||
}),
|
||||
)
|
||||
|
||||
background.instance("background task completion waits for running updates", () =>
|
||||
background.instance("running task_id reports the existing background task", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* BackgroundJob.Service
|
||||
const jobs = yield* Job.Service
|
||||
const { chat, assistant } = yield* seed()
|
||||
const tool = yield* TaskTool
|
||||
const def = yield* tool.init()
|
||||
const first = defer<void>()
|
||||
const second = defer<void>()
|
||||
const updated = defer<SessionPrompt.PromptInput>()
|
||||
const injected = defer<SessionPrompt.PromptInput>()
|
||||
let prompts = 0
|
||||
const promptOps: TaskPromptOps = {
|
||||
|
|
@ -603,9 +606,7 @@ describe("tool.task", () => {
|
|||
return Effect.succeed(reply(input, "done"))
|
||||
}
|
||||
prompts++
|
||||
if (prompts === 1) return Effect.promise(() => first.promise).pipe(Effect.as(reply(input, "first done")))
|
||||
updated.resolve(input)
|
||||
return Effect.promise(() => second.promise).pipe(Effect.as(reply(input, "second done")))
|
||||
return Effect.promise(() => first.promise).pipe(Effect.as(reply(input, "first done")))
|
||||
},
|
||||
}
|
||||
const context = {
|
||||
|
|
@ -640,27 +641,22 @@ describe("tool.task", () => {
|
|||
|
||||
expect(result.metadata.sessionId).toBe(started.metadata.sessionId)
|
||||
expect(result.metadata.background).toBe(true)
|
||||
expect(result.output).toContain("Background task updated")
|
||||
expect(result.output).toContain("Background task already running")
|
||||
expect(prompts).toBe(1)
|
||||
first.resolve()
|
||||
expect((yield* jobs.get(started.metadata.sessionId))?.status).toBe("running")
|
||||
expect((yield* Effect.promise(() => updated.promise)).parts).toEqual([
|
||||
{ type: "text", text: "also inspect cancellation" },
|
||||
])
|
||||
|
||||
second.resolve()
|
||||
const waited = yield* jobs.wait({ id: started.metadata.sessionId, timeout: 1_000 })
|
||||
expect(waited.info?.status).toBe("completed")
|
||||
expect(waited.info?.output).toBe("second done")
|
||||
expect(waited.info?.output).toBe("first done")
|
||||
const notification = yield* Effect.promise(() => injected.promise)
|
||||
expect(notification.variant).toBe("xhigh")
|
||||
expect(notification.parts[0]?.type).toBe("text")
|
||||
if (notification.parts[0]?.type === "text") expect(notification.parts[0].text).toContain("second done")
|
||||
if (notification.parts[0]?.type === "text") expect(notification.parts[0].text).toContain("first done")
|
||||
}),
|
||||
)
|
||||
|
||||
background.instance("background tasks complete through the background job service", () =>
|
||||
background.instance("background tasks complete through the job service", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* BackgroundJob.Service
|
||||
const jobs = yield* Job.Service
|
||||
const { chat, assistant } = yield* seed()
|
||||
const tool = yield* TaskTool
|
||||
const def = yield* tool.init()
|
||||
|
|
@ -693,7 +689,7 @@ describe("tool.task", () => {
|
|||
|
||||
background.instance("background task completion does not wait for the parent async prompt", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* BackgroundJob.Service
|
||||
const jobs = yield* Job.Service
|
||||
const { chat, assistant } = yield* seed()
|
||||
const tool = yield* TaskTool
|
||||
const def = yield* tool.init()
|
||||
|
|
@ -731,7 +727,7 @@ describe("tool.task", () => {
|
|||
|
||||
background.instance("removing the parent session cancels running background tasks", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* BackgroundJob.Service
|
||||
const jobs = yield* Job.Service
|
||||
const sessions = yield* Session.Service
|
||||
const { chat, assistant } = yield* seed()
|
||||
const tool = yield* TaskTool
|
||||
|
|
@ -770,7 +766,7 @@ describe("tool.task", () => {
|
|||
|
||||
background.instance("removing the child task session cancels its running background task", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* BackgroundJob.Service
|
||||
const jobs = yield* Job.Service
|
||||
const sessions = yield* Session.Service
|
||||
const { chat, assistant } = yield* seed()
|
||||
const tool = yield* TaskTool
|
||||
|
|
@ -809,7 +805,7 @@ describe("tool.task", () => {
|
|||
|
||||
background.instance("cancelling the parent run cancels running background tasks", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* BackgroundJob.Service
|
||||
const jobs = yield* Job.Service
|
||||
const runState = yield* SessionRunState.Service
|
||||
const { chat, assistant } = yield* seed()
|
||||
const tool = yield* TaskTool
|
||||
|
|
@ -848,7 +844,7 @@ describe("tool.task", () => {
|
|||
|
||||
it.instance("cancelling a child run cancels its own pre-runner task job", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* BackgroundJob.Service
|
||||
const jobs = yield* Job.Service
|
||||
const runState = yield* SessionRunState.Service
|
||||
const sessions = yield* Session.Service
|
||||
const { chat } = yield* seed()
|
||||
|
|
@ -869,7 +865,7 @@ describe("tool.task", () => {
|
|||
|
||||
it.instance("cancelling a parent run recursively cancels descendant background tasks", () =>
|
||||
Effect.gen(function* () {
|
||||
const jobs = yield* BackgroundJob.Service
|
||||
const jobs = yield* Job.Service
|
||||
const runState = yield* SessionRunState.Service
|
||||
const sessions = yield* Session.Service
|
||||
const { chat } = yield* seed()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue