Skip to content

Commit 84ee58c

Browse files
committed
fix(web): match each pending subagent to its own record
A child thread can host several subagent records. Prefer the task's native id, then the newest live record for that thread, and never reuse a record for two rows.
1 parent a7f7e05 commit 84ee58c

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,26 @@ export function presentBackgroundWork(
6060
): BackgroundWorkPresentation | null {
6161
const pending = presentPendingBackgroundWork(tasks);
6262
if (pending === null) return null;
63-
const byChild = new Map(
64-
subagents.flatMap((agent) =>
65-
agent.childThreadId === null ? [] : [[agent.childThreadId, agent] as const],
66-
),
63+
// A child thread can host several subagent records, so prefer the task's own record, then
64+
// the newest live one for that thread, and never give one record to two rows.
65+
const newestFirst = subagents.toSorted(
66+
(left, right) =>
67+
Number(subagentRowStatus(right.status) === "running") -
68+
Number(subagentRowStatus(left.status) === "running") ||
69+
DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
6770
);
68-
const matched: OrchestrationV2Subagent[] = [];
71+
const rowByAgent = new Map<OrchestrationV2Subagent, BackgroundWorkRow>();
6972
const running = pending.items.map((item): BackgroundWorkRow => {
70-
const agent = item.childThreadId === undefined ? undefined : byChild.get(item.childThreadId);
71-
if (agent) matched.push(agent);
72-
return {
73+
const unclaimed = newestFirst.filter((agent) => !rowByAgent.has(agent));
74+
const agent =
75+
item.kind !== "subagent"
76+
? undefined
77+
: (unclaimed.find((candidate) => candidate.nativeTaskRef?.nativeId === item.taskId) ??
78+
unclaimed.find(
79+
(candidate) =>
80+
item.childThreadId !== undefined && candidate.childThreadId === item.childThreadId,
81+
));
82+
const row: BackgroundWorkRow = {
7383
key: item.taskId,
7484
kind: item.kind,
7585
label: item.label,
@@ -79,7 +89,10 @@ export function presentBackgroundWork(
7989
endedAtMs: null,
8090
childThreadId: item.childThreadId,
8191
};
92+
if (agent) rowByAgent.set(agent, row);
93+
return row;
8294
});
95+
const matched = [...rowByAgent.keys()];
8396
// Siblings from the same run that already finished; the pending list drops them.
8497
const runIds = new Set(matched.map((agent) => agent.runId).filter((runId) => runId !== null));
8598
const matchedIds = new Set(matched.map((agent) => agent.id));
@@ -110,7 +123,7 @@ export function presentBackgroundWork(
110123
(left, right) =>
111124
DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
112125
)[0];
113-
const newestRow = newest && running.find((row) => row.childThreadId === newest.childThreadId);
126+
const newestRow = newest && rowByAgent.get(newest);
114127
const summary =
115128
newestRow?.activity != null
116129
? `${newestRow.label}: ${newestRow.activity}`

0 commit comments

Comments
 (0)