The best way to control Claude Code, Codex and more from any computer or phone
fix(web): preserve thread identity across composer pastes (#30583)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Elliot Drel committed
Nov 14, 2026 at 18:35 UTC
62405fce573b186d559c826fc9f859b49b8c584d
2 files changed
+89
-5
apps/web/src/components/chat/useComposerContextImport.browser.test.tsx
+74
-1
@@ -1,5 +1,11 @@
1
+import { threadContextRecord } from "@t3tools/shared/composerContextReferences";
2
import { useEffect } from "react";
2
-import { type ComposerContextRecord, ComposerContextId, EnvironmentId } from "@t3tools/contracts";
3
+import {
4
+ type ComposerContextRecord,
5
+ ComposerContextId,
6
+ EnvironmentId,
7
+ ThreadId,
8
+} from "@t3tools/contracts";
9
import { expect, it, vi } from "vite-plus/test";
10
11
import { act, render, stubGlobal } from "../../../test/render";
@@ -186,3 +192,70 @@ it.each([
192
: [],
193
);
194
});
195
+
196
+it.each(["same", "other-environment"] as const)(
197
+ "preserves thread identity across repeated pastes: %s",
198
+ async (variant) => {
199
+ const otherEnvironment = variant === "other-environment";
200
+ const target = DraftId.make(`thread-collision-${variant}`);
201
+ const store = useComposerDraftStore.getState();
202
+ using _draft = { [Symbol.dispose]: () => store.clearComposerContent(target) };
203
+ const environmentId = EnvironmentId.make("thread-source");
204
+ const otherEnvironmentId = EnvironmentId.make("other-thread-source");
205
+ const first = threadContextRecord(
206
+ { environmentId, threadId: ThreadId.make("same-id") },
207
+ "First",
208
+ );
209
+ const pasted = otherEnvironment
210
+ ? threadContextRecord(
211
+ { environmentId: otherEnvironmentId, threadId: first.threadId },
212
+ "Second",
213
+ )
214
+ : first;
215
+ store.addThreadContexts(target, [first], { appendReference: false });
216
+ let importer!: Importer;
217
+ await using _screen = await render(
218
+ <Harness
219
+ input={{
220
+ environmentId,
221
+ linkedEnvironments: new Map([[otherEnvironmentId, "Other machine"]]),
222
+ activeThread: undefined,
223
+ activeThreadId: null,
224
+ composerDraftTarget: target,
225
+ attachmentDraftTarget: target,
226
+ attachmentTargetKey: target,
227
+ attachmentTargetKeyRef: { current: target },
228
+ questionAttachmentTarget: null,
229
+ pendingUserInputs: [],
230
+ composerContextRecords: new Map([[first.contextId, { kind: "thread", record: first }]]),
231
+ composerImages: [],
232
+ composerFilesRef: { current: [] },
233
+ pendingThreadImportsRef: { current: new Map() },
234
+ promptRef: { current: "" },
235
+ addComposerFilesToDraft: () => [],
236
+ }}
237
+ ready={(value) => {
238
+ importer = value;
239
+ }}
240
+ />,
241
+ );
242
+ let importedId: string | undefined;
243
+ await act(() => {
244
+ importedId = importer
245
+ .importContextRecords([pasted], pasted.environmentId, target)
246
+ .get(pasted.contextId);
247
+ });
248
+ const records = store.getComposerDraft(target)!.threadContexts;
249
+ expect(records.find((record) => record.contextId === importedId)).toMatchObject({
250
+ environmentId: pasted.environmentId,
251
+ threadId: pasted.threadId,
252
+ });
253
+ expect(records).toHaveLength(otherEnvironment ? 2 : 1);
254
+ expect(records[0]).toEqual(first);
255
+ await act(() => {
256
+ const repeated = importer.importContextRecords([pasted], pasted.environmentId, target);
257
+ expect(repeated.get(pasted.contextId)).toBe(importedId);
258
+ });
259
+ expect(store.getComposerDraft(target)!.threadContexts).toEqual(records);
260
+ },
261
+);
apps/web/src/components/chat/useComposerContextImport.ts
+15
-4
@@ -304,7 +304,7 @@ export function useComposerContextImport(input: {
304
for (const candidate of orderedRecords) {
305
// Producer ids fold into context ids, so two different excerpts can collide. Only skip
306
// when the draft already holds the same payload; a colliding but different record is
307
- // re-minted under a fresh id so both survive the paste.
307
+ // re-minted under a fresh id so both survive the paste. Threads resolve below.
308
const record = asKnownContextRecord(candidate);
309
if (!record) continue;
310
// A reminted annotation needs its screenshot under the new annotation id.
@@ -345,7 +345,7 @@ export function useComposerContextImport(input: {
345
if (
346
existingRecord &&
347
isSameComposerContextPayload(existingRecord, record) &&
348
- (record.kind !== "thread" || threadDraftTarget === attachmentDraftTarget)
348
+ record.kind !== "thread"
349
) {
350
if (record.kind === "preview-annotation" && record.screenshotContextId) {
351
skippedDependentAttachmentIds.add(record.screenshotContextId);
@@ -398,10 +398,21 @@ export function useComposerContextImport(input: {
398
!linkedEnvironments.has(record.environmentId)
399
)
400
break;
401
- addComposerDraftThreadContexts(threadDraftTarget, [record], {
401
+ // Threads reuse their destination record by identity, reminting only on an ID collision.
402
+ const threads = getComposerDraft(threadDraftTarget)?.threadContexts ?? [];
403
+ const attached = threads.find(
404
+ (thread) =>
405
+ thread.environmentId === record.environmentId && thread.threadId === record.threadId,
406
+ );
407
+ const imported =
408
+ attached ??
409
+ (threads.some((thread) => thread.contextId === record.contextId)
410
+ ? { ...record, contextId: toKindScopedComposerContextId("thread", randomUUID()) }
411
+ : record);
412
+ addComposerDraftThreadContexts(threadDraftTarget, [imported], {
413
appendReference: false,
414
});
404
- rewritten.set(record.contextId, record.contextId);
415
+ rewritten.set(record.contextId, imported.contextId);
416
break;
417
}
418
case "environment": {