diff --git a/apps/mobile/src/state/linkedThread.ts b/apps/mobile/src/state/linkedThread.ts index ff4c75b9ac1ac..653224d10dd76 100644 --- a/apps/mobile/src/state/linkedThread.ts +++ b/apps/mobile/src/state/linkedThread.ts @@ -2,6 +2,7 @@ import { useAtomSet, useAtomValue } from "@effect/atom-react"; import { connectLinkedEnvironment, type LinkedThreadTarget, + ownEnvironmentConnection, } from "@t3tools/client-runtime/linked-thread"; import type { EnvironmentId, OrchestrationV2Subagent } from "@t3tools/contracts"; import { useState } from "react"; @@ -23,10 +24,7 @@ export function useLinkedThreadReach( subagents: ReadonlyArray>, ) { const { presentationById } = useEnvironments(); - const ownConnection = (id: EnvironmentId) => { - const own = presentationById.get(id); - return own && { connected: own.connection.phase === "connected" }; - }; + const ownConnection = (id: EnvironmentId) => ownEnvironmentConnection(presentationById.get(id)); const links = useLinkedEnvironments( subagents.some( (subagent) => diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 834584ba7a7f8..e48d4bee22eaf 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -970,8 +970,8 @@ export default function ChatView(props: ChatViewProps) { return { title: delegatedFromThread?.title ?? delegatedFrom.title, environmentLabel: delegatedFromEnvironment?.label ?? activeThread?.linkOrigin?.label ?? "", - // Only an environment this client is connected to can open the parent. - ...(delegatedFromEnvironment === null + // Only an environment enabled in this client can open the parent. + ...(delegatedFromEnvironment?.entry.enabled !== true ? {} : { open: (): void => { diff --git a/apps/web/src/components/chat/ThreadRelationshipsControl.remote-parent.browser.test.tsx b/apps/web/src/components/chat/ThreadRelationshipsControl.remote-parent.browser.test.tsx index 3e32be81a5000..352edb804017f 100644 --- a/apps/web/src/components/chat/ThreadRelationshipsControl.remote-parent.browser.test.tsx +++ b/apps/web/src/components/chat/ThreadRelationshipsControl.remote-parent.browser.test.tsx @@ -52,7 +52,11 @@ vi.mock(import("../../state/entities"), async (original) => ({ })); vi.mock(import("../../state/environments"), async (original) => ({ ...(await original()), - useEnvironment: (() => ({ label: "Linked" })) as never, + useEnvironment: (() => ({ + label: "Linked", + entry: { enabled: true }, + connection: { phase: "connected" }, + })) as never, })); vi.mock(import("../ui/toast"), async (original) => { const actual = await original(); diff --git a/apps/web/src/components/chat/ThreadRelationshipsControl.tsx b/apps/web/src/components/chat/ThreadRelationshipsControl.tsx index 9675a21620d32..0af7495a23df5 100644 --- a/apps/web/src/components/chat/ThreadRelationshipsControl.tsx +++ b/apps/web/src/components/chat/ThreadRelationshipsControl.tsx @@ -371,6 +371,8 @@ function DelegatedFromRow(props: { const router = useRouter(); const { delegatedFrom } = props; const environment = useEnvironment(delegatedFrom.environmentId); + // A saved environment that is switched off can't open its threads. + const openable = environment?.entry.enabled === true ? environment : null; const parentRef = scopeThreadRef(delegatedFrom.environmentId, delegatedFrom.threadId); // The title it had at delegation, until this client sees the parent itself. const parent = useQuietThreadShell(parentRef); @@ -415,9 +417,9 @@ function DelegatedFromRow(props: { size="sm" variant="ghost" part="row" - aria-disabled={environment === null || undefined} + aria-disabled={openable === null || undefined} onClick={() => { - if (environment === null) return; + if (openable === null) return; void openParent(); }} /> @@ -445,7 +447,7 @@ function DelegatedFromRow(props: { diff --git a/apps/web/src/state/linkedThreadView.ts b/apps/web/src/state/linkedThreadView.ts index adeb858c23cab..4a48fd5cc0322 100644 --- a/apps/web/src/state/linkedThreadView.ts +++ b/apps/web/src/state/linkedThreadView.ts @@ -4,6 +4,7 @@ import type { SideThreadOpenIntent } from "@t3tools/client-runtime/side-thread"; import { connectLinkedEnvironment, type LinkedThreadOpen, + ownEnvironmentConnection, resolveLinkedThreadOpen, } from "@t3tools/client-runtime/linked-thread"; import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts"; @@ -43,13 +44,14 @@ export function useLinkedThreadOpener( const navigate = useNavigate(); const viaEnvironmentId = panelRef.environmentId; const own = useEnvironment(thread?.environmentId ?? null); - const connected = own?.connection.phase === "connected"; + const ownConnection = ownEnvironmentConnection(own); + const connected = ownConnection?.connected === true; const links = useLinkedEnvironments(thread === undefined || connected ? null : viaEnvironmentId); if (thread === undefined) return null; const resolved = resolveLinkedThreadOpen({ environmentId: thread.environmentId, viaEnvironmentId, - own: own === null ? undefined : { connected }, + own: ownConnection, linked: links.has(thread.environmentId), }); const mode = resolved === "direct" ? "direct" : "linked"; diff --git a/packages/client-runtime/src/linkedThread.test.ts b/packages/client-runtime/src/linkedThread.test.ts index 8b2d4e105e5b1..fc1917fdd2944 100644 --- a/packages/client-runtime/src/linkedThread.test.ts +++ b/packages/client-runtime/src/linkedThread.test.ts @@ -19,6 +19,8 @@ import { keepLinkedThreadView, LINKED_THREAD_RETRY_MS, type LinkedThreadView, + ownEnvironmentConnection, + resolveLinkedThreadOpen, } from "./linkedThread.ts"; import { v2Now, v2Projection } from "./state/orchestrationV2TestFixtures.ts"; @@ -163,6 +165,27 @@ it.effect("loads the turns before a snapshot's window once per request", () => }).pipe(Effect.scoped), ); +describe("resolveLinkedThreadOpen", () => { + const here = EnvironmentId.make("env-here"); + const laptop = EnvironmentId.make("env-laptop"); + const open = (enabled: boolean, linked: boolean) => + resolveLinkedThreadOpen({ + environmentId: laptop, + viaEnvironmentId: here, + own: ownEnvironmentConnection({ + entry: { enabled }, + connection: { phase: enabled ? "connected" : "disconnected" }, + }), + linked, + }); + + it("never opens a thread in a saved environment that is switched off", () => { + expect(open(true, true)).toBe("direct"); + expect(open(false, true)).toBe("linked"); + expect(open(false, false)).toBe("unavailable"); + }); +}); + describe("connectLinkedEnvironment", () => { const lan = "http://192.168.1.20:3773"; const tailnet = "https://vm.tail.ts.net"; diff --git a/packages/client-runtime/src/linkedThread.ts b/packages/client-runtime/src/linkedThread.ts index 15364cddf54ea..82bf8c3cc5138 100644 --- a/packages/client-runtime/src/linkedThread.ts +++ b/packages/client-runtime/src/linkedThread.ts @@ -57,6 +57,24 @@ export function resolveLinkedThreadOpen(input: { return input.own === undefined ? "unavailable" : "direct"; } +/** + * The client's own connection to a saved environment, as `resolveLinkedThreadOpen` takes it. One + * switched off in settings can't open its threads, so it counts as unsaved and only a link reads them. + */ +export function ownEnvironmentConnection( + saved: + | { + readonly entry: { readonly enabled: boolean }; + readonly connection: { readonly phase: string }; + } + | null + | undefined, +): { readonly connected: boolean } | undefined { + return saved?.entry.enabled === true + ? { connected: saved.connection.phase === "connected" } + : undefined; +} + export type LinkedEnvironmentConnection = /** This client paired with the linked environment and saved it. */ | { readonly type: "connected" }