@devries-cameron20 / t3code-1 / commits / d4b77d2448
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): Claude background commands show how long they have run (#26668)

Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Dara Adedeji committed Nov 12, 2026 at 19:39 UTC d4b77d244879e2d8388ceadc44e41dd5f091cfcd
5 files changed +92 -11
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+45 -8
@@ -11060,7 +11060,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
11060 );
11061
11062 it.effect(
11063 - "uses task_started as an incremental roster fallback and clears internal work on empty snapshot",
11063 + "uses task_started as an incremental roster fallback, keeps its tool call, and clears internal work on empty snapshot",
11064 () =>
11065 Effect.scoped(
11066 Effect.gen(function* () {
@@ -11118,10 +11118,9 @@ describe("ClaudeAdapterV2 background wake turns", () => {
11118 (event) => (event.providerThread.pendingBackgroundTasks?.length ?? 0) > 0,
11119 );
11120 assert.isAtLeast(afterStart.length, 1);
11121 - assert.equal(
11122 - (afterStart.at(-1)?.providerThread.pendingBackgroundTasks ?? [])[0]?.taskId,
11123 - WAKE_TASK_ID,
11124 - );
11121 + const started = (afterStart.at(-1)?.providerThread.pendingBackgroundTasks ?? [])[0];
11122 + assert.equal(started?.taskId, WAKE_TASK_ID);
11123 + assert.equal(started?.nativeItemId, WAKE_TOOL_USE_ID);
11124
11125 const command = harness.events.find(
11126 (event) =>
@@ -11154,6 +11153,21 @@ describe("ClaudeAdapterV2 background wake turns", () => {
11153 details,
11154 );
11155
11156 + // A foreground command that later moves to the background keeps its tool call too.
11157 + yield* Queue.offer(
11158 + harness.sdkMessages,
11159 + claudeSdkFrame({
11160 + type: "system",
11161 + subtype: "task_started",
11162 + task_id: "moved-shell",
11163 + tool_use_id: "toolu_moved_shell",
11164 + description: "Foreground build",
11165 + is_backgrounded: false,
11166 + task_type: "local_bash",
11167 + uuid: "00000000-0000-4000-8000-000000000204",
11168 + session_id: WAKE_NATIVE_SESSION,
11169 + }),
11170 + );
11171 yield* Queue.offer(
11172 harness.sdkMessages,
11173 claudeSdkFrame({
@@ -11166,6 +11180,16 @@ describe("ClaudeAdapterV2 background wake turns", () => {
11180 description: "sleep 150",
11181 parent_task_id: "agent-1",
11182 },
11183 + {
11184 + task_id: WAKE_TASK_ID,
11185 + task_type: "local_bash",
11186 + description: WAKE_TASK_DESCRIPTION,
11187 + },
11188 + {
11189 + task_id: "moved-shell",
11190 + task_type: "local_bash",
11191 + description: "Foreground build",
11192 + },
11193 ],
11194 uuid: "00000000-0000-4000-8000-000000000203",
11195 session_id: WAKE_NATIVE_SESSION,
@@ -11174,7 +11198,10 @@ describe("ClaudeAdapterV2 background wake turns", () => {
11198 yield* harness.until(() =>
11199 providerThreadRosterEvents(harness.events).some(
11200 (event) =>
11177 - event.providerThread.pendingBackgroundTasks?.[0]?.parentTaskId === "agent-1",
11201 + event.providerThread.pendingBackgroundTasks?.[0]?.parentTaskId === "agent-1" &&
11202 + event.providerThread.pendingBackgroundTasks[1]?.nativeItemId === WAKE_TOOL_USE_ID &&
11203 + event.providerThread.pendingBackgroundTasks[2]?.nativeItemId ===
11204 + "toolu_moved_shell",
11205 ),
11206 );
11207
@@ -11456,7 +11483,12 @@ describe("ClaudeAdapterV2 background wake turns", () => {
11483 const roster = providerThreadRosterEvents(harness.events).at(-1)?.providerThread
11484 .pendingBackgroundTasks;
11485 assert.deepEqual(roster, [
11459 - { taskId: longRunning.taskId, kind: "monitor", description: "Monitor 0" },
11486 + {
11487 + taskId: longRunning.taskId,
11488 + kind: "monitor",
11489 + description: "Monitor 0",
11490 + nativeItemId: "toolu_monitor_0",
11491 + },
11492 ]);
11493 }).pipe(
11494 Effect.provide(
@@ -12816,7 +12848,12 @@ describe("ClaudeAdapterV2 background wake turns", () => {
12848 (event) => event.providerThread.id === providerThreadA.id,
12849 )?.providerThread.pendingBackgroundTasks;
12850 assert.deepEqual(rosterAAfterSettle ?? [], [
12819 - { taskId: taskA, description: "work on A", kind: "command" },
12851 + {
12852 + taskId: taskA,
12853 + description: "work on A",
12854 + kind: "command",
12855 + nativeItemId: "toolu-roster-a",
12856 + },
12857 ]);
12858 assert.isTrue(yield* hasPendingBackgroundWork);
12859 assert.isTrue(yield* hasPendingBackgroundWorkForThread(providerThreadA));
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+41 -2
@@ -3421,6 +3421,7 @@ function claudePendingBackgroundTask(input: {
3421 readonly description: string | undefined;
3422 // Undeclared SDK field: the subagent whose own background work this is.
3423 readonly parentTaskId: unknown;
3424 + readonly toolUseId: string | undefined;
3425 }): OrchestrationV2PendingBackgroundTask {
3426 return {
3427 taskId: input.taskId,
@@ -3435,6 +3436,7 @@ function claudePendingBackgroundTask(input: {
3436 ...(typeof input.parentTaskId === "string" && input.parentTaskId.trim().length > 0
3437 ? { parentTaskId: input.parentTaskId }
3438 : {}),
3439 + ...(input.toolUseId === undefined ? {} : { nativeItemId: input.toolUseId }),
3440 };
3441 }
3442
@@ -3489,6 +3491,7 @@ function claudePendingBackgroundTasksFromRoster(
3491 function parseClaudeBackgroundTaskEntry(
3492 entry: unknown,
3493 monitorTasks: ReadonlyMap<string, unknown>,
3494 + toolUseIds: ReadonlyMap<string, string>,
3495 ): OrchestrationV2PendingBackgroundTask | null {
3496 if (entry === null || typeof entry !== "object") {
3497 return null;
@@ -3509,6 +3512,7 @@ function parseClaudeBackgroundTaskEntry(
3512 startedByMonitor: monitorTasks.has(taskId),
3513 description: typeof description === "string" ? description : undefined,
3514 parentTaskId: Reflect.get(entry, "parent_task_id"),
3515 + toolUseId: toolUseIds.get(taskId),
3516 });
3517 }
3518
@@ -5694,6 +5698,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
5698 const lastKnownOpaqueTasks = yield* Ref.make(
5699 new Map<string, OrchestrationV2PendingBackgroundTask>(),
5700 );
5701 + // Roster entries carry only Claude's task id; the timeline item is keyed by the tool call.
5702 + const toolUseIdsByTaskId = yield* Ref.make(new Map<string, string>());
5703 const claudeMonitors = yield* Ref.make<{
5704 readonly calls: ReadonlySet<string>;
5705 readonly tasks: ReadonlyMap<
@@ -9619,8 +9625,19 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
9625 (taskId, task) =>
9626 task.nativeThreadId === input.nativeThreadId && !listedTaskIds.has(taskId),
9627 );
9628 + const toolUseIds = new Map(yield* Ref.get(toolUseIdsByTaskId));
9629 + // A live task keeps its tool call after the bounded map evicts it.
9630 + const current = rosterForNativeThread(
9631 + yield* Ref.get(pendingBackgroundTasksByNativeThread),
9632 + input.nativeThreadId,
9633 + );
9634 + for (const [taskId, task] of current) {
9635 + if (task.nativeItemId !== undefined && !toolUseIds.has(taskId)) {
9636 + toolUseIds.set(taskId, task.nativeItemId);
9637 + }
9638 + }
9639 for (const entry of roster) {
9623 - const task = parseClaudeBackgroundTaskEntry(entry, monitorTasks);
9640 + const task = parseClaudeBackgroundTaskEntry(entry, monitorTasks, toolUseIds);
9641 if (task !== null) {
9642 nextTasks.push(task);
9643 }
@@ -9628,7 +9645,22 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
9645 yield* replacePendingBackgroundTasks(input.nativeThreadId, nextTasks);
9646 rosterChanged = true;
9647 } else if (message.type === "system" && message.subtype === "task_started") {
9631 - if (!isClaudeNonSubagentTask(message) || message.is_backgrounded === false) {
9648 + if (!isClaudeNonSubagentTask(message)) {
9649 + return false;
9650 + }
9651 + const toolUseId = message.tool_use_id?.trim() || undefined;
9652 + if (toolUseId !== undefined) {
9653 + // A foreground command can move to the background later, so remember every one.
9654 + yield* Ref.update(toolUseIdsByTaskId, (current) => {
9655 + const updated = new Map(current).set(message.task_id, toolUseId);
9656 + for (const oldest of updated.keys()) {
9657 + if (updated.size <= 64) break;
9658 + updated.delete(oldest);
9659 + }
9660 + return updated;
9661 + });
9662 + }
9663 + if (message.is_backgrounded === false) {
9664 return false;
9665 }
9666 const launch =
@@ -9661,11 +9693,18 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
9693 description:
9694 typeof message.description === "string" ? message.description : undefined,
9695 parentTaskId: Reflect.get(message, "parent_task_id"),
9696 + toolUseId,
9697 }),
9698 });
9699 rosterChanged = true;
9700 } else if (message.type === "system" && message.subtype === "task_notification") {
9701 yield* endClaudeMonitorTasks((taskId) => taskId === message.task_id);
9702 + yield* Ref.update(toolUseIdsByTaskId, (current) => {
9703 + if (!current.has(message.task_id)) return current;
9704 + const updated = new Map(current);
9705 + updated.delete(message.task_id);
9706 + return updated;
9707 + });
9708 const removed = yield* clearPendingBackgroundTask(
9709 input.nativeThreadId,
9710 message.task_id,
apps/server/src/orchestration-v2/testkit/fixtures/claude_background_monitor_wake/output.ts
+1
@@ -40,6 +40,7 @@ export function assertClaudeBackgroundMonitorWakeOutput(
40 taskId: MONITOR_TASK_ID,
41 description: "Background monitor test",
42 kind: "monitor",
43 + nativeItemId: "toolu_019b4EUbhEcYP4TS7vZn27N1",
44 });
45 }
46 assert.deepEqual(projection.providerThreads[0]?.pendingBackgroundTasks ?? [], []);
apps/web/src/components/chat/ThreadActivityBanners.tsx
+3 -1
@@ -182,9 +182,11 @@ export function backgroundWorkNowItem(input: {
182 const presentation = presentPendingBackgroundWork(input.tasks);
183 if (presentation === null) return null;
184 const rows = presentation.items.map((task) => {
185 + const itemKey =
186 + input.tasks.find((pending) => pending.taskId === task.taskId)?.nativeItemId ?? task.taskId;
187 // Same key derivePendingBackgroundWork gives turn-item tasks; subagent items carry progress.
188 const item = input.turnItems.findLast(
187 - (candidate) => (candidate.nativeItemRef?.nativeId || candidate.id) === task.taskId,
189 + (candidate) => (candidate.nativeItemRef?.nativeId || candidate.id) === itemKey,
190 );
191 const subagent = item?.type === "subagent" ? item : undefined;
192 return {
packages/contracts/src/orchestrationV2.ts
+2
@@ -1038,6 +1038,8 @@ const PendingBackgroundTaskFields = {
1038 description: Schema.optional(TrimmedNonEmptyString),
1039 /** The subagent task that started this work and waits on it. */
1040 parentTaskId: Schema.optional(TrimmedNonEmptyString),
1041 + /** Native id of the turn item that started this work, when it is not `taskId`. */
1042 + nativeItemId: Schema.optional(TrimmedNonEmptyString),
1043 /**
1044 * The agent declared it resumes when this work ends, so the thread holds like it does for a
1045 * subagent. Only the agent knows whether a command is a wait or a dev server it left running.