From 06aa7e6a145928293bb1c4a0130fd5b876817c3e Mon Sep 17 00:00:00 2001 From: Ethan YU Date: Fri, 20 Nov 2026 20:31:28 +0800 Subject: [PATCH 1/2] fix: show each background command once Match pending tasks to their native tool call or turn item so the same command does not appear twice and its details stay on the retained row. --- .../web/src/components/ChatView.logic.test.ts | 25 ++++++ apps/web/src/components/ChatView.logic.ts | 6 +- .../BackgroundWorkDetails.browser.test.tsx | 79 ++++++++++++++++++- .../components/chat/BackgroundWorkDetails.tsx | 3 +- ...chestrationV2PendingBackgroundWork.test.ts | 26 ++++++ .../orchestrationV2PendingBackgroundWork.ts | 5 +- 6 files changed, 140 insertions(+), 4 deletions(-) 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..b891cba2a814d 100644 --- a/apps/web/src/components/chat/BackgroundWorkDetails.browser.test.tsx +++ b/apps/web/src/components/chat/BackgroundWorkDetails.browser.test.tsx @@ -1,5 +1,13 @@ import "~/index.css"; -import { EnvironmentId, ProviderDriverKind, ThreadId, TurnItemId } from "@t3tools/contracts"; +import { + EnvironmentId, + ProviderDriverKind, + ProviderThreadId, + RunId, + ThreadId, + TurnItemId, +} from "@t3tools/contracts"; +import { derivePendingBackgroundWork } from "@t3tools/shared/orchestrationV2PendingBackgroundWork"; import * as DateTime from "effect/DateTime"; import { expect, it, vi } from "vite-plus/test"; import { page } from "vite-plus/test/browser"; @@ -127,3 +135,72 @@ it("gives a subagent the same status as the background work line", async () => { .element(page.getByRole("dialog").getByText("Waiting", { exact: true })) .toBeVisible(); }); + +it("lists a command once when its native task ID differs from its tool call ID", async () => { + const threadId = ThreadId.make("thread"); + const runId = RunId.make("run-1"); + const providerThreadId = ProviderThreadId.make("provider-thread"); + const command = { + id: TurnItemId.make("command-item"), + threadId, + runId, + nodeId: null, + providerThreadId, + providerTurnId: null, + nativeItemRef: { + driver: ProviderDriverKind.make("antigravity"), + nativeId: "call-9", + strength: "strong", + }, + parentItemId: null, + ordinal: 1, + status: "running", + title: "Run tests", + startedAt: null, + completedAt: null, + updatedAt: DateTime.makeUnsafe("2026-11-09T00:00:00Z"), + type: "command_execution", + input: "pnpm test", + output: "Tests are running", + } as const; + const tasks = derivePendingBackgroundWork({ + latestRun: { id: runId, ordinal: 1, status: "failed" }, + providerThreads: [ + { + id: providerThreadId, + pendingBackgroundTasks: [ + { + taskId: "session/task-9", + nativeItemId: "call-9", + kind: "command", + description: "Run tests", + awaited: true, + }, + ], + }, + ], + turnItems: [command], + }); + await using screen = await render( + undefined, + onOpenThread: () => undefined, + })} + />, + ); + await screen.getByRole("button", { name: "Details", exact: true }).click(); + const dialog = page.getByRole("dialog"); + await expect.element(dialog.getByRole("heading", { name: "Background work" })).toBeVisible(); + expect(dialog.getByRole("heading", { name: "Run tests", exact: true }).all()).toHaveLength(1); + await expect.element(dialog.getByText("Command output stream")).toBeVisible(); +}); diff --git a/apps/web/src/components/chat/BackgroundWorkDetails.tsx b/apps/web/src/components/chat/BackgroundWorkDetails.tsx index 72ca70c2b16e1..353037d99c018 100644 --- a/apps/web/src/components/chat/BackgroundWorkDetails.tsx +++ b/apps/web/src/components/chat/BackgroundWorkDetails.tsx @@ -60,7 +60,8 @@ export function BackgroundWorkDetails(props: { {props.tasks.map((task) => { 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..d0753a41e418e 100644 --- a/packages/shared/src/orchestrationV2PendingBackgroundWork.test.ts +++ b/packages/shared/src/orchestrationV2PendingBackgroundWork.test.ts @@ -204,6 +204,32 @@ describe("derivePendingBackgroundWork", () => { ]); }); + it("merges a failed run's still-open command with its distinct native task id", () => { + const task = { + taskId: "session/task-9", + nativeItemId: "call-9", + description: "Verify", + kind: "command" as const, + awaited: true as const, + }; + expect( + derivePendingBackgroundWork({ + latestRun: { id: "run-1" as never, ordinal: 1, status: "failed" }, + providerThreads: [{ id: "pt-1" as never, pendingBackgroundTasks: [task] }], + turnItems: [ + { + id: "item-9", + type: "command_execution", + status: "running", + title: null, + nativeItemRef: { nativeId: "call-9" }, + input: "pnpm test", + }, + ], + }), + ).toEqual([task]); + }); + it("excludes Grok persistent monitors", () => { const tasks = derivePendingBackgroundWork({ latestRun: { id: "run-1" as never, ordinal: 1, status: "completed" }, 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; } From 989213ba1150a00df9fc09e5bd866898df53e095 Mon Sep 17 00:00:00 2001 From: Ethan YU Date: Fri, 20 Nov 2026 21:17:30 +0800 Subject: [PATCH 2/2] test: keep background command deduplication coverage in the shared projection --- .../BackgroundWorkDetails.browser.test.tsx | 87 ++----------------- ...chestrationV2PendingBackgroundWork.test.ts | 84 +++++++----------- 2 files changed, 41 insertions(+), 130 deletions(-) diff --git a/apps/web/src/components/chat/BackgroundWorkDetails.browser.test.tsx b/apps/web/src/components/chat/BackgroundWorkDetails.browser.test.tsx index b891cba2a814d..8645520c2af41 100644 --- a/apps/web/src/components/chat/BackgroundWorkDetails.browser.test.tsx +++ b/apps/web/src/components/chat/BackgroundWorkDetails.browser.test.tsx @@ -1,13 +1,5 @@ import "~/index.css"; -import { - EnvironmentId, - ProviderDriverKind, - ProviderThreadId, - RunId, - ThreadId, - TurnItemId, -} from "@t3tools/contracts"; -import { derivePendingBackgroundWork } from "@t3tools/shared/orchestrationV2PendingBackgroundWork"; +import { EnvironmentId, ProviderDriverKind, ThreadId, TurnItemId } from "@t3tools/contracts"; import * as DateTime from "effect/DateTime"; import { expect, it, vi } from "vite-plus/test"; import { page } from "vite-plus/test/browser"; @@ -45,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, ) => ( { .element(page.getByRole("dialog").getByText("Waiting", { exact: true })) .toBeVisible(); }); - -it("lists a command once when its native task ID differs from its tool call ID", async () => { - const threadId = ThreadId.make("thread"); - const runId = RunId.make("run-1"); - const providerThreadId = ProviderThreadId.make("provider-thread"); - const command = { - id: TurnItemId.make("command-item"), - threadId, - runId, - nodeId: null, - providerThreadId, - providerTurnId: null, - nativeItemRef: { - driver: ProviderDriverKind.make("antigravity"), - nativeId: "call-9", - strength: "strong", - }, - parentItemId: null, - ordinal: 1, - status: "running", - title: "Run tests", - startedAt: null, - completedAt: null, - updatedAt: DateTime.makeUnsafe("2026-11-09T00:00:00Z"), - type: "command_execution", - input: "pnpm test", - output: "Tests are running", - } as const; - const tasks = derivePendingBackgroundWork({ - latestRun: { id: runId, ordinal: 1, status: "failed" }, - providerThreads: [ - { - id: providerThreadId, - pendingBackgroundTasks: [ - { - taskId: "session/task-9", - nativeItemId: "call-9", - kind: "command", - description: "Run tests", - awaited: true, - }, - ], - }, - ], - turnItems: [command], - }); - await using screen = await render( - undefined, - onOpenThread: () => undefined, - })} - />, - ); - await screen.getByRole("button", { name: "Details", exact: true }).click(); - const dialog = page.getByRole("dialog"); - await expect.element(dialog.getByRole("heading", { name: "Background work" })).toBeVisible(); - expect(dialog.getByRole("heading", { name: "Run tests", exact: true }).all()).toHaveLength(1); - await expect.element(dialog.getByText("Command output stream")).toBeVisible(); -}); diff --git a/packages/shared/src/orchestrationV2PendingBackgroundWork.test.ts b/packages/shared/src/orchestrationV2PendingBackgroundWork.test.ts index d0753a41e418e..1fc44e5597248 100644 --- a/packages/shared/src/orchestrationV2PendingBackgroundWork.test.ts +++ b/packages/shared/src/orchestrationV2PendingBackgroundWork.test.ts @@ -177,58 +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("merges a failed run's still-open command with its distinct native task id", () => { - const task = { - taskId: "session/task-9", - nativeItemId: "call-9", - description: "Verify", - kind: "command" as const, - awaited: true as const, - }; - expect( - derivePendingBackgroundWork({ - latestRun: { id: "run-1" as never, ordinal: 1, status: "failed" }, + 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: [ - { - id: "item-9", - type: "command_execution", - status: "running", - title: null, - nativeItemRef: { nativeId: "call-9" }, - input: "pnpm test", - }, - ], - }), - ).toEqual([task]); - }); + turnItems: [{ ...item, id: "item-9", status: "running", title: task.description }], + }); + expect(tasks).toEqual([task]); + }, + ); it("excludes Grok persistent monitors", () => { const tasks = derivePendingBackgroundWork({