Skip to content

Commit c137717

Browse files
fix(composer): don't send attached threads to a machine that can't read them (pingdotgg#18310)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 7cca7d1 commit c137717

5 files changed

Lines changed: 130 additions & 5 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ import {
339339
type TerminalContextSelection,
340340
} from "../lib/terminalContext";
341341
import {
342+
collectInlineContextIds,
342343
ensureInlineContextReferences,
343344
removeInlineContextReference,
344345
} from "../lib/composerContextReferences";
@@ -559,7 +560,10 @@ import {
559560
supportsServerUpdateThreadContinuation,
560561
} from "../versionSkew";
561562
import { useAssetUrls } from "../assets/assetUrls";
562-
import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences";
563+
import {
564+
ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,
565+
threadContextsOutsideEnvironment,
566+
} from "@t3tools/shared/composerContextReferences";
563567
import { recallableComposerPrompt } from "./chat/composerPromptHistory";
564568
import { observeResize } from "~/lib/observeResize";
565569

@@ -1226,6 +1230,15 @@ export default function ChatView(props: ChatViewProps) {
12261230
const draft = store.getComposerDraft(composerDraftTarget);
12271231
return (draft?.images.length ?? 0) > 0 || (draft?.files.length ?? 0) > 0;
12281232
});
1233+
const referencedThreadContexts = useComposerDraftStore(
1234+
useShallow((store) => {
1235+
const draft = store.getComposerDraft(composerDraftTarget);
1236+
if (!draft?.threadContexts.length) return [];
1237+
// Linked prompts can replace text without pruning stored context records.
1238+
const referenced = new Set(collectInlineContextIds(draft.prompt));
1239+
return draft.threadContexts.filter((record) => referenced.has(record.contextId));
1240+
}),
1241+
);
12291242
const composerHasNonPromptContent = useComposerDraftStore((store) => {
12301243
const draft = store.getComposerDraft(composerDraftTarget);
12311244
return draft ? composerDraftHasUserContent({ ...draft, prompt: "" }) : false;
@@ -3128,6 +3141,9 @@ export default function ChatView(props: ChatViewProps) {
31283141
return (
31293142
environment?.connection.phase === "connected" &&
31303143
(loadBalancingSettings.loadBalancingWeights[candidate.environmentId] ?? 50) > 0 &&
3144+
referencedThreadContexts.every(
3145+
(record) => record.environmentId === candidate.environmentId,
3146+
) &&
31313147
providersByEnvironment
31323148
.get(candidate.environmentId)
31333149
?.some(
@@ -6037,7 +6053,14 @@ export default function ChatView(props: ChatViewProps) {
60376053
: "Choose a machine to continue",
60386054
description: loadBalancing.pending
60396055
? "Resource checks are still running. You can choose a machine in the composer."
6040-
: "No eligible machine has available resources. Choose a machine in the composer to override.",
6056+
: referencedThreadContexts.length > 0 &&
6057+
!logicalProjectEnvironments.some((candidate) =>
6058+
referencedThreadContexts.every(
6059+
(record) => record.environmentId === candidate.environmentId,
6060+
),
6061+
)
6062+
? "No machine can read every attached thread. Choose a machine in the composer or remove the threads."
6063+
: "No eligible machine has available resources. Choose a machine in the composer to override.",
60416064
});
60426065
return;
60436066
}
@@ -6122,6 +6145,22 @@ export default function ChatView(props: ChatViewProps) {
61226145
previewAnnotationContextReference(directAnnotation.annotation),
61236146
])
61246147
: promptRef.current;
6148+
const strandedThread = threadContextsOutsideEnvironment({
6149+
text: promptForSend,
6150+
records: composerThreadContexts,
6151+
environmentId,
6152+
})[0];
6153+
if (strandedThread) {
6154+
const owner = environmentById.get(strandedThread.environmentId)?.label ?? "its machine";
6155+
toastManager.add(
6156+
stackedThreadToast({
6157+
type: "warning",
6158+
title: "Attached thread is on another machine",
6159+
description: `This machine's agent can't read "${strandedThread.title}". Remove it, or switch back to ${owner} to send.`,
6160+
}),
6161+
);
6162+
return;
6163+
}
61256164
if (queuedRunEdit.isEditing) {
61266165
// Edit mode repurposes the composer: sending saves the queued message
61276166
// in place instead of dispatching a new turn.

‎apps/web/src/components/ThreadContextChip.tsx‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
99
export function ThreadContextChip(props: {
1010
record: Pick<ThreadContextRecord, "environmentId" | "threadId" | "title">;
1111
copyMarkdown?: string;
12+
unreadable?: boolean;
1213
}) {
1314
const { environmentId, threadId } = props.record;
1415
const shell = useThreadShell({ environmentId, threadId });
@@ -19,6 +20,7 @@ export function ThreadContextChip(props: {
1920
render={
2021
<ContextChip
2122
kind="thread"
23+
{...(props.unreadable ? { state: "invalid" as const } : {})}
2224
render={<Link to="/$environmentId/$threadId" params={{ environmentId, threadId }} />}
2325
aria-label={`Thread, ${title}`}
2426
data-markdown-copy={props.copyMarkdown}
@@ -29,7 +31,13 @@ export function ThreadContextChip(props: {
2931
</ContextChip>
3032
}
3133
/>
32-
<TooltipPopup side="top">{shell ? "Open thread" : "Thread no longer available"}</TooltipPopup>
34+
<TooltipPopup side="top">
35+
{props.unreadable
36+
? "On another machine, so this machine's agent can't read it. Remove it or switch back to send."
37+
: shell
38+
? "Open thread"
39+
: "Thread no longer available"}
40+
</TooltipPopup>
3341
</Tooltip>
3442
);
3543
}

‎apps/web/src/components/composerContextPresentation.tsx‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,16 @@ function ComposerPreviewAnnotationDetails({
323323
);
324324
}
325325

326+
function ComposerThreadContextChip(props: { record: ThreadContextRecord }) {
327+
const { environmentId } = use(ComposerContextActionsContext);
328+
return (
329+
<ThreadContextChip
330+
record={props.record}
331+
unreadable={environmentId !== null && props.record.environmentId !== environmentId}
332+
/>
333+
);
334+
}
335+
326336
function UnresolvedContextChip(props: { label: string }) {
327337
return (
328338
<UnresolvedChip
@@ -432,7 +442,7 @@ const composerContextPresentationRegistry = createContextPresentationRegistry<
432442
canRender: (entry) => entry.kind === "thread",
433443
render: (entry, context) =>
434444
entry.kind === "thread" ? (
435-
<ThreadContextChip record={entry.record} />
445+
<ComposerThreadContextChip record={entry.record} />
436446
) : (
437447
<UnresolvedContextChip label={context.label} />
438448
),

‎packages/shared/src/composerContextReferences.test.ts‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import type { ComposerContextId, ComposerContextRecord } from "@t3tools/contracts";
1+
import {
2+
type ComposerContextId,
3+
type ComposerContextRecord,
4+
EnvironmentId,
5+
ThreadId,
6+
} from "@t3tools/contracts";
27
import { describe, expect, it } from "vite-plus/test";
38

49
import {
@@ -11,6 +16,7 @@ import {
1116
projectComposerContextForProvider,
1217
replaceComposerContextReferences,
1318
sanitizeComposerContextLabel,
19+
threadContextsOutsideEnvironment,
1420
toComposerContextId,
1521
toKindScopedComposerContextId,
1622
} from "./composerContextReferences.ts";
@@ -301,6 +307,48 @@ describe("provider projection", () => {
301307
});
302308
});
303309

310+
describe("threadContextsOutsideEnvironment", () => {
311+
it("returns only referenced foreign threads, ignoring local and removed references", () => {
312+
const local = {
313+
version: 1 as const,
314+
kind: "thread" as const,
315+
contextId: ctx("thread_local"),
316+
label: "Local thread",
317+
environmentId: EnvironmentId.make("env-local"),
318+
threadId: ThreadId.make("local"),
319+
title: "Local thread",
320+
};
321+
const foreign = {
322+
...local,
323+
contextId: ctx("thread_foreign"),
324+
environmentId: EnvironmentId.make("env-foreign"),
325+
threadId: ThreadId.make("foreign"),
326+
};
327+
const removed = {
328+
...foreign,
329+
contextId: ctx("thread_removed"),
330+
threadId: ThreadId.make("removed"),
331+
};
332+
const records = [local, foreign, removed];
333+
const localReference = "[Local](t3-context://v1/thread/thread_local)";
334+
335+
expect(
336+
threadContextsOutsideEnvironment({
337+
text: `${localReference} [Foreign](t3-context://v1/thread/thread_foreign)`,
338+
records,
339+
environmentId: local.environmentId,
340+
}),
341+
).toEqual([foreign]);
342+
expect(
343+
threadContextsOutsideEnvironment({
344+
text: localReference,
345+
records,
346+
environmentId: local.environmentId,
347+
}),
348+
).toEqual([]);
349+
});
350+
});
351+
304352
describe("toComposerContextId", () => {
305353
it("keeps same-kind raw IDs distinct when one contains the namespace prefix", () => {
306354
expect(toKindScopedComposerContextId("image", "x")).toBe("image_x");

‎packages/shared/src/composerContextReferences.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import {
44
type ComposerContextKind,
55
type ComposerContextRecord,
66
type ElementContextDetails,
7+
type EnvironmentId,
78
type KnownComposerContextRecord,
9+
type ThreadContextRecord,
810
} from "@t3tools/contracts";
911

1012
const CONTEXT_PROTOCOL = "t3-context:";
@@ -320,6 +322,24 @@ export function projectComposerContextForProvider(input: {
320322
return `${body}\n\n<${CONTEXT_ENVELOPE_TAG} version="1">\n${entries.join("\n")}\n</${CONTEXT_ENVELOPE_TAG}>`;
321323
}
322324

325+
// Thread reads are environment-local even when a draft moves between machines.
326+
export function threadContextsOutsideEnvironment(input: {
327+
text: string;
328+
records: ReadonlyArray<ComposerContextRecord>;
329+
environmentId: EnvironmentId;
330+
}): ThreadContextRecord[] {
331+
const referenced = new Set(
332+
collectComposerContextReferences(input.text).map((occurrence) => occurrence.contextId),
333+
);
334+
return input.records.filter(
335+
(record): record is ThreadContextRecord =>
336+
record.kind === "thread" &&
337+
"threadId" in record &&
338+
referenced.has(record.contextId) &&
339+
record.environmentId !== input.environmentId,
340+
);
341+
}
342+
323343
export function remapComposerContextAttachments(
324344
context: import("@t3tools/contracts").OrchestrationMessageContext | undefined,
325345
before: ReadonlyArray<{ readonly id?: string | undefined }>,

0 commit comments

Comments
 (0)