diff --git a/apps/mobile/src/features/threads/threadListV2.test.ts b/apps/mobile/src/features/threads/threadListV2.test.ts index ff438f006f1cb..a9e48af5762d8 100644 --- a/apps/mobile/src/features/threads/threadListV2.test.ts +++ b/apps/mobile/src/features/threads/threadListV2.test.ts @@ -137,8 +137,8 @@ describe("resolveThreadListV2Status", () => { }); it.each([ - { roster: "dev server", task: { kind: "command" }, status: "ready" }, - { roster: "dev server watchdog", task: { kind: "monitor" }, status: "ready" }, + { roster: "dev server", task: { kind: "command" }, status: "waiting" }, + { roster: "dev server watchdog", task: { kind: "monitor" }, status: "waiting" }, { roster: "awaited command", task: { kind: "command", awaited: true }, status: "waiting" }, ] as const)( "presents an unseen completion with a $roster roster as $status", diff --git a/apps/mobile/src/features/threads/threadListV2.ts b/apps/mobile/src/features/threads/threadListV2.ts index ad02dd1192d63..7b95e39965f61 100644 --- a/apps/mobile/src/features/threads/threadListV2.ts +++ b/apps/mobile/src/features/threads/threadListV2.ts @@ -10,7 +10,10 @@ import { } from "@t3tools/client-runtime/state/thread-settled"; import type { SnoozePreset } from "@t3tools/client-runtime/state/thread-settled"; import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell"; -import { resolveThreadProviderStack } from "@t3tools/client-runtime/state/models"; +import { + resolveThreadProviderStack, + threadOwnsRunningBackgroundWork, +} from "@t3tools/client-runtime/state/models"; import { threadSearchMatchKey, threadSearchSubject, @@ -207,7 +210,8 @@ export function resolveThreadListV2Status( | "pendingGuestMessageCount" | "runtime" | "blockedOnThreadWait" - >, + > & + Partial>, ): ThreadListV2Status { if (thread.hasPendingApprovals || thread.pendingSubagentRequest === "approval") { return "approval"; @@ -234,6 +238,8 @@ export function resolveThreadListV2Status( if (thread.runtime?.status === "failed") { return thread.runtime.lastErrorClass === "usage_limit" ? "limited" : "failed"; } + // The turn ended while background work it owns still runs. + if (threadOwnsRunningBackgroundWork(thread)) return "waiting"; return "ready"; } diff --git a/apps/web/src/components/Sidebar.logic.test.ts b/apps/web/src/components/Sidebar.logic.test.ts index e4fa68a65201b..8a6b75b97d6d6 100644 --- a/apps/web/src/components/Sidebar.logic.test.ts +++ b/apps/web/src/components/Sidebar.logic.test.ts @@ -15,6 +15,7 @@ import { pinOrderKeyBetween, resolveProjectStatusIndicator, resolveSidebarSweepKeys, + resolveSidebarRunStatus, resolveSidebarThreadStatus, resolveThreadStatusPill, resolveWorkingStartedAt, @@ -980,28 +981,10 @@ describe("navigation after parking a thread", () => { describe("unseen completion with background work", () => { it.each([ - { - roster: "dev server", - task: { kind: "command" }, - status: "ready", - receded: false, - pill: "Completed", - }, - { - roster: "dev server watchdog", - task: { kind: "monitor" }, - status: "ready", - receded: false, - pill: "Completed", - }, - { - roster: "awaited command", - task: { kind: "command", awaited: true }, - status: "waiting", - receded: true, - pill: "Waiting", - }, - ] as const)("presents a completed thread with a $roster roster", (expected) => { + { roster: "dev server", task: { kind: "command" }, runStatus: "ready" }, + { roster: "dev server watchdog", task: { kind: "monitor" }, runStatus: "ready" }, + { roster: "awaited command", task: { kind: "command", awaited: true }, runStatus: "waiting" }, + ] as const)("keeps a completed thread with a $roster roster Waiting in Working", (expected) => { const thread = presentThreadShell(localEnvironmentId, { ...makeThreadFixture().source, latestRunId: RunId.make("run-background-completion"), @@ -1014,7 +997,9 @@ describe("unseen completion with background work", () => { const isUnread = hasUnseenCompletion(thread); expect(isUnread).toBe(true); - expect(status).toBe(expected.status); + expect(status).toBe("waiting"); + // Notifications follow the run, so an unawaited dev server does not hold the completion one. + expect(resolveSidebarRunStatus(thread)).toBe(expected.runStatus); expect( shouldRecedeSidebarThread({ status, @@ -1023,9 +1008,24 @@ describe("unseen completion with background work", () => { isActive: false, isSelected: false, }), - ).toBe(expected.receded); - expect(isSidebarThreadWorking(thread)).toBe(expected.receded); - expect(resolveThreadStatusPill({ thread })).toMatchObject({ label: expected.pill }); + ).toBe(true); + expect(isSidebarThreadWorking(thread)).toBe(true); + expect(resolveThreadStatusPill({ thread })).toMatchObject({ label: "Waiting" }); + }); + + it("leaves Working once no background work remains", () => { + const thread = presentThreadShell(localEnvironmentId, { + ...makeThreadFixture().source, + latestRunId: RunId.make("run-background-completion"), + status: "completed", + latestRunCompletedAt: DateTime.makeUnsafe("2026-06-20T01:00:00.000Z"), + lastVisitedAt: DateTime.makeUnsafe("2026-06-20T00:59:00.000Z"), + pendingBackgroundTasks: [], + }); + + expect(resolveSidebarThreadStatus(thread)).toBe("ready"); + expect(isSidebarThreadWorking(thread)).toBe(false); + expect(resolveThreadStatusPill({ thread })).toMatchObject({ label: "Completed" }); }); }); @@ -1061,6 +1061,12 @@ describe("Working shelf (beta)", () => { it("folds away running threads and threads waiting on background work only", () => { expect(isSidebarThreadWorking({ ...idle, runtime })).toBe(true); expect(isSidebarThreadWorking(waiting)).toBe(true); + expect( + isSidebarThreadWorking({ + ...idle, + pendingBackgroundTasks: [{ taskId: "deploy", kind: "command" as const }], + }), + ).toBe(true); expect(isSidebarThreadWorking(idle)).toBe(false); expect(isSidebarThreadWorking({ ...idle, runtime, hasPendingApprovals: true })).toBe(false); expect(isSidebarThreadWorking({ ...idle, runtime, hasPendingUserInput: true })).toBe(false); diff --git a/apps/web/src/components/Sidebar.logic.ts b/apps/web/src/components/Sidebar.logic.ts index efd8244f8c2b2..a6aaa79cb610a 100644 --- a/apps/web/src/components/Sidebar.logic.ts +++ b/apps/web/src/components/Sidebar.logic.ts @@ -1,6 +1,8 @@ -import { resolveThreadWorkingStartedAt } from "@t3tools/client-runtime/state/models"; +import { + resolveThreadWorkingStartedAt, + threadOwnsRunningBackgroundWork, +} from "@t3tools/client-runtime/state/models"; import { isShownUnderParent } from "@t3tools/client-runtime/state/threads"; -import { backgroundWorkHoldsCompletion } from "@t3tools/shared/orchestrationV2PendingBackgroundWork"; import { threadPullRequestSearchTerms } from "@t3tools/shared/threadPullRequests"; import * as React from "react"; import { @@ -841,9 +843,24 @@ type SidebarThreadStatusInput = Pick< | "pendingGuestMessageCount" | "runtime" | "blockedOnThreadWait" ->; +> & + Partial>; +/** + * A row's status: the run's own status, except that a thread whose turn ended + * while background work it owns is still running reads as Waiting. + */ export function resolveSidebarThreadStatus(thread: SidebarThreadStatusInput): SidebarThreadStatus { + const status = resolveSidebarRunStatus(thread); + return status === "ready" && threadOwnsRunningBackgroundWork(thread) ? "waiting" : status; +} + +/** + * The run's own status, ignoring background work the agent does not wait on, + * such as a dev server. Notifications follow it, so a finished turn still + * notifies while such work runs. + */ +export function resolveSidebarRunStatus(thread: SidebarThreadStatusInput): SidebarThreadStatus { if (thread.hasPendingApprovals || thread.pendingSubagentRequest === "approval") { return "approval"; } @@ -1129,7 +1146,7 @@ export function resolveThreadStatusPill(input: { if ( (thread.runtime?.status === "running" && thread.blockedOnThreadWait === true) || - backgroundWorkHoldsCompletion(thread.pendingBackgroundTasks ?? []) + threadOwnsRunningBackgroundWork(thread) ) { return { label: "Waiting", diff --git a/apps/web/src/components/ThreadNotificationCoordinator.tsx b/apps/web/src/components/ThreadNotificationCoordinator.tsx index fa4fdbb672faa..fe1f3b997fab8 100644 --- a/apps/web/src/components/ThreadNotificationCoordinator.tsx +++ b/apps/web/src/components/ThreadNotificationCoordinator.tsx @@ -29,7 +29,7 @@ import { threadNotificationTag, unlockNotificationAudio, } from "../threadNotifications"; -import { resolveSidebarThreadStatus, type SidebarThreadStatus } from "./Sidebar.logic"; +import { resolveSidebarRunStatus, type SidebarThreadStatus } from "./Sidebar.logic"; import { createThreadNotificationGate, type ThreadNotificationKind, @@ -197,7 +197,7 @@ export function startThreadNotifications( continue; } const thread = presentThreadShell(environmentId, rawThread, subagentRequest); - let status = resolveSidebarThreadStatus(thread); + let status = resolveSidebarRunStatus(thread); if (status === "ready" && thread.latestRun?.status === "failed") status = "failed"; const attention = status === "input" || diff --git a/packages/client-runtime/src/state/models.ts b/packages/client-runtime/src/state/models.ts index ac40471a853ba..6cb9bc040af2d 100644 --- a/packages/client-runtime/src/state/models.ts +++ b/packages/client-runtime/src/state/models.ts @@ -115,6 +115,18 @@ export function threadRuntimeIsActive(runtime: ThreadStructureRuntime | null | u return runtime !== null && runtime !== undefined && threadRunStatusIsActive(runtime.status); } +/** + * Whether the thread still owns running background work: a command, monitor, + * background task or subagent, awaited or not. Lists keep such a thread in + * Working as Waiting until it all ends; a failed run does not hold. + */ +export function threadOwnsRunningBackgroundWork(thread: { + readonly pendingBackgroundTasks?: ReadonlyArray | undefined; + readonly runtime: Pick | null; +}): boolean { + return (thread.pendingBackgroundTasks?.length ?? 0) > 0 && thread.runtime?.status !== "failed"; +} + export function threadRuntimeCanArchive(runtime: ThreadRuntimeSummary | null | undefined): boolean { if (runtime?.status === "queued") return runtime.activeRunId === null; return ( diff --git a/packages/client-runtime/src/state/threadInbox.test.ts b/packages/client-runtime/src/state/threadInbox.test.ts index 35c7fa8355e39..b0f349b89a615 100644 --- a/packages/client-runtime/src/state/threadInbox.test.ts +++ b/packages/client-runtime/src/state/threadInbox.test.ts @@ -34,6 +34,45 @@ function thread(id: string, working: boolean) { }; } +describe("isThreadWorking", () => { + const command = { + taskId: "deploy", + description: "Wait for the deploy", + kind: "command" as const, + }; + + it("keeps a thread whose turn ended with background work still running", () => { + expect(isThreadWorking({ ...thread("a", false), pendingBackgroundTasks: [command] })).toBe( + true, + ); + expect( + isThreadWorking({ + ...thread("a", false), + pendingBackgroundTasks: [{ ...command, kind: "subagent" as const }], + }), + ).toBe(true); + expect(isThreadWorking({ ...thread("a", false), pendingBackgroundTasks: [] })).toBe(false); + }); + + it("does not hold a failed thread or one asking for approval", () => { + const failed = thread("a", true); + expect( + isThreadWorking({ + ...failed, + runtime: { ...failed.runtime!, status: "failed" as const }, + pendingBackgroundTasks: [command], + }), + ).toBe(false); + expect( + isThreadWorking({ + ...thread("a", false), + hasPendingApprovals: true, + pendingBackgroundTasks: [command], + }), + ).toBe(false); + }); +}); + describe("createInboxReturnTracker", () => { beforeEach(() => { vi.useFakeTimers(); diff --git a/packages/client-runtime/src/state/threadInbox.ts b/packages/client-runtime/src/state/threadInbox.ts index de9cd822398d0..09f6ba7b06d4f 100644 --- a/packages/client-runtime/src/state/threadInbox.ts +++ b/packages/client-runtime/src/state/threadInbox.ts @@ -1,5 +1,9 @@ // @effect-diagnostics globalTimers:off - Shelf placement timers run in web and native clients, outside an Effect runtime. -import { threadRuntimeIsActive, type EnvironmentThreadShell } from "./models.ts"; +import { + threadOwnsRunningBackgroundWork, + threadRuntimeIsActive, + type EnvironmentThreadShell, +} from "./models.ts"; import { toSortableTimestamp } from "./threadSort.ts"; import { effectiveSnoozed } from "./threadSettled.ts"; @@ -12,12 +16,19 @@ type WorkingThreadInput = Pick< | "interactionMode" | "latestRun" | "runtime" ->; +> & + Partial>; export function isThreadWorking(thread: WorkingThreadInput): boolean { if (thread.hasPendingApprovals || thread.hasPendingUserInput) return false; if (thread.pendingSubagentRequest != null) return false; - if (!threadRuntimeIsActive(thread.runtime) && thread.runtime?.status !== "idle") return false; + if ( + !threadRuntimeIsActive(thread.runtime) && + thread.runtime?.status !== "idle" && + !threadOwnsRunningBackgroundWork(thread) + ) { + return false; + } const run = thread.latestRun; const runSettled = run !== null &&