The best way to control Claude Code, Codex and more from any computer or phone
fix(web): keep thread mentions when editing sent messages (#37183)
Wout Stiens committed
Nov 15, 2026 at 13:17 UTC
8897a6ebe194b2d22955cda8936f519c484b35a9
4 files changed
+122
-7
apps/web/src/components/ChatView.logic.test.ts
+77
@@ -1,3 +1,11 @@
1
+import { rewindToMessage } from "./chat/chatViewSend";
2
+import * as session from "../state/session";
3
+import {
4
+ environmentContextRecord,
5
+ environmentContextReference,
6
+ threadContextReference,
7
+} from "../lib/composerContextRecords";
8
+import { threadContextRecord } from "@t3tools/shared/composerContextReferences";
9
import {
10
prepareRevertedMessageAttachments,
11
boundAttachmentIds,
@@ -909,6 +917,75 @@ describe("waitForRevertedMessage", () => {
917
918
afterEach(() => vi.restoreAllMocks());
919
920
+ it("restores the rewound message's thread and machine mentions beside the existing draft", async () => {
921
+ const { state, projection } = projectionAtom();
922
+ vi.spyOn(session, "readPreparedConnection").mockReturnValue({
923
+ httpBaseUrl: "http://localhost",
924
+ } as never);
925
+ useComposerDraftStore.setState({ draftsByThreadKey: {}, draftThreadsByThreadKey: {} });
926
+ const thread = (id: string, title: string) =>
927
+ threadContextRecord(
928
+ { environmentId: threadRef.environmentId, threadId: ThreadId.make(id) },
929
+ title,
930
+ );
931
+ const drafted = thread("drafted-thread", "Drafted thread");
932
+ const mentioned = thread("referenced-thread", "Other thread");
933
+ const machine = environmentContextRecord(EnvironmentId.make("env-2"), "Build box");
934
+ useComposerDraftStore.getState().addThreadContexts(threadRef, [drafted]);
935
+ const draftPrompt = useComposerDraftStore.getState().getComposerDraft(threadRef)!.prompt;
936
+ const sentPrompt = ensureInlineContextReferences("Review", [
937
+ threadContextReference(mentioned),
938
+ environmentContextReference(machine),
939
+ ]).trim();
940
+
941
+ const result = await rewindToMessage({
942
+ message: {
943
+ id: messageId,
944
+ role: "user",
945
+ runId: projection.messages[0]!.runId,
946
+ streaming: false,
947
+ createdAt: "2026-03-29T00:00:10.000Z",
948
+ updatedAt: "2026-03-29T00:00:10.000Z",
949
+ text: sentPrompt,
950
+ context: { version: 1, records: [mentioned, machine] },
951
+ },
952
+ environmentId: threadRef.environmentId,
953
+ routeThreadRef: threadRef,
954
+ threadId: threadRef.threadId,
955
+ turnCount: 1,
956
+ restoreFiles: false,
957
+ checkpoint: undefined,
958
+ draftTarget: threadRef,
959
+ composerHasPendingAttachments: false,
960
+ createAssetUrl: async () => {
961
+ throw new Error("No attachment expected");
962
+ },
963
+ revertThreadCheckpoint: async () => {
964
+ appAtomRegistry.set(state, {
965
+ data: Option.some({
966
+ ...projection,
967
+ providerThreads: projection.providerThreads.map((providerThread) => ({
968
+ ...providerThread,
969
+ lastRunOrdinal: 1,
970
+ })),
971
+ runs: projection.runs.map((run) =>
972
+ run.ordinal > 1 ? { ...run, status: "rolled_back" as const } : run,
973
+ ),
974
+ }),
975
+ });
976
+ return AsyncResult.success(undefined);
977
+ },
978
+ });
979
+
980
+ const prompt = `${draftPrompt}\n\n${sentPrompt}`;
981
+ expect(result).toEqual({ error: null, prompt });
982
+ expect(useComposerDraftStore.getState().getComposerDraft(threadRef)).toMatchObject({
983
+ prompt,
984
+ threadContexts: [drafted, mentioned],
985
+ environmentContexts: [machine],
986
+ });
987
+ });
988
+
989
it("rejects with the projected reason when the rollback fails for good", async () => {
990
const { state, projection } = projectionAtom();
991
const waiting = waitForRevertedMessage(threadRef, 1, requestId, async () => {});
apps/web/src/components/chat/chatViewSend.ts
+9
-4
@@ -46,7 +46,7 @@ import {
46
waitForRevertedMessage,
47
} from "../ChatView.logic";
48
import { readPreparedConnection } from "../../state/session";
49
-import { recallableComposerPrompt } from "./composerPromptHistory";
49
+import { recallComposerMessage } from "./composerPromptHistory";
50
51
// Kept per route so a send in flight, and the multiple-model selections, survive
52
// ChatView remounting while the user navigates.
@@ -151,8 +151,8 @@ export async function startFanoutThread(input: {
151
}
152
153
/**
154
- * Reverts the thread to before `message` and puts that message's text and
155
- * attachments back in the composer draft. Returns the restored prompt, or the
154
+ * Reverts the thread to before `message` and puts that message's text, mentions
155
+ * and attachments back in the composer draft. Returns the restored prompt, or the
156
* error to show; on error the draft is untouched.
157
*/
158
export async function rewindToMessage(input: {
@@ -221,7 +221,7 @@ export async function rewindToMessage(input: {
221
if (result._tag === "Failure") throw squashAtomCommandFailure(result);
222
});
223
const currentPrompt = store.getComposerDraft(draftTarget)?.prompt ?? "";
224
- const restoredPrompt = recallableComposerPrompt(message.text);
224
+ const { prompt: restoredPrompt, mentions } = recallComposerMessage(message);
225
const nextPrompt =
226
restoredPrompt.length === 0
227
? currentPrompt
@@ -229,6 +229,11 @@ export async function rewindToMessage(input: {
229
? `${currentPrompt}\n\n${restoredPrompt}`
230
: restoredPrompt;
231
store.setPrompt(draftTarget, nextPrompt);
232
+ // The restored prompt already carries the chips; these add only their records.
233
+ store.addThreadContexts(draftTarget, mentions.threadContexts, { appendReference: false });
234
+ store.addEnvironmentContexts(draftTarget, mentions.environmentContexts, {
235
+ appendReference: false,
236
+ });
237
const images: ComposerImageAttachment[] = [];
238
const restoredFiles: ComposerFileAttachment[] = [];
239
files.forEach((file, index) => {
apps/web/src/components/chat/composerPromptHistory.ts
+4
-3
@@ -141,8 +141,9 @@ export function recallableComposerPrompt(
141
return trimmed;
142
}
143
144
-function recallMessage(
145
- message: ComposerPromptHistoryMessage,
144
+/** A sent message's text as the composer can hold it, with the mention records its chips need. */
145
+export function recallComposerMessage(
146
+ message: Pick<ComposerPromptHistoryMessage, "text" | "context">,
147
): Pick<ComposerPromptHistoryEntry, "prompt" | "mentions"> {
148
const records = message.context?.records ?? [];
149
const threadRecords = records.filter(isThreadContextRecord);
@@ -172,7 +173,7 @@ export function buildComposerPromptHistoryEntries(
173
if (message.role !== "user") continue;
174
// Automations and other agents send user-role messages; recall only what the user typed.
175
if (resolveUserMessagePresentation(message).attribution !== null) continue;
175
- const { prompt, mentions } = recallMessage(message);
176
+ const { prompt, mentions } = recallComposerMessage(message);
177
if (prompt.length === 0) continue;
178
const entry = { id: message.id, prompt, mentions };
179
const previous = entries[entries.length - 1];
scripts/scenarios/edit-from-here-keeps-thread-mention.e2e.ts
new
+32
@@ -0,0 +1,32 @@
1
+import { expect } from "e2e";
2
+
3
+import { test, WEB } from "./t3.ts";
4
+
5
+test(
6
+ "Edit from here puts a sent message back with its thread mention",
7
+ WEB,
8
+ async ({ t3, app, screen, browser }) => {
9
+ const project = await t3.project("demo");
10
+ await t3.thread({ project, title: "Release notes", messages: ["Draft the notes"] });
11
+ const thread = await t3.thread({ project, title: "Review work", messages: ["Start"] });
12
+ await t3.open(thread);
13
+
14
+ const composer = screen.getByRole("textbox");
15
+ await composer.pressSequentially("Review @Release");
16
+ await screen.getByRole("option", "Release notes", { exact: false }).tap();
17
+ await expect(composer).toContainText("Release notes");
18
+ await browser.keyboard.press("Enter");
19
+
20
+ // The seeded message came from the CLI, which the timeline attributes to an agent: only the
21
+ // message sent here offers Edit from here.
22
+ const editFromHere = screen.getByRole("button", "Edit from here");
23
+ await expect(editFromHere).toHaveCount(1, { timeout: 60_000 });
24
+ await expect(editFromHere).toBeEnabled({ timeout: 60_000 });
25
+ await editFromHere.tap();
26
+ await screen.getByRole("button", "Revert and keep changes").tap();
27
+
28
+ await expect(composer).toContainText("Review");
29
+ await app.screenshot("restored-draft");
30
+ await expect(composer).toContainText("Release notes");
31
+ },
32
+);