Skip to content

Commit a6d676f

Browse files
fix(web): keep background tasks visible during follow-up turns (#24336)
1 parent 338e738 commit a6d676f

5 files changed

Lines changed: 145 additions & 8 deletions

File tree

‎apps/web/src/components/ChatView.logic.test.ts‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
snapshotComposerSend,
88
withMessageContext,
99
deriveAvailableRevertTurnCounts,
10+
deriveThreadPendingBackgroundTasks,
1011
} from "./ChatView.logic";
1112
import { recallCheckoutIsRepo, rememberCheckoutIsRepo } from "./ChatView.logic";
1213
import { ProviderDriverKind, type ServerProvider } from "@t3tools/contracts";
@@ -1224,6 +1225,113 @@ describe("deriveAvailableRevertTurnCounts", () => {
12241225
});
12251226
});
12261227

1228+
describe("deriveThreadPendingBackgroundTasks", () => {
1229+
const base = makeThreadProjectionFixture();
1230+
const run = (ordinal: number, status: string) => ({
1231+
id: RunId.make(`run-${ordinal}`),
1232+
ordinal,
1233+
status,
1234+
});
1235+
const item = (ordinal: number, type: string, nativeId: string, input: string) => ({
1236+
id: TurnItemId.make(`item-${nativeId}`),
1237+
type,
1238+
status: "running",
1239+
title: null,
1240+
runId: RunId.make(`run-${ordinal}`),
1241+
nativeItemRef: { nativeId },
1242+
input,
1243+
});
1244+
const projection = (overrides: object) =>
1245+
({ ...base, ...overrides }) as unknown as ReturnType<typeof makeThreadProjectionFixture>;
1246+
1247+
it.each(["queued", "preparing", "running"])(
1248+
"keeps an earlier run's background shell while a %s follow-up run has a foreground command",
1249+
(followUpStatus) => {
1250+
expect(
1251+
deriveThreadPendingBackgroundTasks(
1252+
projection({
1253+
runs: [run(1, "completed"), run(2, followUpStatus)],
1254+
turnItems: [
1255+
item(1, "command_execution", "shell-1", "sleep 600"),
1256+
item(2, "command_execution", "shell-2", "sleep 15"),
1257+
],
1258+
}),
1259+
),
1260+
).toEqual([{ taskId: "shell-1", description: "sleep 600", kind: "command" }]);
1261+
},
1262+
);
1263+
1264+
it("does not list a live run's child from the provider roster as background", () => {
1265+
// The roster is not run-tied; Muse keys it by the child session id its rows carry.
1266+
expect(
1267+
deriveThreadPendingBackgroundTasks(
1268+
projection({
1269+
runs: [run(1, "completed"), run(2, "running")],
1270+
providerThreads: [
1271+
{
1272+
id: "provider-thread-1",
1273+
providerInstanceId: base.thread.providerInstanceId,
1274+
pendingBackgroundTasks: [
1275+
{ taskId: "child-session-1", kind: "subagent" },
1276+
{ taskId: "child-session-2", kind: "subagent" },
1277+
],
1278+
},
1279+
],
1280+
turnItems: [
1281+
{ ...item(1, "subagent", "child-session-1", ""), prompt: "child 1" },
1282+
{ ...item(2, "subagent", "child-session-2", ""), prompt: "child 2" },
1283+
],
1284+
}),
1285+
),
1286+
).toEqual([
1287+
{
1288+
taskId: "child-session-1",
1289+
kind: "subagent",
1290+
providerInstanceId: base.thread.providerInstanceId,
1291+
},
1292+
]);
1293+
});
1294+
1295+
it("keeps an earlier run's roster-only monitor while a follow-up run executes", () => {
1296+
// Claude completes the Monitor launch row on success; only the roster tracks the task.
1297+
expect(
1298+
deriveThreadPendingBackgroundTasks(
1299+
projection({
1300+
runs: [run(1, "completed"), run(2, "running")],
1301+
providerThreads: [
1302+
{
1303+
id: "provider-thread-1",
1304+
providerInstanceId: base.thread.providerInstanceId,
1305+
pendingBackgroundTasks: [{ taskId: "b1htjkjev", kind: "monitor" }],
1306+
},
1307+
],
1308+
turnItems: [
1309+
{ ...item(1, "dynamic_tool", "toolu-monitor", ""), status: "completed" },
1310+
item(2, "command_execution", "shell-2", "sleep 15"),
1311+
],
1312+
}),
1313+
),
1314+
).toEqual([{ taskId: "b1htjkjev", kind: "monitor" }]);
1315+
});
1316+
1317+
it("keeps the provider roster while a follow-up run only waits in the queue", () => {
1318+
expect(
1319+
deriveThreadPendingBackgroundTasks(
1320+
projection({
1321+
runs: [run(1, "waiting"), run(2, "queued")],
1322+
providerThreads: [
1323+
{
1324+
id: "provider-thread-1",
1325+
providerInstanceId: base.thread.providerInstanceId,
1326+
pendingBackgroundTasks: [{ taskId: "task-1", kind: "background_task" }],
1327+
},
1328+
],
1329+
}),
1330+
),
1331+
).toEqual([{ taskId: "task-1", kind: "background_task" }]);
1332+
});
1333+
});
1334+
12271335
describe("Open in for the host environment", () => {
12281336
it("shows both local environments when the primary has a separate WSL home", () => {
12291337
const primaryEnvironmentId = EnvironmentId.make("wsl");

‎apps/web/src/components/ChatView.logic.ts‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,22 +1445,46 @@ export function presentThreadShellFromProjection(
14451445
};
14461446
}
14471447

1448-
/** Background work the provider still runs after the latest run ended. */
1448+
// Excludes `waiting`: that is post-terminal checkpoint drain, so its agent turn is over.
1449+
const LIVE_RUN_STATUSES = new Set(["queued", "preparing", "starting", "running"]);
1450+
1451+
/**
1452+
* Background work the provider still runs after an earlier run ended. It
1453+
* stays listed while a later run is live; that live run's own items are its
1454+
* foreground work, so they never count as background. The provider roster is
1455+
* not run-tied, so it drops entries naming a live run's item by native id.
1456+
*/
14491457
export function deriveThreadPendingBackgroundTasks(projection: ThreadStructure | null) {
14501458
if (projection === null) return [];
14511459
const sessionError =
14521460
projection.providerSessions.findLast(
14531461
(session) => session.providerInstanceId === projection.thread.providerInstanceId,
14541462
)?.lastError ?? null;
1463+
const liveRunIds = new Set(
1464+
projection.runs.filter((run) => LIVE_RUN_STATUSES.has(run.status)).map((run) => run.id),
1465+
);
1466+
const foregroundTaskIds = new Set<string>();
1467+
for (const item of projection.turnItems) {
1468+
if (item.runId === null || !liveRunIds.has(item.runId)) continue;
1469+
if (item.nativeItemRef?.nativeId) foregroundTaskIds.add(item.nativeItemRef.nativeId);
1470+
}
14551471
const latestRun =
14561472
usageLimitRunPresentedAsLatest(projection.runs, projection.turnItems, sessionError) ??
1457-
latestUnheldRun(projection.runs);
1473+
latestUnheldRun(projection.runs.filter((run) => !liveRunIds.has(run.id)));
14581474
return [
14591475
...derivePendingBackgroundWork({
14601476
latestRun,
1461-
providerThreads: projection.providerThreads,
1462-
turnItems: projection.turnItems,
1477+
providerThreads: projection.providerThreads.map((thread) => ({
1478+
...thread,
1479+
pendingBackgroundTasks: thread.pendingBackgroundTasks?.filter(
1480+
(task) => !foregroundTaskIds.has(task.taskId),
1481+
),
1482+
})),
1483+
turnItems: projection.turnItems.filter(
1484+
(item) => item.runId === null || !liveRunIds.has(item.runId),
1485+
),
14631486
activeProviderThreadId: projection.thread.activeProviderThreadId,
1487+
hasActiveRun: false,
14641488
runs: projection.runs,
14651489
pullRequests: projection.thread.pullRequests,
14661490
}),

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,6 +2411,8 @@ export default function ChatView(props: ChatViewProps) {
24112411
serverConfig,
24122412
runtimeMode,
24132413
isWorking,
2414+
// A waiting run is checkpoint drain; Stop there reaches its background work.
2415+
runIsLive: (isWorking && activeRuntime?.status !== "waiting") || phase === "connecting",
24142416
pendingBackgroundTasks,
24152417
turnItems: serverProjection?.turnItems ?? EMPTY_TURN_ITEMS,
24162418
activePendingApproval,

‎apps/web/src/components/chat/useThreadLifecycle.browser.test.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ function RestoreBranchHarness(props: {
117117
serverConfig: null,
118118
runtimeMode: "full-access" as never,
119119
isWorking: false,
120+
runIsLive: false,
120121
pendingBackgroundTasks: [{ taskId: "background-command", kind: "command" }] as never,
121122
turnItems: [],
122123
activePendingApproval: null as never,

‎apps/web/src/components/chat/useThreadLifecycle.tsx‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ interface ThreadLifecycleInput {
105105
readonly serverConfig: EnvironmentServerConfig | null;
106106
readonly runtimeMode: RuntimeMode;
107107
readonly isWorking: boolean;
108+
/** Also true while the server prepares, starts, or queues a run this client did not send. */
109+
readonly runIsLive: boolean;
108110
readonly pendingBackgroundTasks: Parameters<typeof backgroundWorkNowItem>[0]["tasks"];
109111
readonly turnItems: Parameters<typeof backgroundWorkNowItem>[0]["turnItems"];
110112
readonly activePendingApproval: PendingThreadRequests["activePendingApproval"];
@@ -157,6 +159,7 @@ export function useThreadLifecycle(input: ThreadLifecycleInput) {
157159
serverConfig,
158160
runtimeMode,
159161
isWorking,
162+
runIsLive,
160163
pendingBackgroundTasks,
161164
turnItems,
162165
activePendingApproval,
@@ -406,9 +409,7 @@ export function useThreadLifecycle(input: ThreadLifecycleInput) {
406409
const handleSwitchCheckoutToThreadStable = useStableCallback(() => {
407410
void handleSwitchCheckoutToThread();
408411
});
409-
// The interrupt path also accepts a completed run while its provider still
410-
// has background work.
411-
const activeBackgroundTasks = !isWorking && activeThread ? pendingBackgroundTasks : [];
412+
const activeBackgroundTasks = activeThread ? pendingBackgroundTasks : [];
412413
const [backgroundWorkStops, setBackgroundWorkStops] = useState<
413414
Record<string, BackgroundWorkStopRequest | undefined>
414415
>({});
@@ -600,7 +601,8 @@ export function useThreadLifecycle(input: ThreadLifecycleInput) {
600601
threadId: activeThread.id,
601602
tasks: activeBackgroundTasks,
602603
turnItems,
603-
stopDisabled: !canOperateThread || isStoppingBackgroundWork,
604+
// Interrupt stops the active run first, not this work; the composer owns that Stop.
605+
stopDisabled: runIsLive || !canOperateThread || isStoppingBackgroundWork,
604606
stopping: isStoppingBackgroundWork,
605607
onStop: handleStopBackgroundWorkStable,
606608
onOpenThread: onOpenRelatedThreadStable,

0 commit comments

Comments
 (0)