Skip to content

Commit 8f153b0

Browse files
fix(web): keep queued-message edit undo history separate from the thread draft
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent dcf2740 commit 8f153b0

3 files changed

Lines changed: 75 additions & 1 deletion

File tree

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ import {
113113
composerFileNeedsReattach,
114114
composerDraftHasUserContent,
115115
composerTargetKey,
116+
composerUndoHistoryKey,
116117
hydrateImagesFromPersisted,
117118
useComposerDraftStore,
118119
useComposerThreadDraft,
@@ -7334,7 +7335,15 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
73347335
)
73357336
: undefined
73367337
}
7337-
historyScopeKey={composerTargetKey(questionAttachmentTarget ?? routeThreadRef)}
7338+
historyScopeKey={
7339+
questionAttachmentTarget
7340+
? composerTargetKey(questionAttachmentTarget)
7341+
: composerUndoHistoryKey({
7342+
routeThreadRef,
7343+
routeDraftId: draftId,
7344+
composerDraftTarget,
7345+
})
7346+
}
73387347
editorRef={composerEditorRef}
73397348
richTextEnabled={settings.composerRichTextEnabled}
73407349
value={

‎apps/web/src/composerDraftStore.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import {
7676
type ComposerFileAttachment,
7777
type ComposerImageAttachment,
7878
composerFileNeedsReattach,
79+
composerUndoHistoryKey,
7980
partializeComposerDraftStoreState,
8081
useComposerDraftStore,
8182
DraftId,
@@ -363,6 +364,54 @@ describe("composerDraftStore addImages", () => {
363364
});
364365
});
365366

367+
describe("composerUndoHistoryKey", () => {
368+
const routeThreadRef = scopeThreadRef(TEST_ENVIRONMENT_ID, ThreadId.make("thread-undo"));
369+
const routeDraftId = DraftId.make("draft-undo");
370+
const queuedEdit = DraftId.make("queued-edit:thread-undo:run-1");
371+
372+
it("separates a queued-message edit from the thread draft", () => {
373+
const threadKey = composerUndoHistoryKey({
374+
routeThreadRef,
375+
routeDraftId: null,
376+
composerDraftTarget: routeThreadRef,
377+
});
378+
const queuedEditKey = composerUndoHistoryKey({
379+
routeThreadRef,
380+
routeDraftId: null,
381+
composerDraftTarget: queuedEdit,
382+
});
383+
expect(queuedEditKey).not.toBe(threadKey);
384+
});
385+
386+
it("separates a queued-message edit from a draft route that already has a server thread", () => {
387+
const draftKey = composerUndoHistoryKey({
388+
routeThreadRef,
389+
routeDraftId,
390+
composerDraftTarget: routeDraftId,
391+
});
392+
const queuedEditKey = composerUndoHistoryKey({
393+
routeThreadRef,
394+
routeDraftId,
395+
composerDraftTarget: queuedEdit,
396+
});
397+
expect(queuedEditKey).not.toBe(draftKey);
398+
});
399+
400+
it("keeps a draft's history when it is promoted to a server thread", () => {
401+
const draftKey = composerUndoHistoryKey({
402+
routeThreadRef,
403+
routeDraftId,
404+
composerDraftTarget: routeDraftId,
405+
});
406+
const promotedKey = composerUndoHistoryKey({
407+
routeThreadRef,
408+
routeDraftId: null,
409+
composerDraftTarget: routeThreadRef,
410+
});
411+
expect(promotedKey).toBe(draftKey);
412+
});
413+
});
414+
366415
describe("composerDraftStore clearComposerContent", () => {
367416
const threadId = ThreadId.make("thread-clear");
368417
const threadRef = scopeThreadRef(TEST_ENVIRONMENT_ID, threadId);

‎apps/web/src/composerDraftStore.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,22 @@ export function composerTargetKey(target: ScopedThreadRef | DraftId): string {
14631463
return scopedThreadKey(target);
14641464
}
14651465

1466+
/**
1467+
* Composer editor undo identity. A route's own draft keys by the route thread,
1468+
* so a promoted draft keeps its history. Only a queued-message edit retargets
1469+
* the composer to another draft, and it gets its own history.
1470+
*/
1471+
export function composerUndoHistoryKey(input: {
1472+
routeThreadRef: ScopedThreadRef;
1473+
routeDraftId: DraftId | null;
1474+
composerDraftTarget: ScopedThreadRef | DraftId;
1475+
}): string {
1476+
const editsQueuedMessage =
1477+
typeof input.composerDraftTarget === "string" &&
1478+
input.composerDraftTarget !== input.routeDraftId;
1479+
return composerTargetKey(editsQueuedMessage ? input.composerDraftTarget : input.routeThreadRef);
1480+
}
1481+
14661482
/**
14671483
* Legacy persisted data may still be keyed by a raw `ThreadId`. This helper is
14681484
* intentionally migration-only so live code cannot accidentally accept that

0 commit comments

Comments
 (0)