The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 21:00 UTC
fix(web): keep background tasks visible during follow-up turns (#24336)
Julius Marminge committed
Nov 11, 2026 at 17:01 UTC
a6d676fd9a9cee8485029450ded3eb7cdb77d5ee
5 files changed
+145
-8
apps/web/src/components/ChatView.logic.test.ts
+108
@@ -7,6 +7,7 @@ import {
7
snapshotComposerSend,
8
withMessageContext,
9
deriveAvailableRevertTurnCounts,
10
+ deriveThreadPendingBackgroundTasks,
11
} from "./ChatView.logic";
12
import { recallCheckoutIsRepo, rememberCheckoutIsRepo } from "./ChatView.logic";
13
import { ProviderDriverKind, type ServerProvider } from "@t3tools/contracts";
@@ -1224,6 +1225,113 @@ describe("deriveAvailableRevertTurnCounts", () => {
1225
});
1226
});
1227
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
+
1335
describe("Open in for the host environment", () => {
1336
it("shows both local environments when the primary has a separate WSL home", () => {
1337
const primaryEnvironmentId = EnvironmentId.make("wsl");
apps/web/src/components/ChatView.logic.ts
+28
-4
@@ -1445,22 +1445,46 @@ export function presentThreadShellFromProjection(
1445
};
1446
}
1447
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
+ */
1457
export function deriveThreadPendingBackgroundTasks(projection: ThreadStructure | null) {
1458
if (projection === null) return [];
1459
const sessionError =
1460
projection.providerSessions.findLast(
1461
(session) => session.providerInstanceId === projection.thread.providerInstanceId,
1462
)?.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
+ }
1471
const latestRun =
1472
usageLimitRunPresentedAsLatest(projection.runs, projection.turnItems, sessionError) ??
1457
- latestUnheldRun(projection.runs);
1473
+ latestUnheldRun(projection.runs.filter((run) => !liveRunIds.has(run.id)));
1474
return [
1475
...derivePendingBackgroundWork({
1476
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
+ ),
1486
activeProviderThreadId: projection.thread.activeProviderThreadId,
1487
+ hasActiveRun: false,
1488
runs: projection.runs,
1489
pullRequests: projection.thread.pullRequests,
1490
}),
apps/web/src/components/ChatView.tsx
+2
@@ -2411,6 +2411,8 @@ export default function ChatView(props: ChatViewProps) {
2411
serverConfig,
2412
runtimeMode,
2413
isWorking,
2414
+ // A waiting run is checkpoint drain; Stop there reaches its background work.
2415
+ runIsLive: (isWorking && activeRuntime?.status !== "waiting") || phase === "connecting",
2416
pendingBackgroundTasks,
2417
turnItems: serverProjection?.turnItems ?? EMPTY_TURN_ITEMS,
2418
activePendingApproval,
apps/web/src/components/chat/useThreadLifecycle.browser.test.tsx
+1
@@ -117,6 +117,7 @@ function RestoreBranchHarness(props: {
117
serverConfig: null,
118
runtimeMode: "full-access" as never,
119
isWorking: false,
120
+ runIsLive: false,
121
pendingBackgroundTasks: [{ taskId: "background-command", kind: "command" }] as never,
122
turnItems: [],
123
activePendingApproval: null as never,
apps/web/src/components/chat/useThreadLifecycle.tsx
+6
-4
@@ -105,6 +105,8 @@ interface ThreadLifecycleInput {
105
readonly serverConfig: EnvironmentServerConfig | null;
106
readonly runtimeMode: RuntimeMode;
107
readonly isWorking: boolean;
108
+ /** Also true while the server prepares, starts, or queues a run this client did not send. */
109
+ readonly runIsLive: boolean;
110
readonly pendingBackgroundTasks: Parameters<typeof backgroundWorkNowItem>[0]["tasks"];
111
readonly turnItems: Parameters<typeof backgroundWorkNowItem>[0]["turnItems"];
112
readonly activePendingApproval: PendingThreadRequests["activePendingApproval"];
@@ -157,6 +159,7 @@ export function useThreadLifecycle(input: ThreadLifecycleInput) {
159
serverConfig,
160
runtimeMode,
161
isWorking,
162
+ runIsLive,
163
pendingBackgroundTasks,
164
turnItems,
165
activePendingApproval,
@@ -406,9 +409,7 @@ export function useThreadLifecycle(input: ThreadLifecycleInput) {
409
const handleSwitchCheckoutToThreadStable = useStableCallback(() => {
410
void handleSwitchCheckoutToThread();
411
});
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 : [];
413
const [backgroundWorkStops, setBackgroundWorkStops] = useState<
414
Record<string, BackgroundWorkStopRequest | undefined>
415
>({});
@@ -600,7 +601,8 @@ export function useThreadLifecycle(input: ThreadLifecycleInput) {
601
threadId: activeThread.id,
602
tasks: activeBackgroundTasks,
603
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,
606
stopping: isStoppingBackgroundWork,
607
onStop: handleStopBackgroundWorkStable,
608
onOpenThread: onOpenRelatedThreadStable,