TypeScript
fix(web): keep the parent thread highlighted while viewing its subagent (#15163)
Alex Southwella7b5d52c789c
diff --git a/apps/web/src/components/Sidebar.logic.test.ts b/apps/web/src/components/Sidebar.logic.test.tsindex 4883c690bb3..de40918e409 100644--- a/apps/web/src/components/Sidebar.logic.test.ts+++ b/apps/web/src/components/Sidebar.logic.test.ts@@ -23,6 +23,7 @@ import {hasUnseenCompletion,isContextMenuPointerDown,isSidebarSubagentThread,+ resolveSidebarRowThreadId,isSidebarThreadWorking,isTrailingDoubleClick,orderItemsByPreferredIds,@@ -461,6 +462,46 @@ describe("sidebar thread lineage helpers", () => {expect(isSidebarSubagentThread(makeThreadFixture())).toBe(false);});+ it("maps an open subagent to its nearest visible ancestor's sidebar row", () => {+ const environmentId = EnvironmentId.make("environment-row");+ const otherEnvironmentId = EnvironmentId.make("environment-other");+ const subagentOf = (id: string, parentId: ThreadId, environment = environmentId) =>+ makeThreadFixture({+ id: ThreadId.make(id),+ environmentId: environment,+ lineage: {+ rootThreadId: parentId,+ parentThreadId: parentId,+ relationshipToParent: "subagent",+ },+ });+ const root = makeThreadFixture({ id: ThreadId.make("thread-root"), environmentId });+ const fork = makeThreadFixture({+ id: ThreadId.make("thread-fork"),+ environmentId,+ lineage: {+ rootThreadId: root.id,+ parentThreadId: root.id,+ relationshipToParent: "fork",+ },+ });+ const child = subagentOf("thread-child", root.id);+ const grandchild = subagentOf("thread-grandchild", child.id);+ const forkChild = subagentOf("thread-fork-child", fork.id);+ const orphan = subagentOf("thread-orphan", ThreadId.make("thread-missing"));+ const elsewhere = subagentOf("thread-elsewhere", root.id, otherEnvironmentId);+ const threads = [root, fork, child, grandchild, forkChild, orphan, elsewhere];++ expect(resolveSidebarRowThreadId(child.id, environmentId, threads)).toBe(root.id);+ expect(resolveSidebarRowThreadId(grandchild.id, environmentId, threads)).toBe(root.id);+ expect(resolveSidebarRowThreadId(forkChild.id, environmentId, threads)).toBe(fork.id);+ expect(resolveSidebarRowThreadId(elsewhere.id, otherEnvironmentId, threads)).toBe(elsewhere.id);+ expect(resolveSidebarRowThreadId(orphan.id, environmentId, threads)).toBe(orphan.id);+ const loopA = subagentOf("thread-loop-a", ThreadId.make("thread-loop-b"));+ const loopB = subagentOf("thread-loop-b", loopA.id);+ expect(resolveSidebarRowThreadId(loopA.id, environmentId, [loopA, loopB])).toBe(loopA.id);+ });+it("resolves the parent thread for fork sidebar affordances", () => {const parentId = ThreadId.make("thread-parent");const fallbackParentId = ThreadId.make("thread-fallback-parent");diff --git a/apps/web/src/components/Sidebar.logic.ts b/apps/web/src/components/Sidebar.logic.tsindex f10c58fa0de..d0bd3b95838 100644--- a/apps/web/src/components/Sidebar.logic.ts+++ b/apps/web/src/components/Sidebar.logic.ts@@ -517,6 +517,26 @@ export function isSidebarSubagentThread(thread: Pick<SidebarThreadSummary, "linereturn thread.lineage.relationshipToParent === "subagent";}+// Subagent threads have no sidebar row, so an open subagent is represented by+// its nearest non-subagent ancestor. A broken chain (missing parent, cycle,+// other environment) resolves to the thread itself, which then matches no row.+export function resolveSidebarRowThreadId(+ threadId: ThreadId,+ environmentId: EnvironmentId,+ threads: readonly Pick<SidebarThreadSummary, "id" | "environmentId" | "lineage">[],+): ThreadId {+ const findThread = (id: ThreadId) =>+ threads.find((thread) => thread.id === id && thread.environmentId === environmentId);+ const visited = new Set<ThreadId>();+ let thread = findThread(threadId);+ while (thread !== undefined && isSidebarSubagentThread(thread)) {+ if (thread.lineage.parentThreadId === null || visited.has(thread.id)) return threadId;+ visited.add(thread.id);+ thread = findThread(thread.lineage.parentThreadId);+ }+ return thread?.id ?? threadId;+}+export function filterSidebarV2VisibleThreads<T extends Pick<SidebarThreadSummary, "archivedAt" | "lineage"> & {environmentId: string;diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsxindex ce6a9d8e88e..454f8f45f68 100644--- a/apps/web/src/components/Sidebar.tsx+++ b/apps/web/src/components/Sidebar.tsx@@ -231,6 +231,7 @@ import {type SidebarListItem,type SidebarListMarker,type SidebarSection,+ resolveSidebarRowThreadId,} from "./Sidebar.logic";import { resolveLocalCheckoutBranchMismatch } from "./BranchToolbar.logic";import {@@ -449,11 +450,24 @@ function useSidebarThreads(input: {readonly scopedProjectKeys: ReadonlySet<string> | null;readonly serverConfigs: ReadonlyMap<EnvironmentId, EnvironmentServerConfig>;readonly optimisticDrop: SidebarOptimisticDrop | null;- readonly routeThreadKey: string | null;+ readonly routeThreadRef: ScopedThreadRef | null;}) {- const { projectScopeKey, scopedProjectKeys, serverConfigs, optimisticDrop, routeThreadKey } =+ const { projectScopeKey, scopedProjectKeys, serverConfigs, optimisticDrop, routeThreadRef } =input;const threads = useThreadShells();+ const routeRowThreadKey =+ routeThreadRef === null+ ? null+ : scopedThreadKey(+ scopeThreadRef(+ routeThreadRef.environmentId,+ resolveSidebarRowThreadId(+ routeThreadRef.threadId,+ routeThreadRef.environmentId,+ threads,+ ),+ ),+ );const workingShelfEnabled = useClientSettings((s) => s.sidebarWorkingShelfEnabled);const nowMinute = useNowMinute();// Snooze wake times are second-precise, so classifying with the quantized@@ -506,23 +520,23 @@ function useSidebarThreads(input: {const visibleSettledThreads = (() => {if (settledThreads.length <= settledVisibleCount) return settledThreads;const visible = settledThreads.slice(0, settledVisibleCount);- if (routeThreadKey !== null) {+ if (routeRowThreadKey !== null) {const routeThread = settledThreads.slice(settledVisibleCount)- .find((thread) => threadShellKey(thread) === routeThreadKey);+ .find((thread) => threadShellKey(thread) === routeRowThreadKey);if (routeThread !== undefined) visible.push(routeThread);}return visible;})();const renderedSettledThreads = settledShelfExpanded? visibleSettledThreads- : routeThreadOnly(visibleSettledThreads, routeThreadKey);+ : routeThreadOnly(visibleSettledThreads, routeRowThreadKey);const visibleSnoozedThreads = snoozedShelfExpanded? snoozedThreads- : routeThreadOnly(snoozedThreads, routeThreadKey);+ : routeThreadOnly(snoozedThreads, routeRowThreadKey);const visibleWorkingThreads = workingShelfExpanded? workingThreads- : routeThreadOnly(workingThreads, routeThreadKey);+ : routeThreadOnly(workingThreads, routeRowThreadKey);const orderedThreads = [...pinnedThreads,...activeThreads,@@ -541,6 +555,7 @@ function useSidebarThreads(input: {for (const thread of list) sectionByThreadKey.set(threadShellKey(thread), section);}return {+ routeRowThreadKey,...partition,threads,workingShelfEnabled,@@ -1542,6 +1557,9 @@ type SidebarThreadRowProps = {snoozeWakeLabelText: string | null;wokeAt: string | null;isActive: boolean;+ // Unlike isActive, never true for the ancestor standing in for an open+ // subagent: drafts and PR clicks care about which composer is on screen.+ isRouteThread: boolean;openPullRequestsInRightPanel: boolean;jumpLabel: string | null;currentEnvironmentId: string | null;@@ -1617,7 +1635,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP});const terminalStatus = terminalStatusFromRunningIds(runningTerminalIds);const terminalProcessCount = runningTerminalIds.length;- const hasUnsentDraft = useThreadHasUnsentDraft(threadRef) && !props.isActive;+ const hasUnsentDraft = useThreadHasUnsentDraft(threadRef) && !props.isRouteThread;const handleDiscardDraftClick = (event: ReactMouseEvent) => {event.preventDefault();event.stopPropagation();@@ -1895,7 +1913,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowPif (!url) return;const showInRightPanel = targetUrl !== undefined || openPullRequestsInRightPanel;const openedInRightPanel = openPrLink(event, url, showInRightPanel ? threadRef : undefined);- if (openedInRightPanel && showInRightPanel && !props.isActive) {+ if (openedInRightPanel && showInRightPanel && !props.isRouteThread) {onThreadActivate(threadRef);}};@@ -2013,7 +2031,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP: null;const handlePrListClick = () => {useRightPanelStore.getState().open(threadRef, "pull-requests");- if (!props.isActive) onThreadActivate(threadRef);+ if (!props.isRouteThread) onThreadActivate(threadRef);};const prBadge =prBadgeShape?.kind === "stack" || pr || currentLinkedPr ? (@@ -2999,12 +3017,13 @@ export default function Sidebar() {activeKeys,pinnedKeysById,activeKeysById,+ routeRowThreadKey,} = useSidebarThreads({projectScopeKey,scopedProjectKeys,serverConfigs,optimisticDrop,- routeThreadKey,+ routeThreadRef,});const isSearchingThreads = threadSearchQuery.trim().length > 0;@@ -3637,7 +3656,7 @@ export default function Sidebar() {...(draggedActiveOrder === undefined ? {} : { activeOrder: draggedActiveOrder }),settledExpanded: settledShelfExpanded,settledVisibleCount,- routeThreadKey,+ routeThreadKey: routeRowThreadKey,snoozedThreadCount: snoozedThreads.length,});const draggedThreadKey = dragState?.activeKey;@@ -4472,7 +4491,7 @@ export default function Sidebar() {navigateToThreadKey(resolveAdjacentThreadId({threadIds: orderedThreadKeys,- currentThreadId: routeThreadKey,+ currentThreadId: routeRowThreadKey,direction: traversalDirection,}),);@@ -4488,8 +4507,8 @@ export default function Sidebar() {keybindings,navigateToThread,orderedThreadKeys,+ routeRowThreadKey,routeTerminalOpen,- routeThreadKey,threadByKey,]);@@ -4727,7 +4746,7 @@ export default function Sidebar() {EMPTY_PROVIDER_ENTRIES}isHighlighted={activeSearchResultIndex === index}- isRouteActive={routeThreadKey === threadKey}+ isRouteActive={routeRowThreadKey === threadKey}resultId={`sidebar-thread-search-result-${index}`}searchMatch={threadSearchMatchByKey.get(@@ -4848,7 +4867,8 @@ export default function Sidebar() {: null}wokeAt={threadWokeAt(thread, { now: snoozeNow })}- isActive={routeThreadKey === threadKey}+ isActive={routeRowThreadKey === threadKey}+ isRouteThread={routeThreadKey === threadKey}openPullRequestsInRightPanel={routeThreadRef !== null}jumpLabel={showJumpHints ? (jumpLabelByKey.get(threadKey) ?? null) : null