The best way to control Claude Code, Codex and more from any computer or phone
fix(web): recovered queued edits keep their mentions and context (#36578)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Wout Stiens committed
Nov 14, 2026 at 09:42 UTC
5e06246d24b028f5bfa1543b78cf7e55bf98746e
4 files changed
+134
-90
apps/web/src/components/chat/queuedMessageEdit.test.ts
+45
-7
@@ -1,4 +1,4 @@
1
-import { EnvironmentId, ThreadId } from "@t3tools/contracts";
1
+import { EnvironmentId, MessageId, ThreadId, TurnItemId } from "@t3tools/contracts";
2
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
3
import { threadContextRecord } from "@t3tools/shared/composerContextReferences";
4
import { beforeEach, describe, expect, it } from "vite-plus/test";
@@ -8,6 +8,7 @@ import {
8
type ComposerFileAttachment,
9
type ComposerImageAttachment,
10
} from "../../composerDraftStore";
11
+import { environmentContextRecord, responseContextRecord } from "../../lib/composerContextRecords";
12
import { prepareQueuedEditAttachments, recoverQueuedMessageEdit } from "./queuedMessageEdit";
13
14
const environmentId = EnvironmentId.make("remote-environment");
@@ -78,25 +79,62 @@ describe("queued message file edits", () => {
79
it("keeps file-only edits when another client starts the queued run", () => {
80
const store = useComposerDraftStore.getState();
81
store.setPrompt(editTarget, "Original message");
81
- store.addFiles(editTarget, [file]);
82
+ const elsewhere: ComposerFileAttachment = {
83
+ ...uploadedFile,
84
+ id: "file:elsewhere",
85
+ name: "elsewhere.pdf",
86
+ file: null,
87
+ uploadedAttachmentId: "upload:elsewhere",
88
+ uploadEnvironmentId: EnvironmentId.make("other-environment"),
89
+ };
90
+ store.addFiles(editTarget, [file, elsewhere]);
91
expect(
92
recoverQueuedMessageEdit({ editTarget, threadTarget, originalText: "Original message" }),
93
).toBe("kept");
94
expect(store.getComposerDraft(threadTarget)?.prompt).toBe("Original message");
86
- expect(store.getComposerDraft(threadTarget)?.files).toEqual([file]);
95
+ // An upload this environment cannot reach comes back as a file to reattach, not a dropped chip.
96
+ const { uploadedAttachmentId: _id, uploadEnvironmentId: _env, ...reattach } = elsewhere;
97
+ expect(store.getComposerDraft(threadTarget)?.files).toEqual([file, reattach]);
98
expect(store.getComposerDraft(editTarget)).toBeNull();
99
});
100
90
- it("removes a kept edit draft that held a thread mention", () => {
101
+ it("keeps the mentions and reply of a kept edit in the thread draft", () => {
102
const store = useComposerDraftStore.getState();
103
store.setPrompt(editTarget, "Edited message");
93
- store.setThreadContexts(editTarget, [
94
- threadContextRecord(scopeThreadRef(environmentId, ThreadId.make("thread:other")), "Other"),
95
- ]);
104
+ const thread = threadContextRecord(
105
+ scopeThreadRef(environmentId, ThreadId.make("thread:other")),
106
+ "Other",
107
+ );
108
+ const machine = environmentContextRecord(EnvironmentId.make("environment-vps"), "vps");
109
+ const reply = responseContextRecord({
110
+ citation: {
111
+ version: 1,
112
+ environmentId,
113
+ threadId: ThreadId.make("thread:edit"),
114
+ messageId: MessageId.make("message:answer"),
115
+ text: "Done.",
116
+ start: 0,
117
+ end: 5,
118
+ prefix: "",
119
+ suffix: "",
120
+ },
121
+ itemId: TurnItemId.make("item:answer"),
122
+ subagents: [],
123
+ });
124
+ store.addThreadContexts(editTarget, [thread]);
125
+ store.addEnvironmentContexts(editTarget, [machine]);
126
+ store.setReplyTo(editTarget, reply);
127
+ const editedPrompt = store.getComposerDraft(editTarget)!.prompt;
128
expect(
129
recoverQueuedMessageEdit({ editTarget, threadTarget, originalText: "Original message" }),
130
).toBe("kept");
131
expect(store.getComposerDraft(editTarget)).toBeNull();
132
+ expect(store.getComposerDraft(threadTarget)).toMatchObject({
133
+ prompt: editedPrompt,
134
+ threadContexts: [thread],
135
+ environmentContexts: [machine],
136
+ replyTo: reply,
137
+ });
138
});
139
140
it("reports a removed queued attachment as an edit when the run leaves", () => {
apps/web/src/components/chat/queuedMessageEdit.ts
+1
-1
@@ -27,7 +27,7 @@ export function recoverQueuedMessageEdit(input: {
27
!composerDraftHasUserContent(store.getComposerDraft(input.threadTarget)),
28
});
29
if (recovery === "kept") {
30
- store.moveComposerPromptAndImages(input.editTarget, input.threadTarget);
30
+ store.moveComposerContent(input.editTarget, input.threadTarget);
31
}
32
store.clearComposerContent(input.editTarget);
33
return recovery;
apps/web/src/composerDraftStore.ts
+24
-82
@@ -1,4 +1,3 @@
1
-import { stripInlineContextReferences } from "./lib/composerContextReferences";
1
import {
2
composerFileAttachmentMimeType,
3
composerImageDedupKey,
@@ -276,9 +275,10 @@ export function composerFileNeedsReattach(file: ComposerFileAttachment): boolean
275
return file.file === null && file.uploadedAttachmentId === undefined;
276
}
277
278
+/** Strips upload ids another environment owns; local bytes re-upload, byte-less files need reattaching. */
279
function clearStaleFileUploadMetadata(
280
draft: ComposerThreadDraftState,
281
- environmentId: EnvironmentId,
281
+ environmentId: EnvironmentId | null,
282
): ComposerThreadDraftState {
283
let changed = false;
284
const files = draft.files.map((file) => {
@@ -757,8 +757,8 @@ interface ComposerDraftStoreState {
757
clearComposerContent: (threadRef: ComposerThreadTarget) => void;
758
/** Clears text and attachments, preserving terminal/element contexts, preview annotations, and review comments. */
759
clearComposerPromptAndImages: (threadRef: ComposerThreadTarget) => void;
760
- /** Keeps attachments over destination limits, other environments' uploads, terminal/element contexts, annotations, and review comments in source. */
761
- moveComposerPromptAndImages: (from: ComposerThreadTarget, to: ComposerThreadTarget) => void;
760
+ /** Moves all user content into a destination that has none; otherwise leaves both drafts alone. */
761
+ moveComposerContent: (from: ComposerThreadTarget, to: ComposerThreadTarget) => void;
762
}
763
764
export interface EffectiveComposerModelState {
@@ -913,6 +913,18 @@ function createEmptyThreadDraft(): ComposerThreadDraftState {
913
};
914
}
915
916
+/** Keep these content fields aligned with composerDraftHasUserContent. */
917
+function composerDraftContent({
918
+ modelSelectionByProvider: _modelSelectionByProvider,
919
+ activeProvider: _activeProvider,
920
+ modelSelectionExplicit: _modelSelectionExplicit,
921
+ runtimeMode: _runtimeMode,
922
+ interactionMode: _interactionMode,
923
+ ...content
924
+}: ComposerThreadDraftState) {
925
+ return content;
926
+}
927
+
928
export function composerFileDedupKey(
929
file: Pick<ComposerFileAttachment, "mimeType" | "sizeBytes" | "name">,
930
): string {
@@ -4288,17 +4300,7 @@ const composerDraftStore = create<ComposerDraftStoreState>()(
4300
}
4301
const nextDraft: ComposerThreadDraftState = {
4302
...current,
4291
- prompt: "",
4292
- images: [],
4293
- files: [],
4294
- nonPersistedImageIds: [],
4295
- persistedAttachments: [],
4296
- terminalContexts: [],
4297
- previewAnnotations: [],
4298
- reviewComments: [],
4299
- threadContexts: [],
4300
- environmentContexts: [],
4301
- replyTo: null,
4303
+ ...composerDraftContent(createEmptyThreadDraft()),
4304
};
4305
const nextDraftsByThreadKey = { ...state.draftsByThreadKey };
4306
if (shouldRemoveDraft(nextDraft)) {
@@ -4345,7 +4347,7 @@ const composerDraftStore = create<ComposerDraftStoreState>()(
4347
return { draftsByThreadKey: nextDraftsByThreadKey };
4348
});
4349
},
4348
- moveComposerPromptAndImages: (from, to) => {
4350
+ moveComposerContent: (from, to) => {
4351
const fromKey = resolveComposerDraftKey(get(), from) ?? "";
4352
const toKey = resolveComposerDraftKey(get(), to) ?? "";
4353
if (fromKey.length === 0 || toKey.length === 0 || fromKey === toKey) {
@@ -4353,84 +4355,24 @@ const composerDraftStore = create<ComposerDraftStoreState>()(
4355
}
4356
set((state) => {
4357
const source = state.draftsByThreadKey[fromKey];
4356
- if (!source) {
4358
+ const destination = state.draftsByThreadKey[toKey] ?? createEmptyThreadDraft();
4359
+ if (!source || composerDraftHasUserContent(destination)) {
4360
return state;
4361
}
4359
- const destination = state.draftsByThreadKey[toKey] ?? createEmptyThreadDraft();
4362
const destinationEnvironmentId =
4363
typeof to === "string"
4364
? (state.draftThreadsByThreadKey[toKey]?.environmentId ??
4365
parseScopedThreadKey(toKey)?.environmentId ??
4366
null)
4367
: to.environmentId;
4366
- const destinationFileIds = new Set(destination.files.map((file) => file.id));
4367
- const destinationFileKeys = new Set(destination.files.map(composerFileDedupKey));
4368
- const transferableFiles = source.files.filter(
4369
- (file) =>
4370
- (file.file !== null || file.uploadEnvironmentId === destinationEnvironmentId) &&
4371
- !destinationFileIds.has(file.id) &&
4372
- !destinationFileKeys.has(composerFileDedupKey(file)),
4373
- );
4374
- const remainingAttachmentSlots = Math.max(
4375
- 0,
4376
- PROVIDER_SEND_TURN_MAX_ATTACHMENTS -
4377
- destination.images.length -
4378
- destination.files.length,
4368
+ const nextDestination = clearStaleFileUploadMetadata(
4369
+ { ...destination, ...composerDraftContent(source) },
4370
+ destinationEnvironmentId,
4371
);
4380
- const movedImages = source.images.slice(0, remainingAttachmentSlots);
4381
- const movedImageIds = new Set(movedImages.map((image) => image.id));
4382
- const retainedImages = source.images.filter((image) => !movedImageIds.has(image.id));
4383
- const movedFiles = transferableFiles
4384
- .slice(0, remainingAttachmentSlots - movedImages.length)
4385
- .map((file) => {
4386
- // Re-upload local bytes across environments; the destination cannot reach source upload ids.
4387
- if (
4388
- file.file === null ||
4389
- file.uploadEnvironmentId === undefined ||
4390
- file.uploadEnvironmentId === destinationEnvironmentId
4391
- ) {
4392
- return file;
4393
- }
4394
- const { uploadedAttachmentId: _a, uploadEnvironmentId: _e, ...rest } = file;
4395
- return rest;
4396
- });
4397
- const movedFileIds = new Set(movedFiles.map((file) => file.id));
4398
- const retainedFiles = source.files.filter((file) => !movedFileIds.has(file.id));
4399
- const movedPrompt = ensureInlineContextReferences(
4400
- stripInlineContextReferences(source.prompt),
4401
- destination.terminalContexts.map(terminalContextReference),
4402
- );
4403
- const nextDestination: ComposerThreadDraftState = {
4404
- ...destination,
4405
- prompt: movedPrompt,
4406
- images: [...destination.images, ...movedImages],
4407
- files: [...destination.files, ...movedFiles],
4408
- nonPersistedImageIds: [
4409
- ...destination.nonPersistedImageIds,
4410
- ...source.nonPersistedImageIds.filter((imageId) => movedImageIds.has(imageId)),
4411
- ],
4412
- persistedAttachments: [
4413
- ...destination.persistedAttachments,
4414
- ...source.persistedAttachments.filter((attachment) =>
4415
- movedImageIds.has(attachment.id),
4416
- ),
4417
- ],
4418
- };
4372
// Moved images still use these blob URLs at the destination; do not revoke them.
4373
const nextSource: ComposerThreadDraftState = {
4374
...source,
4422
- prompt: ensureInlineContextReferences(
4423
- "",
4424
- source.terminalContexts.map(terminalContextReference),
4425
- ),
4426
- images: retainedImages,
4427
- files: retainedFiles,
4428
- nonPersistedImageIds: source.nonPersistedImageIds.filter(
4429
- (imageId) => !movedImageIds.has(imageId),
4430
- ),
4431
- persistedAttachments: source.persistedAttachments.filter(
4432
- (attachment) => !movedImageIds.has(attachment.id),
4433
- ),
4375
+ ...composerDraftContent(createEmptyThreadDraft()),
4376
};
4377
const nextDraftsByThreadKey = { ...state.draftsByThreadKey };
4378
if (shouldRemoveDraft(nextSource)) {
scripts/scenarios/queued-edit-keeps-thread-mention.e2e.ts
new
+64
@@ -0,0 +1,64 @@
1
+import { expect } from "e2e";
2
+import * as Effect from "effect/Effect";
3
+import * as FileSystem from "effect/FileSystem";
4
+import * as Path from "effect/Path";
5
+import * as Schema from "effect/Schema";
6
+
7
+import { T3_HOME } from "../e2e.config.ts";
8
+import { makeState, runPromise, test, WEB } from "./t3.ts";
9
+
10
+const SettingsJson = Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown));
11
+const decodeSettings = Schema.decodeEffect(SettingsJson);
12
+const encodeSettings = Schema.encodeEffect(SettingsJson);
13
+
14
+const threadRuntimeSettings = Effect.gen(function* () {
15
+ const fs = yield* FileSystem.FileSystem;
16
+ const settingsPath = (yield* Path.Path).join(T3_HOME, "userdata/settings.json");
17
+ const original = yield* fs.readFileString(settingsPath);
18
+ const settings = yield* decodeSettings(original);
19
+ return {
20
+ supervise: fs.writeFileString(
21
+ settingsPath,
22
+ yield* encodeSettings({ ...settings, defaultRuntimeMode: "approval-required" }),
23
+ ),
24
+ restore: fs.writeFileString(settingsPath, original),
25
+ };
26
+});
27
+
28
+test(
29
+ "An unsaved queued edit keeps its thread mention when the queued run starts",
30
+ WEB,
31
+ async ({ t3, app, screen }) => {
32
+ const settings = await runPromise(threadRuntimeSettings);
33
+ try {
34
+ // Supervised mode makes the approval recording wait for an answer, which holds its turn open.
35
+ await runPromise(settings.supervise);
36
+ const project = await t3.project("demo");
37
+ await t3.thread({ project, title: "Release notes", messages: ["Draft the notes"] });
38
+ const thread = await t3.thread({
39
+ project,
40
+ title: "Waiting work",
41
+ messages: ["Change the file"],
42
+ model: "approval",
43
+ });
44
+ const { cli } = makeState(T3_HOME);
45
+ await runPromise(cli(["thread", "send", thread.threadId, "Queued message"]));
46
+ await t3.open(thread);
47
+
48
+ await screen.getByRole("button", "Edit queued message").tap();
49
+ const composer = screen.getByRole("textbox");
50
+ await composer.press("End");
51
+ await composer.pressSequentially(" @Release");
52
+ await screen.getByRole("option", "Release notes", { exact: false }).tap();
53
+ await expect(composer).toContainText("Release notes");
54
+
55
+ await screen.getByRole("button", "Approve", { exact: true }).tap();
56
+ await expect(screen.getByText("was kept in the composer", { exact: false })).toBeVisible();
57
+ await expect(screen.getByRole("button", "Cancel editing queued message")).not.toBeVisible();
58
+ await app.screenshot("recovered-edit");
59
+ await expect(composer).toContainText("Release notes");
60
+ } finally {
61
+ await runPromise(settings.restore);
62
+ }
63
+ },
64
+);