diff --git a/apps/mobile/src/features/threads/ThreadRouteScreen.tsx b/apps/mobile/src/features/threads/ThreadRouteScreen.tsx index 737e1aff90ada..a7a10a8675122 100644 --- a/apps/mobile/src/features/threads/ThreadRouteScreen.tsx +++ b/apps/mobile/src/features/threads/ThreadRouteScreen.tsx @@ -80,7 +80,7 @@ import { useSelectedThreadWorktree } from "../../state/use-selected-thread-workt import { useThreadComposerState } from "../../state/use-thread-composer-state"; import { resolveMergeBackTargetThreadId } from "@t3tools/client-runtime/state/thread-relationships"; import { resolveLatestMergeBackRun } from "@t3tools/client-runtime/state/thread-workflows"; -import { threadEnvironment, useThreadFamily } from "../../state/threads"; +import { threadEnvironment } from "../../state/threads"; import { EMPTY_ENVIRONMENT_THREAD_STATE } from "@t3tools/client-runtime/state/threads"; import { useProjects } from "../../state/entities"; import { projectThreadContentPresentation } from "./threadContentPresentation"; @@ -243,12 +243,6 @@ function LiveThreadRouteController(props: ThreadRouteScreenProps) { const connectionsReady = useConnectionsReady(); const { connectionState } = useRemoteConnectionStatus(); const { selectedThread } = useThreadSelection(); - // Subagent rows, the open child's own row and its ancestors come from the family when the - // shell leaves idle children out. - useThreadFamily( - selectedThread?.environmentId ?? null, - selectedThread?.lineage.rootThreadId ?? null, - ); const params = props.route.params ?? {}; const environmentIdRaw = firstRouteParam(params.environmentId); const threadIdRaw = firstRouteParam(params.threadId); diff --git a/apps/mobile/src/state/threads.ts b/apps/mobile/src/state/threads.ts index 4cb0f92b4ee83..6ba5c635685a3 100644 --- a/apps/mobile/src/state/threads.ts +++ b/apps/mobile/src/state/threads.ts @@ -11,7 +11,6 @@ import { } from "@t3tools/client-runtime/state/threads"; import type { EnvironmentId, ThreadId } from "@t3tools/contracts"; import { AsyncResult, Atom } from "effect/reactivity"; -import { useEffect } from "react"; import { environmentCatalog } from "../connection/catalog"; import { connectionAtomRuntime } from "../connection/runtime"; @@ -45,18 +44,6 @@ export const environmentThreadShells = createEnvironmentThreadShellAtoms({ snapshotAtom: threadEnvironment.snapshotAtom, }); -/** Keeps the subagent tree under `rootThreadId` in the thread list while the caller shows it. */ -export function useThreadFamily( - environmentId: EnvironmentId | null, - rootThreadId: ThreadId | null, -): void { - // 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 - useEffect(() => { - if (environmentId === null || rootThreadId === null) return; - return threadFamilies.mount(appAtomRegistry, environmentId, rootThreadId); - }, [environmentId, rootThreadId]); -} - // Long enough for the opened screen to subscribe; its subscription keeps the thread after this. const OPENING_THREAD_HOLD_MS = 3_000; diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 22ccaaaf807ee..74c537368bc3e 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -233,7 +233,7 @@ import { primaryServerKeybindingsAtom, serverEnvironment, } from "../state/server"; -import { threadEnvironment, useThreadFamily } from "../state/threads"; +import { threadEnvironment } from "../state/threads"; import { resolveProviderSkillsForCwd } from "@t3tools/client-runtime/providerSkills"; import { vcsEnvironment } from "../state/vcs"; import { @@ -482,14 +482,6 @@ export default function ChatView(props: ChatViewProps) { }); const serverThreadProjection = useQuietThreadProjection(routeThreadDetailRef); const serverProjection = serverThreadProjection?.projection ?? null; - // Subagent rows, an open child's own row and its ancestors come from the family when the shell - // leaves idle children out. Every source agrees on the root, so the key never flips. - useThreadFamily( - routeKind === "server" ? environmentId : null, - serverThreadShell?.lineage.rootThreadId ?? - serverProjection?.thread.lineage.rootThreadId ?? - null, - ); // Only the shell-less fallback needs these; with a shell, the shell carries them. const fallbackDetailRef = serverThreadShell === null ? routeThreadDetailRef : null; const fallbackUpdatedAt = useThreadUpdatedAt(fallbackDetailRef); diff --git a/apps/web/src/components/chat/ThreadLineageWorkflowRow.tsx b/apps/web/src/components/chat/ThreadLineageWorkflowRow.tsx index f1df96dd92133..ae13f519e02fd 100644 --- a/apps/web/src/components/chat/ThreadLineageWorkflowRow.tsx +++ b/apps/web/src/components/chat/ThreadLineageWorkflowRow.tsx @@ -54,7 +54,6 @@ function WorkflowMemberRow({ providers, driver, onOpen, - isThreadAvailable, }: { member: RuntimeSubagent; providerInstanceId: ProviderInstanceId; @@ -62,10 +61,9 @@ function WorkflowMemberRow({ providers: ReadonlyArray | undefined; driver: ProviderDriverKind | undefined; onOpen: (threadId: string) => void; - isThreadAvailable: (threadId: string) => boolean; }) { const threadId = member.childThreadId; - const canOpen = threadId !== null && isThreadAvailable(threadId); + const canOpen = threadId !== null; return (
  • @@ -117,7 +115,6 @@ export function ThreadLineageWorkflowRow({ providers, driver, onOpenThread, - isThreadAvailable, onStop, stopping, stopDisabled, @@ -129,7 +126,6 @@ export function ThreadLineageWorkflowRow({ readonly providers: ReadonlyArray | undefined; readonly driver: ProviderDriverKind | undefined; readonly onOpenThread: (threadId: string) => void; - readonly isThreadAvailable: (threadId: string) => boolean; readonly onStop?: (() => void) | undefined; readonly stopping: boolean; readonly stopDisabled: boolean; @@ -227,7 +223,6 @@ export function ThreadLineageWorkflowRow({ providers={providers} driver={driver} onOpen={onOpenThread} - isThreadAvailable={isThreadAvailable} /> ))} @@ -244,7 +239,6 @@ export function ThreadLineageWorkflowRow({ providers={providers} driver={driver} onOpen={onOpenThread} - isThreadAvailable={isThreadAvailable} /> ))} {phases.length === 0 && group.unphasedMembers.length === 0 ? ( diff --git a/apps/web/src/components/chat/ThreadRelationshipsControl.browser.test.tsx b/apps/web/src/components/chat/ThreadRelationshipsControl.browser.test.tsx index 959d0ac724f23..3124cc7b3fa13 100644 --- a/apps/web/src/components/chat/ThreadRelationshipsControl.browser.test.tsx +++ b/apps/web/src/components/chat/ThreadRelationshipsControl.browser.test.tsx @@ -1,4 +1,4 @@ -import { EnvironmentId, ThreadId } from "@t3tools/contracts"; +import { EnvironmentId, NodeId, ThreadId } from "@t3tools/contracts"; import * as Cause from "effect/Cause"; import { AsyncResult } from "effect/reactivity"; import { expect, it, vi } from "vite-plus/test"; @@ -34,6 +34,7 @@ vi.mock(import("../../state/use-atom-command"), () => ({ })); import { render } from "../../../test/render"; +import { makeThreadProjectionFixture } from "../../test-fixtures"; import { ToastProvider } from "../ui/toast"; import { ThreadRelationshipsPanel } from "./ThreadRelationshipsControl"; @@ -134,3 +135,51 @@ it.each([ await expect.element(screen.getByRole("button", { name: "More thread actions" })).toBeEnabled(); } }); + +it("opens an idle child from Lineage when its shell has not been loaded", async () => { + setup(); + const projection = makeThreadProjectionFixture(); + const childId = ThreadId.make("omitted-child"); + const environmentId = EnvironmentId.make("local"); + state.projection = { + ...projection, + subagents: [ + { + id: NodeId.make("agent"), + threadId: projection.thread.id, + runId: null, + parentNodeId: NodeId.make("parent"), + origin: "app_owned", + createdBy: "agent", + driver: "codex", + providerInstanceId: projection.thread.providerInstanceId, + providerThreadId: null, + childThreadId: childId, + nativeTaskRef: null, + prompt: "Inspect the change", + title: "Idle child", + model: null, + status: "completed", + result: null, + startedAt: null, + completedAt: projection.thread.updatedAt, + updatedAt: projection.thread.updatedAt, + }, + ], + }; + await using screen = await render( + , + ); + await screen.getByRole("button", { name: /Previous agents/ }).click(); + const child = screen.getByRole("button", { name: /Idle child/ }); + await expect.element(child).toBeEnabled(); + await child.click(); + await vi.waitFor(() => + expect(screen.router?.state.location.pathname).toBe("/local/omitted-child"), + ); +}); diff --git a/apps/web/src/components/chat/ThreadRelationshipsControl.tsx b/apps/web/src/components/chat/ThreadRelationshipsControl.tsx index b95ae4093a8a6..0a9d20cd71e41 100644 --- a/apps/web/src/components/chat/ThreadRelationshipsControl.tsx +++ b/apps/web/src/components/chat/ThreadRelationshipsControl.tsx @@ -500,6 +500,7 @@ export function ThreadRelationshipsPanel(props: { return null; } + // Idle children can be omitted from the shell; opening one loads its own detail. const openThread = (threadId: ThreadId) => { if (openSideChatTab(ref, graph.nodes.get(threadId)?.thread ?? null)) return; void navigate({ @@ -643,9 +644,7 @@ export function ThreadRelationshipsPanel(props: { ? "group-hover:opacity-0 group-focus-within:opacity-0 pointer-coarse:opacity-0 [@media(hover:none)]:opacity-0" : ""; const project = projects.find((project) => project.id === node?.thread?.projectId); - const relationshipHint = node?.missing - ? "This related thread is unavailable" - : `Open ${relationship.toLowerCase()} in this chat`; + const relationshipHint = `Open ${relationship.toLowerCase()} in this chat`; const RelationshipPopup = agent ? ThreadHoverCardPopup : TooltipPopup; const relationshipTooltip = agent ? ( openThread(threadId)} /> } @@ -733,9 +731,6 @@ export function ThreadRelationshipsPanel(props: { providers={providers} driver={providerDriver} onOpenThread={(memberThreadId) => openThread(memberThreadId as ThreadId)} - isThreadAvailable={(memberThreadId) => - graph.nodes.get(memberThreadId as ThreadId)?.missing === false - } onStop={canStopWorkflow ? () => void stopWorkflow(agent.id) : undefined} stopping={stoppingId === agent.id} stopDisabled={stoppingId !== null} @@ -756,7 +751,6 @@ export function ThreadRelationshipsPanel(props: { variant="ghost" part="primary" aria-label={`${threadTitle} ${threadRelationshipStatusLabel(rowStatus)}`} - disabled={node?.missing === true} onClick={() => openThread(threadId)} /> } diff --git a/apps/web/src/state/threads.ts b/apps/web/src/state/threads.ts index 07fffa97cfbe0..c7919d658ccf6 100644 --- a/apps/web/src/state/threads.ts +++ b/apps/web/src/state/threads.ts @@ -14,11 +14,9 @@ import { import type { EnvironmentId, OrchestrationV2ThreadShell, ThreadId } from "@t3tools/contracts"; import * as Option from "effect/Option"; import { AsyncResult, Atom } from "effect/reactivity"; -import { useEffect } from "react"; import { environmentCatalog } from "../connection/catalog"; import { connectionAtomRuntime } from "../connection/runtime"; -import { appAtomRegistry } from "../rpc/atomRegistry"; import { environmentShell } from "./shell"; export const threadFamilies = createThreadFamilyAtoms( @@ -42,18 +40,6 @@ export const environmentThreadShells = createEnvironmentThreadShellAtoms({ snapshotAtom: threadEnvironment.snapshotAtom, }); -/** Keeps the subagent tree under `rootThreadId` in the thread list while the caller shows it. */ -export function useThreadFamily( - environmentId: EnvironmentId | null, - rootThreadId: ThreadId | null, -): void { - // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- mounts the subagent tree into the thread family registry while the caller shows it - useEffect(() => { - if (environmentId === null || rootThreadId === null) return; - return threadFamilies.mount(appAtomRegistry, environmentId, rootThreadId); - }, [environmentId, rootThreadId]); -} - const EMPTY_THREAD_STATE_ATOM = Atom.make(AsyncResult.success(EMPTY_ENVIRONMENT_THREAD_STATE)).pipe( Atom.withLabel("web-environment-thread:empty"), ); diff --git a/packages/client-runtime/src/state/threadRelationships.test.ts b/packages/client-runtime/src/state/threadRelationships.test.ts index 9070b7628af1d..90dc68c21dd82 100644 --- a/packages/client-runtime/src/state/threadRelationships.test.ts +++ b/packages/client-runtime/src/state/threadRelationships.test.ts @@ -59,6 +59,40 @@ describe("thread relationships", () => { ).toBeNull(); }); + it.each(["fork", "subagent"] as const)( + "keeps the parent edge of an opened %s omitted from the shell", + (relationshipToParent) => { + const parent = ThreadId.make("parent"); + const child = ThreadId.make("omitted-child"); + const projection = { + thread: { + id: child, + forkedFrom: + relationshipToParent === "fork" + ? { type: "run", threadId: parent, runId: "source-run" } + : null, + lineage: { rootThreadId: parent, parentThreadId: parent, relationshipToParent }, + }, + subagents: [], + contextTransfers: [], + } as never; + const graph = deriveThreadRelationshipGraph({ threads: [], projection }); + expect(immediateThreadRelationships(graph, child)).toEqual([ + expect.objectContaining({ + threadId: parent, + edge: expect.objectContaining({ + sourceThreadId: parent, + targetThreadId: child, + kind: relationshipToParent, + }), + }), + ]); + expect(resolveMergeBackTargetThreadId(projection)).toBe( + relationshipToParent === "fork" ? parent : null, + ); + }, + ); + it("keeps an older activity run visible over a newer cancelled run", () => { const parent = ThreadId.make("thread-parent"); const child = ThreadId.make("thread-child"); diff --git a/packages/client-runtime/src/state/threadRelationships.ts b/packages/client-runtime/src/state/threadRelationships.ts index a0c63adccaaca..2aecc93470c2f 100644 --- a/packages/client-runtime/src/state/threadRelationships.ts +++ b/packages/client-runtime/src/state/threadRelationships.ts @@ -80,7 +80,12 @@ export function deriveThreadRelationshipGraph(input: { ); }; - for (const thread of threads) { + const selectedThread = input.projection?.thread; + const lineageThreads = + selectedThread !== undefined && !threadsById.has(selectedThread.id) + ? [...threads, { ...selectedThread, activityRunStatus: null, status: null }] + : threads; + for (const thread of lineageThreads) { const parentThreadId = thread.forkedFrom?.type === "run" ? thread.forkedFrom.threadId