mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-21 04:12:08 +03:00
fix(core): report subagent results to their parent on idle via job table
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
{
|
||||
"version": "7",
|
||||
"dialect": "sqlite",
|
||||
"id": "4f6e16c6-f070-4545-b9d0-19fcb6e5210c",
|
||||
"prevIds": ["0ac88673-a402-45a0-b931-e694d59ccefa"],
|
||||
"id": "e4d01452-f36f-4800-8fe8-17e1a0c4eb68",
|
||||
"prevIds": ["4f6e16c6-f070-4545-b9d0-19fcb6e5210c"],
|
||||
"ddl": [
|
||||
{
|
||||
"name": "account_state",
|
||||
@@ -566,6 +566,36 @@
|
||||
"entityType": "columns",
|
||||
"table": "job"
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"notNull": true,
|
||||
"autoincrement": false,
|
||||
"default": "true",
|
||||
"generated": null,
|
||||
"name": "pending",
|
||||
"entityType": "columns",
|
||||
"table": "job"
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"notNull": false,
|
||||
"autoincrement": false,
|
||||
"default": null,
|
||||
"generated": null,
|
||||
"name": "reported",
|
||||
"entityType": "columns",
|
||||
"table": "job"
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"notNull": true,
|
||||
"autoincrement": false,
|
||||
"default": "false",
|
||||
"generated": null,
|
||||
"name": "dirty",
|
||||
"entityType": "columns",
|
||||
"table": "job"
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"notNull": true,
|
||||
@@ -2393,7 +2423,7 @@
|
||||
"isExpression": false
|
||||
}
|
||||
],
|
||||
"isUnique": false,
|
||||
"isUnique": true,
|
||||
"where": null,
|
||||
"origin": "manual",
|
||||
"name": "job_child_session_idx",
|
||||
|
||||
@@ -98,12 +98,6 @@ export const Plugin = define({
|
||||
agent: selected.id,
|
||||
model: model ?? selected.info?.model ?? parent.model,
|
||||
})
|
||||
yield* sessions.prompt({
|
||||
...input.prompt,
|
||||
sessionID: child.id,
|
||||
text: ["You are a subagent spawned by another session.", text].join("\n"),
|
||||
resume: false,
|
||||
})
|
||||
const recovery = {
|
||||
kind: "subagent" as const,
|
||||
parentSessionID: parent.id,
|
||||
@@ -111,6 +105,14 @@ export const Plugin = define({
|
||||
agent: selected.id,
|
||||
description: command.description ?? name,
|
||||
}
|
||||
yield* subagents.track(recovery)
|
||||
yield* sessions.prompt({
|
||||
...input.prompt,
|
||||
metadata: { source: "subagent" },
|
||||
sessionID: child.id,
|
||||
text: ["You are a subagent spawned by another session.", text].join("\n"),
|
||||
resume: false,
|
||||
})
|
||||
yield* subagents.start(recovery)
|
||||
yield* subagents.background(recovery)
|
||||
return
|
||||
|
||||
+2
@@ -58,6 +58,7 @@ import m55 from "./migration/20261029002625_session_part.js"
|
||||
import m56 from "./migration/20261029073502_session_time_busy.js"
|
||||
import m57 from "./migration/20261029165540_schedule_checks.js"
|
||||
import m58 from "./migration/20261030054400_job.js"
|
||||
import m59 from "./migration/20261030204714_job_subagent.js"
|
||||
|
||||
export const migrations = [
|
||||
m00,
|
||||
@@ -119,4 +120,5 @@ export const migrations = [
|
||||
m56,
|
||||
m57,
|
||||
m58,
|
||||
m59,
|
||||
] satisfies DatabaseMigration.Migration[]
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Effect } from "effect"
|
||||
import type { DatabaseMigration } from "../migration.js"
|
||||
|
||||
const migration: DatabaseMigration.Migration = {
|
||||
id: "20261030204714_job_subagent",
|
||||
up(tx) {
|
||||
return Effect.gen(function* () {
|
||||
yield* tx.run(`ALTER TABLE \`job\` ADD \`pending\` integer DEFAULT true NOT NULL;`)
|
||||
yield* tx.run(`ALTER TABLE \`job\` ADD \`reported\` text;`)
|
||||
yield* tx.run(`ALTER TABLE \`job\` ADD \`dirty\` integer DEFAULT false NOT NULL;`)
|
||||
yield* tx.run(
|
||||
`DELETE FROM \`job\` WHERE \`child_session_id\` IS NOT NULL AND \`rowid\` NOT IN (SELECT MAX(\`rowid\`) FROM \`job\` WHERE \`child_session_id\` IS NOT NULL GROUP BY \`child_session_id\`);`,
|
||||
)
|
||||
yield* tx.run(`DROP INDEX IF EXISTS \`job_child_session_idx\`;`)
|
||||
yield* tx.run(`CREATE UNIQUE INDEX \`job_child_session_idx\` ON \`job\` (\`child_session_id\`);`)
|
||||
yield* tx.run(
|
||||
`UPDATE \`job\` SET \`reported\` = CASE WHEN \`status\` IN ('completed', 'error') THEN (SELECT \`id\` FROM \`session_message\` WHERE \`session_id\` = \`job\`.\`child_session_id\` AND \`type\` = 'idle' ORDER BY \`seq\` DESC LIMIT 1 OFFSET 1) ELSE (SELECT \`id\` FROM \`session_message\` WHERE \`session_id\` = \`job\`.\`child_session_id\` AND \`type\` = 'idle' ORDER BY \`seq\` DESC LIMIT 1) END, \`dirty\` = CASE WHEN \`status\` IN ('completed', 'error') THEN 1 ELSE 0 END WHERE \`kind\` = 'subagent';`,
|
||||
)
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
export default migration
|
||||
Generated
+4
-1
@@ -80,6 +80,9 @@ const schema: Omit<DatabaseMigration.Migration, "id"> = {
|
||||
\`output\` text,
|
||||
\`error\` text,
|
||||
\`recovery\` text NOT NULL,
|
||||
\`pending\` integer DEFAULT true NOT NULL,
|
||||
\`reported\` text,
|
||||
\`dirty\` integer DEFAULT false NOT NULL,
|
||||
\`time_created\` integer NOT NULL,
|
||||
\`time_updated\` integer NOT NULL
|
||||
);
|
||||
@@ -311,7 +314,7 @@ const schema: Omit<DatabaseMigration.Migration, "id"> = {
|
||||
yield* tx.run(`CREATE UNIQUE INDEX \`event_aggregate_seq_idx\` ON \`event\` (\`aggregate_id\`,\`seq\`);`)
|
||||
yield* tx.run(`CREATE INDEX \`event_aggregate_type_seq_idx\` ON \`event\` (\`aggregate_id\`,\`type\`,\`seq\`);`)
|
||||
yield* tx.run(`CREATE INDEX \`job_session_idx\` ON \`job\` (\`session_id\`);`)
|
||||
yield* tx.run(`CREATE INDEX \`job_child_session_idx\` ON \`job\` (\`child_session_id\`);`)
|
||||
yield* tx.run(`CREATE UNIQUE INDEX \`job_child_session_idx\` ON \`job\` (\`child_session_id\`);`)
|
||||
yield* tx.run(`CREATE INDEX \`job_id_idx\` ON \`job\` (\`id\`);`)
|
||||
yield* tx.run(
|
||||
`CREATE UNIQUE INDEX \`permission_project_action_resource_idx\` ON \`permission\` (\`project_id\`,\`action\`,\`resource\`);`,
|
||||
|
||||
+17
-10
@@ -1,6 +1,6 @@
|
||||
export * as Job from "./job.js"
|
||||
|
||||
import { asc, eq, or } from "drizzle-orm"
|
||||
import { and, asc, eq, ne, 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"
|
||||
@@ -235,21 +235,17 @@ export const make = Effect.gen(function* () {
|
||||
output: job.info.output ?? null,
|
||||
error: job.info.error ?? null,
|
||||
recovery: job.recovery,
|
||||
pending: true,
|
||||
time_created: job.info.started_at,
|
||||
time_updated: now,
|
||||
}
|
||||
const { time_created: _, ...update } = values
|
||||
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,
|
||||
},
|
||||
target: job.recovery.kind === "subagent" ? JobTable.child_session_id : JobTable.notification_id,
|
||||
set: update,
|
||||
})
|
||||
.run()
|
||||
.pipe(Effect.orDie)
|
||||
@@ -512,6 +508,7 @@ export const make = Effect.gen(function* () {
|
||||
const rows = yield* db
|
||||
.select()
|
||||
.from(JobTable)
|
||||
.where(eq(JobTable.pending, true))
|
||||
.orderBy(asc(JobTable.time_created), asc(JobTable.notification_id))
|
||||
.all()
|
||||
.pipe(Effect.orDie)
|
||||
@@ -539,7 +536,17 @@ export const make = Effect.gen(function* () {
|
||||
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)
|
||||
yield* db
|
||||
.delete(JobTable)
|
||||
.where(and(eq(JobTable.notification_id, notificationID), ne(JobTable.kind, "subagent")))
|
||||
.run()
|
||||
.pipe(Effect.orDie)
|
||||
yield* db
|
||||
.update(JobTable)
|
||||
.set({ pending: false })
|
||||
.where(and(eq(JobTable.notification_id, notificationID), eq(JobTable.kind, "subagent")))
|
||||
.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)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { index, sqliteTable, text } from "drizzle-orm/sqlite-core"
|
||||
import { index, integer, sqliteTable, text, uniqueIndex } from "drizzle-orm/sqlite-core"
|
||||
import { Timestamps } from "../database/schema.sql.js"
|
||||
import type { Job } from "../job.js"
|
||||
import type { SessionMessage } from "../session/message.js"
|
||||
@@ -17,11 +17,14 @@ export const JobTable = sqliteTable(
|
||||
output: text(),
|
||||
error: text(),
|
||||
recovery: text({ mode: "json" }).$type<Job.Recovery>().notNull(),
|
||||
pending: integer({ mode: "boolean" }).notNull().default(true),
|
||||
reported: text().$type<SessionMessage.ID>(),
|
||||
dirty: integer({ mode: "boolean" }).notNull().default(false),
|
||||
...Timestamps,
|
||||
},
|
||||
(table) => [
|
||||
index("job_session_idx").on(table.session_id),
|
||||
index("job_child_session_idx").on(table.child_session_id),
|
||||
uniqueIndex("job_child_session_idx").on(table.child_session_id),
|
||||
index("job_id_idx").on(table.id),
|
||||
],
|
||||
)
|
||||
|
||||
@@ -12,6 +12,7 @@ import { SessionStore } from "../store.js"
|
||||
import { ImageNotice } from "../../image/notice.js"
|
||||
import { ShellResult } from "../../shell/result.js"
|
||||
import { SubagentCompletion } from "../subagent-completion.js"
|
||||
import { SubagentJob } from "../subagent-job.js"
|
||||
|
||||
const CONTINUE_AFTER_SERVER_RESTART =
|
||||
"The server restarted while you were working. Continue from where you left off without repeating completed work."
|
||||
@@ -72,6 +73,7 @@ export const layer = (options?: Options) =>
|
||||
const bus = yield* Bus.Service
|
||||
const jobs = yield* Job.Service
|
||||
const sessions = yield* Session.Service
|
||||
const subagents = yield* SubagentJob.Service
|
||||
const scope = yield* Effect.scope
|
||||
const maxAttempts = options?.maxAttempts ?? DEFAULT_MAX_ATTEMPTS
|
||||
|
||||
@@ -99,10 +101,7 @@ export const layer = (options?: Options) =>
|
||||
return true
|
||||
})
|
||||
|
||||
const restartMessage = (
|
||||
background: Job.Background,
|
||||
recovery: Exclude<Job.Recovery, { kind: "subagent" }>,
|
||||
) => {
|
||||
const restartMessage = (background: Job.Background, recovery: Exclude<Job.Recovery, { kind: "subagent" }>) => {
|
||||
const state = background.status === "running" ? "cancelled" : background.status
|
||||
if (recovery.kind === "shell") {
|
||||
const text =
|
||||
@@ -172,50 +171,40 @@ export const layer = (options?: Options) =>
|
||||
const child = yield* store.get(recovery.childSessionID)
|
||||
if (!child || child.parentID !== recovery.parentSessionID || !(yield* store.get(recovery.parentSessionID))) {
|
||||
yield* jobs.completeBackground(background.notificationID)
|
||||
yield* subagents.sync(recovery.childSessionID)
|
||||
return
|
||||
}
|
||||
|
||||
const notify = Effect.fnUntraced(function* (result: Pick<Job.Background, "status" | "output" | "error">) {
|
||||
if (background.status === "cancelled") {
|
||||
yield* SubagentCompletion.deliver(sessions, jobs, {
|
||||
...result,
|
||||
status: "cancelled",
|
||||
recovery,
|
||||
notificationID: background.notificationID,
|
||||
resume: suspended.has(recovery.parentSessionID) ? false : undefined,
|
||||
}).pipe(Effect.orDie)
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
if (background.status !== "running") {
|
||||
yield* notify(background)
|
||||
yield* subagents.sync(recovery.childSessionID)
|
||||
yield* jobs.completeBackground(background.notificationID)
|
||||
return
|
||||
}
|
||||
if (yield* execution.isActive(recovery.childSessionID)) return
|
||||
if (!(yield* prepareResume(recovery.childSessionID))) {
|
||||
yield* notify({ status: "error", error: RESUME_EXHAUSTED.message })
|
||||
yield* subagents.sync(recovery.childSessionID)
|
||||
yield* jobs.completeBackground(background.notificationID)
|
||||
return
|
||||
}
|
||||
|
||||
yield* jobs.start({
|
||||
const job = yield* jobs.start({
|
||||
id: background.id,
|
||||
type: "subagent",
|
||||
title: recovery.description,
|
||||
notificationID: background.notificationID,
|
||||
recovery,
|
||||
run: execution.resume(recovery.childSessionID).pipe(
|
||||
Effect.andThen(store.context(recovery.childSessionID)),
|
||||
Effect.map((messages) => {
|
||||
const assistant = messages.findLast(
|
||||
(message) =>
|
||||
message.type === "assistant" && message.time.completed !== undefined && message.error === undefined,
|
||||
)
|
||||
return SubagentCompletion.text(assistant)
|
||||
}),
|
||||
),
|
||||
run: execution.resume(recovery.childSessionID).pipe(Effect.as("")),
|
||||
})
|
||||
yield* jobs.background(background.id)
|
||||
yield* jobs.wait({ id: background.id }).pipe(
|
||||
Effect.flatMap((result) => (result.info ? notify(result.info) : Effect.void)),
|
||||
Effect.forkIn(scope),
|
||||
)
|
||||
yield* subagents.notify(recovery, job.started_at)
|
||||
})
|
||||
|
||||
return Service.of({
|
||||
@@ -232,17 +221,24 @@ export const layer = (options?: Options) =>
|
||||
[...(yield* store.listSuspended()), ...children].filter((sessionID) => !active.has(sessionID)),
|
||||
)
|
||||
yield* store.releaseChildClaims(children)
|
||||
yield* Effect.forEach(
|
||||
// Admit shell outcomes before a recovered child can start its first model request.
|
||||
pending.toSorted((a, b) => Number(a.recovery.kind === "subagent") - Number(b.recovery.kind === "subagent")),
|
||||
Effect.fnUntraced(function* (background) {
|
||||
if ((yield* jobs.get(background.id))?.status === "running") return
|
||||
const recovery = background.recovery
|
||||
if (recovery.kind !== "subagent") return yield* recoverToolJob(background, recovery)
|
||||
yield* recoverSubagent(background, recovery, suspended)
|
||||
}),
|
||||
{ discard: true },
|
||||
)
|
||||
yield* subagents
|
||||
.recover(
|
||||
suspended,
|
||||
Effect.forEach(
|
||||
// Admit shell outcomes before a recovered child can start its first model request.
|
||||
pending.toSorted(
|
||||
(a, b) => Number(a.recovery.kind === "subagent") - Number(b.recovery.kind === "subagent"),
|
||||
),
|
||||
Effect.fnUntraced(function* (background) {
|
||||
if ((yield* jobs.get(background.id))?.status === "running") return
|
||||
const recovery = background.recovery
|
||||
if (recovery.kind !== "subagent") return yield* recoverToolJob(background, recovery)
|
||||
yield* recoverSubagent(background, recovery, suspended)
|
||||
}),
|
||||
{ discard: true },
|
||||
),
|
||||
)
|
||||
.pipe(Effect.orDie)
|
||||
|
||||
// Background completion can wake a parent, so inspect local ownership only after recovery.
|
||||
const resumed = yield* execution.active
|
||||
@@ -264,5 +260,5 @@ export const layer = (options?: Options) =>
|
||||
export const node = makeGlobalNode({
|
||||
service: Service,
|
||||
layer: layer(),
|
||||
deps: [SessionStore.node, SessionExecution.node, Bus.node, Job.node, Session.node],
|
||||
deps: [SessionStore.node, SessionExecution.node, Bus.node, Job.node, Session.node, SubagentJob.node],
|
||||
})
|
||||
|
||||
@@ -25,6 +25,7 @@ import { Project } from "@opencode/schema/project"
|
||||
import { AbsolutePath, RelativePath } from "../schema.js"
|
||||
import type { SessionSchema } from "./schema.js"
|
||||
import { SessionHistory } from "./history.js"
|
||||
import { JobTable } from "../job/sql.js"
|
||||
import { ProjectTable } from "../project/sql.js"
|
||||
|
||||
type DatabaseService = Database.Interface["db"]
|
||||
@@ -209,7 +210,10 @@ const projectFork = Effect.fn("SessionProjector.projectFork")(function* (
|
||||
.orderBy(asc(SessionMessageTable.seq))
|
||||
.limit(ForkBatchSize)
|
||||
.all()
|
||||
.pipe(Effect.orDie, Effect.flatMap((rows) => SessionHistory.withParts(db, rows)))
|
||||
.pipe(
|
||||
Effect.orDie,
|
||||
Effect.flatMap((rows) => SessionHistory.withParts(db, rows)),
|
||||
)
|
||||
if (rows.length === 0) break
|
||||
|
||||
const values = yield* Effect.forEach(rows, (row) =>
|
||||
@@ -531,6 +535,14 @@ function projectIdle(
|
||||
.where(eq(SessionTable.id, event.data.sessionID))
|
||||
.run()
|
||||
.pipe(Effect.orDie)
|
||||
if (outcome !== "interrupted") {
|
||||
yield* db
|
||||
.update(JobTable)
|
||||
.set({ dirty: true })
|
||||
.where(and(eq(JobTable.child_session_id, event.data.sessionID), eq(JobTable.kind, "subagent")))
|
||||
.run()
|
||||
.pipe(Effect.orDie)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -646,6 +658,11 @@ const layer = Layer.effectDiscard(
|
||||
yield* bus.project(SessionEvent.Deleted, (event) =>
|
||||
Effect.gen(function* () {
|
||||
yield* SessionBlob.releaseSession(db, event.data.sessionID)
|
||||
yield* db
|
||||
.delete(JobTable)
|
||||
.where(or(eq(JobTable.session_id, event.data.sessionID), eq(JobTable.child_session_id, event.data.sessionID)))
|
||||
.run()
|
||||
.pipe(Effect.orDie)
|
||||
yield* db.delete(SessionTable).where(eq(SessionTable.id, event.data.sessionID)).run().pipe(Effect.orDie)
|
||||
}),
|
||||
)
|
||||
@@ -908,6 +925,19 @@ const layer = Layer.effectDiscard(
|
||||
.where(eq(SessionTable.id, event.data.sessionID))
|
||||
.run()
|
||||
.pipe(Effect.orDie)
|
||||
yield* db
|
||||
.update(JobTable)
|
||||
.set({
|
||||
reported: sql`(select ${SessionMessageTable.id} from ${SessionMessageTable} where ${SessionMessageTable.session_id} = ${event.data.sessionID} and ${SessionMessageTable.type} = 'idle' order by ${SessionMessageTable.seq} desc limit 1)`,
|
||||
})
|
||||
.where(
|
||||
and(
|
||||
eq(JobTable.child_session_id, event.data.sessionID),
|
||||
sql`${JobTable.reported} is not null and not exists (select 1 from ${SessionMessageTable} where ${SessionMessageTable.session_id} = ${event.data.sessionID} and ${SessionMessageTable.id} = ${JobTable.reported})`,
|
||||
),
|
||||
)
|
||||
.run()
|
||||
.pipe(Effect.orDie)
|
||||
yield* InstructionState.rewind(db, event.data.sessionID, boundary.seq)
|
||||
yield* SessionBlob.releaseFrom(db, event.data.sessionID, boundary.seq)
|
||||
}),
|
||||
|
||||
@@ -7,27 +7,32 @@ import type { SessionMessage } from "./message.js"
|
||||
|
||||
export const NO_TEXT = "Subagent completed without a text response."
|
||||
|
||||
export function extractText(message: SessionMessage.Info | undefined) {
|
||||
if (message?.type !== "assistant") return undefined
|
||||
const joined = message.content
|
||||
.filter((part) => part.type === "text")
|
||||
.map((part) => part.text)
|
||||
.filter((text) => text.length > 0)
|
||||
.join("\n\n")
|
||||
return joined.length > 0 ? joined : undefined
|
||||
}
|
||||
|
||||
export function text(message: SessionMessage.Info | undefined) {
|
||||
if (message?.type !== "assistant") return NO_TEXT
|
||||
return (
|
||||
message.content
|
||||
.filter((part) => part.type === "text")
|
||||
.map((part) => part.text)
|
||||
.filter((text) => text.length > 0)
|
||||
.join("\n\n") || NO_TEXT
|
||||
)
|
||||
return extractText(message) ?? NO_TEXT
|
||||
}
|
||||
|
||||
export const deliver = Effect.fnUntraced(function* (
|
||||
sessions: Pick<Session.Interface, "synthetic">,
|
||||
jobs: Pick<Job.Interface, "completeBackground">,
|
||||
input: Pick<Job.Info, "status" | "output" | "error" | "notificationID"> & {
|
||||
inboxID?: SessionMessage.ID
|
||||
recovery: Extract<Job.Recovery, { kind: "subagent" }>
|
||||
resume?: boolean
|
||||
},
|
||||
) {
|
||||
if (input.status === "running") return
|
||||
const recovery = input.recovery
|
||||
const id = input.inboxID ?? input.notificationID
|
||||
const text =
|
||||
input.status === "completed"
|
||||
? (input.output ?? NO_TEXT)
|
||||
@@ -35,7 +40,7 @@ export const deliver = Effect.fnUntraced(function* (
|
||||
? (input.error ?? "Subagent failed")
|
||||
: "Subagent cancelled"
|
||||
yield* sessions.synthetic({
|
||||
...(input.notificationID ? { id: input.notificationID } : {}),
|
||||
...(id ? { id } : {}),
|
||||
sessionID: recovery.parentSessionID,
|
||||
...(input.resume === false ? { resume: false } : {}),
|
||||
description: recovery.description,
|
||||
|
||||
@@ -1,31 +1,243 @@
|
||||
export * as SubagentJob from "./subagent-job.js"
|
||||
|
||||
import { Context, Effect, Layer, Scope } from "effect"
|
||||
import { asc, eq } from "drizzle-orm"
|
||||
import { Clock, Context, Effect, Layer, Scope, Stream } from "effect"
|
||||
import { Node } from "@opencode/util/effect/app-node"
|
||||
import type { LayerNode } from "@opencode/util/effect/layer-node"
|
||||
import { Bus } from "../bus.js"
|
||||
import { Database } from "../database/database.js"
|
||||
import { KeyedMutex } from "../effect/keyed-mutex.js"
|
||||
import { Job } from "../job.js"
|
||||
import { JobTable } from "../job/sql.js"
|
||||
import { Session } from "../session.js"
|
||||
import { SessionEvent } from "./event.js"
|
||||
import { SessionMessage } from "./message.js"
|
||||
import { SessionSchema } from "./schema.js"
|
||||
import { SessionInboxTable } from "./sql.js"
|
||||
import { SessionStore } from "./store.js"
|
||||
import { SubagentCompletion } from "./subagent-completion.js"
|
||||
|
||||
type Recovery = Extract<Job.Recovery, { kind: "subagent" }>
|
||||
|
||||
const noticeID = (idleID: SessionMessage.ID) => SessionMessage.ID.make(`${idleID}_subagent`)
|
||||
const TURN_PAGE = 50
|
||||
|
||||
export interface Interface {
|
||||
track: (recovery: Recovery) => Effect.Effect<void>
|
||||
start: (recovery: Recovery) => Effect.Effect<Job.Info>
|
||||
block: <E>(recovery: Recovery, prompt: Effect.Effect<unknown, E>) => Effect.Effect<Job.BlockResult | undefined, E>
|
||||
background: (recovery: Recovery) => Effect.Effect<void>
|
||||
notify: (recovery: Recovery, startedAt: number) => Effect.Effect<void>
|
||||
sync: (sessionID: SessionSchema.ID) => Effect.Effect<void>
|
||||
recover: <A, E, R>(
|
||||
suspended: ReadonlySet<SessionSchema.ID>,
|
||||
prepare: Effect.Effect<A, E, R>,
|
||||
) => Effect.Effect<A, E, R>
|
||||
}
|
||||
|
||||
export class Service extends Context.Service<Service, Interface>()("@opencode/SubagentJob") {}
|
||||
|
||||
const make: Effect.Effect<Interface, never, Session.Service | Job.Service | Scope.Scope> = Effect.gen(function* () {
|
||||
const make: Effect.Effect<
|
||||
Interface,
|
||||
never,
|
||||
Database.Service | Session.Service | SessionStore.Service | Job.Service | Bus.Service | Scope.Scope
|
||||
> = Effect.gen(function* () {
|
||||
const db = (yield* Database.Service).db
|
||||
const sessions = yield* Session.Service
|
||||
const store = yield* SessionStore.Service
|
||||
const jobs = yield* Job.Service
|
||||
const bus = yield* Bus.Service
|
||||
const scope = yield* Scope.Scope
|
||||
// Jobs and their completion observers outlive the plugin that launched them.
|
||||
// Fork with the global context too, rather than retaining the caller's location.
|
||||
// Jobs and listeners outlive the plugin that launched them, without retaining a caller Location.
|
||||
const context = yield* Effect.context<never>()
|
||||
// One observer per job generation, including continuations of the same child.
|
||||
const locks = KeyedMutex.makeUnsafe<SessionSchema.ID>()
|
||||
const notifications = new Set<string>()
|
||||
// Holds idle delivery while a synchronous foreground call is waiting on `jobs.block`.
|
||||
const foreground = new Map<SessionSchema.ID, number>()
|
||||
const settled = new Map<SessionSchema.ID, SessionMessage.ID>()
|
||||
const deferredParents = new Set<SessionSchema.ID>()
|
||||
|
||||
const readMarker = (sessionID: SessionSchema.ID) =>
|
||||
db
|
||||
.select()
|
||||
.from(JobTable)
|
||||
.where(eq(JobTable.child_session_id, sessionID))
|
||||
.get()
|
||||
.pipe(
|
||||
Effect.map((row) => (row?.recovery.kind === "subagent" ? { ...row, recovery: row.recovery } : undefined)),
|
||||
Effect.orDie,
|
||||
)
|
||||
|
||||
const updateMarker = (sessionID: SessionSchema.ID, reported: SessionMessage.ID | null, dirty = false) =>
|
||||
db
|
||||
.update(JobTable)
|
||||
.set({ reported, dirty })
|
||||
.where(eq(JobTable.child_session_id, sessionID))
|
||||
.run()
|
||||
.pipe(Effect.orDie)
|
||||
|
||||
const removeMarker = (sessionID: SessionSchema.ID) =>
|
||||
db.delete(JobTable).where(eq(JobTable.child_session_id, sessionID)).run().pipe(Effect.orDie)
|
||||
|
||||
const latestIdle = (sessionID: SessionSchema.ID) =>
|
||||
store.messages({ sessionID, type: "idle", order: "desc", limit: 1 }).pipe(
|
||||
Effect.map(([message]) => (message?.type === "idle" ? message.id : undefined)),
|
||||
Effect.orDie,
|
||||
)
|
||||
|
||||
const deliverToParent = Effect.fnUntraced(function* (
|
||||
recovery: Recovery,
|
||||
idleID: SessionMessage.ID,
|
||||
legacyID: SessionMessage.ID | undefined,
|
||||
outcome: { status: "completed"; output: string } | { status: "error"; error: string },
|
||||
) {
|
||||
let inboxID = noticeID(idleID)
|
||||
if (legacyID) {
|
||||
const delivered = yield* store.message(legacyID)
|
||||
if (delivered) {
|
||||
if (delivered.sessionID === recovery.parentSessionID && delivered.message.type === "synthetic") return true
|
||||
inboxID = legacyID
|
||||
} else if (
|
||||
yield* db
|
||||
.select({ id: SessionInboxTable.id })
|
||||
.from(SessionInboxTable)
|
||||
.where(eq(SessionInboxTable.id, legacyID))
|
||||
.get()
|
||||
.pipe(Effect.orDie)
|
||||
) {
|
||||
inboxID = legacyID
|
||||
}
|
||||
}
|
||||
return yield* SubagentCompletion.deliver(sessions, jobs, {
|
||||
inboxID,
|
||||
recovery,
|
||||
...outcome,
|
||||
resume: deferredParents.has(recovery.parentSessionID) ? false : undefined,
|
||||
}).pipe(
|
||||
Effect.as(true),
|
||||
Effect.catchTag("Session.NotFoundError", () => removeMarker(recovery.childSessionID).pipe(Effect.as(false))),
|
||||
Effect.orDie,
|
||||
)
|
||||
})
|
||||
|
||||
// Every unreported busy period of a child, read forward in sequence order from its saved marker.
|
||||
// Text results and failures notify the parent; empty successes, interruptions, and busy periods
|
||||
// started by a direct user prompt advance the marker silently.
|
||||
const syncUnlocked = Effect.fnUntraced(function* (sessionID: SessionSchema.ID) {
|
||||
if ((foreground.get(sessionID) ?? 0) > 0) return
|
||||
const marker = yield* readMarker(sessionID)
|
||||
if (!marker) return
|
||||
const { recovery } = marker
|
||||
const child = yield* store.get(sessionID)
|
||||
if (!child || child.parentID !== recovery.parentSessionID || !(yield* store.get(recovery.parentSessionID))) {
|
||||
yield* removeMarker(sessionID)
|
||||
return
|
||||
}
|
||||
|
||||
let reported = marker.reported
|
||||
let pendingTurn = marker.pending
|
||||
let cursor = reported ? { id: reported, direction: "next" as const } : undefined
|
||||
let assistant: SessionMessage.Assistant | undefined
|
||||
let starter: "subagent" | "direct" | undefined
|
||||
while (true) {
|
||||
const page = yield* store.messages({ sessionID, order: "asc", limit: TURN_PAGE, cursor }).pipe(Effect.orDie)
|
||||
for (const message of page) {
|
||||
if (starter === undefined && (message.type === "user" || message.type === "synthetic"))
|
||||
starter = message.type === "user" && message.metadata?.source !== "subagent" ? "direct" : "subagent"
|
||||
if (message.type === "assistant" && message.time.completed !== undefined && message.error === undefined)
|
||||
assistant = message
|
||||
if (message.type !== "idle") continue
|
||||
|
||||
const legacyID = pendingTurn ? marker.notification_id : undefined
|
||||
const shouldDeliver = (starter !== "direct" || pendingTurn) && message.outcome !== "interrupted"
|
||||
pendingTurn = false
|
||||
if (shouldDeliver) {
|
||||
const output = message.outcome === "succeeded" ? SubagentCompletion.extractText(assistant) : undefined
|
||||
const outcome =
|
||||
message.outcome === "failed"
|
||||
? { status: "error" as const, error: message.error?.message ?? "Subagent failed" }
|
||||
: output !== undefined
|
||||
? { status: "completed" as const, output }
|
||||
: undefined
|
||||
if (outcome && !(yield* deliverToParent(recovery, message.id, legacyID, outcome))) return
|
||||
}
|
||||
reported = message.id
|
||||
yield* updateMarker(sessionID, reported, true)
|
||||
assistant = undefined
|
||||
starter = undefined
|
||||
}
|
||||
const last = page.at(-1)
|
||||
if (page.length < TURN_PAGE || !last) {
|
||||
if (((yield* latestIdle(sessionID)) ?? null) !== reported) {
|
||||
assistant = undefined
|
||||
starter = undefined
|
||||
cursor = reported ? { id: reported, direction: "next" } : undefined
|
||||
continue
|
||||
}
|
||||
yield* updateMarker(sessionID, reported, false)
|
||||
return
|
||||
}
|
||||
cursor = { id: last.id, direction: "next" }
|
||||
}
|
||||
})
|
||||
|
||||
const sync = (sessionID: SessionSchema.ID) => locks.withLock(sessionID)(syncUnlocked(sessionID))
|
||||
|
||||
const latestTurnOutput = Effect.fnUntraced(function* (sessionID: SessionSchema.ID) {
|
||||
let seenIdle = false
|
||||
let assistant: SessionMessage.Assistant | undefined
|
||||
let cursor: { id: SessionMessage.ID; direction: "next" } | undefined
|
||||
while (true) {
|
||||
const page = yield* store.messages({ sessionID, order: "desc", limit: TURN_PAGE, cursor }).pipe(Effect.orDie)
|
||||
for (const message of page) {
|
||||
if (message.type === "idle") {
|
||||
if (seenIdle) return SubagentCompletion.text(assistant)
|
||||
seenIdle = true
|
||||
settled.set(sessionID, message.id)
|
||||
continue
|
||||
}
|
||||
if (
|
||||
!assistant &&
|
||||
message.type === "assistant" &&
|
||||
message.time.completed !== undefined &&
|
||||
message.error === undefined
|
||||
)
|
||||
assistant = message
|
||||
}
|
||||
const last = page.at(-1)
|
||||
if (page.length < TURN_PAGE || !last) return SubagentCompletion.text(assistant)
|
||||
cursor = { id: last.id, direction: "next" }
|
||||
}
|
||||
})
|
||||
|
||||
yield* bus
|
||||
.subscribe([
|
||||
SessionEvent.Execution.Succeeded,
|
||||
SessionEvent.Execution.Failed,
|
||||
SessionEvent.Execution.Interrupted,
|
||||
SessionEvent.Deleted,
|
||||
])
|
||||
.pipe(
|
||||
Stream.runForEach((event) => {
|
||||
const sessionID = event.data.sessionID
|
||||
if (event.type === SessionEvent.Deleted.type)
|
||||
return locks.withLock(sessionID)(
|
||||
Effect.sync(() => {
|
||||
foreground.delete(sessionID)
|
||||
settled.delete(sessionID)
|
||||
}),
|
||||
)
|
||||
if (event.type === SessionEvent.Execution.Interrupted.type && event.data.reason === "shutdown")
|
||||
return Effect.void
|
||||
return sync(sessionID).pipe(
|
||||
Effect.catchCause((cause) =>
|
||||
Effect.logError("subagent.sync_failed", cause).pipe(Effect.annotateLogs({ sessionID })),
|
||||
),
|
||||
)
|
||||
}),
|
||||
Effect.forkIn(scope, { startImmediately: true }),
|
||||
Effect.setContext(context),
|
||||
)
|
||||
|
||||
const notify = Effect.fn("SubagentJob.notify")(function* (recovery: Recovery, startedAt: number) {
|
||||
const key = `${recovery.childSessionID}:${startedAt}`
|
||||
@@ -33,7 +245,14 @@ const make: Effect.Effect<Interface, never, Session.Service | Job.Service | Scop
|
||||
notifications.add(key)
|
||||
yield* Effect.gen(function* () {
|
||||
const info = (yield* jobs.wait({ id: recovery.childSessionID })).info
|
||||
if (info) yield* SubagentCompletion.deliver(sessions, jobs, { ...info, recovery })
|
||||
if (!info || info.status === "running") return
|
||||
if (info.status === "cancelled") {
|
||||
yield* sessions.interrupt(recovery.childSessionID)
|
||||
yield* SubagentCompletion.deliver(sessions, jobs, { ...info, recovery })
|
||||
return
|
||||
}
|
||||
yield* sync(recovery.childSessionID)
|
||||
if (info.notificationID) yield* jobs.completeBackground(info.notificationID)
|
||||
}).pipe(
|
||||
Effect.ensuring(Effect.sync(() => notifications.delete(key))),
|
||||
Effect.forkIn(scope, { startImmediately: true }),
|
||||
@@ -41,36 +260,165 @@ const make: Effect.Effect<Interface, never, Session.Service | Job.Service | Scop
|
||||
)
|
||||
})
|
||||
|
||||
return Service.of({
|
||||
start: (recovery: Recovery) =>
|
||||
jobs
|
||||
.start({
|
||||
const start = (recovery: Recovery) =>
|
||||
jobs
|
||||
.start({
|
||||
id: recovery.childSessionID,
|
||||
type: "subagent",
|
||||
title: recovery.description,
|
||||
metadata: {},
|
||||
recovery,
|
||||
run: Effect.gen(function* () {
|
||||
settled.delete(recovery.childSessionID)
|
||||
yield* sessions.resume(recovery.childSessionID).pipe(
|
||||
Effect.onError(() =>
|
||||
latestIdle(recovery.childSessionID).pipe(
|
||||
Effect.map((idle) => {
|
||||
if (idle) settled.set(recovery.childSessionID, idle)
|
||||
}),
|
||||
Effect.ignore,
|
||||
),
|
||||
),
|
||||
)
|
||||
return yield* latestTurnOutput(recovery.childSessionID)
|
||||
}),
|
||||
})
|
||||
.pipe(Effect.setContext(context))
|
||||
|
||||
const track = Effect.fn("SubagentJob.track")((recovery: Recovery) =>
|
||||
locks.withLock(recovery.childSessionID)(
|
||||
Effect.gen(function* () {
|
||||
const existing = yield* readMarker(recovery.childSessionID)
|
||||
if (existing) yield* syncUnlocked(recovery.childSessionID)
|
||||
const refreshed = existing ? yield* readMarker(recovery.childSessionID) : undefined
|
||||
const reported = refreshed ? refreshed.reported : ((yield* latestIdle(recovery.childSessionID)) ?? null)
|
||||
const now = yield* Clock.currentTimeMillis
|
||||
const shared = {
|
||||
id: recovery.childSessionID,
|
||||
type: "subagent",
|
||||
session_id: recovery.parentSessionID,
|
||||
title: recovery.description,
|
||||
metadata: {},
|
||||
recovery,
|
||||
run: Effect.gen(function* () {
|
||||
yield* sessions.resume(recovery.childSessionID)
|
||||
const messages = yield* sessions.messages({ sessionID: recovery.childSessionID, order: "desc", limit: 20 })
|
||||
const assistant = messages.find(
|
||||
(message) =>
|
||||
message.type === "assistant" && message.time.completed !== undefined && message.error === undefined,
|
||||
)
|
||||
return SubagentCompletion.text(assistant)
|
||||
}),
|
||||
})
|
||||
.pipe(Effect.setContext(context)),
|
||||
reported,
|
||||
dirty: true,
|
||||
time_updated: now,
|
||||
}
|
||||
yield* db
|
||||
.insert(JobTable)
|
||||
.values({
|
||||
...shared,
|
||||
notification_id: SessionMessage.ID.create(),
|
||||
child_session_id: recovery.childSessionID,
|
||||
kind: "subagent",
|
||||
status: "completed",
|
||||
pending: false,
|
||||
time_created: now,
|
||||
})
|
||||
.onConflictDoUpdate({ target: JobTable.child_session_id, set: shared })
|
||||
.run()
|
||||
.pipe(Effect.orDie)
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
const block: Interface["block"] = (recovery, prompt) =>
|
||||
Effect.uninterruptibleMask((restore) =>
|
||||
Effect.gen(function* () {
|
||||
const childID = recovery.childSessionID
|
||||
let consumeSettled = false
|
||||
foreground.set(childID, (foreground.get(childID) ?? 0) + 1)
|
||||
return yield* Effect.gen(function* () {
|
||||
yield* restore(prompt)
|
||||
yield* start(recovery)
|
||||
const outcome = yield* restore(
|
||||
jobs
|
||||
.block({ id: childID, sessionID: recovery.parentSessionID })
|
||||
.pipe(
|
||||
Effect.onInterrupt(() =>
|
||||
Effect.all([sessions.interrupt(childID), jobs.cancel(childID)], { discard: true }),
|
||||
),
|
||||
),
|
||||
)
|
||||
if (outcome?.type === "backgrounded") {
|
||||
yield* notify(recovery, outcome.info.started_at)
|
||||
return outcome
|
||||
}
|
||||
if (outcome?.info.status === "cancelled") yield* sessions.interrupt(childID)
|
||||
consumeSettled = true
|
||||
return outcome
|
||||
}).pipe(
|
||||
Effect.ensuring(
|
||||
locks
|
||||
.withLock(childID)(
|
||||
Effect.gen(function* () {
|
||||
if (consumeSettled) {
|
||||
const marker = yield* readMarker(childID)
|
||||
const idle = settled.get(childID)
|
||||
if (marker && idle && marker.reported !== idle) yield* updateMarker(childID, idle, true)
|
||||
}
|
||||
const remaining = (foreground.get(childID) ?? 1) - 1
|
||||
if (remaining <= 0) {
|
||||
foreground.delete(childID)
|
||||
settled.delete(childID)
|
||||
yield* syncUnlocked(childID)
|
||||
} else {
|
||||
foreground.set(childID, remaining)
|
||||
}
|
||||
}),
|
||||
)
|
||||
.pipe(Effect.orDie),
|
||||
),
|
||||
)
|
||||
}),
|
||||
)
|
||||
|
||||
const recover: Interface["recover"] = (suspended, prepare) =>
|
||||
Effect.acquireUseRelease(
|
||||
Effect.sync(() => {
|
||||
for (const id of suspended) deferredParents.add(id)
|
||||
}),
|
||||
() =>
|
||||
prepare.pipe(
|
||||
Effect.tap(() =>
|
||||
db
|
||||
.select({ childSessionID: JobTable.child_session_id })
|
||||
.from(JobTable)
|
||||
.where(eq(JobTable.dirty, true))
|
||||
.orderBy(asc(JobTable.time_created), asc(JobTable.notification_id))
|
||||
.all()
|
||||
.pipe(
|
||||
Effect.orDie,
|
||||
Effect.flatMap((rows) =>
|
||||
Effect.forEach(rows, (row) => (row.childSessionID ? sync(row.childSessionID) : Effect.void), {
|
||||
discard: true,
|
||||
}),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
() =>
|
||||
Effect.sync(() => {
|
||||
for (const id of suspended) deferredParents.delete(id)
|
||||
}),
|
||||
)
|
||||
|
||||
return Service.of({
|
||||
track,
|
||||
start,
|
||||
block,
|
||||
background: Effect.fn("SubagentJob.background")(function* (recovery: Recovery) {
|
||||
const info = yield* jobs.background(recovery.childSessionID)
|
||||
if (info) yield* notify(recovery, info.started_at)
|
||||
}),
|
||||
notify,
|
||||
sync,
|
||||
recover,
|
||||
})
|
||||
})
|
||||
|
||||
export const layer = Layer.effect(Service, make)
|
||||
|
||||
export const node: LayerNode.Provider<Service, never, typeof Node.tags.values.global> = Node.makeGlobalNode({
|
||||
service: Service,
|
||||
layer: Layer.effect(Service, make),
|
||||
deps: [Session.node, Job.node],
|
||||
layer,
|
||||
deps: [Database.node, Session.node, SessionStore.node, Job.node, Bus.node],
|
||||
})
|
||||
|
||||
@@ -6,7 +6,6 @@ import type { SessionHooks } from "@opencode/plugin/effect/session"
|
||||
import { Effect, Predicate, Schema } from "effect"
|
||||
import { Agent } from "../../agent.js"
|
||||
import { Config } from "../../config.js"
|
||||
import { Job } from "../../job.js"
|
||||
import { Model } from "../../model.js"
|
||||
import { Permission } from "../../permission.js"
|
||||
import { Session } from "../../session.js"
|
||||
@@ -65,7 +64,6 @@ export const Plugin = {
|
||||
id: "opencode.tool.subagent",
|
||||
effect: Effect.fn("SubagentTool.Plugin")(function* (ctx: Context) {
|
||||
const sessions = yield* Session.Service
|
||||
const jobs = yield* Job.Service
|
||||
const agents = yield* Agent.Service
|
||||
const config = yield* Config.Service
|
||||
const permission = yield* Permission.Service
|
||||
@@ -206,23 +204,6 @@ export const Plugin = {
|
||||
const background = input.background === true
|
||||
yield* context.progress({ sessionID: child.id, status: "running" })
|
||||
|
||||
// Standard prompt admission outside the job: Job.start joining a running child skips
|
||||
// its run effect, and the default wake starts an idle child or steers a running one.
|
||||
yield* sessions
|
||||
.prompt({
|
||||
sessionID: child.id,
|
||||
text:
|
||||
existing === undefined
|
||||
? ["You are a subagent spawned by another session.", input.prompt].join("\n")
|
||||
: input.prompt,
|
||||
...(background && existing === undefined ? { resume: false } : {}),
|
||||
})
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(error) => new ToolFailure({ message: `Failed to prompt subagent: ${child.id}`, error }),
|
||||
),
|
||||
)
|
||||
|
||||
const recovery = {
|
||||
kind: "subagent" as const,
|
||||
parentSessionID: context.sessionID,
|
||||
@@ -230,24 +211,35 @@ export const Plugin = {
|
||||
agent: agent.name,
|
||||
description: input.description,
|
||||
}
|
||||
yield* subagents.start(recovery)
|
||||
yield* subagents.track(recovery)
|
||||
|
||||
// Standard prompt admission outside the job: Job.start joining a running child skips
|
||||
// its run effect, and the default wake starts an idle child or steers a running one.
|
||||
const admitPrompt = sessions
|
||||
.prompt({
|
||||
sessionID: child.id,
|
||||
metadata: { source: "subagent" },
|
||||
text:
|
||||
existing === undefined
|
||||
? ["You are a subagent spawned by another session.", input.prompt].join("\n")
|
||||
: input.prompt,
|
||||
...(existing === undefined ? { resume: false } : {}),
|
||||
})
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(error) => new ToolFailure({ message: `Failed to prompt subagent: ${child.id}`, error }),
|
||||
),
|
||||
)
|
||||
|
||||
if (background) {
|
||||
yield* admitPrompt
|
||||
yield* subagents.start(recovery)
|
||||
yield* subagents.background(recovery)
|
||||
return backgroundResult(child.id)
|
||||
}
|
||||
|
||||
const result = yield* jobs.block({ id: child.id, sessionID: context.sessionID }).pipe(
|
||||
Effect.onInterrupt(() =>
|
||||
Effect.all([sessions.interrupt(child.id), jobs.cancel(child.id)], {
|
||||
discard: true,
|
||||
}),
|
||||
),
|
||||
)
|
||||
if (result?.type === "backgrounded") {
|
||||
yield* subagents.notify(recovery, result.info.started_at)
|
||||
return backgroundResult(child.id)
|
||||
}
|
||||
const result = yield* subagents.block(recovery, admitPrompt)
|
||||
if (result?.type === "backgrounded") return backgroundResult(child.id)
|
||||
// Failure surfaces keep the sessionID visible so the model can continue the child.
|
||||
if (result?.info.status === "error")
|
||||
return yield* new ToolFailure({
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, expect } from "bun:test"
|
||||
import { Database } from "@opencode/core/database/database"
|
||||
import jobMigration from "@opencode/core/database/migration/20261030054400_job"
|
||||
import jobSubagentMigration from "@opencode/core/database/migration/20261030204714_job_subagent"
|
||||
import { Job } from "@opencode/core/job"
|
||||
import { JobTable } from "@opencode/core/job/sql"
|
||||
import { KVTable } from "@opencode/core/kv/sql"
|
||||
@@ -519,7 +520,12 @@ describe("Job", () => {
|
||||
})
|
||||
.run()
|
||||
|
||||
yield* db.transaction((tx) => jobMigration.up(tx))
|
||||
yield* db.transaction((tx) =>
|
||||
Effect.gen(function* () {
|
||||
yield* jobMigration.up(tx)
|
||||
yield* jobSubagentMigration.up(tx)
|
||||
}),
|
||||
)
|
||||
|
||||
expect(yield* db.select().from(KVTable).where(like(KVTable.key, "job.background/%")).all()).toEqual([])
|
||||
expect(yield* jobs.pendingBackground).toMatchObject([
|
||||
|
||||
@@ -1,17 +1,21 @@
|
||||
import type { FileSystem } from "@opencode/core/filesystem"
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { AIError, TransportError } from "@opencode/ai/effect"
|
||||
import { Agent } from "@opencode/core/agent"
|
||||
import { Database } from "@opencode/core/database/database"
|
||||
import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
|
||||
import { LayerNode } from "@opencode/util/effect/layer-node"
|
||||
import { Bus } from "@opencode/core/bus"
|
||||
import { Instance } from "@opencode/core/instance/service"
|
||||
import { Job } from "@opencode/core/job"
|
||||
import { JobTable } from "@opencode/core/job/sql"
|
||||
import { KV } from "@opencode/core/kv"
|
||||
import { LocationServiceMap } from "@opencode/core/location-service-map"
|
||||
import type { LocationServices } from "@opencode/core/location-services"
|
||||
import { Model } from "@opencode/core/model"
|
||||
import { Project } from "@opencode/core/project"
|
||||
import { ProjectTable } from "@opencode/core/project/sql"
|
||||
import { Provider } from "@opencode/core/provider"
|
||||
import { AbsolutePath } from "@opencode/core/schema"
|
||||
import { Session } from "@opencode/core/session"
|
||||
import { SessionExecution } from "@opencode/core/session/execution"
|
||||
@@ -22,6 +26,8 @@ import { SessionMessage } from "@opencode/core/session/message"
|
||||
import { SessionRunner } from "@opencode/core/session/runner/index"
|
||||
import { SessionInboxTable, SessionTable } from "@opencode/core/session/sql"
|
||||
import { SessionStore } from "@opencode/core/session/store"
|
||||
import { SubagentJob } from "@opencode/core/session/subagent-job"
|
||||
import { Money } from "@opencode/schema/money"
|
||||
import { Cause, Context, Deferred, Effect, Exit, Fiber, Layer, LayerMap, Scope } from "effect"
|
||||
import { eq } from "drizzle-orm"
|
||||
import { testEffect } from "./lib/effect"
|
||||
@@ -785,6 +791,7 @@ describe("SessionRestart background recovery", () => {
|
||||
.filter((message) => message.type === "synthetic")
|
||||
.map((message) => message.text),
|
||||
})
|
||||
yield* recordAssistantText(bus, sessionID, "Recovered result")
|
||||
yield* Deferred.await(release)
|
||||
}),
|
||||
{ maxAttempts: 2 },
|
||||
@@ -827,12 +834,16 @@ describe("SessionRestart background recovery", () => {
|
||||
it.effect("resumes a background subagent and notifies its parent exactly once", () =>
|
||||
Effect.gen(function* () {
|
||||
const database = yield* Database.Service
|
||||
const bus = yield* Bus.Service
|
||||
const jobs = yield* Job.Service
|
||||
const parent = Session.ID.make("ses_subagent_recovery_parent")
|
||||
const child = Session.ID.make("ses_subagent_recovery_child")
|
||||
const unrelated = Session.ID.make("ses_subagent_unrelated_child")
|
||||
yield* seedSessions(database, [parent], { time_suspended: Date.now(), resume_attempts: 1 })
|
||||
yield* seedSessions(database, [child, unrelated], { parent_id: parent, time_suspended: Date.now() })
|
||||
// Pre-upgrade subagent prompts had no `metadata.source`; `pending = true` preserves delivery on upgrade.
|
||||
yield* seedInbox(database, child, ["steer"])
|
||||
yield* SessionInbox.promote(database.db, bus, child, "steer")
|
||||
yield* jobs.start({
|
||||
id: child,
|
||||
type: "subagent",
|
||||
@@ -861,6 +872,7 @@ describe("SessionRestart background recovery", () => {
|
||||
Effect.gen(function* () {
|
||||
drained.push(sessionID)
|
||||
if (sessionID === child) {
|
||||
yield* recordAssistantText(bus, sessionID, "Recovered result")
|
||||
yield* Deferred.succeed(resumed, undefined)
|
||||
yield* Deferred.await(release)
|
||||
return
|
||||
@@ -904,30 +916,19 @@ describe("SessionRestart background recovery", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("delivers a subagent result persisted before restart without rerunning the child", () =>
|
||||
it.effect("delivers persisted subagent results in order after restart without rerunning the child", () =>
|
||||
Effect.gen(function* () {
|
||||
const database = yield* Database.Service
|
||||
const jobs = yield* Job.Service
|
||||
const bus = yield* Bus.Service
|
||||
const parent = Session.ID.make("ses_subagent_completed_parent")
|
||||
const child = Session.ID.make("ses_subagent_completed_child")
|
||||
yield* seedSessions(database, [parent])
|
||||
yield* seedSessions(database, [child], { parent_id: parent })
|
||||
const complete = yield* Deferred.make<string>()
|
||||
yield* jobs.start({
|
||||
id: child,
|
||||
type: "subagent",
|
||||
recovery: {
|
||||
kind: "subagent",
|
||||
parentSessionID: parent,
|
||||
childSessionID: child,
|
||||
agent: "explore",
|
||||
description: "Completed inspection",
|
||||
},
|
||||
run: Deferred.await(complete),
|
||||
})
|
||||
yield* jobs.background(child)
|
||||
yield* Deferred.succeed(complete, "Recovered result")
|
||||
yield* jobs.wait({ id: child })
|
||||
yield* seedSubagentMarker(database, parent, child, "Completed inspection")
|
||||
yield* recordAssistantText(bus, child, "First recovered result")
|
||||
yield* bus.publish(SessionEvent.Execution.Succeeded, { sessionID: child })
|
||||
yield* recordAssistantText(bus, child, "Second recovered result")
|
||||
const settled = yield* bus.publish(SessionEvent.Execution.Succeeded, { sessionID: child })
|
||||
|
||||
const parentWoken = yield* Deferred.make<void>()
|
||||
const drained: Session.ID[] = []
|
||||
@@ -948,40 +949,105 @@ describe("SessionRestart background recovery", () => {
|
||||
|
||||
expect(drained).toEqual([parent])
|
||||
expect(yield* SessionInbox.list(database.db, parent)).toMatchObject([
|
||||
{ payload: { text: expect.stringContaining("Recovered result"), metadata: { state: "completed" } } },
|
||||
{ payload: { text: expect.stringContaining("First recovered result"), metadata: { state: "completed" } } },
|
||||
{ payload: { text: expect.stringContaining("Second recovered result"), metadata: { state: "completed" } } },
|
||||
])
|
||||
expect(yield* restarted.pendingBackground).toEqual([])
|
||||
expect(
|
||||
yield* database.db.select().from(JobTable).where(eq(JobTable.child_session_id, child)).get(),
|
||||
).toMatchObject({
|
||||
pending: false,
|
||||
reported: SessionMessage.ID.fromEvent(settled.id),
|
||||
dirty: false,
|
||||
})
|
||||
}),
|
||||
)
|
||||
|
||||
for (const delivered of [false, true]) {
|
||||
it.effect(`does not duplicate a legacy subagent notification already ${delivered ? "delivered" : "admitted"}`, () =>
|
||||
Effect.gen(function* () {
|
||||
const database = yield* Database.Service
|
||||
const bus = yield* Bus.Service
|
||||
const jobs = yield* Job.Service
|
||||
const parent = Session.ID.make("ses_subagent_legacy_parent")
|
||||
const child = Session.ID.make("ses_subagent_legacy_child")
|
||||
yield* seedSessions(database, [parent])
|
||||
yield* seedSessions(database, [child], { parent_id: parent })
|
||||
yield* recordAssistantText(bus, child, "Legacy result")
|
||||
yield* bus.publish(SessionEvent.Execution.Succeeded, { sessionID: child })
|
||||
yield* jobs.start({
|
||||
id: child,
|
||||
type: "subagent",
|
||||
recovery: {
|
||||
kind: "subagent",
|
||||
parentSessionID: parent,
|
||||
childSessionID: child,
|
||||
agent: "explore",
|
||||
description: "Legacy completion",
|
||||
},
|
||||
run: Effect.succeed("Legacy result"),
|
||||
})
|
||||
const backgrounded = yield* jobs.background(child)
|
||||
yield* jobs.wait({ id: child })
|
||||
const notificationID = backgrounded?.notificationID
|
||||
if (!notificationID) return yield* Effect.die("notification ID missing")
|
||||
const sessions = yield* Session.Service
|
||||
yield* sessions.synthetic({
|
||||
id: notificationID,
|
||||
sessionID: parent,
|
||||
text: "Legacy result already admitted",
|
||||
metadata: { source: "subagent", childID: child, agent: "explore", state: "completed" },
|
||||
resume: false,
|
||||
})
|
||||
if (delivered) yield* SessionInbox.promote(database.db, bus, parent, "steer")
|
||||
|
||||
const woken = yield* Deferred.make<void>()
|
||||
const drained: Session.ID[] = []
|
||||
const scope = yield* Scope.make()
|
||||
yield* Effect.addFinalizer(() => Scope.close(scope, Exit.void))
|
||||
const restarted = yield* Job.make.pipe(Effect.provideService(Scope.Scope, scope))
|
||||
const context = yield* buildExecution(
|
||||
scope,
|
||||
({ sessionID }) =>
|
||||
Effect.sync(() => void drained.push(sessionID)).pipe(Effect.andThen(Deferred.succeed(woken, undefined))),
|
||||
undefined,
|
||||
restarted,
|
||||
)
|
||||
yield* Context.get(context, SessionRestart.Service).resumeSuspendedSessions
|
||||
if (!delivered) yield* Deferred.await(woken)
|
||||
|
||||
expect(drained).toEqual(delivered ? [] : [parent])
|
||||
expect(yield* restarted.pendingBackground).toEqual([])
|
||||
expect(yield* SessionInbox.list(database.db, parent)).toHaveLength(delivered ? 0 : 1)
|
||||
if (!delivered) yield* SessionInbox.promote(database.db, bus, parent, "steer")
|
||||
const messages = (yield* sessions.messages({ sessionID: parent })).filter((message) => message.type !== "idle")
|
||||
expect(messages).toMatchObject([
|
||||
{
|
||||
id: notificationID,
|
||||
type: "synthetic",
|
||||
text: "Legacy result already admitted",
|
||||
metadata: { state: "completed" },
|
||||
},
|
||||
])
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
it.effect("retains a subagent completion marker when synthetic admission conflicts", () =>
|
||||
Effect.gen(function* () {
|
||||
const database = yield* Database.Service
|
||||
const bus = yield* Bus.Service
|
||||
const admission = yield* SessionInbox.Service
|
||||
const jobs = yield* Job.Service
|
||||
const sessions = yield* Session.Service
|
||||
const parent = Session.ID.make("ses_completion_conflict_parent")
|
||||
const child = Session.ID.make("ses_completion_conflict_child")
|
||||
yield* seedSessions(database, [parent])
|
||||
yield* seedSessions(database, [child], { parent_id: parent })
|
||||
yield* jobs.start({
|
||||
id: child,
|
||||
type: "subagent",
|
||||
recovery: {
|
||||
kind: "subagent",
|
||||
parentSessionID: parent,
|
||||
childSessionID: child,
|
||||
agent: "explore",
|
||||
description: "Completed inspection",
|
||||
},
|
||||
run: Effect.succeed("Recovered result"),
|
||||
})
|
||||
yield* jobs.wait({ id: child })
|
||||
yield* jobs.background(child)
|
||||
const marker = (yield* jobs.pendingBackground)[0]
|
||||
if (!marker) return yield* Effect.die("background record missing")
|
||||
yield* seedSubagentMarker(database, parent, child, "Completed inspection")
|
||||
yield* recordAssistantText(bus, child, "Recovered result")
|
||||
const settled = yield* bus.publish(SessionEvent.Execution.Succeeded, { sessionID: child })
|
||||
yield* admission.admit({
|
||||
id: marker.notificationID,
|
||||
id: SessionMessage.ID.make(`${SessionMessage.ID.fromEvent(settled.id)}_subagent`),
|
||||
sessionID: parent,
|
||||
item: { type: "user", payload: { text: "User input" }, delivery: "steer" },
|
||||
})
|
||||
@@ -992,7 +1058,12 @@ describe("SessionRestart background recovery", () => {
|
||||
const exit = yield* Context.get(context, SessionRestart.Service).resumeSuspendedSessions.pipe(Effect.exit)
|
||||
expect(Exit.isFailure(exit)).toBe(true)
|
||||
if (Exit.isFailure(exit)) expect(Cause.squash(exit.cause)).toBeInstanceOf(Session.SyntheticConflictError)
|
||||
expect(yield* jobs.pendingBackground).toEqual([marker])
|
||||
expect(
|
||||
yield* database.db.select().from(JobTable).where(eq(JobTable.child_session_id, child)).get(),
|
||||
).toMatchObject({
|
||||
reported: null,
|
||||
dirty: true,
|
||||
})
|
||||
expect(yield* sessions.inbox(parent)).toMatchObject([{ type: "user", payload: { text: "User input" } }])
|
||||
}),
|
||||
)
|
||||
@@ -1002,7 +1073,6 @@ describe("SessionRestart background recovery", () => {
|
||||
Effect.gen(function* () {
|
||||
const database = yield* Database.Service
|
||||
const bus = yield* Bus.Service
|
||||
const jobs = yield* Job.Service
|
||||
const parent = Session.ID.make("ses_subagent_budget_parent")
|
||||
const children = [
|
||||
Session.ID.make("ses_subagent_budget_child_1"),
|
||||
@@ -1010,24 +1080,11 @@ describe("SessionRestart background recovery", () => {
|
||||
]
|
||||
yield* seedSessions(database, [parent], { time_suspended: Date.now(), resume_attempts: resumeAttempts })
|
||||
yield* seedSessions(database, children, { parent_id: parent })
|
||||
const complete = yield* Deferred.make<string>()
|
||||
for (const child of children) {
|
||||
yield* jobs.start({
|
||||
id: child,
|
||||
type: "subagent",
|
||||
recovery: {
|
||||
kind: "subagent",
|
||||
parentSessionID: parent,
|
||||
childSessionID: child,
|
||||
agent: "explore",
|
||||
description: "Completed inspection",
|
||||
},
|
||||
run: Deferred.await(complete),
|
||||
})
|
||||
yield* jobs.background(child)
|
||||
yield* seedSubagentMarker(database, parent, child, "Completed inspection")
|
||||
yield* recordAssistantText(bus, child, "Recovered result")
|
||||
yield* bus.publish(SessionEvent.Execution.Succeeded, { sessionID: child })
|
||||
}
|
||||
yield* Deferred.succeed(complete, "Recovered result")
|
||||
yield* Effect.forEach(children, (id) => jobs.wait({ id }), { discard: true })
|
||||
|
||||
const draining = yield* Deferred.make<number | undefined>()
|
||||
const release = yield* Deferred.make<void>()
|
||||
@@ -1415,6 +1472,7 @@ function buildExecution(
|
||||
)
|
||||
return yield* Layer.buildWithScope(
|
||||
SessionRestart.layer(options).pipe(
|
||||
Layer.provideMerge(Layer.fresh(SubagentJob.layer)),
|
||||
Layer.provideMerge(sessionLayer),
|
||||
Layer.provideMerge(Layer.fresh(SessionExecution.layer)),
|
||||
Layer.provide(Layer.succeed(Database.Service, database)),
|
||||
@@ -1430,3 +1488,51 @@ function buildExecution(
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
function seedSubagentMarker(
|
||||
database: Database.Interface,
|
||||
parentSessionID: Session.ID,
|
||||
childSessionID: Session.ID,
|
||||
description: string,
|
||||
) {
|
||||
const now = Date.now()
|
||||
return database.db
|
||||
.insert(JobTable)
|
||||
.values({
|
||||
notification_id: SessionMessage.ID.create(),
|
||||
id: childSessionID,
|
||||
session_id: parentSessionID,
|
||||
child_session_id: childSessionID,
|
||||
kind: "subagent",
|
||||
title: description,
|
||||
status: "completed",
|
||||
recovery: { kind: "subagent", parentSessionID, childSessionID, agent: "explore", description },
|
||||
pending: false,
|
||||
reported: null,
|
||||
dirty: false,
|
||||
time_created: now,
|
||||
time_updated: now,
|
||||
})
|
||||
.run()
|
||||
.pipe(Effect.orDie)
|
||||
}
|
||||
|
||||
const recordAssistantText = Effect.fnUntraced(function* (bus: Bus.Interface, sessionID: Session.ID, text: string) {
|
||||
const assistantMessageID = SessionMessage.ID.create()
|
||||
yield* bus.publish(SessionEvent.Step.Started, {
|
||||
sessionID,
|
||||
assistantMessageID,
|
||||
agent: Agent.ID.make("explore"),
|
||||
model: { id: Model.ID.make("test"), providerID: Provider.ID.make("test") },
|
||||
started: 0,
|
||||
})
|
||||
yield* bus.publish(SessionEvent.Text.Started, { sessionID, assistantMessageID, ordinal: 0 })
|
||||
yield* bus.publish(SessionEvent.Text.Ended, { sessionID, assistantMessageID, ordinal: 0, text })
|
||||
yield* bus.publish(SessionEvent.Step.Ended, {
|
||||
sessionID,
|
||||
assistantMessageID,
|
||||
finish: "stop",
|
||||
cost: Money.USD.zero,
|
||||
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, expect } from "bun:test"
|
||||
import { Deferred, Effect, Fiber, Layer, Schema, Stream } from "effect"
|
||||
import { LanguageModel } from "@opencode/ai/effect"
|
||||
import { eq } from "drizzle-orm"
|
||||
import { Deferred, Effect, Exit, Fiber, Layer, Option, Queue, Schema, Scope, Stream } from "effect"
|
||||
import { AIError, InvalidRequestError, LanguageModel } from "@opencode/ai/effect"
|
||||
import { OpenAIChat } from "@opencode/ai/protocols"
|
||||
import { TestLLM } from "@opencode/ai/testing"
|
||||
import path from "path"
|
||||
@@ -20,7 +21,6 @@ import { AbsolutePath } from "@opencode/core/schema"
|
||||
import { Agent } from "@opencode/core/agent"
|
||||
import { Job } from "@opencode/core/job"
|
||||
import { JobTable } from "@opencode/core/job/sql"
|
||||
import { KV } from "@opencode/core/kv"
|
||||
import { LocationServiceMap } from "@opencode/core/location-service-map"
|
||||
import { Session } from "@opencode/core/session"
|
||||
import { SessionEvent } from "@opencode/core/session/event"
|
||||
@@ -37,6 +37,7 @@ import { PluginSupervisor } from "@opencode/core/plugin/supervisor"
|
||||
import { Permission } from "@opencode/core/permission"
|
||||
import { SubagentTool } from "@opencode/core/tool/plugin/subagent"
|
||||
import { Tool } from "@opencode/core/tool"
|
||||
import { Watcher } from "@opencode/core/filesystem/watcher"
|
||||
import { tmpdir } from "./fixture/tmpdir"
|
||||
import { tempGlobalLayer } from "./fixture/global"
|
||||
import { offlineModels } from "./fixture/models"
|
||||
@@ -69,8 +70,12 @@ const executionNode = makeGlobalNode({
|
||||
const complete = Effect.fn("SubagentTest.complete")(function* (sessionID: Session.ID) {
|
||||
if (completed.has(sessionID)) return
|
||||
if ((yield* store.get(sessionID))?.title?.includes("fail")) {
|
||||
yield* new SessionRunnerModel.ModelNotSelectedError({ sessionID })
|
||||
return
|
||||
const error = new SessionRunnerModel.ModelNotSelectedError({ sessionID })
|
||||
yield* bus.publish(SessionEvent.Execution.Failed, {
|
||||
sessionID,
|
||||
error: { type: "model.unavailable", message: error.message },
|
||||
})
|
||||
return yield* error
|
||||
}
|
||||
completed.add(sessionID)
|
||||
const assistantMessageID = SessionMessage.ID.create()
|
||||
@@ -99,6 +104,7 @@ const executionNode = makeGlobalNode({
|
||||
cost: Money.USD.zero,
|
||||
tokens,
|
||||
})
|
||||
yield* bus.publish(SessionEvent.Execution.Succeeded, { sessionID })
|
||||
})
|
||||
return SessionExecution.Service.of({
|
||||
active: Effect.succeed(new Set()),
|
||||
@@ -151,29 +157,108 @@ const productionIt = testEffect(AppNodeBuilder.build(nodes, replacements))
|
||||
const it = testEffect(
|
||||
AppNodeBuilder.build(nodes, [...replacements, PluginSupervisor.node.replace(subagentPluginSupervisor)]),
|
||||
)
|
||||
const completionIt = testEffect(
|
||||
AppNodeBuilder.build(LayerNode.group([nodes, SessionRestart.node, KV.node]), [
|
||||
Global.node.replace(tempGlobalLayer),
|
||||
offlineModels,
|
||||
PluginSupervisor.node.replace(subagentPluginSupervisor),
|
||||
LayerNodePlatform.llmClient.replace(TestLLM.testLayer({ fallback: TestLLM.text(childText, "completion") })),
|
||||
SessionRunnerModel.node.replace(
|
||||
Layer.succeed(SessionRunnerModel.Service, {
|
||||
resolve: () =>
|
||||
Effect.succeed(
|
||||
SessionRunnerModel.resolved(
|
||||
LanguageModel.make({ id: "child", provider: "test", route: OpenAIChat.route }),
|
||||
{
|
||||
capabilities: { tools: true, input: ["text"], output: ["text"] },
|
||||
cost: [],
|
||||
limit: { context: 200_000, output: 32_000 },
|
||||
},
|
||||
const liveExecution = (fallback: TestLLM.Response, extraReplacements: LayerNode.Replacements = []) => {
|
||||
const llm = TestLLM.testLayer({ fallback })
|
||||
return Layer.merge(
|
||||
llm,
|
||||
AppNodeBuilder.build(LayerNode.group([nodes, SessionRestart.node, SubagentJob.node]), [
|
||||
Global.node.replace(tempGlobalLayer),
|
||||
offlineModels,
|
||||
Watcher.node.replace(Watcher.configured({ enabled: false })),
|
||||
PluginSupervisor.node.replace(subagentPluginSupervisor),
|
||||
LayerNodePlatform.llmClient.replace(llm),
|
||||
SessionRunnerModel.node.replace(
|
||||
Layer.succeed(SessionRunnerModel.Service, {
|
||||
resolve: () =>
|
||||
Effect.succeed(
|
||||
SessionRunnerModel.resolved(
|
||||
LanguageModel.make({ id: "child", provider: "test", route: OpenAIChat.route }),
|
||||
{
|
||||
capabilities: { tools: true, input: ["text"], output: ["text"] },
|
||||
cost: [],
|
||||
limit: { context: 200_000, output: 32_000 },
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
}),
|
||||
),
|
||||
...extraReplacements,
|
||||
]),
|
||||
)
|
||||
}
|
||||
const completionIt = testEffect(liveExecution(TestLLM.text(childText, "completion")))
|
||||
|
||||
const subagentRow = (database: Database.Interface, childID: Session.ID) =>
|
||||
database.db.select().from(JobTable).where(eq(JobTable.child_session_id, childID)).get()
|
||||
|
||||
// Launches a background child of a fresh parent and collects the synthetic messages the parent receives.
|
||||
const withBackgroundChild = <A, E, R>(
|
||||
callID: string,
|
||||
body: (child: {
|
||||
readonly id: Session.ID
|
||||
readonly parentID: Session.ID
|
||||
readonly sessions: Session.Interface
|
||||
readonly database: Database.Interface
|
||||
readonly llm: TestLLM.TestInterface
|
||||
readonly notifications: Queue.Queue<string>
|
||||
readonly completed: (text: string) => string
|
||||
readonly errored: (text: string) => string
|
||||
readonly settle: Effect.Effect<void>
|
||||
}) => Effect.Effect<A, E, R>,
|
||||
responses: readonly TestLLM.Response[] = [],
|
||||
) =>
|
||||
Effect.acquireRelease(
|
||||
Effect.promise(() => tmpdir()),
|
||||
(dir) => Effect.promise(() => dir[Symbol.asyncDispose]()),
|
||||
).pipe(
|
||||
Effect.flatMap((dir) =>
|
||||
Effect.gen(function* () {
|
||||
const sessions = yield* Session.Service
|
||||
const database = yield* Database.Service
|
||||
const llm = yield* TestLLM.Test
|
||||
if (responses.length > 0) yield* llm.push(...responses)
|
||||
const parent = yield* sessions.create({
|
||||
location: Location.Ref.make({ directory: AbsolutePath.make(dir.path) }),
|
||||
model: parentModel,
|
||||
title: "Background child recipient",
|
||||
})
|
||||
yield* withSubagent(parent.location)
|
||||
const locations = yield* LocationServiceMap.Service
|
||||
const registry = yield* Tool.Service.pipe(Effect.provide(locations.get(parent.location)))
|
||||
const bus = yield* Bus.Service
|
||||
const notifications = yield* Queue.unbounded<string>()
|
||||
yield* bus.project(SessionEvent.InboxEnqueued, (event) =>
|
||||
event.data.sessionID === parent.id && event.data.item.type === "synthetic"
|
||||
? Queue.offer(notifications, event.data.item.payload.text).pipe(Effect.asVoid)
|
||||
: Effect.void,
|
||||
)
|
||||
const result = yield* executeTool(registry, {
|
||||
sessionID: parent.id,
|
||||
...toolIdentity,
|
||||
call: {
|
||||
type: "tool-call",
|
||||
id: callID,
|
||||
name: SubagentTool.name,
|
||||
input: { agent: "reviewer", description: "background review", prompt: "review", background: true },
|
||||
},
|
||||
})
|
||||
const id = outputSessionID(result.metadata)
|
||||
return yield* body({
|
||||
id,
|
||||
parentID: parent.id,
|
||||
sessions,
|
||||
database,
|
||||
llm,
|
||||
notifications,
|
||||
completed: (text) =>
|
||||
`<subagent sessionID="${id}" state="completed" description="background review">\n${text}\n</subagent>`,
|
||||
errored: (text) =>
|
||||
`<subagent sessionID="${id}" state="error" description="background review">\n${text}\n</subagent>`,
|
||||
settle: Effect.all([sessions.wait(id), sessions.wait(parent.id)], { discard: true }).pipe(Effect.orDie),
|
||||
})
|
||||
}),
|
||||
),
|
||||
]),
|
||||
)
|
||||
)
|
||||
|
||||
const withSubagent = (location: Location.Ref) =>
|
||||
Effect.gen(function* () {
|
||||
@@ -232,17 +317,15 @@ describe("SubagentTool", () => {
|
||||
const locations = yield* LocationServiceMap.Service
|
||||
const registry = yield* Tool.Service.pipe(Effect.provide(locations.get(parent.location)))
|
||||
const jobs = yield* Job.Service
|
||||
const database = yield* Database.Service
|
||||
const bus = yield* Bus.Service
|
||||
const admitted = yield* Deferred.make<Job.Background>()
|
||||
const admitted = yield* Deferred.make<SessionMessage.ID>()
|
||||
const notifications: SessionMessage.ID[] = []
|
||||
yield* bus.project(SessionEvent.InboxEnqueued, (event) =>
|
||||
Effect.gen(function* () {
|
||||
if (event.data.sessionID !== parent.id || event.data.item.type !== "synthetic") return
|
||||
notifications.push(event.data.inboxID)
|
||||
const marker = (yield* jobs.pendingBackground).find((job) => job.notificationID === event.data.inboxID)
|
||||
// The marker must survive until admission commits, not merely until delivery starts.
|
||||
expect(marker?.status).toBe("completed")
|
||||
if (marker) yield* Deferred.succeed(admitted, marker)
|
||||
yield* Deferred.succeed(admitted, event.data.inboxID)
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -256,54 +339,441 @@ describe("SubagentTool", () => {
|
||||
input: { agent: "reviewer", description: "background review", prompt: "review", background: true },
|
||||
},
|
||||
})
|
||||
const marker = yield* Deferred.await(admitted)
|
||||
const childID = outputSessionID(result.metadata)
|
||||
const noticeID = yield* Deferred.await(admitted)
|
||||
yield* jobs.pendingBackground.pipe(Effect.repeat({ until: (pending) => pending.length === 0 }))
|
||||
yield* sessions.wait(parent.id)
|
||||
const messages = (yield* sessions.context(parent.id)).filter((message) => message.type === "synthetic")
|
||||
expect(messages).toEqual([
|
||||
expect.objectContaining({
|
||||
id: marker.notificationID,
|
||||
id: noticeID,
|
||||
description: "background review",
|
||||
text: `<subagent sessionID="${outputSessionID(result.metadata)}" state="completed" description="background review">\n${childText}\n</subagent>`,
|
||||
text: `<subagent sessionID="${childID}" state="completed" description="background review">\n${childText}\n</subagent>`,
|
||||
metadata: {
|
||||
source: "subagent",
|
||||
childID: outputSessionID(result.metadata),
|
||||
childID,
|
||||
agent: "reviewer",
|
||||
state: "completed",
|
||||
},
|
||||
}),
|
||||
])
|
||||
const saved = yield* subagentRow(database, childID)
|
||||
expect(saved).toMatchObject({
|
||||
session_id: parent.id,
|
||||
child_session_id: childID,
|
||||
kind: "subagent",
|
||||
pending: false,
|
||||
reported: expect.stringMatching(/^msg_/),
|
||||
dirty: false,
|
||||
})
|
||||
|
||||
// Reproduce a crash after admission but before acknowledgment using the real persisted row.
|
||||
const database = yield* Database.Service
|
||||
// Reproduce a crash after synthetic admission commits, before the saved marker advances.
|
||||
yield* database.db
|
||||
.insert(JobTable)
|
||||
.values({
|
||||
notification_id: marker.notificationID,
|
||||
id: marker.id,
|
||||
session_id: parent.id,
|
||||
child_session_id: outputSessionID(result.metadata),
|
||||
kind: marker.recovery.kind,
|
||||
title: "background review",
|
||||
status: marker.status,
|
||||
output: marker.output ?? null,
|
||||
error: marker.error ?? null,
|
||||
recovery: marker.recovery,
|
||||
})
|
||||
.update(JobTable)
|
||||
.set({ reported: null, dirty: true })
|
||||
.where(eq(JobTable.child_session_id, childID))
|
||||
.run()
|
||||
const restart = yield* SessionRestart.Service
|
||||
yield* restart.resumeSuspendedSessions
|
||||
yield* sessions.wait(parent.id)
|
||||
expect(notifications).toEqual([marker.notificationID])
|
||||
expect(notifications).toEqual([noticeID])
|
||||
expect((yield* sessions.context(parent.id)).filter((message) => message.type === "synthetic")).toEqual(
|
||||
messages,
|
||||
)
|
||||
expect(yield* subagentRow(database, childID)).toMatchObject({
|
||||
pending: false,
|
||||
reported: saved?.reported,
|
||||
dirty: false,
|
||||
})
|
||||
expect(yield* jobs.pendingBackground).toEqual([])
|
||||
}),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
for (const background of [true, false]) {
|
||||
completionIt.live(
|
||||
`notifies the parent when a ${background ? "background" : "foreground"} child continues after its initial report`,
|
||||
() =>
|
||||
Effect.acquireRelease(
|
||||
Effect.promise(() => tmpdir()),
|
||||
(dir) => Effect.promise(() => dir[Symbol.asyncDispose]()),
|
||||
).pipe(
|
||||
Effect.flatMap((dir) =>
|
||||
Effect.gen(function* () {
|
||||
const sessions = yield* Session.Service
|
||||
const llm = yield* TestLLM.Test
|
||||
yield* llm.push(TestLLM.text("Interim update.", "interim"))
|
||||
const parent = yield* sessions.create({
|
||||
location: Location.Ref.make({ directory: AbsolutePath.make(dir.path) }),
|
||||
model: parentModel,
|
||||
title: "Continuation recipient",
|
||||
})
|
||||
yield* withSubagent(parent.location)
|
||||
const locations = yield* LocationServiceMap.Service
|
||||
const registry = yield* Tool.Service.pipe(Effect.provide(locations.get(parent.location)))
|
||||
const bus = yield* Bus.Service
|
||||
const notifications = yield* Queue.unbounded<string>()
|
||||
yield* bus.project(SessionEvent.InboxEnqueued, (event) =>
|
||||
event.data.sessionID === parent.id && event.data.item.type === "synthetic"
|
||||
? Queue.offer(notifications, event.data.item.payload.text).pipe(Effect.asVoid)
|
||||
: Effect.void,
|
||||
)
|
||||
|
||||
const result = yield* executeTool(registry, {
|
||||
sessionID: parent.id,
|
||||
...toolIdentity,
|
||||
call: {
|
||||
type: "tool-call",
|
||||
id: `call-continuation-${background}`,
|
||||
name: SubagentTool.name,
|
||||
input: {
|
||||
agent: "reviewer",
|
||||
description: "background review",
|
||||
prompt: "review",
|
||||
background,
|
||||
},
|
||||
},
|
||||
})
|
||||
const childID = outputSessionID(result.metadata)
|
||||
const framed = (text: string) =>
|
||||
`<subagent sessionID="${childID}" state="completed" description="background review">\n${text}\n</subagent>`
|
||||
|
||||
if (background) {
|
||||
expect(yield* Queue.take(notifications)).toBe(framed("Interim update."))
|
||||
}
|
||||
if (!background) {
|
||||
expect(result.content).toEqual([
|
||||
{
|
||||
type: "text",
|
||||
text: `<subagent sessionID="${childID}" state="completed">\nInterim update.\n</subagent>`,
|
||||
},
|
||||
])
|
||||
}
|
||||
yield* Effect.all([sessions.wait(childID), sessions.wait(parent.id)], { discard: true })
|
||||
expect(yield* Queue.size(notifications)).toBe(0)
|
||||
|
||||
// Something other than the subagent tool (a plugin or background shell) continues the child.
|
||||
yield* sessions.synthetic({ sessionID: childID, text: "Continue from where you left off." })
|
||||
const followUp = yield* Queue.take(notifications).pipe(Effect.timeoutOption("3 seconds"))
|
||||
expect(Option.getOrUndefined(followUp)).toBe(framed(childText))
|
||||
yield* Effect.all([sessions.wait(childID), sessions.wait(parent.id)], { discard: true })
|
||||
expect(yield* Queue.size(notifications)).toBe(0)
|
||||
}),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
completionIt.live("does not wake the parent on empty successes, then reports the later text turn", () =>
|
||||
withBackgroundChild(
|
||||
"call-empty-continuations",
|
||||
(child) =>
|
||||
Effect.gen(function* () {
|
||||
// Neither the initial empty step nor empty plugin retries wake the parent.
|
||||
yield* child.settle
|
||||
expect(yield* Queue.size(child.notifications)).toBe(0)
|
||||
|
||||
for (const _ of [1, 2]) {
|
||||
yield* child.llm.push(TestLLM.stop())
|
||||
yield* child.sessions.synthetic({ sessionID: child.id, text: "Continue from where you left off." })
|
||||
yield* child.settle
|
||||
}
|
||||
expect(yield* Queue.size(child.notifications)).toBe(0)
|
||||
|
||||
yield* child.sessions.synthetic({ sessionID: child.id, text: "Continue from where you left off." })
|
||||
const followUp = yield* Queue.take(child.notifications).pipe(Effect.timeoutOption("3 seconds"))
|
||||
expect(Option.getOrUndefined(followUp)).toBe(child.completed(childText))
|
||||
yield* child.settle
|
||||
expect(yield* Queue.size(child.notifications)).toBe(0)
|
||||
}),
|
||||
[TestLLM.stop()],
|
||||
),
|
||||
)
|
||||
|
||||
completionIt.live("reports a failed continuation to the parent", () =>
|
||||
withBackgroundChild("call-failed-continuation", (child) =>
|
||||
Effect.gen(function* () {
|
||||
expect(yield* Queue.take(child.notifications)).toBe(child.completed(childText))
|
||||
yield* child.settle
|
||||
|
||||
yield* child.llm.push(
|
||||
Stream.fail(new AIError({ reason: new InvalidRequestError({ message: "provider boom" }) })),
|
||||
)
|
||||
yield* child.sessions.synthetic({ sessionID: child.id, text: "Continue from where you left off." })
|
||||
const failed = yield* Queue.take(child.notifications).pipe(Effect.timeoutOption("3 seconds"))
|
||||
expect(Option.getOrUndefined(failed)).toBe(child.errored("provider boom"))
|
||||
yield* child.settle
|
||||
expect(yield* Queue.size(child.notifications)).toBe(0)
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
completionIt.live("does not notify the parent when a continuation is interrupted", () =>
|
||||
withBackgroundChild("call-interrupted-continuation", (child) =>
|
||||
Effect.gen(function* () {
|
||||
expect(yield* Queue.take(child.notifications)).toBe(child.completed(childText))
|
||||
|
||||
// The parent's own turn after the notification must not take the scripted response.
|
||||
yield* child.settle
|
||||
const requests = (yield* child.llm.requests()).length
|
||||
yield* child.llm.push(Stream.never)
|
||||
yield* child.sessions.synthetic({ sessionID: child.id, text: "Continue from where you left off." })
|
||||
yield* child.llm.wait(requests + 1)
|
||||
yield* child.sessions.interrupt(child.id)
|
||||
yield* child.settle
|
||||
expect(yield* Queue.size(child.notifications)).toBe(0)
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
completionIt.live("does not notify a parent being deleted while its child continues and cleans up markers", () =>
|
||||
withBackgroundChild("call-deleted-parent", (child) =>
|
||||
Effect.gen(function* () {
|
||||
expect(yield* Queue.take(child.notifications)).toBe(child.completed(childText))
|
||||
expect(yield* subagentRow(child.database, child.id)).toBeDefined()
|
||||
|
||||
// The parent's own turn after the notification must not take the scripted response.
|
||||
yield* child.settle
|
||||
const requests = (yield* child.llm.requests()).length
|
||||
yield* child.llm.push(Stream.never)
|
||||
yield* child.sessions.synthetic({ sessionID: child.id, text: "Continue from where you left off." })
|
||||
yield* child.llm.wait(requests + 1)
|
||||
yield* child.sessions.remove(child.parentID)
|
||||
expect(yield* Queue.size(child.notifications)).toBe(0)
|
||||
expect(Exit.isFailure(yield* child.sessions.get(child.id).pipe(Effect.exit))).toBe(true)
|
||||
expect(yield* subagentRow(child.database, child.id)).toBeUndefined()
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
completionIt.live("cleans up the saved marker when the child session is deleted", () =>
|
||||
withBackgroundChild("call-deleted-child", (child) =>
|
||||
Effect.gen(function* () {
|
||||
expect(yield* Queue.take(child.notifications)).toBe(child.completed(childText))
|
||||
yield* child.settle
|
||||
expect(yield* subagentRow(child.database, child.id)).toBeDefined()
|
||||
|
||||
yield* child.sessions.remove(child.id)
|
||||
expect(yield* subagentRow(child.database, child.id)).toBeUndefined()
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
completionIt.live("still notifies the parent when a user steers a running background child", () =>
|
||||
Effect.acquireRelease(
|
||||
Effect.promise(() => tmpdir()),
|
||||
(dir) => Effect.promise(() => dir[Symbol.asyncDispose]()),
|
||||
).pipe(
|
||||
Effect.flatMap((dir) =>
|
||||
Effect.gen(function* () {
|
||||
const sessions = yield* Session.Service
|
||||
const llm = yield* TestLLM.Test
|
||||
const gate = yield* llm.gate()
|
||||
const parent = yield* sessions.create({
|
||||
location: Location.Ref.make({ directory: AbsolutePath.make(dir.path) }),
|
||||
model: parentModel,
|
||||
title: "Steered child recipient",
|
||||
})
|
||||
yield* withSubagent(parent.location)
|
||||
const locations = yield* LocationServiceMap.Service
|
||||
const registry = yield* Tool.Service.pipe(Effect.provide(locations.get(parent.location)))
|
||||
const bus = yield* Bus.Service
|
||||
const notifications = yield* Queue.unbounded<string>()
|
||||
yield* bus.project(SessionEvent.InboxEnqueued, (event) =>
|
||||
event.data.sessionID === parent.id && event.data.item.type === "synthetic"
|
||||
? Queue.offer(notifications, event.data.item.payload.text).pipe(Effect.asVoid)
|
||||
: Effect.void,
|
||||
)
|
||||
|
||||
const launched = yield* executeTool(registry, {
|
||||
sessionID: parent.id,
|
||||
...toolIdentity,
|
||||
call: {
|
||||
type: "tool-call",
|
||||
id: "call-steered-child",
|
||||
name: SubagentTool.name,
|
||||
input: { agent: "reviewer", description: "background review", prompt: "review", background: true },
|
||||
},
|
||||
})
|
||||
const childID = outputSessionID(launched.metadata)
|
||||
yield* gate.started
|
||||
// Steering into a turn the parent started keeps that turn owned by the subagent request.
|
||||
yield* sessions.prompt({ sessionID: childID, text: "Also check edge cases." })
|
||||
yield* llm.push(TestLLM.text("Steered final answer.", "steered"))
|
||||
yield* gate.release
|
||||
|
||||
const delivered = yield* Queue.take(notifications).pipe(Effect.timeoutOption("3 seconds"))
|
||||
expect(Option.getOrUndefined(delivered)).toBe(
|
||||
`<subagent sessionID="${childID}" state="completed" description="background review">\nSteered final answer.\n</subagent>`,
|
||||
)
|
||||
yield* Effect.all([sessions.wait(childID), sessions.wait(parent.id)], { discard: true })
|
||||
expect(yield* Queue.size(notifications)).toBe(0)
|
||||
}),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
completionIt.live("does not notify the parent when someone prompts a child directly", () =>
|
||||
withBackgroundChild("call-direct-prompt", (child) =>
|
||||
Effect.gen(function* () {
|
||||
expect(yield* Queue.take(child.notifications)).toContain(childText)
|
||||
yield* child.settle
|
||||
|
||||
yield* child.sessions.prompt({ sessionID: child.id, text: "One more question." })
|
||||
yield* child.settle
|
||||
expect(yield* Queue.size(child.notifications)).toBe(0)
|
||||
|
||||
// Later subagent continuations still report to the parent.
|
||||
yield* child.llm.push(TestLLM.text("Back on task.", "resumed"))
|
||||
yield* child.sessions.synthetic({ sessionID: child.id, text: "Background shell finished." })
|
||||
const followUp = yield* Queue.take(child.notifications).pipe(Effect.timeoutOption("3 seconds"))
|
||||
expect(Option.getOrUndefined(followUp)).toBe(child.completed("Back on task."))
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
completionIt.live("delivers multiple child turns in order without losing any", () =>
|
||||
withBackgroundChild(
|
||||
"call-ordered-turns",
|
||||
(child) =>
|
||||
Effect.gen(function* () {
|
||||
expect(yield* Queue.take(child.notifications)).toBe(child.completed("turn one"))
|
||||
yield* child.settle
|
||||
|
||||
// Pause the parent so both child follow-ups land and queue in order.
|
||||
yield* child.llm.push(TestLLM.text("turn two", "t2"))
|
||||
const second = yield* child.sessions.synthetic({ sessionID: child.id, text: "Step two." })
|
||||
expect(yield* Queue.take(child.notifications)).toBe(child.completed("turn two"))
|
||||
yield* child.settle
|
||||
|
||||
// Reverting turn two re-anchors `job.reported` to turn one so later turns still deliver.
|
||||
yield* child.sessions.revert.stage({ sessionID: child.id, messageID: second.id, files: false })
|
||||
yield* child.llm.push(TestLLM.text("turn three", "t3"))
|
||||
yield* child.sessions.prompt({
|
||||
sessionID: child.id,
|
||||
metadata: { source: "subagent" },
|
||||
text: "Step three.",
|
||||
})
|
||||
expect(yield* Queue.take(child.notifications)).toBe(child.completed("turn three"))
|
||||
yield* child.settle
|
||||
expect(yield* Queue.size(child.notifications)).toBe(0)
|
||||
}),
|
||||
[TestLLM.text("turn one", "t1")],
|
||||
),
|
||||
)
|
||||
|
||||
testEffect(Layer.empty).live(
|
||||
"preserves the saved marker across a server restart without repeating reported turns",
|
||||
() =>
|
||||
Effect.acquireRelease(
|
||||
Effect.promise(() => tmpdir()),
|
||||
(dir) => Effect.promise(() => dir[Symbol.asyncDispose]()),
|
||||
).pipe(
|
||||
Effect.flatMap((dir) =>
|
||||
Effect.gen(function* () {
|
||||
const dbPath = path.join(dir.path, "restart.db")
|
||||
const projectDir = path.join(dir.path, "project")
|
||||
yield* Effect.promise(() => Bun.write(path.join(projectDir, ".keep"), ""))
|
||||
const boot = (fallback: string) =>
|
||||
liveExecution(TestLLM.text(fallback, "boot"), [
|
||||
Database.node.replace(Database.configured({ path: dbPath })),
|
||||
])
|
||||
|
||||
// First server process: background child completes turn 1 and reports it.
|
||||
const firstScope = yield* Scope.make()
|
||||
const first = yield* Layer.buildWithScope(boot("before restart"), firstScope)
|
||||
const { parentID, childID } = yield* Effect.gen(function* () {
|
||||
const sessions = yield* Session.Service
|
||||
const bus = yield* Bus.Service
|
||||
const locations = yield* LocationServiceMap.Service
|
||||
const parent = yield* sessions.create({
|
||||
location: Location.Ref.make({ directory: AbsolutePath.make(projectDir) }),
|
||||
model: parentModel,
|
||||
title: "Restart parent",
|
||||
})
|
||||
yield* withSubagent(parent.location)
|
||||
const registry = yield* Tool.Service.pipe(Effect.provide(locations.get(parent.location)))
|
||||
const notices = yield* Queue.unbounded<string>()
|
||||
yield* bus.project(SessionEvent.InboxEnqueued, (event) =>
|
||||
event.data.sessionID === parent.id && event.data.item.type === "synthetic"
|
||||
? Queue.offer(notices, event.data.item.payload.text).pipe(Effect.asVoid)
|
||||
: Effect.void,
|
||||
)
|
||||
const launched = yield* executeTool(registry, {
|
||||
sessionID: parent.id,
|
||||
...toolIdentity,
|
||||
call: {
|
||||
type: "tool-call",
|
||||
id: "call-across-restart",
|
||||
name: SubagentTool.name,
|
||||
input: { agent: "reviewer", description: "restart review", prompt: "review", background: true },
|
||||
},
|
||||
})
|
||||
const id = outputSessionID(launched.metadata)
|
||||
expect(yield* Queue.take(notices)).toBe(
|
||||
`<subagent sessionID="${id}" state="completed" description="restart review">\nbefore restart\n</subagent>`,
|
||||
)
|
||||
yield* Effect.all([sessions.wait(id), sessions.wait(parent.id)], { discard: true })
|
||||
return { parentID: parent.id, childID: id }
|
||||
}).pipe(Effect.provide(first))
|
||||
yield* Scope.close(firstScope, Exit.void)
|
||||
|
||||
// Second server process on the same SQLite database: turn 1 is not repeated, and turn 2 reports cleanly.
|
||||
const secondScope = yield* Scope.make()
|
||||
yield* Effect.addFinalizer(() => Scope.close(secondScope, Exit.void))
|
||||
const second = yield* Layer.buildWithScope(boot("after restart"), secondScope)
|
||||
yield* Effect.gen(function* () {
|
||||
const sessions = yield* Session.Service
|
||||
const restart = yield* SessionRestart.Service
|
||||
const database = yield* Database.Service
|
||||
const bus = yield* Bus.Service
|
||||
yield* withSubagent(Location.Ref.make({ directory: AbsolutePath.make(projectDir) }))
|
||||
const notices = yield* Queue.unbounded<string>()
|
||||
yield* bus.project(SessionEvent.InboxEnqueued, (event) =>
|
||||
event.data.sessionID === parentID && event.data.item.type === "synthetic"
|
||||
? Queue.offer(notices, event.data.item.payload.text).pipe(Effect.asVoid)
|
||||
: Effect.void,
|
||||
)
|
||||
yield* restart.resumeSuspendedSessions
|
||||
expect(yield* Queue.size(notices)).toBe(0)
|
||||
|
||||
yield* sessions.synthetic({ sessionID: childID, text: "Continue after server restart." })
|
||||
const afterRestart = yield* Queue.take(notices).pipe(Effect.timeoutOption("3 seconds"))
|
||||
expect(Option.getOrUndefined(afterRestart)).toBe(
|
||||
`<subagent sessionID="${childID}" state="completed" description="restart review">\nafter restart\n</subagent>`,
|
||||
)
|
||||
yield* Effect.all([sessions.wait(childID), sessions.wait(parentID)], { discard: true })
|
||||
expect(yield* Queue.size(notices)).toBe(0)
|
||||
|
||||
// Simulate a crash before two completed child turns were reported: catch-up reconciles both in order.
|
||||
const llm = yield* TestLLM.Test
|
||||
const reportedBeforeCatchUp = (yield* subagentRow(database, childID))?.reported ?? null
|
||||
yield* llm.push(TestLLM.text("missed one", "m1"))
|
||||
yield* sessions.synthetic({ sessionID: childID, text: "First missed turn." })
|
||||
expect(yield* Queue.take(notices)).toContain("missed one")
|
||||
yield* Effect.all([sessions.wait(childID), sessions.wait(parentID)], { discard: true })
|
||||
|
||||
yield* llm.push(TestLLM.text("missed two", "m2"))
|
||||
yield* sessions.synthetic({ sessionID: childID, text: "Second missed turn." })
|
||||
expect(yield* Queue.take(notices)).toContain("missed two")
|
||||
yield* Effect.all([sessions.wait(childID), sessions.wait(parentID)], { discard: true })
|
||||
|
||||
// Rewinding the marker before those two turns reconciles both idempotently on restart without duplicates.
|
||||
yield* database.db
|
||||
.update(JobTable)
|
||||
.set({ reported: reportedBeforeCatchUp, dirty: true })
|
||||
.where(eq(JobTable.child_session_id, childID))
|
||||
.run()
|
||||
yield* restart.resumeSuspendedSessions
|
||||
expect(yield* Queue.size(notices)).toBe(0)
|
||||
expect((yield* subagentRow(database, childID))?.dirty).toBe(false)
|
||||
}).pipe(Effect.provide(second))
|
||||
}),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
productionIt.live("registers globally while resolving agents from the caller location", () =>
|
||||
Effect.acquireRelease(
|
||||
Effect.promise(() => tmpdir()),
|
||||
@@ -619,7 +1089,7 @@ describe("SubagentTool", () => {
|
||||
yield* jobs.start({
|
||||
id: child.id,
|
||||
type: SubagentTool.name,
|
||||
run: Deferred.await(gate).pipe(Effect.as("done")),
|
||||
run: Deferred.await(gate).pipe(Effect.andThen(sessions.resume(child.id)), Effect.as("done")),
|
||||
})
|
||||
const notified = yield* bus.subscribe(SessionEvent.InboxEnqueued).pipe(
|
||||
Stream.filter((event) => event.data.sessionID === parent.id && event.data.item.type === "synthetic"),
|
||||
|
||||
@@ -30,6 +30,7 @@ import { LocationServiceMap } from "@opencode/core/location-service-map"
|
||||
import { LocationActivity } from "@opencode/core/location-activity"
|
||||
import { ModelsDev } from "@opencode/core/models-dev"
|
||||
import { SessionRestart } from "@opencode/core/session/execution/restart"
|
||||
import { SubagentJob } from "@opencode/core/session/subagent-job"
|
||||
import { Schedule } from "@opencode/core/schedule"
|
||||
import { PluginUpdate } from "@opencode/core/plugin/update"
|
||||
import { SdkPlugins } from "@opencode/core/plugin/sdk"
|
||||
@@ -80,6 +81,7 @@ const applicationServiceNodes = [
|
||||
LocationServiceMap.node,
|
||||
LocationActivity.node,
|
||||
SessionRestart.node,
|
||||
SubagentJob.node,
|
||||
Schedule.node,
|
||||
Workspace.node,
|
||||
FileWatch.node,
|
||||
|
||||
Reference in New Issue
Block a user