mirror of
https://github.com/expo/eas-cli.git
synced 2026-11-15 17:31:46 -10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
34e8b45bad | ||
|
|
d1de958201 | ||
|
|
2b672fc07f | ||
|
|
8453efcfb6 | ||
|
|
0456e8eea6 | ||
|
|
0ec9a81552 | ||
|
|
3b4fedeee5 | ||
|
|
570617f467 | ||
|
|
8262aa34c6 | ||
|
|
1f72736126 | ||
|
|
3328b60f85 | ||
|
|
4abb8601d5 | ||
|
|
9a881d8366 | ||
|
|
657034a1a6 | ||
|
|
bb98f5beef | ||
|
|
4aca8b1cc4 | ||
|
|
800a0e0893 | ||
|
|
2911b04ea1 | ||
|
|
50b381abe0 | ||
|
|
c8696f6012 | ||
|
|
45d795e076 | ||
|
|
8e98a915b9 | ||
|
|
73c65d3edb |
No files matched your search
@@ -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:
|
||||
|
||||
+24
-2
@@ -8,12 +8,32 @@ This is the log of notable changes to EAS CLI and related packages.
|
||||
|
||||
### 🎉 New features
|
||||
|
||||
- [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))
|
||||
|
||||
### 🧹 Chores
|
||||
|
||||
## [24.12.0](https://github.com/expo/eas-cli/releases/tag/v24.12.0) - 2026-10-07
|
||||
|
||||
### 🎉 New features
|
||||
@@ -236,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
|
||||
|
||||
@@ -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
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"STAGING": "24.12.0",
|
||||
"PRODUCTION": "24.12.0"
|
||||
"STAGING": "24.12.1",
|
||||
"PRODUCTION": "24.12.1"
|
||||
}
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"npmClient": "yarn",
|
||||
"version": "24.12.1",
|
||||
"version": "24.13.0",
|
||||
"$schema": "node_modules/lerna/schemas/lerna-schema.json"
|
||||
}
|
||||
@@ -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
@@ -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",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@expo/build-tools",
|
||||
"version": "24.12.1",
|
||||
"version": "24.13.0",
|
||||
"bugs": "https://github.com/expo/eas-cli/issues",
|
||||
"license": "BUSL-1.1",
|
||||
"author": "Expo <[email protected]>",
|
||||
@@ -41,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",
|
||||
@@ -88,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",
|
||||
|
||||
@@ -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>) {
|
||||
|
||||
@@ -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';
|
||||
@@ -44,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';
|
||||
@@ -85,6 +87,7 @@ export function getEasFunctions(ctx: CustomBuildContext): BuildFunction[] {
|
||||
createReadIpaInfoBuildFunction(),
|
||||
createReadAndroidAppInfoBuildFunction(),
|
||||
createDownloadBuildFunction(ctx),
|
||||
createDownloadSubmissionArchiveFunction(ctx),
|
||||
createInstallBuildFunction(),
|
||||
createLaunchApplicationFunction(),
|
||||
createEasExportBuildFunction(),
|
||||
@@ -121,6 +124,7 @@ export function getEasFunctions(ctx: CustomBuildContext): BuildFunction[] {
|
||||
createStartServeSimLogsBuildFunction(),
|
||||
createCollectServeSimLogsBuildFunction(ctx),
|
||||
createStartSandboxBuildFunction(ctx),
|
||||
createRunAgentBuildFunction(ctx),
|
||||
createCollectServeSimMetricsBuildFunction(ctx),
|
||||
createInstallMaestroBuildFunction(),
|
||||
createInstallMitmproxyBuildFunction(),
|
||||
|
||||
+73
@@ -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);
|
||||
});
|
||||
@@ -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".'
|
||||
);
|
||||
});
|
||||
});
|
||||
+2
-1
@@ -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,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);
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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 = () =>
|
||||
|
||||
@@ -122,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');
|
||||
@@ -156,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);
|
||||
@@ -196,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;
|
||||
@@ -222,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');
|
||||
@@ -235,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);
|
||||
});
|
||||
|
||||
@@ -259,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();
|
||||
@@ -282,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');
|
||||
@@ -297,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 () => {
|
||||
|
||||
@@ -479,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));
|
||||
|
||||
@@ -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' }]);
|
||||
});
|
||||
});
|
||||
+3
@@ -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}
|
||||
+11
@@ -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}
|
||||
+14
@@ -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)"}}
|
||||
+7
@@ -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 [];
|
||||
}
|
||||
}
|
||||
@@ -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}.`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
-1
@@ -1,6 +1,6 @@
|
||||
import { vol } from 'memfs';
|
||||
|
||||
import { decompressTarAsync, isFileTarGzAsync } from '../files';
|
||||
import { decompressTarAsync, isFileTarGzAsync } from '..';
|
||||
|
||||
// contains a 'hello.txt' file with 'hello' content
|
||||
const HELLO_TAR_GZ_BUFFER = Buffer.from(
|
||||
@@ -0,0 +1,46 @@
|
||||
import { IMAGE_HEADER_BYTES, detectFileTypeFromHeader } from '..';
|
||||
|
||||
const PNG_SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
|
||||
|
||||
function bytes(...parts: (number[] | string)[]): Buffer {
|
||||
return Buffer.concat(
|
||||
parts.map(part => (typeof part === 'string' ? Buffer.from(part, 'latin1') : Buffer.from(part)))
|
||||
);
|
||||
}
|
||||
|
||||
function header(...parts: (number[] | string)[]): Buffer {
|
||||
return Buffer.concat([bytes(...parts), Buffer.alloc(IMAGE_HEADER_BYTES, 0xaa)]).subarray(
|
||||
0,
|
||||
IMAGE_HEADER_BYTES
|
||||
);
|
||||
}
|
||||
|
||||
describe(detectFileTypeFromHeader.name, () => {
|
||||
it.each([
|
||||
['PNG', 'image/png', header(PNG_SIGNATURE, [0x00, 0x00, 0x00, 0x0d], 'IHDR')],
|
||||
['JFIF JPEG', 'image/jpeg', header([0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10], 'JFIF')],
|
||||
['EXIF JPEG', 'image/jpeg', header([0xff, 0xd8, 0xff, 0xe1, 0x12, 0x34], 'Exif')],
|
||||
['GIF87a', 'image/gif', header('GIF87a', [0x01, 0x00, 0x01, 0x00])],
|
||||
['GIF89a', 'image/gif', header('GIF89a', [0x01, 0x00, 0x01, 0x00])],
|
||||
['WebP', 'image/webp', header('RIFF', [0x24, 0x10, 0x00, 0x00], 'WEBP', 'VP8 ')],
|
||||
])('detects a %s header followed by image data', (_, mimeType, contents) => {
|
||||
expect(detectFileTypeFromHeader(contents)).toBe(mimeType);
|
||||
});
|
||||
|
||||
it.each([
|
||||
['an empty file', bytes()],
|
||||
['text', header('export const')],
|
||||
['a PNG signature that is cut short', bytes(PNG_SIGNATURE.slice(0, -1))],
|
||||
['a JPEG SOI marker without a following marker', bytes([0xff, 0xd8])],
|
||||
['a GIF signature that is cut short', bytes('GIF89')],
|
||||
['a WebP header that is cut short', bytes('RIFF', [0x24, 0x10, 0x00, 0x00], 'WEB')],
|
||||
['a PNG signature with one changed byte', header(PNG_SIGNATURE.slice(0, -1), [0x0b])],
|
||||
['a PNG signature that does not start at the first byte', header([0x00], PNG_SIGNATURE)],
|
||||
['a JPEG SOI marker followed by a non-marker byte', header([0xff, 0xd8, 0x00, 0xe0])],
|
||||
['an unknown GIF version', header('GIF88a', [0x01, 0x00, 0x01, 0x00])],
|
||||
['a RIFF file that is not WebP', header('RIFF', [0x24, 0x10, 0x00, 0x00], 'WAVE')],
|
||||
['a big-endian RIFX file', header('RIFX', [0x00, 0x00, 0x10, 0x24], 'WEBP')],
|
||||
])('does not detect an image in %s', (_, contents) => {
|
||||
expect(detectFileTypeFromHeader(contents)).toBe(null);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,10 @@
|
||||
export const GIF_HEADER_BYTES = 6;
|
||||
|
||||
// GIF89a section 17 (Header): https://www.w3.org/Graphics/GIF/spec-gif89a.txt
|
||||
export function isGif(header: Buffer): boolean {
|
||||
if (header.byteLength < GIF_HEADER_BYTES) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ['GIF87a', 'GIF89a'].includes(header.toString('latin1', 0, 6));
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { FileHandle } from 'node:fs/promises';
|
||||
import { GIF_HEADER_BYTES, isGif } from './gif';
|
||||
import { JPEG_HEADER_BYTES, isJpeg } from './jpeg';
|
||||
import { PNG_HEADER_BYTES, isPng } from './png';
|
||||
import { WEBP_HEADER_BYTES, isWebp } from './webp';
|
||||
import { readBytesAsync } from '..';
|
||||
|
||||
export type ImageMimeType = 'image/png' | 'image/jpeg' | 'image/gif' | 'image/webp';
|
||||
|
||||
export const IMAGE_HEADER_BYTES = Math.max(
|
||||
JPEG_HEADER_BYTES,
|
||||
PNG_HEADER_BYTES,
|
||||
GIF_HEADER_BYTES,
|
||||
WEBP_HEADER_BYTES
|
||||
);
|
||||
|
||||
export function detectFileTypeFromHeader(header: Buffer): ImageMimeType | null {
|
||||
if (isPng(header)) {
|
||||
return 'image/png';
|
||||
}
|
||||
if (isJpeg(header)) {
|
||||
return 'image/jpeg';
|
||||
}
|
||||
if (isGif(header)) {
|
||||
return 'image/gif';
|
||||
}
|
||||
if (isWebp(header)) {
|
||||
return 'image/webp';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function detectFileType(file: FileHandle): Promise<ImageMimeType | null> {
|
||||
const header = await readBytesAsync(file, IMAGE_HEADER_BYTES);
|
||||
return detectFileTypeFromHeader(header);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// SOI marker followed by the next marker, ITU-T T.81 B.1.1.3: https://www.w3.org/Graphics/JPEG/itu-t81.pdf (page 32)
|
||||
const JPEG_SIGNATURE = Buffer.from([0xff, 0xd8, 0xff]);
|
||||
export const JPEG_HEADER_BYTES = JPEG_SIGNATURE.byteLength;
|
||||
|
||||
export function isJpeg(header: Buffer): boolean {
|
||||
if (header.byteLength < JPEG_HEADER_BYTES) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return header.subarray(0, JPEG_SIGNATURE.length).equals(JPEG_SIGNATURE);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// https://www.w3.org/TR/png-3/#3PNGsignature
|
||||
const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
||||
export const PNG_HEADER_BYTES = PNG_SIGNATURE.byteLength;
|
||||
|
||||
export function isPng(header: Buffer): boolean {
|
||||
if (header.byteLength < PNG_HEADER_BYTES) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return header.subarray(0, PNG_SIGNATURE.length).equals(PNG_SIGNATURE);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
export const WEBP_HEADER_BYTES = 12;
|
||||
|
||||
// https://www.rfc-editor.org/rfc/rfc9649#section-2.4
|
||||
export function isWebp(header: Buffer): boolean {
|
||||
if (header.byteLength < WEBP_HEADER_BYTES) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return header.toString('latin1', 0, 4) === 'RIFF' && header.toString('latin1', 8, 12) === 'WEBP';
|
||||
}
|
||||
+26
-1
@@ -1,4 +1,4 @@
|
||||
import fsPromises from 'node:fs/promises';
|
||||
import fsPromises, { type FileHandle } from 'node:fs/promises';
|
||||
import * as tar from 'tar';
|
||||
|
||||
export async function decompressTarAsync({
|
||||
@@ -34,3 +34,28 @@ export async function isFileTarGzAsync(path: string): Promise<boolean> {
|
||||
// Source: https://en.wikipedia.org/wiki/Gzip#File_format
|
||||
return buffer[0] === 0x1f && buffer[1] === 0x8b && buffer[2] === 0x08;
|
||||
}
|
||||
|
||||
export async function readBytesAsync(file: FileHandle, length: number): Promise<Buffer> {
|
||||
const { bytesRead, buffer } = await file.read(Buffer.alloc(length), 0, length, 0);
|
||||
return buffer.subarray(0, bytesRead);
|
||||
}
|
||||
|
||||
export async function readTextAsync(
|
||||
file: FileHandle,
|
||||
length: number,
|
||||
isTruncated: boolean
|
||||
): Promise<string | null> {
|
||||
const bytes = await readBytesAsync(file, length);
|
||||
return decodeText(bytes, isTruncated);
|
||||
}
|
||||
|
||||
function decodeText(contents: Buffer, isTruncated: boolean): string | null {
|
||||
try {
|
||||
const text = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(contents, {
|
||||
stream: isTruncated,
|
||||
});
|
||||
return text;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "create-eas-build-function",
|
||||
"version": "24.9.0",
|
||||
"version": "24.13.0",
|
||||
"description": "Create functions for use in EAS Build custom builds.",
|
||||
"keywords": [
|
||||
"expo",
|
||||
@@ -35,13 +35,13 @@
|
||||
"clean": "rimraf ./build/ \"*.tsbuildinfo\""
|
||||
},
|
||||
"dependencies": {
|
||||
"@expo/steps": "24.9.0"
|
||||
"@expo/steps": "24.13.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@expo/package-manager": "1.9.10",
|
||||
"@jest/globals": "^29.7.0",
|
||||
"@types/jest": "^29.5.12",
|
||||
"@types/node": "20.19.43",
|
||||
"@types/node": "22.20.5",
|
||||
"@types/prompts": "^2.4.9",
|
||||
"@vercel/ncc": "^0.38.1",
|
||||
"arg": "^5.0.2",
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@expo/steps": "^1.0.119",
|
||||
"@types/node": "20.19.43",
|
||||
"@types/node": "22.20.5",
|
||||
"@vercel/ncc": "^0.38.1",
|
||||
"typescript": "^6.0.3"
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@expo/downloader",
|
||||
"version": "24.8.0",
|
||||
"version": "24.13.0",
|
||||
"bugs": "https://github.com/expo/eas-cli/issues",
|
||||
"license": "BUSL-1.1",
|
||||
"author": "Expo <[email protected]>",
|
||||
@@ -31,7 +31,7 @@
|
||||
"devDependencies": {
|
||||
"@types/fs-extra": "^11.0.4",
|
||||
"@types/jest": "^29.5.12",
|
||||
"@types/node": "20.19.43",
|
||||
"@types/node": "22.20.5",
|
||||
"jest": "^29.7.0",
|
||||
"rimraf": "3.0.2",
|
||||
"ts-jest": "^29.4.12",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "eas-build-cache-provider",
|
||||
"version": "24.8.0",
|
||||
"version": "24.13.0",
|
||||
"description": "A build cache provider plugin for the Expo CLI",
|
||||
"homepage": "https://github.com/expo/eas-cli",
|
||||
"bugs": "https://github.com/expo/eas-cli/issues",
|
||||
@@ -44,7 +44,7 @@
|
||||
"tslib": "2.4.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@tsconfig/node20": "20.1.8",
|
||||
"@tsconfig/node22": "22.0.6",
|
||||
"@types/babel__code-frame": "7.0.3",
|
||||
"@types/fs-extra": "11.0.4",
|
||||
"@types/getenv": "^1.0.0",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"extends": "@tsconfig/node20/tsconfig.json",
|
||||
"extends": "@tsconfig/node22/tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"declaration": true,
|
||||
"importHelpers": true,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@expo/eas-build-job",
|
||||
"version": "24.9.0",
|
||||
"version": "24.13.0",
|
||||
"bugs": "https://github.com/expo/eas-cli/issues",
|
||||
"license": "MIT",
|
||||
"author": "Expo <[email protected]>",
|
||||
@@ -25,16 +25,16 @@
|
||||
"clean": "rimraf node_modules dist coverage \"*.tsbuildinfo\""
|
||||
},
|
||||
"dependencies": {
|
||||
"@expo/logger": "24.8.0",
|
||||
"@expo/logger": "24.13.0",
|
||||
"@expo/results": "1.0.0",
|
||||
"@expo/turtle-spawn": "24.8.0",
|
||||
"@expo/turtle-spawn": "24.13.0",
|
||||
"joi": "^17.13.1",
|
||||
"semver": "^7.6.2",
|
||||
"zod": "^4.3.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^29.5.12",
|
||||
"@types/node": "20.19.43",
|
||||
"@types/node": "22.20.5",
|
||||
"jest": "^29.7.0",
|
||||
"rimraf": "3.0.2",
|
||||
"ts-jest": "^29.4.12",
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
import { SandboxDaemonCommands, SandboxDaemonRequestZ, SandboxDaemonResponseZ } from '../sandbox';
|
||||
import {
|
||||
SandboxDaemonCommands,
|
||||
SandboxDaemonErrorCode,
|
||||
SandboxDaemonRequestZ,
|
||||
SandboxDaemonResponseZ,
|
||||
} from '../sandbox';
|
||||
|
||||
describe('sandbox daemon protocol', () => {
|
||||
it('validates command parameters', () => {
|
||||
@@ -19,6 +24,32 @@ describe('sandbox daemon protocol', () => {
|
||||
).toEqual({ sessionId: 1, chars: '\u0003', yieldTimeMs: 0 });
|
||||
});
|
||||
|
||||
it.each(['maxTextBytes', 'maxImageBytes'])(
|
||||
'requires the caller to set %s when reading a file',
|
||||
limit => {
|
||||
const params: Record<string, unknown> = {
|
||||
path: 'screenshot.png',
|
||||
maxTextBytes: 40_000,
|
||||
maxImageBytes: 3_000_000,
|
||||
};
|
||||
delete params[limit];
|
||||
|
||||
expect(() => SandboxDaemonCommands.readFile.params.parse(params)).toThrow();
|
||||
}
|
||||
);
|
||||
|
||||
it('rejects an image result with both data and an error', () => {
|
||||
expect(() =>
|
||||
SandboxDaemonCommands.readFile.result.parse({
|
||||
kind: 'image',
|
||||
mimeType: 'image/png',
|
||||
data: 'iVBORw0KGgo=',
|
||||
size: 8,
|
||||
error: 'tooLarge',
|
||||
})
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ output: '', wallTimeSeconds: 0, exitCode: 0 },
|
||||
{ output: '', wallTimeSeconds: 0, terminationSignal: 'SIGTERM' },
|
||||
@@ -34,6 +65,46 @@ describe('sandbox daemon protocol', () => {
|
||||
expect(() => SandboxDaemonCommands.writeStdin.result.parse(result)).toThrow();
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ path: '', name: 'Crash log' },
|
||||
{ path: 'crash.log', name: '' },
|
||||
{ path: 'crash.log', name: 'a'.repeat(1025) },
|
||||
{ path: 'crash.log', name: 'Crash log', yieldTimeMs: 30_001 },
|
||||
])('rejects invalid artifact upload parameters', params => {
|
||||
expect(() => SandboxDaemonCommands.uploadArtifact.params.parse(params)).toThrow();
|
||||
});
|
||||
|
||||
it('accepts a valid artifact name', () => {
|
||||
const params = { path: 'crash.log', name: 'a'.repeat(1024) };
|
||||
expect(SandboxDaemonCommands.uploadArtifact.params.parse(params)).toEqual(params);
|
||||
});
|
||||
|
||||
it('rejects an artifact upload result without a status, with a non-UUID id or extra fields', () => {
|
||||
const id = '0199c0de-7b3a-7c1e-8f00-1234567890ab';
|
||||
const result = { id, status: 'uploading' };
|
||||
expect(SandboxDaemonCommands.uploadArtifact.result.parse(result)).toEqual(result);
|
||||
expect(() => SandboxDaemonCommands.uploadArtifact.result.parse({ id })).toThrow();
|
||||
expect(() =>
|
||||
SandboxDaemonCommands.uploadArtifact.result.parse({ ...result, id: 'artifact' })
|
||||
).toThrow();
|
||||
expect(() =>
|
||||
SandboxDaemonCommands.uploadArtifact.result.parse({ ...result, url: 'https://r2.test' })
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
it('requires a download URL exactly when an artifact upload result is uploaded', () => {
|
||||
const id = '0199c0de-7b3a-7c1e-8f00-1234567890ab';
|
||||
const downloadUrl = 'https://r2.test/artifact?X-Amz-Signature=signature';
|
||||
const uploaded = { id, status: 'uploaded', downloadUrl };
|
||||
expect(SandboxDaemonCommands.uploadArtifact.result.parse(uploaded)).toEqual(uploaded);
|
||||
expect(() =>
|
||||
SandboxDaemonCommands.uploadArtifact.result.parse({ id, status: 'uploaded' })
|
||||
).toThrow();
|
||||
expect(() =>
|
||||
SandboxDaemonCommands.uploadArtifact.result.parse({ id, status: 'failed', downloadUrl })
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
it('validates success and error response envelopes', () => {
|
||||
expect(
|
||||
SandboxDaemonRequestZ.parse({
|
||||
@@ -63,4 +134,14 @@ describe('sandbox daemon protocol', () => {
|
||||
error: { code: -32603, message: 'Command failed' },
|
||||
});
|
||||
});
|
||||
|
||||
it('accepts an error code that this version does not define', () => {
|
||||
const response = {
|
||||
jsonrpc: '2.0',
|
||||
id: 'request-id',
|
||||
error: { code: 99, message: 'Added by a newer daemon' },
|
||||
};
|
||||
expect(Object.values(SandboxDaemonErrorCode)).not.toContain(99);
|
||||
expect(SandboxDaemonResponseZ.parse(response)).toEqual(response);
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,7 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const SandboxImageMimeTypeZ = z.enum(['image/png', 'image/jpeg', 'image/gif', 'image/webp']);
|
||||
|
||||
export const SandboxDaemonCommands = {
|
||||
execCommand: {
|
||||
params: z.object({
|
||||
@@ -50,6 +52,54 @@ export const SandboxDaemonCommands = {
|
||||
}),
|
||||
]),
|
||||
},
|
||||
readFile: {
|
||||
params: z.object({
|
||||
path: z.string().min(1),
|
||||
maxTextBytes: z.number().int().positive(),
|
||||
maxImageBytes: z.number().int().positive(),
|
||||
}),
|
||||
result: z.union([
|
||||
z.strictObject({
|
||||
kind: z.literal('image'),
|
||||
mimeType: SandboxImageMimeTypeZ,
|
||||
data: z.base64(),
|
||||
}),
|
||||
z.strictObject({
|
||||
kind: z.literal('image'),
|
||||
mimeType: SandboxImageMimeTypeZ,
|
||||
error: z.literal('tooLarge'),
|
||||
size: z.number().int().nonnegative(),
|
||||
}),
|
||||
z.strictObject({
|
||||
kind: z.literal('text'),
|
||||
text: z.string(),
|
||||
truncated: z.boolean(),
|
||||
size: z.number().int().nonnegative(),
|
||||
}),
|
||||
z.strictObject({
|
||||
kind: z.literal('binary'),
|
||||
size: z.number().int().nonnegative(),
|
||||
}),
|
||||
]),
|
||||
},
|
||||
uploadArtifact: {
|
||||
params: z.object({
|
||||
path: z.string().min(1),
|
||||
name: z.string().min(1).max(1024),
|
||||
yieldTimeMs: z.number().int().min(0).max(30_000).optional(),
|
||||
}),
|
||||
result: z.union([
|
||||
z.strictObject({
|
||||
id: z.uuid(),
|
||||
status: z.literal('uploaded'),
|
||||
downloadUrl: z.url(),
|
||||
}),
|
||||
z.strictObject({
|
||||
id: z.uuid(),
|
||||
status: z.enum(['uploading', 'failed']),
|
||||
}),
|
||||
]),
|
||||
},
|
||||
} as const satisfies Record<string, { params: z.ZodType; result: z.ZodType }>;
|
||||
|
||||
export type SandboxDaemonMethod = keyof typeof SandboxDaemonCommands;
|
||||
@@ -84,3 +134,25 @@ export const SandboxDaemonResponseZ = z.union([
|
||||
]);
|
||||
|
||||
export type SandboxDaemonResponse = z.output<typeof SandboxDaemonResponseZ>;
|
||||
|
||||
export enum SandboxDaemonErrorCode {
|
||||
PARSE_ERROR = -32700,
|
||||
INVALID_REQUEST = -32600,
|
||||
METHOD_NOT_FOUND = -32601,
|
||||
INVALID_PARAMS = -32602,
|
||||
INTERNAL_ERROR = -32603,
|
||||
BAD_REQUEST = 1,
|
||||
}
|
||||
|
||||
export type SandboxDaemonErrorCodeName = keyof typeof SandboxDaemonErrorCode;
|
||||
|
||||
export class SandboxDaemonError extends Error {
|
||||
constructor(
|
||||
public readonly code: (typeof SandboxDaemonErrorCode)[SandboxDaemonErrorCodeName],
|
||||
message: string,
|
||||
options?: ErrorOptions
|
||||
) {
|
||||
super(message, options);
|
||||
this.name = 'SandboxDaemonError';
|
||||
}
|
||||
}
|
||||
@@ -299,4 +299,4 @@ describe('MyCommand', () => {
|
||||
- **README**: Auto-generated from oclif - update via `yarn oclif readme`
|
||||
- **Manifest**: Generated during `prepack` step
|
||||
- **Templates**: `src/commandUtils/new/templates` are copied to `build/` during build
|
||||
- **Node Version**: ^20.18.3 or >= 22.0.0
|
||||
- **Node Version**: Develop on Node 22. Users can run Node ^20.18.3 (deprecated, `bin/run` prints a warning) or >= 22.0.0
|
||||
+144
-144
File diff suppressed because it is too large.
Load diff
@@ -1,5 +1,13 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
if (require('semver').lt(process.version, '22.0.0')) {
|
||||
console.warn(
|
||||
require('chalk').yellow(
|
||||
`Node.js ${process.version} is deprecated. A future release of EAS CLI will require Node.js 22 or newer.`
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
(async () => {
|
||||
const oclif = require('@oclif/core');
|
||||
await oclif.execute({ dir: __dirname });
|
||||
|
||||
Generated
+358
@@ -52552,6 +52552,12 @@
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "SIMULATOR",
|
||||
"description": null,
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "UPDATES",
|
||||
"description": null,
|
||||
@@ -52617,6 +52623,12 @@
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "SIMULATOR_USAGE",
|
||||
"description": null,
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "UNIQUE_UPDATERS",
|
||||
"description": null,
|
||||
@@ -52703,6 +52715,12 @@
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "MINUTE",
|
||||
"description": null,
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "REQUEST",
|
||||
"description": null,
|
||||
@@ -59468,6 +59486,18 @@
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "jobTypeBreakdown",
|
||||
"description": null,
|
||||
"args": [],
|
||||
"type": {
|
||||
"kind": "OBJECT",
|
||||
"name": "EstimatedUsageJobTypeBreakdown",
|
||||
"ofType": null
|
||||
},
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "limit",
|
||||
"description": null,
|
||||
@@ -59566,6 +59596,65 @@
|
||||
"enumValues": null,
|
||||
"possibleTypes": null
|
||||
},
|
||||
{
|
||||
"kind": "OBJECT",
|
||||
"name": "EstimatedUsageJobTypeBreakdown",
|
||||
"description": null,
|
||||
"fields": [
|
||||
{
|
||||
"name": "other",
|
||||
"description": null,
|
||||
"args": [],
|
||||
"type": {
|
||||
"kind": "NON_NULL",
|
||||
"name": null,
|
||||
"ofType": {
|
||||
"kind": "SCALAR",
|
||||
"name": "Float",
|
||||
"ofType": null
|
||||
}
|
||||
},
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "simulator",
|
||||
"description": null,
|
||||
"args": [],
|
||||
"type": {
|
||||
"kind": "NON_NULL",
|
||||
"name": null,
|
||||
"ofType": {
|
||||
"kind": "SCALAR",
|
||||
"name": "Float",
|
||||
"ofType": null
|
||||
}
|
||||
},
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "workflows",
|
||||
"description": null,
|
||||
"args": [],
|
||||
"type": {
|
||||
"kind": "NON_NULL",
|
||||
"name": null,
|
||||
"ofType": {
|
||||
"kind": "SCALAR",
|
||||
"name": "Float",
|
||||
"ofType": null
|
||||
}
|
||||
},
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
}
|
||||
],
|
||||
"inputFields": null,
|
||||
"interfaces": [],
|
||||
"enumValues": null,
|
||||
"possibleTypes": null
|
||||
},
|
||||
{
|
||||
"kind": "OBJECT",
|
||||
"name": "EstimatedUsagePlatformBreakdown",
|
||||
@@ -77368,6 +77457,22 @@
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "usageBudget",
|
||||
"description": null,
|
||||
"args": [],
|
||||
"type": {
|
||||
"kind": "NON_NULL",
|
||||
"name": null,
|
||||
"ofType": {
|
||||
"kind": "OBJECT",
|
||||
"name": "UsageBudgetQuery",
|
||||
"ofType": null
|
||||
}
|
||||
},
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "userActorPublicData",
|
||||
"description": "Top-level query object for querying UserActorPublicData publicly.",
|
||||
@@ -82786,6 +82891,39 @@
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "generateSubmissionArchiveDownloadUrl",
|
||||
"description": null,
|
||||
"args": [
|
||||
{
|
||||
"name": "submissionId",
|
||||
"description": null,
|
||||
"type": {
|
||||
"kind": "NON_NULL",
|
||||
"name": null,
|
||||
"ofType": {
|
||||
"kind": "SCALAR",
|
||||
"name": "ID",
|
||||
"ofType": null
|
||||
}
|
||||
},
|
||||
"defaultValue": null,
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
}
|
||||
],
|
||||
"type": {
|
||||
"kind": "NON_NULL",
|
||||
"name": null,
|
||||
"ofType": {
|
||||
"kind": "SCALAR",
|
||||
"name": "String",
|
||||
"ofType": null
|
||||
}
|
||||
},
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "retrySubmission",
|
||||
"description": "Retry an EAS Submit submission",
|
||||
@@ -90350,6 +90488,22 @@
|
||||
},
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "remaining",
|
||||
"description": null,
|
||||
"args": [],
|
||||
"type": {
|
||||
"kind": "NON_NULL",
|
||||
"name": null,
|
||||
"ofType": {
|
||||
"kind": "OBJECT",
|
||||
"name": "UsageBudgetRemaining",
|
||||
"ofType": null
|
||||
}
|
||||
},
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
}
|
||||
],
|
||||
"inputFields": null,
|
||||
@@ -90471,6 +90625,85 @@
|
||||
"enumValues": null,
|
||||
"possibleTypes": null
|
||||
},
|
||||
{
|
||||
"kind": "OBJECT",
|
||||
"name": "UsageBudgetQuery",
|
||||
"description": null,
|
||||
"fields": [
|
||||
{
|
||||
"name": "byId",
|
||||
"description": null,
|
||||
"args": [
|
||||
{
|
||||
"name": "id",
|
||||
"description": null,
|
||||
"type": {
|
||||
"kind": "NON_NULL",
|
||||
"name": null,
|
||||
"ofType": {
|
||||
"kind": "SCALAR",
|
||||
"name": "ID",
|
||||
"ofType": null
|
||||
}
|
||||
},
|
||||
"defaultValue": null,
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
}
|
||||
],
|
||||
"type": {
|
||||
"kind": "NON_NULL",
|
||||
"name": null,
|
||||
"ofType": {
|
||||
"kind": "OBJECT",
|
||||
"name": "UsageBudget",
|
||||
"ofType": null
|
||||
}
|
||||
},
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
}
|
||||
],
|
||||
"inputFields": null,
|
||||
"interfaces": [],
|
||||
"enumValues": null,
|
||||
"possibleTypes": null
|
||||
},
|
||||
{
|
||||
"kind": "OBJECT",
|
||||
"name": "UsageBudgetRemaining",
|
||||
"description": null,
|
||||
"fields": [
|
||||
{
|
||||
"name": "builds",
|
||||
"description": "Available builds, or null when there is no build limit.",
|
||||
"args": [],
|
||||
"type": {
|
||||
"kind": "SCALAR",
|
||||
"name": "Int",
|
||||
"ofType": null
|
||||
},
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "simulatorMinutes",
|
||||
"description": "Available minutes after completed usage and active reservations, or null when unlimited.",
|
||||
"args": [],
|
||||
"type": {
|
||||
"kind": "SCALAR",
|
||||
"name": "Float",
|
||||
"ofType": null
|
||||
},
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
}
|
||||
],
|
||||
"inputFields": null,
|
||||
"interfaces": [],
|
||||
"enumValues": null,
|
||||
"possibleTypes": null
|
||||
},
|
||||
{
|
||||
"kind": "OBJECT",
|
||||
"name": "UsageMetricTotal",
|
||||
@@ -104340,6 +104573,12 @@
|
||||
"inputFields": null,
|
||||
"interfaces": null,
|
||||
"enumValues": [
|
||||
{
|
||||
"name": "AGENT",
|
||||
"description": null,
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "APPLE_DEVICE_REGISTRATION_REQUEST",
|
||||
"description": null,
|
||||
@@ -105586,6 +105825,11 @@
|
||||
"kind": "OBJECT",
|
||||
"name": "WorkflowRunCancelReasonConcurrencyGroup",
|
||||
"ofType": null
|
||||
},
|
||||
{
|
||||
"kind": "OBJECT",
|
||||
"name": "WorkflowRunCancelReasonWorkflowCall",
|
||||
"ofType": null
|
||||
}
|
||||
]
|
||||
},
|
||||
@@ -105612,6 +105856,29 @@
|
||||
"enumValues": null,
|
||||
"possibleTypes": null
|
||||
},
|
||||
{
|
||||
"kind": "OBJECT",
|
||||
"name": "WorkflowRunCancelReasonWorkflowCall",
|
||||
"description": null,
|
||||
"fields": [
|
||||
{
|
||||
"name": "parentWorkflowRun",
|
||||
"description": "The run that called this run through workflow_call and whose cancellation\ncanceled this run. Null if that run no longer exists.",
|
||||
"args": [],
|
||||
"type": {
|
||||
"kind": "OBJECT",
|
||||
"name": "WorkflowRun",
|
||||
"ofType": null
|
||||
},
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
}
|
||||
],
|
||||
"inputFields": null,
|
||||
"interfaces": [],
|
||||
"enumValues": null,
|
||||
"possibleTypes": null
|
||||
},
|
||||
{
|
||||
"kind": "OBJECT",
|
||||
"name": "WorkflowRunEdge",
|
||||
@@ -106787,6 +107054,41 @@
|
||||
"enumValues": null,
|
||||
"possibleTypes": null
|
||||
},
|
||||
{
|
||||
"kind": "OBJECT",
|
||||
"name": "WorkflowsInsightsNullableMetric",
|
||||
"description": "Same shape as WorkflowsInsightsMetric but nullable on both sides. Null means the window had\nno qualifying run, which is different from 0.",
|
||||
"fields": [
|
||||
{
|
||||
"name": "currentValue",
|
||||
"description": null,
|
||||
"args": [],
|
||||
"type": {
|
||||
"kind": "SCALAR",
|
||||
"name": "Float",
|
||||
"ofType": null
|
||||
},
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "previousValue",
|
||||
"description": null,
|
||||
"args": [],
|
||||
"type": {
|
||||
"kind": "SCALAR",
|
||||
"name": "Float",
|
||||
"ofType": null
|
||||
},
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
}
|
||||
],
|
||||
"inputFields": null,
|
||||
"interfaces": [],
|
||||
"enumValues": null,
|
||||
"possibleTypes": null
|
||||
},
|
||||
{
|
||||
"kind": "OBJECT",
|
||||
"name": "WorkflowsInsightsOverviewMetrics",
|
||||
@@ -106808,6 +107110,22 @@
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "avgDurationMs",
|
||||
"description": "Mean milliseconds from run creation to finalization, the same duration the run page shows,\nso queue and approval wait count. Canceled runs are excluded.",
|
||||
"args": [],
|
||||
"type": {
|
||||
"kind": "NON_NULL",
|
||||
"name": null,
|
||||
"ofType": {
|
||||
"kind": "OBJECT",
|
||||
"name": "WorkflowsInsightsNullableMetric",
|
||||
"ofType": null
|
||||
}
|
||||
},
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "canceledRuns",
|
||||
"description": null,
|
||||
@@ -106840,6 +107158,22 @@
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "p75DurationMs",
|
||||
"description": "Approximate 75th percentile of the same duration, excluding canceled runs.",
|
||||
"args": [],
|
||||
"type": {
|
||||
"kind": "NON_NULL",
|
||||
"name": null,
|
||||
"ofType": {
|
||||
"kind": "OBJECT",
|
||||
"name": "WorkflowsInsightsNullableMetric",
|
||||
"ofType": null
|
||||
}
|
||||
},
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "successfulRuns",
|
||||
"description": null,
|
||||
@@ -107163,6 +107497,18 @@
|
||||
"name": "WorkflowsInsightsWorkflowNode",
|
||||
"description": null,
|
||||
"fields": [
|
||||
{
|
||||
"name": "avgDurationMs",
|
||||
"description": null,
|
||||
"args": [],
|
||||
"type": {
|
||||
"kind": "SCALAR",
|
||||
"name": "Float",
|
||||
"ofType": null
|
||||
},
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "canceledRuns",
|
||||
"description": null,
|
||||
@@ -107227,6 +107573,18 @@
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "p75DurationMs",
|
||||
"description": null,
|
||||
"args": [],
|
||||
"type": {
|
||||
"kind": "SCALAR",
|
||||
"name": "Float",
|
||||
"ofType": null
|
||||
},
|
||||
"isDeprecated": false,
|
||||
"deprecationReason": null
|
||||
},
|
||||
{
|
||||
"name": "successfulRuns",
|
||||
"description": null,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "eas-cli",
|
||||
"version": "24.12.1",
|
||||
"version": "24.13.0",
|
||||
"description": "EAS command line tool",
|
||||
"keywords": [
|
||||
"cli",
|
||||
@@ -48,11 +48,11 @@
|
||||
"@expo/code-signing-certificates": "0.0.5",
|
||||
"@expo/config": "55.0.10",
|
||||
"@expo/config-plugins": "55.0.7",
|
||||
"@expo/eas-build-job": "24.9.0",
|
||||
"@expo/eas-json": "24.9.0",
|
||||
"@expo/eas-build-job": "24.13.0",
|
||||
"@expo/eas-json": "24.13.0",
|
||||
"@expo/env": "^1.0.0",
|
||||
"@expo/json-file": "8.3.3",
|
||||
"@expo/logger": "24.8.0",
|
||||
"@expo/logger": "24.13.0",
|
||||
"@expo/multipart-body-parser": "2.0.0",
|
||||
"@expo/osascript": "2.1.4",
|
||||
"@expo/package-manager": "1.9.10",
|
||||
@@ -64,7 +64,7 @@
|
||||
"@expo/results": "1.0.0",
|
||||
"@expo/rudder-sdk-node": "1.1.1",
|
||||
"@expo/spawn-async": "1.7.2",
|
||||
"@expo/steps": "24.9.0",
|
||||
"@expo/steps": "24.13.0",
|
||||
"@expo/timeago.js": "1.0.0",
|
||||
"@oclif/core": "^4.8.3",
|
||||
"@oclif/plugin-autocomplete": "^3.2.40",
|
||||
@@ -142,7 +142,7 @@
|
||||
"@graphql-codegen/introspection": "5.0.2",
|
||||
"@graphql-codegen/typescript": "5.0.10",
|
||||
"@graphql-codegen/typescript-operations": "5.1.0",
|
||||
"@tsconfig/node20": "20.1.8",
|
||||
"@tsconfig/node22": "22.0.6",
|
||||
"@types/cli-progress": "3.11.5",
|
||||
"@types/dateformat": "3.0.1",
|
||||
"@types/debug": "4.1.13",
|
||||
@@ -152,7 +152,7 @@
|
||||
"@types/getenv": "^1.0.0",
|
||||
"@types/invariant": "^2.2.37",
|
||||
"@types/mime": "3.0.1",
|
||||
"@types/node": "20.19.43",
|
||||
"@types/node": "22.20.5",
|
||||
"@types/node-fetch": "2.6.12",
|
||||
"@types/node-forge": "1.3.1",
|
||||
"@types/pngjs": "6.0.4",
|
||||
@@ -250,7 +250,7 @@
|
||||
},
|
||||
"update": {
|
||||
"node": {
|
||||
"version": "20.19.4"
|
||||
"version": "22.23.3"
|
||||
},
|
||||
"s3": {
|
||||
"templates": {
|
||||
|
||||
@@ -57,7 +57,6 @@ import {
|
||||
isExpoUpdatesInstalledAsDevDependency,
|
||||
isExpoUpdatesInstalledOrAvailable,
|
||||
isUsingEASUpdate,
|
||||
validateAppVersionRuntimePolicySupportAsync,
|
||||
} from '../project/projectUtils';
|
||||
import {
|
||||
ensureAppVersionSourceIsSetAsync,
|
||||
@@ -481,7 +480,6 @@ async function prepareAndStartBuildAsync({
|
||||
}
|
||||
}
|
||||
|
||||
await validateAppVersionRuntimePolicySupportAsync(buildCtx.projectDir, buildCtx.exp);
|
||||
if (
|
||||
easJsonCliConfig?.appVersionSource === undefined &&
|
||||
buildProfile.profile.autoIncrement !== 'version'
|
||||
|
||||
@@ -39,6 +39,16 @@ const appResponse: any = {
|
||||
successfulRuns: metric(75, 64),
|
||||
failedRuns: metric(20, 10),
|
||||
activeWorkflows: metric(3, 3),
|
||||
avgDurationMs: {
|
||||
__typename: 'WorkflowsInsightsNullableMetric',
|
||||
currentValue: 600000,
|
||||
previousValue: 660000,
|
||||
},
|
||||
p75DurationMs: {
|
||||
__typename: 'WorkflowsInsightsNullableMetric',
|
||||
currentValue: 900000,
|
||||
previousValue: null,
|
||||
},
|
||||
},
|
||||
runsOverTime: {
|
||||
__typename: 'WorkflowsInsightsRunsOverTimeData',
|
||||
@@ -72,6 +82,8 @@ const appResponse: any = {
|
||||
successfulRuns: 75,
|
||||
failedRuns: 20,
|
||||
canceledRuns: 5,
|
||||
avgDurationMs: 600000,
|
||||
p75DurationMs: 900000,
|
||||
lastRunAt: '2026-09-01T10:00:00.000Z',
|
||||
},
|
||||
},
|
||||
@@ -273,6 +285,8 @@ describe(WorkflowInsights, () => {
|
||||
});
|
||||
expect(json.overview.totalRuns).toEqual({ current: 100, previous: 80 });
|
||||
expect(json.overview.successRatePercent).toEqual({ current: 75, previous: 80 });
|
||||
expect(json.overview.avgDurationMs).toEqual({ current: 600000, previous: 660000 });
|
||||
expect(json.overview.p75DurationMs).toEqual({ current: 900000, previous: null });
|
||||
expect(json.runsOverTime.granularity).toBe('DAY');
|
||||
expect(json.workflows[0].name).toBe('Build');
|
||||
expect(json.workflows[0].fileName).toBe('build.yml');
|
||||
|
||||
@@ -31,7 +31,7 @@ const MAX_WORKFLOWS_LIMIT = 100;
|
||||
|
||||
export default class WorkflowInsights extends EasCommand {
|
||||
static override description =
|
||||
'display run counts, success rate, and per-workflow trends for a time range';
|
||||
'display run counts, success rate, run durations, and per-workflow trends for a time range';
|
||||
|
||||
static override flags = {
|
||||
...WorkflowsInsightsSharedFilterFlags,
|
||||
|
||||
@@ -6,7 +6,6 @@ import { UserRole } from '@expo/apple-utils';
|
||||
|
||||
import { formatAppleTeam } from './AppleTeamFormatting';
|
||||
import { ExpoGraphqlClient } from '../../../commandUtils/context/contextUtils/createGraphqlClient';
|
||||
import env from '../../../env';
|
||||
import { AccountFragment, AppStoreConnectApiKeyFragment } from '../../../graphql/generated';
|
||||
import { AppStoreConnectApiKeyQuery } from '../../../graphql/queries/AppStoreConnectApiKeyQuery';
|
||||
import Log, { learnMore } from '../../../log';
|
||||
@@ -41,10 +40,7 @@ export async function promptForAscApiKeyPathAsync(
|
||||
): Promise<AscApiKeyPath> {
|
||||
// Individual keys are valid only as submission keys. Every other purpose
|
||||
// requires a team key, so the key type question is not asked there.
|
||||
// The option stays behind an env var until every server-side submission
|
||||
// path runs a fastlane version that accepts keys without an issuer ID.
|
||||
const isIndividualKey =
|
||||
!!env.enableIndividualAscApiKeys &&
|
||||
purpose === AppStoreApiKeyPurpose.SUBMISSION_SERVICE &&
|
||||
!ctx.nonInteractive &&
|
||||
(await promptForAscApiKeyTypeIsIndividualAsync());
|
||||
@@ -113,6 +109,11 @@ export async function provideOrGenerateAscApiKeyAsync(
|
||||
return await generateAscApiKeyAsync(ctx, purpose);
|
||||
}
|
||||
|
||||
// The validation below supports team keys only.
|
||||
if (!userProvided.issuerId) {
|
||||
return userProvided;
|
||||
}
|
||||
|
||||
if (!ctx.appStore.authCtx) {
|
||||
Log.warn('Unable to validate App Store Connect API Key, you are not authenticated with Apple.');
|
||||
return userProvided;
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import { UserRole } from '@expo/apple-utils';
|
||||
import fs from 'fs-extra';
|
||||
|
||||
import env from '../../../../env';
|
||||
import { AppStoreConnectApiKeyQuery } from '../../../../graphql/queries/AppStoreConnectApiKeyQuery';
|
||||
import Log from '../../../../log';
|
||||
import { promptAsync, selectAsync } from '../../../../prompts';
|
||||
import { confirmAsync, promptAsync, selectAsync } from '../../../../prompts';
|
||||
import { getAppstoreMock, testAuthCtx } from '../../../__tests__/fixtures-appstore';
|
||||
import { createCtxMock } from '../../../__tests__/fixtures-context';
|
||||
import { testAscApiKey } from '../../../__tests__/fixtures-ios';
|
||||
@@ -47,6 +46,7 @@ function enumKeys<O extends object, K extends keyof O = keyof O>(obj: O): K[] {
|
||||
afterEach(() => {
|
||||
jest.mocked(promptAsync).mockClear();
|
||||
jest.mocked(selectAsync).mockClear();
|
||||
jest.mocked(confirmAsync).mockClear();
|
||||
jest.mocked(getCredentialsFromUserAsync).mockClear();
|
||||
jest.mocked(shouldAutoGenerateCredentialsAsync).mockClear();
|
||||
jest.mocked(fs.readFile).mockClear();
|
||||
@@ -85,13 +85,6 @@ describe(getAscApiKeyName, () => {
|
||||
});
|
||||
|
||||
describe(promptForAscApiKeyPathAsync, () => {
|
||||
beforeEach(() => {
|
||||
env.enableIndividualAscApiKeys = '1';
|
||||
});
|
||||
afterEach(() => {
|
||||
env.enableIndividualAscApiKeys = undefined;
|
||||
});
|
||||
|
||||
it('prompts for keyId, keyP8Path and issuerId when user is not authenticated to Apple', async () => {
|
||||
jest.mocked(selectAsync).mockResolvedValueOnce(false); // team key
|
||||
jest.mocked(promptAsync).mockImplementationOnce(async () => ({
|
||||
@@ -208,35 +201,6 @@ describe(promptForAscApiKeyPathAsync, () => {
|
||||
});
|
||||
expect(selectAsync).not.toHaveBeenCalled();
|
||||
});
|
||||
it('does not ask the key type in the submission flow when individual keys are not enabled', async () => {
|
||||
env.enableIndividualAscApiKeys = undefined;
|
||||
jest.mocked(promptAsync).mockImplementationOnce(async () => ({
|
||||
keyP8Path: '/asc-api-key.p8',
|
||||
}));
|
||||
jest.mocked(getCredentialsFromUserAsync).mockImplementation(async () => ({
|
||||
keyId: 'test-key-id',
|
||||
issuerId: 'test-issuer-id',
|
||||
}));
|
||||
const ctx = createCtxMock({
|
||||
nonInteractive: false,
|
||||
appStore: {
|
||||
...getAppstoreMock(),
|
||||
authCtx: null,
|
||||
},
|
||||
});
|
||||
const ascApiKeyPath = await promptForAscApiKeyPathAsync(
|
||||
ctx,
|
||||
AppStoreApiKeyPurpose.SUBMISSION_SERVICE
|
||||
);
|
||||
expect(ascApiKeyPath).toEqual({
|
||||
keyId: 'test-key-id',
|
||||
issuerId: 'test-issuer-id',
|
||||
keyP8Path: '/asc-api-key.p8',
|
||||
});
|
||||
expect(selectAsync).not.toHaveBeenCalled();
|
||||
expect(promptAsync).toHaveBeenCalledTimes(1); // keyP8Path
|
||||
expect(getCredentialsFromUserAsync).toHaveBeenCalledTimes(2); // keyId, issuerId
|
||||
});
|
||||
});
|
||||
|
||||
describe(provideOrGenerateAscApiKeyAsync, () => {
|
||||
@@ -318,6 +282,39 @@ describe(provideOrGenerateAscApiKeyAsync, () => {
|
||||
expect(selectAsync).not.toHaveBeenCalled();
|
||||
expect(createAscApiKeyAsync).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('accepts a user-provided individual key without validating it on Apple servers', async () => {
|
||||
const logWarnSpy = jest.spyOn(Log, 'warn').mockImplementation(() => {});
|
||||
jest.mocked(shouldAutoGenerateCredentialsAsync).mockResolvedValue(false);
|
||||
jest.mocked(selectAsync).mockResolvedValueOnce(true); // individual key
|
||||
jest.mocked(promptAsync).mockResolvedValue({ keyP8Path: '/asc-api-key.p8' });
|
||||
jest.mocked(fs.readFile).mockImplementation(async () => 'test-key-p8' as any);
|
||||
jest.mocked(getCredentialsFromUserAsync).mockResolvedValueOnce({ keyId: 'test-key-id' });
|
||||
|
||||
const getAscApiKeyAsync = jest.fn(async () => null);
|
||||
const ctx = createCtxMock({
|
||||
nonInteractive: false,
|
||||
appStore: {
|
||||
...getAppstoreMock(),
|
||||
authCtx: testAuthCtx,
|
||||
getAscApiKeyAsync,
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await provideOrGenerateAscApiKeyAsync(
|
||||
ctx,
|
||||
AppStoreApiKeyPurpose.SUBMISSION_SERVICE
|
||||
);
|
||||
|
||||
expect(result).toEqual({ keyP8: 'test-key-p8', keyId: 'test-key-id' });
|
||||
expect(getAscApiKeyAsync).not.toHaveBeenCalled();
|
||||
expect(confirmAsync).not.toHaveBeenCalled();
|
||||
expect(logWarnSpy).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
logWarnSpy.mockRestore();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe(tryAuthenticateAppStoreWithEasAscApiKeyAsync, () => {
|
||||
|
||||
@@ -21,8 +21,4 @@ export default {
|
||||
* Comma separated list of feature gate keys of feature gates to evaluate override to false.
|
||||
*/
|
||||
featureGateDisable: process.env.EAS_FG_DISABLE,
|
||||
/**
|
||||
* Enables the individual App Store Connect API key option in the interactive submission key prompt.
|
||||
*/
|
||||
enableIndividualAscApiKeys: process.env.EAS_ENABLE_INDIVIDUAL_ASC_API_KEYS,
|
||||
};
|
||||
+68
-2
@@ -7482,6 +7482,7 @@ export enum EasService {
|
||||
Jobs = 'JOBS',
|
||||
Mcp = 'MCP',
|
||||
Observe = 'OBSERVE',
|
||||
Simulator = 'SIMULATOR',
|
||||
Updates = 'UPDATES'
|
||||
}
|
||||
|
||||
@@ -7494,6 +7495,7 @@ export enum EasServiceMetric {
|
||||
McpRequests = 'MCP_REQUESTS',
|
||||
ObserveEvents = 'OBSERVE_EVENTS',
|
||||
RunTime = 'RUN_TIME',
|
||||
SimulatorUsage = 'SIMULATOR_USAGE',
|
||||
UniqueUpdaters = 'UNIQUE_UPDATERS',
|
||||
UniqueUsers = 'UNIQUE_USERS'
|
||||
}
|
||||
@@ -7509,6 +7511,7 @@ export enum EasTotalPlanEnablementUnit {
|
||||
Byte = 'BYTE',
|
||||
Concurrency = 'CONCURRENCY',
|
||||
Event = 'EVENT',
|
||||
Minute = 'MINUTE',
|
||||
Request = 'REQUEST',
|
||||
Updater = 'UPDATER',
|
||||
User = 'USER'
|
||||
@@ -8531,6 +8534,7 @@ export type EstimatedOverageAndCost = {
|
||||
export type EstimatedUsage = {
|
||||
__typename?: 'EstimatedUsage';
|
||||
id: Scalars['ID']['output'];
|
||||
jobTypeBreakdown?: Maybe<EstimatedUsageJobTypeBreakdown>;
|
||||
limit: Scalars['Float']['output'];
|
||||
metricType: UsageMetricType;
|
||||
platformBreakdown?: Maybe<EstimatedUsagePlatformBreakdown>;
|
||||
@@ -8539,6 +8543,13 @@ export type EstimatedUsage = {
|
||||
value: Scalars['Float']['output'];
|
||||
};
|
||||
|
||||
export type EstimatedUsageJobTypeBreakdown = {
|
||||
__typename?: 'EstimatedUsageJobTypeBreakdown';
|
||||
other: Scalars['Float']['output'];
|
||||
simulator: Scalars['Float']['output'];
|
||||
workflows: Scalars['Float']['output'];
|
||||
};
|
||||
|
||||
export type EstimatedUsagePlatformBreakdown = {
|
||||
__typename?: 'EstimatedUsagePlatformBreakdown';
|
||||
android: EstimatedUsagePlatformDetail;
|
||||
@@ -11029,6 +11040,7 @@ export type RootQuery = {
|
||||
updates: UpdateQuery;
|
||||
/** fetch all updates in a group */
|
||||
updatesByGroup: Array<Update>;
|
||||
usageBudget: UsageBudgetQuery;
|
||||
/** Top-level query object for querying UserActorPublicData publicly. */
|
||||
userActorPublicData: UserActorPublicDataQuery;
|
||||
/** Top-level query object for querying User Audit Logs. */
|
||||
@@ -11812,6 +11824,7 @@ export type SubmissionMutation = {
|
||||
createAndroidSubmission: CreateSubmissionResult;
|
||||
/** Create an iOS EAS Submit submission */
|
||||
createIosSubmission: CreateSubmissionResult;
|
||||
generateSubmissionArchiveDownloadUrl: Scalars['String']['output'];
|
||||
/** Retry an EAS Submit submission */
|
||||
retrySubmission: CreateSubmissionResult;
|
||||
};
|
||||
@@ -11832,6 +11845,11 @@ export type SubmissionMutation_CreateIosSubmissionArgs = {
|
||||
};
|
||||
|
||||
|
||||
export type SubmissionMutation_GenerateSubmissionArchiveDownloadUrlArgs = {
|
||||
submissionId: Scalars['ID']['input'];
|
||||
};
|
||||
|
||||
|
||||
export type SubmissionMutation_RetrySubmissionArgs = {
|
||||
parentSubmissionId: Scalars['ID']['input'];
|
||||
};
|
||||
@@ -12910,6 +12928,7 @@ export type UsageBudget = {
|
||||
__typename?: 'UsageBudget';
|
||||
id: Scalars['ID']['output'];
|
||||
limits: UsageBudgetLimits;
|
||||
remaining: UsageBudgetRemaining;
|
||||
};
|
||||
|
||||
export type UsageBudgetLimits = {
|
||||
@@ -12933,6 +12952,24 @@ export type UsageBudgetMutation_CreateUsageBudgetArgs = {
|
||||
limits: UsageBudgetLimitsInput;
|
||||
};
|
||||
|
||||
export type UsageBudgetQuery = {
|
||||
__typename?: 'UsageBudgetQuery';
|
||||
byId: UsageBudget;
|
||||
};
|
||||
|
||||
|
||||
export type UsageBudgetQuery_ByIdArgs = {
|
||||
id: Scalars['ID']['input'];
|
||||
};
|
||||
|
||||
export type UsageBudgetRemaining = {
|
||||
__typename?: 'UsageBudgetRemaining';
|
||||
/** Available builds, or null when there is no build limit. */
|
||||
builds?: Maybe<Scalars['Int']['output']>;
|
||||
/** Available minutes after completed usage and active reservations, or null when unlimited. */
|
||||
simulatorMinutes?: Maybe<Scalars['Float']['output']>;
|
||||
};
|
||||
|
||||
export type UsageMetricTotal = {
|
||||
__typename?: 'UsageMetricTotal';
|
||||
billingPeriod: BillingPeriod;
|
||||
@@ -14808,6 +14845,7 @@ export enum WorkflowJobStatus {
|
||||
}
|
||||
|
||||
export enum WorkflowJobType {
|
||||
Agent = 'AGENT',
|
||||
AppleDeviceRegistrationRequest = 'APPLE_DEVICE_REGISTRATION_REQUEST',
|
||||
BranchDelete = 'BRANCH_DELETE',
|
||||
Build = 'BUILD',
|
||||
@@ -14963,7 +15001,7 @@ export type WorkflowRun = ActivityTimelineProjectActivity & {
|
||||
workflowRevision?: Maybe<WorkflowRevision>;
|
||||
};
|
||||
|
||||
export type WorkflowRunCancelReason = WorkflowRunCancelReasonConcurrencyGroup;
|
||||
export type WorkflowRunCancelReason = WorkflowRunCancelReasonConcurrencyGroup | WorkflowRunCancelReasonWorkflowCall;
|
||||
|
||||
export type WorkflowRunCancelReasonConcurrencyGroup = {
|
||||
__typename?: 'WorkflowRunCancelReasonConcurrencyGroup';
|
||||
@@ -14974,6 +15012,15 @@ export type WorkflowRunCancelReasonConcurrencyGroup = {
|
||||
cancelingWorkflowRun?: Maybe<WorkflowRun>;
|
||||
};
|
||||
|
||||
export type WorkflowRunCancelReasonWorkflowCall = {
|
||||
__typename?: 'WorkflowRunCancelReasonWorkflowCall';
|
||||
/**
|
||||
* The run that called this run through workflow_call and whose cancellation
|
||||
* canceled this run. Null if that run no longer exists.
|
||||
*/
|
||||
parentWorkflowRun?: Maybe<WorkflowRun>;
|
||||
};
|
||||
|
||||
export type WorkflowRunEdge = {
|
||||
__typename?: 'WorkflowRunEdge';
|
||||
cursor: Scalars['String']['output'];
|
||||
@@ -15153,11 +15200,28 @@ export type WorkflowsInsightsMutation_ExportWorkflowRunsArgs = {
|
||||
input: WorkflowsInsightsExportInput;
|
||||
};
|
||||
|
||||
/**
|
||||
* Same shape as WorkflowsInsightsMetric but nullable on both sides. Null means the window had
|
||||
* no qualifying run, which is different from 0.
|
||||
*/
|
||||
export type WorkflowsInsightsNullableMetric = {
|
||||
__typename?: 'WorkflowsInsightsNullableMetric';
|
||||
currentValue?: Maybe<Scalars['Float']['output']>;
|
||||
previousValue?: Maybe<Scalars['Float']['output']>;
|
||||
};
|
||||
|
||||
export type WorkflowsInsightsOverviewMetrics = {
|
||||
__typename?: 'WorkflowsInsightsOverviewMetrics';
|
||||
activeWorkflows: WorkflowsInsightsMetric;
|
||||
/**
|
||||
* Mean milliseconds from run creation to finalization, the same duration the run page shows,
|
||||
* so queue and approval wait count. Canceled runs are excluded.
|
||||
*/
|
||||
avgDurationMs: WorkflowsInsightsNullableMetric;
|
||||
canceledRuns: WorkflowsInsightsMetric;
|
||||
failedRuns: WorkflowsInsightsMetric;
|
||||
/** Approximate 75th percentile of the same duration, excluding canceled runs. */
|
||||
p75DurationMs: WorkflowsInsightsNullableMetric;
|
||||
successfulRuns: WorkflowsInsightsMetric;
|
||||
totalRuns: WorkflowsInsightsMetric;
|
||||
};
|
||||
@@ -15201,10 +15265,12 @@ export type WorkflowsInsightsWorkflowMetrics = {
|
||||
|
||||
export type WorkflowsInsightsWorkflowNode = {
|
||||
__typename?: 'WorkflowsInsightsWorkflowNode';
|
||||
avgDurationMs?: Maybe<Scalars['Float']['output']>;
|
||||
canceledRuns: Scalars['Int']['output'];
|
||||
failedRuns: Scalars['Int']['output'];
|
||||
lastRunAt: Scalars['DateTime']['output'];
|
||||
name: Scalars['String']['output'];
|
||||
p75DurationMs?: Maybe<Scalars['Float']['output']>;
|
||||
successfulRuns: Scalars['Int']['output'];
|
||||
totalRuns: Scalars['Int']['output'];
|
||||
workflowId: Scalars['ID']['output'];
|
||||
@@ -17350,7 +17416,7 @@ export type WorkflowsInsightsByAppIdQueryVariables = Exact<{
|
||||
}>;
|
||||
|
||||
|
||||
export type WorkflowsInsightsByAppIdQuery = { __typename?: 'RootQuery', app: { __typename?: 'AppQuery', byId: { __typename?: 'App', id: string, fullName: string, workflows: Array<{ __typename?: 'Workflow', id: string, fileName: string }>, workflowsInsights: { __typename?: 'AppWorkflowsInsights', overviewMetrics: { __typename?: 'WorkflowsInsightsOverviewMetrics', totalRuns: { __typename?: 'WorkflowsInsightsMetric', currentValue: number, previousValue: number }, successfulRuns: { __typename?: 'WorkflowsInsightsMetric', currentValue: number, previousValue: number }, failedRuns: { __typename?: 'WorkflowsInsightsMetric', currentValue: number, previousValue: number }, activeWorkflows: { __typename?: 'WorkflowsInsightsMetric', currentValue: number, previousValue: number } }, runsOverTime: { __typename?: 'WorkflowsInsightsRunsOverTimeData', lineChart: { __typename?: 'LineChartData', labels: Array<string>, datasets: Array<{ __typename?: 'LineDataset', id: string, data: Array<number | null> }> } }, workflows: { __typename?: 'WorkflowsInsightsWorkflowConnection', edges: Array<{ __typename?: 'WorkflowsInsightsWorkflowEdge', node: { __typename?: 'WorkflowsInsightsWorkflowNode', workflowId: string, name: string, totalRuns: number, successfulRuns: number, failedRuns: number, canceledRuns: number, lastRunAt: any } }>, pageInfo: { __typename?: 'PageInfo', hasNextPage: boolean } } } } } };
|
||||
export type WorkflowsInsightsByAppIdQuery = { __typename?: 'RootQuery', app: { __typename?: 'AppQuery', byId: { __typename?: 'App', id: string, fullName: string, workflows: Array<{ __typename?: 'Workflow', id: string, fileName: string }>, workflowsInsights: { __typename?: 'AppWorkflowsInsights', overviewMetrics: { __typename?: 'WorkflowsInsightsOverviewMetrics', totalRuns: { __typename?: 'WorkflowsInsightsMetric', currentValue: number, previousValue: number }, successfulRuns: { __typename?: 'WorkflowsInsightsMetric', currentValue: number, previousValue: number }, failedRuns: { __typename?: 'WorkflowsInsightsMetric', currentValue: number, previousValue: number }, activeWorkflows: { __typename?: 'WorkflowsInsightsMetric', currentValue: number, previousValue: number }, avgDurationMs: { __typename?: 'WorkflowsInsightsNullableMetric', currentValue?: number | null, previousValue?: number | null }, p75DurationMs: { __typename?: 'WorkflowsInsightsNullableMetric', currentValue?: number | null, previousValue?: number | null } }, runsOverTime: { __typename?: 'WorkflowsInsightsRunsOverTimeData', lineChart: { __typename?: 'LineChartData', labels: Array<string>, datasets: Array<{ __typename?: 'LineDataset', id: string, data: Array<number | null> }> } }, workflows: { __typename?: 'WorkflowsInsightsWorkflowConnection', edges: Array<{ __typename?: 'WorkflowsInsightsWorkflowEdge', node: { __typename?: 'WorkflowsInsightsWorkflowNode', workflowId: string, name: string, totalRuns: number, successfulRuns: number, failedRuns: number, canceledRuns: number, avgDurationMs?: number | null, p75DurationMs?: number | null, lastRunAt: any } }>, pageInfo: { __typename?: 'PageInfo', hasNextPage: boolean } } } } } };
|
||||
|
||||
export type AccountFragment = { __typename?: 'Account', id: string, name: string, ownerUserActor?:
|
||||
| { __typename?: 'SSOUser', id: string, username: string }
|
||||
|
||||
@@ -67,6 +67,14 @@ export const WorkflowsInsightsQuery = {
|
||||
currentValue
|
||||
previousValue
|
||||
}
|
||||
avgDurationMs {
|
||||
currentValue
|
||||
previousValue
|
||||
}
|
||||
p75DurationMs {
|
||||
currentValue
|
||||
previousValue
|
||||
}
|
||||
}
|
||||
runsOverTime(timespan: $timespan, filters: $filters, granularity: $granularity) {
|
||||
lineChart {
|
||||
@@ -86,6 +94,8 @@ export const WorkflowsInsightsQuery = {
|
||||
successfulRuns
|
||||
failedRuns
|
||||
canceledRuns
|
||||
avgDurationMs
|
||||
p75DurationMs
|
||||
lastRunAt
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,11 @@
|
||||
import { formatRateWithDelta, formatTrend, ratePercent, toMetricSummary } from '../metrics';
|
||||
import {
|
||||
formatNullableMetricWithTrend,
|
||||
formatRateWithDelta,
|
||||
formatTrend,
|
||||
ratePercent,
|
||||
toMetricSummary,
|
||||
toNullableMetricSummary,
|
||||
} from '../metrics';
|
||||
|
||||
function stripAnsi(text: string): string {
|
||||
return text.replace(/\x1b\[[0-9;]*m/g, '');
|
||||
@@ -54,3 +61,39 @@ describe(formatRateWithDelta, () => {
|
||||
).toBe('80.0% (+4.5 pts)');
|
||||
});
|
||||
});
|
||||
|
||||
describe(toNullableMetricSummary, () => {
|
||||
it('keeps missing values as null', () => {
|
||||
expect(toNullableMetricSummary({ currentValue: 3, previousValue: null })).toEqual({
|
||||
current: 3,
|
||||
previous: null,
|
||||
});
|
||||
expect(toNullableMetricSummary({})).toEqual({ current: null, previous: null });
|
||||
});
|
||||
});
|
||||
|
||||
describe(formatNullableMetricWithTrend, () => {
|
||||
const seconds = (value: number): string => `${value}s`;
|
||||
|
||||
it('is n/a when the current period has no value', () => {
|
||||
expect(stripAnsi(formatNullableMetricWithTrend({ current: null, previous: 10 }, seconds))).toBe(
|
||||
'n/a'
|
||||
);
|
||||
});
|
||||
|
||||
it('shows the value without a trend when the previous period has no value', () => {
|
||||
expect(stripAnsi(formatNullableMetricWithTrend({ current: 10, previous: null }, seconds))).toBe(
|
||||
'10s (n/a)'
|
||||
);
|
||||
});
|
||||
|
||||
it('formats the value and its trend', () => {
|
||||
expect(
|
||||
stripAnsi(
|
||||
formatNullableMetricWithTrend({ current: 90, previous: 100 }, seconds, {
|
||||
lowerIsBetter: true,
|
||||
})
|
||||
)
|
||||
).toBe('90s (-10.0%)');
|
||||
});
|
||||
});
|
||||
@@ -13,6 +13,18 @@ export function toMetricSummary(metric: {
|
||||
return { current: metric.currentValue, previous: metric.previousValue };
|
||||
}
|
||||
|
||||
export interface NullableMetricSummary {
|
||||
current: number | null;
|
||||
previous: number | null;
|
||||
}
|
||||
|
||||
export function toNullableMetricSummary(metric: {
|
||||
currentValue?: number | null;
|
||||
previousValue?: number | null;
|
||||
}): NullableMetricSummary {
|
||||
return { current: metric.currentValue ?? null, previous: metric.previousValue ?? null };
|
||||
}
|
||||
|
||||
export function ratePercent(part: number, total: number): number {
|
||||
return total === 0 ? 0 : (part / total) * 100;
|
||||
}
|
||||
@@ -56,6 +68,21 @@ export function formatTrend(
|
||||
return formatSignedChange(pct, '%', lowerIsBetter ? pct < 0 : pct > 0);
|
||||
}
|
||||
|
||||
export function formatNullableMetricWithTrend(
|
||||
metric: NullableMetricSummary,
|
||||
formatValue: (value: number) => string,
|
||||
options?: { lowerIsBetter?: boolean }
|
||||
): string {
|
||||
if (metric.current === null) {
|
||||
return chalk.dim('n/a');
|
||||
}
|
||||
const trend =
|
||||
metric.previous === null
|
||||
? chalk.dim('n/a')
|
||||
: formatTrend({ current: metric.current, previous: metric.previous }, options);
|
||||
return `${formatValue(metric.current)} (${trend})`;
|
||||
}
|
||||
|
||||
function formatSignedChange(value: number, unit: string, isImprovement: boolean): string {
|
||||
if (value === 0) {
|
||||
return chalk.dim(`0.0${unit}`);
|
||||
|
||||
@@ -99,28 +99,6 @@ export async function getExpoUpdatesPackageVersionIfInstalledAsync(
|
||||
return version ?? null;
|
||||
}
|
||||
|
||||
export async function validateAppVersionRuntimePolicySupportAsync(
|
||||
projectDir: string,
|
||||
exp: ExpoConfig
|
||||
): Promise<void> {
|
||||
if (typeof exp.runtimeVersion !== 'object' || exp.runtimeVersion?.policy !== 'appVersion') {
|
||||
return;
|
||||
}
|
||||
|
||||
const expoUpdatesPackageVersion = await getExpoUpdatesPackageVersionIfInstalledAsync(projectDir);
|
||||
if (
|
||||
expoUpdatesPackageVersion !== null &&
|
||||
(semver.gte(expoUpdatesPackageVersion, '0.14.4') ||
|
||||
expoUpdatesPackageVersion.includes('canary'))
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
Log.warn(
|
||||
`You need to be on SDK 46 or higher, and use expo-updates >= 0.14.4 to use appVersion runtime policy.`
|
||||
);
|
||||
}
|
||||
|
||||
export async function enforceRollBackToEmbeddedUpdateSupportAsync(
|
||||
projectDir: string
|
||||
): Promise<void> {
|
||||
|
||||
@@ -14,6 +14,10 @@ function metric(currentValue: number, previousValue: number): any {
|
||||
return { __typename: 'WorkflowsInsightsMetric', currentValue, previousValue };
|
||||
}
|
||||
|
||||
function nullableMetric(currentValue: number | null, previousValue: number | null): any {
|
||||
return { __typename: 'WorkflowsInsightsNullableMetric', currentValue, previousValue };
|
||||
}
|
||||
|
||||
function makeApp(
|
||||
overrides: {
|
||||
labels?: string[];
|
||||
@@ -38,6 +42,8 @@ function makeApp(
|
||||
successfulRuns: metric(75, 64),
|
||||
failedRuns: metric(20, 10),
|
||||
activeWorkflows: metric(3, 3),
|
||||
avgDurationMs: nullableMetric(600000, 660000),
|
||||
p75DurationMs: nullableMetric(900000, null),
|
||||
},
|
||||
runsOverTime: {
|
||||
__typename: 'WorkflowsInsightsRunsOverTimeData',
|
||||
@@ -76,6 +82,8 @@ function makeApp(
|
||||
successfulRuns: 45,
|
||||
failedRuns: 12,
|
||||
canceledRuns: 3,
|
||||
avgDurationMs: 600000,
|
||||
p75DurationMs: 900000,
|
||||
lastRunAt: '2026-09-02T10:30:00.000Z',
|
||||
},
|
||||
{
|
||||
@@ -85,6 +93,8 @@ function makeApp(
|
||||
successfulRuns: 0,
|
||||
failedRuns: 0,
|
||||
canceledRuns: 0,
|
||||
avgDurationMs: null,
|
||||
p75DurationMs: null,
|
||||
lastRunAt: '2026-08-20T08:00:00.000Z',
|
||||
},
|
||||
]
|
||||
@@ -129,6 +139,19 @@ describe(toWorkflowsInsightsSummary, () => {
|
||||
'successRatePercent',
|
||||
'activeWorkflows',
|
||||
'failedRuns',
|
||||
'avgDurationMs',
|
||||
'p75DurationMs',
|
||||
]);
|
||||
});
|
||||
|
||||
it('copies durations as nullable current/previous pairs', () => {
|
||||
const summary = makeSummary();
|
||||
|
||||
expect(summary.overview.avgDurationMs).toEqual({ current: 600000, previous: 660000 });
|
||||
expect(summary.overview.p75DurationMs).toEqual({ current: 900000, previous: null });
|
||||
expect(summary.workflows.map(w => [w.avgDurationMs, w.p75DurationMs])).toEqual([
|
||||
[600000, 900000],
|
||||
[null, null],
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -205,6 +228,8 @@ describe(buildWorkflowsInsightsJson, () => {
|
||||
failedRuns: 12,
|
||||
canceledRuns: 3,
|
||||
successRatePercent: 75,
|
||||
avgDurationMs: 600000,
|
||||
p75DurationMs: 900000,
|
||||
lastRunAt: '2026-09-02T10:30:00.000Z',
|
||||
});
|
||||
expect(json.hasMoreWorkflows).toBe(false);
|
||||
@@ -235,6 +260,12 @@ describe(buildWorkflowsInsightsTable, () => {
|
||||
expect(table).toContain('Success rate');
|
||||
expect(table).toContain('75.0%');
|
||||
expect(table).toContain('-5.0 pts');
|
||||
expect(table).toContain('Avg duration');
|
||||
expect(table).toContain('10m (');
|
||||
expect(table).toContain('-9.1%');
|
||||
expect(table).toContain('P75 duration');
|
||||
expect(table).toContain('15m (');
|
||||
expect(table.replace(/\x1b\[[0-9;]*m/g, '')).toMatch(/tests\.yml.*n\/a.*n\/a/);
|
||||
expect(table).toContain('Runs over time (daily, UTC):');
|
||||
expect(table).toContain('2026-09-01');
|
||||
expect(table).toContain('build.yml');
|
||||
|
||||
Loaded 100 of 115 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user