@devries-cameron20 / t3code-1 / commits / 83838af7ba
Fork 0

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

fix(mobile): finished subagents stay reachable from the Agents pill (#25759)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Alex Southwell committed Oct 27, 2026 at 05:13 UTC 83838af7ba7dcc539311640d09b6338372beb148
2 files changed +122 -4
packages/client-runtime/src/state/threadSubagents.test.ts
+94 -2
@@ -41,8 +41,17 @@ function subagent(
41 };
42 }
43
44 -const run = (id: string, status: "running" | "completed" | "rolled_back") =>
45 - ({ id: RunId.make(id), status }) as never;
44 +const run = (
45 + id: string,
46 + status: "queued" | "running" | "completed" | "cancelled" | "rolled_back",
47 + overrides: { readonly ordinal?: number; readonly started?: boolean } = {},
48 +) =>
49 + ({
50 + id: RunId.make(id),
51 + status,
52 + ordinal: overrides.ordinal ?? Number(id.slice("run-".length)),
53 + startedAt: overrides.started === false ? null : at("2026-06-20T00:00:00.000Z"),
54 + }) as never;
55 const activeRun = run("run-1", "running");
56 const finishedRun = run("run-1", "completed");
57
@@ -168,6 +177,89 @@ describe("deriveThreadTurnSubagents", () => {
177 expect(resolveSubagentPillSegment(turn)?.label).toBe("1/2");
178 });
179
180 + it("keeps the finished agents reachable while their turn is the latest", () => {
181 + const turn = deriveThreadTurnSubagents({
182 + runs: [finishedRun],
183 + subagents: [
184 + subagent({ id: "a", status: "completed" }),
185 + subagent({ id: "b", status: "failed" }),
186 + ],
187 + });
188 +
189 + expect(resolveSubagentPillSegment(turn)?.label).toBe("2 done");
190 + });
191 +
192 + it("drops finished agents from the pill once a later turn ran without any", () => {
193 + const turn = deriveThreadTurnSubagents({
194 + runs: [finishedRun, run("run-2", "completed")],
195 + subagents: [subagent({ id: "a", status: "completed" })],
196 + });
197 +
198 + expect(resolveSubagentPillSegment(turn)).toBeNull();
199 + });
200 +
201 + it("shows finished agents again when the later turn was rolled back", () => {
202 + const turn = deriveThreadTurnSubagents({
203 + runs: [finishedRun, run("run-2", "rolled_back")],
204 + subagents: [subagent({ id: "a", status: "completed" })],
205 + });
206 +
207 + expect(resolveSubagentPillSegment(turn)?.label).toBe("1 done");
208 + });
209 +
210 + it("keeps finished agents while a later turn is only queued or was cancelled before starting", () => {
211 + for (const later of [
212 + run("run-2", "queued", { started: false }),
213 + run("run-2", "cancelled", { started: false }),
214 + ]) {
215 + const turn = deriveThreadTurnSubagents({
216 + runs: [finishedRun, later],
217 + subagents: [subagent({ id: "a", status: "completed" })],
218 + });
219 +
220 + expect(resolveSubagentPillSegment(turn)?.label).toBe("1 done");
221 + }
222 + });
223 +
224 + it("finds the latest turn by ordinal when a reordered queue left runs out of order", () => {
225 + const turn = deriveThreadTurnSubagents({
226 + runs: [run("run-1", "completed", { ordinal: 3 }), run("run-2", "completed", { ordinal: 2 })],
227 + subagents: [subagent({ id: "a", status: "completed" })],
228 + });
229 +
230 + expect(resolveSubagentPillSegment(turn)?.label).toBe("1 done");
231 + });
232 +
233 + it("keeps the latest turn's agents when an earlier turn's background agent finishes later", () => {
234 + const turn = deriveThreadTurnSubagents({
235 + runs: [finishedRun, run("run-2", "completed")],
236 + subagents: [
237 + subagent({ id: "late", status: "completed", updatedAt: at("2026-06-20T00:00:09.000Z") }),
238 + subagent({
239 + id: "latest",
240 + runId: RunId.make("run-2"),
241 + status: "completed",
242 + updatedAt: at("2026-06-20T00:00:05.000Z"),
243 + }),
244 + ],
245 + });
246 +
247 + expect(turn?.subagents.map((entry) => entry.id)).toEqual(["latest"]);
248 + expect(resolveSubagentPillSegment(turn)?.label).toBe("1 done");
249 + });
250 +
251 + it("counts only terminal agents as done while a held agent remains", () => {
252 + const turn = deriveThreadTurnSubagents({
253 + runs: [finishedRun],
254 + subagents: [
255 + subagent({ id: "a", status: "completed" }),
256 + subagent({ id: "b", status: "idle" }),
257 + ],
258 + });
259 +
260 + expect(resolveSubagentPillSegment(turn)?.label).toBe("1 done");
261 + });
262 +
263 it("counts idle as neither working nor done", () => {
264 const turn = deriveThreadTurnSubagents({
265 runs: [activeRun],
packages/client-runtime/src/state/threadSubagents.ts
+28 -2
@@ -12,6 +12,8 @@ type RunId = Projection["runs"][number]["id"];
12 export interface ThreadTurnSubagents {
13 readonly runId: RunId | null;
14 readonly turnActive: boolean;
15 + /** No later turn has run since these agents' turn. */
16 + readonly latestTurn: boolean;
17 readonly subagents: ReadonlyArray<Subagent>;
18 readonly liveCount: number;
19 readonly settledCount: number;
@@ -45,7 +47,22 @@ export function deriveThreadTurnSubagents(
47 : latest,
48 first,
49 );
48 - const runId = activeRun?.id ?? latestUpdated.runId;
50 + // Ordinal, not array position: a reordered queue can leave the projection's runs out of order.
51 + // Queued and never-started runs have not replaced anything yet.
52 + const latestStartedRun = projection.runs.reduce<Projection["runs"][number] | null>(
53 + (latest, run) =>
54 + run.startedAt !== null &&
55 + !rolledBackRunIds.has(run.id) &&
56 + (latest === null || run.ordinal > latest.ordinal)
57 + ? run
58 + : latest,
59 + null,
60 + );
61 + const latestStartedRunHasAgents =
62 + latestStartedRun !== null &&
63 + eligible.some((subagent) => subagent.runId === latestStartedRun.id);
64 + const runId =
65 + activeRun?.id ?? (latestStartedRunHasAgents ? latestStartedRun.id : latestUpdated.runId);
66 const subagents = copySorted(
67 eligible.filter(
68 (subagent) =>
@@ -63,15 +80,24 @@ export function deriveThreadTurnSubagents(
80 if (isActiveSubagentStatus(subagent.status)) liveCount += 1;
81 else if (isTerminalSubagentStatus(subagent.status)) settledCount += 1;
82 }
66 - return { runId, turnActive: activeRun !== null, subagents, liveCount, settledCount };
83 + return {
84 + runId,
85 + turnActive: activeRun !== null,
86 + latestTurn: runId === null || latestStartedRun === null || runId === latestStartedRun.id,
87 + subagents,
88 + liveCount,
89 + settledCount,
90 + };
91 }
92
93 export function resolveSubagentPillSegment(
94 turn: ThreadTurnSubagents | null,
95 ): SubagentPillSegment | null {
96 if (turn === null) return null;
97 + // Finished agents stay reachable until a later turn replaces them; workflows always stay reachable.
98 if (
99 !turn.turnActive &&
100 + !turn.latestTurn &&
101 turn.liveCount === 0 &&
102 !turn.subagents.some((subagent) => subagent.workflow !== undefined)
103 ) {