Skip to content

Commit 267e9a5

Browse files
thymikeeclaude
andauthored
refactor(runtime): use the owning host environment at process boundaries (#3753) (#3828)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent d6f3172 commit 267e9a5

19 files changed

Lines changed: 194 additions & 67 deletions

‎src/__tests__/platform-runtime-runtime-hints.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { bindTestCommandHost } from '@agent-device/test-kit';
12
import { test } from 'vitest';
23
import assert from 'node:assert/strict';
34
import { promises as fs } from 'node:fs';
@@ -418,3 +419,5 @@ test('clearRuntimeHintValues deletes iOS simulator React Native defaults', async
418419
);
419420
});
420421
});
422+
423+
bindTestCommandHost();

‎src/__tests__/provider-device-runtimes.test.ts‎

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import {
1313
test('daemon composition registers installed provider runtimes and their lazy platform modules', async () => {
1414
const { home, env } = pluginHome();
1515
selectPlugin(home, 'example', 'example', registrationSource('example'));
16-
const composition = await createDaemonProviderRuntimeComposition(env);
16+
const composition = await createDaemonProviderRuntimeComposition({
17+
env: env,
18+
cwd: process.cwd(),
19+
});
1720
const plugin = composition.runtimes.find((runtime) => runtime.provider === 'example');
1821
assert.ok(plugin);
1922
assert.equal(
@@ -26,7 +29,10 @@ test('daemon composition registers installed provider runtimes and their lazy pl
2629
test('built-in providers register ahead of the store and an unconfigured Limrun still claims its id', async () => {
2730
const { home, env } = pluginHome();
2831
selectPlugin(home, 'example', 'example', registrationSource('example'));
29-
const composition = await createDaemonProviderRuntimeComposition(env);
32+
const composition = await createDaemonProviderRuntimeComposition({
33+
env: env,
34+
cwd: process.cwd(),
35+
});
3036

3137
assert.deepEqual(
3238
composition.runtimes.map((runtime) => runtime.provider),
@@ -47,10 +53,13 @@ test('daemon composition records Limrun as failed for a partial instance set and
4753
const { home, env } = pluginHome();
4854
selectPlugin(home, 'example', 'example', registrationSource('example'));
4955
const composition = await createDaemonProviderRuntimeComposition({
50-
...env,
51-
LIMRUN_API_KEY: 'lim_test_key',
52-
LIM_ANDROID_INSTANCE_URL: 'https://region.limrun.example/v1/android_x/api',
53-
LIM_ANDROID_INSTANCE_TOKEN: 'android-instance-token',
56+
env: {
57+
...env,
58+
LIMRUN_API_KEY: 'lim_test_key',
59+
LIM_ANDROID_INSTANCE_URL: 'https://region.limrun.example/v1/android_x/api',
60+
LIM_ANDROID_INSTANCE_TOKEN: 'android-instance-token',
61+
},
62+
cwd: process.cwd(),
5463
});
5564

5665
assert.deepEqual(
@@ -68,7 +77,10 @@ test('a plugin claiming a built-in provider ID is recorded as failed and the bui
6877
for (const provider of ['limrun', 'browserstack', 'aws-device-farm']) {
6978
const { home, env } = pluginHome();
7079
selectPlugin(home, 'example', provider, 'throw new Error("must not evaluate");');
71-
const composition = await createDaemonProviderRuntimeComposition(env);
80+
const composition = await createDaemonProviderRuntimeComposition({
81+
env: env,
82+
cwd: process.cwd(),
83+
});
7284
const plugin = composition.inventory.find(({ package: name }) => name === 'example');
7385
assert.equal(plugin?.state, 'failed');
7486
assert.equal(plugin?.error?.reason, 'duplicate_provider');
@@ -85,8 +97,11 @@ test('Limrun stays unregistered when only the removed API key alias is configure
8597
const { env } = pluginHome();
8698
const { runtimes, platformModules, inventory, shutdown } =
8799
await createDaemonProviderRuntimeComposition({
88-
...env,
89-
LIM_API_KEY: 'lim_test_key',
100+
env: {
101+
...env,
102+
LIM_API_KEY: 'lim_test_key',
103+
},
104+
cwd: process.cwd(),
90105
});
91106

92107
assert.equal(
@@ -104,8 +119,11 @@ test('Limrun stays unregistered when only the removed API key alias is configure
104119
test('Limrun registers when a Limrun API key is configured', async () => {
105120
const { env } = pluginHome();
106121
const { runtimes, platformModules, shutdown } = await createDaemonProviderRuntimeComposition({
107-
...env,
108-
LIMRUN_API_KEY: 'lim_test_key',
122+
env: {
123+
...env,
124+
LIMRUN_API_KEY: 'lim_test_key',
125+
},
126+
cwd: process.cwd(),
109127
});
110128

111129
const limrun = runtimes.find((runtime) => runtime.provider === 'limrun');
@@ -118,9 +136,12 @@ test('Limrun registers when a Limrun API key is configured', async () => {
118136
test('Limrun registers for an existing instance without an API key', async () => {
119137
const { env } = pluginHome();
120138
const { runtimes, platformModules, shutdown } = await createDaemonProviderRuntimeComposition({
121-
...env,
122-
LIM_IOS_INSTANCE_URL: 'https://region.limrun.example/v1/ios_x/api',
123-
LIM_IOS_INSTANCE_TOKEN: 'ios-instance-token',
139+
env: {
140+
...env,
141+
LIM_IOS_INSTANCE_URL: 'https://region.limrun.example/v1/ios_x/api',
142+
LIM_IOS_INSTANCE_TOKEN: 'ios-instance-token',
143+
},
144+
cwd: process.cwd(),
124145
});
125146

126147
const limrun = runtimes.find((runtime) => runtime.provider === 'limrun');
@@ -167,7 +188,10 @@ test('composition shutdown is the only route the daemon uses: it shuts every run
167188
});`,
168189
{ selectionOptions: { log } },
169190
);
170-
const composition = await createDaemonProviderRuntimeComposition(env);
191+
const composition = await createDaemonProviderRuntimeComposition({
192+
env: env,
193+
cwd: process.cwd(),
194+
});
171195
assert.ok(composition.runtimes.some((runtime) => runtime.provider === 'cleaning'));
172196

173197
await composition.shutdown();

‎src/__tests__/test-utils/builtin-plugin-inventory.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { builtinProviderCandidates } from '../../builtin-provider-plugins.ts';
88
*/
99
export async function builtinPluginInventory(): Promise<readonly PluginInventoryEntry[]> {
1010
const resolved = await resolveProviderPlugins(
11-
builtinProviderCandidates({ LIMRUN_API_KEY: 'configured' }),
11+
builtinProviderCandidates({ env: { LIMRUN_API_KEY: 'configured' }, cwd: process.cwd() }),
1212
);
1313
return resolved.inventory();
1414
}

‎src/builtin-provider-plugins.ts‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
type CloudWebDriverProviderDeclaration,
66
} from '@agent-device/provider-webdriver/providers';
77
import { LIMRUN_ID } from './builtin-provider-ids.ts';
8-
import type { EnvMap } from '@agent-device/kernel/source-value';
8+
import type { HostEnvironment } from '@agent-device/kernel/source-value';
99
import {
1010
limrunCredentialDeclaration,
1111
readLimrunCredentials,
@@ -17,14 +17,14 @@ import {
1717
* load only when a runtime is set up; a candidate that needs configuration claims its id without
1818
* registering until the environment holds it.
1919
*/
20-
export function builtinProviderCandidates(env: EnvMap): PluginCandidate[] {
20+
export function builtinProviderCandidates(host: HostEnvironment): PluginCandidate[] {
2121
return [
2222
...CLOUD_WEBDRIVER_PROVIDER_DECLARATIONS.map((declaration): PluginCandidate => ({
2323
source: 'builtin',
2424
provider: declaration.provider,
2525
load: () => cloudWebDriverPlugin(declaration),
2626
})),
27-
limrunCandidate(env),
27+
limrunCandidate(host),
2828
];
2929
}
3030

@@ -64,11 +64,11 @@ function cloudWebDriverPlugin(declaration: CloudWebDriverProviderDeclaration): A
6464
};
6565
}
6666

67-
function limrunCandidate(env: EnvMap): PluginCandidate {
67+
function limrunCandidate(host: HostEnvironment): PluginCandidate {
6868
const base = { source: 'builtin', provider: LIMRUN_ID } as const;
6969
let credentials: LimrunCredentials | undefined;
7070
try {
71-
credentials = readLimrunCredentials(env);
71+
credentials = readLimrunCredentials(host.env);
7272
} catch (error) {
7373
return { ...base, load: () => Promise.reject(error) };
7474
}
@@ -85,9 +85,13 @@ function limrunCandidate(env: EnvMap): PluginCandidate {
8585
import('@agent-device/provider-limrun'),
8686
import('./sdk/limrun-runtime-dependencies.ts'),
8787
]);
88-
return runtime.createLimrunRuntime(limrun, dependencies.createLimrunRuntimeDependencies(), {
89-
includePlatformModule: true,
90-
});
88+
return runtime.createLimrunRuntime(
89+
limrun,
90+
dependencies.createLimrunRuntimeDependencies(host),
91+
{
92+
includePlatformModule: true,
93+
},
94+
);
9195
},
9296
}),
9397
};

‎src/daemon-composition.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export const createDaemonRuntimeDeps: ComposeDaemonRuntimeDeps = async (
9393
resolveSessionDir,
9494
});
9595
const hostDiagnostics = createHostDiagnostics(host);
96-
const providerComposition = await createDaemonProviderRuntimeComposition(env);
96+
const providerComposition = await createDaemonProviderRuntimeComposition(host);
9797
// The registry has set up every plugin. Until this function returns, nothing else owns its
9898
// cleanup, so any later failure closes it here; success transfers ownership to the daemon.
9999
try {

‎src/platform-runtime-android-adb-binding.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ vi.mock('@agent-device/host-kit/command', async (importOriginal) => {
2626
});
2727

2828
import { createPlatformRuntimeGateway } from './platform-runtime.ts';
29-
import { createLiveTestHostEnvironment } from '@agent-device/test-kit';
29+
import { bindTestCommandHost, createLiveTestHostEnvironment } from '@agent-device/test-kit';
3030

3131
const sessionArtifacts = {
3232
sessionsDir: '/sessions',
@@ -102,3 +102,5 @@ test('the composed gateway reads Android app state from inside the platform pack
102102

103103
await gateway.shutdown();
104104
});
105+
106+
bindTestCommandHost();

‎src/platform-runtime-android-adb-host.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
parseAndroidAdbArgv,
1212
runAndroidHostAdb,
1313
} from '@agent-device/platform-android/mechanics';
14-
import { ANDROID_EMULATOR, mkdtempForTestSync } from '@agent-device/test-kit';
14+
import { bindTestCommandHost, ANDROID_EMULATOR, mkdtempForTestSync } from '@agent-device/test-kit';
1515
import './platform-runtime-android-adb-host.ts';
1616

1717
const UNGUARDED_DEVICE_SHELL = {
@@ -324,3 +324,5 @@ test.skipIf(process.platform === 'win32')(
324324
assert.equal(fs.existsSync(markerPath), true);
325325
},
326326
);
327+
328+
bindTestCommandHost();

‎src/platform-runtime-android-adb-host.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { assertDeviceShellArgv } from '@agent-device/kernel/device-shell';
44
import { createHash, randomUUID } from 'node:crypto';
55
import {
66
coerceExecResult,
7+
commandHostEnvironment,
78
execFailureDetails,
89
runCmd,
910
runCmdBackground,
@@ -27,8 +28,6 @@ import {
2728
import os from 'node:os';
2829
import path from 'node:path';
2930

30-
const environment = process.env;
31-
3231
/**
3332
* The adb process boundary: a `shell`/`exec-out` command reaches the device's `sh` verbatim, so the
3433
* command must have been minted by the device-shell funnel. Checked on the command, not the lowered
@@ -39,7 +38,9 @@ function requireQuotedDeviceShellCommand(command: readonly string[]): void {
3938
}
4039

4140
bindAndroidAdbHost({
42-
environment,
41+
get environment() {
42+
return commandHostEnvironment().env;
43+
},
4344
files: {
4445
access: async (candidate) => await access(candidate),
4546
ensureDirectory: async (directory) => {
@@ -93,7 +94,7 @@ bindAndroidAdbHost({
9394
},
9495
execAdb: async (invocation, options) => {
9596
requireQuotedDeviceShellCommand(invocation.command);
96-
const lowered = lowerAndroidAdbInvocation(invocation, options, environment);
97+
const lowered = lowerAndroidAdbInvocation(invocation, options, commandHostEnvironment().env);
9798
return await runCmd('adb', lowered.args, {
9899
...lowered.options,
99100
// adb's fork-server is a grandchild: without its own process group a deadline can only
@@ -103,7 +104,7 @@ bindAndroidAdbHost({
103104
},
104105
spawnAdb: (invocation, options) => {
105106
requireQuotedDeviceShellCommand(invocation.command);
106-
const lowered = lowerAndroidAdbInvocation(invocation, options, environment);
107+
const lowered = lowerAndroidAdbInvocation(invocation, options, commandHostEnvironment().env);
107108
const background = runCmdBackground('adb', lowered.args, {
108109
...lowered.options,
109110
allowFailure: true,

‎src/platform-runtime-app-log-android-transport.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
AppLogProcessTransport,
44
} from '@agent-device/contracts/app-log-runtime';
55
import type { HostCommandResult } from '@agent-device/contracts/platform-runtime-host';
6+
import type { EnvMap } from '@agent-device/kernel/source-value';
67
import type { DeviceInfo } from '@agent-device/kernel/device';
78
import type { AndroidAdbProcess } from '@agent-device/platform-android/mechanics';
89
import {
@@ -15,20 +16,21 @@ export async function resolveAndroidAppLogProcessTransport(
1516
sessionsDir: string,
1617
device: DeviceInfo,
1718
local: AppLogProcessTransport,
19+
env: EnvMap,
1820
): Promise<AppLogProcessTransport> {
1921
const { resolveScopedAndroidAdbBackgroundTransport } = await loadAndroidMechanics();
2022
const transport = resolveScopedAndroidAdbBackgroundTransport(device);
2123
if (transport.mode === 'local') return local;
2224
if (!transport.spawn) return Object.freeze({ mode: 'transport-composed' });
2325
const spawn = transport.spawn;
24-
const processes = createManagedAppLogProcesses(sessionsDir, {
26+
const processes = createManagedAppLogProcesses(sessionsDir, env, {
2527
launch: async (command, signal) => {
2628
signal?.throwIfAborted();
2729
const adb = providerAdbCommand(command, device.id);
2830
const child = spawn([...adb.args], {
2931
allowFailure: adb.options?.allowFailure,
3032
cwd: adb.options?.cwd,
31-
env: adb.options?.env ? { ...process.env, ...adb.options.env } : undefined,
33+
env: { ...env, ...adb.options?.env },
3234
captureOutput: false,
3335
signal,
3436
});

0 commit comments

Comments
 (0)