fix(background-task): retry transient missing output tasks
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -52,6 +52,47 @@ function createMockClient(): BackgroundOutputClient {
|
||||
}
|
||||
|
||||
describe("createBackgroundOutput block=true polling", () => {
|
||||
test("retries a missing background task id before reporting not found", async () => {
|
||||
// #given
|
||||
let lookupCount = 0
|
||||
const task = createTask({
|
||||
id: "bg_retry_visible",
|
||||
status: "completed",
|
||||
sessionId: "ses-retry-visible",
|
||||
})
|
||||
const manager: BackgroundOutputManager = {
|
||||
getTask: (id: string) => {
|
||||
if (id !== task.id) return undefined
|
||||
lookupCount += 1
|
||||
return lookupCount === 1 ? undefined : task
|
||||
},
|
||||
}
|
||||
const client: BackgroundOutputClient = {
|
||||
session: {
|
||||
messages: async () => ({
|
||||
data: [
|
||||
{
|
||||
id: "m1",
|
||||
info: { role: "assistant", time: "2026-01-01T00:00:00Z" },
|
||||
parts: [{ type: "text", text: "visible result" }],
|
||||
},
|
||||
],
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
const tool = createBackgroundOutput(manager, client)
|
||||
|
||||
// #when
|
||||
const output = await tool.execute({ task_id: task.id }, mockContext)
|
||||
|
||||
// #then
|
||||
expect(lookupCount).toBe(2)
|
||||
expect(output).toContain("Task Result")
|
||||
expect(output).toContain("visible result")
|
||||
expect(output).not.toContain("Task not found")
|
||||
})
|
||||
|
||||
test("returns terminal error output when task fails during blocking wait", async () => {
|
||||
// #given
|
||||
let pollCount = 0
|
||||
|
||||
@@ -13,6 +13,7 @@ import { getAgentDisplayName } from "../../shared/agent-display-names"
|
||||
import { recordBackgroundOutputConsumption } from "../../shared/background-output-consumption"
|
||||
|
||||
const SISYPHUS_JUNIOR_AGENT = getAgentDisplayName("sisyphus-junior")
|
||||
const MISSING_BACKGROUND_TASK_RETRY_DELAY_MS = 100
|
||||
|
||||
type ToolContextWithMetadata = {
|
||||
sessionID: string
|
||||
@@ -40,6 +41,23 @@ function isSessionId(value: string): boolean {
|
||||
return /^ses[_-]/.test(value)
|
||||
}
|
||||
|
||||
function isBackgroundTaskId(value: string): boolean {
|
||||
return /^bg[_-]/.test(value)
|
||||
}
|
||||
|
||||
async function getTaskWithMissingRetry(
|
||||
manager: BackgroundOutputManager,
|
||||
taskId: string,
|
||||
): Promise<BackgroundTask | undefined> {
|
||||
const task = manager.getTask(taskId)
|
||||
if (task || !isBackgroundTaskId(taskId)) {
|
||||
return task
|
||||
}
|
||||
|
||||
await delay(MISSING_BACKGROUND_TASK_RETRY_DELAY_MS)
|
||||
return manager.getTask(taskId)
|
||||
}
|
||||
|
||||
function formatTaskNotFoundMessage(taskId: string): string {
|
||||
if (!isSessionId(taskId)) {
|
||||
return `Task not found: ${taskId}`
|
||||
@@ -74,7 +92,7 @@ export function createBackgroundOutput(manager: BackgroundOutputManager, client:
|
||||
async execute(args: BackgroundOutputArgs, toolContext) {
|
||||
try {
|
||||
const ctx = toolContext as ToolContextWithMetadata
|
||||
const task = manager.getTask(args.task_id)
|
||||
const task = await getTaskWithMissingRetry(manager, args.task_id)
|
||||
if (!task) {
|
||||
return formatTaskNotFoundMessage(args.task_id)
|
||||
}
|
||||
@@ -103,7 +121,7 @@ export function createBackgroundOutput(manager: BackgroundOutputManager, client:
|
||||
while (Date.now() - startTime < timeoutMs) {
|
||||
await delay(1000)
|
||||
|
||||
const currentTask = manager.getTask(args.task_id)
|
||||
const currentTask = await getTaskWithMissingRetry(manager, args.task_id)
|
||||
if (!currentTask) {
|
||||
return `Task was deleted: ${args.task_id}`
|
||||
}
|
||||
@@ -116,7 +134,7 @@ export function createBackgroundOutput(manager: BackgroundOutputManager, client:
|
||||
}
|
||||
|
||||
if (isTaskActiveStatus(resolvedTask.status)) {
|
||||
const finalCheck = manager.getTask(args.task_id)
|
||||
const finalCheck = await getTaskWithMissingRetry(manager, args.task_id)
|
||||
if (finalCheck) {
|
||||
resolvedTask = finalCheck
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user