Skip to content

Commit ec37b5f

Browse files
committed
refactor(host): read Linux, Vega, web and HarmonyOS configuration from an explicit host (#3794)
1 parent f6fc4f9 commit ec37b5f

39 files changed

Lines changed: 741 additions & 177 deletions

‎packages/daemon/src/__tests__/support/snapshot-runtime-fixture.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ export function snapshotRuntimeFixture(requestId?: string): Readonly<{
113113
// runner underneath is the suite's own mock.
114114
const runnerOptions = { signal: requestSignal };
115115
const appleInteractor = isApplePlatform(device.platform)
116-
? await applePlugin.createInteractor(device, runnerOptions)
116+
? await applePlugin.createInteractor(device, runnerOptions, {
117+
env: process.env,
118+
cwd: process.cwd(),
119+
})
117120
: undefined;
118121
const alertOptions = (input: AlertRuntimeInput) => ({
119122
...(input.timeoutMs === undefined ? {} : { timeoutMs: input.timeoutMs }),

‎packages/platform-harmonyos/src/__tests__/hdc.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ test('ensureHarmonyToolchainPathConfigured defaults to the host environment', as
144144
vi.stubEnv('PATH', '/usr/bin');
145145
vi.stubEnv('HDC_SDK_PATH', toolchains);
146146

147-
await ensureHarmonyToolchainPathConfigured();
147+
await ensureHarmonyToolchainPathConfigured(process.env);
148148

149149
assert.equal(process.env.PATH, [toolchains, '/usr/bin'].join(path.delimiter));
150150
});

‎packages/platform-harmonyos/src/hdc.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
runCmd,
1111
type ExecOptions,
1212
type ExecResult,
13-
commandHostEnvironment,
1413
} from '@agent-device/host-kit/command';
1514

1615
export type HarmonyHdcOptions = Pick<
@@ -49,9 +48,7 @@ export async function runHarmonyShell(
4948
* DevEco's command-line tools do not amend PATH for non-interactive processes.
5049
* Honor the documented roots so the daemon sees the same HDC binary as a shell.
5150
*/
52-
export async function ensureHarmonyToolchainPathConfigured(
53-
env: NodeJS.ProcessEnv = commandHostEnvironment().env,
54-
): Promise<void> {
51+
export async function ensureHarmonyToolchainPathConfigured(env: NodeJS.ProcessEnv): Promise<void> {
5552
const toolchainRoots = [
5653
env.HDC_SDK_PATH,
5754
env.DEVECO_SDK_HOME

‎packages/platform-linux/src/__tests__/input-actions.test.ts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ vi.mock('@agent-device/host-kit/command', async (importOriginal) => {
88
});
99

1010
import { runCmd, whichCmd } from '@agent-device/host-kit/command';
11-
import { resetInputToolCache } from '../linux-env.ts';
1211
import { fillLinux, pressLinux, scrollLinux, typeLinux, sendKey } from '../input-actions.ts';
1312

1413
bindTestCommandHost();
@@ -23,7 +22,6 @@ function setupXdotool(): void {
2322
Object.defineProperty(process, 'platform', { value: 'linux' });
2423
process.env['XDG_SESSION_TYPE'] = 'x11';
2524
delete process.env['WAYLAND_DISPLAY'];
26-
resetInputToolCache();
2725
mockWhichCmd.mockImplementation(async (cmd) => cmd === 'xdotool');
2826
mockRunCmd.mockResolvedValue({ exitCode: 0, stdout: '', stderr: '' });
2927
}
@@ -32,7 +30,6 @@ function setupYdotool(): void {
3230
Object.defineProperty(process, 'platform', { value: 'linux' });
3331
process.env['XDG_SESSION_TYPE'] = 'wayland';
3432
process.env['WAYLAND_DISPLAY'] = 'wayland-0';
35-
resetInputToolCache();
3633
mockWhichCmd.mockImplementation(async (cmd) => cmd === 'ydotool');
3734
mockRunCmd.mockResolvedValue({ exitCode: 0, stdout: '', stderr: '' });
3835
}
@@ -45,7 +42,6 @@ function calls(): Array<[string, string[]]> {
4542
beforeEach(() => {
4643
mockRunCmd.mockReset();
4744
mockWhichCmd.mockReset();
48-
resetInputToolCache();
4945
});
5046

5147
afterAll(() => {
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { afterAll, beforeEach, test, vi } from 'vitest';
2+
import assert from 'node:assert/strict';
3+
import { bindTestCommandHost } from '@agent-device/test-kit';
4+
5+
vi.mock('@agent-device/host-kit/command', async (importOriginal) => {
6+
const actual = await importOriginal<typeof import('@agent-device/host-kit/command')>();
7+
return { ...actual, runCmd: vi.fn(), whichCmd: vi.fn() };
8+
});
9+
10+
import { runCmd, whichCmd } from '@agent-device/host-kit/command';
11+
import { createLinuxInteractor } from '../interactor.ts';
12+
import type { HostEnvironment } from '@agent-device/kernel/source-value';
13+
14+
bindTestCommandHost();
15+
16+
// Two composing runtimes drive the plain local provider through their own hosts. Each interactor
17+
// keeps its own display decision and input-tool cache: A(X11) → B(Wayland) → A resolves
18+
// xdotool → ydotool → xdotool, against a process environment that claims Wayland for everyone,
19+
// and no cache resets between the calls. The old cache keyed by the default singleton answered
20+
// host A's xdotool for host B (#3794).
21+
22+
const originalPlatform = process.platform;
23+
const originalEnv = { ...process.env };
24+
25+
const mockRunCmd = vi.mocked(runCmd);
26+
const mockWhichCmd = vi.mocked(whichCmd);
27+
28+
function hostWithEnv(env: Record<string, string | undefined>): HostEnvironment {
29+
return { env: { ...process.env, ...env }, cwd: process.cwd() };
30+
}
31+
32+
beforeEach(() => {
33+
Object.defineProperty(process, 'platform', { value: 'linux' });
34+
// The process environment claims a Wayland session for the whole machine; neither host names
35+
// this display, so any answer read from the process instead of the runtime host is wrong here.
36+
process.env['WAYLAND_DISPLAY'] = 'wayland-9';
37+
process.env['XDG_SESSION_TYPE'] = 'wayland';
38+
mockRunCmd.mockReset();
39+
mockWhichCmd.mockReset();
40+
mockWhichCmd.mockImplementation(async (cmd) => cmd === 'xdotool' || cmd === 'ydotool');
41+
mockRunCmd.mockResolvedValue({ exitCode: 0, stdout: '', stderr: '' });
42+
});
43+
44+
afterAll(() => {
45+
Object.defineProperty(process, 'platform', { value: originalPlatform });
46+
for (const key of Object.keys(process.env)) {
47+
if (!(key in originalEnv)) delete process.env[key];
48+
}
49+
Object.assign(process.env, originalEnv);
50+
});
51+
52+
function synthesisCommands(): string[] {
53+
return mockRunCmd.mock.calls
54+
.map(([cmd, args]) => ({ cmd, first: (args as string[])[0] }))
55+
.filter(({ cmd, first }) => cmd !== 'xdotool' || first !== 'getmouselocation')
56+
.map(({ cmd }) => cmd);
57+
}
58+
59+
test('interactors on different hosts keep their own display decision and input-tool cache', async () => {
60+
const hostA = hostWithEnv({ WAYLAND_DISPLAY: undefined, XDG_SESSION_TYPE: 'x11' });
61+
const hostB = hostWithEnv({ WAYLAND_DISPLAY: 'wayland-1', XDG_SESSION_TYPE: 'wayland' });
62+
const interactorA = createLinuxInteractor(hostA);
63+
const interactorB = createLinuxInteractor(hostB);
64+
65+
// Phase A: the X11 host resolves xdotool and caches the choice.
66+
await interactorA.tap!(1, 2);
67+
assert.deepEqual(synthesisCommands(), ['xdotool', 'xdotool']);
68+
69+
// Phase B: the Wayland host must resolve ydotool even though A cached xdotool first. The old
70+
// singleton-keyed cache answered A's cached xdotool here, against B's own host.
71+
mockRunCmd.mockClear();
72+
await interactorB.tap!(3, 4);
73+
assert.deepEqual(
74+
mockRunCmd.mock.calls.map(([cmd, args]) => [cmd, ...(args as string[])]),
75+
[
76+
['ydotool', 'mousemove', '--absolute', '-x', '3', '-y', '4'],
77+
['ydotool', 'click', '0xC0'],
78+
],
79+
);
80+
81+
// Phase A again: A's cached choice survived B untouched.
82+
mockRunCmd.mockClear();
83+
await interactorA.tap!(5, 6);
84+
assert.deepEqual(synthesisCommands(), ['xdotool', 'xdotool']);
85+
});

‎packages/platform-linux/src/__tests__/tool-provider.test.ts‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { bindTestCommandHost } from '@agent-device/test-kit';
22
import { afterAll, beforeEach, test } from 'vitest';
33
import assert from 'node:assert/strict';
44
import { pressLinux } from '../input-actions.ts';
5-
import { resetInputToolCache } from '../linux-env.ts';
65
import { createLocalLinuxToolProvider, withLinuxToolProvider } from '../tool-provider.ts';
76

87
bindTestCommandHost();
@@ -14,12 +13,12 @@ beforeEach(() => {
1413
Object.defineProperty(process, 'platform', { value: 'linux' });
1514
process.env['XDG_SESSION_TYPE'] = 'x11';
1615
delete process.env['WAYLAND_DISPLAY'];
17-
resetInputToolCache();
1816
});
1917

2018
test('scoped Linux tool provider handles input discovery and command execution', async () => {
2119
const commands: Array<[string, string[]]> = [];
2220
const provider = createLocalLinuxToolProvider({
21+
host: { env: { ...process.env, XDG_SESSION_TYPE: 'x11' }, cwd: process.cwd() },
2322
whichCommand: async (cmd) => cmd === 'xdotool',
2423
runCommand: async (cmd, args) => {
2524
commands.push([cmd, args]);
@@ -42,6 +41,7 @@ test('scoped Linux tool provider handles input discovery and command execution',
4241
test('xdotool press skips the --sync move when the pointer already sits on the target', async () => {
4342
const commands: Array<[string, string[]]> = [];
4443
const provider = createLocalLinuxToolProvider({
44+
host: { env: { ...process.env, XDG_SESSION_TYPE: 'x11' }, cwd: process.cwd() },
4545
whichCommand: async (cmd) => cmd === 'xdotool',
4646
runCommand: async (cmd, args) => {
4747
commands.push([cmd, args]);
@@ -64,6 +64,7 @@ test('xdotool press skips the --sync move when the pointer already sits on the t
6464
test('scoped Linux input provider handles semantic input without host tool discovery', async () => {
6565
const inputCalls: Array<[string, string, string, string]> = [];
6666
const provider = createLocalLinuxToolProvider({
67+
host: { env: { ...process.env, XDG_SESSION_TYPE: 'x11' }, cwd: process.cwd() },
6768
whichCommand: async (cmd) => {
6869
throw new Error(`unexpected input discovery: ${cmd}`);
6970
},
@@ -92,10 +93,18 @@ test('scoped Linux input provider handles semantic input without host tool disco
9293

9394
test('Linux tool provider scopes do not share cached input tool resolution', async () => {
9495
const providerA = createLocalLinuxToolProvider({
96+
host: {
97+
env: { ...process.env, XDG_SESSION_TYPE: 'x11', WAYLAND_DISPLAY: '' },
98+
cwd: process.cwd(),
99+
},
95100
whichCommand: async (cmd) => cmd === 'xdotool',
96101
runCommand: async () => ({ exitCode: 0, stdout: '', stderr: '' }),
97102
});
98103
const providerB = createLocalLinuxToolProvider({
104+
host: {
105+
env: { ...process.env, XDG_SESSION_TYPE: 'wayland', WAYLAND_DISPLAY: 'wayland-0' },
106+
cwd: process.cwd(),
107+
},
99108
whichCommand: async (cmd) => cmd === 'ydotool',
100109
runCommand: async () => ({ exitCode: 0, stdout: '', stderr: '' }),
101110
});
@@ -110,6 +119,10 @@ test('Linux tool provider scopes do not share cached input tool resolution', asy
110119

111120
await withLinuxToolProvider(
112121
createLocalLinuxToolProvider({
122+
host: {
123+
env: { ...process.env, XDG_SESSION_TYPE: 'wayland', WAYLAND_DISPLAY: 'wayland-0' },
124+
cwd: process.cwd(),
125+
},
113126
...providerB,
114127
runCommand: async (cmd, args) => {
115128
commands.push([cmd, args]);
@@ -130,6 +143,7 @@ test('Linux tool provider scopes do not share cached input tool resolution', asy
130143
test('local Linux desktop provider translates semantic lifecycle calls to host tools', async () => {
131144
const commands: Array<[string, string[], boolean | undefined]> = [];
132145
const provider = createLocalLinuxToolProvider({
146+
host: { env: { ...process.env, XDG_SESSION_TYPE: 'x11' }, cwd: process.cwd() },
133147
whichCommand: async (cmd) => cmd === 'wmctrl',
134148
runCommand: async (cmd, args, options) => {
135149
commands.push([cmd, args, options?.allowFailure]);
@@ -151,6 +165,7 @@ test('local Linux desktop provider translates semantic lifecycle calls to host t
151165
test('local Linux desktop provider falls back to pkill when wmctrl is unavailable', async () => {
152166
const commands: Array<[string, string[], boolean | undefined]> = [];
153167
const provider = createLocalLinuxToolProvider({
168+
host: { env: { ...process.env, XDG_SESSION_TYPE: 'x11' }, cwd: process.cwd() },
154169
whichCommand: async () => false,
155170
runCommand: async (cmd, args, options) => {
156171
commands.push([cmd, args, options?.allowFailure]);
@@ -166,6 +181,7 @@ test('local Linux desktop provider falls back to pkill when wmctrl is unavailabl
166181
test('local Linux screenshot provider translates capture to available host tool', async () => {
167182
const commands: Array<[string, string[]]> = [];
168183
const provider = createLocalLinuxToolProvider({
184+
host: { env: { ...process.env, XDG_SESSION_TYPE: 'x11' }, cwd: process.cwd() },
169185
whichCommand: async (cmd) => cmd === 'scrot',
170186
runCommand: async (cmd, args) => {
171187
commands.push([cmd, args]);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { EnvMap, HostEnvironment } from '@agent-device/kernel/source-value';
2+
3+
/** The Linux family's display-server vocabulary, from one detector (#3794). */
4+
export type DisplayServer = 'wayland' | 'x11';
5+
6+
/**
7+
* The display decision bound to one host. A local provider builds it at construction from its
8+
* runtime's host, so input, clipboard and screenshot selection share one answer that no other
9+
* runtime's host can change (#3794).
10+
*/
11+
export type LinuxDisplayConfig = {
12+
detectDisplayServer(): DisplayServer;
13+
};
14+
15+
/** Reads the display server from one host's environment; the only display decision in the family. */
16+
export function detectLinuxDisplayServer(env: Readonly<EnvMap>): DisplayServer {
17+
if (env['WAYLAND_DISPLAY']) return 'wayland';
18+
if (env['XDG_SESSION_TYPE'] === 'wayland') return 'wayland';
19+
return 'x11';
20+
}
21+
22+
export function createLinuxDisplayConfig(host: HostEnvironment): LinuxDisplayConfig {
23+
return Object.freeze({
24+
detectDisplayServer: () => detectLinuxDisplayServer(host.env),
25+
});
26+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { DisplayServer } from './display.ts';
2+
3+
export type InputTool = 'xdotool' | 'ydotool';
4+
5+
/** The input tool one provider resolved, tagged with the display decision it resolved for. */
6+
export type LinuxInputSelection = { tool: InputTool; display: DisplayServer };
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Interactor } from '@agent-device/contracts/interactor-types';
2+
import type { HostEnvironment } from '@agent-device/kernel/source-value';
23

3-
export async function createLinuxInteractor(): Promise<Interactor> {
4+
export async function createLinuxInteractor(host: HostEnvironment): Promise<Interactor> {
45
const { createLinuxInteractor: create } = await import('./interactor.ts');
5-
return create();
6+
return create(host);
67
}

‎packages/platform-linux/src/interactor.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ vi.mock('./index.ts', () => ({
2929
beforeEach(() => vi.clearAllMocks());
3030

3131
test('Linux owns alternate buttons while the runtime binder owns primary series', async () => {
32-
const interactor = createLinuxInteractor();
32+
const interactor = createLinuxInteractor({ env: process.env, cwd: process.cwd() });
3333

3434
await interactor.alternateClick!({ x: 10, y: 20 }, 'secondary');
3535
await interactor.alternateClick!({ x: 30, y: 40 }, 'middle');
@@ -46,7 +46,7 @@ test('Linux executes a planned fling as one drag with the resolved duration', as
4646
'linux',
4747
);
4848

49-
await createLinuxInteractor().performGesture!(plan);
49+
await createLinuxInteractor({ env: process.env, cwd: process.cwd() }).performGesture!(plan);
5050

5151
assert.deepEqual(vi.mocked(swipeLinux).mock.calls, [[10, 20, 110, 20, 100]]);
5252
});

0 commit comments

Comments
 (0)