Skip to content

Commit a3e4564

Browse files
fix(web): queued edits seed only referenced mentions and drop the draft on recovery
Opening a queued-message edit seeded every thread and machine record from the message context, and the store re-inserted any record the text did not reference, bringing back removed mentions and making an untouched edit look changed. Seed only records whose reference appears in the message text. When the run left the queue and the edit was kept, moving it to the thread draft left the edit draft's mention records behind, so the queued-edit draft persisted forever. Clear the edit draft after the move too. AI-assisted; verified with the tests and tools listed above.
1 parent a2b706e commit a3e4564

4 files changed

Lines changed: 44 additions & 9 deletions

File tree

‎apps/web/src/components/chat/queuedMessageEdit.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
22
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
3+
import { threadContextRecord } from "@t3tools/shared/composerContextReferences";
34
import { beforeEach, describe, expect, it } from "vite-plus/test";
45
import {
56
DraftId,
@@ -86,6 +87,18 @@ describe("queued message file edits", () => {
8687
expect(store.getComposerDraft(editTarget)).toBeNull();
8788
});
8889

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+
89102
it("does not overwrite a separate draft when the queued run leaves", () => {
90103
const store = useComposerDraftStore.getState();
91104
store.setPrompt(threadTarget, "Separate draft");

‎apps/web/src/components/chat/queuedMessageEdit.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ export function recoverQueuedMessageEdit(input: {
2323
});
2424
if (recovery === "kept") {
2525
store.moveComposerPromptAndImages(input.editTarget, input.threadTarget);
26-
} else {
27-
store.clearComposerContent(input.editTarget);
2826
}
27+
store.clearComposerContent(input.editTarget);
2928
return recovery;
3029
}
3130

‎apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import { toastManager } from "../ui/toast";
1818
import { act, render } from "../../../test/render";
1919
import { useComposerDraftStore, useComposerThreadDraft } from "../../composerDraftStore";
2020
import { formatInlineContextReference } from "../../lib/composerContextReferences";
21-
import { threadContextReference } from "../../lib/composerContextRecords";
21+
import {
22+
environmentContextRecord,
23+
environmentContextReference,
24+
threadContextReference,
25+
} from "../../lib/composerContextRecords";
2226
import { deriveComposerSendState } from "../ChatView.logic";
2327
import { ComposerPrimaryActions } from "./ComposerPrimaryActions";
2428
import { AppAtomRegistryProvider, appAtomRegistry } from "../../rpc/atomRegistry";
@@ -276,19 +280,36 @@ describe("useQueuedRunEdit", () => {
276280
);
277281
});
278282

279-
it("opens an edit with the thread records its mentions refer to", async () => {
283+
it("opens an edit with the thread and machine records its mentions refer to", async () => {
284+
const { hook } = setup();
285+
const thread = threadContextRecord(THREAD_B, "Support Any File Uploads");
286+
const machine = environmentContextRecord(ENV, "Build box");
287+
const messageId = MessageId.make("message-1");
288+
const text = `wait for ${formatInlineContextReference(threadContextReference(thread))} on ${formatInlineContextReference(environmentContextReference(machine))}`;
289+
const messages = [{ id: messageId, text, context: { version: 1, records: [thread, machine] } }];
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("opens an edit without records the message text no longer mentions", async () => {
280301
const { hook } = setup();
281-
const mentioned = threadContextRecord(THREAD_B, "Support Any File Uploads");
302+
const unreferenced = threadContextRecord(THREAD_B, "Removed earlier");
282303
const messageId = MessageId.make("message-1");
283-
const text = `wait for ${formatInlineContextReference(threadContextReference(mentioned))} to merge`;
284-
const messages = [{ id: messageId, text, context: { version: 1, records: [mentioned] } }];
304+
const text = "plain text";
305+
const messages = [{ id: messageId, text, context: { version: 1, records: [unreferenced] } }];
285306
await using _screen = await render(view(THREAD_A, "queued", hook, messages));
286307

287308
await act(() => requireHook(hook).beginEdit({ runId: RUN, messageId, text, attachments: [] }));
288309

289310
const draft = useComposerDraftStore.getState().getComposerDraft(requireHook(hook).draftTarget);
290311
expect(draft?.prompt).toBe(text);
291-
expect(draft?.threadContexts).toEqual([mentioned]);
312+
expect(draft?.threadContexts).toEqual([]);
292313
});
293314

294315
it("saves only the thread mentions still in the edited message", async () => {

‎apps/web/src/components/chat/useQueuedRunEdit.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
startAttachmentUpload,
4242
} from "../../lib/attachmentUploadQueue";
4343
import { buildMessageContext } from "../../lib/composerContextRecords";
44+
import { collectInlineContextIds } from "../../lib/composerContextReferences";
4445
import { readFileAsDataUrl } from "../../lib/imageCompression";
4546
import type { TerminalContextDraft } from "../../lib/terminalContext";
4647
import type { ReviewCommentContext } from "../../reviewCommentContext";
@@ -240,7 +241,8 @@ export function useQueuedRunEdit(input: {
240241
const context = serverProjection?.messages.find(
241242
(message) => message.id === request.messageId,
242243
)?.context;
243-
const records = context?.records ?? [];
244+
const referenced = new Set(collectInlineContextIds(text));
245+
const records = (context?.records ?? []).filter((record) => referenced.has(record.contextId));
244246
const store = useComposerDraftStore.getState();
245247
store.clearComposerContent(target);
246248
store.setPrompt(target, text);

0 commit comments

Comments
 (0)