Skip to content

Commit 7ce31b6

Browse files
fix(web): pasted threads the agent cannot read keep a link to their machine
A pasted or restored chip for a thread on an unlinked machine became its bare label, so a failed transcript import left the agent nothing to find the thread by. It now becomes a link that names the thread and its environment, while the transcript import still runs. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 94d52af commit 7ce31b6

5 files changed

Lines changed: 33 additions & 15 deletions

File tree

‎apps/web/src/components/chat/useComposerContextImport.browser.test.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ import {
1111
encodeComposerContextFragment,
1212
} from "@t3tools/shared/composerContextClipboard";
1313
import { threadContextRecord } from "@t3tools/shared/composerContextReferences";
14+
import { hostedThreadLink } from "@t3tools/shared/threadLinks";
1415
import { expect, it, vi } from "vite-plus/test";
1516

1617
import { act, render, stubGlobal } from "../../../test/render";
1718
import { DraftId, useComposerDraftStore } from "../../composerDraftStore";
19+
import { configuredHostedAppUrl } from "../../hostedPairing";
1820
import { formatInlineContextReference } from "../../lib/composerContextReferences";
1921
import { threadContextReference } from "../../lib/composerContextRecords";
2022
import { rememberCopiedComposerAttachments } from "../../lib/copiedComposerAttachments";
@@ -231,10 +233,10 @@ it.each([
231233
);
232234
});
233235

234-
// Its chip used to resolve to nothing, or block the send; a dropped one comes in as its transcript.
236+
// Its chip used to resolve to nothing; now it keeps a link naming the thread and its machine.
235237
it.each([
236238
{ machine: "unlinked-machine", transcript: true },
237-
// Nothing can load a machine this client is not connected to, so the chip is only its label.
239+
// Nothing can load a machine this client is not connected to, so only the link remains.
238240
{ machine: "disconnected-machine", transcript: false },
239241
])(
240242
"pastes a thread from $machine, importing its transcript: $transcript",
@@ -276,7 +278,7 @@ it.each([
276278
importer.importContextFragment,
277279
);
278280
});
279-
expect(pasted).toBe("See Remote thread");
281+
expect(pasted).toBe(`See [Remote thread](${hostedThreadLink(ref, configuredHostedAppUrl())})`);
280282
expect(importThreadTranscript.mock.calls).toEqual(transcript ? [[ref, target]] : []);
281283
expect(store.getComposerDraft(target)?.threadContexts ?? []).toEqual([]);
282284
},

‎apps/web/src/components/chat/useComposerContextImport.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ export function useComposerContextImport(input: {
302302
sourceEnvironmentId: EnvironmentId | null,
303303
threadDraftTarget = attachmentDraftTarget,
304304
): ReadonlyMap<string, string | null> => {
305-
// A null id means the reference becomes its plain label.
305+
// A null id means the reference becomes a link to a thread the agent cannot read.
306306
const rewritten = new Map<string, string | null>();
307307
const dependentAttachmentLocalIds = new Map<string, string>();
308308
const skippedDependentAttachmentIds = new Set<string>();
@@ -416,11 +416,12 @@ export function useComposerContextImport(input: {
416416
record.environmentId !== environmentId &&
417417
!linkedEnvironments.has(record.environmentId)
418418
) {
419-
if (!readPreparedConnection(record.environmentId)) break;
420-
importThreadTranscript(
421-
{ environmentId: record.environmentId, threadId: record.threadId },
422-
threadDraftTarget,
423-
);
419+
if (readPreparedConnection(record.environmentId)) {
420+
importThreadTranscript(
421+
{ environmentId: record.environmentId, threadId: record.threadId },
422+
threadDraftTarget,
423+
);
424+
}
424425
rewritten.set(record.contextId, null);
425426
break;
426427
}

‎apps/web/src/components/chat/useComposerStash.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { compressImageForStash } from "../../lib/imageCompression";
3232
import { formatInlineContextReference } from "~/lib/composerContextReferences";
3333
import { isCommandPaletteOpen } from "../../commandPaletteBus";
3434
import { pendingDraftWork } from "./pendingDraftWork";
35+
import { unreadableThreadLink } from "../composerInlineTokenPaste";
3536
import {
3637
previewAnnotationContextRecord,
3738
reviewCommentContextRecord,
@@ -209,7 +210,7 @@ export function useComposerStash(input: {
209210
: new Map<string, string | null>();
210211
const restoredPrompt = replaceComposerContextReferences(entry.prompt, (reference) => {
211212
const contextId = rewrittenContextIds.get(reference.contextId);
212-
if (contextId === null) return reference.label;
213+
if (contextId === null) return unreadableThreadLink(entry.records ?? [], reference);
213214
return contextId
214215
? formatInlineContextReference({
215216
...reference,

‎apps/web/src/components/composerInlineTokenPaste.ts‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ import {
1313
collectComposerContextReferences,
1414
formatComposerContextReference,
1515
replaceComposerContextReferences,
16+
sanitizeComposerContextLabel,
1617
threadLinkContextReferences,
1718
} from "@t3tools/shared/composerContextReferences";
19+
import { hostedThreadLink } from "@t3tools/shared/threadLinks";
1820
import { isClosingFence, parseOpeningFence } from "~/composer-rich-text-doc";
21+
import { configuredHostedAppUrl } from "~/hostedPairing";
1922
import { readThreadShell } from "~/state/entities";
2023

2124
const threadTitle = (ref: ScopedThreadRef) => readThreadShell(ref)?.title;
@@ -97,6 +100,17 @@ function codeOffsetTest(text: string): (offset: number) => boolean {
97100
};
98101
}
99102

103+
/** A thread the agent cannot read stays a link to its machine, whether or not its transcript arrives. */
104+
export function unreadableThreadLink(
105+
records: ReadonlyArray<ComposerContextRecord>,
106+
reference: { readonly contextId: string; readonly label: string },
107+
): string {
108+
const record = records.find((candidate) => candidate.contextId === reference.contextId);
109+
if (record?.kind !== "thread" || !("threadId" in record)) return reference.label;
110+
const link = hostedThreadLink(record, configuredHostedAppUrl());
111+
return `[${sanitizeComposerContextLabel(reference.label, "thread")}](${link})`;
112+
}
113+
100114
/** Pasted thread links count as context too, so they become chips like a copied chip does. */
101115
export function readPastedComposerContext(
102116
clipboardData: Pick<DataTransfer, "getData">,
@@ -169,10 +183,10 @@ export function importPastedComposerText(
169183
kind: occurrence.kind === "element" ? "preview-annotation" : occurrence.kind,
170184
});
171185
}
172-
return rewrittenIds?.get(occurrence.contextId) !== null &&
173-
heldContextIds.has(occurrence.contextId)
174-
? occurrence.source
175-
: occurrence.label;
186+
if (rewrittenIds?.get(occurrence.contextId) === null) {
187+
return unreadableThreadLink(fragment?.records ?? [], occurrence);
188+
}
189+
return heldContextIds.has(occurrence.contextId) ? occurrence.source : occurrence.label;
176190
});
177191
// A thread the import refused, on a machine this agent cannot read, stays the link it was.
178192
return threadLinkContextReferences(text, fragment?.records ?? [], threadTitle, (record) =>

‎docs/user/composer.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ thread does not change it, and the agent cannot send messages to it unless you a
349349

350350
A thread from a linked machine, picked, pasted or dropped, stays a reference that the agent reads
351351
on that machine. Pasting or dropping a thread from any other environment attaches a saved
352-
transcript file, and a pasted link to it stays a link. The source
352+
transcript file, and a pasted one also stays a link to the thread. The source
353353
environment must be connected and up to date. The transcript includes the visible history and
354354
saved text context at the time you attached it; later messages and the contents of attached files
355355
are not copied. The agent can read the saved transcript after the source disconnects. Normal

0 commit comments

Comments
 (0)