From 6f40e23536ad9f80c6379543acc22871380fdc25 Mon Sep 17 00:00:00 2001 From: Devin Foley Date: Mon, 28 Sep 2026 09:59:15 -0700 Subject: [PATCH 1/6] fix(logging): redact cloud authentication headers (#14413) ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - The server records HTTP requests to help operators diagnose failures. > - Cloud requests carry tenant credentials and signed assertions in headers. > - The HTTP logger did not redact four of these headers. > - This pull request adds those headers to the existing redaction list. > - Operators retain the route, method, and response status without recording these values. ## Linked Issues or Issue Description **What happened?** HTTP request logs could contain cloud tenant credentials, session identifiers, runtime identity assertions, and cloud control assertions. **Expected behavior** The logger must redact these header values for successful requests and failed requests. **Steps to reproduce** 1. Create an Express app with the production HTTP logger and redaction configuration. 2. Send a request with the four cloud headers and distinct test values. 3. Inspect the serialized request headers for responses with status 200, 403, and 500. **Paperclip version or commit** Reproduced on `0f14d26123` before this fix. **Deployment mode** Server with cloud proxy authentication. The regression test uses an in-process Express server. ## What Changed - Redact `x-paperclip-cloud-tenant-token`, `x-paperclip-cloud-session-id`, `x-paperclip-cloud-runtime-identity`, and `x-paperclip-cloud-control` in HTTP request logs. - Test real logger output for HTTP 200, 403, and 500 with mixed-case request header names. Route 403 and 500 through the production error handler. Check response bodies, log levels, and server error context. - Check that the method, route, and status remain available. ## Verification The `server/src/__tests__/http-log-redaction.test.ts` suite passed, including all three new cloud-header cases. - Rebased onto master at `14795136f5`. - `pnpm exec vitest run server/src/__tests__/http-log-redaction.test.ts` passed: 59 tests, including all three new response-status cases. The suite and server typecheck also passed after the error-handler coverage update. - `pnpm build` and `pnpm -r typecheck` passed. The full local `pnpm test:run` was attempted and stopped after the failures listed below. Greptile is 5/5 with zero unresolved review threads on the latest head. All checks for head `373d29e2f1` passed (53 successful, two intentional skips). - Full local validation did not pass. The attempt reproduced company-skill cache permission failures, the terminal-workspace cleanup assertion, a heartbeat feedback timeout, and one process-conversation timing failure. It was stopped during the general-server stage after these failures. Remaining general-server tests, other workspace groups, and serialized-server stages did not complete locally. Earlier clean-master checks reproduced the cache failures and isolated cleanup retries passed. The latest-head GitHub suite passed all of these groups. - No browser suites ran locally. This change does not affect browser behavior. ## Risks - These four values will no longer be available in HTTP logs. Route, method, and status remain available. - This change applies to new log entries. It does not remove old entries or rotate credentials. - No schema, API, or authentication behavior changes. ## Model Used OpenAI Codex, GPT-6, with repository inspection, code editing, and test execution. The exact deployment model ID and context window are not exposed in this session. ## 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 (focused suites; full local-run failures and incomplete stages are documented above) - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip --- .../src/__tests__/http-log-redaction.test.ts | 52 +++++++++++++++++++ server/src/middleware/http-log-redaction.ts | 5 ++ 2 files changed, 57 insertions(+) diff --git a/server/src/__tests__/http-log-redaction.test.ts b/server/src/__tests__/http-log-redaction.test.ts index 62def738d8b..49fe5f11e4e 100644 --- a/server/src/__tests__/http-log-redaction.test.ts +++ b/server/src/__tests__/http-log-redaction.test.ts @@ -428,6 +428,58 @@ describe("HTTP logger redaction", () => { expect(log.res.statusCode).toBe(status); }); + it.each([200, 403, 500])("redacts cloud credentials and assertions from HTTP %i logs", async (status) => { + const headers = { + "X-Paperclip-Cloud-Tenant-Token": "cloud-tenant-token-canary", + "X-Paperclip-Cloud-Session-Id": "cloud-session-id-canary", + "X-Paperclip-Cloud-Runtime-Identity": "cloud-runtime-identity-canary", + "X-Paperclip-Cloud-Control": "cloud-control-canary", + }; + const chunks: string[] = []; + const stream = new Writable({ + write(chunk, _encoding, callback) { + chunks.push(chunk.toString()); + callback(); + }, + }); + const app = express(); + app.use(createHttpLogger(pino({ redact: [...HTTP_LOG_REDACT_PATHS] }, stream))); + app.get("/api/companies", (_req, res, next) => { + if (status === 403) { + next(new HttpError(403, "Cloud tenant authentication required")); + return; + } + if (status === 500) { + next(new Error("Synthetic cloud request failure")); + return; + } + res.status(status).json({ status }); + }); + app.use(errorHandler); + + const response = await request(app).get("/api/companies").set(headers).expect(status); + if (status === 403) { + expect(response.body).toEqual({ error: "Cloud tenant authentication required" }); + } else if (status === 500) { + expect(response.body).toEqual({ error: "Internal server error" }); + } + + const output = chunks.join(""); + const log = JSON.parse(output.trim()); + for (const [header, secret] of Object.entries(headers)) { + expect(output).not.toContain(secret); + expect(log.req.headers[header.toLowerCase()]).toBe("[Redacted]"); + } + expect(log.req.method).toBe("GET"); + expect(log.req.url).toBe("/api/companies"); + expect(log.res.statusCode).toBe(status); + expect(log.level).toBe(status === 500 ? 50 : status === 403 ? 40 : 30); + if (status === 500) { + expect(log.errorContext.message).toBe("Synthetic cloud request failure"); + expect(log.err.message).toBe("Synthetic cloud request failure"); + } + }); + it("drops OAuth callback query data from the message and structured request", async () => { const chunks: string[] = []; const stream = new Writable({ diff --git a/server/src/middleware/http-log-redaction.ts b/server/src/middleware/http-log-redaction.ts index 8b50c5cf371..684e7cc13fc 100644 --- a/server/src/middleware/http-log-redaction.ts +++ b/server/src/middleware/http-log-redaction.ts @@ -10,6 +10,11 @@ export const HTTP_LOG_REDACT_PATHS = [ 'req.headers["x-csrf-token"]', 'req.headers["x-xsrf-token"]', 'req.headers["x-api-key"]', + // Cloud proxy credentials and signed assertions authorize tenant access. + 'req.headers["x-paperclip-cloud-tenant-token"]', + 'req.headers["x-paperclip-cloud-session-id"]', + 'req.headers["x-paperclip-cloud-runtime-identity"]', + 'req.headers["x-paperclip-cloud-control"]', // Runtime GitHub capabilities authorize credential acquisition for a live run. 'req.headers["x-paperclip-github-capability"]', // Telegram's optional webhook verification header is a reusable bearer From e9debd3eac328e6abe87ab89e9ff40ee2bb56b73 Mon Sep 17 00:00:00 2001 From: Devin Foley Date: Mon, 28 Sep 2026 09:59:56 -0700 Subject: [PATCH 2/6] fix(workspaces): allow larger status output for readiness checks (#14414) ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - The server checks workspace contents before it allows cleanup or branch reconciliation. > - These checks need the full Git status output to count untracked files. > - Nested task worktrees can make this output exceed the scheduler's default 1 MiB limit. > - A scan failure then blocks an otherwise inspectable workspace. > - This pull request raises the limit for these status checks to 32 MiB. > - The checks retain exact counts and still protect uncommitted work from cleanup. ## Linked Issues or Issue Description Refs #14194 and #14253. Those changes address snapshot enumeration. This PR addresses buffered execution-workspace status checks. Refs #13619 for separate work on caching these checks. The related journal identity failure is covered by #14312. **What happened?** Close-readiness checks failed when Git status output exceeded 1 MiB. A workspace with thousands of long untracked paths could not report its file count or complete readiness inspection. **Expected behavior** Allow up to 32 MiB of status output for execution-workspace readiness and branch reconciliation. Preserve exact counts. Continue to block cleanup when the workspace has uncommitted data or the scan exceeds its bound. **Steps to reproduce** 1. Create an execution workspace with a merged delivery. 2. Add 5,000 long untracked filenames under a nested task directory. The status output exceeds 1 MiB. 3. Request close readiness. Before this fix the status scan fails. After this fix it reports all 5,000 files. 4. Run the terminal-workspace sweep. Confirm that it preserves the workspace and files. **Paperclip version or commit** Reproduced on master at `14795136f5` before this fix. **Deployment mode** Server with managed Git workspaces. ## What Changed - Set a 32 MiB stdout bound for execution-workspace status scans. - Add a real Git regression with 5,000 long untracked paths and a cleanup-preservation assertion. Assert that the measured status output exceeds 1 MiB. - Document the bound and failure behavior. ## Verification - The new regression failed on master before the service change: the status result had no untracked files or count after the scan exceeded its bound. - `pnpm exec vitest run server/src/__tests__/execution-workspaces-service.test.ts server/src/services/workspace-git-operation-scheduler.test.ts` passed: 82 tests, including the new regression. The regression and server typecheck also passed after the explicit byte-count assertion. - `pnpm build` and `pnpm -r typecheck` passed. The full local `pnpm test:run` was attempted and stopped after the failures listed below. Greptile is 5/5 with zero unresolved review threads on the latest head. All checks for head `45f93ad5ad` passed (53 successful, two intentional skips). - Full local validation did not pass. The attempt reproduced 13 company/runtime skill-cache permission failures, the terminal-workspace cleanup assertion, and a heartbeat feedback timeout. It was stopped during the general-server stage after these failures. Remaining general-server tests, other workspace groups, and serialized-server stages did not complete locally. Earlier clean-master checks reproduced the cache failures and isolated cleanup retries passed. The latest-head GitHub suite passed all of these groups. - One GitHub browser shard initially failed because its GitHub-connection test checked the resume-action array before the mocked request completed. The single failed-job retry passed without a source change. All latest-head checks are green. - No browser suites ran locally. This change does not affect browser behavior. ## Risks - Each active status scan can buffer up to 32 MiB before parsing. The existing scheduler bounds scan concurrency and queue size. - Output above the bound still fails the scan and blocks destructive cleanup. The change does not truncate output or change cleanup rules. - There are no API, database, or snapshot-streaming changes. ## Model Used OpenAI Codex, GPT-6, with repository inspection, code editing, and test execution. The exact deployment model ID and context window are not exposed in this session. ## 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 (focused suites; full local-run failures and incomplete stages are documented above) - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip --- doc/DEVELOPING.md | 2 ++ .../execution-workspaces-service.test.ts | 27 +++++++++++++++++++ server/src/services/execution-workspaces.ts | 3 +++ 3 files changed, 32 insertions(+) diff --git a/doc/DEVELOPING.md b/doc/DEVELOPING.md index 60891a5a54f..85e38bd8160 100644 --- a/doc/DEVELOPING.md +++ b/doc/DEVELOPING.md @@ -715,6 +715,8 @@ These bounds apply to application filename storage, not total process memory. Re Workspace preparation resolves an existing root symlink before reading the snapshot and uses that resolved directory for the rest of the operation. Overlay staging checks the captured root identity and each selected path's ancestors before and after copying. A replaced root or a symlink in an ancestor directory stops staging before upload. A missing source file can be skipped; other source inspection errors stop staging. Selected symlink entries remain symlinks. +Execution-workspace close-readiness and branch-reconciliation status checks allow up to 32 MiB of buffered output. This accommodates nested task worktrees while preserving exact untracked-file counts. Larger output fails the scan and blocks destructive cleanup. Other buffered scan bounds stay unchanged. + The cache intentionally trades up to a few seconds of changed-file freshness for stable server latency. The file browser retains an explicit refresh action, does not start its query while the panel or browser tab is hidden, and presents overloads as retryable failures rather than an empty workspace. A full queue returns `503` with code `workspace_git_scan_saturated`; a scan exceeding its wall-clock limit returns `504` with code `workspace_git_scan_timeout`. Both responses include `Retry-After: 1`. Sandbox Git sync treats only the selected repository root as a clone source. A selected subfolder uses directory sync within that folder, applies the enclosing repository's ignore rules, and does not transfer parent files or Git history. diff --git a/server/src/__tests__/execution-workspaces-service.test.ts b/server/src/__tests__/execution-workspaces-service.test.ts index 48431a003bd..1d1a3c22f16 100644 --- a/server/src/__tests__/execution-workspaces-service.test.ts +++ b/server/src/__tests__/execution-workspaces-service.test.ts @@ -1059,6 +1059,33 @@ describeEmbeddedPostgres("executionWorkspaceService.getCloseReadiness", () => { expect(workspace?.status).toBe("active"); }); + it("counts large untracked worktrees without allowing destructive cleanup", async () => { + const seeded = await seedTerminalWorkspace({ mergedPr: true }); + const directory = path.join(seeded.worktreePath, ".worktrees", "task-retry"); + await fs.mkdir(directory, { recursive: true }); + for (let offset = 0; offset < 5_000; offset += 100) { + await Promise.all(Array.from({ length: 100 }, (_, index) => + fs.writeFile(path.join(directory, `${offset + index}-${"source".repeat(32)}.ts`), "uncommitted\n"), + )); + } + + const status = await execFileAsync( + "git", + ["-C", seeded.worktreePath, "status", "--porcelain", "--untracked-files=all"], + { maxBuffer: 2 * 1024 * 1024 }, + ); + expect(Buffer.byteLength(status.stdout, "utf8")).toBeGreaterThan(1024 * 1024); + + const readiness = await svc.getCloseReadiness(seeded.executionWorkspaceId); + expect(readiness?.git).toMatchObject({ hasUntrackedFiles: true, untrackedEntryCount: 5_000 }); + expect(readiness?.warnings).toContain("The workspace has 5000 untracked files."); + expect(readiness?.blockingReasons).not.toContain( + "Paperclip could not verify the workspace git status. Retry before destructive cleanup.", + ); + expect(await svc.sweepTerminalWorkspaces()).toMatchObject({ archived: 0, skippedUndelivered: 1 }); + await expect(fs.access(seeded.worktreePath)).resolves.toBeUndefined(); + }, 20_000); + it("refuses cleanup when the worktree changes after delivery assessment", async () => { const seeded = await seedTerminalWorkspace({ mergedPr: true }); await db.update(executionWorkspaces).set({ diff --git a/server/src/services/execution-workspaces.ts b/server/src/services/execution-workspaces.ts index c1ec53ffd66..2382faef829 100644 --- a/server/src/services/execution-workspaces.ts +++ b/server/src/services/execution-workspaces.ts @@ -415,6 +415,9 @@ async function runExpensiveGitStatus(input: { operation: input.operation, fairnessKeys: input.fairnessKeys, cacheTtlMs: 0, + // Nested task worktrees can exceed the scheduler's 1 MiB default. + // Keep exact file counts for readiness and reconciliation checks. + maxStdoutBytes: 32 * 1024 * 1024, }); } From 270afd2fb819cfb5e064de0ff4d65a074b0083eb Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Mon, 28 Sep 2026 13:43:30 -0500 Subject: [PATCH 3/6] feat(ui): show running commit in staging account menu (#14410) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - The account menu shows the signed-in user's identity. > - Staging users need to know which server commit is running after a deploy. > - The health endpoint already returns that commit, but the menu does not show it. > - This pull request adds the short commit below the email on staging hosts. > - Users can open the menu to check a deploy without opening deployment tools. ## Linked Issues or Issue Description **What existing behavior does this improve?** The account menu on staging instances. **Current behavior** The menu shows the user's name and email. It does not show the running server commit. **Proposed behavior** On `*.staging.paperclip.app`, show `SHA 8751e2d` below the email. Use the current `/api/health` commit. Show the full SHA on hover and link to the commit on GitHub. Refresh the health query when the menu opens. Hide the label on other hosts and when commit metadata is unavailable. **Reason and benefit** A user can confirm which commit a staging instance runs after an automatic deploy. **Breaking changes** None. The server already returns the commit field. Refs #14060 for related account-menu work. This change adds deployment information only. ## What Changed - Add the existing health response commit field to the UI type. - Share the staging host check and a separate health query across both account-menu variants. A menu refresh failure leaves the access gate health state unchanged. - Show a short SHA below the email. Link to the full commit on GitHub and include the full SHA in its accessible name and hover title. - Document the staging label and cover staging hosts, other hosts, missing metadata, refresh on reopen, and request failure isolation. ## Verification - `pnpm exec vitest run ui/src/components/SidebarAccountMenu.test.tsx` — 24 tests passed. - `pnpm check:token-gates` — passed. - `pnpm -r typecheck` — passed. - `pnpm build` — passed. The UI build and typecheck also passed again after review fixes. - Full test matrix — passed on the latest commit in [CI](https://github.com/paperclipai/paperclip/actions/runs/36447810742). The local `pnpm test:run` was stopped before completion after CI finished the same suites. The focused local tests, full local typecheck, and full local build passed. - Browser shard 6 passed on one rerun. Its first attempt lost part of the draft text in the existing attachment-receipt reload test. No code changed for the rerun. - Rendered the real account menu in a local browser fixture with a staging hostname condition and mocked health response. Confirmed the SHA fits below the email in the dark menu. - Greptile review — 5/5, all review threads resolved. - Manual check after deployment: open the menu on a staging host and compare the SHA with `/api/health`. Open the menu again after a deploy to refresh it. Confirm the label is absent on production and localhost. ## Risks - Low risk. Each menu opening on staging can make one additional health request. - The host check applies to `*.staging.paperclip.app`. Other staging domains will need an explicit update. - The label identifies the running server commit. It can briefly show cached data while the request completes. ## Model Used OpenAI Codex, GPT-6. The exact model variant and context window are not exposed in this session. Used code execution, repository inspection, and browser inspection. ## 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 #123` / `Refs #123` 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 - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip --- doc/DEVELOPING.md | 6 + ui/src/api/health.ts | 2 + .../SidebarAccountMenu.production.tsx | 14 ++ ui/src/components/SidebarAccountMenu.test.tsx | 121 ++++++++++++++++++ ui/src/components/SidebarAccountMenu.tsx | 14 ++ ui/src/hooks/useStagingCommit.ts | 18 +++ ui/src/lib/queryKeys.ts | 1 + 7 files changed, 176 insertions(+) create mode 100644 ui/src/hooks/useStagingCommit.ts diff --git a/doc/DEVELOPING.md b/doc/DEVELOPING.md index 85e38bd8160..417715fc925 100644 --- a/doc/DEVELOPING.md +++ b/doc/DEVELOPING.md @@ -1289,6 +1289,12 @@ Expected: - `/api/health` returns `{"status":"ok"}` - `/api/companies` returns a JSON array +On `*.staging.paperclip.app`, the account menu shows the running server's +short commit SHA below the user's email. Hover over it for the full SHA, +or follow the link to inspect the commit on GitHub. +Opening the menu refreshes `/api/health` so the label reflects recent deploys. +The label is hidden on other hosts and when commit metadata is unavailable. + ## Reset Local Dev Database To wipe local dev data and start fresh: diff --git a/ui/src/api/health.ts b/ui/src/api/health.ts index ac8cbc00a2b..df5c3b50319 100644 --- a/ui/src/api/health.ts +++ b/ui/src/api/health.ts @@ -26,6 +26,8 @@ export type CloudInstanceHealthStatus = { export type HealthStatus = { status: "ok"; version?: string; + /** Commit of the running server; null when build metadata is unavailable. */ + commit?: string | null; deploymentMode?: "local_trusted" | "authenticated"; deploymentExposure?: "private" | "public"; localAiLoginSupported?: boolean; diff --git a/ui/src/components/SidebarAccountMenu.production.tsx b/ui/src/components/SidebarAccountMenu.production.tsx index c96aa2e2d1e..005a31ad9f7 100644 --- a/ui/src/components/SidebarAccountMenu.production.tsx +++ b/ui/src/components/SidebarAccountMenu.production.tsx @@ -14,6 +14,7 @@ import { authApi } from "@/api/auth"; import { queryKeys } from "@/lib/queryKeys"; import { useCloudInstance } from "@/hooks/useCloudInstance"; import { useSignOut } from "@/hooks/useSignOut"; +import { useStagingCommit } from "@/hooks/useStagingCommit"; import { useSidebar } from "../context/SidebarContext"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"; @@ -112,6 +113,7 @@ export function SidebarAccountMenu({ const { isMobile, setSidebarOpen, collapsed, peeking } = useSidebar(); const rail = collapsed && !peeking; const open = controlledOpen ?? internalOpen; + const stagingCommit = useStagingCommit(open); const setOpen = onOpenChange ?? setInternalOpen; const { data: session } = useQuery({ queryKey: queryKeys.auth.session, @@ -174,6 +176,18 @@ export function SidebarAccountMenu({

{displayName}

{secondaryLabel}

+ {stagingCommit ? ( + + SHA {stagingCommit.slice(0, 7)} + + ) : null}
diff --git a/ui/src/components/SidebarAccountMenu.test.tsx b/ui/src/components/SidebarAccountMenu.test.tsx index dab607771c3..8fd2eeaaafc 100644 --- a/ui/src/components/SidebarAccountMenu.test.tsx +++ b/ui/src/components/SidebarAccountMenu.test.tsx @@ -19,6 +19,7 @@ const mockAuthApi = vi.hoisted(() => ({ const mockInstanceSettingsApi = vi.hoisted(() => ({ getExperimental: vi.fn(), })); +const mockHealthApi = vi.hoisted(() => ({ get: vi.fn() })); const mockToggleTheme = vi.hoisted(() => vi.fn()); const mockSetSidebarOpen = vi.hoisted(() => vi.fn()); const mockNavigateTopLevel = vi.hoisted(() => vi.fn()); @@ -27,6 +28,8 @@ vi.mock("@/api/auth", () => ({ authApi: mockAuthApi, })); +vi.mock("@/api/health", () => ({ healthApi: mockHealthApi })); + vi.mock("@/lib/browserNavigation", () => ({ navigateTopLevel: mockNavigateTopLevel, })); @@ -100,6 +103,124 @@ describe("SidebarAccountMenu", () => { container.remove(); document.body.innerHTML = ""; vi.clearAllMocks(); + vi.unstubAllGlobals(); + }); + + describe.each([SidebarAccountMenu, ProductionSidebarAccountMenu])("staging commit (%#)", (AccountMenu) => { + const commit = "8751e2de4626ff5e7355fe28b30509991cfff920"; + + it.each([ + ["paperclip.staging.paperclip.app", true], + ["another.staging.paperclip.app", true], + ["paperclip.paperclip.app", false], + ["localhost", false], + ["paperclip.staging.paperclip.app.example.com", false], + ])("shows the running SHA only on staging: %s", async (hostname, visible) => { + vi.stubGlobal("location", new URL(`https://${hostname}`)); + mockHealthApi.get.mockResolvedValue({ status: "ok", commit }); + const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }); + // Production must stay hidden even if the shared cache contains a commit. + queryClient.setQueryData(queryKeys.health, { status: "ok", commit }); + const root = createRoot(container); + await act(async () => { + root.render( + + + , + ); + }); + await flushReact(); + + const label = document.body.querySelector(`a[title="${commit}"]`); + expect(Boolean(label)).toBe(visible); + if (visible) { + expect(label?.textContent).toBe("SHA 8751e2d"); + expect(label?.previousElementSibling?.textContent).toBe("jane@example.com"); + expect(label?.getAttribute("href")).toBe(`https://github.com/paperclipai/paperclip/commit/${commit}`); + expect(label?.getAttribute("aria-label")).toBe(`View commit ${commit} on GitHub`); + expect(mockHealthApi.get).toHaveBeenCalledOnce(); + } else { + expect(mockHealthApi.get).not.toHaveBeenCalled(); + } + await act(() => root.unmount()); + queryClient.clear(); + }); + + it("refreshes the running SHA each time the staging menu opens", async () => { + vi.stubGlobal("location", new URL("https://paperclip.staging.paperclip.app")); + const nextCommit = "3447609d2247e75e55d91493dda91a608364f672"; + mockHealthApi.get + .mockResolvedValueOnce({ status: "ok", commit }) + .mockResolvedValueOnce({ status: "ok", commit: nextCommit }); + const queryClient = new QueryClient(); + const root = createRoot(container); + const renderMenu = async (open: boolean) => { + await act(() => { + root.render( + + + , + ); + }); + await flushReact(); + }; + + await renderMenu(false); + expect(mockHealthApi.get).not.toHaveBeenCalled(); + await renderMenu(true); + expect(document.body.textContent).toContain("SHA 8751e2d"); + await renderMenu(false); + await renderMenu(true); + expect(document.body.textContent).toContain("SHA 3447609"); + expect(document.body.textContent).not.toContain("SHA 8751e2d"); + expect(mockHealthApi.get).toHaveBeenCalledTimes(2); + await act(() => root.unmount()); + queryClient.clear(); + }); + + it("does not change the board health state when a menu refresh fails", async () => { + vi.stubGlobal("location", new URL("https://paperclip.staging.paperclip.app")); + mockHealthApi.get.mockRejectedValueOnce(new Error("Deploy in progress")); + const queryClient = new QueryClient(); + const boardHealth = { status: "ok", commit }; + queryClient.setQueryData(queryKeys.health, boardHealth); + queryClient.setQueryData(queryKeys.stagingCommit, boardHealth); + const root = createRoot(container); + await act(() => { + root.render( + + + , + ); + }); + await flushReact(); + + expect(mockHealthApi.get).toHaveBeenCalledOnce(); + expect(queryClient.getQueryState(queryKeys.stagingCommit)?.status).toBe("error"); + expect(queryClient.getQueryState(queryKeys.health)?.status).toBe("success"); + expect(queryClient.getQueryData(queryKeys.health)).toEqual(boardHealth); + expect(document.body.textContent).not.toContain("SHA "); + await act(() => root.unmount()); + queryClient.clear(); + }); + + it.each([null, undefined])("omits unavailable commit metadata (%s)", async (commit) => { + vi.stubGlobal("location", new URL("https://paperclip.staging.paperclip.app")); + mockHealthApi.get.mockResolvedValue({ status: "ok", commit }); + const queryClient = new QueryClient(); + const root = createRoot(container); + await act(() => { + root.render( + + + , + ); + }); + await flushReact(); + expect(document.body.textContent).not.toContain("SHA "); + await act(() => root.unmount()); + queryClient.clear(); + }); }); it("shares the nav background without separator borders", async () => { diff --git a/ui/src/components/SidebarAccountMenu.tsx b/ui/src/components/SidebarAccountMenu.tsx index 199e8cbeeea..d6552c3a509 100644 --- a/ui/src/components/SidebarAccountMenu.tsx +++ b/ui/src/components/SidebarAccountMenu.tsx @@ -15,6 +15,7 @@ import { authApi } from "@/api/auth"; import { queryKeys } from "@/lib/queryKeys"; import { useCloudInstance } from "@/hooks/useCloudInstance"; import { useSignOut } from "@/hooks/useSignOut"; +import { useStagingCommit } from "@/hooks/useStagingCommit"; import { useSidebar } from "../context/SidebarContext"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"; @@ -118,6 +119,7 @@ export function SidebarAccountMenu({ const { isMobile, setSidebarOpen, collapsed, peeking } = useSidebar(); const rail = collapsed && !peeking && !forceExpanded; const open = controlledOpen ?? internalOpen; + const stagingCommit = useStagingCommit(open); const setOpen = onOpenChange ?? setInternalOpen; const { data: session } = useQuery({ queryKey: queryKeys.auth.session, @@ -180,6 +182,18 @@ export function SidebarAccountMenu({

{secondaryLabel}

+ {stagingCommit ? ( + + SHA {stagingCommit.slice(0, 7)} + + ) : null} diff --git a/ui/src/hooks/useStagingCommit.ts b/ui/src/hooks/useStagingCommit.ts new file mode 100644 index 00000000000..ca68b0bfaf3 --- /dev/null +++ b/ui/src/hooks/useStagingCommit.ts @@ -0,0 +1,18 @@ +import { useQuery } from "@tanstack/react-query"; +import { healthApi } from "@/api/health"; +import { queryKeys } from "@/lib/queryKeys"; + +export function useStagingCommit(menuOpen: boolean) { + const isStaging = window.location.hostname.endsWith(".staging.paperclip.app"); + const { data, isError } = useQuery({ + // Keep optional menu refresh failures separate from the board's access gate. + queryKey: queryKeys.stagingCommit, + queryFn: () => healthApi.get(), + enabled: isStaging && menuOpen, + // Opening the menu must refresh the server SHA after a staging rollout. + staleTime: 0, + retry: false, + }); + + return isStaging && !isError ? data?.commit ?? null : null; +} diff --git a/ui/src/lib/queryKeys.ts b/ui/src/lib/queryKeys.ts index 45d03d80db5..56c4879b837 100644 --- a/ui/src/lib/queryKeys.ts +++ b/ui/src/lib/queryKeys.ts @@ -615,6 +615,7 @@ export const queryKeys = { experimentalSettings: ["instance", "experimental-settings"] as const, }, health: ["health"] as const, + stagingCommit: ["staging-commit"] as const, cloud: { stacks: ["cloud", "stacks"] as const, }, From d9d2147171844c4f4c80dec4db7edc0a04ac8f07 Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Mon, 28 Sep 2026 13:45:27 -0500 Subject: [PATCH 4/6] fix(auth): keep Cloud tenants on the Cloud sign-in flow (#14407) ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Cloud owns human identity and passes a verified identity to each tenant. > - The tenant can report no session while the Cloud session is still valid. > - The access gate and direct `/auth` route then show the instance password form. > - This pull request sends those users through the configured Cloud entry endpoint. > - Cloud can renew the tenant session or show its login page, then return to the original task. ## Linked Issues or Issue Description **What happened?** A Cloud tenant can display the self-hosted email/password form after an instance session check returns no session. This gives Cloud users the wrong login method. **Expected behavior** An active Cloud session renews tenant access automatically. A signed-out user signs in through Cloud. Staging and production use their own configured Cloud origins. Self-hosted instances keep their instance login form. **Steps to reproduce** 1. Open a Cloud tenant task or an `/auth?next=...` link. 2. Keep the Cloud session active but make the instance session check return 401. 3. Observe the instance password form instead of Cloud session recovery. **Deployment mode** Cloud-managed authenticated instances. No database or server API changes. Searched related authentication PRs. Native self-hosted OIDC support in #10411 is a separate feature; this change uses the existing Cloud entry contract. ## What Changed - Wait for deployment metadata before showing an instance login form. - Use the health response's Cloud origin and stack slug for session recovery. - Preserve the tenant path, query, and fragment. Reject external and recursive login return targets. - Limit automatic recovery per tab. Show a manual Cloud retry after failed recovery. Show service failures as errors. - Keep self-hosted login and local trusted access. Add focused tests, browser regressions, deployment documentation, and an unavailable-state design example. ## Verification - `pnpm -r typecheck` and `pnpm build` passed. - UI typecheck and `pnpm check:token-gates` passed after the final UI edits. - All UI tests passed: 639 files, 6,777 tests. - 63 focused Vitest tests passed across Auth, CloudAccessGate, Cloud links, and recovery coordination. - `pnpm exec playwright test --config tests/e2e/playwright.config.ts tests/e2e/cloud-auth.spec.ts`: 5 passed. The tests use the real tenant UI and database with a simulated Cloud HTTP endpoint. They cover both Cloud origins, direct auth/task links, no password-form flash, preserved URLs, reload, and self-hosted login. - Hands-on browser test used the real Cloud gateway and a fresh tenant build with disposable local data. Active Cloud session plus a forced missing instance session returned to the task. An expired tenant cookie also renewed automatically and returned to the task. Removing both sessions reached the real Cloud email/social login UI. Persistent failure stopped at the retry screen; retry succeeded after removing the injected fault. The fixture used a loopback transport adapter and a simulated signed-out OIDC issuer. No production session or deployment was changed. - The default local browser startup hit the host's embedded PostgreSQL resource limit. The passing run used a separate disposable database on the test PostgreSQL process. - The full local `pnpm test:run` sweep was stopped after about 31 minutes once CI completed the full suite. It had reported 33 failures in the unchanged runner API unit/integration files; both files pass in isolation (1,749 + 28 tests). The local sweep did not reach the later workspace/serialized groups. CI completed all of those groups successfully. - Greptile reviewed commit `d603fd4e39455de44da9dae81b72197096c0e1e8` at 5/5 with its only thread resolved. All CI gates are green on this commit, including all general/serialized server groups, workspace tests, Runner checks, typecheck, build, and all eight browser shards ([run](https://github.com/paperclipai/paperclip/actions/runs/36446230697)). ## Risks - Recovery depends on valid Cloud origin and stack metadata. Incomplete metadata shows an unavailable message instead of a password form. - Browsers with session storage disabled use the manual Cloud link, since automatic retries cannot be bounded across documents. - The external identity provider's email/social login was not completed in this local test. Existing Cloud authentication owns that flow. - No migration, credential format, membership rule, or production deployment changes. ## Model Used OpenAI GPT-6 through Codex. The exact served variant and context-window limit are not exposed in this session. Used reasoning, repository tools, code execution, and browser testing. ## 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 #` 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 - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip --- doc/DEPLOYMENT-MODES.md | 21 +++++++++ tests/e2e/cloud-auth.spec.ts | 64 +++++++++++++++++++++++++++ ui/src/App.test.tsx | 45 ++++++++++++++++++- ui/src/components/CloudAccessGate.tsx | 21 +++++++-- ui/src/components/CloudSignIn.tsx | 36 +++++++++++++++ ui/src/lib/cloud-sign-in.test.ts | 36 +++++++++++++++ ui/src/lib/cloud-sign-in.ts | 24 ++++++++++ ui/src/lib/cloudLinks.test.ts | 22 ++++++++- ui/src/lib/cloudLinks.ts | 23 ++++++++++ ui/src/pages/Auth.test.tsx | 62 ++++++++++++++++++++++++-- ui/src/pages/Auth.tsx | 25 +++++++++-- ui/src/pages/DesignGuide.tsx | 5 +++ 12 files changed, 371 insertions(+), 13 deletions(-) create mode 100644 tests/e2e/cloud-auth.spec.ts create mode 100644 ui/src/components/CloudSignIn.tsx create mode 100644 ui/src/lib/cloud-sign-in.test.ts create mode 100644 ui/src/lib/cloud-sign-in.ts diff --git a/doc/DEPLOYMENT-MODES.md b/doc/DEPLOYMENT-MODES.md index 04a9897973b..99dd5532268 100644 --- a/doc/DEPLOYMENT-MODES.md +++ b/doc/DEPLOYMENT-MODES.md @@ -3,6 +3,27 @@ Status: Canonical deployment and auth mode model Date: 2026-02-23 +### Paperclip Cloud sign-in + +Cloud-managed instances use Cloud for human sign-in. The instance `/auth` +route waits for deployment metadata before rendering; it never renders the +email/password form when health identifies a Cloud-managed instance. A missing +instance session returns through Cloud's `/v1/stacks/:slug/entry-redirect`. +Cloud renews the tenant session from the existing Cloud session, or sends the +user through its sign-in flow. The original tenant path, query, and fragment +travel as `returnTo` so the user returns to the same task. + +Both the Cloud origin and stack slug come from the server's health metadata +(`PAPERCLIP_CLOUD_API_ORIGIN` and `PAPERCLIP_STACK_SLUG`). Do not infer the +environment from the browser hostname or hardcode staging/production domains. +Missing configuration shows an unavailable state. An automatic recovery attempt +is limited per browser tab until a session is verified, with a five-minute +expiry and an explicit retry link if recovery fails. Network/server failures +show an error rather than treating the user as signed out. + +Self-hosted authenticated instances retain their instance sign-in form, and +`local_trusted` instances retain their normal access path. + ## 1. Purpose Paperclip supports two runtime modes: diff --git a/tests/e2e/cloud-auth.spec.ts b/tests/e2e/cloud-auth.spec.ts new file mode 100644 index 00000000000..e18084feef0 --- /dev/null +++ b/tests/e2e/cloud-auth.spec.ts @@ -0,0 +1,64 @@ +import { randomUUID } from "node:crypto"; +import { expect, test } from "@playwright/test"; + +// The tenant UI and task database are real. Cloud is an external dependency: +// simulate its entry endpoint and independent session states at the HTTP edge. +for (const cloudOrigin of ["https://my.paperclip.app", "https://my-staging.paperclip.app"]) { + for (const entry of ["auth", "task"] as const) { + test(`Cloud recovery preserves the task (${cloudOrigin}, ${entry})`, async ({ page, request, baseURL }, testInfo) => { + const companyResponse = await request.post("/api/companies", { data: { name: `Cloud auth ${randomUUID()}` } }); + expect(companyResponse.ok()).toBe(true); + const company = await companyResponse.json(); + const issueResponse = await request.post(`/api/companies/${company.id}/issues`, { + data: { title: "Return here after Cloud sign-in", status: "backlog" }, + }); + expect(issueResponse.ok()).toBe(true); + const issue = await issueResponse.json(); + const target = `/${company.issuePrefix}/issues/${issue.identifier}?view=activity#recovered`; + let authenticated = false; + let handoffs = 0; + let formWasRendered = false; + await page.exposeFunction("reportInstanceLoginForm", () => { formWasRendered = true; }); + await page.addInitScript(() => { + new MutationObserver(() => { + if (document.querySelector('input[autocomplete="current-password"]')) { + void (window as unknown as { reportInstanceLoginForm: () => Promise }).reportInstanceLoginForm(); + } + }).observe(document, { childList: true, subtree: true }); + }); + await page.route("**/api/health", async (route) => { + const response = await route.fetch(); + await route.fulfill({ response, json: { + ...await response.json(), deploymentMode: "authenticated", bootstrapStatus: "ready", + cloud: { managed: true, managedBy: "paperclip-cloud", cloudBaseUrl: cloudOrigin, stackSlug: "test-workspace" }, + } }); + }); + await page.route("**/api/auth/get-session", (route) => route.fulfill(authenticated ? { + json: { session: { id: "test-session", userId: "local-board" }, user: { id: "local-board", email: "test@example.test", name: "Test user", image: null }, sentryDsn: null }, + } : { status: 401, json: { error: "Board authentication required" } })); + await page.route(`${cloudOrigin}/v1/stacks/test-workspace/entry-redirect?**`, async (route) => { + handoffs++; + expect(new URL(route.request().url()).searchParams.get("returnTo")).toBe(target); + authenticated = true; + await route.fulfill({ status: 302, headers: { Location: `${baseURL}${target}` } }); + }); + await page.goto(entry === "auth" ? `/auth?next=${encodeURIComponent(target)}` : target); + await expect(page.getByRole("heading", { name: issue.title, exact: true })).toBeVisible(); + expect(page.url()).toBe(`${baseURL}${target}`); + expect(handoffs).toBe(1); + expect(formWasRendered).toBe(false); + await page.screenshot({ path: testInfo.outputPath("cloud-session-recovered.png") }); + await page.reload(); + await expect(page.getByRole("heading", { name: issue.title, exact: true })).toBeVisible(); + expect(handoffs).toBe(1); + expect(formWasRendered).toBe(false); + }); + } +} + +test("self-hosted auth still shows the instance sign-in form", async ({ page }) => { + await page.route("**/api/auth/get-session", (route) => route.fulfill({ status: 401, json: { error: "Board authentication required" } })); + await page.goto("/auth"); + await expect(page.getByRole("heading", { name: "Sign in to Paperclip", exact: true })).toBeVisible(); + await expect(page.getByLabel("Password", { exact: true })).toBeVisible(); +}); diff --git a/ui/src/App.test.tsx b/ui/src/App.test.tsx index 2cea7cea64c..4730e3b7822 100644 --- a/ui/src/App.test.tsx +++ b/ui/src/App.test.tsx @@ -6,6 +6,7 @@ import { createRoot } from "react-dom/client"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { CloudAccessGate } from "./components/CloudAccessGate"; +import { queryKeys } from "./lib/queryKeys"; import appSource from "./App.tsx?raw"; const mockHealthApi = vi.hoisted(() => ({ @@ -20,6 +21,11 @@ const mockAccessApi = vi.hoisted(() => ({ getCurrentBoardAccess: vi.fn(), claimBootstrapAdmin: vi.fn(), })); +const beginCloudSignInMock = vi.hoisted(() => vi.fn()); +vi.mock("@/lib/cloud-sign-in", () => ({ + beginCloudSignIn: (url: string) => beginCloudSignInMock(url), + clearCloudSignInAttempt: vi.fn(), +})); vi.mock("./api/health", () => ({ healthApi: mockHealthApi, @@ -57,9 +63,9 @@ async function waitForText(container: HTMLElement, text: string) { await vi.waitFor(() => expect(container.textContent).toContain(text)); } -function renderGate(container: HTMLElement, allowMembershipRequest = false) { +function renderGate(container: HTMLElement, allowMembershipRequest = false, client?: QueryClient) { const root = createRoot(container); - const queryClient = new QueryClient({ + const queryClient = client ?? new QueryClient({ defaultOptions: { queries: { retry: false } }, }); @@ -100,6 +106,41 @@ describe("CloudAccessGate", () => { vi.clearAllMocks(); }); + it("renews a missing Cloud instance session without opening local auth", async () => { + mockHealthApi.get.mockResolvedValue({ deploymentMode: "authenticated", cloud: { managed: true, managedBy: "paperclip-cloud", cloudBaseUrl: "https://my-staging.paperclip.app", stackSlug: "team" } }); + mockAuthApi.getSession.mockResolvedValue(null); + beginCloudSignInMock.mockReturnValue(true); + const root = renderGate(container); + await vi.waitFor(() => expect(beginCloudSignInMock).toHaveBeenCalledTimes(1)); + expect(beginCloudSignInMock).toHaveBeenCalledWith("https://my-staging.paperclip.app/v1/stacks/team/entry-redirect?returnTo=%2Finstance%2Fsettings%2Fgeneral"); + expect(container.textContent).not.toContain("Navigate:/auth"); + expect(container.textContent).not.toContain("Outlet content"); + unmountRoot(root); + }); + + it("does not mistake a session service failure for a signed-out user", async () => { + mockAuthApi.getSession.mockRejectedValue(new Error("Session service unavailable")); + const root = renderGate(container); + await waitForText(container, "Session service unavailable"); + expect(container.textContent).not.toContain("Navigate:/auth"); + expect(beginCloudSignInMock).not.toHaveBeenCalled(); + unmountRoot(root); + }); + + it.each([undefined, { managed: true, managedBy: "paperclip-cloud", cloudBaseUrl: "https://my.paperclip.app", stackSlug: "team" }])( + "does not require a session in local trusted mode, including with Cloud metadata %j", async (cloud) => { + mockHealthApi.get.mockResolvedValue({ deploymentMode: "local_trusted", cloud }); + const client = new QueryClient({ defaultOptions: { queries: { retry: false } } }); + await client.fetchQuery({ + queryKey: queryKeys.auth.session, + queryFn: () => Promise.reject(new Error("Session service unavailable")), + }).catch(() => {}); + const root = renderGate(container, false, client); + await waitForText(container, "Outlet content"); + expect(beginCloudSignInMock).not.toHaveBeenCalled(); + unmountRoot(root); + }); + it("shows a no-access message for signed-in users without org access", async () => { mockAuthApi.getSession.mockResolvedValue({ session: { id: "session-1", userId: "user-1" }, diff --git a/ui/src/components/CloudAccessGate.tsx b/ui/src/components/CloudAccessGate.tsx index 1dbe28b0de8..ff193c467d5 100644 --- a/ui/src/components/CloudAccessGate.tsx +++ b/ui/src/components/CloudAccessGate.tsx @@ -1,3 +1,4 @@ +import { useEffect } from "react"; import { Navigate, Outlet, useLocation } from "@/lib/router"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { accessApi } from "@/api/access"; @@ -8,6 +9,8 @@ import { queryKeys } from "@/lib/queryKeys"; import { BootstrapPendingPage } from "@/components/BootstrapPendingPage"; import { PaperclipLoading } from "@/components/AnimatedPaperclipIcon"; import { Card } from "@/components/ui/card"; +import { CloudSignIn } from "@/components/CloudSignIn"; +import { clearCloudSignInAttempt } from "@/lib/cloud-sign-in"; function NoBoardAccessPage() { return ( @@ -53,6 +56,10 @@ export function CloudAccessGate({ allowMembershipRequest = false }: { allowMembe retry: false, }); + useEffect(() => { + if (sessionQuery.data) clearCloudSignInAttempt(); + }, [sessionQuery.data]); + const boardAccessQuery = useQuery({ queryKey: queryKeys.access.currentBoardAccess, queryFn: () => accessApi.getCurrentBoardAccess(), @@ -78,18 +85,24 @@ export function CloudAccessGate({ allowMembershipRequest = false }: { allowMembe return ; } - if (healthQuery.error || boardAccessQuery.error) { + if (healthQuery.error || (isAuthenticatedMode && sessionQuery.error) || boardAccessQuery.error) { return (
{healthQuery.error instanceof Error ? healthQuery.error.message - : boardAccessQuery.error instanceof Error - ? boardAccessQuery.error.message - : "Failed to load app state"} + : sessionQuery.error instanceof Error + ? sessionQuery.error.message + : boardAccessQuery.error instanceof Error + ? boardAccessQuery.error.message + : "Failed to load app state"}
); } + if (isAuthenticatedMode && healthQuery.data?.cloud && !sessionQuery.data) { + return ; + } + if (isBootstrapPending) { const health = healthQuery.data; if (!health) { diff --git a/ui/src/components/CloudSignIn.tsx b/ui/src/components/CloudSignIn.tsx new file mode 100644 index 00000000000..4a7c43d665e --- /dev/null +++ b/ui/src/components/CloudSignIn.tsx @@ -0,0 +1,36 @@ +import { useEffect, useRef, useState } from "react"; +import type { CloudInstanceHealthStatus } from "@/api/health"; +import { cloudStackEntryUrl } from "@/lib/cloudLinks"; +import { beginCloudSignIn, clearCloudSignInAttempt } from "@/lib/cloud-sign-in"; +import { PaperclipLoading } from "@/components/AnimatedPaperclipIcon"; +import { Button } from "@/components/ui/button"; + +export function CloudSignIn({ cloud, returnTo }: { cloud: CloudInstanceHealthStatus; returnTo: string }) { + const entryUrl = cloudStackEntryUrl(cloud.cloudBaseUrl, cloud.stackSlug, returnTo); + const started = useRef(false); + const [blocked, setBlocked] = useState(false); + + useEffect(() => { + if (!entryUrl || started.current) return; + started.current = true; + setBlocked(!beginCloudSignIn(entryUrl)); + }, [entryUrl]); + + if (entryUrl && !blocked) return ; + + return ( +
+

Sign in to Paperclip Cloud

+

+ {entryUrl + ? "We couldn't restore your session. Continue to Paperclip Cloud to try again." + : "Cloud sign-in is unavailable for this workspace. Please contact your administrator."} +

+ {entryUrl && ( + + )} +
+ ); +} diff --git a/ui/src/lib/cloud-sign-in.test.ts b/ui/src/lib/cloud-sign-in.test.ts new file mode 100644 index 00000000000..436c0343110 --- /dev/null +++ b/ui/src/lib/cloud-sign-in.test.ts @@ -0,0 +1,36 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { beginCloudSignIn, clearCloudSignInAttempt } from "./cloud-sign-in"; + +afterEach(() => vi.unstubAllGlobals()); + +describe("Cloud sign-in recovery", () => { + it("bounds redirects across documents and resets after verified authentication", () => { + const values = new Map(); + const replace = vi.fn(); + vi.stubGlobal("window", { + location: { replace }, + sessionStorage: { + getItem: (key: string) => values.get(key) ?? null, + setItem: (key: string, value: string) => values.set(key, value), + removeItem: (key: string) => values.delete(key), + }, + }); + expect(beginCloudSignIn("https://cloud.test/entry")).toBe(true); + expect(beginCloudSignIn("https://cloud.test/entry")).toBe(false); + expect(replace).toHaveBeenCalledTimes(1); + clearCloudSignInAttempt(); + expect(beginCloudSignIn("https://cloud.test/entry")).toBe(true); + expect(replace).toHaveBeenCalledTimes(2); + }); + + it("uses manual recovery when tab storage is unavailable", () => { + const replace = vi.fn(); + vi.stubGlobal("window", { + location: { replace }, + get sessionStorage() { throw new Error("Storage disabled"); }, + }); + expect(beginCloudSignIn("https://cloud.test/entry")).toBe(false); + expect(replace).not.toHaveBeenCalled(); + expect(clearCloudSignInAttempt).not.toThrow(); + }); +}); diff --git a/ui/src/lib/cloud-sign-in.ts b/ui/src/lib/cloud-sign-in.ts new file mode 100644 index 00000000000..8d1cb313d46 --- /dev/null +++ b/ui/src/lib/cloud-sign-in.ts @@ -0,0 +1,24 @@ +const RECOVERY_KEY = "paperclip.cloud-sign-in-attempt"; +const RECOVERY_WINDOW_MS = 5 * 60 * 1000; + +export function clearCloudSignInAttempt() { + try { + window.sessionStorage.removeItem(RECOVERY_KEY); + } catch { + // Storage may be disabled. The manual Cloud sign-in link still works. + } +} + +/** One automatic handoff per tab until a session is verified, with a loop guard. */ +export function beginCloudSignIn(url: string): boolean { + try { + const previous = Number(window.sessionStorage.getItem(RECOVERY_KEY)); + if (previous > Date.now() - RECOVERY_WINDOW_MS) return false; + window.sessionStorage.setItem(RECOVERY_KEY, String(Date.now())); + } catch { + // Without durable tab state we cannot safely bound automatic navigation. + return false; + } + window.location.replace(url); + return true; +} diff --git a/ui/src/lib/cloudLinks.test.ts b/ui/src/lib/cloudLinks.test.ts index 9a1657ef6b1..f587b4aa249 100644 --- a/ui/src/lib/cloudLinks.test.ts +++ b/ui/src/lib/cloudLinks.test.ts @@ -1,7 +1,27 @@ import { describe, expect, it } from "vitest"; -import { cloudAppUrl, cloudPortfolioManageUrl, cloudStackCreateUrl, cloudStackInviteUrl } from "./cloudLinks"; +import { cloudAppUrl, cloudPortfolioManageUrl, cloudStackCreateUrl, cloudStackInviteUrl, cloudStackEntryUrl, tenantSignInReturnPath } from "./cloudLinks"; describe("cloudLinks", () => { + it.each(["https://my.paperclip.app", "https://my-staging.paperclip.app", "http://cloud.localhost:3200"])( + "renews the session through the configured Cloud origin %s", (origin) => { + const url = new URL(cloudStackEntryUrl(`${origin}/control-plane`, "team", "/TEST/issues/TEST-1?tab=activity#comment")!); + expect(url.origin).toBe(origin); + expect(url.pathname).toBe("/v1/stacks/team/entry-redirect"); + expect(url.searchParams.get("returnTo")).toBe("/TEST/issues/TEST-1?tab=activity#comment"); + }, + ); + + it.each(["https://evil.test", "//evil.test", "/\\evil.test", "/\n/evil.test", "/auth?next=/auth", "/auth/", "/a/../auth"])( + "rejects unsafe or recursive return targets: %j", (path) => { + expect(tenantSignInReturnPath(path)).toBe("/"); + }, + ); + + it("requires complete Cloud metadata", () => { + expect(cloudStackEntryUrl(null, "team", "/")).toBeNull(); + expect(cloudStackEntryUrl("https://my.paperclip.app", null, "/")).toBeNull(); + expect(cloudStackEntryUrl("javascript:alert(1)", "team", "/")).toBeNull(); + }); it("resolves stack links against the cloud origin", () => { expect(cloudStackCreateUrl("https://app.paperclip.app")).toBe( "https://app.paperclip.app/stacks/new", diff --git a/ui/src/lib/cloudLinks.ts b/ui/src/lib/cloudLinks.ts index 68d02f1ed1a..7784a493ede 100644 --- a/ui/src/lib/cloudLinks.ts +++ b/ui/src/lib/cloudLinks.ts @@ -30,6 +30,29 @@ export function cloudStackCreateUrl(cloudBaseUrl: string | null | undefined): st return cloudAppUrl(cloudBaseUrl, "/stacks/new"); } +/** Keep auth return targets inside the tenant, and never return to the login form. */ +export function tenantSignInReturnPath(path: string): string { + if (!path.startsWith("/") || path.startsWith("//") || /[\\\x00-\x20]/.test(path)) return "/"; + const url = new URL(path, "https://tenant.invalid"); + if (url.pathname.replace(/\/+$/, "") === "/auth") return "/"; + return `${url.pathname}${url.search}${url.hash}`; +} + +/** Cloud renews a tenant session here, or starts its own sign-in flow. */ +export function cloudStackEntryUrl( + cloudBaseUrl: string | null | undefined, + stackSlug: string | null | undefined, + returnTo: string, +): string | null { + const slug = stackSlug?.trim(); + if (!slug) return null; + const entry = cloudAppUrl(cloudBaseUrl, `/v1/stacks/${encodeURIComponent(slug)}/entry-redirect`); + if (!entry) return null; + const url = new URL(entry); + url.searchParams.set("returnTo", tenantSignInReturnPath(returnTo)); + return url.toString(); +} + /** Cloud manages human invitations in the current stack's People settings. */ export function cloudStackInviteUrl( cloudBaseUrl: string | null | undefined, diff --git a/ui/src/pages/Auth.test.tsx b/ui/src/pages/Auth.test.tsx index 59d70be809a..82bcd9a7386 100644 --- a/ui/src/pages/Auth.test.tsx +++ b/ui/src/pages/Auth.test.tsx @@ -11,6 +11,14 @@ import { AuthPage } from "./Auth"; const getSessionMock = vi.hoisted(() => vi.fn()); const signInEmailMock = vi.hoisted(() => vi.fn()); const signUpEmailMock = vi.hoisted(() => vi.fn()); +const healthMock = vi.hoisted(() => vi.fn()); +const beginCloudSignInMock = vi.hoisted(() => vi.fn()); + +vi.mock("../api/health", () => ({ healthApi: { get: () => healthMock() } })); +vi.mock("@/lib/cloud-sign-in", () => ({ + beginCloudSignIn: (url: string) => beginCloudSignInMock(url), + clearCloudSignInAttempt: vi.fn(), +})); vi.mock("../api/auth", () => ({ authApi: { @@ -86,6 +94,8 @@ describe("AuthPage", () => { container = document.createElement("div"); document.body.appendChild(container); getSessionMock.mockResolvedValue(null); + healthMock.mockResolvedValue({ status: "ok", deploymentMode: "authenticated" }); + beginCloudSignInMock.mockReturnValue(true); signInEmailMock.mockResolvedValue(undefined); signUpEmailMock.mockResolvedValue(undefined); }); @@ -96,11 +106,11 @@ describe("AuthPage", () => { vi.clearAllMocks(); }); - async function mount() { + async function mount(path = "/auth") { const { root, queryClient } = renderAuthPage(container); await act(async () => { root.render( - + } /> @@ -114,6 +124,52 @@ describe("AuthPage", () => { return { root, queryClient }; } + it.each(["https://my.paperclip.app", "https://my-staging.paperclip.app"])("recovers Cloud auth through %s without rendering an instance form", async (origin) => { + healthMock.mockResolvedValue({ cloud: { managed: true, managedBy: "paperclip-cloud", cloudBaseUrl: origin, stackSlug: "team" } }); + const { root } = await mount("/auth?next=%2FTEST%2Fissues%2FTEST-1%3Ftab%3Dactivity%23comment"); + await vi.waitFor(() => expect(beginCloudSignInMock).toHaveBeenCalledTimes(1)); + const target = new URL(beginCloudSignInMock.mock.calls[0][0]); + expect(target.origin).toBe(origin); + expect(target.pathname).toBe("/v1/stacks/team/entry-redirect"); + expect(target.searchParams.get("returnTo")).toBe("/TEST/issues/TEST-1?tab=activity#comment"); + expect(container.querySelector("form")).toBeNull(); + await act(() => root.unmount()); + }); + + it("never flashes a form while deployment metadata is loading", async () => { + healthMock.mockReturnValue(new Promise(() => {})); + const { root } = await mount(); + expect(container.querySelector("form")).toBeNull(); + expect(container.textContent).toContain("Loading"); + await act(() => root.unmount()); + }); + + it("fails closed when deployment metadata cannot be loaded", async () => { + healthMock.mockRejectedValue(new Error("offline")); + const { root } = await mount(); + await vi.waitFor(() => expect(container.textContent).toContain("Unable to check sign-in")); + expect(container.querySelector("form")).toBeNull(); + await act(() => root.unmount()); + }); + + it("offers a manual Cloud retry after the automatic recovery limit", async () => { + healthMock.mockResolvedValue({ cloud: { managed: true, managedBy: "paperclip-cloud", cloudBaseUrl: "https://my.paperclip.app", stackSlug: "team" } }); + beginCloudSignInMock.mockReturnValue(false); + const { root } = await mount(); + await vi.waitFor(() => expect(container.textContent).toContain("Continue to Paperclip Cloud")); + expect(container.querySelector("form")).toBeNull(); + await act(() => root.unmount()); + }); + + it("keeps incomplete Cloud configuration out of the instance form", async () => { + healthMock.mockResolvedValue({ cloud: { managed: true, managedBy: "paperclip-cloud", cloudBaseUrl: null, stackSlug: null } }); + const { root } = await mount(); + await vi.waitFor(() => expect(container.textContent).toContain("Cloud sign-in is unavailable")); + expect(container.querySelector("form")).toBeNull(); + expect(beginCloudSignInMock).not.toHaveBeenCalled(); + await act(() => root.unmount()); + }); + it("exposes password-manager metadata and a11y attributes on the sign-in form", async () => { const { root } = await mount(); @@ -247,7 +303,7 @@ describe("AuthPage", () => { email: "jane@example.com", password: "supersecret", }); - expect(queryClient.getQueryState(queryKeys.health)?.isInvalidated).toBe(true); + expect(healthMock).toHaveBeenCalledTimes(2); await act(async () => { root.unmount(); diff --git a/ui/src/pages/Auth.tsx b/ui/src/pages/Auth.tsx index d16592949db..c391dfd542e 100644 --- a/ui/src/pages/Auth.tsx +++ b/ui/src/pages/Auth.tsx @@ -2,6 +2,10 @@ import { useEffect, useMemo, useState } from "react"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { useNavigate, useSearchParams } from "@/lib/router"; import { authApi } from "../api/auth"; +import { healthApi } from "../api/health"; +import { CloudSignIn } from "@/components/CloudSignIn"; +import { clearCloudSignInAttempt } from "@/lib/cloud-sign-in"; +import { tenantSignInReturnPath } from "@/lib/cloudLinks"; import { queryKeys } from "../lib/queryKeys"; import { getRememberedInvitePath } from "../lib/invite-memory"; import { Button } from "@/components/ui/button"; @@ -24,10 +28,15 @@ export function AuthPage() { const errorId = "auth-error"; const nextPath = useMemo( - () => searchParams.get("next") || getRememberedInvitePath() || "/", + () => tenantSignInReturnPath(searchParams.get("next") || getRememberedInvitePath() || "/"), [searchParams], ); - const { data: session, isLoading: isSessionLoading } = useQuery({ + const healthQuery = useQuery({ + queryKey: queryKeys.health, + queryFn: () => healthApi.get(), + retry: false, + }); + const { data: session, isLoading: isSessionLoading, error: sessionError } = useQuery({ queryKey: queryKeys.auth.session, queryFn: () => authApi.getSession(), retry: false, @@ -35,6 +44,7 @@ export function AuthPage() { useEffect(() => { if (session) { + clearCloudSignInAttempt(); navigate(nextPath, { replace: true }); } }, [session, navigate, nextPath]); @@ -72,7 +82,7 @@ export function AuthPage() { password.trim().length > 0 && (mode === "sign_in" || (name.trim().length > 0 && password.trim().length >= 8)); - if (isSessionLoading) { + if (healthQuery.isLoading || isSessionLoading || session) { return (
@@ -80,6 +90,15 @@ export function AuthPage() { ); } + // A health/session failure must not be mistaken for a self-hosted instance. + if (healthQuery.error || sessionError) { + return

Unable to check sign-in. Refresh and try again.

; + } + + if (healthQuery.data?.cloud) { + return ; + } + return (
diff --git a/ui/src/pages/DesignGuide.tsx b/ui/src/pages/DesignGuide.tsx index f5e22e99715..3fa40fbb12d 100644 --- a/ui/src/pages/DesignGuide.tsx +++ b/ui/src/pages/DesignGuide.tsx @@ -1,4 +1,5 @@ import { DispositionRecoveryNotice } from "../components/DispositionRecoveryNotice"; +import { CloudSignIn } from "../components/CloudSignIn"; import { SetupPrompt } from "./apps/chat/SetupPrompt"; import { MediaArtifactCard } from "@/components/artifacts/MediaArtifactCard"; import { WebhookUrlWarning } from "@/components/routine-triggers/WebhookUrlWarning"; @@ -2206,6 +2207,10 @@ export function DesignGuide() {

+
+ +
+
{}} loading={false} error={false} /> {}} loading error={false} /> From f1a394bd30cb56fb9e479f98b9f50176fe921858 Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Mon, 28 Sep 2026 13:54:36 -0500 Subject: [PATCH 5/6] feat(runner): add Grok Build through native ACP (#13882) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Thinking Path > - Paperclip manages AI agents and governs their work. > - Its native runner uses structured provider protocols for sessions and tools. > - Grok Build supports ACP over stdio, but the runner did not expose it. > - Native execution requires company-scoped credentials, verified identities, and permission gates. > - This change adds Grok through ACPX for local and Daytona execution. > - Subscription login and explicit API-key execution have separate credential paths. > - Qualification grades real tool outcomes, durable state, and browser workflows. ## Linked Issues or Issue Description Refs #13845, #13847, #13850, #13878, #13901, #13973, #13977, #13979. Add **Grok Build** to `paperclip_runner` with `provider: "acpx"`, `acpxAgent: "grok"`, and model `grok-4.7`. Existing legacy Grok agents keep their adapter. Merge the three companion fixes (#13973, #13977, #13979) before treating the integrated Product qualification as deployed behavior. ## What Changed - Synchronize shared, TypeScript, Rust, server, validation, and UI provider contracts. - Run Grok native ACP stdio through ACPX and the authenticated Paperclip MCP bridge. Verify the pinned executable and exact ACP model identity. - Prefer company subscription login. Support an explicit company-secret API key without automatic paid fallback. Fence refresh and copyback to the same account and remove private runtime credentials after containment. - Preserve selected permissions, cancellation, durable session identity, resume, and restart recovery. Keep unsupported steering and goals unavailable. Preserve missing usage and cost as unknown. - Package checksum-verified Grok Build 1.0.13 for Daytona with an immutable, signed image built on EC2. - Add deterministic admission, protocol, permissions, identity, credential, failure, and cleanup checks. Add the maintained 39-case protocol roster and separate subscription/API Product profiles. - Fix live-test findings in reasoning events, reloads, idle-owner retirement, credential-home cleanup, expired-login model discovery, launcher pinning, and rerun evidence selection. - Align control-plane state readers with the transport's 64 MiB bound while retaining identity, ownership, lifecycle, and size rejection checks. - Stabilize two asynchronous CI assertions while retaining actual outcome and filesystem-evidence checks. ## Verification Current integration head `f114948376056fe0b6b34c1496ae8667b59daa63` includes master `3447609d2247e75e55d91493dda91a608364f672` (2026-09-28). Two master advances during verification overlapped the eval catalog; the final merge preserves Grok qualification, completion updates, and bounded API-response reading in all 348 cells. All 77 focused catalog/eval/workflow tests pass. Both native stack layers (#14397) are mergeable, and both exact-head Greptile reviews are 5/5 with successful security scans and no unresolved review threads. All current-head CI is green: 56 successful checks/statuses and four intentional skips ([CI run](https://github.com/paperclipai/paperclip/actions/runs/36447097232)). Trunk code-owner requirements remain enforced. The review summary’s non-blocking saved-asset offset classification note concerns code already merged in #14301; those runtime files are identical to master and outside this stack’s diff. Historical live evidence below retains its original source revisions. Earlier integration checkpoint: `24fc9b94ca0afb21ccdc8d26dbb2e4b258ad72cb`. Refreshed against master `0f14d2612`, preserving Grok qualification alongside the new accounting and lifecycle suites. All 124 focused catalog, evidence, and service-worker checks pass. The current base workflow includes the explicitly selected public-install verification lane; follow-up #14024 supplies its verifier script. CI at that earlier checkpoint was green (56 successful checks/statuses, four intentional skips), and the review is 5/5 with no unresolved findings. Prior feature CI at `fd73f0a9b1ecdf4094685054028df71739ddc3e1` passed ([run 36148259902](https://github.com/paperclipai/paperclip/actions/runs/36148259902)); that is historical evidence, not a current-head result. Paid Product measurements use frozen integrated source `2d939a92b21dcaf5c77c88b54d96784d2ddd0699`, which combines the feature with #13973, #13977, and #13979. That source passed all 52 CI checks and clean 5/5 review. Later master syncs incorporate upstream changes. Their checks remain separate from these pinned live measurements. | Check | Result and source-pinned report | | --- | --- | | Subscription protocol roster | [39/39 first attempts; 206 assertions](https://d1p6rlowie26tp.cloudfront.net/runner-protocol-evals/campaigns/gha-36046839612-1/index.html), runtime `bc6833f7`, evals `92bb4b8c` | | API protocol roster | [39/39 first attempts; 206 assertions](https://d1p6rlowie26tp.cloudfront.net/runner-protocol-evals/campaigns/gha-35926577007-1/index.html), runtime `4a1061c8`, evals `3213dbec` | | Subscription full Product matrix | [16/16 first attempts; 144 assertions; cleanup passed](https://d1p6rlowie26tp.cloudfront.net/runner-e2e/campaigns/gha-36096908572-1/index.html), source `2d939a92` | | Subscription core repetitions | 18/18: tool use, planning approval, and Stop/resume each passed three times in local and Daytona profiles. The full matrix contains repetition one; [repeat two](https://d1p6rlowie26tp.cloudfront.net/runner-e2e/campaigns/gha-36104551060-1/index.html) and [repeat three](https://d1p6rlowie26tp.cloudfront.net/runner-e2e/campaigns/gha-36108100404-1/index.html) each passed 6/6. Total: 28 unique subscription attempts at `2d939a92`. | | API smoke and question continuation | [4/4 first attempts; cleanup passed](https://d1p6rlowie26tp.cloudfront.net/runner-e2e/campaigns/gha-36147315401-1/index.html), both environments at `2d939a92` | | Historical API Product coverage | [16/16 full matrix](https://d1p6rlowie26tp.cloudfront.net/runner-e2e/campaigns/gha-35875144860-1/index.html) and 18/18 core repetitions at `4a1061c8`; retained as measurements of that revision | | Native Daytona proof | Three subscription and three API MCP/permissions/resume runs passed at `bc6833f7`. Three expired-login admission and fenced refresh checks passed without inference. All test sandboxes were removed. | | Inspectable artifacts and UI | Current-source screenshots verify planning approval, direct Ask completion, question continuation after controller restart, and two downloadable project revisions. The project downloads pass 12 and 18 tests; all 40 independent artifact oracle checks pass. | | Provider-free checks | 116 eval-validator tests, 39 Grok definitions, and 359 enabled/external campaign cells pass. Continuation regressions above 2 MiB and 16 MiB failed before their fixes; 32 focused recovery/ownership/size checks pass. | The 32 unique current-source Product attempts have no failures, retries, or skipped cells, and all cleanup checks pass. Whole-workflow timing, model identity, image and provider-pack provenance, attempts, and accounting coverage are retained in the canonical reports. The report publisher's conservative `complete=false` flag is preserved; independent audits verify the exact selected source catalog and immutable result rows. Pins: Grok Build `1.0.13 (5e9a58528b76)`, ACPX `0.13.1`, ACP model `grok-4.7`. Linux binary SHA-256: `edf79521581bb5e6b95abef848491a6a742e860da3e237ebe86a280d30dce4c1`. Launcher SHA-256: `f0b698395a3704ed2ffaf84ea19bdb20c36c8a0a70b7c629c7b6ffe144e59e55`. Image: `ghcr.io/paperclipai/paperclip-daytona-runner@sha256:76b24edfd850219e949418b19e4ceba690e84d51d199ade426e484953329b5e9`. Image build source is `4196a4cd`, recorded separately from application source `2d939a92`; each campaign verifies the image signature and provider pack. Original failed campaigns remain available: [continuation bound](https://github.com/paperclipai/paperclip/actions/runs/36057718059), [scheduler/event capture](https://github.com/paperclipai/paperclip/actions/runs/36071063537), and [startup cleanup plus EC2 interruption](https://github.com/paperclipai/paperclip/actions/runs/36080870743). They retain their original grades. No Docker or Rust builds ran on the developer laptop for these follow-ups. ## Risks Merge packaging follow-up #14024 with this base before public release. The follow-up replaces the private Grok bridge package with a built-in launcher and makes the native binary an explicit sandbox prerequisite. Three separate, reviewed fixes are part of the tested integrated behavior: #13973 serializes task-run admission; #13977 captures complete event evidence; #13979 durably reconciles failed Daytona creation. Each has green CI and clean 5/5 review. Failed-create recovery has 277 plugin tests, 92 SDK tests, host-runtime recovery tests, and a real Daytona lost-deletion-receipt proof. The live proof uses a private file for journal persistence; database durability is covered by host tests. Worker death before delivery of a failure envelope remains outside that recovery mechanism. Subscription fixtures stage an authorized company login; interactive browser sign-in is not qualified. Local Product profiles ran on EC2 Linux. The temporary subscription credential was removed from the protected GitHub environment after all subscription audits, with absence verified. Runtime homes and refresh copyback remain ownership-fenced. Protocol results remain pinned to their original revisions; they are not relabeled as tests of the latest feature commit. New binary/model versions require qualification. Missing token usage and model cost remain unknown; runtime estimates do not establish a full bill. Automatic paid Grok scheduling remains disabled pending separate reviewed enablement. The 64 MiB bound can increase memory use for verbose sessions, and larger files still fail closed. No automatic legacy-agent migration occurs. ## Model Used OpenAI GPT-6 through Codex, with tool use and code execution. The exact serving model identifier and context-window size are not exposed in this session. ## 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 - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip --- .dockerignore | 2 + .env.runner-e2e.example | 4 + .github/workflows/docker-runner-check.yml | 174 ++++++++++- Dockerfile | 1 + doc/evals.md | 6 + doc/grok-native-runner.md | 85 ++++++ docker/daytona-runner/Dockerfile | 11 +- docker/daytona-runner/README.md | 12 +- .../codex-local/src/ui/build-config.ts | 4 +- .../adapters/grok-local/src/server/index.ts | 2 + packages/grok-acp/.gitignore | 1 + packages/grok-acp/install.mjs | 25 ++ packages/grok-acp/launcher.cjs | 66 ++++ packages/grok-acp/package.json | 24 ++ packages/grok-acp/platforms.json | 13 + packages/paperclip-runner/package.json | 4 +- .../runner-core/src/acpx_provider_backend.rs | 10 +- .../runner-core/src/acpx_provider_session.rs | 5 +- .../runner-core/src/acpx_sidecar_transport.rs | 3 +- .../tests/acpx_provider_session.rs | 4 +- .../scripts/build-provider-pack.mjs | 12 + .../scripts/grok-native-smoke.mjs | 95 ++++++ .../src/backends/codex-native-backend.ts | 2 +- .../src/cli/acpx-runtime-sidecar.test.ts | 2 +- .../src/cli/acpx-runtime-sidecar.ts | 10 +- .../src/cli/eval-session-contract.test.ts | 7 + .../src/cli/eval-session-contract.ts | 4 +- .../paperclip-runner/src/cli/eval-session.ts | 3 +- .../src/contracts/native-execution.ts | 6 +- .../durable-prp-control-plane.ts | 2 + .../src/drivers/acpx/codex-acpx-driver.ts | 10 +- .../acpx/codex-runtime-adapter.test.ts | 21 ++ .../src/drivers/acpx/codex-runtime-adapter.ts | 16 +- .../src/drivers/acpx/driver-profile.test.ts | 2 + .../src/drivers/acpx/driver-profile.ts | 11 +- .../src/drivers/acpx/environment.ts | 4 +- .../src/drivers/acpx/grok-credentials.test.ts | 95 ++++++ .../src/drivers/acpx/grok-credentials.ts | 92 ++++++ .../acpx/installation-integrity.test.ts | 111 ++++++- .../drivers/acpx/installation-integrity.ts | 29 +- .../src/drivers/acpx/private-snapshot.ts | 3 + .../drivers/acpx/qualified-profiles.test.ts | 2 +- .../src/drivers/acpx/qualified-profiles.ts | 10 +- .../src/drivers/acpx/runtime-host.test.ts | 19 ++ .../src/drivers/acpx/runtime-host.ts | 12 +- .../src/drivers/acpx/runtime-sandbox.test.ts | 21 +- .../src/drivers/acpx/runtime-sandbox.ts | 22 ++ .../src/eval/live-workflow-executor.ts | 3 +- packages/paperclip-runner/src/index.ts | 2 +- .../src/issue-thread/live-projection.ts | 2 +- packages/paperclip-runner/src/live/index.ts | 2 +- .../src/live/live-session.test.ts | 41 ++- .../paperclip-runner/src/live/live-session.ts | 23 +- .../src/live/run-result-feedback.test.ts | 20 ++ .../src/live/run-result-feedback.ts | 18 ++ .../src/live/runnerd-codex-transport.test.ts | 51 ++++ .../src/live/runnerd-codex-transport.ts | 49 ++- .../src/live/runnerd-tool-response.test.ts | 14 + .../src/provider-events.test.ts | 24 ++ .../paperclip-runner/src/provider-events.ts | 17 ++ .../test/acpx-codex-package-contract.test.mjs | 22 ++ packages/shared/src/ai-connections.ts | 2 + patches/acpx@0.13.1.patch | 92 ++++-- server/src/__tests__/adapter-registry.test.ts | 5 +- server/src/__tests__/adapter-routes.test.ts | 11 +- server/src/adapters/registry.test.ts | 26 +- server/src/adapters/registry.ts | 23 +- .../grok-runner-credentials.test.ts | 34 +++ .../native-runtime/grok-runner-credentials.ts | 32 ++ .../native-session-executor.test.ts | 284 +++++++++++++++++- .../native-runtime/native-session-executor.ts | 161 ++++++++-- .../native-runtime/provider-profile.ts | 3 +- tests/e2e/chat-adapters-ui-providers.spec.ts | 6 +- tests/runner-e2e/FIXTURES.md | 8 + tests/runner-e2e/README.md | 16 + tests/runner-e2e/SECURITY.md | 8 + tests/runner-e2e/catalog.test.ts | 49 ++- tests/runner-e2e/catalog.ts | 52 +++- tests/runner-e2e/daytona-image-content.ts | 4 + tests/runner-e2e/daytona-image.test.ts | 13 + .../grok-subscription-fixture.test.ts | 54 ++++ tests/runner-e2e/grok-subscription-fixture.ts | 42 +++ tests/runner-e2e/harness-env.ts | 5 +- tests/runner-e2e/history.test.ts | 3 + tests/runner-e2e/live-fixtures.ts | 21 ++ tests/runner-e2e/redaction.ts | 26 +- tests/runner-e2e/run-observations.ts | 13 + tests/runner-e2e/runner.spec.ts | 5 +- .../runner-e2e/select-rerun-artifacts.test.ts | 31 ++ tests/runner-e2e/select-rerun-artifacts.ts | 13 +- tests/runner-e2e/support.test.ts | 26 ++ tests/runner-e2e/types.ts | 2 + tests/runner-e2e/workflow-security.test.ts | 17 +- ui/public/sw.js | 9 + ui/src/adapters/codex-local/config-fields.tsx | 12 +- ui/src/components/AgentConfigForm.tsx | 3 +- .../new-agent/AgentBasicsDialog.tsx | 1 + ui/src/components/new-agent/NewAgentSetup.tsx | 12 +- .../lib/service-worker-cache-policy.test.ts | 11 + 99 files changed, 2302 insertions(+), 200 deletions(-) create mode 100644 doc/grok-native-runner.md create mode 100644 packages/grok-acp/.gitignore create mode 100644 packages/grok-acp/install.mjs create mode 100755 packages/grok-acp/launcher.cjs create mode 100644 packages/grok-acp/package.json create mode 100644 packages/grok-acp/platforms.json create mode 100644 packages/paperclip-runner/scripts/grok-native-smoke.mjs create mode 100644 packages/paperclip-runner/src/drivers/acpx/grok-credentials.test.ts create mode 100644 packages/paperclip-runner/src/drivers/acpx/grok-credentials.ts create mode 100644 packages/paperclip-runner/src/live/run-result-feedback.test.ts create mode 100644 packages/paperclip-runner/src/live/run-result-feedback.ts create mode 100644 packages/paperclip-runner/src/live/runnerd-tool-response.test.ts create mode 100644 server/src/services/native-runtime/grok-runner-credentials.test.ts create mode 100644 server/src/services/native-runtime/grok-runner-credentials.ts create mode 100644 tests/runner-e2e/grok-subscription-fixture.test.ts create mode 100644 tests/runner-e2e/grok-subscription-fixture.ts diff --git a/.dockerignore b/.dockerignore index ee773610d57..7aaa533fe76 100644 --- a/.dockerignore +++ b/.dockerignore @@ -42,3 +42,5 @@ packages/paperclip-runner/scripts/*-smoke.mjs # they must survive the *.test.ts rule above. !packages/paperclip-runner/src/**/*.test.ts !packages/paperclip-runner/src/**/*.test.tsx + +packages/grok-acp/bin diff --git a/.env.runner-e2e.example b/.env.runner-e2e.example index 85c99a7836a..1e208f879fb 100644 --- a/.env.runner-e2e.example +++ b/.env.runner-e2e.example @@ -2,9 +2,13 @@ # Never commit the local file or put these values in fixture source. OPENAI_API_KEY= ANTHROPIC_API_KEY= +XAI_API_KEY= OPENROUTER_API_KEY= DAYTONA_API_KEY= # Required only for Daytona cells. Use an immutable, anonymously pullable # digest from the Runner Full-Stack E2E image job or a locally published image. PAPERCLIP_E2E_DAYTONA_IMAGE=ghcr.io/paperclipai/paperclip-daytona-runner@sha256:REPLACE_ME +# Optional, explicit subscription qualification only. Supply privately at runtime; +# never commit an actual auth.json payload. API-key profiles do not use this value. +# GROK_AUTH_JSON= diff --git a/.github/workflows/docker-runner-check.yml b/.github/workflows/docker-runner-check.yml index 1f6002b4cd8..39d148a7925 100644 --- a/.github/workflows/docker-runner-check.yml +++ b/.github/workflows/docker-runner-check.yml @@ -1,6 +1,28 @@ name: Docker Runner check on: + workflow_dispatch: + inputs: + target_branch: + description: "Branch in this repository to build; resolved to one immutable commit before checkout" + type: string + required: false + publish_eval_image: + description: "Publish an immutable Daytona qualification image on the EC2 fleet (no provider credentials)" + type: boolean + default: false + verify_source: + description: "Run broad source checks on EC2" + type: boolean + default: false + verify_public_install: + description: "Build and verify clean public npm installation on EC2 (no provider credentials)" + type: boolean + default: false + build_eval_viewer: + description: "Build the canonical eval report viewer on EC2" + type: boolean + default: false pull_request: paths: - .github/workflows/docker-runner-check.yml @@ -14,11 +36,13 @@ on: permissions: {} concurrency: - group: docker-runner-check-${{ github.event.pull_request.number }} + # Publishing a newer image must not discard an earlier verification's evidence. + group: docker-runner-check-${{ github.event.pull_request.number || github.ref }}-${{ inputs.verify_source && 'verification' || inputs.verify_public_install && 'public-install' || inputs.build_eval_viewer && 'viewer' || 'image' }} cancel-in-progress: true jobs: runner: + if: github.event_name == 'pull_request' name: Compile isolated native Runner runs-on: ubuntu-latest timeout-minutes: 20 @@ -39,3 +63,151 @@ jobs: # No registry credentials or image publication. - name: Verify native build and dependency cache reuse run: bash scripts/check-docker-runner-cache.sh + + + authorize_manual: + if: github.event_name == 'workflow_dispatch' + runs-on: ubuntu-latest + permissions: + contents: read + timeout-minutes: 5 + outputs: + target_sha: ${{ steps.authorize.outputs.target_sha }} + steps: + - name: Authorize an explicit maintainer image build + id: authorize + env: + GH_TOKEN: ${{ github.token }} + REPOSITORY: ${{ github.repository }} + REPOSITORY_ID: ${{ github.repository_id }} + ACTOR_ID: ${{ github.actor_id }} + TRIGGERING_ACTOR: ${{ github.triggering_actor }} + ALLOWED_IDS: ${{ vars.AWS_CI_TRUSTED_USER_IDS }} + TARGET_BRANCH: ${{ inputs.target_branch || github.ref_name }} + run: | + set -euo pipefail + test "$REPOSITORY" = paperclipai/paperclip + test "$REPOSITORY_ID" = 1170821064 + jq -e 'type == "array" and length > 0 and all(.[]; type == "number")' <<< "$ALLOWED_IDS" >/dev/null + triggering_id="$(gh api "users/$TRIGGERING_ACTOR" --jq .id)" + for id in "$ACTOR_ID" "$triggering_id"; do + jq -e --argjson id "$id" 'index($id) != null' <<< "$ALLOWED_IDS" >/dev/null + done + target_sha="$(gh api "repos/$REPOSITORY/git/ref/heads/$TARGET_BRANCH" --jq '.object.sha')" + [[ "$target_sha" =~ ^[0-9a-f]{40}$ ]] + echo "target_sha=$target_sha" >> "$GITHUB_OUTPUT" + + manual_image: + name: Build and verify on EC2 + needs: authorize_manual + runs-on: runs-on/fleet=paperclip-public-pr-x64/env=public-ci + timeout-minutes: 90 + permissions: + contents: read + packages: write + steps: + - name: Authorize an explicit maintainer image build + env: + GH_TOKEN: ${{ github.token }} + REPOSITORY: ${{ github.repository }} + REPOSITORY_ID: ${{ github.repository_id }} + ACTOR_ID: ${{ github.actor_id }} + TRIGGERING_ACTOR: ${{ github.triggering_actor }} + ALLOWED_IDS: ${{ vars.AWS_CI_TRUSTED_USER_IDS }} + run: | + set -euo pipefail + test "$REPOSITORY" = paperclipai/paperclip + test "$REPOSITORY_ID" = 1170821064 + jq -e 'type == "array" and length > 0 and all(.[]; type == "number")' <<< "$ALLOWED_IDS" >/dev/null + triggering_id="$(gh api "users/$TRIGGERING_ACTOR" --jq .id)" + for id in "$ACTOR_ID" "$triggering_id"; do + jq -e --argjson id "$id" 'index($id) != null' <<< "$ALLOWED_IDS" >/dev/null + done + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + with: + ref: ${{ needs.authorize_manual.outputs.target_sha }} + persist-credentials: false + - uses: pnpm/action-setup@0977fd99725f1db4007ccb2928dbb4e90d06cc86 # v6 + with: + version: 9.15.4 + - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7 + with: + node-version: 24 + package-manager-cache: false + - name: Resolve source dependencies without lifecycle scripts + run: | + pnpm install --resolution-only --ignore-scripts --no-frozen-lockfile + mkdir -p remote-verification + sha256sum pnpm-lock.yaml > remote-verification/lock.sha256 + git rev-parse HEAD > remote-verification/source.txt + - uses: docker/setup-buildx-action@37fe631027851001ddb9b187196cc803df7f5f0e # v4 + - if: inputs.publish_eval_image + uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Build checksum-verified Grok provider image + if: inputs.publish_eval_image + env: + SOURCE_SHA: ${{ needs.authorize_manual.outputs.target_sha }} + run: | + set -euo pipefail + image="ghcr.io/paperclipai/paperclip-daytona-runner:qualification-${SOURCE_SHA}-${GITHUB_RUN_ID}" + lock_sha="$(sha256sum pnpm-lock.yaml | cut -d ' ' -f 1)" + docker buildx build --platform linux/amd64 \ + --file docker/daytona-runner/Dockerfile \ + --build-arg "PAPERCLIP_RUNNER_SOURCE_REVISION=$SOURCE_SHA" \ + --build-arg "PAPERCLIP_RUNNER_CONTENT_ID=qualification-$SOURCE_SHA" \ + --build-arg "PAPERCLIP_RUNNER_LOCK_SHA256=$lock_sha" \ + --cache-from type=registry,ref=ghcr.io/paperclipai/paperclip-daytona-runner:e2e-buildcache-amd64 \ + --tag "$image" --metadata-file remote-verification/image.json --push . + digest="$(jq -r '."containerimage.digest"' remote-verification/image.json)" + [[ "$digest" =~ ^sha256:[0-9a-f]{64}$ ]] + immutable="${image%:*}@$digest" + docker logout ghcr.io + docker buildx imagetools inspect "$immutable" >/dev/null + echo "$immutable" > remote-verification/image.txt + echo "Image: $immutable" >> "$GITHUB_STEP_SUMMARY" + - name: Verify repository on EC2 + if: inputs.verify_source + # Leave time for artifact retention before the fleet's one-hour lifetime. + timeout-minutes: 38 + run: | + set -uo pipefail + pnpm install --frozen-lockfile --ignore-scripts + status=0 + for check in 'pnpm -r typecheck' 'pnpm test:run' 'pnpm check:token-gates' 'pnpm test:e2e:runner:typecheck' 'pnpm test:e2e:runner:unit' 'pnpm build' 'if [ -f scripts/verify-grok-npm-install.mjs ]; then node scripts/verify-grok-npm-install.mjs; fi'; do + label="$(echo "$check" | tr -cs 'a-zA-Z0-9' '-')" + echo "Starting $check" + check_status=0 + timeout --signal=TERM --kill-after=15s 15m bash -c "$check" > "remote-verification/$label.log" 2>&1 || check_status=$? + printf '%s\t%s\n' "$check_status" "$check" >> remote-verification/check-status.tsv + if [ "$check_status" -ne 0 ]; then status=1; fi + echo "Finished $check (exit $check_status)" + done + exit "$status" + - name: Verify clean public npm installation + if: inputs.verify_public_install && !inputs.verify_source + timeout-minutes: 35 + run: | + set -euo pipefail + test -f scripts/verify-grok-npm-install.mjs || { echo "Selected source does not provide the public-install verifier"; exit 1; } + pnpm install --frozen-lockfile --ignore-scripts > remote-verification/npm-setup.log 2>&1 + pnpm build > remote-verification/npm-build.log 2>&1 + node scripts/verify-grok-npm-install.mjs > remote-verification/public-npm-install.log 2>&1 + - name: Build canonical eval report viewer + if: always() && (inputs.verify_source || inputs.build_eval_viewer) + run: | + set -euo pipefail + pnpm install --frozen-lockfile --ignore-scripts --filter '@paperclipai/paperclip-runner...' + pnpm --filter @paperclipai/paperclip-runner build:issue-thread > remote-verification/viewer-build.log 2>&1 + tar -czf remote-verification/eval-viewer.tar.gz -C packages/paperclip-runner dist-issue-thread + sha256sum remote-verification/eval-viewer.tar.gz > remote-verification/eval-viewer.sha256 + - name: Retain source, image and verification evidence + if: always() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: grok-remote-verification-${{ github.run_id }} + path: remote-verification/ + retention-days: 7 diff --git a/Dockerfile b/Dockerfile index eda927f77af..9d4d81ca906 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,6 +22,7 @@ COPY packages/shared/package.json packages/shared/ COPY packages/db/package.json packages/db/ COPY packages/adapter-utils/package.json packages/adapter-utils/ COPY packages/google-sheets-mcp-server/package.json packages/google-sheets-mcp-server/ +COPY packages/grok-acp/package.json packages/grok-acp/ COPY packages/kv-demo-mcp-server/package.json packages/kv-demo-mcp-server/ COPY packages/mcp-server/package.json packages/mcp-server/ COPY packages/paperclip-eval-kernel/package.json packages/paperclip-eval-kernel/ diff --git a/doc/evals.md b/doc/evals.md index f343f07c01d..9fa207158f2 100644 --- a/doc/evals.md +++ b/doc/evals.md @@ -77,6 +77,12 @@ fixture-driven file wait and persisted-plan oracle are documented in the [Product E2E guide](../tests/runner-e2e/README.md). It does not qualify the native onboarding wizard or change the native API-tool rollout defaults. +The explicit-only `grok-qualification` and `grok-subscription-qualification` +Product suites exercise Grok Build with API and company subscription +authentication respectively. Keep their results separate; the subscription +fixture seeds an explicitly supplied login and does not qualify interactive +login. See the [Grok fixture contract](../tests/runner-e2e/README.md#grok-build-qualification). + ## Validation ladder Start with credential-free checks and a catalog listing. For Product E2E: diff --git a/doc/grok-native-runner.md b/doc/grok-native-runner.md new file mode 100644 index 00000000000..bc8373f4b78 --- /dev/null +++ b/doc/grok-native-runner.md @@ -0,0 +1,85 @@ +# Grok Build native runner + +Select **Grok Build** in the native runner provider selector. The stored contract is +`adapterType: "paperclip_runner"` with `provider: "acpx"`, `acpxAgent: "grok"`, +and `model: "grok-4.7"`. Existing `grok_local` agents keep their legacy adapter. + +Grok Build speaks [ACP over stdio](https://github.com/xai-org/grok-build/blob/main/crates/codegen/xai-grok-pager/docs/user-guide/15-agent-mode.md). +The runner owns `grok agent --no-leader stdio` through ACPX, including session +identity, cancellation, recovery and the authenticated Paperclip MCP bridge. +It does not add `--always-approve`. Restricted operations use the selected ACPX +permission policy and return the existing approval-required outcome. Isolated ask +rules override project allow rules, and compatible always-approve settings are +locked off. Compatible hook/MCP discovery and shell login capture are disabled. + +## Installation and identity + +Run `pnpm --filter @paperclipai/paperclip-runner install:grok` after installing +workspace dependencies. This explicitly downloads Grok Build 1.0.13, verifies +the native executable digest in `packages/grok-acp/platforms.json`, and installs +it privately. Only macOS arm64 and Linux x64 are admitted. Provider packs install +the same verified binary. No ambient `grok` from PATH is used by the native runner. +ACP must report the requested exact model; absent or mismatched identities fail. + +Instructions use Grok ACP session rules. Assigned skills live in the isolated +Grok home. Steering and goals are unsupported. Token and cost values remain +unknown when Grok does not report them; missing usage is not zero usage. + +## Authentication + +Use the existing Grok company connection/login flow for subscription execution. +An explicitly selected company-secret `XAI_API_KEY` selects paid API execution. +There is no automatic subscription-to-API fallback. Remote execution cannot +borrow the operator's home credentials. Local execution can use the operator's +existing Grok login when no company login has been selected. + +Only the selected credential is staged in the private runtime home. An ownership +lease fences concurrent processes. Before ACP startup, an expiring subscription +credential is refreshed through the verified Grok executable’s non-inference +`models` command. This bounded step suppresses output and prevents Grok 1.0.13 +from caching a pre-refresh model list. Exact model verification still precedes +any prompt; refresh failure requires reconnecting Grok Build. After the provider exits, refreshed credentials +are copied back through Grok's existing identity and refresh checks. Runtime +credentials, refresh handoffs, and diagnostic logs are excluded from workspace +backups and removed on close. Session history remains available for resume. Host +configuration, other provider credentials and unselected keys are not forwarded +to Grok. + +## Evaluation + +The private `paperclip-evals` repository maintains `live-acpx-grok-4.7.json` and +`rosters/live-acpx-grok.json`. The roster covers all 39 protocol cases. Its campaign +lane remains disabled pending complete live qualification. + +Product E2E exposes `runner-acpx-grok` in core local/Daytona compatibility and +local session integrity. The explicit `grok-qualification` suite covers replies, +planning approvals, structured questions, downloadable project revisions, stop/resume +and continuation after controller restart +in both environments. Run with `--suite grok-qualification`; it is excluded from +scheduled `--all`. Use the canonical Product E2E dashboard and Evalbook reports. + +The separate `grok-subscription-qualification` suite covers the same workflows +with the explicit `GROK_AUTH_JSON` fixture credential. It seeds only the disposable +company's private login home and supplies no API key. It does not exercise the +interactive sign-in UI. Authentication mode remains part of the profile identity. +After a subscription upgrade, a fresh `grok login` may be needed if the existing +login still reports the previous entitlement through ACP. + +`packages/paperclip-runner/scripts/grok-native-smoke.mjs` records explicit auth, +model/binary identity, MCP outcome, durable resume and restrictive permissions. +Pass `--auth subscription --auth-file /private/path/auth.json --output /private/report.json` +or `--auth api --output /private/report.json` with an explicitly supplied key. +Each attempt is retained; unknown usage and cost are null. + +Qualification requires the full protocol roster, selected product workflows, +subscription and API execution locally and on Daytona, and three successful +repetitions of core tool, approval and resume cases. Deterministic tests or a +single successful browser task do not establish that qualification. + +## Remote verification + +Do not run Docker on a developer laptop when using remote verification. The +`Docker Runner check` workflow offers maintainer-authorized manual EC2 image +builds and broad source checks without provider credentials. It records the +source revision, resolved lock digest and immutable image reference. Paid +Product E2E remains behind the protected default-branch workflow and environment. diff --git a/docker/daytona-runner/Dockerfile b/docker/daytona-runner/Dockerfile index 3f471f5f653..e3a5fa795df 100644 --- a/docker/daytona-runner/Dockerfile +++ b/docker/daytona-runner/Dockerfile @@ -18,9 +18,9 @@ WORKDIR /workspace COPY package.json pnpm-lock.yaml pnpm-workspace.yaml .npmrc tsconfig.base.json ./ COPY patches ./patches COPY scripts/link-plugin-dev-sdk.mjs ./scripts/link-plugin-dev-sdk.mjs -# CI owns pnpm-lock.yaml and regenerates it for manifest-only PRs. Include the -# complete workspace manifest graph so all source-owned patches apply, then -# resolve before the frozen install. This happens only during image creation. +# Include the complete workspace manifest graph so source-owned patches apply. +# Verify the reviewed lockfile as supplied; never resolve dependencies again +# against the registry during an immutable image build. COPY packages ./packages COPY server/package.json ./server/package.json COPY ui/package.json ./ui/package.json @@ -28,9 +28,8 @@ COPY cli/package.json ./cli/package.json # The complete resolved lock (including transitive integrity hashes) is reviewed. # Reject registry-time drift BEFORE installing packages or running lifecycle code. # Refresh this digest together with source/provider dependency changes. -ARG PAPERCLIP_RUNNER_LOCK_SHA256=57b298aceebc48bb94ea0593347348256475da7b2fddb77025d9e57cc8759420 -RUN pnpm install --resolution-only --ignore-scripts --no-frozen-lockfile \ - && printf '%s pnpm-lock.yaml\n' "${PAPERCLIP_RUNNER_LOCK_SHA256}" > /tmp/provider-lock.sha256 \ +ARG PAPERCLIP_RUNNER_LOCK_SHA256=0c06ab38c1d4b6b58e1c9f5ee85eb79f97cecd83e751b5d27c3b262faf5ab265 +RUN printf '%s pnpm-lock.yaml\n' "${PAPERCLIP_RUNNER_LOCK_SHA256}" > /tmp/provider-lock.sha256 \ && sha256sum -c /tmp/provider-lock.sha256 \ && pnpm install --frozen-lockfile --filter '@paperclipai/paperclip-runner...' ARG PAPERCLIP_RUNNER_SOURCE_REVISION diff --git a/docker/daytona-runner/README.md b/docker/daytona-runner/README.md index bee316460ad..f262fa72cd5 100644 --- a/docker/daytona-runner/README.md +++ b/docker/daytona-runner/README.md @@ -95,8 +95,9 @@ Do not bake provider credentials, Paperclip bootstrap tickets, or Daytona preview tokens into this image. They remain per-run secret material. Provider CLI updates are manifest-only changes: repository CI owns the root -lockfile. The image build resolves the complete workspace manifest graph before -its frozen install, matching CI when a source commit precedes the lockfile bot. +lockfile. Resolve the complete workspace manifest graph in the build context +before invoking Docker, matching CI when a source commit precedes the lockfile +bot. The trusted workflow supplies this resolved lockfile as an immutable artifact. The complete resolved lockfile must match `PAPERCLIP_RUNNER_LOCK_SHA256` before package installation or lifecycle execution. Review and refresh that digest with source dependency changes; registry-time resolution drift fails closed. @@ -105,6 +106,7 @@ verifies the downloaded artifact, then passes that artifact's SHA-256 as the `PAPERCLIP_RUNNER_LOCK_SHA256` build argument. The Dockerfile checks the resolved lock against this value before installation. The fixed Dockerfile default is for standalone builds; it must not replace a campaign's verified lock digest. -Keep one latest stable CLI installation per provider; refresh exact runtime -versions and qualification digests together, never install a private older copy -or download dependencies when a task starts. +Refresh exact runtime versions and qualification digests together; never download +dependencies when a task starts. Grok's additive native ACP profile keeps its +qualified 1.0.13 executable at a verified package path. It does not replace the +legacy adapter's `grok` command on PATH. diff --git a/packages/adapters/codex-local/src/ui/build-config.ts b/packages/adapters/codex-local/src/ui/build-config.ts index b2835539206..25f2010392c 100644 --- a/packages/adapters/codex-local/src/ui/build-config.ts +++ b/packages/adapters/codex-local/src/ui/build-config.ts @@ -233,8 +233,8 @@ export function buildPaperclipRunnerConfig(v: CreateConfigValues): Record createHash("sha256").update(bytes).digest("hex"); +const installed = await readFile(destination).catch(() => null); +if (!installed || digest(installed) !== platform.sha256) { + const url = `https://x.ai/cli/grok-${manifest.version}-${platform.artifact}.gz`; + const response = await fetch(url, { signal: AbortSignal.timeout(120_000) }); + if (!response.ok) throw new Error(`Grok download failed: HTTP ${response.status}`); + const bytes = gunzipSync(Buffer.from(await response.arrayBuffer()), { maxOutputLength: 384 * 1024 * 1024 }); + if (digest(bytes) !== platform.sha256) throw new Error("Grok binary digest mismatch"); + await mkdir(new URL("./bin/", import.meta.url), { recursive: true }); + const temporary = `${destination}.${process.pid}.tmp`; + try { + await writeFile(temporary, bytes, { flag: "wx", mode: 0o700 }); + await rename(temporary, destination); + } finally { await rm(temporary, { force: true }); } +} +await chmod(destination, 0o700); +console.log(`Verified Grok ${manifest.version} (${process.platform}-${process.arch})`); diff --git a/packages/grok-acp/launcher.cjs b/packages/grok-acp/launcher.cjs new file mode 100755 index 00000000000..a6b7339c7ae --- /dev/null +++ b/packages/grok-acp/launcher.cjs @@ -0,0 +1,66 @@ +// This launcher is admitted by digest and receives a verified native executable. +// Never resolve an executable through PATH or accept command-line overrides. +const { isAbsolute, join } = require("node:path"); +const { constants, openSync, fstatSync, readSync, closeSync } = require("node:fs"); +const { spawnSync } = require("node:child_process"); +const executable = process.env.PAPERCLIP_GROK_VERIFIED_EXECUTABLE; +if (!executable || !isAbsolute(executable)) { + throw new Error("Verified Grok executable is unavailable"); +} +const environment = { ...process.env }; +delete environment.PAPERCLIP_GROK_VERIFIED_EXECUTABLE; + +// Grok 1.0.13 can cache the pre-refresh model list during ACP initialization. +// Refresh an expiring subscription first, without inference or protocol output. +// The verified launcher remains the fenced owner throughout this subprocess. +if (!environment.XAI_API_KEY && environment.GROK_HOME && isAbsolute(environment.GROK_HOME)) { + let auth; + try { + const fd = openSync(join(environment.GROK_HOME, "auth.json"), constants.O_RDONLY | constants.O_NOFOLLOW); + try { + const stat = fstatSync(fd); + if (!stat.isFile() || stat.uid !== process.getuid() || (stat.mode & 0o777) !== 0o600 || stat.size > 256 * 1024) { + throw new Error("Grok subscription credential is invalid"); + } + const bytes = Buffer.alloc(256 * 1024 + 1); + try { + let size = 0; + while (size < bytes.length) { + const count = readSync(fd, bytes, size, bytes.length - size, size); + if (count === 0) break; + size += count; + } + if (size > 256 * 1024) throw new Error("Grok subscription credential exceeds its bound"); + auth = JSON.parse(bytes.subarray(0, size).toString("utf8")); + } finally { bytes.fill(0); } + } finally { closeSync(fd); } + } catch (error) { + if (error.code !== "ENOENT") throw new Error("Grok subscription credential is invalid"); + } + const entries = auth && typeof auth === "object" && !Array.isArray(auth) ? Object.values(auth) : []; + const rawExpiry = entries.length === 1 ? entries[0]?.expires_at : undefined; + // Match the managed Grok auth contract: ISO-8601, epoch seconds, or milliseconds. + const isoExpiry = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/; + const expiry = typeof rawExpiry === "number" && Number.isFinite(rawExpiry) + ? rawExpiry < 1e12 ? rawExpiry * 1000 : rawExpiry + : typeof rawExpiry === "string" && isoExpiry.test(rawExpiry) ? Date.parse(rawExpiry) : NaN; + if (Number.isFinite(expiry) && expiry <= Date.now() + 60_000) { + // Descriptor-backed executables must retain their exact descriptor in the + // child. Other descriptors, including ACP stdin/stdout, are not forwarded. + const stdio = ["ignore", "ignore", "ignore"]; + const descriptor = /^\/(?:proc\/self\/fd|dev\/fd)\/(\d+)$/.exec(executable); + if (descriptor) { + const fd = Number(descriptor[1]); + if (!Number.isSafeInteger(fd) || fd < 3 || fd > 1024) throw new Error("Verified Grok descriptor is invalid"); + while (stdio.length <= fd) stdio.push("ignore"); + stdio[fd] = fd; + } + const refreshed = spawnSync(executable, ["models"], { + env: environment, stdio, timeout: 15_000, killSignal: "SIGKILL", + }); + if (refreshed.error || refreshed.status !== 0 || refreshed.signal) { + throw new Error("Grok subscription refresh failed; reconnect Grok Build"); + } + } +} +process.execve(executable, [executable, "agent", "--no-leader", "stdio"], environment); diff --git a/packages/grok-acp/package.json b/packages/grok-acp/package.json new file mode 100644 index 00000000000..79d5522de79 --- /dev/null +++ b/packages/grok-acp/package.json @@ -0,0 +1,24 @@ +{ + "name": "@paperclipai/grok-acp", + "version": "1.0.13", + "private": true, + "license": "MIT", + "bin": { + "paperclip-grok-acp": "launcher.cjs" + }, + "files": [ + "launcher.cjs", + "bin", + "install.mjs", + "platforms.json" + ], + "scripts": { + "install:binary": "node install.mjs" + }, + "exports": { + "./package.json": "./package.json" + }, + "engines": { + "node": ">=24.11.0" + } +} diff --git a/packages/grok-acp/platforms.json b/packages/grok-acp/platforms.json new file mode 100644 index 00000000000..7731434c275 --- /dev/null +++ b/packages/grok-acp/platforms.json @@ -0,0 +1,13 @@ +{ + "version": "1.0.13", + "platforms": { + "darwin-arm64": { + "artifact": "macos-aarch64", + "sha256": "8669e0fdadceec25b8c159c355f427ffbd82583525d774b6ab1522197ea83b80" + }, + "linux-x64": { + "artifact": "linux-x86_64", + "sha256": "edf79521581bb5e6b95abef848491a6a742e860da3e237ebe86a280d30dce4c1" + } + } +} diff --git a/packages/paperclip-runner/package.json b/packages/paperclip-runner/package.json index ebc0614e39e..8ab714ec921 100644 --- a/packages/paperclip-runner/package.json +++ b/packages/paperclip-runner/package.json @@ -160,6 +160,7 @@ "browser:preview": "vite preview --config vite.config.ts", "verify": "pnpm run build && pnpm run typecheck && pnpm run check:replay-goldens && pnpm run test && pnpm run test:sdk && pnpm run test:scenarios && pnpm run check:browser-tokens && pnpm run test:browser && pnpm run test:browser:sdk && pnpm run test:browser:scenarios && pnpm run check:forbidden-imports && pnpm run check:tracked-imports && pnpm run check:numbered-milestones && pnpm run check:package-boundaries && pnpm run check:clean-consumers && pnpm run docs:validate && pnpm run check:conformance-parity && pnpm run check:replay-parity && pnpm run trace:conformance && pnpm run replay:fixture && pnpm run trace:local-runner -- --quiet", "verify:rootless": "bash scripts/verify-rootless-linux.sh", + "install:grok": "pnpm --filter @paperclipai/grok-acp install:binary", "test:opencode:qualification": "PAPERCLIP_OPENCODE_QUALIFY=1 node --test scripts/qualify-opencode-runtime.test.mjs" }, "dependencies": { @@ -172,7 +173,8 @@ "opencode-ai": "1.18.32", "react-markdown": "^10.1.0", "remark-gfm": "^4.0.1", - "smol-toml": "^1.4.2" + "smol-toml": "^1.4.2", + "@paperclipai/grok-acp": "workspace:*" }, "peerDependencies": { "react": ">=18", diff --git a/packages/paperclip-runner/runner/crates/runner-core/src/acpx_provider_backend.rs b/packages/paperclip-runner/runner/crates/runner-core/src/acpx_provider_backend.rs index 41aa785e585..22581e7d2a5 100644 --- a/packages/paperclip-runner/runner/crates/runner-core/src/acpx_provider_backend.rs +++ b/packages/paperclip-runner/runner/crates/runner-core/src/acpx_provider_backend.rs @@ -162,6 +162,14 @@ impl AcpxProviderDescriptor { Some("0.156.0"), "sha256:c4538599d1ab767db5dff50934f13bb5ba313a59d9c4a83e993fac4617ea63d3", ), + "grok" => ( + "grok-4.7", + "@paperclipai/grok-acp", + "1.0.13", + Some("@paperclipai/grok-acp"), + Some("1.0.13"), + "sha256:f0b698395a3704ed2ffaf84ea19bdb20c36c8a0a70b7c629c7b6ffe144e59e55", + ), "pi" => return Err(DurableRunnerError::invalid( "ACPX agent pi is not executable through the verified runnerd provider boundary", )), @@ -176,7 +184,7 @@ impl AcpxProviderDescriptor { || self.driver != "acpx_runtime" || self.provider_version != "0.13.1" || self.acpx_version != "0.13.1" - || (self.agent != "claude" && self.model != expected.0) + || (self.agent != "claude" && self.agent != "grok" && self.model != expected.0) || self.model.trim().is_empty() || self.model.len() > 1024 || self.agent_server_package != expected.1 diff --git a/packages/paperclip-runner/runner/crates/runner-core/src/acpx_provider_session.rs b/packages/paperclip-runner/runner/crates/runner-core/src/acpx_provider_session.rs index 09038b02424..c64d02bc5b7 100644 --- a/packages/paperclip-runner/runner/crates/runner-core/src/acpx_provider_session.rs +++ b/packages/paperclip-runner/runner/crates/runner-core/src/acpx_provider_session.rs @@ -74,14 +74,15 @@ impl AcpxProviderSessionConfig { self.transport.validate()?; let qualified_model = match self.agent.as_str() { "claude" => "claude-sonnet-5", + "grok" => "grok-4.7", "codex" => "gpt-5.6-sol", _ => { return Err(LocalRunnerError::invalid( - "ACPX agent must be claude or codex", + "ACPX agent must be claude, codex, or grok", )) } }; - if self.agent != "claude" && self.model != qualified_model { + if self.agent != "claude" && self.agent != "grok" && self.model != qualified_model { return Err(LocalRunnerError::invalid(format!( "ACPX {} profile requires exact model {qualified_model}", self.agent diff --git a/packages/paperclip-runner/runner/crates/runner-core/src/acpx_sidecar_transport.rs b/packages/paperclip-runner/runner/crates/runner-core/src/acpx_sidecar_transport.rs index 86602e4713d..409b87cf35f 100644 --- a/packages/paperclip-runner/runner/crates/runner-core/src/acpx_sidecar_transport.rs +++ b/packages/paperclip-runner/runner/crates/runner-core/src/acpx_sidecar_transport.rs @@ -98,9 +98,10 @@ impl AcpxSidecarTransport { let credential_keys: &[&str] = match agent { "claude" => &["ANTHROPIC_API_KEY", "CLAUDE_CODE_OAUTH_TOKEN"], "codex" => &["OPENAI_API_KEY", "CODEX_API_KEY"], + "grok" => &["XAI_API_KEY", "PAPERCLIP_ACPX_GROK_AUTH_JSON_SECRET"], _ => { return Err(LocalRunnerError::invalid( - "ACPX sidecar credentials require a qualified claude or codex agent", + "ACPX sidecar credentials require a qualified claude, codex, or grok agent", )) } }; diff --git a/packages/paperclip-runner/runner/crates/runner-core/tests/acpx_provider_session.rs b/packages/paperclip-runner/runner/crates/runner-core/tests/acpx_provider_session.rs index b91d49ba21b..7223ae7c5f6 100644 --- a/packages/paperclip-runner/runner/crates/runner-core/tests/acpx_provider_session.rs +++ b/packages/paperclip-runner/runner/crates/runner-core/tests/acpx_provider_session.rs @@ -131,7 +131,7 @@ fn bootstraps_a_codex_session_and_confirms_run_identity() { fn validates_qualified_policy_and_tool_catalog_before_spawning() { let mut invalid_agent = config("bootstrap"); invalid_agent.agent = "pi".to_owned(); - assert!(start_error(&invalid_agent).contains("claude or codex")); + assert!(start_error(&invalid_agent).contains("claude, codex, or grok")); let mut unpinned = config("bootstrap"); unpinned.permission_mode_pinned = false; @@ -155,6 +155,8 @@ fn admits_custom_claude_models_and_legacy_codex_profile() { ("claude", "claude-sonnet-5"), ("claude", "claude-opus-5"), ("claude", "custom-provider-model"), + ("grok", "grok-4.7"), + ("grok", "future-exact-model"), ] { let mut qualified = config("bootstrap"); qualified.agent = agent.to_owned(); diff --git a/packages/paperclip-runner/scripts/build-provider-pack.mjs b/packages/paperclip-runner/scripts/build-provider-pack.mjs index d9bac8ba490..03e5ede50ad 100644 --- a/packages/paperclip-runner/scripts/build-provider-pack.mjs +++ b/packages/paperclip-runner/scripts/build-provider-pack.mjs @@ -188,6 +188,12 @@ try { ); writePortableExecutableShim("node", "node/bin/node"); writePortableExecutableShim("opencode", "opencode-ai/bin/opencode.exe"); + const grokPackage = dirname(packRequire.resolve("@paperclipai/grok-acp/package.json")); + const grokInstall = spawnSync(process.execPath, [join(grokPackage, "install.mjs")], { stdio: "inherit" }); + if (grokInstall.status !== 0) throw new Error("Pinned Grok installation failed"); + // Native ACP launches the verified absolute package path. Do not expose a + // generic grok shim: the legacy adapter owns that command and its version. + writePortableNodeShim("paperclip-grok-acp", "@paperclipai/grok-acp/launcher.cjs"); writePortableNodeShim("acpx", "acpx/dist/cli.js"); writePortableNodeShim( "claude-agent-acp", @@ -293,6 +299,7 @@ try { codex: "0.156.0", opencode: "1.18.32", acpx: "0.13.1", + grok: "1.0.13", claudeAcp: "0.73.0", codexAcp: "1.6.2", }, @@ -307,12 +314,17 @@ try { .update(distDigest) .digest("hex")}`, acpxProfileDigests: { + grok: "sha256:f0b698395a3704ed2ffaf84ea19bdb20c36c8a0a70b7c629c7b6ffe144e59e55", claude: "sha256:9d73d1f0f121fb96cc8badb28c22d5bff02d8582eb2e40360a81c189e1b9422a", codex: "sha256:c4538599d1ab767db5dff50934f13bb5ba313a59d9c4a83e993fac4617ea63d3", }, artifacts: { + grokExecutable: { + path: "node_modules/@paperclipai/grok-acp/bin/grok", + sha256: sha256File(join(grokPackage, "bin/grok")), + }, nodeCommand: { path: nodeCommand, sha256: sha256File(join(temporaryRoot, nodeCommand)), diff --git a/packages/paperclip-runner/scripts/grok-native-smoke.mjs b/packages/paperclip-runner/scripts/grok-native-smoke.mjs new file mode 100644 index 00000000000..0ebfd3dc787 --- /dev/null +++ b/packages/paperclip-runner/scripts/grok-native-smoke.mjs @@ -0,0 +1,95 @@ +import assert from 'node:assert/strict'; +import { randomUUID, createHash } from 'node:crypto'; +import { execFileSync } from 'node:child_process'; +import { readFile, writeFile, mkdir, mkdtemp, rm, realpath } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import { basename, dirname, join, resolve } from 'node:path'; +import { parseArgs } from 'node:util'; +import { createRequire } from 'node:module'; +import { AcpxRuntimeHost } from '../dist/drivers/acpx/runtime-host.js'; +import { openQualifiedAcpxRuntime } from '../dist/drivers/acpx/codex-runtime-adapter.js'; +import { acpxProviderSessionIdentity } from '../dist/drivers/acpx/recovery-identity.js'; +import { QUALIFIED_ACPX_PROFILES } from '../dist/drivers/acpx/qualified-profiles.js'; + +const { values } = parseArgs({ options: { auth: { type: 'string', default: 'subscription' }, 'auth-file': { type: 'string' }, output: { type: 'string' }, repetitions: { type: 'string', default: '3' } } }); +assert(values.output, 'An output path is required to retain every attempt'); +assert(['subscription', 'api'].includes(values.auth)); +const repetitions = Number(values.repetitions); +assert(Number.isSafeInteger(repetitions) && repetitions > 0 && repetitions <= 3); +const environment = { PATH: process.env.PATH }; +let copyBackGrokAuth; +if (values.auth === 'api') { + assert(process.env.XAI_API_KEY, 'Explicit XAI_API_KEY is required'); + environment.XAI_API_KEY = process.env.XAI_API_KEY; +} else { + assert(values['auth-file'], 'An explicit subscription auth-file is required'); + assert.equal(basename(values['auth-file']), 'auth.json', 'Subscription copyback requires an auth.json source'); + const serverRequire=createRequire(new URL('../../../server/package.json',import.meta.url)); + const { tsImport } = await import(serverRequire.resolve('tsx/esm/api')); + ({ copyBackGrokAuth } = await tsImport('../../adapters/grok-local/src/server/grok-auth-copyback.ts', import.meta.url)); + environment.PAPERCLIP_ACPX_GROK_AUTH_JSON_SECRET = await readFile(values['auth-file'], 'utf8'); +} +const report = { schema: 'paperclip.grok-native-smoke.v1', auth: values.auth, sourceRevision: execFileSync('git', ['rev-parse', 'HEAD'], { encoding: 'utf8' }).trim(), sourceDirty: execFileSync('git', ['status', '--porcelain'], { encoding: 'utf8' }).length > 0, profile: QUALIFIED_ACPX_PROFILES.grok, platform: `${process.platform}-${process.arch}`, startedAt: new Date().toISOString(), attempts: [] }; +const binary = await readFile(new URL('../../grok-acp/bin/grok', import.meta.url)); +report.binaryDigest = `sha256:${createHash('sha256').update(binary).digest('hex')}`; +async function persist() { await writeFile(resolve(values.output), JSON.stringify(report, null, 2)+'\n', { mode: 0o600 }); } +await persist(); +for (let repetition=1; repetition<=repetitions; repetition++) { + const root = await realpath(await mkdtemp(join(tmpdir(), 'paperclip-grok-smoke-'))); + await mkdir(join(root,'runtime'), { mode:0o700 }); await mkdir(join(root,'workspace'), { mode:0o700 }); + const nonce=randomUUID(); let calls=0; let host; + const attempt = { repetition, startedAt: new Date().toISOString(), status: 'running', checks: {}, usage: null, cost: null }; + report.attempts.push(attempt); await persist(); + const start = Date.now(); + async function phase(value) { attempt.phase=value; await persist(); } + const options = { runtimeDirectory:join(root,'runtime'), normalizedSessionId:'grok-smoke', workingDirectory:join(root,'workspace'), agent:'grok', model:'grok-4.7', permissionMode:'approve-all', environment, systemInstructions:'Use the requested MCP tool and retain its nonce in this session. Do not use other tools. Answer briefly.', semanticTools: { tools:[{ name:'check_context', description:'Read this task’s qualification nonce.', inputSchema:{type:'object', properties:{}, additionalProperties:false} }], handler:async call=>{assert.equal(call.tool,'check_context'); calls++; return {nonce};} } }; + const dependencies={openRuntime:openQualifiedAcpxRuntime, reportRetainedCleanupFailure:()=>{ attempt.checks.cleanup=false; }}; + async function closeHost(reason) { + const runtimeRoot=host.binding().runtimeRoot; + await host.close({reason}); + if(copyBackGrokAuth) { + await copyBackGrokAuth({hostHomeDir:dirname(resolve(values['auth-file'])),readSandboxAuth:()=>readFile(join(runtimeRoot,'grok-home','auth-refresh.json')),log:()=>{}}); + environment.PAPERCLIP_ACPX_GROK_AUTH_JSON_SECRET=await readFile(values['auth-file'],'utf8'); + } + host=null; + } + async function turn(text,id) { + const handle=host.startTurn({text, requestId:id, signal:AbortSignal.timeout(120000)}); let output=''; const counts={}; + const drain=(async()=>{for await(const event of handle.events){counts[event.type]=(counts[event.type]??0)+1;if(event.type==='text_delta' && event.stream!=='thought')output+=event.text;}})(); + const [result]=await Promise.all([handle.result,drain]);attempt.toolCalls=calls;attempt.providerFailureReason=/rate.?limit|quota/i.test(result.error?.message??'')?'rate_limited':/auth|token|login|credential/i.test(result.error?.message??'')?'authentication':result.error?'other_runtime':null;attempt.lastTurnStatus=result.status;attempt.stopReason=result.stopReason;attempt.failureCode=result.error?.code??null;attempt.failureCategory=result.error?.category??null; attempt.lastEventCounts=counts;attempt.outputLength=output.length;if(result.status!=='completed') throw Object.assign(new Error('Provider turn did not complete'),{code:result.error?.code ?? result.status});return {output,counts}; + } + try { + await phase('initialize'); + host=await AcpxRuntimeHost.open({...options,signal:AbortSignal.timeout(45000)},dependencies); + const identity=acpxProviderSessionIdentity(host.identity(),host.binding()); assert.equal(identity.effectiveModel,'grok-4.7');attempt.checks.model=true; + await phase('authenticated-tool'); + const first=await turn('Call check_context exactly once, then reply with only the returned nonce.',`tool-${repetition}`); + attempt.toolCalls=calls;attempt.nonceObserved=first.output.includes(nonce);assert.equal(calls,1);assert(first.output.includes(nonce));attempt.checks.authenticatedTool=true;attempt.events=first.counts; + await phase('resume'); + await closeHost('verify persisted resume'); + host=await AcpxRuntimeHost.open({...options,expectedIdentity:identity,signal:AbortSignal.timeout(45000)},dependencies); + assert.equal(host.identity().agentSessionId,identity.agentSessionId); + const second=await turn('Without using tools, reply with only the nonce returned in the previous turn.',`resume-${repetition}`); + assert(second.output.includes(nonce));assert.equal(calls,1);attempt.checks.resumeIdentity=true; + await phase('permissions'); + await closeHost('verify restrictive permissions'); + await mkdir(join(root,'workspace','.grok')); + await writeFile(join(root,'workspace','.grok','config.toml'),'[permission]\nallow = ["Bash", "Edit"]\n'); + await mkdir(join(root,'workspace','.claude')); + await writeFile(join(root,'workspace','.claude','settings.json'),JSON.stringify({permissions:{defaultMode:'bypassPermissions',allow:['Bash','Edit']}})); + const restricted={...options,normalizedSessionId:'grok-restricted',permissionMode:'approve-reads',systemInstructions:'Follow the requested action using native tools and respect permission decisions.'}; + host=await AcpxRuntimeHost.open({...restricted,signal:AbortSignal.timeout(45000)},dependencies); + let restrictedResult; + try { await turn('Use the shell to create a file named restricted-proof.txt in the working directory, then stop.',`permission-${repetition}`); } + catch(error) { restrictedResult=error.code ?? error.name; attempt.permissionOutcome=restrictedResult; } + // Restricted Grok writes must route through the existing approval-required outcome. + assert.equal(restrictedResult,'approval_required'); + await assert.rejects(readFile(join(root,'workspace','restricted-proof.txt')),{code:'ENOENT'}); + attempt.checks.permissionBoundary=true; + attempt.status='passed'; + } catch(error) { attempt.status='failed'; attempt.errorCode=error.code??error.name; /* Raw errors and provider text stay out of this sanitized proof. */ } + finally { if(host) {try{await closeHost('qualification cleanup');}catch{attempt.status='failed';attempt.checks.cleanup=false;}} await rm(root,{recursive:true,force:true}); if(attempt.checks.cleanup!==false) attempt.checks.cleanup=true; attempt.elapsedMs=Date.now()-start;await persist(); } + console.log(`Grok ${values.auth} repetition ${repetition}: ${attempt.status}`); +} +report.finishedAt=new Date().toISOString(); await persist(); +process.exitCode=report.attempts.every(attempt=>attempt.status==='passed')?0:1; diff --git a/packages/paperclip-runner/src/backends/codex-native-backend.ts b/packages/paperclip-runner/src/backends/codex-native-backend.ts index 93aaa7c92ac..f78367d6aa6 100644 --- a/packages/paperclip-runner/src/backends/codex-native-backend.ts +++ b/packages/paperclip-runner/src/backends/codex-native-backend.ts @@ -91,7 +91,7 @@ function transportDriverIdentity(input: NativeExecutionInput): { } return { kind: "acpx_runtime", - displayName: `${input.provider.agent === "claude" ? "Claude" : "Codex"} via ACPX`, + displayName: `${input.provider.agent === "grok" ? "Grok Build" : input.provider.agent === "claude" ? "Claude" : "Codex"} via ACPX`, version: "0.13.1", }; default: diff --git a/packages/paperclip-runner/src/cli/acpx-runtime-sidecar.test.ts b/packages/paperclip-runner/src/cli/acpx-runtime-sidecar.test.ts index 489178cdc7c..927631ba91c 100644 --- a/packages/paperclip-runner/src/cli/acpx-runtime-sidecar.test.ts +++ b/packages/paperclip-runner/src/cli/acpx-runtime-sidecar.test.ts @@ -545,7 +545,7 @@ describe("qualified ACPX runtime sidecar", () => { ok: false, error: { code: "acpx_sidecar_command_failed", - message: "ACPX agent must be claude or codex", + message: "ACPX agent must be claude, codex, or grok", retryable: false, }, }); diff --git a/packages/paperclip-runner/src/cli/acpx-runtime-sidecar.ts b/packages/paperclip-runner/src/cli/acpx-runtime-sidecar.ts index 2753ecb9bdc..ddb436cfa59 100644 --- a/packages/paperclip-runner/src/cli/acpx-runtime-sidecar.ts +++ b/packages/paperclip-runner/src/cli/acpx-runtime-sidecar.ts @@ -9,7 +9,7 @@ import type { AcpRuntimeEvent, } from "acpx/runtime"; -import { createAcpxToolEventNormalizer } from "../provider-events.js"; +import { createAcpxToolEventNormalizer, createGrokMessageNormalizer } from "../provider-events.js"; import { parseNativeRuntimeContext } from "../contracts/runtime-context.js"; import { PRP_BLOCK_TOOL_NAME, @@ -534,11 +534,13 @@ async function pumpTurn( // display metadata for later progress/completion frames before they cross // the sidecar boundary, matching the in-process ACPX driver path. const normalizeToolEvent = createAcpxToolEventNormalizer(); + const normalizeMessage = initializedAgent === "grok" + ? createGrokMessageNormalizer() : (event: AcpRuntimeEvent) => event; for await (const event of runtimeTurn.events) { emit( "runtime.event", sanitizeRuntimeEvent( - normalizeToolEvent(boundRuntimeEventForNormalization(event)), + normalizeMessage(normalizeToolEvent(boundRuntimeEventForNormalization(event))), ), currentTurnId, ); @@ -1206,8 +1208,8 @@ function requireHost( } function requireQualifiedAgent(value: unknown): QualifiedAcpxAgent { - if (value !== "codex" && value !== "claude") { - throw new Error("ACPX agent must be claude or codex"); + if (value !== "codex" && value !== "claude" && value !== "grok") { + throw new Error("ACPX agent must be claude, codex, or grok"); } return value; } diff --git a/packages/paperclip-runner/src/cli/eval-session-contract.test.ts b/packages/paperclip-runner/src/cli/eval-session-contract.test.ts index 590d575e3eb..361a7d002d9 100644 --- a/packages/paperclip-runner/src/cli/eval-session-contract.test.ts +++ b/packages/paperclip-runner/src/cli/eval-session-contract.test.ts @@ -223,6 +223,13 @@ describe("eval-session request contract", () => { }); describe("eval-session usage", () => { + it("preserves Grok's missing usage as unknown instead of manufacturing zero tokens or cost", () => { + const parsed = { ...parseEvalSessionRequest(request()), provider: "acpx" as const, acpxAgent: "grok" as const }; + const turn = { turnId: "turn-1", status: "completed" as const, assistantText: "done", snapshot: { usageLedger: [] } as unknown as CapabilityLiveSessionSnapshot }; + expect(boundedEvalSessionUsage(parsed, turn)).toBeNull(); + expect(() => boundedEvalSessionUsage({ ...parsed, provider: "codex", acpxAgent: undefined }, turn)).toThrow(); + }); + it("retains durable failed turns even when their reported usage exceeds completed-turn limits", () => { const parsed = parseEvalSessionRequest(request()); const snapshot = { diff --git a/packages/paperclip-runner/src/cli/eval-session-contract.ts b/packages/paperclip-runner/src/cli/eval-session-contract.ts index 9c48f987b97..5e64ef43fc2 100644 --- a/packages/paperclip-runner/src/cli/eval-session-contract.ts +++ b/packages/paperclip-runner/src/cli/eval-session-contract.ts @@ -241,9 +241,9 @@ export function parseEvalSessionRequest(value: unknown): EvalSessionRequest { if ( acpxAgent !== undefined && acpxAgent !== "codex" && - acpxAgent !== "claude" + acpxAgent !== "claude" && acpxAgent !== "grok" ) { - throw new Error("eval-session acpxAgent must be codex or claude"); + throw new Error("eval-session acpxAgent must be codex, claude, or grok"); } if (provider !== "acpx" && acpxAgent !== undefined) { throw new Error("eval-session acpxAgent requires provider acpx"); diff --git a/packages/paperclip-runner/src/cli/eval-session.ts b/packages/paperclip-runner/src/cli/eval-session.ts index 63b5d84d1a0..33c56f79624 100644 --- a/packages/paperclip-runner/src/cli/eval-session.ts +++ b/packages/paperclip-runner/src/cli/eval-session.ts @@ -235,7 +235,8 @@ export function boundedEvalSessionUsage( request: EvalSessionRequest, turn: CapabilityLiveTurnResult, ): EvalSessionUsage | null { - if (turn.status !== "completed") { + if (turn.status !== "completed" || + (request.provider === "acpx" && request.acpxAgent === "grok" && !turn.snapshot.usageLedger?.length)) { return usageIfAvailable(request, turn.snapshot); } const usage = evalSessionUsage(request.model, turn.snapshot); diff --git a/packages/paperclip-runner/src/contracts/native-execution.ts b/packages/paperclip-runner/src/contracts/native-execution.ts index c23c2f9ab18..492ff842e8b 100644 --- a/packages/paperclip-runner/src/contracts/native-execution.ts +++ b/packages/paperclip-runner/src/contracts/native-execution.ts @@ -77,7 +77,7 @@ export interface NativeAwsAgentCoreProfileSnapshot { eventExpiryDays: 90; } -export type NativeAcpxAgent = "pi" | "claude" | "codex"; +export type NativeAcpxAgent = "pi" | "claude" | "codex" | "grok"; export type NativeCodexApprovalPolicy = "never" | "on-request" | "untrusted"; export type NativeOpenCodePermissionMode = "allow" | "ask" | "deny"; export type NativeAcpxPermissionMode = "approve-all" | "approve-paperclip" | "approve-reads" | "deny-all"; @@ -532,8 +532,8 @@ export function parseNativeExecutionInput(value: unknown): NativeExecutionInput if (providerModel === null) { throw new NativeExecutionInputError("input.provider.model is required for acpx"); } - if (provider.agent !== "pi" && provider.agent !== "claude" && provider.agent !== "codex") { - throw new NativeExecutionInputError("input.provider.agent must be pi, claude, or codex"); + if (provider.agent !== "pi" && provider.agent !== "claude" && provider.agent !== "codex" && provider.agent !== "grok") { + throw new NativeExecutionInputError("input.provider.agent must be pi, claude, codex, or grok"); } if (isV4) { if (provider.permissionMode !== "approve-all" && provider.permissionMode !== "approve-paperclip" && provider.permissionMode !== "approve-reads" && provider.permissionMode !== "deny-all") { diff --git a/packages/paperclip-runner/src/control-plane/durable-prp-control-plane.ts b/packages/paperclip-runner/src/control-plane/durable-prp-control-plane.ts index 5729c4dded4..fd1c7714877 100644 --- a/packages/paperclip-runner/src/control-plane/durable-prp-control-plane.ts +++ b/packages/paperclip-runner/src/control-plane/durable-prp-control-plane.ts @@ -3283,6 +3283,8 @@ const runnerExplicitProviderEnvironmentKeys = [ "OPENAI_API_KEY", "CODEX_API_KEY", "PAPERCLIP_ACPX_CODEX_AUTH_JSON_SECRET", + "PAPERCLIP_ACPX_GROK_AUTH_JSON_SECRET", + "XAI_API_KEY", "AWS_REGION", "AWS_DEFAULT_REGION", "AWS_WEB_IDENTITY_TOKEN_FILE", diff --git a/packages/paperclip-runner/src/drivers/acpx/codex-acpx-driver.ts b/packages/paperclip-runner/src/drivers/acpx/codex-acpx-driver.ts index 6334a91fc6b..065b72d66d9 100644 --- a/packages/paperclip-runner/src/drivers/acpx/codex-acpx-driver.ts +++ b/packages/paperclip-runner/src/drivers/acpx/codex-acpx-driver.ts @@ -42,6 +42,7 @@ import { validatePrpStructuredRunResult } from "../../protocol/replay-contract.j import { canonicalProviderEventsFromAcpxRuntimeEvent, createAcpxToolEventNormalizer, + createGrokMessageNormalizer, } from "../../provider-events.js"; import { canonicalRunnerToolName, @@ -740,6 +741,7 @@ class CodexAcpxSession implements HarnessSession { } | null = null; #usage: Record | null = null; #assistantText = ""; + #assistantMessageId: string | null = null; #closed = false; #closingStarted = false; #eventStreamClosed = false; @@ -852,6 +854,7 @@ class CodexAcpxSession implements HarnessSession { const turnId = `turn-${randomBytes(12).toString("hex")}`; this.#activeTurnId = turnId; this.#assistantText = ""; + this.#assistantMessageId = null; this.#emit("turn.submitted", { text: input.message.text }, { turnId }); this.#emit("turn.accepted", { turnId }, { turnId }); this.#emit("turn.started", { status: "inProgress" }, { turnId }); @@ -1355,8 +1358,10 @@ class CodexAcpxSession implements HarnessSession { let index = 0; const normalizeToolEvent = createAcpxToolEventNormalizer(); + const normalizeMessage = this.#agent === "grok" + ? createGrokMessageNormalizer() : (event: AcpRuntimeEvent) => event; for await (const event of turn.events) { - this.#mapRuntimeEvent(normalizeToolEvent(event), turnId, ++index); + this.#mapRuntimeEvent(normalizeMessage(normalizeToolEvent(event)), turnId, ++index); } const result = await turn.result; this.#cancelPendingRuntimeRequests("provider turn settled", turnId); @@ -1541,6 +1546,9 @@ class CodexAcpxSession implements HarnessSession { const isReasoning = event.stream === "thought" || event.tag === "agent_thought_chunk"; if (!isReasoning) { + const messageId = typeof event.messageId === "string" && event.messageId ? event.messageId : null; + if (messageId && this.#assistantMessageId && messageId !== this.#assistantMessageId) this.#assistantText = ""; + if (messageId) this.#assistantMessageId = messageId; this.#assistantText = boundedText( `${this.#assistantText}${output}`, 256 * 1024, diff --git a/packages/paperclip-runner/src/drivers/acpx/codex-runtime-adapter.test.ts b/packages/paperclip-runner/src/drivers/acpx/codex-runtime-adapter.test.ts index 9d53e2cab1b..2f4598c894e 100644 --- a/packages/paperclip-runner/src/drivers/acpx/codex-runtime-adapter.test.ts +++ b/packages/paperclip-runner/src/drivers/acpx/codex-runtime-adapter.test.ts @@ -70,6 +70,27 @@ describe("Codex ACPX runtime adapter", () => { } }); + it.each(["PERMISSION_PROMPT_UNAVAILABLE", "AGENT_DISCONNECTED"])("preserves the meaning of ACPX terminal failure %s", async (code) => { + const runtime = fakeRuntime(); + const result = { status: "failed" as const, error: { code, message: "Provider failed" } }; + vi.mocked(runtime.startTurn).mockReturnValue({ + requestId: "permission-fallback", promptStarted: Promise.resolve(), + events: { async *[Symbol.asyncIterator]() {} }, result: Promise.resolve(result), + cancel: vi.fn(), closeStream: vi.fn(), + }); + const port = await openCodexAcpxRuntime(openOptions(fakeCommand()), { + createRegistry: () => registry(), createStore: () => store(), createRuntime: () => runtime, + }); + try { + const turn = port.startTurn({ text: "Attempt a write", requestId: "permission-fallback" }); + if (code === "PERMISSION_PROMPT_UNAVAILABLE") { + await expect(turn.result).rejects.toMatchObject({ code: "approval_required" }); + } else { + await expect(turn.result).resolves.toEqual(result); + } + } finally { await port.close({ reason: "terminal permission outcome verified" }); } + }); + it("rejects a pre-aborted admission before constructing or spawning ACPX", async () => { const cancellation = new Error("runtime admission cancelled"); const controller = new AbortController(); diff --git a/packages/paperclip-runner/src/drivers/acpx/codex-runtime-adapter.ts b/packages/paperclip-runner/src/drivers/acpx/codex-runtime-adapter.ts index 1d237ed8d28..1444b9d4206 100644 --- a/packages/paperclip-runner/src/drivers/acpx/codex-runtime-adapter.ts +++ b/packages/paperclip-runner/src/drivers/acpx/codex-runtime-adapter.ts @@ -273,9 +273,9 @@ export async function openQualifiedAcpxRuntime( agentRegistry: createRegistry({ // Preserve Claude's ACP capability identity. This is metadata only: the // spawn callback below always launches the verified command lease. - overrides: { [options.profile.agent]: [options.profile.agent === "claude" - ? "/paperclip-verified/claude-agent-acp" - : VERIFIED_COMMAND_SENTINEL] }, + overrides: { [options.profile.agent]: options.profile.agent === "grok" + ? ["/paperclip-verified/grok", "agent", "stdio"] + : [options.profile.agent === "claude" ? "/paperclip-verified/claude-agent-acp" : VERIFIED_COMMAND_SENTINEL] }, }), // ACPX does not know the Paperclip-specific mode. Exact SDK rules allow // admitted actions; all remaining requests keep its closed read policy. @@ -1230,7 +1230,15 @@ function runtimePort( } const guarded = turnWithVerifiedLifetimeOwnership(turn, finishOwnershipAdmission); const result = guarded.result.then( - (value) => { approval.signal.throwIfAborted(); return value; }, + (value) => { + approval.signal.throwIfAborted(); + // ACPX may resolve its own permission policy before invoking the + // host callback. Keep that typed denial on the same runner outcome. + if (value.status === "failed" && value.error?.code === "PERMISSION_PROMPT_UNAVAILABLE") { + throw new AcpxApprovalRequiredError(); + } + return value; + }, (error: unknown) => { approval.signal.throwIfAborted(); throw error; }, ).finally(() => { if (permissionBoundary.active === approval) permissionBoundary.active = null; diff --git a/packages/paperclip-runner/src/drivers/acpx/driver-profile.test.ts b/packages/paperclip-runner/src/drivers/acpx/driver-profile.test.ts index acf5efce76a..054956788cb 100644 --- a/packages/paperclip-runner/src/drivers/acpx/driver-profile.test.ts +++ b/packages/paperclip-runner/src/drivers/acpx/driver-profile.test.ts @@ -9,6 +9,7 @@ import { describe("ACPX driver profile", () => { it.each([ ["codex", "available"], + ["grok", "available"], ["claude", "available"], ["pi", "unsupported"], ] as const)( @@ -48,6 +49,7 @@ describe("ACPX driver profile", () => { it.each([ ["claude", "claude-sonnet-5"], ["codex", "gpt-5.6-sol"], + ["grok", "grok-4.7"], ] as const)("accepts the exact qualified %s model", (agent, model) => { expect(validateAcpxDriverConfig({ agent, model })).toEqual({ ok: true, diff --git a/packages/paperclip-runner/src/drivers/acpx/driver-profile.ts b/packages/paperclip-runner/src/drivers/acpx/driver-profile.ts index 7fe49dcbac1..559be9b3494 100644 --- a/packages/paperclip-runner/src/drivers/acpx/driver-profile.ts +++ b/packages/paperclip-runner/src/drivers/acpx/driver-profile.ts @@ -12,7 +12,7 @@ import { type QualifiedAcpxAgent, } from "./qualified-profiles.js"; -const ACPX_AGENTS = ["claude", "codex"] as const; +const ACPX_AGENTS = ["claude", "codex", "grok"] as const; const ACPX_PERMISSION_MODES = [ "approve-all", "approve-paperclip", @@ -37,8 +37,8 @@ export function acpxCapabilities( plan: agent === "pi" ? "unsupported" : "available", tool_execution: "available", model_identity: "available", - review: "available", - provider_notice: "available", + review: agent === "grok" ? "unsupported" : "available", + provider_notice: agent === "grok" ? "unsupported" : "available", artifact: "policy_disabled", }), steering: false, @@ -46,7 +46,7 @@ export function acpxCapabilities( structuredResult: true, read: true, reconciliation: true, - usage: true, + usage: agent !== "grok", dynamicTools: true, runtimeRequestResolution: true, runtimeRequestHandoff: true, @@ -96,7 +96,7 @@ export function validateAcpxDriverConfig( return invalid( "agent", "invalid_agent", - "ACPX agent must be claude or codex.", + "ACPX agent must be claude, codex, or grok.", ); } const model = text(config.model); @@ -144,6 +144,7 @@ function isPermissionMode(value: string): value is NativeAcpxPermissionMode { } function displayAgent(agent: QualifiedAcpxAgent): string { + if (agent === "grok") return "Grok Build"; if (agent === "pi") return "Pi"; if (agent === "claude") return "Claude"; return "Codex"; diff --git a/packages/paperclip-runner/src/drivers/acpx/environment.ts b/packages/paperclip-runner/src/drivers/acpx/environment.ts index 57fef1b4e3c..0e1ff50b80a 100644 --- a/packages/paperclip-runner/src/drivers/acpx/environment.ts +++ b/packages/paperclip-runner/src/drivers/acpx/environment.ts @@ -25,7 +25,9 @@ export function createSanitizedAcpxSpawnInput( const source = environment ?? process.env; const result: NodeJS.ProcessEnv = {}; const credentialNames = - agent === "pi" + agent === "grok" + ? ["XAI_API_KEY"] + : agent === "pi" ? ["OPENROUTER_API_KEY"] : agent === "claude" ? ["ANTHROPIC_API_KEY", "CLAUDE_CODE_OAUTH_TOKEN"] diff --git a/packages/paperclip-runner/src/drivers/acpx/grok-credentials.test.ts b/packages/paperclip-runner/src/drivers/acpx/grok-credentials.test.ts new file mode 100644 index 00000000000..e5267f360d8 --- /dev/null +++ b/packages/paperclip-runner/src/drivers/acpx/grok-credentials.test.ts @@ -0,0 +1,95 @@ +import { mkdir, mkdtemp, readFile, rm, stat, symlink, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; +import { stageManagedGrokCredential } from "./grok-credentials.js"; +import { createSanitizedAcpxSpawnInput } from "./environment.js"; +import { resolveQualifiedAcpxProfile } from "./qualified-profiles.js"; +import { requireVerifiedAcpxModel } from "./model-verification.js"; +import { decideAcpxPermission } from "./permission-policy.js"; + +const roots: string[] = []; +const credential = JSON.stringify({ "https://accounts.x.ai::11111111-1111-4111-8111-111111111111": { key: "test-token", refresh_token: "test-refresh" } }); +async function home() { const path = await mkdtemp(join(tmpdir(), "grok-credential-test-")); roots.push(path); return path; } +afterEach(async () => { await Promise.all(roots.splice(0).map(path => rm(path, { recursive: true, force: true }))); }); + +describe("Grok isolated credentials", () => { + it.each([{}, { PAPERCLIP_ACPX_GROK_AUTH_JSON_SECRET: "{}" }, { PAPERCLIP_ACPX_GROK_AUTH_JSON_SECRET: "not-json" }, { PAPERCLIP_ACPX_GROK_AUTH_JSON_SECRET: credential, XAI_API_KEY: "test-key" }])("rejects missing, malformed and ambiguous sources", async environment => { + await expect(stageManagedGrokCredential({ agentHomeDirectory: await home(), environment })).rejects.toThrow(/required|malformed|ambiguous/); + }); + it("stages only subscription auth privately and retains refreshed auth until controller copyback", async () => { + const directory = await home(); + const lease = await stageManagedGrokCredential({ agentHomeDirectory: directory, environment: { PAPERCLIP_ACPX_GROK_AUTH_JSON_SECRET: credential } }); + expect((await stat(join(directory, "auth.json"))).mode & 0o777).toBe(0o600); + expect(await readFile(join(directory, "auth.json"), "utf8")).toBe(credential); + const refreshed = credential.replace("test-token", "refreshed-token"); + await writeFile(join(directory, "auth.json"), refreshed, { mode: 0o600 }); + await Promise.all([lease.close(), lease.close()]); + await expect(readFile(join(directory, "auth.json"))).rejects.toMatchObject({ code: "ENOENT" }); + expect(await readFile(join(directory, "auth-refresh.json"), "utf8")).toBe(refreshed); + await lease.close(); + }); + it.each([true, false])("removes diagnostic logs after shutdown without removing session history (admitted=%s)", async retainRefresh => { + const directory = await home(); + const lease = await stageManagedGrokCredential({ agentHomeDirectory: directory, + environment: { PAPERCLIP_ACPX_GROK_AUTH_JSON_SECRET: credential }, retainRefresh: () => retainRefresh }); + await mkdir(join(directory, "logs")); + await mkdir(join(directory, "sessions")); + await writeFile(join(directory, "logs", "unified.jsonl"), credential, { mode: 0o600 }); + await writeFile(join(directory, "sessions", "session.json"), "resume-state", { mode: 0o600 }); + await lease.close(); + await expect(stat(join(directory, "logs"))).rejects.toMatchObject({ code: "ENOENT" }); + expect(await readFile(join(directory, "sessions", "session.json"), "utf8")).toBe("resume-state"); + if (retainRefresh) expect(await readFile(join(directory, "auth-refresh.json"), "utf8")).toBe(credential); + else await expect(stat(join(directory, "auth-refresh.json"))).rejects.toMatchObject({ code: "ENOENT" }); + }); + it("does not stage subscription credentials for an explicit API key", async () => { + const directory = await home(); + const lease = await stageManagedGrokCredential({ agentHomeDirectory: directory, environment: { XAI_API_KEY: "test-key" } }); + await expect(readFile(join(directory, "auth.json"))).rejects.toMatchObject({ code: "ENOENT" }); + await lease.close(); + await expect(readFile(join(directory, "auth-refresh.json"))).rejects.toMatchObject({ code: "ENOENT" }); + }); + it("fences concurrent refresh owners and removes a stale refresh before the next turn", async () => { + const directory = await home(); + const options = { agentHomeDirectory: directory, environment: { PAPERCLIP_ACPX_GROK_AUTH_JSON_SECRET: credential } }; + const first = await stageManagedGrokCredential(options); + await expect(stageManagedGrokCredential(options)).rejects.toThrow(/active lease|owned/); + await first.close(); + const second = await stageManagedGrokCredential(options); + await expect(readFile(join(directory, "auth-refresh.json"))).rejects.toMatchObject({ code: "ENOENT" }); + await second.close(); + }); + it("rejects a substituted refresh symlink without releasing ownership or copying its target", async () => { + const directory = await home(); + const options = { agentHomeDirectory: directory, environment: { PAPERCLIP_ACPX_GROK_AUTH_JSON_SECRET: credential } }; + const lease = await stageManagedGrokCredential(options); + const target = join(await home(), "private.json"); + await writeFile(target, "unrelated", { mode: 0o600 }); + await rm(join(directory, "auth.json")); + await symlink(target, join(directory, "auth.json")); + await expect(lease.close()).rejects.toThrow(); + await expect(stageManagedGrokCredential(options)).rejects.toThrow(/active lease|owned/); + expect(await readFile(target, "utf8")).toBe("unrelated"); + await rm(join(directory, "auth.json")); + await writeFile(join(directory, "auth.json"), credential, { mode: 0o600 }); + await lease.close(); + }); + it("never passes other providers, host homes or the inline credential to Grok", () => { + const result = createSanitizedAcpxSpawnInput({ XAI_API_KEY: "selected", HOME: "/host", GROK_HOME: "/host/.grok", OPENAI_API_KEY: "other", PAPERCLIP_ACPX_GROK_AUTH_JSON_SECRET: credential }, "grok"); + expect(result.env).toEqual({ XAI_API_KEY: "selected" }); + }); + it("cannot pass model verification with a missing or mismatched model", async () => { + const profile = resolveQualifiedAcpxProfile("grok", "grok-4.7"); + for (const status of [{}, { models: { currentModelId: "grok-other" } }]) { + await expect(requireVerifiedAcpxModel({ getStatus: async () => status, setModel: async () => {} }, profile)).rejects.toMatchObject({ code: "ACPX_EFFECTIVE_MODEL_MISMATCH" }); + } + }); + it("keeps untrusted read hints and Paperclip-looking tools behind the selected approval policy", () => { + const request = { inferredKind: "read", raw: { toolCall: { title: "mcp__paperclip__finish_task" } } }; + expect(decideAcpxPermission("grok", "approve-reads", request)).toBe("delegate"); + expect(decideAcpxPermission("grok", "approve-paperclip", request)).toBe("delegate"); + expect(decideAcpxPermission("grok", "deny-all", request)).toBe("reject_once"); + expect(decideAcpxPermission("grok", "approve-all", request)).toBe("allow_once"); + }); +}); diff --git a/packages/paperclip-runner/src/drivers/acpx/grok-credentials.ts b/packages/paperclip-runner/src/drivers/acpx/grok-credentials.ts new file mode 100644 index 00000000000..88060a45bf9 --- /dev/null +++ b/packages/paperclip-runner/src/drivers/acpx/grok-credentials.ts @@ -0,0 +1,92 @@ +import { constants } from "node:fs"; +import { open, rename, rm, writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { stageManagedCodexCredential, type AcpxProviderLifetimeLease } from "./codex-credentials.js"; + +export const GROK_AUTH_REFRESH_FILE = "auth-refresh.json"; + +/** Shares the established credential staging, crash cleanup and process fence. + * The Grok document uses the same auth.json filename, but its own source and + * validation. Never forward the controller's inline credential to the provider. + */ +export async function stageManagedGrokCredential(input: { + agentHomeDirectory: string; + environment?: NodeJS.ProcessEnv; + retainRefresh?: () => boolean; +}): Promise { + const source = input.environment ?? {}; + const inline = source.PAPERCLIP_ACPX_GROK_AUTH_JSON_SECRET; + const apiKey = source.XAI_API_KEY; + if (inline && apiKey) throw new Error("Grok credential source is ambiguous"); + if (!inline && !apiKey) throw new Error("Grok login or explicit XAI_API_KEY is required"); + if (inline) { + if (Buffer.byteLength(inline) > 256 * 1024) throw new Error("Grok credential exceeds its bound"); + let value: unknown; + try { value = JSON.parse(inline); } catch { throw new Error("Grok credential is malformed"); } + if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error("Grok credential is malformed"); + const entries = Object.entries(value); + // Keep aligned with grok-local's parseGrokAuthPayload / hasUsableGrokAuthValue. + const [identity, auth] = entries[0] ?? []; + if (entries.length !== 1 || !/^.+::[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(identity ?? "") || + !auth || typeof auth.key !== "string" || !auth.key.trim() || + typeof auth.refresh_token !== "string" || !auth.refresh_token.trim()) { + throw new Error("Grok credential is malformed"); + } + } + const lease = await stageManagedCodexCredential({ + agentHomeDirectory: input.agentHomeDirectory, + environment: inline ? { PAPERCLIP_ACPX_CODEX_AUTH_JSON_SECRET: inline } : { OPENAI_API_KEY: apiKey }, + }); + const refresh = join(input.agentHomeDirectory, GROK_AUTH_REFRESH_FILE); + await rm(refresh, { force: true }).catch(async (error) => { await lease.close(); throw error; }); + let closed = false; + let closing: Promise | null = null; + return { + lifetimeFenceCandidates: lease.lifetimeFenceCandidates, + lifetimeFenceFds: lease.lifetimeFenceFds, + activateLifetimeOwner: (pid) => lease.activateLifetimeOwner(pid), + close() { + if (closed) return Promise.resolve(); + if (closing) return closing; + closing = (async () => { + // The runtime host calls close only after verified provider exit. Retain + // a private refresh handoff for the controller's existing Grok merge + // predicate, then scrub auth.json before releasing the process fence. + if (inline && (input.retainRefresh?.() ?? true)) { + const auth = await open(join(input.agentHomeDirectory, "auth.json"), constants.O_RDONLY | constants.O_NOFOLLOW).catch((error) => { + if (error.code === "ENOENT") return null; + throw error; + }); + if (auth) { + try { + const stat = await auth.stat(); + if (!stat.isFile() || stat.uid !== process.getuid?.() || (stat.mode & 0o777) !== 0o600 || stat.size > 256 * 1024) throw new Error("Grok refreshed credential is invalid"); + const buffer = Buffer.alloc(256 * 1024 + 1); + let size = 0; + while (size < buffer.length) { + const read = await auth.read(buffer, size, buffer.length - size, size); + if (!read.bytesRead) break; + size += read.bytesRead; + } + if (size > 256 * 1024) { buffer.fill(0); throw new Error("Grok refreshed credential exceeds its bound"); } + const bytes = buffer.subarray(0, size); + const temporary = `${refresh}.tmp`; + try { + await rm(temporary, { force: true }); + await writeFile(temporary, bytes, { flag: "wx", mode: 0o600 }); + await rename(temporary, refresh); + } finally { bytes.fill(0); await rm(temporary, { force: true }); } + } finally { await auth.close(); } + } + } + // Grok diagnostics can contain account/authentication fields. They are + // disposable, unlike session history, and must not outlive this owner. + // close is invoked only after the provider has been contained. + await rm(join(input.agentHomeDirectory, "logs"), { recursive: true, force: true }); + await lease.close(); + closed = true; + })().finally(() => { closing = null; }); + return closing; + }, + }; +} diff --git a/packages/paperclip-runner/src/drivers/acpx/installation-integrity.test.ts b/packages/paperclip-runner/src/drivers/acpx/installation-integrity.test.ts index 7c7a9356a3b..963255639aa 100644 --- a/packages/paperclip-runner/src/drivers/acpx/installation-integrity.test.ts +++ b/packages/paperclip-runner/src/drivers/acpx/installation-integrity.test.ts @@ -1,4 +1,6 @@ import { createHash } from "node:crypto"; +import { constants } from "node:fs"; +import { runInNewContext } from "node:vm"; import { fork, type ChildProcess } from "node:child_process"; import { once } from "node:events"; import { @@ -16,7 +18,7 @@ import { } from "node:fs/promises"; import { createServer, type Server } from "node:net"; import { tmpdir } from "node:os"; -import { dirname, join } from "node:path"; +import { dirname, isAbsolute, join } from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; @@ -605,6 +607,20 @@ describe("ACPX installation integrity", () => { }, ); + it("rejects a tampered native Grok executable and symlink substitution", async () => { + const fixture = await installationFixture(); + const base = resolveQualifiedAcpxProfile("grok", "grok-4.7"); + const profile = { ...base, commandDigest: fixture.profile.commandDigest }; + await writeFile(fixture.serverPackageJsonPath, JSON.stringify({ version: "1.0.13", bin: "bin/server.js" })); + const native = join(fixture.commandDirectory, "grok"); + await writeFile(native, "tampered native binary", { mode: 0o700 }); + const resolvePackage = () => fixture.serverPackageJsonPath; + await expect(verifyQualifiedAcpxInstallation(profile, resolvePackage)).rejects.toThrow(/digest mismatch/); + await rm(native); + await symlink(fixture.commandPath, native); + await expect(verifyQualifiedAcpxInstallation(profile, resolvePackage)).rejects.toThrow(/regular file|symlink|no-follow/); + }); + it("rejects package version and executable digest drift", async () => { const fixture = await installationFixture(); await writeFile( @@ -2099,3 +2115,96 @@ async function installationFixture() { }, }; } + + +describe("Grok launcher subscription refresh", () => { + it("keeps the Grok launcher digest synchronized across TypeScript, Rust, server, and provider pack", async () => { + const launcher = await readFile(new URL("../../../../grok-acp/launcher.cjs", import.meta.url)); + const digest = `sha256:${createHash("sha256").update(launcher).digest("hex")}`; + expect(resolveQualifiedAcpxProfile("grok", "grok-4.7").commandDigest).toBe(digest); + const [server, pack, rust] = await Promise.all([ + readFile(new URL("../../../../../server/src/services/native-runtime/native-session-executor.ts", import.meta.url), "utf8"), + readFile(new URL("../../../scripts/build-provider-pack.mjs", import.meta.url), "utf8"), + readFile(new URL("../../../runner/crates/runner-core/src/acpx_provider_backend.rs", import.meta.url), "utf8"), + ]); + expect(server).toContain(`grok: "${digest}"`); + expect(pack).toContain(`grok: "${digest}"`); + expect(rust.split('"grok" =>')[1]?.slice(0, 600)).toContain(`"${digest}"`); + }); + + async function fixture(input: { + expiry?: number; rawExpiry?: unknown; apiKey?: string; executable?: string; + refresh?: { status: number | null; signal?: string | null; error?: Error }; + mode?: number; fileError?: string; growingFile?: boolean; + } = {}) { + const script = await readFile(new URL("../../../../grok-acp/launcher.cjs", import.meta.url), "utf8"); + const payload = Buffer.from(JSON.stringify({ account: { + key: "PRIVATE-CREDENTIAL-SENTINEL", refresh_token: "PRIVATE-REFRESH-SENTINEL", + expires_at: "rawExpiry" in input ? input.rawExpiry : new Date(input.expiry ?? Date.now() - 60_000).toISOString(), + } })); + const fs = { + constants, + openSync: vi.fn(() => { if (input.fileError) throw Object.assign(new Error("PRIVATE-CREDENTIAL-SENTINEL"), { code: input.fileError }); return 42; }), + fstatSync: vi.fn(() => ({ isFile: () => true, uid: 1000, mode: input.mode ?? 0o600, size: payload.length })), + readSync: vi.fn((_fd: number, buffer: Buffer, offset: number, length: number, position: number) => { + if (input.growingFile) { buffer.fill(65, offset, offset + length); return length; } + return payload.copy(buffer, offset, position, position + length); + }), + closeSync: vi.fn(), + }; + const spawnSync = vi.fn((_file: string, _args: string[], _options: { env: Record; stdio: Array; timeout: number; killSignal: string }) => input.refresh ?? { status: 0, signal: null }); + const execve = vi.fn(); + const executable = input.executable ?? "/private/verified/grok"; + return { fs, spawnSync, execve, executable, run: () => runInNewContext(script, { + Buffer, Date, + process: { env: { PAPERCLIP_GROK_VERIFIED_EXECUTABLE: executable, GROK_HOME: "/isolated/grok", ...(input.apiKey ? { XAI_API_KEY: input.apiKey } : {}) }, getuid: () => 1000, execve }, + require: (name: string) => name === "node:path" ? { isAbsolute, join } : name === "node:fs" ? fs : name === "node:child_process" ? { spawnSync } : undefined, + }) }; + } + + it("refreshes an expired subscription without inference or ACP output before native startup", async () => { + const test = await fixture(); test.run(); + expect(test.spawnSync).toHaveBeenCalledExactlyOnceWith(test.executable, ["models"], { + env: { GROK_HOME: "/isolated/grok" }, stdio: ["ignore", "ignore", "ignore"], timeout: 15_000, killSignal: "SIGKILL", + }); + expect(test.execve).toHaveBeenCalledExactlyOnceWith(test.executable, [test.executable, "agent", "--no-leader", "stdio"], { GROK_HOME: "/isolated/grok" }); + expect(test.spawnSync.mock.invocationCallOrder[0]).toBeLessThan(test.execve.mock.invocationCallOrder[0]!); + expect(test.fs.openSync).toHaveBeenCalledWith("/isolated/grok/auth.json", constants.O_RDONLY | constants.O_NOFOLLOW); + expect(test.fs.closeSync).toHaveBeenCalledWith(42); + }); + it.each(["/proc/self/fd/8", "/dev/fd/8"])("retains only the verified executable descriptor for %s", async executable => { + const test = await fixture({ executable }); test.run(); + expect(test.spawnSync.mock.calls[0]![2].stdio).toEqual(["ignore", "ignore", "ignore", "ignore", "ignore", "ignore", "ignore", "ignore", 8]); + }); + it("does not refresh a fresh subscription", async () => { + const test = await fixture({ expiry: Date.now() + 600_000 }); test.run(); + expect(test.spawnSync).not.toHaveBeenCalled(); expect(test.execve).toHaveBeenCalledOnce(); + }); + it.each(["seconds", "milliseconds"])("refreshes expired and near-expiry numeric %s but skips fresh credentials", async encoding => { + for (const [offset, expected] of [[-60_000, true], [30_000, true], [600_000, false]] as const) { + const ms = Date.now() + offset; + const test = await fixture({ rawExpiry: encoding === "seconds" ? ms / 1000 : ms }); + test.run(); + expect(test.spawnSync).toHaveBeenCalledTimes(expected ? 1 : 0); + expect(test.execve).toHaveBeenCalledOnce(); + } + }); + it.each([null, "1760000000", "2020-01-01", true, {}, []])("does not coerce unsupported expiry encodings: %j", async rawExpiry => { + const test = await fixture({ rawExpiry }); test.run(); + expect(test.spawnSync).not.toHaveBeenCalled(); + expect(test.execve).toHaveBeenCalledOnce(); + }); + it("never discovers subscription credentials for an explicit API key", async () => { + const test = await fixture({ apiKey: "explicit-api-key" }); test.run(); + expect(test.fs.openSync).not.toHaveBeenCalled(); expect(test.spawnSync).not.toHaveBeenCalled(); expect(test.execve).toHaveBeenCalledOnce(); + }); + it.each([{ status: 1 }, { status: null, signal: "SIGKILL" }, { status: null, error: new Error("PRIVATE-CREDENTIAL-SENTINEL") }])("fails closed on refresh failure without exposing provider output: %j", async refresh => { + const test = await fixture({ refresh }); + expect(test.run).toThrow("Grok subscription refresh failed; reconnect Grok Build"); + expect(test.execve).not.toHaveBeenCalled(); + }); + it.each([{ mode: 0o644 }, { fileError: "ELOOP" }, { growingFile: true }])("rejects unsafe or growing credential files: %j", async input => { + const test = await fixture(input); expect(test.run).toThrow("Grok subscription credential is invalid"); + expect(test.spawnSync).not.toHaveBeenCalled(); expect(test.execve).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/paperclip-runner/src/drivers/acpx/installation-integrity.ts b/packages/paperclip-runner/src/drivers/acpx/installation-integrity.ts index ef101bdb793..5f2c2df7bca 100644 --- a/packages/paperclip-runner/src/drivers/acpx/installation-integrity.ts +++ b/packages/paperclip-runner/src/drivers/acpx/installation-integrity.ts @@ -342,7 +342,13 @@ export function createAcpxPackageJsonResolver( const packageJsonPath = realpathSync( resolvePackageJsonFromIssuer(packageName, canonicalIssuer), ); - if (!pathIsInside(canonicalNodeModules, packageJsonPath)) { + // The native Grok launcher is a source-owned workspace package. Admit + // only its exact source location when the issuer is the source Runner; + // installed packs still resolve exclusively below their node_modules. + const sourceGrokPackage = packageName === "@paperclipai/grok-acp" && + canonicalManifest === resolve(canonicalRoot, "packages/paperclip-runner/package.json") && + packageJsonPath === resolve(canonicalRoot, "packages/grok-acp/package.json"); + if (!pathIsInside(canonicalNodeModules, packageJsonPath) && !sourceGrokPackage) { throw new Error( `ACPX provider package ${packageName} resolves outside the selected provider root`, ); @@ -490,7 +496,7 @@ interface VerifiedAcpxRuntimeExecutable { path: string; digest: string; identity: VerifiedAcpxCommandIdentity; - environmentVariable: "CLAUDE_CODE_EXECUTABLE" | "CODEX_PATH"; + environmentVariable: "CLAUDE_CODE_EXECUTABLE" | "CODEX_PATH" | "PAPERCLIP_GROK_VERIFIED_EXECUTABLE"; } interface AcpxPackageMetadata { @@ -838,6 +844,19 @@ async function verifyQualifiedRuntimeExecutable(input: { runtimePackageJsonPath: string; resolvePackageJson: AcpxPackageJsonResolver; }): Promise { + if (input.profile.agent === "grok") { + const digests: Record = { + "darwin-arm64": "8669e0fdadceec25b8c159c355f427ffbd82583525d774b6ab1522197ea83b80", + "linux-x64": "edf79521581bb5e6b95abef848491a6a742e860da3e237ebe86a280d30dce4c1", + }; + const digest = digests[`${process.platform}-${process.arch}`]; + if (!digest) throw new Error(`Grok verified runtime unavailable for ${process.platform}-${process.arch}`); + const executablePath = resolve(dirname(input.runtimePackageJsonPath), "bin/grok"); + const verified = await openVerifiedRuntimeExecutable(executablePath, `sha256:${digest}`, "grok"); + await verified.handle.close(); + return { path: executablePath, digest: `sha256:${digest}`, identity: verified.identity, + environmentVariable: "PAPERCLIP_GROK_VERIFIED_EXECUTABLE" }; + } const qualification = input.profile.agent === "claude" ? process.platform === "darwin" && (process.arch === "arm64" || process.arch === "x64") @@ -1689,7 +1708,7 @@ function snapshotBootstrap(format: AcpxCommandFormat, guarded = false): string { 'if ((serverPackageFormat !== "module" && serverPackageFormat !== "commonjs") || !Array.isArray(dependencyAncestorFormats) || dependencyAncestorFormats.length !== dependencyAncestorCount || dependencyAncestorFormats.some((value) => value !== "module" && value !== "commonjs")) throw new Error("ACPX provider package formats are invalid");', 'if (providerRuntimeExecutableCount !== 0 && providerRuntimeExecutableCount !== 1) throw new Error("ACPX provider runtime executable count is invalid");', `const providerRuntimeExecutableFd = ${DEPENDENCY_ANCESTOR_FD_START} + dependencyAncestorCount;`, - 'if (providerRuntimeExecutableCount === 1) { if (providerRuntimeEnvironmentVariable !== "CODEX_PATH" && providerRuntimeEnvironmentVariable !== "CLAUDE_CODE_EXECUTABLE") throw new Error("ACPX provider runtime environment target is invalid"); fs.fstatSync(providerRuntimeExecutableFd); process.env[providerRuntimeEnvironmentVariable] = privateSnapshot ? privateSnapshot.executable : "/proc/" + process.pid + "/fd/" + providerRuntimeExecutableFd; } else if (providerRuntimeEnvironmentVariable !== undefined) throw new Error("ACPX provider runtime environment target is unexpected");', + 'if (providerRuntimeExecutableCount === 1) { if (providerRuntimeEnvironmentVariable !== "CODEX_PATH" && providerRuntimeEnvironmentVariable !== "CLAUDE_CODE_EXECUTABLE" && providerRuntimeEnvironmentVariable !== "PAPERCLIP_GROK_VERIFIED_EXECUTABLE") throw new Error("ACPX provider runtime environment target is invalid"); fs.fstatSync(providerRuntimeExecutableFd); process.env[providerRuntimeEnvironmentVariable] = privateSnapshot ? privateSnapshot.executable : "/proc/" + process.pid + "/fd/" + providerRuntimeExecutableFd; } else if (providerRuntimeEnvironmentVariable !== undefined) throw new Error("ACPX provider runtime environment target is unexpected");', ...(guarded ? [ `const guardianFd = ${DEPENDENCY_ANCESTOR_FD_START} + dependencyAncestorCount + providerRuntimeExecutableCount;`, @@ -2019,3 +2038,7 @@ export async function probeAcpxClaudeInstallation(model: string): Promise const lease = await installation.openCommand(); await lease.close(); } + +export async function probeAcpxGrokInstallation(model: string): Promise { + await verifyQualifiedAcpxInstallation(resolveQualifiedAcpxProfile("grok", model)); +} diff --git a/packages/paperclip-runner/src/drivers/acpx/private-snapshot.ts b/packages/paperclip-runner/src/drivers/acpx/private-snapshot.ts index 65076c8f2e2..87208fe70d8 100644 --- a/packages/paperclip-runner/src/drivers/acpx/private-snapshot.ts +++ b/packages/paperclip-runner/src/drivers/acpx/private-snapshot.ts @@ -55,6 +55,7 @@ export async function createAcpxPrivateSnapshot( executable: FileHandle | null, ): Promise { sourceRoots = await Promise.all(sourceRoots.map((root) => realpath(root))); + const executableIdentity = executable ? await executable.stat({ bigint: true }) : null; const sourceIdentities = await Promise.all( sourceRoots.map((root) => lstat(root, { bigint: true })), ); @@ -116,6 +117,8 @@ export async function createAcpxPrivateSnapshot( throw new Error("ACPX package directory changed during snapshot"); return; } + // The separately pinned native binary is copied below with its own bound. + if (before.isFile() && executableIdentity && before.dev === executableIdentity.dev && before.ino === executableIdentity.ino) return; if (!before.isFile() || before.size > 16n * 1024n * 1024n) throw new Error("ACPX module must be a bounded regular file"); bytesCopied += Number(before.size); diff --git a/packages/paperclip-runner/src/drivers/acpx/qualified-profiles.test.ts b/packages/paperclip-runner/src/drivers/acpx/qualified-profiles.test.ts index 836e27c485b..9cde5dd0a96 100644 --- a/packages/paperclip-runner/src/drivers/acpx/qualified-profiles.test.ts +++ b/packages/paperclip-runner/src/drivers/acpx/qualified-profiles.test.ts @@ -7,7 +7,7 @@ import { describe("qualified ACPX profiles", () => { it("binds each agent to one immutable package and model declaration", () => { - for (const agent of ["pi", "claude", "codex"] as const) { + for (const agent of ["pi", "claude", "codex", "grok"] as const) { const profile = QUALIFIED_ACPX_PROFILES[agent]; expect(profile.agent).toBe(agent); expect(profile.commandDigest).toMatch(/^sha256:[a-f0-9]{64}$/); diff --git a/packages/paperclip-runner/src/drivers/acpx/qualified-profiles.ts b/packages/paperclip-runner/src/drivers/acpx/qualified-profiles.ts index fd931b18081..0d33811be69 100644 --- a/packages/paperclip-runner/src/drivers/acpx/qualified-profiles.ts +++ b/packages/paperclip-runner/src/drivers/acpx/qualified-profiles.ts @@ -31,6 +31,14 @@ export interface QualifiedAcpxProfile { export const QUALIFIED_ACPX_PROFILES: Readonly< Record > = deepFreeze({ + grok: { + driverKind: ACPX_DRIVER_KIND, protocolVersion: ACPX_DRIVER_PROTOCOL_VERSION, + acpxVersion: QUALIFIED_ACPX_VERSION, agent: "grok", agentProfileVersion: 1, + agentServerPackage: "@paperclipai/grok-acp", agentServerVersion: "1.0.13", + agentRuntimePackage: "@paperclipai/grok-acp", agentRuntimeVersion: "1.0.13", + commandDigest: "sha256:f0b698395a3704ed2ffaf84ea19bdb20c36c8a0a70b7c629c7b6ffe144e59e55", + qualificationModel: "grok-4.7", reportedModelId: "grok-4.7", permissionPolicy: "interactive", + }, pi: { driverKind: ACPX_DRIVER_KIND, protocolVersion: ACPX_DRIVER_PROTOCOL_VERSION, @@ -87,7 +95,7 @@ export function resolveQualifiedAcpxProfile( ): QualifiedAcpxProfile { const profile = QUALIFIED_ACPX_PROFILES[agent]; if (!requestedModel.trim()) throw new Error("ACPX model must not be empty"); - if (agent !== "claude" && requestedModel !== profile.qualificationModel) { + if (agent !== "claude" && agent !== "grok" && requestedModel !== profile.qualificationModel) { throw new Error( `ACPX ${agent} profile requires exact model ${profile.qualificationModel}; received ${requestedModel}`, ); diff --git a/packages/paperclip-runner/src/drivers/acpx/runtime-host.test.ts b/packages/paperclip-runner/src/drivers/acpx/runtime-host.test.ts index bfc6e710dbc..1413d5c0a25 100644 --- a/packages/paperclip-runner/src/drivers/acpx/runtime-host.test.ts +++ b/packages/paperclip-runner/src/drivers/acpx/runtime-host.test.ts @@ -853,6 +853,25 @@ describe("ACPX runtime host", () => { expect(openRuntime).not.toHaveBeenCalled(); }); + it.each(["expired credential", "provider process died"])("cleans Grok credentials after failed initialization: %s", async (failure) => { + const fixture = await hostFixture(); + let home = ""; + await expect(AcpxRuntimeHost.open({ + ...fixture.options, agent: "grok", model: "grok-4.7", permissionMode: "approve-reads", + environment: { PAPERCLIP_ACPX_GROK_AUTH_JSON_SECRET: JSON.stringify({ "https://accounts.x.ai::11111111-1111-4111-8111-111111111111": { key: "test-expired", refresh_token: "test-refresh" } }) }, + }, fixture.dependencies({ openRuntime: async (options) => { + home = options.launchEnvironment.GROK_HOME!; + expect(options.permissionMode).toBe("approve-reads"); + expect(options.launchEnvironment).not.toHaveProperty("PAPERCLIP_ACPX_GROK_AUTH_JSON_SECRET"); + expect(await readFile(join(home, "auth.json"), "utf8")).toContain("test-expired"); + throw new Error(failure); + } }))).rejects.toThrow(failure); + for (const filename of ["auth.json", "auth-refresh.json"]) { + await expect(readFile(join(home, filename))).rejects.toMatchObject({ code: "ENOENT" }); + } + expect(fixture.commandClose).toHaveBeenCalledOnce(); + }); + it("cleans credentials and command leases when provider open fails", async () => { const fixture = await hostFixture(); let authPath = ""; diff --git a/packages/paperclip-runner/src/drivers/acpx/runtime-host.ts b/packages/paperclip-runner/src/drivers/acpx/runtime-host.ts index efc09052a5b..20de8c9aace 100644 --- a/packages/paperclip-runner/src/drivers/acpx/runtime-host.ts +++ b/packages/paperclip-runner/src/drivers/acpx/runtime-host.ts @@ -1,3 +1,4 @@ +import { stageManagedGrokCredential } from "./grok-credentials.js"; import { join } from "node:path"; import { claudeNativeSkillPrompt } from "./native-skill-prompt.js"; import { nativeMcpLaunchBinding } from "../native-mcp.js"; @@ -337,6 +338,7 @@ export class AcpxRuntimeHost { if (installation.commandDigest !== profile.commandDigest) { throw new Error("Verified ACPX installation does not match its profile"); } + let admissionSucceeded = false; let command: VerifiedAcpxCommandLease | null = null; let credential: AcpxProviderLifetimeLease | null = null; let toolBridge: RunnerToolBridge | null = null; @@ -404,6 +406,13 @@ export class AcpxRuntimeHost { reportFailure: (failure) => dependencies.reportRetainedCleanupFailure(failure), }); + } else if (options.agent === "grok") { + credential = await acquireAbortableAdmissionResource({ + signal: options.signal, + acquire: () => stageManagedGrokCredential({ agentHomeDirectory: sandbox.agentHomeDirectory, environment: options.environment, retainRefresh: () => admissionSucceeded }), + resource: "credential", releaseLate: (lease) => lease.close(), + reportFailure: (failure) => dependencies.reportRetainedCleanupFailure(failure), + }); } else { credential = await acquireAbortableAdmissionResource({ signal: options.signal, @@ -417,7 +426,7 @@ export class AcpxRuntimeHost { dependencies.reportRetainedCleanupFailure(failure), }); } - if (options.agent === "claude") { + if (options.agent === "claude" || options.agent === "grok") { // The lifetime lease proves the previous provider has stopped. Refresh // the assigned snapshot before every launch, including durable resume; // Claude discovers user skills beneath its isolated CLAUDE_CONFIG_DIR. @@ -542,6 +551,7 @@ export class AcpxRuntimeHost { verifyExpectedAcpxIdentity(options.expectedIdentity, binding, identity); } options.signal?.throwIfAborted(); + admissionSucceeded = true; return new AcpxRuntimeHost({ runtime, binding, diff --git a/packages/paperclip-runner/src/drivers/acpx/runtime-sandbox.test.ts b/packages/paperclip-runner/src/drivers/acpx/runtime-sandbox.test.ts index cba482523a5..e3c7ffd1f3a 100644 --- a/packages/paperclip-runner/src/drivers/acpx/runtime-sandbox.test.ts +++ b/packages/paperclip-runner/src/drivers/acpx/runtime-sandbox.test.ts @@ -34,6 +34,24 @@ afterEach(async () => { }); describe("ACPX runtime sandbox", () => { + it("restores Grok permission controls on recovery without importing compatible hooks or credentials", async () => { + const fixture = await sandboxFixture("grok"); + const sandbox = await prepareAcpxRuntimeSandbox({ binding: fixture.binding, agent: "grok" }); + const configPath = join(sandbox.agentHomeDirectory, "config.toml"); + const requirementsPath = join(sandbox.agentHomeDirectory, "requirements.toml"); + await writeFile(configPath, '[permission]\nallow = ["Bash"]\n'); + await writeFile(requirementsPath, '[ui]\ndisable_bypass_permissions_mode = false\n'); + await prepareAcpxRuntimeSandbox({ binding: fixture.binding, agent: "grok" }); + const config = await readFile(configPath, "utf8"); + expect(config).toContain('ask = ["Bash", "Read", "Edit", "Grep", "MCPTool", "WebFetch", "WebSearch"]'); + expect(config).not.toContain('allow ='); + expect(config).toContain('login_shell_capture = false'); + expect(config).toContain('exclude = ["XAI_API_KEY"]'); + expect(config).toContain('[compat.claude]\nhooks = false\nmcps = false'); + expect(await readFile(requirementsPath, "utf8")).toContain('disable_bypass_permissions_mode = true'); + expect((await stat(requirementsPath)).mode & 0o777).toBe(0o600); + }); + it.each(["claude-sonnet-5", "sonnet", "custom-deployment-id"])( "preserves the requested Claude model %s in its isolated settings", async (model) => { @@ -318,13 +336,14 @@ describe("ACPX runtime sandbox", () => { ); }); -async function sandboxFixture(agent: "pi" | "claude" | "codex") { +async function sandboxFixture(agent: "pi" | "claude" | "codex" | "grok") { const root = await mkdtemp(join(tmpdir(), "paperclip-acpx-sandbox-")); temporaryDirectories.push(root); const workspace = join(root, "workspace"); const runtimeDirectory = join(root, "runtime"); await Promise.all([mkdir(workspace), mkdir(runtimeDirectory)]); const models = { + grok: "grok-4.7", pi: "openrouter/deepseek/deepseek-v4-flash-0731", claude: "claude-sonnet-5", codex: "gpt-5.6-sol", diff --git a/packages/paperclip-runner/src/drivers/acpx/runtime-sandbox.ts b/packages/paperclip-runner/src/drivers/acpx/runtime-sandbox.ts index 30d2f4cbb79..44782dae8e0 100644 --- a/packages/paperclip-runner/src/drivers/acpx/runtime-sandbox.ts +++ b/packages/paperclip-runner/src/drivers/acpx/runtime-sandbox.ts @@ -384,6 +384,27 @@ export async function prepareAcpxRuntimeSandbox(input: { })}\n`, ); } + if (input.agent === "grok") { + // Native project rules merge with global rules. Ask rules win over project + // allows, keeping operation approval at the runner's authenticated boundary. + await writePrivateFile(join(agentHomeDirectory, "config.toml"), [ + "[permission]", + 'ask = ["Bash", "Read", "Edit", "Grep", "MCPTool", "WebFetch", "WebSearch"]', + "[ui]", 'permission_mode = "ask"', + "[compat.claude]", "hooks = false", "mcps = false", + "[compat.cursor]", "hooks = false", "mcps = false", + "[managed_mcps]", "enabled = false", "gateway_tools_enabled = false", + "[toolset.bash]", "login_shell_capture = false", + "[shell_environment_policy]", 'exclude = ["XAI_API_KEY"]', + "[subagents]", "enabled = false", + "[workflows]", "enabled = false", + "[goal]", "enabled = false", "", + ].join("\n")); + // Always-approve may otherwise be enabled by compatible project settings. + // Even approve-all runs use one host decision per requested operation. + await writePrivateFile(join(agentHomeDirectory, "requirements.toml"), + "[ui]\ndisable_bypass_permissions_mode = true\n"); + } if (input.agent === "pi") { await writePrivateFile( join(agentHomeDirectory, "settings.json"), @@ -428,6 +449,7 @@ export async function prepareAcpxRuntimeSandbox(input: { XDG_CACHE_HOME: cacheDirectory, PAPERCLIP_ACPX_PROFILE: input.agent, PAPERCLIP_ACPX_ISOLATED_CONTEXT: "1", + ...(input.agent === "grok" ? { GROK_HOME: agentHomeDirectory, NO_BROWSER: "1", GROK_DISABLE_AUTOUPDATER: "1" } : {}), ...(input.agent === "pi" ? { PI_CODING_AGENT_DIR: agentHomeDirectory, diff --git a/packages/paperclip-runner/src/eval/live-workflow-executor.ts b/packages/paperclip-runner/src/eval/live-workflow-executor.ts index 7d81d5ef4fc..561aa6ffae1 100644 --- a/packages/paperclip-runner/src/eval/live-workflow-executor.ts +++ b/packages/paperclip-runner/src/eval/live-workflow-executor.ts @@ -339,8 +339,9 @@ async function restoreAfterDelegatedChild(input: { function acpxAgent( candidate: RunnerLiveEvalCandidate, -): "claude" | "codex" | undefined { +): "claude" | "codex" | "grok" | undefined { if (candidate.adapter !== "acpx_runtime") return undefined; + if (candidate.qualification.profile === "grok") return "grok"; if (candidate.qualification.profile === "claude") return "claude"; if (candidate.qualification.profile === "codex") return "codex"; return undefined; diff --git a/packages/paperclip-runner/src/index.ts b/packages/paperclip-runner/src/index.ts index e4eaa380f35..458de962dd7 100644 --- a/packages/paperclip-runner/src/index.ts +++ b/packages/paperclip-runner/src/index.ts @@ -42,7 +42,7 @@ export * from "./drivers/codex/codex-app-server-driver.js"; export * from "./drivers/opencode/opencode-server-driver.js"; export * from "./drivers/opencode/mcp-bridge.js"; export * from "./drivers/acpx/qualified-profiles.js"; -export { acpxRuntimeSessionDirectoryName } from "./drivers/acpx/recovery-identity.js"; +export { acpxRuntimeSessionDirectoryName, resolveAcpxRuntimeRoot } from "./drivers/acpx/recovery-identity.js"; export { probeQualifiedAcpxEnvironment, type ProbeQualifiedAcpxEnvironmentOptions, diff --git a/packages/paperclip-runner/src/issue-thread/live-projection.ts b/packages/paperclip-runner/src/issue-thread/live-projection.ts index 06f5ceab78e..7069b5ddb7b 100644 --- a/packages/paperclip-runner/src/issue-thread/live-projection.ts +++ b/packages/paperclip-runner/src/issue-thread/live-projection.ts @@ -761,7 +761,7 @@ export function projectCapabilityIssueThread( : snapshot.config.provider === "opencode" || snapshot.config.driver === "opencode_server" ? "Real OpenCode" : snapshot.config.provider === "acpx" || snapshot.config.driver === "acpx_runtime" - ? `Real ${snapshot.config.acpxAgent === "claude" ? "Claude" : snapshot.config.acpxAgent === "codex" ? "Codex" : "Pi"} via ACPX` + ? `Real ${snapshot.config.acpxAgent === "grok" ? "Grok Build" : snapshot.config.acpxAgent === "claude" ? "Claude" : snapshot.config.acpxAgent === "codex" ? "Codex" : "Pi"} via ACPX` : "Real Codex" : mode === "replay" ? "Replay" : "Fake agent"; const connection = input.connection ?? { state: "connected", attempt: 0 }; diff --git a/packages/paperclip-runner/src/live/index.ts b/packages/paperclip-runner/src/live/index.ts index c74c4ef5b18..22b2bc5e375 100644 --- a/packages/paperclip-runner/src/live/index.ts +++ b/packages/paperclip-runner/src/live/index.ts @@ -5,4 +5,4 @@ export * from "./durable-live-session-store.js"; export * from "./runnerd-codex-transport.js"; export * from "./turn-stream.js"; -export { probeAcpxClaudeInstallation } from "../drivers/acpx/installation-integrity.js"; +export { probeAcpxClaudeInstallation, probeAcpxGrokInstallation } from "../drivers/acpx/installation-integrity.js"; diff --git a/packages/paperclip-runner/src/live/live-session.test.ts b/packages/paperclip-runner/src/live/live-session.test.ts index 00bdc571f24..999c5a96830 100644 --- a/packages/paperclip-runner/src/live/live-session.test.ts +++ b/packages/paperclip-runner/src/live/live-session.test.ts @@ -24,6 +24,7 @@ import { import { DurableCapabilityLiveSessionStore } from "./durable-live-session-store.js"; import { defaultCapabilityRunnerdBinary } from "./runnerd-codex-transport.js"; import { captureTurnRejection } from "../../test/capture-turn-rejection.js"; +import * as workspaceDiff from "./workspace-diff.js"; class AsyncNotifications implements AsyncIterable { #values: CodexRpcNotification[] = []; @@ -753,20 +754,30 @@ describe("Capability live runnerd and Codex session", () => { const service = new CapabilityLiveSessionService({ transportFactory: fakeTransportFactory(state) }); const session = await service.create({ workingDirectory: root }); - const result = await session.sendMessage("edit the workspace"); - expect(result.snapshot.workspaceDiffs).toHaveLength(1); - expect(result.snapshot.workspaceDiffs?.[0]).toMatchObject({ - turnId: "turn-1", - diff: { - schema: "paperclip.workspace.diff.v1", - source: "runner_verified", - complete: true, - totals: { files: 2 }, - }, - }); - expect(result.snapshot.workspaceDiffs?.[0]?.diff.files.map((file) => file.path)) - .toEqual(["created.ts", "existing.txt"]); - await service.shutdown(session.id); + // Test the file-change evidence, not whether a loaded CI worker scans the + // fixture within the production admission deadline. Keep both snapshots + // real; make only the already-captured baseline immediately available. + const baseline = await workspaceDiff.capturePaperclipWorkspace(root); + const capture = vi.spyOn(workspaceDiff, "capturePaperclipWorkspace") + .mockResolvedValueOnce(baseline); + try { + const result = await session.sendMessage("edit the workspace"); + expect(result.snapshot.workspaceDiffs).toHaveLength(1); + expect(result.snapshot.workspaceDiffs?.[0]).toMatchObject({ + turnId: "turn-1", + diff: { + schema: "paperclip.workspace.diff.v1", + source: "runner_verified", + complete: true, + totals: { files: 2 }, + }, + }); + expect(result.snapshot.workspaceDiffs?.[0]?.diff.files.map((file) => file.path)) + .toEqual(["created.ts", "existing.txt"]); + } finally { + capture.mockRestore(); + await service.shutdown(session.id); + } }); it("suspends after every per-turn response and restores the same provider session", async () => { @@ -895,7 +906,7 @@ describe("Capability live runnerd and Codex session", () => { (request) => request.method === "thread/start", )?.params.baseInstructions, ).toBe( - "Native instructions\n\nRead-only instruction sibling root: /runtime/instructions", + 'Native instructions\n\nRead-only instruction sibling root: /runtime/instructions\nFor native paperclip_finish/paperclip_block reports, completionContract={"revision":"paperclip-capability-live-v1","criterionIds":["objective"]}. This report does not change mock task state; use the exposed semantic tools for task changes.', ); await service.shutdown(session.id); }); diff --git a/packages/paperclip-runner/src/live/live-session.ts b/packages/paperclip-runner/src/live/live-session.ts index 73359bf267c..cad893333e0 100644 --- a/packages/paperclip-runner/src/live/live-session.ts +++ b/packages/paperclip-runner/src/live/live-session.ts @@ -1,3 +1,4 @@ +import { liveRunResultFeedback } from "./run-result-feedback.js"; import { createHash, randomUUID } from "node:crypto"; import { resolve } from "node:path"; @@ -628,7 +629,7 @@ export function assertCapabilityLiveSessionSnapshot( } if (provider === "acpx") { const agent = config.acpxAgent; - if (agent !== "pi" && agent !== "claude" && agent !== "codex") { + if (agent !== "pi" && agent !== "claude" && agent !== "codex" && agent !== "grok") { throw new Error("capability_live_checkpoint_corrupt: invalid config.acpxAgent"); } const expected = resolveQualifiedAcpxProfile(agent, text(config.requestedModel)); @@ -1659,7 +1660,8 @@ export class CapabilityLiveSession { } await this.#captureTurnUsage( result.turnId, - result.status !== "completed" || options.allowMissingUsage === true, + result.status !== "completed" || options.allowMissingUsage === true || + (this.#config.provider === "acpx" && this.#config.acpxAgent === "grok"), ); await this.#persist(); await this.#afterTurnSettled(); @@ -1783,6 +1785,10 @@ export class CapabilityLiveSession { totalKeys: Object.keys(total).sort(), })}`); } + // Grok 1.0.13 does not report verified token/cost measurements. Do not + // synthesize a zero receipt from the fallback when nothing was observed. + if (this.#config.provider === "acpx" && this.#config.acpxAgent === "grok" && + (captured?.reported ?? captured?.raw ?? captured?.terminal) == null && Object.keys(total).length === 0) return; const finalUsage = selectedWithReportedCost ?? cumulativeFallback; await this.recordUsage({ receiptId: `${turnId}:usage`, @@ -2389,7 +2395,8 @@ export class CapabilityLiveSession { permissions: CODEX_PERMISSION_PROFILE, runtimeWorkspaceRoots: [this.#config.workingDirectory], baseInstructions: - this.#transportOptions.baseInstructions ?? LIVE_BASE_INSTRUCTIONS, + [this.#transportOptions.baseInstructions ?? LIVE_BASE_INSTRUCTIONS, + `For native paperclip_finish/paperclip_block reports, completionContract=${JSON.stringify(LIVE_COMPLETION_CONTRACT)}. This report does not change mock task state; use the exposed semantic tools for task changes.`].join("\n"), persistExtendedHistory: true, }); const resumedThread = record(resumed.thread); @@ -2415,7 +2422,8 @@ export class CapabilityLiveSession { runtimeWorkspaceRoots: [this.#config.workingDirectory], approvalPolicy: "never", baseInstructions: - this.#transportOptions.baseInstructions ?? LIVE_BASE_INSTRUCTIONS, + [this.#transportOptions.baseInstructions ?? LIVE_BASE_INSTRUCTIONS, + `For native paperclip_finish/paperclip_block reports, completionContract=${JSON.stringify(LIVE_COMPLETION_CONTRACT)}. This report does not change mock task state; use the exposed semantic tools for task changes.`].join("\n"), completionContract: LIVE_COMPLETION_CONTRACT, dynamicTools: [ ...tools.map(dynamicToolSpec), @@ -2511,6 +2519,13 @@ export class CapabilityLiveSession { contentItems: [{ type: "inputText", text: "Tool call was outside the active Capability thread and turn." }], }; } + const runResult = liveRunResultFeedback(operationId, request.params.arguments, LIVE_COMPLETION_CONTRACT.revision); + if (runResult) { + this.#appendEvidence("tool_call", turnId, { callId, operationId, input: jsonValue(request.params.arguments), beforeRevision: this.#port.snapshot().revision }); + this.#appendEvidence("tool_result", turnId, { callId, operationId, result: jsonValue(runResult), beforeRevision: this.#port.snapshot().revision, afterRevision: this.#port.snapshot().revision }); + await this.#persist(); + return this.#codexToolResponse(runResult); + } if (operationId === DISCOVER_TOOL) { const args = record(request.params.arguments); try { diff --git a/packages/paperclip-runner/src/live/run-result-feedback.test.ts b/packages/paperclip-runner/src/live/run-result-feedback.test.ts new file mode 100644 index 00000000000..af6331d3da7 --- /dev/null +++ b/packages/paperclip-runner/src/live/run-result-feedback.test.ts @@ -0,0 +1,20 @@ +import { describe, expect, it } from "vitest"; +import { liveRunResultFeedback } from "./run-result-feedback.js"; +const result = { + reportedWorkDisposition: "done", summary: "Reported completion", + completionClaim: { contractRevision: "revision-1", objectiveSatisfied: true, criteria: [{ criterionId: "objective", status: "satisfied", evidenceRefs: [] }], remainingWork: [] }, + evidence: [], verification: [], attentionRequests: [], artifacts: [], +}; +describe("live run-result feedback", () => { + it("acknowledges a valid native run report without pretending to mutate the task", () => { + expect(liveRunResultFeedback("paperclip_finish", result, "revision-1")).toEqual({ ok: true, result: { reportedWorkDisposition: "done", accepted: true, taskStateChanged: false } }); + }); + it.each([null, {}, { ...result, completionClaim: {} }, { ...result, completionClaim: { ...result.completionClaim, criteria: [] } }])("rejects missing or malformed evidence", value => { + expect(liveRunResultFeedback("paperclip_finish", value, "revision-1")?.ok).toBe(false); + }); + it("rejects stale revisions and mismatched terminal operations", () => { + expect(liveRunResultFeedback("paperclip_finish", result, "revision-2")?.ok).toBe(false); + expect(liveRunResultFeedback("paperclip_block", result, "revision-1")?.ok).toBe(false); + expect(liveRunResultFeedback("finish_task", result, "revision-1")).toBeNull(); + }); +}); diff --git a/packages/paperclip-runner/src/live/run-result-feedback.ts b/packages/paperclip-runner/src/live/run-result-feedback.ts new file mode 100644 index 00000000000..5d7f44a1bae --- /dev/null +++ b/packages/paperclip-runner/src/live/run-result-feedback.ts @@ -0,0 +1,18 @@ +import { PRP_BLOCK_TOOL_NAME, PRP_COMPLETION_TOOL_NAME } from "../contracts/completion-result.js"; +import { validatePrpStructuredRunResult } from "../protocol/replay-contract.js"; + +/** Accept a provider run report without mutating or grading the mock task. */ +export function liveRunResultFeedback(operation: string, value: unknown, revision: string) { + if (operation !== PRP_COMPLETION_TOOL_NAME && operation !== PRP_BLOCK_TOOL_NAME) return null; + const parsed = validatePrpStructuredRunResult(value); + if (!parsed.ok) return { ok: false, denial: { code: "invalid_run_result", message: "Run result does not satisfy the completion schema." } }; + const result = parsed.result; + if ((operation === PRP_BLOCK_TOOL_NAME) !== (result.reportedWorkDisposition === "blocked")) { + return { ok: false, denial: { code: "invalid_run_result", message: "Run disposition does not match the selected tool." } }; + } + if (result.completionClaim.contractRevision !== revision || + result.completionClaim.criteria.length !== 1 || result.completionClaim.criteria[0]?.criterionId !== "objective") { + return { ok: false, denial: { code: "stale_completion_contract", message: `Use completion contract ${revision} with criterion objective.` } }; + } + return { ok: true, result: { reportedWorkDisposition: result.reportedWorkDisposition, accepted: true, taskStateChanged: false } }; +} diff --git a/packages/paperclip-runner/src/live/runnerd-codex-transport.test.ts b/packages/paperclip-runner/src/live/runnerd-codex-transport.test.ts index 2f5d38c249a..b1113094ecb 100644 --- a/packages/paperclip-runner/src/live/runnerd-codex-transport.test.ts +++ b/packages/paperclip-runner/src/live/runnerd-codex-transport.test.ts @@ -1089,6 +1089,30 @@ it("quiesces the control route before checkpoint and containment regardless of p expect(failedCheckpointSteps).toEqual(["release", "checkpoint", "kill"]); }); +it("does not release a session before asynchronous process containment settles", async () => { + let finishKill!: () => void; + const killed = new Promise((resolve) => { finishKill = resolve; }); + const steps: string[] = []; + const released = runnerdRecoveryInternals.releaseRunnerProcessOwnership({ + runnerSettled: false, + release: () => { steps.push("route-closed"); }, + checkpoint: () => { steps.push("checkpoint"); }, + forceKill: async () => { steps.push("kill-dispatched"); await killed; steps.push("process-exited"); }, + }).then(() => { steps.push("session-reusable"); }); + await new Promise(resolve => setImmediate(resolve)); + expect(steps).toEqual(["route-closed", "checkpoint", "kill-dispatched"]); + finishKill(); + await released; + expect(steps).toEqual(["route-closed", "checkpoint", "kill-dispatched", "process-exited", "session-reusable"]); +}); + +it("refuses session reuse when asynchronous process containment fails", async () => { + await expect(runnerdRecoveryInternals.releaseRunnerProcessOwnership({ + runnerSettled: true, release: null, checkpoint: null, + forceKill: async () => { throw new Error("remote process still alive"); }, + })).rejects.toThrow("remote process still alive"); +}); + it("waits for the exact durable suspension command behind prior close work", async () => { const commands = [ { @@ -2120,6 +2144,33 @@ it("routes canonical session goals back through the Codex notification facade", ); }); +it("keeps canonical ACPX reasoning out of assistant deltas", () => { + expect(runnerdCanonicalNotificationMethod("item.delta", { + kind: "reasoning", channel: "summary", text: "Private reasoning", + })).toBe("item/reasoning/summaryTextDelta"); + expect(runnerdCanonicalNotificationMethod("item.delta", { + kind: "reasoning", channel: "detail", text: "Private reasoning", + })).toBe("item/reasoning/textDelta"); + expect(runnerdCanonicalNotificationMethod("item.delta", { + kind: "agentMessage", text: "Visible answer", + })).toBe("item/agentMessage/delta"); +}); + +it("preserves reasoning kinds and order inside coalesced canonical deltas", () => { + const events = [ + { kind: "reasoning", channel: "summary", text: "Private reasoning", itemId: "thought-1" }, + { kind: "agentMessage", text: "Visible answer", itemId: "answer-1" }, + { kind: "reasoning", channel: "detail", text: "Private detail", itemId: "thought-2" }, + ]; + expect(expandRunnerdCanonicalNotifications("item/agentMessage/delta", { + coalescedCount: events.length, events, + }, "item.delta")).toEqual([ + { method: "item/reasoning/summaryTextDelta", params: events[0] }, + { method: "item/agentMessage/delta", params: events[1] }, + { method: "item/reasoning/textDelta", params: events[2] }, + ]); +}); + it("continues consuming after the durable committed-event window rolls", () => { const rollingWindow = Array.from({ length: 64 }, (_, index) => ({ sourceSeq: index + 65, diff --git a/packages/paperclip-runner/src/live/runnerd-codex-transport.ts b/packages/paperclip-runner/src/live/runnerd-codex-transport.ts index bdfb23bdb3c..829413ad815 100644 --- a/packages/paperclip-runner/src/live/runnerd-codex-transport.ts +++ b/packages/paperclip-runner/src/live/runnerd-codex-transport.ts @@ -678,7 +678,7 @@ async function releaseRunnerProcessOwnership(input: { runnerSettled: boolean; checkpoint: ((settlement: "settled" | "unsettled") => Promise | void) | null; - forceKill: () => void; + forceKill: () => Promise | void; release: (() => Promise | void) | null; }): Promise { let releaseFailure: unknown; @@ -701,7 +701,7 @@ async function releaseRunnerProcessOwnership(input: { } catch (error) { checkpointFailure = error; } finally { - input.forceKill(); + await input.forceKill(); } if (checkpointFailure !== undefined) throw checkpointFailure; if (releaseFailure !== undefined) throw releaseFailure; @@ -1322,10 +1322,19 @@ export function unseenRunnerdCommittedEvents< export function expandRunnerdCanonicalNotifications( method: string, input: unknown, + eventType?: string, ): Array<{ method: string; params: Record }> { const payload = record(input); if (!Array.isArray(payload.events)) return [{ method, params: payload }]; - return payload.events.map((event) => ({ method, params: record(event) })); + return payload.events.map((event) => { + const params = record(event); + return { + method: eventType + ? runnerdCanonicalNotificationMethod(eventType, params) ?? method + : method, + params, + }; + }); } export function runnerdCanonicalNotificationMethod( @@ -1338,6 +1347,14 @@ export function runnerdCanonicalNotificationMethod( if (eventType === "session.goal.snapshot" && payload.goal === null) { return undefined; } + // ACPX thoughts retain their reasoning kind through the notification facade. + // Treating them as assistant deltas exposes them in the task transcript and + // bypasses the existing reasoning redaction and progress handling. + if (eventType === "item.delta" && payload.kind === "reasoning") { + return payload.channel === "detail" + ? "item/reasoning/textDelta" + : "item/reasoning/summaryTextDelta"; + } return ( { "turn.started": "turn/started", @@ -3121,6 +3138,8 @@ export function createCapabilityRunnerdProviderEnvironment(input: { input.options.environment, input.options.acpxAgent ?? "codex", ).env, + ...(input.options.acpxAgent === "grok" && input.options.environment?.PAPERCLIP_ACPX_GROK_AUTH_JSON_SECRET + ? { PAPERCLIP_ACPX_GROK_AUTH_JSON_SECRET: input.options.environment.PAPERCLIP_ACPX_GROK_AUTH_JSON_SECRET } : {}), ...commonIdentity, // The verified sidecar bundle cannot use import.meta.url while Node // executes it through /proc/self/fd. Anchor its closed provider package @@ -3263,7 +3282,7 @@ export function createRunnerdCodexAppServerArgs(input: { ); } -function unwrapToolResponse(response: Record): { +export function unwrapToolResponse(response: Record, preserveEnvelope = false): { readonly __paperclipSemanticToolOutcome: true; readonly result: unknown; readonly isError: boolean; @@ -3274,7 +3293,7 @@ function unwrapToolResponse(response: Record): { const value = record(items[0]).text; let result: unknown = response; try { - if (typeof value === "string") result = JSON.parse(value); + if (!preserveEnvelope && typeof value === "string") result = JSON.parse(value); } catch { result = response; } @@ -4261,8 +4280,18 @@ class DurablePrpCodexTransport implements CodexAppServerTransport { adoptedRunner && !this.#adoptedRunnerAuthenticated ? null : this.#controlPlaneCheckpoint, - forceKill: () => { - this.#handle?.child.kill("SIGKILL"); + forceKill: async () => { + const handle = this.#handle; + if (!handle || this.#evidence.runnerExited) return; + handle.child.kill("SIGKILL"); + // A remote kill dispatches an asynchronous, ownership-fenced RPC. + // Do not release the session for reuse until its process monitor has + // settled: the successor would otherwise overwrite that ownership + // marker while the previous runner still holds the fixed listener. + const result = await waitForProcess(handle, 15_000); + this.#evidence.runnerExited = true; + this.#evidence.runnerExitCode = result.code; + this.#evidence.runnerSignal = result.signal as NodeJS.Signals | null; }, release: this.#controlPlaneRelease, }); @@ -4734,7 +4763,7 @@ class DurablePrpCodexTransport implements CodexAppServerTransport { : provider === "acpx" ? acpxAgent === "pi" ? "openrouter" - : acpxAgent === "claude" + : acpxAgent === "grok" ? "xai" : acpxAgent === "claude" ? "anthropic" : "openai" : "openai", @@ -5470,6 +5499,8 @@ class DurablePrpCodexTransport implements CodexAppServerTransport { } : {}), }), + this.options.provider === "acpx" && + (call.operationId === "paperclip_finish" || call.operationId === "paperclip_block"), ); if (call.operationId === "call_api") { const result = record(outcome.result); @@ -5968,7 +5999,7 @@ class DurablePrpCodexTransport implements CodexAppServerTransport { : event.eventType === "provider.event" ? unwrapRunnerdProviderNotifications(eventPayload) : canonicalMethod - ? expandRunnerdCanonicalNotifications(canonicalMethod, eventPayload) + ? expandRunnerdCanonicalNotifications(canonicalMethod, eventPayload, event.eventType) : []; for (const payload of notifications) { const method = payload.method; diff --git a/packages/paperclip-runner/src/live/runnerd-tool-response.test.ts b/packages/paperclip-runner/src/live/runnerd-tool-response.test.ts new file mode 100644 index 00000000000..536e41ae2b0 --- /dev/null +++ b/packages/paperclip-runner/src/live/runnerd-tool-response.test.ts @@ -0,0 +1,14 @@ +import { describe, expect, it } from "vitest"; +import { unwrapToolResponse } from "./runnerd-codex-transport.js"; + +describe("runnerd semantic tool response", () => { + it("preserves the validated ACPX terminal acknowledgement envelope", () => { + const response = { success: true, contentItems: [{ type: "inputText", text: JSON.stringify({ ok: true, accepted: true }) }] }; + expect(unwrapToolResponse(response, true)).toEqual({ __paperclipSemanticToolOutcome: true, result: response, isError: false }); + expect(unwrapToolResponse(response)).toEqual({ __paperclipSemanticToolOutcome: true, result: { ok: true, accepted: true }, isError: false }); + }); + it("keeps denial feedback and does not promote rejected terminal reports to success", () => { + const response = { success: false, contentItems: [{ type: "inputText", text: JSON.stringify({ ok: false, denial: { code: "stale_completion_contract" } }) }] }; + expect(unwrapToolResponse(response, true)).toMatchObject({ isError: true, result: { success: false } }); + }); +}); diff --git a/packages/paperclip-runner/src/provider-events.test.ts b/packages/paperclip-runner/src/provider-events.test.ts index f7485a4593e..c395477b41e 100644 --- a/packages/paperclip-runner/src/provider-events.test.ts +++ b/packages/paperclip-runner/src/provider-events.test.ts @@ -7,6 +7,7 @@ import { canonicalProviderEventsFromCodex, canonicalProviderEventsFromOpenCodePart, createAcpxToolEventNormalizer, + createGrokMessageNormalizer, providerFamilyCapabilities, } from "./provider-events.js"; import { validatePrpEvent } from "./protocol/replay-contract.js"; @@ -795,3 +796,26 @@ describe("provider-neutral events", () => { ).toMatchObject({ ok: false }); }); }); + + +describe("Grok output message boundaries", () => { + it("keeps streamed chunks together and separates commentary from the post-tool answer", () => { + const normalize = createGrokMessageNormalizer(); + expect(normalize({ type: "text_delta", text: "I will " }).messageId).toBe("grok-output-0"); + expect(normalize({ type: "text_delta", text: "check." }).messageId).toBe("grok-output-0"); + normalize({ type: "tool_call", toolCallId: "tool", status: "pending" }); + normalize({ type: "tool_call", toolCallId: "tool", status: "completed" }); + const thought = { type: "text_delta", stream: "thought", text: "private" }; + expect(normalize(thought)).toBe(thought); + expect(normalize({ type: "text_delta", text: "Final" }).messageId).toBe("grok-output-1"); + expect(normalize({ type: "text_delta", text: " answer" }).messageId).toBe("grok-output-1"); + }); + it("preserves native IDs and does not invent boundaries for status updates", () => { + const normalize = createGrokMessageNormalizer(); + expect(normalize({ type: "text_delta", messageId: "native-id", text: "hi" }).messageId).toBe("native-id"); + normalize({ type: "status", text: "working" }); + expect(normalize({ type: "text_delta", messageId: "native-id", text: " there" }).messageId).toBe("native-id"); + const reasoning = { type: "text_delta", tag: "agent_thought_chunk", text: "private" }; + expect(normalize(reasoning)).toBe(reasoning); + }); +}); diff --git a/packages/paperclip-runner/src/provider-events.ts b/packages/paperclip-runner/src/provider-events.ts index 9efd699aed0..360a70c96d5 100644 --- a/packages/paperclip-runner/src/provider-events.ts +++ b/packages/paperclip-runner/src/provider-events.ts @@ -246,6 +246,23 @@ function boundedAcpxLifecycleLocations(value: unknown): unknown[] | undefined { * title as the literal `tool call`, so consumers must restore lifecycle * identity before translating the event into a durable protocol record. */ +/** Grok omits message IDs. A tool boundary ends its preceding output segment. + * Thoughts never become output and tool progress cannot fragment output chunks. + * Create one normalizer per turn; native IDs, when present, remain authoritative. + */ +export function createGrokMessageNormalizer(): (event: T) => T { + let segment = 0; + let hasOutput = false; + let boundary = false; + return (event) => { + if (event.type === "tool_call" && hasOutput) boundary = true; + if (event.type !== "text_delta" || event.stream === "thought" || event.tag === "agent_thought_chunk") return event; + if (boundary) { segment += 1; boundary = false; } + hasOutput = true; + return { ...event, messageId: event.messageId || `grok-output-${segment}` }; + }; +} + export function createAcpxToolEventNormalizer< T extends AcpRuntimeEventShape, >(): (event: T) => T { diff --git a/packages/paperclip-runner/test/acpx-codex-package-contract.test.mjs b/packages/paperclip-runner/test/acpx-codex-package-contract.test.mjs index 14e6018a8ba..3868dda6c5e 100644 --- a/packages/paperclip-runner/test/acpx-codex-package-contract.test.mjs +++ b/packages/paperclip-runner/test/acpx-codex-package-contract.test.mjs @@ -178,6 +178,28 @@ test("the ACPX patch fails closed on an invalid spawn environment", () => { ); }); +test("authentication rejects invalid isolated environments without host fallback", () => { + const addedSource = acpxPatch.split("\n") + .filter((line) => line.startsWith("+") && !line.startsWith("+++")) + .map((line) => line.slice(1)).join("\n"); + const start = addedSource.indexOf("function isPlainStringEnvironment(value)"); + const end = addedSource.indexOf("function buildAgentEnvironment(", start); + assert.ok(start >= 0 && end > start); + const hostEnvironment = { XAI_API_KEY: "host-credential-must-not-leak" }; + const resolveEnvironment = new Function( + "process", `${addedSource.slice(start, end)}; return resolveAgentEnvironment;`, + )({ env: hostEnvironment }); + for (const invalid of [undefined, null, [], "invalid", { XAI_API_KEY: 1 }]) { + assert.throws(() => resolveEnvironment(() => invalid), TypeError); + } + const isolated = {}; + assert.equal(resolveEnvironment(() => isolated), isolated); + assert.equal(resolveEnvironment(() => isolated).XAI_API_KEY, undefined); + assert.equal(resolveEnvironment(undefined), hostEnvironment); + assert.match(addedSource, /readEnvCredential\(method\.id, resolveAgentEnvironment\(this\.options\.spawnEnvironment\)\)/); + assert.match(addedSource, /resolveAgentEnvironment\(this\.options\.spawnEnvironment\)\)\.XAI_API_KEY/); +}); + test("the Codex patch enforces isolated instructions, tools, and skills", () => { for (const token of [ "PAPERCLIP_ACPX_ISOLATED_CONTEXT", diff --git a/packages/shared/src/ai-connections.ts b/packages/shared/src/ai-connections.ts index 663dac36676..496d3744567 100644 --- a/packages/shared/src/ai-connections.ts +++ b/packages/shared/src/ai-connections.ts @@ -120,6 +120,8 @@ export function isAiConnectionCompatible( runnerProvider === "claude" || (runnerProvider === "acpx" && acpxAgent === "claude") ? "claude_local" + : runnerProvider === "acpx" && acpxAgent === "grok" + ? "grok_local" : runnerProvider === "codex" ? "codex_local" : runnerProvider === "opencode" diff --git a/patches/acpx@0.13.1.patch b/patches/acpx@0.13.1.patch index 57e1cfe435b..cd662aba257 100644 --- a/patches/acpx@0.13.1.patch +++ b/patches/acpx@0.13.1.patch @@ -1,5 +1,5 @@ diff --git a/dist/client-CxNllqui.d.ts b/dist/client-CxNllqui.d.ts -index 5e2113ac0a1a92c99322cf01e5c106760a0b0dbf..b7b5151f3da45b5e0d12aea55e9f3b071ab54a8d 100644 +index 5e2113a..b7b5151 100644 --- a/dist/client-CxNllqui.d.ts +++ b/dist/client-CxNllqui.d.ts @@ -135,6 +135,7 @@ declare class AcpClient { @@ -11,7 +11,7 @@ index 5e2113ac0a1a92c99322cf01e5c106760a0b0dbf..b7b5151f3da45b5e0d12aea55e9f3b07 private setSessionModelThroughConfig; private setSessionModelThroughLegacyMethod; diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js -index d454fd7c5bf742b469be75eb8c3988b694a0ffaa..e930a933d259e79a6b03b1148aa58d686b781129 100644 +index d454fd7..991d21d 100644 --- a/dist/live-checkpoint-BSIrfgVo.js +++ b/dist/live-checkpoint-BSIrfgVo.js @@ -1068,6 +1068,7 @@ function serializeSessionRecordForDisk(record) { @@ -39,7 +39,20 @@ index d454fd7c5bf742b469be75eb8c3988b694a0ffaa..e930a933d259e79a6b03b1148aa58d68 const OPAQUE_VALUE_PATHS = /* @__PURE__ */ new Set([ "agent_capabilities", "messages.Agent.content.ToolUse.input", -@@ -3135,8 +3137,18 @@ function promotePrefixedAuthEnvironment(env) { +@@ -3104,10 +3106,10 @@ function authEnvKey(methodId) { + authEnvKeyCache.set(methodId, key); + return key; + } +-function readEnvCredential(methodId) { ++function readEnvCredential(methodId, environment = process.env) { + const key = authEnvKey(methodId); + if (!key) return; +- const value = process.env[key]; ++ const value = environment[key]; + if (typeof value === "string" && value.trim().length > 0) return value; + } + function protectedEnvKey(key) { +@@ -3135,8 +3137,21 @@ function promotePrefixedAuthEnvironment(env) { } return protectedKeys; } @@ -50,17 +63,20 @@ index d454fd7c5bf742b469be75eb8c3988b694a0ffaa..e930a933d259e79a6b03b1148aa58d68 + const prototype = Object.getPrototypeOf(value); + return (prototype === Object.prototype || prototype === null) && Object.values(value).every((entry) => typeof entry === "string"); +} -+function buildAgentEnvironment(authCredentials, sessionEnv, spawnEnvironment) { ++function resolveAgentEnvironment(spawnEnvironment) { + let sourceEnvironment = process.env; + if (spawnEnvironment !== void 0) { + sourceEnvironment = spawnEnvironment(); + if (!isPlainStringEnvironment(sourceEnvironment)) throw new TypeError("ACPX spawn environment must be a plain record of string values"); + } -+ const env = { ...sourceEnvironment }; ++ return sourceEnvironment; ++} ++function buildAgentEnvironment(authCredentials, sessionEnv, spawnEnvironment) { ++ const env = { ...resolveAgentEnvironment(spawnEnvironment) }; const protectedAuthEnvKeys = promotePrefixedAuthEnvironment(env); if (authCredentials) for (const [methodId, credential] of Object.entries(authCredentials)) { addAuthCredentialEnvKeys(protectedAuthEnvKeys, methodId, credential); -@@ -3178,10 +3190,10 @@ function resolveConfiguredAuthCredential(methodId, authCredentials) { +@@ -3178,10 +3193,10 @@ function resolveConfiguredAuthCredential(methodId, authCredentials) { const configCredentials = authCredentials ?? {}; return configCredentials[methodId] ?? configCredentials[toEnvToken(methodId)]; } @@ -73,7 +89,7 @@ index d454fd7c5bf742b469be75eb8c3988b694a0ffaa..e930a933d259e79a6b03b1148aa58d68 stdio: [ "pipe", "pipe", -@@ -3961,10 +3973,13 @@ function resolveClientCapabilities(params) { +@@ -3961,10 +3976,13 @@ function resolveClientCapabilities(params) { ...params.elicitationModes.includes("url") ? { url: {} } : {} } } : {} }; @@ -89,7 +105,7 @@ index d454fd7c5bf742b469be75eb8c3988b694a0ffaa..e930a933d259e79a6b03b1148aa58d68 }; } function hasResponseField(response, field) { -@@ -4216,9 +4231,21 @@ var AcpClient = class { +@@ -4216,9 +4234,21 @@ var AcpClient = class { this.lastAgentExit = void 0; this.lastKnownPid = child.pid ?? void 0; this.attachAgentLifecycleObservers(child); @@ -111,7 +127,7 @@ index d454fd7c5bf742b469be75eb8c3988b694a0ffaa..e930a933d259e79a6b03b1148aa58d68 if (!this.options.verbose) return; process.stderr.write(chunk); }); -@@ -4253,7 +4280,12 @@ var AcpClient = class { +@@ -4253,7 +4283,12 @@ var AcpClient = class { geminiAcp: isGeminiAcpCommand(spawnCommand, args), copilotAcp: isCopilotAcpCommand(spawnCommand, args), claudeAcp: isClaudeAcpCommand(spawnCommand, args), @@ -125,7 +141,7 @@ index d454fd7c5bf742b469be75eb8c3988b694a0ffaa..e930a933d259e79a6b03b1148aa58d68 }; } logAgentLaunch(plan) { -@@ -4279,10 +4311,17 @@ var AcpClient = class { +@@ -4279,10 +4314,17 @@ var AcpClient = class { } async spawnAgentProcess(plan) { const spawnCommand = buildAgentSpawnCommand(plan.spawnCommand, plan.args, process.platform, plan.spawnOptions.env); @@ -145,7 +161,37 @@ index d454fd7c5bf742b469be75eb8c3988b694a0ffaa..e930a933d259e79a6b03b1148aa58d68 try { await waitForSpawn$1(spawnedChild); } catch (error) { -@@ -5028,6 +5067,12 @@ var AcpClient = class { +@@ -4422,7 +4464,10 @@ var AcpClient = class { + const createPromise = this.runConnectionRequest(() => connection.newSession({ + cwd: sessionCwd, + mcpServers: this.options.mcpServers ?? [], +- _meta: buildClaudeCodeOptionsMeta(this.options.sessionOptions, claudeAcp) ++ _meta: this.isGrokBuildAcpCommand() ++ ? { rules: typeof this.options.sessionOptions?.systemPrompt === "string" ++ ? this.options.sessionOptions.systemPrompt : this.options.sessionOptions?.systemPrompt?.append ?? "" } ++ : buildClaudeCodeOptionsMeta(this.options.sessionOptions, claudeAcp) + })); + result = claudeAcp ? await withTimeout(createPromise, resolveClaudeAcpSessionCreateTimeoutMs()) : await createPromise; + } catch (error) { +@@ -4864,7 +4909,7 @@ var AcpClient = class { + } + selectAuthMethod(methods) { + for (const method of methods) { +- const envCredential = readEnvCredential(method.id); ++ const envCredential = readEnvCredential(method.id, resolveAgentEnvironment(this.options.spawnEnvironment)); + if (envCredential) return { + methodId: method.id, + credential: envCredential, +@@ -4890,7 +4935,7 @@ var AcpClient = class { + } + readAgentSpecificEnvCredential(methodId) { + if (!this.isGrokBuildAcpCommand() || methodId !== "xai.api_key") return; +- const value = process.env.XAI_API_KEY; ++ const value = (resolveAgentEnvironment(this.options.spawnEnvironment)).XAI_API_KEY; + return typeof value === "string" && value.trim().length > 0 ? value : void 0; + } + selectAgentManagedAuthMethod(methodId) { +@@ -5028,6 +5073,12 @@ var AcpClient = class { attachAgentLifecycleObservers(child) { child.once("exit", (exitCode, signal) => { this.recordAgentExit("process_exit", exitCode, signal); @@ -158,7 +204,7 @@ index d454fd7c5bf742b469be75eb8c3988b694a0ffaa..e930a933d259e79a6b03b1148aa58d68 }); child.once("close", (exitCode, signal) => { this.recordAgentExit("process_close", exitCode, signal); -@@ -6435,6 +6480,27 @@ async function withConnectedSession(options) { +@@ -6435,6 +6486,27 @@ async function withConnectedSession(options) { //#region src/runtime/engine/prompt-turn.ts const SESSION_REPLY_IDLE_MS = 1e3; const SESSION_REPLY_DRAIN_TIMEOUT_MS = 5e3; @@ -186,7 +232,7 @@ index d454fd7c5bf742b469be75eb8c3988b694a0ffaa..e930a933d259e79a6b03b1148aa58d68 async function runPromptTurn(params) { try { const promptPromise = params.client.prompt(params.sessionId, params.prompt, params.onPromptRequestStarted, params.onElicitation); -@@ -6444,6 +6510,19 @@ async function runPromptTurn(params) { +@@ -6444,6 +6516,19 @@ async function runPromptTurn(params) { idleMs: SESSION_REPLY_IDLE_MS, timeoutMs: SESSION_REPLY_DRAIN_TIMEOUT_MS }).catch(() => {}); @@ -206,15 +252,8 @@ index d454fd7c5bf742b469be75eb8c3988b694a0ffaa..e930a933d259e79a6b03b1148aa58d68 recordPromptResponseUsage(params.conversation, response.usage, params.promptMessageId); return { stopReason: response.stopReason, -@@ -6518,4 +6597,4 @@ var LiveSessionCheckpoint = class { - //#endregion - export { writeSessionRecord as $, PERMISSION_POLICY_ACTIONS as $t, REQUESTED_MODEL_UNSUPPORTED_ERROR_CODE as A, TimeoutError as At, getAcpxVersion as B, formatErrorMessage as Bt, mergeSessionOptions as C, PromptInputValidationError as Ct, applyLifecycleSnapshotToRecord as D, promptToDisplayText as Dt, applyConversation as E, parsePromptSource as Et, modelStateFromConfigOptions as F, normalizeAgentName$1 as Ft, findSession as G, toAcpErrorPayload as Gt, DEFAULT_HISTORY_LIMIT as H, normalizeOutputError as Ht, normalizeAgentCommandInput as I, resolveAgentArgv as It, listSessions as J, NON_INTERACTIVE_PERMISSION_POLICIES as Jt, findSessionByDirectoryWalk as K, AUTH_POLICIES as Kt, renderArgvIdentity as L, resolveAgentCommand as Lt, RequestedModelUnsupportedError as M, withTimeout as Mt, assertRequestedModelSupported as N, DEFAULT_AGENT_NAME as Nt, reconcileAgentSessionId as O, textPrompt as Ot, isRequestedModelUnsupportedError as P, listBuiltInAgents as Pt, resolveSessionRecord as Q, PERMISSION_MODES as Qt, runTimedExecFile as R, resolveCanonicalAgentName as Rt, advertisedModelState as S, parsePromptStopReason as St, sessionOptionsFromRecord as T, mergePromptSourceWithText as Tt, absolutePath as U, extractAcpError as Ut, permissionModeSatisfies as V, isRetryablePromptError as Vt, findGitRepositoryRoot as W, isAcpResourceNotFoundError as Wt, normalizeName as X, OUTPUT_ERROR_ORIGINS as Xt, listSessionsForAgent as Y, OUTPUT_ERROR_CODES as Yt, pruneSessions as Z, OUTPUT_FORMATS as Zt, createSessionConversation as _, sessionEventLockPath as _t, applyRequestedModelIfAdvertised as a, measurePerf as at, recordSessionUpdate as b, isAcpJsonRpcMessage as bt, setCurrentModelId as c, setPerfGauge as ct, setDesiredModelId as d, serializeSessionRecordForDisk as dt, SESSION_RECORD_SCHEMA as en, createAtomicWriteTempPath as et, syncAdvertisedModelState as f, normalizeRuntimeSessionId as ft, cloneSessionConversation as g, sessionEventActivePath as gt, cloneSessionAcpxState as h, sessionBaseDir$1 as ht, connectAndLoadSession as i, QueueProtocolError as in, incrementPerfCounter as it, REQUESTED_MODEL_UNSUPPORTED_REASONS as j, withInterrupt as jt, AcpClient as k, InterruptedError as kt, setDesiredConfigOption as l, startPerfTimer as lt, applyConfigOptionsToState as m, defaultSessionEventLog as mt, runPromptTurn as n, AgentSpawnError as nn, formatPerfMetric as nt, currentModelIdFromSetModelResponse as o, recordPerfDuration as ot, applyConfigOptionsToRecord as p, DEFAULT_EVENT_SEGMENT_MAX_BYTES as pt, isoNow$2 as q, EXIT_CODES as qt, withConnectedSession as r, QueueConnectionError as rn, getPerfMetricsSnapshot as rt, clearDesiredConfigOption as s, resetPerfMetrics as st, LiveSessionCheckpoint as t, AcpxOperationalError as tn, assertPersistedKeyPolicy as tt, setDesiredModeId as u, parseSessionRecord as ut, recordClientOperation as v, sessionEventSegmentPath as vt, persistSessionOptions as w, isPromptInput as wt, trimConversationForRuntime as x, parseJsonRpcErrorMessage as xt, recordPromptSubmission as y, extractSessionUpdateNotification as yt, splitCommandLine as z, exitCodeForOutputErrorCode as zt }; - --//# sourceMappingURL=live-checkpoint-BSIrfgVo.js.map -\ No newline at end of file -+//# sourceMappingURL=live-checkpoint-BSIrfgVo.js.map diff --git a/dist/runtime.d.ts b/dist/runtime.d.ts -index e8102acb03c4c38830ad5ec22f356125eb0423b7..2277321c58c1265ca930c69eb3f8869bc30cfa8a 100644 +index e8102ac..2277321 100644 --- a/dist/runtime.d.ts +++ b/dist/runtime.d.ts @@ -1,7 +1,8 @@ @@ -311,7 +350,7 @@ index e8102acb03c4c38830ad5ec22f356125eb0423b7..2277321c58c1265ca930c69eb3f8869b text: string; attachments?: AcpRuntimeTurnAttachment[]; diff --git a/dist/runtime.js b/dist/runtime.js -index a1f4a70a003792c6eacf68b6b038f37bfec1db53..57117c9f6f2e9c737b138bc86977a43b3383f379 100644 +index a1f4a70..57117c9 100644 --- a/dist/runtime.js +++ b/dist/runtime.js @@ -371,7 +371,7 @@ const PROMPT_EVENT_PARSERS = { @@ -511,15 +550,8 @@ index a1f4a70a003792c6eacf68b6b038f37bfec1db53..57117c9f6f2e9c737b138bc86977a43b async cancel(input) { const { handle } = this.resolveManagerHandle(input.handle); await (await this.getManager()).cancel(handle); -@@ -2178,4 +2260,4 @@ function createRuntimeStore(options) { - //#endregion - export { ACPX_BACKEND_ID, AcpRuntimeError, AcpxRuntime, DEFAULT_AGENT_NAME, REQUESTED_MODEL_UNSUPPORTED_ERROR_CODE, REQUESTED_MODEL_UNSUPPORTED_REASONS, RequestedModelUnsupportedError, createAcpRuntime, createAgentRegistry, createFileSessionStore, createRuntimeStore, decodeAcpxRuntimeHandleState, encodeAcpxRuntimeHandleState, isAcpRuntimeError, isRequestedModelUnsupportedError }; - --//# sourceMappingURL=runtime.js.map -\ No newline at end of file -+//# sourceMappingURL=runtime.js.map diff --git a/dist/session-options-DwRDODlr.d.ts b/dist/session-options-DwRDODlr.d.ts -index c3da1645235bbea22de3f8484149051cd7dca56b..ab6c1bf8aaf879800552d22f357e88b57b36faa2 100644 +index c3da164..ab6c1bf 100644 --- a/dist/session-options-DwRDODlr.d.ts +++ b/dist/session-options-DwRDODlr.d.ts @@ -1,4 +1,5 @@ diff --git a/server/src/__tests__/adapter-registry.test.ts b/server/src/__tests__/adapter-registry.test.ts index 39688126530..805a959ac84 100644 --- a/server/src/__tests__/adapter-registry.test.ts +++ b/server/src/__tests__/adapter-registry.test.ts @@ -17,7 +17,10 @@ import { setOverridePaused, } from "../adapters/registry.js"; -vi.mock("@paperclipai/paperclip-runner/live", () => ({ probeAcpxClaudeInstallation: vi.fn(async () => undefined) })); +vi.mock("@paperclipai/paperclip-runner/live", () => ({ + probeAcpxClaudeInstallation: vi.fn(async () => undefined), + probeAcpxGrokInstallation: vi.fn(async () => undefined), +})); const externalAdapter: ServerAdapterModule = { type: "external_test", diff --git a/server/src/__tests__/adapter-routes.test.ts b/server/src/__tests__/adapter-routes.test.ts index 6f5e857046a..c27161dfd88 100644 --- a/server/src/__tests__/adapter-routes.test.ts +++ b/server/src/__tests__/adapter-routes.test.ts @@ -375,8 +375,15 @@ describe("adapter routes", () => { }), ])); const acpxAgent = res.body.fields.find((field: { key?: string }) => field.key === "acpxAgent"); - expect(acpxAgent).toBeUndefined(); - expect(JSON.stringify(res.body)).toContain("ACPX Claude"); + expect(acpxAgent).toMatchObject({ + type: "select", + default: "claude", + meta: { visibleWhen: { key: "provider", value: "acpx" } }, + options: [ + { value: "claude", label: "Claude" }, + { value: "grok", label: "Grok Build" }, + ], + }); expect(JSON.stringify(res.body)).not.toContain("Codex via ACPX"); }); diff --git a/server/src/adapters/registry.test.ts b/server/src/adapters/registry.test.ts index 4f078588fc3..ddd655de7f5 100644 --- a/server/src/adapters/registry.test.ts +++ b/server/src/adapters/registry.test.ts @@ -4,8 +4,14 @@ import { listServerAdapters, requireServerAdapter } from "./registry.js"; import * as executionTarget from "@paperclipai/adapter-utils/execution-target"; import { BUILTIN_ADAPTER_TYPES } from "./builtin-adapter-types.js"; -const { probeInstallation } = vi.hoisted(() => ({ probeInstallation: vi.fn() })); -vi.mock("@paperclipai/paperclip-runner/live", () => ({ probeAcpxClaudeInstallation: probeInstallation })); +const { probeInstallation, probeGrokInstallation } = vi.hoisted(() => ({ + probeInstallation: vi.fn(), + probeGrokInstallation: vi.fn(), +})); +vi.mock("@paperclipai/paperclip-runner/live", () => ({ + probeAcpxClaudeInstallation: probeInstallation, + probeAcpxGrokInstallation: probeGrokInstallation, +})); // The registry registers a login capability for the two built-in interactive // adapters. The test checks the scalar values and the presence of the required @@ -86,7 +92,10 @@ describe("built-in runtime connection tool delivery", () => { describe("native ACPX environment checks", () => { - beforeEach(() => { probeInstallation.mockReset().mockResolvedValue(undefined); }); + beforeEach(() => { + probeInstallation.mockReset().mockResolvedValue(undefined); + probeGrokInstallation.mockReset().mockResolvedValue(undefined); + }); afterEach(() => vi.restoreAllMocks()); const context = { @@ -111,6 +120,17 @@ describe("native ACPX environment checks", () => { expect(probeInstallation).toHaveBeenCalledWith(context.config.model); }); + it.each([true, false])("checks Grok's own installation readiness (%s)", async (ready) => { + if (!ready) probeGrokInstallation.mockRejectedValueOnce(new Error("Grok executable digest mismatch")); + const result = await requireServerAdapter("paperclip_runner").testEnvironment!({ + ...context, + config: { provider: "acpx", acpxAgent: "grok", model: "grok-4.7" }, + }); + expect(result.status).toBe(ready ? "pass" : "fail"); + expect(probeGrokInstallation).toHaveBeenCalledWith("grok-4.7"); + expect(probeInstallation).not.toHaveBeenCalled(); + }); + it("does not use the host platform to reject a remote environment", async () => { vi.spyOn(process, "platform", "get").mockReturnValue("darwin"); const result = await requireServerAdapter("paperclip_runner").testEnvironment!({ diff --git a/server/src/adapters/registry.ts b/server/src/adapters/registry.ts index 6f70a9a7e06..236fd06410f 100644 --- a/server/src/adapters/registry.ts +++ b/server/src/adapters/registry.ts @@ -405,7 +405,7 @@ const paperclipRunnerAdapter: ServerAdapterModule = { } if (profile.provider === "acpx") { try { - if (profile.acpxAgent !== "claude") throw new Error("Select Codex to use the native Codex runner."); + if (profile.acpxAgent !== "claude" && profile.acpxAgent !== "grok") throw new Error("Select Codex to use the native Codex runner."); const target = context.executionTarget; if (target?.kind === "remote") { const probe = await runAdapterExecutionTargetShellCommand( @@ -414,8 +414,8 @@ const paperclipRunnerAdapter: ServerAdapterModule = { ); if (probe.timedOut || probe.exitCode !== 0) throw new Error("Could not verify the remote ACPX runner platform."); const [os, arch] = probe.stdout.trim().split(/\s+/); - if (!((os === "Linux" && arch === "x86_64") || (os === "Darwin" && ["arm64", "x86_64"].includes(arch ?? "")))) { - throw new Error("ACPX Claude requires Linux x64 or macOS ARM64/x64."); + if (!((os === "Linux" && arch === "x86_64") || (os === "Darwin" && (arch === "arm64" || (profile.acpxAgent !== "grok" && arch === "x86_64"))))) { + throw new Error(`ACPX ${profile.acpxAgent} requires a qualified Linux x64 or macOS architecture.`); } return { adapterType: "paperclip_runner", status: "warn" as const, testedAt: new Date().toISOString(), @@ -423,11 +423,11 @@ const paperclipRunnerAdapter: ServerAdapterModule = { message: "The remote platform is supported. Runtime package integrity and readiness must still be verified by the remote runner before launch." }], }; } - const { probeAcpxClaudeInstallation } = await import("@paperclipai/paperclip-runner/live"); - await probeAcpxClaudeInstallation(profile.model); + const { probeAcpxClaudeInstallation, probeAcpxGrokInstallation } = await import("@paperclipai/paperclip-runner/live"); + await (profile.acpxAgent === "grok" ? probeAcpxGrokInstallation : probeAcpxClaudeInstallation)(profile.model); return { adapterType: "paperclip_runner", status: "pass" as const, testedAt: new Date().toISOString(), - checks: [{ code: "acpx_runtime_ready", level: "info" as const, message: "ACPX Claude runtime is installed and verified. Model access is checked when Claude runs." }], + checks: [{ code: "acpx_runtime_ready", level: "info" as const, message: `ACPX ${profile.acpxAgent} runtime is installed and verified. Model access is checked when it runs.` }], }; } catch (error) { return { @@ -510,7 +510,7 @@ const paperclipRunnerAdapter: ServerAdapterModule = { ) : buildNpmRuntimeCommandSpec(config, "codex", "@openai/codex@0.156.0"), agentConfigurationDoc: - "# Paperclip Runner\n\nAdapter: paperclip_runner\n\nRuns Codex, OpenCode, Claude Managed, AWS AgentCore, or ACPX Claude through the Rust Paperclip runner and authenticated PRP transport. Pi is not available through the qualified ACPX profile. Managed providers use company-scoped qualified profiles, explicit retention acknowledgement, and spend limits.\n", + "# Paperclip Runner\n\nAdapter: paperclip_runner\n\nRuns Codex, OpenCode, Claude Managed, AWS AgentCore, or ACPX Claude/Grok Build through the Rust Paperclip runner and authenticated PRP transport. Pi is not available through the qualified ACPX profile. Managed providers use company-scoped qualified profiles, explicit retention acknowledgement, and spend limits.\n", getConfigSchema: () => ({ fields: [ { @@ -523,9 +523,14 @@ const paperclipRunnerAdapter: ServerAdapterModule = { { value: "opencode", label: `OpenCode ${QUALIFIED_OPENCODE_RUNNER_VERSION}` }, { value: "claude_managed", label: "Claude Managed" }, { value: "aws_agentcore", label: "AWS AgentCore" }, - { value: "acpx", label: "ACPX Claude" }, + { value: "acpx", label: "ACPX (Claude / Grok Build)" }, ], - hint: "Select a local provider, company-qualified managed provider, or ACPX Claude.", + hint: "Select a local provider, company-qualified managed provider, or Claude or Grok Build through ACPX.", + }, + { + key: "acpxAgent", label: "ACP agent", type: "select" as const, default: "claude", + options: [{ value: "claude", label: "Claude" }, { value: "grok", label: "Grok Build" }], + meta: { visibleWhen: { key: "provider", value: "acpx" } }, }, { key: "codexPermissionMode", diff --git a/server/src/services/native-runtime/grok-runner-credentials.test.ts b/server/src/services/native-runtime/grok-runner-credentials.test.ts new file mode 100644 index 00000000000..07cc32e46b5 --- /dev/null +++ b/server/src/services/native-runtime/grok-runner-credentials.test.ts @@ -0,0 +1,34 @@ +import { mkdtemp, mkdir, realpath, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { prepareGrokRunnerCredentials } from "./grok-runner-credentials.js"; + +const { companyHome } = vi.hoisted(() => ({ companyHome: vi.fn() })); +vi.mock("@paperclipai/adapter-grok-local/server", () => ({ + resolveManagedGrokHomeDir: companyHome, + grokHomeHasUsableAuth: async () => false, +})); +const roots: string[] = []; +async function home() { const root = await realpath(await mkdtemp(join(tmpdir(), "grok-controller-test-"))); roots.push(root); return root; } +afterEach(async () => { await Promise.all(roots.splice(0).map(path => rm(path, { recursive: true, force: true }))); vi.clearAllMocks(); }); + +describe("Grok runner company credentials", () => { + it("requires the company login for remote runs and never borrows a configured host home", async () => { + const root = await home(); + companyHome.mockReturnValue(join(root, "company-without-login")); + const host = join(root, "host"); await mkdir(host, { mode: 0o700 }); + await writeFile(join(host, "auth.json"), "host-secret", { mode: 0o600 }); + await expect(prepareGrokRunnerCredentials({ companyId: "test-company", remote: true, environment: { GROK_HOME: host, PAPERCLIP_HOME: host, PAPERCLIP_INSTANCE_ID: "other" } })).rejects.toThrow("subscription login is unavailable"); + expect(companyHome).toHaveBeenCalledWith(process.env, "test-company"); + }); + it("uses only an explicitly selected API key and strips subscription payloads and homes", async () => { + expect(await prepareGrokRunnerCredentials({ companyId: "test-company", remote: true, environment: { XAI_API_KEY: "selected", GROK_HOME: "/host", PAPERCLIP_ACPX_GROK_AUTH_JSON_SECRET: "stale" } })).toEqual({ environment: { XAI_API_KEY: "selected" }, home: null }); + }); + it("reads the controller-selected managed connection privately", async () => { + const root = await home(); companyHome.mockReturnValue(join(root, "unused")); + await writeFile(join(root, "auth.json"), '{"private":"fixture"}', { mode: 0o600 }); + const value = await prepareGrokRunnerCredentials({ companyId: "test-company", remote: true, managedHome: root, environment: { GROK_HOME: "/untrusted", XAI_API_KEY: "" } }); + expect(value).toEqual({ home: root, environment: { PAPERCLIP_ACPX_GROK_AUTH_JSON_SECRET: '{"private":"fixture"}' } }); + }); +}); diff --git a/server/src/services/native-runtime/grok-runner-credentials.ts b/server/src/services/native-runtime/grok-runner-credentials.ts new file mode 100644 index 00000000000..1d7a297818c --- /dev/null +++ b/server/src/services/native-runtime/grok-runner-credentials.ts @@ -0,0 +1,32 @@ +import { homedir } from "node:os"; +import { join } from "node:path"; +import { grokHomeHasUsableAuth, resolveManagedGrokHomeDir } from "@paperclipai/adapter-grok-local/server"; +import { readLocalAiCredentialFile } from "../local-ai-credential-file.js"; + +/** Resolve credentials on the controller. Remote runtimes receive only the + * selected credential, never an ambient host home or an implicit API key. + */ +export async function prepareGrokRunnerCredentials(input: { + companyId: string; + environment: NodeJS.ProcessEnv; + remote: boolean; + managedHome?: string; +}): Promise<{ environment: NodeJS.ProcessEnv; home: string | null }> { + const environment = { ...input.environment }; + delete environment.PAPERCLIP_ACPX_GROK_AUTH_JSON_SECRET; + const configuredHome = input.managedHome?.trim(); + delete environment.GROK_HOME; + if (environment.XAI_API_KEY?.trim()) return { environment, home: null }; + delete environment.XAI_API_KEY; + // Credential discovery belongs to the controller instance. Agent-supplied + // PAPERCLIP_HOME/INSTANCE_ID values must not redirect company login reads. + const companyHome = resolveManagedGrokHomeDir(process.env, input.companyId); + const home = configuredHome ?? (await grokHomeHasUsableAuth(companyHome) + ? companyHome : input.remote ? companyHome : join(homedir(), ".grok")); + try { + environment.PAPERCLIP_ACPX_GROK_AUTH_JSON_SECRET = await readLocalAiCredentialFile(join(home, "auth.json")); + } catch { + throw new Error("Grok subscription login is unavailable. Connect Grok Build or select an xAI API key."); + } + return { environment, home }; +} diff --git a/server/src/services/native-runtime/native-session-executor.test.ts b/server/src/services/native-runtime/native-session-executor.test.ts index 98f96aaebca..ddaaded398d 100644 --- a/server/src/services/native-runtime/native-session-executor.test.ts +++ b/server/src/services/native-runtime/native-session-executor.test.ts @@ -1,10 +1,12 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { access, + cp, lstat, mkdir, mkdtemp, readdir, + realpath, readFile, rename, rm, @@ -25,6 +27,7 @@ import { } from "@paperclipai/db"; import { acpxRuntimeSessionDirectoryName, + resolveAcpxRuntimeRoot, createPrpSemanticToolInputEnvelope, createPrpSemanticToolResultEnvelope, validatePrpStructuredRunResult, @@ -82,6 +85,7 @@ type BackendFactoryOptions = { }; type RunnerTransportOptions = { + acpxRuntimeDirectory?: string; adoptExistingRunner?: { pid: number; isAlive: () => Promise | boolean }; stateDirectory?: string; runnerBinary?: string; @@ -179,6 +183,12 @@ const state = vi.hoisted(() => ({ release: null as null | (() => void), })); +const grokCopyBack = vi.hoisted(() => vi.fn(async (_input: { readSandboxAuth: () => Promise; hostHomeDir: string }) => undefined)); +vi.mock("@paperclipai/adapter-grok-local/server", async importOriginal => ({ + ...await importOriginal(), + copyBackGrokAuth: grokCopyBack, +})); + vi.mock("../../vendor/paperclip-runner/index.js", async (importOriginal) => { const original = await importOriginal< typeof import("../../vendor/paperclip-runner/index.js") @@ -1031,6 +1041,9 @@ describe("remote provider pack manifest", () => { const lockfile = "lockfileVersion: '9.0'\n"; const opencodeCommand = "#!/bin/sh\n"; const opencodeExecutable = "opencode-binary\n"; + const grokExecutable = "grok-binary\n"; + await mkdir(join(root, "node_modules/@paperclipai/grok-acp/bin"), { recursive: true }); + await writeFile(join(root, "node_modules/@paperclipai/grok-acp/bin/grok"), grokExecutable); await writeFile( join(root, "dist", "cli", "opencode-app-server-proxy.cjs"), proxy, @@ -1061,18 +1074,21 @@ describe("remote provider pack manifest", () => { acpx: "0.13.1", claudeAcp: "0.73.0", codexAcp: "1.6.2", + grok: "1.0.13", }, target: { platform: "linux", architecture: "x64" }, runnerSourceRevision: "1".repeat(40), distDigest: sha256DirectoryTree(join(root, "dist")), bridgeDigest: "", acpxProfileDigests: { + grok: "sha256:f0b698395a3704ed2ffaf84ea19bdb20c36c8a0a70b7c629c7b6ffe144e59e55", claude: "sha256:9d73d1f0f121fb96cc8badb28c22d5bff02d8582eb2e40360a81c189e1b9422a", codex: "sha256:c4538599d1ab767db5dff50934f13bb5ba313a59d9c4a83e993fac4617ea63d3", }, artifacts: { + grokExecutable: { path: "node_modules/@paperclipai/grok-acp/bin/grok", sha256: digest(grokExecutable) }, nodeCommand: { path: "node_modules/node/bin/node", sha256: digest(node), @@ -1801,6 +1817,40 @@ describe("split durable provider checkpoint identity", () => { }); describe("remote provider checkpoint snapshots", () => { + it("excludes Grok credentials and diagnostic logs from real checkpoint copies while retaining sessions", async () => { + const root = await mkdtemp(join(tmpdir(), "grok-checkpoint-")); + try { + const source = join(root, "source"); + const target = join(root, "backup"); + const sessionDirectory = acpxRuntimeSessionDirectoryName("session"); + const relativeHome = join("acpx", sessionDirectory, "grok-home"); + const home = join(source, relativeHome); + await mkdir(join(home, "logs"), { recursive: true }); + await mkdir(join(home, "sessions")); + await writeFile(join(home, "logs", "unified.jsonl"), "AUTH_SENTINEL"); + await writeFile(join(home, "auth.json"), "AUTH_SENTINEL"); + await writeFile(join(home, "auth-refresh.json"), "AUTH_SENTINEL"); + await writeFile(join(home, "sessions", "session.json"), "resume-state"); + const profile = resolveNativeHarnessPersistenceProfile({ + provider: { kind: "acpx", agent: "grok" }, + session: { normalizedSessionId: "session", driverKind: "acpx_runtime" }, + } as unknown as NativeExecutionInputV1); + const execute = async ({ command, args }: { command: string; args: string[] }) => ({ + exitCode: 0, timedOut: false, stdout: execFileSync(command, args, { encoding: "utf8" }), stderr: "", + }); + const syncOut = async (operations: Array<{ files: Array<{ sourcePath: string; targetPath: string }> }>) => { + for (const operation of operations) for (const file of operation.files) + await cp(file.sourcePath, file.targetPath, { recursive: true }); + }; + await syncRemoteRunnerDirectoryOut({ runner: { execute, syncOut } as never, + sourcePath: source, targetPath: target, mode: 0o700, + excludeEntries: profile.directories.find(directory => directory.name === "acpx")!.excludeEntries }); + for (const entry of ["auth.json", "auth-refresh.json", "logs"]) + await expect(access(join(target, relativeHome, entry))).rejects.toMatchObject({ code: "ENOENT" }); + expect(await readFile(join(target, relativeHome, "sessions", "session.json"), "utf8")).toBe("resume-state"); + expect(await readFile(join(home, "logs", "unified.jsonl"), "utf8")).toBe("AUTH_SENTINEL"); + } finally { await rm(root, { recursive: true, force: true }); } + }); it("excludes Codex scratch and credential state without mutating the live provider home", async () => { const execute = vi .fn() @@ -5766,6 +5816,110 @@ describe("native session same-turn steering", () => { }); describe("native warm session supervision", () => { + describe("warm session identity transitions", () => { + let previousHome: string | undefined; + let isolatedHome: string; + beforeEach(async () => { + previousHome = process.env.PAPERCLIP_HOME; + isolatedHome = await mkdtemp(join(tmpdir(), "native-identity-transition-")); + process.env.PAPERCLIP_HOME = isolatedHome; + }); + afterEach(async () => { + await closeIdleWarmNativeSessionsForRestart(); + if (previousHome === undefined) delete process.env.PAPERCLIP_HOME; + else process.env.PAPERCLIP_HOME = previousHome; + await rm(isolatedHome, { recursive: true, force: true }); + }); + const result = { + result: { summary: "completed" }, terminal: { runTerminalState: "succeeded" }, + turnId: "turn", normalizedSessionId: "old", providerSessionId: "provider", + driverKind: "test", driverVersion: "1", nativeEventCount: 1, + highestContiguousSourceSeq: 1, usage: null, + }; + function fixture(name: string) { + const base = { + ...execution, + binding: { ...execution.binding, runId: `${name}-first`, executionWorkspaceId: name }, + session: { ...execution.session, normalizedSessionId: `${name}-old`, + lifecyclePolicy: { mode: "warm" as const, idleTimeoutMs: 60_000 } }, + } as NativeExecutionInputV1; + const next = { ...base, binding: { ...base.binding, runId: `${name}-second` }, + session: { ...base.session, normalizedSessionId: `${name}-new` } }; + const run = (input: NativeExecutionInputV1) => executePaperclipNativeSession({ + db: leaseDb(input), execution: input, runnerInstanceId: "runner", + }); + return { base, next, run }; + } + + it("awaits prior idle ownership retirement before launching the accepted-plan session", async () => { + const { base, next, run } = fixture("identity-handoff"); + let finishClose!: () => void; + const close = vi.fn(() => new Promise(resolve => { finishClose = resolve; })); + state.execute.mockReset().mockImplementationOnce(async options => { + options.onSession?.({ close }); return result; + }).mockImplementationOnce(async options => { + expect(close).toHaveBeenCalledOnce(); + expect(options.existingSession).toBeUndefined(); + return result; + }); + await run(base); + const replacement = run(next); + await vi.waitFor(() => expect(close).toHaveBeenCalledOnce()); + expect(state.execute).toHaveBeenCalledTimes(1); + await expect(run({ ...next, binding: { ...next.binding, runId: "racing-new-session" } })) + .rejects.toThrow("native_session_supervisor_busy"); + finishClose(); + await replacement; + expect(close).toHaveBeenCalledWith({ reason: "warm native session identity changed" }); + expect(state.execute).toHaveBeenCalledTimes(2); + }); + + it("does not retire an active turn when a new session identity arrives", async () => { + const { base, next, run } = fixture("identity-active"); + let finish!: () => void; + const close = vi.fn(async () => undefined); + state.execute.mockReset().mockImplementationOnce(async options => { + options.onSession?.({ close }); + await new Promise(resolve => { finish = resolve; }); + return result; + }); + const active = run(base); + await vi.waitFor(() => expect(finish).toBeTypeOf("function")); + await expect(run(next)).rejects.toThrow("native_session_supervisor_busy"); + expect(close).not.toHaveBeenCalled(); + finish(); await active; + await closeIdleWarmNativeSessionsForRestart(); + }); + + it("retains failed retirement for retry and never launches over an uncontained owner", async () => { + const { base, next, run } = fixture("identity-close-failure"); + const close = vi.fn().mockRejectedValueOnce(new Error("containment unavailable")).mockResolvedValue(undefined); + state.execute.mockReset().mockImplementationOnce(async options => { + options.onSession?.({ close }); return result; + }).mockResolvedValue(result); + await run(base); + await expect(run(next)).rejects.toThrow("containment unavailable"); + expect(state.execute).toHaveBeenCalledTimes(1); + await run(next); + expect(close).toHaveBeenCalledTimes(2); + expect(state.execute).toHaveBeenCalledTimes(2); + }); + + it.each(["companyId", "agentId", "issueId", "executionWorkspaceId"] as const)( + "does not retire an unrelated %s owner", async field => { + const { base, next, run } = fixture(`identity-isolation-${field}`); + const close = vi.fn(async () => undefined); + state.execute.mockReset().mockImplementationOnce(async options => { + options.onSession?.({ close }); return result; + }).mockResolvedValue(result); + await run(base); + await run({ ...next, binding: { ...next.binding, [field]: `different-${field}` } }); + expect(close).not.toHaveBeenCalled(); + await closeIdleWarmNativeSessionsForRestart(); + }, + ); + }); + it.each([true, false])( "uses provider turn completion without a semantic-result cutoff: chat=%s", async (conversationMode) => { @@ -7694,6 +7848,49 @@ describe("runnerd provider runtime wiring", () => { await expect(readFile(authPath, "utf8")).resolves.toBe(auth); }); + it("copies back and removes Grok credentials from the exact ACPX launch home", async () => { + const priorHome = process.env.PAPERCLIP_HOME; + const privateRoot = await realpath(isolatedStateDirectory); + process.env.PAPERCLIP_HOME = privateRoot; + const managedHome = join(privateRoot, "company-login"); + await mkdir(managedHome, { mode: 0o700 }); + await writeFile(join(managedHome, "auth.json"), "fixture-old-login", { mode: 0o600 }); + const grokExecution = { ...execution, + provider: { kind: "acpx", agent: "grok", model: "grok-4.7", permissionMode: "deny-all" }, + session: { ...execution.session, driverKind: "acpx_runtime" }, + } as unknown as NativeExecutionInputV1; + state.createBackend.mockReturnValueOnce({ + kind: "test", openSession: async () => ({ close: vi.fn(async () => undefined) }), + } as never); + try { + const backend = await createRunnerdBackend({ db: leaseDb(grokExecution), execution: grokExecution, + runnerInstanceId: "grok-cleanup", managedAiCredentialHome: managedHome }); + state.createBackend.mock.calls.at(-1)![1].codexTransportFactory!(); + const runtimeDirectory = state.createTransport.mock.calls.at(-1)![0].acpxRuntimeDirectory!; + await mkdir(runtimeDirectory, { recursive: true }); + // Use the runtime's real resolver, so this test cannot reproduce a + // controller-side guess that omits the nested ACPX namespace. + const agentHome = join(await resolveAcpxRuntimeRoot(runtimeDirectory, execution.session.normalizedSessionId!), "grok-home"); + await mkdir(agentHome, { recursive: true, mode: 0o700 }); + for (const name of ["auth.json", "auth-refresh.json", "auth-refresh.json.tmp"]) { + await writeFile(join(agentHome, name), "fixture-refreshed-login", { mode: 0o600 }); + } + grokCopyBack.mockReset().mockImplementationOnce(async input => { + expect(input.hostHomeDir).toBe(managedHome); + expect((await input.readSandboxAuth()).toString()).toBe("fixture-refreshed-login"); + }); + const session = await backend.openSession({} as never); + await session.close({ reason: "completed" }); + expect(grokCopyBack).toHaveBeenCalledOnce(); + for (const name of ["auth.json", "auth-refresh.json", "auth-refresh.json.tmp"]) { + await expect(access(join(agentHome, name))).rejects.toMatchObject({ code: "ENOENT" }); + } + } finally { + if (priorHome === undefined) delete process.env.PAPERCLIP_HOME; + else process.env.PAPERCLIP_HOME = priorHome; + } + }); + it("still cleans up managed Codex credentials after an owned session closes", async () => { const close = vi.fn(async () => undefined); state.copyBackCodexAuth.mockClear(); @@ -8377,7 +8574,7 @@ describe("runnerd provider runtime wiring", () => { } }); - it("migrates a suspended prior-run authority only when its persisted execution has the same full session scope", async () => { + it.each([0, 2048])("migrates a suspended prior-run authority in the same full session scope with %i retained events", async (eventCount) => { const stateBase = await mkdtemp( join(tmpdir(), "paperclip-prior-run-session-state-"), ); @@ -8435,16 +8632,45 @@ describe("runnerd provider runtime wiring", () => { try { await mkdir(join(legacyRoot, "control-plane"), { recursive: true }); await mkdir(join(legacyRoot, "runner"), { recursive: true }); + const identity = { + runId: priorExecution.binding.runId, + normalizedSessionId: priorExecution.session.normalizedSessionId, + runnerInstanceId: "runner-prior-run-scope", + environmentLeaseId: "lease-prior-run-scope", + }; + const controlPlaneBytes = JSON.stringify({ + ...durableControlPlaneState(identity), + // The identity shares a file with the retained PRP event window. A + // verbose valid turn can exceed the old 2 MiB identity-read limit. + committedEvents: Array.from({ length: eventCount }, (_, index) => ({ + sourceSeq: index + 1, + sourceEventId: `event-${index + 1}`, + eventType: "item.delta", + priority: 1, + envelope: { + schema: "paperclip.prp.event.v1", + schemaVersion: 1, + sourceKind: "runner", + sourceInstanceId: identity.runnerInstanceId, + sourceEventId: `event-${index + 1}`, + sourceSeq: index + 1, + normalizedSessionId: identity.normalizedSessionId, + runId: identity.runId, + turnId: "turn-prior-run-scope", + itemId: "item-prior-run-scope", + eventType: "item.delta", + priority: 1, + emittedAt: "2026-09-24T00:00:00.000Z", + payload: { delta: "x".repeat(1024) }, + }, + deliveryCount: 1, + logicalEffectCount: 1, + })), + }); + if (eventCount > 0) expect(Buffer.byteLength(controlPlaneBytes)).toBeGreaterThan(2 * 1024 * 1024); await writeFile( join(legacyRoot, "control-plane", "control-plane-state.json"), - JSON.stringify( - durableControlPlaneState({ - runId: priorExecution.binding.runId, - normalizedSessionId: priorExecution.session.normalizedSessionId, - runnerInstanceId: "runner-prior-run-scope", - environmentLeaseId: "lease-prior-run-scope", - }), - ), + controlPlaneBytes, ); await writeFile( join(legacyRoot, "runner", "runner-state.json"), @@ -8473,6 +8699,17 @@ describe("runnerd provider runtime wiring", () => { state.createTransport.mock.calls[0]![0].stateDirectory!; expect(migratedRoot).not.toBe(legacyRoot); await expect(access(legacyRoot)).rejects.toThrow(); + // Re-enter through the canonical scoped root as ordinary continuation + // does, not only through the legacy migration path above. + state.createBackend.mockClear(); + state.createTransport.mockClear(); + await createRunnerdBackend({ + db: priorRunDb, + execution: currentExecution, + runnerInstanceId: "runner-current-run-scope", + }); + state.createBackend.mock.calls[0]![1].codexTransportFactory!(); + expect(state.createTransport.mock.calls[0]![0].stateDirectory).toBe(migratedRoot); expect(state.createTransport.mock.calls[0]![0].prpIdentity).toEqual( expect.objectContaining({ runId: currentExecution.binding.runId, @@ -8899,6 +9136,7 @@ describe("runnerd provider runtime wiring", () => { it.each([ "quarantined", + "large control plane", "empty retry shell", "unsuspended current", "live runner", @@ -9097,6 +9335,12 @@ describe("runnerd provider runtime wiring", () => { }, ], committedEvents: [ + ...(scenario === "large control plane" + ? Array.from({ length: 2048 }, () => ({ + eventType: "item.delta", + envelope: { ...identity, payload: { delta: "x".repeat(8192) } }, + })) + : []), { eventType: "run.terminal", envelope: identity }, ], }), @@ -9136,6 +9380,7 @@ describe("runnerd provider runtime wiring", () => { state.createTransport.mockClear(); const shouldRecover = [ "quarantined", + "large control plane", "empty retry shell", "unsuspended current", "active goal", @@ -9643,7 +9888,7 @@ describe("runnerd provider runtime wiring", () => { }, ); - it.each(["missing", "malformed", "unknown_schema", "mismatched"] as const)( + it.each(["missing", "malformed", "unknown_schema", "mismatched", "oversized"] as const)( "fails closed on %s durable identity in an existing scoped root", async (caseName) => { const stateBase = await mkdtemp( @@ -9704,6 +9949,25 @@ describe("runnerd provider runtime wiring", () => { ), ); } + if (caseName === "oversized") { + const identity = { + runId: scopedExecution.binding.runId, + normalizedSessionId: scopedExecution.session.normalizedSessionId, + runnerInstanceId: `runner-${caseName}-scoped-state`, + environmentLeaseId: scopedExecution.binding.executionWorkspaceId, + }; + // Valid JSON and valid ready authority: only the byte bound rejects + // this file. Malformed sparse padding would not test that boundary. + await writeFile( + join(scopedRoot, "control-plane", "control-plane-state.json"), + JSON.stringify(durableControlPlaneState(identity)).padEnd(64 * 1024 * 1024 + 1, " "), + ); + await mkdir(join(scopedRoot, "runner"), { recursive: true }); + await writeFile( + join(scopedRoot, "runner", "runner-state.json"), + JSON.stringify(durableRunnerState(identity, "ready")), + ); + } state.createBackend.mockClear(); state.createTransport.mockClear(); diff --git a/server/src/services/native-runtime/native-session-executor.ts b/server/src/services/native-runtime/native-session-executor.ts index e446642e7ed..69474366ff0 100644 --- a/server/src/services/native-runtime/native-session-executor.ts +++ b/server/src/services/native-runtime/native-session-executor.ts @@ -1,3 +1,7 @@ +import { readLocalAiCredentialFile } from "../local-ai-credential-file.js"; +import { prepareGrokRunnerCredentials } from "./grok-runner-credentials.js"; +import { copyBackGrokAuth } from "@paperclipai/adapter-grok-local/server"; + import { isSupportedRemoteCodexVersion, parseCodexCliVersion, @@ -276,7 +280,10 @@ export async function detachNativeSessionsForRestart( const MAX_REMOTE_CHECKPOINT_ARCHIVE_BYTES = 64 * 1024 * 1024; const MAX_REMOTE_CHECKPOINT_EXPANDED_BYTES = 64 * 1024 * 1024; const MAX_REMOTE_CHECKPOINT_ENTRIES = 20_000; -const NATIVE_DURABLE_IDENTITY_MAX_BYTES = 2 * 1024 * 1024; +// Identity is read from the complete control-plane state, including its PRP +// event window. Match the transport's bounded reader so verbose valid turns do +// not lose continuation authority merely because their history exceeds 2 MiB. +const NATIVE_CONTROL_PLANE_STATE_MAX_BYTES = 64 * 1024 * 1024; const NATIVE_RUNNER_STATE_MAX_BYTES = 16 * 1024 * 1024; const NATIVE_WARM_CHECKPOINT_MAX_BYTES = 8 * 1024 * 1024; const CODEX_HOME_NON_PERSISTENT_ENTRIES = [ @@ -391,6 +398,7 @@ type WarmNativeSession = { session: NativeSession; ownerToken: symbol; configDigest: string; + ownerScope: string; companyId: string; environmentId: string | null; busy: boolean; @@ -1448,6 +1456,7 @@ class SessionToolAuthorityEpoch { const sessionToolAuthorityEpochs = new Map(); const initializingSessionToolAuthorities = new Set(); const executingRunnerdSessionScopes = new Map(); +const executingNativeOwnerScopes = new Map(); function nativeSessionKey(execution: NativeExecutionInput): string { return ( @@ -1519,6 +1528,45 @@ function nativeSessionScopeKey(execution: NativeExecutionInput): string { }); } +// A plan acceptance or explicit reset can rotate the provider/session identity +// while retaining the same task sandbox and its fixed ingress port. +function nativeSessionOwnerScope( + execution: NativeExecutionInput, + environmentId: string | null, +): string { + return canonicalJson({ + companyId: execution.binding.companyId, + agentId: execution.binding.agentId, + issueId: execution.binding.issueId, + workspace: nativeSessionWorkspaceScope(execution), + environmentId, + }); +} + +async function retireSupersededWarmNativeSessions( + ownerScope: string, + nextSessionScope: string, +): Promise { + for (const [scope, entry] of warmNativeSessions) { + if (scope === nextSessionScope || entry.ownerScope !== ownerScope) continue; + if (entry.busy || executingRunnerdSessionScopes.has(scope)) { + throw new Error("native_session_supervisor_busy"); + } + if (entry.idleTimer !== null) clearTimeout(entry.idleTimer); + entry.idleTimer = null; + entry.busy = true; + entry.closeOnReleaseReason = "warm native session identity changed"; + try { + await closeWarmNativeSession(entry, entry.closeOnReleaseReason); + if (warmNativeSessions.get(scope) === entry) warmNativeSessions.delete(scope); + } finally { + // A failed close retains the owner and prevents launch. A later attempt + // must retry retirement, never adopt this partially closed transport. + entry.busy = false; + } + } +} + function legacyCompanyNativeSessionScopeKey( execution: NativeExecutionInput, ): string { @@ -1787,7 +1835,9 @@ function cleanupStateSnapshot(root: string, providerFile = "codex-provider-state const bytes = files.map((file) => readBoundedNativeFile( resolve(root, file), - NATIVE_RUNNER_STATE_MAX_BYTES, + file === "control-plane/control-plane-state.json" + ? NATIVE_CONTROL_PLANE_STATE_MAX_BYTES + : NATIVE_RUNNER_STATE_MAX_BYTES, "native_cleanup_maintenance_unproven", ), ); @@ -4060,7 +4110,7 @@ function hasRetainedWarmTransitionEvidence(root: string): boolean { [ "control-plane", "control-plane-state.json", - NATIVE_DURABLE_IDENTITY_MAX_BYTES, + NATIVE_CONTROL_PLANE_STATE_MAX_BYTES, ], ["runner", "runner-state.json", NATIVE_RUNNER_STATE_MAX_BYTES], ] as const) { @@ -4117,7 +4167,7 @@ function readWarmTransitionSnapshot(root: string) { } const core = readBoundedNativeFile( resolve(root, "control-plane", "control-plane-state.json"), - NATIVE_DURABLE_IDENTITY_MAX_BYTES, + NATIVE_CONTROL_PLANE_STATE_MAX_BYTES, "native_runner_warm_transition_recovery_unproven", ); const runner = readBoundedNativeFile( @@ -4718,7 +4768,9 @@ async function recoverQuiescentRunnerdState(input: { throw new Error("unsafe_recovery_state"); return readBoundedNativeFile( resolve(root, directory, name), - NATIVE_RUNNER_STATE_MAX_BYTES, + directory === "control-plane" && name === "control-plane-state.json" + ? NATIVE_CONTROL_PLANE_STATE_MAX_BYTES + : NATIVE_RUNNER_STATE_MAX_BYTES, "recovery_state_too_large", ).toString("utf8"); }; @@ -4887,15 +4939,15 @@ async function recoverQuiescentRunnerdState(input: { const candidate = verified[0]!; // Candidate enumeration can await other database reads. Revalidate the // selected evidence and dead owner immediately before the atomic moves. - for (const [relativePath, expected] of [ - ["runner/runner-state.json", candidate.runnerBytes], - ["runner/codex-provider-state.json", candidate.providerBytes], - ["control-plane/control-plane-state.json", candidate.controlBytes], - ]) { + for (const [relativePath, expected, maxBytes] of [ + ["runner/runner-state.json", candidate.runnerBytes, NATIVE_RUNNER_STATE_MAX_BYTES], + ["runner/codex-provider-state.json", candidate.providerBytes, NATIVE_RUNNER_STATE_MAX_BYTES], + ["control-plane/control-plane-state.json", candidate.controlBytes, NATIVE_CONTROL_PLANE_STATE_MAX_BYTES], + ] as const) { if ( readBoundedNativeFile( - resolve(candidate.root, relativePath!), - NATIVE_RUNNER_STATE_MAX_BYTES, + resolve(candidate.root, relativePath), + maxBytes, "recovery_state_too_large", ).toString("utf8") !== expected ) { @@ -4958,7 +5010,7 @@ export function runnerdStateProvesIncompleteBootstrap(root: string): boolean { JSON.parse( readBoundedNativeFile( statePath, - NATIVE_DURABLE_IDENTITY_MAX_BYTES, + NATIVE_CONTROL_PLANE_STATE_MAX_BYTES, "runner_durable_identity_too_large", ).toString("utf8"), ), @@ -5011,7 +5063,7 @@ function readRunnerdDurableIdentity( JSON.parse( readBoundedNativeFile( statePath, - NATIVE_DURABLE_IDENTITY_MAX_BYTES, + NATIVE_CONTROL_PLANE_STATE_MAX_BYTES, "runner_durable_identity_too_large", ).toString("utf8"), ), @@ -5257,9 +5309,13 @@ export function resolveNativeHarnessPersistenceProfile( // Codex creates process-local executable aliases in tmp/arg0; // they may point outside the runtime tree and are neither safe // nor necessary to restore. Credentials and launch-time config - // are also re-materialized in the replacement sandbox. + // are also re-materialized in the replacement sandbox. Grok + // diagnostics can contain auth fields and are not session state. excludeEntries: - execution.provider.agent === "codex" + execution.provider.agent === "grok" + ? ["auth.json", "auth-refresh.json", "auth-refresh.json.tmp", "config.toml", "logs"].map((entry) => + `acpx/${acpxRuntimeSessionDirectoryName(nativeSessionKey(execution))}/grok-home/${entry}`) + : execution.provider.agent === "codex" ? CODEX_HOME_NON_PERSISTENT_ENTRIES.map( (entry) => `acpx/${acpxRuntimeSessionDirectoryName(nativeSessionKey(execution))}/codex-home/${entry}`, @@ -7164,7 +7220,20 @@ export async function executePaperclipNativeSession(input: { let sessionScopeId: string | null = null; let ownsSessionScope = false; let executionFailure: unknown; + const ownerScope = nativeSessionOwnerScope( + input.execution, + input.runnerExecutionTarget?.environmentId ?? null, + ); + const ownerToken = Symbol("native execution owner"); try { + // Reserve across session identities before retiring or staging anything. + if (executingNativeOwnerScopes.has(ownerScope)) { + throw new Error("native_session_supervisor_busy"); + } + executingNativeOwnerScopes.set(ownerScope, ownerToken); + await retireSupersededWarmNativeSessions( + ownerScope, nativeSessionScopeKey(input.execution), + ); if (!input.useRunnerd) { return await executePaperclipNativeSessionWithinScope(input); } @@ -7234,6 +7303,9 @@ export async function executePaperclipNativeSession(input: { executionFailure = error; throw error; } finally { + if (executingNativeOwnerScopes.get(ownerScope) === ownerToken) { + executingNativeOwnerScopes.delete(ownerScope); + } startup.resolve(null); if (nativeSessionStartups.get(runId) === startup) { nativeSessionStartups.delete(runId); @@ -7972,6 +8044,7 @@ async function executePaperclipNativeSessionWithinScope( (hasBrokerCapability && entry.credentialRunId !== input.execution.binding.runId); if ( + entry.closeOnReleaseReason !== undefined || entry.configDigest !== warmConfigDigest || entry.managedAiCredentialIdentity !== input.managedAiCredentialIdentity || credentialRunChanged || @@ -8336,6 +8409,9 @@ async function executePaperclipNativeSessionWithinScope( session, ownerToken: warmSessionOwnerToken, configDigest: warmConfigDigest, + ownerScope: nativeSessionOwnerScope( + input.execution, input.runnerExecutionTarget?.environmentId ?? null, + ), companyId: input.execution.binding.companyId, environmentId: input.runnerExecutionTarget?.environmentId ?? null, @@ -9189,14 +9265,17 @@ const REMOTE_PROVIDER_PACK_PINS = { acpx: "0.13.1", claudeAcp: "0.73.0", codexAcp: "1.6.2", + grok: "1.0.13", } as const; const REMOTE_PROVIDER_PACK_PROFILE_DIGESTS = { + grok: "sha256:f0b698395a3704ed2ffaf84ea19bdb20c36c8a0a70b7c629c7b6ffe144e59e55", claude: "sha256:9d73d1f0f121fb96cc8badb28c22d5bff02d8582eb2e40360a81c189e1b9422a", codex: "sha256:c4538599d1ab767db5dff50934f13bb5ba313a59d9c4a83e993fac4617ea63d3", } as const; const REMOTE_PROVIDER_PACK_ARTIFACT_PATHS = { + grokExecutable: "node_modules/@paperclipai/grok-acp/bin/grok", nodeCommand: "node_modules/node/bin/node", productionLock: "pnpm-lock.yaml", opencodeCommand: "node_modules/.bin/opencode", @@ -9216,6 +9295,7 @@ type RemoteProviderPackManifest = { bridgeDigest: string; acpxProfileDigests: typeof REMOTE_PROVIDER_PACK_PROFILE_DIGESTS; artifacts: { + grokExecutable: { path: string; sha256: string }; nodeCommand: { path: string; sha256: string }; productionLock: { path: string; sha256: string }; opencodeCommand: { path: string; sha256: string }; @@ -9314,6 +9394,7 @@ export function readRemoteProviderPackManifest( ); } const artifactEntries = [ + ["Grok executable", payload.artifacts?.grokExecutable, REMOTE_PROVIDER_PACK_ARTIFACT_PATHS.grokExecutable], [ "provider Node", payload.artifacts?.nodeCommand, @@ -10773,14 +10854,14 @@ async function createRunnerdBackendWithinSessionClaim( "if(canonical(manifest)!==expected)throw new Error('manifest mismatch')", "const hash=(p)=>'sha256:'+crypto.createHash('sha256').update(fs.readFileSync(path.join(root,p))).digest('hex')", "const tree=(treeRoot)=>{const digest=crypto.createHash('sha256');const visit=(directory,prefix='')=>{for(const entry of fs.readdirSync(directory,{withFileTypes:true}).sort((a,b)=>a.name.localeCompare(b.name))){const relative=prefix?prefix+'/'+entry.name:entry.name;const absolute=path.join(directory,entry.name);if(entry.isDirectory()){digest.update('directory\\0'+relative+'\\n');visit(absolute,relative)}else if(entry.isFile()){digest.update('file\\0'+relative+'\\0'+'sha256:'+crypto.createHash('sha256').update(fs.readFileSync(absolute)).digest('hex')+'\\n')}else if(entry.isSymbolicLink()){digest.update('symlink\\0'+relative+'\\0'+fs.readlinkSync(absolute)+'\\n')}else throw new Error('unsupported dist entry '+relative)}};visit(treeRoot);return 'sha256:'+digest.digest('hex')}", - "for(const name of ['nodeCommand','productionLock','opencodeCommand','opencodeExecutable','opencodeProxy','acpxSidecar']){const artifact=manifest.payload.artifacts[name];if(hash(artifact.path)!==artifact.sha256)throw new Error(name+' digest mismatch')}", + "for(const name of ['nodeCommand','productionLock','opencodeCommand','opencodeExecutable','opencodeProxy','acpxSidecar','grokExecutable']){const artifact=manifest.payload.artifacts[name];if(hash(artifact.path)!==artifact.sha256)throw new Error(name+' digest mismatch')}", "if(tree(path.join(root,'dist'))!==manifest.payload.distDigest)throw new Error('dist tree digest mismatch')", "const version=process.versions.node.split('.').map(Number)", "const minimum=manifest.payload.pins.nodeMinimum.split('.').map(Number)", "if(version[0]JSON.parse(fs.readFileSync(path.join(root,'node_modules',...pkg.split('/'),'package.json'),'utf8')).version", - "const expectedPackages={acpx:manifest.payload.pins.acpx,'@agentclientprotocol/claude-agent-acp':manifest.payload.pins.claudeAcp,'@agentclientprotocol/codex-acp':manifest.payload.pins.codexAcp,'opencode-ai':manifest.payload.pins.opencode}", + "const expectedPackages={acpx:manifest.payload.pins.acpx,'@agentclientprotocol/claude-agent-acp':manifest.payload.pins.claudeAcp,'@agentclientprotocol/codex-acp':manifest.payload.pins.codexAcp,'opencode-ai':manifest.payload.pins.opencode,'@paperclipai/grok-acp':manifest.payload.pins.grok}", "for(const [pkg,version] of Object.entries(expectedPackages))if(packageVersion(pkg)!==version)throw new Error(pkg+' version mismatch')", ].join(";"); const verified = await remoteCommandRunner.execute({ @@ -12071,9 +12152,15 @@ async function createRunnerdBackendWithinSessionClaim( }, } : input.execution; - const effectiveRunnerEnvironmentBase: NodeJS.ProcessEnv = { - ...(input.runnerEnvironment ?? process.env), + const isGrok = input.execution.provider.kind === "acpx" && input.execution.provider.agent === "grok"; + let effectiveRunnerEnvironmentBase: NodeJS.ProcessEnv = { + ...(input.runnerEnvironment ?? (isGrok ? {} : process.env)), }; + const grokCredential = isGrok ? await prepareGrokRunnerCredentials({ + companyId: input.execution.binding.companyId, environment: effectiveRunnerEnvironmentBase, remote: Boolean(remoteTarget), + managedHome: input.managedAiCredentialHome, + }) : null; + if (grokCredential) effectiveRunnerEnvironmentBase = grokCredential.environment; if (relayAssignedMcp) { // The server-held tool authority owns this credential. Do not deliver a // duplicate HTTP MCP server or its bearer token to the remote provider. @@ -12650,6 +12737,40 @@ async function createRunnerdBackendWithinSessionClaim( }); const boundManagedSessions = new WeakSet(); const wrapManagedSession = (session: NativeSession): NativeSession => { + if (isGrok && grokCredential?.home && !boundManagedSessions.has(session)) { + boundManagedSessions.add(session); + // The launch runtime directory already ends in "acpx"; ACPX adds its + // own namespace beneath it in resolveAcpxRuntimeRoot. + const relativeHome = `acpx/acpx/${acpxRuntimeSessionDirectoryName(nativeSessionKey(input.execution))}/grok-home`; + const localHome = resolve(resolvePaperclipInstanceRoot(), "runtime", "paperclip-runner", relativeHome); + const remoteHome = remoteRunnerFilesystemRoot ? posix.join(remoteRunnerFilesystemRoot, relativeHome) : null; + const readAuth = async (name: string): Promise => { + if (!remoteHome || !remoteCommandRunner) return Buffer.from(await readLocalAiCredentialFile(join(localHome, name))); + const script = `const fs=require('node:fs'),path=require('node:path');let fd;try{const file=process.argv[1];let parent=path.dirname(file);while(true){if(!fs.lstatSync(parent).isDirectory())throw Error('directory');const next=path.dirname(parent);if(next===parent)break;parent=next;}fd=fs.openSync(file,fs.constants.O_RDONLY|fs.constants.O_NOFOLLOW);const st=fs.fstatSync(fd);if(!st.isFile()||st.uid!==process.getuid()||(st.mode&511)!==384||st.size>65536)throw Error('credential');const b=Buffer.alloc(65537);let n=0;while(n65536)throw Error('size');process.stdout.write(b.subarray(0,n).toString('base64'));b.fill(0);}catch(e){process.exitCode=e.code==='ENOENT'?66:1;}finally{if(fd!==undefined)fs.closeSync(fd);}`; + const result = await remoteCommandRunner.execute({ command: "node", args: ["-e", script, posix.join(remoteHome, name)], bypassSession: true, timeoutMs: 10000 }); + if (result.exitCode !== 0 || result.timedOut) throw Object.assign(new Error("Grok credential refresh handoff unavailable"), { code: result.exitCode === 66 ? "ENOENT" : "INVALID_CREDENTIAL" }); + return Buffer.from(result.stdout, "base64"); + }; + return bindManagedNativeCredentialTurn(session, { + copyBack: async () => { + await copyBackGrokAuth({ hostHomeDir: grokCredential.home!, log: () => {}, + readSandboxAuth: () => readAuth("auth.json").catch((error) => { + if (error.code !== "ENOENT") throw error; + return readAuth("auth-refresh.json"); + }), + }); + }, + remove: async () => { + for (const name of ["auth.json", "auth-refresh.json", "auth-refresh.json.tmp"]) { + rmSync(join(localHome, name), { force: true }); + if (remoteHome && remoteCommandRunner) { + const result = await remoteCommandRunner.execute({ command: "rm", args: ["-f", "--", posix.join(remoteHome, name)], bypassSession: true, timeoutMs: 10000 }); + if (result.exitCode !== 0 || result.timedOut) throw new Error("Grok credential cleanup failed"); + } + } + }, + }); + } if (!input.managedAiCredentialHome || input.execution.provider.kind !== "codex" || boundManagedSessions.has(session)) return session; boundManagedSessions.add(session); const remoteAuth = remoteRunnerFilesystemRoot ? posix.join(remoteRunnerFilesystemRoot, "codex-home", "auth.json") : null; diff --git a/server/src/services/native-runtime/provider-profile.ts b/server/src/services/native-runtime/provider-profile.ts index 3cd80f49f19..a5fbaa1867b 100644 --- a/server/src/services/native-runtime/provider-profile.ts +++ b/server/src/services/native-runtime/provider-profile.ts @@ -15,6 +15,7 @@ export const DEFAULT_OPENCODE_RUNNER_MODEL = export const CLAUDE_MANAGED_BETA_VERSION = "managed-agents-2026-04-01" as const; export const QUALIFIED_ACPX_RUNNER_MODELS = { + grok: "grok-4.7", claude: "claude-sonnet-5", codex: "gpt-5.6-sol", } as const; @@ -404,7 +405,7 @@ export function resolvePaperclipRunnerProviderProfile( } const acpxAgent = config.acpxAgent ?? "claude"; - if (acpxAgent !== "claude" && acpxAgent !== "codex") { + if (acpxAgent !== "claude" && acpxAgent !== "codex" && acpxAgent !== "grok") { throw new PaperclipRunnerProviderProfileError( "paperclip_runner_acpx_agent_unavailable", "Paperclip Runner ACPX requires the qualified Claude or Codex agent profile; Pi is not available.", diff --git a/tests/e2e/chat-adapters-ui-providers.spec.ts b/tests/e2e/chat-adapters-ui-providers.spec.ts index 84531559990..017c3d10c28 100644 --- a/tests/e2e/chat-adapters-ui-providers.spec.ts +++ b/tests/e2e/chat-adapters-ui-providers.spec.ts @@ -75,7 +75,7 @@ async function exerciseGitHubReviewSetup(page: Page, mock: ChatMock, seed: Seed, await page.getByText("Connection health and controls", { exact: true }).click(); await page.getByRole("button", { name: "Pause", exact: true }).click(); await page.getByRole("button", { name: "Resume", exact: true }).click(); - expect(mock.lifecycleActions).toEqual(["pause", "resume"]); + await expect.poll(() => mock.lifecycleActions).toEqual(["pause", "resume"]); await page.getByRole("button", { name: "Replay failed delivery", exact: true }).click(); await expect.poll(() => mock.replayedDelivery).toBe(true); mock.setStatus("attention"); @@ -651,7 +651,7 @@ test.describe.serial("native chat adapter UI", () => { await expect( page.getByRole("button", { name: "Pause", exact: true }), ).toBeVisible(); - expect(mock.lifecycleActions).toEqual(["pause", "resume"]); + await expect.poll(() => mock.lifecycleActions).toEqual(["pause", "resume"]); mock.setStatus("attention"); await page.reload(); @@ -776,7 +776,7 @@ test.describe.serial("native chat adapter UI", () => { .click(); await expect(page).toHaveURL(new RegExp(`/${seed.prefix}/apps$`)); await expect.poll(() => mock.removed).toBe(true); - expect(mock.lifecycleActions).toEqual(["pause", "resume", "remove"]); + await expect.poll(() => mock.lifecycleActions).toEqual(["pause", "resume", "remove"]); }); } }); diff --git a/tests/runner-e2e/FIXTURES.md b/tests/runner-e2e/FIXTURES.md index 885b3e724cf..1b1041bd19e 100644 --- a/tests/runner-e2e/FIXTURES.md +++ b/tests/runner-e2e/FIXTURES.md @@ -46,6 +46,14 @@ Agent `adapterConfig.env` values must be `{type:"secret_ref", secretId, version:"latest"}` objects supplied to the factory. A fixture source containing a raw secret-looking value is rejected by catalog validation. +The manual Grok subscription profile uses `GROK_AUTH_JSON` as an explicit login +fixture. It does not put this credential in agent configuration or substitute an +API key. Setup seeds a new company-scoped Grok home inside the disposable instance +with mode 0700 and an exclusive mode-0600 auth file. Setup rejects redirected, +occupied, or nonisolated homes. Production runner discovery and refresh operate on +that company login; teardown destroys it after the remote environment is removed. +This fixture tests subscription execution, not the interactive browser login flow. + ## Environments An `EnvironmentFixture` declares driver/provider, credential requirements, diff --git a/tests/runner-e2e/README.md b/tests/runner-e2e/README.md index 04dfe551e25..d72ad1053ea 100644 --- a/tests/runner-e2e/README.md +++ b/tests/runner-e2e/README.md @@ -1079,6 +1079,22 @@ These fault-injection tests prove recovery from interrupted startup. They do not establish the cause of the historical intermittent Vite module-graph stall; ordinary 304 responses and successful reruns alone are not evidence of that cause. +### Grok Build qualification + +`runner-acpx-grok` uses native ACPX with Grok Build 1.0.13 and `grok-4.7`. +Set an explicit `XAI_API_KEY` for this API-key profile. The manual +`grok-qualification` suite runs the core and restart workflows on local and +Daytona runtimes. The separate manual `grok-subscription-qualification` suite +uses `runner-acpx-grok-subscription` and explicit `GROK_AUTH_JSON`. Its fixture +stages that login in a private, disposable company home; this is credential +setup, not a test of interactive browser login. The server never inherits the +JSON secret or an API key from the harness. See [FIXTURES.md](FIXTURES.md) and +[SECURITY.md](SECURITY.md) for staging, cleanup, and redaction requirements. +Missing subscription coverage remains a qualification blocker; +an API-key pass must not be reported as subscription evidence. See +[`doc/grok-native-runner.md`](../../doc/grok-native-runner.md) for installation, +credential boundaries and repetition requirements. + ### Grok branch qualification on EC2 The trusted default-branch workflow can run the explicit `grok-qualification` diff --git a/tests/runner-e2e/SECURITY.md b/tests/runner-e2e/SECURITY.md index 2653cec086f..08cdbe90b0e 100644 --- a/tests/runner-e2e/SECURITY.md +++ b/tests/runner-e2e/SECURITY.md @@ -63,6 +63,14 @@ rejects mutable tag or branch references. ## Secrets and protected environments +Explicit Grok subscription campaigns may select `GROK_AUTH_JSON` from the same +protected paid environment. Deliver it only to that subscription profile's paid +test step. Prefer a dedicated test-account login and refresh it before a campaign. +The fixture stores only the selected login in its disposable company home, rejects +ambient/redirected homes, and removes the login during teardown. Token fragments +and account identifiers inside its JSON join the evidence redaction set. The +subscription suite remains manual-only and never falls back to `XAI_API_KEY`. + Create `runner-e2e-paid`, restrict deployments to the default branch, and put only `OPENAI_API_KEY`, `ANTHROPIC_API_KEY`, `OPENROUTER_API_KEY`, `XAI_API_KEY`, and `DAYTONA_API_KEY` in it. Do not duplicate these credentials as repository- or diff --git a/tests/runner-e2e/catalog.test.ts b/tests/runner-e2e/catalog.test.ts index ac64f1db0ef..a68f188cea4 100644 --- a/tests/runner-e2e/catalog.test.ts +++ b/tests/runner-e2e/catalog.test.ts @@ -64,25 +64,25 @@ describe("runner E2E catalog", () => { }); it("validates the core, local-integrity, breadth, and warm suites", () => { - expect(runnerProfiles).toHaveLength(7); + expect(runnerProfiles).toHaveLength(8); expect(openRouterBreadthProfiles).toHaveLength(4); expect(runnerEnvironments).toHaveLength(2); expect(runnerTasks).toHaveLength(3); expect(localIntegrityTasks).toHaveLength(2); expect(openRouterBreadthTasks).toHaveLength(3); expect(runnerSuites.map((suite) => suite.expectedMatrixSize)).toEqual([ - 2, 8, 46, 23, 47, 52, 28, 18, 6, 6, 4, 42, 14, 10, 2, + 16, 16, 2, 8, 46, 23, 47, 52, 28, 18, 6, 6, 4, 48, 16, 10, 2, ]); - expect(validateRunnerCatalog()).toHaveLength(308); - expect(new Set(runnerMatrix.map((entry) => entry.id)).size).toBe(308); + expect(validateRunnerCatalog()).toHaveLength(348); + expect(new Set(runnerMatrix.map((entry) => entry.id)).size).toBe(348); expect( runnerMatrix.filter((entry) => entry.suite.id === "core-compatibility"), - ).toHaveLength(42); + ).toHaveLength(48); expect( runnerMatrix.filter( (entry) => entry.suite.id === "local-session-integrity", ), - ).toHaveLength(14); + ).toHaveLength(16); expect( runnerMatrix.filter( (entry) => entry.suite.id === "openrouter-model-breadth", @@ -98,7 +98,7 @@ describe("runner E2E catalog", () => { (total, execution) => total + execution.task.expectedRunCount, 0, ), - ).toBe(371); + ).toBe(385); expect( runnerTasks.find((task) => task.id === "plan-revise-accept") ?.attemptTimeoutMs, @@ -550,6 +550,31 @@ describe("runner E2E selectors", () => { ]); }); + it("keeps Grok artifact and stop/resume qualification explicit in both environments", () => { + const selected = selectRunnerExecutions(parseRunnerSelectors(["--suite", "grok-qualification"])); + expect(selected).toHaveLength(16); + for (const environment of ["local", "daytona"]) { + for (const task of ["build-revise", "stop-new-resume", "continuity-restart"]) { + expect(selected.some(execution => execution.id === `grok-qualification.runner-acpx-grok.${environment}.${task}`)).toBe(true); + } + } + }); + + it("keeps subscription qualification separate and never injects an API key", () => { + const selected = selectRunnerExecutions(parseRunnerSelectors(["--suite", "grok-subscription-qualification"])); + expect(selected).toHaveLength(16); + const all = selectRunnerExecutions(parseRunnerSelectors(["--all"])); + for (const execution of selected) { + expect(all.some(candidate => candidate.id === execution.id)).toBe(false); + expect(execution.requiredCredentials).toEqual(execution.environment.id === "daytona" + ? ["GROK_AUTH_JSON", "DAYTONA_API_KEY"] : ["GROK_AUTH_JSON"]); + expect(execution.profile.buildAgent({ + environmentId: "env", environmentFixtureId: execution.environment.id, + workspacePath: "/tmp/workspace", secretRefs: {}, executionId: "subscription", + })).toMatchObject({ adapterConfig: { provider: "acpx", acpxAgent: "grok", env: {} } }); + } + }); + it("selects a suite without exploding its environment matrix", () => { const selected = selectRunnerExecutions( parseRunnerSelectors(["--suite", "openrouter-model-breadth"]), @@ -572,7 +597,7 @@ describe("runner E2E selectors", () => { "daytona", ]); const selected = selectRunnerExecutions(options); - expect(selected).toHaveLength(13); + expect(selected).toHaveLength(16); expect( selected.every( (entry) => @@ -591,10 +616,10 @@ describe("runner E2E selectors", () => { const jobs = buildMatrixJobs( selectRunnerExecutions(parseRunnerSelectors(["--all"])), ); - expect(jobs).toHaveLength(171); - expect(jobs.filter((job) => job.needsDaytona)).toHaveLength(23); - expect(jobs.filter((job) => !job.needsDaytona)).toHaveLength(148); - expect(new Set(jobs.map((job) => job.executionId)).size).toBe(171); + expect(jobs).toHaveLength(179); + expect(jobs.filter((job) => job.needsDaytona)).toHaveLength(26); + expect(jobs.filter((job) => !job.needsDaytona)).toHaveLength(153); + expect(new Set(jobs.map((job) => job.executionId)).size).toBe(179); expect( jobs.find( (job) => diff --git a/tests/runner-e2e/catalog.ts b/tests/runner-e2e/catalog.ts index e1a613cb2f9..4824ff7aa10 100644 --- a/tests/runner-e2e/catalog.ts +++ b/tests/runner-e2e/catalog.ts @@ -145,7 +145,7 @@ function nativeProfile(input: { provider: "codex" | "opencode" | "acpx"; model: string; credential: RunnerProfileFixture["credential"]; - acpxAgent?: "claude" | "codex"; + acpxAgent?: "claude" | "codex" | "grok"; supportedEnvironments?: readonly (typeof ENVIRONMENT_IDS)[number][]; modelQualification?: RunnerProfileFixture["modelQualification"]; ranking?: RunnerProfileFixture["ranking"]; @@ -173,7 +173,9 @@ function nativeProfile(input: { provider: input.provider, }, buildAgent(buildInput) { - const credentialRef = requiredSecret(buildInput, input.credential); + const credentialRef = input.credential === "GROK_AUTH_JSON" + ? null + : requiredSecret(buildInput, input.credential); const permissionConfig = input.provider === "codex" ? { codexPermissionMode: "never" } @@ -187,7 +189,7 @@ function nativeProfile(input: { idleTimeoutMs: 300_000, ...permissionConfig, env: { - [input.credential]: credentialRef, + ...(credentialRef ? { [input.credential]: credentialRef } : {}), // Codex's supported automation credential is CODEX_API_KEY. Keep // OPENAI_API_KEY as the operator-facing fixture secret name and bind // the same encrypted reference to the runtime-specific alias. @@ -284,6 +286,14 @@ export const runnerProfiles: readonly RunnerProfileFixture[] = [ model: QUALIFIED_ACPX_PROFILES.claude.qualificationModel, credential: "ANTHROPIC_API_KEY", }), + nativeProfile({ + id: "runner-acpx-grok", + label: "Runner Grok Build", + provider: "acpx", + acpxAgent: "grok", + model: QUALIFIED_ACPX_PROFILES.grok.qualificationModel, + credential: "XAI_API_KEY", + }), nativeProfile({ id: "runner-acpx-codex", label: "Runner ACPX Codex", @@ -912,6 +922,37 @@ const everydayProfiles = [ ].map(productionStoryProfile); export const runnerSuites: readonly RunnerSuiteFixture[] = [ + { + id: "grok-subscription-qualification", label: "Grok Build Subscription Qualification", manualOnly: true, + description: "Explicit company subscription login across Grok browser workflows in local and Daytona environments.", + groups: ["native"], + profiles: [nativeProfile({ + id: "runner-acpx-grok-subscription", label: "Grok Build Subscription", + provider: "acpx", acpxAgent: "grok", + model: QUALIFIED_ACPX_PROFILES.grok.qualificationModel, + credential: "GROK_AUTH_JSON", + })], + environments: [localEnvironment, daytonaWarmEnvironment], + tasks: [ + ...runnerTasks, ...localIntegrityTasks, + ...everydayTasks.filter(task => task.id === "build-revise"), + ...chatHardeningTasks.filter(task => ["stop-new-resume", "continuity-restart"].includes(task.id)), + ], expectedMatrixSize: 16, + definitionMetadata: { version: 1, authentication: "company-subscription", binary: "1.0.13", model: "grok-4.7", scheduling: "explicit-only", repetitionsRequired: 3, artifactOracle: "independent-python-contract", stopBoundary: "provider-turn-started" }, + }, + { + id: "grok-qualification", label: "Grok Build Qualification", manualOnly: true, + description: "Grok replies, planning approval, questions, downloadable artifacts, stop/resume and controller restart in local and Daytona environments.", + groups: ["native"], + profiles: runnerProfiles.filter(profile => profile.id === "runner-acpx-grok"), + environments: [localEnvironment, daytonaWarmEnvironment], + tasks: [ + ...runnerTasks, ...localIntegrityTasks, + ...everydayTasks.filter(task => task.id === "build-revise"), + ...chatHardeningTasks.filter(task => ["stop-new-resume", "continuity-restart"].includes(task.id)), + ], expectedMatrixSize: 16, + definitionMetadata: { version: 2, binary: "1.0.13", model: "grok-4.7", scheduling: "explicit-only", repetitionsRequired: 3, artifactOracle: "independent-python-contract", stopBoundary: "provider-turn-started" }, + }, { id: "api-response-reading", label: "Bounded API response reading", manualOnly: true, description: "Retrieve evidence beyond a saved API preview through authorized bounded text windows.", @@ -1029,7 +1070,7 @@ export const runnerSuites: readonly RunnerSuiteFixture[] = [ profiles: runnerProfiles, environments: runnerEnvironments, tasks: runnerTasks, - expectedMatrixSize: 42, + expectedMatrixSize: 48, }, { id: "local-session-integrity", @@ -1040,7 +1081,7 @@ export const runnerSuites: readonly RunnerSuiteFixture[] = [ profiles: runnerProfiles, environments: [localEnvironment], tasks: localIntegrityTasks, - expectedMatrixSize: 14, + expectedMatrixSize: 16, }, { id: "openrouter-model-breadth", @@ -1226,6 +1267,7 @@ export function validateRunnerCatalog(): MatrixExecution[] { "OPENAI_API_KEY", "ANTHROPIC_API_KEY", "OPENROUTER_API_KEY", + "XAI_API_KEY", "DAYTONA_API_KEY", ].map((name, index) => [ name, diff --git a/tests/runner-e2e/daytona-image-content.ts b/tests/runner-e2e/daytona-image-content.ts index 23a9a5dca8f..7f8b3b03866 100644 --- a/tests/runner-e2e/daytona-image-content.ts +++ b/tests/runner-e2e/daytona-image-content.ts @@ -27,6 +27,10 @@ export const DAYTONA_IMAGE_INPUT_PATHS = [ "packages/paperclip-eval-kernel/package.json", "packages/paperclip-eval-kernel/src", "packages/paperclip-eval-kernel/tsconfig.json", + "packages/grok-acp/package.json", + "packages/grok-acp/launcher.cjs", + "packages/grok-acp/install.mjs", + "packages/grok-acp/platforms.json", "packages/paperclip-runner/package.json", "packages/paperclip-runner/protocol", "packages/paperclip-runner/runner/Cargo.lock", diff --git a/tests/runner-e2e/daytona-image.test.ts b/tests/runner-e2e/daytona-image.test.ts index acacba9603c..74e79ca5559 100644 --- a/tests/runner-e2e/daytona-image.test.ts +++ b/tests/runner-e2e/daytona-image.test.ts @@ -13,6 +13,19 @@ import { const repositoryRoot = path.resolve(import.meta.dirname, "../.."); describe("runner E2E Daytona image contract", () => { + it("keeps the qualified native Grok binary separate from the legacy command", async () => { + const [dockerfile, packBuilder, nativePackage] = await Promise.all([ + readFile(path.join(repositoryRoot, "docker/daytona-runner/Dockerfile"), "utf8"), + readFile(path.join(repositoryRoot, "packages/paperclip-runner/scripts/build-provider-pack.mjs"), "utf8"), + readFile(path.join(repositoryRoot, "packages/grok-acp/package.json"), "utf8"), + ]); + expect(dockerfile).toMatch(/@xai-official\/grok@\d+\.\d+\.\d+/); + expect(dockerfile).not.toMatch(/for cli in[^;]*\bgrok\b/); + expect(packBuilder).not.toMatch(/writePortable\w+Shim\("grok"/); + expect(JSON.parse(nativePackage).bin).not.toHaveProperty("grok"); + expect(packBuilder).toContain('path: "node_modules/@paperclipai/grok-acp/bin/grok"'); + }); + it("builds runnerd and the provider pack and verifies every required transport", async () => { const [dockerfile, dockerignore, workflow] = await Promise.all([ readFile( diff --git a/tests/runner-e2e/grok-subscription-fixture.test.ts b/tests/runner-e2e/grok-subscription-fixture.test.ts new file mode 100644 index 00000000000..a14d0f693e8 --- /dev/null +++ b/tests/runner-e2e/grok-subscription-fixture.test.ts @@ -0,0 +1,54 @@ +import { mkdtemp, mkdir, readFile, realpath, stat, symlink, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { afterEach, expect, it } from "vitest"; +import { resolveManagedGrokHomeDir } from "../../packages/adapters/grok-local/src/server/grok-home.js"; +import { stageGrokSubscriptionFixture } from "./grok-subscription-fixture.js"; +import { findSecretLeak, normalizedSecrets, sanitizeJson } from "./redaction.js"; +import { buildPaperclipServerEnvironment } from "./harness-env.js"; + +const companyId = "11111111-2222-4333-8444-555555555555"; +const raw = JSON.stringify({ [`https://auth.x.ai::${companyId}`]: { + key: "fixture-access-credential", refresh_token: "fixture-refresh-credential", email: "fixture@example.invalid", +} }); +const roots: string[] = []; +afterEach(async () => { await Promise.all(roots.splice(0).map(root => rm(root, { recursive: true, force: true }))); }); +async function setup() { + const root = await realpath(await mkdtemp(path.join(tmpdir(), "grok-fixture-"))); + roots.push(root); + const home = path.join(root, "paperclip-home"); + await mkdir(home); + return { PAPERCLIP_RUNNER_E2E_TEMP_ROOT: root, PAPERCLIP_HOME: home, PAPERCLIP_INSTANCE_ID: "runner-e2e-test" }; +} + +it("stages only the explicit company credential privately and cleans refreshed state", async () => { + const environment = await setup(); + const remove = await stageGrokSubscriptionFixture({ raw, companyId, environment }); + const home = resolveManagedGrokHomeDir(environment, companyId); + expect(await readFile(path.join(home, "auth.json"), "utf8")).toBe(raw); + expect((await stat(home)).mode & 0o777).toBe(0o700); + expect((await stat(path.join(home, "auth.json"))).mode & 0o777).toBe(0o600); + await expect(stageGrokSubscriptionFixture({ raw, companyId, environment })).rejects.toThrow(); + await remove(); + await expect(stat(home)).rejects.toThrow(); +}); + +it("refuses malformed credentials and nonisolated or redirected homes", async () => { + const environment = await setup(); + await expect(stageGrokSubscriptionFixture({ raw: "{}", companyId, environment })).rejects.toThrow("malformed"); + await expect(stageGrokSubscriptionFixture({ raw, companyId: "../outside", environment })).rejects.toThrow("isolated"); + await expect(stageGrokSubscriptionFixture({ raw, companyId, environment: { ...environment, PAPERCLIP_HOME: tmpdir() } })).rejects.toThrow("outside"); + const home = resolveManagedGrokHomeDir(environment, companyId); + await mkdir(path.dirname(home), { recursive: true }); + await symlink(tmpdir(), home); + await expect(stageGrokSubscriptionFixture({ raw, companyId, environment })).rejects.toThrow("redirected"); +}); + +it("strips subscription input from the server environment and masks token fragments", () => { + const env = buildPaperclipServerEnvironment({ GROK_AUTH_JSON: raw, XAI_API_KEY: "other-key" }); + expect(env.GROK_AUTH_JSON).toBeUndefined(); + expect(env.XAI_API_KEY).toBeUndefined(); + const secrets = normalizedSecrets([raw]); + expect(findSecretLeak("fixture-refresh-credential", secrets)).toBeTruthy(); + expect(sanitizeJson({ message: "fixture-access-credential fixture@example.invalid" }, secrets)).toEqual({ message: "[REDACTED] [REDACTED]" }); +}); diff --git a/tests/runner-e2e/grok-subscription-fixture.ts b/tests/runner-e2e/grok-subscription-fixture.ts new file mode 100644 index 00000000000..26524ea592d --- /dev/null +++ b/tests/runner-e2e/grok-subscription-fixture.ts @@ -0,0 +1,42 @@ +import { mkdir, realpath, rm, writeFile } from "node:fs/promises"; +import path from "node:path"; +import { hasUsableGrokAuthValue, parseGrokAuthPayload, resolveManagedGrokHomeDir } from "../../packages/adapters/grok-local/src/server/grok-home.js"; + +function credential(raw: string) { + if (Buffer.byteLength(raw) > 256 * 1024) throw new Error("Grok fixture credential exceeds its bound"); + let parsed: unknown; + try { parsed = JSON.parse(raw); } catch { throw new Error("Grok fixture credential is malformed"); } + const result = parseGrokAuthPayload(parsed); + if (!result || !hasUsableGrokAuthValue(result.value)) throw new Error("Grok fixture credential is malformed"); + return result; +} + +/** Test setup only: seed the explicit login in a new disposable company slot. + * Production credential discovery, process fencing, and refresh remain unchanged. + */ +export async function stageGrokSubscriptionFixture(input: { + raw: string; + companyId: string; + environment: NodeJS.ProcessEnv; +}): Promise<() => Promise> { + credential(input.raw); + const env = input.environment; + const root = env.PAPERCLIP_RUNNER_E2E_TEMP_ROOT; + if (!root || !/^[0-9a-f-]{36}$/i.test(input.companyId) || + !/^runner-e2e-[a-z0-9-]+$/.test(env.PAPERCLIP_INSTANCE_ID ?? "")) { + throw new Error("Grok fixture requires an isolated test company and instance"); + } + const canonicalRoot = await realpath(root); + const home = env.PAPERCLIP_HOME; + if (!home || await realpath(home) !== path.join(canonicalRoot, "paperclip-home")) { + throw new Error("Grok fixture refuses a home outside its isolated test root"); + } + const companyHome = resolveManagedGrokHomeDir(env, input.companyId); + await mkdir(companyHome, { recursive: true, mode: 0o700 }); + if (!(await realpath(companyHome)).startsWith(`${canonicalRoot}${path.sep}`)) { + throw new Error("Grok fixture refuses a redirected company home"); + } + // Never replace an existing company's login, including through a symlink. + await writeFile(path.join(companyHome, "auth.json"), input.raw, { flag: "wx", mode: 0o600 }); + return () => rm(companyHome, { recursive: true, force: true }); +} diff --git a/tests/runner-e2e/harness-env.ts b/tests/runner-e2e/harness-env.ts index aea18086336..7b280009dd2 100644 --- a/tests/runner-e2e/harness-env.ts +++ b/tests/runner-e2e/harness-env.ts @@ -25,7 +25,7 @@ const AMBIENT_EXTERNAL_STATE_KEYS = [ "PAPERCLIP_STORAGE_S3_PREFIX", "PAPERCLIP_STORAGE_S3_FORCE_PATH_STYLE", ] as const; -const PROVIDER_SECRET_KEY = /^(?:OPENAI|ANTHROPIC|OPENROUTER|DAYTONA)(?:_|$)/; +const PROVIDER_SECRET_KEY = /^(?:OPENAI|ANTHROPIC|OPENROUTER|DAYTONA|XAI|GROK)(?:_|$)/; export function runnerE2EServerControlPaths(temporaryRoot: string) { const controlDirectory = path.join(temporaryRoot, "control"); @@ -129,7 +129,8 @@ export function buildRunnerE2EProcessEnvironment( /** * Build the environment inherited by the Paperclip server. Paid credentials * deliberately stay in the launcher/Playwright process and cross the server - * boundary only once, in the encrypted company-secrets API request. + * boundary through encrypted company secrets. Explicit subscription fixtures + * stage their login in the disposable company's private credential home. */ export function buildPaperclipServerEnvironment( source: NodeJS.ProcessEnv, diff --git a/tests/runner-e2e/history.test.ts b/tests/runner-e2e/history.test.ts index 27aa8a7bae0..9ead6475436 100644 --- a/tests/runner-e2e/history.test.ts +++ b/tests/runner-e2e/history.test.ts @@ -164,6 +164,9 @@ describe("runner E2E campaign history", () => { }); it("records the resolved paid target instead of the trusted workflow checkout", () => { + vi.stubEnv("GITHUB_SERVER_URL", ""); + vi.stubEnv("GITHUB_REPOSITORY", ""); + vi.stubEnv("GITHUB_RUN_ID", ""); vi.stubEnv("PAPERCLIP_RUNNER_E2E_SOURCE_SHA", "target-sha"); vi.stubEnv("PAPERCLIP_RUNNER_E2E_SOURCE_REF", "refs/heads/target"); vi.stubEnv("GITHUB_SHA", "trusted-master-sha"); diff --git a/tests/runner-e2e/live-fixtures.ts b/tests/runner-e2e/live-fixtures.ts index edb960b851b..7346ceb6dd0 100644 --- a/tests/runner-e2e/live-fixtures.ts +++ b/tests/runner-e2e/live-fixtures.ts @@ -1,6 +1,7 @@ import path from "node:path"; import { isManagedHiringCase } from "./chat-cases.js"; import { FixtureRegistry } from "./fixture-registry.js"; +import { stageGrokSubscriptionFixture } from "./grok-subscription-fixture.js"; import type { RunnerApi } from "./api.js"; import type { CredentialName, @@ -150,6 +151,7 @@ export async function setupLiveFixtures(input: { const company = value(resolved, "company"); const refs: SecretReferenceMap = {}; for (const credentialName of execution.requiredCredentials) { + if (credentialName === "GROK_AUTH_JSON") continue; const rawValue = input.credentials[credentialName]; if (!rawValue) throw new Error(`Missing credential ${credentialName}`); const secret = await api.postSensitive( @@ -171,11 +173,29 @@ export async function setupLiveFixtures(input: { }, }); + const grokSubscription = execution.profile.credential === "GROK_AUTH_JSON"; + if (grokSubscription) { + registry.register<() => Promise>({ + id: "subscription-login", + dependencies: ["company"], + async setup(resolved) { + const raw = input.credentials.GROK_AUTH_JSON; + if (!raw) throw new Error("Missing credential GROK_AUTH_JSON"); + return stageGrokSubscriptionFixture({ + raw, companyId: value(resolved, "company").id, + environment: process.env, + }); + }, + async teardown(remove) { await remove(); }, + }); + } + registry.register({ id: "environment", dependencies: [ "company", "secrets", + ...(grokSubscription ? ["subscription-login"] : []), ...(execution.environment.id === "daytona" ? ["sandbox-provider"] : []), ], async setup(resolved) { @@ -252,6 +272,7 @@ export async function setupLiveFixtures(input: { "company", "secrets", "environment", + ...(grokSubscription ? ["subscription-login"] : []), ...(managedHiring ? ["ai-connection"] : []), ], async setup(resolved) { diff --git a/tests/runner-e2e/redaction.ts b/tests/runner-e2e/redaction.ts index d6316a6f591..ec9df5fd5c9 100644 --- a/tests/runner-e2e/redaction.ts +++ b/tests/runner-e2e/redaction.ts @@ -14,11 +14,27 @@ const SENSITIVE_JSON_KEY = export function normalizedSecrets(values: readonly (string | undefined)[]) { return [ - ...new Set( - values - .map((value) => value?.trim()) - .filter((value): value is string => Boolean(value)), - ), + ...new Set(values.flatMap((value) => { + const trimmed = value?.trim(); + if (!trimmed) return []; + if (trimmed.startsWith("{")) { + try { + const parsed = JSON.parse(trimmed) as unknown; + // Subscription credentials are structured JSON. A log may contain an + // individual token or identity rather than the whole serialized file. + const leaves = (value: unknown, depth = 0): string[] => { + if (depth > 8) return []; + if (typeof value === "string") return value.length >= 8 ? [value] : []; + if (!value || typeof value !== "object") return []; + return Object.entries(value).flatMap(([key, child]) => [ + ...(key.includes("::") ? [key] : []), ...leaves(child, depth + 1), + ]); + }; + return [trimmed, ...leaves(parsed)]; + } catch { /* Preserve ordinary non-JSON secrets. */ } + } + return [trimmed]; + })), ].sort((left, right) => right.length - left.length); } diff --git a/tests/runner-e2e/run-observations.ts b/tests/runner-e2e/run-observations.ts index 1e5b1949e22..08c8ad184a8 100644 --- a/tests/runner-e2e/run-observations.ts +++ b/tests/runner-e2e/run-observations.ts @@ -106,6 +106,19 @@ function record(value: unknown): Record { : {}; } +export function reasoningProjectionFailures(events: readonly ObservableRunEvent[]): string[] { + const misclassified = events.filter((event) => { + const envelope = record(record(event.payload).prpEvent); + const payload = record(envelope.payload); + return envelope.eventType === "item.delta" && + payload.kind === "agentMessage" && + record(payload.update).kind === "reasoning"; + }); + return misclassified.length === 0 ? [] : [ + `provider reasoning was projected as assistant text in ${misclassified.length} durable events`, + ]; +} + export function isNonExecutingReviewFenceRun(run: ObservableRunState) { return ( run.status === "cancelled" && diff --git a/tests/runner-e2e/runner.spec.ts b/tests/runner-e2e/runner.spec.ts index 1cd4ad18a30..03e81ae5fe8 100644 --- a/tests/runner-e2e/runner.spec.ts +++ b/tests/runner-e2e/runner.spec.ts @@ -32,6 +32,7 @@ import { isOpenRouterDeepSeekHelloTerminalVariance, numberedPlanStepCount, providerSessionContinuityFailures, + reasoningProjectionFailures, } from "./run-observations.js"; import { resolveRunnerE2ESource } from "./source.js"; import { isValidNativePrpEnvelope } from "./native-event-envelope.js"; @@ -1791,7 +1792,9 @@ for (const execution of executions) { const pendingInteractions = terminal.interactions.filter( (interaction) => interaction.status === "pending", ); - const invariantFailures: string[] = []; + const invariantFailures: string[] = reasoningProjectionFailures( + runEventsByRun.flatMap((entry) => entry.events), + ); if (pendingInteractions.length > 0) invariantFailures.push( `expected no unresolved interaction; observed ${pendingInteractions.length}`, diff --git a/tests/runner-e2e/select-rerun-artifacts.test.ts b/tests/runner-e2e/select-rerun-artifacts.test.ts index cc27acf09ac..b6793b2885b 100644 --- a/tests/runner-e2e/select-rerun-artifacts.test.ts +++ b/tests/runner-e2e/select-rerun-artifacts.test.ts @@ -160,6 +160,37 @@ function singletonSelectionInput(paths: Awaited>) { } describe("runner E2E workflow rerun artifact selection", () => { + + it.each(["before", "after"])("ignores a queued placeholder %s a started replacement without hiding its failure", async order => { + const paths = await fixture(); + await addArtifact({ root: paths.artifactRoot, executionId: RERUN, workflowAttempt: 1, status: "passed" }); + const latest = await addArtifact({ root: paths.artifactRoot, executionId: RERUN, workflowAttempt: 2, status: "failed" }); + const input = singletonSelectionInput(paths); + const jobs = input.jobs.jobs.map(job => ({ ...job, status: "completed" })); + const placeholder = { ...jobs.find(job => job.run_attempt === 2)!, status: "queued" }; + if (order === "before") jobs.unshift(placeholder); else jobs.push(placeholder); + const selected = await selectRerunArtifacts({ ...input, jobs: { ...input.jobs, jobs } }); + expect(selected).toEqual([{ executionId: RERUN, workflowAttempt: 2, artifactName: latest.artifactName }]); + const evidence = JSON.parse(await readFile(path.join(paths.selectedRoot, latest.artifactName, latest.campaignName, "results", "attempt-1", "result.json"), "utf8")); + expect(evidence.status).toBe("failed"); + }); + + it.each(["completed", "in_progress", "unknown", "queued"])("still rejects ambiguous duplicate %s jobs", async status => { + const paths = await fixture(); + const input = singletonSelectionInput(paths); + const jobs = input.jobs.jobs.map(job => ({ ...job, status })); + jobs.push({ ...jobs.find(job => job.run_attempt === 2)! }); + await expect(selectRerunArtifacts({ ...input, jobs: { ...input.jobs, jobs } })).rejects.toThrow("contains duplicate job"); + }); + + it("does not fall back to an older pass when the latest job is only queued", async () => { + const paths = await fixture(); + await addArtifact({ root: paths.artifactRoot, executionId: RERUN, workflowAttempt: 1, status: "passed" }); + const input = singletonSelectionInput(paths); + const jobs = input.jobs.jobs.map(job => ({ ...job, status: job.run_attempt === 2 ? "queued" : "completed" })); + expect(await selectRerunArtifacts({ ...input, jobs: { ...input.jobs, jobs } })).toEqual([]); + }); + it("accepts the v8 flattened layout for one expected artifact", async () => { const paths = await fixture(); const latest = await addArtifact({ diff --git a/tests/runner-e2e/select-rerun-artifacts.ts b/tests/runner-e2e/select-rerun-artifacts.ts index 358d064dda0..37458815323 100644 --- a/tests/runner-e2e/select-rerun-artifacts.ts +++ b/tests/runner-e2e/select-rerun-artifacts.ts @@ -7,6 +7,7 @@ interface WorkflowJob { name: string; run_attempt: number; started_at: string; + status?: string; } interface WorkflowJobsResponse { @@ -130,7 +131,17 @@ export async function selectRerunArtifacts(input: SelectRerunArtifactsInput) { // current attempt's trusted run_started_at, so they are not real reruns. if (jobStartedAt < attemptStartedAt.get(attempt)!) continue; const attempts = attemptsByExecution.get(candidate.name) ?? new Map(); - if (attempts.has(attempt)) { + const previous = attempts.get(attempt); + if (previous) { + const hasStarted = (job: WorkflowJob) => job.status === "in_progress" || job.status === "completed"; + // GitHub can retain a queued placeholder with a different job ID even + // after its replacement starts. It has no execution evidence. Keep a + // lone queued latest attempt, however, so an older pass cannot mask it. + if (previous.status === "queued" && hasStarted(candidate)) { + attempts.set(attempt, candidate); + continue; + } + if (hasStarted(previous) && candidate.status === "queued") continue; throw new Error( `workflow attempt ${attempt} contains duplicate job ${candidate.name}`, ); diff --git a/tests/runner-e2e/support.test.ts b/tests/runner-e2e/support.test.ts index 9c71367fa0b..157f36f099f 100644 --- a/tests/runner-e2e/support.test.ts +++ b/tests/runner-e2e/support.test.ts @@ -45,11 +45,31 @@ import { isOpenRouterDeepSeekHelloTerminalVariance, numberedPlanStepCount, providerSessionContinuityFailures, + reasoningProjectionFailures, } from "./run-observations.js"; import { runnerE2EWebServerCommand } from "./web-server-command.js"; const cleanupDirectories: string[] = []; +it("rejects reasoning mislabeled as assistant text without echoing private content", () => { + const reasoning = { + eventType: "item.delta", + payload: { prpEvent: { + eventType: "item.delta", + payload: { kind: "agentMessage", text: "PRIVATE_THOUGHT", update: { kind: "reasoning" } }, + } }, + }; + expect(reasoningProjectionFailures([reasoning])).toEqual([ + "provider reasoning was projected as assistant text in 1 durable events", + ]); + const correctlyTyped = structuredClone(reasoning); + correctlyTyped.payload.prpEvent.payload.kind = "reasoning"; + expect(reasoningProjectionFailures([correctlyTyped])).toEqual([]); + const assistant = structuredClone(reasoning); + assistant.payload.prpEvent.payload.update.kind = "agentMessage"; + expect(reasoningProjectionFailures([assistant])).toEqual([]); +}); + describe("complete run event evidence", () => { const page = Array.from({ length: 1000 }, (_, i) => ({ seq: i + 1, eventType: "item.delta" })); it("reads completion events beyond the first 1000 rows", async () => { @@ -866,6 +886,9 @@ describe("runner E2E server isolation", () => { ANTHROPIC_API_KEY: "anthropic", OPENROUTER_API_KEY: "openrouter", DAYTONA_API_KEY: "daytona", + XAI_API_KEY: "xai", + XAI_ORG_ID: "xai-sensitive", + GROK_HOME: "/outside/grok", OPENAI_ORG_ID: "also-provider-sensitive", PAPERCLIP_API_KEY: "ambient-board-key", PAPERCLIP_AGENT_API_KEY: "ambient-agent-key", @@ -889,6 +912,9 @@ describe("runner E2E server isolation", () => { expect(env.DATABASE_URL).toBeUndefined(); expect(env.OPENAI_API_KEY).toBeUndefined(); expect(env.OPENAI_ORG_ID).toBeUndefined(); + expect(env.XAI_API_KEY).toBeUndefined(); + expect(env.XAI_ORG_ID).toBeUndefined(); + expect(env.GROK_HOME).toBeUndefined(); expect(env.PAPERCLIP_API_KEY).toBeUndefined(); expect(env.PAPERCLIP_AGENT_API_KEY).toBeUndefined(); expect(env.XDG_CACHE_HOME).toBe("/tmp/cell/xdg-cache"); diff --git a/tests/runner-e2e/types.ts b/tests/runner-e2e/types.ts index d2704db83e2..28332b7e404 100644 --- a/tests/runner-e2e/types.ts +++ b/tests/runner-e2e/types.ts @@ -2,6 +2,8 @@ export const CREDENTIAL_NAMES = [ "OPENAI_API_KEY", "ANTHROPIC_API_KEY", "OPENROUTER_API_KEY", + "XAI_API_KEY", + "GROK_AUTH_JSON", "DAYTONA_API_KEY", ] as const; diff --git a/tests/runner-e2e/workflow-security.test.ts b/tests/runner-e2e/workflow-security.test.ts index 0616e9d47aa..31e6337af38 100644 --- a/tests/runner-e2e/workflow-security.test.ts +++ b/tests/runner-e2e/workflow-security.test.ts @@ -15,6 +15,21 @@ const everydayOracleImage = "python@sha256:9d2e5553305c7c7b0097999bb17187c69b921ccd6bc9d40e4bb5ebe652c00285"; describe("public repository paid workflow security", () => { + it("keeps the manual EC2 image build credential-free and pins the authorized target", async () => { + const workflow = await readFile(path.join(repositoryRoot, ".github/workflows/docker-runner-check.yml"), "utf8"); + const manual = workflow.slice(workflow.indexOf(" authorize_manual:")); + expect(manual.match(/AWS_CI_TRUSTED_USER_IDS/gu)).toHaveLength(2); + expect(manual.match(/test "\$REPOSITORY_ID" = 1170821064/gu)).toHaveLength(2); + expect(manual).toContain('runs-on: runs-on/fleet=paperclip-public-pr-x64/env=public-ci'); + expect(manual).toContain('repos/$REPOSITORY/git/ref/heads/$TARGET_BRANCH'); + expect(manual).toContain('ref: ${{ needs.authorize_manual.outputs.target_sha }}'); + expect(manual).toContain('SOURCE_SHA: ${{ needs.authorize_manual.outputs.target_sha }}'); + expect([...manual.matchAll(/secrets\.([A-Z_]+)/gu)].map(match => match[1])).toEqual(["GITHUB_TOKEN"]); + expect(manual).toContain('pnpm install --resolution-only --ignore-scripts --no-frozen-lockfile'); + expect(manual).toContain('PAPERCLIP_RUNNER_LOCK_SHA256=$lock_sha'); + expect(manual).toContain('docker logout ghcr.io'); + }); + it("uses the reviewed master branch for the first-party trusted PR workflow", async () => { const ordinaryPrWorkflow = await readFile( path.join(repositoryRoot, ".github/workflows/pr.yml"), @@ -206,7 +221,7 @@ describe("public repository paid workflow security", () => { ); expect(daytonaImageJob).not.toContain("name: runner-e2e-paid"); expect(daytonaImageJob).not.toMatch( - /(?:OPENAI|ANTHROPIC|OPENROUTER|DAYTONA|XAI)_API_KEY/, + /(?:(?:OPENAI|ANTHROPIC|OPENROUTER|DAYTONA|XAI)_API_KEY|GROK_AUTH_JSON)/, ); expect(authorizeJob).toContain( "aws_runner='runs-on/fleet=paperclip-public-pr-x64/env=public-ci'", diff --git a/ui/public/sw.js b/ui/public/sw.js index a9b325a4f86..7f443c6a001 100644 --- a/ui/public/sw.js +++ b/ui/public/sw.js @@ -69,6 +69,15 @@ self.addEventListener("fetch", (event) => { return; } + // Vite development modules use the browser's conditional-response cache. + // Passing their requests through fetch/respondWith can return a bodyless 304 + // to the module loader on reload and leave the app root empty. They are not + // build assets and have no offline contract, so leave them to the browser. + if (url.origin === self.location.origin && + /^\/(?:@fs|@vite|@id|src|node_modules)\//.test(url.pathname)) { + return; + } + // Network-first; only public build assets can use an offline fallback. event.respondWith( fetch(request) diff --git a/ui/src/adapters/codex-local/config-fields.tsx b/ui/src/adapters/codex-local/config-fields.tsx index 4c518314b80..3282956d85f 100644 --- a/ui/src/adapters/codex-local/config-fields.tsx +++ b/ui/src/adapters/codex-local/config-fields.tsx @@ -195,9 +195,10 @@ export function CodexLocalConfigFields({ > )} diff --git a/ui/src/components/AgentConfigForm.tsx b/ui/src/components/AgentConfigForm.tsx index 8556e8d5146..ad44b7b3964 100644 --- a/ui/src/components/AgentConfigForm.tsx +++ b/ui/src/components/AgentConfigForm.tsx @@ -1067,6 +1067,7 @@ export function AgentConfigForm(props: AgentConfigFormProps) { if (props.compactTestFeedback) { const providerAdapter = adapterType === "paperclip_runner" ? adapterConfig.provider === "codex" ? "codex_local" + : adapterConfig.provider === "acpx" && adapterConfig.acpxAgent === "grok" ? "grok_local" : adapterConfig.provider === "acpx" && adapterConfig.acpxAgent === "claude" ? "claude_local" : adapterType : adapterType; @@ -1657,7 +1658,7 @@ export function AgentConfigForm(props: AgentConfigFormProps) { )} - {!isCreate && selectedCompanyId && | undefined)?.aiConnection ?? runtimeConfig.aiConnection).data} model={String(eff("adapterConfig", "model", config.model) ?? "")} environmentId={currentDefaultEnvironmentId || undefined} legacy onChange={binding => mark("runtime", "runtimeConfig", { ...runtimeConfig, aiConnection: binding })} />} diff --git a/ui/src/components/new-agent/AgentBasicsDialog.tsx b/ui/src/components/new-agent/AgentBasicsDialog.tsx index 173e1cd248d..d778c2bb146 100644 --- a/ui/src/components/new-agent/AgentBasicsDialog.tsx +++ b/ui/src/components/new-agent/AgentBasicsDialog.tsx @@ -274,6 +274,7 @@ export function AgentBasicsDialog({ > + diff --git a/ui/src/components/new-agent/NewAgentSetup.tsx b/ui/src/components/new-agent/NewAgentSetup.tsx index 5ebb4e318be..7f55a0b2360 100644 --- a/ui/src/components/new-agent/NewAgentSetup.tsx +++ b/ui/src/components/new-agent/NewAgentSetup.tsx @@ -111,7 +111,9 @@ function Setup({ const appearanceDraft = useAgentAppearanceDraft(`${companyId}:new-agent`); const isRunner = adapterType === "paperclip_runner"; const brandType = isRunner - ? runnerProvider === "claude" + ? runnerProvider === "grok" + ? "grok_local" + : runnerProvider === "claude" ? "claude_local" : runnerProvider === "opencode" ? "opencode_local" @@ -340,8 +342,8 @@ function Setup({ ...(isRunner ? { adapterSchemaValues: { - provider: runnerProvider === "claude" ? "acpx" : runnerProvider, - ...(runnerProvider === "claude" ? { acpxAgent: "claude" } : {}), + provider: (runnerProvider === "claude" || runnerProvider === "grok") ? "acpx" : runnerProvider, + ...((runnerProvider === "claude" || runnerProvider === "grok") ? { acpxAgent: runnerProvider } : {}), }, } : {}), @@ -349,8 +351,8 @@ function Setup({ const config = getUIAdapter(adapterType).buildAdapterConfig(values); if (isRunner) Object.assign(config, { - provider: runnerProvider === "claude" ? "acpx" : runnerProvider, - ...(runnerProvider === "claude" ? { acpxAgent: "claude" } : {}), + provider: (runnerProvider === "claude" || runnerProvider === "grok") ? "acpx" : runnerProvider, + ...((runnerProvider === "claude" || runnerProvider === "grok") ? { acpxAgent: runnerProvider } : {}), ...(model ? { model } : {}), }); if (!aiBinding && !nextConnection?.aiConnection && hasCredentialField && binding) { diff --git a/ui/src/lib/service-worker-cache-policy.test.ts b/ui/src/lib/service-worker-cache-policy.test.ts index c72b68725d4..630b2a8c5b2 100644 --- a/ui/src/lib/service-worker-cache-policy.test.ts +++ b/ui/src/lib/service-worker-cache-policy.test.ts @@ -23,6 +23,17 @@ function worker(development = false) { } describe("service worker privacy boundaries", () => { + it.each([ + "/@fs/tmp/runner/vite-cache/deps/@assistant-ui_react.js?v=fixture", + "/@vite/client", "/@id/__x00__virtual:module", "/src/main.tsx", "/node_modules/.vite/deps/react.js", + ])("leaves conditional Vite module request %s to the browser", pathname => { + const w = worker(); + w.fetch.mockResolvedValue(new Response(null, { status: 304 })); + expect(w.request("default", pathname)).not.toHaveBeenCalled(); + expect(w.fetch).not.toHaveBeenCalled(); + expect(w.put).not.toHaveBeenCalled(); + expect(w.match).not.toHaveBeenCalled(); + }); it("leaves development navigation and module revalidation to the browser", () => { const w = worker(true); for (const path of ["/RUN/issues/RUN-1", "/src/main.tsx", "/@fs/vite-cache/deps/react.js?v=1"]) { From 2f585ef26a1814fa209715242d1ca791b63e4c4e Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Mon, 28 Sep 2026 14:03:36 -0500 Subject: [PATCH 6/6] fix(ui): preserve newer drafts after repeated receipt cleanup (#14332) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - The task composer keeps unsent text across page reloads. > - A server receipt confirms a submitted message after a reload. > - Replayed cleanup can apply the old text offset to a newer draft twice. > - This pull request reconciles each confirmed attempt once and preserves both tabs’ unsent intent. > - The full newer draft stays available for the next send. ## Linked Issues or Issue Description **What happened?** Reloading the classic composer while a save was pending could truncate a newer draft. Replayed receipt cleanup changed `A newer draft written while delivery was pending.` into `nding.`. The storage helper rejected the duplicate settlement, but the effect still changed the editor and its body reference. **Expected behavior** A receipt settles its retained submission once. Later cleanup must preserve the newer draft in both the editor and browser storage. **Steps to reproduce** 1. Send a comment and hold its HTTP response after the server accepts it. 2. Type a newer draft, then reload the page. 3. Restore the matching receipt under React StrictMode. 4. Inspect the newer draft after effect replay and unmount. The deterministic regression reproduces this on current master. The existing browser test exposed the issue during #14329 verification. Related draft persistence code came from #13338. A search found no separate fix for this duplicate settlement. ## What Changed - Reconcile each confirmed attempt once in memory so duplicate effects cannot trim its newer draft again. - Unlock confirmed drafts when storage writes fail. Never acquire another tab’s pending receipt or assume its text is newer. Keep a conflicting local draft and its attachments in tab-scoped session storage while preserving the shared draft unchanged. - Add component regressions for StrictMode replay, exact editor and stored bytes, foreign receipts, attachment-only differences, reload recovery, unavailable storage, and task navigation before autosave. A brief notice explains when this tab has a separate draft. ## Verification - RED: the new test received `nding.` instead of the full newer draft before the fix. - Review RED: three cases reproduced a locked composer after failed storage writes or another tab's settlement. A further negative case preserves a newer retained attempt and its attachments. - Cross-tab review RED: four deterministic cases reproduced lost local text, foreign receipt takeover, attachment loss when text matched, and overwriting newer stored text. - GREEN: 118 tests across `IssueChatThread`, `composer-draft`, and `comment-submit-draft`, including same-mounted A → B → A navigation and a full recovery-storage failure. Two further lifecycle RED tests verify finishing a recovery returns to the shared draft on a later visit while continued typing and attachments retain recovery. - Both existing browser reload cases passed against a fresh server and database on exact head `ceb80aca77fc8cc0f813c328ba87025b3e1a2222` (42.5 seconds), with classic mode enabled and disabled. The browser flow verifies one original comment, a preserved draft, and a successful second send. - UI typecheck, token gates, and the shipped static UI build passed. The initial typecheck required the fresh worktree's plugin SDK build; the retry passed after that dependency built. - The session-only failure mock also preserves localStorage on platforms where both share the Storage prototype; this fixes the Linux workspace test failure. - All 56 checks passed on `4c30dcccc4fe5b90dcd08dd1d90475ea89e4a376`; Greptile is 5/5 with zero open review threads. An unrelated runner baseline scan hit its existing 100 ms deadline once; its isolated test and the single failed CI job passed on retry without source changes. This four-file UI fix does not change server or database code. - Final alternate-staging acceptance passed on deployed source `d884e1ab046cc76004e35e6091e9e6e2c918c9eb`, with exact health checked before and after. Three real browser cases used shipped static UI and real API saves: reload during an accepted-but-unacknowledged save in both composer variants, plus two classic tabs with different drafts. The test deliberately held only its own accepted POST acknowledgement and temporarily withheld its own GET receipt from one tab to reproduce settlement ordering. Both drafts survived independent reloads, the shared draft remained intact after the other tab sent, and finishing recovery returned to the shared draft on a later visit. Exact request IDs/counts and server comment bytes passed; no provider was invoked. Screenshots preserve the live drafts before fixture cleanup. - An ordinary authenticated Chrome/CUA visit independently showed the exact original and newer comments; an additional unsent draft survived reload and was then cleared. All three isolated fixtures are complete, browser contexts are closed, and original user tasks were untouched. ## Risks - Reconciliation uses the exact request ID and draft key. Conflicting drafts stay separate; the tab recovery survives same-tab reload through session storage and does not outlive the browser tab. If recovery storage is unavailable, the editor keeps its in-memory text and explicitly warns the user to copy it before leaving. - Attachment selections remain bounded to 20 per draft; if combining equal-text snapshots would exceed that limit, each original selection stays in its respective draft. - No API, schema, or migration changes. The status notice uses existing design tokens. ## Model Used OpenAI Codex, GPT-6, with tool use and code execution. The runtime does not expose the exact deployment variant or context-window size. ## 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 - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip --- ui/src/components/IssueChatThread.test.tsx | 230 +++++++++++++++++++++ ui/src/components/IssueChatThread.tsx | 78 +++++-- ui/src/lib/composer-draft.test.ts | 44 ++++ ui/src/lib/composer-draft.ts | 47 ++++- 4 files changed, 385 insertions(+), 14 deletions(-) diff --git a/ui/src/components/IssueChatThread.test.tsx b/ui/src/components/IssueChatThread.test.tsx index 82268181708..62e1e686d11 100644 --- a/ui/src/components/IssueChatThread.test.tsx +++ b/ui/src/components/IssueChatThread.test.tsx @@ -15,6 +15,7 @@ import { MemoryRouter } from "react-router-dom"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import type { Agent } from "@paperclipai/shared"; import { CommentSubmissionUnknownError } from "../lib/comment-submit-result"; +import { loadDraft, preserveDraftInTab, settleDraftSubmission } from "../lib/composer-draft"; import { IssueAssigneePausedNotice, IssueChatThread, @@ -379,6 +380,7 @@ describe("IssueChatThread", () => { document.body.appendChild(container); window.scrollTo = vi.fn(); localStorage.clear(); + sessionStorage.clear(); }); afterEach(() => { @@ -3276,6 +3278,234 @@ describe("IssueChatThread", () => { act(() => root.unmount()); }); + it("settles a restored submission only once through StrictMode effect replay", () => { + const key = "strict-restored-submission"; + const attemptId = "aaf8228f-0be7-45ae-a104-6fbe0af6f1d3"; + const submitted = "One text-only save interrupted by reload."; + const nextDraft = "A newer draft written while delivery was pending."; + localStorage.setItem(key, `${submitted}\n\n${nextDraft}`); + localStorage.setItem(`${key}:submission:v1`, JSON.stringify({ + version: 1, + draftKey: key, + attemptId, + reviewed: false, + nextDraftOffset: submitted.length + 2, + submittedAttachmentIds: [], + })); + const root = createRoot(container); + try { + act(() => root.render( + + + {}} + draftKey={key} + enableLiveTranscriptPolling={false} + /> + + , + )); + expect(container.querySelector( + 'textarea[aria-label="Issue chat editor"]', + )?.value).toBe(nextDraft); + expect(localStorage.getItem(key)).toBe(nextDraft); + expect(localStorage.getItem(`${key}:submission:v1`)).toBeNull(); + expect(container.textContent).not.toContain("We couldn’t confirm"); + } finally { + act(() => root.unmount()); + } + expect(localStorage.getItem(key)).toBe(nextDraft); + }); + + it.each(["submission write", "all storage"])( + "unlocks a confirmed in-memory submission after failed %s", + async (failure) => { + const key = "unavailable-submission-storage"; + const originalSetItem = localStorage.setItem.bind(localStorage); + const write = vi.spyOn(localStorage, "setItem").mockImplementation((key, value) => { + if (failure === "all storage" || key.endsWith(":submission:v1")) throw new Error("Storage unavailable"); + originalSetItem(key, value); + }); + const read = failure === "all storage" + ? vi.spyOn(localStorage, "getItem").mockImplementation(() => { throw new Error("Storage unavailable"); }) + : null; + let rejectSend!: (error: Error) => void; + const onAdd = vi.fn().mockReturnValueOnce(new Promise((_, reject) => { rejectSend = reject; })).mockResolvedValue(undefined); + const root = createRoot(container); + const element = (attemptId?: string) => ( + + + + ); + const editor = () => container.querySelector('textarea[aria-label="Issue chat editor"]')!; + const type = (value: string) => act(() => { + Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, "value")!.set!.call(editor(), value); + editor().dispatchEvent(new Event("input", { bubbles: true })); + }); + const send = () => Array.from(container.querySelectorAll("button")).find(button => button.textContent === "Send") as HTMLButtonElement; + try { + await act(async () => root.render(element())); + type("Earlier message"); + await act(async () => send().click()); + const attemptId = onAdd.mock.calls[0]![4] as string; + type("The full next draft"); + await act(async () => rejectSend(new CommentSubmissionUnknownError())); + expect(container.textContent).toContain("We couldn’t confirm"); + await act(async () => root.render(element(attemptId))); + expect(editor().value).toBe("The full next draft"); + expect(container.textContent).not.toContain("We couldn’t confirm"); + expect(send().disabled).toBe(false); + await act(async () => send().click()); + expect(onAdd.mock.calls[1]![0]).toBe("The full next draft"); + } finally { + await act(async () => root.unmount()); + write.mockRestore(); + read?.mockRestore(); + } + }, + ); + + it.each(["newer local text", "foreign pending receipt", "matching text with new attachments", "matching full text with new attachments", "own receipt with newer stored text", "unavailable recovery storage"])( + "preserves both tabs through settlement and reload: %s", + async (scenario) => { + const key = `cross-tab-preserved-${scenario}`; + const attemptId = "aaf8228f-0be7-45ae-a104-6fbe0af6f1d3"; + const newerId = "baf8228f-0be7-45ae-a104-6fbe0af6f1d3"; + const attachmentId = "caf8228f-0be7-45ae-a104-6fbe0af6f1d3"; + const localText = "This tab's newer unsent draft"; + const otherText = scenario === "matching text with new attachments" ? localText : scenario === "matching full text with new attachments" ? `Earlier message\n\n${localText}` : "The other tab's unsent draft"; + localStorage.setItem(key, `Earlier message\n\n${localText}`); + localStorage.setItem(`${key}:submission:v1`, JSON.stringify({ version: 1, draftKey: key, attemptId, reviewed: false, nextDraftOffset: 17, submittedAttachmentIds: [] })); + const element = (confirmed: boolean, foreignConfirmed = false) => ( + + {}} draftKey={key} enableLiveTranscriptPolling={false} + /> + + ); + let root = createRoot(container); + const storagePrototype = Object.getPrototypeOf(sessionStorage) as Storage; + const originalStorageWrite = storagePrototype.setItem; + const recoveryWrite = scenario === "unavailable recovery storage" + ? vi.spyOn(storagePrototype, "setItem").mockImplementation(function (this: Storage, key: string, value: string) { + if (this === sessionStorage) throw new Error("Storage full"); + return originalStorageWrite.call(this, key, value); + }) + : null; + try { + await act(async () => root.render(element(false))); + if (scenario === "own receipt with newer stored text") localStorage.setItem(key, `Earlier message\n\n${otherText}`); + else expect(settleDraftSubmission(key, attemptId, otherText)).toBe(true); + if (scenario === "foreign pending receipt") localStorage.setItem(`${key}:submission:v1`, JSON.stringify({ version: 1, draftKey: key, attemptId: newerId, reviewed: false, nextDraftOffset: 0, submittedAttachmentIds: [] })); + localStorage.setItem(`${key}:attachments:v1`, JSON.stringify({ version: 1, draftKey: key, attachments: [{ attachmentId, name: "another-tab.txt", inline: false, contentPath: `/api/attachments/${attachmentId}/content` }] })); + await act(async () => root.render(element(true))); + const editor = () => container.querySelector('textarea[aria-label="Issue chat editor"]')!; + expect(editor().value).toBe(localText); + expect(container.textContent).not.toContain("We couldn’t confirm"); + if (scenario === "unavailable recovery storage") expect(container.textContent).toContain("copy your text before leaving"); + if (scenario.includes("with new attachments")) expect(container.textContent).toContain("another-tab.txt"); + if (scenario === "foreign pending receipt") { + localStorage.setItem(key, "The owning tab kept typing after the first receipt"); + await act(async () => root.render(element(true, true))); + expect(localStorage.getItem(`${key}:submission:v1`)).toContain(newerId); + } else expect(localStorage.getItem(`${key}:submission:v1`)).toBeNull(); + const storedText = localStorage.getItem(key); + expect(storedText).toBe(scenario === "foreign pending receipt" ? "The owning tab kept typing after the first receipt" : otherText); + expect(localStorage.getItem(`${key}:attachments:v1`)).toContain(attachmentId); + await act(async () => root.unmount()); + expect(localStorage.getItem(key)).toBe(storedText); + if (scenario === "unavailable recovery storage") return; + root = createRoot(container); + await act(async () => root.render(element(true, true))); + expect(editor().value).toBe(localText); + if (scenario.includes("with new attachments")) expect(container.textContent).toContain("another-tab.txt"); + expect(localStorage.getItem(key)).toBe(storedText); + expect(localStorage.getItem(`${key}:attachments:v1`)).toContain(attachmentId); + } finally { + if (container.childNodes.length) await act(async () => root.unmount()); + recoveryWrite?.mockRestore(); + } + }, + ); + + it.each(["ordinary", "recovered"])("preserves %s drafts when the same composer switches A to B to A before debounce", async (kind) => { + vi.useFakeTimers(); + const a = `navigation-a-${kind}`; + const b = `navigation-b-${kind}`; + const keys = kind === "recovered" + ? [preserveDraftInTab(a, "Draft A", []).key, preserveDraftInTab(b, "Draft B", []).key] + : [a, b]; + if (kind === "ordinary") { localStorage.setItem(a, "Draft A"); localStorage.setItem(b, "Draft B"); } + const root = createRoot(container); + const element = (draftKey: string) => ( + {}} draftKey={draftKey} enableLiveTranscriptPolling={false} /> + ); + const editor = () => container.querySelector('textarea[aria-label="Issue chat editor"]')!; + const type = (value: string) => act(() => { + Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, "value")!.set!.call(editor(), value); + editor().dispatchEvent(new Event("input", { bubbles: true })); + }); + try { + await act(async () => root.render(element(a))); + expect(editor().value).toBe("Draft A"); + type("Draft A typed just now"); + await act(async () => root.render(element(b))); + expect(editor().value).toBe("Draft B"); + expect(loadDraft(keys[0]!)).toBe("Draft A typed just now"); + type("Draft B typed just now"); + await act(async () => root.render(element(a))); + expect(editor().value).toBe("Draft A typed just now"); + expect(loadDraft(keys[1]!)).toBe("Draft B typed just now"); + } finally { await act(async () => root.unmount()); } + }); + + it("returns to the shared draft after finishing a recovery and leaving the task", async () => { + const a = "finished-recovery-a"; + const b = "finished-recovery-b"; + localStorage.setItem(a, "The other tab's preserved draft"); + preserveDraftInTab(a, "My recovered message", []); + const onAdd = vi.fn().mockResolvedValue(undefined); + const root = createRoot(container); + const element = (draftKey: string) => ( + + ); + const editor = () => container.querySelector('textarea[aria-label="Issue chat editor"]')!; + try { + await act(async () => root.render(element(a))); + expect(editor().value).toBe("My recovered message"); + await act(async () => (Array.from(container.querySelectorAll("button")).find(button => button.textContent === "Send") as HTMLButtonElement).click()); + expect(onAdd.mock.calls[0]![0]).toBe("My recovered message"); + expect(editor().value).toBe(""); + await act(async () => root.render(element(b))); + await act(async () => root.render(element(a))); + expect(editor().value).toBe("The other tab's preserved draft"); + expect(container.textContent).not.toContain("This draft is kept separately"); + } finally { await act(async () => root.unmount()); } + }); + it("stores and restores the composer draft per issue key", () => { vi.useFakeTimers(); const root = createRoot(container); diff --git a/ui/src/components/IssueChatThread.tsx b/ui/src/components/IssueChatThread.tsx index cd1c4d5b0e4..1b7f8418e72 100644 --- a/ui/src/components/IssueChatThread.tsx +++ b/ui/src/components/IssueChatThread.tsx @@ -59,6 +59,9 @@ import { useOptionalToastActions } from "../context/ToastContext"; import { copyTextToClipboard } from "../lib/clipboard"; import { loadDraft, + loadDraftIfAvailable, + loadDraftRecoveryKey, + preserveDraftInTab, saveDraft, clearDraft, loadDraftAttachments, @@ -4652,7 +4655,7 @@ const IssueChatComposer = forwardRef< stopScope = "leaf", onImageUpload, onAttachImage, - draftKey, + draftKey: sharedDraftKey, enableReassign = false, reassignOptions = [], currentAssigneeValue = "", @@ -4672,6 +4675,17 @@ const IssueChatComposer = forwardRef< forwardedRef, ) { const stopControl = useComposerStop(onStop, stopPending); + const restoredRecovery = useMemo(() => { + const key = sharedDraftKey && loadDraftRecoveryKey(sharedDraftKey); + return key ? { sourceKey: sharedDraftKey!, key, persisted: true } : null; + }, [sharedDraftKey]); + const [newRecovery, setDraftRecovery] = useState<{ sourceKey: string; key: string; persisted: boolean } | null>(null); + const draftRecovery = newRecovery?.sourceKey === sharedDraftKey ? newRecovery : restoredRecovery; + const draftKey = draftRecovery?.key ?? sharedDraftKey; + // Keep the active buffer through send completion. Re-entering a task may + // return to its shared draft after an empty recovery has been retired. + useEffect(() => setDraftRecovery(restoredRecovery), [sharedDraftKey, restoredRecovery]); + const retiredDraftKeyRef = useRef(undefined); // Initialize before StrictMode's mount cleanup can flush an empty value over // the stored draft. The effect below handles subsequent task-key changes. const [body, setBody] = useState(() => (draftKey ? loadDraft(draftKey) : "")); @@ -4691,6 +4705,7 @@ const IssueChatComposer = forwardRef< }, [draftKey]); const bodyRef = useRef(body); bodyRef.current = body; + const reconciledSubmissionRef = useRef<{ draftKey: string | undefined; attemptId: string } | null>(null); const pendingDraftRef = useRef<{ draftKey: string; attemptId: string; @@ -4806,7 +4821,7 @@ const IssueChatComposer = forwardRef< } useEffect(() => { - if (!draftKey) return; + if (!draftKey || (draftRecovery?.key === draftKey && !draftRecovery.persisted)) return; setBody(loadDraft(draftKey)); setComposerAttachments( loadDraftAttachments(draftKey).map((item) => ({ @@ -4822,21 +4837,54 @@ const IssueChatComposer = forwardRef< // Text equality is not delivery proof: users may intentionally repeat text. useEffect(() => { if (!uncertainSubmission || !confirmedSubmissionIds.has(uncertainSubmission.attemptId)) return; + const { attemptId } = uncertainSubmission; + const reconciled = reconciledSubmissionRef.current; + if (reconciled && reconciled.draftKey === draftKey && reconciled.attemptId === attemptId) return; const nextDraft = uncertainSubmission.nextDraftOffset === undefined ? "" : bodyRef.current.slice(uncertainSubmission.nextDraftOffset); - if (draftKey) settleDraftSubmission(draftKey, uncertainSubmission.attemptId, nextDraft); + const submittedIds = uncertainSubmission.submittedAttachmentIds; + let nextAttachments = submittedIds + ? composerAttachmentsRef.current.filter(item => !item.attachmentId || !submittedIds.includes(item.attachmentId)) + : []; + if (draftKey) { + const retained = loadDraftSubmission(draftKey); + // Settle storage from its own snapshot, never from this tab's stale copy. + // A different retained attempt is owned by its original tab. + if (retained?.attemptId === attemptId) settleDraftSubmission(draftKey, attemptId); + const storedDraft = loadDraftIfAvailable(draftKey); + const storedAttachments = loadDraftAttachments(draftKey).filter(item => !submittedIds?.includes(item.attachmentId)); + const foreignAttempt = retained && retained.attemptId !== attemptId; + const differentAttachments = JSON.stringify(nextAttachments.map(item => item.attachmentId).sort()) !== + JSON.stringify(storedAttachments.map(item => item.attachmentId).sort()); + if (storedDraft !== null && (foreignAttempt || !retained || storedDraft !== nextDraft || differentAttachments)) { + // Text has no cross-tab ordering. Preserve each buffer separately. When + // both snapshots describe the same text, retain all attachment receipts. + if (!foreignAttempt && (storedDraft === nextDraft || storedDraft === bodyRef.current)) { + const ids = new Set(nextAttachments.map(item => item.attachmentId)); + const combined = [...nextAttachments, ...storedAttachments.filter(item => !ids.has(item.attachmentId)).map(item => ({ + ...item, size: item.size ?? 0, id: `receipt:${item.attachmentId}`, status: "attached" as const, + }))]; + // Each draft is limited to 20 receipts. If their union exceeds that, + // keep the local selection here and the other selection in shared storage. + if (combined.length <= 20) nextAttachments = combined; + } + const recovered = preserveDraftInTab(sharedDraftKey!, nextDraft, nextAttachments); + // Fence old effect cleanups before switching keys or updating bodyRef. + retiredDraftKeyRef.current = draftKey; + setDraftRecovery({ sourceKey: sharedDraftKey!, ...recovered }); + } + // With unavailable storage, the confirmed in-memory attempt still settles. + } + reconciledSubmissionRef.current = { draftKey, attemptId }; setUncertainSubmission(null); setBody(nextDraft); bodyRef.current = nextDraft; - const submittedIds = uncertainSubmission.submittedAttachmentIds; - setComposerAttachments(current => submittedIds - ? current.filter(item => !item.attachmentId || !submittedIds.includes(item.attachmentId)) - : []); - }, [confirmedSubmissionIds, draftKey, uncertainSubmission]); + setComposerAttachments(nextAttachments); + }, [confirmedSubmissionIds, draftKey, sharedDraftKey, uncertainSubmission]); useEffect(() => { if ( - !draftKey || + !draftKey || retiredDraftKeyRef.current === draftKey || submitting || composerAttachments !== composerAttachmentsRef.current ) @@ -4853,14 +4901,14 @@ const IssueChatComposer = forwardRef< if (!draftKey || submitting) return; if (draftTimer.current) clearTimeout(draftTimer.current); draftTimer.current = setTimeout(() => { - saveDraft(draftKey, body); + if (retiredDraftKeyRef.current !== draftKey) saveDraft(draftKey, body); }, DRAFT_DEBOUNCE_MS); }, [body, draftKey, submitting]); useEffect(() => { return () => { if (draftTimer.current) clearTimeout(draftTimer.current); - if (draftKey && !submittingRef.current) + if (draftKey && retiredDraftKeyRef.current !== draftKey && !submittingRef.current) saveDraft(draftKey, bodyRef.current); }; }, [draftKey]); @@ -4868,7 +4916,7 @@ const IssueChatComposer = forwardRef< useEffect(() => { if (!draftKey) return; const flushDraft = () => { - if (!submittingRef.current) saveDraft(draftKey, bodyRef.current); + if (retiredDraftKeyRef.current !== draftKey && !submittingRef.current) saveDraft(draftKey, bodyRef.current); }; window.addEventListener("beforeunload", flushDraft); return () => window.removeEventListener("beforeunload", flushDraft); @@ -5351,6 +5399,12 @@ const IssueChatComposer = forwardRef<
) : null} + {draftRecovery && draftRecovery.sourceKey === sharedDraftKey ? ( +

+ Another tab changed the saved draft. This draft is kept separately in this tab. + {!draftRecovery.persisted ? " Browser storage is unavailable; copy your text before leaving." : " It will be restored if you reload this tab."} +

+ ) : null} {uncertainSubmission ? (
{ contentPath: `/api/attachments/${id}/content`, }; beforeEach(() => localStorage.clear()); + it("persists a tab recovery without touching shared text, receipts, or pending submission", () => { + sessionStorage.clear(); + saveDraft(key, "Other tab text"); + saveDraftAttachments(key, [receipt]); + saveDraftSubmission(key, { attemptId: id, reviewed: false }); + const fork = preserveDraftInTab(key, "This tab text", [receipt]); + expect(fork.persisted).toBe(true); + expect(loadDraftRecoveryKey(key)).toBe(fork.key); + expect(loadDraft(fork.key)).toBe("This tab text"); + expect(loadDraftAttachments(fork.key)).toEqual([receipt]); + expect(loadDraftSubmission(fork.key)).toBeNull(); + expect(loadDraft(key)).toBe("Other tab text"); + expect(loadDraftAttachments(key)).toEqual([receipt]); + expect(loadDraftSubmission(key)?.attemptId).toBe(id); + clearDraft(fork.key); + expect(loadDraft(fork.key)).toBe(""); + expect(loadDraft(key)).toBe("Other tab text"); + expect(loadDraftSubmission(key)?.attemptId).toBe(id); + }); + + it("retires a finished recovery but keeps or restores its mapping for unsent work", () => { + sessionStorage.clear(); + saveDraft(key, "Shared draft stays available"); + const fork = preserveDraftInTab(key, "Recovered message", []); + saveDraftSubmission(fork.key, { attemptId: id, reviewed: false }); + expect(settleDraftSubmission(fork.key, id)).toBe(true); + expect(loadDraftRecoveryKey(key)).toBeNull(); + expect(loadDraft(key)).toBe("Shared draft stays available"); + + saveDraft(fork.key, "Next recovered draft"); + expect(loadDraftRecoveryKey(key)).toBe(fork.key); + saveDraftSubmission(fork.key, { attemptId: id, reviewed: false, nextDraftOffset: 0, submittedAttachmentIds: [] }); + expect(settleDraftSubmission(fork.key, id)).toBe(true); + expect(loadDraftRecoveryKey(key)).toBe(fork.key); + expect(loadDraft(fork.key)).toBe("Next recovered draft"); + clearDraft(fork.key); + expect(loadDraftRecoveryKey(key)).toBeNull(); + saveDraftAttachments(fork.key, [receipt]); + expect(loadDraftRecoveryKey(key)).toBe(fork.key); + expect(loadDraftAttachments(fork.key)).toEqual([receipt]); + }); + it("settles only submitted text and attachments while preserving the next draft", () => { const nextId = "aaf8228f-0be7-45ae-a104-6fbe0af6f1d3"; const nextReceipt = { ...receipt, attachmentId: nextId, contentPath: `/api/attachments/${nextId}/content` }; diff --git a/ui/src/lib/composer-draft.ts b/ui/src/lib/composer-draft.ts index 1c28b721aa9..2ca07566def 100644 --- a/ui/src/lib/composer-draft.ts +++ b/ui/src/lib/composer-draft.ts @@ -18,10 +18,47 @@ function draftStorage(draftKey: string): Storage { } export function loadDraft(draftKey: string): string { + return loadDraftIfAvailable(draftKey) ?? ""; +} + +/** Distinguish an empty stored draft from unavailable browser storage. */ +export function loadDraftIfAvailable(draftKey: string): string | null { try { return draftStorage(draftKey).getItem(draftKey) ?? ""; } catch { - return ""; + return null; + } +} + +/** A conflicting tab keeps its own recoverable buffer without replacing the + * shared draft or acquiring another tab's pending receipt. */ +export function loadDraftRecoveryKey(draftKey: string): string | null { + const key = `paperclip:agent-chat-draft:recovered:${draftKey}`; + try { + return sessionStorage.getItem(`${key}:recovery:v1`) === draftKey ? key : null; + } catch { return null; } +} + +export function preserveDraftInTab(draftKey: string, body: string, attachments: unknown): { key: string; persisted: boolean } { + const key = `paperclip:agent-chat-draft:recovered:${draftKey}`; + try { + sessionStorage.setItem(key, body); + sessionStorage.setItem(`${key}:attachments:v1`, JSON.stringify({ version: 1, draftKey: key, attachments: draftAttachments(attachments) })); + sessionStorage.setItem(`${key}:recovery:v1`, draftKey); + return { key, persisted: true }; + } catch { + return { key, persisted: false }; + } +} + +function syncDraftRecoveryMarker(draftKey: string) { + const prefix = "paperclip:agent-chat-draft:recovered:"; + if (!draftKey.startsWith(prefix)) return; + const marker = `${draftKey}:recovery:v1`; + if (loadDraft(draftKey).trim() || loadDraftAttachments(draftKey).length || loadDraftSubmission(draftKey)) { + sessionStorage.setItem(marker, draftKey.slice(prefix.length)); + } else { + sessionStorage.removeItem(marker); } } @@ -38,6 +75,7 @@ export function saveDraft(draftKey: string, value: string, attemptId?: string) { } else { draftStorage(draftKey).removeItem(draftKey); } + syncDraftRecoveryMarker(draftKey); } catch { // Ignore browser storage failures. } @@ -49,6 +87,7 @@ export function clearDraft(draftKey: string, attemptId?: string) { draftStorage(draftKey).removeItem(draftKey); draftStorage(draftKey).removeItem(`${draftKey}:attachments:v1`); draftStorage(draftKey).removeItem(`${draftKey}:submission:v1`); + syncDraftRecoveryMarker(draftKey); } catch { // Ignore browser storage failures. } @@ -101,6 +140,7 @@ export function saveDraftSubmission( `${draftKey}:submission:v1`, JSON.stringify({ version: 1, draftKey, ...submission }), ); + syncDraftRecoveryMarker(draftKey); } catch { /* The composer also retains the fence in memory. */ } @@ -108,8 +148,10 @@ export function saveDraftSubmission( export function clearDraftSubmission(draftKey: string, attemptId: string) { try { - if (loadDraftSubmission(draftKey)?.attemptId === attemptId) + if (loadDraftSubmission(draftKey)?.attemptId === attemptId) { draftStorage(draftKey).removeItem(`${draftKey}:submission:v1`); + syncDraftRecoveryMarker(draftKey); + } } catch { /* Disabled browser storage is supported in memory. */ } @@ -212,6 +254,7 @@ export function saveDraftAttachments(draftKey: string, attachments: unknown, att JSON.stringify({ version: 1, draftKey, attachments: selected }), ); else draftStorage(draftKey).removeItem(`${draftKey}:attachments:v1`); + syncDraftRecoveryMarker(draftKey); } catch { /* Disabled/full browser storage must not break the composer. */ }