diff --git a/ui/src/components/IssueChatThread.test.tsx b/ui/src/components/IssueChatThread.test.tsx index 52112cd04ae..90dc9165d8e 100644 --- a/ui/src/components/IssueChatThread.test.tsx +++ b/ui/src/components/IssueChatThread.test.tsx @@ -3003,7 +3003,8 @@ describe("IssueChatThread", () => { } finally { act(() => root.unmount()); client.clear(); } }); - it("resends an unconfirmed comment with the same request ID and keeps the next draft", async () => { + it.each(["secure context", "plain HTTP"])("resends an unconfirmed comment with the same request ID and keeps the next draft on %s", async (context) => { + if (context === "plain HTTP") vi.stubGlobal("crypto", { getRandomValues: crypto.getRandomValues.bind(crypto) }); const key = "resend-unconfirmed-comment"; let rejectSend!: (error: Error) => void; const onAdd = vi.fn() @@ -3026,6 +3027,7 @@ describe("IssueChatThread", () => { type("Typed during the outage"); await act(async () => button("Send").click()); type("Next thought"); + expect(onAdd).toHaveBeenCalledOnce(); await act(async () => rejectSend(new CommentSubmissionUnknownError())); expect(container.textContent).toContain("Sending when reconnected…"); expect(JSON.parse(localStorage.getItem(`${key}:submission:v1`)!).request.body).toBe("Typed during the outage"); @@ -3038,6 +3040,7 @@ describe("IssueChatThread", () => { expect(localStorage.getItem(`${key}:submission:v1`)).toBeNull(); } finally { await act(async () => root.unmount()); + vi.unstubAllGlobals(); } }); diff --git a/ui/src/components/IssueChatThread.tsx b/ui/src/components/IssueChatThread.tsx index be1a04a9822..85fa7270006 100644 --- a/ui/src/components/IssueChatThread.tsx +++ b/ui/src/components/IssueChatThread.tsx @@ -1,3 +1,4 @@ +import { createUuid } from "@/lib/uuid"; import { useCurrentAccountGuard } from "../hooks/useAccountBoundMutation"; import { resolveIssueChatHumanAuthor } from "@/lib/issue-chat-human-author"; import { useCurrentWorkspaceRetry, useWorkspaceBaseRefRecovery, useWorkspaceRetry } from "./WorkspaceBaseRefRecovery"; @@ -5182,7 +5183,7 @@ const IssueChatComposer = forwardRef< setBody(trimmed); return; } - attemptId = crypto.randomUUID(); + attemptId = createUuid(); if (requestOwner === undefined) markRequestUnbound(attemptId); // The exact request is stored before it is sent, so a lost response or // an outage can resend it with the same ID instead of losing it. @@ -6229,7 +6230,7 @@ export function IssueChatThread({ const sendComposerComment = useCallback( async (body, reopen, reassignment, attachmentIds, clientRequestId, runSettings) => { pendingSubmitScrollRef.current = true; - clientRequestId ??= crypto.randomUUID(); + clientRequestId ??= createUuid(); const attempt = beginSubmission(clientRequestId, undefined, reopen); try { await (runSettings diff --git a/ui/src/components/task-chat/TaskChatComposer.test.tsx b/ui/src/components/task-chat/TaskChatComposer.test.tsx index 098bd5715e8..157b984ab88 100644 --- a/ui/src/components/task-chat/TaskChatComposer.test.tsx +++ b/ui/src/components/task-chat/TaskChatComposer.test.tsx @@ -312,6 +312,65 @@ function renderComposer(props: ComponentProps) { } describe("TaskChatComposer", () => { + describe("plain HTTP without randomUUID", () => { + beforeEach(() => { + vi.stubGlobal("crypto", { getRandomValues: crypto.getRandomValues.bind(crypto) }); + }); + afterEach(() => vi.unstubAllGlobals()); + + it.each([false, true])("sends repeated messages with distinct receipts (conversation=%s)", async (conversationMode) => { + const onAdd = vi.fn().mockResolvedValue(undefined); + renderComposer({ onAdd, conversationMode, workMode: "standard", draftKey: "http-repeated" }); + for (let turn = 0; turn < 5; turn++) { + typeText(`Message ${turn}`); + if (turn % 2) pressKey("Enter", { metaKey: true }); + else flushSync(() => sendButton().click()); + await flushAsync(); + expect(onAdd).toHaveBeenCalledTimes(turn + 1); + expect(onAdd.mock.calls[turn][0]).toBe(`Message ${turn}`); + expect(onAdd.mock.calls[turn][4]).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/); + expect(editable().textContent).toBe(""); + expect(loadDraftSubmission("http-repeated")).toBeNull(); + } + expect(new Set(onAdd.mock.calls.map(call => call[4])).size).toBe(5); + }); + + it("keeps the request identity and next draft after a lost receipt", async () => { + let rejectSend!: (error: Error) => void; + const onAdd = vi.fn() + .mockReturnValueOnce(new Promise((_, reject) => { rejectSend = reject; })) + .mockResolvedValue(undefined); + renderComposer({ onAdd, workMode: "standard", draftKey: "http-retry" }); + typeText("Keep this request"); + await act(async () => sendButton().click()); + expect(onAdd).toHaveBeenCalledOnce(); + const attemptId = onAdd.mock.calls[0][4]; + expect(loadDraftSubmission("http-retry")?.attemptId).toBe(attemptId); + typeText("Next draft"); + await act(async () => rejectSend(new CommentSubmissionUnknownError())); + const resend = Array.from(container.querySelectorAll("button")).find(button => button.textContent === "Resend now")!; + await act(async () => resend.click()); + expect(onAdd).toHaveBeenNthCalledWith(2, "Keep this request", undefined, undefined, undefined, attemptId); + expect(editable().textContent).toBe("Next draft"); + expect(loadDraftSubmission("http-retry")).toBeNull(); + }); + + it("uploads an image and submits its receipt on plain HTTP", async () => { + const onAdd = vi.fn().mockResolvedValue(undefined); + const attachmentId = "cdd72566-48f4-4996-8899-285101990a30"; + const contentPath = `/api/attachments/${attachmentId}/content`; + const onAttachImage = vi.fn().mockResolvedValue({ id: attachmentId, contentPath }); + renderComposer({ onAdd, onAttachImage, workMode: "standard" }); + typeText("Inspect this image"); + pasteFiles([new File(["synthetic image"], "sample.png", { type: "image/png" })]); + await flushAsync(); + expect(onAttachImage).toHaveBeenCalledOnce(); + expect(sendButton().disabled).toBe(false); + await act(async () => sendButton().click()); + expect(onAdd).toHaveBeenCalledWith(`Inspect this image\n\n![sample.png](${contentPath})`, undefined, undefined, [attachmentId], expect.any(String)); + }); + }); + it("stages creation images above the editor without changing the text", async () => { const creation = { value: "Before the screenshot", diff --git a/ui/src/components/task-chat/TaskChatComposer.tsx b/ui/src/components/task-chat/TaskChatComposer.tsx index 98a6e57ddb4..54898e6682d 100644 --- a/ui/src/components/task-chat/TaskChatComposer.tsx +++ b/ui/src/components/task-chat/TaskChatComposer.tsx @@ -1,3 +1,4 @@ +import { createUuid } from "@/lib/uuid"; import { useCurrentAccountGuard } from "@/hooks/useAccountBoundMutation"; import { ComposerFileDropTarget } from "./ComposerFileDropTarget"; import { AgentAvatar } from "../AgentAvatar"; @@ -721,7 +722,7 @@ export function TaskChatComposer({ } async function uploadImage(file: File): Promise { - const id = crypto.randomUUID(); + const id = createUuid(); setAttachments((prev) => [ ...prev, { @@ -1076,7 +1077,7 @@ export function TaskChatComposer({ setBody(submittedBody); return; } - attemptId = crypto.randomUUID(); + attemptId = createUuid(); if (requestOwner === undefined) markRequestUnbound(attemptId); // IDs come only from this composer's upload receipts, never from parsing // arbitrary Markdown. A removed inline image no longer selects its receipt. diff --git a/ui/src/pages/IssueDetail.test.tsx b/ui/src/pages/IssueDetail.test.tsx index a56c77ef3d8..a333fccfa6b 100644 --- a/ui/src/pages/IssueDetail.test.tsx +++ b/ui/src/pages/IssueDetail.test.tsx @@ -1532,6 +1532,7 @@ describe("IssueDetail", () => { }); afterEach(async () => { + vi.unstubAllGlobals(); await act(async () => { root.unmount(); }); @@ -2090,7 +2091,8 @@ describe("IssueDetail", () => { }); - it.each(["message", "attachment"])("creates an unused conversation only for the first %s and updates its canonical cache", async (kind) => { + it.each(["message", "attachment", "plain HTTP message"])("creates an unused conversation only for the first %s and updates its canonical cache", async (kind) => { + if (kind === "plain HTTP message") vi.stubGlobal("crypto", { getRandomValues: crypto.getRandomValues.bind(crypto) }); mockIssuesApi.markRead.mockClear(); const agent = createAgent(); const canonical = createIssue({ conversationAgentId: agent.id, conversationUserId: "user-1", conversationState: "waiting", status: "in_review" }); @@ -2108,7 +2110,7 @@ describe("IssueDetail", () => { onAdd: (body: string) => Promise; onAttachImage: (file: File) => Promise; }; - if (kind === "message") { + if (kind !== "attachment") { await act(async () => { await props.onAdd("Clarify this goal"); }); expect(mockIssuesApi.addComment).toHaveBeenCalledWith(canonical.id, "Clarify this goal", undefined, undefined, undefined, expect.any(String)); expect(invalidate).toHaveBeenCalledWith({ queryKey: queryKeys.issues.comments(canonical.id) }); diff --git a/ui/src/pages/IssueDetail.tsx b/ui/src/pages/IssueDetail.tsx index ae519fc132a..f3223a435c9 100644 --- a/ui/src/pages/IssueDetail.tsx +++ b/ui/src/pages/IssueDetail.tsx @@ -1,3 +1,4 @@ +import { createUuid } from "@/lib/uuid"; import { companyProjectsQuery } from "@/lib/company-projects-query"; import { NativeControllerContainmentRecovery } from "../components/IssueRecoveryActionCard"; import { useIssueIdBatches } from "@/hooks/useIssueIdBatches"; @@ -5116,7 +5117,7 @@ export function TaskDetailSurface({ conversation, tasksTab }: { tasksTab?: TaskS assertCurrentAccount(); queryClient.setQueryData(queryKeys.issues.detail(writableIssueId), updated); } - return issuesApi.addComment(writableIssueId, body, reopen, interrupt, attachmentIds, clientRequestId ?? crypto.randomUUID()); + return issuesApi.addComment(writableIssueId, body, reopen, interrupt, attachmentIds, clientRequestId ?? createUuid()); }, onMutate: async ({ body, reopen, interrupt }) => { // Start cache cancellation immediately but do not put it in front of the