Skip to content
6 changes: 2 additions & 4 deletions apps/mobile/src/state/linkedThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -23,10 +24,7 @@ export function useLinkedThreadReach(
subagents: ReadonlyArray<Pick<OrchestrationV2Subagent, "childThreadId" | "remoteChild">>,
) {
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) =>
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/components/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 6 additions & 4 deletions apps/web/src/components/chat/ThreadRelationshipsControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}}
/>
Expand Down Expand Up @@ -445,15 +447,15 @@ function DelegatedFromRow(props: {
<LineageTooltipContent
title={title}
hint={
environment === null
openable === null
? label
? gt("Delegated from {label}. Connect to it to open the parent thread.", {
label,
})
: gt(
"Delegated from a linked environment. Connect to it to open the parent thread.",
)
: gt("Open parent agent on {label}", { label: environment.label })
: gt("Open parent agent on {label}", { label: openable.label })
}
/>
</TooltipPopup>
Expand Down
6 changes: 4 additions & 2 deletions apps/web/src/state/linkedThreadView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down
23 changes: 23 additions & 0 deletions packages/client-runtime/src/linkedThread.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
keepLinkedThreadView,
LINKED_THREAD_RETRY_MS,
type LinkedThreadView,
ownEnvironmentConnection,
resolveLinkedThreadOpen,
} from "./linkedThread.ts";
import { v2Now, v2Projection } from "./state/orchestrationV2TestFixtures.ts";

Expand Down Expand Up @@ -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";
Expand Down
18 changes: 18 additions & 0 deletions packages/client-runtime/src/linkedThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
Loading