Skip to content

Commit 15c3b48

Browse files
amankansal-ltclaude
andcommitted
Merge callstack/agent-device main into testmu-plugin-merge
Brings in #3207 (refuse lease allocation when the daemon holds other provider credentials) and #3222. Conflicts: - provider-definitions.ts: keep main's requireBrowserStackCredentials and drop its local request helpers, which this branch moved to webdriver-utils.ts; requireEnv is no longer used here but stays exported from provider-webdriver/plugin for TestMu. - cli-help.ts: keep the TestMu AI provider and credential text and add main's daemon credential comparison line. Co-Authored-By: Claude Opus 5.5 <[email protected]>
2 parents 6b19fde + e74ddfe commit 15c3b48

39 files changed

Lines changed: 1097 additions & 87 deletions

‎packages/kernel/src/contracts.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ export type DaemonRequestMeta = {
148148
leaseProvider?: string;
149149
deviceKey?: string;
150150
clientId?: string;
151+
/** A local caller's digest of its lease-provider credential variables, compared on allocation. */
152+
providerCredentialFingerprint?: string;
151153
sessionIsolation?: SessionIsolationMode;
152154
uploadedArtifactId?: string;
153155
clientArtifactPaths?: Record<string, string>;

‎packages/platform-apple/src/core/runner-host.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ import {
4444
getRunnerLeaseOwnerStateDir,
4545
} from './runner-owner-state.ts';
4646
import { buildSimctlArgsForDevice, simulatorAddressFor } from './simctl.ts';
47-
import { readApplePlistJson, runAppleToolCommand, runXcrun } from './tool-provider.ts';
47+
import {
48+
hasScopedAppleToolProvider,
49+
readApplePlistJson,
50+
runAppleToolCommand,
51+
runXcrun,
52+
} from './tool-provider.ts';
4853

4954
/**
5055
* The real host capabilities for `@agent-device/platform-apple/runner`: the one place
@@ -97,6 +102,7 @@ export const appleRunnerHost: AppleRunnerHost = {
97102
runAppleToolCommand,
98103
runXcrun,
99104
readApplePlistJson,
105+
hasScopedAppleToolProvider,
100106
buildSimctlArgsForDevice,
101107
simulatorAddressFor,
102108
resolveIosPhysicalDeviceControl,

‎packages/platform-apple/src/runner/__tests__/runner-client-prewarm.test.ts‎

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ import { AppError } from '@agent-device/kernel/errors';
44
import { IOS_SIMULATOR } from './device-fixtures.ts';
55
import { makeRunnerSession } from './runner-session-fixtures.ts';
66
import { appleRunnerTestHost } from '../test-host.ts';
7+
import { createLocalAppleToolProvider, withAppleToolProvider } from '../../core/tool-provider.ts';
78

8-
const { mockEnsureRunnerSession, mockExecuteRunnerCommandWithSession, mockEmitDiagnostic } =
9-
vi.hoisted(() => ({
10-
mockEnsureRunnerSession: vi.fn(),
11-
mockExecuteRunnerCommandWithSession: vi.fn(),
12-
mockEmitDiagnostic: vi.fn(),
13-
}));
9+
const {
10+
mockEnsureRunnerSession,
11+
mockExecuteRunnerCommandWithSession,
12+
mockEnsureXctestrunArtifact,
13+
mockEmitDiagnostic,
14+
} = vi.hoisted(() => ({
15+
mockEnsureRunnerSession: vi.fn(),
16+
mockExecuteRunnerCommandWithSession: vi.fn(),
17+
mockEnsureXctestrunArtifact: vi.fn(),
18+
mockEmitDiagnostic: vi.fn(),
19+
}));
1420

1521
vi.mock('../runner-session.ts', async () => {
1622
const actual =
@@ -22,7 +28,13 @@ vi.mock('../runner-session.ts', async () => {
2228
};
2329
});
2430

25-
import { prewarmIosRunnerSession } from '../runner-client.ts';
31+
vi.mock('../runner-xctestrun.ts', async () => {
32+
const actual =
33+
await vi.importActual<typeof import('../runner-xctestrun.ts')>('../runner-xctestrun.ts');
34+
return { ...actual, ensureXctestrunArtifact: mockEnsureXctestrunArtifact };
35+
});
36+
37+
import { prewarmAppleRunnerCache, prewarmIosRunnerSession } from '../runner-client.ts';
2638

2739
beforeEach(() => {
2840
vi.resetAllMocks();
@@ -82,3 +94,40 @@ test('prewarmIosRunnerSession can propagate setup failures for blocking callers'
8294
});
8395
assert.equal(mockEnsureRunnerSession.mock.calls[0]?.[1]?.propagateError, undefined);
8496
});
97+
98+
test('prewarmAppleRunnerCache builds the runner artifact when the host serves Apple tooling', async () => {
99+
mockEnsureXctestrunArtifact.mockResolvedValueOnce({});
100+
101+
await prewarmAppleRunnerCache(IOS_SIMULATOR, { requestId: 'cache-request' });
102+
103+
assert.equal(mockEnsureXctestrunArtifact.mock.calls.length, 1);
104+
assert.equal(mockEnsureXctestrunArtifact.mock.calls[0]?.[0], IOS_SIMULATOR);
105+
assert.equal(mockEnsureXctestrunArtifact.mock.calls[0]?.[1]?.requestId, 'cache-request');
106+
});
107+
108+
test('prewarmAppleRunnerCache starts no host build while a provider serves Apple tooling', async () => {
109+
const providerCalls: string[] = [];
110+
const scriptedTools = createLocalAppleToolProvider({
111+
runCommand: async (cmd, args) => {
112+
providerCalls.push([cmd, ...args].join(' '));
113+
throw new Error('a cache prewarm must not reach provider-served Apple tooling');
114+
},
115+
});
116+
117+
const scoped = await withAppleToolProvider(scriptedTools, async () => ({
118+
prewarm: prewarmAppleRunnerCache(IOS_SIMULATOR, { requestId: 'provider-request' }),
119+
}));
120+
121+
assert.equal(scoped.prewarm, undefined);
122+
assert.equal(mockEnsureXctestrunArtifact.mock.calls.length, 0);
123+
assert.deepEqual(providerCalls, []);
124+
assert.deepEqual(mockEmitDiagnostic.mock.calls, [
125+
[
126+
{
127+
level: 'debug',
128+
phase: 'ios_runner_cache_prewarm_unavailable',
129+
data: { deviceId: IOS_SIMULATOR.id },
130+
},
131+
],
132+
]);
133+
});

‎packages/platform-apple/src/runner/host.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ export type AppleRunnerHost = Pick<
8181
Pick<typeof KernelSourceValue, 'parseBooleanLiteral'> &
8282
Pick<typeof KernelDeviceShell, 'shellQuote'> &
8383
Pick<typeof BootDiagnostics, 'classifyBootFailure' | 'bootFailureHint'> &
84-
Pick<typeof AppleToolProvider, 'runAppleToolCommand' | 'runXcrun' | 'readApplePlistJson'> &
84+
Pick<
85+
typeof AppleToolProvider,
86+
'runAppleToolCommand' | 'runXcrun' | 'readApplePlistJson' | 'hasScopedAppleToolProvider'
87+
> &
8588
Pick<typeof AppleSimctl, 'buildSimctlArgsForDevice' | 'simulatorAddressFor'> &
8689
Pick<typeof ApplePlistXml, 'visitXmlPlistEntries'> & {
8790
/**
@@ -185,6 +188,7 @@ export const bootFailureHint = delegate('bootFailureHint');
185188
export const runAppleToolCommand = delegate('runAppleToolCommand');
186189
export const runXcrun = delegate('runXcrun');
187190
export const readApplePlistJson = delegate('readApplePlistJson');
191+
export const hasScopedAppleToolProvider = delegate('hasScopedAppleToolProvider');
188192
export const buildSimctlArgsForDevice = delegate('buildSimctlArgsForDevice');
189193
export const simulatorAddressFor = delegate('simulatorAddressFor');
190194
export const visitXmlPlistEntries = delegate('visitXmlPlistEntries');

‎packages/platform-apple/src/runner/runner-client.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { retryWithPolicy, emitDiagnostic } from './host.ts';
1+
import { retryWithPolicy, emitDiagnostic, hasScopedAppleToolProvider } from './host.ts';
22
import { isIosFamily, type DeviceInfo } from '@agent-device/kernel/device';
33
import {
44
ensureRunnerSession,
@@ -119,13 +119,22 @@ type PrewarmIosRunnerOptions = AppleRunnerPrewarmOptions & {
119119
propagateError?: boolean;
120120
};
121121

122+
/** Speculative host build: a request whose Apple tooling a provider serves has none to warm. */
122123
export function prewarmAppleRunnerCache(
123124
device: DeviceInfo,
124125
options: PrewarmIosRunnerOptions = {},
125126
): Promise<void> | undefined {
126127
if (!isIosFamily(device)) {
127128
return undefined;
128129
}
130+
if (hasScopedAppleToolProvider()) {
131+
emitDiagnostic({
132+
level: 'debug',
133+
phase: 'ios_runner_cache_prewarm_unavailable',
134+
data: { deviceId: device.id },
135+
});
136+
return undefined;
137+
}
129138
return runBestEffortIosRunnerPrewarm({
130139
device,
131140
options,

‎packages/provider-webdriver/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import type { CloudWebDriverRuntime } from './runtime.ts';
2121
export { CLOUD_WEBDRIVER_PROFILE_FIELDS, CLOUD_WEBDRIVER_PROVIDERS };
2222
export { readAwsDeviceFarmRegionFromArn };
2323
export { parseBrowserStackAppReference } from './browserstack.ts';
24+
export { requireBrowserStackCredentials } from './provider-definitions.ts';
2425
export type { CloudWebDriverKnownProviderName } from './providers.ts';
2526
export type { ProviderWebDriverDependencies, RunHostCommand } from './dependencies.ts';
2627
export type {

‎packages/provider-webdriver/src/provider-definitions.ts‎

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ import {
2222
buildBrowserStackDeviceFeatureCapabilities,
2323
readBrowserStackDeviceFeatureFields,
2424
} from './browserstack-device-features.ts';
25-
import { CLOUD_WEBDRIVER_PROVIDERS, type CloudWebDriverKnownProviderName } from './providers.ts';
25+
import {
26+
BROWSERSTACK_CREDENTIAL_VARIABLES,
27+
CLOUD_WEBDRIVER_PROVIDERS,
28+
readBrowserStackCredentials,
29+
type CloudWebDriverKnownProviderName,
30+
} from './providers.ts';
2631
import { readAwsDeviceFarmRegionFromArn } from './connection-verification.ts';
2732
import {
2833
readFlag,
29-
requireEnv,
3034
requireFlag,
3135
requireRequest,
3236
requireRequestPlatform,
@@ -150,14 +154,8 @@ export function createCloudWebDriverProviderDefinitions(
150154
endpoint: env.BROWSERSTACK_WEBDRIVER_ENDPOINT ?? BROWSERSTACK_APP_AUTOMATE_ENDPOINT,
151155
capabilityOverrides: BROWSERSTACK_CAPABILITY_OVERRIDES,
152156
listArtifacts: async ({ provider, providerSessionId }) => {
153-
const username = requireEnv(
154-
env,
155-
'BROWSERSTACK_USERNAME',
156-
'BrowserStack artifact lookup',
157-
);
158-
const accessKey = requireEnv(
157+
const { username, accessKey } = requireBrowserStackCredentials(
159158
env,
160-
'BROWSERSTACK_ACCESS_KEY',
161159
'BrowserStack artifact lookup',
162160
);
163161
return await listBrowserStackCloudArtifacts(provider, providerSessionId, {
@@ -169,8 +167,7 @@ export function createCloudWebDriverProviderDefinitions(
169167
},
170168
prepareSession: async ({ req, lease, base }) => {
171169
const request = requireRequest(req, 'BrowserStack');
172-
const username = requireEnv(env, 'BROWSERSTACK_USERNAME', 'BrowserStack');
173-
const accessKey = requireEnv(env, 'BROWSERSTACK_ACCESS_KEY', 'BrowserStack');
170+
const { username, accessKey } = requireBrowserStackCredentials(env, 'BrowserStack');
174171
const platform = requireRequestPlatform(request, 'BrowserStack');
175172
const deviceName = requireFlag(
176173
request,
@@ -228,10 +225,8 @@ export function createCloudWebDriverProviderDefinitions(
228225
},
229226
}),
230227
listArtifactsFromEnv: async (providerSessionId, env) => {
231-
const username = requireEnv(env, 'BROWSERSTACK_USERNAME', 'BrowserStack artifact lookup');
232-
const accessKey = requireEnv(
228+
const { username, accessKey } = requireBrowserStackCredentials(
233229
env,
234-
'BROWSERSTACK_ACCESS_KEY',
235230
'BrowserStack artifact lookup',
236231
);
237232
return await listBrowserStackCloudArtifacts(
@@ -346,3 +341,15 @@ function readAwsInteractionMode(
346341
function dasherize(value: string): string {
347342
return value.replaceAll(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
348343
}
344+
345+
export function requireBrowserStackCredentials(
346+
env: Readonly<Record<string, string | undefined>>,
347+
consumer: string,
348+
): { username: string; accessKey: string } {
349+
const { username, accessKey } = readBrowserStackCredentials(env);
350+
if (username && accessKey) return { username, accessKey };
351+
const missing = username
352+
? BROWSERSTACK_CREDENTIAL_VARIABLES.accessKey
353+
: BROWSERSTACK_CREDENTIAL_VARIABLES.username;
354+
throw new AppError('INVALID_ARGS', `${consumer} requires ${missing} in the environment.`);
355+
}

‎packages/provider-webdriver/src/providers.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ export function isCloudWebDriverProviderName(
1414
return provider !== undefined && CLOUD_WEBDRIVER_KNOWN_PROVIDERS.has(provider);
1515
}
1616

17+
/** The environment variables that hold BrowserStack credentials. */
18+
export const BROWSERSTACK_CREDENTIAL_VARIABLES = {
19+
username: 'BROWSERSTACK_USERNAME',
20+
accessKey: 'BROWSERSTACK_ACCESS_KEY',
21+
} as const;
22+
23+
/** The BrowserStack credentials in the environment, exactly as every consumer and the fingerprint use them. */
24+
export function readBrowserStackCredentials(
25+
env: Readonly<Record<string, string | undefined>>,
26+
): Readonly<{ username?: string; accessKey?: string }> {
27+
return {
28+
username: env[BROWSERSTACK_CREDENTIAL_VARIABLES.username] || undefined,
29+
accessKey: env[BROWSERSTACK_CREDENTIAL_VARIABLES.accessKey] || undefined,
30+
};
31+
}
32+
1733
const BROWSERSTACK_APP_SCHEME = 'bs://';
1834

1935
/**

‎src/__tests__/hermetic-env-setup.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ import { afterEach } from 'vitest';
1515
// daemonBaseUrl/daemonAuthToken keys, and daemon-client tests take the remote
1616
// path ("Remote daemon is unavailable") instead of the local one they exercise.
1717
//
18+
// An HTTP auth hook likewise makes a local daemon treat every HTTP caller as remote.
19+
//
1820
// CI runs with these unset. Delete them here so a configured host matches CI.
1921
// Tests that genuinely need them assign their own value or pass an explicit env
2022
// object; that happens inside the test, after this module has loaded, so this
2123
// scrub does not interfere.
2224
const AMBIENT_DAEMON_ENV_VARS = [
2325
'AGENT_DEVICE_DAEMON_BASE_URL',
2426
'AGENT_DEVICE_DAEMON_AUTH_TOKEN',
27+
'AGENT_DEVICE_HTTP_AUTH_HOOK',
28+
'AGENT_DEVICE_HTTP_AUTH_EXPORT',
2529
] as const;
2630

2731
for (const name of AMBIENT_DAEMON_ENV_VARS) {

‎src/cli/connection/cloud-webdriver-profile.ts‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
CLOUD_WEBDRIVER_PROVIDERS,
44
parseBrowserStackAppReference,
55
readAwsDeviceFarmRegionFromArn,
6+
requireBrowserStackCredentials,
67
type CloudWebDriverKnownProviderName,
78
} from '@agent-device/provider-webdriver';
89
import { rejectRefusedProviderProfileFields } from '@agent-device/contracts/provider-profile-fields';
@@ -99,8 +100,7 @@ function browserStackProfileFields(options: {
99100
env?: EnvMap;
100101
cwd: string;
101102
}): RemoteConfigProfile {
102-
requireEnv(options.env, 'BROWSERSTACK_USERNAME', 'connect browserstack');
103-
requireEnv(options.env, 'BROWSERSTACK_ACCESS_KEY', 'connect browserstack');
103+
requireBrowserStackCredentials(options.env ?? {}, 'connect browserstack');
104104
const platform = requireCloudWebDriverPlatform(
105105
options.flags.platform,
106106
'connect browserstack requires --platform ios|android.',
@@ -195,12 +195,6 @@ function requireFlag(value: string | undefined, message: string): string {
195195
throw new AppError('INVALID_ARGS', message);
196196
}
197197

198-
function requireEnv(env: EnvMap | undefined, name: string, command: string): string {
199-
const value = env?.[name];
200-
if (value) return value;
201-
throw new AppError('INVALID_ARGS', `${command} requires ${name} in the environment.`);
202-
}
203-
204198
function requireAwsProfileValue(
205199
flagValue: string | undefined,
206200
env: EnvMap | undefined,

0 commit comments

Comments
 (0)