feat(background-task): render retry timelines and links
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -154,6 +154,76 @@ Use \`background_output(task_id="<id>")\` to retrieve each result.
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given a completed task with retry attempt history", () => {
|
||||
test("#when building the final notification #then it renders the spec-aligned balanced attempt timeline", () => {
|
||||
// given
|
||||
const notification = buildBackgroundTaskNotificationText({
|
||||
task: {
|
||||
id: "task-3",
|
||||
description: "Fallback task",
|
||||
status: "completed",
|
||||
attempts: [
|
||||
{
|
||||
attemptID: "att-1",
|
||||
attemptNumber: 1,
|
||||
sessionID: "ses-primary",
|
||||
providerID: "genai-proxy-openai",
|
||||
modelID: "gpt-5.4-mini",
|
||||
status: "error",
|
||||
error: "Forbidden: Selected provider is forbidden",
|
||||
},
|
||||
{
|
||||
attemptID: "att-2",
|
||||
attemptNumber: 2,
|
||||
sessionID: "ses-fallback",
|
||||
providerID: "anthropic",
|
||||
modelID: "claude-haiku-4.5",
|
||||
status: "completed",
|
||||
},
|
||||
],
|
||||
},
|
||||
duration: "10s",
|
||||
statusText: "COMPLETED",
|
||||
allComplete: true,
|
||||
remainingCount: 0,
|
||||
completedTasks: [
|
||||
{
|
||||
id: "task-3",
|
||||
description: "Fallback task",
|
||||
status: "completed",
|
||||
attempts: [
|
||||
{
|
||||
attemptID: "att-1",
|
||||
attemptNumber: 1,
|
||||
sessionID: "ses-primary",
|
||||
providerID: "genai-proxy-openai",
|
||||
modelID: "gpt-5.4-mini",
|
||||
status: "error",
|
||||
error: "Forbidden: Selected provider is forbidden",
|
||||
},
|
||||
{
|
||||
attemptID: "att-2",
|
||||
attemptNumber: 2,
|
||||
sessionID: "ses-fallback",
|
||||
providerID: "anthropic",
|
||||
modelID: "claude-haiku-4.5",
|
||||
status: "completed",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
// then
|
||||
expect(notification).toContain("[ALL BACKGROUND TASKS COMPLETE]")
|
||||
expect(notification).toContain("- `task-3`: Fallback task")
|
||||
expect(notification).toContain("Background task attempts:")
|
||||
expect(notification).toContain(" - Attempt 1 — ERROR — genai-proxy-openai/gpt-5.4-mini — ses-primary")
|
||||
expect(notification).toContain(" Error: Forbidden: Selected provider is forbidden")
|
||||
expect(notification).toContain(" - Attempt 2 — COMPLETED — anthropic/claude-haiku-4.5 — ses-fallback")
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given a single task notification with undefined description", () => {
|
||||
test("#when building the partial notification #then it uses task ID as fallback", () => {
|
||||
// given
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { BackgroundTaskStatus } from "./types"
|
||||
import type { BackgroundTaskAttempt, BackgroundTaskStatus } from "./types"
|
||||
|
||||
export type BackgroundTaskNotificationStatus = "COMPLETED" | "CANCELLED" | "INTERRUPTED" | "ERROR"
|
||||
|
||||
@@ -7,6 +7,55 @@ export interface BackgroundTaskNotificationTask {
|
||||
description: string
|
||||
status: BackgroundTaskStatus
|
||||
error?: string
|
||||
attempts?: BackgroundTaskAttempt[]
|
||||
}
|
||||
|
||||
function formatAttemptModel(attempt: BackgroundTaskAttempt): string {
|
||||
if (attempt.providerID && attempt.modelID) {
|
||||
return `${attempt.providerID}/${attempt.modelID}`
|
||||
}
|
||||
|
||||
if (attempt.modelID) {
|
||||
return attempt.modelID
|
||||
}
|
||||
|
||||
if (attempt.providerID) {
|
||||
return attempt.providerID
|
||||
}
|
||||
|
||||
return "unknown-model"
|
||||
}
|
||||
|
||||
function formatAttemptTimeline(task: BackgroundTaskNotificationTask): string {
|
||||
if (!task.attempts || task.attempts.length <= 1) {
|
||||
return ""
|
||||
}
|
||||
|
||||
const lines = task.attempts
|
||||
.map((attempt) => {
|
||||
const attemptLines = [
|
||||
` - Attempt ${attempt.attemptNumber} — ${attempt.status.toUpperCase()} — ${formatAttemptModel(attempt)} — ${attempt.sessionID ?? "unknown"}`,
|
||||
]
|
||||
|
||||
if (attempt.status !== "completed" && attempt.error) {
|
||||
attemptLines.push(` Error: ${attempt.error}`)
|
||||
}
|
||||
|
||||
return attemptLines.join("\n")
|
||||
})
|
||||
.join("\n")
|
||||
|
||||
return `Background task attempts:\n${lines}`
|
||||
}
|
||||
|
||||
function formatTaskSummaryLine(task: BackgroundTaskNotificationTask): string {
|
||||
const baseLine = `- \`${task.id}\`: ${task.description || task.id}`
|
||||
const statusSuffix = task.status === "completed"
|
||||
? ""
|
||||
: ` [${task.status.toUpperCase()}]${task.error ? ` - ${task.error}` : ""}`
|
||||
const timeline = formatAttemptTimeline(task)
|
||||
|
||||
return `${baseLine}${statusSuffix}${timeline ? `\n${timeline}` : ""}`
|
||||
}
|
||||
|
||||
export function buildBackgroundTaskNotificationText(input: {
|
||||
@@ -27,10 +76,10 @@ export function buildBackgroundTaskNotificationText(input: {
|
||||
const failedTasks = completedTasks.filter((t) => t.status !== "completed")
|
||||
|
||||
const succeededText = succeededTasks.length > 0
|
||||
? succeededTasks.map((t) => `- \`${t.id}\`: ${safeDescription(t)}`).join("\n")
|
||||
? succeededTasks.map((t) => formatTaskSummaryLine(t)).join("\n")
|
||||
: ""
|
||||
const failedText = failedTasks.length > 0
|
||||
? failedTasks.map((t) => `- \`${t.id}\`: ${safeDescription(t)} [${t.status.toUpperCase()}]${t.error ? ` - ${t.error}` : ""}`).join("\n")
|
||||
? failedTasks.map((t) => formatTaskSummaryLine(t)).join("\n")
|
||||
: ""
|
||||
|
||||
const hasFailures = failedTasks.length > 0
|
||||
@@ -46,7 +95,7 @@ export function buildBackgroundTaskNotificationText(input: {
|
||||
body += `\n**Failed:**\n${failedText}\n`
|
||||
}
|
||||
if (!body) {
|
||||
body = `- \`${task.id}\`: ${safeDescription(task)} [${task.status.toUpperCase()}]${task.error ? ` - ${task.error}` : ""}\n`
|
||||
body = `${formatTaskSummaryLine(task)}\n`
|
||||
}
|
||||
|
||||
return `<system-reminder>
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { BackgroundManager } from "../../features/background-agent"
|
||||
|
||||
export type BackgroundOutputMessage = {
|
||||
id?: string
|
||||
info?: { role?: string; time?: string | { created?: number }; agent?: string }
|
||||
info?: { role?: string; time?: string | { created?: number }; agent?: string; error?: unknown }
|
||||
parts?: Array<{
|
||||
type?: string
|
||||
text?: string
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
|
||||
import type { BackgroundTask } from "../../features/background-agent"
|
||||
import type { BackgroundOutputClient } from "./clients"
|
||||
import { formatTaskResult } from "./task-result-format"
|
||||
|
||||
function createTask(overrides: Partial<BackgroundTask> = {}): BackgroundTask {
|
||||
return {
|
||||
id: "task-1",
|
||||
sessionID: "ses-1",
|
||||
parentSessionID: "main-1",
|
||||
parentMessageID: "msg-1",
|
||||
description: "background task",
|
||||
prompt: "do work",
|
||||
agent: "test-agent",
|
||||
status: "completed",
|
||||
startedAt: new Date("2026-01-01T00:00:00.000Z"),
|
||||
completedAt: new Date("2026-01-01T00:00:05.000Z"),
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
describe("formatTaskResult", () => {
|
||||
test("returns assistant session errors instead of masking them as success text", async () => {
|
||||
const task = createTask()
|
||||
const client: BackgroundOutputClient = {
|
||||
session: {
|
||||
messages: async () => ({
|
||||
data: [
|
||||
{
|
||||
info: {
|
||||
role: "assistant",
|
||||
time: { created: 1 },
|
||||
error: { data: { message: "Forbidden: Selected provider is forbidden" } },
|
||||
},
|
||||
parts: [],
|
||||
},
|
||||
],
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
const output = await formatTaskResult(task, client)
|
||||
|
||||
expect(output).toContain("Session error")
|
||||
expect(output).toContain("Forbidden: Selected provider is forbidden")
|
||||
})
|
||||
})
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { BackgroundTask } from "../../features/background-agent"
|
||||
import { extractErrorMessage } from "../../features/background-agent/error-classifier"
|
||||
import { consumeNewMessages } from "../../shared/session-cursor"
|
||||
import type { BackgroundOutputClient, BackgroundOutputMessagesResult } from "./clients"
|
||||
import { extractMessages, getErrorMessage } from "./session-messages"
|
||||
@@ -56,6 +57,23 @@ Session ID: ${task.sessionID}
|
||||
return timeA.localeCompare(timeB)
|
||||
})
|
||||
|
||||
const sessionError = sortedMessages
|
||||
.filter((message) => message.info?.role === "assistant" && message.info?.error)
|
||||
.map((message) => extractErrorMessage(message.info?.error))
|
||||
.find((message): message is string => typeof message === "string" && message.length > 0)
|
||||
if (sessionError) {
|
||||
return `Task Result
|
||||
|
||||
Task ID: ${task.id}
|
||||
Description: ${task.description}
|
||||
Duration: ${formatDuration(task.startedAt ?? new Date(), task.completedAt)}
|
||||
Session ID: ${task.sessionID}
|
||||
|
||||
---
|
||||
|
||||
Session error: ${sessionError}`
|
||||
}
|
||||
|
||||
const newMessages = consumeNewMessages(task.sessionID, sortedMessages)
|
||||
if (newMessages.length === 0) {
|
||||
const duration = formatDuration(task.startedAt ?? new Date(), task.completedAt)
|
||||
|
||||
Reference in New Issue
Block a user