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
@@ -97,7 +97,11 @@ function createManagerWithClient(clientOverrides: Record<string, unknown> = {}):
...clientOverrides,
},
}
return new BackgroundManager({ client, directory: tmpdir() } as unknown as PluginInput)
return new BackgroundManager(
{ client, directory: tmpdir() } as unknown as PluginInput,
undefined,
{ enableParentSessionNotifications: false },
)
}
describe("BackgroundManager verifySessionExists", () => {
@@ -201,6 +205,39 @@ describe("BackgroundManager pollRunningTasks", () => {
//#then
expect(task.status).toBe("completed")
})
test("#when output was already observed from events #then it completes without fetching messages", async () => {
//#given
let messagesCallCount = 0
const manager = createManagerWithClient({
status: async () => ({ data: { "ses-idle-cached": { type: "idle" } } }),
messages: async () => {
messagesCallCount += 1
return {
data: [{
info: { role: "assistant", finish: "end_turn", id: "msg-2" },
parts: [{ type: "text", text: "done" }],
}],
}
},
})
const task = createRunningTask("ses-idle-cached")
injectTask(manager, task)
manager.handleEvent({
type: "message.part.updated",
properties: { sessionID: "ses-idle-cached", type: "text" },
})
//#when
const poll = (manager as unknown as { pollRunningTasks: () => Promise<void> }).pollRunningTasks
await poll.call(manager)
manager.shutdown()
//#then
expect(task.status).toBe("completed")
expect(messagesCallCount).toBe(0)
})
})
describe("#given a running task whose session status is busy", () => {