From 7e8c1699bfdacb93c372f1d66b6815b5dcf7f369 Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Wed, 4 Nov 2026 17:27:45 -0800 Subject: [PATCH] fix(core): release a held subagent report when background work finishes silently --- .../src/session/execution/subagent-report.ts | 18 ++++++++++++++++-- packages/core/src/session/subagent-job.ts | 19 +++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/packages/core/src/session/execution/subagent-report.ts b/packages/core/src/session/execution/subagent-report.ts index eb6893ecc43..6c055258670 100644 --- a/packages/core/src/session/execution/subagent-report.ts +++ b/packages/core/src/session/execution/subagent-report.ts @@ -46,6 +46,9 @@ 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() + // 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() const started = Effect.fnUntraced(function* (sessionID: SessionSchema.ID) { const job = yield* deps.jobs.get(sessionID) @@ -76,10 +79,18 @@ export const make = (deps: Deps) => { const report = Effect.fnUntraced(function* (sessionID: SessionSchema.ID, outcome: Outcome, snap: Snapshot) { // Shutdown keeps the claim, and restart owns the report that resumes it. if (outcome.type === "interrupted" && outcome.reason === "shutdown") return + // Every settlement decides afresh; a period that holds again re-adds the child below. This + // returns true for the period that releases an earlier hold. + held.delete(sessionID) const child = yield* deps.store.get(sessionID) if (!child?.parentID) return - if (!SubagentMetadata.notify(child.metadata)) return const parentID = child.parentID + if (!SubagentMetadata.notify(child.metadata)) { + // A silent descendant sends no notice, so wake a held parent to report its final state. The + // wake runs no model step when the parent has no input. + if (held.has(parentID) && (yield* deps.store.get(parentID))) yield* deps.wake(parentID) + return + } if (!(yield* deps.store.get(parentID))) return // A foreground caller receives this result inline through its own blocking registration. if (snap.blocked) return @@ -91,7 +102,10 @@ export const make = (deps: Deps) => { if (!live && snap.remembered) 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. - if (outcome.type === "succeeded" && (yield* deps.pending(child.id))) return + if (outcome.type === "succeeded" && (yield* deps.pending(child.id))) { + held.add(child.id) + return + } const recovery = subagentRecovery(live?.notificationID ? snap.recovery : undefined, child, parentID) const status = outcome.type === "succeeded" ? "completed" : outcome.type === "interrupted" ? "cancelled" : "error" diff --git a/packages/core/src/session/subagent-job.ts b/packages/core/src/session/subagent-job.ts index 0be57297425..5c01066973b 100644 --- a/packages/core/src/session/subagent-job.ts +++ b/packages/core/src/session/subagent-job.ts @@ -9,6 +9,7 @@ import { Job } from "../job.js" import { Session } from "../session.js" import { SessionBackground } from "./background.js" import { SessionEvent } from "./event.js" +import { SessionExecution } from "./execution.js" import type { SessionSchema } from "./schema.js" import { SessionStore } from "./store.js" import { SubagentCompletion } from "./subagent-completion.js" @@ -32,9 +33,16 @@ export class Service extends Context.Service()("@opencode/Su const make: Effect.Effect< Interface, never, - Session.Service | Job.Service | Bus.Service | SessionStore.Service | Database.Service | Scope.Scope + | Session.Service + | SessionExecution.Service + | Job.Service + | Bus.Service + | SessionStore.Service + | Database.Service + | Scope.Scope > = Effect.gen(function* () { const sessions = yield* Session.Service + const execution = yield* SessionExecution.Service const jobs = yield* Job.Service const bus = yield* Bus.Service const store = yield* SessionStore.Service @@ -114,6 +122,13 @@ const make: Effect.Effect< ) if (!SubagentMetadata.notify(metadata)) { if (info.notificationID) yield* jobs.completeBackground(info.notificationID) + // Nothing reaches the parent, so wake it once idle: a parent whose report waited on this job + // settles again and reports its final state, without running a model step. Waiting for idle + // rather than skipping an active parent covers a settlement that read this job as running. + const parentID = recovery.parentSessionID + if (!(yield* store.get(parentID))) return + yield* execution.awaitIdle(parentID) + yield* execution.wake(parentID) return } yield* SubagentCompletion.deliver(sessions, jobs, { ...info, recovery }) @@ -161,5 +176,5 @@ const make: Effect.Effect< export const node: LayerNode.Provider = Node.makeGlobalNode({ service: Service, layer: Layer.effect(Service, make), - deps: [Session.node, Job.node, Bus.node, SessionStore.node, Database.node], + deps: [Session.node, SessionExecution.node, Job.node, Bus.node, SessionStore.node, Database.node], })