Skip to content

Commit 6a0fb60

Browse files
thymikeeclaude
andcommitted
refactor(host): default the hub app cwd to the host in one place and pass the held env to host diagnostics
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 0b6ab69 commit 6a0fb60

13 files changed

Lines changed: 62 additions & 43 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,7 @@ export async function resolveBrowserStackAppReference(
171171
return await resolveHubAppReference({
172172
service: 'BrowserStack',
173173
app,
174-
cwd:
175-
options.cwd ?? (await import('@agent-device/host-kit/command')).commandHostEnvironment().cwd,
174+
cwd: options.cwd,
176175
referenceLabel: 'a bs:// app id',
177176
parseReference: parseBrowserStackAppReference,
178177
uploadFile: async (appPath, signal) => await uploadBrowserStackApp(appPath, options, signal),

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { resolveHubAppReference as resolveHubAppReferenceFrom } from './webdriver-utils.ts';
21
import type { CloudWebDriverRuntimeOptions, CloudWebDriverRuntime } from './runtime.ts';
32

43
export async function createCloudWebDriverRuntime(
@@ -28,14 +27,8 @@ export {
2827
requireProviderDeviceOrientation,
2928
requireRequest,
3029
requireRequestPlatform,
30+
resolveHubAppReference,
3131
resolveLocalAppArtifact,
3232
} from './webdriver-utils.ts';
3333
export { cloudArtifactsReadyOrPending, urlArtifactFromDetails } from './artifact-results.ts';
3434
export { buildCloudWebDriverBaseCapabilities } from './capabilities.ts';
35-
36-
/** The released entry: an app path without `cwd` resolves against the process's working directory. */
37-
export async function resolveHubAppReference(
38-
options: Omit<Parameters<typeof resolveHubAppReferenceFrom>[0], 'cwd'> & { cwd?: string },
39-
): Promise<string> {
40-
return await resolveHubAppReferenceFrom({ ...options, cwd: options.cwd ?? process.cwd() });
41-
}

‎packages/provider-webdriver/src/webdriver-utils.test.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
22
import { promises as fs } from 'node:fs';
33
import path from 'node:path';
44
import { afterEach, test, vi } from 'vitest';
5-
import { runCmd } from '@agent-device/host-kit/command';
5+
import { runCmd, withCommandHost } from '@agent-device/host-kit/command';
66
import { AppError } from '@agent-device/kernel/errors';
77
import { asOptionalRecord } from '@agent-device/kernel/record';
88
import {
@@ -141,6 +141,31 @@ test('the hub app resolver passes references through, uploads local files, and p
141141
}
142142
});
143143

144+
test('the hub app resolver resolves a relative app against the host working directory when no cwd is given', async () => {
145+
const hostDir = await mkdtempForTest('agent-device-hub-host-cwd-');
146+
try {
147+
await fs.writeFile(path.join(hostDir, 'App.apk'), 'placeholder');
148+
const uploadFile = vi.fn(async (appPath: string) => `hub://${path.basename(appPath)}`);
149+
150+
const reference = await withCommandHost(
151+
{ host: { env: {}, cwd: hostDir }, requestEnv: undefined },
152+
async () =>
153+
await resolveHubAppReference({
154+
service: 'Hub',
155+
app: 'App.apk',
156+
referenceLabel: 'a hub:// app id',
157+
...hubReferenceGrammar,
158+
uploadFile,
159+
}),
160+
);
161+
162+
assert.equal(reference, 'hub://App.apk');
163+
assert.deepEqual(uploadFile.mock.calls, [[path.join(hostDir, 'App.apk'), undefined]]);
164+
} finally {
165+
await fs.rm(hostDir, { recursive: true, force: true });
166+
}
167+
});
168+
144169
test('appFileUploadForm carries a regular app file and refuses anything else typed', async () => {
145170
const tempDir = await mkdtempForTest('agent-device-upload-form-');
146171
try {

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ export function createHubUploadApp(
150150
export async function resolveHubAppReference(options: {
151151
service: string;
152152
app: string;
153-
cwd: string;
153+
/** Defaults to the working directory of the host the request runs on. */
154+
cwd?: string;
154155
/** How the scheme reads in the error message, e.g. `a bs:// app id`. */
155156
referenceLabel: string;
156157
/**
@@ -165,7 +166,7 @@ export async function resolveHubAppReference(options: {
165166
const reference = options.parseReference(app);
166167
if (reference !== undefined) return reference;
167168
if (/^https?:\/\//i.test(app)) return app;
168-
const appPath = path.resolve(options.cwd, app);
169+
const appPath = path.resolve(options.cwd ?? (await hostWorkingDirectory()), app);
169170
if (!fs.existsSync(appPath)) {
170171
throw new AppError(
171172
'INVALID_ARGS',
@@ -380,3 +381,8 @@ export function resolveLocalAppArtifact(app: string, cwd: string, service: strin
380381
if (fs.statSync(resolved, { throwIfNoEntry: false })?.isFile()) return resolved;
381382
throw new AppError('INVALID_ARGS', `${service} app file not found: ${resolved}`);
382383
}
384+
385+
// Loaded on demand so the provider closure does not evaluate host-kit's command module eagerly.
386+
async function hostWorkingDirectory(): Promise<string> {
387+
return (await import('@agent-device/host-kit/command')).commandHostEnvironment().cwd;
388+
}

‎src/cli/commands/recording.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ export const recordingCommand: ClientCommandHandler = async ({ positionals, flag
2727
: undefined;
2828

2929
// Lazy: createAgentDevice pulls in the client-side command runtime, which only this action needs.
30-
const [{ createAgentDevice }, { createLocalArtifactAdapterAt }] = await Promise.all([
30+
const [{ createAgentDevice }, { createLocalArtifactAdapter }] = await Promise.all([
3131
import('../../runtime.ts'),
3232
import('../../io.ts'),
3333
]);
3434
const runtime = createAgentDevice({
3535
backend: { platform: 'ios' },
36-
artifacts: createLocalArtifactAdapterAt({ cwd: commandHostEnvironment().cwd }),
36+
artifacts: createLocalArtifactAdapter({ cwd: commandHostEnvironment().cwd }),
3737
sessions: {
3838
get: (name) => ({ name }),
3939
set: () => {},

‎src/cli/commands/screenshot.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ export const diffCommand: ClientCommandHandler = async ({ positionals, flags, cl
6767

6868
// Lazy: createAgentDevice pulls the whole client-side command runtime
6969
// (including screenshot pixel diffing), which only `diff screenshot` needs.
70-
const [{ createAgentDevice }, { createLocalArtifactAdapterAt }] = await Promise.all([
70+
const [{ createAgentDevice }, { createLocalArtifactAdapter }] = await Promise.all([
7171
import('../../runtime.ts'),
7272
import('../../io.ts'),
7373
]);
7474
const runtime = createAgentDevice({
7575
backend: createClientScreenshotBackend(client, flags),
76-
artifacts: createLocalArtifactAdapterAt({ cwd: commandHostEnvironment().cwd }),
76+
artifacts: createLocalArtifactAdapter({ cwd: commandHostEnvironment().cwd }),
7777
sessions: {
7878
get: (name) => ({ name }),
7979
set: () => {},

‎src/cli/replay-test/reporters/__tests__/custom.test.ts‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import assert from 'node:assert/strict';
22
import fs from 'node:fs/promises';
33
import path from 'node:path';
44
import { test } from 'vitest';
5+
import { withCommandHost } from '@agent-device/host-kit/command';
56
import { AppError } from '@agent-device/kernel/errors';
67
import { createCustomReplayTestReporter } from '../custom.ts';
78
import { mkdtempForTest, bindTestCommandHost } from '@agent-device/test-kit';
@@ -139,21 +140,21 @@ test('rejects malformed reporter hooks when the module is loaded', async () => {
139140
}
140141
});
141142

142-
test('expands a ~/ reporter path against the host HOME', async () => {
143+
test('expands a ~/ reporter path against the host HOME, not the process HOME', async () => {
143144
const home = await mkdtempForTest('agent-device-reporter-home-');
144-
const previousHome = process.env.HOME;
145145
try {
146146
await fs.writeFile(path.join(home, 'reporter.mjs'), "export default { name: 'home' };", 'utf8');
147-
process.env.HOME = home;
148-
const reporter = await createCustomReplayTestReporter({
149-
kind: 'custom',
150-
modulePath: '~/reporter.mjs',
151-
raw: '~/reporter.mjs',
152-
});
147+
const reporter = await withCommandHost(
148+
{ host: { env: { HOME: home }, cwd: home }, requestEnv: undefined },
149+
async () =>
150+
await createCustomReplayTestReporter({
151+
kind: 'custom',
152+
modulePath: '~/reporter.mjs',
153+
raw: '~/reporter.mjs',
154+
}),
155+
);
153156
assert.equal(reporter.name, 'home');
154157
} finally {
155-
if (previousHome === undefined) delete process.env.HOME;
156-
else process.env.HOME = previousHome;
157158
await fs.rm(home, { recursive: true, force: true });
158159
}
159160
});

‎src/daemon-composition.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export const createDaemonRuntimeDeps: ComposeDaemonRuntimeDeps = async (
8888
sessionsDir,
8989
resolveSessionDir,
9090
});
91-
const hostDiagnostics = createHostDiagnostics();
91+
const hostDiagnostics = createHostDiagnostics(env);
9292
const providerComposition = await createDaemonProviderRuntimeComposition(env);
9393
const providerDeviceRuntimes = [...providerComposition.runtimes];
9494
const deviceRuntimeGateway = createPlatformRuntimeGateway({

‎src/io.ts‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,10 @@ import type {
77
LocalArtifactAdapterOptions,
88
} from '@agent-device/contracts/artifact-adapter';
99

10-
/** The released entry: without `cwd`, relative paths resolve against the process's working directory. */
1110
export function createLocalArtifactAdapter(
1211
options: LocalArtifactAdapterOptions = {},
1312
): ArtifactAdapter {
14-
return createLocalArtifactAdapterAt({ ...options, cwd: options.cwd ?? process.cwd() });
15-
}
16-
17-
export function createLocalArtifactAdapterAt(
18-
options: LocalArtifactAdapterOptions & { cwd: string },
19-
): ArtifactAdapter {
20-
const { cwd } = options;
13+
const cwd = options.cwd ?? process.cwd();
2114
const tempDir = options.tempDir ?? os.tmpdir();
2215
const rootDir = options.rootDir ? resolveLocalPath(options.rootDir, cwd) : undefined;
2316

‎src/platform-runtime-host-diagnostics.test.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ test('warmup hands an Apple device to the Apple runner warmup and returns its ch
3535
};
3636
mockAppleRunnerWarmupCheck.mockResolvedValue(check);
3737

38-
const result = await createHostDiagnostics().warmupCheck(IOS_SIMULATOR, context);
38+
const result = await createHostDiagnostics(process.env).warmupCheck(IOS_SIMULATOR, context);
3939

4040
expect(result).toEqual(check);
4141
expect(mockAppleRunnerWarmupCheck).toHaveBeenCalledWith(IOS_SIMULATOR, context);
4242
});
4343

4444
test('warmup runs no probe for a non-Apple device', async () => {
45-
expect(await createHostDiagnostics().warmupCheck(ANDROID_EMULATOR, context)).toBeUndefined();
45+
expect(
46+
await createHostDiagnostics(process.env).warmupCheck(ANDROID_EMULATOR, context),
47+
).toBeUndefined();
4648
expect(mockAppleRunnerWarmupCheck).not.toHaveBeenCalled();
4749
});

0 commit comments

Comments
 (0)