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 23, 05:30 UTC
fix(client-runtime): the agents pill counts background agents from earlier turns (#20785)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 16, 2026 at 23:23 UTC
b7e376e96e2eb6fe5f23985ddf54172dd5f1fd98
2 files changed
+140
-8
packages/client-runtime/src/state/threadSubagents.test.ts
+116
-2
@@ -41,7 +41,7 @@ function subagent(
41
};
42
}
43
44
-const run = (id: string, status: "running" | "completed") =>
44
+const run = (id: string, status: "running" | "completed" | "rolled_back") =>
45
({ id: RunId.make(id), status }) as never;
46
const activeRun = run("run-1", "running");
47
const finishedRun = run("run-1", "completed");
@@ -56,7 +56,7 @@ describe("deriveThreadTurnSubagents", () => {
56
runs: [run("run-0", "completed"), activeRun],
57
subagents: [
58
subagent({ id: "b", startedAt: at("2026-06-20T00:00:05.000Z") }),
59
- subagent({ id: "old", runId: RunId.make("run-0") }),
59
+ subagent({ id: "old", runId: RunId.make("run-0"), status: "completed" }),
60
subagent({ id: "a", startedAt: at("2026-06-20T00:00:02.000Z") }),
61
],
62
});
@@ -85,6 +85,120 @@ describe("deriveThreadTurnSubagents", () => {
85
expect(turn?.subagents.map((entry) => entry.id)).toEqual(["recent"]);
86
});
87
88
+ it("lists every live agent from earlier turns once no turn is running", () => {
89
+ const turn = deriveThreadTurnSubagents({
90
+ runs: ["run-103", "run-105", "run-107", "run-109"].map((id) => run(id, "completed")),
91
+ subagents: [
92
+ subagent({
93
+ id: "settled-103",
94
+ runId: RunId.make("run-103"),
95
+ status: "completed",
96
+ startedAt: at("2026-06-20T00:00:00.000Z"),
97
+ updatedAt: at("2026-06-20T00:00:04.000Z"),
98
+ }),
99
+ subagent({
100
+ id: "bg-103",
101
+ runId: RunId.make("run-103"),
102
+ startedAt: at("2026-06-20T00:00:01.000Z"),
103
+ updatedAt: at("2026-06-20T00:00:05.000Z"),
104
+ }),
105
+ subagent({
106
+ id: "bg-105",
107
+ runId: RunId.make("run-105"),
108
+ startedAt: at("2026-06-20T00:00:02.000Z"),
109
+ updatedAt: at("2026-06-20T00:00:06.000Z"),
110
+ }),
111
+ subagent({
112
+ id: "bg-107",
113
+ runId: RunId.make("run-107"),
114
+ startedAt: at("2026-06-20T00:00:03.000Z"),
115
+ updatedAt: at("2026-06-20T00:00:07.000Z"),
116
+ }),
117
+ ],
118
+ });
119
+
120
+ expect(turn?.subagents.map((entry) => entry.id)).toEqual(["bg-103", "bg-105", "bg-107"]);
121
+ expect(resolveSubagentPillSegment(turn)?.label).toBe("3/3");
122
+ });
123
+
124
+ it("keeps the active turn's roster and adds a live agent from an earlier turn", () => {
125
+ const turn = deriveThreadTurnSubagents({
126
+ runs: [run("run-0", "completed"), activeRun],
127
+ subagents: [
128
+ subagent({ id: "background", runId: RunId.make("run-0") }),
129
+ subagent({ id: "earlier-done", runId: RunId.make("run-0"), status: "completed" }),
130
+ subagent({ id: "working", startedAt: at("2026-06-20T00:00:05.000Z") }),
131
+ subagent({
132
+ id: "done",
133
+ status: "completed",
134
+ startedAt: at("2026-06-20T00:00:06.000Z"),
135
+ }),
136
+ ],
137
+ });
138
+
139
+ expect(turn?.runId).toBe("run-1");
140
+ expect(turn?.turnActive).toBe(true);
141
+ expect(turn?.subagents.map((entry) => entry.id)).toEqual(["background", "working", "done"]);
142
+ expect(resolveSubagentPillSegment(turn)?.label).toBe("2/3");
143
+ });
144
+
145
+ it("leaves out a live agent whose turn was rolled back", () => {
146
+ const turn = deriveThreadTurnSubagents({
147
+ runs: [run("run-0", "rolled_back"), finishedRun],
148
+ subagents: [
149
+ subagent({ id: "rolled-back", runId: RunId.make("run-0") }),
150
+ subagent({ id: "live", updatedAt: at("2026-06-20T00:00:09.000Z") }),
151
+ ],
152
+ });
153
+
154
+ expect(turn?.subagents.map((entry) => entry.id)).toEqual(["live"]);
155
+ });
156
+
157
+ it("does not pick a rolled-back turn when its agent was updated last", () => {
158
+ const turn = deriveThreadTurnSubagents({
159
+ runs: [run("run-0", "rolled_back"), finishedRun],
160
+ subagents: [
161
+ subagent({
162
+ id: "rolled-back",
163
+ runId: RunId.make("run-0"),
164
+ updatedAt: at("2026-06-20T00:00:09.000Z"),
165
+ }),
166
+ subagent({
167
+ id: "done",
168
+ status: "completed",
169
+ updatedAt: at("2026-06-20T00:00:05.000Z"),
170
+ }),
171
+ ],
172
+ });
173
+
174
+ expect(turn?.runId).toBe("run-1");
175
+ expect(turn?.subagents.map((entry) => entry.id)).toEqual(["done"]);
176
+ expect(
177
+ deriveThreadTurnSubagents({
178
+ runs: [run("run-0", "rolled_back")],
179
+ subagents: [subagent({ id: "rolled-back", runId: RunId.make("run-0") })],
180
+ }),
181
+ ).toBeNull();
182
+ });
183
+
184
+ it("lists a workflow coordinator's members, which carry no run", () => {
185
+ const turn = deriveThreadTurnSubagents({
186
+ runs: [],
187
+ subagents: [
188
+ subagent({ id: "member-0", runId: null, startedAt: at("2026-06-20T00:00:01.000Z") }),
189
+ subagent({
190
+ id: "member-1",
191
+ runId: null,
192
+ status: "completed",
193
+ startedAt: at("2026-06-20T00:00:02.000Z"),
194
+ }),
195
+ ],
196
+ });
197
+
198
+ expect(turn?.subagents.map((entry) => entry.id)).toEqual(["member-0", "member-1"]);
199
+ expect(resolveSubagentPillSegment(turn)?.label).toBe("1/2");
200
+ });
201
+
202
it("counts idle as neither working nor done", () => {
203
const turn = deriveThreadTurnSubagents({
204
runs: [activeRun],
packages/client-runtime/src/state/threadSubagents.ts
+24
-6
@@ -26,19 +26,37 @@ function orderKey(subagent: Subagent): number {
26
return DateTime.toEpochMillis(subagent.startedAt ?? subagent.updatedAt);
27
}
28
29
+/**
30
+ * The agents behind the thread's agents pill and sheet: every agent of the active run (or, once
31
+ * the thread is idle, of the run the most recently updated agent belongs to), plus any agent
32
+ * still working from another run. A background agent outlives the turn that launched it, and the
33
+ * Waiting strip counts it, so the pill has to count it and the sheet has to reach it too. Agents
34
+ * of rolled-back runs stay out, as they do from the strip.
35
+ */
36
export function deriveThreadTurnSubagents(
37
projection: Pick<Projection, "runs" | "subagents">,
38
): ThreadTurnSubagents | null {
32
- if (projection.subagents.length === 0) return null;
39
+ const rolledBackRunIds = new Set(
40
+ projection.runs.filter((run) => run.status === "rolled_back").map((run) => run.id),
41
+ );
42
+ const eligible = projection.subagents.filter(
43
+ (subagent) => subagent.runId === null || !rolledBackRunIds.has(subagent.runId),
44
+ );
45
+ const [first] = eligible;
46
+ if (first === undefined) return null;
47
const activeRun = resolveActiveThreadRun(projection);
34
- const latestUpdated = projection.subagents.reduce((latest, subagent) =>
35
- DateTime.toEpochMillis(subagent.updatedAt) > DateTime.toEpochMillis(latest.updatedAt)
36
- ? subagent
37
- : latest,
48
+ const latestUpdated = eligible.reduce(
49
+ (latest, subagent) =>
50
+ DateTime.toEpochMillis(subagent.updatedAt) > DateTime.toEpochMillis(latest.updatedAt)
51
+ ? subagent
52
+ : latest,
53
+ first,
54
);
55
const runId = activeRun?.id ?? latestUpdated.runId;
56
const subagents = copySorted(
41
- projection.subagents.filter((subagent) => subagent.runId === runId),
57
+ eligible.filter(
58
+ (subagent) => subagent.runId === runId || isActiveSubagentStatus(subagent.status),
59
+ ),
60
(left, right) => orderKey(left) - orderKey(right) || left.id.localeCompare(right.id),
61
);
62
if (subagents.length === 0) return null;