diff --git a/packages/core/src/session/execution.ts b/packages/core/src/session/execution.ts index 2788d9ea0d4..267e5582cce 100644 --- a/packages/core/src/session/execution.ts +++ b/packages/core/src/session/execution.ts @@ -1,6 +1,6 @@ export * as SessionExecution from "./execution.js" -import { Cause, Context, Effect, Exit, Layer } from "effect" +import { Cause, Context, Effect, Exit, Layer, Scope } from "effect" import { Bus } from "../bus.js" import { Database } from "../database/database.js" import { Job } from "../job.js" @@ -56,6 +56,7 @@ export function terminal(exit: Exit.Exit, reason?: export const layer = Layer.effect( Service, Effect.gen(function* () { + const scope = yield* Scope.Scope const store = yield* SessionStore.Service const instances = yield* Instance.Service const bus = yield* Bus.Service @@ -113,6 +114,8 @@ export const layer = Layer.effect( store, jobs, inbox, + // Long-lived: each busy period forks one job watcher here and settlement interrupts it. + scope, // The coordinator is created below; a report only runs after a busy period has settled. wake: (sessionID) => coordinator.wake(sessionID), pending: (sessionID) => SessionBackground.unsettled(jobs, store, db, sessionID), diff --git a/packages/core/src/session/execution/subagent-report.ts b/packages/core/src/session/execution/subagent-report.ts index f4971921db5..8b935e1d5c9 100644 --- a/packages/core/src/session/execution/subagent-report.ts +++ b/packages/core/src/session/execution/subagent-report.ts @@ -1,6 +1,6 @@ export * as SubagentReport from "./subagent-report.js" -import { Effect } from "effect" +import { Effect, Fiber, Scope } from "effect" import type { Job } from "../../job.js" import { SessionInbox } from "../inbox.js" import { SessionMessage } from "../message.js" @@ -11,6 +11,12 @@ import { SubagentMetadata } from "../subagent-metadata.js" type Recovery = Extract +type Bound = { + readonly generation: number + settled: Job.Info | undefined + fiber: Fiber.Fiber | undefined +} + /** The terminal outcome of one busy period, the same mapping execution publishes. */ export type Outcome = | { readonly type: "succeeded" } @@ -25,7 +31,11 @@ export type Snapshot = { readonly blocked: boolean readonly job: Job.Info | undefined readonly recovery: Job.Recovery | undefined - readonly remembered: boolean + /** + * The job generation running when this period began was cancelled during it, so its observer or + * its cancelled caller already owns the result. + */ + readonly owned: boolean /** The child's newest answer when the period began; undefined when that was never read. */ readonly baseline: { readonly id: SessionMessage.ID | undefined } | undefined } @@ -37,6 +47,8 @@ export type Deps = { readonly wake: (sessionID: SessionSchema.ID) => Effect.Effect /** True while the child still has background work or undelivered input that will wake it again. */ readonly pending: (sessionID: SessionSchema.ID) => Effect.Effect + /** Long-lived scope for per-period job watchers. */ + readonly scope: Scope.Scope } /** @@ -45,9 +57,10 @@ export type Deps = { * same notification ID, so first admission wins and the parent sees one message. */ export const make = (deps: Deps) => { - // Busy periods that began with a running job. That job owns its report whenever it finishes or - // is cancelled before settlement, so settlement must not send a second one. - const jobPeriods = new Set() + // The job generation running when a busy period began. Its watcher records that generation's + // terminal when it settles within the period, because only a cancellation there owns the + // period's report: completion or failure is settling an earlier period. Dropped at settlement. + const periods = new Map() // Children whose last succeeded period held its report for background work. Process-local: after // a restart, recovery and background notices wake the child instead. const held = new Set() @@ -65,24 +78,60 @@ export const make = (deps: Deps) => { }) const started = Effect.fnUntraced(function* (sessionID: SessionSchema.ID) { + // A record left behind means the previous period never settled; drop its watcher so it cannot + // write into this one. + const leftover = periods.get(sessionID) + if (leftover) { + periods.delete(sessionID) + if (leftover.job?.fiber) yield* Fiber.interrupt(leftover.job.fiber) + } const job = yield* deps.jobs.get(sessionID) - if (job?.status === "running") jobPeriods.add(sessionID) // A finished entry from an earlier generation must not make a re-prompt silently reconcile. - else jobPeriods.delete(sessionID) + if (job?.status !== "running") periods.set(sessionID, { job: undefined }) + else { + const bound: Bound = { generation: job.started_at, settled: undefined, fiber: undefined } + // Record before forking: startImmediately can run the watcher inline when this generation + // already settled. Every write is guarded by the record, so an old watcher cannot mark a + // later period. + periods.set(sessionID, { job: bound }) + bound.fiber = yield* deps.jobs.wait({ id: sessionID }).pipe( + Effect.flatMap((result) => + Effect.sync(() => { + if (periods.get(sessionID)?.job !== bound) return + const info = result.info + if (info === undefined || info.status === "running" || info.started_at !== bound.generation) return + bound.settled = info + }), + ), + Effect.forkIn(deps.scope, { startImmediately: true }), + ) + } baselines.delete(sessionID) if (!(yield* deps.store.get(sessionID))?.parentID) return baselines.set(sessionID, (yield* lastAssistant(sessionID))?.id) }) const snapshot = Effect.fnUntraced(function* (sessionID: SessionSchema.ID): Effect.fn.Return { - const remembered = jobPeriods.delete(sessionID) + const period = periods.get(sessionID) + periods.delete(sessionID) + const job = yield* deps.jobs.get(sessionID) + const bound = period?.job + // A cancellation whose watcher continuation has not been scheduled yet: the entry still names + // the generation that began this period. + const settled = + bound?.settled ?? + (bound !== undefined && job !== undefined && job.status !== "running" && job.started_at === bound.generation + ? job + : undefined) + // Read the watcher's write before interrupting it, or a resumed continuation loses it. + if (bound?.fiber) yield* Fiber.interrupt(bound.fiber) const baseline = baselines.has(sessionID) ? { id: baselines.get(sessionID) } : undefined baselines.delete(sessionID) return { blocked: yield* deps.jobs.isBlocked(sessionID), - job: yield* deps.jobs.get(sessionID), + job, recovery: yield* deps.jobs.recovery(sessionID), - remembered, + owned: settled?.status === "cancelled", baseline, } }) @@ -114,8 +163,10 @@ export const make = (deps: Deps) => { // A running job without a notification is a foreground spawn whose block has not registered, // or one whose block already returned. Either way that job owns the result. if (live && !live.notificationID) return - // A job that finished or was cancelled mid-period already reported through its observer. - if (!live && snap.remembered) return + // A generation that began this period and was cancelled during it already reported through its + // observer or its cancelled caller. One that completed or failed during the period was settling + // an earlier period, so this period still reports its own answer with a fresh message ID. + if (!live && snap.owned) return // The child's own background work will wake it again, and that later settlement reports the // final state. Failures and user interrupts report now. Mark held before asking, so a silent // descendant settling meanwhile still wakes this child; a spurious wake sends nothing. diff --git a/packages/core/test/subagent-report.test.ts b/packages/core/test/subagent-report.test.ts index 48b1f868f74..b531569e830 100644 --- a/packages/core/test/subagent-report.test.ts +++ b/packages/core/test/subagent-report.test.ts @@ -1,6 +1,6 @@ import { describe, expect } from "bun:test" import { eq } from "drizzle-orm" -import { Effect, Layer } from "effect" +import { Deferred, Effect, Layer } from "effect" import { LanguageModel } from "@opencode/ai/effect" import { OpenAIChat } from "@opencode/ai/protocols" import { TestLLM } from "@opencode/ai/testing" @@ -88,6 +88,29 @@ const synthetics = (sessions: Session.Interface, sessionID: Session.ID) => Effect.map((messages) => messages.filter((message) => message.type === "synthetic")), ) +/** + * A job that stands in for the child's spawn without an observer or a notification ID, so only + * settlement can report a busy period it overlaps. + */ +const startChildJob = ( + jobs: Job.Interface, + parent: SessionSchema.Info, + child: SessionSchema.Info, + run: Effect.Effect, +) => + jobs.start({ + id: child.id, + type: "subagent", + recovery: { + kind: "subagent", + parentSessionID: parent.id, + childSessionID: child.id, + agent: "reviewer", + description: "Review fixture", + }, + run, + }) + describe("subagent report", () => { it.live("reports a directly prompted child to its parent once", () => Effect.gen(function* () { @@ -160,6 +183,79 @@ describe("subagent report", () => { }), ) + it.live("reports a period whose starting job completed during it", () => + Effect.gen(function* () { + const { sessions, parent, child } = yield* family() + const jobs = yield* Job.Service + const release = yield* Deferred.make() + const llm = yield* TestLLM.Test + const gate = yield* llm.gate() + yield* startChildJob( + jobs, + parent, + child, + Deferred.await(release).pipe(Effect.as("stale answer")), + ) + yield* sessions.prompt({ sessionID: child.id, text: "Review the fixture" }) + yield* gate.started + // The generation that began this period finishes on its own, settling an earlier period. + yield* Deferred.succeed(release, undefined) + expect((yield* jobs.wait({ id: child.id })).info?.status).toBe("completed") + yield* gate.release + yield* sessions.wait(child.id) + yield* sessions.wait(parent.id) + + const reports = yield* synthetics(sessions, parent.id) + expect(reports).toHaveLength(1) + expect(reports[0]).toMatchObject({ + metadata: { source: "subagent", childID: child.id, state: "completed" }, + }) + expect(reports[0]?.text).toContain(childText) + }), + ) + + it.live("stays silent when the starting job is cancelled during the period", () => + Effect.gen(function* () { + const { sessions, parent, child } = yield* family() + const jobs = yield* Job.Service + const llm = yield* TestLLM.Test + const gate = yield* llm.gate() + yield* startChildJob(jobs, parent, child, Effect.never) + yield* sessions.prompt({ sessionID: child.id, text: "Review the fixture" }) + yield* gate.started + yield* jobs.cancel(child.id) + yield* gate.release + yield* sessions.wait(child.id) + yield* sessions.wait(parent.id) + + expect(yield* synthetics(sessions, parent.id)).toEqual([]) + }), + ) + + it.live("does not let a cancelled earlier period silence a later one", () => + Effect.gen(function* () { + const { sessions, parent, child } = yield* family() + const jobs = yield* Job.Service + yield* startChildJob(jobs, parent, child, Effect.never) + yield* sessions.prompt({ sessionID: child.id, text: "Review the fixture" }) + yield* sessions.wait(child.id) + yield* sessions.wait(parent.id) + // The running job owns period one, so only its cancellation could have reported it. + expect(yield* synthetics(sessions, parent.id)).toEqual([]) + yield* jobs.cancel(child.id) + yield* sessions.prompt({ sessionID: child.id, text: "Review it again" }) + yield* sessions.wait(child.id) + yield* sessions.wait(parent.id) + + const reports = yield* synthetics(sessions, parent.id) + expect(reports).toHaveLength(1) + expect(reports[0]).toMatchObject({ + metadata: { source: "subagent", childID: child.id, state: "completed" }, + }) + expect(reports[0]?.text).toContain(childText) + }), + ) + it.live("settles without a parent", () => Effect.gen(function* () { const { sessions, child } = yield* family()