Skip to content

Commit 0688fd0

Browse files
committed
refactor(limrun): build adb invocations through the platform builders
The provider wrote an AndroidAdbTarget by hand, so a field added to that type could drift without an error. The composition root now hands it deviceAdbInvocation and hostAdbInvocation, both built with androidAdbInvocation over androidAdbSerialTarget / the new androidAdbHostTarget, which is also what parseAndroidAdbArgv reads an unaddressed argv as. The provider keeps the addressing decision, not its syntax. It cannot import the mechanics facet itself: ADR-0019 keeps a provider's eager closure off concrete platform implementation, which eager-closure-budgets.test.ts reports as soon as the edge appears.
1 parent 164a9fd commit 0688fd0

15 files changed

Lines changed: 137 additions & 59 deletions

‎packages/platform-android/src/adb-transport.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ADB_MANAGED_FORBIDDEN_COMMANDS,
55
ADB_WAIT_STATES,
66
ADB_WAIT_TRANSPORTS,
7+
androidAdbHostTarget,
78
androidAdbInvocation,
89
androidAdbPayloadWithoutSerial,
910
androidAdbSerialTarget,
@@ -255,6 +256,20 @@ describe('requireAndroidAdbServerPort', () => {
255256
});
256257
});
257258

259+
describe('androidAdbHostTarget', () => {
260+
it('addresses a server-level command without any device option', () => {
261+
const invocation = androidAdbInvocation(androidAdbHostTarget(), [
262+
'disconnect',
263+
'127.0.0.1:5037',
264+
]);
265+
expect(serializeAndroidAdbInvocation(invocation)).toEqual(['disconnect', '127.0.0.1:5037']);
266+
});
267+
268+
it('is what an argv naming neither device nor server reads as', () => {
269+
expect(parseAndroidAdbArgv(['devices']).target).toEqual(androidAdbHostTarget());
270+
});
271+
});
272+
258273
describe('serializeAndroidAdbInvocation', () => {
259274
it('emits owned addressing ahead of the command in adb order', () => {
260275
const invocation = androidAdbInvocation(

‎packages/platform-android/src/adb-transport.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,11 @@ export function androidAdbSerialTarget(
319319
};
320320
}
321321

322+
/** Addressing for a server-level command: it selects no device and names no private adb server. */
323+
export function androidAdbHostTarget(): Readonly<AndroidAdbTarget> {
324+
return { selector: { kind: 'unspecified' }, server: { kind: 'ambient' } };
325+
}
326+
322327
export function androidAdbInvocation(
323328
target: AndroidAdbTarget,
324329
command: readonly string[],
@@ -428,9 +433,10 @@ function applyAndroidAdbOptionEffect(
428433
* accept (ambient adb) or refuse (managed transport).
429434
*/
430435
export function parseAndroidAdbArgv(args: readonly string[]): AndroidAdbInvocation {
436+
const host = androidAdbHostTarget();
431437
const read: AndroidAdbAddressingRead = {
432-
selector: { kind: 'unspecified' },
433-
server: { kind: 'ambient' },
438+
selector: host.selector,
439+
server: host.server,
434440
waitFor: undefined,
435441
hostGlobals: [],
436442
};

‎packages/platform-android/src/mechanics.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* the root composition module.
77
*/
88
export {
9+
androidAdbHostTarget,
910
androidAdbInvocation,
1011
androidAdbSerialTarget,
1112
lowerAndroidAdbInvocation,

‎packages/provider-limrun/src/android.test.ts‎

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,30 @@
11
import { expect, test, vi } from 'vitest';
22
import {
3+
androidAdbHostTarget,
4+
androidAdbInvocation,
35
serializeAndroidAdbInvocation,
46
type AndroidAdbInvocation,
57
} from '@agent-device/platform-android/mechanics';
6-
import {
7-
cleanupLimrunAndroidAdbTunnel,
8-
limrunDeviceAdbInvocation,
9-
limrunHostAdbInvocation,
10-
type LimrunAndroidSession,
11-
} from './android.ts';
8+
import { cleanupLimrunAndroidAdbTunnel, type LimrunAndroidSession } from './android.ts';
129
import type { LimrunAdbCommandOptions, LimrunAdbProvider } from './runtime-dependencies.ts';
1310

1411
const ok = { exitCode: 0, stdout: '', stderr: '' };
1512

16-
test('carries the tunnel serial on the invocation target and never in the command', () => {
17-
const device = limrunDeviceAdbInvocation('127.0.0.1:62001', ['shell', 'pm', 'list', 'packages']);
18-
expect(device.target).toEqual({
19-
selector: { kind: 'serial', serial: '127.0.0.1:62001' },
20-
server: { kind: 'ambient' },
21-
});
22-
expect(device.command).toEqual(['shell', 'pm', 'list', 'packages']);
23-
expect(device.rawArgv).toBeUndefined();
24-
expect(serializeAndroidAdbInvocation(device)).toEqual([
25-
'-s',
26-
'127.0.0.1:62001',
27-
'shell',
28-
'pm',
29-
'list',
30-
'packages',
31-
]);
32-
});
33-
34-
test('addresses a server-level command to no device', () => {
35-
const host = limrunHostAdbInvocation(['disconnect', '127.0.0.1:62001']);
36-
expect(host.target.selector).toEqual({ kind: 'unspecified' });
37-
expect(serializeAndroidAdbInvocation(host)).toEqual(['disconnect', '127.0.0.1:62001']);
38-
});
39-
40-
test('cleanup disconnects the tunnel serial as a server-level command and drops it', async () => {
13+
test('cleanup asks the platform to address the disconnect as a server-level command and hands it over unchanged', async () => {
4114
const calls: Array<{ argv: string[]; options: LimrunAdbCommandOptions | undefined }> = [];
4215
const close = vi.fn();
4316
const provider: LimrunAdbProvider = { exec: async () => ok };
17+
const hostAdbInvocation = vi.fn((command: readonly string[]) =>
18+
androidAdbInvocation(androidAdbHostTarget(), command),
19+
);
4420
const session = {
4521
platform: 'android',
4622
adbProvider: provider,
4723
adbSerial: '127.0.0.1:62001',
4824
adbTunnel: { close },
4925
adbTunnelPromise: Promise.resolve(),
5026
dependencies: {
27+
android: { hostAdbInvocation },
5128
host: {
5229
runAdb: async (invocation: AndroidAdbInvocation, options?: LimrunAdbCommandOptions) => {
5330
calls.push({ argv: serializeAndroidAdbInvocation(invocation), options });
@@ -59,6 +36,7 @@ test('cleanup disconnects the tunnel serial as a server-level command and drops
5936

6037
await cleanupLimrunAndroidAdbTunnel(session);
6138

39+
expect(hostAdbInvocation).toHaveBeenCalledWith(['disconnect', '127.0.0.1:62001']);
6240
expect(calls).toEqual([
6341
{
6442
argv: ['disconnect', '127.0.0.1:62001'],

‎packages/provider-limrun/src/android.ts‎

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -137,33 +137,13 @@ export async function configureLimrunAndroidPortReverse(
137137
});
138138
}
139139

140-
/**
141-
* Addresses one command to the tunnel's persistent serial on whatever adb the host resolves. The
142-
* serial is this provider's own addressing decision, so it belongs to the target and never to the
143-
* command array the Android cluster handed over.
144-
*/
145-
export function limrunDeviceAdbInvocation(serial: string, args: string[]): AndroidAdbInvocation {
146-
return {
147-
target: { selector: { kind: 'serial', serial }, server: { kind: 'ambient' } },
148-
command: args,
149-
};
150-
}
151-
152-
/** Addresses one server-level command, which selects no device. */
153-
export function limrunHostAdbInvocation(args: string[]): AndroidAdbInvocation {
154-
return {
155-
target: { selector: { kind: 'unspecified' }, server: { kind: 'ambient' } },
156-
command: args,
157-
};
158-
}
159-
160140
export async function cleanupLimrunAndroidAdbTunnel(session: LimrunAndroidSession): Promise<void> {
161141
await session.adbTunnelPromise?.catch(() => {});
162142
const serial = session.adbSerial;
163143
if (serial) {
164144
await cleanupAndroidPortReverse(session);
165145
await session.dependencies.host
166-
.runAdb(limrunHostAdbInvocation(['disconnect', serial]), {
146+
.runAdb(session.dependencies.android.hostAdbInvocation(['disconnect', serial]), {
167147
allowFailure: true,
168148
timeoutMs: 10_000,
169149
})
@@ -215,7 +195,7 @@ async function executeLimrunAndroidAdb(
215195
options?: LimrunAdbCommandOptions,
216196
): Promise<{ invocation: AndroidAdbInvocation; result: LimrunAdbCommandResult }> {
217197
const serial = await ensurePersistentAndroidAdbSerial(session);
218-
const invocation = limrunDeviceAdbInvocation(serial, args);
198+
const invocation = session.dependencies.android.deviceAdbInvocation(serial, args);
219199
const result = await session.dependencies.host.runAdb(invocation, {
220200
allowFailure: options?.allowFailure,
221201
binaryStdout: options?.binaryStdout,

‎packages/provider-limrun/src/app-log-reconnect.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ vi.mock('@limrun/api/instance-client', () => ({
2424
}));
2525

2626
import {
27+
androidAdbHostTarget,
28+
androidAdbInvocation,
29+
androidAdbSerialTarget,
2730
serializeAndroidAdbInvocation,
2831
type AndroidAdbInvocation,
2932
} from '@agent-device/platform-android/mechanics';
@@ -170,6 +173,10 @@ test('addresses app-log adb traffic at the tunnel serial and hands cleanup a com
170173
},
171174
},
172175
android: {
176+
deviceAdbInvocation: (serial: string, command: readonly string[]) =>
177+
androidAdbInvocation(androidAdbSerialTarget(serial), command),
178+
hostAdbInvocation: (command: readonly string[]) =>
179+
androidAdbInvocation(androidAdbHostTarget(), command),
173180
readLogs: async (adb: LimrunAdbExecutor) => {
174181
await adb(['shell', 'logcat', '-d', '-T', '20']);
175182
return 'line\n';

‎packages/provider-limrun/src/app-log-reconnect.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type { LimrunAppLogDescriptor } from './app-log-descriptor.ts';
77
import type { LimrunAppLogReader } from './app-log-poller.ts';
88
import type { LimrunAppLogReconnectOutcome } from './app-log-runtime.ts';
99
import type { LimrunRuntimeDependencies } from './runtime-dependencies.ts';
10-
import { limrunDeviceAdbInvocation, limrunHostAdbInvocation } from './android.ts';
1110

1211
export async function reconnectLimrunAppLogReader(options: {
1312
limrun: Limrun;
@@ -96,7 +95,7 @@ async function reconnectAndroid(options: {
9695
commandOptions?: Parameters<LimrunRuntimeDependencies['host']['runAdb']>[1],
9796
) =>
9897
await options.dependencies.host.runAdb(
99-
limrunDeviceAdbInvocation(serial, args),
98+
options.dependencies.android.deviceAdbInvocation(serial, args),
10099
commandOptions,
101100
);
102101
const reader: LimrunAppLogReader = {
@@ -107,7 +106,7 @@ async function reconnectAndroid(options: {
107106
await options.dependencies.android.readLogs(adb, lineLimit),
108107
[Symbol.asyncDispose]: async () => {
109108
await options.dependencies.host
110-
.runAdb(limrunHostAdbInvocation(['disconnect', serial]), {
109+
.runAdb(options.dependencies.android.hostAdbInvocation(['disconnect', serial]), {
111110
allowFailure: true,
112111
timeoutMs: 10_000,
113112
})

‎packages/provider-limrun/src/device-session.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import type { AppsFilter, DeviceLease } from '@agent-device/contracts/device';
44
import type { Interactor } from '@agent-device/contracts/interactor-types';
55
import type { DeviceInfo } from '@agent-device/kernel/device';
66
import { AppError } from '@agent-device/kernel/errors';
7+
import {
8+
androidAdbHostTarget,
9+
androidAdbInvocation,
10+
androidAdbSerialTarget,
11+
} from '@agent-device/platform-android/mechanics';
712
import type {
813
LimrunAdbExecutor,
914
LimrunAdbProvider,
@@ -44,6 +49,9 @@ const TEST_DEPENDENCIES = {
4449
dismissed: false,
4550
}),
4651
readLogs: async () => 'log line\n',
52+
deviceAdbInvocation: (serial, command) =>
53+
androidAdbInvocation(androidAdbSerialTarget(serial), command),
54+
hostAdbInvocation: (command) => androidAdbInvocation(androidAdbHostTarget(), command),
4755
adbError: async (message) => new AppError('COMMAND_FAILED', message),
4856
},
4957
host: {

‎packages/provider-limrun/src/runtime-dependencies.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import type { AppsFilter, DeviceLease } from '@agent-device/contracts/device';
44
import type { Interactor } from '@agent-device/contracts/interactor-types';
55
import type { DeviceInfo } from '@agent-device/kernel/device';
66
import {
7+
androidAdbHostTarget,
8+
androidAdbInvocation,
9+
androidAdbSerialTarget,
710
serializeAndroidAdbInvocation,
811
type AndroidAdbInvocation,
912
} from '@agent-device/platform-android/mechanics';
@@ -295,6 +298,10 @@ function createContractFixture() {
295298
dismissed: false,
296299
}),
297300
readLogs: async () => 'log line\n',
301+
deviceAdbInvocation: (serial: string, command: readonly string[]) =>
302+
androidAdbInvocation(androidAdbSerialTarget(serial), command),
303+
hostAdbInvocation: (command: readonly string[]) =>
304+
androidAdbInvocation(androidAdbHostTarget(), command),
298305
adbError: async (message: string) => new AppError('COMMAND_FAILED', message),
299306
},
300307
host: {

‎packages/provider-limrun/src/runtime-dependencies.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ export type LimrunAndroidRuntimeAdapter = {
8989
getKeyboardState(adb: LimrunAdbExecutor): Promise<LimrunAndroidKeyboardState>;
9090
dismissKeyboard(adb: LimrunAdbExecutor): Promise<LimrunAndroidKeyboardDismissResult>;
9191
readLogs(adb: LimrunAdbExecutor, lineLimit: number): Promise<string>;
92+
/**
93+
* Addresses one device command at `serial`, the tunnel this provider opened. The serial is the
94+
* provider's own addressing decision and belongs to the target, so the command array the Android
95+
* cluster handed over reaches the host unchanged. The builders come from the composition root
96+
* rather than an import: ADR-0019 keeps a provider's eager closure off the platform
97+
* implementation, and an invocation is built by the platform's typed grammar.
98+
*/
99+
deviceAdbInvocation(serial: string, command: readonly string[]): AndroidAdbInvocation;
100+
/** Addresses one server-level command, which selects no device. */
101+
hostAdbInvocation(command: readonly string[]): AndroidAdbInvocation;
92102
/**
93103
* Builds the failure an ADB command answered with. The invocation is what was asked of adb; how
94104
* it is named in the error belongs to whoever renders it, not to this provider.

0 commit comments

Comments
 (0)