The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 21:00 UTC
fix(web): editing a queued message keeps its thread mention chips (#30889)
Julius Marminge committed
Nov 11, 2026 at 16:05 UTC
7271ea06a26af0c2ee64bba4f18c7fb3b7f22d5a
4 files changed
+108
-12
apps/web/src/components/chat/queuedMessageEdit.test.ts
+13
@@ -1,5 +1,6 @@
1
import { EnvironmentId, ThreadId } 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";
5
import {
6
DraftId,
@@ -86,6 +87,18 @@ describe("queued message file edits", () => {
87
expect(store.getComposerDraft(editTarget)).toBeNull();
88
});
89
90
+ it("removes a kept edit draft that held a thread mention", () => {
91
+ const store = useComposerDraftStore.getState();
92
+ store.setPrompt(editTarget, "Edited message");
93
+ store.setThreadContexts(editTarget, [
94
+ threadContextRecord(scopeThreadRef(environmentId, ThreadId.make("thread:other")), "Other"),
95
+ ]);
96
+ expect(
97
+ recoverQueuedMessageEdit({ editTarget, threadTarget, originalText: "Original message" }),
98
+ ).toBe("kept");
99
+ expect(store.getComposerDraft(editTarget)).toBeNull();
100
+ });
101
+
102
it("reports a removed queued attachment as an edit when the run leaves", () => {
103
const store = useComposerDraftStore.getState();
104
store.setPrompt(editTarget, "Original message");
apps/web/src/components/chat/queuedMessageEdit.ts
+1
-2
@@ -28,9 +28,8 @@ export function recoverQueuedMessageEdit(input: {
28
});
29
if (recovery === "kept") {
30
store.moveComposerPromptAndImages(input.editTarget, input.threadTarget);
31
- } else {
32
- store.clearComposerContent(input.editTarget);
31
}
32
+ store.clearComposerContent(input.editTarget);
33
return recovery;
34
}
35
apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx
+71
-6
@@ -9,11 +9,20 @@ import {
9
} from "@t3tools/contracts";
10
import { useImperativeHandle, type RefObject } from "react";
11
import { describe, expect, it, vi } from "vite-plus/test";
12
-import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences";
12
+import {
13
+ ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,
14
+ threadContextRecord,
15
+} from "@t3tools/shared/composerContextReferences";
16
import { toastManager } from "../ui/toast";
17
18
import { act, render } from "../../../test/render";
19
import { useComposerDraftStore, useComposerThreadDraft } from "../../composerDraftStore";
20
+import { formatInlineContextReference } from "../../lib/composerContextReferences";
21
+import {
22
+ environmentContextRecord,
23
+ environmentContextReference,
24
+ threadContextReference,
25
+} from "../../lib/composerContextRecords";
26
import { deriveComposerSendState } from "../ChatView.logic";
27
import { ComposerPrimaryActions } from "./ComposerPrimaryActions";
28
import { AppAtomRegistryProvider, appAtomRegistry } from "../../rpc/atomRegistry";
@@ -74,8 +83,8 @@ const RUN = RunId.make("run-1");
83
84
type HookResult = ReturnType<typeof useQueuedRunEdit>;
85
77
-function projection(runs: ReadonlyArray<RunStatus>) {
78
- return { runs, messages: [] } as unknown as ThreadStructure;
86
+function projection(runs: ReadonlyArray<RunStatus>, messages: ReadonlyArray<unknown>) {
87
+ return { runs, messages } as unknown as ThreadStructure;
88
}
89
90
function setRuns(ref: ScopedThreadRef, status: string) {
@@ -86,13 +95,14 @@ function Harness(props: {
95
readonly threadRef: ScopedThreadRef;
96
readonly runStatus: string;
97
readonly hookRef: RefObject<HookResult | null>;
98
+ readonly messages: ReadonlyArray<unknown>;
99
}) {
100
const result = useQueuedRunEdit({
101
environmentId: ENV,
102
routeThreadRef: props.threadRef,
103
threadDraftTarget: props.threadRef,
104
activeThreadId: props.threadRef.threadId,
95
- serverProjection: projection([{ id: RUN, status: props.runStatus }]),
105
+ serverProjection: projection([{ id: RUN, status: props.runStatus }], props.messages),
106
promptRef: { current: "" },
107
composerRef: { current: null },
108
});
@@ -124,10 +134,15 @@ function Harness(props: {
134
) : null;
135
}
136
127
-function view(threadRef: ScopedThreadRef, runStatus: string, hook: { current: HookResult | null }) {
137
+function view(
138
+ threadRef: ScopedThreadRef,
139
+ runStatus: string,
140
+ hook: { current: HookResult | null },
141
+ messages: ReadonlyArray<unknown> = [],
142
+) {
143
return (
144
<AppAtomRegistryProvider>
130
- <Harness threadRef={threadRef} runStatus={runStatus} hookRef={hook} />
145
+ <Harness threadRef={threadRef} runStatus={runStatus} hookRef={hook} messages={messages} />
146
</AppAtomRegistryProvider>
147
);
148
}
@@ -262,6 +277,56 @@ describe("useQueuedRunEdit", () => {
277
);
278
});
279
280
+ it("opens an edit with only the thread and machine records its text still mentions", async () => {
281
+ const { hook } = setup();
282
+ const thread = threadContextRecord(THREAD_B, "Support Any File Uploads");
283
+ const machine = environmentContextRecord(ENV, "Build box");
284
+ const unreferenced = threadContextRecord(THREAD_A, "Removed earlier");
285
+ const messageId = MessageId.make("message-1");
286
+ const text = `wait for ${formatInlineContextReference(threadContextReference(thread))} on ${formatInlineContextReference(environmentContextReference(machine))}`;
287
+ const messages = [
288
+ { id: messageId, text, context: { version: 1, records: [thread, machine, unreferenced] } },
289
+ ];
290
+ await using _screen = await render(view(THREAD_A, "queued", hook, messages));
291
+
292
+ await act(() => requireHook(hook).beginEdit({ runId: RUN, messageId, text, attachments: [] }));
293
+
294
+ const draft = useComposerDraftStore.getState().getComposerDraft(requireHook(hook).draftTarget);
295
+ expect(draft?.prompt).toBe(text);
296
+ expect(draft?.threadContexts).toEqual([thread]);
297
+ expect(draft?.environmentContexts).toEqual([machine]);
298
+ });
299
+
300
+ it("saves only the thread mentions still in the edited message", async () => {
301
+ const { hook } = setup();
302
+ mocks.editQueuedRun.mockResolvedValue({ _tag: "Success" });
303
+ const kept = threadContextRecord(THREAD_B, "Kept");
304
+ const removed = threadContextRecord(THREAD_A, "Removed");
305
+ const messageId = MessageId.make("message-1");
306
+ const text = [kept, removed]
307
+ .map((record) => formatInlineContextReference(threadContextReference(record)))
308
+ .join(" and ");
309
+ const messages = [{ id: messageId, text, context: { version: 1, records: [kept, removed] } }];
310
+ await using _screen = await render(view(THREAD_A, "queued", hook, messages));
311
+ await act(() => requireHook(hook).beginEdit({ runId: RUN, messageId, text, attachments: [] }));
312
+ const target = requireHook(hook).draftTarget;
313
+ await act(() => useComposerDraftStore.getState().setThreadContexts(target, [kept]));
314
+
315
+ const draft = useComposerDraftStore.getState().getComposerDraft(target);
316
+ await requireHook(hook).saveEdit(
317
+ { ...savedContent(draft?.prompt ?? ""), threadContexts: draft?.threadContexts ?? [] },
318
+ () => {},
319
+ );
320
+
321
+ expect(mocks.editQueuedRun).toHaveBeenCalledWith(
322
+ expect.objectContaining({
323
+ input: expect.objectContaining({
324
+ edit: expect.objectContaining({ context: { version: 1, records: [kept] } }),
325
+ }),
326
+ }),
327
+ );
328
+ });
329
+
330
it("keeps an open edit when the user leaves the thread and comes back", async () => {
331
const { hook } = setup();
332
await using screen = await render(view(THREAD_A, "queued", hook));
apps/web/src/components/chat/useQueuedRunEdit.ts
+23
-4
@@ -7,6 +7,7 @@ import type { ThreadStructure } from "@t3tools/client-runtime/state/shell";
7
import {
8
PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
9
type ChatAttachment,
10
+ type ComposerContextRecord,
11
type EnvironmentContextRecord,
12
type EnvironmentId,
13
type MessageId,
@@ -40,6 +41,7 @@ import {
41
startAttachmentUpload,
42
} from "../../lib/attachmentUploadQueue";
43
import { buildMessageContext } from "../../lib/composerContextRecords";
44
+import { collectInlineContextIds } from "../../lib/composerContextReferences";
45
import { readFileAsDataUrl } from "../../lib/imageCompression";
46
import type { TerminalContextDraft } from "../../lib/terminalContext";
47
import type { ReviewCommentContext } from "../../reviewCommentContext";
@@ -85,6 +87,9 @@ interface QueuedRunEditContent {
87
readonly environmentContexts: ReadonlyArray<EnvironmentContextRecord>;
88
}
89
90
+const isDraftOwnedRecord = (record: ComposerContextRecord) =>
91
+ record.kind === "thread" || record.kind === "environment";
92
+
93
function queuedEditDraftTarget(routeThreadRef: ScopedThreadRef, runId: RunId) {
94
return DraftId.make(`queued-edit:${scopedThreadKey(routeThreadRef)}:${runId}`);
95
}
@@ -127,10 +132,12 @@ async function submitQueuedRunEdit(input: {
132
readImage: readFileAsDataUrl,
133
uploadFiles,
134
});
135
+ // The edit draft owns thread and machine mentions, so a chip removed while editing is dropped.
136
const keptRecords = (editing.context?.records ?? []).filter(
137
(record) =>
132
- !("attachmentId" in record) ||
133
- editing.existingAttachments.some((attachment) => attachment.id === record.attachmentId),
138
+ !isDraftOwnedRecord(record) &&
139
+ (!("attachmentId" in record) ||
140
+ editing.existingAttachments.some((attachment) => attachment.id === record.attachmentId)),
141
);
142
const newRecords =
143
buildMessageContext({
@@ -233,9 +240,22 @@ export function useQueuedRunEdit(input: {
240
if (editing !== null && editing.runId !== request.runId) clearEditDraft(editing.runId);
241
const target = queuedEditDraftTarget(routeThreadRef, request.runId);
242
const text = stripAttachmentOnlyBootstrapPrompt(request.text);
243
+ const context = serverProjection?.messages.find(
244
+ (message) => message.id === request.messageId,
245
+ )?.context;
246
+ const referenced = new Set(collectInlineContextIds(text));
247
+ const records = (context?.records ?? []).filter((record) => referenced.has(record.contextId));
248
const store = useComposerDraftStore.getState();
249
store.clearComposerContent(target);
250
store.setPrompt(target, text);
251
+ store.setThreadContexts(
252
+ target,
253
+ records.filter((record): record is ThreadContextRecord => record.kind === "thread"),
254
+ );
255
+ store.setEnvironmentContexts(
256
+ target,
257
+ records.filter((record): record is EnvironmentContextRecord => record.kind === "environment"),
258
+ );
259
const threadRef = { environmentId, threadId: activeThreadId };
260
queuedRunEdits.begin(
261
editKey,
@@ -245,8 +265,7 @@ export function useQueuedRunEdit(input: {
265
messageId: request.messageId,
266
originalText: text,
267
existingAttachments: request.attachments,
248
- context: serverProjection?.messages.find((message) => message.id === request.messageId)
249
- ?.context,
268
+ context,
269
},
270
{
271
runs: makeQueuedRunsAtom({