diff --git a/apps/mobile/src/features/threads/thread-list-v2-items.tsx b/apps/mobile/src/features/threads/thread-list-v2-items.tsx index 7a18e1429934e..8bcf52ba9fe54 100644 --- a/apps/mobile/src/features/threads/thread-list-v2-items.tsx +++ b/apps/mobile/src/features/threads/thread-list-v2-items.tsx @@ -105,6 +105,24 @@ interface StatusLabel { readonly iconTintClassName: string; } const STATUS_LABEL_BY_STATUS: Partial> = { + connecting: { + label: msg("Connecting", { $context: "connection status" }), + icon: "wifi.slash", + className: "text-foreground-muted", + iconTintClassName: "accent-foreground-muted", + }, + reconnecting: { + label: msg("Reconnecting", { $context: "connection status" }), + icon: "wifi.slash", + className: "text-foreground-muted", + iconTintClassName: "accent-foreground-muted", + }, + offline: { + label: msg("Offline", { $context: "connection status" }), + icon: "wifi.slash", + className: "text-foreground-muted", + iconTintClassName: "accent-foreground-muted", + }, approval: { label: msg("Approval", { $context: "thread status, waiting for approval" }), icon: "exclamationmark.shield", diff --git a/apps/mobile/src/features/threads/threadListV2.test.ts b/apps/mobile/src/features/threads/threadListV2.test.ts index 29f6e97b047b4..408bd78d7d2d4 100644 --- a/apps/mobile/src/features/threads/threadListV2.test.ts +++ b/apps/mobile/src/features/threads/threadListV2.test.ts @@ -152,6 +152,74 @@ describe("layoutThreadListV2RowMenuActions", () => { }); describe("resolveThreadListV2Status", () => { + it.each(["preparing", "queued", "starting", "running", "waiting"] as const)( + "qualifies a cached %s runtime while reconnecting", + (status) => { + const thread = presentThreadShell( + environmentId, + makeRawThreadShell({ + status, + activeRunId: RunId.make("run-disconnected"), + latestRunId: RunId.make("run-disconnected"), + }), + ); + expect(resolveThreadListV2Status({ ...thread, connectionPhase: "reconnecting" })).toBe( + "reconnecting", + ); + }, + ); + + it.each(["failed", "completed", "cancelled", "interrupted"] as const)( + "preserves a cached %s outcome while disconnected", + (status) => { + const thread = presentThreadShell( + environmentId, + makeRawThreadShell({ + status, + latestRunId: RunId.make("run-ended"), + latestRunCompletedAt: DateTime.makeUnsafe(NOW), + lastVisitedAt: DateTime.makeUnsafe("2026-06-01T23:59:00.000Z"), + }), + ); + const disconnected = { ...thread, connectionPhase: "reconnecting" as const }; + expect(resolveThreadListV2Status(disconnected)).toBe( + status === "failed" ? "failed" : "ready", + ); + expect(threadHasUnseenCompletion(disconnected)).toBe(threadHasUnseenCompletion(thread)); + }, + ); + + it.each([ + ["connecting", "connecting"], + ["reconnecting", "reconnecting"], + ["offline", "offline"], + ["error", "offline"], + ["unsupported", "offline"], + ["available", "offline"], + ["connected", "working"], + [undefined, "working"], + ] as const)( + "presents cached running work with connection phase %s", + (connectionPhase, expected) => { + const thread = makeThread({ + id: ThreadId.make("disconnected"), + title: "Disconnected", + runtime: { + status: "running", + activeRunId: RunId.make("run-disconnected"), + providerInstanceId: ProviderInstanceId.make("codex"), + providerName: "Codex", + lastError: null, + updatedAt: NOW, + }, + connectionPhase, + }); + expect(resolveThreadListV2Status(thread)).toBe(expected); + expect(resolveThreadListV2Status({ ...thread, hasPendingApprovals: true })).toBe("approval"); + expect(resolveThreadListV2Status({ ...thread, hasPendingUserInput: true })).toBe("input"); + }, + ); + it("presents decoded thread waits as Waiting while preserving requests and older servers", () => { const raw = makeRawThreadShell({ status: "running", @@ -1769,6 +1837,20 @@ describe("Working section beta", () => { expect(off.workingCount).toBe(0); }); + it("preserves pins and Working membership across connection loss and recovery", () => { + const before = build({ workingShelfExpanded: true }); + for (const connectionPhase of ["reconnecting", "offline", "connected"] as const) { + const layout = build({ + workingShelfExpanded: true, + threads: threads.map((thread) => ({ ...thread, connectionPhase })), + }); + expect(ids(layout)).toEqual(ids(before)); + expect(layout.workingCount).toBe(before.workingCount); + expect(layout.items[0]?.thread.id).toBe("pinned-working"); + expect(layout.items[0]?.thread.runtime?.status).toBe("running"); + } + }); + it("keeps an inactive row in Working during the shared grace and returns it on expiry", () => { vi.useFakeTimers(); vi.setSystemTime(new Date(NOW)); diff --git a/apps/mobile/src/features/threads/threadListV2.ts b/apps/mobile/src/features/threads/threadListV2.ts index b8029d3b0b189..f5a2a9cfffbfc 100644 --- a/apps/mobile/src/features/threads/threadListV2.ts +++ b/apps/mobile/src/features/threads/threadListV2.ts @@ -14,7 +14,10 @@ import { type SnoozePreset, } from "@t3tools/client-runtime/state/thread-settled"; import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell"; -import { resolveThreadProviderStack } from "@t3tools/client-runtime/state/models"; +import { + resolveThreadConnectionStatus, + resolveThreadProviderStack, +} from "@t3tools/client-runtime/state/models"; import { threadSearchMatchKey, threadSearchSubject, @@ -69,6 +72,9 @@ export function resolveThreadListV2ProviderDrivers( } export type ThreadListV2Status = + | "connecting" + | "reconnecting" + | "offline" | "approval" | "input" | "request" @@ -280,6 +286,8 @@ export function resolveThreadListV2Status( | "pendingGuestMessageCount" | "runtime" | "blockedOnThreadWait" + | "connectionPhase" + | "pendingBackgroundTasks" >, ): ThreadListV2Status { if (thread.hasPendingApprovals || thread.pendingSubagentRequest === "approval") { @@ -292,6 +300,8 @@ export function resolveThreadListV2Status( if ((thread.pendingGuestMessageCount ?? 0) > 0) { return "request"; } + const connectionStatus = resolveThreadConnectionStatus(thread); + if (connectionStatus !== null) return connectionStatus; if (thread.runtime?.status === "running" && thread.blockedOnThreadWait === true) { return "waiting"; } diff --git a/apps/mobile/src/state/threads.ts b/apps/mobile/src/state/threads.ts index bf2013aef5398..8820d2a3d5a05 100644 --- a/apps/mobile/src/state/threads.ts +++ b/apps/mobile/src/state/threads.ts @@ -24,6 +24,7 @@ import { } from "./pending-thread-creation"; import { serverEnvironment } from "./server"; import { environmentShell } from "./shell"; +import { environmentPresentations } from "./presentation"; import { threadOutboxManager } from "./thread-outbox"; export const threadFamilies = createThreadFamilyAtoms( @@ -44,6 +45,7 @@ export const environmentThreadDetails = createEnvironmentThreadDetailAtoms( ); export const environmentThreadShells = createEnvironmentThreadShellAtoms({ catalogValueAtom: environmentCatalog.catalogValueAtom, + connectionPhaseAtom: environmentPresentations.connectionPhaseAtom, snapshotAtom: threadEnvironment.snapshotAtom, hiddenThreadTagsAtom: serverEnvironment.hiddenThreadTagsAtom, }); @@ -61,6 +63,7 @@ const steadyShells = createSteadyShellAtoms({ export const steadyShellsHoldingAtom = steadyShells.holdingAtom; export const steadyNavigationThreadShellsAtom = createEnvironmentThreadShellAtoms({ catalogValueAtom: environmentCatalog.catalogValueAtom, + connectionPhaseAtom: environmentPresentations.connectionPhaseAtom, snapshotAtom: steadyShells.snapshotAtom, hiddenThreadTagsAtom: serverEnvironment.hiddenThreadTagsAtom, }).navigationThreadShellsAtom; diff --git a/apps/mobile/src/state/use-composer-drafts.test.ts b/apps/mobile/src/state/use-composer-drafts.test.ts index a951c13839ee4..afae04a6a8f76 100644 --- a/apps/mobile/src/state/use-composer-drafts.test.ts +++ b/apps/mobile/src/state/use-composer-drafts.test.ts @@ -398,6 +398,7 @@ function modelReconciliationHarness( const noPendingThreadIdsAtom = Atom.make((): ReadonlySet => new Set()); const shells = createEnvironmentThreadShellAtoms({ catalogValueAtom, + connectionPhaseAtom: () => Atom.make("connected" as const), snapshotAtom: () => snapshotAtom, hiddenThreadTagsAtom: () => Atom.make(null), }); diff --git a/apps/mobile/src/state/use-thread-pr.demand.test.tsx b/apps/mobile/src/state/use-thread-pr.demand.test.tsx index b3e6965531f63..2e77e9a9e4c6d 100644 --- a/apps/mobile/src/state/use-thread-pr.demand.test.tsx +++ b/apps/mobile/src/state/use-thread-pr.demand.test.tsx @@ -60,7 +60,10 @@ vi.mock("../connection/runtime", async () => { vi.mock("./server", async () => { const { Atom } = await import("effect/reactivity"); const config = Atom.make({ environment: { capabilities: { threadPullRequests: true } } }); - return { serverEnvironment: { configValueAtom: () => config } }; + return { + environmentServerConfigsAtom: Atom.make(new Map()), + serverEnvironment: { configValueAtom: () => config }, + }; }); import { ThreadPrDemandProvider } from "./thread-pr-demand"; diff --git a/apps/web/src/components/Sidebar.logic.test.ts b/apps/web/src/components/Sidebar.logic.test.ts index a17427fcdb54e..4fbcc4df5b4a2 100644 --- a/apps/web/src/components/Sidebar.logic.test.ts +++ b/apps/web/src/components/Sidebar.logic.test.ts @@ -481,6 +481,49 @@ describe("resolveThreadStatusPill", () => { }, }; + it.each([ + ["connecting", "connecting", "Connecting"], + ["reconnecting", "reconnecting", "Reconnecting"], + ["offline", "offline", "Offline"], + ["error", "offline", "Offline"], + ["unsupported", "offline", "Offline"], + ["available", "offline", "Offline"], + ["connected", "working", "Working"], + [undefined, "working", "Working"], + ] as const)( + "presents cached running work with connection phase %s", + (connectionPhase, status, label) => { + const thread = { ...baseThread, connectionPhase }; + expect(resolveSidebarThreadStatus(thread)).toBe(status); + expect(resolveThreadStatusPill({ thread })).toMatchObject({ + label, + pulse: status === "working", + }); + expect(thread.runtime.status).toBe("running"); + }, + ); + + it("keeps cached requests and terminal outcomes when the connection is lost", () => { + const thread = { ...baseThread, connectionPhase: "reconnecting" as const }; + expect( + resolveThreadStatusPill({ thread: { ...thread, hasPendingApprovals: true } })?.label, + ).toBe("Pending Approval"); + expect( + resolveThreadStatusPill({ thread: { ...thread, hasPendingUserInput: true } })?.label, + ).toBe("Awaiting Input"); + for (const status of ["failed", "completed", "cancelled", "interrupted", "idle"] as const) { + const online = { + ...baseThread, + runtime: { ...baseThread.runtime, status, activeRunId: null }, + }; + const disconnected = { ...online, connectionPhase: "reconnecting" as const }; + expect(resolveSidebarThreadStatus(disconnected)).toBe(resolveSidebarThreadStatus(online)); + expect(resolveThreadStatusPill({ thread: disconnected })).toEqual( + resolveThreadStatusPill({ thread: online }), + ); + } + }); + it("shows an active thread wait without a pulse and resumes Working when it ends", () => { const blocked = { ...baseThread, blockedOnThreadWait: true }; expect(resolveSidebarThreadStatus(blocked)).toBe("waiting"); diff --git a/apps/web/src/components/Sidebar.logic.ts b/apps/web/src/components/Sidebar.logic.ts index 0771ea2e33d5d..b8ef1505d567d 100644 --- a/apps/web/src/components/Sidebar.logic.ts +++ b/apps/web/src/components/Sidebar.logic.ts @@ -1,6 +1,9 @@ import type { EnvironmentServerConfig } from "@t3tools/client-runtime/connection"; import { scopeThreadRef, scopedThreadKey } from "@t3tools/client-runtime/environment"; -import { resolveThreadWorkingStartedAt } from "@t3tools/client-runtime/state/models"; +import { + resolveThreadConnectionStatus, + resolveThreadWorkingStartedAt, +} from "@t3tools/client-runtime/state/models"; import { isShownUnderParent } from "@t3tools/client-runtime/state/threads"; import { backgroundWorkHoldsCompletion } from "@t3tools/shared/orchestrationV2PendingBackgroundWork"; import { threadPullRequestSearchTerms } from "@t3tools/shared/threadPullRequests"; @@ -659,6 +662,7 @@ type ThreadStatusInput = Pick< | "interactionMode" | "latestRun" | "runtime" + | "connectionPhase" > & { lastVisitedAt?: string | null | undefined; pendingBackgroundTasks?: SidebarThreadSummary["pendingBackgroundTasks"] | undefined; @@ -917,6 +921,9 @@ export function resolveAdjacentThreadId(input: { } export type SidebarThreadStatus = + | "connecting" + | "reconnecting" + | "offline" | "approval" | "input" | "request" @@ -959,7 +966,10 @@ type SidebarThreadStatusInput = Pick< | "pendingGuestMessageCount" | "runtime" | "blockedOnThreadWait" ->; + | "connectionPhase" +> & { + readonly pendingBackgroundTasks?: SidebarThreadSummary["pendingBackgroundTasks"]; +}; export function resolveSidebarThreadStatus(thread: SidebarThreadStatusInput): SidebarThreadStatus { if (thread.hasPendingApprovals || thread.pendingSubagentRequest === "approval") { @@ -972,6 +982,8 @@ export function resolveSidebarThreadStatus(thread: SidebarThreadStatusInput): Si if ((thread.pendingGuestMessageCount ?? 0) > 0) { return "request"; } + const connectionStatus = resolveThreadConnectionStatus(thread); + if (connectionStatus !== null) return connectionStatus; if (thread.runtime?.status === "running" && thread.blockedOnThreadWait === true) { return "waiting"; } @@ -1262,6 +1274,21 @@ export function resolveThreadStatusPill(input: { }; } + const connectionStatus = resolveThreadConnectionStatus(thread); + if (connectionStatus !== null) { + return { + label: + connectionStatus === "connecting" + ? msg("Connecting") + : connectionStatus === "reconnecting" + ? msg("Reconnecting") + : msg("Offline"), + colorClass: "text-sidebar-muted-foreground", + dotClass: "bg-sidebar-muted-foreground", + pulse: false, + }; + } + if ( (thread.runtime?.status === "running" && thread.blockedOnThreadWait !== true) || thread.runtime?.status === "waiting" diff --git a/apps/web/src/components/sidebar/SidebarThreadRow.tsx b/apps/web/src/components/sidebar/SidebarThreadRow.tsx index 2aa9be4c244a6..a549c91c98394 100644 --- a/apps/web/src/components/sidebar/SidebarThreadRow.tsx +++ b/apps/web/src/components/sidebar/SidebarThreadRow.tsx @@ -57,6 +57,7 @@ import { SquarePenIcon, TerminalIcon, Undo2Icon, + UnplugIcon, UserRoundIcon, XIcon, } from "lucide-react"; @@ -565,6 +566,7 @@ const dropVerbBadge: Record = { }; const TOP_STATUS_ICONS = { + connection: UnplugIcon, working: CircleDashedIcon, waiting: CircleDotDashedIcon, input: MessageCircleQuestionIcon, @@ -786,7 +788,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP isActive: props.isActive, isSelected, }); - const topStatus = + const runStatus = status === "preparing" || status === "working" ? { label: @@ -847,6 +849,19 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP className: "text-success-foreground", } : null; + const topStatus = + status === "connecting" || status === "reconnecting" || status === "offline" + ? { + label: + status === "connecting" + ? gt("Connecting") + : status === "reconnecting" + ? gt("Reconnecting") + : gt("Offline"), + icon: "connection" as const, + className: "text-muted-foreground", + } + : runStatus; const isWokeStatus = topStatus?.icon === "woke"; const branchMismatch = resolveLocalCheckoutBranchMismatch({ diff --git a/apps/web/src/composerDraftStore.test.ts b/apps/web/src/composerDraftStore.test.ts index c6942b7e01c88..7e709465d6e25 100644 --- a/apps/web/src/composerDraftStore.test.ts +++ b/apps/web/src/composerDraftStore.test.ts @@ -1386,6 +1386,7 @@ function modelReconciliationHarness( ); const shells = createEnvironmentThreadShellAtoms({ catalogValueAtom, + connectionPhaseAtom: () => Atom.make("connected" as const), snapshotAtom: () => visibleSnapshotAtom, hiddenThreadTagsAtom: () => Atom.make(null), }); diff --git a/apps/web/src/state/threads.ts b/apps/web/src/state/threads.ts index 6d2ce2a07c90b..6faa336b89731 100644 --- a/apps/web/src/state/threads.ts +++ b/apps/web/src/state/threads.ts @@ -20,6 +20,7 @@ import { environmentCatalog } from "../connection/catalog"; import { connectionAtomRuntime } from "../connection/runtime"; import { serverEnvironment } from "./server"; import { environmentShell } from "./shell"; +import { environmentPresentations } from "./presentation"; export const threadFamilies = createThreadFamilyAtoms( connectionAtomRuntime, @@ -39,6 +40,7 @@ export const environmentThreadDetails = createEnvironmentThreadDetailAtoms( ); export const environmentThreadShells = createEnvironmentThreadShellAtoms({ catalogValueAtom: environmentCatalog.catalogValueAtom, + connectionPhaseAtom: environmentPresentations.connectionPhaseAtom, snapshotAtom: threadEnvironment.snapshotAtom, hiddenThreadTagsAtom: serverEnvironment.hiddenThreadTagsAtom, }); diff --git a/packages/client-runtime/src/state/models.ts b/packages/client-runtime/src/state/models.ts index 4b708662839b2..ce3ded3ad8234 100644 --- a/packages/client-runtime/src/state/models.ts +++ b/packages/client-runtime/src/state/models.ts @@ -21,6 +21,7 @@ import { mFallback, msg } from "gt-i18n"; import type { MFunctionType } from "gt-i18n/types"; import { formatSubagentDisplayTitle } from "./subagentDisplay.ts"; +import type { EnvironmentConnectionPhase } from "../connection/presentation.ts"; export interface EnvironmentProject extends OrchestrationProjectShell { readonly environmentId: EnvironmentId; @@ -127,6 +128,34 @@ export function threadRuntimeIsActive(runtime: ThreadStructureRuntime | null | u return runtime !== null && runtime !== undefined && threadRunStatusIsActive(runtime.status); } +export function resolveThreadConnectionStatus( + thread: Pick & { + readonly pendingBackgroundTasks?: EnvironmentThreadShell["pendingBackgroundTasks"] | undefined; + }, +): "connecting" | "reconnecting" | "offline" | null { + if ( + !threadRuntimeIsActive(thread.runtime) && + !( + thread.runtime?.status === "idle" && + backgroundWorkHoldsCompletion(thread.pendingBackgroundTasks ?? []) + ) + ) + return null; + switch (thread.connectionPhase) { + case "connecting": + return "connecting"; + case "reconnecting": + return "reconnecting"; + case "available": + case "offline": + case "error": + case "unsupported": + return "offline"; + default: + return null; + } +} + export function threadRuntimeCanArchive(runtime: ThreadRuntimeSummary | null | undefined): boolean { if (runtime?.status === "queued") return runtime.activeRunId === null; return ( @@ -148,6 +177,7 @@ function threadRunStatusIsActive(status: ThreadRuntimeSummary["status"]): boolea export interface EnvironmentThreadShell { readonly environmentId: EnvironmentId; + readonly connectionPhase?: EnvironmentConnectionPhase | undefined; readonly id: ThreadId; readonly projectId: ProjectId; readonly title: string; diff --git a/packages/client-runtime/src/state/presentation.ts b/packages/client-runtime/src/state/presentation.ts index 7f270081bd4af..5f0f01832c911 100644 --- a/packages/client-runtime/src/state/presentation.ts +++ b/packages/client-runtime/src/state/presentation.ts @@ -119,6 +119,10 @@ export function createEnvironmentPresentationAtoms(input: { }).pipe(Atom.withLabel(`environment-presentation:${environmentId}`)), ); + const connectionPhaseAtom = Atom.family((environmentId: EnvironmentId) => + Atom.make((get) => get(presentationAtom(environmentId))?.connection.phase), + ); + let previous: ReadonlyMap = new Map(); const presentationsAtom = Atom.make((get) => { const next = new Map(); @@ -194,6 +198,7 @@ export function createEnvironmentPresentationAtoms(input: { return { presentationAtom, + connectionPhaseAtom, presentationsAtom, serverConfigsAtom, providersByEnvironmentAtom, diff --git a/packages/client-runtime/src/state/threadShell.test.ts b/packages/client-runtime/src/state/threadShell.test.ts index 9210949afb737..9b91d9509831b 100644 --- a/packages/client-runtime/src/state/threadShell.test.ts +++ b/packages/client-runtime/src/state/threadShell.test.ts @@ -13,6 +13,7 @@ import { Atom, AtomRegistry } from "effect/reactivity"; import { describe, expect, it } from "vite-plus/test"; import { PrimaryConnectionTarget } from "../connection/model.ts"; +import type { EnvironmentConnectionPhase } from "../connection/presentation.ts"; import { v2Now, v2ShellSnapshot, v2ThreadShell } from "./orchestrationV2TestFixtures.ts"; import { applyShellStreamEvent } from "./shellReducer.ts"; import { isThreadWorking } from "./threadInbox.ts"; @@ -27,6 +28,9 @@ const remoteEnvironmentId = EnvironmentId.make("remote-environment-v2"); const otherProjectId = ProjectId.make("other-project"); function makeHarness(environmentIds: ReadonlyArray = [environmentId]) { + const connectionPhaseAtom = Atom.family((_environmentId: EnvironmentId) => + Atom.make("connected"), + ); const snapshotAtom = Atom.family((_environmentId: EnvironmentId) => Atom.make(v2ShellSnapshot), ); @@ -52,15 +56,67 @@ function makeHarness(environmentIds: ReadonlyArray = [environment registry: AtomRegistry.make(), snapshotAtom, catalogValueAtom, + connectionPhaseAtom, threads: createEnvironmentThreadShellAtoms({ catalogValueAtom, snapshotAtom, + connectionPhaseAtom, hiddenThreadTagsAtom: () => Atom.make(null), }), }; } describe("v2 thread shell lists", () => { + it("updates cached point and list reads on connection loss and recovery without changing runtime", () => { + const { registry, threads, snapshotAtom, connectionPhaseAtom } = makeHarness([ + environmentId, + remoteEnvironmentId, + ]); + const raw = { + ...v2ThreadShell, + status: "running" as const, + activeRunId: RunId.make("run-disconnected"), + latestRunId: RunId.make("run-disconnected"), + pinnedAt: v2Now, + }; + registry.set(snapshotAtom(environmentId), { ...v2ShellSnapshot, threads: [raw] }); + const ref = { environmentId, threadId: raw.id }; + const reads = Atom.make((get) => + [ + get(threads.threadShellAtom(ref)), + get(threads.quietThreadShellAtom(ref)), + ...get(threads.threadShellsAtom), + ...get(threads.quietThreadShellsAtom), + ...get(threads.navigationThreadShellsAtom), + ...get( + threads.threadShellsForProjectRefsAtom([{ environmentId, projectId: raw.projectId }]), + ), + ].filter((thread) => thread?.environmentId === environmentId), + ); + const dispose = registry.mount(reads); + const before = registry.get(threads.threadShellAtom(ref)); + const other = registry.get( + threads.threadShellAtom({ ...ref, environmentId: remoteEnvironmentId }), + ); + for (const phase of ["reconnecting", "error", "offline", "connecting", "connected"] as const) { + registry.set(connectionPhaseAtom(environmentId), phase); + expect(registry.get(reads)).toHaveLength(6); + for (const thread of registry.get(reads)) { + expect(thread).toMatchObject({ + connectionPhase: phase, + pinnedAt: DateTime.formatIso(v2Now), + }); + expect(thread?.runtime).toEqual(before?.runtime); + expect(thread?.source).toBe(raw); + } + expect( + registry.get(threads.threadShellAtom({ ...ref, environmentId: remoteEnvironmentId })), + ).toBe(other); + } + dispose(); + registry.dispose(); + }); + it("builds no child index while no child asks for input", () => { const { registry, threads, snapshotAtom } = makeHarness(); let parentIdReads = 0; diff --git a/packages/client-runtime/src/state/threadShell.ts b/packages/client-runtime/src/state/threadShell.ts index 3325346d5d153..6aaf9ba713112 100644 --- a/packages/client-runtime/src/state/threadShell.ts +++ b/packages/client-runtime/src/state/threadShell.ts @@ -20,6 +20,7 @@ import type { EnvironmentThreadShell, PendingSubagentRequest } from "./models.ts import { presentThreadShell, threadRuntimeIsActive } from "./models.ts"; import { hiddenThreadKeys, NO_HIDDEN_THREADS } from "./threadTagFilter.ts"; import { type EnvironmentCatalogState, enabledEnvironmentIds } from "./connections.ts"; +import type { EnvironmentConnectionPhase } from "../connection/presentation.ts"; import { arrayElementsEqual, parseProjectRefCollectionKey, @@ -241,6 +242,7 @@ function sameApartFromStreaming( ): boolean { if ( previous.environmentId !== next.environmentId || + previous.connectionPhase !== next.connectionPhase || previous.pendingSubagentRequest !== next.pendingSubagentRequest || !threadRuntimeIsActive(next.runtime) ) { @@ -266,6 +268,9 @@ function sameApartFromStreaming( export function createEnvironmentThreadShellAtoms(input: { readonly catalogValueAtom: Atom.Atom; + readonly connectionPhaseAtom: ( + environmentId: EnvironmentId, + ) => Atom.Atom; readonly snapshotAtom: ( environmentId: EnvironmentId, ) => Atom.Atom; @@ -284,6 +289,7 @@ export function createEnvironmentThreadShellAtoms(input: { environmentId: EnvironmentId, thread: OrchestrationV2ThreadShell, subagentRequests: ReadonlyMap, + connectionPhase: EnvironmentConnectionPhase | undefined, ) => { let byEnvironment = scopedThreads.get(thread); if (byEnvironment === undefined) { @@ -291,10 +297,10 @@ export function createEnvironmentThreadShellAtoms(input: { scopedThreads.set(thread, byEnvironment); } const request = subagentRequests.get(thread.id) ?? null; - const key = request === null ? environmentId : `${environmentId}:${request}`; + const key = `${environmentId}:${request ?? ""}:${connectionPhase ?? ""}`; let value = byEnvironment.get(key); if (value === undefined) { - value = presentThreadShell(environmentId, thread, request); + value = { ...presentThreadShell(environmentId, thread, request), connectionPhase }; byEnvironment.set(key, value); } return value; @@ -480,6 +486,7 @@ export function createEnvironmentThreadShellAtoms(input: { ref.environmentId, source, get(pendingSubagentRequestsAtom(ref.environmentId)), + get(input.connectionPhaseAtom(ref.environmentId)), ); }).pipe(Atom.withLabel(`environment-thread-shell:${key}`)); }); @@ -553,7 +560,12 @@ export function createEnvironmentThreadShellAtoms(input: { pending.push(childKey); if (asksParent(thread)) { next.push( - scopedThread(environmentId, thread, get(pendingSubagentRequestsAtom(environmentId))), + scopedThread( + environmentId, + thread, + get(pendingSubagentRequestsAtom(environmentId)), + get(input.connectionPhaseAtom(environmentId)), + ), ); } } @@ -590,7 +602,12 @@ export function createEnvironmentThreadShellAtoms(input: { } const subagentRequests = get(pendingSubagentRequestsAtom(ref.environmentId)); const next = sideChats.map((thread) => - scopedThread(ref.environmentId, thread, subagentRequests), + scopedThread( + ref.environmentId, + thread, + subagentRequests, + get(input.connectionPhaseAtom(ref.environmentId)), + ), ); if ( previous.length === next.length && @@ -624,6 +641,7 @@ export function createEnvironmentThreadShellAtoms(input: { if (refs.length === 0) continue; const threads = get(environmentThreadIndexAtom(projectRef.environmentId)); const subagentRequests = get(pendingSubagentRequestsAtom(projectRef.environmentId)); + const connectionPhase = get(input.connectionPhaseAtom(projectRef.environmentId)); for (const ref of refs) { const key = threadKey(ref); if (seen.has(key)) { @@ -632,7 +650,7 @@ export function createEnvironmentThreadShellAtoms(input: { seen.add(key); const thread = threads.get(ref.threadId); if (thread !== undefined) { - next.push(scopedThread(ref.environmentId, thread, subagentRequests)); + next.push(scopedThread(ref.environmentId, thread, subagentRequests, connectionPhase)); } } } @@ -662,8 +680,9 @@ export function createEnvironmentThreadShellAtoms(input: { const next: EnvironmentThreadShell[] = []; for (const environmentId of enabledEnvironmentIds(get(input.catalogValueAtom))) { const subagentRequests = get(pendingSubagentRequestsAtom(environmentId)); + const connectionPhase = get(input.connectionPhaseAtom(environmentId)); for (const thread of get(environmentThreadsAtom(environmentId))) { - next.push(scopedThread(environmentId, thread, subagentRequests)); + next.push(scopedThread(environmentId, thread, subagentRequests, connectionPhase)); } } if (arrayElementsEqual(previousThreadShells, next)) { @@ -777,6 +796,7 @@ export function createEnvironmentThreadShellAtoms(input: { const hidden = get(hiddenThreadKeysAtom); for (const environmentId of enabledEnvironmentIds(get(input.catalogValueAtom))) { const subagentRequests = get(pendingSubagentRequestsAtom(environmentId)); + const connectionPhase = get(input.connectionPhaseAtom(environmentId)); for (const thread of get(environmentThreadsAtom(environmentId))) { if ( thread.archivedAt !== null || @@ -785,7 +805,7 @@ export function createEnvironmentThreadShellAtoms(input: { ) { continue; } - next.push(scopedThread(environmentId, thread, subagentRequests)); + next.push(scopedThread(environmentId, thread, subagentRequests, connectionPhase)); } } if (arrayElementsEqual(previousNavigationShells, next)) return previousNavigationShells;