From b84964e5a2fa8b1e6498a1ccb471f6adba97d470 Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Fri, 4 Sep 2026 11:16:20 -0500 Subject: [PATCH] fix(runner): stabilize local paid E2E recovery (#12836) ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Paid runner E2E tests verify the complete runner, control-plane, and UI path. > - A server restart could load a fresh task page while Playwright still waited on an unsettled Vite navigation lifecycle. > - The current server also ignored the isolated Vite cache path and skipped Vite's per-request HTML transform from the known-green runner snapshot. > - A one-cell paid run then exposed that download-artifact v8 removes the artifact-name directory for one pattern match. > - This pull request restores the Vite contract, proves a fresh document after restart, and accepts only the exact singleton artifact layout. > - The benefit is reliable local runner qualification without weaker UI, source, or artifact checks. ## Linked Issues or Issue Description Refs #12769 Refs #12828 Refs #12829 Refs #12833 **What happened?** The structured-question restart test could time out after the replacement server returned the task route and rendered the durable pending interaction. A focused one-cell rerun passed the paid test but failed aggregation because download-artifact v8 flattened its single artifact. **Expected behavior** The test must prove that a new document loaded after the server restart and that the same pending interaction survived. The aggregate must accept the exact documented singleton download layout while it continues to reject ambiguous or foreign artifacts. **Steps to reproduce** 1. Run the local ACPX-Codex structured-question restart-resume cell. 2. Restart the isolated server while the question waits for an answer. 3. Observe that the route and task UI can reload before Playwright settles the navigation promise. 4. Run a paid campaign with one selected cell. 5. Observe download-artifact v8 extract the sole campaign directory directly into the requested path. **Paperclip version or commit** The local campaign reproduced the navigation failure at `3586956a1b794b3cb4a9c5f57ffb7355e2b0c46d`. The one-cell aggregate reproduced the singleton layout at `f487660c0a06ba06ca140b57386f21ed39f13120`. This fix is `de4ccceff453a4b39436bf9a2eb8f03924151af7`. **Deployment mode** Local development and paid GitHub Actions. **Installation method** Built from source. **Agent adapter(s) involved** ACPX-Codex. The Vite and aggregate fixes are provider-neutral. ## What Changed - Prove a new post-restart browser document with an in-memory sentinel. - Tolerate only Playwright's navigation timeout before the exact UI and API checks run. - Honor `PAPERCLIP_VITE_CACHE_DIR` in the embedded Vite server. - Limit dependency optimization to the real UI entry. - Run `vite.transformIndexHtml` for each request while caching only the branded source template. - Accept download-artifact v8's flattened layout only for one expected cell with one unique recognized campaign. - Keep source SHA, source ref, workflow URL, execution ID, attempt, and unexpected-entry validation. - Add focused positive and negative regressions for Vite rendering and singleton artifact selection. ## Verification - Exact 45-cell local campaign https://github.com/paperclipai/paperclip/actions/runs/33888939013 passed 44/45. Its only failure was the post-restart navigation false negative fixed here. - Exact focused rerun https://github.com/paperclipai/paperclip/actions/runs/33891207957 passed the ACPX-Codex restart cell first attempt with the same session, two durable runs, the terminal marker once, and cleanup complete. - The focused Vite renderer suite passed 2/2 tests. - The focused rerun-artifact selector suite passed 12/12 tests. - Prettier and `git diff --check` passed. - An exact-head 45-cell confirmation is pending. ## Risks Low to medium risk. The Vite change restores known-green per-request transforms and isolated cache behavior. It can affect all development UI loads. The paid matrix and ordinary CI will verify that behavior. The singleton selector remains fail-closed for ambiguous layouts and validates every result source. ## Model Used OpenAI Codex, `gpt-5.6-sol`, extended reasoning, tool use, code execution, and parallel focused agents. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [ ] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --- .../src/__tests__/vite-html-renderer.test.ts | 37 +++-- server/src/app.ts | 9 ++ server/src/vite-html-renderer.ts | 44 ++--- tests/runner-e2e/runner.spec.ts | 31 +++- .../runner-e2e/select-rerun-artifacts.test.ts | 150 +++++++++++++++++- tests/runner-e2e/select-rerun-artifacts.ts | 98 +++++++++--- 6 files changed, 295 insertions(+), 74 deletions(-) diff --git a/server/src/__tests__/vite-html-renderer.test.ts b/server/src/__tests__/vite-html-renderer.test.ts index add3ba8004..46b065f913 100644 --- a/server/src/__tests__/vite-html-renderer.test.ts +++ b/server/src/__tests__/vite-html-renderer.test.ts @@ -1,7 +1,7 @@ import fs from "node:fs"; import os from "node:os"; import path from "node:path"; -import { afterEach, describe, expect, it } from "vitest"; +import { afterEach, describe, expect, it, vi } from "vitest"; import { createCachedViteHtmlRenderer, type ViteWatcherHost } from "../vite-html-renderer.js"; function createWatcher() { @@ -32,7 +32,7 @@ describe("createCachedViteHtmlRenderer", () => { } }); - it("reuses the injected dev html shell until index.html changes", async () => { + it("caches the branded template until index.html changes while transforming every request", async () => { const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "paperclip-vite-html-")); tempDirs.push(tempDir); const indexPath = path.join(tempDir, "index.html"); @@ -43,25 +43,36 @@ describe("createCachedViteHtmlRenderer", () => { ); const watcher = createWatcher(); + const transformIndexHtml = vi.fn(async (_url: string, html: string) => + html.replace( + '', + '\n', + ), + ); + const brandHtml = vi.fn((html: string) => html.replace("
", '')); const vite: ViteWatcherHost = { watcher, + transformIndexHtml, }; - const renderer = createCachedViteHtmlRenderer({ vite, uiRoot: tempDir }); + const renderer = createCachedViteHtmlRenderer({ vite, uiRoot: tempDir, brandHtml }); await expect(renderer.render("/")).resolves.toContain("/@vite/client"); - await expect(renderer.render("/")).resolves.toContain('"/@react-refresh"'); const first = await renderer.render("/"); const second = await renderer.render("/issues"); expect(first).toBe(second); + expect(first).toContain('data-brand="paperclip"'); expect(first.match(/\/@vite\/client/g)?.length).toBe(1); - expect(first).toContain("window.$RefreshReg$"); + expect(brandHtml).toHaveBeenCalledTimes(1); + expect(transformIndexHtml).toHaveBeenCalledTimes(3); + expect(transformIndexHtml).toHaveBeenLastCalledWith("/issues", expect.stringContaining("v1")); const sourcePath = path.join(tempDir, "src", "main.tsx"); fs.mkdirSync(path.dirname(sourcePath), { recursive: true }); fs.writeFileSync(sourcePath, "export {};\n", "utf8"); watcher.emit("change", sourcePath); expect(await renderer.render("/")).toBe(first); + expect(brandHtml).toHaveBeenCalledTimes(1); fs.writeFileSync( indexPath, @@ -71,27 +82,33 @@ describe("createCachedViteHtmlRenderer", () => { watcher.emit("change", indexPath); await expect(renderer.render("/")).resolves.toContain("v2"); + expect(brandHtml).toHaveBeenCalledTimes(2); renderer.dispose(); }); - it("does not duplicate the vite client tag or react refresh preamble when already present", async () => { + it("runs Vite's HTML transform on every render so HMR entry timestamps stay current", async () => { const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "paperclip-vite-html-")); tempDirs.push(tempDir); fs.writeFileSync( path.join(tempDir, "index.html"), - '', + '', "utf8", ); + let timestamp = 0; + const transformIndexHtml = vi.fn(async (_url: string, html: string) => + html.replace("/src/main.tsx", `/src/main.tsx?t=${++timestamp}`), + ); const vite: ViteWatcherHost = { watcher: createWatcher(), + transformIndexHtml, }; const renderer = createCachedViteHtmlRenderer({ vite, uiRoot: tempDir }); - const html = await renderer.render("/"); - expect(html.match(/\/@vite\/client/g)?.length).toBe(1); - expect(html.match(/\/@react-refresh/g)?.length).toBe(1); + await expect(renderer.render("/")).resolves.toContain("/src/main.tsx?t=1"); + await expect(renderer.render("/issues/ISS-1")).resolves.toContain("/src/main.tsx?t=2"); + expect(transformIndexHtml).toHaveBeenCalledTimes(2); }); }); diff --git a/server/src/app.ts b/server/src/app.ts index 3c0b284341..669823a648 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -774,9 +774,18 @@ export async function createApp( res.end("Upgrade Required"); }); const { createServer: createViteServer } = await import("vite"); + const configuredViteCacheDir = process.env.PAPERCLIP_VITE_CACHE_DIR?.trim(); const vite = await createViteServer({ root: uiRoot, + ...(configuredViteCacheDir + ? { cacheDir: path.resolve(configuredViteCacheDir) } + : {}), appType: "custom", + // Vite otherwise discovers every HTML entry below the UI root. Generated + // Storybook output can reference dependencies that are intentionally not + // part of the application install, poisoning a clean embedded dev-server + // cache before the browser opens. The embedded UI has one real entry. + optimizeDeps: { entries: [path.resolve(uiRoot, "index.html")] }, server: { // Listener binding and browser HMR hostname are deliberately separate: // exposed branch runtimes stay loopback-only while the browser uses the diff --git a/server/src/vite-html-renderer.ts b/server/src/vite-html-renderer.ts index 983a3ea158..171686aed3 100644 --- a/server/src/vite-html-renderer.ts +++ b/server/src/vite-html-renderer.ts @@ -4,6 +4,7 @@ import path from "node:path"; type ViteWatcherEvent = "add" | "change" | "unlink"; export interface ViteWatcherHost { + transformIndexHtml(url: string, html: string): Promise