fix(background-agent): avoid branched parent wakes

This commit is contained in:
YeonGyu-Kim
2026-05-15 14:39:55 +09:00
parent 9f6d0d2281
commit cd1c1a5921
3 changed files with 282 additions and 4 deletions
@@ -32,6 +32,7 @@ type PendingParentWakeForTest = {
promptContext: Record<string, unknown>
notifications: string[]
shouldReply: boolean
dispatchedAt?: number
}
class MockBackgroundManager {
@@ -5168,6 +5169,71 @@ describe("BackgroundManager.handleEvent - session.error", () => {
manager.shutdown()
})
test("does not requeue dispatched parent wake when session history already contains assistant output after the wake", async () => {
//#given
const promptCalls: Array<{ path: { id: string }; body: Record<string, unknown> }> = []
const client = {
session: {
status: async () => ({ data: { "parent-session-wake": { type: "idle" } } }),
messages: async () => [
{
info: {
role: "assistant",
time: { created: Date.now() },
},
parts: [{ type: "text", text: "wake was already accepted" }],
},
],
promptAsync: async (args: { path: { id: string }; body: Record<string, unknown> }) => {
promptCalls.push(args)
return {}
},
abort: async () => ({}),
},
}
const manager = new BackgroundManager({ pluginContext: createPluginInput(client) })
const managerInternals = cast<{
queuePendingParentWake: (
sessionID: string,
notification: string,
promptContext: Record<string, unknown>,
shouldReply: boolean,
delayMs?: number,
) => void
flushPendingParentWake: (sessionID: string) => Promise<void>
}>(manager)
managerInternals.queuePendingParentWake(
"parent-session-wake",
"<system-reminder>done</system-reminder>",
{ agent: "sisyphus" },
true,
0,
)
await managerInternals.flushPendingParentWake("parent-session-wake")
const wake = getDispatchedParentWakes(manager).get("parent-session-wake")
if (!wake) {
throw new Error("Missing dispatched parent wake")
}
wake.dispatchedAt = Date.now() - 1_000
//#when
manager.handleEvent({
type: "session.error",
properties: {
sessionID: "parent-session-wake",
error: { name: "UnknownError", message: "late provider failure" },
},
})
await flushBackgroundNotifications()
//#then
expect(promptCalls).toHaveLength(1)
expect(getDispatchedParentWakes(manager).has("parent-session-wake")).toBe(false)
expect(getPendingParentWakes(manager).has("parent-session-wake")).toBe(false)
manager.shutdown()
})
test("terminates task on session.error when session is gone", async () => {
//#given
const manager = createBackgroundManager()