Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/mobile/src/features/threads/threadListV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 8 additions & 2 deletions apps/mobile/src/features/threads/threadListV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -207,7 +210,8 @@ export function resolveThreadListV2Status(
| "pendingGuestMessageCount"
| "runtime"
| "blockedOnThreadWait"
>,
> &
Partial<Pick<EnvironmentThreadShell, "pendingBackgroundTasks">>,
): ThreadListV2Status {
if (thread.hasPendingApprovals || thread.pendingSubagentRequest === "approval") {
return "approval";
Expand All @@ -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";
}

Expand Down
58 changes: 32 additions & 26 deletions apps/web/src/components/Sidebar.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
pinOrderKeyBetween,
resolveProjectStatusIndicator,
resolveSidebarSweepKeys,
resolveSidebarRunStatus,
resolveSidebarThreadStatus,
resolveThreadStatusPill,
resolveWorkingStartedAt,
Expand Down Expand Up @@ -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"),
Expand All @@ -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,
Expand All @@ -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" });
});
});

Expand Down Expand Up @@ -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);
Expand Down
25 changes: 21 additions & 4 deletions apps/web/src/components/Sidebar.logic.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -841,9 +843,24 @@ type SidebarThreadStatusInput = Pick<
| "pendingGuestMessageCount"
| "runtime"
| "blockedOnThreadWait"
>;
> &
Partial<Pick<SidebarThreadSummary, "pendingBackgroundTasks">>;

/**
* 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";
}
Expand Down Expand Up @@ -1129,7 +1146,7 @@ export function resolveThreadStatusPill(input: {

if (
(thread.runtime?.status === "running" && thread.blockedOnThreadWait === true) ||
backgroundWorkHoldsCompletion(thread.pendingBackgroundTasks ?? [])
threadOwnsRunningBackgroundWork(thread)
) {
return {
label: "Waiting",
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/components/ThreadNotificationCoordinator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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" ||
Expand Down
12 changes: 12 additions & 0 deletions packages/client-runtime/src/state/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown> | undefined;
readonly runtime: Pick<ThreadStructureRuntime, "status"> | 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 (
Expand Down
39 changes: 39 additions & 0 deletions packages/client-runtime/src/state/threadInbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
17 changes: 14 additions & 3 deletions packages/client-runtime/src/state/threadInbox.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -12,12 +16,19 @@ type WorkingThreadInput = Pick<
| "interactionMode"
| "latestRun"
| "runtime"
>;
> &
Partial<Pick<EnvironmentThreadShell, "pendingBackgroundTasks">>;

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 &&
Expand Down
Loading