Skip to content
TypeScript

fix(composer): don't send attached threads to a machine that can't read them (#18310)

Nikita Maximilian Grimmc1377176b110
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx
index 425007855ba..0e1b1d30a7e 100644
--- a/apps/web/src/components/ChatView.tsx
+++ b/apps/web/src/components/ChatView.tsx
@@ -339,6 +339,7 @@ import {
type TerminalContextSelection,
} from "../lib/terminalContext";
import {
+ collectInlineContextIds,
ensureInlineContextReferences,
removeInlineContextReference,
} from "../lib/composerContextReferences";
@@ -559,7 +560,10 @@ import {
supportsServerUpdateThreadContinuation,
} from "../versionSkew";
import { useAssetUrls } from "../assets/assetUrls";
-import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences";
+import {
+ ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,
+ threadContextsOutsideEnvironment,
+} from "@t3tools/shared/composerContextReferences";
import { recallableComposerPrompt } from "./chat/composerPromptHistory";
import { observeResize } from "~/lib/observeResize";
@@ -1226,6 +1230,15 @@ export default function ChatView(props: ChatViewProps) {
const draft = store.getComposerDraft(composerDraftTarget);
return (draft?.images.length ?? 0) > 0 || (draft?.files.length ?? 0) > 0;
});
+ const referencedThreadContexts = useComposerDraftStore(
+ useShallow((store) => {
+ const draft = store.getComposerDraft(composerDraftTarget);
+ if (!draft?.threadContexts.length) return [];
+ // Linked prompts can replace text without pruning stored context records.
+ const referenced = new Set(collectInlineContextIds(draft.prompt));
+ return draft.threadContexts.filter((record) => referenced.has(record.contextId));
+ }),
+ );
const composerHasNonPromptContent = useComposerDraftStore((store) => {
const draft = store.getComposerDraft(composerDraftTarget);
return draft ? composerDraftHasUserContent({ ...draft, prompt: "" }) : false;
@@ -3128,6 +3141,9 @@ export default function ChatView(props: ChatViewProps) {
return (
environment?.connection.phase === "connected" &&
(loadBalancingSettings.loadBalancingWeights[candidate.environmentId] ?? 50) > 0 &&
+ referencedThreadContexts.every(
+ (record) => record.environmentId === candidate.environmentId,
+ ) &&
providersByEnvironment
.get(candidate.environmentId)
?.some(
@@ -6037,7 +6053,14 @@ export default function ChatView(props: ChatViewProps) {
: "Choose a machine to continue",
description: loadBalancing.pending
? "Resource checks are still running. You can choose a machine in the composer."
- : "No eligible machine has available resources. Choose a machine in the composer to override.",
+ : referencedThreadContexts.length > 0 &&
+ !logicalProjectEnvironments.some((candidate) =>
+ referencedThreadContexts.every(
+ (record) => record.environmentId === candidate.environmentId,
+ ),
+ )
+ ? "No machine can read every attached thread. Choose a machine in the composer or remove the threads."
+ : "No eligible machine has available resources. Choose a machine in the composer to override.",
});
return;
}
@@ -6122,6 +6145,22 @@ export default function ChatView(props: ChatViewProps) {
previewAnnotationContextReference(directAnnotation.annotation),
])
: promptRef.current;
+ const strandedThread = threadContextsOutsideEnvironment({
+ text: promptForSend,
+ records: composerThreadContexts,
+ environmentId,
+ })[0];
+ if (strandedThread) {
+ const owner = environmentById.get(strandedThread.environmentId)?.label ?? "its machine";
+ toastManager.add(
+ stackedThreadToast({
+ type: "warning",
+ title: "Attached thread is on another machine",
+ description: `This machine's agent can't read "${strandedThread.title}". Remove it, or switch back to ${owner} to send.`,
+ }),
+ );
+ return;
+ }
if (queuedRunEdit.isEditing) {
// Edit mode repurposes the composer: sending saves the queued message
// in place instead of dispatching a new turn.
diff --git a/apps/web/src/components/ThreadContextChip.tsx b/apps/web/src/components/ThreadContextChip.tsx
index fb9d020a570..f120cbde298 100644
--- a/apps/web/src/components/ThreadContextChip.tsx
+++ b/apps/web/src/components/ThreadContextChip.tsx
@@ -9,6 +9,7 @@ import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
export function ThreadContextChip(props: {
record: Pick<ThreadContextRecord, "environmentId" | "threadId" | "title">;
copyMarkdown?: string;
+ unreadable?: boolean;
}) {
const { environmentId, threadId } = props.record;
const shell = useThreadShell({ environmentId, threadId });
@@ -19,6 +20,7 @@ export function ThreadContextChip(props: {
render={
<ContextChip
kind="thread"
+ {...(props.unreadable ? { state: "invalid" as const } : {})}
render={<Link to="/$environmentId/$threadId" params={{ environmentId, threadId }} />}
aria-label={`Thread, ${title}`}
data-markdown-copy={props.copyMarkdown}
@@ -29,7 +31,13 @@ export function ThreadContextChip(props: {
</ContextChip>
}
/>
- <TooltipPopup side="top">{shell ? "Open thread" : "Thread no longer available"}</TooltipPopup>
+ <TooltipPopup side="top">
+ {props.unreadable
+ ? "On another machine, so this machine's agent can't read it. Remove it or switch back to send."
+ : shell
+ ? "Open thread"
+ : "Thread no longer available"}
+ </TooltipPopup>
</Tooltip>
);
}
diff --git a/apps/web/src/components/composerContextPresentation.tsx b/apps/web/src/components/composerContextPresentation.tsx
index 81d8fb4f0e1..c642a29c89a 100644
--- a/apps/web/src/components/composerContextPresentation.tsx
+++ b/apps/web/src/components/composerContextPresentation.tsx
@@ -323,6 +323,16 @@ function ComposerPreviewAnnotationDetails({
);
}
+function ComposerThreadContextChip(props: { record: ThreadContextRecord }) {
+ const { environmentId } = use(ComposerContextActionsContext);
+ return (
+ <ThreadContextChip
+ record={props.record}
+ unreadable={environmentId !== null && props.record.environmentId !== environmentId}
+ />
+ );
+}
+
function UnresolvedContextChip(props: { label: string }) {
return (
<UnresolvedChip
@@ -432,7 +442,7 @@ const composerContextPresentationRegistry = createContextPresentationRegistry<
canRender: (entry) => entry.kind === "thread",
render: (entry, context) =>
entry.kind === "thread" ? (
- <ThreadContextChip record={entry.record} />
+ <ComposerThreadContextChip record={entry.record} />
) : (
<UnresolvedContextChip label={context.label} />
),
diff --git a/packages/shared/src/composerContextReferences.test.ts b/packages/shared/src/composerContextReferences.test.ts
index b1a41b2eac3..aceb63698ea 100644
--- a/packages/shared/src/composerContextReferences.test.ts
+++ b/packages/shared/src/composerContextReferences.test.ts
@@ -1,4 +1,9 @@
-import type { ComposerContextId, ComposerContextRecord } from "@t3tools/contracts";
+import {
+ type ComposerContextId,
+ type ComposerContextRecord,
+ EnvironmentId,
+ ThreadId,
+} from "@t3tools/contracts";
import { describe, expect, it } from "vite-plus/test";
import {
@@ -11,6 +16,7 @@ import {
projectComposerContextForProvider,
replaceComposerContextReferences,
sanitizeComposerContextLabel,
+ threadContextsOutsideEnvironment,
toComposerContextId,
toKindScopedComposerContextId,
} from "./composerContextReferences.ts";
@@ -301,6 +307,48 @@ describe("provider projection", () => {
});
});
+describe("threadContextsOutsideEnvironment", () => {
+ it("returns only referenced foreign threads, ignoring local and removed references", () => {
+ const local = {
+ version: 1 as const,
+ kind: "thread" as const,
+ contextId: ctx("thread_local"),
+ label: "Local thread",
+ environmentId: EnvironmentId.make("env-local"),
+ threadId: ThreadId.make("local"),
+ title: "Local thread",
+ };
+ const foreign = {
+ ...local,
+ contextId: ctx("thread_foreign"),
+ environmentId: EnvironmentId.make("env-foreign"),
+ threadId: ThreadId.make("foreign"),
+ };
+ const removed = {
+ ...foreign,
+ contextId: ctx("thread_removed"),
+ threadId: ThreadId.make("removed"),
+ };
+ const records = [local, foreign, removed];
+ const localReference = "[Local](t3-context://v1/thread/thread_local)";
+
+ expect(
+ threadContextsOutsideEnvironment({
+ text: `${localReference} [Foreign](t3-context://v1/thread/thread_foreign)`,
+ records,
+ environmentId: local.environmentId,
+ }),
+ ).toEqual([foreign]);
+ expect(
+ threadContextsOutsideEnvironment({
+ text: localReference,
+ records,
+ environmentId: local.environmentId,
+ }),
+ ).toEqual([]);
+ });
+});
+
describe("toComposerContextId", () => {
it("keeps same-kind raw IDs distinct when one contains the namespace prefix", () => {
expect(toKindScopedComposerContextId("image", "x")).toBe("image_x");
diff --git a/packages/shared/src/composerContextReferences.ts b/packages/shared/src/composerContextReferences.ts
index 8bad6dcd3f7..a9815ba57cb 100644
--- a/packages/shared/src/composerContextReferences.ts
+++ b/packages/shared/src/composerContextReferences.ts
@@ -4,7 +4,9 @@ import {
type ComposerContextKind,
type ComposerContextRecord,
type ElementContextDetails,
+ type EnvironmentId,
type KnownComposerContextRecord,
+ type ThreadContextRecord,
} from "@t3tools/contracts";
const CONTEXT_PROTOCOL = "t3-context:";
@@ -320,6 +322,24 @@ export function projectComposerContextForProvider(input: {
return `${body}\n\n<${CONTEXT_ENVELOPE_TAG} version="1">\n${entries.join("\n")}\n</${CONTEXT_ENVELOPE_TAG}>`;
}
+// Thread reads are environment-local even when a draft moves between machines.
+export function threadContextsOutsideEnvironment(input: {
+ text: string;
+ records: ReadonlyArray<ComposerContextRecord>;
+ environmentId: EnvironmentId;
+}): ThreadContextRecord[] {
+ const referenced = new Set(
+ collectComposerContextReferences(input.text).map((occurrence) => occurrence.contextId),
+ );
+ return input.records.filter(
+ (record): record is ThreadContextRecord =>
+ record.kind === "thread" &&
+ "threadId" in record &&
+ referenced.has(record.contextId) &&
+ record.environmentId !== input.environmentId,
+ );
+}
+
export function remapComposerContextAttachments(
context: import("@t3tools/contracts").OrchestrationMessageContext | undefined,
before: ReadonlyArray<{ readonly id?: string | undefined }>,