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 95d29ad32e7..c1e2377a391 100644 --- a/.env.runner-e2e.example +++ b/.env.runner-e2e.example @@ -2,6 +2,7 @@ # 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= @@ -12,3 +13,6 @@ COPILOT_GITHUB_TOKEN= # 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/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/doc/DEVELOPING.md b/doc/DEVELOPING.md index 60891a5a54f..417715fc925 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. @@ -1287,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/doc/evals.md b/doc/evals.md index 90b37a4fa82..2db1a0416da 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 0e1588713e0..b32975d5d49 100644 --- a/docker/daytona-runner/Dockerfile +++ b/docker/daytona-runner/Dockerfile @@ -9,7 +9,7 @@ WORKDIR /workspace/packages/paperclip-runner/runner COPY packages/paperclip-runner/runner/Cargo.toml packages/paperclip-runner/runner/Cargo.lock ./ COPY packages/paperclip-runner/runner/crates ./crates COPY packages/paperclip-runner/protocol ../protocol -RUN cargo build --locked --release -p paperclip-runner-core --bin paperclip-runnerd \ +RUN cargo build --locked --release -j 2 -p paperclip-runner-core --bin paperclip-runnerd \ && strip /workspace/packages/paperclip-runner/runner/target/release/paperclip-runnerd FROM node:24-bookworm@sha256:9137a20e25879e0b557227b57e3ee4e9af4bde29eb3db66134cd1723e84f830b AS provider-pack-build @@ -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=650e23d20e967bcfbfced888e131199b9a06e66a1ba4f64cfb68383b59def4a8 -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=9c163adf40fe1c6a65abda6207bd9f5acfc8196bc94f81348b8bf1b2769e050e +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 d5ea93606e3..d2326115d4e 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. @@ -109,9 +110,8 @@ Refresh the default from the clean tracked lockfile using the exact `pnpm install --resolution-only --ignore-scripts --no-frozen-lockfile` command, and verify a second resolution preserves the digest. A lockfile left by a filtered or incremental install can retain stale importer patch identities. -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; do not +download dependencies when a task starts. ## Candidate ACP qualification assets @@ -137,3 +137,7 @@ The pack builder tests its copied Node interpreter after relocation. Use a standalone Node distribution if the host interpreter depends on a package manager's private shared libraries. Pi additionally pins its complete portable interpreter and npm dependency graph. +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/adapter-utils/src/paperclip-runner-permissions.ts b/packages/adapter-utils/src/paperclip-runner-permissions.ts index b23590af0e6..cb32c0fd78c 100644 --- a/packages/adapter-utils/src/paperclip-runner-permissions.ts +++ b/packages/adapter-utils/src/paperclip-runner-permissions.ts @@ -226,6 +226,7 @@ export function normalizeLegacyRunnerProvider( /** Qualification is a release property, never an operator-configurable bypass. */ export const PAPERCLIP_RUNNER_ACPX_PROFILES = Object.freeze([ + { value: "grok", label: "Grok Build", qualified: true, credentialEnvironment: ["XAI_API_KEY"] }, { value: "claude", label: "Claude", qualified: true, credentialEnvironment: ["ANTHROPIC_API_KEY", "CLAUDE_CODE_OAUTH_TOKEN"] }, { value: "cursor", label: "Cursor", qualified: false, credentialEnvironment: ["CURSOR_API_KEY", "CURSOR_AUTH_TOKEN"] }, { value: "copilot", label: "GitHub Copilot", qualified: false, credentialEnvironment: ["COPILOT_GITHUB_TOKEN"] }, 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 596b74ad881..b4ade5f76d8 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 336b3445e43..d1a7f69148e 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 @@ -188,6 +188,14 @@ impl AcpxProviderDescriptor { None, "sha256:b18c01603dd0169d233140709cfaa8bf5304a03cf5de78ca4f625f30013e8457", ), + "grok" => ( + "grok-4.7", + "@paperclipai/grok-acp", + "1.0.13", + Some("@paperclipai/grok-acp"), + Some("1.0.13"), + "sha256:f0b698395a3704ed2ffaf84ea19bdb20c36c8a0a70b7c629c7b6ffe144e59e55", + ), _ => { return Err(DurableRunnerError::invalid( "ACPX agent must name a known immutable profile", @@ -199,7 +207,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() > 240 || self.model.contains('\0') 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 8d8a2cfa626..c92f0449402 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 @@ -81,6 +81,7 @@ 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", "pi" => "openrouter/deepseek/deepseek-v4-flash-0731", "cursor" | "copilot" => self.model.as_str(), @@ -90,7 +91,7 @@ impl AcpxProviderSessionConfig { )) } }; - 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 731227117e4..5614dcf5099 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,6 +98,7 @@ 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"], "pi" => &["OPENROUTER_API_KEY"], "cursor" => &["CURSOR_API_KEY", "CURSOR_AUTH_TOKEN"], "copilot" => &["COPILOT_GITHUB_TOKEN"], 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 951a5697091..c9b9ce59338 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 @@ -160,6 +160,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 ae776b745b4..4208ea806cf 100644 --- a/packages/paperclip-runner/scripts/build-provider-pack.mjs +++ b/packages/paperclip-runner/scripts/build-provider-pack.mjs @@ -207,6 +207,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", @@ -312,6 +318,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", }, @@ -326,6 +333,7 @@ try { .update(distDigest) .digest("hex")}`, acpxProfileDigests: { + grok: "sha256:f0b698395a3704ed2ffaf84ea19bdb20c36c8a0a70b7c629c7b6ffe144e59e55", claude: "sha256:9d73d1f0f121fb96cc8badb28c22d5bff02d8582eb2e40360a81c189e1b9422a", codex: @@ -333,6 +341,10 @@ try { }, ...(candidates.length ? { candidateProviders } : {}), 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/cli/acpx-runtime-sidecar.test.ts b/packages/paperclip-runner/src/cli/acpx-runtime-sidecar.test.ts index cf276bfa910..35cfd2635ca 100644 --- a/packages/paperclip-runner/src/cli/acpx-runtime-sidecar.test.ts +++ b/packages/paperclip-runner/src/cli/acpx-runtime-sidecar.test.ts @@ -586,7 +586,7 @@ describe("qualified ACPX runtime sidecar", () => { ok: false, error: { code: "acpx_sidecar_command_failed", - message: "ACPX agent must be claude, codex, cursor, copilot, or pi", + message: "ACPX agent must be claude, codex, grok, cursor, copilot, or pi", 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 f40c8f4777e..3f32d5baf85 100644 --- a/packages/paperclip-runner/src/cli/acpx-runtime-sidecar.ts +++ b/packages/paperclip-runner/src/cli/acpx-runtime-sidecar.ts @@ -15,7 +15,7 @@ import type { import { acpxProfileClientCapabilities, bindAcpxExtensionTurn, validateAcpxRichEvent, createAcpxProfileExtensionAdapter, type AcpxExtensionInput } from "../drivers/acpx/profile-extensions.js"; import type { PaperclipQuestionSet } from "../contracts/question-set.js"; -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, @@ -596,11 +596,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, ); @@ -1321,8 +1323,8 @@ function requireHost( } function requireQualifiedAgent(value: unknown): QualifiedAcpxAgent { - if (value !== "codex" && value !== "claude" && value !== "pi" && value !== "cursor" && value !== "copilot") { - throw new Error("ACPX agent must be claude, codex, cursor, copilot, or pi"); + if (value !== "grok" && value !== "codex" && value !== "claude" && value !== "pi" && value !== "cursor" && value !== "copilot") { + throw new Error("ACPX agent must be claude, codex, grok, cursor, copilot, or pi"); } 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 1cd7feee824..7082be9ad58 100644 --- a/packages/paperclip-runner/src/cli/eval-session-contract.test.ts +++ b/packages/paperclip-runner/src/cli/eval-session-contract.test.ts @@ -246,6 +246,15 @@ 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)).toThrow("budget cost coverage is unavailable"); + expect(turn.status).toBe("completed"); + expect(turn.snapshot.usageLedger).toEqual([]); + 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 = { @@ -405,37 +414,39 @@ describe("eval-session usage", () => { }); describe("eval-session budget settlement", () => { - it.each(["no_receipt", "unpriced", "mixed", "over_limit"] as const)("retains the completed provider outcome while failing %s accounting", async (kind) => { + it.each(["no_receipt", "grok_no_receipt", "unpriced", "mixed", "over_limit"] as const)("retains the completed provider outcome while failing %s accounting", async (kind) => { const workspace = await mkdtemp(join(tmpdir(), "eval-budget-settlement-")); try { const binary = join(workspace, "runnerd"); await writeFile(binary, "unused fake runner"); - const model = kind === "unpriced" || kind === "no_receipt" ? "exact-unpriced-candidate" : "gpt-5.6-sol"; + const agent = kind === "grok_no_receipt" ? "grok" : "copilot"; + const noReceipt = kind === "no_receipt" || kind === "grok_no_receipt"; + const model = kind === "grok_no_receipt" ? "grok-4.7" : kind === "unpriced" || kind === "no_receipt" ? "exact-unpriced-candidate" : "gpt-5.6-sol"; const unavailable = { turnId: kind === "mixed" ? "prior-turn" : "turn-1", attemptId: "attempt-1", agent: "copilot", reason: "provider_did_not_report_usage", tokenUsage: null, costNanodollars: null, observedAt: "2026-09-28T19:00:00.000Z" }; const state = JSON.stringify(createCapabilityFixtureState({})); const snapshot = { sessionId: "session-1", revision: 1, providerThreadId: "provider-1", providerSessionId: "provider-1", - providerModel: { id: model, provider: "github" }, status: "idle", activeTurnId: null, + providerModel: { id: model, provider: agent === "grok" ? "xai" : "github" }, status: "idle", activeTurnId: null, createdAt: "2026-09-28T19:00:00.000Z", updatedAt: "2026-09-28T19:00:01.000Z", authority: { companyId: "company-1", actorId: "actor-1", taskId: "task-1", runId: "run-1", sessionId: "session-1", scenarioId: "budget-test" }, - config: { provider: "acpx", acpxAgent: "copilot", driver: "acpx_runtime", seedState: state, workingDirectory: workspace, scenario: { id: "budget-test" }, capabilities: [], explicitClaims: [], turnTimeoutMs: 1000 }, + config: { provider: "acpx", acpxAgent: agent, driver: "acpx_runtime", seedState: state, workingDirectory: workspace, scenario: { id: "budget-test" }, capabilities: [], explicitClaims: [], turnTimeoutMs: 1000 }, mockState: state, process: null, networkEvidence: { realPaperclipRequests: 0, childPaperclipEnvironmentKeys: [] }, transcript: [{ id: "assistant-1", role: "assistant", text: "Retained actual provider response", turnId: "turn-1", at: "2026-09-28T19:00:01.000Z" }], evidence: [], authorizationRecords: [], attempts: [], terminalTurns: [{ turnId: "turn-1", status: "completed" }], - usageLedger: kind === "no_receipt" ? [] : [{ receiptId: "receipt-1", turnId: "turn-1", providerCalls: 1, providerRequests: 1, inputTokens: 100, outputTokens: kind === "over_limit" ? 1_000_000 : 10, cachedInputTokens: 0, reasoningTokens: 0, costNanodollars: 0 }], + usageLedger: noReceipt ? [] : [{ receiptId: "receipt-1", turnId: "turn-1", providerCalls: 1, providerRequests: 1, inputTokens: 100, outputTokens: kind === "over_limit" ? 1_000_000 : 10, cachedInputTokens: 0, reasoningTokens: 0, costNanodollars: 0 }], usageUnavailable: kind === "no_receipt" || kind === "mixed" ? [unavailable] : [], } as unknown as CapabilityLiveSessionSnapshot; const sendMessage = vi.fn(async () => ({ turnId: "turn-1", status: "completed", assistantText: "Retained actual provider response", snapshot })); const completeAttempt = vi.fn(async () => undefined); const shutdown = vi.fn(async () => { snapshot.status = "closed"; }); - const input = request({ provider: "acpx", acpxAgent: "copilot", model, + const input = request({ provider: "acpx", acpxAgent: agent, model, runnerd: { path: binary, sha256: createHash("sha256").update("unused fake runner").digest("hex") }, session: { workingDirectory: workspace }, limits: { turnTimeoutMs: 1000, maxAgentTurns: 2, maxEstimatedCostNanodollars: 100_000_000 }, }); const requestPath = join(workspace, "request.json"); const outputPath = join(workspace, "output.json"); await writeFile(requestPath, JSON.stringify(input)); - const exitCode = await runEvalSessionCli(["--request", requestPath, "--output", outputPath, "--candidate-profile", "copilot"], { + const exitCode = await runEvalSessionCli(["--request", requestPath, "--output", outputPath, ...(agent === "grok" ? [] : ["--candidate-profile", "copilot"])], { serviceFactory: () => ({ create: async () => ({ sendMessage, completeAttempt, shutdown, snapshot: () => snapshot }) }) as never, }); const artifact = JSON.parse(await readFile(outputPath, "utf8")); @@ -444,10 +455,10 @@ describe("eval-session budget settlement", () => { expect(artifact.accountingFailure).toMatchObject({ class: kind === "over_limit" ? "provider_budget_reached" : "provider_budget_coverage_unknown", retryable: false }); expect(artifact.turn).toMatchObject({ status: "completed", assistantText: "Retained actual provider response" }); expect(artifact.snapshot.status).toBe("closed"); - expect(artifact.acpxProfile.agent).toBe("copilot"); + expect(artifact.acpxProfile.agent).toBe(agent); expect(artifact.devtools).toBeDefined(); expect(artifact.issueThread).toBeDefined(); - if (kind === "no_receipt") expect(artifact).not.toHaveProperty("usage"); + if (noReceipt) expect(artifact).not.toHaveProperty("usage"); else expect(artifact.usage.providerReportedCostNanodollars).toBeNull(); expect(sendMessage).toHaveBeenCalledTimes(1); expect(shutdown).toHaveBeenCalledTimes(1); diff --git a/packages/paperclip-runner/src/cli/eval-session-contract.ts b/packages/paperclip-runner/src/cli/eval-session-contract.ts index 87345c56351..d1446cb4ac0 100644 --- a/packages/paperclip-runner/src/cli/eval-session-contract.ts +++ b/packages/paperclip-runner/src/cli/eval-session-contract.ts @@ -250,7 +250,7 @@ export function parseEvalSessionRequest( if ( acpxAgent !== undefined && acpxAgent !== "codex" && - acpxAgent !== "claude" && + acpxAgent !== "claude" && acpxAgent !== "grok" && acpxAgent !== "pi" && acpxAgent !== "cursor" && acpxAgent !== "copilot" ) { throw new Error("eval-session acpxAgent must be a registered ACPX profile"); diff --git a/packages/paperclip-runner/src/cli/eval-session.ts b/packages/paperclip-runner/src/cli/eval-session.ts index 5718acf5e09..19153159445 100644 --- a/packages/paperclip-runner/src/cli/eval-session.ts +++ b/packages/paperclip-runner/src/cli/eval-session.ts @@ -260,6 +260,9 @@ export function boundedEvalSessionUsage( if (turn.status !== "completed") { return usageIfAvailable(request, turn.snapshot); } + if (request.provider === "acpx" && request.acpxAgent === "grok" && !turn.snapshot.usageLedger?.length) { + throw new EvalSessionBudgetError("budget cost coverage is unavailable for the completed Grok turn", true); + } const candidate = request.provider === "acpx" && request.acpxAgent !== undefined && ["pi", "cursor", "copilot"].includes(request.acpxAgent) diff --git a/packages/paperclip-runner/src/contracts/native-execution.ts b/packages/paperclip-runner/src/contracts/native-execution.ts index 6fd70d624d5..c046171cfa1 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" | "cursor" | "copilot"; +export type NativeAcpxAgent = "pi" | "claude" | "codex" | "grok" | "cursor" | "copilot"; 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" && provider.agent !== "cursor" && provider.agent !== "copilot") { - throw new NativeExecutionInputError("input.provider.agent must be pi, claude, codex, cursor, or copilot"); + if (provider.agent !== "pi" && provider.agent !== "claude" && provider.agent !== "codex" && provider.agent !== "grok" && provider.agent !== "cursor" && provider.agent !== "copilot") { + throw new NativeExecutionInputError("input.provider.agent must be pi, claude, codex, grok, cursor, or copilot"); } 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 8045236ce31..38d12b5f6b3 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 @@ -3287,6 +3287,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/capability-profiles.ts b/packages/paperclip-runner/src/drivers/acpx/capability-profiles.ts index 27d6d9e61e3..4a70c22e25b 100644 --- a/packages/paperclip-runner/src/drivers/acpx/capability-profiles.ts +++ b/packages/paperclip-runner/src/drivers/acpx/capability-profiles.ts @@ -31,6 +31,12 @@ export const ACPX_CAPABILITY_PROFILES: Readonly | null = null; #assistantText = ""; + #assistantMessageId: string | null = null; #closed = false; #closingStarted = false; #eventStreamClosed = false; @@ -875,6 +877,7 @@ class CodexAcpxSession implements HarnessSession { this.#activeTurnId = turnId; this.#turnControls.begin(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 }); @@ -1423,8 +1426,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; await drainExtensions(); @@ -1616,6 +1621,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 74051d4ac13..f79a3e291e4 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 @@ -188,6 +188,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 ce4c861fe4a..fcc480f74f4 100644 --- a/packages/paperclip-runner/src/drivers/acpx/codex-runtime-adapter.ts +++ b/packages/paperclip-runner/src/drivers/acpx/codex-runtime-adapter.ts @@ -303,9 +303,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. @@ -1358,7 +1358,15 @@ function runtimePort( const guarded = turnWithVerifiedLifetimeOwnership(turn, finishOwnershipAdmission); extensionTurn.promptStarted = guarded.promptStarted; 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(() => { releaseExtensionTurn(); 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 7c5988b8750..a325f1e29dc 100644 --- a/packages/paperclip-runner/src/drivers/acpx/driver-profile.test.ts +++ b/packages/paperclip-runner/src/drivers/acpx/driver-profile.test.ts @@ -17,6 +17,7 @@ describe("ACPX driver profile", () => { }); it.each([ ["codex", "available"], + ["grok", "available"], ["claude", "available"], ["pi", "unsupported"], ] as const)( @@ -56,6 +57,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 6c2bb12697f..259f547b170 100644 --- a/packages/paperclip-runner/src/drivers/acpx/driver-profile.ts +++ b/packages/paperclip-runner/src/drivers/acpx/driver-profile.ts @@ -14,7 +14,7 @@ import { import { ACPX_CAPABILITY_PROFILES } from "./capability-profiles.js"; -const ACPX_AGENTS = ["claude", "codex", "pi", "cursor", "copilot"] as const; +const ACPX_AGENTS = ["claude", "codex", "grok", "pi", "cursor", "copilot"] as const; const ACPX_PERMISSION_MODES = [ "approve-all", "approve-paperclip", @@ -42,8 +42,8 @@ export function acpxCapabilities( plan: profile.plans === "semantic-only" ? "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: controls?.steering === true, @@ -102,7 +102,7 @@ export function validateAcpxDriverConfig( return invalid( "agent", "invalid_agent", - "ACPX agent must be claude, codex, cursor, copilot, or pi.", + "ACPX agent must be claude, codex, grok, cursor, copilot, or pi.", ); } if (ACPX_CAPABILITY_PROFILES[agent].qualification !== "qualified") { diff --git a/packages/paperclip-runner/src/drivers/acpx/environment.ts b/packages/paperclip-runner/src/drivers/acpx/environment.ts index 3c63eb959e6..a78fdcbe268 100644 --- a/packages/paperclip-runner/src/drivers/acpx/environment.ts +++ b/packages/paperclip-runner/src/drivers/acpx/environment.ts @@ -2,6 +2,7 @@ import type { QualifiedAcpxAgent } from "./qualified-profiles.js"; export const ACPX_CREDENTIAL_BINDING_ENV = "PAPERCLIP_ACPX_CREDENTIAL_BINDING"; export const ACPX_CREDENTIAL_NAMES: Readonly> = { + grok: ["XAI_API_KEY"], pi: ["OPENROUTER_API_KEY"], cursor: ["CURSOR_API_KEY", "CURSOR_AUTH_TOKEN"], copilot: ["COPILOT_GITHUB_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 0fd5dbcbabd..0823d6579aa 100644 --- a/packages/paperclip-runner/src/drivers/acpx/installation-integrity.ts +++ b/packages/paperclip-runner/src/drivers/acpx/installation-integrity.ts @@ -344,7 +344,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`, ); @@ -529,7 +535,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 { @@ -877,6 +883,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") @@ -1728,7 +1747,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;`, @@ -2058,3 +2077,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/profile-installation.ts b/packages/paperclip-runner/src/drivers/acpx/profile-installation.ts index 165f97298af..a78aa47c6c5 100644 --- a/packages/paperclip-runner/src/drivers/acpx/profile-installation.ts +++ b/packages/paperclip-runner/src/drivers/acpx/profile-installation.ts @@ -3,7 +3,7 @@ import { verifyQualifiedAcpxInstallation, type VerifiedAcpxInstallation } from " /** Closed build-owned registry. Provider branches add their pinned installations here. */ export async function verifyAcpxProfileInstallation(profile: QualifiedAcpxProfile): Promise { - if (profile.agent !== "claude" && profile.agent !== "codex") { + if (profile.agent !== "claude" && profile.agent !== "codex" && profile.agent !== "grok") { throw new Error(`ACPX ${profile.agent} verified candidate distribution is not installed in this build`); } return verifyQualifiedAcpxInstallation(profile); 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 4efda04d48a..f8b5222d2bc 100644 --- a/packages/paperclip-runner/src/drivers/acpx/qualified-profiles.ts +++ b/packages/paperclip-runner/src/drivers/acpx/qualified-profiles.ts @@ -33,6 +33,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, @@ -111,7 +119,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" && profile.modelPolicy !== "explicit-provider-verified" && requestedModel !== profile.qualificationModel) { + if (agent !== "claude" && agent !== "grok" && profile.modelPolicy !== "explicit-provider-verified" && 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 f892f157ba1..4e56542126d 100644 --- a/packages/paperclip-runner/src/drivers/acpx/runtime-host.test.ts +++ b/packages/paperclip-runner/src/drivers/acpx/runtime-host.test.ts @@ -879,6 +879,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 7d45e84567d..ec3229985b4 100644 --- a/packages/paperclip-runner/src/drivers/acpx/runtime-host.ts +++ b/packages/paperclip-runner/src/drivers/acpx/runtime-host.ts @@ -1,6 +1,7 @@ import { dirname, join } from "node:path"; import { assertAcpxProfileEnvironment, assertAcpxProfileWorkspace, classifyAcpxProfileError, verifyAcpxProfileInstallation } from "./profile-installation.js"; import { createAcpxRuntimeSkillLease } from "./runtime-skill-lease.js"; +import { stageManagedGrokCredential } from "./grok-credentials.js"; import { claudeNativeSkillPrompt } from "./native-skill-prompt.js"; import { nativeMcpLaunchBinding } from "../native-mcp.js"; @@ -357,6 +358,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; @@ -429,6 +431,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, @@ -466,7 +475,7 @@ export class AcpxRuntimeHost { PAPERCLIP_PI_READ_ROOTS: JSON.stringify([...(options.providerPolicy?.readRoots ?? []), ...skills.readRoots]), }); } - 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. @@ -595,6 +604,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 c083366b8c2..fb80619a55d 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) => { @@ -367,13 +385,14 @@ describe("ACPX runtime sandbox", () => { ); }); -async function sandboxFixture(agent: "pi" | "claude" | "codex" | "cursor" | "copilot") { +async function sandboxFixture(agent: "pi" | "claude" | "codex" | "grok" | "cursor" | "copilot") { 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 cf0229fd2c2..8ecdbc88ede 100644 --- a/packages/paperclip-runner/src/drivers/acpx/runtime-sandbox.ts +++ b/packages/paperclip-runner/src/drivers/acpx/runtime-sandbox.ts @@ -401,6 +401,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"), @@ -454,6 +475,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/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 f1d7a1d37ce..8eef8f295fb 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"; it.each(["pi", "cursor", "copilot"] as const)("requires separately bound evaluation opt-in for %s", async (acpxAgent) => { const service = new CapabilityLiveSessionService(); @@ -766,20 +767,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 () => { @@ -952,6 +963,9 @@ describe("Capability live runnerd and Codex session", () => { await session.sendMessage("Orient to the assigned task."); expect(outcomes.map((outcome) => outcome.success)).toEqual([false, false, false, true, false, false]); expect(session.snapshot().semanticResult).toMatchObject(result); + const completionEvidence = session.snapshot().evidence.filter((entry) => entry.data.operationId === "paperclip_finish"); + expect(completionEvidence.map((entry) => entry.kind)).toEqual(["tool_call", "tool_result"]); + expect(completionEvidence[1]?.data.beforeRevision).toBe(completionEvidence[1]?.data.afterRevision); expect(session.mockState().tasks).toEqual(before.tasks); expect(session.snapshot().terminalTurns).toHaveLength(1); expect(session.snapshot().authorizationRecords).toContainEqual(expect.objectContaining({ operationId: "create_task", allowed: false })); diff --git a/packages/paperclip-runner/src/live/live-session.ts b/packages/paperclip-runner/src/live/live-session.ts index ecf16f24320..d24995b5ec0 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"; @@ -648,7 +649,7 @@ export function assertCapabilityLiveSessionSnapshot( } if (provider === "acpx") { const agent = config.acpxAgent; - if (agent !== "pi" && agent !== "claude" && agent !== "codex" && agent !== "cursor" && agent !== "copilot") { + if (agent !== "pi" && agent !== "claude" && agent !== "codex" && agent !== "grok" && agent !== "cursor" && agent !== "copilot") { throw new Error("capability_live_checkpoint_corrupt: invalid config.acpxAgent"); } const expected = resolveQualifiedAcpxProfile(agent, text(config.requestedModel)); @@ -1710,7 +1711,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(); @@ -1853,6 +1855,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`, @@ -2605,10 +2611,16 @@ export class CapabilityLiveSession { if (this.#semanticResult !== null && JSON.stringify(this.#semanticResult) !== JSON.stringify(result)) { return rejectedCodexToolCall("A different native completion report was already accepted for this turn."); } + const runResult = liveRunResultFeedback(operationId, request.params.arguments, LIVE_COMPLETION_CONTRACT.revision); + if (!runResult?.ok) { + return rejectedCodexToolCall("Native completion does not satisfy the current run result contract."); + } this.#semanticResult = result; + const revision = this.#port.snapshot().revision; + this.#appendEvidence("tool_call", turnId, { callId, operationId, input: jsonValue(request.params.arguments), beforeRevision: revision }); + this.#appendEvidence("tool_result", turnId, { callId, operationId, result: jsonValue(runResult), beforeRevision: revision, afterRevision: revision }); await this.#persist(); - return { success: true, contentItems: [{ type: "inputText", text: - "Native run report accepted. This does not change mock task state. End this provider turn after your final response; task mutations require their separately authorized semantic tools." }] }; + return this.#codexToolResponse(runResult); } if (operationId === DISCOVER_TOOL) { const args = record(request.params.arguments); 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 08b79ca7dcb..bf250a4cb72 100644 --- a/packages/paperclip-runner/src/live/runnerd-codex-transport.test.ts +++ b/packages/paperclip-runner/src/live/runnerd-codex-transport.test.ts @@ -1114,6 +1114,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 = [ { @@ -2206,6 +2230,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 ca83ca1221b..0609e957bc8 100644 --- a/packages/paperclip-runner/src/live/runnerd-codex-transport.ts +++ b/packages/paperclip-runner/src/live/runnerd-codex-transport.ts @@ -679,7 +679,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; @@ -702,7 +702,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; @@ -1331,10 +1331,10 @@ export function expandRunnerdCanonicalNotifications( const events = Array.isArray(payload.events) ? payload.events : [payload]; return events.map((event) => { const params = record(event); - // A coalesced envelope can contain both reasoning and visible messages. - // Classify each payload after expansion, never using the envelope's kind. + // A coalesced envelope can contain different event kinds. Classify each + // payload after expansion, never using the envelope's kind. return { - method: eventType === "item.delta" + method: eventType ? runnerdCanonicalNotificationMethod(eventType, params) ?? method : method, params, @@ -1352,6 +1352,9 @@ 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" @@ -3143,6 +3146,8 @@ export function createCapabilityRunnerdProviderEnvironment(input: { input.options.acpxAgent ?? "codex", ).env, ...(credentialBinding === undefined ? {} : { [ACPX_CREDENTIAL_BINDING_ENV]: credentialBinding }), + ...(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 @@ -3285,7 +3290,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; @@ -3296,7 +3301,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; } @@ -4315,8 +4320,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, }); @@ -5512,6 +5527,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); @@ -6687,6 +6704,7 @@ function openedThreadModelProvider( if (provider === "aws_agentcore") return "aws"; if (provider === "acpx") { if (acpxAgent === "pi") return "openrouter"; + if (acpxAgent === "grok") return "xai"; if (acpxAgent === "claude") return "anthropic"; if (acpxAgent === "cursor") return "cursor"; if (acpxAgent === "copilot") return "github"; 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 686bb7498dd..2d535d0b329 100644 --- a/patches/acpx@0.13.1.patch +++ b/patches/acpx@0.13.1.patch @@ -1,7 +1,8 @@ diff --git a/dist/client-CxNllqui.d.ts b/dist/client-CxNllqui.d.ts +index 5e2113a..b7b5151 100644 --- a/dist/client-CxNllqui.d.ts +++ b/dist/client-CxNllqui.d.ts -@@ -135,6 +135,7 @@ +@@ -135,6 +135,7 @@ declare class AcpClient { private throwPromptPermissionFailureIfPresent; setSessionMode(sessionId: string, modeId: string): Promise; setSessionConfigOption(sessionId: string, configId: string, value: string): Promise; @@ -10,9 +11,10 @@ diff --git a/dist/client-CxNllqui.d.ts b/dist/client-CxNllqui.d.ts private setSessionModelThroughConfig; private setSessionModelThroughLegacyMethod; diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js +index d454fd7..4312cdd 100644 --- a/dist/live-checkpoint-BSIrfgVo.js +++ b/dist/live-checkpoint-BSIrfgVo.js -@@ -1068,6 +1068,7 @@ +@@ -1068,6 +1068,7 @@ function serializeSessionRecordForDisk(record) { last_agent_disconnect_reason: canonical.lastAgentDisconnectReason, protocol_version: canonical.protocolVersion, agent_capabilities: canonical.agentCapabilities, @@ -20,7 +22,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js title: canonical.title, messages: canonical.messages, updated_at: canonical.updated_at, -@@ -1239,7 +1240,8 @@ +@@ -1239,7 +1240,8 @@ function parseRequestTokenUsage(raw) { for (const [key, value] of Object.entries(record)) { const parsed = parseTokenUsage(value); if (parsed == null) return null; @@ -30,7 +32,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js } return usage; } -@@ -1541,6 +1543,7 @@ +@@ -1541,6 +1543,7 @@ function parseSessionRecord(raw) { lastAgentDisconnectReason: optionals.lastAgentDisconnectReason, protocolVersion: typeof record.protocol_version === "number" ? record.protocol_version : void 0, agentCapabilities: asRecord$4(record.agent_capabilities), @@ -38,7 +40,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js title: conversation.title, messages: conversation.messages, updated_at: conversation.updated_at, -@@ -1661,7 +1664,7 @@ +@@ -1661,7 +1664,7 @@ const ZED_TAG_KEYS = /* @__PURE__ */ new Set([ "RedactedThinking", "ToolUse" ]); @@ -47,7 +49,20 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js const OPAQUE_VALUE_PATHS = /* @__PURE__ */ new Set([ "agent_capabilities", "messages.Agent.content.ToolUse.input", -@@ -3135,8 +3138,18 @@ +@@ -3104,10 +3107,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 +3138,21 @@ function promotePrefixedAuthEnvironment(env) { } return protectedKeys; } @@ -58,17 +73,20 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js + 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 +3191,10 @@ +@@ -3178,10 +3194,10 @@ function resolveConfiguredAuthCredential(methodId, authCredentials) { const configCredentials = authCredentials ?? {}; return configCredentials[methodId] ?? configCredentials[toEnvToken(methodId)]; } @@ -81,7 +99,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js stdio: [ "pipe", "pipe", -@@ -3961,10 +3974,13 @@ +@@ -3961,10 +3977,13 @@ function resolveClientCapabilities(params) { ...params.elicitationModes.includes("url") ? { url: {} } : {} } } : {} }; @@ -97,7 +115,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js }; } function hasResponseField(response, field) { -@@ -4067,6 +4083,27 @@ +@@ -4067,6 +4086,27 @@ function createNdJsonMessageStream(agentCommand, output, input) { } }) }; } @@ -125,7 +143,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js var AcpClient = class { options; connection; -@@ -4105,7 +4142,9 @@ +@@ -4105,7 +4145,9 @@ var AcpClient = class { cwd: asAbsoluteCwd(options.cwd), authPolicy: options.authPolicy ?? "skip", permissionPolicy: snapshotPermissionPolicy(options.permissionPolicy), @@ -136,7 +154,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js }; this.eventHandlers = { onAcpMessage: this.options.onAcpMessage, -@@ -4216,9 +4255,21 @@ +@@ -4216,9 +4258,21 @@ var AcpClient = class { this.lastAgentExit = void 0; this.lastKnownPid = child.pid ?? void 0; this.attachAgentLifecycleObservers(child); @@ -158,7 +176,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js if (!this.options.verbose) return; process.stderr.write(chunk); }); -@@ -4226,6 +4277,7 @@ +@@ -4226,6 +4280,7 @@ var AcpClient = class { const output = Readable.toWeb(child.stdout); const stream = this.createTappedStream(createNdJsonMessageStream(this.options.agentCommand, input, output)); const connection = this.createConnection(stream, launch); @@ -166,7 +184,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js connection.signal.addEventListener("abort", () => { this.recordAgentExit("connection_close", child.exitCode ?? null, child.signalCode ?? null); }, { once: true }); -@@ -4253,7 +4305,12 @@ +@@ -4253,7 +4308,12 @@ var AcpClient = class { geminiAcp: isGeminiAcpCommand(spawnCommand, args), copilotAcp: isCopilotAcpCommand(spawnCommand, args), claudeAcp: isClaudeAcpCommand(spawnCommand, args), @@ -180,7 +198,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js }; } logAgentLaunch(plan) { -@@ -4279,10 +4336,17 @@ +@@ -4279,10 +4339,17 @@ var AcpClient = class { } async spawnAgentProcess(plan) { const spawnCommand = buildAgentSpawnCommand(plan.spawnCommand, plan.args, process.platform, plan.spawnOptions.env); @@ -200,7 +218,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js try { await waitForSpawn$1(spawnedChild); } catch (error) { -@@ -4293,10 +4357,10 @@ +@@ -4293,10 +4360,10 @@ var AcpClient = class { createConnection(stream, launch) { const app = client({ name: "acpx" }).onNotification(methods.client.session.update, async ({ params }) => { await this.handleSessionUpdate(params); @@ -214,7 +232,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js }).onRequest(methods.client.fs.readTextFile, async ({ params }) => { return await this.handleReadTextFile(params); }).onRequest(methods.client.fs.writeTextFile, async ({ params }) => { -@@ -4312,6 +4376,11 @@ +@@ -4312,6 +4379,11 @@ var AcpClient = class { }).onRequest(methods.client.terminal.release, async ({ params }) => { return await this.handleReleaseTerminal(params); }); @@ -226,7 +244,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js if (launch.devinAcp) app.onRequest("_cognition.ai/request_diagnostics", (params) => { return params && typeof params === "object" && !Array.isArray(params) ? params : {}; }, async () => ({})); -@@ -4333,12 +4402,12 @@ +@@ -4333,12 +4405,12 @@ var AcpClient = class { async initializeProtocolConnection(connection, launch) { const initializePromise = connection.initialize({ protocolVersion: PROTOCOL_VERSION, @@ -241,7 +259,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js clientInfo: resolveClientInfo(launch.devinAcp) }); const initialized = launch.geminiAcp ? await withTimeout(initializePromise, resolveGeminiAcpStartupTimeoutMs()) : await initializePromise; -@@ -4358,6 +4427,36 @@ +@@ -4358,6 +4430,36 @@ var AcpClient = class { throw normalizedError; } createTappedStream(base) { @@ -278,7 +296,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js const onAcpMessage = () => this.eventHandlers.onAcpMessage; const onAcpOutputMessage = () => this.eventHandlers.onAcpOutputMessage; const elicitationRequestIds = /* @__PURE__ */ new Set(); -@@ -4367,8 +4466,9 @@ +@@ -4367,8 +4469,9 @@ var AcpClient = class { const shouldSuppressInboundReplaySessionUpdate = (message) => { return this.suppressReplaySessionUpdateMessages && isSessionUpdateNotification(message); }; @@ -289,7 +307,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js if (requestId !== void 0) { elicitationRequestIds.add(requestId); return; -@@ -4379,6 +4479,7 @@ +@@ -4379,6 +4482,7 @@ var AcpClient = class { } }; return { @@ -297,7 +315,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js readable: new ReadableStream({ async start(controller) { const reader = base.readable.getReader(); try { -@@ -4387,10 +4488,16 @@ +@@ -4387,10 +4491,16 @@ var AcpClient = class { if (done) break; if (!value) continue; observeInbound(value); @@ -315,7 +333,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js reader.releaseLock(); controller.close(); } -@@ -4403,10 +4510,20 @@ +@@ -4403,10 +4513,20 @@ var AcpClient = class { onAcpOutputMessage()?.("outbound", message); onAcpMessage()?.("outbound", message); } @@ -336,7 +354,37 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js writer.releaseLock(); } } }) -@@ -4915,9 +5032,9 @@ +@@ -4422,7 +4542,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 +4987,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 +5013,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) { +@@ -4915,9 +5038,9 @@ var AcpClient = class { await connection.authenticate({ methodId: selected.methodId }); this.log(`authenticated with method ${selected.methodId} (${selected.source})`); } @@ -348,7 +396,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js if (hostResponse) return hostResponse; const { response, recorded } = await this.resolvePermissionRequestFromMode(params); if (!recorded) { -@@ -4926,14 +5043,49 @@ +@@ -4926,14 +5049,49 @@ var AcpClient = class { } return response; } @@ -400,7 +448,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js })).then((response) => ({ kind: "response", response -@@ -4970,7 +5122,7 @@ +@@ -4970,7 +5128,7 @@ var AcpClient = class { isElicitationSessionCancelling(sessionId) { return this.closing || this.cancellingSessionIds.has(sessionId); } @@ -409,7 +457,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js if (!this.options.onPermissionRequest) return; const signal = this.cancellationSignalForSession(params.sessionId); try { -@@ -4978,7 +5130,7 @@ +@@ -4978,7 +5136,7 @@ var AcpClient = class { sessionId: params.sessionId, raw: params, inferredKind: inferToolKind(params) @@ -418,7 +466,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js return this.hostPermissionDecisionResponse(params, signal, decision); } catch (error) { return this.hostPermissionErrorResponse(params, signal, error); -@@ -5028,6 +5180,12 @@ +@@ -5028,6 +5186,12 @@ var AcpClient = class { attachAgentLifecycleObservers(child) { child.once("exit", (exitCode, signal) => { this.recordAgentExit("process_exit", exitCode, signal); @@ -431,7 +479,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js }); child.once("close", (exitCode, signal) => { this.recordAgentExit("process_close", exitCode, signal); -@@ -5771,10 +5929,20 @@ +@@ -5771,10 +5935,20 @@ function recordSessionUpdate(conversation, state, notification, timestamp = isoN trimConversationForRuntime(conversation); return acpx; } @@ -454,7 +502,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js updateConversationTimestamp(conversation, timestamp); trimConversationForRuntime(conversation); return true; -@@ -6435,6 +6603,27 @@ +@@ -6435,6 +6609,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; @@ -482,7 +530,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js async function runPromptTurn(params) { try { const promptPromise = params.client.prompt(params.sessionId, params.prompt, params.onPromptRequestStarted, params.onElicitation); -@@ -6444,7 +6633,21 @@ +@@ -6444,7 +6639,21 @@ async function runPromptTurn(params) { idleMs: SESSION_REPLY_IDLE_MS, timeoutMs: SESSION_REPLY_DRAIN_TIMEOUT_MS }).catch(() => {}); @@ -504,7 +552,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js return { stopReason: response.stopReason, source: "rpc" -@@ -6518,4 +6721,4 @@ +@@ -6518,4 +6727,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 }; @@ -512,6 +560,7 @@ diff --git a/dist/live-checkpoint-BSIrfgVo.js b/dist/live-checkpoint-BSIrfgVo.js \ 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 e8102ac..d3a9ba6 100644 --- a/dist/runtime.d.ts +++ b/dist/runtime.d.ts @@ -1,7 +1,8 @@ @@ -524,7 +573,7 @@ diff --git a/dist/runtime.d.ts b/dist/runtime.d.ts //#region src/agent-registry.d.ts declare const DEFAULT_AGENT_NAME = "codex"; //#endregion -@@ -44,6 +45,7 @@ +@@ -44,6 +45,7 @@ type AcpRuntimeTurnAttachment = { data: string; }; type AcpRuntimeTurnInput = { @@ -532,7 +581,7 @@ diff --git a/dist/runtime.d.ts b/dist/runtime.d.ts handle: AcpRuntimeHandle; text: string; attachments?: AcpRuntimeTurnAttachment[]; -@@ -141,6 +143,11 @@ +@@ -141,6 +143,11 @@ type AcpTextDeltaOriginMeta = { kind?: string; source?: string; }; @@ -544,7 +593,7 @@ diff --git a/dist/runtime.d.ts b/dist/runtime.d.ts type AcpRuntimeEvent = { type: "text_delta"; text: string; -@@ -186,6 +193,10 @@ +@@ -186,6 +193,10 @@ type AcpRuntimeEvent = { * non-null `input` schema. */ availableCommands?: AcpRuntimeAvailableCommand[]; @@ -555,7 +604,7 @@ diff --git a/dist/runtime.d.ts b/dist/runtime.d.ts } | { type: "tool_call"; text: string; -@@ -312,7 +323,49 @@ +@@ -312,7 +323,49 @@ type AcpRuntimeOptions = { elicitationModes?: readonly AcpElicitationMode[]; onPermissionRequest?: (req: AcpPermissionRequest, ctx: { signal: AbortSignal; @@ -605,7 +654,7 @@ diff --git a/dist/runtime.d.ts b/dist/runtime.d.ts }; type AcpFileSessionStoreOptions = { stateDir: string; -@@ -369,6 +422,7 @@ +@@ -369,6 +422,7 @@ declare class AcpRuntimeManager { private createAndSaveRuntimeRecord; private retainInitializedSessionOwner; startTurn(input: { @@ -613,7 +662,7 @@ diff --git a/dist/runtime.d.ts b/dist/runtime.d.ts handle: AcpRuntimeHandle; text: string; attachments?: AcpRuntimeTurnAttachment[]; -@@ -414,6 +468,7 @@ +@@ -414,6 +468,7 @@ declare class AcpRuntimeManager { private cleanupRuntimeTurn; private finalizeRuntimeTurnRecord; runTurn(input: { @@ -622,9 +671,10 @@ diff --git a/dist/runtime.d.ts b/dist/runtime.d.ts text: string; attachments?: AcpRuntimeTurnAttachment[]; diff --git a/dist/runtime.js b/dist/runtime.js +index a1f4a70..fe2484d 100644 --- a/dist/runtime.js +++ b/dist/runtime.js -@@ -371,7 +371,7 @@ +@@ -371,7 +371,7 @@ const PROMPT_EVENT_PARSERS = { current_mode_update: (payload) => statusUpdateEvent("current_mode_update", payload), config_option_update: (payload) => statusUpdateEvent("config_option_update", payload), session_info_update: (payload) => statusUpdateEvent("session_info_update", payload), @@ -633,11 +683,10 @@ diff --git a/dist/runtime.js b/dist/runtime.js client_operation: clientOperationEvent, update: updateStatusEvent, done: () => null, -@@ -423,6 +423,34 @@ - tag: "available_commands_update", +@@ -424,6 +424,34 @@ function availableCommandsUpdateEvent(payload) { availableCommands }; -+} + } +function persistedGoalCapability(goal) { + if (!isRecord(goal) || goal.version !== 1 || goal.controlMethod !== "_session/goal" || !Array.isArray(goal.actions)) return; + const actions = goal.actions.filter((action) => ["set", "pause", "resume", "clear"].includes(action)); @@ -665,10 +714,11 @@ diff --git a/dist/runtime.js b/dist/runtime.js + entries.push({ content, status, ...priority ? { priority } : {} }); + } + return { type: "plan", tag: "plan", entries }; - } ++} function normalizeUsageCost(value) { if (!isRecord(value)) return; -@@ -812,7 +840,21 @@ + const amount = asOptionalFiniteNumber(value.amount); +@@ -812,7 +840,21 @@ var AcpRuntimeManager = class { this.deps = deps; } createClient(options) { @@ -691,7 +741,7 @@ diff --git a/dist/runtime.js b/dist/runtime.js } createSessionOwner(input) { const owner = { -@@ -821,12 +863,16 @@ +@@ -821,12 +863,16 @@ var AcpRuntimeManager = class { pendingSessionUpdates: [] }; input.client.setEventHandlers({ @@ -708,7 +758,7 @@ diff --git a/dist/runtime.js b/dist/runtime.js const active = owner.activeTurn; if (active) { const { task, turn } = active; -@@ -863,6 +909,9 @@ +@@ -863,6 +909,9 @@ var AcpRuntimeManager = class { projection.checkpoint.request(); } routeOwnedClientOperation(owner, operation) { @@ -718,7 +768,7 @@ diff --git a/dist/runtime.js b/dist/runtime.js const active = owner.activeTurn; if (!active) return; const { task, turn } = active; -@@ -1008,6 +1057,8 @@ +@@ -1008,6 +1057,8 @@ var AcpRuntimeManager = class { record.closedAt = void 0; record.protocolVersion = owner.client.initializeResult?.protocolVersion; record.agentCapabilities = owner.client.initializeResult?.agentCapabilities; @@ -727,7 +777,7 @@ diff --git a/dist/runtime.js b/dist/runtime.js applyLifecycleSnapshotToRecord(record, owner.client.getAgentLifecycleSnapshot()); } async finishBufferedOwnerControl(owner, record) { -@@ -1086,6 +1137,11 @@ +@@ -1086,6 +1137,11 @@ var AcpRuntimeManager = class { record.closed = false; record.closedAt = void 0; this.closingActiveRecords.delete(record.acpxRecordId); @@ -739,7 +789,7 @@ diff --git a/dist/runtime.js b/dist/runtime.js await this.options.sessionStore.save(record); return record; } -@@ -1149,6 +1205,8 @@ +@@ -1149,6 +1205,8 @@ var AcpRuntimeManager = class { this.closingActiveRecords.delete(record.acpxRecordId); record.protocolVersion = client.initializeResult?.protocolVersion; record.agentCapabilities = client.initializeResult?.agentCapabilities; @@ -748,7 +798,7 @@ diff --git a/dist/runtime.js b/dist/runtime.js applyConfigOptionsToRecord(record, session.sessionResult); const modelApplication = await applyRequestedModelIfAdvertised({ client, -@@ -1301,6 +1359,7 @@ +@@ -1301,6 +1359,7 @@ var AcpRuntimeManager = class { client: turn.client, sessionId, prompt: task.promptInput, @@ -756,7 +806,7 @@ diff --git a/dist/runtime.js b/dist/runtime.js timeoutMs: task.input.timeoutMs ?? this.options.timeoutMs, conversation: turn.conversation, promptMessageId: turn.promptMessageId, -@@ -1469,6 +1528,10 @@ +@@ -1469,6 +1528,10 @@ var AcpRuntimeManager = class { setSessionConfigOption: async (configId, value) => { return (await task.state.activeController.setResolvedSessionConfigOption(configId, value)).response; }, @@ -767,7 +817,7 @@ diff --git a/dist/runtime.js b/dist/runtime.js setResolvedSessionConfigOption: async (configId, value) => await this.setRuntimeResolvedSessionConfigOption(task, turn, configId, value) }; } -@@ -1575,6 +1638,8 @@ +@@ -1575,6 +1638,8 @@ var AcpRuntimeManager = class { reconcileAgentSessionId(turn.record, turn.record.agentSessionId); turn.record.protocolVersion = turn.client.initializeResult?.protocolVersion; turn.record.agentCapabilities = turn.client.initializeResult?.agentCapabilities; @@ -776,11 +826,10 @@ diff --git a/dist/runtime.js b/dist/runtime.js turn.record.acpx = turn.acpxState; applyConversation(turn.record, turn.conversation); applyLifecycleSnapshotToRecord(turn.record, turn.client.getAgentLifecycleSnapshot()); -@@ -1706,6 +1771,17 @@ - applyDesiredConfigOptionToRecord(connectedRecord, configId, value); +@@ -1707,6 +1772,17 @@ var AcpRuntimeManager = class { }); await this.options.sessionStore.save(result.record); -+ } + } + async requestExtension(input) { + const recordId = input.handle.acpxRecordId ?? input.handle.sessionKey; + return await this.withManagerLock(this.runtimeOperationLocks, recordId, async () => { @@ -791,10 +840,11 @@ diff --git a/dist/runtime.js b/dist/runtime.js + return await client.requestExtension(input.method, input.params); + })).value; + }); - } ++ } async cancel(handle) { await this.activeControllers.get(handle.acpxRecordId ?? handle.sessionKey)?.requestCancelActivePrompt(); -@@ -2055,6 +2131,7 @@ + } +@@ -2055,6 +2131,7 @@ var AcpxRuntime = class { const turnPromise = this.getManager().then((manager) => manager.startTurn({ handle, text: input.text, @@ -802,7 +852,7 @@ diff --git a/dist/runtime.js b/dist/runtime.js attachments: input.attachments, mode: input.mode, sessionMode: state.mode, -@@ -2087,6 +2164,7 @@ +@@ -2087,6 +2164,7 @@ var AcpxRuntime = class { yield* (await this.getManager()).runTurn({ handle, text: input.text, @@ -810,11 +860,10 @@ diff --git a/dist/runtime.js b/dist/runtime.js attachments: input.attachments, mode: input.mode, sessionMode: state.mode, -@@ -2118,6 +2196,14 @@ - async setConfigOption(input) { +@@ -2119,6 +2197,14 @@ var AcpxRuntime = class { const { handle, state } = this.resolveManagerHandle(input.handle); await (await this.getManager()).setConfigOption(handle, input.key, input.value, state.mode); -+ } + } + async requestExtension(input) { + const { handle, state } = this.resolveManagerHandle(input.handle); + return await (await this.getManager()).requestExtension({ @@ -822,10 +871,11 @@ diff --git a/dist/runtime.js b/dist/runtime.js + handle, + sessionMode: input.sessionMode ?? state.mode + }); - } ++ } async cancel(input) { const { handle } = this.resolveManagerHandle(input.handle); -@@ -2178,4 +2264,4 @@ + await (await this.getManager()).cancel(handle); +@@ -2178,4 +2264,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 }; @@ -833,6 +883,7 @@ diff --git a/dist/runtime.js b/dist/runtime.js \ 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 c3da164..5ccc8dd 100644 --- a/dist/session-options-DwRDODlr.d.ts +++ b/dist/session-options-DwRDODlr.d.ts @@ -1,4 +1,5 @@ @@ -841,7 +892,7 @@ diff --git a/dist/session-options-DwRDODlr.d.ts b/dist/session-options-DwRDODlr. //#region src/prompt-content.d.ts type PromptInput = ContentBlock[]; //#endregion -@@ -27,6 +28,8 @@ +@@ -27,6 +28,8 @@ type AcpElicitationContext = { requestId: JsonRpcId; /** Aborts with the request itself or its owning prompt turn/session. */ signal: AbortSignal; @@ -850,7 +901,7 @@ diff --git a/dist/session-options-DwRDODlr.d.ts b/dist/session-options-DwRDODlr. }; type AcpElicitationResponseMeta = { _meta?: Record | null; -@@ -116,6 +119,35 @@ +@@ -116,6 +119,35 @@ type AcpClientOptions = { }; env?: Record; }; @@ -886,7 +937,7 @@ diff --git a/dist/session-options-DwRDODlr.d.ts b/dist/session-options-DwRDODlr. onAcpMessage?: (direction: AcpMessageDirection, message: AcpJsonRpcMessage) => void; onAcpOutputMessage?: (direction: AcpMessageDirection, message: AcpJsonRpcMessage) => void; onSessionUpdate?: (notification: SessionNotification) => void; -@@ -123,6 +155,8 @@ +@@ -123,6 +155,8 @@ type AcpClientOptions = { onPermissionEscalation?: (event: PermissionEscalationEvent) => void; onPermissionRequest?: (req: AcpPermissionRequest, ctx: { signal: AbortSignal; @@ -895,7 +946,7 @@ diff --git a/dist/session-options-DwRDODlr.d.ts b/dist/session-options-DwRDODlr. }) => Promise; }; declare const SESSION_RECORD_SCHEMA: "acpx.session.v1"; -@@ -263,12 +297,15 @@ +@@ -263,12 +297,15 @@ type SessionRecord = { lastAgentDisconnectReason?: string; protocolVersion?: number; agentCapabilities?: AgentCapabilities; @@ -912,7 +963,7 @@ diff --git a/dist/session-options-DwRDODlr.d.ts b/dist/session-options-DwRDODlr. acpx?: SessionAcpxState; importedFrom?: SessionImportedFrom; }; -@@ -295,4 +332,4 @@ +@@ -295,4 +332,4 @@ type SessionAgentOptions = { }; //#endregion export { SessionRecord as _, AcpElicitationHandler as a, AcpElicitationResponse as c, AuthPolicy as d, McpServer$1 as f, PermissionStats as g, PermissionPolicy as h, AcpElicitationContext as i, AcpPermissionDecision as l, PermissionMode as m, SystemPromptOption as n, AcpElicitationMode as o, NonInteractivePermissionPolicy as p, AcpClientOptions as r, AcpElicitationRequest as s, SessionAgentOptions as t, AcpPermissionRequest as u, PromptInput as v }; diff --git a/server/src/__tests__/adapter-registry.test.ts b/server/src/__tests__/adapter-registry.test.ts index aaf5f7e7ce4..48539de9b48 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/__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/__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/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 56f0ab6d704..d4abb5249ed 100644 --- a/server/src/adapters/registry.ts +++ b/server/src/adapters/registry.ts @@ -414,7 +414,7 @@ const paperclipRunnerAdapter: ServerAdapterModule = { }; } 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( @@ -423,8 +423,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(), @@ -432,11 +432,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 { @@ -519,7 +519,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. Cursor, GitHub Copilot, and Pi are awaiting local and Daytona qualification and are not enabled for production runs. 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. Cursor, GitHub Copilot, and Pi are awaiting local and Daytona qualification and are not enabled for production runs. Managed providers use company-scoped qualified profiles, explicit retention acknowledgement, and spend limits.\n", getConfigSchema: () => ({ fields: [ { @@ -536,6 +536,11 @@ const paperclipRunnerAdapter: ServerAdapterModule = { ], hint: "Select a local provider, company-qualified managed provider, or an ACP agent.", }, + { + 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", label: "Codex permission mode", 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 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, }); } 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 8d2988eabb3..4ab8491af0a 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, @@ -83,6 +86,7 @@ type BackendFactoryOptions = { }; type RunnerTransportOptions = { + acpxRuntimeDirectory?: string; adoptExistingRunner?: { pid: number; isAlive: () => Promise | boolean }; stateDirectory?: string; runnerBinary?: string; @@ -180,6 +184,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") @@ -1041,6 +1051,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, @@ -1071,18 +1084,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), @@ -1831,6 +1847,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() @@ -5813,6 +5863,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) => { @@ -7741,6 +7895,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(); @@ -8424,7 +8621,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-"), ); @@ -8482,16 +8679,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"), @@ -8520,6 +8746,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, @@ -8946,6 +9183,7 @@ describe("runnerd provider runtime wiring", () => { it.each([ "quarantined", + "large control plane", "empty retry shell", "unsuspended current", "live runner", @@ -9144,6 +9382,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 }, ], }), @@ -9183,6 +9427,7 @@ describe("runnerd provider runtime wiring", () => { state.createTransport.mockClear(); const shouldRecover = [ "quarantined", + "large control plane", "empty retry shell", "unsuspended current", "active goal", @@ -9690,7 +9935,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( @@ -9751,6 +9996,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 5be5dffb351..76aa50204eb 100644 --- a/server/src/services/native-runtime/native-session-executor.ts +++ b/server/src/services/native-runtime/native-session-executor.ts @@ -1,4 +1,8 @@ import { resolveAcpxQualification } from "./acpx-qualification.js"; +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, @@ -277,7 +281,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 = [ @@ -392,6 +399,7 @@ type WarmNativeSession = { session: NativeSession; ownerToken: symbol; configDigest: string; + ownerScope: string; companyId: string; environmentId: string | null; busy: boolean; @@ -1462,6 +1470,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 ( @@ -1533,6 +1542,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 { @@ -1801,7 +1849,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", ), ); @@ -4074,7 +4124,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) { @@ -4131,7 +4181,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( @@ -4732,7 +4782,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"); }; @@ -4901,15 +4953,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 ) { @@ -4972,7 +5024,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"), ), @@ -5025,7 +5077,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"), ), @@ -5271,9 +5323,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}`, @@ -7178,7 +7234,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); } @@ -7248,6 +7317,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); @@ -7987,6 +8059,7 @@ async function executePaperclipNativeSessionWithinScope( (hasBrokerCapability && entry.credentialRunId !== input.execution.binding.runId); if ( + entry.closeOnReleaseReason !== undefined || entry.configDigest !== warmConfigDigest || entry.managedAiCredentialIdentity !== input.managedAiCredentialIdentity || credentialRunChanged || @@ -8351,6 +8424,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, @@ -9220,14 +9296,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", @@ -9251,6 +9330,7 @@ type RemoteProviderPackManifest = { path: string; sha256: string; }>>; artifacts: { + grokExecutable: { path: string; sha256: string }; nodeCommand: { path: string; sha256: string }; productionLock: { path: string; sha256: string }; opencodeCommand: { path: string; sha256: string }; @@ -9349,6 +9429,7 @@ export function readRemoteProviderPackManifest( ); } const artifactEntries = [ + ["Grok executable", payload.artifacts?.grokExecutable, REMOTE_PROVIDER_PACK_ARTIFACT_PATHS.grokExecutable], [ "provider Node", payload.artifacts?.nodeCommand, @@ -10830,7 +10911,7 @@ 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')", "for(const candidate of Object.values(manifest.payload.candidateProviders||{})){if(tree(path.join(root,candidate.path))!==candidate.sha256)throw new Error('candidate asset tree digest mismatch')}", "const version=process.versions.node.split('.').map(Number)", @@ -10838,7 +10919,7 @@ async function createRunnerdBackendWithinSessionClaim( "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({ @@ -12129,9 +12210,17 @@ async function createRunnerdBackendWithinSessionClaim( }, } : input.execution; - const effectiveRunnerEnvironmentBase: NodeJS.ProcessEnv = { - ...resolveNativeProviderEnvironment(input.execution.provider, input.runnerEnvironment), + const isGrok = input.execution.provider.kind === "acpx" && input.execution.provider.agent === "grok"; + let effectiveRunnerEnvironmentBase: NodeJS.ProcessEnv = { + ...(isGrok + ? input.runnerEnvironment ?? {} + : resolveNativeProviderEnvironment(input.execution.provider, input.runnerEnvironment)), }; + 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. @@ -12710,6 +12799,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 5d860b235f4..2a6f6ed12d9 100644 --- a/server/src/services/native-runtime/provider-profile.ts +++ b/server/src/services/native-runtime/provider-profile.ts @@ -17,6 +17,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; @@ -423,7 +424,7 @@ export function resolvePaperclipRunnerProviderProfile( } throw new PaperclipRunnerProviderProfileError("paperclip_runner_acpx_agent_unavailable", `${pendingAcpxProfile.label} is awaiting local and Daytona qualification. Its profile is not enabled for production runs.`); } - if (acpxAgent !== "claude" && acpxAgent !== "codex") { + if (acpxAgent !== "claude" && acpxAgent !== "codex" && acpxAgent !== "grok") { throw new PaperclipRunnerProviderProfileError( "paperclip_runner_acpx_agent_unavailable", "Paperclip Runner ACPX requires a qualified agent profile.", 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/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/tests/runner-e2e/FIXTURES.md b/tests/runner-e2e/FIXTURES.md index 9f86d56bc15..0506028c44e 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 18b55e5d27f..4a61128bd82 100644 --- a/tests/runner-e2e/README.md +++ b/tests/runner-e2e/README.md @@ -1081,6 +1081,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 57be4a20d54..6eaaee57436 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 90e7ef82f44..df9d1ccfeed 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([ - 30, 2, 8, 46, 23, 47, 52, 28, 18, 6, 6, 4, 42, 14, 10, 2, + 30, 16, 16, 2, 8, 46, 23, 47, 52, 28, 18, 6, 6, 4, 48, 16, 10, 2, ]); - expect(validateRunnerCatalog()).toHaveLength(338); - expect(new Set(runnerMatrix.map((entry) => entry.id)).size).toBe(338); + expect(validateRunnerCatalog()).toHaveLength(378); + expect(new Set(runnerMatrix.map((entry) => entry.id)).size).toBe(378); 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 90342929158..2977551f0b0 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" | "cursor" | "copilot" | "pi"; + acpxAgent?: "claude" | "codex" | "grok" | "cursor" | "copilot" | "pi"; qualificationCandidate?: RunnerProfileFixture["qualificationCandidate"]; supportedEnvironments?: readonly (typeof ENVIRONMENT_IDS)[number][]; modelQualification?: RunnerProfileFixture["modelQualification"]; @@ -174,7 +174,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" } @@ -189,7 +191,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. @@ -286,6 +288,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", @@ -962,6 +972,37 @@ export const runnerSuites: readonly RunnerSuiteFixture[] = [ expectedMatrixSize: 30, definitionMetadata: { version: 1, qualification: "pending", scheduling: "explicit-only", admission: "host-exact-candidate-and-model", authenticatedDiscoveryDate: "2026-09-28" }, }, + { + 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.", @@ -1079,7 +1120,7 @@ export const runnerSuites: readonly RunnerSuiteFixture[] = [ profiles: runnerProfiles, environments: runnerEnvironments, tasks: runnerTasks, - expectedMatrixSize: 42, + expectedMatrixSize: 48, }, { id: "local-session-integrity", @@ -1090,7 +1131,7 @@ export const runnerSuites: readonly RunnerSuiteFixture[] = [ profiles: runnerProfiles, environments: [localEnvironment], tasks: localIntegrityTasks, - expectedMatrixSize: 14, + expectedMatrixSize: 16, }, { id: "openrouter-model-breadth", diff --git a/tests/runner-e2e/daytona-image-content.ts b/tests/runner-e2e/daytona-image-content.ts index 45538962c49..46f4ea7dc1f 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 ad8d41d882f..8e91b74da88 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 3089433b10b..33d168def17 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|CURSOR|COPILOT|GITHUB|GH)(?:_|$)/; +const PROVIDER_SECRET_KEY = /^(?:OPENAI|ANTHROPIC|OPENROUTER|DAYTONA|XAI|GROK|CURSOR|COPILOT|GITHUB|GH)(?:_|$)/; export function runnerE2EServerControlPaths(temporaryRoot: string) { const controlDirectory = path.join(temporaryRoot, "control"); @@ -148,7 +148,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 0fb01b2412e..f4cf45f9cef 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 5710df1443b..af18974b8cd 100644 --- a/tests/runner-e2e/redaction.ts +++ b/tests/runner-e2e/redaction.ts @@ -15,11 +15,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 f8c4c5caeb9..29e29199f03 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", "CURSOR_AUTH_TOKEN", "COPILOT_GITHUB_TOKEN", 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/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/adapters/codex-local/config-fields.tsx b/ui/src/adapters/codex-local/config-fields.tsx index 50def1e07b3..bb737ee45ce 100644 --- a/ui/src/adapters/codex-local/config-fields.tsx +++ b/ui/src/adapters/codex-local/config-fields.tsx @@ -196,9 +196,10 @@ export function CodexLocalConfigFields({ > )} - {runnerManaged && runnerProvider === "acpx" && ( + {runnerManaged && runnerProvider === "acpx" && runnerSchemaValue("acpxAgent", "claude") !== "grok" && ( 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/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/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/lib/composer-draft.test.ts b/ui/src/lib/composer-draft.test.ts index 5b37088d002..e03cdfb7240 100644 --- a/ui/src/lib/composer-draft.test.ts +++ b/ui/src/lib/composer-draft.test.ts @@ -3,6 +3,8 @@ import { beforeEach, describe, expect, it } from "vitest"; import { clearDraft, loadDraft, + loadDraftRecoveryKey, + preserveDraftInTab, loadDraftAttachments, saveDraft, saveDraftAttachments, @@ -23,6 +25,48 @@ describe("task draft upload receipts", () => { 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. */ } 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, }, 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"]) { 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} />