fix(core): release a held subagent report when background work finishes silently

This commit is contained in:
Kit Langton
2026-11-04 17:27:45 -08:00
Unverified
parent d009fc00b7
commit 7e8c1699bf
2 changed files with 33 additions and 4 deletions
@@ -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<SessionSchema.ID>()
// 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<SessionSchema.ID>()
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"
+17 -2
View File
@@ -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<Service, Interface>()("@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<Service, never, typeof Node.tags.values.global> = 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],
})