fix(core): hold subagent report until child background work finishes

This commit is contained in:
Kit Langton
2026-11-04 17:57:26 -08:00
Unverified
parent 89e86ec501
commit d6f276d5b8
2 changed files with 78 additions and 38 deletions
@@ -26,6 +26,8 @@ export type Snapshot = {
readonly job: Job.Info | undefined
readonly recovery: Job.Recovery | undefined
readonly remembered: boolean
/** The child's newest answer when the period began; undefined when that was never read. */
readonly baseline: { readonly id: SessionMessage.ID | undefined } | undefined
}
export type Deps = {
@@ -49,23 +51,9 @@ export const make = (deps: Deps) => {
// 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)
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)
})
const snapshot = Effect.fnUntraced(function* (sessionID: SessionSchema.ID): Effect.fn.Return<Snapshot> {
const remembered = jobPeriods.delete(sessionID)
return {
blocked: yield* deps.jobs.isBlocked(sessionID),
job: yield* deps.jobs.get(sessionID),
recovery: yield* deps.jobs.recovery(sessionID),
remembered,
}
})
// A child's newest answer when its busy period began, so settlement can tell a period that
// produced no new answer. Process-local, like held.
const baselines = new Map<SessionSchema.ID, SessionMessage.ID | undefined>()
// The same assistant message the SubagentJob run and restart recovery read: the newest
// completed assistant answer, whose text becomes the report body.
@@ -76,11 +64,34 @@ export const make = (deps: Deps) => {
)
})
const started = Effect.fnUntraced(function* (sessionID: SessionSchema.ID) {
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)
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<Snapshot> {
const remembered = jobPeriods.delete(sessionID)
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),
recovery: yield* deps.jobs.recovery(sessionID),
remembered,
baseline,
}
})
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.
held.delete(sessionID)
const owed = held.delete(sessionID)
const child = yield* deps.store.get(sessionID)
if (!child?.parentID) return
const parentID = child.parentID
@@ -113,6 +124,9 @@ export const make = (deps: Deps) => {
// compaction or a recovered notice settles that way, and a spawn that never ran reports
// through the tool's inline error instead.
if (status === "completed" && assistant === undefined) return
// A no-input wake (a silent descendant's release, or a resume) runs no model step and must not
// re-send an answer the parent already has.
if (status === "completed" && !owed && snap.baseline !== undefined && snap.baseline.id === assistant?.id) return
const output = status === "completed" ? SubagentCompletion.text(assistant) : undefined
const error = outcome.type === "failed" ? outcome.error.message : undefined
const notificationID = live?.notificationID
+46 -20
View File
@@ -14,6 +14,7 @@ import { Plugin } from "@opencode/core/plugin"
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"
import { SessionRunnerModel } from "@opencode/core/session/runner/model"
import { SessionSchema } from "@opencode/core/session/schema"
import { SubagentJob } from "@opencode/core/session/subagent-job"
@@ -30,27 +31,30 @@ const llmLayer = TestLLM.testLayer({ fallback: TestLLM.text("Review complete", "
const it = testEffect(
Layer.merge(
llmLayer,
AppNodeBuilder.build(LayerNode.group([Session.node, Job.node, SubagentJob.node, LocationServiceMap.node]), [
Global.node.replace(tempGlobalLayer),
offlineModels,
Watcher.node.replace(Watcher.configured({ enabled: false })),
LayerNodePlatform.llmClient.replace(llmLayer),
SessionRunnerModel.node.replace(
Layer.succeed(SessionRunnerModel.Service, {
resolve: () =>
Effect.succeed(
SessionRunnerModel.resolved(
LanguageModel.make({ id: "review", provider: "test", route: OpenAIChat.route }),
{
capabilities: { tools: true, input: ["text"], output: ["text"] },
cost: [],
limit: { context: 200_000, output: 32_000 },
},
AppNodeBuilder.build(
LayerNode.group([Session.node, Job.node, SubagentJob.node, SessionExecution.node, LocationServiceMap.node]),
[
Global.node.replace(tempGlobalLayer),
offlineModels,
Watcher.node.replace(Watcher.configured({ enabled: false })),
LayerNodePlatform.llmClient.replace(llmLayer),
SessionRunnerModel.node.replace(
Layer.succeed(SessionRunnerModel.Service, {
resolve: () =>
Effect.succeed(
SessionRunnerModel.resolved(
LanguageModel.make({ id: "review", provider: "test", route: OpenAIChat.route }),
{
capabilities: { tools: true, input: ["text"], output: ["text"] },
cost: [],
limit: { context: 200_000, output: 32_000 },
},
),
),
),
}),
),
]),
}),
),
],
),
),
)
@@ -441,4 +445,26 @@ describe("subagent observer lifetime", () => {
expect(yield* llm.requests()).toHaveLength(3)
}),
)
it.live("a no-input wake after a child reported sends no second report", () =>
Effect.gen(function* () {
const execution = yield* SessionExecution.Service
const llm = yield* TestLLM.Test
const f = yield* family()
yield* llm.push(TestLLM.text("done", "done"))
yield* f.sessions.prompt({ sessionID: f.child.id, text: "Check the tests" })
yield* f.sessions.wait(f.child.id)
yield* f.sessions.wait(f.parent.id)
expect(yield* reports(f.parent.id)).toHaveLength(1)
const requests = (yield* llm.requests()).length
// The wake a silent descendant's release sends: no input, so no model step.
yield* execution.wake(f.child.id)
yield* f.sessions.wait(f.child.id)
yield* f.sessions.wait(f.parent.id)
expect(yield* reports(f.parent.id)).toHaveLength(1)
expect(yield* llm.requests()).toHaveLength(requests)
}),
)
})