fix(background-agent): cache observed output for completion checks

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
YeonGyu-Kim
2026-04-04 18:39:26 +09:00
parent a71dd54f8e
commit f9a9a60b82
3 changed files with 137 additions and 2 deletions
@@ -5020,6 +5020,66 @@ describe("BackgroundManager.handleEvent - non-tool event lastUpdate", () => {
//#then - task should still be running (delta event refreshed lastUpdate)
expect(task.status).toBe("running")
})
test("should complete idle task without fetching messages after output event was observed", async () => {
//#given - a running task with observed output from message part events
let messagesCallCount = 0
let todoCallCount = 0
const sessionID = "session-output-cached-idle"
const client = {
session: {
prompt: async () => ({}),
promptAsync: async () => ({}),
abort: async () => ({}),
messages: async () => {
messagesCallCount += 1
return {
data: [
{
info: { role: "assistant" },
parts: [{ type: "text", text: "ok" }],
},
],
}
},
todo: async () => {
todoCallCount += 1
return { data: [] }
},
},
}
const manager = new BackgroundManager({ client, directory: tmpdir() } as unknown as PluginInput)
stubNotifyParentSession(manager)
const task: BackgroundTask = {
id: "task-output-cached-idle",
sessionID,
parentSessionID: "parent-session",
parentMessageID: "msg-1",
description: "idle cached output task",
prompt: "test",
agent: "explore",
status: "running",
startedAt: new Date(Date.now() - (MIN_IDLE_TIME_MS + 10)),
}
getTaskMap(manager).set(task.id, task)
manager.handleEvent({
type: "message.part.updated",
properties: { sessionID, type: "text" },
})
//#when - session.idle fires after output event was already observed
manager.handleEvent({ type: "session.idle", properties: { sessionID } })
//#then - task completes without refetching session.messages
await new Promise((resolve) => setTimeout(resolve, 10))
expect(task.status).toBe("completed")
expect(messagesCallCount).toBe(0)
expect(todoCallCount).toBe(1)
manager.shutdown()
})
})
describe("BackgroundManager regression fixes - resume and aborted notification", () => {