Compare commits

...
47 Commits
Author SHA1 Message Date
Expo CI 34e8b45bad v24.13.0 2026-10-12 17:44:06 +00:00
Tomasz Mazur d1de958201 [build-tools] Close artifact upload streams before an upload finishes (#4609) 2026-10-12 17:58:38 +02:00
Tomasz Mazur 2b672fc07f [build-tools] Add a sandbox uploadArtifact command (#4563)
* [build-tools] Add a sandbox uploadArtifact command

* [build-tools] Add changelog entry for sandbox uploadArtifact

* [build-tools] Return BAD_REQUEST for refused sandbox artifact uploads

* [build-tools] Let sandbox uploadArtifact wait for the upload and report its status and download URL

* [build-tools] Wait up to 20 seconds for sandbox artifact uploads by default

* [build-tools] Count upload session creation toward the sandbox upload yield time

* [build-tools] Replace the shared withTimeoutAsync with abort signal timeouts for sandbox artifact uploads
2026-10-12 15:56:21 +02:00
Tomasz Mazur 8453efcfb6 [eas-build-job] Add uploadArtifact to the sandbox daemon protocol (#4562)
* [eas-build-job] Add uploadArtifact to the sandbox daemon protocol

* [eas-build-job] Add changelog entry for sandbox uploadArtifact

* [build-tools] Reject sandbox uploadArtifact calls until uploads are implemented

* [eas-build-job] Add yieldTimeMs, status and downloadUrl to sandbox uploadArtifact
2026-10-12 15:56:21 +02:00
Stanisław Chmiela 0456e8eea6 [build-tools] Download submission archives with fresh URLs (#4539)
* [build-tools] request fresh submission archive URLs

* [build-tools] link submission archive changelog entry

* [build-tools] simplify submission archive downloads

* [build-tools] validate submission download inputs together

* [build-tools] retry submission archive URL requests

* [build-tools] infer submission archive mutation types from schema
2026-10-12 12:20:34 +02:00
Szymon Świerk 0ec9a81552 [eas-cli] Don't hide inputting individual ASC API keys for submissions behind a flag (#4589)
* [eas-cli] Remove EAS_ENABLE_INDIVIDUAL_ASC_API_KEYS and enable individual ASC API keys for submissions

* [eas-cli] changelog entry about enabling individual ASC API keys for submissions

* [eas-cli] Point changelog entry to #4589
2026-10-12 11:27:55 +02:00
Szymon Świerk 3b4fedeee5 [eas-cli] Don't check for ASC API key validity on Apple servers for individual keys (#4588)
* [eas-cli] Don't check for ASC API key validity on Apple servers for individual keys

* [eas-cli] changelog entry about not checking for individual asc api keys validity on apple servers
2026-10-12 11:14:15 +02:00
Ash Wu 570617f467 [eas-cli] show avg and P75 run duration in workflow:insights (#4566)
* [eas-cli] regenerate GraphQL types

* [eas-cli] share the nullable insights metric helpers

Maestro insights kept NullableMetricSummary and its duration-with-trend
formatter private. Workflows insights needs the same pair for run durations,
so they move to insights/metrics.ts as toNullableMetricSummary and
formatNullableMetricWithTrend, which takes the value formatter as an
argument. Maestro output is unchanged.

* [eas-cli] show run durations in workflow:insights

A customer doing weekly CI health reporting asked for the average and P75
end-to-end workflow run duration, excluding canceled runs (ENG-27927). The
server now exposes avgDurationMs and p75DurationMs on the Workflows Insights
overview metrics and on each workflow node.

eas workflow:insights requests both, shows "Avg duration" and "P75 duration"
in the overview with a lower-is-better trend, adds the two columns to the
workflows table, and includes them in --json. Durations render as h/m/s;
a window with no non-canceled run shows n/a.
2026-10-12 16:51:41 +08:00
Ash Wu 8262aa34c6 [build-tools] Write readable lines to the eas/run_agent job log instead of raw JSON events (#4568) 2026-10-12 16:24:28 +08:00
1f72736126 [build-tools] Update Appium command list (#4607)
* [build-tools] Update Appium command list

* [build-tools] Add summaries for simulated hinge angle commands

---------

Co-authored-by: Expo Bot <[email protected]>
Co-authored-by: Stanisław Chmiela <[email protected]>
2026-10-12 08:41:42 +02:00
Ash Wu 3328b60f85 [build-tools] Support Codex in eas/run_agent (#4553)
Codex has no single switch for its built-in tools, so its shell, image,
apps, plugins and subagent features are turned off by name, it runs in a
read-only sandbox, and the MCP server's tools are pre-approved. Resuming
uses exec resume --last, which starts a new session when none was saved,
so the continuation prompt now repeats the task.
2026-10-12 14:39:11 +08:00
Ash Wu 4abb8601d5 [build-tools] Add the eas/run_agent step function (#4552)
www creates an agent run as a generic job with this single step. The step
validates its inputs, reads the prompt and the job token from the job
environment, takes the API and MCP URLs from the worker config, and hands
over to the agent runner.
2026-10-12 14:39:10 +08:00
Ash Wu 9a881d8366 [build-tools] Add the agent runner for Claude Code (#4551)
Installs the pinned Claude Code CLI into a private run directory, points
it at the Expo MCP server with no built-in tools and a minimal environment,
enforces the maximum duration, and resumes the CLI's session when its
credentials expire mid-run. Nothing calls it yet.
2026-10-12 14:39:10 +08:00
Ash Wu 657034a1a6 [build-tools] Add the agent run lease client (#4550)
Leases the provider credentials of an agent run from www with the job
token. Network failures and 5xx responses are retried; every 4xx is final,
as the route's contract says. Nothing calls it yet.
2026-10-12 14:39:10 +08:00
Douglas LowderandClaude Opus 5.5 bb98f5beef [eas-cli][local-build-plugin] Deprecate Node 20, use Node 22 in CI (#4579)
* [eas-cli][local-build-plugin] Deprecate Node 20, use Node 22 in CI

Print a deprecation warning from the eas-cli and local-build-plugin
bin scripts when running on Node.js < 22. Develop and test on Node.js
22: bump mise, drop 20.18.3 from the test matrix, and move to
@tsconfig/node22 and @types/node 22. Published engines are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-11 15:45:17 -07:00
Gabe DebesandClaude Opus 5.5 4aca8b1cc4 [build-tools] Look up ffmpeg on PATH instead of running it (#4582)
The first run of the preinstalled ffmpeg-full on
macos-tahoe-26.6-xcode-27.1 takes about 13 s while it loads its
libraries, and the ffmpeg setup waited for that `ffmpeg -version`.
Find ffmpeg on PATH the way device-session tools do, and run it once in
the background so its first real use does not pay the cold start.

Co-authored-by: Claude Opus 5.5 <[email protected]>
2026-10-09 11:31:50 -07:00
Tomasz Mazur 800a0e0893 [build-tools] Add a sandbox readFile command for text and image files (#4555) 2026-10-09 17:23:40 +02:00
Tomasz Mazur 2911b04ea1 [build-tools] Return sandbox daemon error codes (#4571) 2026-10-09 17:02:27 +02:00
Tomasz Mazur 50b381abe0 [eas-build-job] Add sandbox daemon error codes (#4570) 2026-10-09 17:02:26 +02:00
Gabe Debes c8696f6012 [build-tools] Install ffmpeg before starting Appium sessions (#4577) 2026-10-08 16:12:29 -07:00
Brent Vatne 45d795e076 [eas-cli] Remove obsolete appVersion runtime policy warning (#4375) 2026-10-08 15:02:35 -07:00
Expo CI 8e98a915b9 bump STAGING and PRODUCTION to 24.12.1 in cli-versions.json 2026-10-08 21:54:04 +00:00
Expo CI 73c65d3edb update CHANGELOG.md for v24.12.1 2026-10-08 21:51:34 +00:00
Expo CI 01e3be71aa v24.12.1 2026-10-08 21:49:04 +00:00
SJ Kim 991bbeae8c [eas-cli] Move the #4522 and #4483 changelog entries to their releases (#4576) 2026-10-08 17:43:35 -04:00
SJ Kim 6c342abb97 [eas-cli] Stop a simulator session created after Ctrl+C during create (ENG-27606) (#4522)
* [eas-cli] Log client request events for simulator sessions (ENG-27138)

* [eas-cli] Flush analytics and exit with 130 when Ctrl+C cancels a simulator session request (ENG-27138)

* [eas-cli] Add changelog entry for simulator request events (ENG-27138)

* [eas-cli] Bound the Ctrl+C analytics flush and tighten its tests (ENG-27138)

* [eas-cli] Stop a session created after Ctrl+C during create (ENG-27138)

* [eas-cli] Don't count an HTTP error answer as a failed simulator session request (ENG-27138)

* [eas-cli] Add changelog entry for #4522 (ENG-27606)

* [eas-cli] Keep the simulator request analytics wrapper with its only caller (ENG-27138)

* [eas-cli] Keep a failed Ctrl+C stop test from hanging later simulator tests (ENG-27606)

* [eas-cli] Accept an unsent create request when stopping a session after Ctrl+C
2026-10-08 16:28:09 -04:00
Stanisław Chmiela 1016aa35f3 [build-tools] Skip WDA cache for DeviceLab Runner versions (#4560)
# Why

<img width="487" height="329" alt="Zrzut ekranu 2026-10-7 o 10 29 04" src="https://github.com/user-attachments/assets/a63f0550-bf93-46ed-83c7-187faa2ac20b" />

# How

Skip WDA cache preparation for detected Runner versions 1.1.28 and later.

# Test Plan

CI should pass.
2026-10-08 14:16:53 +02:00
Stanisław Chmiela ceb8f846c2 [build-tools] Address Android app info review feedback (#4567)
# Why

I merged pull request without noticing review comments. #4514.

# How

Adhered to review comments. Added integration tests using real APK/AAB and real tools, fixed error code.

# Test Plan

CI should pass. Validated locally.
2026-10-08 12:10:43 +02:00
Stanisław Chmiela 53cb3a7263 [build-tools] Add typed Google Play API client (#4515)
* [build-tools] Add authenticated Google Play API client

* [build-tools] Remove Android fixture dependency from client tests

* [build-tools] Add typed Google Play API methods with separate OAuth transport

* [build-tools] Simplify Google Play client and remove retry changes

* [build-tools] Improve Google Play client error details

* [build-tools] Load Google Play tokens with service account keys

* [build-tools] Extract Google Play assertion helper

* [build-tools] Move token creation into GooglePlayAuthUtils

* [build-tools] Make assertion helper private and capitalize utility filename

* [build-tools] Remove upload support from Google Play client PR

* [build-tools] Improve Google Play client schema spacing

* [build-tools] Reuse Google Play release schema
2026-10-08 10:39:56 +02:00
Stanisław Chmiela 09188ab271 [build-tools] Add typed Google Play OAuth API client (#4542)
* [build-tools] Add typed Google Play OAuth API client

* [build-tools] Align Google Play OAuth errors and response parsing

* [build-tools] Include Google Play OAuth error diagnostics

* [build-tools] Keep OAuth diagnostics without response secrets
2026-10-08 10:30:18 +02:00
Stanisław Chmiela c4ffc1605e [build-tools] Add Android artifact inspection function (#4514)
* [build-tools] Add Android artifact inspection function

* [build-tools] Use Android tools to inspect app artifacts

* [build-tools] Read Android info from downloaded binary paths

* [build-tools] Detect Android type before reading package name

* [build-tools] Read APK package name with aapt2 packagename

* [build-tools] Keep Android package readers explicit

* [build-tools] Pass step cancellation to Android app inspection

* [build-tools] Simplify Android type dispatch and ZIP lookup

* [build-tools] use caller cancellation for Android artifact inspection

* [build-tools] Run checks after rebasing Android artifact inspection
2026-10-08 10:23:15 +02:00
Expo CI d90f022966 bump STAGING and PRODUCTION to 24.12.0 in cli-versions.json 2026-10-07 21:20:27 +00:00
Expo CI 8b8362e4ae update CHANGELOG.md for v24.12.0 2026-10-07 21:17:55 +00:00
Expo CI b5ac163b37 v24.12.0 2026-10-07 21:15:21 +00:00
Douglas LowderandClaude Opus 5 92c8944cda [eas-cli] Include sourceMapUrl and assetMapUrl in eas update:view --json (#4534)
For update:view, add both asset map and source map URLs to the returned results when --json is used. This allows CLI users to retrieve source maps needed for symbolicating JS crashes.

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
2026-10-07 21:06:12 +00:00
Douglas LowderandClaude Opus 5.5 96581b8094 [ci] Move ffmpeg-dependent tests to a separate workflow (#4565)
For better performance of the main test workflow, move ffmpeg-dependent tests to a separate workflow, with a restricted set of source files in the workflow trigger, and ensure that this workflow is not required for PRs.

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
2026-10-07 14:00:32 -07:00
Gabe Debes e229f5378f [build-tools] Show eas update:embedded:upload output in build logs (#4557) 2026-10-07 13:38:05 -07:00
Gabe Debes 19657cc7ff [build-tools] Fix unnotarized mitmproxy cask install on iOS network capture sessions (#4556) 2026-10-07 11:04:55 -07:00
Tomasz Mazur be7acb7ab5 [eas-cli] Fix workflow:ssh lookup for job run ids (#4529) 2026-10-07 17:42:49 +02:00
Gabe DebesandClaude 3c302a055d [build-tools] Upload embedded bundle from custom builds (#4494)
* [build-tools] Upload embedded bundle from eas/build custom builds

Co-Authored-By: Claude  <[email protected]>

* [build-tools] Add ignore_error to eas/upload_embedded_bundle and share archive path resolvers

Co-Authored-By: Claude <[email protected]>
2026-10-06 15:06:48 -07:00
Samuel Newman 449bd4981e Select containing iOS app when archive includes an App Clip (#4541)
For build:download, exclude nested app bundles (like app clips) from the installable candidates. Return the sole outer app without prompting, and keep the selection prompt when there are genuinely separate apps.
2026-10-06 09:58:17 -07:00
Szymon Dziedzic 4fd36987ba [build-tools] Encode macOS session previews with cwebp (#4549)
* [build-tools] Encode macOS session previews with cwebp

Homebrew's FFmpeg is built without libwebp, so every macOS preview failed
with "Encoder not found" (exit code 8). Resize the screenshot with sips and
encode it with cwebp from Homebrew's webp package instead. Android keeps
using FFmpeg.

* [build-tools] Add changelog entry for macOS session preview encoding

* [build-tools] Format the changelog entry
2026-10-06 18:14:16 +02:00
Szymon Dziedzic 01c1ffcaf4 [build-tools] Fix the session preview device reference on main (#4548)
#4398 was based on #4499, which renamed device to readyDevice in
startDeviceSessionHostAsync. #4540 reverted #4499 before #4398 merged, so
main referred to a readyDevice name that no longer exists and the
build-tools typecheck failed. Use device instead.
2026-10-06 14:10:28 +02:00
Szymon Dziedzic b10cc829bd [build-tools] Refresh simulator session preview artifacts (#4398)
Capture a WebP thumbnail of the session device every minute while the
session host runs, and upload it to one reusable preview artifact. Upload
request errors no longer include the signed upload URL in logs.
2026-10-06 13:54:42 +02:00
SJ Kim 3d8ed4c81f [eas-cli] Log client request events for simulator sessions (#4483)
* [eas-cli] Log client request events for simulator sessions (ENG-27138)

* [eas-cli] Flush analytics and exit with 130 when Ctrl+C cancels a simulator session request (ENG-27138)

* [eas-cli] Add changelog entry for simulator request events (ENG-27138)

* [eas-cli] Bound the Ctrl+C analytics flush and tighten its tests (ENG-27138)

* [eas-cli] Don't count an HTTP error answer as a failed simulator session request (ENG-27138)

* [eas-cli] Keep the simulator request analytics wrapper with its only caller (ENG-27138)
2026-10-05 23:10:34 -04:00
Expo CI a344c0e1e8 bump STAGING and PRODUCTION to 24.11.0 in cli-versions.json 2026-10-05 22:05:17 +00:00
Expo CI f80f548e2f update CHANGELOG.md for v24.11.0 2026-10-05 22:03:02 +00:00
166 changed files with 9879 additions and 1264 deletions

No files matched your search

@@ -0,0 +1,43 @@
name: Test recording recovery
on:
push:
branches: [main]
paths: &recording-paths
- packages/build-tools/src/steps/utils/deviceRunSessionScreenRecordings.ts
- packages/build-tools/src/steps/utils/deviceRunSessionArtifacts.ts
- packages/build-tools/src/steps/utils/__tests__/*.test.ffmpeg.ts
- packages/build-tools/jest/ffmpeg-config.ts
- .github/workflows/test-recording-recovery.yml
pull_request:
types: [opened, synchronize]
paths: *recording-paths
permissions:
contents: read
concurrency:
group: test-recording-recovery-${{ github.ref }}
cancel-in-progress: true
jobs:
test:
runs-on: ubuntu-latest
name: Test recording recovery with ffmpeg
steps:
- uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
with:
persist-credentials: false
- name: Setup tools
uses: ./.github/actions/setup-mise
- name: Install ffmpeg
# The recording recovery test cuts and decodes real MP4 files with ffmpeg and ffprobe.
run: sudo apt-get update && sudo apt-get install -y --no-install-recommends ffmpeg
- name: Install dependencies
run: yarn install --immutable
- name: Build build-tools dependencies
# tsc emits the dist files that the test imports from workspace packages such as @expo/logger.
run: yarn run -T lerna run typecheck --scope @expo/build-tools --include-dependencies
- name: Run recording recovery test
working-directory: packages/build-tools
run: yarn jest-ffmpeg
+2 -5
View File
@@ -15,9 +15,9 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: ['20.18.3', '22', '24']
node: ['22', '24']
include:
- node: '20.18.3'
- node: '22'
coverage: true
name: Test with Node ${{ matrix.node }}
steps:
@@ -26,9 +26,6 @@ jobs:
uses: ./.github/actions/setup-mise
env:
MISE_NODE_VERSION: ${{ matrix.node }}
- name: Install ffmpeg
# build-tools recording-recovery tests cut and decode real MP4 files with ffmpeg and ffprobe.
run: sudo apt-get update && sudo apt-get install -y --no-install-recommends ffmpeg
- name: Install dependencies
run: yarn install --immutable
env:
+56 -3
View File
@@ -8,12 +8,63 @@ This is the log of notable changes to EAS CLI and related packages.
### 🎉 New features
- [build-tools] Record iOS Simulator sessions through serve-sim. ([#4474](https://github.com/expo/eas-cli/pull/4474) by [@gwdp](https://github.com/gwdp))
- [eas-cli] Add browser device login with resumable commands for chat agents. ([#4502](https://github.com/expo/eas-cli/pull/4502) by [@eiiot](https://github.com/eiiot))
- [build-tools] Upload the display rotation agent-device reports for a screenshot as device run session artifact metadata. ([#4508](https://github.com/expo/eas-cli/pull/4508) by [@szdziedzic](https://github.com/szdziedzic))
- [build-tools] Let the MCP server read text and image files from sandboxes. ([#4555](https://github.com/expo/eas-cli/pull/4555) by [@AHGIJMKLKKZNPJKQR](https://github.com/AHGIJMKLKKZNPJKQR))
- [eas-cli][local-build-plugin] Deprecate Node.js 20, and print a warning when running on it. A future release will require Node.js 22 or newer. Development and CI now use Node.js 22. ([#4579](https://github.com/expo/eas-cli/pull/4579) by [@douglowder](https://github.com/douglowder))
- [build-tools] Add `eas/run_agent`, a step that runs Claude Code for an agent run and connects it to the Expo MCP server. ([#4552](https://github.com/expo/eas-cli/pull/4552) by [@hSATAC](https://github.com/hSATAC))
- [build-tools] Support Codex in `eas/run_agent`. ([#4553](https://github.com/expo/eas-cli/pull/4553) by [@hSATAC](https://github.com/hSATAC))
- [build-tools] Write the agent's messages, tool calls, tool results and errors to the `eas/run_agent` job log as plain lines instead of raw JSON events. ([#4568](https://github.com/expo/eas-cli/pull/4568) by [@hSATAC](https://github.com/hSATAC))
- [eas-cli] Show the average and P75 end-to-end run duration in `eas workflow:insights`. ([#4566](https://github.com/expo/eas-cli/pull/4566) by [@hSATAC](https://github.com/hSATAC))
- [eas-cli] Ask for the App Store Connect API key type (team or individual) in the submission key prompt and skip the Issuer ID prompt for individual keys. ([#4589](https://github.com/expo/eas-cli/pull/4589) by [@sswrk](https://github.com/sswrk))
- [build-tools] Add `eas/download_submission_archive` to request a fresh archive URL when the step runs. ([#4539](https://github.com/expo/eas-cli/pull/4539) by [@sjchmiela](https://github.com/sjchmiela))
- [eas-build-job] Add an `uploadArtifact` command to the sandbox daemon protocol. ([#4562](https://github.com/expo/eas-cli/pull/4562) by [@AHGIJMKLKKZNPJKQR](https://github.com/AHGIJMKLKKZNPJKQR))
- [build-tools] Let connected sandboxes upload files as sandbox artifacts through the Expo MCP server. ([#4563](https://github.com/expo/eas-cli/pull/4563) by [@AHGIJMKLKKZNPJKQR](https://github.com/AHGIJMKLKKZNPJKQR))
### 🐛 Bug fixes
- [build-tools] Look up ffmpeg on PATH instead of running it. ([#4582](https://github.com/expo/eas-cli/pull/4582) by [@gwdp](https://github.com/gwdp))
- [eas-cli] Stop reporting individual App Store Connect API keys as not valid on Apple's servers when adding them in `eas credentials`. ([#4588](https://github.com/expo/eas-cli/pull/4588) by [@sswrk](https://github.com/sswrk))
- [build-tools] Install ffmpeg for Appium simulator sessions. ([#4577](https://github.com/expo/eas-cli/pull/4577) by [@gwdp](https://github.com/gwdp))
- [build-tools] Close the file of an artifact upload before reporting that the upload is done. ([#4609](https://github.com/expo/eas-cli/pull/4609) by [@AHGIJMKLKKZNPJKQR](https://github.com/AHGIJMKLKKZNPJKQR))
### 🧹 Chores
## [24.12.1](https://github.com/expo/eas-cli/releases/tag/v24.12.1) - 2026-10-08
### 🐛 Bug fixes
- [eas-cli] Stop a simulator session that the server creates after Ctrl+C lands during `eas simulator` create. ([#4522](https://github.com/expo/eas-cli/pull/4522) by [@sjkim-expo](https://github.com/sjkim-expo))
## [24.12.0](https://github.com/expo/eas-cli/releases/tag/v24.12.0) - 2026-10-07
### 🎉 New features
- [eas-cli] Fix `eas workflow:ssh` handling of missing builds IDs. ([#4529](https://github.com/expo/eas-cli/pull/4529) by [@AHGIJMKLKKZNPJKQR](https://github.com/AHGIJMKLKKZNPJKQR))
- [build-tools] Upload the embedded bundle from custom builds that use `eas/build`. ([#4494](https://github.com/expo/eas-cli/pull/4494) by [@gwdp](https://github.com/gwdp))
- [build-tools] Add the `eas/upload_embedded_bundle` step for custom build configs. ([#4494](https://github.com/expo/eas-cli/pull/4494) by [@gwdp](https://github.com/gwdp))
- [eas-cli] Include `assetMapUrl` and `sourceMapUrl` in the `eas update:view --json` output. Each value is a signed download URL, or null when the update was published without that file. ([#4534](https://github.com/expo/eas-cli/pull/4534) by [@douglowder](https://github.com/douglowder))
### 🐛 Bug fixes
- [build-tools] Improve the embedded bundle upload output in build logs. ([#4557](https://github.com/expo/eas-cli/pull/4557) by [@gwdp](https://github.com/gwdp))
- [build-tools] Allow the unnotarized mitmproxy cask to run in iOS network capture sessions. ([#4556](https://github.com/expo/eas-cli/pull/4556) by [@gwdp](https://github.com/gwdp))
- [build-tools] Encode macOS simulator session previews with cwebp, because Homebrew's FFmpeg cannot encode WebP. ([#4549](https://github.com/expo/eas-cli/pull/4549) by [@szdziedzic](https://github.com/szdziedzic))
### 🧹 Chores
- [eas-cli] Log analytics events when an `eas simulator` session request is sent, cancelled, or fails before the server answers. ([#4483](https://github.com/expo/eas-cli/pull/4483) by [@sjkim-expo](https://github.com/sjkim-expo))
## [24.11.0](https://github.com/expo/eas-cli/releases/tag/v24.11.0) - 2026-10-05
### 🎉 New features
- [build-tools] Record iOS Simulator sessions through serve-sim. ([#4474](https://github.com/expo/eas-cli/pull/4474) by [@gwdp](https://github.com/gwdp))
- [eas-cli] Add browser device login with resumable commands for chat agents. ([#4502](https://github.com/expo/eas-cli/pull/4502) by [@eiiot](https://github.com/eiiot))
- [build-tools] Upload the display rotation agent-device reports for a screenshot as device run session artifact metadata. ([#4508](https://github.com/expo/eas-cli/pull/4508) by [@szdziedzic](https://github.com/szdziedzic))
- [build-tools] Refresh a WebP thumbnail of the simulator session screen every minute as a reusable session preview artifact. ([#4398](https://github.com/expo/eas-cli/pull/4398) by [@szdziedzic](https://github.com/szdziedzic))
### 🐛 Bug fixes
- [eas-cli] Select the containing iOS app when its archive also includes a nested App Clip. ([#4541](https://github.com/expo/eas-cli/pull/4541) by [@mozzius](https://github.com/mozzius))
- [build-tools] Allow expo.test subdomains to open local and staging simulator previews. ([#4480](https://github.com/expo/eas-cli/pull/4480) by [@hirbod](https://github.com/hirbod))
- [build-tools] Retry App Store Connect connection failures and safely recover interrupted status reads and upload commits. ([#4520](https://github.com/expo/eas-cli/pull/4520) by [@sjchmiela](https://github.com/sjchmiela))
@@ -205,6 +256,8 @@ This is the log of notable changes to EAS CLI and related packages.
- [build-tools] Report a system error when project source refresh fails for a remote build. ([#4359](https://github.com/expo/eas-cli/pull/4359) by [@sjchmiela](https://github.com/sjchmiela))
- [eas-cli] Remove the obsolete `appVersion` runtime policy warning that referenced SDK 46 and expo-updates 0.14.4. ([#4375](https://github.com/expo/eas-cli/pull/4375) by [@brentvatne](https://github.com/brentvatne))
## [24.0.0](https://github.com/expo/eas-cli/releases/tag/v24.0.0) - 2026-09-09
### 🛠 Breaking changes
+1 -1
View File
@@ -164,7 +164,7 @@ Each package has independent TypeScript compilation:
TypeScript configs:
- `tsconfig.json` - Base configuration (extends @tsconfig/node20)
- `tsconfig.json` - Base configuration (extends @tsconfig/node22)
- `tsconfig.build.json` - Production builds
- `tsconfig.allowUnused.json` - Development with relaxed rules
+2 -2
View File
@@ -1,4 +1,4 @@
{
"STAGING": "24.10.0",
"PRODUCTION": "24.10.0"
"STAGING": "24.12.1",
"PRODUCTION": "24.12.1"
}
+1 -1
View File
@@ -1,5 +1,5 @@
{
"npmClient": "yarn",
"version": "24.11.0",
"version": "24.13.0",
"$schema": "node_modules/lerna/schemas/lerna-schema.json"
}
+1 -1
View File
@@ -6,4 +6,4 @@ MISE_NODE_COREPACK = "true"
actionlint = "1.7.12"
gcloud = "570.0.0"
jq = "1.8.1"
node = "20.19.4"
node = "22.23.3"
+1 -1
View File
@@ -22,7 +22,7 @@
},
"devDependencies": {
"@types/jest": "29.5.11",
"@types/node": "20.19.43",
"@types/node": "22.20.5",
"eslint-plugin-async-protect": "3.1.0",
"jest": "29.7.0",
"jest-watch-typeahead": "2.2.2",
@@ -0,0 +1,13 @@
import type { Config } from '@jest/types';
import baseConfig from '../../../jest/jest.shared.config';
const config: Config.InitialOptions = {
...baseConfig,
rootDir: '../src',
testMatch: ['**/__tests__/*.test.ffmpeg.ts'],
clearMocks: true,
setupFilesAfterEnv: ['<rootDir>/../jest/setup-tests.ts'],
};
export default config;
+9 -8
View File
@@ -1,6 +1,6 @@
{
"name": "@expo/build-tools",
"version": "24.11.0",
"version": "24.13.0",
"bugs": "https://github.com/expo/eas-cli/issues",
"license": "BUSL-1.1",
"author": "Expo <[email protected]>",
@@ -30,6 +30,7 @@
"prepack": "rimraf dist \"*.tsbuildinfo\" && yarn gql && tsc -p tsconfig.build.json",
"jest-unit": "jest --config jest/unit-config.ts",
"jest-integration": "jest --config jest/integration-config.ts",
"jest-ffmpeg": "jest --config jest/ffmpeg-config.ts",
"jest-unit-watch": "jest --config jest/unit-config.ts --watch",
"jest-integration-watch": "jest --config jest/integration-config.ts --watch",
"clean": "rimraf node_modules dist coverage \"*.tsbuildinfo\"",
@@ -40,17 +41,17 @@
"dependencies": {
"@expo/config": "55.0.10",
"@expo/config-plugins": "55.0.7",
"@expo/downloader": "24.8.0",
"@expo/eas-build-job": "24.9.0",
"@expo/downloader": "24.13.0",
"@expo/eas-build-job": "24.13.0",
"@expo/env": "^0.4.0",
"@expo/logger": "24.8.0",
"@expo/logger": "24.13.0",
"@expo/package-manager": "1.9.10",
"@expo/plist": "^0.3.5",
"@expo/results": "^1.0.0",
"@expo/spawn-async": "1.7.2",
"@expo/steps": "24.9.0",
"@expo/template-file": "24.8.0",
"@expo/turtle-spawn": "24.8.0",
"@expo/steps": "24.13.0",
"@expo/template-file": "24.13.0",
"@expo/turtle-spawn": "24.13.0",
"@expo/xcpretty": "^4.3.1",
"@ngrok/ngrok": "1.7.0",
"@sentry/node": "7.77.0",
@@ -87,7 +88,7 @@
"@types/fs-extra": "^11.0.4",
"@types/jest": "^29.5.12",
"@types/lodash": "^4.17.4",
"@types/node": "20.19.43",
"@types/node": "22.20.5",
"@types/node-fetch": "^2.6.11",
"@types/node-forge": "^1.3.11",
"@types/plist": "^3.0.5",
@@ -0,0 +1,5 @@
import { Android } from '@expo/eas-build-job';
export function resolveArtifactPath(job: Android.Job): string {
return job.applicationArchivePath ?? 'android/app/build/outputs/**/*.{apk,aab}';
}
@@ -61,7 +61,7 @@ jest.mock('../../utils/expoUpdates', () => ({
}));
jest.mock('../../utils/expoUpdatesEmbedded', () => ({
...jest.requireActual('../../utils/expoUpdatesEmbedded'),
uploadEmbeddedBundleAsync: jest.fn(),
uploadEmbeddedBundleAsync: jest.fn(async () => ({ status: 'uploaded' })),
}));
jest.mock('../../utils/hooks', () => ({
Hook: {
@@ -344,7 +344,29 @@ describe(androidBuilder, () => {
await androidBuilder(ctx);
expect(uploadEmbeddedBundleAsync).toHaveBeenCalledWith(ctx);
expect(uploadEmbeddedBundleAsync).toHaveBeenCalledWith(
expect.objectContaining({ job: ctx.job, env: ctx.env, projectDir: '/workingdir/build' })
);
});
it.each([
['uploaded', BuildPhaseResult.SUCCESS],
['skipped', BuildPhaseResult.SKIPPED],
['failed', BuildPhaseResult.WARNING],
] as const)('marks the phase from the %s result', async (uploadResult, phaseResult) => {
jest.mocked(uploadEmbeddedBundleAsync).mockResolvedValueOnce({ status: uploadResult });
const ctx = createContext({ sdkVersion: '58.0.0' });
await androidBuilder(ctx);
expect(datadogDistributionMock).toHaveBeenCalledWith(
'eas.build.phase_duration',
expect.any(Number),
expect.objectContaining({
build_phase: BuildPhase.UPLOAD_EMBEDDED_BUNDLE.toLowerCase(),
result: phaseResult,
})
);
});
it('skips by default on SDK 57 and earlier', async () => {
@@ -371,7 +393,9 @@ describe(androidBuilder, () => {
await androidBuilder(ctx);
expect(uploadEmbeddedBundleAsync).toHaveBeenCalledWith(ctx);
expect(uploadEmbeddedBundleAsync).toHaveBeenCalledWith(
expect.objectContaining({ job: ctx.job })
);
});
it('skips on SDK 58 and later when EAS_UPDATE_UPLOAD_EMBEDDED_BUNDLE is 0', async () => {
@@ -393,7 +417,9 @@ describe(androidBuilder, () => {
await androidBuilder(ctx);
expect(uploadEmbeddedBundleAsync).toHaveBeenCalledWith(ctx);
expect(uploadEmbeddedBundleAsync).toHaveBeenCalledWith(
expect.objectContaining({ job: ctx.job })
);
});
});
});
+14 -2
View File
@@ -11,6 +11,7 @@ import {
runGradleCommand,
} from '../android/gradle';
import { formatGradleProfileReport, parseGradleProfile } from '../android/gradleProfile';
import { resolveArtifactPath } from '../android/resolve';
import { Sentry } from '../sentry';
import { eagerBundleAsync, shouldUseEagerBundle } from '../common/eagerBundle';
import { prebuildAsync } from '../common/prebuild';
@@ -220,7 +221,7 @@ async function buildInnerAsync(
await ctx.runBuildPhase(BuildPhase.UPLOAD_APPLICATION_ARCHIVE, async () => {
await uploadApplicationArchive(ctx, {
patternOrPath: ctx.job.applicationArchivePath ?? 'android/app/build/outputs/**/*.{apk,aab}',
patternOrPath: resolveArtifactPath(ctx.job),
rootDir: ctx.getReactNativeProjectDirectory(),
logger: ctx.logger,
});
@@ -228,7 +229,18 @@ async function buildInnerAsync(
if (shouldUploadEmbeddedBundle(ctx)) {
await ctx.runBuildPhase(BuildPhase.UPLOAD_EMBEDDED_BUNDLE, async () => {
await uploadEmbeddedBundleAsync(ctx);
const { status } = await uploadEmbeddedBundleAsync({
job: ctx.job,
env: ctx.env,
logger: ctx.logger,
projectDir: ctx.getReactNativeProjectDirectory(),
appConfig: await ctx.appConfig,
});
if (status === 'skipped') {
ctx.markBuildPhaseSkipped();
} else if (status === 'failed') {
ctx.markBuildPhaseHasWarnings();
}
});
}
+13 -2
View File
@@ -237,7 +237,7 @@ async function buildInnerAsync(
await ctx.runBuildPhase(BuildPhase.UPLOAD_APPLICATION_ARCHIVE, async () => {
await uploadApplicationArchive(ctx, {
patternOrPath: resolveArtifactPath(ctx),
patternOrPath: resolveArtifactPath(ctx.job),
rootDir: ctx.getReactNativeProjectDirectory(),
logger: ctx.logger,
});
@@ -245,7 +245,18 @@ async function buildInnerAsync(
if (shouldUploadEmbeddedBundle(ctx)) {
await ctx.runBuildPhase(BuildPhase.UPLOAD_EMBEDDED_BUNDLE, async () => {
await uploadEmbeddedBundleAsync(ctx);
const { status } = await uploadEmbeddedBundleAsync({
job: ctx.job,
env: ctx.env,
logger: ctx.logger,
projectDir: ctx.getReactNativeProjectDirectory(),
appConfig: await ctx.appConfig,
});
if (status === 'skipped') {
ctx.markBuildPhaseSkipped();
} else if (status === 'failed') {
ctx.markBuildPhaseHasWarnings();
}
});
}
@@ -63,12 +63,12 @@ export class CustomBuildContext<TJob extends Job = Job> implements ExternalBuild
public readonly logger: bunyan;
public readonly graphqlClient: Client;
public readonly runtimeApi: BuilderRuntimeApi;
public readonly expoApiV2BaseUrl?: string;
public readonly mcpServerUrl?: string;
public job: TJob;
public metadata?: Metadata;
private _env: Env;
private readonly expoApiV2BaseUrl?: string;
private readonly pendingMetricUploads: Promise<void>[] = [];
constructor(buildCtx: BuildContext<TJob>) {
+4 -4
View File
@@ -15,10 +15,10 @@ export function resolveScheme(ctx: BuildContext<Ios.Job>): string {
return schemes[0];
}
export function resolveArtifactPath(ctx: BuildContext<Ios.Job>): string {
if (ctx.job.applicationArchivePath) {
return ctx.job.applicationArchivePath;
} else if (ctx.job.simulator) {
export function resolveArtifactPath(job: Ios.Job): string {
if (job.applicationArchivePath) {
return job.applicationArchivePath;
} else if (job.simulator) {
return 'ios/build/Build/Products/*simulator/*.app';
} else {
return 'ios/build/*.ipa';
@@ -10,6 +10,7 @@ import { configureIosVersionFunction } from './functions/configureIosVersion';
import { createSubmissionEntityFunction } from './functions/createSubmissionEntity';
import { createDownloadArtifactFunction } from './functions/downloadArtifact';
import { createDownloadBuildFunction } from './functions/downloadBuild';
import { createDownloadSubmissionArchiveFunction } from './functions/downloadSubmissionArchive';
import { createEasDeployBuildFunction } from './functions/deploy';
import { createEasExportBuildFunction } from './functions/export';
import { eagerBundleBuildFunction } from './functions/eagerBundle';
@@ -27,6 +28,7 @@ import { createLaunchApplicationFunction } from './functions/launchApplication';
import { createPrebuildBuildFunction } from './functions/prebuild';
import { createReadAppConfigBuildFunction } from './functions/readAppConfig';
import { createReadIpaInfoBuildFunction } from './functions/readIpaInfo';
import { createReadAndroidAppInfoBuildFunction } from './functions/readAndroidAppInfo';
import { createReadPackageJsonBuildFunction } from './functions/readPackageJson';
import { createRepackBuildFunction } from './functions/repack';
import { createReportMaestroTestResultsFunction } from './functions/reportMaestroTestResults';
@@ -43,6 +45,7 @@ import { runGradleFunction } from './functions/runGradle';
import { createMaestroTestsBuildFunction } from './functions/maestroTests';
import { createSaveBuildCacheFunction } from './functions/saveBuildCache';
import { createRolloutPosthogFlagFunction } from './functions/rolloutPosthogFlag';
import { createRunAgentBuildFunction } from './functions/runAgent';
import { createSaveCacheFunction } from './functions/saveCache';
import { createSendSlackMessageFunction } from './functions/sendSlackMessage';
import { createStartAgentDeviceSessionBuildFunction } from './functions/startAgentDeviceSession';
@@ -61,6 +64,7 @@ import { createStartSandboxBuildFunction } from './functions/startSandbox';
import { createCollectServeSimMetricsBuildFunction } from './functions/collectServeSimMetrics';
import { createUploadArtifactBuildFunction } from './functions/uploadArtifact';
import { createUploadDeviceRunSessionScreenRecordingsBuildFunction } from './functions/uploadDeviceRunSessionScreenRecordings';
import { createUploadEmbeddedBundleBuildFunction } from './functions/uploadEmbeddedBundle';
import { createUploadToAscBuildFunction } from './functions/uploadToAsc';
import { createUpdateTestFlightMetadataBuildFunction } from './functions/updateTestFlightMetadata';
import { createSetUpNpmrcBuildFunction } from './functions/useNpmToken';
@@ -81,7 +85,9 @@ export function getEasFunctions(ctx: CustomBuildContext): BuildFunction[] {
createInstallNodeModulesBuildFunction(),
createPrebuildBuildFunction(),
createReadIpaInfoBuildFunction(),
createReadAndroidAppInfoBuildFunction(),
createDownloadBuildFunction(ctx),
createDownloadSubmissionArchiveFunction(ctx),
createInstallBuildFunction(),
createLaunchApplicationFunction(),
createEasExportBuildFunction(),
@@ -118,6 +124,7 @@ export function getEasFunctions(ctx: CustomBuildContext): BuildFunction[] {
createStartServeSimLogsBuildFunction(),
createCollectServeSimLogsBuildFunction(ctx),
createStartSandboxBuildFunction(ctx),
createRunAgentBuildFunction(ctx),
createCollectServeSimMetricsBuildFunction(ctx),
createInstallMaestroBuildFunction(),
createInstallMitmproxyBuildFunction(),
@@ -145,6 +152,7 @@ export function getEasFunctions(ctx: CustomBuildContext): BuildFunction[] {
functions.push(
...[
createFindAndUploadBuildArtifactsBuildFunction(ctx),
createUploadEmbeddedBundleBuildFunction(ctx),
createResolveBuildConfigBuildFunction(ctx),
createGetCredentialsForBuildTriggeredByGithubIntegration(ctx),
]
@@ -219,4 +219,71 @@ describe(createEasBuildBuildFunctionGroup, () => {
'Build function group is not supported in generic jobs.'
);
});
describe('embedded bundle upload', () => {
function getStepNames(
options: Parameters<typeof createMockBuildToolsContext>[0],
{ sdkVersion, env = {} }: { sdkVersion?: string; env?: Record<string, string> }
): string[] {
const functionGroup = createEasBuildBuildFunctionGroup(createMockBuildToolsContext(options));
const globalCtx = createGlobalContextMock({
logger: createMockLogger(),
staticContextContent: { metadata: sdkVersion ? { sdkVersion } : null },
});
globalCtx.updateEnv(env);
return functionGroup
.createBuildStepsFromFunctionGroupCall(globalCtx)
.map(step => step.displayName);
}
it.each([
['Android', { platform: Platform.ANDROID }],
['Android with credentials', { platform: Platform.ANDROID, buildCredentials: { test: {} } }],
['iOS with credentials', { platform: Platform.IOS, buildCredentials: { test: {} } }],
])('uploads the embedded bundle after the build artifacts on SDK 58 (%s)', (_, options) => {
const stepNames = getStepNames(options, { sdkVersion: '58.0.0' });
expect(stepNames.indexOf('Upload embedded bundle')).toBe(
stepNames.indexOf('Find and upload build artifacts') + 1
);
});
it.each([
['Android', { platform: Platform.ANDROID }],
['Android with credentials', { platform: Platform.ANDROID, buildCredentials: { test: {} } }],
['iOS with credentials', { platform: Platform.IOS, buildCredentials: { test: {} } }],
])('passes ignore_error to the embedded bundle step (%s)', (_, options) => {
const functionGroup = createEasBuildBuildFunctionGroup(createMockBuildToolsContext(options));
const globalCtx = createGlobalContextMock({
logger: createMockLogger(),
staticContextContent: { metadata: { sdkVersion: '58.0.0' } },
});
const uploadStep = functionGroup
.createBuildStepsFromFunctionGroupCall(globalCtx)
.find(step => step.displayName === 'Upload embedded bundle');
expect(uploadStep).toBeDefined();
expect(uploadStep!.inputs?.find(input => input.id === 'ignore_error')?.rawValue).toBe(true);
});
it('does not upload the embedded bundle on SDK 57 without the opt-in', () => {
expect(getStepNames({}, { sdkVersion: '57.0.0' })).not.toContain('Upload embedded bundle');
});
it('uploads the embedded bundle on SDK 57 with the opt-in', () => {
expect(
getStepNames(
{},
{ sdkVersion: '57.0.0', env: { EAS_UPDATE_EXPERIMENTAL_UPLOAD_EMBEDDED_BUNDLE: '1' } }
)
).toContain('Upload embedded bundle');
});
it('does not upload the embedded bundle for iOS simulator builds', () => {
expect(
getStepNames({ platform: Platform.IOS, simulator: true }, { sdkVersion: '58.0.0' })
).not.toContain('Upload embedded bundle');
});
});
});
@@ -10,6 +10,7 @@ import path from 'path';
import { shouldUseEagerBundle } from '../../common/eagerBundle';
import { CustomBuildContext } from '../../customBuildContext';
import { shouldUploadEmbeddedBundle } from '../../utils/expoUpdatesEmbedded';
import { calculateEASUpdateRuntimeVersionFunction } from '../functions/calculateEASUpdateRuntimeVersion';
import { createCheckoutBuildFunction } from '../functions/checkout';
import { configureAndroidVersionFunction } from '../functions/configureAndroidVersion';
@@ -32,6 +33,7 @@ import {
import { runFastlaneFunction } from '../functions/runFastlane';
import { runGradleFunction } from '../functions/runGradle';
import { createSaveBuildCacheFunction } from '../functions/saveBuildCache';
import { createUploadEmbeddedBundleBuildFunction } from '../functions/uploadEmbeddedBundle';
import { createSetUpNpmrcBuildFunction } from '../functions/useNpmToken';
interface HelperFunctionsInput {
@@ -303,6 +305,19 @@ function createStepsForIosBuildWithCredentials({
createFindAndUploadBuildArtifactsBuildFunction(
buildToolsContext
).createBuildStepFromFunctionCall(globalCtx, { workingDirectory }),
...(shouldUploadEmbeddedBundle({
env: globalCtx.env,
metadata: globalCtx.staticContext.metadata,
})
? [
createUploadEmbeddedBundleBuildFunction(
buildToolsContext
).createBuildStepFromFunctionCall(globalCtx, {
workingDirectory,
callInputs: { ignore_error: true },
}),
]
: []),
saveCache,
createCacheStatsBuildFunction().createBuildStepFromFunctionCall(globalCtx, {
workingDirectory,
@@ -390,6 +405,19 @@ function createStepsForAndroidBuildWithoutCredentials({
createFindAndUploadBuildArtifactsBuildFunction(
buildToolsContext
).createBuildStepFromFunctionCall(globalCtx, { workingDirectory }),
...(shouldUploadEmbeddedBundle({
env: globalCtx.env,
metadata: globalCtx.staticContext.metadata,
})
? [
createUploadEmbeddedBundleBuildFunction(
buildToolsContext
).createBuildStepFromFunctionCall(globalCtx, {
workingDirectory,
callInputs: { ignore_error: true },
}),
]
: []),
saveCache,
createCacheStatsBuildFunction().createBuildStepFromFunctionCall(globalCtx, {
workingDirectory,
@@ -483,6 +511,19 @@ function createStepsForAndroidBuildWithCredentials({
createFindAndUploadBuildArtifactsBuildFunction(
buildToolsContext
).createBuildStepFromFunctionCall(globalCtx, { workingDirectory }),
...(shouldUploadEmbeddedBundle({
env: globalCtx.env,
metadata: globalCtx.staticContext.metadata,
})
? [
createUploadEmbeddedBundleBuildFunction(
buildToolsContext
).createBuildStepFromFunctionCall(globalCtx, {
workingDirectory,
callInputs: { ignore_error: true },
}),
]
: []),
saveCache,
createCacheStatsBuildFunction().createBuildStepFromFunctionCall(globalCtx, {
workingDirectory,
@@ -0,0 +1,47 @@
import { UserError } from '@expo/eas-build-job';
import path from 'node:path';
import { createGlobalContextMock } from '../../../__tests__/utils/context';
import { readAndroidArtifactInfoAsync } from '../../utils/android/appArtifact';
import { createReadAndroidAppInfoBuildFunction } from '../readAndroidAppInfo';
jest.unmock('fs');
jest.unmock('node:fs');
jest.unmock('fs/promises');
jest.unmock('node:fs/promises');
jest.unmock('@expo/spawn-async');
const fixturesDirectory = path.join(__dirname, 'fixtures/android-app-info');
it.each(['apk', 'aab'])('reads a real %s and publishes build step outputs', async type => {
const step = createReadAndroidAppInfoBuildFunction().createBuildStepFromFunctionCall(
createGlobalContextMock({
projectTargetDirectory: fixturesDirectory,
staticContextContent: { job: {} },
}),
{ callInputs: { artifact_path: `app.${type}` } }
);
await step.executeAsync();
expect(step.outputById.artifact_type.value).toBe(type);
expect(step.outputById.package_name.value).toBe('com.example.app');
});
it('reports an invalid ZIP as a user error', async () => {
await expect(readAndroidArtifactInfoAsync(__filename)).rejects.toMatchObject({
constructor: UserError,
errorCode: 'EAS_READ_ANDROID_APP_INFO_INVALID_ARTIFACT',
cause: expect.any(Error),
});
});
it.each(['apk', 'aab'])('retains the native tool error for an invalid %s manifest', async type => {
await expect(
readAndroidArtifactInfoAsync(path.join(fixturesDirectory, `invalid-manifest-${type}.zip`))
).rejects.toMatchObject({
constructor: UserError,
errorCode: 'EAS_READ_ANDROID_APP_INFO_INVALID_MANIFEST',
cause: expect.any(Error),
});
});
@@ -260,3 +260,76 @@ describe.each([
);
});
});
describe('Appium ffmpeg setup', () => {
async function runAppiumAsync(): Promise<void> {
const fn = createStartAppiumRemoteSessionBuildFunction({} as CustomBuildContext);
await fn.fn!(
{
logger,
global: { runtimePlatform: BuildRuntimePlatform.LINUX },
} as unknown as BuildStepContext,
{
inputs: new Proxy({} as Record<string, { value: unknown }>, {
get: (target, id: string) => target[id] ?? { value: undefined },
}),
outputs: {},
env: {
DEVICE_RUN_SESSION_ID: 'session-id',
EAS_SIMULATOR_NGROK_TUNNEL_DOMAIN: 'example.test',
NGROK_AUTHTOKEN: 'token',
},
} as never
);
}
function holdFfmpegInstall({ appiumError }: { appiumError?: Error } = {}) {
let finish!: (outcome: 'installed' | 'failed') => void;
const install = new Promise((resolve, reject) => {
finish = outcome =>
outcome === 'installed' ? resolve({}) : reject(new Error('apt-get failed'));
});
jest.mocked(spawn).mockImplementation(((command: string, args: string[]) => {
if (command === 'ffmpeg') {
return Promise.reject(new Error('ffmpeg missing'));
}
if (args.includes('install') && args.includes('ffmpeg')) {
return install;
}
return command === 'npm' && appiumError
? Promise.reject(appiumError)
: Promise.resolve({ stdout: '{}' });
}) as never);
return finish;
}
it.each(['installed', 'failed'] as const)(
'starts Appium after the ffmpeg install settles (%s)',
async outcome => {
const finishFfmpegInstall = holdFfmpegInstall();
const run = runAppiumAsync();
await new Promise(resolve => setImmediate(resolve));
expect(spawnDetached).not.toHaveBeenCalled();
finishFfmpegInstall(outcome);
await run;
expect(spawnDetached).toHaveBeenCalledTimes(1);
}
);
it('waits for the ffmpeg install before failing on an Appium install error', async () => {
const appiumError = new Error('npm failed');
const finishFfmpegInstall = holdFfmpegInstall({ appiumError });
let settled = false;
const run = runAppiumAsync().finally(() => {
settled = true;
});
await new Promise(resolve => setImmediate(resolve));
expect(settled).toBe(false);
finishFfmpegInstall('installed');
await expect(run).rejects.toBe(appiumError);
});
});
@@ -0,0 +1,192 @@
import { Client, CombinedError } from '@urql/core';
import fetch, { Response } from 'node-fetch';
import fs from 'node:fs';
import { Readable } from 'node:stream';
import { CustomBuildContext } from '../../../customBuildContext';
import { createGlobalContextMock } from '../../../__tests__/utils/context';
import { createMockLogger } from '../../../__tests__/utils/logger';
import { createDownloadSubmissionArchiveFunction } from '../downloadSubmissionArchive';
const submissionId = 'aabbccdd-1234-4321-abcd-123456789012';
const url = 'https://storage.example/app.ipa?signature=secret';
it('reports invalid submission ID and extensions together', async () => {
const mutation = jest.fn();
const step = createDownloadSubmissionArchiveFunction({
graphqlClient: { mutation } as unknown as Client,
} as CustomBuildContext).createBuildStepFromFunctionCall(createGlobalContextMock(), {
callInputs: { submission_id: 'invalid', extensions: [123] },
});
await expect(step.executeAsync()).rejects.toMatchObject({
issues: expect.arrayContaining([
expect.objectContaining({ path: ['submissionId'] }),
expect.objectContaining({ path: ['extensions', 0] }),
]),
});
expect(mutation).not.toHaveBeenCalled();
});
it('requests a fresh URL on each execution, downloads without auth and does not log the URL', async () => {
const mutation = jest.fn(() => ({
toPromise: async () => ({
data: {
submission: { generateSubmissionArchiveDownloadUrl: url },
},
}),
}));
const logger = createMockLogger();
const fn = createDownloadSubmissionArchiveFunction({
graphqlClient: { mutation } as unknown as Client,
} as CustomBuildContext);
for (let i = 0; i < 2; i++) {
jest.mocked(fetch).mockResolvedValue({
ok: true,
url,
body: Readable.from(Buffer.from('IPA bytes')),
headers: { get: () => null },
} as unknown as Response);
const step = fn.createBuildStepFromFunctionCall(createGlobalContextMock({ logger }), {
callInputs: { submission_id: submissionId },
});
await step.executeAsync();
expect(await fs.promises.readFile(step.getOutputValueByName('artifact_path')!, 'utf8')).toBe(
'IPA bytes'
);
}
expect(mutation).toHaveBeenCalledTimes(2);
expect(fetch).toHaveBeenCalledWith(url, { headers: undefined, signal: undefined });
expect(JSON.stringify(jest.mocked(logger.info).mock.calls)).not.toContain(url);
});
it('preserves the API error as the cause without downloading', async () => {
const error = new CombinedError({ networkError: new Error('Connection reset') });
const mutation = jest.fn(() => ({
toPromise: async () => ({
error,
}),
}));
const step = createDownloadSubmissionArchiveFunction({
graphqlClient: { mutation } as unknown as Client,
} as CustomBuildContext).createBuildStepFromFunctionCall(createGlobalContextMock(), {
callInputs: { submission_id: submissionId },
});
await expect(step.executeAsync()).rejects.toMatchObject({
message: 'Could not request the submission archive. Try again later.',
cause: error,
});
expect(fetch).not.toHaveBeenCalled();
expect(mutation).toHaveBeenCalledTimes(4);
});
it('retries a temporary URL request failure and then downloads the archive', async () => {
const mutation = jest
.fn()
.mockReturnValueOnce({
toPromise: async () => ({
error: new CombinedError({ networkError: new Error('Connection reset') }),
}),
})
.mockReturnValue({
toPromise: async () => ({
data: { submission: { generateSubmissionArchiveDownloadUrl: url } },
}),
});
jest.mocked(fetch).mockResolvedValue({
ok: true,
url,
body: Readable.from(Buffer.from('IPA bytes')),
headers: { get: () => null },
} as unknown as Response);
const step = createDownloadSubmissionArchiveFunction({
graphqlClient: { mutation } as unknown as Client,
} as CustomBuildContext).createBuildStepFromFunctionCall(createGlobalContextMock(), {
callInputs: { submission_id: submissionId },
});
await step.executeAsync();
expect(mutation).toHaveBeenCalledTimes(2);
expect(await fs.promises.readFile(step.getOutputValueByName('artifact_path')!, 'utf8')).toBe(
'IPA bytes'
);
});
it('does not retry an access error', async () => {
const error = new CombinedError({ graphQLErrors: [{ message: 'Not authorized' }] });
const mutation = jest.fn(() => ({ toPromise: async () => ({ error }) }));
const step = createDownloadSubmissionArchiveFunction({
graphqlClient: { mutation } as unknown as Client,
} as CustomBuildContext).createBuildStepFromFunctionCall(createGlobalContextMock(), {
callInputs: { submission_id: submissionId },
});
await expect(step.executeAsync()).rejects.toMatchObject({ cause: error });
expect(mutation).toHaveBeenCalledTimes(1);
expect(fetch).not.toHaveBeenCalled();
});
it('does not send another URL request after the step times out during retry backoff', async () => {
const mutation = jest.fn(() => ({
toPromise: async () => ({
error: new CombinedError({ networkError: new Error('Connection reset') }),
}),
}));
const step = createDownloadSubmissionArchiveFunction({
graphqlClient: { mutation } as unknown as Client,
} as CustomBuildContext).createBuildStepFromFunctionCall(createGlobalContextMock(), {
callInputs: { submission_id: submissionId },
timeoutMs: 100,
});
await expect(step.executeAsync()).rejects.toThrow('timed out');
await new Promise(resolve => setTimeout(resolve, 1100));
expect(mutation).toHaveBeenCalledTimes(1);
expect(fetch).not.toHaveBeenCalled();
});
it('uses the requested extension for an archive without a filename extension', async () => {
const mutation = jest.fn(() => ({
toPromise: async () => ({
data: {
submission: { generateSubmissionArchiveDownloadUrl: 'https://storage.example/archive' },
},
}),
}));
jest.mocked(fetch).mockResolvedValue({
ok: true,
url: 'https://storage.example/archive',
body: Readable.from(Buffer.from('Android archive bytes')),
headers: { get: () => null },
} as unknown as Response);
const step = createDownloadSubmissionArchiveFunction({
graphqlClient: { mutation } as unknown as Client,
} as CustomBuildContext).createBuildStepFromFunctionCall(createGlobalContextMock(), {
callInputs: { submission_id: submissionId, extensions: ['aab'] },
});
await step.executeAsync();
const artifactPath = step.getOutputValueByName('artifact_path')!;
expect(artifactPath).toMatch(/\.aab$/);
expect(await fs.promises.readFile(artifactPath, 'utf8')).toBe('Android archive bytes');
});
it('propagates the original download error', async () => {
const error = new Error('Download failed');
const mutation = jest.fn(() => ({
toPromise: async () => ({
data: { submission: { generateSubmissionArchiveDownloadUrl: url } },
}),
}));
jest.mocked(fetch).mockRejectedValue(error);
const step = createDownloadSubmissionArchiveFunction({
graphqlClient: { mutation } as unknown as Client,
} as CustomBuildContext).createBuildStepFromFunctionCall(createGlobalContextMock(), {
callInputs: { submission_id: submissionId },
});
await expect(step.executeAsync()).rejects.toBe(error);
});
@@ -463,114 +463,127 @@ describe('createInstallMaestroBuildFunction', () => {
}
});
it('installs the prebuilt WDA cache for each available iOS runtime', async () => {
const homeDirectory = await fs.promises.mkdtemp(
path.join(os.tmpdir(), 'install-maestro-test-')
);
mockedSpawn.mockImplementation((async (command: string, args: string[]) => {
if (command === 'maestro-runner') {
return { stdout: 'maestro-runner 1.2.3\n' };
}
if (command === 'xcrun') {
return {
stdout: JSON.stringify({
runtimes: [
{
identifier: 'com.apple.CoreSimulator.SimRuntime.iOS-18-3',
isAvailable: true,
version: '18.3.1',
},
{
identifier: 'com.apple.CoreSimulator.SimRuntime.iOS-26-0',
isAvailable: true,
version: '26.0',
},
{
identifier: 'com.apple.CoreSimulator.SimRuntime.iOS-17-5',
isAvailable: false,
version: '17.5',
},
],
}),
};
}
if (command === 'xcodebuild') {
return { stdout: 'Xcode 26.0\nBuild version 17A324\n' };
}
if (command === 'tar') {
const maestroRunnerHome = args[args.indexOf('-C') + 1];
const productsDirectory = path.join(
maestroRunnerHome,
'cache',
'wda-builds',
'generic',
'DerivedData',
'Build',
'Products'
);
await fs.promises.mkdir(productsDirectory, { recursive: true });
await fs.promises.writeFile(
path.join(productsDirectory, 'WebDriverAgentRunner.xctestrun'),
'cached'
);
}
return { stdout: '' };
}) as any);
try {
await writeInstalledWdaVersion(homeDirectory, '11.1.3');
const installMaestro = createInstallMaestroBuildFunction();
const globalCtx = createGlobalContextMock({
runtimePlatform: BuildRuntimePlatform.DARWIN,
});
globalCtx.updateEnv({
EAS_BUILD_RUNNER: 'eas-build',
EAS_BUILD_COCOAPODS_CACHE_URL: 'https://cache.example.com',
HOME: homeDirectory,
});
const step = installMaestro.createBuildStepFromFunctionCall(globalCtx, {
callInputs: { backend: 'maestro-runner' },
});
await step.executeAsync();
expect(mockedDownloadFile).toHaveBeenCalledWith(
'https://cache.example.com/storage.googleapis.com/turtle-v2/maestro-runner-wda-cache/xcode-26.0-wda-11.1.3.tar.gz',
expect.stringMatching(/install_maestro_runner_wda_cache.*\/wda-cache\.tar\.gz$/),
{ retry: 3, timeout: 20_000 }
it.each(['1.1.27', '1.1.28', '1.2.3'])(
'prepares the WDA cache only for legacy Runner versions (%s)',
async version => {
const homeDirectory = await fs.promises.mkdtemp(
path.join(os.tmpdir(), 'install-maestro-test-')
);
for (const runtimeVersion of ['18.3', '26.0']) {
mockedSpawn.mockImplementation((async (command: string, args: string[]) => {
if (command === 'maestro-runner') {
return { stdout: `maestro-runner ${version}\n` };
}
if (command === 'xcrun') {
return {
stdout: JSON.stringify({
runtimes: [
{
identifier: 'com.apple.CoreSimulator.SimRuntime.iOS-18-3',
isAvailable: true,
version: '18.3.1',
},
{
identifier: 'com.apple.CoreSimulator.SimRuntime.iOS-26-0',
isAvailable: true,
version: '26.0',
},
{
identifier: 'com.apple.CoreSimulator.SimRuntime.iOS-17-5',
isAvailable: false,
version: '17.5',
},
],
}),
};
}
if (command === 'xcodebuild') {
return { stdout: 'Xcode 26.0\nBuild version 17A324\n' };
}
if (command === 'tar') {
const maestroRunnerHome = args[args.indexOf('-C') + 1];
const productsDirectory = path.join(
maestroRunnerHome,
'cache',
'wda-builds',
'generic',
'DerivedData',
'Build',
'Products'
);
await fs.promises.mkdir(productsDirectory, { recursive: true });
await fs.promises.writeFile(
path.join(productsDirectory, 'WebDriverAgentRunner.xctestrun'),
'cached'
);
}
return { stdout: '' };
}) as any);
try {
await writeInstalledWdaVersion(homeDirectory, '11.1.3');
const installMaestro = createInstallMaestroBuildFunction();
const globalCtx = createGlobalContextMock({
runtimePlatform: BuildRuntimePlatform.DARWIN,
});
globalCtx.updateEnv({
EAS_BUILD_RUNNER: 'eas-build',
EAS_BUILD_COCOAPODS_CACHE_URL: 'https://cache.example.com',
HOME: homeDirectory,
});
const step = installMaestro.createBuildStepFromFunctionCall(globalCtx, {
callInputs: { backend: 'maestro-runner' },
});
await step.executeAsync();
expect(step.getOutputValueByName('maestro_version')).toBe(version);
if (version !== '1.1.27') {
expect(mockedDownloadFile).not.toHaveBeenCalled();
expect(mockedSpawn.mock.calls.map(([command]) => command)).not.toContain('xcrun');
await expect(
fs.promises.access(path.join(homeDirectory, '.maestro-runner', 'cache'))
).rejects.toThrow();
return;
}
expect(mockedDownloadFile).toHaveBeenCalledWith(
'https://cache.example.com/storage.googleapis.com/turtle-v2/maestro-runner-wda-cache/xcode-26.0-wda-11.1.3.tar.gz',
expect.stringMatching(/install_maestro_runner_wda_cache.*\/wda-cache\.tar\.gz$/),
{ retry: 3, timeout: 20_000 }
);
for (const runtimeVersion of ['18.3', '26.0']) {
await expect(
fs.promises.readFile(
path.join(
homeDirectory,
'.maestro-runner',
'cache',
'wda-builds',
`sim-ios${runtimeVersion}-iphone`,
'DerivedData',
'Build',
'Products',
'WebDriverAgentRunner.xctestrun'
),
'utf8'
)
).resolves.toBe('cached');
}
await expect(
fs.promises.readFile(
path.join(
homeDirectory,
'.maestro-runner',
'cache',
'wda-builds',
`sim-ios${runtimeVersion}-iphone`,
'DerivedData',
'Build',
'Products',
'WebDriverAgentRunner.xctestrun'
),
'utf8'
fs.promises.access(
path.join(homeDirectory, '.maestro-runner', 'cache', 'wda-builds', 'sim-ios17.5-iphone')
)
).resolves.toBe('cached');
).rejects.toThrow();
} finally {
await fs.promises.rm(homeDirectory, { force: true, recursive: true });
}
await expect(
fs.promises.access(
path.join(homeDirectory, '.maestro-runner', 'cache', 'wda-builds', 'sim-ios17.5-iphone')
)
).rejects.toThrow();
} finally {
await fs.promises.rm(homeDirectory, { force: true, recursive: true });
}
});
);
it('does not download the WDA cache when the installed WDA version is unknown', async () => {
mockedSpawn.mockImplementation((async (command: string) => ({
stdout:
command === 'xcodebuild' ? 'Xcode 26.5\nBuild version 17F90\n' : 'maestro-runner 1.2.3\n',
command === 'xcodebuild' ? 'Xcode 26.5\nBuild version 17F90\n' : 'maestro-runner 1.1.27\n',
})) as any);
const installMaestro = createInstallMaestroBuildFunction();
const globalCtx = createGlobalContextMock({
@@ -593,7 +606,7 @@ describe('createInstallMaestroBuildFunction', () => {
mockedSpawn.mockImplementation((async (command: string) => {
switch (command) {
case 'maestro-runner':
return { stdout: 'maestro-runner 1.2.3\n' };
return { stdout: 'maestro-runner 1.1.27\n' };
case 'xcodebuild':
return { stdout: 'Xcode 26.5\nBuild version 17F90\n' };
case 'xcrun':
@@ -633,7 +646,7 @@ describe('createInstallMaestroBuildFunction', () => {
});
await expect(step.executeAsync()).resolves.toBeUndefined();
expect(step.getOutputValueByName('maestro_version')).toBe('1.2.3');
expect(step.getOutputValueByName('maestro_version')).toBe('1.1.27');
expect(mockedDownloadFile.mock.calls.map(([url]) => url)).toEqual([
'https://cache.example.com/storage.googleapis.com/turtle-v2/maestro-runner-wda-cache/xcode-26.5-wda-11.1.3.tar.gz',
'https://storage.googleapis.com/turtle-v2/maestro-runner-wda-cache/xcode-26.5-wda-11.1.3.tar.gz',
@@ -1,5 +1,6 @@
import { BuildRuntimePlatform, BuildStep } from '@expo/steps';
import spawn from '@expo/turtle-spawn';
import { fs, vol } from 'memfs';
import { createGlobalContextMock } from '../../../__tests__/utils/context';
import { createInstallMitmproxyBuildFunction } from '../installMitmproxy';
@@ -11,6 +12,58 @@ jest.mock('@expo/turtle-spawn', () => ({
const mockedSpawn = jest.mocked(spawn);
// An x86 Homebrew prefix, to check that the app is found from PATH and not from process.arch.
const APP = '/usr/local/Caskroom/mitmproxy/12.2.3/mitmproxy.app';
const LINK = '/usr/local/bin/mitmdump';
function installCask(): void {
vol.fromJSON({ [`${APP}/Contents/MacOS/mitmdump`]: '', '/usr/local/bin': null });
fs.symlinkSync(`${APP}/Contents/MacOS/mitmdump`, LINK);
}
/**
* `which` finds mitmdump once the cask is installed, `brew` installs it, `xattr -p` reports
* quarantine while `stuck` is set, and each `mitmdump --version` takes the next result.
*/
function mockCommands({
installed = false,
stuck = false,
runs = [],
}: {
installed?: boolean;
stuck?: boolean;
runs?: boolean[];
}): void {
if (installed) {
installCask();
}
mockedSpawn.mockImplementation((async (command: string, args: string[]) => {
if (command === 'which' && !installed) {
throw new Error('not found');
}
if (command === 'which') {
return { stdout: `${LINK}\n` };
}
if (command === 'brew' && !installed) {
installed = true;
installCask();
}
if (command === 'xattr' && args[0] === '-p' && !stuck) {
throw new Error('No such xattr: com.apple.quarantine');
}
if (command === 'mitmdump' && !runs.shift()) {
throw new Error('not found');
}
return { stdout: '' };
}) as never);
}
function spawnedCommands(): string[] {
return mockedSpawn.mock.calls.map(([command, args]) =>
command === 'xattr' ? `xattr ${args?.[0]}` : command
);
}
describe('createInstallMitmproxyBuildFunction', () => {
beforeEach(() => {
jest.clearAllMocks();
@@ -24,28 +77,51 @@ describe('createInstallMitmproxyBuildFunction', () => {
});
}
it('does not install when mitmdump is already on PATH', async () => {
mockedSpawn.mockResolvedValueOnce({} as never);
it('takes the app behind mitmdump on PATH out of quarantine before running it', async () => {
mockCommands({ installed: true, runs: [true] });
await createStep({ EAS_BUILD_RUNNER: 'eas-build' }).executeAsync();
expect(mockedSpawn).toHaveBeenCalledWith('mitmdump', ['--version'], expect.anything());
expect(mockedSpawn).toHaveBeenCalledTimes(1);
expect(mockedSpawn).toHaveBeenCalledWith(
'xattr',
['-dr', 'com.apple.quarantine', APP],
expect.anything()
);
expect(spawnedCommands()).toEqual(['which', 'xattr -dr', 'xattr -p', 'mitmdump']);
});
it('warns instead of installing outside EAS Build VMs', async () => {
mockedSpawn.mockRejectedValueOnce(new Error('not found'));
it('kills a mitmdump --version that does not return', async () => {
mockCommands({ installed: true, runs: [true] });
await createStep({ EAS_BUILD_RUNNER: 'eas-build' }).executeAsync();
expect(mockedSpawn).toHaveBeenCalledWith(
'mitmdump',
['--version'],
expect.objectContaining({ stdio: 'ignore', timeout: 60_000, killSignal: 'SIGKILL' })
);
});
it('does not run a mitmdump that stays quarantined', async () => {
mockCommands({ installed: true, stuck: true });
await expect(
createStep({ EAS_BUILD_RUNNER: 'eas-build' }).executeAsync()
).resolves.toBeUndefined();
expect(spawnedCommands()).not.toContain('mitmdump');
});
it('does not change quarantine outside EAS Build VMs', async () => {
mockCommands({ installed: true, stuck: true, runs: [true] });
await createStep({}).executeAsync();
expect(mockedSpawn).toHaveBeenCalledTimes(1);
expect(spawnedCommands()).toEqual(['mitmdump']);
});
it('installs the cask with Homebrew and leaves auto-update off', async () => {
mockedSpawn
.mockRejectedValueOnce(new Error('not found'))
.mockResolvedValueOnce({} as never)
.mockResolvedValueOnce({} as never);
it('installs the cask with Homebrew and takes it out of quarantine', async () => {
mockCommands({ runs: [false, true] });
await createStep({ EAS_BUILD_RUNNER: 'eas-build' }).executeAsync();
@@ -54,13 +130,19 @@ describe('createInstallMitmproxyBuildFunction', () => {
['install', '--cask', 'mitmproxy'],
expect.objectContaining({ env: expect.objectContaining({ HOMEBREW_NO_AUTO_UPDATE: '1' }) })
);
expect(spawnedCommands()).toEqual([
'which',
'mitmdump',
'brew',
'which',
'xattr -dr',
'xattr -p',
'mitmdump',
]);
});
it('does not fail the job when mitmdump is still not runnable', async () => {
mockedSpawn
.mockRejectedValueOnce(new Error('not found'))
.mockResolvedValueOnce({} as never)
.mockRejectedValueOnce(new Error('not found'));
mockCommands({ runs: [false, false] });
await expect(
createStep({ EAS_BUILD_RUNNER: 'eas-build' }).executeAsync()
@@ -0,0 +1,187 @@
import { SystemError } from '@expo/eas-build-job';
import turtleSpawn from '@expo/turtle-spawn';
import { vol } from 'memfs';
import { spawn } from 'node:child_process';
import { EventEmitter, once } from 'node:events';
import path from 'node:path';
import { PassThrough } from 'node:stream';
import { createMockLogger } from '../../../__tests__/utils/logger';
import * as agentRun from '../../utils/agentRun';
import { leaseAgentRunProviderCredentialsAsync } from '../../utils/agentRunLease';
import { createRunAgentBuildFunction } from '../runAgent';
jest.mock('@expo/turtle-spawn', () => ({ __esModule: true, default: jest.fn() }));
jest.mock('node:child_process', () => ({
...jest.requireActual('node:child_process'),
spawn: jest.fn(),
}));
jest.mock('../../utils/agentRunLease');
const installMock = jest.mocked(turtleSpawn);
const spawnMock = jest.mocked(spawn);
const leaseMock = jest.mocked(leaseAgentRunProviderCredentialsAsync);
const ctx = {
expoApiV2BaseUrl: 'https://staging-api.expo.test/v2/',
mcpServerUrl: 'wss://staging-mcp.expo.test',
} as any;
const inputs = {
agent_kind: { value: 'claude-code' },
agent_run_id: { value: '0199b0a0-1111-7222-8333-444455556666' },
max_duration_seconds: { value: 1800 },
prompt: { value: 'Fix the failing test.' },
};
const env = {
PATH: '/usr/local/bin:/usr/bin',
HOME: '/home/expo',
EXPO_TOKEN: 'expo-token',
__API_SERVER_URL: 'https://staging-api.expo.test',
EAS_BUILD_ID: 'build-id',
NODE_OPTIONS: '--max-old-space-size=3072',
USER_DEFINED_SECRET: 'secret',
};
async function runAsync(
overrides: {
ctx?: any;
inputs?: Record<string, { value: unknown }>;
env?: Record<string, string>;
} = {}
): Promise<void> {
const fn = createRunAgentBuildFunction(overrides.ctx ?? ctx);
await fn.fn!({ logger: createMockLogger() } as any, {
inputs: overrides.inputs ?? inputs,
outputs: {},
env: overrides.env ?? env,
});
}
function mockSuccessfulRun(): { mcpConfig?: any } {
const captured: { mcpConfig?: any } = {};
leaseMock.mockResolvedValue({ provider: 'anthropic', accessToken: 'anthropic-access-token' });
installMock.mockResolvedValue({} as any);
spawnMock.mockImplementation(((_command: string, _args: string[], spawnOptions: any) => {
const child = Object.assign(new EventEmitter(), {
stdin: { on: jest.fn(), end: jest.fn() },
stdout: new PassThrough(),
stderr: new PassThrough(),
});
captured.mcpConfig = JSON.parse(
vol.readFileSync(path.join(spawnOptions.env.HOME, 'mcp.json'), 'utf8') as string
);
process.nextTick(() => {
child.emit('exit', 0, null);
child.stdout.end();
child.stderr.end();
void Promise.all([once(child.stdout, 'end'), once(child.stderr, 'end')]).then(() =>
child.emit('close')
);
});
return child;
}) as any);
return captured;
}
describe(createRunAgentBuildFunction, () => {
it('is the eas/run_agent function', () => {
expect(createRunAgentBuildFunction(ctx).getFullId()).toBe('eas/run_agent');
});
it('passes none of the job environment to npm or the agent', async () => {
mockSuccessfulRun();
await runAsync();
expect(leaseMock).toHaveBeenCalledWith(
expect.objectContaining({
expoApiV2BaseUrl: 'https://staging-api.expo.test/v2/',
expoToken: 'expo-token',
agentRunId: '0199b0a0-1111-7222-8333-444455556666',
})
);
const npmEnv = installMock.mock.calls[0][2]!.env!;
const agentEnv = spawnMock.mock.calls[0][2].env!;
expect(Object.keys(npmEnv).sort()).toEqual(['HOME', 'LANG', 'PATH', 'TMPDIR']);
expect(Object.keys(agentEnv).sort()).toEqual([
'CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC',
'CLAUDE_CODE_OAUTH_TOKEN',
'HOME',
'LANG',
'PATH',
'TMPDIR',
]);
expect(agentEnv.PATH).toBe('/usr/local/bin:/usr/bin');
expect(agentEnv.HOME).not.toBe('/home/expo');
});
it.each([
['wss://staging-mcp.expo.test', 'https://staging-mcp.expo.test/mcp'],
['ws://localhost:8787', 'http://localhost:8787/mcp'],
])('points the agent at the HTTP endpoint of %s', async (mcpServerUrl, mcpUrl) => {
const captured = mockSuccessfulRun();
await runAsync({ ctx: { ...ctx, mcpServerUrl } });
expect(captured.mcpConfig.mcpServers.expo.url).toBe(mcpUrl);
});
it('stops the run at max_duration_seconds and reports a timeout', async () => {
jest.useFakeTimers({ doNotFake: ['nextTick', 'setImmediate', 'queueMicrotask'] });
const runAgentSpy = jest.spyOn(agentRun, 'runAgentAsync').mockImplementation(
({ signal }) =>
new Promise((_resolve, reject) => {
signal!.addEventListener('abort', () => reject(signal!.reason));
})
);
try {
const run = runAsync({ inputs: { ...inputs, max_duration_seconds: { value: 60 } } });
const rejection = expect(run).rejects.toMatchObject({
errorCode: 'EAS_RUN_AGENT_TIMEOUT',
message: expect.stringContaining('60 seconds'),
});
await jest.advanceTimersByTimeAsync(59_000);
expect(runAgentSpy.mock.calls[0][0].signal!.aborted).toBe(false);
await jest.advanceTimersByTimeAsync(1_000);
await rejection;
} finally {
runAgentSpy.mockRestore();
jest.useRealTimers();
}
});
it.each([
['an unknown agent_kind', { inputs: { ...inputs, agent_kind: { value: 'gemini' } } }],
['a malformed agent_run_id', { inputs: { ...inputs, agent_run_id: { value: 'not-a-uuid' } } }],
['an empty agent_run_id', { inputs: { ...inputs, agent_run_id: { value: '' } } }],
['a zero duration', { inputs: { ...inputs, max_duration_seconds: { value: 0 } } }],
['a negative duration', { inputs: { ...inputs, max_duration_seconds: { value: -300 } } }],
['a fractional duration', { inputs: { ...inputs, max_duration_seconds: { value: 12.5 } } }],
['an empty prompt', { inputs: { ...inputs, prompt: { value: '' } } }],
['no EXPO_TOKEN', { env: { ...env, EXPO_TOKEN: '' } }],
['no PATH', { env: { ...env, PATH: '' } }],
['no Expo API URL', { ctx: { ...ctx, expoApiV2BaseUrl: undefined } }],
['no MCP server URL', { ctx: { ...ctx, mcpServerUrl: undefined } }],
])('throws a system error for %s', async (_name, overrides) => {
await expect(runAsync(overrides)).rejects.toBeInstanceOf(SystemError);
expect(leaseMock).not.toHaveBeenCalled();
expect(installMock).not.toHaveBeenCalled();
expect(spawnMock).not.toHaveBeenCalled();
});
it('reports every invalid input at once', async () => {
await expect(
runAsync({
inputs: {
...inputs,
agent_run_id: { value: 'not-a-uuid' },
max_duration_seconds: { value: 0 },
},
})
).rejects.toThrow(
'The "agent_run_id" input must be the UUID of an agent run, but received "not-a-uuid". ' +
'The "max_duration_seconds" input must be a positive whole number of seconds, but received "0".'
);
});
});
@@ -20,7 +20,8 @@ describe(createUploadDeviceRunSessionScreenRecordingsBuildFunction, () => {
jest
.mocked(uploadDeviceRunSessionArtifactAsync)
.mockReset()
.mockImplementation(async (_ctx, { stream }) => {
.mockImplementation(async (_ctx, { openStream }) => {
const stream = openStream();
await new Promise<void>((resolve, reject) => {
stream.once('end', resolve);
stream.once('error', reject);
@@ -0,0 +1,129 @@
import { ProjectConfig } from '@expo/config';
import { BuildJob } from '@expo/eas-build-job';
import { createGlobalContextMock } from '../../../__tests__/utils/context';
import { createTestAndroidJob } from '../../../__tests__/utils/job';
import { createMockLogger } from '../../../__tests__/utils/logger';
import { BuildContext } from '../../../context';
import { CustomBuildContext } from '../../../customBuildContext';
import { readAppConfig } from '../../../utils/appConfig';
import { uploadEmbeddedBundleAsync } from '../../../utils/expoUpdatesEmbedded';
import { createUploadEmbeddedBundleBuildFunction } from '../uploadEmbeddedBundle';
jest.mock('../../../utils/appConfig');
jest.mock('../../../utils/expoUpdatesEmbedded');
function mockAppConfig(updatesUrl: string | undefined): void {
jest.mocked(readAppConfig).mockResolvedValue({
exp: { name: 'my-app', slug: 'my-app', updates: updatesUrl ? { url: updatesUrl } : undefined },
} as ProjectConfig);
}
function createCustomContext(job: BuildJob): CustomBuildContext<BuildJob> {
return new CustomBuildContext(
new BuildContext(job, {
env: { __API_SERVER_URL: 'http://api.expo.test' },
logBuffer: { getLogs: () => [], getPhaseLogs: () => [] },
logger: createMockLogger(),
uploadArtifact: jest.fn(),
workingdir: '',
})
);
}
describe(createUploadEmbeddedBundleBuildFunction, () => {
const job = createTestAndroidJob({});
const customContext = createCustomContext(job);
beforeEach(() => {
jest.resetAllMocks();
});
it('uploads the embedded bundle from the working directory', async () => {
mockAppConfig('https://u.expo.dev/project-id');
jest.mocked(uploadEmbeddedBundleAsync).mockResolvedValue({ status: 'uploaded' });
const globalContext = createGlobalContextMock({});
globalContext.updateEnv({ EAS_BUILD_ID: 'build-123' });
const buildStep =
createUploadEmbeddedBundleBuildFunction(customContext).createBuildStepFromFunctionCall(
globalContext
);
await buildStep.executeAsync();
expect(uploadEmbeddedBundleAsync).toHaveBeenCalledWith(
expect.objectContaining({
job,
env: expect.objectContaining({ EAS_BUILD_ID: 'build-123' }),
projectDir: globalContext.defaultWorkingDirectory,
appConfig: expect.objectContaining({ updates: { url: 'https://u.expo.dev/project-id' } }),
})
);
});
it('logs when the embedded bundle upload is skipped', async () => {
mockAppConfig('https://u.expo.dev/project-id');
jest.mocked(uploadEmbeddedBundleAsync).mockResolvedValue({ status: 'skipped' });
const buildStep = createUploadEmbeddedBundleBuildFunction(
customContext
).createBuildStepFromFunctionCall(createGlobalContextMock({}));
await buildStep.executeAsync();
expect(buildStep.ctx.logger.info).toHaveBeenCalledWith('Skipping embedded bundle upload.');
});
it('fails the step when the upload fails', async () => {
mockAppConfig('https://u.expo.dev/project-id');
jest.mocked(uploadEmbeddedBundleAsync).mockResolvedValue({ status: 'failed' });
const buildStep = createUploadEmbeddedBundleBuildFunction(
customContext
).createBuildStepFromFunctionCall(createGlobalContextMock({}));
await expect(buildStep.executeAsync()).rejects.toThrow('Failed to upload embedded bundle.');
});
it('does not fail the step when the upload fails and ignore_error is set', async () => {
mockAppConfig('https://u.expo.dev/project-id');
jest.mocked(uploadEmbeddedBundleAsync).mockResolvedValue({ status: 'failed' });
const buildStep = createUploadEmbeddedBundleBuildFunction(
customContext
).createBuildStepFromFunctionCall(createGlobalContextMock({}), {
callInputs: { ignore_error: true },
});
await buildStep.executeAsync();
expect(buildStep.ctx.logger.warn).toHaveBeenCalledWith(
expect.objectContaining({ err: expect.any(Error) }),
'Failed to upload embedded bundle. Ignoring error.'
);
});
it('fails the step when the app config cannot be read', async () => {
jest.mocked(readAppConfig).mockRejectedValue(new Error('Invalid app config'));
const buildStep = createUploadEmbeddedBundleBuildFunction(
customContext
).createBuildStepFromFunctionCall(createGlobalContextMock({}));
await expect(buildStep.executeAsync()).rejects.toThrow('Invalid app config');
expect(uploadEmbeddedBundleAsync).not.toHaveBeenCalled();
});
it('does not fail the step when the app config cannot be read and ignore_error is set', async () => {
jest.mocked(readAppConfig).mockRejectedValue(new Error('Invalid app config'));
const buildStep = createUploadEmbeddedBundleBuildFunction(
customContext
).createBuildStepFromFunctionCall(createGlobalContextMock({}), {
callInputs: { ignore_error: true },
});
await buildStep.executeAsync();
expect(buildStep.ctx.logger.warn).toHaveBeenCalledWith(
expect.objectContaining({ err: expect.any(Error) }),
'Failed to upload embedded bundle. Ignoring error.'
);
expect(uploadEmbeddedBundleAsync).not.toHaveBeenCalled();
});
});
@@ -0,0 +1,114 @@
import { SystemError, UserError } from '@expo/eas-build-job';
import {
BuildFunction,
BuildStepInput,
BuildStepInputValueTypeName,
BuildStepOutput,
} from '@expo/steps';
import { graphql } from 'gql.tada';
import { z } from 'zod';
import { downloadBuildAsync } from './downloadBuild';
import { CustomBuildContext } from '../../customBuildContext';
import { graphqlAbortContext } from '../../utils/graphqlAbort';
import { promiseRetryWithCondition } from '../../utils/promiseRetryWithCondition';
const CREATE_ARCHIVE_DOWNLOAD_URL = graphql(`
mutation GenerateSubmissionArchiveDownloadUrl($submissionId: ID!) {
submission {
generateSubmissionArchiveDownloadUrl(submissionId: $submissionId)
}
}
`);
export function createDownloadSubmissionArchiveFunction(ctx: CustomBuildContext): BuildFunction {
return new BuildFunction({
namespace: 'eas',
id: 'download_submission_archive',
name: 'Download submission archive',
__metricsId: 'eas/download_submission_archive',
inputProviders: [
BuildStepInput.createProvider({
id: 'submission_id',
required: true,
allowedValueTypeName: BuildStepInputValueTypeName.STRING,
}),
BuildStepInput.createProvider({
id: 'extensions',
required: false,
allowedValueTypeName: BuildStepInputValueTypeName.JSON,
defaultValue: ['apk', 'aab', 'ipa'],
}),
],
outputProviders: [
BuildStepOutput.createProvider({
id: 'artifact_path',
required: true,
}),
],
fn: async (stepsCtx, { inputs, outputs, signal }) => {
const { submissionId, extensions } = z
.object({
submissionId: z.string().uuid(),
extensions: z.array(z.string()),
})
.parse({
submissionId: inputs.submission_id.value,
extensions: inputs.extensions.value,
});
stepsCtx.logger.info(`Downloading archive for submission ${submissionId}...`);
const applicationArchiveUrl = await promiseRetryWithCondition(
async () => {
signal?.throwIfAborted();
const result = await ctx.graphqlClient
.mutation(CREATE_ARCHIVE_DOWNLOAD_URL, { submissionId }, graphqlAbortContext(signal))
.toPromise();
signal?.throwIfAborted();
if (result.error) {
throw result.error.networkError || result.error.response?.status >= 500
? new SystemError('Could not request the submission archive. Try again later.', {
cause: result.error,
})
: new UserError(
'EAS_SUBMISSION_ARCHIVE_FETCH_FAILED',
'Could not request the submission archive. Check your project access and the submission archive.',
{ cause: result.error }
);
}
const url = result.data?.submission.generateSubmissionArchiveDownloadUrl;
if (!url) {
throw new SystemError(
'The server did not return a submission archive URL. Try again later.'
);
}
return url;
},
error => !signal?.aborted && error instanceof SystemError,
{ retries: 3, minTimeout: 1000, maxTimeout: 1000 },
({ attemptNumber, maxAttemptsCount }) => {
stepsCtx.logger.info(
`Retrying submission archive URL request (${attemptNumber}/${maxAttemptsCount})...`
);
}
)();
const { artifactPath } = await downloadBuildAsync({
logger: stepsCtx.logger,
graphqlClient: ctx.graphqlClient,
applicationArchiveUrl,
robotAccessToken: null,
extensions,
signal,
});
outputs.artifact_path.set(artifactPath);
},
});
}
@@ -199,10 +199,15 @@ export function createInstallMaestroBuildFunction(): BuildFunction {
}
if (backend === 'maestro-runner' && global.runtimePlatform === BuildRuntimePlatform.DARWIN) {
await installMaestroRunnerWdaCache({
logger,
env,
});
// Runner 1.1.28 switched the default iOS driver to the bundled DeviceLab agent.
// Use the detected version after installation, including when `latest` was requested.
const installedRunnerVersion = semver.coerce(maestroVersionResult.value);
if (!installedRunnerVersion || semver.lt(installedRunnerVersion, '1.1.28')) {
await installMaestroRunnerWdaCache({
logger,
env,
});
}
}
logger.info(`${backend} ${maestroVersionResult.value} is ready.`);
@@ -2,6 +2,7 @@ import { bunyan } from '@expo/logger';
import { asyncResult } from '@expo/results';
import { BuildFunction, BuildRuntimePlatform, BuildStepEnv } from '@expo/steps';
import spawn from '@expo/turtle-spawn';
import fs from 'node:fs';
import { Sentry } from '../../sentry';
@@ -13,7 +14,10 @@ export function createInstallMitmproxyBuildFunction(): BuildFunction {
__metricsId: 'eas/install_mitmproxy',
supportedRuntimePlatforms: [BuildRuntimePlatform.DARWIN],
fn: async ({ logger }, { env }) => {
if (await isMitmproxyAvailableAsync(env)) {
// Some images ship the cask, and its quarantined first launch can hang.
const canLaunch =
env.EAS_BUILD_RUNNER !== 'eas-build' || (await removeMitmproxyQuarantineAsync(env));
if (canLaunch && (await isMitmproxyAvailableAsync(env))) {
logger.info('mitmproxy is already installed.');
return;
}
@@ -28,9 +32,11 @@ export function createInstallMitmproxyBuildFunction(): BuildFunction {
try {
logger.info('Installing mitmproxy with Homebrew.');
await installMitmproxyWithHomebrewAsync({ env, logger });
if (!(await removeMitmproxyQuarantineAsync(env))) {
throw new Error('Could not launch mitmproxy because it is still quarantined.');
}
if (!(await isMitmproxyAvailableAsync(env))) {
throw new Error('`brew install --cask mitmproxy` succeeded but mitmdump is not on PATH.');
throw new Error('`brew install --cask mitmproxy` succeeded but mitmdump did not run.');
}
logger.info('Installed mitmproxy.');
} catch (err) {
@@ -48,7 +54,18 @@ export function createInstallMitmproxyBuildFunction(): BuildFunction {
}
async function isMitmproxyAvailableAsync(env: BuildStepEnv): Promise<boolean> {
return (await asyncResult(spawn('mitmdump', ['--version'], { env }))).ok;
// Kills a first launch that Gatekeeper still holds, if the quarantine check missed it. Without
// pipes, a child process that outlives mitmdump cannot hold the call open past the timeout.
return (
await asyncResult(
spawn('mitmdump', ['--version'], {
env,
stdio: 'ignore',
timeout: 60_000,
killSignal: 'SIGKILL',
})
)
).ok;
}
async function installMitmproxyWithHomebrewAsync({
@@ -63,3 +80,25 @@ async function installMitmproxyWithHomebrewAsync({
logger,
});
}
/**
* Gatekeeper has rejected the mitmproxy 12.2.3 cask as unnotarized since 2026-10-06, and a
* quarantined first launch then never returns. Takes the app behind the `mitmdump` on PATH out of
* quarantine, and returns false when it stays quarantined, so it is not launched.
*/
async function removeMitmproxyQuarantineAsync(env: BuildStepEnv): Promise<boolean> {
const which = await asyncResult(spawn('which', ['mitmdump'], { env, stdio: 'pipe' }));
const mitmdump = which.ok
? await fs.promises.realpath(which.value.stdout.trim()).catch(() => null)
: null;
if (!mitmdump) {
return true;
}
const appEnd = mitmdump.lastIndexOf('.app/');
const app = appEnd === -1 ? mitmdump : mitmdump.slice(0, appEnd + '.app'.length);
await asyncResult(spawn('xattr', ['-dr', 'com.apple.quarantine', app], { env, stdio: 'pipe' }));
const quarantined = await asyncResult(
spawn('xattr', ['-p', 'com.apple.quarantine', mitmdump], { env, stdio: 'pipe' })
);
return !quarantined.ok;
}
@@ -0,0 +1,42 @@
import {
BuildFunction,
BuildStepInput,
BuildStepInputValueTypeName,
BuildStepOutput,
} from '@expo/steps';
import path from 'node:path';
import { z } from 'zod';
import { readAndroidArtifactInfoAsync } from '../utils/android/appArtifact';
export function createReadAndroidAppInfoBuildFunction(): BuildFunction {
return new BuildFunction({
namespace: 'eas',
id: 'read_android_app_info',
name: 'Read Android app info',
__metricsId: 'eas/read_android_app_info',
inputProviders: [
BuildStepInput.createProvider({
id: 'artifact_path',
required: true,
allowedValueTypeName: BuildStepInputValueTypeName.STRING,
}),
],
outputProviders: [
BuildStepOutput.createProvider({ id: 'artifact_type', required: true }),
BuildStepOutput.createProvider({ id: 'package_name', required: true }),
],
fn: async (ctx, { inputs, outputs, signal }) => {
const artifactPath = path.resolve(
ctx.workingDirectory,
z.string().min(1).parse(inputs.artifact_path.value)
);
ctx.logger.info(`Reading Android artifact: ${artifactPath}.`);
const artifact = await readAndroidArtifactInfoAsync(artifactPath, signal);
ctx.logger.info(`Android artifact type: ${artifact.artifactType}.`);
ctx.logger.info(`Android package name: ${artifact.packageName}.`);
outputs.artifact_type.set(artifact.artifactType);
outputs.package_name.set(artifact.packageName);
},
});
}
@@ -0,0 +1,136 @@
import { SystemError, UserError } from '@expo/eas-build-job';
import {
BuildFunction,
BuildRuntimePlatform,
BuildStepInput,
BuildStepInputValueTypeName,
} from '@expo/steps';
import { z } from 'zod';
import { CustomBuildContext } from '../../customBuildContext';
import { AGENT_KINDS, runAgentAsync } from '../utils/agentRun';
export function createRunAgentBuildFunction(ctx: CustomBuildContext): BuildFunction {
return new BuildFunction({
namespace: 'eas',
id: 'run_agent',
name: 'Run agent',
__metricsId: 'eas/run_agent',
supportedRuntimePlatforms: [BuildRuntimePlatform.LINUX],
inputProviders: [
BuildStepInput.createProvider({
id: 'agent_kind',
required: true,
allowedValueTypeName: BuildStepInputValueTypeName.STRING,
}),
BuildStepInput.createProvider({
id: 'agent_run_id',
required: true,
allowedValueTypeName: BuildStepInputValueTypeName.STRING,
}),
BuildStepInput.createProvider({
id: 'max_duration_seconds',
required: true,
allowedValueTypeName: BuildStepInputValueTypeName.NUMBER,
}),
BuildStepInput.createProvider({
id: 'prompt',
required: true,
allowedValueTypeName: BuildStepInputValueTypeName.STRING,
}),
],
fn: async (stepCtx, { inputs, env, signal }) => {
const inputErrors = {
agentKind: `The "agent_kind" input must be one of ${AGENT_KINDS.join(', ')}, but received "${String(inputs.agent_kind.value)}".`,
agentRunId: `The "agent_run_id" input must be the UUID of an agent run, but received "${String(inputs.agent_run_id.value)}".`,
maxDurationSeconds: `The "max_duration_seconds" input must be a positive whole number of seconds, but received "${String(inputs.max_duration_seconds.value)}".`,
prompt: 'The "prompt" input must be a non-empty string.',
};
const parsedInputs = z
.object({
agent_kind: z.enum(AGENT_KINDS, { error: inputErrors.agentKind }),
agent_run_id: z.uuid({ error: inputErrors.agentRunId }),
max_duration_seconds: z
.number({ error: inputErrors.maxDurationSeconds })
.int({ error: inputErrors.maxDurationSeconds })
.positive({ error: inputErrors.maxDurationSeconds }),
prompt: z.string({ error: inputErrors.prompt }).min(1, { error: inputErrors.prompt }),
})
.safeParse({
agent_kind: inputs.agent_kind.value,
agent_run_id: inputs.agent_run_id.value,
max_duration_seconds: inputs.max_duration_seconds.value,
prompt: inputs.prompt.value,
});
if (!parsedInputs.success) {
throw createValidationError(parsedInputs.error);
}
const {
agent_kind: agentKind,
agent_run_id: agentRunId,
max_duration_seconds: maxDurationSeconds,
prompt,
} = parsedInputs.data;
const parsedEnv = z
.object({
EXPO_TOKEN: z
.string({ error: 'EXPO_TOKEN is required to run an agent.' })
.min(1, { error: 'EXPO_TOKEN is required to run an agent.' }),
PATH: z
.string({ error: 'PATH is required to run an agent.' })
.min(1, { error: 'PATH is required to run an agent.' }),
EAS_BUILD_NPM_CACHE_URL: z.string().optional(),
})
.safeParse(env);
if (!parsedEnv.success) {
throw createValidationError(parsedEnv.error);
}
const {
EXPO_TOKEN: expoToken,
PATH: pathEnv,
EAS_BUILD_NPM_CACHE_URL: npmCacheUrl,
} = parsedEnv.data;
if (!ctx.expoApiV2BaseUrl) {
throw new SystemError('Expo API URL is required to run an agent.');
}
if (!ctx.mcpServerUrl) {
throw new SystemError('MCP server URL is required to run an agent.');
}
// The worker config holds the MCP server's WebSocket URL; its HTTP endpoint is on the same host.
const mcpUrl = new URL('/mcp', ctx.mcpServerUrl);
mcpUrl.protocol = mcpUrl.protocol.replace(/^ws/, 'http');
const timeoutError = new UserError(
'EAS_RUN_AGENT_TIMEOUT',
`The agent did not finish within its limit of ${maxDurationSeconds} seconds, so it was stopped. Raise the agent's maximum duration or give it a smaller task, then start a new agent run.`
);
const timeoutController = new AbortController();
const timeout = setTimeout(
() => timeoutController.abort(timeoutError),
maxDurationSeconds * 1000
);
try {
await runAgentAsync({
agentKind,
agentRunId,
prompt,
expoToken,
expoApiV2BaseUrl: ctx.expoApiV2BaseUrl,
mcpUrl: mcpUrl.toString(),
pathEnv,
npmCacheUrl,
logger: stepCtx.logger,
signal: signal
? AbortSignal.any([signal, timeoutController.signal])
: timeoutController.signal,
});
} finally {
clearTimeout(timeout);
}
},
});
}
function createValidationError(error: z.ZodError): SystemError {
return new SystemError(error.issues.map(issue => issue.message).join(' '));
}
@@ -1,5 +1,6 @@
import { SystemError } from '@expo/eas-build-job';
import { type bunyan } from '@expo/logger';
import { asyncResult } from '@expo/results';
import {
BuildFunction,
BuildRuntimePlatform,
@@ -37,6 +38,7 @@ import { startAppiumEventCollectionAsync } from '../utils/appiumEvents';
import {
createServeSimLaunchInputProviders,
describeServeSimLaunch,
ensureFfmpegInstalledOnceAsync,
finishRemoteSessionAsync,
getDeviceRunSessionIdOrThrow,
getNgrokAuthtokenOrThrow,
@@ -108,12 +110,17 @@ export function createStartAppiumRemoteSessionBuildFunction(
`Starting Appium remote session (version: ${versionSpec}, runtime: ${runtimePlatform}).`
);
const device = await resolveAppiumDeviceAsync({ runtimePlatform, env, logger });
const { appiumHome, appiumBinPath, appiumEnv } = await installAppiumAsync({
versionSpec,
driverName: device.driverName,
env,
logger,
});
// Appium's startRecordingScreen runs ffmpeg on this host: XCUITest encodes the
// simulator stream with it, and UiAutomator2 merges long recordings with it. The
// macOS session image does not ship ffmpeg, so install it concurrently with Appium.
// Both installs settle before the step continues or fails, so no install outlives it.
const [appiumInstall] = await Promise.all([
asyncResult(
installAppiumAsync({ versionSpec, driverName: device.driverName, env, logger })
),
ensureFfmpegInstalledOnceAsync({ runtimePlatform, env, logger }),
]);
const { appiumHome, appiumBinPath, appiumEnv } = appiumInstall.enforceValue();
const appiumProcess = spawnDetached({
command: appiumBinPath,
@@ -52,6 +52,8 @@ export function createStartSandboxBuildFunction(ctx: CustomBuildContext): BuildF
signal,
workingDirectory: stepCtx.workingDirectory,
env: commandEnv,
graphqlClient: ctx.graphqlClient,
sandboxId,
});
try {
await daemon.ready;
@@ -0,0 +1,57 @@
import { BuildJob, UserError } from '@expo/eas-build-job';
import { BuildFunction, BuildStepInput, BuildStepInputValueTypeName } from '@expo/steps';
import { CustomBuildContext } from '../../customBuildContext';
import { readAppConfig } from '../../utils/appConfig';
import { uploadEmbeddedBundleAsync } from '../../utils/expoUpdatesEmbedded';
export function createUploadEmbeddedBundleBuildFunction(
ctx: CustomBuildContext<BuildJob>
): BuildFunction {
return new BuildFunction({
namespace: 'eas',
id: 'upload_embedded_bundle',
name: 'Upload embedded bundle',
__metricsId: 'eas/upload_embedded_bundle',
inputProviders: [
BuildStepInput.createProvider({
id: 'ignore_error',
required: false,
allowedValueTypeName: BuildStepInputValueTypeName.BOOLEAN,
}),
],
fn: async (stepCtx, { env, inputs }) => {
try {
const appConfig = (
await readAppConfig({
projectDir: stepCtx.workingDirectory,
env,
logger: stepCtx.logger,
sdkVersion: stepCtx.global.staticContext.metadata?.sdkVersion,
})
).exp;
const { status } = await uploadEmbeddedBundleAsync({
job: ctx.job,
env,
logger: stepCtx.logger,
projectDir: stepCtx.workingDirectory,
appConfig,
});
if (status === 'skipped') {
stepCtx.logger.info('Skipping embedded bundle upload.');
} else if (status === 'failed') {
throw new UserError(
'EAS_UPLOAD_EMBEDDED_BUNDLE_FAILED',
'Failed to upload embedded bundle. See the warning above for details.'
);
}
} catch (err) {
if (inputs.ignore_error.value) {
stepCtx.logger.warn({ err }, 'Failed to upload embedded bundle. Ignoring error.');
return;
}
throw err;
}
},
});
}
@@ -153,8 +153,8 @@ describe(uploadAgentDeviceArtifactAsync, () => {
jest.mocked(fetch).mockResolvedValueOnce(new Response(Readable.from([data])));
jest
.mocked(uploadDeviceRunSessionArtifactAsync)
.mockImplementationOnce(async (_ctx, { stream }) => {
await readStreamAsync(stream);
.mockImplementationOnce(async (_ctx, { openStream }) => {
await readStreamAsync(openStream());
});
await uploadAgentDeviceArtifactAsync(ctx, {
@@ -179,7 +179,7 @@ describe(uploadAgentDeviceArtifactAsync, () => {
filename: 'report.json',
kind: 'agent-device-test-report',
size: data.length,
stream: expect.anything(),
openStream: expect.any(Function),
});
});
@@ -191,8 +191,8 @@ describe(uploadAgentDeviceArtifactAsync, () => {
jest.mocked(fetch).mockResolvedValueOnce(new Response(Readable.from([data])));
jest
.mocked(uploadDeviceRunSessionArtifactAsync)
.mockImplementationOnce(async (_ctx, { stream }) => {
await readStreamAsync(stream);
.mockImplementationOnce(async (_ctx, { openStream }) => {
await readStreamAsync(openStream());
});
await uploadAgentDeviceArtifactAsync(ctx, {
@@ -222,8 +222,8 @@ describe(uploadAgentDeviceArtifactAsync, () => {
jest.mocked(fetch).mockResolvedValueOnce(new Response(Readable.from([data])));
jest
.mocked(uploadDeviceRunSessionArtifactAsync)
.mockImplementationOnce(async (_ctx, { stream }) => {
await readStreamAsync(stream);
.mockImplementationOnce(async (_ctx, { openStream }) => {
await readStreamAsync(openStream());
});
await uploadAgentDeviceArtifactAsync(ctx, {
@@ -255,8 +255,8 @@ describe(uploadAgentDeviceArtifactAsync, () => {
jest.mocked(fetch).mockResolvedValueOnce(new Response(Readable.from([data])));
jest
.mocked(uploadDeviceRunSessionArtifactAsync)
.mockImplementationOnce(async (_ctx, { stream }) => {
await readStreamAsync(stream);
.mockImplementationOnce(async (_ctx, { openStream }) => {
await readStreamAsync(openStream());
});
await uploadAgentDeviceArtifactAsync(ctx, {
@@ -345,8 +345,8 @@ describe(pollAgentDeviceArtifactsForUploadAsync, () => {
jest
.mocked(uploadDeviceRunSessionArtifactAsync)
.mockRejectedValueOnce(new Error('upload failed'))
.mockImplementationOnce(async (_ctx, { stream }) => {
await readStreamAsync(stream);
.mockImplementationOnce(async (_ctx, { openStream }) => {
await readStreamAsync(openStream());
});
void pollAgentDeviceArtifactsForUploadAsync(ctx, {
@@ -0,0 +1,592 @@
import { SystemError } from '@expo/eas-build-job';
import turtleSpawn from '@expo/turtle-spawn';
import { vol } from 'memfs';
import { spawn } from 'node:child_process';
import { EventEmitter, once } from 'node:events';
import os from 'node:os';
import path from 'node:path';
import { PassThrough } from 'node:stream';
import { createMockLogger } from '../../../__tests__/utils/logger';
import { Sentry } from '../../../sentry';
import { killProcessGroup } from '../../../utils/processes';
import { runAgentAsync } from '../agentRun';
import {
type AgentRunProviderCredentials,
leaseAgentRunProviderCredentialsAsync,
} from '../agentRunLease';
import { CLAUDE_CODE_CLI } from '../agents/claude';
jest.mock('@expo/turtle-spawn', () => ({ __esModule: true, default: jest.fn() }));
jest.mock('node:child_process', () => ({
...jest.requireActual('node:child_process'),
spawn: jest.fn(),
}));
jest.mock('../agentRunLease');
jest.mock('../../../sentry');
jest.mock('../../../utils/processes', () => ({
...jest.requireActual('../../../utils/processes'),
killProcessGroup: jest.fn(),
}));
const installMock = jest.mocked(turtleSpawn);
const spawnMock = jest.mocked(spawn);
const leaseMock = jest.mocked(leaseAgentRunProviderCredentialsAsync);
const killProcessGroupMock = jest.mocked(killProcessGroup);
const agentRunId = '0199b0a0-1111-7222-8333-444455556666';
const anthropicLease: AgentRunProviderCredentials = {
provider: 'anthropic',
accessToken: 'anthropic-access-token',
};
const openaiLease: AgentRunProviderCredentials = {
provider: 'openai',
idToken: 'openai-id-token',
accessToken: 'openai-access-token',
accountId: 'openai-account-id',
};
interface AgentInvocation {
command: string;
args: string[];
env: Record<string, string>;
cwd: string;
stdin: jest.Mock;
child: EventEmitter & { stdout: PassThrough; stderr: PassThrough };
/** The run removes its files when it ends, so call this while the agent is running. */
readHomeFile(relativePath: string): { content: string; mode: number };
exit(status: number | null, signal?: NodeJS.Signals, options?: { closeOutput: boolean }): void;
closeOutput(): void;
}
function mockSpawn({
onAgent,
onInstall,
}: {
onAgent: (invocation: AgentInvocation) => void;
onInstall?: () => void;
}): AgentInvocation[] {
installMock.mockImplementation((() => {
try {
onInstall?.();
return Promise.resolve({});
} catch (error) {
return Promise.reject(error);
}
}) as any);
const invocations: AgentInvocation[] = [];
spawnMock.mockImplementation(((command: string, args: string[], spawnOptions: any) => {
const child = Object.assign(new EventEmitter(), {
pid: 4242,
stdin: { on: jest.fn(), end: jest.fn() },
stdout: new PassThrough(),
stderr: new PassThrough(),
});
// Like a real child: `close` comes after everything it printed has been read.
const closeOutput = (): void => {
child.stdout.end();
child.stderr.end();
void Promise.all([once(child.stdout, 'end'), once(child.stderr, 'end')]).then(() =>
child.emit('close')
);
};
const invocation: AgentInvocation = {
command,
args,
env: spawnOptions.env,
cwd: spawnOptions.cwd,
stdin: child.stdin.end,
child,
readHomeFile(relativePath) {
const filePath = path.join(spawnOptions.env.HOME, relativePath);
return {
content: vol.readFileSync(filePath, 'utf8') as string,
mode: vol.statSync(filePath).mode & 0o777,
};
},
exit(status, signal, options = { closeOutput: true }) {
process.nextTick(() => {
child.emit('exit', status, signal ?? null);
if (options.closeOutput) {
closeOutput();
}
});
},
closeOutput,
};
invocations.push(invocation);
onAgent(invocation);
return child;
}) as any);
return invocations;
}
function runDirectories(): string[] {
return (vol.readdirSync(os.tmpdir()) as string[]).filter(name =>
name.startsWith('eas-agent-run-')
);
}
function createOptions(
overrides: Partial<Parameters<typeof runAgentAsync>[0]> = {}
): Parameters<typeof runAgentAsync>[0] {
return {
agentKind: 'claude-code',
agentRunId,
prompt: 'Fix the failing test.',
expoToken: 'expo-token',
expoApiV2BaseUrl: 'https://staging-api.expo.test/v2/',
mcpUrl: 'https://staging-mcp.expo.test/mcp',
pathEnv: '/usr/local/bin:/usr/bin',
npmCacheUrl: 'https://npm-cache.expo.test',
logger: createMockLogger(),
signal: new AbortController().signal,
...overrides,
};
}
describe(runAgentAsync, () => {
afterEach(() => {
jest.useRealTimers();
expect(runDirectories()).toEqual([]);
});
it('runs Claude Code with MCP as its only tool source and an allowlisted environment', async () => {
leaseMock.mockResolvedValue(anthropicLease);
const invocations = mockSpawn({
onAgent: agent => {
const mcpConfig = agent.readHomeFile('mcp.json');
expect(JSON.parse(mcpConfig.content)).toEqual({
mcpServers: {
expo: {
type: 'http',
url: 'https://staging-mcp.expo.test/mcp',
headers: {
Authorization: 'Bearer expo-token',
'X-Expo-Agent-Run-Id': agentRunId,
},
},
},
});
expect(mcpConfig.mode).toBe(0o600);
agent.exit(0);
},
});
await runAgentAsync(createOptions());
expect(installMock.mock.calls[0].slice(0, 2)).toEqual([
'npm',
[
'install',
'--prefix',
expect.stringMatching(/eas-agent-run-.*\/cli$/),
'--no-save',
'--package-lock=false',
'--no-audit',
'--no-fund',
'--registry',
'https://npm-cache.expo.test',
'@anthropic-ai/[email protected]',
],
]);
expect(invocations).toHaveLength(1);
const [agent] = invocations;
const runDirectory = path.dirname(agent.env.HOME);
expect(path.dirname(runDirectory)).toBe(os.tmpdir());
expect(agent.command).toBe(path.join(runDirectory, 'cli', 'node_modules', '.bin', 'claude'));
expect(agent.args).toEqual([
'-p',
'--output-format',
'stream-json',
'--verbose',
'--tools',
'',
'--allowedTools',
'mcp__expo',
'--mcp-config',
path.join(agent.env.HOME, 'mcp.json'),
'--strict-mcp-config',
]);
expect(agent.env).toEqual({
PATH: '/usr/local/bin:/usr/bin',
HOME: path.join(runDirectory, 'home'),
TMPDIR: path.join(runDirectory, 'tmp'),
LANG: 'C.UTF-8',
CLAUDE_CODE_OAUTH_TOKEN: 'anthropic-access-token',
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: '1',
});
expect(agent.cwd).toBe(path.join(runDirectory, 'cwd'));
expect(agent.stdin).toHaveBeenCalledWith('Fix the failing test.');
});
it('writes the agent output as log lines with the leased credentials masked', async () => {
leaseMock.mockResolvedValue(anthropicLease);
const logger = createMockLogger();
mockSpawn({
onAgent: agent => {
// One JSON event split across two chunks, in the middle of the token.
agent.child.stdout.write(
'{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"toolu_1","content":"token: anthropic-acc'
);
agent.child.stdout.write('ess-token"}]}}\n');
agent.child.stdout.write(
'{"type":"assistant","message":{"content":[{"type":"text","text":"Done.\\nThe token was anthropic-access-token.\\n"}]}}\n'
);
agent.child.stdout.write(
'{"type":"result","subtype":"error_during_execution","is_error":true,"errors":["boom"]}\n'
);
agent.child.stderr.write('warning: anthropic-access-token\n');
agent.exit(0);
},
});
await runAgentAsync(createOptions({ logger }));
const child = jest.mocked(logger.child);
expect(child.mock.calls).toEqual([[{ source: 'stdout' }], [{ source: 'stderr' }]]);
const [stdoutLogger, stderrLogger] = child.mock.results.map(result => result.value);
expect(stdoutLogger.info.mock.calls).toEqual([
['Tool result: token: [redacted]'],
['Agent: Done.\nThe token was [redacted].'],
]);
expect(stdoutLogger.error.mock.calls).toEqual([['Agent failed: boom']]);
expect(stderrLogger.info.mock.calls).toEqual([['warning: [redacted]']]);
});
it('reports a line the formatter cannot handle instead of ending the run', async () => {
leaseMock.mockResolvedValue(anthropicLease);
const logger = createMockLogger();
const formatOutputLine = jest
.spyOn(CLAUDE_CODE_CLI, 'formatOutputLine')
.mockImplementationOnce(() => {
throw new RangeError('Maximum call stack size exceeded');
});
mockSpawn({
onAgent: agent => {
agent.child.stdout.write(
'{"type":"assistant","message":{"content":[{"type":"tool_use","name":"t","input":{}}]}}\n'
);
agent.child.stdout.write(
'{"type":"assistant","message":{"content":[{"type":"text","text":"Still here."}]}}\n'
);
agent.exit(0);
},
});
try {
await runAgentAsync(createOptions({ logger }));
} finally {
formatOutputLine.mockRestore();
}
expect(Sentry.capture).toHaveBeenCalledWith(
'Could not format a line of the agent output',
expect.any(RangeError)
);
const [stdoutLogger] = jest.mocked(logger.child).mock.results.map(result => result.value);
expect(stdoutLogger.info.mock.calls).toEqual([['Agent: Still here.']]);
});
it('runs Codex with its shell tool off and the credentials in a private home', async () => {
leaseMock.mockResolvedValue(openaiLease);
const invocations = mockSpawn({
onAgent: agent => {
const config = agent.readHomeFile('.codex/config.toml');
expect(config.content).toContain(
'[features]\nshell_tool = false\nview_image = false\nimage_generation = false\napps = false\nplugins = false\n'
);
expect(config.content).toContain('sandbox_mode = "read-only"');
expect(config.content).toContain('[agents]\nenabled = false\n');
expect(config.content).toContain(
[
'[mcp_servers.expo]',
'url = "http://localhost:8787/mcp"',
'default_tools_approval_mode = "approve"',
'',
'[mcp_servers.expo.http_headers]',
'"Authorization" = "Bearer expo-token"',
`"X-Expo-Agent-Run-Id" = "${agentRunId}"`,
].join('\n')
);
expect(config.mode).toBe(0o600);
const auth = agent.readHomeFile('.codex/auth.json');
expect(JSON.parse(auth.content)).toEqual({
auth_mode: 'chatgptAuthTokens',
tokens: {
id_token: 'openai-id-token',
access_token: 'openai-access-token',
refresh_token: '',
account_id: 'openai-account-id',
},
last_refresh: expect.any(String),
});
expect(auth.mode).toBe(0o600);
agent.exit(0);
},
});
await runAgentAsync(
createOptions({
agentKind: 'codex',
mcpUrl: 'http://localhost:8787/mcp',
npmCacheUrl: undefined,
})
);
const npmArgs = installMock.mock.calls[0][1]!;
expect(npmArgs).not.toContain('--registry');
expect(npmArgs.at(-1)).toBe('@openai/[email protected]');
const [agent] = invocations;
const runDirectory = path.dirname(agent.env.HOME);
expect(agent.command).toBe(path.join(runDirectory, 'cli', 'node_modules', '.bin', 'codex'));
expect(agent.args).toEqual(['exec', '--strict-config', '--json', '--skip-git-repo-check']);
expect(agent.cwd).toBe(path.join(runDirectory, 'cwd'));
expect(agent.env).toEqual({
PATH: '/usr/local/bin:/usr/bin',
HOME: path.join(runDirectory, 'home'),
TMPDIR: path.join(runDirectory, 'tmp'),
LANG: 'C.UTF-8',
});
expect(agent.stdin).toHaveBeenCalledWith('Fix the failing test.');
});
it('escalates to SIGKILL when the agent ignores the stop', async () => {
jest.useFakeTimers({ doNotFake: ['nextTick', 'setImmediate', 'queueMicrotask'] });
leaseMock.mockResolvedValue(anthropicLease);
const controller = new AbortController();
let agentStarted!: () => void;
const started = new Promise<void>(resolve => {
agentStarted = resolve;
});
const invocations = mockSpawn({ onAgent: () => agentStarted() });
const run = runAgentAsync(createOptions({ signal: controller.signal }));
const rejection = expect(run).rejects.toMatchObject({ name: 'AbortError' });
await started;
const [agent] = invocations;
controller.abort();
expect(killProcessGroupMock.mock.calls).toEqual([[agent.child, 'SIGTERM']]);
await jest.advanceTimersByTimeAsync(10_000);
expect(killProcessGroupMock.mock.calls).toEqual([
[agent.child, 'SIGTERM'],
[agent.child, 'SIGKILL'],
]);
agent.exit(null, 'SIGKILL');
await rejection;
expect(leaseMock).toHaveBeenCalledTimes(1);
});
it('ends what the agent left running and waits a bounded time for its output', async () => {
jest.useFakeTimers({ doNotFake: ['nextTick', 'setImmediate', 'queueMicrotask'] });
leaseMock.mockResolvedValue(anthropicLease);
let agentStarted!: () => void;
const started = new Promise<void>(resolve => {
agentStarted = resolve;
});
const invocations = mockSpawn({ onAgent: () => agentStarted() });
const run = runAgentAsync(createOptions());
await started;
invocations[0].exit(0, undefined, { closeOutput: false });
await jest.advanceTimersByTimeAsync(5_000);
await expect(run).resolves.toBeUndefined();
expect(killProcessGroupMock.mock.calls).toEqual([[invocations[0].child, 'SIGKILL']]);
});
it('stops the agent when the step is aborted and fails with the abort reason', async () => {
leaseMock.mockResolvedValue(anthropicLease);
const controller = new AbortController();
const reason = new Error('the step was canceled');
const invocations = mockSpawn({
onAgent: agent => {
queueMicrotask(() => {
controller.abort(reason);
agent.exit(null, 'SIGTERM');
});
},
});
await expect(runAgentAsync(createOptions({ signal: controller.signal }))).rejects.toBe(reason);
expect(killProcessGroupMock.mock.calls).toEqual([
[invocations[0].child, 'SIGTERM'],
[invocations[0].child, 'SIGKILL'],
]);
expect(leaseMock).toHaveBeenCalledTimes(1);
});
it('resumes Claude Code with new credentials when the lease changed during the run', async () => {
const renewedLease: AgentRunProviderCredentials = {
...anthropicLease,
accessToken: 'renewed-access-token',
};
leaseMock.mockResolvedValueOnce(anthropicLease).mockResolvedValueOnce(renewedLease);
const logger = createMockLogger();
const invocations = mockSpawn({
onAgent: agent => {
if (invocations.length === 2) {
agent.child.stderr.write('tokens: anthropic-access-token renewed-access-token\n');
}
agent.exit(invocations.length === 1 ? 1 : 0);
},
});
await runAgentAsync(createOptions({ logger }));
expect(installMock).toHaveBeenCalledTimes(1);
expect(invocations).toHaveLength(2);
const [first, resumed] = invocations;
expect(resumed.args).toEqual(['-p', '--continue', ...first.args.slice(1)]);
expect(resumed.env.CLAUDE_CODE_OAUTH_TOKEN).toBe('renewed-access-token');
expect(resumed.stdin).toHaveBeenCalledWith(
expect.stringMatching(/^Your previous run of this task stopped.*\n\nFix the failing test\.$/s)
);
const child = jest.mocked(logger.child);
expect(child.mock.calls).toEqual([
[{ source: 'stdout' }],
[{ source: 'stderr' }],
[{ source: 'stdout' }],
[{ source: 'stderr' }],
]);
const [, , , resumedStderrLogger] = child.mock.results.map(result => result.value);
expect(resumedStderrLogger.info.mock.calls).toEqual([['tokens: [redacted] [redacted]']]);
});
it('resumes Codex with new credentials when the lease changed during the run', async () => {
const renewedLease: AgentRunProviderCredentials = {
...openaiLease,
accessToken: 'renewed-access-token',
idToken: 'renewed-id-token',
};
leaseMock.mockResolvedValueOnce(openaiLease).mockResolvedValueOnce(renewedLease);
const invocations = mockSpawn({
onAgent: agent => {
if (invocations.length === 1) {
agent.exit(1);
return;
}
expect(JSON.parse(agent.readHomeFile('.codex/auth.json').content).tokens).toMatchObject({
id_token: 'renewed-id-token',
access_token: 'renewed-access-token',
});
agent.exit(0);
},
});
await runAgentAsync(createOptions({ agentKind: 'codex' }));
expect(invocations).toHaveLength(2);
expect(invocations[1].args).toEqual([
'exec',
'--strict-config',
'--json',
'--skip-git-repo-check',
'resume',
'--last',
'-',
]);
expect(invocations[1].stdin).toHaveBeenCalledWith(
expect.stringContaining('\n\nFix the failing test.')
);
expect(invocations[1].cwd).toBe(invocations[0].cwd);
});
it('does not resume an agent when the step was aborted while its credentials were renewed', async () => {
const controller = new AbortController();
leaseMock.mockResolvedValueOnce(anthropicLease).mockImplementationOnce(async () => {
controller.abort();
return { ...anthropicLease, accessToken: 'renewed-access-token' };
});
const invocations = mockSpawn({ onAgent: agent => agent.exit(1) });
await expect(runAgentAsync(createOptions({ signal: controller.signal }))).rejects.toMatchObject(
{ name: 'AbortError' }
);
expect(invocations).toHaveLength(1);
});
it.each([
[2, undefined, 'exited with code 2'],
[null, 'SIGKILL' as const, 'was terminated by SIGKILL'],
])(
'fails when the agent fails and the lease is unchanged (status %p, signal %p)',
async (status, signal, description) => {
leaseMock.mockResolvedValue(anthropicLease);
const invocations = mockSpawn({ onAgent: agent => agent.exit(status, signal) });
await expect(runAgentAsync(createOptions())).rejects.toMatchObject({
errorCode: 'EAS_RUN_AGENT_FAILED',
message: expect.stringContaining(description),
});
expect(invocations).toHaveLength(1);
expect(leaseMock).toHaveBeenCalledTimes(2);
}
);
it('fails with the lease error when the lease cannot be renewed after the agent failed', async () => {
leaseMock
.mockResolvedValueOnce(anthropicLease)
.mockRejectedValueOnce(new Error('lease refused'));
const invocations = mockSpawn({ onAgent: agent => agent.exit(1) });
await expect(runAgentAsync(createOptions())).rejects.toThrow('lease refused');
expect(invocations).toHaveLength(1);
});
it('reports an agent CLI that cannot start', async () => {
leaseMock.mockResolvedValue(anthropicLease);
mockSpawn({
onAgent: agent => {
process.nextTick(() => agent.child.emit('error', new Error('spawn claude ENOENT')));
},
});
await expect(runAgentAsync(createOptions())).rejects.toThrow(
'Could not start the agent CLI on the worker: spawn claude ENOENT.'
);
expect(leaseMock).toHaveBeenCalledTimes(1);
});
it('cleans up when the installation fails', async () => {
leaseMock.mockResolvedValue(anthropicLease);
const invocations = mockSpawn({
onInstall: () => {
throw new Error('npm ERR! network');
},
onAgent: agent => agent.exit(0),
});
await expect(runAgentAsync(createOptions())).rejects.toBeInstanceOf(SystemError);
expect(invocations).toHaveLength(0);
});
it('does not install or start anything when the lease is refused', async () => {
leaseMock.mockRejectedValue(new Error('lease refused'));
mockSpawn({ onAgent: agent => agent.exit(0) });
await expect(runAgentAsync(createOptions())).rejects.toThrow('lease refused');
expect(installMock).not.toHaveBeenCalled();
expect(spawnMock).not.toHaveBeenCalled();
});
it('refuses credentials of the wrong provider', async () => {
leaseMock.mockResolvedValue(openaiLease);
const invocations = mockSpawn({ onAgent: agent => agent.exit(0) });
await expect(runAgentAsync(createOptions({ agentKind: 'claude-code' }))).rejects.toBeInstanceOf(
SystemError
);
expect(invocations).toHaveLength(0);
});
});
@@ -0,0 +1,148 @@
import { SystemError, UserError } from '@expo/eas-build-job';
import fetch, { type Response } from 'node-fetch';
import { leaseAgentRunProviderCredentialsAsync } from '../agentRunLease';
jest.mock('node-fetch');
const fetchMock = jest.mocked(fetch);
afterEach(() => {
jest.useRealTimers();
});
const agentRunId = '0199b0a0-1111-7222-8333-444455556666';
const options = {
expoApiV2BaseUrl: 'https://staging-api.expo.test/v2/',
expoToken: 'expo-token',
agentRunId,
};
function createResponse(status: number, body: unknown): Response {
return {
ok: status >= 200 && status < 300,
status,
json: async () => {
if (body === undefined) {
throw new Error('not JSON');
}
return body;
},
} as unknown as Response;
}
describe(leaseAgentRunProviderCredentialsAsync, () => {
it('leases Anthropic credentials with the job token', async () => {
const lease = { provider: 'anthropic', accessToken: 'access-token' };
fetchMock.mockResolvedValue(
createResponse(200, { data: { ...lease, expiresAt: '2026-10-06T12:00:00.000Z' } })
);
await expect(leaseAgentRunProviderCredentialsAsync(options)).resolves.toEqual(lease);
expect(fetchMock).toHaveBeenCalledTimes(1);
const [url, init] = fetchMock.mock.calls[0];
expect(url).toBe(`https://staging-api.expo.test/v2/agent-runs/${agentRunId}/auth-lease`);
expect(init).toMatchObject({
method: 'POST',
headers: { Authorization: 'Bearer expo-token' },
});
});
it('leases OpenAI credentials', async () => {
const lease = {
provider: 'openai',
idToken: 'id-token',
accessToken: 'access-token',
accountId: 'account-id',
};
fetchMock.mockResolvedValue(createResponse(200, { data: lease }));
await expect(leaseAgentRunProviderCredentialsAsync(options)).resolves.toEqual(lease);
});
it('reports a 4xx with the message from the server and does not retry', async () => {
fetchMock.mockResolvedValue(
createResponse(400, {
errors: [
{
code: 'AGENT_RUN_PROVIDER_CONNECTION_DELETED',
message: 'The provider connection for this agent run was deleted.',
},
],
})
);
const lease = leaseAgentRunProviderCredentialsAsync(options);
await expect(lease).rejects.toBeInstanceOf(UserError);
await expect(lease).rejects.toMatchObject({
errorCode: 'EAS_RUN_AGENT_CREDENTIALS_UNAVAILABLE',
message: 'The provider connection for this agent run was deleted.',
});
expect(fetchMock).toHaveBeenCalledTimes(1);
});
it('reports a 4xx without a readable body', async () => {
fetchMock.mockResolvedValue(createResponse(401, undefined));
await expect(leaseAgentRunProviderCredentialsAsync(options)).rejects.toThrow(
'Expo refused to issue provider credentials for this agent run (HTTP 401).'
);
expect(fetchMock).toHaveBeenCalledTimes(1);
});
it('retries server errors and network failures', async () => {
jest.useFakeTimers();
const lease = { provider: 'anthropic', accessToken: 'access-token' };
fetchMock
.mockResolvedValueOnce(createResponse(503, undefined))
.mockRejectedValueOnce(new Error('socket hang up'))
.mockResolvedValueOnce(createResponse(200, { data: lease }));
const assertion = expect(leaseAgentRunProviderCredentialsAsync(options)).resolves.toEqual(
lease
);
await jest.runAllTimersAsync();
await assertion;
expect(fetchMock).toHaveBeenCalledTimes(3);
});
it('retries when the body of a successful response cannot be read', async () => {
jest.useFakeTimers();
const lease = { provider: 'anthropic', accessToken: 'access-token' };
fetchMock
.mockResolvedValueOnce(createResponse(200, undefined))
.mockResolvedValueOnce(createResponse(200, { data: lease }));
const assertion = expect(leaseAgentRunProviderCredentialsAsync(options)).resolves.toEqual(
lease
);
await jest.runAllTimersAsync();
await assertion;
expect(fetchMock).toHaveBeenCalledTimes(2);
});
it('gives up after three failed attempts', async () => {
jest.useFakeTimers();
fetchMock.mockResolvedValue(createResponse(500, undefined));
const assertion = expect(leaseAgentRunProviderCredentialsAsync(options)).rejects.toBeInstanceOf(
SystemError
);
await jest.runAllTimersAsync();
await assertion;
expect(fetchMock).toHaveBeenCalledTimes(3);
});
it('rejects a successful response that is not a lease', async () => {
fetchMock.mockResolvedValue(
createResponse(200, { data: { provider: 'anthropic', refreshToken: 'nope' } })
);
await expect(leaseAgentRunProviderCredentialsAsync(options)).rejects.toBeInstanceOf(
SystemError
);
expect(fetchMock).toHaveBeenCalledTimes(1);
});
});
@@ -124,8 +124,8 @@ describe(uploadArgentArtifactAsync, () => {
jest.mocked(fetch).mockResolvedValueOnce(new Response(Readable.from([data])));
jest
.mocked(uploadDeviceRunSessionArtifactAsync)
.mockImplementationOnce(async (_ctx, { stream }) => {
await readStreamAsync(stream);
.mockImplementationOnce(async (_ctx, { openStream }) => {
await readStreamAsync(openStream());
});
await uploadArgentArtifactAsync(ctx, {
@@ -151,7 +151,7 @@ describe(uploadArgentArtifactAsync, () => {
filename: 'report.json',
kind: undefined,
size: data.length,
stream: expect.anything(),
openStream: expect.any(Function),
});
});
@@ -168,8 +168,8 @@ describe(uploadArgentArtifactAsync, () => {
.mockResolvedValueOnce(new Response(Readable.from([Buffer.from('artifact-data')])));
jest
.mocked(uploadDeviceRunSessionArtifactAsync)
.mockImplementationOnce(async (_ctx, { stream }) => {
await readStreamAsync(stream);
.mockImplementationOnce(async (_ctx, { openStream }) => {
await readStreamAsync(openStream());
});
await uploadArgentArtifactAsync(ctx, {
@@ -345,8 +345,8 @@ describe(pollArgentArtifactsForUploadAsync, () => {
});
jest
.mocked(uploadDeviceRunSessionArtifactAsync)
.mockImplementation(async (_ctx, { stream }) => {
await readStreamAsync(stream);
.mockImplementation(async (_ctx, { openStream }) => {
await readStreamAsync(openStream());
});
const pollingPromise = pollArgentArtifactsForUploadAsync(ctx, {
@@ -371,7 +371,7 @@ describe(pollArgentArtifactsForUploadAsync, () => {
name: 'b.json (artifact-b)',
filename: 'b.json',
size: 'artifact-b-data'.length,
stream: expect.anything(),
openStream: expect.any(Function),
});
});
@@ -49,7 +49,7 @@ describe(uploadDeviceRunSessionArtifactAsync, () => {
kind: 'agent-device-test-report',
metadata: { firstFrameRecordAt: 'test-time' },
size: reportedSize,
stream,
openStream: () => stream,
});
expect(mutation).toHaveBeenCalledWith(
@@ -103,7 +103,7 @@ describe(uploadDeviceRunSessionArtifactAsync, () => {
filename: 'logs.ndjson',
kind: 'simulator-log',
size: 0,
stream: Readable.from([]),
openStream: () => Readable.from([]),
signal: controller.signal,
})
).rejects.toThrow();
@@ -0,0 +1,400 @@
import { type bunyan } from '@expo/logger';
import { BuildRuntimePlatform } from '@expo/steps';
import spawn from '@expo/turtle-spawn';
import { Client, fetchExchange } from '@urql/core';
import { readFile, readdir, writeFile } from 'node:fs/promises';
import { Readable } from 'node:stream';
import os from 'node:os';
import fetch from 'node-fetch';
import { type CustomBuildContext } from '../../../customBuildContext';
import {
captureDeviceRunSessionPreviewAsync,
ensureMacosPreviewEncoderInstalledAsync,
startDeviceRunSessionPreview,
} from '../deviceRunSessionPreview';
jest.mock('node-fetch');
jest.mock('@expo/turtle-spawn');
const { Response } = jest.requireActual('node-fetch') as typeof import('node-fetch');
describe(startDeviceRunSessionPreview, () => {
const image = Buffer.from('webp-image');
let captureAsync: jest.Mock<Promise<Buffer>, [AbortSignal]>;
let mutation: jest.Mock;
let logger: bunyan;
let ctx: CustomBuildContext;
let preview: ReturnType<typeof startDeviceRunSessionPreview> | undefined;
beforeEach(() => {
jest.useFakeTimers();
captureAsync = jest.fn().mockResolvedValue(image);
mutation = jest.fn().mockReturnValue({
toPromise: async () => ({
data: {
deviceRunSession: {
createPreviewUploadSession: {
uploadSession: {
url: 'https://uploads.expo.test/preview',
headers: { 'content-type': 'image/webp' },
},
},
},
},
}),
});
ctx = { graphqlClient: { mutation } } as unknown as CustomBuildContext;
logger = { warn: jest.fn() } as unknown as bunyan;
jest
.mocked(fetch)
.mockReset()
.mockResolvedValue(new Response('', { status: 200 }));
});
afterEach(async () => {
await preview?.stopAsync();
preview = undefined;
jest.useRealTimers();
});
const start = () => {
preview = startDeviceRunSessionPreview({
ctx,
deviceRunSessionId: 'session-id',
captureAsync,
logger,
});
return preview;
};
it('uploads immediately and reuses the URL every 60 seconds', async () => {
start();
await jest.advanceTimersByTimeAsync(0);
expect(fetch).toHaveBeenCalledWith(
'https://uploads.expo.test/preview',
expect.objectContaining({
method: 'PUT',
body: image,
headers: { 'content-type': 'image/webp' },
})
);
await jest.advanceTimersByTimeAsync(59_999);
expect(captureAsync).toHaveBeenCalledTimes(1);
await jest.advanceTimersByTimeAsync(1);
expect(captureAsync).toHaveBeenCalledTimes(2);
expect(mutation).toHaveBeenCalledTimes(1);
await preview!.stopAsync();
await jest.advanceTimersByTimeAsync(120_000);
expect(captureAsync).toHaveBeenCalledTimes(2);
});
it('retries capture errors without creating an empty artifact', async () => {
captureAsync.mockRejectedValueOnce(new Error('device not ready'));
start();
await jest.advanceTimersByTimeAsync(0);
expect(mutation).not.toHaveBeenCalled();
expect(logger.warn).toHaveBeenCalled();
await jest.advanceTimersByTimeAsync(60_000);
expect(fetch).toHaveBeenCalledTimes(1);
});
it('retries a failed upload with the same URL', async () => {
jest.mocked(fetch).mockResolvedValueOnce(new Response('', { status: 403 }));
start();
await jest.advanceTimersByTimeAsync(60_000);
expect(mutation).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenCalledTimes(2);
});
it('does not log the signed upload URL when the upload request fails', async () => {
const { FetchError } = jest.requireActual('node-fetch') as typeof import('node-fetch');
jest
.mocked(fetch)
.mockRejectedValueOnce(
new FetchError('network timeout at: https://uploads.expo.test/preview', 'request-timeout')
);
start();
await jest.advanceTimersByTimeAsync(0);
expect(logger.warn).toHaveBeenCalledTimes(1);
const [fields] = jest.mocked(logger.warn).mock.calls[0] as unknown as [{ err: Error }];
expect(fields.err.message).toBe(
'Session preview upload request failed (name: FetchError, type: request-timeout).'
);
expect(JSON.stringify(jest.mocked(logger.warn).mock.calls)).not.toContain('uploads.expo.test');
});
it('reuses the upload URL for the full session', async () => {
start();
await jest.advanceTimersByTimeAsync(119 * 60_000);
expect(mutation).toHaveBeenCalledTimes(1);
});
it('does not overlap captures and aborts pending work when stopped', async () => {
captureAsync.mockImplementation(
signal =>
new Promise((_, reject) => {
signal.addEventListener('abort', () => reject(new Error('aborted')), { once: true });
})
);
const handle = start();
await jest.advanceTimersByTimeAsync(60_000);
expect(captureAsync).toHaveBeenCalledTimes(1);
await handle.stopAsync();
expect(captureAsync.mock.calls[0][0].aborted).toBe(true);
expect(fetch).not.toHaveBeenCalled();
});
it('does not upload a capture that finishes after shutdown', async () => {
let finish!: (image: Buffer) => void;
captureAsync.mockImplementation(
() =>
new Promise(resolve => {
finish = resolve;
})
);
const handle = start();
const stopping = handle.stopAsync();
finish(image);
await stopping;
expect(mutation).not.toHaveBeenCalled();
});
});
describe('session preview with the real GraphQL client', () => {
const uploadSession = {
url: 'https://uploads.expo.test/preview',
headers: { 'content-type': 'image/webp' },
};
let graphqlFetch: jest.SpyInstance;
let preview: ReturnType<typeof startDeviceRunSessionPreview> | undefined;
let logger: bunyan;
beforeEach(() => {
jest.useFakeTimers();
graphqlFetch = jest.spyOn(globalThis, 'fetch').mockResolvedValue(
new globalThis.Response(
JSON.stringify({
data: { deviceRunSession: { createPreviewUploadSession: { uploadSession } } },
}),
{ headers: { 'content-type': 'application/json' } }
)
);
logger = { warn: jest.fn() } as unknown as bunyan;
jest
.mocked(fetch)
.mockReset()
.mockResolvedValue(new Response('', { status: 200 }));
});
afterEach(async () => {
await preview?.stopAsync();
preview = undefined;
jest.restoreAllMocks();
jest.useRealTimers();
});
const start = () => {
const graphqlClient = new Client({
url: 'https://api.expo.test/graphql',
exchanges: [fetchExchange],
fetchOptions: { headers: { Authorization: 'Bearer worker-token' } },
});
preview = startDeviceRunSessionPreview({
ctx: { graphqlClient } as CustomBuildContext,
deviceRunSessionId: 'session-id',
captureAsync: async () => Buffer.from('webp-image'),
logger,
});
return preview;
};
it('preserves the worker authentication when creating the upload session', async () => {
start();
await jest.advanceTimersByTimeAsync(0);
const [, options] = graphqlFetch.mock.calls[0];
expect(new Headers(options.headers).get('authorization')).toBe('Bearer worker-token');
expect(fetch).toHaveBeenCalledTimes(1);
});
it.each(['shutdown', 'timeout'])('aborts a stalled GraphQL request on %s', async reason => {
const timeout = new AbortController();
jest.spyOn(AbortSignal, 'timeout').mockReturnValue(timeout.signal);
let requestSignal: AbortSignal | undefined;
let rejectRequest!: (error: Error) => void;
graphqlFetch.mockImplementation(
(_url, options) =>
new Promise((_, reject) => {
rejectRequest = reject;
requestSignal = options.signal;
requestSignal!.addEventListener('abort', () => reject(requestSignal!.reason), {
once: true,
});
})
);
const handle = start();
await jest.advanceTimersByTimeAsync(0);
let stopped = false;
let stopping: Promise<void> | undefined;
if (reason === 'shutdown') {
stopping = handle.stopAsync().then(() => {
stopped = true;
});
} else {
timeout.abort(new Error('preview attempt timed out'));
}
await jest.advanceTimersByTimeAsync(0);
const aborted = requestSignal!.aborted;
const stoppedAfterAbort = stopped;
// Always release the fake transport so a regression cannot hang test cleanup.
rejectRequest(new Error('test cleanup'));
await jest.advanceTimersByTimeAsync(0);
expect(aborted).toBe(true);
expect(fetch).not.toHaveBeenCalled();
if (stopping) {
expect(stoppedAfterAbort).toBe(true);
await stopping;
} else {
expect(logger.warn).toHaveBeenCalled();
graphqlFetch.mockResolvedValue(
new globalThis.Response(
JSON.stringify({
data: { deviceRunSession: { createPreviewUploadSession: { uploadSession } } },
}),
{ headers: { 'content-type': 'application/json' } }
)
);
jest.spyOn(AbortSignal, 'timeout').mockReturnValue(new AbortController().signal);
await jest.advanceTimersByTimeAsync(60_000);
expect(fetch).toHaveBeenCalledTimes(1);
}
});
});
describe(captureDeviceRunSessionPreviewAsync, () => {
const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0xff, 0x00]);
const webp = Buffer.from('encoded-webp');
const resized = Buffer.from('resized-png');
it.each([
[BuildRuntimePlatform.DARWIN, 'selected-device'],
[BuildRuntimePlatform.LINUX, 'selected-device'],
[BuildRuntimePlatform.LINUX, 'no-device-id'],
] as const)(
'captures the selected device on %s and removes temporary files',
async (runtimePlatform, device) => {
jest.mocked(spawn).mockImplementation((command, args) => {
if (command === 'adb' && args[0] === 'devices') {
return Promise.resolve({
stdout: 'List of devices attached\nselected-device\tdevice\noffline-device\toffline\n',
}) as ReturnType<typeof spawn>;
}
if (command === 'adb') {
expect(args).toEqual(['-s', 'selected-device', 'exec-out', 'screencap', '-p']);
return Object.assign(Promise.resolve({ stdout: '', stderr: '' }), {
child: { stdout: Readable.from([png]) },
}) as ReturnType<typeof spawn>;
}
return (async () => {
if (command === 'xcrun') {
expect(args.slice(0, 4)).toEqual(['simctl', 'io', 'selected-device', 'screenshot']);
await writeFile(args[4], Uint8Array.from(png));
} else if (command === 'sips') {
expect(args.slice(0, 2)).toEqual(['-Z', '320']);
expect(await readFile(args[2])).toEqual(png);
await writeFile(args[args.indexOf('--out') + 1], Uint8Array.from(resized));
} else if (command === 'cwebp') {
expect(await readFile(args[args.indexOf('-o') - 1])).toEqual(resized);
await writeFile(args[args.indexOf('-o') + 1], Uint8Array.from(webp));
} else {
// Homebrew's FFmpeg cannot encode WebP, so macOS must not use it.
expect(runtimePlatform).toBe(BuildRuntimePlatform.LINUX);
expect(command).toBe('ffmpeg');
expect(await readFile(args[args.indexOf('-i') + 1])).toEqual(png);
expect(args).toContain('libwebp');
await writeFile(args[args.length - 1], Uint8Array.from(webp));
}
return { stdout: '', stderr: '' };
})() as ReturnType<typeof spawn>;
});
expect(
await captureDeviceRunSessionPreviewAsync({
runtimePlatform,
device,
env: {},
signal: new AbortController().signal,
})
).toEqual(webp);
expect(
(await readdir(os.tmpdir())).filter(name => name.startsWith('session-preview-'))
).toEqual([]);
}
);
it('skips ambiguous Android devices instead of capturing the wrong screen', async () => {
jest
.mocked(spawn)
.mockResolvedValue({ stdout: 'device-one\tdevice\ndevice-two\tdevice\n' } as Awaited<
ReturnType<typeof spawn>
>);
await expect(
captureDeviceRunSessionPreviewAsync({
runtimePlatform: BuildRuntimePlatform.LINUX,
device: 'no-device-id',
env: {},
signal: new AbortController().signal,
})
).rejects.toThrow('Expected exactly one connected Android device');
});
it('cleans up temporary files when capture fails', async () => {
jest.mocked(spawn).mockRejectedValue(new Error('capture failed'));
await expect(
captureDeviceRunSessionPreviewAsync({
runtimePlatform: BuildRuntimePlatform.DARWIN,
device: 'device',
env: {},
signal: new AbortController().signal,
})
).rejects.toThrow('capture failed');
expect(
(await readdir(os.tmpdir())).filter(name => name.startsWith('session-preview-'))
).toEqual([]);
});
});
describe(ensureMacosPreviewEncoderInstalledAsync, () => {
const logger = { info: jest.fn() } as unknown as bunyan;
it.each([true, false])(
'installs webp only when cwebp is missing (present: %s)',
async present => {
jest.mocked(spawn).mockReset();
jest
.mocked(spawn)
.mockImplementation(
command =>
(command === 'cwebp' && !present
? Promise.reject(new Error('command not found: cwebp'))
: Promise.resolve({ stdout: '', stderr: '' })) as ReturnType<typeof spawn>
);
await ensureMacosPreviewEncoderInstalledAsync({ env: { HOME: '/Users/expo' }, logger });
const brewCalls = jest.mocked(spawn).mock.calls.filter(([command]) => command === 'brew');
if (present) {
expect(brewCalls).toEqual([]);
} else {
expect(brewCalls).toEqual([
[
'brew',
['install', 'webp'],
{ env: { HOME: '/Users/expo', HOMEBREW_NO_AUTO_UPDATE: '1' }, logger },
],
]);
}
}
);
it('rejects when webp cannot be installed', async () => {
jest.mocked(spawn).mockReset();
jest.mocked(spawn).mockRejectedValue(new Error('brew failed'));
await expect(ensureMacosPreviewEncoderInstalledAsync({ env: {}, logger })).rejects.toThrow(
'brew failed'
);
});
});
@@ -0,0 +1,200 @@
import type { bunyan } from '@expo/logger';
import type { BuildStepEnv } from '@expo/steps';
import { Client, fetchExchange } from '@urql/core';
import { execFileSync } from 'node:child_process';
import { copyFile, mkdir, mkdtemp, rm, stat, writeFile } from 'node:fs/promises';
import { type IncomingMessage, type ServerResponse, createServer } from 'node:http';
import { tmpdir } from 'node:os';
import path from 'node:path';
import type { CustomBuildContext } from '../../../customBuildContext';
import {
findUnlistedDeviceScreenRecordingsAsync,
uploadDeviceRunSessionScreenRecordingsAsync,
} from '../deviceRunSessionScreenRecordings';
// Runs with `yarn jest-ffmpeg`, which needs ffmpeg and ffprobe on PATH.
jest.unmock('node-fetch');
jest.unmock('node:fs');
jest.unmock('node:fs/promises');
let handle: (request: IncomingMessage, response: ServerResponse) => void;
let server: ReturnType<typeof createServer>;
let ctx: CustomBuildContext;
let url: string;
let directory: string;
let creationBodies: string[];
beforeEach(async () => {
creationBodies = [];
directory = await mkdtemp(path.join(tmpdir(), 'recording-recovery-'));
server = createServer((request, response) => {
if (request.url === '/graphql') {
request.setEncoding('utf8');
let body = '';
request.on('data', chunk => (body += chunk));
request.on('end', () => {
creationBodies.push(body);
response.setHeader('Content-Type', 'application/json');
response.end(
JSON.stringify({
data: {
deviceRunSession: {
createArtifactUploadSession: {
uploadSession: { url: `${url}/artifact`, headers: {} },
},
},
},
})
);
});
} else {
handle(request, response);
}
});
await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
const address = server.address();
if (!address || typeof address === 'string') {
throw new Error('Missing test server address');
}
url = `http://127.0.0.1:${address.port}`;
ctx = {
graphqlClient: new Client({
url: `${url}/graphql`,
exchanges: [fetchExchange],
fetchOptions: { headers: { Authorization: 'Bearer test-auth' } },
}),
} as CustomBuildContext;
});
afterEach(async () => {
server.closeAllConnections();
await new Promise<void>(resolve => server.close(() => resolve()));
await rm(directory, { recursive: true, force: true });
});
it('recovers unlisted recordings from a killed host and flags the unfinished ones as partial', async () => {
const logger = { info: jest.fn(), warn: jest.fn() } as unknown as bunyan;
const env = process.env as BuildStepEnv;
const cut = path.join(directory, 'cut');
const empty = path.join(directory, 'empty');
const failed = path.join(directory, 'failed');
const done = path.join(directory, 'done');
await mkdir(cut);
await mkdir(empty);
await mkdir(failed);
await mkdir(done);
const manifest = {
udid: 'emulator-5554',
deviceName: 'Pixel',
runtimeDisplayName: 'Android 16',
status: 'recording',
recording: 'recording.mp4.partial',
firstFrameWallClock: { iso8601: '2026-07-10T10:00:00.000Z' },
width: 128,
height: 96,
};
execFileSync('ffmpeg', [
'-v',
'error',
'-f',
'lavfi',
'-i',
'color=c=red:s=128x96:r=1',
'-t',
'3',
'-c:v',
'libx264',
'-g',
'1',
'-pix_fmt',
'yuv420p',
'-movflags',
'frag_keyframe+empty_moov',
'-f',
'mp4',
path.join(cut, 'recording.mp4.partial'),
]);
await writeFile(path.join(cut, 'session.json'), JSON.stringify(manifest));
await writeFile(path.join(empty, 'recording.mp4.partial'), new Uint8Array(28));
await writeFile(path.join(empty, 'session.json'), JSON.stringify(manifest));
await copyFile(
path.join(cut, 'recording.mp4.partial'),
path.join(failed, 'recording.mp4.partial')
);
await writeFile(
path.join(failed, 'session.json'),
JSON.stringify({
...manifest,
status: 'failed',
error: 'Recording finalization exceeded 30000 ms.',
})
);
await copyFile(path.join(cut, 'recording.mp4.partial'), path.join(done, 'recording.mp4'));
await writeFile(
path.join(done, 'session.json'),
JSON.stringify({ ...manifest, status: 'complete', recording: 'recording.mp4' })
);
const recordings = await findUnlistedDeviceScreenRecordingsAsync({
root: directory,
env,
logger,
});
expect(recordings).toEqual([
{
udid: 'emulator-5554',
deviceName: 'Pixel',
runtimeDisplayName: 'Android 16',
directory: cut,
},
{
udid: 'emulator-5554',
deviceName: 'Pixel',
runtimeDisplayName: 'Android 16',
directory: done,
},
{
udid: 'emulator-5554',
deviceName: 'Pixel',
runtimeDisplayName: 'Android 16',
directory: failed,
},
]);
expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('empty/recording.mp4.partial'));
let putBytes = 0;
handle = (request, response) => {
request.on('data', chunk => (putBytes += chunk.length));
request.on('end', () => {
response.statusCode = 200;
response.end();
});
};
await expect(
uploadDeviceRunSessionScreenRecordingsAsync(ctx, {
logger,
deviceRunSessionId: 'drs-id',
recordings,
})
).resolves.toBe(true);
expect(putBytes).toBe(3 * (await stat(path.join(cut, 'recording.mp4.partial'))).size);
const inputs = creationBodies
.map(body => JSON.parse(body).variables.input)
.sort((a, b) => a.filename.localeCompare(b.filename));
expect(inputs.map(input => input.filename)).toEqual(['cut.mp4', 'done.mp4', 'failed.mp4']);
expect(inputs.map(input => input.name.includes(', partial)'))).toEqual([true, false, true]);
expect(inputs[1].metadata).not.toHaveProperty('partial');
expect(inputs[0].metadata).toMatchObject({
partial: true,
partialReason: 'The Device Hub stopped before it could finalize the recording.',
width: 128,
height: 96,
});
expect(inputs[2].metadata).toMatchObject({
partial: true,
partialReason: 'Recording finalization exceeded 30000 ms.',
});
});
@@ -27,18 +27,17 @@ beforeEach(async () => {
directory = await mkdtemp(path.join(os.tmpdir(), 'screenshots-test-'));
uploaded = [];
failedUploads = new Map();
jest.mocked(uploadDeviceRunSessionArtifactAsync).mockImplementation(async (_ctx, { stream }) => {
const chunks: Buffer[] = [];
for await (const chunk of stream) {
chunks.push(Buffer.from(chunk));
}
uploaded.push(Buffer.concat(chunks));
});
jest
.mocked(uploadDeviceRunSessionArtifactAsync)
.mockImplementation(async (_ctx, { openStream }) => {
const chunks: Buffer[] = [];
for await (const chunk of openStream()) {
chunks.push(Buffer.from(chunk));
}
uploaded.push(Buffer.concat(chunks));
});
});
afterEach(async () => {
for (const [, { stream }] of jest.mocked(uploadDeviceRunSessionArtifactAsync).mock.calls) {
stream.destroy();
}
await rm(directory, { recursive: true, force: true });
});
const flush = () =>
@@ -1,9 +1,8 @@
import type { bunyan } from '@expo/logger';
import type { BuildStepEnv } from '@expo/steps';
import { Client, fetchExchange } from '@urql/core';
import { execFileSync } from 'node:child_process';
import { createReadStream } from 'node:fs';
import { copyFile, mkdir, mkdtemp, rm, stat, writeFile } from 'node:fs/promises';
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
import { type IncomingMessage, type ServerResponse, createServer } from 'node:http';
import type { Socket } from 'node:net';
import { tmpdir } from 'node:os';
@@ -13,10 +12,7 @@ import { setTimeout as delay } from 'node:timers/promises';
import type { CustomBuildContext } from '../../../customBuildContext';
import { uploadDeviceRunSessionArtifactAsync } from '../deviceRunSessionArtifacts';
import {
findUnlistedDeviceScreenRecordingsAsync,
uploadDeviceRunSessionScreenRecordingsAsync,
} from '../deviceRunSessionScreenRecordings';
import { findUnlistedDeviceScreenRecordingsAsync } from '../deviceRunSessionScreenRecordings';
jest.unmock('node-fetch');
jest.unmock('node:fs');
@@ -47,13 +43,11 @@ let creates: number;
let authorization: string | undefined;
let stallCreation: boolean;
let creationStarted: ReturnType<typeof deferred<IncomingMessage>>;
let creationBodies: string[];
beforeEach(async () => {
creates = 0;
authorization = undefined;
stallCreation = false;
creationBodies = [];
creationStarted = deferred();
directory = await mkdtemp(path.join(tmpdir(), 'recording-upload-http-'));
server = createServer((request, response) => {
@@ -61,10 +55,7 @@ beforeEach(async () => {
creates++;
authorization = request.headers.authorization;
creationStarted.resolve(request);
request.setEncoding('utf8');
let body = '';
request.on('data', chunk => (body += chunk));
request.on('end', () => creationBodies.push(body));
request.resume();
request.on('end', () => {
if (stallCreation) {
return;
@@ -131,8 +122,8 @@ it('replays the full file after a transient PUT failure without recreating the u
await uploadDeviceRunSessionArtifactAsync(ctx, {
...metadata,
size: payload.length,
stream: open(),
reopenStream: open,
openStream: open,
attempts: 3,
});
expect(creates).toBe(1);
expect(authorization).toBe('Bearer test-auth');
@@ -165,7 +156,11 @@ it('keeps a slow PUT alive while its body is still flowing', async () => {
onChunk();
});
const stream = new Readable({ read() {} });
const upload = uploadDeviceRunSessionArtifactAsync(ctx, { ...metadata, size: 3, stream });
const upload = uploadDeviceRunSessionArtifactAsync(ctx, {
...metadata,
size: 3,
openStream: () => stream,
});
const settled = expect(upload).resolves.toBeUndefined();
stream.push('a');
await waitForBytes(1);
@@ -205,8 +200,8 @@ it('retries a PUT that stalled past the idle deadline with a fresh stream', asyn
const upload = uploadDeviceRunSessionArtifactAsync(ctx, {
...metadata,
size: 9,
stream: open(),
reopenStream: open,
openStream: open,
attempts: 3,
});
const settled = expect(upload).resolves.toBeUndefined();
await firstBodyRead.promise;
@@ -231,11 +226,11 @@ it('cancels an active PUT socket and destroys its source without retrying', asyn
const controller = new AbortController();
const stream = new Readable({ read() {} });
stream.push(Buffer.alloc(1024));
const reopenStream = jest.fn(() => Readable.from('unexpected retry'));
const openStream = jest.fn(() => stream);
const upload = uploadDeviceRunSessionArtifactAsync(ctx, {
...metadata,
stream,
reopenStream,
openStream,
attempts: 3,
signal: controller.signal,
});
const rejected = expect(upload).rejects.toThrow('test cancellation');
@@ -244,7 +239,7 @@ it('cancels an active PUT socket and destroys its source without retrying', asyn
await rejected;
await closed.promise;
expect(stream.destroyed).toBe(true);
expect(reopenStream).not.toHaveBeenCalled();
expect(openStream).toHaveBeenCalledTimes(1);
expect(puts).toBe(1);
});
@@ -268,8 +263,8 @@ it.each([200, 400, 503])(
};
const upload = uploadDeviceRunSessionArtifactAsync(ctx, {
...metadata,
stream: Readable.from('recording'),
reopenStream: () => Readable.from('recording'),
openStream: () => Readable.from('recording'),
attempts: 3,
});
if (status === 200) {
await expect(upload).resolves.toBeUndefined();
@@ -291,10 +286,10 @@ it('cancels urql session creation with auth intact and never starts a PUT', asyn
const puts = jest.fn();
handle = puts;
const controller = new AbortController();
const stream = Readable.from('recording');
const openStream = jest.fn(() => Readable.from('recording'));
const upload = uploadDeviceRunSessionArtifactAsync(ctx, {
...metadata,
stream,
openStream,
signal: controller.signal,
});
const rejected = expect(upload).rejects.toThrow('test cancellation');
@@ -306,7 +301,7 @@ it('cancels urql session creation with auth intact and never starts a PUT', asyn
expect(authorization).toBe('Bearer test-auth');
expect(creates).toBe(1);
expect(puts).not.toHaveBeenCalled();
expect(stream.destroyed).toBe(true);
expect(openStream).not.toHaveBeenCalled();
});
it('says ffprobe is missing instead of calling an unlisted recording undecodable', async () => {
@@ -336,128 +331,3 @@ it('says ffprobe is missing instead of calling an unlisted recording undecodable
'ffprobe is not available; cannot recover the unlisted recording session/recording.mp4.partial.'
);
});
it('recovers unlisted recordings from a killed host and flags the unfinished ones as partial', async () => {
const logger = { info: jest.fn(), warn: jest.fn() } as unknown as bunyan;
const env = process.env as BuildStepEnv;
const cut = path.join(directory, 'cut');
const empty = path.join(directory, 'empty');
const failed = path.join(directory, 'failed');
const done = path.join(directory, 'done');
await mkdir(cut);
await mkdir(empty);
await mkdir(failed);
await mkdir(done);
const manifest = {
udid: 'emulator-5554',
deviceName: 'Pixel',
runtimeDisplayName: 'Android 16',
status: 'recording',
recording: 'recording.mp4.partial',
firstFrameWallClock: { iso8601: '2026-07-10T10:00:00.000Z' },
width: 128,
height: 96,
};
execFileSync('ffmpeg', [
'-v',
'error',
'-f',
'lavfi',
'-i',
'color=c=red:s=128x96:r=1',
'-t',
'3',
'-c:v',
'libx264',
'-g',
'1',
'-pix_fmt',
'yuv420p',
'-movflags',
'frag_keyframe+empty_moov',
'-f',
'mp4',
path.join(cut, 'recording.mp4.partial'),
]);
await writeFile(path.join(cut, 'session.json'), JSON.stringify(manifest));
await writeFile(path.join(empty, 'recording.mp4.partial'), new Uint8Array(28));
await writeFile(path.join(empty, 'session.json'), JSON.stringify(manifest));
await copyFile(
path.join(cut, 'recording.mp4.partial'),
path.join(failed, 'recording.mp4.partial')
);
await writeFile(
path.join(failed, 'session.json'),
JSON.stringify({
...manifest,
status: 'failed',
error: 'Recording finalization exceeded 30000 ms.',
})
);
await copyFile(path.join(cut, 'recording.mp4.partial'), path.join(done, 'recording.mp4'));
await writeFile(
path.join(done, 'session.json'),
JSON.stringify({ ...manifest, status: 'complete', recording: 'recording.mp4' })
);
const recordings = await findUnlistedDeviceScreenRecordingsAsync({
root: directory,
env,
logger,
});
expect(recordings).toEqual([
{
udid: 'emulator-5554',
deviceName: 'Pixel',
runtimeDisplayName: 'Android 16',
directory: cut,
},
{
udid: 'emulator-5554',
deviceName: 'Pixel',
runtimeDisplayName: 'Android 16',
directory: done,
},
{
udid: 'emulator-5554',
deviceName: 'Pixel',
runtimeDisplayName: 'Android 16',
directory: failed,
},
]);
expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('empty/recording.mp4.partial'));
let putBytes = 0;
handle = (request, response) => {
request.on('data', chunk => (putBytes += chunk.length));
request.on('end', () => {
response.statusCode = 200;
response.end();
});
};
await expect(
uploadDeviceRunSessionScreenRecordingsAsync(ctx, {
logger,
deviceRunSessionId: 'drs-id',
recordings,
})
).resolves.toBe(true);
expect(putBytes).toBe(3 * (await stat(path.join(cut, 'recording.mp4.partial'))).size);
const inputs = creationBodies
.map(body => JSON.parse(body).variables.input)
.sort((a, b) => a.filename.localeCompare(b.filename));
expect(inputs.map(input => input.filename)).toEqual(['cut.mp4', 'done.mp4', 'failed.mp4']);
expect(inputs.map(input => input.name.includes(', partial)'))).toEqual([true, false, true]);
expect(inputs[1].metadata).not.toHaveProperty('partial');
expect(inputs[0].metadata).toMatchObject({
partial: true,
partialReason: 'The Device Hub stopped before it could finalize the recording.',
width: 128,
height: 96,
});
expect(inputs[2].metadata).toMatchObject({
partial: true,
partialReason: 'Recording finalization exceeded 30000 ms.',
});
});
@@ -12,8 +12,12 @@ import {
findUnlistedDeviceScreenRecordingsAsync,
uploadDeviceRunSessionScreenRecordingsAsync,
} from '../deviceRunSessionScreenRecordings';
import {
ensureMacosPreviewEncoderInstalledAsync,
startDeviceRunSessionPreview,
} from '../deviceRunSessionPreview';
import { startDeviceSessionHostAsync } from '../deviceSessionHost';
import { spawnDetached } from '../remoteDeviceRunSession';
import { ensureFfmpegInstalledOnceAsync, spawnDetached } from '../remoteDeviceRunSession';
jest.mock('@ngrok/ngrok');
jest.mock('../deviceRunSessionArtifacts');
@@ -30,6 +34,11 @@ jest.mock('../remoteDeviceRunSession', () => ({
fetchWebPreviewTurnArgsAsync: jest.fn().mockResolvedValue([]),
spawnDetached: jest.fn(),
}));
jest.mock('../deviceRunSessionPreview', () => ({
...jest.requireActual('../deviceRunSessionPreview'),
ensureMacosPreviewEncoderInstalledAsync: jest.fn(),
startDeviceRunSessionPreview: jest.fn(),
}));
jest.mock('../deviceRunSessionScreenRecordings', () => ({
...jest.requireActual('../deviceRunSessionScreenRecordings'),
findUnlistedDeviceScreenRecordingsAsync: jest.fn(),
@@ -64,8 +73,14 @@ async function startHostAsync() {
});
}
const stopSessionPreview = jest.fn();
beforeEach(() => {
jest.clearAllMocks();
stopSessionPreview.mockResolvedValue(undefined);
jest.mocked(startDeviceRunSessionPreview).mockReturnValue({ stopAsync: stopSessionPreview });
jest.mocked(ensureFfmpegInstalledOnceAsync).mockResolvedValue(undefined);
jest.mocked(ensureMacosPreviewEncoderInstalledAsync).mockResolvedValue(undefined);
stopServer.mockResolvedValue(undefined);
closeTunnel.mockResolvedValue(undefined);
jest.mocked(uploadDeviceRunSessionScreenRecordingsAsync).mockReset().mockResolvedValue(false);
@@ -464,9 +479,9 @@ it.each([BuildRuntimePlatform.LINUX, BuildRuntimePlatform.DARWIN])(
const uploads: Buffer[] = [];
jest
.mocked(uploadDeviceRunSessionArtifactAsync)
.mockImplementation(async (_ctx, { stream }) => {
.mockImplementation(async (_ctx, { openStream }) => {
const chunks: Buffer[] = [];
for await (const chunk of stream) {
for await (const chunk of openStream()) {
chunks.push(Buffer.from(chunk));
}
uploads.push(Buffer.concat(chunks));
@@ -502,3 +517,60 @@ it.each([BuildRuntimePlatform.LINUX, BuildRuntimePlatform.DARWIN])(
await expect(access(directory)).rejects.toMatchObject({ code: 'ENOENT' });
}
);
it.each([BuildRuntimePlatform.LINUX, BuildRuntimePlatform.DARWIN])(
'refreshes the session preview of the ready device until the %s host finishes',
async runtimePlatform => {
const host = await startDeviceSessionHostAsync(ctx, {
runtimePlatform,
env,
logger,
timeoutMs: 10_000,
});
await new Promise(resolve => setImmediate(resolve));
expect(startDeviceRunSessionPreview).toHaveBeenCalledTimes(1);
expect(startDeviceRunSessionPreview).toHaveBeenCalledWith(
expect.objectContaining({ deviceRunSessionId: 'drs-id', captureAsync: expect.any(Function) })
);
await host.finishAsync();
expect(stopSessionPreview).toHaveBeenCalledTimes(1);
expect(stopSessionPreview.mock.invocationCallOrder[0]).toBeLessThan(
stopServer.mock.invocationCallOrder[0]
);
}
);
it('does not delay macOS readiness on encoder setup or start the preview after finish', async () => {
const install = deferred<void>();
jest.mocked(ensureMacosPreviewEncoderInstalledAsync).mockReturnValueOnce(install.promise);
const host = await startDeviceSessionHostAsync(ctx, {
runtimePlatform: BuildRuntimePlatform.DARWIN,
env,
logger,
timeoutMs: 10_000,
});
await host.finishAsync();
install.resolve();
await new Promise(resolve => setImmediate(resolve));
expect(startDeviceRunSessionPreview).not.toHaveBeenCalled();
expect(stopServer).toHaveBeenCalledTimes(1);
});
it('keeps the session running when encoder setup for the preview fails', async () => {
jest
.mocked(ensureMacosPreviewEncoderInstalledAsync)
.mockRejectedValueOnce(new Error('brew failed'));
const host = await startDeviceSessionHostAsync(ctx, {
runtimePlatform: BuildRuntimePlatform.DARWIN,
env,
logger,
timeoutMs: 10_000,
});
await new Promise(resolve => setImmediate(resolve));
expect(startDeviceRunSessionPreview).not.toHaveBeenCalled();
expect(logger.warn).toHaveBeenCalledWith(
{ err: expect.objectContaining({ message: 'brew failed' }) },
'Could not start refreshing the session preview.'
);
await host.finishAsync();
});
@@ -0,0 +1,3 @@
Robin, sparrow, leiothrix and chat
Zażółć gęślą jaźń
€ 🐦
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

@@ -5,6 +5,7 @@ import spawn from '@expo/turtle-spawn';
import * as ngrok from '@ngrok/ngrok';
import { EventEmitter } from 'node:events';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import {
clearTimeout as clearTimeoutCallback,
@@ -842,11 +843,9 @@ describe(startDeviceSessionHostAsync, () => {
url: () => 'https://android-preview.example.test',
close,
} as never);
// `env` has no PATH, so the host installs ffmpeg.
jest
.mocked(spawn)
.mockReturnValueOnce(
Promise.reject(new Error('ffmpeg is missing')) as ReturnType<typeof spawn>
)
.mockReturnValueOnce(Promise.resolve({}) as unknown as ReturnType<typeof spawn>)
.mockReturnValueOnce(Promise.resolve({}) as unknown as ReturnType<typeof spawn>);
@@ -860,24 +859,24 @@ describe(startDeviceSessionHostAsync, () => {
const preview = await host.openPreviewAsync({ baseDomain });
const spawnCalls = jest.mocked(spawn).mock.calls;
expect(spawnCalls[0]).toEqual(['ffmpeg', ['-version'], { env }]);
expect(spawnCalls[1]).toEqual([
expect(spawnCalls[0]).toEqual([
'sudo',
['apt-get', 'update'],
expect.objectContaining({
env: expect.objectContaining({ DEBIAN_FRONTEND: 'noninteractive' }),
}),
]);
expect(spawnCalls[2]).toEqual([
expect(spawnCalls[1]).toEqual([
'sudo',
['apt-get', 'install', '-y', 'ffmpeg'],
expect.objectContaining({
env: expect.objectContaining({ DEBIAN_FRONTEND: 'noninteractive' }),
}),
]);
expect(spawnCalls[2]).toEqual(['ffmpeg', ['-version'], { env }]);
const expoDeviceHubCallIndex = spawnCalls.findIndex(([command]) => command === 'npx');
expect(expoDeviceHubCallIndex).toBeGreaterThan(2);
expect(jest.mocked(spawn).mock.invocationCallOrder[2]).toBeLessThan(
expect(jest.mocked(spawn).mock.invocationCallOrder[1]).toBeLessThan(
jest.mocked(spawn).mock.invocationCallOrder[expoDeviceHubCallIndex]
);
const [command, args] = spawnCalls[expoDeviceHubCallIndex];
@@ -1508,6 +1507,7 @@ describe(waitForDeviceRunSessionStoppedAsync, () => {
describe(ensureFfmpegInstalledOnceAsync, () => {
const spawnMock = jest.mocked(spawn);
let binDirectory: string;
function spawnResolved(): ReturnType<typeof spawn> {
return Promise.resolve({}) as unknown as ReturnType<typeof spawn>;
@@ -1517,68 +1517,140 @@ describe(ensureFfmpegInstalledOnceAsync, () => {
return Promise.reject(new Error('boom')) as unknown as ReturnType<typeof spawn>;
}
beforeEach(() => {
function spawnPending(): ReturnType<typeof spawn> {
return new Promise(() => {}) as unknown as ReturnType<typeof spawn>;
}
async function installFakeFfmpegAsync({ mode }: { mode: number }): Promise<string> {
const ffmpegPath = path.join(binDirectory, 'ffmpeg');
await fs.writeFile(ffmpegPath, '#!/bin/sh\n');
await fs.chmod(ffmpegPath, mode);
return ffmpegPath;
}
function createPathEnv(): BuildStepEnv {
return { ...createEnvMock(), PATH: binDirectory };
}
const warmUpCall = ['ffmpeg', ['-version'], expect.anything()];
beforeEach(async () => {
spawnMock.mockReset();
jest.mocked(Sentry).capture.mockReset();
binDirectory = await fs.mkdtemp(path.join(os.tmpdir(), 'ffmpeg-path-'));
});
it('does not install when ffmpeg is on PATH', async () => {
spawnMock.mockReturnValueOnce(spawnResolved());
afterEach(async () => {
await fs.rm(binDirectory, { recursive: true, force: true });
});
it('finds ffmpeg on PATH without waiting for it to run, then warms it up', async () => {
const ffmpegPath = await installFakeFfmpegAsync({ mode: 0o755 });
spawnMock.mockReturnValueOnce(spawnPending());
const logger = createLoggerMock();
await ensureFfmpegInstalledOnceAsync({
runtimePlatform: BuildRuntimePlatform.DARWIN,
env: createEnvMock(),
logger: createLoggerMock(),
env: createPathEnv(),
logger,
});
expect(spawnMock).toHaveBeenCalledTimes(1);
expect(spawnMock).toHaveBeenCalledWith('ffmpeg', ['-version'], expect.anything());
expect(spawnMock).toHaveBeenCalledWith(...warmUpCall);
expect(logger.info).toHaveBeenCalledWith(`ffmpeg is already installed at ${ffmpegPath}.`);
});
it('installs ffmpeg with Homebrew on darwin when it is missing', async () => {
spawnMock.mockReturnValueOnce(spawnRejected()).mockReturnValueOnce(spawnResolved());
it('installs ffmpeg when the file on PATH is not executable', async () => {
await installFakeFfmpegAsync({ mode: 0o644 });
spawnMock.mockReturnValue(spawnResolved());
await ensureFfmpegInstalledOnceAsync({
runtimePlatform: BuildRuntimePlatform.DARWIN,
env: createEnvMock(),
env: createPathEnv(),
logger: createLoggerMock(),
});
expect(spawnMock).toHaveBeenLastCalledWith(
expect(spawnMock).toHaveBeenCalledWith('brew', ['install', 'ffmpeg'], expect.anything());
});
it('installs ffmpeg when the ffmpeg entry on PATH is a directory', async () => {
await fs.mkdir(path.join(binDirectory, 'ffmpeg'));
spawnMock.mockReturnValue(spawnResolved());
await ensureFfmpegInstalledOnceAsync({
runtimePlatform: BuildRuntimePlatform.DARWIN,
env: createPathEnv(),
logger: createLoggerMock(),
});
expect(spawnMock).toHaveBeenCalledWith('brew', ['install', 'ffmpeg'], expect.anything());
});
it('treats an empty PATH entry as the current directory', async () => {
await installFakeFfmpegAsync({ mode: 0o755 });
jest.spyOn(process, 'cwd').mockReturnValue(binDirectory);
spawnMock.mockReturnValue(spawnResolved());
try {
await ensureFfmpegInstalledOnceAsync({
runtimePlatform: BuildRuntimePlatform.DARWIN,
env: { ...createEnvMock(), PATH: `${path.delimiter}/nonexistent` },
logger: createLoggerMock(),
});
} finally {
jest.mocked(process.cwd).mockRestore();
}
expect(spawnMock).toHaveBeenCalledTimes(1);
expect(spawnMock).toHaveBeenCalledWith(...warmUpCall);
});
it('installs ffmpeg with Homebrew on darwin when it is missing, then warms it up', async () => {
spawnMock.mockReturnValue(spawnResolved());
await ensureFfmpegInstalledOnceAsync({
runtimePlatform: BuildRuntimePlatform.DARWIN,
env: createPathEnv(),
logger: createLoggerMock(),
});
expect(spawnMock).toHaveBeenNthCalledWith(
1,
'brew',
['install', 'ffmpeg'],
expect.objectContaining({
env: expect.objectContaining({ HOMEBREW_NO_AUTO_UPDATE: '1' }),
})
);
expect(spawnMock).toHaveBeenLastCalledWith(...warmUpCall);
});
it('installs ffmpeg with apt on linux when it is missing', async () => {
spawnMock
.mockReturnValueOnce(spawnRejected()) // ffmpeg -version
.mockReturnValueOnce(spawnResolved()) // apt-get update
.mockReturnValueOnce(spawnResolved()); // apt-get install
spawnMock.mockReturnValue(spawnResolved());
await ensureFfmpegInstalledOnceAsync({
runtimePlatform: BuildRuntimePlatform.LINUX,
env: createEnvMock(),
env: createPathEnv(),
logger: createLoggerMock(),
});
expect(spawnMock).toHaveBeenCalledWith(
expect(spawnMock).toHaveBeenNthCalledWith(
1,
'sudo',
['apt-get', 'update'],
expect.objectContaining({
env: expect.objectContaining({ DEBIAN_FRONTEND: 'noninteractive' }),
})
);
expect(spawnMock).toHaveBeenLastCalledWith(
expect(spawnMock).toHaveBeenNthCalledWith(
2,
'sudo',
['apt-get', 'install', '-y', 'ffmpeg'],
expect.objectContaining({
env: expect.objectContaining({ DEBIAN_FRONTEND: 'noninteractive' }),
})
);
expect(spawnMock).toHaveBeenLastCalledWith(...warmUpCall);
});
it('shares an in-flight ffmpeg setup between callers', async () => {
@@ -1587,21 +1659,21 @@ describe(ensureFfmpegInstalledOnceAsync, () => {
finishInstall = resolve;
});
spawnMock
.mockReturnValueOnce(spawnRejected()) // ffmpeg -version
.mockReturnValueOnce(spawnResolved()) // apt-get update
.mockReturnValueOnce(pendingInstall as unknown as ReturnType<typeof spawn>); // apt-get install
.mockReturnValueOnce(pendingInstall as unknown as ReturnType<typeof spawn>) // apt-get install
.mockReturnValueOnce(spawnResolved()); // ffmpeg -version warm-up
const options = {
runtimePlatform: BuildRuntimePlatform.LINUX,
env: createEnvMock(),
env: createPathEnv(),
logger: createLoggerMock(),
};
const firstSetup = ensureFfmpegInstalledOnceAsync(options);
await new Promise<void>(resolve => setImmediate(resolve));
expect(spawnMock).toHaveBeenCalledTimes(3);
expect(spawnMock).toHaveBeenCalledTimes(2);
const secondSetup = ensureFfmpegInstalledOnceAsync(options);
expect(spawnMock).toHaveBeenCalledTimes(3);
expect(spawnMock).toHaveBeenCalledTimes(2);
finishInstall?.();
await Promise.all([firstSetup, secondSetup]);
@@ -1610,18 +1682,17 @@ describe(ensureFfmpegInstalledOnceAsync, () => {
it('still installs on linux when the apt index refresh fails', async () => {
spawnMock
.mockReturnValueOnce(spawnRejected()) // ffmpeg -version
.mockReturnValueOnce(spawnRejected()) // apt-get update
.mockReturnValueOnce(spawnResolved()); // apt-get install
.mockReturnValue(spawnResolved());
const logger = createLoggerMock();
await ensureFfmpegInstalledOnceAsync({
runtimePlatform: BuildRuntimePlatform.LINUX,
env: createEnvMock(),
env: createPathEnv(),
logger,
});
expect(spawnMock).toHaveBeenLastCalledWith(
expect(spawnMock).toHaveBeenCalledWith(
'sudo',
['apt-get', 'install', '-y', 'ffmpeg'],
expect.anything()
@@ -1630,17 +1701,18 @@ describe(ensureFfmpegInstalledOnceAsync, () => {
});
it('warns and resolves when the install fails, so the session still starts', async () => {
spawnMock.mockReturnValueOnce(spawnRejected()).mockReturnValueOnce(spawnRejected());
spawnMock.mockReturnValueOnce(spawnRejected());
const logger = createLoggerMock();
await expect(
ensureFfmpegInstalledOnceAsync({
runtimePlatform: BuildRuntimePlatform.DARWIN,
env: createEnvMock(),
env: createPathEnv(),
logger,
})
).resolves.toBeUndefined();
expect(spawnMock).toHaveBeenCalledTimes(1);
expect(logger.warn).toHaveBeenCalled();
expect(jest.mocked(Sentry).capture).toHaveBeenCalled();
});
@@ -1648,7 +1720,7 @@ describe(ensureFfmpegInstalledOnceAsync, () => {
// The caller runs this with `void` and the worker installs no unhandledRejection
// handler, so a rejection here would crash the process. `spawn` is not async and
// can throw synchronously, which `asyncResult` cannot catch.
it('resolves when the availability check throws synchronously', async () => {
it('resolves when the install throws synchronously', async () => {
spawnMock.mockImplementationOnce(() => {
throw new Error('sync spawn failure');
});
@@ -1657,7 +1729,7 @@ describe(ensureFfmpegInstalledOnceAsync, () => {
await expect(
ensureFfmpegInstalledOnceAsync({
runtimePlatform: BuildRuntimePlatform.DARWIN,
env: createEnvMock(),
env: createPathEnv(),
logger,
})
).resolves.toBeUndefined();
@@ -1665,4 +1737,29 @@ describe(ensureFfmpegInstalledOnceAsync, () => {
expect(logger.warn).toHaveBeenCalled();
expect(jest.mocked(Sentry).capture).toHaveBeenCalled();
});
it.each([
['rejects', () => spawnRejected()],
[
'throws synchronously',
() => {
throw new Error('sync spawn failure');
},
],
])('ignores a warm-up that %s', async (_, warmUp) => {
await installFakeFfmpegAsync({ mode: 0o755 });
spawnMock.mockImplementationOnce(warmUp as () => ReturnType<typeof spawn>);
const logger = createLoggerMock();
await expect(
ensureFfmpegInstalledOnceAsync({
runtimePlatform: BuildRuntimePlatform.DARWIN,
env: createPathEnv(),
logger,
})
).resolves.toBeUndefined();
await new Promise<void>(resolve => setImmediate(resolve));
expect(logger.warn).not.toHaveBeenCalled();
});
});
@@ -0,0 +1,451 @@
jest.unmock('fs');
jest.unmock('fs/promises');
jest.unmock('node:fs');
jest.unmock('node:fs/promises');
import { SandboxDaemonErrorCode, SystemError } from '@expo/eas-build-job';
import { type bunyan } from '@expo/logger';
import { type Client, CombinedError } from '@urql/core';
import fetch, { type RequestInit } from 'node-fetch';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { performance } from 'node:perf_hooks';
import { type Readable } from 'node:stream';
import { setTimeout as delay } from 'node:timers/promises';
import { inspect } from 'node:util';
import { SandboxArtifactUploadManager, startSandboxArtifactUploadAsync } from '../sandboxArtifacts';
const { AbortError, FetchError, Response } = jest.requireActual(
'node-fetch'
) as typeof import('node-fetch');
const ARTIFACT_ID = '0199c0de-7b3a-7c1e-8f00-1234567890ab';
const SIGNED_URL = 'https://uploads.expo.test/artifact?X-Amz-Signature=secret-signature';
const DOWNLOAD_URL = 'https://downloads.expo.test/artifact?X-Amz-Signature=download-signature';
const createdResult = {
data: {
sandbox: {
createArtifactUploadSession: {
artifact: { id: ARTIFACT_ID },
uploadSession: { url: SIGNED_URL, headers: { 'If-None-Match': '*' } },
},
},
},
};
const finalizedResult = {
data: { sandbox: { finalizeArtifact: { id: ARTIFACT_ID, downloadUrl: DOWNLOAD_URL } } },
};
describe('sandbox artifact uploads', () => {
let directory: string;
let filePath: string;
let mutation: jest.Mock;
let graphqlClient: Client;
let logger: { info: jest.Mock; error: jest.Mock };
let uploadArtifactAsync: (options: {
filePath: string;
name: string;
signal: AbortSignal;
}) => ReturnType<typeof startSandboxArtifactUploadAsync>;
beforeEach(async () => {
directory = await fs.mkdtemp(path.join(os.tmpdir(), 'sandbox-artifacts-'));
filePath = path.join(directory, 'crash.log');
await fs.writeFile(filePath, 'crash');
jest.mocked(fetch).mockReset();
mutation = jest.fn();
logger = { info: jest.fn(), error: jest.fn() };
graphqlClient = { mutation } as unknown as Client;
uploadArtifactAsync = async options =>
await startSandboxArtifactUploadAsync({
graphqlClient,
sandboxId: 'sandbox-id',
logger: logger as unknown as bunyan,
...options,
});
});
afterEach(async () => {
await fs.rm(directory, { recursive: true, force: true });
});
it('returns the artifact id before the PUT completes', async () => {
mockMutationResults(createdResult, finalizedResult);
let respond!: (response: InstanceType<typeof Response>) => void;
jest.mocked(fetch).mockReturnValueOnce(
new Promise(resolve => {
respond = resolve;
}) as ReturnType<typeof fetch>
);
const upload = await uploadArtifactAsync({
filePath,
name: 'Crash log',
signal: new AbortController().signal,
});
expect(upload.id).toBe(ARTIFACT_ID);
expect(mutation).toHaveBeenCalledTimes(1);
expect(mutation).toHaveBeenCalledWith(
expect.anything(),
{ sandboxId: 'sandbox-id', input: { name: 'Crash log', filename: 'crash.log', size: 5 } },
expect.anything()
);
respond(new Response('', { status: 200 }));
await expect(upload.completed).resolves.toEqual({
status: 'uploaded',
downloadUrl: DOWNLOAD_URL,
});
expect(mutation).toHaveBeenLastCalledWith(
expect.anything(),
{ artifactId: ARTIFACT_ID },
expect.anything()
);
expect(logger.error).not.toHaveBeenCalled();
});
it('sends exactly the stat size on every attempt when the file grows', async () => {
mutation
.mockReturnValueOnce({
toPromise: async () => {
await fs.appendFile(filePath, ' and more');
return createdResult;
},
})
.mockReturnValueOnce({ toPromise: async () => finalizedResult });
const bodies: string[] = [];
const respondAfterReadingBody =
(status: number) =>
async (_url: unknown, init?: RequestInit): Promise<InstanceType<typeof Response>> => {
const chunks: Buffer[] = [];
for await (const chunk of init!.body as Readable) {
chunks.push(chunk);
}
bodies.push(Buffer.concat(chunks).toString());
return new Response('', { status });
};
jest
.mocked(fetch)
.mockImplementationOnce(respondAfterReadingBody(503) as unknown as typeof fetch)
.mockImplementationOnce(respondAfterReadingBody(200) as unknown as typeof fetch);
const upload = await uploadArtifactAsync({
filePath,
name: 'Crash log',
signal: new AbortController().signal,
});
await upload.completed;
expect(bodies).toEqual(['crash', 'crash']);
expect(logger.error).not.toHaveBeenCalled();
});
it('uploads an empty file with an empty body', async () => {
await fs.writeFile(filePath, '');
mockMutationResults(createdResult, finalizedResult);
let body: string | undefined;
jest.mocked(fetch).mockImplementationOnce((async (_url: unknown, init?: RequestInit) => {
const chunks: Buffer[] = [];
for await (const chunk of init!.body as Readable) {
chunks.push(chunk);
}
body = Buffer.concat(chunks).toString();
return new Response('', { status: 200 });
}) as unknown as typeof fetch);
const upload = await uploadArtifactAsync({
filePath,
name: 'Empty log',
signal: new AbortController().signal,
});
await upload.completed;
expect(mutation).toHaveBeenCalledWith(
expect.anything(),
{ sandboxId: 'sandbox-id', input: { name: 'Empty log', filename: 'crash.log', size: 0 } },
expect.anything()
);
expect(body).toBe('');
expect(logger.error).not.toHaveBeenCalled();
});
it('finalizes when a retry fails the upload precondition', async () => {
mockMutationResults(createdResult, finalizedResult);
jest
.mocked(fetch)
.mockRejectedValueOnce(
new FetchError(`request to ${SIGNED_URL} failed, reason: socket hang up`, 'system', {
code: 'ECONNRESET',
} as any)
)
.mockResolvedValueOnce(new Response('', { status: 412, statusText: 'Precondition Failed' }));
const upload = await uploadArtifactAsync({
filePath,
name: 'Crash log',
signal: new AbortController().signal,
});
await upload.completed;
expect(fetch).toHaveBeenCalledTimes(2);
expect(mutation).toHaveBeenLastCalledWith(
expect.anything(),
{ artifactId: ARTIFACT_ID },
expect.anything()
);
expect(logger.error).not.toHaveBeenCalled();
expect(logger.info).toHaveBeenLastCalledWith(
`Uploaded sandbox artifact "Crash log" (${ARTIFACT_ID}, 5 bytes).`
);
});
it('retries finalize after a server error and logs a failure that persists', async () => {
mockMutationResults(
createdResult,
{
error: new CombinedError({
networkError: new Error('Bad Gateway'),
response: { status: 502 },
}),
},
{
error: new CombinedError({
graphQLErrors: ['Sandbox artifact "Crash log" has no uploaded file.'],
response: { status: 200 },
}),
}
);
jest.mocked(fetch).mockResolvedValueOnce(new Response('', { status: 200 }));
const upload = await uploadArtifactAsync({
filePath,
name: 'Crash log',
signal: new AbortController().signal,
});
await expect(upload.completed).resolves.toEqual({ status: 'failed' });
expect(mutation).toHaveBeenCalledTimes(3);
expect(logger.error).toHaveBeenCalledWith(
expect.anything(),
`Uploaded sandbox artifact "Crash log" (${ARTIFACT_ID}), but could not finalize it.`
);
});
it.each(['VALIDATION_ERROR', 'UNAUTHORIZED'])(
'rejects a %s upload session error as a bad request with the GraphQL message',
async errorCode => {
const message =
'Sandbox already has an artifact named "Crash log". Use a different name for this artifact.';
mockMutationResults({
error: new CombinedError({ graphQLErrors: [{ message, extensions: { errorCode } }] }),
});
const upload = uploadArtifactAsync({
filePath,
name: 'Crash log',
signal: new AbortController().signal,
});
await expect(upload).rejects.toMatchObject({
code: SandboxDaemonErrorCode.BAD_REQUEST,
message,
});
expect(fetch).not.toHaveBeenCalled();
}
);
it('rejects an unexpected upload session error as a system error', async () => {
mockMutationResults({
error: new CombinedError({
graphQLErrors: [
{ message: 'Request failed.', extensions: { errorCode: 'UNEXPECTED_ERROR' } },
],
}),
});
const upload = uploadArtifactAsync({
filePath,
name: 'Crash log',
signal: new AbortController().signal,
});
await expect(upload).rejects.toBeInstanceOf(SystemError);
await expect(upload).rejects.toMatchObject({ message: 'Request failed.' });
});
it('rejects a missing file as a bad request without creating an upload session', async () => {
const missingPath = path.join(directory, 'missing.log');
const upload = uploadArtifactAsync({
filePath: missingPath,
name: 'Crash log',
signal: new AbortController().signal,
});
await expect(upload).rejects.toMatchObject({
code: SandboxDaemonErrorCode.BAD_REQUEST,
message: `File does not exist: ${missingPath}`,
});
expect(mutation).not.toHaveBeenCalled();
});
it('keeps the signed URL out of logs', async () => {
mockMutationResults(createdResult);
jest.mocked(fetch).mockRejectedValue(
new FetchError(`request to ${SIGNED_URL} failed, reason: socket hang up`, 'system', {
code: 'ECONNRESET',
} as any)
);
const upload = await uploadArtifactAsync({
filePath,
name: 'Crash log',
signal: new AbortController().signal,
});
await upload.completed;
expect(logger.error).toHaveBeenCalledTimes(1);
const logged = inspect([logger.info.mock.calls, logger.error.mock.calls], { depth: 10 });
expect(fetch).toHaveBeenCalledTimes(3);
expect(logged).toContain('Artifact PUT failed: ECONNRESET.');
expect(logged).not.toContain('secret-signature');
});
it('waits for an aborted upload before the manager reports that it stopped', async () => {
mockMutationResults(createdResult);
mockPutUntilAborted();
const controller = new AbortController();
const manager = createManager(controller.signal);
await expect(
manager.startAsync({ filePath, name: 'Crash log', yieldTimeMs: 0 })
).resolves.toEqual({
id: ARTIFACT_ID,
status: 'uploading',
});
controller.abort();
await manager.stoppedPromise;
expect(logger.error).toHaveBeenCalledWith(
expect.anything(),
`Failed to upload sandbox artifact "Crash log" (${ARTIFACT_ID}).`
);
await expect(
manager.startAsync({ filePath, name: 'Late log', yieldTimeMs: 0 })
).rejects.toMatchObject({
name: 'AbortError',
});
expect(mutation).toHaveBeenCalledTimes(1);
});
it.each([
{
case: 'a finalized upload as uploaded with its download URL',
finalizeResult: finalizedResult,
outcome: { status: 'uploaded', downloadUrl: DOWNLOAD_URL },
},
{
case: 'a failed finalization as failed',
finalizeResult: {
error: new CombinedError({
graphQLErrors: ['Sandbox artifact "Crash log" has no uploaded file.'],
response: { status: 200 },
}),
},
outcome: { status: 'failed' },
},
{
case: 'a finalized artifact without a download URL as failed',
finalizeResult: {
data: { sandbox: { finalizeArtifact: { id: ARTIFACT_ID, downloadUrl: null } } },
},
outcome: { status: 'failed' },
},
])(
'waits for an upload that ends within the yield time and reports $case',
async ({ finalizeResult, outcome }) => {
mockMutationResults(createdResult, finalizeResult);
jest.mocked(fetch).mockResolvedValueOnce(new Response('', { status: 200 }));
const manager = createManager(new AbortController().signal);
await expect(
manager.startAsync({ filePath, name: 'Crash log', yieldTimeMs: 30_000 })
).resolves.toEqual({ id: ARTIFACT_ID, ...outcome });
}
);
it('reports an upload that outlasts the yield time as uploading and keeps uploading', async () => {
let finalizeCalled!: () => void;
const finalizeCalledPromise = new Promise<void>(resolve => {
finalizeCalled = resolve;
});
mockMutationResults(createdResult);
mutation.mockReturnValueOnce({
toPromise: async () => {
finalizeCalled();
return finalizedResult;
},
});
let respond!: (response: InstanceType<typeof Response>) => void;
jest.mocked(fetch).mockReturnValueOnce(
new Promise(resolve => {
respond = resolve;
}) as ReturnType<typeof fetch>
);
const manager = createManager(new AbortController().signal);
await expect(
manager.startAsync({ filePath, name: 'Crash log', yieldTimeMs: 10 })
).resolves.toEqual({ id: ARTIFACT_ID, status: 'uploading' });
respond(new Response('', { status: 200 }));
await finalizeCalledPromise;
});
it('counts upload session creation toward the yield time', async () => {
mutation.mockReturnValueOnce({
toPromise: async () => {
await delay(400);
return createdResult;
},
});
mockPutUntilAborted();
const controller = new AbortController();
const manager = createManager(controller.signal);
const startedAt = performance.now();
await expect(
manager.startAsync({ filePath, name: 'Crash log', yieldTimeMs: 400 })
).resolves.toEqual({ id: ARTIFACT_ID, status: 'uploading' });
expect(performance.now() - startedAt).toBeLessThan(600);
controller.abort();
await manager.stoppedPromise;
});
function createManager(signal: AbortSignal): SandboxArtifactUploadManager {
return new SandboxArtifactUploadManager({
graphqlClient,
sandboxId: 'sandbox-id',
logger: logger as unknown as bunyan,
signal,
});
}
function mockPutUntilAborted(): void {
jest.mocked(fetch).mockImplementationOnce((async (_url: unknown, init?: RequestInit) => {
await new Promise<never>((_resolve, reject) => {
init!.signal!.addEventListener('abort', () => {
reject(new AbortError('The user aborted a request.'));
});
});
}) as unknown as typeof fetch);
}
function mockMutationResults(...results: unknown[]): void {
for (const result of results) {
mutation.mockReturnValueOnce({ toPromise: async () => result });
}
}
});
@@ -1,9 +1,11 @@
import { SandboxDaemonResponseZ, SystemError } from '@expo/eas-build-job';
import Log from '@expo/logger';
import { type Client } from '@urql/core';
import http from 'node:http';
import net, { AddressInfo } from 'node:net';
import WebSocket, { WebSocketServer } from 'ws';
import * as sandboxCommandImplementations from '../sandboxCommandImplementations';
import { startSandboxDaemonAsync } from '../sandboxDaemon';
jest.unmock('@expo/logger');
@@ -14,6 +16,7 @@ jest.unmock('node:fs/promises');
describe(startSandboxDaemonAsync.name, () => {
const logger = Log.child({ buildStepId: 'sandbox-test' });
const graphqlClient = {} as Client;
it.each(['ftp://localhost', 'not a URL'])('reports invalid server URL %s', async serverUrl => {
const errorLog = jest.spyOn(logger, 'error').mockImplementation(() => {});
const daemon = await startSandboxDaemonAsync({
@@ -23,6 +26,8 @@ describe(startSandboxDaemonAsync.name, () => {
logger,
workingDirectory: process.cwd(),
env: process.env,
graphqlClient,
sandboxId: 'sandbox-id',
});
try {
await expect(daemon.ready).rejects.toBeInstanceOf(SystemError);
@@ -48,6 +53,8 @@ describe(startSandboxDaemonAsync.name, () => {
logger,
workingDirectory: process.cwd(),
env: process.env,
graphqlClient,
sandboxId: 'sandbox-id',
});
try {
const socket = await connection;
@@ -82,6 +89,8 @@ describe(startSandboxDaemonAsync.name, () => {
logger,
workingDirectory: process.cwd(),
env: process.env,
graphqlClient,
sandboxId: 'sandbox-id',
});
try {
await expect(daemon.ready).rejects.toBeInstanceOf(SystemError);
@@ -112,6 +121,8 @@ describe(startSandboxDaemonAsync.name, () => {
signal: controller.signal,
workingDirectory: process.cwd(),
env: process.env,
graphqlClient,
sandboxId: 'sandbox-id',
});
const connectionError = daemon.ready.catch(error => error);
@@ -140,6 +151,8 @@ describe(startSandboxDaemonAsync.name, () => {
logger,
workingDirectory: process.cwd(),
env: process.env,
graphqlClient,
sandboxId: 'sandbox-id',
});
await expect(daemon.ready).rejects.toThrow('Sandbox MCP server connection failed');
@@ -173,6 +186,8 @@ describe(startSandboxDaemonAsync.name, () => {
logger,
workingDirectory: process.cwd(),
env: process.env,
graphqlClient,
sandboxId: 'sandbox-id',
});
const socket = await connection;
const daemon = await daemonPromise;
@@ -204,6 +219,8 @@ describe(startSandboxDaemonAsync.name, () => {
logger,
workingDirectory: process.cwd(),
env: process.env,
graphqlClient,
sandboxId: 'sandbox-id',
});
const socket = await connection;
await daemon.ready;
@@ -247,6 +264,8 @@ describe(startSandboxDaemonAsync.name, () => {
logger,
workingDirectory: process.cwd(),
env: process.env,
graphqlClient,
sandboxId: 'sandbox-id',
});
const socket = await connection;
await daemon.ready;
@@ -289,7 +308,7 @@ describe(startSandboxDaemonAsync.name, () => {
)
).resolves.toMatchObject({
id: '3',
error: { code: -32603, message: 'Command session 999 does not exist.' },
error: { code: 1, message: 'Command session 999 does not exist.' },
});
await daemon.stopAsync();
@@ -297,6 +316,68 @@ describe(startSandboxDaemonAsync.name, () => {
await new Promise<void>(resolve => httpServer.close(() => resolve()));
});
it('returns an internal error for a command failure without a daemon error code', async () => {
const createCommandImplementations =
sandboxCommandImplementations.createSandboxCommandImplementations;
const createSpy = jest
.spyOn(sandboxCommandImplementations, 'createSandboxCommandImplementations')
.mockImplementation(options => {
const created = createCommandImplementations(options);
return {
...created,
commandImplementations: {
...created.commandImplementations,
execCommand: async () => {
throw new Error('Unexpected failure');
},
},
};
});
const httpServer = http.createServer();
const mcpServer = new WebSocketServer({ noServer: true });
httpServer.on('upgrade', (request, socket, head) => {
mcpServer.handleUpgrade(request, socket, head, client =>
mcpServer.emit('connection', client)
);
});
await new Promise<void>(resolve => httpServer.listen(0, '127.0.0.1', resolve));
const address = httpServer.address() as AddressInfo;
const connection = new Promise<WebSocket>(resolve => mcpServer.once('connection', resolve));
const daemon = await startSandboxDaemonAsync({
credential: 'secret-token',
serverUrl: `ws://127.0.0.1:${address.port}`,
reconnectDelayMs: 10,
logger,
workingDirectory: process.cwd(),
env: process.env,
graphqlClient,
sandboxId: 'sandbox-id',
});
const socket = await connection;
await daemon.ready;
const commandResponse = new Promise<string>(resolve =>
socket.once('message', data => resolve(`${data}`))
);
socket.send(
JSON.stringify({
jsonrpc: '2.0',
id: 'command-1',
method: 'execCommand',
params: { cmd: 'pwd' },
})
);
expect(JSON.parse(await commandResponse)).toEqual({
jsonrpc: '2.0',
id: 'command-1',
error: { code: -32603, message: 'Unexpected failure' },
});
await daemon.stopAsync();
mcpServer.close();
await new Promise<void>(resolve => httpServer.close(() => resolve()));
createSpy.mockRestore();
});
it('cancels the reconnect delay when stopped', async () => {
const httpServer = http.createServer();
const mcpServer = new WebSocketServer({ noServer: true });
@@ -315,6 +396,8 @@ describe(startSandboxDaemonAsync.name, () => {
logger,
workingDirectory: process.cwd(),
env: process.env,
graphqlClient,
sandboxId: 'sandbox-id',
});
const socket = await connection;
const daemon = await daemonPromise;
@@ -3,8 +3,12 @@ jest.unmock('fs/promises');
jest.unmock('node:fs');
jest.unmock('node:fs/promises');
import { type SandboxDaemonCommandResult, type SandboxDaemonMethod } from '@expo/eas-build-job';
import fs from 'node:fs/promises';
import { type SandboxDaemonCommandResult, SandboxDaemonErrorCode } from '@expo/eas-build-job';
import { type bunyan } from '@expo/logger';
import { type Client } from '@urql/core';
import fetch from 'node-fetch';
import { readFileSync, statSync } from 'node:fs';
import fs, { type FileHandle } from 'node:fs/promises';
import { spawnSync } from 'node:child_process';
import os from 'node:os';
import path from 'node:path';
@@ -16,18 +20,34 @@ import {
createSandboxCommandImplementations,
} from '../sandboxCommandImplementations';
const READ_FILE_LIMITS = { maxTextBytes: 40_000, maxImageBytes: 3_000_000 };
const FIXTURES_DIRECTORY = path.join(__dirname, 'fixtures');
const { Response } = jest.requireActual('node-fetch') as typeof import('node-fetch');
const ARTIFACT_ID = '0199c0de-7b3a-7c1e-8f00-1234567890ab';
describe('sandbox daemon commands', () => {
let commandImplementations: SandboxDaemonCommandImplementations;
let stopAsync: () => Promise<void>;
let workingDirectory: string;
let mutation: jest.Mock;
let graphqlClient: Client;
let logger: bunyan;
beforeEach(async () => {
workingDirectory = await fs.mkdtemp(path.join(os.tmpdir(), 'sandbox-command-executor-'));
mutation = jest.fn();
graphqlClient = { mutation } as unknown as Client;
logger = { info: jest.fn(), error: jest.fn() } as unknown as bunyan;
const controller = new AbortController();
const commands = createSandboxCommandImplementations({
workingDirectory,
env: process.env,
signal: controller.signal,
graphqlClient,
sandboxId: 'sandbox-id',
logger,
});
commandImplementations = commands.commandImplementations;
stopAsync = async () => {
@@ -55,6 +75,9 @@ describe('sandbox daemon commands', () => {
workingDirectory,
env: process.env,
signal: controller.signal,
graphqlClient,
sandboxId: 'sandbox-id',
logger,
});
const command = commands.commandImplementations.execCommand({ cmd: 'printf should-not-run' });
controller.abort();
@@ -194,9 +217,10 @@ describe('sandbox daemon commands', () => {
it('reports a missing working directory', async () => {
await expect(
commandImplementations.execCommand({ cmd: 'true', workdir: 'missing' })
).rejects.toThrow(
`Working directory does not exist: ${path.join(workingDirectory, 'missing')}`
);
).rejects.toMatchObject({
code: SandboxDaemonErrorCode.BAD_REQUEST,
message: `Working directory does not exist: ${path.join(workingDirectory, 'missing')}`,
});
});
it('reports a working directory that is not a directory', async () => {
@@ -205,7 +229,10 @@ describe('sandbox daemon commands', () => {
await expect(
commandImplementations.execCommand({ cmd: 'true', workdir: 'file' })
).rejects.toThrow(`Working directory is not a directory: ${file}`);
).rejects.toMatchObject({
code: SandboxDaemonErrorCode.BAD_REQUEST,
message: `Working directory is not a directory: ${file}`,
});
});
it('reports an inaccessible working directory', async () => {
@@ -217,7 +244,10 @@ describe('sandbox daemon commands', () => {
await expect(
commandImplementations.execCommand({ cmd: 'true', workdir: 'inaccessible' })
).rejects.toThrow(`Working directory is not accessible: ${directory}`);
).rejects.toMatchObject({
code: SandboxDaemonErrorCode.BAD_REQUEST,
message: `Working directory is not accessible: ${directory}`,
});
await fs.chmod(directory, 0o700);
});
@@ -237,10 +267,226 @@ describe('sandbox daemon commands', () => {
expect(isProcessRunning(childPid)).toBe(false);
});
describe('readFile', () => {
let readLengths: number[];
beforeEach(() => {
readLengths = [];
const open = fs.open;
jest.spyOn(fs, 'open').mockImplementation(async (...args) => {
const file = await open(...args);
const read = file.read.bind(file);
file.read = ((buffer: Buffer, offset: number, length: number, position: number) => {
readLengths.push(length);
return read(buffer, offset, length, position);
}) as FileHandle['read'];
return file;
});
});
afterEach(() => {
jest.restoreAllMocks();
});
async function readAsync(
contents: Buffer | string,
limits: Partial<typeof READ_FILE_LIMITS> = {}
) {
await fs.writeFile(path.join(workingDirectory, 'file'), contents);
return await commandImplementations.readFile({
path: 'file',
...READ_FILE_LIMITS,
...limits,
});
}
async function readFixtureAsync(name: string, limits: Partial<typeof READ_FILE_LIMITS> = {}) {
return await commandImplementations.readFile({
path: path.join(FIXTURES_DIRECTORY, name),
...READ_FILE_LIMITS,
...limits,
});
}
it.each([
['leiothrix.png', 'image/png'],
['sparrow.jpg', 'image/jpeg'],
['robin.gif', 'image/gif'],
['chat.webp', 'image/webp'],
])('returns %s as an %s image with its data', async (name, mimeType) => {
const contents = readFileSync(path.join(FIXTURES_DIRECTORY, name));
expect(await readFixtureAsync(name)).toEqual({
kind: 'image',
mimeType,
data: contents.toString('base64'),
});
expect(readLengths).toEqual([12, contents.length]);
});
it('detects an image by its content instead of its extension', async () => {
await fs.copyFile(
path.join(FIXTURES_DIRECTORY, 'leiothrix.png'),
path.join(workingDirectory, 'screenshot.txt')
);
expect(
await commandImplementations.readFile({ path: 'screenshot.txt', ...READ_FILE_LIMITS })
).toMatchObject({ kind: 'image', mimeType: 'image/png' });
});
it('reports an image over the byte limit after reading only its header', async () => {
const { size } = statSync(path.join(FIXTURES_DIRECTORY, 'leiothrix.png'));
expect(await readFixtureAsync('leiothrix.png', { maxImageBytes: size - 1 })).toEqual({
kind: 'image',
mimeType: 'image/png',
size,
error: 'tooLarge',
});
expect(readLengths).toEqual([12]);
});
it('returns a text file within the limit in full', async () => {
expect(await readFixtureAsync('birds.txt')).toEqual({
kind: 'text',
text: 'Robin, sparrow, leiothrix and chat\nZażółć gęślą jaźń\n€ 🐦\n',
truncated: false,
size: 71,
});
});
it('reads no more text than the limit without splitting a character', async () => {
expect(await readFixtureAsync('birds.txt', { maxTextBytes: 38 })).toEqual({
kind: 'text',
text: 'Robin, sparrow, leiothrix and chat\nZa',
truncated: true,
size: 71,
});
expect(readLengths).toEqual([12, 38]);
});
it.each([
['invalid UTF-8', Buffer.from([0x61, 0x80, 0x62])],
['an incomplete final character', Buffer.from([0x61, 0xe2, 0x82])],
])('reports a file with %s as binary', async (_, contents) => {
expect(await readAsync(contents)).toEqual({ kind: 'binary', size: 3 });
});
it('reports a missing file', async () => {
await expect(
commandImplementations.readFile({ path: 'missing', ...READ_FILE_LIMITS })
).rejects.toMatchObject({
code: SandboxDaemonErrorCode.BAD_REQUEST,
message: `File does not exist: ${path.join(workingDirectory, 'missing')}`,
});
});
it('reports an inaccessible file', async () => {
const file = path.join(workingDirectory, 'inaccessible');
await fs.writeFile(file, 'content', { mode: 0o000 });
await expect(
commandImplementations.readFile({ path: 'inaccessible', ...READ_FILE_LIMITS })
).rejects.toMatchObject({
code: SandboxDaemonErrorCode.BAD_REQUEST,
message: `File is not accessible: ${file}`,
});
});
it('rejects a directory', async () => {
const directory = path.join(workingDirectory, 'directory');
await fs.mkdir(directory);
await expect(
commandImplementations.readFile({ path: 'directory', ...READ_FILE_LIMITS })
).rejects.toMatchObject({
code: SandboxDaemonErrorCode.BAD_REQUEST,
message: `${directory} is not a regular file.`,
});
});
it('rejects a FIFO without waiting for a writer', async () => {
const fifo = path.join(workingDirectory, 'fifo');
expect(spawnSync('mkfifo', [fifo]).status).toBe(0);
await expect(
commandImplementations.readFile({ path: 'fifo', ...READ_FILE_LIMITS })
).rejects.toMatchObject({
code: SandboxDaemonErrorCode.BAD_REQUEST,
message: `${fifo} is not a regular file.`,
});
});
});
it('rejects a directory with a hint to upload an archive', async () => {
await fs.mkdir(path.join(workingDirectory, 'dist'));
await expect(
commandImplementations.uploadArtifact({ path: 'dist', name: 'Build output' })
).rejects.toMatchObject({
code: SandboxDaemonErrorCode.BAD_REQUEST,
message: expect.stringContaining(
`${path.join(workingDirectory, 'dist')} is not a regular file. To upload a directory, create an archive first`
),
});
expect(mutation).not.toHaveBeenCalled();
});
it('uploads an artifact from a path relative to the working directory and waits for it by default', async () => {
await fs.mkdir(path.join(workingDirectory, 'logs'));
await fs.writeFile(path.join(workingDirectory, 'logs', 'crash.log'), 'crash');
mutation
.mockReturnValueOnce({
toPromise: async () => ({
data: {
sandbox: {
createArtifactUploadSession: {
artifact: { id: ARTIFACT_ID },
uploadSession: { url: 'https://uploads.expo.test/artifact', headers: {} },
},
},
},
}),
})
.mockReturnValueOnce({
toPromise: async () => ({
data: {
sandbox: {
finalizeArtifact: {
id: ARTIFACT_ID,
downloadUrl: 'https://downloads.expo.test/artifact',
},
},
},
}),
});
jest.mocked(fetch).mockResolvedValueOnce(new Response('', { status: 200 }));
const result = await commandImplementations.uploadArtifact({
path: 'logs/crash.log',
name: 'Crash log',
});
expect(result).toEqual({
id: ARTIFACT_ID,
status: 'uploaded',
downloadUrl: 'https://downloads.expo.test/artifact',
});
expect(mutation).toHaveBeenCalledWith(
expect.anything(),
{ sandboxId: 'sandbox-id', input: { name: 'Crash log', filename: 'crash.log', size: 5 } },
expect.anything()
);
});
async function readUntilAsync(
initial: SandboxDaemonCommandResult<SandboxDaemonMethod>,
isReady: (output: string, result: SandboxDaemonCommandResult<SandboxDaemonMethod>) => boolean
): Promise<SandboxDaemonCommandResult<SandboxDaemonMethod>> {
initial: SandboxDaemonCommandResult<'execCommand' | 'writeStdin'>,
isReady: (
output: string,
result: SandboxDaemonCommandResult<'execCommand' | 'writeStdin'>
) => boolean
): Promise<SandboxDaemonCommandResult<'execCommand' | 'writeStdin'>> {
let result = initial;
let output = result.output;
const deadline = Date.now() + 10_000;
@@ -1,5 +1,4 @@
import { type bunyan } from '@expo/logger';
import { Readable } from 'node:stream';
import { writeFile } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
@@ -14,7 +13,7 @@ const ctx = {} as CustomBuildContext;
const filePath = path.join(os.tmpdir(), 'logs.ndjson');
const args = { deviceRunSessionId: 'session', udid: 'A', filePath, logger };
it('uploads NDJSON with explicit user-app scope and destroys the stream afterward', async () => {
it('uploads NDJSON with explicit user-app scope', async () => {
await writeFile(filePath, '{"pid":42}\n');
await uploadServeSimLogsFileAsync(ctx, args);
expect(uploadDeviceRunSessionArtifactAsync).toHaveBeenCalledWith(
@@ -26,9 +25,6 @@ it('uploads NDJSON with explicit user-app scope and destroys the stream afterwar
metadata: expect.objectContaining({ scope: 'user-apps', udid: 'A' }),
})
);
expect(
(jest.mocked(uploadDeviceRunSessionArtifactAsync).mock.calls[0][1].stream as Readable).destroyed
).toBe(true);
});
it('skips empty and missing files', async () => {
@@ -38,15 +34,15 @@ it('skips empty and missing files', async () => {
expect(uploadDeviceRunSessionArtifactAsync).not.toHaveBeenCalled();
});
it('handles a file read error while upload allocation is pending without an unhandled error', async () => {
it('aborts the upload on a file read error without an unhandled error', async () => {
await writeFile(filePath, '{}\n');
jest
.mocked(uploadDeviceRunSessionArtifactAsync)
.mockImplementationOnce(async (_ctx, { stream, signal }) => {
.mockImplementationOnce(async (_ctx, { openStream, signal }) => {
const aborted = new Promise<void>((_resolve, reject) => {
signal!.addEventListener('abort', () => reject(signal!.reason), { once: true });
});
(stream as Readable).destroy(new Error('read failure'));
openStream().destroy(new Error('read failure'));
await aborted;
});
await expect(uploadServeSimLogsFileAsync(ctx, args)).resolves.toBeUndefined();
@@ -50,8 +50,8 @@ describe(uploadServeSimMetricsFileAsync, () => {
let uploaded = '';
jest
.mocked(uploadDeviceRunSessionArtifactAsync)
.mockImplementation(async (_ctx, { stream }) => {
uploaded = await readStreamAsync(stream);
.mockImplementation(async (_ctx, { openStream }) => {
uploaded = await readStreamAsync(openStream());
});
await uploadServeSimMetricsFileAsync(ctx, {
@@ -87,8 +87,8 @@ describe(uploadServeSimMetricsFileAsync, () => {
await writeFile(filePath, NDJSON);
jest
.mocked(uploadDeviceRunSessionArtifactAsync)
.mockImplementation(async (_ctx, { stream }) => {
await readStreamAsync(stream);
.mockImplementation(async (_ctx, { openStream }) => {
await readStreamAsync(openStream());
});
await uploadServeSimMetricsFileAsync(ctx, {
@@ -163,7 +163,7 @@ export async function uploadAgentDeviceArtifactAsync(
? { metadata: { displayRotation: artifact.displayRotation } }
: {}),
size,
stream: createReadStream(temporaryArtifactPath),
openStream: () => createReadStream(temporaryArtifactPath),
});
} finally {
await rm(temporaryDirectory, { recursive: true, force: true });
@@ -0,0 +1,253 @@
import { SystemError, UserError } from '@expo/eas-build-job';
import { type bunyan } from '@expo/logger';
import turtleSpawn from '@expo/turtle-spawn';
import { spawn } from 'node:child_process';
import { once } from 'node:events';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import readline from 'node:readline';
import { leaseAgentRunProviderCredentialsAsync } from './agentRunLease';
import { type AgentCli, type AgentInvocation, type AgentLogMessage } from './agents/agent';
import { CLAUDE_CODE_CLI, prepareClaudeCodeAsync } from './agents/claude';
import { CODEX_CLI, prepareCodexAsync } from './agents/codex';
import { Sentry } from '../../sentry';
import { killProcessGroup } from '../../utils/processes';
export type AgentKind = 'claude-code' | 'codex';
const AGENT_CLIS: Record<AgentKind, AgentCli> = {
'claude-code': CLAUDE_CODE_CLI,
codex: CODEX_CLI,
};
export const AGENT_KINDS = Object.keys(AGENT_CLIS) as AgentKind[];
const KILL_GRACE_PERIOD_MS = 10_000;
const OUTPUT_CLOSE_GRACE_PERIOD_MS = 5_000;
export async function runAgentAsync({
agentKind,
agentRunId,
prompt,
expoToken,
expoApiV2BaseUrl,
mcpUrl,
pathEnv,
npmCacheUrl,
logger,
signal,
}: {
agentKind: AgentKind;
agentRunId: string;
prompt: string;
expoToken: string;
expoApiV2BaseUrl: string;
/** The Expo MCP server's streamable HTTP endpoint. */
mcpUrl: string;
pathEnv: string;
npmCacheUrl?: string;
logger: bunyan;
signal: AbortSignal;
}): Promise<void> {
const leaseOptions = { expoApiV2BaseUrl, expoToken, agentRunId, signal };
let credentials = await leaseAgentRunProviderCredentialsAsync(leaseOptions);
const secrets = new Set<string>();
const runDirectory = await fs.mkdtemp(path.join(os.tmpdir(), 'eas-agent-run-'));
try {
const cliDirectory = path.join(runDirectory, 'cli');
const homeDirectory = path.join(runDirectory, 'home');
const tmpDirectory = path.join(runDirectory, 'tmp');
const workingDirectory = path.join(runDirectory, 'cwd');
await Promise.all(
[cliDirectory, homeDirectory, tmpDirectory, workingDirectory].map(directory =>
fs.mkdir(directory)
)
);
// Of the job's environment, only `PATH` reaches npm and the agent.
const minimalEnv = {
PATH: pathEnv,
HOME: homeDirectory,
TMPDIR: tmpDirectory,
LANG: 'C.UTF-8',
};
const { packageSpec, bin, formatOutputLine } = AGENT_CLIS[agentKind];
logger.info(`Installing ${packageSpec}.`);
try {
await turtleSpawn(
'npm',
[
'install',
...['--prefix', cliDirectory],
...['--no-save', '--package-lock=false', '--no-audit', '--no-fund'],
...(npmCacheUrl ? ['--registry', npmCacheUrl] : []),
packageSpec,
],
{ env: minimalEnv, signal }
);
} catch (error) {
signal.throwIfAborted();
throw new SystemError(
`Could not install ${packageSpec} on the worker, so the agent cannot start. This is usually a temporary network or registry problem; start the agent run again.`,
{ cause: error }
);
}
const mcpServer = {
url: mcpUrl,
headers: { Authorization: `Bearer ${expoToken}`, 'X-Expo-Agent-Run-Id': agentRunId },
};
logger.info('Starting the agent.');
for (let isResuming = false; ; isResuming = true) {
signal.throwIfAborted();
let invocation: AgentInvocation;
switch (agentKind) {
case 'claude-code':
invocation = await prepareClaudeCodeAsync({
homeDirectory,
minimalEnv,
mcpServer,
prompt,
credentials,
isResuming,
});
break;
case 'codex':
invocation = await prepareCodexAsync({
homeDirectory,
minimalEnv,
mcpServer,
prompt,
credentials,
isResuming,
});
break;
}
for (const secret of invocation.secrets) {
secrets.add(secret);
}
const { exitCode, exitSignal } = await runAgentProcessAsync({
command: path.join(cliDirectory, 'node_modules', '.bin', bin),
...invocation,
cwd: workingDirectory,
formatOutputLine,
secrets: [...secrets],
logger,
signal,
});
if (exitCode === 0) {
return;
}
const exitDescription = exitSignal
? `was terminated by ${exitSignal}`
: `exited with code ${exitCode}`;
// www hands out the stored token until it has expired, so a different token means the
// one the agent used has expired.
const nextCredentials = await leaseAgentRunProviderCredentialsAsync(leaseOptions);
if (nextCredentials.accessToken === credentials.accessToken) {
throw new UserError(
'EAS_RUN_AGENT_FAILED',
`The agent ${exitDescription} before it finished the task. Its output above shows what went wrong; fix the cause and start a new agent run.`
);
}
logger.info(
`The agent ${exitDescription} after its provider credentials expired. Resuming it with new credentials.`
);
credentials = nextCredentials;
}
} finally {
await fs.rm(runDirectory, { recursive: true, force: true });
}
}
async function runAgentProcessAsync({
command,
args,
env,
cwd,
stdin,
formatOutputLine,
secrets,
logger,
signal,
}: {
command: string;
args: string[];
env: Record<string, string>;
cwd: string;
stdin: string;
formatOutputLine: AgentCli['formatOutputLine'];
secrets: string[];
logger: bunyan;
signal: AbortSignal;
}): Promise<{ exitCode: number | null; exitSignal: NodeJS.Signals | null }> {
signal.throwIfAborted();
// Detached, so the agent leads a process group that can be signaled as a whole.
const child = spawn(command, args, { cwd, env, detached: true });
const outputClosed = new Promise<void>(resolve => child.once('close', () => resolve()));
const redact = (text: string): string =>
secrets.reduce((masked, secret) => masked.replaceAll(secret, '[redacted]'), text);
const stdoutLogger = logger.child({ source: 'stdout' });
readline.createInterface({ input: child.stdout }).on('line', line => {
let messages: AgentLogMessage[];
try {
messages = formatOutputLine(line);
} catch (error) {
// An exception in a readline listener is uncaught and would end the worker process.
Sentry.capture('Could not format a line of the agent output', error as Error);
return;
}
for (const { level, message } of messages) {
stdoutLogger[level](redact(message).trimEnd());
}
});
const stderrLogger = logger.child({ source: 'stderr' });
readline.createInterface({ input: child.stderr }).on('line', line => {
stderrLogger.info(redact(line));
});
// The agent may exit before it reads the prompt; its exit status reports that.
child.stdin.on('error', () => {});
child.stdin.end(stdin);
let killTimer: NodeJS.Timeout | undefined;
const stop = (): void => {
if (killTimer) {
return;
}
killProcessGroup(child, 'SIGTERM');
killTimer = setTimeout(() => killProcessGroup(child, 'SIGKILL'), KILL_GRACE_PERIOD_MS);
};
signal.addEventListener('abort', stop);
let exitCode: number | null;
let exitSignal: NodeJS.Signals | null;
try {
[exitCode, exitSignal] = await once(child, 'exit');
} catch (error: any) {
// `once` rejects when the child emits `error`, which is how a failed spawn reports.
throw new SystemError(
`Could not start the agent CLI on the worker: ${error.message}. Start the agent run again, and contact Expo support if it keeps failing.`,
{ cause: error }
);
} finally {
clearTimeout(killTimer);
signal.removeEventListener('abort', stop);
}
// Whatever the agent left running ends with it. Something outside its process group could
// still hold the output open, so the wait for the last lines is bounded.
killProcessGroup(child, 'SIGKILL');
let outputGraceTimer: NodeJS.Timeout | undefined;
await Promise.race([
outputClosed,
new Promise<void>(resolve => {
outputGraceTimer = setTimeout(resolve, OUTPUT_CLOSE_GRACE_PERIOD_MS);
}),
]);
clearTimeout(outputGraceTimer);
signal.throwIfAborted();
return { exitCode, exitSignal };
}
@@ -0,0 +1,103 @@
import { SystemError, UserError } from '@expo/eas-build-job';
import fetch from 'node-fetch';
import { z } from 'zod';
import { promiseRetryWithCondition } from '../../utils/promiseRetryWithCondition';
const AgentRunProviderCredentialsZ = z.discriminatedUnion('provider', [
z.object({
provider: z.literal('anthropic'),
accessToken: z.string().min(1),
}),
z.object({
provider: z.literal('openai'),
idToken: z.string().min(1),
accessToken: z.string().min(1),
accountId: z.string().min(1),
}),
]);
const LeaseResponseZ = z.object({ data: AgentRunProviderCredentialsZ });
const ErrorResponseZ = z.object({
errors: z.array(z.object({ message: z.string().min(1) })).min(1),
});
export type AgentRunProviderCredentials = z.infer<typeof AgentRunProviderCredentialsZ>;
const MAX_ATTEMPTS = 3;
const RETRY_DELAY_MS = 2_000;
const REQUEST_TIMEOUT_MS = 15_000;
class RetryableLeaseError extends Error {}
export async function leaseAgentRunProviderCredentialsAsync(options: {
expoApiV2BaseUrl: string;
expoToken: string;
agentRunId: string;
signal?: AbortSignal;
}): Promise<AgentRunProviderCredentials> {
try {
return await promiseRetryWithCondition(
requestLeaseAsync,
error => error instanceof RetryableLeaseError,
{ retries: MAX_ATTEMPTS - 1, factor: 1, minTimeout: RETRY_DELAY_MS }
)(options);
} catch (error) {
if (error instanceof RetryableLeaseError) {
throw new SystemError(
`Could not lease provider credentials for the agent run because the Expo API failed ${MAX_ATTEMPTS} times in a row. This is usually temporary; start the agent run again, and contact Expo support if it keeps failing.`,
{ cause: error.cause ?? error }
);
}
throw error;
}
}
// Which failures count as retryable follows www's contract for this route.
async function requestLeaseAsync({
expoApiV2BaseUrl,
expoToken,
agentRunId,
signal,
}: {
expoApiV2BaseUrl: string;
expoToken: string;
agentRunId: string;
signal?: AbortSignal;
}): Promise<AgentRunProviderCredentials> {
const url = new URL(`agent-runs/${agentRunId}/auth-lease`, expoApiV2BaseUrl).toString();
let response;
let body: unknown;
try {
response = await fetch(url, {
method: 'POST',
headers: { Authorization: `Bearer ${expoToken}` },
timeout: REQUEST_TIMEOUT_MS,
signal,
});
// Reading the body can fail the way the request can, so it is retried with it.
body = response.ok ? await response.json() : undefined;
} catch (error) {
signal?.throwIfAborted();
throw new RetryableLeaseError('The request to the Expo API failed.', { cause: error });
}
if (response.ok) {
const parsed = LeaseResponseZ.safeParse(body);
if (!parsed.success) {
throw new SystemError(
'The Expo API returned provider credentials in an unexpected format, so the agent cannot run. This is a problem on our side; contact Expo support if it keeps happening.',
{ cause: parsed.error }
);
}
return parsed.data.data;
}
if (response.status < 500) {
const errorBody = ErrorResponseZ.safeParse(await response.json().catch(() => null));
throw new UserError(
'EAS_RUN_AGENT_CREDENTIALS_UNAVAILABLE',
errorBody.success
? errorBody.data.errors[0].message
: `Expo refused to issue provider credentials for this agent run (HTTP ${response.status}). The run may have ended, or its provider connection may have been removed. Start a new agent run.`
);
}
throw new RetryableLeaseError(`The Expo API responded with HTTP ${response.status}.`);
}
@@ -0,0 +1,137 @@
import { CLAUDE_CODE_CLI } from '../claude';
import { readFixtureLines } from './readFixture';
const format = CLAUDE_CODE_CLI.formatOutputLine;
function formatFixture(name: string): ReturnType<typeof format> {
return readFixtureLines(name).flatMap(line => format(line));
}
describe('CLAUDE_CODE_CLI.formatOutputLine', () => {
it('renders a run with a tool call and skips thinking and token counts', () => {
expect(formatFixture('claude-code-tool-call.jsonl')).toEqual([
{ level: 'info', message: 'MCP server expo is connected.' },
{ level: 'info', message: 'Tool call: mcp__expo__sandbox_exec {"command":"echo hi"}' },
{ level: 'info', message: 'Tool result: stub ran: echo hi' },
{
level: 'info',
message:
'Agent: The command returned exactly: `stub ran: echo hi`\n\nThe tools I can call are:\n\n1. `mcp__expo__sandbox_exec` — Execute a shell command in a running sandbox\n2. `mcp__expo__sandbox_list` — List the sandboxes of the project',
},
]);
});
it('renders a run that failed before it could do anything', () => {
expect(formatFixture('claude-code-not-logged-in.jsonl')).toEqual([
{ level: 'info', message: 'MCP server expo is connected.' },
{ level: 'info', message: 'Agent: Not logged in · Please run /login' },
{ level: 'error', message: 'Agent failed: Not logged in · Please run /login' },
]);
});
it('reports every MCP server, warning about one the agent could not connect to', () => {
const event = {
type: 'system',
subtype: 'init',
mcp_servers: [
{ name: 'expo', status: 'failed' },
{ name: 'other', status: 'connected' },
],
};
expect(format(JSON.stringify(event))).toEqual([
{ level: 'warn', message: 'MCP server expo is not connected (failed).' },
{ level: 'info', message: 'MCP server other is connected.' },
]);
});
it('renders a failed tool result as a warning and joins its text blocks', () => {
const event = {
type: 'user',
message: {
content: [
{
type: 'tool_result',
tool_use_id: 'toolu_1',
is_error: true,
content: [
{ type: 'text', text: 'first' },
{ type: 'image', source: {} },
{ type: 'text', text: 'second' },
],
},
],
},
};
expect(format(JSON.stringify(event))).toEqual([
{ level: 'warn', message: 'Tool error: first\nsecond' },
]);
});
it('describes a failure without a result text by its errors, then by its subtype', () => {
expect(
format(
JSON.stringify({
type: 'result',
subtype: 'error_max_turns',
is_error: true,
errors: ['Reached max turns (1)'],
})
)
).toEqual([{ level: 'error', message: 'Agent failed: Reached max turns (1)' }]);
expect(
format(
JSON.stringify({
type: 'result',
subtype: 'error_max_turns',
is_error: true,
result: '',
errors: ['Reached max turns (1)'],
})
)
).toEqual([{ level: 'error', message: 'Agent failed: Reached max turns (1)' }]);
expect(
format(
JSON.stringify({ type: 'result', subtype: 'error_max_turns', is_error: true, errors: [] })
)
).toEqual([{ level: 'error', message: 'Agent failed: error_max_turns' }]);
expect(
format(JSON.stringify({ type: 'result', subtype: 'error_during_execution', is_error: true }))
).toEqual([{ level: 'error', message: 'Agent failed: error_during_execution' }]);
});
it('skips what it does not know without failing', () => {
expect(format(JSON.stringify({ type: 'rate_limit_event', rate_limit_info: {} }))).toEqual([]);
expect(format(JSON.stringify({ type: 'stream_event', event: {} }))).toEqual([]);
expect(format(JSON.stringify({ type: 'assistant', message: { content: 'text' } }))).toEqual([]);
expect(format(JSON.stringify({ type: 'user', message: { content: 'the prompt' } }))).toEqual(
[]
);
expect(format(JSON.stringify({ type: 'result', subtype: 'success', is_error: 'yes' }))).toEqual(
[]
);
expect(format('null')).toEqual([]);
expect(format('[]')).toEqual([]);
});
it('keeps the known blocks of a message with an unknown block', () => {
const event = {
type: 'assistant',
message: {
content: [
{ type: 'server_tool_use', id: 'srvtoolu_1', name: 'web_search', input: {} },
{ type: 'text', text: 'Searching.' },
{ type: 'tool_use', id: 'toolu_1', name: 'mcp__expo__sandbox_list', input: 'bad' },
],
},
};
expect(format(JSON.stringify(event))).toEqual([
{ level: 'info', message: 'Agent: Searching.' },
]);
});
it('passes a line that is not JSON through as it is', () => {
expect(format('Error: ENOENT: no such file or directory')).toEqual([
{ level: 'info', message: 'Error: ENOENT: no such file or directory' },
]);
});
});
@@ -0,0 +1,138 @@
import { CODEX_CLI } from '../codex';
import { readFixtureLines } from './readFixture';
const format = CODEX_CLI.formatOutputLine;
function formatFixture(name: string): ReturnType<typeof format> {
return readFixtureLines(name).flatMap(line => format(line));
}
describe('CODEX_CLI.formatOutputLine', () => {
it('renders a run with two tool calls and skips the thread and turn events', () => {
expect(formatFixture('codex-tool-calls.jsonl')).toEqual([
{
level: 'info',
message:
'Agent: I will run these three operations in order and report the exact result of each.\n',
},
{ level: 'info', message: 'Tool call: expo.sandbox_exec {"command":"echo hi"}' },
{ level: 'info', message: 'Tool result: stub ran: echo hi' },
{ level: 'info', message: 'Tool call: expo.sandbox_list {}' },
{ level: 'info', message: 'Tool result: no sandboxes' },
{
level: 'info',
message:
'Agent: The exact results, in order:\n\n1. `expo sandbox_exec` with command `echo hi`:\n ```json\n {"content":[{"type":"text","text":"stub ran: echo hi"}],"isError":false}\n ```\n\n2. `expo sandbox_list`:\n ```json\n {"content":[{"type":"text","text":"no sandboxes"}],"isError":false}\n ```\n\n3. `apply_patch` to create `probe.txt` with the single line `hello` failed:\n ```text\n patch rejected: writing is blocked by read-only sandbox; rejected by user approval settings\n ```\n The file was not created.',
},
]);
});
it('renders a refused tool call as a warning', () => {
expect(formatFixture('codex-tool-call-refused.jsonl')).toEqual([
{
level: 'info',
message:
"Agent: I will run these three operations in order and report each tool's actual result.\n",
},
{ level: 'info', message: 'Tool call: expo.sandbox_exec {"command":"echo hi"}' },
{
level: 'warn',
message: 'Tool error: MCP tool call requires approval, but approval policy is never',
},
{
level: 'info',
message: expect.stringMatching(
/^Agent: 1\. Called `sandbox_exec` on the `expo` MCP server/
),
},
]);
});
it('renders a failed turn with its transient errors as warnings and the final one as an error', () => {
const reconnecting = (attempt: number): { level: 'warn'; message: string } => ({
level: 'warn',
message: `Error: Reconnecting... ${attempt}/5 (workspace routing discovery unauthorized (401))`,
});
expect(formatFixture('codex-failed-turn.jsonl')).toEqual([
reconnecting(2),
reconnecting(3),
reconnecting(4),
reconnecting(5),
{
level: 'warn',
message:
'Error: Falling back from WebSockets to HTTPS transport. workspace routing discovery unauthorized (401)',
},
reconnecting(1),
reconnecting(2),
reconnecting(3),
reconnecting(4),
reconnecting(5),
{ level: 'warn', message: 'Error: workspace routing discovery unauthorized (401)' },
{ level: 'error', message: 'Agent failed: workspace routing discovery unauthorized (401)' },
]);
});
it('renders an agent message once, when it completes', () => {
const item = {
id: 'item_1',
type: 'agent_message',
text: 'Hello',
};
expect(format(JSON.stringify({ type: 'item.started', item }))).toEqual([]);
expect(format(JSON.stringify({ type: 'item.completed', item }))).toEqual([
{ level: 'info', message: 'Agent: Hello' },
]);
});
it('renders a tool call without arguments', () => {
const item = {
id: 'item_1',
type: 'mcp_tool_call',
server: 'expo',
tool: 'sandbox_list',
arguments: null,
result: null,
error: null,
status: 'in_progress',
};
expect(format(JSON.stringify({ type: 'item.started', item }))).toEqual([
{ level: 'info', message: 'Tool call: expo.sandbox_list {}' },
]);
});
it('renders a tool result the server marked as an error as a warning', () => {
const item = {
id: 'item_1',
type: 'mcp_tool_call',
server: 'expo',
tool: 'sandbox_exec',
arguments: { command: 'false' },
result: { content: [{ type: 'text', text: 'exit code 1' }], structured_content: null },
error: null,
status: 'failed',
};
expect(format(JSON.stringify({ type: 'item.completed', item }))).toEqual([
{ level: 'warn', message: 'Tool error: exit code 1' },
]);
});
it('skips what it does not know without failing', () => {
expect(format(JSON.stringify({ type: 'turn.completed', usage: {} }))).toEqual([]);
expect(
format(
JSON.stringify({
type: 'item.completed',
item: { id: 'item_1', type: 'reasoning', text: 'Thinking.' },
})
)
).toEqual([]);
expect(format(JSON.stringify({ type: 'item.completed', item: 'bad' }))).toEqual([]);
expect(format(JSON.stringify({ type: 'error', message: 42 }))).toEqual([]);
expect(format('true')).toEqual([]);
});
it('passes a line that is not JSON through as it is', () => {
expect(format('thread panicked')).toEqual([{ level: 'info', message: 'thread panicked' }]);
});
});
@@ -0,0 +1,3 @@
{"type": "system", "subtype": "init", "cwd": "/tmp/eas-agent-run-example/cwd", "session_id": "11111111-1111-4111-8111-111111111111", "tools": ["mcp__expo__sandbox_exec", "mcp__expo__sandbox_list"], "mcp_servers": [{"name": "expo", "status": "connected", "source": "dynamic"}], "model": "claude-haiku-4-5-20251001", "permissionMode": "default", "apiKeySource": "none", "claude_code_version": "2.1.291", "uuid": "22222222-2222-4222-8222-000000000000"}
{"type": "assistant", "message": {"diagnostics": null, "id": "msg_001", "container": null, "model": "<synthetic>", "role": "assistant", "stop_details": null, "stop_reason": "stop_sequence", "stop_sequence": "", "type": "message", "usage": {"output_tokens_details": null, "input_tokens": 0, "output_tokens": 0, "cache_creation_input_tokens": 0, "cache_read_input_tokens": 0, "server_tool_use": {"web_search_requests": 0, "web_fetch_requests": 0}, "service_tier": null, "cache_creation": {"ephemeral_1h_input_tokens": 0, "ephemeral_5m_input_tokens": 0}, "inference_geo": null, "iterations": null, "speed": null, "fallback_credit": null}, "content": [{"type": "text", "text": "Not logged in · Please run /login"}], "context_management": null}, "parent_tool_use_id": null, "session_id": "11111111-1111-4111-8111-111111111111", "uuid": "22222222-2222-4222-8222-000000000000", "timestamp": "2026-10-06T12:24:54.066Z", "error": "authentication_failed", "is_api_error_message": true}
{"duration_api_ms": 0, "stop_reason": "stop_sequence", "session_id": "11111111-1111-4111-8111-111111111111", "total_cost_usd": 0, "usage": {"output_tokens_details": {"thinking_tokens": 0}, "input_tokens": 0, "cache_creation_input_tokens": 0, "cache_read_input_tokens": 0, "output_tokens": 0, "server_tool_use": {"web_search_requests": 0, "web_fetch_requests": 0}, "service_tier": "standard", "cache_creation": {"ephemeral_1h_input_tokens": 0, "ephemeral_5m_input_tokens": 0}, "inference_geo": "", "iterations": [], "speed": "standard", "fallback_credit": null}, "modelUsage": {}, "permission_denials": [], "terminal_reason": "api_error", "fast_mode_state": "off", "fast_mode_disabled_reason": "sdk_opt_in_required", "subagent_stats": {"spawned": 0, "requested": {"background": 0, "foreground": 0, "unset": 0}, "started_in_background": 0, "max_depth": 0, "spawned_by_subagents": 0, "completed": 0, "failed": 0, "killed": {"parent": 0, "user": 0, "system": 0}, "refused": {"depth_limit": 0, "concurrency_limit": 0, "budget": 0}, "by_type": {}}, "is_error": true, "num_turns": 1, "subtype": "success", "api_error_status": null, "result": "Not logged in · Please run /login", "type": "result", "duration_ms": 20, "uuid": "22222222-2222-4222-8222-000000000000", "queued_turn_count": 0, "result_index": 0}
@@ -0,0 +1,11 @@
{"type": "system", "subtype": "init", "cwd": "/tmp/eas-agent-run-example/cwd", "session_id": "11111111-1111-4111-8111-111111111111", "tools": ["mcp__expo__sandbox_exec", "mcp__expo__sandbox_list"], "mcp_servers": [{"name": "expo", "status": "connected", "source": "dynamic"}], "model": "claude-haiku-4-5-20251001", "permissionMode": "default", "apiKeySource": "none", "claude_code_version": "2.1.291", "uuid": "22222222-2222-4222-8222-000000000000"}
{"type": "system", "subtype": "thinking_tokens", "estimated_tokens": 50, "estimated_tokens_delta": 50, "session_id": "11111111-1111-4111-8111-111111111111", "uuid": "22222222-2222-4222-8222-000000000000"}
{"type": "system", "subtype": "thinking_tokens", "estimated_tokens": 135, "estimated_tokens_delta": 85, "session_id": "11111111-1111-4111-8111-111111111111", "uuid": "22222222-2222-4222-8222-000000000000"}
{"type": "assistant", "message": {"model": "claude-haiku-4-5-20251001", "id": "msg_001", "type": "message", "role": "assistant", "content": [{"type": "thinking", "thinking": "", "signature": "signature-removed"}], "container": null, "stop_reason": null, "stop_sequence": null, "stop_details": null, "usage": {"input_tokens": 9, "cache_creation_input_tokens": 8450, "cache_read_input_tokens": 0, "cache_creation": {"ephemeral_5m_input_tokens": 0, "ephemeral_1h_input_tokens": 8450}, "output_tokens": 1, "service_tier": "standard", "inference_geo": "not_available"}, "input_transformations": [], "diagnostics": null, "context_management": null}, "parent_tool_use_id": null, "session_id": "11111111-1111-4111-8111-111111111111", "uuid": "22222222-2222-4222-8222-000000000000", "timestamp": "2026-10-06T12:24:56.369Z", "request_id": "req_001", "thinking_duration_ms": 1012}
{"type": "assistant", "message": {"model": "claude-haiku-4-5-20251001", "id": "msg_001", "type": "message", "role": "assistant", "content": [{"type": "tool_use", "id": "toolu_001", "name": "mcp__expo__sandbox_exec", "input": {"command": "echo hi"}, "caller": {"type": "direct"}}], "container": null, "stop_reason": null, "stop_sequence": null, "stop_details": null, "usage": {"input_tokens": 9, "cache_creation_input_tokens": 8450, "cache_read_input_tokens": 0, "cache_creation": {"ephemeral_5m_input_tokens": 0, "ephemeral_1h_input_tokens": 8450}, "output_tokens": 1, "service_tier": "standard", "inference_geo": "not_available"}, "input_transformations": [], "diagnostics": null, "context_management": null}, "parent_tool_use_id": null, "session_id": "11111111-1111-4111-8111-111111111111", "uuid": "22222222-2222-4222-8222-000000000000", "timestamp": "2026-10-06T12:24:56.590Z", "request_id": "req_001", "tool_use_meta": [{"id": "toolu_01FxZWi8cmA45ua2osBST33c", "display_name": "Sandbox Exec", "server_display_name": "expo-stub"}]}
{"type": "user", "message": {"role": "user", "content": [{"tool_use_id": "toolu_001", "type": "tool_result", "content": [{"type": "text", "text": "stub ran: echo hi"}]}]}, "parent_tool_use_id": null, "session_id": "11111111-1111-4111-8111-111111111111", "uuid": "22222222-2222-4222-8222-000000000000", "timestamp": "2026-10-06T12:24:56.604Z", "tool_use_result": [{"type": "text", "text": "stub ran: echo hi"}]}
{"type": "system", "subtype": "thinking_tokens", "estimated_tokens": 50, "estimated_tokens_delta": 50, "session_id": "11111111-1111-4111-8111-111111111111", "uuid": "22222222-2222-4222-8222-000000000000"}
{"type": "system", "subtype": "thinking_tokens", "estimated_tokens": 156, "estimated_tokens_delta": 106, "session_id": "11111111-1111-4111-8111-111111111111", "uuid": "22222222-2222-4222-8222-000000000000"}
{"type": "assistant", "message": {"model": "claude-haiku-4-5-20251001", "id": "msg_002", "type": "message", "role": "assistant", "content": [{"type": "thinking", "thinking": "", "signature": "signature-removed"}], "container": null, "stop_reason": null, "stop_sequence": null, "stop_details": null, "usage": {"input_tokens": 8, "cache_creation_input_tokens": 206, "cache_read_input_tokens": 8450, "cache_creation": {"ephemeral_5m_input_tokens": 0, "ephemeral_1h_input_tokens": 206}, "output_tokens": 2, "service_tier": "standard", "inference_geo": "not_available"}, "input_transformations": [], "diagnostics": null, "context_management": null}, "parent_tool_use_id": null, "session_id": "11111111-1111-4111-8111-111111111111", "uuid": "22222222-2222-4222-8222-000000000000", "timestamp": "2026-10-06T12:24:58.292Z", "request_id": "req_002", "thinking_duration_ms": 828}
{"type": "assistant", "message": {"model": "claude-haiku-4-5-20251001", "id": "msg_002", "type": "message", "role": "assistant", "content": [{"type": "text", "text": "The command returned exactly: `stub ran: echo hi`\n\nThe tools I can call are:\n\n1. `mcp__expo__sandbox_exec` — Execute a shell command in a running sandbox\n2. `mcp__expo__sandbox_list` — List the sandboxes of the project"}], "container": null, "stop_reason": null, "stop_sequence": null, "stop_details": null, "usage": {"input_tokens": 8, "cache_creation_input_tokens": 206, "cache_read_input_tokens": 8450, "cache_creation": {"ephemeral_5m_input_tokens": 0, "ephemeral_1h_input_tokens": 206}, "output_tokens": 2, "service_tier": "standard", "inference_geo": "not_available"}, "input_transformations": [], "diagnostics": null, "context_management": null}, "parent_tool_use_id": null, "session_id": "11111111-1111-4111-8111-111111111111", "uuid": "22222222-2222-4222-8222-000000000000", "timestamp": "2026-10-06T12:24:58.835Z", "request_id": "req_002"}
{"duration_api_ms": 4085, "stop_reason": "end_turn", "session_id": "11111111-1111-4111-8111-111111111111", "total_cost_usd": 0, "usage": {"input_tokens": 17, "cache_creation_input_tokens": 8656, "cache_read_input_tokens": 8450, "output_tokens": 310, "output_tokens_details": {"thinking_tokens": 180}, "server_tool_use": {"web_search_requests": 0, "web_fetch_requests": 0}, "service_tier": "standard", "cache_creation": {"ephemeral_1h_input_tokens": 8656, "ephemeral_5m_input_tokens": 0}, "inference_geo": "not_available", "iterations": [], "speed": "standard", "fallback_credit": null}, "modelUsage": {"claude-haiku-4-5-20251001": {"inputTokens": 17, "outputTokens": 310, "cacheReadInputTokens": 8450, "cacheCreationInputTokens": 8656, "webSearchRequests": 0, "costUSD": 0, "contextWindow": 200000, "maxOutputTokens": 32000, "thinkingTokens": 180, "canonicalModel": "claude-haiku-4-5", "provider": "firstParty", "costBasis": "list"}}, "permission_denials": [], "terminal_reason": "completed", "fast_mode_state": "off", "fast_mode_disabled_reason": "sdk_opt_in_required", "subagent_stats": {"spawned": 0, "requested": {"background": 0, "foreground": 0, "unset": 0}, "started_in_background": 0, "max_depth": 0, "spawned_by_subagents": 0, "completed": 0, "failed": 0, "killed": {"parent": 0, "user": 0, "system": 0}, "refused": {"depth_limit": 0, "concurrency_limit": 0, "budget": 0}, "by_type": {}}, "is_error": false, "num_turns": 2, "subtype": "success", "api_error_status": null, "result": "The command returned exactly: `stub ran: echo hi`\n\nThe tools I can call are:\n\n1. `mcp__expo__sandbox_exec` — Execute a shell command in a running sandbox\n2. `mcp__expo__sandbox_list` — List the sandboxes of the project", "ttft_ms": 1696, "type": "result", "duration_ms": 4257, "uuid": "22222222-2222-4222-8222-000000000000", "ttft_stream_ms": 684, "time_to_request_ms": 85, "first_content_frame_ms": 684, "queued_turn_count": 0, "result_index": 0}
@@ -0,0 +1,14 @@
{"type": "thread.started", "thread_id": "01a10000-0000-7000-8000-000000000001"}
{"type": "turn.started"}
{"type": "error", "message": "Reconnecting... 2/5 (workspace routing discovery unauthorized (401))"}
{"type": "error", "message": "Reconnecting... 3/5 (workspace routing discovery unauthorized (401))"}
{"type": "error", "message": "Reconnecting... 4/5 (workspace routing discovery unauthorized (401))"}
{"type": "error", "message": "Reconnecting... 5/5 (workspace routing discovery unauthorized (401))"}
{"type": "item.completed", "item": {"id": "item_0", "type": "error", "message": "Falling back from WebSockets to HTTPS transport. workspace routing discovery unauthorized (401)"}}
{"type": "error", "message": "Reconnecting... 1/5 (workspace routing discovery unauthorized (401))"}
{"type": "error", "message": "Reconnecting... 2/5 (workspace routing discovery unauthorized (401))"}
{"type": "error", "message": "Reconnecting... 3/5 (workspace routing discovery unauthorized (401))"}
{"type": "error", "message": "Reconnecting... 4/5 (workspace routing discovery unauthorized (401))"}
{"type": "error", "message": "Reconnecting... 5/5 (workspace routing discovery unauthorized (401))"}
{"type": "error", "message": "workspace routing discovery unauthorized (401)"}
{"type": "turn.failed", "error": {"message": "workspace routing discovery unauthorized (401)"}}
@@ -0,0 +1,7 @@
{"type": "thread.started", "thread_id": "01a10000-0000-7000-8000-000000000001"}
{"type": "turn.started"}
{"type": "item.completed", "item": {"id": "item_0", "type": "agent_message", "text": "I will run these three operations in order and report each tool's actual result.\n"}}
{"type": "item.started", "item": {"id": "item_1", "type": "mcp_tool_call", "server": "expo", "tool": "sandbox_exec", "arguments": {"command": "echo hi"}, "result": null, "error": null, "status": "in_progress"}}
{"type": "item.completed", "item": {"id": "item_1", "type": "mcp_tool_call", "server": "expo", "tool": "sandbox_exec", "arguments": {"command": "echo hi"}, "result": null, "error": {"message": "MCP tool call requires approval, but approval policy is never"}, "status": "failed"}}
{"type": "item.completed", "item": {"id": "item_2", "type": "agent_message", "text": "1. Called `sandbox_exec` on the `expo` MCP server with command `echo hi`. Actual result:\n ```text\n MCP tool call requires approval, but approval policy is never\n ```\n The command did not run.\n\n2. Used `apply_patch` to create `probe.txt` with the single line `hello`. Actual result:\n ```text\n Script error:\n patch rejected: writing is blocked by read-only sandbox; rejected by user approval settings\n ```\n The file was not created.\n\n3. All callable tool names (being callable does not mean the current permissions allow running them):\n\n Direct tools:\n - `functions.exec`\n - `functions.wait`\n - `functions.request_user_input`\n - `functions.request_user_input_async`\n - `clock.sleep`\n\n Nested tools inside `functions.exec`:\n - `apply_patch`\n - `clock__curr_time`\n - `create_goal`\n - `get_goal`\n - `list_mcp_resource_templates`\n - `list_mcp_resources`\n - `mcp__expo__sandbox_exec`\n - `mcp__expo__sandbox_list`\n - `read_mcp_resource`\n - `update_goal`"}}
{"type": "turn.completed", "usage": {"input_tokens": 48856, "cached_input_tokens": 28416, "cache_write_input_tokens": 0, "output_tokens": 518, "reasoning_output_tokens": 0}}
@@ -0,0 +1,9 @@
{"type": "thread.started", "thread_id": "01a10000-0000-7000-8000-000000000001"}
{"type": "turn.started"}
{"type": "item.completed", "item": {"id": "item_0", "type": "agent_message", "text": "I will run these three operations in order and report the exact result of each.\n"}}
{"type": "item.started", "item": {"id": "item_1", "type": "mcp_tool_call", "server": "expo", "tool": "sandbox_exec", "arguments": {"command": "echo hi"}, "result": null, "error": null, "status": "in_progress"}}
{"type": "item.completed", "item": {"id": "item_1", "type": "mcp_tool_call", "server": "expo", "tool": "sandbox_exec", "arguments": {"command": "echo hi"}, "result": {"content": [{"type": "text", "text": "stub ran: echo hi"}], "structured_content": null}, "error": null, "status": "completed"}}
{"type": "item.started", "item": {"id": "item_2", "type": "mcp_tool_call", "server": "expo", "tool": "sandbox_list", "arguments": {}, "result": null, "error": null, "status": "in_progress"}}
{"type": "item.completed", "item": {"id": "item_2", "type": "mcp_tool_call", "server": "expo", "tool": "sandbox_list", "arguments": {}, "result": {"content": [{"type": "text", "text": "no sandboxes"}], "structured_content": null}, "error": null, "status": "completed"}}
{"type": "item.completed", "item": {"id": "item_3", "type": "agent_message", "text": "The exact results, in order:\n\n1. `expo sandbox_exec` with command `echo hi`:\n ```json\n {\"content\":[{\"type\":\"text\",\"text\":\"stub ran: echo hi\"}],\"isError\":false}\n ```\n\n2. `expo sandbox_list`:\n ```json\n {\"content\":[{\"type\":\"text\",\"text\":\"no sandboxes\"}],\"isError\":false}\n ```\n\n3. `apply_patch` to create `probe.txt` with the single line `hello` failed:\n ```text\n patch rejected: writing is blocked by read-only sandbox; rejected by user approval settings\n ```\n The file was not created."}}
{"type": "turn.completed", "usage": {"input_tokens": 49143, "cached_input_tokens": 28672, "cache_write_input_tokens": 0, "output_tokens": 354, "reasoning_output_tokens": 0}}
@@ -0,0 +1,11 @@
import path from 'node:path';
const realFs = jest.requireActual<typeof import('node:fs')>('node:fs');
/** The lines of a capture of an agent's stdout; ids and local details replaced, text in English. */
export function readFixtureLines(name: string): string[] {
return realFs
.readFileSync(path.join(__dirname, 'fixtures', name), 'utf8')
.split('\n')
.filter(line => line !== '');
}
@@ -0,0 +1,70 @@
import { SystemError } from '@expo/eas-build-job';
import { z } from 'zod';
import { type AgentRunProviderCredentials } from '../agentRunLease';
export const MCP_SERVER_NAME = 'expo';
export interface McpServer {
url: string;
headers: Record<string, string>;
}
export interface AgentInvocation {
args: string[];
env: Record<string, string>;
stdin: string;
secrets: string[];
}
export interface AgentLogMessage {
level: 'info' | 'warn' | 'error';
message: string;
}
export interface AgentCli {
packageSpec: string;
bin: string;
formatOutputLine(line: string): AgentLogMessage[];
}
export function parseJsonLine(line: string): unknown {
try {
return JSON.parse(line);
} catch {
return undefined;
}
}
export function tolerantArray<Element extends z.ZodType>(element: Element) {
// `.catch(undefined)` turns an element the schema rejects into `undefined`.
return z
.array(element.optional().catch(undefined))
.transform(elements =>
elements.filter((parsed): parsed is z.output<Element> => parsed !== undefined)
);
}
/** The text blocks of a content list, as both the Anthropic API and MCP shape them. */
export const ContentBlocksSchema = tolerantArray(
z.object({ type: z.literal('text'), text: z.string() })
);
export function contentBlocksText(blocks: z.output<typeof ContentBlocksSchema>): string {
return blocks.map(block => block.text).join('\n');
}
// Repeats the task because Codex's `resume --last` starts a new session when none was saved,
// and that session would otherwise have no task.
export function createContinuationPrompt(prompt: string): string {
return `Your previous run of this task stopped before it was finished. Continue it from where it stopped; do not start over. The task was:\n\n${prompt}`;
}
export function createProviderMismatchError(
cliName: string,
credentials: AgentRunProviderCredentials
): SystemError {
return new SystemError(
`The agent run uses ${cliName}, but Expo issued ${credentials.provider} credentials for it. This is a problem on our side; contact Expo support.`
);
}
@@ -0,0 +1,160 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import { z } from 'zod';
import {
type AgentCli,
type AgentInvocation,
type AgentLogMessage,
ContentBlocksSchema,
MCP_SERVER_NAME,
type McpServer,
contentBlocksText,
createContinuationPrompt,
createProviderMismatchError,
parseJsonLine,
tolerantArray,
} from './agent';
import { type AgentRunProviderCredentials } from '../agentRunLease';
// The flags and the output format below are only known to hold for this version.
export const CLAUDE_CODE_CLI: AgentCli = {
packageSpec: '@anthropic-ai/[email protected]',
bin: 'claude',
formatOutputLine: formatClaudeCodeOutputLine,
};
export async function prepareClaudeCodeAsync({
homeDirectory,
minimalEnv,
mcpServer,
prompt,
credentials,
isResuming,
}: {
homeDirectory: string;
minimalEnv: Record<string, string>;
mcpServer: McpServer;
prompt: string;
credentials: AgentRunProviderCredentials;
isResuming: boolean;
}): Promise<AgentInvocation> {
if (credentials.provider !== 'anthropic') {
throw createProviderMismatchError('Claude Code', credentials);
}
const mcpConfigPath = path.join(homeDirectory, 'mcp.json');
await fs.writeFile(
mcpConfigPath,
JSON.stringify({ mcpServers: { [MCP_SERVER_NAME]: { type: 'http', ...mcpServer } } }),
{ mode: 0o600 }
);
return {
args: [
'-p',
...(isResuming ? ['--continue'] : []),
// Print mode requires `--verbose` for `stream-json`.
...['--output-format', 'stream-json', '--verbose'],
// No built-in tools. Print mode denies any tool without an allow rule, so the MCP server
// gets one.
...['--tools', '', '--allowedTools', `mcp__${MCP_SERVER_NAME}`],
...['--mcp-config', mcpConfigPath, '--strict-mcp-config'],
],
env: {
...minimalEnv,
CLAUDE_CODE_OAUTH_TOKEN: credentials.accessToken,
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: '1',
},
stdin: isResuming ? createContinuationPrompt(prompt) : prompt,
secrets: [credentials.accessToken],
};
}
const AssistantBlockSchema = z.discriminatedUnion('type', [
z.object({ type: z.literal('text'), text: z.string() }),
z.object({
type: z.literal('tool_use'),
name: z.string(),
input: z.record(z.string(), z.unknown()),
}),
]);
const ToolResultBlockSchema = z.object({
type: z.literal('tool_result'),
content: z.union([z.string(), ContentBlocksSchema]).default(''),
is_error: z.boolean().optional(),
});
const ClaudeCodeEventSchema = z.discriminatedUnion('type', [
z.object({
type: z.literal('system'),
subtype: z.literal('init'),
mcp_servers: tolerantArray(z.object({ name: z.string(), status: z.string() })),
}),
z.object({
type: z.literal('assistant'),
message: z.object({ content: tolerantArray(AssistantBlockSchema) }),
}),
z.object({
type: z.literal('user'),
message: z.object({ content: tolerantArray(ToolResultBlockSchema) }),
}),
z.object({
type: z.literal('result'),
subtype: z.string(),
is_error: z.boolean(),
result: z.string().optional(),
errors: z.array(z.string()).optional(),
}),
]);
function formatClaudeCodeOutputLine(line: string): AgentLogMessage[] {
const json = parseJsonLine(line);
if (json === undefined) {
return [{ level: 'info', message: line }];
}
const event = ClaudeCodeEventSchema.safeParse(json);
if (!event.success) {
return [];
}
switch (event.data.type) {
case 'system':
return event.data.mcp_servers.map(server =>
server.status === 'connected'
? { level: 'info', message: `MCP server ${server.name} is connected.` }
: {
level: 'warn',
message: `MCP server ${server.name} is not connected (${server.status}).`,
}
);
case 'assistant':
return event.data.message.content.map(block => {
switch (block.type) {
case 'text':
return { level: 'info', message: `Agent: ${block.text}` };
case 'tool_use':
return {
level: 'info',
message: `Tool call: ${block.name} ${JSON.stringify(block.input)}`,
};
}
});
case 'user':
return event.data.message.content.map(block => {
const text =
typeof block.content === 'string' ? block.content : contentBlocksText(block.content);
return block.is_error
? { level: 'warn', message: `Tool error: ${text}` }
: { level: 'info', message: `Tool result: ${text}` };
});
case 'result': {
// `result` repeats the agent's last text block, so on success there is nothing to add.
const { is_error, result, errors, subtype } = event.data;
return is_error
? [
{
level: 'error',
message: `Agent failed: ${result || errors?.join('\n') || subtype}`,
},
]
: [];
}
}
}
@@ -0,0 +1,185 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import { z } from 'zod';
import {
type AgentCli,
type AgentInvocation,
type AgentLogMessage,
ContentBlocksSchema,
MCP_SERVER_NAME,
type McpServer,
contentBlocksText,
createContinuationPrompt,
createProviderMismatchError,
parseJsonLine,
} from './agent';
import { type AgentRunProviderCredentials } from '../agentRunLease';
// The flags, settings and output format below are only known to hold for this version.
export const CODEX_CLI: AgentCli = {
packageSpec: '@openai/[email protected]',
bin: 'codex',
formatOutputLine: formatCodexOutputLine,
};
export async function prepareCodexAsync({
homeDirectory,
minimalEnv,
mcpServer,
prompt,
credentials,
isResuming,
}: {
homeDirectory: string;
minimalEnv: Record<string, string>;
mcpServer: McpServer;
prompt: string;
credentials: AgentRunProviderCredentials;
isResuming: boolean;
}): Promise<AgentInvocation> {
if (credentials.provider !== 'openai') {
throw createProviderMismatchError('Codex', credentials);
}
const codexHomeDirectory = path.join(homeDirectory, '.codex');
await fs.mkdir(codexHomeDirectory, { recursive: true });
await fs.writeFile(
path.join(codexHomeDirectory, 'config.toml'),
[
'approval_policy = "never"',
// Codex always offers `apply_patch`.
'sandbox_mode = "read-only"',
'web_search = "disabled"',
'check_for_update_on_startup = false',
'',
'[features]',
'shell_tool = false',
'view_image = false',
'image_generation = false',
// Apps and plugins installed on the credential owner's ChatGPT account.
'apps = false',
'plugins = false',
'',
// A subagent's session could otherwise be the one `resume --last` picks.
'[agents]',
'enabled = false',
'',
'[analytics]',
'enabled = false',
'',
`[mcp_servers.${MCP_SERVER_NAME}]`,
// JSON strings are valid TOML basic strings.
`url = ${JSON.stringify(mcpServer.url)}`,
// `approval_policy` would otherwise refuse the server's tools that are not read-only.
'default_tools_approval_mode = "approve"',
'',
`[mcp_servers.${MCP_SERVER_NAME}.http_headers]`,
...Object.entries(mcpServer.headers).map(
([name, value]) => `${JSON.stringify(name)} = ${JSON.stringify(value)}`
),
'',
].join('\n'),
{ mode: 0o600 }
);
await fs.writeFile(
path.join(codexHomeDirectory, 'auth.json'),
JSON.stringify({
auth_mode: 'chatgptAuthTokens',
tokens: {
id_token: credentials.idToken,
access_token: credentials.accessToken,
// www keeps the refresh token.
refresh_token: '',
account_id: credentials.accountId,
},
// Codex sends no bearer token unless this is set.
last_refresh: new Date().toISOString(),
}),
{ mode: 0o600 }
);
return {
args: [
'exec',
// Without it Codex skips settings it does not know, leaving on whatever they turn off.
'--strict-config',
...['--json', '--skip-git-repo-check'],
...(isResuming ? ['resume', '--last', '-'] : []),
],
env: minimalEnv,
stdin: isResuming ? createContinuationPrompt(prompt) : prompt,
secrets: [credentials.accessToken, credentials.idToken],
};
}
const CodexItemSchema = z.discriminatedUnion('type', [
z.object({ type: z.literal('agent_message'), text: z.string() }),
z.object({
type: z.literal('mcp_tool_call'),
server: z.string(),
tool: z.string(),
arguments: z.record(z.string(), z.unknown()).nullable(),
result: z.object({ content: ContentBlocksSchema }).nullable(),
error: z.object({ message: z.string() }).nullable(),
status: z.string(),
}),
z.object({ type: z.literal('error'), message: z.string() }),
]);
const CodexEventSchema = z.discriminatedUnion('type', [
z.object({ type: z.literal('item.started'), item: CodexItemSchema.optional().catch(undefined) }),
z.object({
type: z.literal('item.completed'),
item: CodexItemSchema.optional().catch(undefined),
}),
z.object({ type: z.literal('error'), message: z.string() }),
z.object({ type: z.literal('turn.failed'), error: z.object({ message: z.string() }) }),
]);
function formatCodexOutputLine(line: string): AgentLogMessage[] {
const json = parseJsonLine(line);
if (json === undefined) {
return [{ level: 'info', message: line }];
}
const event = CodexEventSchema.safeParse(json);
if (!event.success) {
return [];
}
switch (event.data.type) {
case 'item.started': {
const item = event.data.item;
if (item?.type !== 'mcp_tool_call') {
return [];
}
return [
{
level: 'info',
message: `Tool call: ${item.server}.${item.tool} ${JSON.stringify(item.arguments ?? {})}`,
},
];
}
case 'item.completed':
return formatCompletedItem(event.data.item);
case 'error':
return [{ level: 'warn', message: `Error: ${event.data.message}` }];
case 'turn.failed':
return [{ level: 'error', message: `Agent failed: ${event.data.error.message}` }];
}
}
function formatCompletedItem(
item: z.output<typeof CodexItemSchema> | undefined
): AgentLogMessage[] {
switch (item?.type) {
case 'agent_message':
return [{ level: 'info', message: `Agent: ${item.text}` }];
case 'mcp_tool_call': {
const resultText = contentBlocksText(item.result?.content ?? []);
return item.status === 'failed'
? [{ level: 'warn', message: `Tool error: ${item.error?.message ?? resultText}` }]
: [{ level: 'info', message: `Tool result: ${resultText}` }];
}
case 'error':
return [{ level: 'warn', message: `Error: ${item.message}` }];
case undefined:
return [];
}
}
@@ -0,0 +1,134 @@
import { SystemError } from '@expo/eas-build-job';
import { asyncResult } from '@expo/results';
import fetch, { Response } from 'node-fetch';
import { z } from 'zod';
const PostApi = {
'/token': {
request: z.object({
grant_type: z.literal('urn:ietf:params:oauth:grant-type:jwt-bearer'),
assertion: z.string().min(1),
}),
response: z.object({
access_token: z.string().min(1),
expires_in: z.number().finite().gt(60),
token_type: z.string().refine(value => value.toLowerCase() === 'bearer'),
}),
},
};
const OAuthErrorZ = z.enum([
'invalid_request',
'invalid_client',
'invalid_grant',
'unauthorized_client',
'unsupported_grant_type',
'invalid_scope',
'server_error',
'temporarily_unavailable',
]);
export class GooglePlayAuthRequestError extends Error {
constructor(
public readonly status: number,
public readonly errorCode?: z.output<typeof OAuthErrorZ>
) {
super(`Google Play OAuth request failed (HTTP ${status})${errorCode ? `: ${errorCode}` : ''}.`);
}
}
export class GooglePlayAuthClient {
private readonly baseUrl = 'https://oauth2.googleapis.com';
async postAsync<TPath extends keyof typeof PostApi>(
path: TPath,
body: z.input<(typeof PostApi)[TPath]['request']>,
signal?: AbortSignal
): Promise<z.output<(typeof PostApi)[TPath]['response']>> {
const schema = PostApi[path];
return await this.requestAsync({
method: 'POST',
path,
body,
requestSchema: schema.request,
responseSchema: schema.response,
signal,
});
}
private async requestAsync({
method,
path,
body,
requestSchema,
responseSchema,
signal,
}: {
method: 'POST';
path: string;
body: unknown;
requestSchema: z.ZodType<any>;
responseSchema: z.ZodType<any>;
signal?: AbortSignal;
}): Promise<any> {
const parsedBody = await asyncResult((async () => requestSchema.parse(body))());
if (!parsedBody.ok) {
throw new Error(
`Malformed request to Google Play OAuth: ${z.prettifyError(
parsedBody.enforceError() as z.ZodError
)}`
);
}
signal?.throwIfAborted();
let response: Response;
try {
response = await fetch(new URL(path, this.baseUrl).toString(), {
method,
redirect: 'manual',
signal,
body: new URLSearchParams(parsedBody.value),
});
} catch (cause) {
signal?.throwIfAborted();
throw new SystemError('Google Play OAuth request failed before a response was received.', {
cause,
});
}
let text: string;
try {
text = await response.text();
} catch (cause) {
signal?.throwIfAborted();
throw new SystemError(
`Could not read the Google Play OAuth response (HTTP ${response.status}).`,
{
cause,
}
);
}
const parsedJson = await asyncResult((async () => JSON.parse(text))());
if (!response.ok) {
const errorCode = OAuthErrorZ.safeParse(parsedJson.ok ? parsedJson.value?.error : undefined);
throw new GooglePlayAuthRequestError(
response.status,
errorCode.success ? errorCode.data : undefined
);
}
if (!parsedJson.ok) {
throw new SystemError(
`Malformed JSON response from Google Play OAuth (HTTP ${response.status}).`
);
}
const parsedResponse = await asyncResult(
(async () => responseSchema.parse(parsedJson.value))()
);
if (!parsedResponse.ok) {
throw new SystemError(
`Malformed response from Google Play OAuth (HTTP ${response.status}): ${z.prettifyError(
parsedResponse.enforceError() as z.ZodError
)}`
);
}
return parsedResponse.value;
}
}
@@ -0,0 +1,38 @@
import * as jose from 'jose';
import { KeyObject } from 'node:crypto';
import { GooglePlayAuthClient } from './GooglePlayAuthClient';
export type GoogleServiceAccount = {
client_email: string;
private_key: KeyObject;
private_key_id?: string;
};
export namespace GooglePlayAuthUtils {
async function createAssertionAsync(serviceAccount: GoogleServiceAccount): Promise<string> {
return await new jose.SignJWT({
scope: 'https://www.googleapis.com/auth/androidpublisher',
})
.setProtectedHeader({ alg: 'RS256', typ: 'JWT', kid: serviceAccount.private_key_id })
.setIssuer(serviceAccount.client_email)
.setAudience('https://oauth2.googleapis.com/token')
.setIssuedAt()
.setExpirationTime('1h')
.sign(serviceAccount.private_key);
}
export async function createTokenAsync(
serviceAccount: GoogleServiceAccount,
signal?: AbortSignal
): Promise<{ value: string; expiresAt: number }> {
const startedAt = Date.now();
const assertion = await createAssertionAsync(serviceAccount);
const response = await new GooglePlayAuthClient().postAsync(
'/token',
{ grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer', assertion },
signal
);
return { value: response.access_token, expiresAt: startedAt + response.expires_in * 1000 };
}
}
@@ -0,0 +1,285 @@
import { SystemError } from '@expo/eas-build-job';
import fetch, { Response } from 'node-fetch';
import { z } from 'zod';
import { GooglePlayAuthUtils, GoogleServiceAccount } from './GooglePlayAuthUtils';
export class GooglePlayApiError extends Error {
constructor(
readonly status: number,
readonly apiMessage: string,
readonly reasons: string[]
) {
super(`Google Play request failed (HTTP ${status})${apiMessage ? `: ${apiMessage}` : '.'}`);
}
}
const ReleaseStatusZ = z.enum(['draft', 'inProgress', 'halted', 'completed']);
const ReleaseNoteZ = z.object({
language: z.string(),
text: z.string(),
});
const ReleaseZ = z.object({
name: z.string().optional(),
versionCodes: z.array(z.string()),
status: ReleaseStatusZ,
userFraction: z.number().gt(0).lt(1).optional(),
releaseNotes: z.array(ReleaseNoteZ).optional(),
});
const GetApi = {
'/androidpublisher/v3/applications/:packageName/edits/:editId': {
path: z.object({ packageName: z.string().min(1), editId: z.string().min(1) }),
request: z.object({}),
query: z.object({}),
response: z.object({ id: z.string().min(1), expiryTimeSeconds: z.string().optional() }),
},
'/androidpublisher/v3/applications/:packageName/edits/:editId/tracks/:track': {
path: z.object({
packageName: z.string().min(1),
editId: z.string().min(1),
track: z.string().min(1),
}),
request: z.object({}),
query: z.object({}),
response: z.object({
track: z.string().min(1),
releases: z.array(ReleaseZ).optional(),
}),
},
};
const PostApi = {
'/androidpublisher/v3/applications/:packageName/edits': {
path: z.object({ packageName: z.string().min(1) }),
request: z.object({}),
query: z.object({}),
response: z.object({ id: z.string().min(1), expiryTimeSeconds: z.string().optional() }),
},
'/androidpublisher/v3/applications/:packageName/edits/:editId:commit': {
path: z.object({ packageName: z.string().min(1), editId: z.string().min(1) }),
request: z.object({}),
query: z.object({ changesNotSentForReview: z.boolean().optional() }),
response: z.object({ id: z.string().min(1), expiryTimeSeconds: z.string().optional() }),
},
'/androidpublisher/v3/applications/:packageName/edits/:editId:validate': {
path: z.object({ packageName: z.string().min(1), editId: z.string().min(1) }),
request: z.object({}),
query: z.object({}),
response: z.object({ id: z.string().min(1), expiryTimeSeconds: z.string().optional() }),
},
};
const PutApi = {
'/androidpublisher/v3/applications/:packageName/edits/:editId/tracks/:track': {
path: z.object({
packageName: z.string().min(1),
editId: z.string().min(1),
track: z.string().min(1),
}),
request: z.object({
track: z.string().min(1),
releases: z.array(ReleaseZ).optional(),
}),
query: z.object({}),
response: z.object({
track: z.string().min(1),
releases: z.array(ReleaseZ).optional(),
}),
},
};
const DeleteApi = {
'/androidpublisher/v3/applications/:packageName/edits/:editId': {
path: z.object({ packageName: z.string().min(1), editId: z.string().min(1) }),
},
};
export class GooglePlayClient {
private readonly baseUrl = 'https://androidpublisher.googleapis.com';
private cachedToken?: Promise<{ value: string; expiresAt: number }>;
constructor(private readonly serviceAccount: GoogleServiceAccount) {}
private async getTokenAsync(signal?: AbortSignal): Promise<string> {
signal?.throwIfAborted();
while (this.cachedToken) {
const pending = this.cachedToken;
const cached = await pending;
signal?.throwIfAborted();
if (Date.now() < cached.expiresAt - 60_000) {
return cached.value;
}
if (this.cachedToken === pending) {
break;
}
}
const pending = GooglePlayAuthUtils.createTokenAsync(this.serviceAccount, signal);
this.cachedToken = pending;
try {
return (await pending).value;
} catch (error) {
if (this.cachedToken === pending) {
this.cachedToken = undefined;
}
throw error;
}
}
async getAsync<TPath extends keyof typeof GetApi>(
path: TPath,
params: z.input<(typeof GetApi)[TPath]['path']>,
signal?: AbortSignal
): Promise<z.output<(typeof GetApi)[TPath]['response']>> {
return await this.sendJsonRequestAsync('GET', path, GetApi[path], {}, params, {}, signal);
}
async postAsync<TPath extends keyof typeof PostApi>(
path: TPath,
body: z.input<(typeof PostApi)[TPath]['request']>,
params: z.input<(typeof PostApi)[TPath]['path']>,
options: { query?: z.input<(typeof PostApi)[TPath]['query']>; signal?: AbortSignal } = {}
): Promise<z.output<(typeof PostApi)[TPath]['response']>> {
return await this.sendJsonRequestAsync(
'POST',
path,
PostApi[path],
body,
params,
options.query ?? {},
options.signal
);
}
async putAsync<TPath extends keyof typeof PutApi>(
path: TPath,
body: z.input<(typeof PutApi)[TPath]['request']>,
params: z.input<(typeof PutApi)[TPath]['path']>,
signal?: AbortSignal
): Promise<z.output<(typeof PutApi)[TPath]['response']>> {
return await this.sendJsonRequestAsync('PUT', path, PutApi[path], body, params, {}, signal);
}
async deleteAsync<TPath extends keyof typeof DeleteApi>(
path: TPath,
params: z.input<(typeof DeleteApi)[TPath]['path']>,
signal?: AbortSignal
): Promise<void> {
let url: string = path;
for (const [key, value] of Object.entries(DeleteApi[path].path.parse(params))) {
url = url.replace(`:${key}`, encodeURIComponent(value));
}
await this.requestAsync('DELETE', url, undefined, signal);
}
private async sendJsonRequestAsync(
method: 'GET' | 'POST' | 'PUT' | 'DELETE',
path: string,
schema: {
path: z.ZodType<Record<string, string>>;
request: z.ZodType;
query: z.ZodType;
response: z.ZodType;
},
body: unknown,
params: unknown,
query: unknown,
signal?: AbortSignal
): Promise<any> {
const parsedBody = schema.request.parse(body);
for (const [key, value] of Object.entries(schema.path.parse(params))) {
path = path.replace(`:${key}`, encodeURIComponent(value));
}
const search = new URLSearchParams();
for (const [key, value] of Object.entries(
schema.query.parse(query) as Record<string, unknown>
)) {
if (value !== undefined) {
search.set(key, String(value));
}
}
const response = await this.requestAsync(
method,
search.size ? `${path}?${search}` : path,
method === 'GET' ? undefined : JSON.stringify(parsedBody),
signal
);
let data: unknown;
try {
data = await response.json();
} catch {
signal?.throwIfAborted();
throw new SystemError(
`Could not read the Google Play JSON response (HTTP ${response.status}).`
);
}
const parsed = schema.response.safeParse(data);
if (!parsed.success) {
throw new SystemError(
`Malformed response from Google Play (HTTP ${response.status}): ${z.prettifyError(parsed.error)}`
);
}
return parsed.data;
}
private async requestAsync(
method: 'GET' | 'POST' | 'PUT' | 'DELETE',
path: string,
body?: string,
signal?: AbortSignal
): Promise<Response> {
const url = new URL(path, this.baseUrl);
const token = await this.getTokenAsync(signal);
let response: Response;
signal?.throwIfAborted();
try {
response = await fetch(url.toString(), {
method,
body,
signal,
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
redirect: 'manual',
});
} catch {
signal?.throwIfAborted();
throw new SystemError('Google Play request failed before a response was received.');
}
if (!response.ok) {
let data: unknown;
try {
data = await response.json();
} catch {
signal?.throwIfAborted();
}
const parsed = z
.object({
error: z.object({
message: z.string().optional(),
errors: z.array(z.object({ reason: z.string().optional() })).optional(),
}),
})
.safeParse(data);
throw new GooglePlayApiError(
response.status,
parsed.success ? (parsed.data.error.message ?? '') : '',
parsed.success
? (parsed.data.error.errors?.flatMap(error => (error.reason ? [error.reason] : [])) ?? [])
: []
);
}
return response;
}
}
@@ -0,0 +1,111 @@
import { SystemError } from '@expo/eas-build-job';
import nock from 'nock';
import { inspect } from 'node:util';
import { GooglePlayAuthClient, GooglePlayAuthRequestError } from '../GooglePlayAuthClient';
jest.unmock('node-fetch');
const request = {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer' as const,
assertion: 'caller-signed-assertion',
};
let client: GooglePlayAuthClient;
beforeEach(() => {
nock.disableNetConnect();
client = new GooglePlayAuthClient();
});
afterEach(() => {
const pending = nock.pendingMocks();
nock.cleanAll();
nock.enableNetConnect();
expect(pending).toEqual([]);
});
it('posts the caller request and returns a validated response on each call', async () => {
nock('https://oauth2.googleapis.com')
.post('/token', request)
.reply(200, { access_token: 'first-token', expires_in: 3600, token_type: 'Bearer' })
.post('/token', request)
.reply(200, { access_token: 'second-token', expires_in: 1800, token_type: 'Bearer' });
await expect(client.postAsync('/token', request)).resolves.toEqual({
access_token: 'first-token',
expires_in: 3600,
token_type: 'Bearer',
});
await expect(client.postAsync('/token', request)).resolves.toEqual({
access_token: 'second-token',
expires_in: 1800,
token_type: 'Bearer',
});
});
it.each([
{ access_token: '', expires_in: 3600, token_type: 'Bearer' },
{ access_token: 'example-token', expires_in: 60, token_type: 'Bearer' },
{ access_token: 'example-token', expires_in: '3600', token_type: 'Bearer' },
{ access_token: 'example-token', expires_in: 3600, token_type: 1 },
{ access_token: 'example-token', expires_in: 3600, token_type: 'Basic' },
])(
'includes validation details without token data for invalid OAuth responses (%j)',
async body => {
nock('https://oauth2.googleapis.com').post('/token').reply(200, body);
const result = client.postAsync('/token', request);
await expect(result).rejects.toThrow('Malformed response from Google Play OAuth (HTTP 200):');
try {
await result;
} catch (error) {
expect(inspect(error, { depth: null })).not.toContain('example-token');
expect((error as Error).message).toMatch(/access_token|expires_in|token_type/);
}
}
);
it.each([
[{ error: 'invalid_grant', error_description: 'example-token' }, 'invalid_grant'],
[{ error: 'example-token' }, undefined],
['<html>example-token</html>', undefined],
])(
'keeps HTTP status and known OAuth codes without retaining response data (%j)',
async (body, errorCode) => {
nock('https://oauth2.googleapis.com').post('/token').reply(400, body);
let error: unknown;
try {
await client.postAsync('/token', request);
} catch (caught) {
error = caught;
}
expect(error).toBeInstanceOf(GooglePlayAuthRequestError);
expect(error).toMatchObject({ status: 400, errorCode });
expect((error as Error).message).toContain('HTTP 400');
if (errorCode) {
expect((error as Error).message).toContain(errorCode);
}
expect(inspect(error, { depth: null })).not.toContain('example-token');
}
);
it.each(['network failure', 'malformed JSON'])('returns a system error for %s', async failure => {
const endpoint = nock('https://oauth2.googleapis.com').post('/token');
if (failure === 'network failure') {
endpoint.replyWithError('connection failed');
} else {
endpoint.reply(200, 'example-token is not JSON');
}
const result = client.postAsync('/token', request);
await expect(result).rejects.toBeInstanceOf(SystemError);
if (failure === 'network failure') {
await expect(result).rejects.toMatchObject({
cause: { message: expect.stringContaining('connection failed') },
});
} else {
await expect(result).rejects.toThrow(
'Malformed JSON response from Google Play OAuth (HTTP 200).'
);
try {
await result;
} catch (error) {
expect(inspect(error, { depth: null })).not.toContain('example-token');
}
}
});
@@ -0,0 +1,158 @@
import nock from 'nock';
import { generateKeyPairSync } from 'node:crypto';
import { SystemError } from '@expo/eas-build-job';
import { ZodError } from 'zod';
import { GooglePlayClient } from '../GooglePlayClient';
const packageName = 'dev.expo.submitfixture';
const editPath = `/androidpublisher/v3/applications/${packageName}/edits/edit`;
const api = (): nock.Scope =>
nock('https://androidpublisher.googleapis.com', {
reqheaders: { authorization: 'Bearer test-access-token' },
});
jest.unmock('node-fetch');
const serviceAccount = {
client_email: '[email protected]',
private_key: generateKeyPairSync('rsa', { modulusLength: 2048 }).privateKey,
};
let client: GooglePlayClient;
beforeEach(() => {
nock.disableNetConnect();
client = new GooglePlayClient(serviceAccount);
nock('https://oauth2.googleapis.com')
.post('/token')
.optionally()
.reply(200, { access_token: 'test-access-token', expires_in: 3600, token_type: 'Bearer' });
});
afterEach(() => {
const pending = nock.pendingMocks();
nock.cleanAll();
nock.enableNetConnect();
jest.restoreAllMocks();
expect(pending).toEqual([]);
});
it('creates an edit and validates the response', async () => {
api().post(`/androidpublisher/v3/applications/${packageName}/edits`, {}).reply(200, {
id: 'edit',
expiryTimeSeconds: '1234567890',
});
await expect(
client.postAsync(
'/androidpublisher/v3/applications/:packageName/edits',
{},
{
packageName,
}
)
).resolves.toEqual({ id: 'edit', expiryTimeSeconds: '1234567890' });
});
it('encodes track path parameters and validates track responses', async () => {
api().get(`${editPath}/tracks/wear%3Aproduction`).reply(200, { track: 'wear:production' });
await expect(
client.getAsync('/androidpublisher/v3/applications/:packageName/edits/:editId/tracks/:track', {
packageName,
editId: 'edit',
track: 'wear:production',
})
).resolves.toEqual({ track: 'wear:production' });
});
it('rejects invalid rollout requests before sending them', async () => {
await expect(
client.putAsync(
'/androidpublisher/v3/applications/:packageName/edits/:editId/tracks/:track',
{
track: 'production',
releases: [{ versionCodes: ['123'], status: 'inProgress', userFraction: 1 }],
},
{ packageName, editId: 'edit', track: 'production' }
)
).rejects.toBeInstanceOf(ZodError);
});
it('does not retry an ambiguous edit commit', async () => {
api()
.post(`${editPath}:commit`, {})
.query({ changesNotSentForReview: false })
.replyWithError('Lost response');
await expect(
client.postAsync(
'/androidpublisher/v3/applications/:packageName/edits/:editId:commit',
{},
{ packageName, editId: 'edit' },
{ query: { changesNotSentForReview: false } }
)
).rejects.toBeInstanceOf(SystemError);
});
it('deletes an edit with an empty response', async () => {
api().delete(editPath).reply(204);
await expect(
client.deleteAsync('/androidpublisher/v3/applications/:packageName/edits/:editId', {
packageName,
editId: 'edit',
})
).resolves.toBeUndefined();
});
it('rejects malformed success responses without exposing their contents', async () => {
api()
.get(editPath)
.reply(200, { id: 'edit', expiryTimeSeconds: { secret: 'PRIVATE' } });
await expect(
client.getAsync('/androidpublisher/v3/applications/:packageName/edits/:editId', {
packageName,
editId: 'edit',
})
).rejects.toThrow('Malformed response from Google Play (HTTP 200)');
});
it('retains structured API errors', async () => {
api()
.get(editPath)
.reply(403, { error: { message: 'Denied', errors: [{ reason: 'forbidden' }] } });
await expect(
client.getAsync('/androidpublisher/v3/applications/:packageName/edits/:editId', {
packageName,
editId: 'edit',
})
).rejects.toMatchObject({ status: 403, apiMessage: 'Denied', reasons: ['forbidden'] });
});
it.each(['body', 'query'])('accepts extra fields in an empty %s object', async field => {
api()
.post(`/androidpublisher/v3/applications/${packageName}/edits`, {})
.reply(200, { id: 'edit' });
await expect(
client.postAsync(
'/androidpublisher/v3/applications/:packageName/edits',
(field === 'body' ? { unexpected: true } : {}) as never,
{ packageName },
{ query: (field === 'query' ? { unexpected: true } : {}) as never }
)
).resolves.toEqual({ id: 'edit' });
});
it('shares a token across concurrent requests and refreshes before expiry', async () => {
nock.cleanAll();
const now = Date.now();
const clock = jest.spyOn(Date, 'now').mockReturnValue(now);
nock('https://oauth2.googleapis.com')
.post('/token')
.twice()
.reply(200, { access_token: 'test-access-token', expires_in: 3600, token_type: 'Bearer' });
api().get(editPath).times(4).reply(200, { id: 'edit' });
const get = () =>
client.getAsync('/androidpublisher/v3/applications/:packageName/edits/:editId', {
packageName,
editId: 'edit',
});
await Promise.all([get(), get()]);
clock.mockReturnValue(now + 3550_000);
await Promise.all([get(), get()]);
});
@@ -0,0 +1,92 @@
import { SystemError, UserError } from '@expo/eas-build-job';
import spawn from '@expo/spawn-async';
import StreamZip from 'node-stream-zip';
export type AndroidArtifactType = 'apk' | 'aab';
export async function readAndroidArtifactInfoAsync(
artifactPath: string,
signal?: AbortSignal
): Promise<{ artifactType: AndroidArtifactType; packageName: string }> {
signal?.throwIfAborted();
const artifactType = await detectAndroidArtifactTypeAsync(artifactPath);
switch (artifactType) {
case 'apk': {
const packageName = await readApkPackageNameAsync(artifactPath, signal);
return { artifactType, packageName };
}
case 'aab': {
const packageName = await readAabPackageNameAsync(artifactPath, signal);
return { artifactType, packageName };
}
}
}
async function detectAndroidArtifactTypeAsync(artifactPath: string): Promise<AndroidArtifactType> {
const zip = new StreamZip.async({ file: artifactPath });
try {
const bundleConfig = await zip.entry('BundleConfig.pb');
return bundleConfig ? 'aab' : 'apk';
} catch (error) {
throw new UserError(
'EAS_READ_ANDROID_APP_INFO_INVALID_ARTIFACT',
'Cannot read the Android artifact as a ZIP archive.',
{ cause: error }
);
} finally {
await zip.close().catch(() => {});
}
}
async function readApkPackageNameAsync(
artifactPath: string,
signal?: AbortSignal
): Promise<string> {
signal?.throwIfAborted();
try {
const { stdout } = await spawn('aapt2', ['dump', 'packagename', artifactPath], {
stdio: 'pipe',
signal,
});
return stdout.trim();
} catch (error) {
signal?.throwIfAborted();
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
throw new SystemError('aapt2 is not installed or is not available on the worker PATH.', {
cause: error,
});
}
throw new UserError(
'EAS_READ_ANDROID_APP_INFO_INVALID_MANIFEST',
'Cannot read the APK manifest with aapt2. Check the app binary.',
{ cause: error }
);
}
}
async function readAabPackageNameAsync(
artifactPath: string,
signal?: AbortSignal
): Promise<string> {
signal?.throwIfAborted();
try {
const { stdout } = await spawn(
'bundletool',
['dump', 'manifest', '--bundle', artifactPath, '--xpath', '/manifest/@package'],
{ stdio: 'pipe', signal }
);
return stdout.trim();
} catch (error) {
signal?.throwIfAborted();
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
throw new SystemError('bundletool is not installed or is not available on the worker PATH.', {
cause: error,
});
}
throw new UserError(
'EAS_READ_ANDROID_APP_INFO_INVALID_MANIFEST',
'Cannot read the AAB manifest with bundletool. Check the app binary.',
{ cause: error }
);
}
}
@@ -285,6 +285,7 @@ export const APPIUM_COMMAND_SUMMARIES: Record<AppiumCommand, string> = {
mobileGetPerformanceData: 'Read app performance data',
mobileGetPermission: 'Read an app permission',
mobileGetPermissions: 'Read app permissions',
mobileGetSimulatedHingeAngle: 'Read the simulated hinge angle',
mobileGetSimulatedLocation: 'Read the simulated location',
mobileGetSource: 'Read the screen contents',
mobileGetUiMode: 'Read the Android UI mode',
@@ -363,6 +364,7 @@ export const APPIUM_COMMAND_SUMMARIES: Record<AppiumCommand, string> = {
mobileSetIncreaseContrast: 'Changed the Increase Contrast setting',
mobileSetPasteboard: 'Wrote to the pasteboard',
mobileSetPermissions: 'Set app permissions',
mobileSetSimulatedHingeAngle: 'Set the simulated hinge angle',
mobileSetSimulatedLocation: 'Set a simulated location',
mobileSetUiMode: 'Changed the Android UI mode',
mobileShake: 'Shook the device',
@@ -184,6 +184,7 @@ export type XCUITestAppiumCommand =
| 'mobileGetIncreaseContrast'
| 'mobileGetPasteboard'
| 'mobileGetPermission'
| 'mobileGetSimulatedHingeAngle'
| 'mobileGetSimulatedLocation'
| 'mobileGetSource'
| 'mobileGetXctestScreenRecordingInfo'
@@ -200,6 +201,7 @@ export type XCUITestAppiumCommand =
| 'mobileLaunchApp'
| 'mobileListApps'
| 'mobileListCertificates'
| 'mobileListDisplays'
| 'mobileListXCTestBundles'
| 'mobilePerformAccessibilityAudit'
| 'mobilePerformHandGesture'
@@ -230,6 +232,7 @@ export type XCUITestAppiumCommand =
| 'mobileSetIncreaseContrast'
| 'mobileSetPasteboard'
| 'mobileSetPermissions'
| 'mobileSetSimulatedHingeAngle'
| 'mobileSetSimulatedLocation'
| 'mobileShake'
| 'mobileSimctl'
@@ -216,7 +216,7 @@ export async function uploadArgentArtifactAsync(
filename,
kind: getArgentArtifactKind(artifact),
size,
stream: createReadStream(temporaryArtifactPath),
openStream: () => createReadStream(temporaryArtifactPath),
});
} finally {
await rm(temporaryDirectory, { recursive: true, force: true });
@@ -0,0 +1,120 @@
import { SystemError } from '@expo/eas-build-job';
import fetch, { Headers } from 'node-fetch';
import type { Readable } from 'node:stream';
import { finished } from 'node:stream/promises';
import { setTimeout as delay } from 'node:timers/promises';
import { AbortUtils } from '../../utils/AbortUtils';
export type ArtifactUploadSession = { url: string; headers: unknown };
export type ArtifactSource = { openStream: () => Readable };
export async function putArtifactWithRetriesAsync(
{
uploadSession,
source,
signal,
}: {
uploadSession: ArtifactUploadSession;
source: ArtifactSource;
signal?: AbortSignal;
},
{ attempts = 1 }: { attempts?: number } = {}
): Promise<void> {
for (let attempt = 0; attempt < attempts; attempt++) {
signal?.throwIfAborted();
const stream = source.openStream();
// Upload time scales with file size, so the deadline bounds stalls, not the whole request.
const timeout = AbortUtils.resettableTimeout(90_000);
try {
await putArtifactAsync({
uploadSession,
stream,
signal: signal ? AbortSignal.any([signal, timeout.signal]) : timeout.signal,
onProgress: timeout.resetDeadline,
});
return;
} catch (error) {
signal?.throwIfAborted();
const retryable =
error instanceof ArtifactPutError
? [408, 429, 500, 502, 503, 504].includes(error.status)
: timeout.signal.aborted || error instanceof ArtifactPutNetworkError;
if (!retryable || attempt + 1 === attempts) {
throw error;
}
await delay(500 * (attempt + 1), undefined, { signal });
} finally {
stream.destroy();
await finished(stream).catch(() => {});
}
}
}
async function putArtifactAsync({
uploadSession,
stream,
signal,
onProgress,
}: {
uploadSession: ArtifactUploadSession;
stream: Readable;
signal: AbortSignal;
onProgress: () => void;
}): Promise<void> {
const requestController = new AbortController();
try {
const responding = fetch(uploadSession.url, {
method: 'PUT',
headers: new Headers(uploadSession.headers as Record<string, string>),
body: stream,
signal: AbortSignal.any([signal, requestController.signal]),
});
// fetch has piped the body already, so this listener observes flow without starting it.
stream.on('data', onProgress);
let response;
try {
response = await responding;
} catch (error) {
signal.throwIfAborted();
throw error instanceof Error && error.name === 'FetchError'
? new ArtifactPutNetworkError(error)
: error;
}
signal.throwIfAborted();
if (!response.ok) {
throw new ArtifactPutError(response);
}
} finally {
// node-fetch destroys the request body on abort, and its response stream is a PassThrough
// whose destruction leaves the socket open, so the request itself is aborted here.
requestController.abort();
}
}
export class ArtifactPutError extends SystemError {
readonly status: number;
readonly reason: string;
constructor(response: { status: number; statusText: string }) {
const reason = `HTTP ${response.status} ${response.statusText}`;
super(`Artifact PUT failed: ${reason}.`);
this.status = response.status;
this.reason = reason;
}
}
// node-fetch names the request URL in its message, and here that URL is signed. The message keeps
// only the error code, and the original is not kept as the cause because loggers and Sentry print causes.
export class ArtifactPutNetworkError extends SystemError {
readonly code: string | undefined;
readonly reason: string;
constructor(error: Error & { code?: unknown }) {
const code = typeof error.code === 'string' ? error.code : undefined;
const reason = code ?? 'network error';
super(`Artifact PUT failed: ${reason}.`);
this.code = code;
this.reason = reason;
}
}
@@ -1,15 +1,15 @@
import { SystemError } from '@expo/eas-build-job';
import { graphql } from 'gql.tada';
import fetch, { Headers } from 'node-fetch';
import type { Readable } from 'node:stream';
import { setTimeout as delay } from 'node:timers/promises';
import { CustomBuildContext } from '../../customBuildContext';
import { graphqlAbortContext } from '../../utils/graphqlAbort';
import {
DeviceRunSessionTimeoutError,
withDeviceRunSessionTimeoutAsync,
} from './deviceRunSessionTimeout';
ArtifactPutError,
ArtifactPutNetworkError,
type ArtifactSource,
putArtifactWithRetriesAsync,
} from './artifactPut';
import { withDeviceRunSessionTimeoutAsync } from './deviceRunSessionTimeout';
const CREATE_DEVICE_RUN_SESSION_ARTIFACT_UPLOAD_SESSION_MUTATION = graphql(`
mutation CreateDeviceRunSessionArtifactUploadSession(
@@ -37,14 +37,9 @@ type ArtifactMetadata = {
size: number;
};
type ArtifactSource = { stream: Readable; reopenStream?: () => Readable };
type ArtifactUploadSession = Awaited<
ReturnType<typeof createDeviceRunSessionArtifactUploadSessionAsync>
>;
export async function uploadDeviceRunSessionArtifactAsync(
ctx: CustomBuildContext,
options: ArtifactMetadata & ArtifactSource & { signal?: AbortSignal }
options: ArtifactMetadata & ArtifactSource & { signal?: AbortSignal; attempts?: number }
): Promise<void> {
try {
const uploadSession = await withDeviceRunSessionTimeoutAsync(
@@ -52,128 +47,21 @@ export async function uploadDeviceRunSessionArtifactAsync(
async signal => await createDeviceRunSessionArtifactUploadSessionAsync(ctx, options, signal)
);
options.signal?.throwIfAborted();
await putArtifactWithRetriesAsync({
uploadSession,
source: options,
artifactId: options.artifactId,
signal: options.signal,
});
} finally {
// One-shot callers may have opened their stream before upload-session creation failed.
options.stream.destroy();
}
}
async function putArtifactWithRetriesAsync({
uploadSession,
source,
artifactId,
signal,
}: {
uploadSession: ArtifactUploadSession;
source: ArtifactSource;
artifactId: string;
signal?: AbortSignal;
}): Promise<void> {
const attempts = source.reopenStream ? 3 : 1;
for (let attempt = 0; attempt < attempts; attempt++) {
signal?.throwIfAborted();
const stream = attempt > 0 && source.reopenStream ? source.reopenStream() : source.stream;
try {
// Upload time scales with file size, so the deadline bounds stalls, not the whole request.
await withDeviceRunSessionTimeoutAsync(
{ name: 'Artifact PUT', timeoutMs: 90_000, signal },
async (putSignal, resetDeadline) =>
await putArtifactAsync({
uploadSession,
stream,
artifactId,
signal: putSignal,
onProgress: resetDeadline,
})
);
return;
} catch (error) {
signal?.throwIfAborted();
const retryable =
error instanceof ArtifactPutError
? [408, 429, 500, 502, 503, 504].includes(error.status)
: error instanceof DeviceRunSessionTimeoutError ||
error instanceof ArtifactPutNetworkError;
if (!retryable || attempt + 1 === attempts) {
throw error;
}
await delay(500 * (attempt + 1), undefined, { signal });
} finally {
stream.destroy();
}
}
}
async function putArtifactAsync({
uploadSession,
stream,
artifactId,
signal,
onProgress,
}: {
uploadSession: ArtifactUploadSession;
stream: Readable;
artifactId: string;
signal: AbortSignal;
onProgress: () => void;
}): Promise<void> {
const requestController = new AbortController();
try {
const responding = fetch(uploadSession.url, {
method: 'PUT',
headers: new Headers(uploadSession.headers as Record<string, string>),
body: stream,
signal: AbortSignal.any([signal, requestController.signal]),
});
// fetch has piped the body already, so this listener observes flow without starting it.
stream.on('data', onProgress);
let response;
try {
response = await responding;
} catch (error) {
throw error instanceof Error && error.name === 'FetchError'
? new ArtifactPutNetworkError(artifactId, error)
: error;
}
signal.throwIfAborted();
if (!response.ok) {
throw new ArtifactPutError(artifactId, response);
}
} finally {
// node-fetch destroys the request body on abort, and its response stream is a PassThrough
// whose destruction leaves the socket open, so the request itself is aborted here.
requestController.abort();
}
}
class ArtifactPutError extends SystemError {
readonly status: number;
constructor(artifactId: string, response: { status: number; statusText: string }) {
super(
`Failed to upload device run session artifact ${artifactId}: HTTP ${response.status} ${response.statusText}.`
await putArtifactWithRetriesAsync(
{ uploadSession, source: options, signal: options.signal },
{ attempts: options.attempts }
);
this.status = response.status;
} catch (error) {
if (error instanceof ArtifactPutError || error instanceof ArtifactPutNetworkError) {
throw new DeviceRunSessionArtifactUploadError(options.artifactId, error.reason);
}
throw error;
}
}
// node-fetch names the request URL in its message, and here that URL is signed. The message keeps
// only the error code, and the original is not kept as the cause because loggers and Sentry print causes.
class ArtifactPutNetworkError extends SystemError {
readonly code: string | undefined;
constructor(artifactId: string, error: Error & { code?: unknown }) {
const code = typeof error.code === 'string' ? error.code : undefined;
super(
`Failed to upload device run session artifact ${artifactId}: ${code ?? 'network error'}.`
);
this.code = code;
class DeviceRunSessionArtifactUploadError extends SystemError {
constructor(artifactId: string, reason: string) {
super(`Failed to upload device run session artifact ${artifactId}: ${reason}.`);
}
}
@@ -0,0 +1,264 @@
import { type bunyan } from '@expo/logger';
import { asyncResult } from '@expo/results';
import { BuildRuntimePlatform, type BuildStepEnv } from '@expo/steps';
import spawn from '@expo/turtle-spawn';
import { graphql } from 'gql.tada';
import fetch from 'node-fetch';
import { createWriteStream } from 'node:fs';
import { pipeline } from 'node:stream/promises';
import { mkdtemp, readFile, rm } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { type CustomBuildContext } from '../../customBuildContext';
const PREVIEW_INTERVAL_MS = 60_000;
const PREVIEW_MAX_DIMENSION = 320;
const PREVIEW_QUALITY = 70;
const MAX_PREVIEW_SIZE_BYTES = 5 * 1024 * 1024;
const CREATE_PREVIEW_UPLOAD_SESSION_MUTATION = graphql(`
mutation CreateDeviceRunSessionPreviewUploadSession($deviceRunSessionId: ID!) {
deviceRunSession {
createPreviewUploadSession(deviceRunSessionId: $deviceRunSessionId) {
uploadSession {
url
headers
}
}
}
}
`);
/**
* node-fetch includes the request URL in its error messages, and this URL is a signed upload URL
* that is valid for the whole session. Replace such errors with one that names only the failure.
*/
async function putPreviewAsync(
uploadSession: { url: string; headers: Record<string, string> },
{ image, signal }: { image: Buffer; signal: AbortSignal }
): Promise<Awaited<ReturnType<typeof fetch>>> {
try {
return await fetch(uploadSession.url, {
method: 'PUT',
headers: uploadSession.headers,
body: image,
signal,
timeout: 30_000,
});
} catch (err) {
const { name, type, code } = err as { name?: unknown; type?: unknown; code?: unknown };
const details = Object.entries({ name, type, code })
.filter(([, value]) => typeof value === 'string' && value.length > 0)
.map(([key, value]) => `${key}: ${value}`)
.join(', ');
throw new Error(`Session preview upload request failed (${details || 'unknown error'}).`);
}
}
export function startDeviceRunSessionPreview({
ctx,
deviceRunSessionId,
captureAsync,
logger,
}: {
ctx: CustomBuildContext;
deviceRunSessionId: string;
captureAsync: (signal: AbortSignal) => Promise<Buffer>;
logger: bunyan;
}): { stopAsync: () => Promise<void> } {
const controller = new AbortController();
let timer: NodeJS.Timeout | undefined;
let uploadSession: { url: string; headers: Record<string, string> } | undefined;
let pending: Promise<void>;
const uploadAsync = async (): Promise<void> => {
const signal = AbortSignal.any([controller.signal, AbortSignal.timeout(60_000)]);
try {
const image = await captureAsync(signal);
if (controller.signal.aborted) {
return;
}
if (image.length === 0 || image.length > MAX_PREVIEW_SIZE_BYTES) {
throw new Error('Session preview is empty or exceeds the 5 MiB upload limit.');
}
if (!uploadSession) {
const result = await ctx.graphqlClient
.mutation(
CREATE_PREVIEW_UPLOAD_SESSION_MUTATION,
{ deviceRunSessionId },
{
// Keep the client's auth headers and combine cancellation after URQL sets its signal.
fetch: (url, options) =>
globalThis.fetch(url, {
...options,
signal: AbortSignal.any([signal, ...(options?.signal ? [options.signal] : [])]),
}),
}
)
.toPromise();
if (result.error) {
throw result.error;
}
const session = result.data?.deviceRunSession.createPreviewUploadSession.uploadSession;
if (!session) {
throw new Error('No session preview upload URL was returned.');
}
uploadSession = {
url: session.url,
headers: session.headers as Record<string, string>,
};
}
if (controller.signal.aborted) {
return;
}
const response = await putPreviewAsync(uploadSession, { image, signal });
if (!response.ok) {
throw new Error(`Session preview upload failed: ${response.status} ${response.statusText}`);
}
} catch (err) {
if (!controller.signal.aborted) {
logger.warn({ err }, 'Could not refresh the session preview; retrying in 60 seconds.');
}
}
};
const tick = (): void => {
pending = uploadAsync().finally(() => {
if (!controller.signal.aborted) {
timer = setTimeout(tick, PREVIEW_INTERVAL_MS);
timer.unref();
}
});
};
tick();
return {
stopAsync: async () => {
controller.abort();
clearTimeout(timer);
await pending;
},
};
}
/**
* Homebrew's FFmpeg is built without libwebp, so macOS encodes previews with `cwebp` from
* Homebrew's webp package instead. Throws when it cannot be installed.
*/
export async function ensureMacosPreviewEncoderInstalledAsync({
env,
logger,
}: {
env: BuildStepEnv;
logger: bunyan;
}): Promise<void> {
if ((await asyncResult(spawn('cwebp', ['-version'], { env, stdio: 'pipe' }))).ok) {
return;
}
logger.info('Installing webp with Homebrew for the session preview.');
await spawn('brew', ['install', 'webp'], {
env: { ...env, HOMEBREW_NO_AUTO_UPDATE: '1' },
logger,
});
}
export async function captureDeviceRunSessionPreviewAsync({
runtimePlatform,
device,
env,
signal,
}: {
runtimePlatform: BuildRuntimePlatform;
device: string;
env: BuildStepEnv;
signal: AbortSignal;
}): Promise<Buffer> {
const directory = await mkdtemp(path.join(os.tmpdir(), 'session-preview-'));
try {
const options = { env, signal, timeout: 30_000, stdio: 'pipe' as const };
const preview = path.join(directory, 'preview.webp');
if (runtimePlatform === BuildRuntimePlatform.DARWIN) {
await captureIosPreviewAsync({ device, directory, preview, options });
} else {
await captureAndroidPreviewAsync({ device, directory, preview, options });
}
return await readFile(preview);
} finally {
await rm(directory, { recursive: true, force: true });
}
}
type CaptureOptions = {
env: BuildStepEnv;
signal: AbortSignal;
timeout: number;
stdio: 'pipe';
};
async function captureIosPreviewAsync({
device,
directory,
preview,
options,
}: {
device: string;
directory: string;
preview: string;
options: CaptureOptions;
}): Promise<void> {
const screenshot = path.join(directory, 'screen.png');
const resized = path.join(directory, 'resized.png');
await spawn('xcrun', ['simctl', 'io', device, 'screenshot', screenshot], options);
await spawn('sips', ['-Z', String(PREVIEW_MAX_DIMENSION), screenshot, '--out', resized], options);
await spawn('cwebp', ['-quiet', '-q', String(PREVIEW_QUALITY), resized, '-o', preview], options);
}
async function captureAndroidPreviewAsync({
device,
directory,
preview,
options,
}: {
device: string;
directory: string;
preview: string;
options: CaptureOptions;
}): Promise<void> {
const screenshot = path.join(directory, 'screen.png');
let serial = device;
// expo-device-hub's readiness endpoint currently reports a placeholder instead of a serial.
if (serial === 'no-device-id') {
const result = await spawn('adb', ['devices'], options);
const devices = result.stdout
.split('\n')
.map(line => line.trim().split(/\s+/))
.filter(([, status]) => status === 'device');
if (devices.length !== 1) {
throw new Error('Expected exactly one connected Android device for the session preview.');
}
serial = devices[0][0];
}
const capture = spawn('adb', ['-s', serial, 'exec-out', 'screencap', '-p'], options);
// Preserve binary PNG output instead of spawn-async's UTF-8 stdout conversion.
await Promise.all([
capture,
pipeline(capture.child.stdout!, createWriteStream(screenshot), { signal: options.signal }),
]);
await spawn(
'ffmpeg',
[
'-nostdin',
'-y',
'-i',
screenshot,
'-vf',
`scale=${PREVIEW_MAX_DIMENSION}:${PREVIEW_MAX_DIMENSION}:force_original_aspect_ratio=decrease`,
'-frames:v',
'1',
'-c:v',
'libwebp',
'-quality',
String(PREVIEW_QUALITY),
preview,
],
options
);
}
@@ -183,8 +183,8 @@ export async function uploadDeviceRunSessionScreenRecordingsAsync(
: {}),
},
size,
stream: createReadStream(recordingPath),
reopenStream: () => createReadStream(recordingPath),
openStream: () => createReadStream(recordingPath),
attempts: 3,
});
return true;
} catch (err) {
@@ -96,8 +96,8 @@ async function uploadScreenshotAsync(
metadata: artifactDetails.metadata,
kind: 'screenshot',
size,
stream: createReadStream(file),
reopenStream: () => createReadStream(file),
openStream: () => createReadStream(file),
attempts: 3,
signal,
});
await rm(file);
@@ -21,6 +21,11 @@ import {
parseDeviceScreenRecordings,
uploadDeviceRunSessionScreenRecordingsAsync,
} from './deviceRunSessionScreenRecordings';
import {
captureDeviceRunSessionPreviewAsync,
ensureMacosPreviewEncoderInstalledAsync,
startDeviceRunSessionPreview,
} from './deviceRunSessionPreview';
import { startDeviceRunSessionScreenshotsAsync } from './deviceRunSessionScreenshots';
import {
type DetachedProcessHandle,
@@ -379,6 +384,7 @@ export async function startDeviceSessionHostAsync(
let previewTask: Promise<DeviceWebPreview> | null = null;
let finishTask: Promise<void> | null = null;
let hostReady = false;
let sessionPreview: ReturnType<typeof startDeviceRunSessionPreview> | null = null;
const host: DeviceSessionHost = {
openPreviewAsync({ baseDomain }) {
@@ -436,6 +442,7 @@ export async function startDeviceSessionHostAsync(
finishAsync() {
return (finishTask ??= finishDeviceSessionHostAsync(ctx, {
previewTask,
stopSessionPreviewAsync: async () => await sessionPreview?.stopAsync(),
previewServer,
screenshots,
serverName,
@@ -467,6 +474,29 @@ export async function startDeviceSessionHostAsync(
}
IosSimulatorRecordingUtils.useServeSimPackage(packageSpec);
}
// Android installed FFmpeg before launching the host. The optional thumbnail must not delay
// readiness on macOS, so it starts after its encoder is installed there.
void (async () => {
if (!isAndroid) {
await ensureMacosPreviewEncoderInstalledAsync({ env, logger });
}
if (!finishTask) {
sessionPreview = startDeviceRunSessionPreview({
ctx,
deviceRunSessionId: getDeviceRunSessionIdOrThrow(env),
logger,
captureAsync: signal =>
captureDeviceRunSessionPreviewAsync({
runtimePlatform,
device,
env,
signal,
}),
});
}
})().catch(err => {
logger.warn({ err }, 'Could not start refreshing the session preview.');
});
return host;
} catch (error) {
await host.finishAsync();
@@ -478,6 +508,7 @@ async function finishDeviceSessionHostAsync(
ctx: CustomBuildContext,
{
previewTask,
stopSessionPreviewAsync,
previewServer,
screenshots,
serverName,
@@ -486,6 +517,7 @@ async function finishDeviceSessionHostAsync(
logger,
}: {
previewTask: Promise<DeviceWebPreview> | null;
stopSessionPreviewAsync: () => Promise<void>;
previewServer: DetachedProcessHandle;
screenshots: { finishAsync(hostStopped: boolean): Promise<void> };
serverName: string;
@@ -494,6 +526,8 @@ async function finishDeviceSessionHostAsync(
logger: bunyan;
}
): Promise<void> {
// Stop capturing before the host stops, so the last thumbnail shows the session, not shutdown.
await stopSessionPreviewAsync();
// Native ngrok operations have no scoped cancellation. Retire a late listener too.
const retirePreview = withDeviceRunSessionTimeoutAsync(
{ name: 'Preview tunnel retirement', timeoutMs: 5_000 },
@@ -16,6 +16,7 @@ import { z } from 'zod';
import { randomBytes } from 'node:crypto';
import fs from 'node:fs';
import { createServer } from 'node:net';
import path from 'node:path';
import { clearTimeout, setTimeout } from 'node:timers';
import { setTimeout as setTimeoutAsync } from 'node:timers/promises';
@@ -299,11 +300,31 @@ async function sleepUntilAbortedAsync(
}
}
// Device-session tools resolve `ffmpeg` from PATH. Spawning it with the step's
// environment rejects with ENOENT when the binary is absent, and running it also
// proves that the installed binary works.
async function isFfmpegAvailableAsync(env: BuildStepEnv): Promise<boolean> {
return (await asyncResult(spawn('ffmpeg', ['-version'], { env }))).ok;
// Look up ffmpeg on PATH like device-session tools do. Running it on a fresh VM is slow
// (about 13 s for ffmpeg-full on macos-tahoe-26.6-xcode-27.1).
async function findFfmpegOnPathAsync(env: BuildStepEnv): Promise<string | null> {
for (const directory of (env.PATH ?? '').split(path.delimiter)) {
// An empty entry is the current directory.
const candidate = path.join(directory || '.', 'ffmpeg');
const stats = await asyncResult(fs.promises.stat(candidate));
if (
stats.ok &&
stats.value.isFile() &&
(await asyncResult(fs.promises.access(candidate, fs.constants.X_OK))).ok
) {
return candidate;
}
}
return null;
}
// Load ffmpeg in the background so its first real use skips the cold start.
function warmUpFfmpeg(env: BuildStepEnv): void {
void asyncResult(
(async () => {
await spawn('ffmpeg', ['-version'], { env });
})()
);
}
async function installFfmpegWithHomebrewAsync({
@@ -360,8 +381,10 @@ async function ensureFfmpegInstalledAsync({
logger: bunyan;
}): Promise<void> {
try {
if (await isFfmpegAvailableAsync(env)) {
logger.info('ffmpeg is already installed.');
const ffmpegPath = await findFfmpegOnPathAsync(env);
if (ffmpegPath) {
logger.info(`ffmpeg is already installed at ${ffmpegPath}.`);
warmUpFfmpeg(env);
return;
}
@@ -377,6 +400,7 @@ async function ensureFfmpegInstalledAsync({
await installFfmpegWithAptAsync({ env, logger });
}
logger.info('Installed ffmpeg.');
warmUpFfmpeg(env);
} catch (err) {
const error = err instanceof Error ? err : new Error(String(err));
Sentry.capture('Could not install ffmpeg for the device session', error, {
@@ -0,0 +1,298 @@
import {
type SandboxDaemonCommandResult,
SandboxDaemonError,
SandboxDaemonErrorCode,
SystemError,
} from '@expo/eas-build-job';
import { type bunyan } from '@expo/logger';
import { type Client, CombinedError } from '@urql/core';
import { graphql } from 'gql.tada';
import { createReadStream } from 'node:fs';
import fs from 'node:fs/promises';
import path from 'node:path';
import { performance } from 'node:perf_hooks';
import { Readable } from 'node:stream';
import { setTimeout as delay } from 'node:timers/promises';
import { graphqlAbortContext } from '../../utils/graphqlAbort';
import {
ArtifactPutError,
type ArtifactUploadSession,
putArtifactWithRetriesAsync,
} from './artifactPut';
const CREATE_SANDBOX_ARTIFACT_UPLOAD_SESSION_MUTATION = graphql(`
mutation CreateSandboxArtifactUploadSession(
$sandboxId: ID!
$input: CreateSandboxArtifactUploadSessionInput!
) {
sandbox {
createArtifactUploadSession(sandboxId: $sandboxId, input: $input) {
artifact {
id
}
uploadSession {
url
headers
}
}
}
}
`);
const FINALIZE_SANDBOX_ARTIFACT_MUTATION = graphql(`
mutation FinalizeSandboxArtifact($artifactId: ID!) {
sandbox {
finalizeArtifact(artifactId: $artifactId) {
id
downloadUrl
}
}
}
`);
const FINALIZE_ATTEMPTS = 3;
const REJECTED_UPLOAD_SESSION_ERROR_CODES = new Set(['VALIDATION_ERROR', 'UNAUTHORIZED']);
type SandboxArtifactUploadOutcome =
| { status: 'uploaded'; downloadUrl: string }
| { status: 'failed' };
export class SandboxArtifactUploadManager {
private readonly uploads = new Set<Promise<unknown>>();
public readonly stoppedPromise: Promise<void>;
public constructor(
private readonly options: {
graphqlClient: Client;
sandboxId: string;
logger: bunyan;
signal: AbortSignal;
}
) {
this.stoppedPromise = (async () => {
if (!options.signal.aborted) {
await new Promise<void>(resolve => {
options.signal.addEventListener('abort', () => resolve(), { once: true });
});
}
await Promise.all(this.uploads);
})();
}
public async startAsync({
filePath,
name,
yieldTimeMs,
}: {
filePath: string;
name: string;
yieldTimeMs: number;
}): Promise<SandboxDaemonCommandResult<'uploadArtifact'>> {
this.options.signal.throwIfAborted();
const deadline = performance.now() + yieldTimeMs;
const uploadPromise = startSandboxArtifactUploadAsync({ ...this.options, filePath, name });
const completed = uploadPromise.then(
({ completed }) => completed,
() => {}
);
this.uploads.add(completed);
void completed.finally(() => this.uploads.delete(completed));
const upload = await uploadPromise;
let timer: NodeJS.Timeout | undefined;
try {
const outcome = await Promise.race([
upload.completed,
new Promise<{ status: 'uploading' }>(resolve => {
timer = setTimeout(
() => resolve({ status: 'uploading' }),
Math.max(0, deadline - performance.now())
);
}),
]);
return { id: upload.id, ...outcome };
} finally {
clearTimeout(timer);
}
}
}
export async function startSandboxArtifactUploadAsync({
graphqlClient,
sandboxId,
logger,
filePath,
name,
signal,
}: {
graphqlClient: Client;
sandboxId: string;
logger: bunyan;
filePath: string;
name: string;
signal: AbortSignal;
}): Promise<{ id: string; completed: Promise<SandboxArtifactUploadOutcome> }> {
let stats;
try {
stats = await fs.stat(filePath);
} catch (error: any) {
if (error?.code === 'ENOENT') {
throw new SandboxDaemonError(
SandboxDaemonErrorCode.BAD_REQUEST,
`File does not exist: ${filePath}`
);
}
if (error?.code === 'EACCES') {
throw new SandboxDaemonError(
SandboxDaemonErrorCode.BAD_REQUEST,
`File is not accessible: ${filePath}`
);
}
throw error;
}
if (!stats.isFile()) {
throw new SandboxDaemonError(
SandboxDaemonErrorCode.BAD_REQUEST,
`${filePath} is not a regular file. To upload a directory, create an archive first, for example tar -czf out.tgz ${filePath}, then upload the archive.`
);
}
const size = stats.size;
const {
artifact: { id: artifactId },
uploadSession,
} = await createSandboxArtifactUploadSessionAsync(
graphqlClient,
{ sandboxId, name, filename: path.basename(filePath), size },
AbortSignal.any([signal, AbortSignal.timeout(15_000)])
);
const completed = (async (): Promise<SandboxArtifactUploadOutcome> => {
logger.info(`Uploading sandbox artifact "${name}" (${artifactId}, ${size} bytes).`);
try {
await putSandboxArtifactAsync({ filePath, size, uploadSession, signal });
} catch (error) {
logger.error({ err: error }, `Failed to upload sandbox artifact "${name}" (${artifactId}).`);
return { status: 'failed' };
}
let downloadUrl: string;
try {
downloadUrl = await finalizeSandboxArtifactAsync(graphqlClient, artifactId, signal);
} catch (error) {
logger.error(
{ err: error },
`Uploaded sandbox artifact "${name}" (${artifactId}), but could not finalize it.`
);
return { status: 'failed' };
}
logger.info(`Uploaded sandbox artifact "${name}" (${artifactId}, ${size} bytes).`);
return { status: 'uploaded', downloadUrl };
})();
return { id: artifactId, completed };
}
async function createSandboxArtifactUploadSessionAsync(
graphqlClient: Client,
{
sandboxId,
name,
filename,
size,
}: { sandboxId: string; name: string; filename: string; size: number },
signal: AbortSignal
) {
const result = await graphqlClient
.mutation(
CREATE_SANDBOX_ARTIFACT_UPLOAD_SESSION_MUTATION,
{ sandboxId, input: { name, filename, size } },
graphqlAbortContext(signal)
)
.toPromise();
signal.throwIfAborted();
if (result.error) {
const { graphQLErrors } = result.error;
const message =
graphQLErrors.length > 0
? graphQLErrors.map(error => error.message).join('\n')
: `Failed to create an upload session for sandbox artifact "${name}": ${result.error.message}`;
if (
graphQLErrors.length > 0 &&
graphQLErrors.every(error =>
REJECTED_UPLOAD_SESSION_ERROR_CODES.has(error.extensions?.errorCode as string)
)
) {
throw new SandboxDaemonError(SandboxDaemonErrorCode.BAD_REQUEST, message, {
cause: result.error,
});
}
throw new SystemError(message, { cause: result.error });
}
return result.data!.sandbox.createArtifactUploadSession;
}
async function putSandboxArtifactAsync({
filePath,
size,
uploadSession,
signal,
}: {
filePath: string;
size: number;
uploadSession: ArtifactUploadSession;
signal: AbortSignal;
}): Promise<void> {
const openStream = (): Readable =>
size === 0 ? Readable.from([]) : createReadStream(filePath, { start: 0, end: size - 1 });
try {
await putArtifactWithRetriesAsync(
{ uploadSession, source: { openStream }, signal },
{ attempts: 3 }
);
} catch (error) {
if (!(error instanceof ArtifactPutError && error.status === 412)) {
throw error;
}
}
}
async function finalizeSandboxArtifactAsync(
graphqlClient: Client,
artifactId: string,
signal: AbortSignal
): Promise<string> {
for (let attempt = 1; ; attempt++) {
const timeoutSignal = AbortSignal.timeout(15_000);
const finalizeSignal = AbortSignal.any([signal, timeoutSignal]);
try {
const result = await graphqlClient
.mutation(
FINALIZE_SANDBOX_ARTIFACT_MUTATION,
{ artifactId },
graphqlAbortContext(finalizeSignal)
)
.toPromise();
finalizeSignal.throwIfAborted();
if (result.error) {
throw result.error;
}
const { downloadUrl } = result.data!.sandbox.finalizeArtifact;
if (downloadUrl === null) {
throw new SystemError(`Finalized sandbox artifact ${artifactId} has no download URL.`);
}
return downloadUrl;
} catch (error) {
signal.throwIfAborted();
const retryable =
timeoutSignal.aborted || (error instanceof CombinedError && isTransientGraphqlError(error));
if (!retryable || attempt >= FINALIZE_ATTEMPTS) {
throw error;
}
await delay(500 * attempt, undefined, { signal });
}
}
}
function isTransientGraphqlError(error: CombinedError): boolean {
const status: number | undefined = error.response?.status;
return status === undefined ? error.networkError !== undefined : status >= 500;
}
@@ -1,14 +1,24 @@
import {
type SandboxDaemonCommandParams,
type SandboxDaemonCommandResult,
SandboxDaemonError,
SandboxDaemonErrorCode,
type SandboxDaemonMethod,
} from '@expo/eas-build-job';
import { type bunyan } from '@expo/logger';
import { type Client } from '@urql/core';
import fs, { type FileHandle } from 'node:fs/promises';
import path from 'node:path';
import { performance } from 'node:perf_hooks';
import { readBytesAsync, readTextAsync } from '../../utils/files';
import { detectFileType } from '../../utils/files/filetypes';
import { SandboxArtifactUploadManager } from './sandboxArtifacts';
import { ShellSessionManager } from './shellSessionManager';
const DEFAULT_EXEC_YIELD_TIME_MS = 10_000;
const DEFAULT_WRITE_YIELD_TIME_MS = 250;
const DEFAULT_UPLOAD_YIELD_TIME_MS = 20_000;
export type SandboxDaemonCommandImplementations = {
[Method in SandboxDaemonMethod]: (
@@ -20,15 +30,32 @@ export function createSandboxCommandImplementations({
workingDirectory,
env,
signal,
graphqlClient,
sandboxId,
logger,
}: {
workingDirectory: string;
env: NodeJS.ProcessEnv;
signal: AbortSignal;
graphqlClient: Client;
sandboxId: string;
logger: bunyan;
}): {
commandImplementations: SandboxDaemonCommandImplementations;
stoppedPromise: Promise<void>;
} {
const sessions = new ShellSessionManager({ workingDirectory, env, signal });
const artifactUploads = new SandboxArtifactUploadManager({
graphqlClient,
sandboxId,
logger,
signal,
});
const stoppedPromise = Promise.all([
sessions.stoppedPromise,
artifactUploads.stoppedPromise,
]).then(() => {});
stoppedPromise.catch(() => {});
return {
commandImplementations: {
async execCommand(params) {
@@ -55,7 +82,69 @@ export function createSandboxCommandImplementations({
);
return { ...result, wallTimeSeconds: (performance.now() - callStartedAt) / 1_000 };
},
async readFile(params) {
const filePath = path.resolve(workingDirectory, params.path);
const file = await openFileAsync(filePath);
try {
const stats = await file.stat();
if (!stats.isFile()) {
throw new SandboxDaemonError(
SandboxDaemonErrorCode.BAD_REQUEST,
`${filePath} is not a regular file.`
);
}
const mimeType = await detectFileType(file);
if (mimeType === null) {
const truncated = stats.size > params.maxTextBytes;
const text = await readTextAsync(
file,
Math.min(stats.size, params.maxTextBytes),
truncated
);
if (text === null) {
return { kind: 'binary', size: stats.size };
}
return { kind: 'text', text, size: stats.size, truncated };
}
if (stats.size > params.maxImageBytes) {
return { kind: 'image', mimeType, size: stats.size, error: 'tooLarge' };
}
const data = await readBytesAsync(file, stats.size);
return { kind: 'image', mimeType, data: data.toString('base64') };
} finally {
await file.close();
}
},
async uploadArtifact(params) {
return await artifactUploads.startAsync({
filePath: path.resolve(workingDirectory, params.path),
name: params.name,
yieldTimeMs: params.yieldTimeMs ?? DEFAULT_UPLOAD_YIELD_TIME_MS,
});
},
},
stoppedPromise: sessions.stoppedPromise,
stoppedPromise,
};
}
async function openFileAsync(filePath: string): Promise<FileHandle> {
try {
return await fs.open(filePath, fs.constants.O_RDONLY | fs.constants.O_NONBLOCK);
} catch (error: any) {
if (error?.code === 'ENOENT') {
throw new SandboxDaemonError(
SandboxDaemonErrorCode.BAD_REQUEST,
`File does not exist: ${filePath}`
);
}
if (error?.code === 'EACCES') {
throw new SandboxDaemonError(
SandboxDaemonErrorCode.BAD_REQUEST,
`File is not accessible: ${filePath}`
);
}
throw error;
}
}
@@ -1,12 +1,15 @@
import {
type SandboxDaemonCommandResult,
SandboxDaemonCommands,
SandboxDaemonError,
SandboxDaemonErrorCode,
type SandboxDaemonMethod,
SandboxDaemonRequestZ,
type SandboxDaemonResponse,
SystemError,
} from '@expo/eas-build-job';
import { type bunyan } from '@expo/logger';
import { type Client } from '@urql/core';
import { setTimeout as setTimeoutAsync } from 'node:timers/promises';
import WebSocket from 'ws';
@@ -23,6 +26,8 @@ export interface SandboxDaemonOptions {
signal?: AbortSignal;
workingDirectory: string;
env: NodeJS.ProcessEnv;
graphqlClient: Client;
sandboxId: string;
}
export interface SandboxDaemon {
@@ -44,6 +49,9 @@ export async function startSandboxDaemonAsync(
workingDirectory: options.workingDirectory,
env: options.env,
signal: abortController.signal,
graphqlClient: options.graphqlClient,
sandboxId: options.sandboxId,
logger: options.logger,
});
const connected = new Promise<void>((resolve, reject) => {
resolveConnected = resolve;
@@ -158,23 +166,39 @@ async function handleMessageAsync(
try {
rawRequest = JSON.parse(message);
} catch {
return { jsonrpc: '2.0', id: null, error: { code: -32700, message: 'Parse error' } };
return {
jsonrpc: '2.0',
id: null,
error: { code: SandboxDaemonErrorCode.PARSE_ERROR, message: 'Parse error' },
};
}
const request = SandboxDaemonRequestZ.safeParse(rawRequest);
if (!request.success) {
return { jsonrpc: '2.0', id: null, error: { code: -32600, message: 'Invalid request' } };
return {
jsonrpc: '2.0',
id: null,
error: { code: SandboxDaemonErrorCode.INVALID_REQUEST, message: 'Invalid request' },
};
}
const { id, method, params } = request.data;
if (!Object.hasOwn(SandboxDaemonCommands, method)) {
return { jsonrpc: '2.0', id, error: { code: -32601, message: 'Method not found' } };
return {
jsonrpc: '2.0',
id,
error: { code: SandboxDaemonErrorCode.METHOD_NOT_FOUND, message: 'Method not found' },
};
}
const commandMethod = method as SandboxDaemonMethod;
const parsedParams = SandboxDaemonCommands[commandMethod].params.safeParse(params);
if (!parsedParams.success) {
return { jsonrpc: '2.0', id, error: { code: -32602, message: 'Invalid params' } };
return {
jsonrpc: '2.0',
id,
error: { code: SandboxDaemonErrorCode.INVALID_PARAMS, message: 'Invalid params' },
};
}
try {
return {
@@ -190,7 +214,11 @@ async function handleMessageAsync(
return {
jsonrpc: '2.0',
id,
error: { code: -32603, message: error instanceof Error ? error.message : 'Internal error' },
error: {
code:
error instanceof SandboxDaemonError ? error.code : SandboxDaemonErrorCode.INTERNAL_ERROR,
message: error instanceof Error ? error.message : 'Internal error',
},
};
}
}
@@ -21,16 +21,12 @@ export async function uploadServeSimLogsFileAsync(
signal?: AbortSignal;
}
): Promise<void> {
let stream: ReturnType<typeof createReadStream> | undefined;
try {
const { size } = await stat(filePath);
if (size === 0) {
return;
}
const fileController = new AbortController();
stream = createReadStream(filePath);
// A read error can occur while the upload session is still being allocated.
stream.on('error', err => fileController.abort(err));
await uploadDeviceRunSessionArtifactAsync(ctx, {
deviceRunSessionId,
artifactId: `simulator-log-${udid}`,
@@ -39,7 +35,7 @@ export async function uploadServeSimLogsFileAsync(
kind: 'simulator-log',
metadata: { __eas_type: 'simulator-log', udid, scope: 'user-apps', source: 'serve-sim/logs' },
size,
stream,
openStream: () => createReadStream(filePath).on('error', err => fileController.abort(err)),
signal: AbortSignal.any([fileController.signal, ...(signal ? [signal] : [])]),
});
} catch (err) {
@@ -47,7 +43,5 @@ export async function uploadServeSimLogsFileAsync(
{ err },
`Could not upload simulator logs for ${udid}; other artifacts will continue.`
);
} finally {
stream?.destroy();
}
}
@@ -56,7 +56,7 @@ export async function uploadServeSimMetricsFileAsync(
}),
},
size,
stream: createReadStream(filePath),
openStream: () => createReadStream(filePath),
});
} catch (err) {
const error = err instanceof Error ? err : new Error(String(err));
@@ -1,3 +1,4 @@
import { SandboxDaemonError, SandboxDaemonErrorCode } from '@expo/eas-build-job';
import * as pty from 'node-pty';
import { type ChildProcess, spawn } from 'node:child_process';
import fs from 'node:fs/promises';
@@ -126,7 +127,10 @@ export class ShellSessionManager {
private getSession(sessionId: number): CommandSession {
const session = this.sessions.get(sessionId);
if (!session) {
throw new Error(`Command session ${sessionId} does not exist.`);
throw new SandboxDaemonError(
SandboxDaemonErrorCode.BAD_REQUEST,
`Command session ${sessionId} does not exist.`
);
}
return session;
}
@@ -258,21 +262,33 @@ async function validateWorkingDirectoryAsync(workingDirectory: string): Promise<
stats = await fs.stat(workingDirectory);
} catch (error: any) {
if (error?.code === 'ENOENT') {
throw new Error(`Working directory does not exist: ${workingDirectory}`);
throw new SandboxDaemonError(
SandboxDaemonErrorCode.BAD_REQUEST,
`Working directory does not exist: ${workingDirectory}`
);
}
if (error?.code === 'EACCES') {
throw new Error(`Working directory is not accessible: ${workingDirectory}`);
throw new SandboxDaemonError(
SandboxDaemonErrorCode.BAD_REQUEST,
`Working directory is not accessible: ${workingDirectory}`
);
}
throw error;
}
if (!stats.isDirectory()) {
throw new Error(`Working directory is not a directory: ${workingDirectory}`);
throw new SandboxDaemonError(
SandboxDaemonErrorCode.BAD_REQUEST,
`Working directory is not a directory: ${workingDirectory}`
);
}
try {
await fs.access(workingDirectory, fs.constants.R_OK | fs.constants.X_OK);
} catch (error: any) {
if (error?.code === 'EACCES') {
throw new Error(`Working directory is not accessible: ${workingDirectory}`);
throw new SandboxDaemonError(
SandboxDaemonErrorCode.BAD_REQUEST,
`Working directory is not accessible: ${workingDirectory}`
);
}
throw error;
}
@@ -0,0 +1,27 @@
export namespace AbortUtils {
export type ResettableAbortSignal = {
signal: AbortSignal;
resetDeadline: () => void;
};
/**
* Like AbortSignal.timeout, but resetDeadline restarts the timer, so the timeout bounds stalls
* instead of total time.
*/
export function resettableTimeout(timeoutMs: number): ResettableAbortSignal {
const controller = new AbortController();
const timer = setTimeout(() => {
controller.abort(
new DOMException(`The operation timed out after ${timeoutMs}ms.`, 'TimeoutError')
);
}, timeoutMs).unref();
return {
signal: controller.signal,
resetDeadline: () => {
if (!controller.signal.aborted) {
timer.refresh();
}
},
};
}
}
@@ -0,0 +1,39 @@
import { AbortUtils } from '../AbortUtils';
describe(AbortUtils.resettableTimeout, () => {
beforeEach(() => jest.useFakeTimers());
afterEach(() => jest.useRealTimers());
it('aborts with a TimeoutError when the deadline expires', () => {
const { signal } = AbortUtils.resettableTimeout(100);
jest.advanceTimersByTime(99);
expect(signal.aborted).toBe(false);
jest.advanceTimersByTime(1);
expect(signal.reason).toMatchObject({
name: 'TimeoutError',
message: 'The operation timed out after 100ms.',
});
});
it('restarts the deadline from the last reset', () => {
const { signal, resetDeadline } = AbortUtils.resettableTimeout(100);
jest.advanceTimersByTime(80);
resetDeadline();
jest.advanceTimersByTime(80);
resetDeadline();
jest.advanceTimersByTime(99);
expect(signal.aborted).toBe(false);
jest.advanceTimersByTime(1);
expect(signal.aborted).toBe(true);
});
it('does not rearm the deadline after it expired', () => {
const { resetDeadline } = AbortUtils.resettableTimeout(100);
jest.advanceTimersByTime(100);
resetDeadline();
expect(jest.getTimerCount()).toBe(0);
});
});
@@ -1,12 +1,13 @@
import { Platform } from '@expo/eas-build-job';
import { PipeMode } from '@expo/logger';
import { BuildContext } from '../../context';
import * as expoUpdates from '../expoUpdates';
import * as expoUpdates from '../../steps/utils/expoUpdates';
import { uploadEmbeddedBundleAsync } from '../expoUpdatesEmbedded';
import * as easCli from '../easCli';
import * as artifacts from '../artifacts';
import { createMockLogger } from '../../__tests__/utils/logger';
jest.mock('../expoUpdates');
jest.mock('../../steps/utils/expoUpdates');
jest.mock('../easCli');
jest.mock('../artifacts');
@@ -29,13 +30,13 @@ function zipEntryMap(entries: Record<string, true>): Record<string, { name: stri
return Object.fromEntries(Object.keys(entries).map(name => [name, { name }]));
}
function makeCtx(overrides: {
function makeArgs(overrides: {
platform: Platform;
simulator?: boolean;
developmentClient?: boolean;
channel?: string;
env?: Record<string, string>;
}): BuildContext<any> {
}): Parameters<typeof uploadEmbeddedBundleAsync>[0] {
const job =
overrides.platform === Platform.IOS
? {
@@ -53,22 +54,19 @@ function makeCtx(overrides: {
return {
job,
env: overrides.env ?? {},
appConfig: Promise.resolve({
appConfig: {
name: 'my-app',
slug: 'my-app',
updates: { url: 'https://u.expo.dev/project-id' },
}),
logger: {
info: jest.fn(),
warn: jest.fn(),
},
markBuildPhaseSkipped: jest.fn(),
markBuildPhaseHasWarnings: jest.fn(),
getReactNativeProjectDirectory: () => '/project',
} as any;
logger: createMockLogger(),
projectDir: '/project',
} as Parameters<typeof uploadEmbeddedBundleAsync>[0];
}
describe('uploadEmbeddedBundleAsync', () => {
beforeEach(() => {
jest.mocked(expoUpdates.isEASUpdateConfigured).mockResolvedValue(true);
jest.mocked(expoUpdates.isEASUpdateConfigured).mockReturnValue(true);
jest.mocked(easCli.runEasCliCommand).mockResolvedValue({} as any);
jest.mocked(artifacts.findArtifacts).mockResolvedValue([]);
mockZipEntries.mockResolvedValue({});
@@ -82,33 +80,36 @@ describe('uploadEmbeddedBundleAsync', () => {
});
it('skips when EAS Update is not configured', async () => {
jest.mocked(expoUpdates.isEASUpdateConfigured).mockResolvedValue(false);
const ctx = makeCtx({ platform: Platform.ANDROID, channel: 'production' });
jest.mocked(expoUpdates.isEASUpdateConfigured).mockReturnValue(false);
const args = makeArgs({ platform: Platform.ANDROID, channel: 'production' });
await uploadEmbeddedBundleAsync(ctx);
const { status } = await uploadEmbeddedBundleAsync(args);
expect(ctx.markBuildPhaseSkipped).toHaveBeenCalled();
expect(status).toBe('skipped');
expect(artifacts.findArtifacts).not.toHaveBeenCalled();
});
it('warns when no channel is configured and does not look for the archive', async () => {
const ctx = makeCtx({ platform: Platform.ANDROID });
const args = makeArgs({ platform: Platform.ANDROID });
await uploadEmbeddedBundleAsync(ctx);
const { status } = await uploadEmbeddedBundleAsync(args);
expect(ctx.logger.warn).toHaveBeenCalledWith(
expect(args.logger.warn).toHaveBeenCalledWith(
'Skipping embedded bundle upload: no channel configured for this build profile.'
);
expect(ctx.markBuildPhaseHasWarnings).toHaveBeenCalled();
expect(status).toBe('failed');
expect(artifacts.findArtifacts).not.toHaveBeenCalled();
});
it('throws for an unsupported platform', async () => {
const ctx = makeCtx({ platform: Platform.ANDROID, channel: 'production' });
(ctx.job as { platform: string }).platform = 'web';
it('warns for an unsupported platform', async () => {
const args = makeArgs({ platform: Platform.ANDROID, channel: 'production' });
(args.job as { platform: string }).platform = 'web';
await expect(uploadEmbeddedBundleAsync(ctx)).rejects.toThrow(
'Uploading embedded updates is not supported for the web platform.'
const { status } = await uploadEmbeddedBundleAsync(args);
expect(status).toBe('failed');
expect(args.logger.warn).toHaveBeenCalledWith(
'Skipping embedded bundle upload: the web platform is not supported.'
);
expect(artifacts.findArtifacts).not.toHaveBeenCalled();
});
@@ -121,14 +122,18 @@ describe('uploadEmbeddedBundleAsync', () => {
'assets/app.manifest': true,
})
);
const ctx = makeCtx({
const args = makeArgs({
platform: Platform.ANDROID,
channel: 'production',
env: { EAS_BUILD_ID: 'build-123' },
});
await uploadEmbeddedBundleAsync(ctx);
const { status } = await uploadEmbeddedBundleAsync(args);
expect(status).toBe('uploaded');
expect(artifacts.findArtifacts).toHaveBeenCalledWith(
expect.objectContaining({ patternOrPath: 'android/app/build/outputs/**/*.{apk,aab}' })
);
expect(mockZipExtract).toHaveBeenCalledWith(
'assets/index.android.bundle',
expect.stringContaining('index.android.bundle')
@@ -144,10 +149,24 @@ describe('uploadEmbeddedBundleAsync', () => {
'--build-id',
'build-123',
]),
options: expect.objectContaining({ mode: PipeMode.COMBINED_AS_STDOUT }),
})
);
});
it('uses applicationArchivePath to find the archive', async () => {
const args = makeArgs({ platform: Platform.ANDROID, channel: 'production' });
await uploadEmbeddedBundleAsync({
...args,
job: { ...args.job, applicationArchivePath: 'custom/app.aab' },
});
expect(artifacts.findArtifacts).toHaveBeenCalledWith(
expect.objectContaining({ patternOrPath: 'custom/app.aab' })
);
});
it('uploads from Android AAB archives', async () => {
jest.mocked(artifacts.findArtifacts).mockResolvedValue(['/tmp/app-release.aab']);
mockZipEntries.mockResolvedValue(
@@ -156,10 +175,11 @@ describe('uploadEmbeddedBundleAsync', () => {
'base/assets/app.manifest': true,
})
);
const ctx = makeCtx({ platform: Platform.ANDROID, channel: 'production' });
const args = makeArgs({ platform: Platform.ANDROID, channel: 'production' });
await uploadEmbeddedBundleAsync(ctx);
const { status } = await uploadEmbeddedBundleAsync(args);
expect(status).toBe('uploaded');
expect(mockZipExtract).toHaveBeenCalledWith(
'base/assets/index.android.bundle',
expect.stringContaining('index.android.bundle')
@@ -175,10 +195,14 @@ describe('uploadEmbeddedBundleAsync', () => {
'Payload/App.app/EXUpdates.bundle/app.manifest': true,
})
);
const ctx = makeCtx({ platform: Platform.IOS, channel: 'production' });
const args = makeArgs({ platform: Platform.IOS, channel: 'production' });
await uploadEmbeddedBundleAsync(ctx);
const { status } = await uploadEmbeddedBundleAsync(args);
expect(status).toBe('uploaded');
expect(artifacts.findArtifacts).toHaveBeenCalledWith(
expect.objectContaining({ patternOrPath: 'ios/build/*.ipa' })
);
expect(easCli.runEasCliCommand).toHaveBeenCalledWith(
expect.objectContaining({
args: expect.arrayContaining(['--platform', Platform.IOS]),
@@ -187,25 +211,24 @@ describe('uploadEmbeddedBundleAsync', () => {
});
it('skips simulator builds', async () => {
const ctx = makeCtx({ platform: Platform.IOS, simulator: true, channel: 'preview' });
const args = makeArgs({ platform: Platform.IOS, simulator: true, channel: 'preview' });
await uploadEmbeddedBundleAsync(ctx);
const { status } = await uploadEmbeddedBundleAsync(args);
expect(ctx.markBuildPhaseSkipped).toHaveBeenCalled();
expect(status).toBe('skipped');
expect(artifacts.findArtifacts).not.toHaveBeenCalled();
});
it('skips development client builds', async () => {
const ctx = makeCtx({
const args = makeArgs({
platform: Platform.ANDROID,
developmentClient: true,
channel: 'development',
});
await uploadEmbeddedBundleAsync(ctx);
const { status } = await uploadEmbeddedBundleAsync(args);
expect(ctx.markBuildPhaseSkipped).toHaveBeenCalled();
expect(ctx.markBuildPhaseHasWarnings).not.toHaveBeenCalled();
expect(status).toBe('skipped');
expect(artifacts.findArtifacts).not.toHaveBeenCalled();
});
@@ -216,11 +239,12 @@ describe('uploadEmbeddedBundleAsync', () => {
'assets/app.manifest': true,
})
);
const ctx = makeCtx({ platform: Platform.ANDROID, channel: 'production' });
const args = makeArgs({ platform: Platform.ANDROID, channel: 'production' });
await uploadEmbeddedBundleAsync(ctx);
const { status } = await uploadEmbeddedBundleAsync(args);
expect(ctx.logger.warn).toHaveBeenCalledWith(
expect(status).toBe('failed');
expect(args.logger.warn).toHaveBeenCalledWith(
'Skipping embedded bundle upload: bundle or manifest not found in archive.'
);
expect(easCli.runEasCliCommand).not.toHaveBeenCalled();
@@ -228,27 +252,27 @@ describe('uploadEmbeddedBundleAsync', () => {
it('warns when build archive is not found', async () => {
jest.mocked(artifacts.findArtifacts).mockResolvedValue([]);
const ctx = makeCtx({ platform: Platform.ANDROID, channel: 'production' });
const args = makeArgs({ platform: Platform.ANDROID, channel: 'production' });
await uploadEmbeddedBundleAsync(ctx);
const { status } = await uploadEmbeddedBundleAsync(args);
expect(ctx.logger.warn).toHaveBeenCalledWith(
expect(args.logger.warn).toHaveBeenCalledWith(
'Skipping embedded bundle upload: build archive not found.'
);
expect(ctx.markBuildPhaseHasWarnings).toHaveBeenCalled();
expect(status).toBe('failed');
expect(easCli.runEasCliCommand).not.toHaveBeenCalled();
});
it('treats findArtifacts errors as no archive found', async () => {
jest.mocked(artifacts.findArtifacts).mockRejectedValue(new Error('glob failed'));
const ctx = makeCtx({ platform: Platform.ANDROID, channel: 'production' });
const args = makeArgs({ platform: Platform.ANDROID, channel: 'production' });
await uploadEmbeddedBundleAsync(ctx);
const { status } = await uploadEmbeddedBundleAsync(args);
expect(ctx.logger.warn).toHaveBeenCalledWith(
expect(args.logger.warn).toHaveBeenCalledWith(
'Skipping embedded bundle upload: build archive not found.'
);
expect(ctx.markBuildPhaseHasWarnings).toHaveBeenCalled();
expect(status).toBe('failed');
expect(easCli.runEasCliCommand).not.toHaveBeenCalled();
});
@@ -261,15 +285,15 @@ describe('uploadEmbeddedBundleAsync', () => {
})
);
jest.mocked(easCli.runEasCliCommand).mockRejectedValue(new Error('upload failed'));
const ctx = makeCtx({ platform: Platform.ANDROID, channel: 'production' });
const args = makeArgs({ platform: Platform.ANDROID, channel: 'production' });
await uploadEmbeddedBundleAsync(ctx);
const { status } = await uploadEmbeddedBundleAsync(args);
expect(ctx.logger.warn).toHaveBeenCalledWith(
expect(args.logger.warn).toHaveBeenCalledWith(
expect.objectContaining({ err: expect.any(Error) }),
'Failed to upload embedded bundle.'
);
expect(ctx.markBuildPhaseHasWarnings).toHaveBeenCalled();
expect(status).toBe('failed');
});
it('swallows zip.close() failures so they do not mask the upload result', async () => {
@@ -281,9 +305,9 @@ describe('uploadEmbeddedBundleAsync', () => {
})
);
mockZipClose.mockRejectedValue(new Error('close failed'));
const ctx = makeCtx({ platform: Platform.ANDROID, channel: 'production' });
const args = makeArgs({ platform: Platform.ANDROID, channel: 'production' });
await expect(uploadEmbeddedBundleAsync(ctx)).resolves.toBeUndefined();
await expect(uploadEmbeddedBundleAsync(args)).resolves.toEqual({ status: 'uploaded' });
expect(easCli.runEasCliCommand).toHaveBeenCalled();
expect(mockZipClose).toHaveBeenCalled();
});
@@ -1,5 +1,6 @@
import { Android, BuildJob, Ios, Platform } from '@expo/eas-build-job';
import { PipeMode } from '@expo/logger';
import { ExpoConfig } from '@expo/config';
import { Android, BuildJob, Env, Ios, Metadata, Platform } from '@expo/eas-build-job';
import { PipeMode, bunyan } from '@expo/logger';
import { asyncResult } from '@expo/results';
import fs from 'fs-extra';
import os from 'os';
@@ -9,9 +10,9 @@ import semver from 'semver';
import { findArtifacts } from './artifacts';
import { runEasCliCommand } from './easCli';
import { resolveArtifactPath } from '../ios/resolve';
import { BuildContext } from '../context';
import { isEASUpdateConfigured } from './expoUpdates';
import { resolveArtifactPath as resolveAndroidArtifactPath } from '../android/resolve';
import { resolveArtifactPath as resolveIosArtifactPath } from '../ios/resolve';
import { isEASUpdateConfigured } from '../steps/utils/expoUpdates';
function parseBooleanEnvVar(value: string | undefined): boolean | undefined {
if (!value) {
@@ -27,55 +28,64 @@ function parseBooleanEnvVar(value: string | undefined): boolean | undefined {
* On SDK 57 and below the feature is still experimental and off by default. Projects opt in by
* setting EAS_UPDATE_EXPERIMENTAL_UPLOAD_EMBEDDED_BUNDLE to "1".
*/
export function shouldUploadEmbeddedBundle(ctx: BuildContext<BuildJob>): boolean {
export function shouldUploadEmbeddedBundle({
env,
metadata,
}: {
env: Env;
metadata?: Metadata | null;
}): boolean {
const explicitFlag =
parseBooleanEnvVar(ctx.env.EAS_UPDATE_UPLOAD_EMBEDDED_BUNDLE) ??
parseBooleanEnvVar(ctx.env.EAS_UPDATE_EXPERIMENTAL_UPLOAD_EMBEDDED_BUNDLE);
parseBooleanEnvVar(env.EAS_UPDATE_UPLOAD_EMBEDDED_BUNDLE) ??
parseBooleanEnvVar(env.EAS_UPDATE_EXPERIMENTAL_UPLOAD_EMBEDDED_BUNDLE);
if (explicitFlag !== undefined) {
return explicitFlag;
}
const sdkVersion = ctx.metadata?.sdkVersion;
const sdkVersion = metadata?.sdkVersion;
return !!sdkVersion && semver.satisfies(sdkVersion, '>=58');
}
export async function uploadEmbeddedBundleAsync(ctx: BuildContext<BuildJob>): Promise<void> {
if (!(await isEASUpdateConfigured(ctx))) {
ctx.markBuildPhaseSkipped();
return;
export async function uploadEmbeddedBundleAsync({
job,
env,
logger,
projectDir,
appConfig,
}: {
job: BuildJob;
env: Env;
logger: bunyan;
projectDir: string;
appConfig: ExpoConfig;
}): Promise<{ status: 'uploaded' | 'skipped' | 'failed' }> {
if (!isEASUpdateConfigured(appConfig, logger)) {
return { status: 'skipped' };
}
if (ctx.job.developmentClient) {
ctx.markBuildPhaseSkipped();
return;
if (job.developmentClient) {
return { status: 'skipped' };
}
const { platform } = ctx.job;
if (platform === Platform.IOS && (ctx.job as Ios.Job).simulator) {
ctx.markBuildPhaseSkipped();
return;
const { platform } = job;
if (platform === Platform.IOS && (job as Ios.Job).simulator) {
return { status: 'skipped' };
}
const channel = ctx.job.updates?.channel;
const channel = job.updates?.channel;
if (!channel) {
ctx.logger.warn(
'Skipping embedded bundle upload: no channel configured for this build profile.'
);
ctx.markBuildPhaseHasWarnings();
return;
logger.warn('Skipping embedded bundle upload: no channel configured for this build profile.');
return { status: 'failed' };
}
const projectDir = ctx.getReactNativeProjectDirectory();
let archivePattern: string;
if (platform === Platform.IOS) {
archivePattern = resolveArtifactPath(ctx as BuildContext<Ios.Job>);
archivePattern = resolveIosArtifactPath(job as Ios.Job);
} else if (platform === Platform.ANDROID) {
archivePattern =
(ctx as BuildContext<Android.Job>).job.applicationArchivePath ??
'android/app/build/outputs/**/*.{apk,aab}';
archivePattern = resolveAndroidArtifactPath(job as Android.Job);
} else {
throw new Error(`Uploading embedded updates is not supported for the ${platform} platform.`);
logger.warn(`Skipping embedded bundle upload: the ${platform} platform is not supported.`);
return { status: 'failed' };
}
const [archivePath] = await findArtifacts({
@@ -85,9 +95,8 @@ export async function uploadEmbeddedBundleAsync(ctx: BuildContext<BuildJob>): Pr
}).catch(() => [] as string[]);
if (!archivePath) {
ctx.logger.warn('Skipping embedded bundle upload: build archive not found.');
ctx.markBuildPhaseHasWarnings();
return;
logger.warn('Skipping embedded bundle upload: build archive not found.');
return { status: 'failed' };
}
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'eas-embedded-bundle-'));
@@ -109,9 +118,8 @@ export async function uploadEmbeddedBundleAsync(ctx: BuildContext<BuildJob>): Pr
);
if (!bundleEntry || !manifestEntry) {
ctx.logger.warn('Skipping embedded bundle upload: bundle or manifest not found in archive.');
ctx.markBuildPhaseHasWarnings();
return;
logger.warn('Skipping embedded bundle upload: bundle or manifest not found in archive.');
return { status: 'failed' };
}
await zip.extract(bundleEntry.name, bundlePath);
@@ -129,21 +137,22 @@ export async function uploadEmbeddedBundleAsync(ctx: BuildContext<BuildJob>): Pr
channel,
'--non-interactive',
];
if (ctx.env.EAS_BUILD_ID) {
args.push('--build-id', ctx.env.EAS_BUILD_ID);
if (env.EAS_BUILD_ID) {
args.push('--build-id', env.EAS_BUILD_ID);
}
await runEasCliCommand({
args,
options: {
cwd: projectDir,
env: ctx.env,
logger: ctx.logger,
mode: PipeMode.STDERR_ONLY_AS_STDOUT,
env,
logger,
mode: PipeMode.COMBINED_AS_STDOUT,
},
});
return { status: 'uploaded' };
} catch (err: any) {
ctx.logger.warn({ err }, 'Failed to upload embedded bundle.');
ctx.markBuildPhaseHasWarnings();
logger.warn({ err }, 'Failed to upload embedded bundle.');
return { status: 'failed' };
} finally {
await asyncResult(zip.close());
}
Loaded 100 of 166 files, more files were not shown because too many files have changed in this diff. Show more