diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts index 298a7c9b44f01..e5081178ba922 100644 --- a/apps/web/src/components/ChatView.logic.test.ts +++ b/apps/web/src/components/ChatView.logic.test.ts @@ -1516,6 +1516,31 @@ describe("deriveThreadPendingBackgroundTasks", () => { }, ); + it("keeps a live command with a separate native task id out of the background list", () => { + expect( + deriveThreadPendingBackgroundTasks( + projection({ + runs: [run(1, "completed"), run(2, "running")], + thread: { ...base.thread, activeProviderThreadId: "pt" }, + providerThreads: [ + { + id: "pt", + pendingBackgroundTasks: [ + { + taskId: "session/task-2", + nativeItemId: "call-2", + kind: "command", + awaited: true, + }, + ], + }, + ], + turnItems: [item(2, "command_execution", "call-2", "pnpm test")], + }), + ), + ).toEqual([]); + }); + it("does not list a live run's child from the provider roster as background", () => { // The roster is not run-tied; Muse keys it by the child session id its rows carry. expect( diff --git a/apps/web/src/components/ChatView.logic.ts b/apps/web/src/components/ChatView.logic.ts index 1f8bb8e10043c..82d482da83f3d 100644 --- a/apps/web/src/components/ChatView.logic.ts +++ b/apps/web/src/components/ChatView.logic.ts @@ -1594,6 +1594,7 @@ export function deriveThreadPendingBackgroundTasks(projection: ThreadStructure | const foregroundTaskIds = new Set(); for (const item of projection.turnItems) { if (item.runId === null || !liveRunIds.has(item.runId)) continue; + foregroundTaskIds.add(item.id); if (item.nativeItemRef?.nativeId) foregroundTaskIds.add(item.nativeItemRef.nativeId); } const latestRun = @@ -1605,7 +1606,10 @@ export function deriveThreadPendingBackgroundTasks(projection: ThreadStructure | providerThreads: projection.providerThreads.map((thread) => ({ ...thread, pendingBackgroundTasks: thread.pendingBackgroundTasks?.filter( - (task) => !foregroundTaskIds.has(task.taskId), + (task) => + !foregroundTaskIds.has(task.taskId) && + (task.nativeItemId === undefined || !foregroundTaskIds.has(task.nativeItemId)) && + (task.itemId === undefined || !foregroundTaskIds.has(task.itemId)), ), })), turnItems: projection.turnItems.filter( diff --git a/apps/web/src/components/chat/BackgroundWorkDetails.browser.test.tsx b/apps/web/src/components/chat/BackgroundWorkDetails.browser.test.tsx index d5d5d62d3c2ab..8645520c2af41 100644 --- a/apps/web/src/components/chat/BackgroundWorkDetails.browser.test.tsx +++ b/apps/web/src/components/chat/BackgroundWorkDetails.browser.test.tsx @@ -37,6 +37,7 @@ it("opens full command details for short labels and keeps pending tasks current" description?: string, status: "running" | "completed" = "completed", explicitItem = true, + nativeItemId?: string, ) => ( { const item = props.turnItems.findLast((candidate) => task.itemId === undefined - ? (candidate.nativeItemRef?.nativeId || candidate.id) === task.taskId + ? (candidate.nativeItemRef?.nativeId || candidate.id) === + (task.nativeItemId ?? task.taskId) : candidate.id === task.itemId, ); const command = diff --git a/packages/shared/src/orchestrationV2PendingBackgroundWork.test.ts b/packages/shared/src/orchestrationV2PendingBackgroundWork.test.ts index 5b8f67e1fae1a..1fc44e5597248 100644 --- a/packages/shared/src/orchestrationV2PendingBackgroundWork.test.ts +++ b/packages/shared/src/orchestrationV2PendingBackgroundWork.test.ts @@ -177,32 +177,40 @@ describe("derivePendingBackgroundWork", () => { expect(tasks).toEqual([{ taskId: "cmd-1", description: "npm test", kind: "command" }]); }); - it("dedupes roster entries against turn items by native task id", () => { - const tasks = derivePendingBackgroundWork({ - latestRun: { id: "run-1" as never, ordinal: 1, status: "completed" }, - providerThreads: [ - { - id: "pt-1" as never, - pendingBackgroundTasks: [ - { taskId: "task-9", description: "Agent review", kind: "background_task" }, - ], - }, - ], - turnItems: [ - { - id: "item-sub" as never, - type: "subagent", - status: "running", - title: "Agent review", - nativeItemRef: { nativeId: "task-9" }, - prompt: "review the plan", - }, - ], - }); - expect(tasks).toEqual([ - { taskId: "task-9", description: "Agent review", kind: "background_task" }, - ]); - }); + it.each([ + { + identity: "matching task ID", + runStatus: "completed", + task: { taskId: "task-9", description: "Agent review", kind: "background_task" }, + item: { type: "subagent", nativeItemRef: { nativeId: "task-9" }, prompt: "review the plan" }, + }, + { + identity: "distinct native tool call ID", + runStatus: "failed", + task: { + taskId: "session/task-9", + nativeItemId: "call-9", + description: "Verify", + kind: "command", + awaited: true, + }, + item: { + type: "command_execution", + nativeItemRef: { nativeId: "call-9" }, + input: "pnpm test", + }, + }, + ] as const)( + "retains the roster entry for a running turn item with a $identity", + ({ runStatus, task, item }) => { + const tasks = derivePendingBackgroundWork({ + latestRun: { id: "run-1" as never, ordinal: 1, status: runStatus }, + providerThreads: [{ id: "pt-1" as never, pendingBackgroundTasks: [task] }], + turnItems: [{ ...item, id: "item-9", status: "running", title: task.description }], + }); + expect(tasks).toEqual([task]); + }, + ); it("excludes Grok persistent monitors", () => { const tasks = derivePendingBackgroundWork({ diff --git a/packages/shared/src/orchestrationV2PendingBackgroundWork.ts b/packages/shared/src/orchestrationV2PendingBackgroundWork.ts index cc8513fb64e60..de9a075997b12 100644 --- a/packages/shared/src/orchestrationV2PendingBackgroundWork.ts +++ b/packages/shared/src/orchestrationV2PendingBackgroundWork.ts @@ -285,6 +285,7 @@ export function derivePendingBackgroundWork(input: { } const byTaskId = new Map(); + const rosterItemIds = new Set(); const providerThreads = input.activeProviderThreadId === undefined || input.activeProviderThreadId === null @@ -296,6 +297,8 @@ export function derivePendingBackgroundWork(input: { if (task.taskId.length === 0 || byTaskId.has(task.taskId)) { continue; } + if (task.nativeItemId !== undefined) rosterItemIds.add(task.nativeItemId); + if (task.itemId !== undefined) rosterItemIds.add(task.itemId); const { description: rawDescription, ...named } = task; const description = rawDescription?.trim(); byTaskId.set(task.taskId, { @@ -310,7 +313,7 @@ export function derivePendingBackgroundWork(input: { for (const item of pendingBackgroundTurnItems(input)) { const taskId = nativeTaskIdFromTurnItem(item); - if (byTaskId.has(taskId)) { + if (byTaskId.has(taskId) || rosterItemIds.has(taskId) || rosterItemIds.has(item.id)) { continue; }