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