From 7c35ad90b91b07ed469b1e29dc70cc17f78a90e7 Mon Sep 17 00:00:00 2001 From: "paseo-bot[bot]" <266920839+paseo-bot[bot]@users.noreply.github.com> Date: Fri, 13 Nov 2026 02:05:34 +0000 Subject: [PATCH] Start an edited agent draft after its first start failed (#9534) * test(app): reproduce edited draft retries after a failed agent start Resubmitting an agent draft after its start failed reuses the draft ID as the creation key with different inputs, and the daemon rejects it with agent_request_key_conflict (or workspace_request_key_conflict from New workspace). Co-Authored-By: Claude Opus 5.5 * fix(app): start an edited agent draft after its first start failed The draft sent its draft ID as the creation key on every submit. After a failed start the daemon keeps that key's request, so resubmitting an edited draft was rejected with agent_request_key_conflict. The key now names the draft's exact request: a retry resumes it, an edit starts a new one. A retry after New workspace's agent failed to start creates the agent in the workspace that is already ready, instead of replaying workspace provisioning with a different agent. Co-authored-by: michaelmwu <421504+michaelmwu@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: paseo-bot[bot] <266920839+paseo-bot[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 Co-authored-by: michaelmwu <421504+michaelmwu@users.noreply.github.com> --- .../e2e/browser/creation-idempotency.spec.ts | 38 ++++++++++++++- packages/app/e2e/support/helpers/creation.ts | 7 ++- .../src/composer/draft/creation-key.test.ts | 30 ++++++++++++ .../app/src/composer/draft/creation-key.ts | 29 ++++++++++++ .../app/src/composer/draft/workspace-tab.tsx | 10 ++-- .../app/src/screens/new-workspace-screen.tsx | 18 ++----- .../new-workspace/retry-agent-request.test.ts | 47 ------------------- .../new-workspace/retry-agent-request.ts | 28 ----------- .../workspace-draft-submission-store.test.ts | 24 ---------- .../workspace-draft-submission-store.ts | 11 +---- 10 files changed, 114 insertions(+), 128 deletions(-) create mode 100644 packages/app/src/composer/draft/creation-key.test.ts create mode 100644 packages/app/src/composer/draft/creation-key.ts delete mode 100644 packages/app/src/screens/new-workspace/retry-agent-request.test.ts delete mode 100644 packages/app/src/screens/new-workspace/retry-agent-request.ts diff --git a/packages/app/e2e/browser/creation-idempotency.spec.ts b/packages/app/e2e/browser/creation-idempotency.spec.ts index 7658addf7..8de9f40fb 100644 --- a/packages/app/e2e/browser/creation-idempotency.spec.ts +++ b/packages/app/e2e/browser/creation-idempotency.spec.ts @@ -127,8 +127,42 @@ for (const scenario of [ await creation.submitPrompt("Retry this workspace and agent together."); await creation.expectAgentCount(1); }); + + test(`an edited prompt after failed agent initialization starts the agent in its workspace${scenario.suffix}`, async ({ + creation, + startup, + page, + }) => { + await creation.openWorkspaceForm("local"); + await creation.submitPrompt("Start the original task.", "Create"); + await creation.expectAgentStillStarting(); + await startup.fail(); + await creation.expectStartupFailure(); + await creation.expectOneCreatedWorkspace(); + await scenario.prepare(creation); + await creation.submitPrompt("Start the edited task."); + await creation.expectPromptVisible("Start the edited task."); + await creation.expectOneCreatedWorkspace(); + await creation.expectAgentCount(1); + await expect(page.getByText(/request_key_conflict/)).toHaveCount(0); + }); } +test("an edited agent draft starts after its first start failed", async ({ + creation, + startup, + page, +}) => { + await creation.openAgentDraft(); + await startup.fail(); + await creation.submitPrompt("Start the original task."); + await creation.expectStartupFailure(); + await creation.submitPrompt("Start the edited task."); + await creation.expectPromptVisible("Start the edited task."); + await creation.expectAgentCount(1); + await expect(page.getByText(/request_key_conflict/)).toHaveCount(0); +}); + for (const entry of [ { name: "agent draft", @@ -159,7 +193,9 @@ for (const entry of [ await entry.settled(creation); await creation.expectCreatedAgentError(); await creation.expectPromptBeforeError(prompt); - await page.screenshot({ path: testInfo.outputPath("created-agent-prompt-error.png") }); + await page.screenshot({ + path: testInfo.outputPath("created-agent-prompt-error.png"), + }); await creation.reloadAgent(); await creation.expectPromptBeforeError(prompt); await creation.submitPrompt("emit 1 coalesced agent stream updates for a corrected prompt."); diff --git a/packages/app/e2e/support/helpers/creation.ts b/packages/app/e2e/support/helpers/creation.ts index 1c3d5dda8..0de6d6672 100644 --- a/packages/app/e2e/support/helpers/creation.ts +++ b/packages/app/e2e/support/helpers/creation.ts @@ -142,6 +142,8 @@ export async function createCreationScenario(page: Page) { const requests = await observeCreationRequests(page); const project = await seedWorkspace({ repoPrefix: "creation-idempotency-" }); let workspaceId = project.workspaceId; + // Workspaces the scenario opens itself to evict the deck; they are not creations under test. + const evictionWorkspaceIds = new Set(); return { cleanup: project.cleanup, async openWorkspaceForm(isolation: "local" | "worktree") { @@ -232,7 +234,9 @@ export async function createCreationScenario(page: Page) { await requests.settled(); const workspaces = (await project.client.fetchWorkspaces()).entries.filter( (workspace) => - workspace.projectId === project.projectId && workspace.id !== project.workspaceId, + workspace.projectId === project.projectId && + workspace.id !== project.workspaceId && + !evictionWorkspaceIds.has(workspace.id), ); expect(workspaces).toHaveLength(1); workspaceId = workspaces[0]!.id; @@ -276,6 +280,7 @@ export async function createCreationScenario(page: Page) { source: { kind: "directory", path: project.repoPath }, }); if (!result.workspace) throw new Error(result.error ?? "Failed to seed eviction workspace"); + evictionWorkspaceIds.add(result.workspace.id); await switchWorkspaceViaSidebar({ page, serverId: getServerId(), diff --git a/packages/app/src/composer/draft/creation-key.test.ts b/packages/app/src/composer/draft/creation-key.test.ts new file mode 100644 index 000000000..b32480ba3 --- /dev/null +++ b/packages/app/src/composer/draft/creation-key.test.ts @@ -0,0 +1,30 @@ +import { describe, expect, it } from "vitest"; +import { draftCreationKey } from "./creation-key"; + +const request = { + config: { provider: "codex", cwd: "/repo", model: "gpt-6" }, + workspaceId: "workspace-1", + initialPrompt: "Start the original task.", + clientMessageId: "draft-1:initial-message", +}; + +describe("draftCreationKey", () => { + it("keeps the key for a retry of the same request, whatever its key order", () => { + const reordered = { + clientMessageId: request.clientMessageId, + initialPrompt: request.initialPrompt, + workspaceId: request.workspaceId, + config: { model: "gpt-6", cwd: "/repo", provider: "codex" }, + }; + expect(draftCreationKey("draft-1", reordered)).toBe(draftCreationKey("draft-1", request)); + }); + + it.each([ + { name: "prompt", edited: { ...request, initialPrompt: "Start the edited task." } }, + { name: "model", edited: { ...request, config: { ...request.config, model: "gpt-6-mini" } } }, + ])("gives an edited $name a new key on the same draft", ({ edited }) => { + const key = draftCreationKey("draft-1", edited); + expect(key).not.toBe(draftCreationKey("draft-1", request)); + expect(key.startsWith("draft-1:")).toBe(true); + }); +}); diff --git a/packages/app/src/composer/draft/creation-key.ts b/packages/app/src/composer/draft/creation-key.ts new file mode 100644 index 000000000..556086756 --- /dev/null +++ b/packages/app/src/composer/draft/creation-key.ts @@ -0,0 +1,29 @@ +/** + * The creation key for a draft's request. The daemon keeps one request per key, so retrying the + * same draft resumes its creation, while an edited draft is a new request with its own key. + */ +export function draftCreationKey(draftId: string, request: object): string { + return `${draftId}:${hash(canonicalJson(request))}`; +} + +function canonicalJson(value: unknown): string { + return JSON.stringify(value, (_key, entry: unknown) => + entry && typeof entry === "object" && !Array.isArray(entry) + ? Object.fromEntries(Object.entries(entry).sort(([a], [b]) => a.localeCompare(b))) + : entry, + ); +} + +// 53-bit cyrb53: distinguishes the edits of one draft without WebCrypto, which needs HTTPS. +function hash(text: string): string { + let h1 = 0xdeadbeef; + let h2 = 0x41c6ce57; + for (let index = 0; index < text.length; index += 1) { + const code = text.charCodeAt(index); + h1 = Math.imul(h1 ^ code, 2654435761); + h2 = Math.imul(h2 ^ code, 1597334677); + } + h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909); + h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909); + return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(36); +} diff --git a/packages/app/src/composer/draft/workspace-tab.tsx b/packages/app/src/composer/draft/workspace-tab.tsx index 0815603e5..12f392397 100644 --- a/packages/app/src/composer/draft/workspace-tab.tsx +++ b/packages/app/src/composer/draft/workspace-tab.tsx @@ -47,6 +47,7 @@ import { } from "@/workspace-tabs/model"; import { openWorkspaceChanges } from "@/workspace-tabs/open-supporting-view"; import { useSettings } from "@/hooks/use-settings"; +import { draftCreationKey } from "@/composer/draft/creation-key"; interface AutoSubmitConfig { provider: string; @@ -164,8 +165,7 @@ async function submitDraftCreateRequest(input: { const attachmentsArray = Array.isArray(attachments) ? attachments : undefined; const imagesData = await encodeImages(images); - const options = { - idempotencyKey: input.draftId, + const request = { config, workspaceId, initialPrompt: text, @@ -174,8 +174,10 @@ async function submitDraftCreateRequest(input: { ...(imagesData && imagesData.length > 0 ? { images: imagesData } : {}), ...(attachmentsArray && attachmentsArray.length > 0 ? { attachments: attachmentsArray } : {}), }; - const creation = useWorkspaceDraftSubmissionStore.getState().creationByDraftId[input.draftId]; - const result = creation ? await creation.retry(options) : await client.createAgent(options); + const result = await client.createAgent({ + ...request, + idempotencyKey: draftCreationKey(input.draftId, request), + }); return { agentId: result.id, diff --git a/packages/app/src/screens/new-workspace-screen.tsx b/packages/app/src/screens/new-workspace-screen.tsx index bab4df8f0..ccf49c9b4 100644 --- a/packages/app/src/screens/new-workspace-screen.tsx +++ b/packages/app/src/screens/new-workspace-screen.tsx @@ -1,8 +1,5 @@ import { useAddHost } from "@/hosts/add-host"; -import type { - CreateAgentRequestOptions, - CreateWorkspaceRequestOptions, -} from "@getpaseo/client/internal/daemon-client"; +import type { CreateWorkspaceRequestOptions } from "@getpaseo/client/internal/daemon-client"; import type { AgentSnapshotPayload, CreationSnapshot } from "@getpaseo/protocol/messages"; import { encodeImages } from "@/utils/encode-images"; import { getHostRuntimeStore } from "@/runtime/host-runtime"; @@ -126,7 +123,6 @@ import { } from "./workspace/terminals/state"; import { captureWorkspaceDraftCleanup } from "./new-workspace/background-handoff"; import { useNewWorkspaceScreenPresence } from "./new-workspace/screen-presence"; -import { retriedAgentRequest } from "./new-workspace/retry-agent-request"; function useIsNewWorkspaceDraftHandoffActive(input: { draftId: string | undefined; @@ -243,7 +239,6 @@ interface SubmitDraftInput { clearConsumedDraft: () => void; agentCreation?: { result: Promise; - retry: (input: CreateAgentRequestOptions) => Promise; }; serverId: string; draftKey: string; @@ -471,13 +466,13 @@ async function createWorkspaceChatAgent(input: CreateChatAgentInput): Promise => { + const execute = async (): Promise => { const { agent } = await ensureWorkspace({ cwd, prompt: text, attachments: workspaceNamingAttachments, withInitialAgent: true, - agent: requestedAgent, + agent: initialAgent, onEvent: (snapshot) => { if (!snapshot.workspace || navigated) return; navigated = true; @@ -516,11 +511,8 @@ async function createWorkspaceChatAgent(input: CreateChatAgentInput): Promise execute()), - retry: (request: CreateAgentRequestOptions) => - execute(retriedAgentRequest(initialAgent, request, cwd)), - }; + // A retry after a failed agent start belongs to the ready workspace's draft, not to this creation. + const agentCreation = { result: Promise.resolve().then(() => execute()) }; await agentCreation.result; if (outcome === "background") clearConsumedDraft(); return outcome; diff --git a/packages/app/src/screens/new-workspace/retry-agent-request.test.ts b/packages/app/src/screens/new-workspace/retry-agent-request.test.ts deleted file mode 100644 index 248a044a3..000000000 --- a/packages/app/src/screens/new-workspace/retry-agent-request.test.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { retriedAgentRequest, type WorkspaceAgentRequest } from "./retry-agent-request"; - -const initial: WorkspaceAgentRequest = { - config: { provider: "codex", cwd: "/repo", model: "gpt-6" }, - metadata: { "issues.key": "PAS-1" }, - initialPrompt: "first try", - clientMessageId: "draft_1:initial-message", -}; - -describe("retriedAgentRequest", () => { - it("creates with the metadata the draft resubmitted", () => { - const retried = retriedAgentRequest( - initial, - { - config: { provider: "codex", cwd: "/repo", model: "gpt-6" }, - initialPrompt: "second try", - metadata: { "issues.key": "PAS-2" }, - }, - "/repo", - ); - expect(retried.metadata).toEqual({ "issues.key": "PAS-2" }); - expect(retried.initialPrompt).toBe("second try"); - expect(retried.clientMessageId).toBe("draft_1:initial-message"); - }); - - it("creates with no metadata once the draft cleared it", () => { - const retried = retriedAgentRequest( - initial, - { config: { provider: "codex", cwd: "/repo", model: "gpt-6" }, initialPrompt: "second try" }, - "/repo", - ); - expect("metadata" in retried).toBe(false); - }); - - it("keeps the first attempt's directory over the resubmitted config", () => { - const retried = retriedAgentRequest( - initial, - { - config: { provider: "codex", cwd: "/elsewhere", model: "gpt-6-mini" }, - initialPrompt: "second try", - }, - "/repo", - ); - expect(retried.config).toEqual({ provider: "codex", cwd: "/repo", model: "gpt-6-mini" }); - }); -}); diff --git a/packages/app/src/screens/new-workspace/retry-agent-request.ts b/packages/app/src/screens/new-workspace/retry-agent-request.ts deleted file mode 100644 index 4a7efa02c..000000000 --- a/packages/app/src/screens/new-workspace/retry-agent-request.ts +++ /dev/null @@ -1,28 +0,0 @@ -import type { - CreateAgentRequestOptions, - CreateWorkspaceRequestOptions, -} from "@getpaseo/client/internal/daemon-client"; - -export type WorkspaceAgentRequest = NonNullable; - -/** - * The agent a retried creation asks for: everything the draft resubmitted (config, prompt, - * images, attachments and metadata, cleared included) on the first attempt's identity, so a - * retry creates what the composer shows now, not what it showed when creation first failed. - */ -export function retriedAgentRequest( - initial: WorkspaceAgentRequest, - request: CreateAgentRequestOptions, - cwd: string, -): WorkspaceAgentRequest { - const { metadata: _replaced, ...identity } = initial; - return { - ...identity, - config: { ...request.config!, cwd }, - initialPrompt: request.initialPrompt ?? "", - clientMessageId: initial.clientMessageId, - images: request.images, - attachments: request.attachments, - ...(request.metadata ? { metadata: request.metadata } : {}), - }; -} diff --git a/packages/app/src/stores/workspace-draft-submission-store.test.ts b/packages/app/src/stores/workspace-draft-submission-store.test.ts index 73e1a4089..317b60576 100644 --- a/packages/app/src/stores/workspace-draft-submission-store.test.ts +++ b/packages/app/src/stores/workspace-draft-submission-store.test.ts @@ -45,27 +45,3 @@ test("the production app consumes a draft once and leaves other drafts pending", expect(store.getState().consumePending(submission)).toBeNull(); expect(Object.keys(store.getState().pendingByDraftId)).toEqual(["draft-2"]); }); - -test("consuming draft presentation retains its running creation through remount and retry", () => { - const store = loadProductionStore(); - const result = new Promise(() => {}); - const creation = { result, retry: () => result }; - const submission = { - serverId: "server", - workspaceId: "workspace", - draftId: "draft-one", - text: "hello", - attachments: [], - cwd: "/repo", - provider: "codex", - clientMessageId: "message", - timestamp: 1, - agentCreation: creation, - }; - store.getState().setPending(submission); - store.getState().consumePending(submission); - expect(store.getState().pendingByDraftId[submission.draftId]).toBeUndefined(); - expect(store.getState().creationByDraftId[submission.draftId]).toBe(creation); - store.getState().clearDraftSetup({ draftId: submission.draftId }); - expect(store.getState().creationByDraftId[submission.draftId]).toBeUndefined(); -}); diff --git a/packages/app/src/stores/workspace-draft-submission-store.ts b/packages/app/src/stores/workspace-draft-submission-store.ts index 4a7d3ceaf..b3eb086d4 100644 --- a/packages/app/src/stores/workspace-draft-submission-store.ts +++ b/packages/app/src/stores/workspace-draft-submission-store.ts @@ -1,4 +1,3 @@ -import type { CreateAgentRequestOptions } from "@getpaseo/client/internal/daemon-client"; import type { AgentSnapshotPayload } from "@getpaseo/protocol/messages"; import { create } from "zustand"; import type { ComposerAttachment } from "@/attachments/types"; @@ -9,7 +8,6 @@ export interface PendingWorkspaceDraftSubmission { /** Already-running creation. Mounting the draft only observes its result. */ agentCreation?: { result: Promise; - retry: (input: CreateAgentRequestOptions) => Promise; }; serverId: string; workspaceId: string; @@ -35,7 +33,6 @@ export interface PendingWorkspaceDraftSetup { interface WorkspaceDraftSubmissionState { pendingByDraftId: Record; setupByDraftId: Record; - creationByDraftId: Record>; setPending: (submission: PendingWorkspaceDraftSubmission) => void; setDraftSetup: (input: { draftId: string; @@ -69,12 +66,8 @@ export const useWorkspaceDraftSubmissionStore = create ({ pendingByDraftId: {}, setupByDraftId: {}, - creationByDraftId: {}, setPending: (submission) => set((state) => ({ - creationByDraftId: submission.agentCreation - ? { ...state.creationByDraftId, [submission.draftId]: submission.agentCreation } - : state.creationByDraftId, pendingByDraftId: { ...state.pendingByDraftId, [submission.draftId]: submission, @@ -94,10 +87,8 @@ export const useWorkspaceDraftSubmissionStore = create { - const creationByDraftId = { ...state.creationByDraftId }; - delete creationByDraftId[normalizedDraftId]; const { [normalizedDraftId]: _removed, ...setupByDraftId } = state.setupByDraftId; - return { setupByDraftId, creationByDraftId }; + return { setupByDraftId }; }); }, consumePending: (input) => {