Skip to content

Commit eef35b0

Browse files
authored
fix(clients): stop loading thread families when opening chats (#26762)
1 parent 2359643 commit eef35b0

9 files changed

Lines changed: 95 additions & 60 deletions

File tree

‎apps/mobile/src/features/threads/ThreadRouteScreen.tsx‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ import { useSelectedThreadWorktree } from "../../state/use-selected-thread-workt
8080
import { useThreadComposerState } from "../../state/use-thread-composer-state";
8181
import { resolveMergeBackTargetThreadId } from "@t3tools/client-runtime/state/thread-relationships";
8282
import { resolveLatestMergeBackRun } from "@t3tools/client-runtime/state/thread-workflows";
83-
import { threadEnvironment, useThreadFamily } from "../../state/threads";
83+
import { threadEnvironment } from "../../state/threads";
8484
import { EMPTY_ENVIRONMENT_THREAD_STATE } from "@t3tools/client-runtime/state/threads";
8585
import { useProjects } from "../../state/entities";
8686
import { projectThreadContentPresentation } from "./threadContentPresentation";
@@ -243,12 +243,6 @@ function LiveThreadRouteController(props: ThreadRouteScreenProps) {
243243
const connectionsReady = useConnectionsReady();
244244
const { connectionState } = useRemoteConnectionStatus();
245245
const { selectedThread } = useThreadSelection();
246-
// Subagent rows, the open child's own row and its ancestors come from the family when the
247-
// shell leaves idle children out.
248-
useThreadFamily(
249-
selectedThread?.environmentId ?? null,
250-
selectedThread?.lineage.rootThreadId ?? null,
251-
);
252246
const params = props.route.params ?? {};
253247
const environmentIdRaw = firstRouteParam(params.environmentId);
254248
const threadIdRaw = firstRouteParam(params.threadId);

‎apps/mobile/src/state/threads.ts‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
} from "@t3tools/client-runtime/state/threads";
1212
import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
1313
import { AsyncResult, Atom } from "effect/reactivity";
14-
import { useEffect } from "react";
1514

1615
import { environmentCatalog } from "../connection/catalog";
1716
import { connectionAtomRuntime } from "../connection/runtime";
@@ -45,18 +44,6 @@ export const environmentThreadShells = createEnvironmentThreadShellAtoms({
4544
snapshotAtom: threadEnvironment.snapshotAtom,
4645
});
4746

48-
/** Keeps the subagent tree under `rootThreadId` in the thread list while the caller shows it. */
49-
export function useThreadFamily(
50-
environmentId: EnvironmentId | null,
51-
rootThreadId: ThreadId | null,
52-
): void {
53-
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- mounts the subagent tree under the root thread into the shared thread registry while the caller shows it
54-
useEffect(() => {
55-
if (environmentId === null || rootThreadId === null) return;
56-
return threadFamilies.mount(appAtomRegistry, environmentId, rootThreadId);
57-
}, [environmentId, rootThreadId]);
58-
}
59-
6047
// Long enough for the opened screen to subscribe; its subscription keeps the thread after this.
6148
const OPENING_THREAD_HOLD_MS = 3_000;
6249

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ import {
234234
primaryServerKeybindingsAtom,
235235
serverEnvironment,
236236
} from "../state/server";
237-
import { threadEnvironment, useThreadFamily } from "../state/threads";
237+
import { threadEnvironment } from "../state/threads";
238238
import { resolveProviderSkillsForCwd } from "@t3tools/client-runtime/providerSkills";
239239
import { vcsEnvironment } from "../state/vcs";
240240
import {
@@ -483,14 +483,6 @@ export default function ChatView(props: ChatViewProps) {
483483
});
484484
const serverThreadProjection = useQuietThreadProjection(routeThreadDetailRef);
485485
const serverProjection = serverThreadProjection?.projection ?? null;
486-
// Subagent rows, an open child's own row and its ancestors come from the family when the shell
487-
// leaves idle children out. Every source agrees on the root, so the key never flips.
488-
useThreadFamily(
489-
routeKind === "server" ? environmentId : null,
490-
serverThreadShell?.lineage.rootThreadId ??
491-
serverProjection?.thread.lineage.rootThreadId ??
492-
null,
493-
);
494486
// Only the shell-less fallback needs these; with a shell, the shell carries them.
495487
const fallbackDetailRef = serverThreadShell === null ? routeThreadDetailRef : null;
496488
const fallbackUpdatedAt = useThreadUpdatedAt(fallbackDetailRef);

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,16 @@ function WorkflowMemberRow({
5454
providers,
5555
driver,
5656
onOpen,
57-
isThreadAvailable,
5857
}: {
5958
member: RuntimeSubagent;
6059
providerInstanceId: ProviderInstanceId;
6160
provider: ServerProvider | undefined;
6261
providers: ReadonlyArray<ServerProvider> | undefined;
6362
driver: ProviderDriverKind | undefined;
6463
onOpen: (threadId: string) => void;
65-
isThreadAvailable: (threadId: string) => boolean;
6664
}) {
6765
const threadId = member.childThreadId;
68-
const canOpen = threadId !== null && isThreadAvailable(threadId);
66+
const canOpen = threadId !== null;
6967
return (
7068
<li>
7169
<Tooltip>
@@ -117,7 +115,6 @@ export function ThreadLineageWorkflowRow({
117115
providers,
118116
driver,
119117
onOpenThread,
120-
isThreadAvailable,
121118
onStop,
122119
stopping,
123120
stopDisabled,
@@ -129,7 +126,6 @@ export function ThreadLineageWorkflowRow({
129126
readonly providers: ReadonlyArray<ServerProvider> | undefined;
130127
readonly driver: ProviderDriverKind | undefined;
131128
readonly onOpenThread: (threadId: string) => void;
132-
readonly isThreadAvailable: (threadId: string) => boolean;
133129
readonly onStop?: (() => void) | undefined;
134130
readonly stopping: boolean;
135131
readonly stopDisabled: boolean;
@@ -227,7 +223,6 @@ export function ThreadLineageWorkflowRow({
227223
providers={providers}
228224
driver={driver}
229225
onOpen={onOpenThread}
230-
isThreadAvailable={isThreadAvailable}
231226
/>
232227
))}
233228
</ul>
@@ -244,7 +239,6 @@ export function ThreadLineageWorkflowRow({
244239
providers={providers}
245240
driver={driver}
246241
onOpen={onOpenThread}
247-
isThreadAvailable={isThreadAvailable}
248242
/>
249243
))}
250244
{phases.length === 0 && group.unphasedMembers.length === 0 ? (

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

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
1+
import { EnvironmentId, NodeId, ThreadId } from "@t3tools/contracts";
22
import * as Cause from "effect/Cause";
33
import { AsyncResult } from "effect/reactivity";
44
import { expect, it, vi } from "vite-plus/test";
@@ -34,6 +34,7 @@ vi.mock(import("../../state/use-atom-command"), () => ({
3434
}));
3535

3636
import { render } from "../../../test/render";
37+
import { makeThreadProjectionFixture } from "../../test-fixtures";
3738
import { ToastProvider } from "../ui/toast";
3839
import { ThreadRelationshipsPanel } from "./ThreadRelationshipsControl";
3940

@@ -134,3 +135,51 @@ it.each([
134135
await expect.element(screen.getByRole("button", { name: "More thread actions" })).toBeEnabled();
135136
}
136137
});
138+
139+
it("opens an idle child from Lineage when its shell has not been loaded", async () => {
140+
setup();
141+
const projection = makeThreadProjectionFixture();
142+
const childId = ThreadId.make("omitted-child");
143+
const environmentId = EnvironmentId.make("local");
144+
state.projection = {
145+
...projection,
146+
subagents: [
147+
{
148+
id: NodeId.make("agent"),
149+
threadId: projection.thread.id,
150+
runId: null,
151+
parentNodeId: NodeId.make("parent"),
152+
origin: "app_owned",
153+
createdBy: "agent",
154+
driver: "codex",
155+
providerInstanceId: projection.thread.providerInstanceId,
156+
providerThreadId: null,
157+
childThreadId: childId,
158+
nativeTaskRef: null,
159+
prompt: "Inspect the change",
160+
title: "Idle child",
161+
model: null,
162+
status: "completed",
163+
result: null,
164+
startedAt: null,
165+
completedAt: projection.thread.updatedAt,
166+
updatedAt: projection.thread.updatedAt,
167+
},
168+
],
169+
};
170+
await using screen = await render(
171+
<ThreadRelationshipsPanel
172+
environmentId={environmentId}
173+
threadId={projection.thread.id}
174+
showLineage
175+
showSideChats
176+
/>,
177+
);
178+
await screen.getByRole("button", { name: /Previous agents/ }).click();
179+
const child = screen.getByRole("button", { name: /Idle child/ });
180+
await expect.element(child).toBeEnabled();
181+
await child.click();
182+
await vi.waitFor(() =>
183+
expect(screen.router?.state.location.pathname).toBe("/local/omitted-child"),
184+
);
185+
});

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ export function ThreadRelationshipsPanel(props: {
500500
return null;
501501
}
502502

503+
// Idle children can be omitted from the shell; opening one loads its own detail.
503504
const openThread = (threadId: ThreadId) => {
504505
if (openSideChatTab(ref, graph.nodes.get(threadId)?.thread ?? null)) return;
505506
void navigate({
@@ -643,9 +644,7 @@ export function ThreadRelationshipsPanel(props: {
643644
? "group-hover:opacity-0 group-focus-within:opacity-0 pointer-coarse:opacity-0 [@media(hover:none)]:opacity-0"
644645
: "";
645646
const project = projects.find((project) => project.id === node?.thread?.projectId);
646-
const relationshipHint = node?.missing
647-
? "This related thread is unavailable"
648-
: `Open ${relationship.toLowerCase()} in this chat`;
647+
const relationshipHint = `Open ${relationship.toLowerCase()} in this chat`;
649648
const RelationshipPopup = agent ? ThreadHoverCardPopup : TooltipPopup;
650649
const relationshipTooltip = agent ? (
651650
<SubagentTooltipContent
@@ -713,7 +712,6 @@ export function ThreadRelationshipsPanel(props: {
713712
size="sm"
714713
variant="ghost"
715714
part={workflowGroup ? "primary" : "row"}
716-
disabled={node?.missing === true}
717715
onClick={() => openThread(threadId)}
718716
/>
719717
}
@@ -733,9 +731,6 @@ export function ThreadRelationshipsPanel(props: {
733731
providers={providers}
734732
driver={providerDriver}
735733
onOpenThread={(memberThreadId) => openThread(memberThreadId as ThreadId)}
736-
isThreadAvailable={(memberThreadId) =>
737-
graph.nodes.get(memberThreadId as ThreadId)?.missing === false
738-
}
739734
onStop={canStopWorkflow ? () => void stopWorkflow(agent.id) : undefined}
740735
stopping={stoppingId === agent.id}
741736
stopDisabled={stoppingId !== null}
@@ -756,7 +751,6 @@ export function ThreadRelationshipsPanel(props: {
756751
variant="ghost"
757752
part="primary"
758753
aria-label={`${threadTitle} ${threadRelationshipStatusLabel(rowStatus)}`}
759-
disabled={node?.missing === true}
760754
onClick={() => openThread(threadId)}
761755
/>
762756
}

‎apps/web/src/state/threads.ts‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ import {
1414
import type { EnvironmentId, OrchestrationV2ThreadShell, ThreadId } from "@t3tools/contracts";
1515
import * as Option from "effect/Option";
1616
import { AsyncResult, Atom } from "effect/reactivity";
17-
import { useEffect } from "react";
1817

1918
import { environmentCatalog } from "../connection/catalog";
2019
import { connectionAtomRuntime } from "../connection/runtime";
21-
import { appAtomRegistry } from "../rpc/atomRegistry";
2220
import { environmentShell } from "./shell";
2321

2422
export const threadFamilies = createThreadFamilyAtoms(
@@ -42,18 +40,6 @@ export const environmentThreadShells = createEnvironmentThreadShellAtoms({
4240
snapshotAtom: threadEnvironment.snapshotAtom,
4341
});
4442

45-
/** Keeps the subagent tree under `rootThreadId` in the thread list while the caller shows it. */
46-
export function useThreadFamily(
47-
environmentId: EnvironmentId | null,
48-
rootThreadId: ThreadId | null,
49-
): void {
50-
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- mounts the subagent tree into the thread family registry while the caller shows it
51-
useEffect(() => {
52-
if (environmentId === null || rootThreadId === null) return;
53-
return threadFamilies.mount(appAtomRegistry, environmentId, rootThreadId);
54-
}, [environmentId, rootThreadId]);
55-
}
56-
5743
const EMPTY_THREAD_STATE_ATOM = Atom.make(AsyncResult.success(EMPTY_ENVIRONMENT_THREAD_STATE)).pipe(
5844
Atom.withLabel("web-environment-thread:empty"),
5945
);

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,40 @@ describe("thread relationships", () => {
5959
).toBeNull();
6060
});
6161

62+
it.each(["fork", "subagent"] as const)(
63+
"keeps the parent edge of an opened %s omitted from the shell",
64+
(relationshipToParent) => {
65+
const parent = ThreadId.make("parent");
66+
const child = ThreadId.make("omitted-child");
67+
const projection = {
68+
thread: {
69+
id: child,
70+
forkedFrom:
71+
relationshipToParent === "fork"
72+
? { type: "run", threadId: parent, runId: "source-run" }
73+
: null,
74+
lineage: { rootThreadId: parent, parentThreadId: parent, relationshipToParent },
75+
},
76+
subagents: [],
77+
contextTransfers: [],
78+
} as never;
79+
const graph = deriveThreadRelationshipGraph({ threads: [], projection });
80+
expect(immediateThreadRelationships(graph, child)).toEqual([
81+
expect.objectContaining({
82+
threadId: parent,
83+
edge: expect.objectContaining({
84+
sourceThreadId: parent,
85+
targetThreadId: child,
86+
kind: relationshipToParent,
87+
}),
88+
}),
89+
]);
90+
expect(resolveMergeBackTargetThreadId(projection)).toBe(
91+
relationshipToParent === "fork" ? parent : null,
92+
);
93+
},
94+
);
95+
6296
it("keeps an older activity run visible over a newer cancelled run", () => {
6397
const parent = ThreadId.make("thread-parent");
6498
const child = ThreadId.make("thread-child");

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,12 @@ export function deriveThreadRelationshipGraph(input: {
8080
);
8181
};
8282

83-
for (const thread of threads) {
83+
const selectedThread = input.projection?.thread;
84+
const lineageThreads =
85+
selectedThread !== undefined && !threadsById.has(selectedThread.id)
86+
? [...threads, { ...selectedThread, activityRunStatus: null, status: null }]
87+
: threads;
88+
for (const thread of lineageThreads) {
8489
const parentThreadId =
8590
thread.forkedFrom?.type === "run"
8691
? thread.forkedFrom.threadId

0 commit comments

Comments
 (0)