fix(session-recovery): recover interrupted idle tool turns

This commit is contained in:
YeonGyu-Kim
2026-05-17 15:08:39 +09:00
parent fbec112bc2
commit f43effb842
11 changed files with 531 additions and 14 deletions
@@ -24,7 +24,7 @@ type SessionMessageForTest = {
finish?: string
time?: { created?: number }
}
parts?: Array<{ type?: string }>
parts?: Array<{ type?: string; state?: { status?: string } }>
}
type FakeTimers = {
@@ -508,6 +508,44 @@ describe("BackgroundManager.notifyParentSession cleanup scheduling", () => {
expect(promptAsyncCalls).toHaveLength(0)
})
test("#when parent status is idle but latest assistant turn has running tool state without finish #then background completion does not fork a reply", async () => {
// given
const sessionStatuses: Record<string, { type: string }> = {
"parent-1": { type: "idle" },
}
const sessionMessages: SessionMessageForTest[] = [
{
info: { role: "user", time: { created: 1778819814009 } },
parts: [{ type: "text" }],
},
{
info: { role: "assistant", time: { created: 1778819997535 } },
parts: [
{ type: "tool", state: { status: "running" } },
{ type: "tool", state: { status: "pending" } },
],
},
]
const { manager, promptAsyncCalls } = createManager(true, sessionStatuses, undefined, sessionMessages)
managerUnderTest = manager
const task = createTask({
id: "task-a",
parentSessionId: "parent-1",
description: "task A",
status: "completed",
completedAt: new Date("2026-05-17T05:25:01.000Z"),
})
getTasks(manager).set(task.id, task)
getPendingByParent(manager).set(task.parentSessionId, new Set([task.id]))
// when
await notifyParentSessionForTest(manager, task)
await waitForCoalescedFlush()
// then
expect(promptAsyncCalls).toHaveLength(0)
})
test("#when stale tool-call history keeps blocking an all-complete wake #then completion eventually wakes the parent", async () => {
// given
const sessionStatuses: Record<string, { type: string }> = {