Skip to content

Commit c0bebd2

Browse files
committed
fix(web): finished remote subagents move into Previous agents
Subagents running in a linked environment were rendered in their own list with no status filter, so every finished one stayed in the parent's lineage forever. They now join the same active and Previous agents groups as local subagents.
1 parent 125b619 commit c0bebd2

3 files changed

Lines changed: 117 additions & 43 deletions

File tree

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

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ import { formatSubagentDisplayTitle } from "@t3tools/client-runtime/state/subage
1515
import { deriveProviderSubagentStatus } from "@t3tools/client-runtime/state/thread-execution";
1616
import {
1717
deriveThreadRelationshipGraph,
18+
groupWebThreadLineageRows,
1819
immediateThreadRelationships,
1920
isParentThreadRelationship,
2021
threadRelationshipRowStatus,
2122
orderWebThreadLineageRows,
2223
resolveMergeBackTargetThreadId,
24+
webThreadLineageRowStatus,
25+
type RemoteSubagent,
2326
type ThreadRelationshipEdge,
24-
type ThreadRelationshipWalkRow,
27+
type WebThreadLineageRow,
2528
} from "@t3tools/client-runtime/state/thread-relationships";
2629
import {
2730
canDetachThreadProviderSession,
@@ -33,14 +36,12 @@ import {
3336
NodeId,
3437
type EnvironmentId,
3538
type OrchestrationV2DelegatedFrom,
36-
type OrchestrationV2RemoteTaskChild,
3739
type OrchestrationV2Subagent,
3840
type OrchestrationV2ThreadShell,
3941
type ServerProvider,
4042
type ThreadId,
4143
} from "@t3tools/contracts";
4244
import { deriveSubagentElapsedMs } from "@t3tools/shared/orchestrationTiming";
43-
import { groupBy } from "effect/Array";
4445
import * as DateTime from "effect/DateTime";
4546
import { useNavigate } from "@tanstack/react-router";
4647
import {
@@ -127,15 +128,15 @@ function ThreadLineageRowList(props: {
127128

128129
function ThreadLineageGroup(props: {
129130
readonly label: string | null;
130-
readonly rows: ReadonlyArray<ThreadRelationshipWalkRow>;
131+
readonly rows: ReadonlyArray<WebThreadLineageRow>;
131132
readonly expanded: boolean;
132-
readonly children: (rows: ReadonlyArray<ThreadRelationshipWalkRow>) => ReactNode;
133+
readonly children: (rows: ReadonlyArray<WebThreadLineageRow>) => ReactNode;
133134
}) {
134135
const [expanded, setExpanded] = useState(props.expanded);
135136
const [visibleCount, setVisibleCount] = useState(THREAD_LINEAGE_INITIAL_COUNT);
136137
const { visibleRows, hiddenCount } = resolveThreadLineageWindow(props.rows, visibleCount);
137-
const failedCount = props.rows.filter(
138-
({ edge }) => edge.status === "failed" || edge.status === "error",
138+
const failedCount = props.rows.filter((row) =>
139+
["failed", "error"].includes(webThreadLineageRowStatus(row) ?? ""),
139140
).length;
140141
if (props.rows.length === 0) return null;
141142
return (
@@ -226,9 +227,7 @@ function currentSubagent<Agent extends RuntimeSubagent>(
226227
* the thread there, when this client is connected to that environment.
227228
*/
228229
function RemoteSubagentRow(props: {
229-
readonly agent: OrchestrationV2Subagent & {
230-
readonly remoteChild: OrchestrationV2RemoteTaskChild;
231-
};
230+
readonly agent: RemoteSubagent;
232231
readonly provider: ServerProvider | undefined;
233232
}) {
234233
const navigate = useNavigate();
@@ -443,33 +442,24 @@ export function ThreadRelationshipsPanel(props: {
443442
const canMerge = mergeTargetThreadId !== null && latestMergeBackRun !== null;
444443
const canDetach = projection ? canDetachThreadProviderSession(projection) : false;
445444

446-
const {
447-
related = [],
448-
active = [],
449-
previous = [],
450-
} = groupBy(relationshipRows, ({ edge }) => {
451-
if (edge.kind !== "subagent" || isParentThreadRelationship(edge, props.threadId))
452-
return "related";
453-
return ["completed", "failed", "error", "cancelled", "interrupted", "idle"].includes(
454-
edge.status ?? "",
455-
)
456-
? "previous"
457-
: "active";
445+
const remoteSubagents = (projection?.subagents ?? []).filter(
446+
(agent): agent is RemoteSubagent => agent.remoteChild !== undefined,
447+
);
448+
const { related, active, previous } = groupWebThreadLineageRows({
449+
rows: relationshipRows,
450+
remoteSubagents,
451+
currentThreadId: props.threadId,
458452
});
459453
const groups = [
460454
{ id: "related", label: null, rows: related, expanded: true },
461455
{ id: "active", label: null, rows: active, expanded: true },
462456
{ id: "previous", label: "Previous agents", rows: previous, expanded: false },
463457
];
464-
// Tasks running in a linked environment have no thread here; they get their own rows.
465-
const remoteSubagents = (projection?.subagents ?? []).filter(
466-
(agent): agent is OrchestrationV2Subagent & { remoteChild: OrchestrationV2RemoteTaskChild } =>
467-
agent.remoteChild !== undefined,
468-
);
469458
const runningCount =
470459
(projection?.subagents.filter(
471460
(agent) => agent.childThreadId === null && agent.status === "running",
472-
).length ?? 0) + active.filter(({ edge }) => edge.status === "running").length;
461+
).length ?? 0) +
462+
active.filter((row) => !("remoteChild" in row) && row.edge.status === "running").length;
473463

474464
// A linked environment's agent may have delegated this thread from there.
475465
const delegatedFrom = currentThread?.delegatedFrom;
@@ -599,7 +589,20 @@ export function ThreadRelationshipsPanel(props: {
599589
{groups.map((group) => (
600590
<ThreadLineageGroup key={`${scopedThreadKey(ref)}:${group.id}`} {...group}>
601591
{(visibleRows) =>
602-
visibleRows.map(({ threadId, edge }) => {
592+
visibleRows.map((row) => {
593+
if ("remoteChild" in row) {
594+
return (
595+
<RemoteSubagentRow
596+
key={row.id}
597+
agent={row}
598+
// Its provider instance belongs to the environment it runs in.
599+
provider={providersByEnvironment
600+
.get(row.remoteChild.environmentId)
601+
?.find((entry) => entry.instanceId === row.providerInstanceId)}
602+
/>
603+
);
604+
}
605+
const { threadId, edge } = row;
603606
const node = graph.nodes.get(threadId);
604607
const isSubagent = edge.kind === "subagent";
605608
const isMergeTarget = threadId === mergeTargetThreadId;
@@ -856,20 +859,6 @@ export function ThreadRelationshipsPanel(props: {
856859
}
857860
</ThreadLineageGroup>
858861
))}
859-
{remoteSubagents.length > 0 ? (
860-
<ThreadLineageRowList hiddenCount={0} onShowMore={() => {}}>
861-
{remoteSubagents.map((agent) => (
862-
<RemoteSubagentRow
863-
key={agent.id}
864-
agent={agent}
865-
// Its provider instance belongs to the environment it runs in.
866-
provider={providersByEnvironment
867-
.get(agent.remoteChild.environmentId)
868-
?.find((entry) => entry.instanceId === agent.providerInstanceId)}
869-
/>
870-
))}
871-
</ThreadLineageRowList>
872-
) : null}
873862
</ThreadDetailsSection>
874863
);
875864
}

‎packages/client-runtime/src/state/threadRelationships.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import * as DateTime from "effect/DateTime";
44

55
import {
66
deriveThreadRelationshipGraph,
7+
groupWebThreadLineageRows,
78
immediateThreadRelationships,
89
orderWebThreadLineageRows,
910
resolveMergeBackTargetThreadId,
1011
threadRelationshipRowStatus,
12+
type RemoteSubagent,
13+
type ThreadRelationshipWalkRow,
1114
} from "./threadRelationships.ts";
1215

1316
describe("thread relationships", () => {
@@ -405,3 +408,39 @@ describe("web thread lineage ordering", () => {
405408
).toEqual([dated, missingTransfer, undated]);
406409
});
407410
});
411+
412+
describe("web thread lineage grouping", () => {
413+
it("moves finished subagents into previous, including ones in a linked environment", () => {
414+
const row = (threadId: string, kind: "fork" | "subagent", status: string) =>
415+
({
416+
threadId: ThreadId.make(threadId),
417+
fromThreadId: current,
418+
depth: 1,
419+
edge: { sourceThreadId: current, targetThreadId: ThreadId.make(threadId), kind, status },
420+
}) satisfies ThreadRelationshipWalkRow;
421+
const remote = (id: string, status: string) =>
422+
({
423+
id,
424+
status,
425+
childThreadId: null,
426+
remoteChild: { label: "box" },
427+
}) as never as RemoteSubagent;
428+
const fork = row("thread-fork", "fork", "completed");
429+
const localRunning = row("thread-local-running", "subagent", "running");
430+
const localDone = row("thread-local-done", "subagent", "completed");
431+
const remoteRunning = remote("remote-running", "running");
432+
const remoteDone = remote("remote-done", "completed");
433+
434+
expect(
435+
groupWebThreadLineageRows({
436+
rows: [fork, localRunning, localDone],
437+
remoteSubagents: [remoteRunning, remoteDone],
438+
currentThreadId: current,
439+
}),
440+
).toEqual({
441+
related: [fork],
442+
active: [localRunning, remoteRunning],
443+
previous: [localDone, remoteDone],
444+
});
445+
});
446+
});

‎packages/client-runtime/src/state/threadRelationships.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {
22
isOrchestrationV2WorkActive,
3+
type OrchestrationV2RemoteTaskChild,
4+
type OrchestrationV2Subagent,
35
type OrchestrationV2ThreadProjection,
46
type OrchestrationV2ThreadShell,
57
type ThreadId,
@@ -200,3 +202,47 @@ export function orderWebThreadLineageRows(input: {
200202
return left.threadId < right.threadId ? -1 : left.threadId > right.threadId ? 1 : 0;
201203
});
202204
}
205+
206+
const PREVIOUS_SUBAGENT_STATUSES: ReadonlySet<string | null> = new Set([
207+
"completed",
208+
"failed",
209+
"error",
210+
"cancelled",
211+
"interrupted",
212+
"idle",
213+
]);
214+
215+
/** A task delegated to a linked environment. It has no thread here. */
216+
export type RemoteSubagent = OrchestrationV2Subagent & {
217+
readonly remoteChild: OrchestrationV2RemoteTaskChild;
218+
};
219+
220+
export type WebThreadLineageRow = ThreadRelationshipWalkRow | RemoteSubagent;
221+
222+
export function webThreadLineageRowStatus(row: WebThreadLineageRow): string | null {
223+
return "remoteChild" in row ? row.status : row.edge.status;
224+
}
225+
226+
/** Remote subagents share the active and previous groups with local ones. */
227+
export function groupWebThreadLineageRows(input: {
228+
readonly rows: ReadonlyArray<ThreadRelationshipWalkRow>;
229+
readonly remoteSubagents: ReadonlyArray<RemoteSubagent>;
230+
readonly currentThreadId: ThreadId;
231+
}) {
232+
const related: ThreadRelationshipWalkRow[] = [];
233+
const active: WebThreadLineageRow[] = [];
234+
const previous: WebThreadLineageRow[] = [];
235+
for (const row of [...input.rows, ...input.remoteSubagents]) {
236+
if (
237+
!("remoteChild" in row) &&
238+
(row.edge.kind !== "subagent" || isParentThreadRelationship(row.edge, input.currentThreadId))
239+
) {
240+
related.push(row);
241+
} else if (PREVIOUS_SUBAGENT_STATUSES.has(webThreadLineageRowStatus(row))) {
242+
previous.push(row);
243+
} else {
244+
active.push(row);
245+
}
246+
}
247+
return { related, active, previous };
248+
}

0 commit comments

Comments
 (0)