diff --git a/e2e/agentDraftSubmission.spec.ts b/e2e/agentDraftSubmission.spec.ts new file mode 100644 index 000000000..9ef35920b --- /dev/null +++ b/e2e/agentDraftSubmission.spec.ts @@ -0,0 +1,133 @@ +import type { Page } from "@playwright/test"; +import type { AgentTask } from "../src/bindings"; +import { expect, test } from "./fixtures"; + +const task: AgentTask = { + id: "draft-ack", + title: "Draft acknowledgement", + provider: "codex", + status: "ready", + isolation: "local", + model: null, + cwd: "/dev/acme-api", + branch: null, + base: null, + baseCommit: null, + error: null, + createdMs: 0, + updatedMs: 0, + config: [], + commands: [], + usage: null, + queue: [], +}; + +async function prepare(page: Page, home: boolean, failure: boolean) { + await page.evaluate( + async ({ task, home, failure }) => { + const storePath = "/src/features/agents/store.ts"; + const ipcPath = "/src/platform/ipc.ts"; + const store = await import(storePath); + const { commands } = await import(ipcPath); + const provider = { + id: "codex", + name: "Codex", + command: "codex", + program: "codex", + args: [], + defaultProgram: null, + defaultArgs: [], + loginHint: null, + commandOverridden: false, + commandEditable: true, + commandReadOnlyReason: null, + installed: true, + env: {}, + commands: [], + source: { kind: "custom" }, + config: [], + thoughtLevels: {}, + }; + // Delayed local acknowledgements exercise the real editor without starting an agent. + commands.agentProviders = async () => ({ status: "ok", data: [provider] }); + commands.agentProbe = async () => ({ status: "ok", data: null }); + commands.agentChanges = async () => ({ status: "ok", data: [] }); + const acknowledge = (data: unknown, prompt: unknown) => { + document.documentElement.dataset.submittedPrompt = JSON.stringify(prompt); + return new Promise((resolve) => + document.addEventListener( + "test:agent-ack", + () => { + resolve( + failure + ? { status: "error", error: { kind: "internal", message: "Local send failed" } } + : { status: "ok", data }, + ); + }, + { once: true }, + ), + ); + }; + commands.agentCreate = (request: { prompt: unknown }) => acknowledge(task, request.prompt); + commands.agentPrompt = (_id: string, prompt: unknown) => acknowledge(null, prompt); + store.useAgents.setState({ + loaded: true, + providers: [provider], + tasks: new Map([[task.id, task]]), + entries: new Map([[task.id, []]]), + composers: new Map(), + draft: { provider: "codex", config: {}, isolation: "local", base: null }, + sessionLocation: "editor", + }); + if (home) store.openHome(); + else store.openTask(task.id); + }, + { task, home, failure }, + ); +} + +for (const home of [false, true]) { + for (const edited of [false, true]) { + for (const failure of [false, true]) { + test(`${home ? "new task" : "session"}: ${failure ? "failed" : "successful"} acknowledgement with ${edited ? "new typing" : "unchanged draft"}`, async ({ + workbench: page, + }) => { + await prepare(page, home, failure); + const box = page.getByRole("textbox", { name: "Message", exact: true }); + await box.fill("submitted prompt"); + await page.getByRole("button", { name: home ? "Start" : "Send", exact: true }).click(); + await expect(page.locator("html")).toHaveAttribute( + "data-submitted-prompt", + JSON.stringify({ text: "submitted prompt", files: [], images: [] }), + ); + await expect(page.getByRole("button", { name: home ? "Starting…" : "Send", exact: true })).toBeDisabled(); + if (edited) await box.fill("next unsent prompt"); + await page.evaluate(() => document.dispatchEvent(new Event("test:agent-ack"))); + if (home && !failure) { + await expect(page.getByRole("tab", { name: "Draft acknowledgement" })).toBeVisible(); + await page.evaluate(async () => { + const storePath = "/src/features/agents/store.ts"; + (await import(storePath)).openHome(); + }); + } + const expected = edited ? "next unsent prompt" : failure ? "submitted prompt" : ""; + if (expected) await expect(box).toHaveText(expected); + else await expect(box.locator(".cm-placeholder")).toBeVisible(); + await expect + .poll(() => + page.evaluate( + async (key) => { + const storePath = "/src/features/agents/store.ts"; + return (await import(storePath)).composerDraft(key).text; + }, + home ? "home" : task.id, + ), + ) + .toBe(expected); + await expect(page.getByRole("button", { name: home ? "Start" : "Send", exact: true })).toBeEnabled({ + enabled: !!expected, + }); + }); + } + } +} diff --git a/src/features/agents/composerSubmission.test.tsx b/src/features/agents/composerSubmission.test.tsx new file mode 100644 index 000000000..aafa3f97b --- /dev/null +++ b/src/features/agents/composerSubmission.test.tsx @@ -0,0 +1,398 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react"; +import type { AgentProvider, AgentTask, ProjectInfo } from "@/bindings"; +import { useProject } from "@/platform/project"; +import { HomeEditor } from "./HomeEditor"; +import { SessionEditor } from "./SessionEditor"; +import { emptyDraft, type ComposerDraft } from "./composerModel"; +import { setCaret, textOf, typeIn } from "./composerTesting"; +import { HOME_KEY, SESSION_KIND, sessionKey } from "./model"; +import { + HOME, + composerDraft, + connect, + disconnect, + sendMessage, + startTask, + updateComposer, + updateDraft, + useAgents, +} from "./store"; + +const backend = vi.hoisted(() => ({ + tauri: false, + create: vi.fn(), + prompt: vi.fn(), + open: vi.fn(), + close: vi.fn(), +})); +vi.mock("@tauri-apps/api/core", async (original) => ({ + ...(await original()), + Channel: class { + onmessage = () => {}; + }, +})); +vi.mock("@/platform/ipc", async (original) => ({ + ...(await original()), + isTauri: () => backend.tauri, + commands: { + agentCreate: backend.create, + agentPrompt: backend.prompt, + agentSubscribe: async () => ({ status: "ok", data: [] }), + agentProviders: async () => ({ status: "ok", data: [provider] }), + agentProbe: async () => ({ status: "ok", data: null }), + agentEntries: async () => ({ status: "ok", data: [] }), + agentChanges: async () => ({ status: "ok", data: [] }), + vcsBranchInfo: async () => ({ status: "ok", data: { head: "main" } }), + vcsBranches: async () => ({ status: "ok", data: [] }), + }, +})); +vi.mock("@/workbench/contributions", async (original) => ({ + ...(await original()), + editorService: () => ({ openInput: backend.open, closeInput: backend.close }), + maybeEditorService: () => ({ activePath: () => null }), + toolWindowService: () => ({ show: vi.fn() }), +})); + +const ok = (data: T) => ({ status: "ok" as const, data }); +function deferred() { + let resolve!: (value: unknown) => void; + let reject!: (reason: Error) => void; + const promise = new Promise((done, fail) => { + resolve = done; + reject = fail; + }); + return { promise, resolve, reject }; +} +const task = (id: string): AgentTask => ({ + id, + title: id, + provider: "codex", + status: "ready", + isolation: "local", + model: null, + cwd: "/project-a", + branch: null, + base: null, + baseCommit: null, + error: null, + createdMs: 0, + updatedMs: 0, + config: [], + commands: [], + usage: null, + queue: [], +}); +const provider: AgentProvider = { + id: "codex", + name: "Codex", + command: "codex", + program: "codex", + args: [], + defaultProgram: null, + defaultArgs: [], + loginHint: null, + commandOverridden: false, + commandEditable: true, + commandReadOnlyReason: null, + installed: true, + env: {}, + commands: [], + source: { kind: "custom" }, + config: [], + thoughtLevels: {}, +}; +const file = (name: string) => ({ path: `/project-a/${name}`, rel: name, attached: true }); +const image = (name: string) => ({ mimeType: "image/png", data: name, name }); +const mixed = (): ComposerDraft => ({ + text: " submitted prompt ", + files: [file("sent.ts")], + images: [image("sent.png")], +}); +const setProject = (root: string) => useProject.setState({ project: { root, trusted: true } as ProjectInfo }); + +beforeEach(() => { + disconnect(); + vi.resetAllMocks(); + backend.tauri = false; + backend.close.mockResolvedValue(false); + setProject("/project-a"); + useProject.setState({ settings: null }); + useAgents.setState({ + loaded: true, + tasks: new Map([["A", task("A")]]), + entries: new Map([["A", []]]), + providers: [provider], + draft: { provider: "codex", config: {}, isolation: "local", base: null }, + composers: new Map(), + sessionLocation: "editor", + lastComposer: HOME, + history: [], + focus: null, + }); +}); +afterEach(() => { + cleanup(); + disconnect(); +}); + +const paths = [ + { name: "queued message", key: "A", send: () => sendMessage("A", false), mock: backend.prompt }, + { name: "send now", key: "A", send: () => sendMessage("A", true), mock: backend.prompt }, + { name: "new task", key: HOME, send: () => startTask(), mock: backend.create }, +]; + +describe.each(paths)("$name acknowledgement", (path) => { + function begin() { + const response = deferred(); + path.mock.mockReturnValueOnce(response.promise); + const pending = path.send(); + return { ...response, pending }; + } + const success = () => ok(path.key === HOME ? task("created") : null); + + it.each([ + { name: "text", draft: { ...emptyDraft(), text: "hello" } }, + { name: "file only", draft: { ...emptyDraft(), files: [file("sent.ts")] } }, + { name: "image only", draft: { ...emptyDraft(), images: [image("sent.png")] } }, + { name: "text, files and images", draft: mixed() }, + ])("clears unchanged $name after success", async ({ draft }) => { + updateComposer(path.key, draft); + const response = begin(); + expect(path.mock).toHaveBeenCalledTimes(1); + expect(composerDraft(path.key)).toEqual(draft); + response.resolve(success()); + await response.pending; + expect(composerDraft(path.key)).toEqual(emptyDraft()); + }); + + it.each(["", " \n\t "])("does not send an empty draft (%j)", async (text) => { + updateComposer(path.key, { text }); + await path.send(); + expect(path.mock).not.toHaveBeenCalled(); + expect(composerDraft(path.key).text).toBe(text); + }); + + it.each([ + { name: "replace text", patch: { text: "next prompt" } }, + { name: "append text", patch: { text: mixed().text + "and another instruction" } }, + { name: "delete text", patch: { text: "" } }, + { name: "whitespace edit", patch: { text: mixed().text + " " } }, + { name: "add file", patch: { files: [...mixed().files, file("next.ts")] } }, + { name: "remove file", patch: { files: [] } }, + { name: "replace file", patch: { files: [file("next.ts")] } }, + { name: "add image", patch: { images: [...mixed().images, image("next.png")] } }, + { name: "remove image", patch: { images: [] } }, + { name: "replace image", patch: { images: [image("next.png")] } }, + { + name: "replace everything", + patch: { text: "next prompt", files: [file("next.ts")], images: [image("next.png")] }, + }, + ])("keeps the newer draft after $name", async ({ patch }) => { + updateComposer(path.key, mixed()); + const response = begin(); + updateComposer(path.key, patch); + const next = structuredClone(composerDraft(path.key)); + response.resolve(success()); + await response.pending; + expect(composerDraft(path.key)).toEqual(next); + }); + + it("keeps a new revision even when edits restore the submitted content", async () => { + const submitted = mixed(); + updateComposer(path.key, submitted); + const response = begin(); + updateComposer(path.key, { text: "temporary change" }); + updateComposer(path.key, submitted); + response.resolve(success()); + await response.pending; + expect(composerDraft(path.key)).toEqual(submitted); + }); + + it.each([false, true])("keeps current content after a command error (edited=%s)", async (edited) => { + updateComposer(path.key, mixed()); + const response = begin(); + if (edited) updateComposer(path.key, { text: "unsent changes", images: [image("new.png")] }); + const next = structuredClone(composerDraft(path.key)); + const rejected = expect(response.pending).rejects.toThrow("send failed"); + response.resolve({ status: "error", error: { kind: "internal", message: "send failed" } }); + await rejected; + expect(composerDraft(path.key)).toEqual(next); + }); + + it.each([false, true])("keeps current content after a transport error (edited=%s)", async (edited) => { + updateComposer(path.key, mixed()); + const response = begin(); + if (edited) updateComposer(path.key, { files: [file("new.ts")] }); + const next = structuredClone(composerDraft(path.key)); + const rejected = expect(response.pending).rejects.toThrow("connection lost"); + response.reject(new Error("connection lost")); + await rejected; + expect(composerDraft(path.key)).toEqual(next); + }); + + it("clears only the submitted composer and keeps the last edited destination", async () => { + updateComposer(path.key, mixed()); + const response = begin(); + updateComposer("B", { text: "another session", files: [file("other.ts")] }); + response.resolve(success()); + await response.pending; + expect(composerDraft(path.key)).toEqual(emptyDraft()); + expect(composerDraft("B")).toEqual({ text: "another session", files: [file("other.ts")], images: [] }); + expect(useAgents.getState().lastComposer).toBe("B"); + }); + + it("still clears when task status, provider or model settings change", async () => { + updateComposer(path.key, mixed()); + const response = begin(); + updateDraft({ provider: "other", config: { model: "new-model", mode: "plan", effort: "high" } }); + useAgents.setState({ + providers: [], + tasks: new Map([["A", { ...task("A"), status: "running", model: "new-model" }]]), + }); + response.resolve(success()); + await response.pending; + expect(composerDraft(path.key)).toEqual(emptyDraft()); + }); + + it.each(["disconnect", "reconnect", "switch project", "switch away and back"])( + "does not consume a retained draft after %s", + async (change) => { + updateComposer(path.key, mixed()); + const response = begin(); + disconnect(); + if (change.startsWith("switch")) setProject("/project-b"); + if (change === "switch away and back") { + disconnect(); + setProject("/project-a"); + } + if (change !== "disconnect") { + backend.tauri = true; + await connect(); + } + response.resolve(success()); + await response.pending; + expect(composerDraft(path.key)).toEqual(mixed()); + }, + ); + + it("does not recreate a deleted composer", async () => { + updateComposer(path.key, mixed()); + const response = begin(); + useAgents.setState({ composers: new Map() }); + response.resolve(success()); + await response.pending; + expect(useAgents.getState().composers.has(path.key)).toBe(false); + }); + + it("does not consume a retained draft after replacing the connection without disconnecting", async () => { + updateComposer(path.key, mixed()); + const response = begin(); + backend.tauri = true; + await connect(); + response.resolve(success()); + await response.pending; + expect(composerDraft(path.key)).toEqual(mixed()); + }); + + it.each(["old first", "new first"])("handles overlapping submissions with %s", async (order) => { + updateComposer(path.key, { text: "first" }); + const first = begin(); + updateComposer(path.key, { text: "second" }); + const second = begin(); + if (order === "old first") { + first.resolve(success()); + await first.pending; + expect(composerDraft(path.key).text).toBe("second"); + second.resolve(success()); + await second.pending; + } else { + second.resolve(success()); + await second.pending; + updateComposer(path.key, { text: "third" }); + first.resolve(success()); + await first.pending; + } + expect(composerDraft(path.key).text).toBe(order === "old first" ? "" : "third"); + }); + + it("keeps a third draft when both earlier acknowledgements arrive", async () => { + updateComposer(path.key, { text: "first" }); + const first = begin(); + updateComposer(path.key, { text: "second" }); + const second = begin(); + updateComposer(path.key, { text: "third", images: [image("third.png")] }); + first.resolve(success()); + second.resolve(success()); + await Promise.all([first.pending, second.pending]); + expect(composerDraft(path.key)).toEqual({ text: "third", files: [], images: [image("third.png")] }); + }); + + it("sends the submitted payload and remembers only the submitted Home prompt", async () => { + updateComposer(path.key, mixed()); + const response = begin(); + const prompt = { text: "submitted prompt", files: [file("sent.ts").path], images: mixed().images }; + if (path.key === HOME) expect(backend.create).toHaveBeenCalledWith(expect.objectContaining({ prompt })); + else expect(backend.prompt).toHaveBeenCalledWith("A", prompt, path.name === "send now"); + updateComposer(path.key, { text: "not submitted", images: [] }); + response.resolve(success()); + await response.pending; + expect(useAgents.getState().history).toEqual(path.key === HOME ? ["submitted prompt"] : []); + }); +}); + +describe("composer UI during submission", () => { + it("keeps typing and attachments in the session after Send completes", async () => { + const response = deferred(); + backend.prompt.mockReturnValue(response.promise); + render(); + const box = screen.getByRole("textbox", { name: "Message" }); + typeIn(box, "sent message"); + fireEvent.click(screen.getByRole("button", { name: "Send" })); + await waitFor(() => expect(backend.prompt).toHaveBeenCalledTimes(1)); + expect(screen.getByRole("button", { name: "Send" })).toBeDisabled(); + expect(box).toHaveAttribute("contenteditable", "true"); + typeIn(box, "next unsent message"); + act(() => updateComposer("A", { files: [file("next.ts")], images: [image("next.png")] })); + await act(async () => response.resolve(ok(null))); + expect(textOf(box)).toBe("next unsent message"); + expect(composerDraft("A").files).toEqual([file("next.ts")]); + expect(composerDraft("A").images).toEqual([image("next.png")]); + expect(screen.getByRole("button", { name: "Send" })).toBeEnabled(); + }); + + it("clears an unchanged session draft after only moving the caret", async () => { + const response = deferred(); + backend.prompt.mockReturnValue(response.promise); + render(); + const box = screen.getByRole("textbox", { name: "Message" }); + typeIn(box, "sent message"); + fireEvent.click(screen.getByRole("button", { name: "Send" })); + setCaret(box, 2); + await act(async () => response.resolve(ok(null))); + expect(textOf(box)).toBe(""); + }); + + it("keeps the next Home draft when task creation succeeds and opens the created task", async () => { + const response = deferred(); + backend.create.mockReturnValue(response.promise); + backend.close.mockResolvedValue(true); + const home = render(); + const box = screen.getByRole("textbox"); + typeIn(box, "submitted task"); + fireEvent.click(screen.getByRole("button", { name: "Start" })); + await waitFor(() => expect(backend.create).toHaveBeenCalledTimes(1)); + expect(screen.getByRole("button", { name: "Starting…" })).toBeDisabled(); + typeIn(box, "next unsent task"); + await act(async () => response.resolve(ok(task("created")))); + expect(backend.close).toHaveBeenCalledWith(HOME_KEY); + expect(backend.open).toHaveBeenCalledWith( + { kind: SESSION_KIND, key: sessionKey("created"), data: { task: "created", title: "created" } }, + { focus: true }, + ); + home.unmount(); + render(); + expect(textOf(screen.getByRole("textbox"))).toBe("next unsent task"); + expect(useAgents.getState().history).toEqual(["submitted task"]); + }); +}); diff --git a/src/features/agents/store.ts b/src/features/agents/store.ts index 87432e636..0fe53c75d 100644 --- a/src/features/agents/store.ts +++ b/src/features/agents/store.ts @@ -268,6 +268,15 @@ export function updateComposer(key: string, patch: Partial) { })); } +/** An acknowledgement consumes only the submitted revision, in the same project connection. */ +function clearSubmittedComposer(key: string, submitted: ComposerDraft, started: number) { + useAgents.setState((s) => + generation === started && s.composers.get(key) === submitted + ? { composers: new Map(s.composers).set(key, emptyDraft()) } + : s, + ); +} + /** Session tabs on screen; a task can be shown in more than one split. */ const visible = new Map(); const windowFocused = () => document.visibilityState !== "hidden" && document.hasFocus(); @@ -787,6 +796,7 @@ export async function startTask( const { draft, providers } = useAgents.getState(); const composer = composerDraft(HOME); if (!canSend(composer)) return; + const started = generation; const options = draftConfig( providers.find((p) => p.id === draft.provider), draft, @@ -821,7 +831,7 @@ export async function startTask( })); updateDraft({ projectRoot }); remember(composer.text.trim()); - updateComposer(HOME, emptyDraft()); + clearSubmittedComposer(HOME, composer, started); if ((await editorService().closeInput?.(HOME_KEY)) !== false) openTask(task.id); } @@ -927,8 +937,9 @@ function remember(text: string) { export async function sendMessage(task: string, now: boolean): Promise { const composer = composerDraft(task); if (!canSend(composer)) return; + const started = generation; await call(commands.agentPrompt(task, toPrompt(composer), now)); - updateComposer(task, emptyDraft()); + clearSubmittedComposer(task, composer, started); } /** Moves the caret to the end of composer `key`. */