diff --git a/apps/mobile/src/features/threads/ThreadRelationshipsBanner.tsx b/apps/mobile/src/features/threads/ThreadRelationshipsBanner.tsx index dec61bc94a..1f4f6c8b53 100644 --- a/apps/mobile/src/features/threads/ThreadRelationshipsBanner.tsx +++ b/apps/mobile/src/features/threads/ThreadRelationshipsBanner.tsx @@ -83,7 +83,13 @@ export function ThreadRelationshipsBanner(props: { const rows = useMemo( () => copySorted( - immediateThreadRelationships(graph, props.threadId), + // Subagent edges come from the projection, not from thread shells, so + // they survive the shell filtering and would point at threads the + // client no longer receives — rendering as "Unavailable", and taking + // over the banner headline when they sort first. + immediateThreadRelationships(graph, props.threadId).filter( + (relationship) => relationship.edge.kind !== "subagent", + ), (left, right) => Number(right.threadId === mergeTargetThreadId) - Number(left.threadId === mergeTargetThreadId), diff --git a/apps/mobile/src/features/threads/thread-work-log.tsx b/apps/mobile/src/features/threads/thread-work-log.tsx index 1b759d5183..ee822e4e80 100644 --- a/apps/mobile/src/features/threads/thread-work-log.tsx +++ b/apps/mobile/src/features/threads/thread-work-log.tsx @@ -128,9 +128,6 @@ function ThreadActivityThreadLink(props: { if (item.type === "thread_created") { targetThreadId = item.targetThreadId; label = "Open created thread"; - } else if (item.type === "subagent") { - targetThreadId = support.subagent?.childThreadId ?? item.childThreadId; - label = "Open subagent thread"; } else if (item.type === "fork") { targetThreadId = item.targetThreadId === row.sourceThreadId && item.source.type === "run" diff --git a/apps/server/src/orchestration-v2/ThreadManagementService.test.ts b/apps/server/src/orchestration-v2/ThreadManagementService.test.ts index e03d60c00d..5fa8a05514 100644 --- a/apps/server/src/orchestration-v2/ThreadManagementService.test.ts +++ b/apps/server/src/orchestration-v2/ThreadManagementService.test.ts @@ -5,6 +5,7 @@ import { NodeId, type OrchestrationV2Command, type OrchestrationV2ThreadProjection, + type OrchestrationV2ThreadShell, ProjectId, ProviderInstanceId, RunId, @@ -26,6 +27,7 @@ import { ThreadManagementThreadNotInterruptibleError, ThreadManagementThreadArchivedError, ThreadManagementNoSteerableRunError, + userFacingShellSnapshot, withCreationProvenance, } from "./ThreadManagementService.ts"; @@ -318,3 +320,59 @@ it.effect("uses thread-not-found only after a projection loads outside the proje expect("cause" in error).toBe(false); }).pipe(Effect.provide(testLayer)); }); + +it("removes internal subagent children from active and archived shell collections", () => { + const rootId = ThreadId.make("thread:thread-management:root"); + const forkId = ThreadId.make("thread:thread-management:fork"); + const lineageSubagentId = ThreadId.make("thread:thread-management:lineage-subagent"); + const nodeSubagentId = ThreadId.make("thread:thread-management:node-subagent"); + const shell = ( + id: ThreadId, + lineage: OrchestrationV2ThreadShell["lineage"], + forkedFrom: OrchestrationV2ThreadShell["forkedFrom"], + ) => + ({ + id, + lineage, + forkedFrom, + }) as OrchestrationV2ThreadShell; + const rootLineage = { + rootThreadId: rootId, + parentThreadId: null, + relationshipToParent: null, + } as const; + const snapshot = userFacingShellSnapshot({ + schemaVersion: 3, + snapshotSequence: 10, + threads: [ + shell(rootId, rootLineage, null), + shell( + forkId, + { + rootThreadId: rootId, + parentThreadId: rootId, + relationshipToParent: "fork", + }, + { type: "run", threadId: rootId, runId: RunId.make("run:thread-management:fork") }, + ), + shell( + lineageSubagentId, + { + rootThreadId: rootId, + parentThreadId: rootId, + relationshipToParent: "subagent", + }, + null, + ), + ], + archivedThreads: [ + shell(nodeSubagentId, rootLineage, { + type: "node", + nodeId: NodeId.make("node:thread-management:subagent"), + }), + ], + }); + + expect(snapshot.threads.map((thread) => thread.id)).toEqual([rootId, forkId]); + expect(snapshot.archivedThreads).toEqual([]); +}); diff --git a/apps/server/src/orchestration-v2/ThreadManagementService.ts b/apps/server/src/orchestration-v2/ThreadManagementService.ts index a85477b788..9611591091 100644 --- a/apps/server/src/orchestration-v2/ThreadManagementService.ts +++ b/apps/server/src/orchestration-v2/ThreadManagementService.ts @@ -308,6 +308,16 @@ export class ThreadManagementService extends Context.Service< ThreadManagementServiceShape >()("t3/orchestration-v2/ThreadManagementService") {} +export const isInternalSubagentThread = ( + thread: Pick, +) => thread.lineage.relationshipToParent === "subagent" || thread.forkedFrom?.type === "node"; + +export const userFacingShellSnapshot = (snapshot: OrchestrationV2ThreadShellSnapshot) => ({ + ...snapshot, + threads: snapshot.threads.filter((thread) => !isInternalSubagentThread(thread)), + archivedThreads: snapshot.archivedThreads.filter((thread) => !isInternalSubagentThread(thread)), +}); + export function isActiveRun(run: OrchestrationV2Run): boolean { return ( run.status === "preparing" || @@ -452,10 +462,7 @@ const make = Effect.gen(function* () { Effect.map((snapshot) => snapshot.threads .filter((thread) => thread.projectId === input.projectId) - .filter( - (thread) => - input.includeSubagents || thread.lineage.relationshipToParent !== "subagent", - ) + .filter((thread) => input.includeSubagents || !isInternalSubagentThread(thread)) .toSorted( (left, right) => DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt) || diff --git a/apps/server/src/orchestration-v2/http.ts b/apps/server/src/orchestration-v2/http.ts index 045afefd53..9bdb212549 100644 --- a/apps/server/src/orchestration-v2/http.ts +++ b/apps/server/src/orchestration-v2/http.ts @@ -89,7 +89,9 @@ export const orchestrationHttpApiLayer = HttpApiBuilder.group( const base = yield* sql.withTransaction( Effect.gen(function* () { const projects = yield* projectionSnapshotQuery.getShellSnapshotWithoutEnrichment(); - const threads = yield* threadManagement.getShellSnapshot(); + const threads = yield* threadManagement + .getShellSnapshot() + .pipe(Effect.map(ThreadManagementService.userFacingShellSnapshot)); return { schemaVersion: threads.schemaVersion, snapshotSequence: yield* applicationEvents.latestApplicationSequence, diff --git a/apps/server/src/serverRuntimeStartup.ts b/apps/server/src/serverRuntimeStartup.ts index 99af107696..1cf30a7b74 100644 --- a/apps/server/src/serverRuntimeStartup.ts +++ b/apps/server/src/serverRuntimeStartup.ts @@ -221,7 +221,7 @@ export const resolveAutoBootstrapWelcomeTargets = Effect.gen(function* () { const shell = yield* threads.getShellSnapshot(); const existingThread = shell.threads.find( (thread) => - thread.projectId === project.id && thread.lineage.relationshipToParent !== "subagent", + thread.projectId === project.id && !ThreadManagement.isInternalSubagentThread(thread), ); if (existingThread === undefined) { const launched = yield* threadLaunch.launch({ diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index 3daaa28a84..43424f6880 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -744,7 +744,9 @@ const makeWsRpcLayer = ( const base = yield* sql.withTransaction( Effect.gen(function* () { const projects = yield* projectionSnapshotQuery.getShellSnapshotWithoutEnrichment(); - const threads = yield* threadManagement.getShellSnapshot(); + const threads = yield* threadManagement + .getShellSnapshot() + .pipe(Effect.map(ThreadManagementService.userFacingShellSnapshot)); return { schemaVersion: threads.schemaVersion, snapshotSequence: yield* applicationEvents.latestApplicationSequence, @@ -802,7 +804,16 @@ const makeWsRpcLayer = ( return yield* projectItem(stored); } const shell = yield* threadManagement.getThreadShell(stored.event.threadId); - return shellStreamItemFromThreadShell({ stored, shell }); + // An internal subagent thread is not part of the user-facing + // stream; treating its shell as absent emits a removal the + // client reducer absorbs. + return shellStreamItemFromThreadShell({ + stored, + shell: + shell !== null && ThreadManagementService.isInternalSubagentThread(shell) + ? null + : shell, + }); }), { concurrency: 8 }, ); @@ -937,7 +948,9 @@ const makeWsRpcLayer = ( .withTransaction( Effect.gen(function* () { const projects = yield* projectionSnapshotQuery.getShellSnapshotWithoutEnrichment(); - const threads = yield* threadManagement.getShellSnapshot(); + const threads = yield* threadManagement + .getShellSnapshot() + .pipe(Effect.map(ThreadManagementService.userFacingShellSnapshot)); return { schemaVersion: threads.schemaVersion, snapshotSequence: yield* applicationEvents.latestApplicationSequence, @@ -973,13 +986,17 @@ const makeWsRpcLayer = ( Effect.forEach( coalesceStoredThreadEvents(Array.from(events)), (stored) => - threadManagement - .getThreadShell(stored.event.threadId) - .pipe( - Effect.map((shell) => - archivedShellStreamItemFromThreadShell({ stored, shell }), - ), + threadManagement.getThreadShell(stored.event.threadId).pipe( + Effect.map((shell) => + archivedShellStreamItemFromThreadShell({ + stored, + shell: + shell !== null && ThreadManagementService.isInternalSubagentThread(shell) + ? null + : shell, + }), ), + ), { concurrency: 8 }, ), ), diff --git a/apps/web/src/components/Sidebar.logic.test.ts b/apps/web/src/components/Sidebar.logic.test.ts index 81855f4d55..8be21dab01 100644 --- a/apps/web/src/components/Sidebar.logic.test.ts +++ b/apps/web/src/components/Sidebar.logic.test.ts @@ -41,7 +41,14 @@ import { sortSidebarV2ProjectGroups, sortThreadsForSidebar, } from "./Sidebar.logic"; -import { EnvironmentId, ProjectId, ProviderInstanceId, RunId, ThreadId } from "@t3tools/contracts"; +import { + EnvironmentId, + NodeId, + ProjectId, + ProviderInstanceId, + RunId, + ThreadId, +} from "@t3tools/contracts"; import { DEFAULT_INTERACTION_MODE, DEFAULT_RUNTIME_MODE, @@ -304,6 +311,13 @@ describe("sidebar thread lineage helpers", () => { }); expect(isSidebarSubagentThread(subagent)).toBe(true); + expect( + isSidebarSubagentThread( + makeThreadFixture({ + forkedFrom: { type: "node", nodeId: NodeId.make("node-provider-subagent") }, + }), + ), + ).toBe(true); expect(isSidebarSubagentThread(makeThreadFixture())).toBe(false); }); diff --git a/apps/web/src/components/Sidebar.logic.ts b/apps/web/src/components/Sidebar.logic.ts index fdbd87b56e..551b7c355a 100644 --- a/apps/web/src/components/Sidebar.logic.ts +++ b/apps/web/src/components/Sidebar.logic.ts @@ -1,6 +1,9 @@ import * as React from "react"; import type { ContextMenuItem } from "@t3tools/contracts"; import type { SidebarProjectSortOrder, SidebarThreadSortOrder } from "@t3tools/contracts/settings"; +import { isInternalSubagentThread as isSidebarSubagentThread } from "../threadVisibility"; + +export { isSidebarSubagentThread }; import { getThreadSortTimestamp, sortThreads, @@ -97,12 +100,8 @@ export function buildMultiSelectThreadContextMenuItems(input: { ]; } -export function isSidebarSubagentThread(thread: Pick): boolean { - return thread.lineage.relationshipToParent === "subagent"; -} - export function filterSidebarV2VisibleThreads< - T extends Pick & { + T extends Pick & { environmentId: string; projectId: string; }, @@ -982,7 +981,7 @@ export function sortLogicalProjectsForSidebar< export function sortSidebarV2ProjectGroups< TProject extends LogicalSidebarProject, - TThread extends ScopedSidebarThread & Pick, + TThread extends ScopedSidebarThread & Pick, >( projects: readonly TProject[], threads: readonly TThread[], diff --git a/apps/web/src/components/chat/MessagesTimeline.test.tsx b/apps/web/src/components/chat/MessagesTimeline.test.tsx index 1892d0b8be..41e635fd5f 100644 --- a/apps/web/src/components/chat/MessagesTimeline.test.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.test.tsx @@ -1117,7 +1117,7 @@ describe("MessagesTimeline", () => { ); expect(markup).toContain('data-v2-item-type="subagent"'); - expect(markup).toContain('aria-label="Open Package audit"'); + expect(markup).not.toContain('aria-label="Open Package audit"'); expect(markup).toContain("Reading src/index.ts"); expect(markup).not.toContain("Inspect the package"); expect(markup).not.toContain('data-v2-subagent-result-disclosure="true"'); @@ -1173,7 +1173,7 @@ describe("MessagesTimeline", () => { expect(markup).toContain('data-v2-subagent-result-disclosure="true"'); expect(markup).toContain('data-v2-subagent-result="true"'); expect(markup).toContain('aria-label="Show full result for Isolation report"'); - expect(markup).toContain('aria-label="Open Isolation report"'); + expect(markup).not.toContain('aria-label="Open Isolation report"'); expect(markup).toContain("Tests should be isolated."); expect(markup).toContain("Result: no shared state."); expect(markup).not.toContain("Explain test isolation"); @@ -1226,7 +1226,7 @@ describe("MessagesTimeline", () => { ); expect(markup).toContain('data-v2-item-type="subagent"'); - expect(markup).toContain('aria-label="Open Package audit"'); + expect(markup).not.toContain('aria-label="Open Package audit"'); expect(markup).toContain("Reading src/index.ts"); expect(markup).not.toContain("Partial streamed answer so far"); expect(markup).not.toContain('data-v2-subagent-result-disclosure="true"'); diff --git a/apps/web/src/components/chat/ThreadRelationshipsControl.tsx b/apps/web/src/components/chat/ThreadRelationshipsControl.tsx index f2685bc92c..af1f49ac7b 100644 --- a/apps/web/src/components/chat/ThreadRelationshipsControl.tsx +++ b/apps/web/src/components/chat/ThreadRelationshipsControl.tsx @@ -150,7 +150,11 @@ export function ThreadRelationshipsPanel(props: { () => orderWebThreadLineageRows({ graph, - rows: immediateThreadRelationships(graph, props.threadId), + // Subagent edges point at hidden internal threads; rendering them + // would offer navigation to conversations the client never receives. + rows: immediateThreadRelationships(graph, props.threadId).filter( + (relationship) => relationship.edge.kind !== "subagent", + ), currentThreadId: props.threadId, mergeTargetThreadId, }), diff --git a/apps/web/src/components/chat/V2ItemInspector.tsx b/apps/web/src/components/chat/V2ItemInspector.tsx index f39e977b29..ceea24eb4e 100644 --- a/apps/web/src/components/chat/V2ItemInspector.tsx +++ b/apps/web/src/components/chat/V2ItemInspector.tsx @@ -265,12 +265,6 @@ export const V2ItemInspector = memo(function V2ItemInspector(props: V2ItemInspec ) : null} - {item.type === "subagent" && item.childThreadId !== null ? ( - - ) : null} - {item.type === "handoff" ? (

diff --git a/apps/web/src/components/chat/V2LifecycleRow.tsx b/apps/web/src/components/chat/V2LifecycleRow.tsx index 079ddc8a62..10ddf665e4 100644 --- a/apps/web/src/components/chat/V2LifecycleRow.tsx +++ b/apps/web/src/components/chat/V2LifecycleRow.tsx @@ -211,7 +211,7 @@ export function V2LifecycleRow(props: { title={subagentDisplayTitle(item.title ?? "Subagent")} detail={detail} badge={item.status} - threadId={item.childThreadId} + threadId={null} expandedDetail={finalResult} onOpenThread={props.onOpenThread} /> diff --git a/apps/web/src/routes/_chat.$environmentId.$threadId.tsx b/apps/web/src/routes/_chat.$environmentId.$threadId.tsx index 271f507396..10c0933818 100644 --- a/apps/web/src/routes/_chat.$environmentId.$threadId.tsx +++ b/apps/web/src/routes/_chat.$environmentId.$threadId.tsx @@ -4,11 +4,16 @@ import { useEffect } from "react"; import ChatView from "../components/ChatView"; import { threadHasStarted } from "../components/ChatView.logic"; import { finalizePromotedDraftThreadByRef, useComposerDraftStore } from "../composerDraftStore"; -import { resolveThreadRouteRef, resolveThreadRouteRenderState } from "../threadRoutes"; +import { + buildThreadRouteParams, + resolveThreadRouteRef, + resolveThreadRouteRenderState, +} from "../threadRoutes"; import { SidebarInset } from "~/components/ui/sidebar"; import { useEnvironmentThreadRefs, useThreadShell } from "../state/entities"; import { useEnvironmentQuery } from "../state/query"; import { environmentShell } from "../state/shell"; +import { isInternalSubagentThread } from "../threadVisibility"; function ChatThreadRouteView() { const navigate = useNavigate(); @@ -42,6 +47,10 @@ function ChatThreadRouteView() { }); const serverThreadStarted = threadHasStarted(serverThreadShell); const environmentHasAnyThreads = environmentHasServerThreads || environmentHasDraftThreads; + const subagentParentThreadId = + serverThreadShell && isInternalSubagentThread(serverThreadShell) + ? serverThreadShell.lineage.parentThreadId + : null; useEffect(() => { if (!threadRef || !bootstrapComplete) { @@ -53,6 +62,24 @@ function ChatThreadRouteView() { } }, [bootstrapComplete, environmentHasAnyThreads, navigate, renderState, threadRef]); + useEffect(() => { + if (!threadRef || !serverThreadShell || !isInternalSubagentThread(serverThreadShell)) { + return; + } + if (subagentParentThreadId === null) { + void navigate({ to: "/", replace: true }); + return; + } + void navigate({ + to: "/$environmentId/$threadId", + params: buildThreadRouteParams({ + environmentId: threadRef.environmentId, + threadId: subagentParentThreadId, + }), + replace: true, + }); + }, [navigate, serverThreadShell, subagentParentThreadId, threadRef]); + useEffect(() => { if (!threadRef || !serverThreadStarted || !draftThread) { return; @@ -60,7 +87,11 @@ function ChatThreadRouteView() { finalizePromotedDraftThreadByRef(threadRef); }, [draftThread, serverThreadStarted, threadRef]); - if (!threadRef || renderState !== "ready") { + if ( + !threadRef || + renderState !== "ready" || + (serverThreadShell !== null && isInternalSubagentThread(serverThreadShell)) + ) { return null; } diff --git a/apps/web/src/state/entities.ts b/apps/web/src/state/entities.ts index 3471499435..8d0b44b624 100644 --- a/apps/web/src/state/entities.ts +++ b/apps/web/src/state/entities.ts @@ -1,9 +1,11 @@ import { useAtomValue } from "@effect/atom-react"; +import { useMemo } from "react"; import type { EnvironmentProject, EnvironmentThread, EnvironmentThreadShell, } from "@t3tools/client-runtime/state/shell"; +import { scopeThreadRef } from "@t3tools/client-runtime/environment"; import { EMPTY_THREAD_HISTORY_META, type EnvironmentThreadStatus, @@ -13,6 +15,7 @@ import type { ScopedProjectRef, ScopedThreadRef, ServerConfig } from "@t3tools/c import type { EnvironmentId, OrchestrationV2ProjectedTurnItem, ThreadId } from "@t3tools/contracts"; import { Atom } from "effect/unstable/reactivity"; import { appAtomRegistry } from "../rpc/atomRegistry"; +import { isUserFacingThread } from "../threadVisibility"; import { environmentProjects } from "./projects"; import { environmentServerConfigsAtom } from "./server"; import { allEnvironmentShellsBootstrappedAtom } from "./shell"; @@ -29,9 +32,6 @@ const EMPTY_PROJECT_ATOM = Atom.make(null).pipe( const EMPTY_PROJECT_REFS_ATOM = Atom.make(EMPTY_PROJECT_REFS).pipe( Atom.withLabel("web-project-refs:empty"), ); -const EMPTY_THREAD_REFS_ATOM = Atom.make(EMPTY_THREAD_REFS).pipe( - Atom.withLabel("web-thread-refs:empty"), -); const EMPTY_THREAD_SHELL_ATOM = Atom.make(null).pipe( Atom.withLabel("web-thread-shell:empty"), ); @@ -70,7 +70,11 @@ export function useProjectRefs(): ReadonlyArray { } export function useThreadRefs(): ReadonlyArray { - return useAtomValue(environmentThreadShells.threadRefsAtom); + const threads = useThreadShells(); + return useMemo( + () => threads.map((thread) => scopeThreadRef(thread.environmentId, thread.id)), + [threads], + ); } export function useEnvironmentProjectRefs( @@ -86,10 +90,17 @@ export function useEnvironmentProjectRefs( export function useEnvironmentThreadRefs( environmentId: EnvironmentId | null, ): ReadonlyArray { - return useAtomValue( - environmentId === null - ? EMPTY_THREAD_REFS_ATOM - : environmentThreadShells.environmentThreadRefsAtom(environmentId), + const threads = useThreadShells(); + return useMemo( + () => + environmentId === null + ? EMPTY_THREAD_REFS + : threads.flatMap((thread) => + thread.environmentId === environmentId + ? [scopeThreadRef(thread.environmentId, thread.id)] + : [], + ), + [environmentId, threads], ); } @@ -102,7 +113,8 @@ export function useServerConfigs(): ReadonlyMap { } export function useThreadShells(): ReadonlyArray { - return useAtomValue(environmentThreadShells.threadShellsAtom); + const threads = useAtomValue(environmentThreadShells.threadShellsAtom); + return useMemo(() => threads.filter(isUserFacingThread), [threads]); } export function useAllEnvironmentShellsBootstrapped(): boolean { @@ -112,7 +124,8 @@ export function useAllEnvironmentShellsBootstrapped(): boolean { export function useThreadShellsForProjectRefs( refs: ReadonlyArray, ): ReadonlyArray { - return useAtomValue(environmentThreadShells.threadShellsForProjectRefsAtom(refs)); + const threads = useAtomValue(environmentThreadShells.threadShellsForProjectRefsAtom(refs)); + return useMemo(() => threads.filter(isUserFacingThread), [threads]); } export function useProject(ref: ScopedProjectRef | null): EnvironmentProject | null { @@ -244,11 +257,17 @@ export function readEnvironmentSupportsVisitedTracking(environmentId: Environmen export function readEnvironmentThreadRefs( environmentId: EnvironmentId, ): ReadonlyArray { - return appAtomRegistry.get(environmentThreadShells.environmentThreadRefsAtom(environmentId)); + return appAtomRegistry + .get(environmentThreadShells.threadShellsAtom) + .filter((thread) => thread.environmentId === environmentId && isUserFacingThread(thread)) + .map((thread) => scopeThreadRef(thread.environmentId, thread.id)); } export function readThreadRefs(): ReadonlyArray { - return appAtomRegistry.get(environmentThreadShells.threadRefsAtom); + return appAtomRegistry + .get(environmentThreadShells.threadShellsAtom) + .filter(isUserFacingThread) + .map((thread) => scopeThreadRef(thread.environmentId, thread.id)); } export function readThreadShells(): ReadonlyArray { @@ -256,9 +275,5 @@ export function readThreadShells(): ReadonlyArray { } export function findThreadRef(threadId: ThreadId): ScopedThreadRef | null { - return ( - appAtomRegistry - .get(environmentThreadShells.threadRefsAtom) - .find((ref) => ref.threadId === threadId) ?? null - ); + return readThreadRefs().find((ref) => ref.threadId === threadId) ?? null; } diff --git a/apps/web/src/threadVisibility.test.ts b/apps/web/src/threadVisibility.test.ts new file mode 100644 index 0000000000..8bfad0a796 --- /dev/null +++ b/apps/web/src/threadVisibility.test.ts @@ -0,0 +1,53 @@ +import { NodeId, RunId, ThreadId } from "@t3tools/contracts"; +import { describe, expect, it } from "vite-plus/test"; + +import { isInternalSubagentThread, isUserFacingThread } from "./threadVisibility"; + +const rootId = ThreadId.make("thread:visibility:root"); +const rootLineage = { + rootThreadId: rootId, + parentThreadId: null, + relationshipToParent: null, +} as const; + +describe("thread visibility", () => { + it("keeps roots and user forks visible", () => { + expect(isUserFacingThread({ lineage: rootLineage, forkedFrom: null })).toBe(true); + expect( + isUserFacingThread({ + lineage: { + rootThreadId: rootId, + parentThreadId: rootId, + relationshipToParent: "fork", + }, + forkedFrom: { + type: "run", + threadId: rootId, + runId: RunId.make("run:visibility:fork"), + }, + }), + ).toBe(true); + }); + + it("hides explicit and node-owned subagent children", () => { + expect( + isInternalSubagentThread({ + lineage: { + rootThreadId: rootId, + parentThreadId: rootId, + relationshipToParent: "subagent", + }, + forkedFrom: null, + }), + ).toBe(true); + expect( + isInternalSubagentThread({ + lineage: rootLineage, + forkedFrom: { + type: "node", + nodeId: NodeId.make("node:visibility:subagent"), + }, + }), + ).toBe(true); + }); +}); diff --git a/apps/web/src/threadVisibility.ts b/apps/web/src/threadVisibility.ts new file mode 100644 index 0000000000..9c71cb4a75 --- /dev/null +++ b/apps/web/src/threadVisibility.ts @@ -0,0 +1,9 @@ +import type { OrchestrationV2ThreadShell } from "@t3tools/contracts"; + +export const isInternalSubagentThread = ( + thread: Pick, +) => thread.lineage.relationshipToParent === "subagent" || thread.forkedFrom?.type === "node"; + +export const isUserFacingThread = ( + thread: Pick, +) => !isInternalSubagentThread(thread);