Skip to content

Commit 8e894a0

Browse files
committed
fix(remote): collapse a recorded apple family alias when a device gets bound
A connection opened with `--platform apple` records that alias before any device exists. resolveProxyLeaseState already re-keyed deviceKey and leaseBackend from the resolved device while leaving platform on the family axis, so the record named a family while its own deviceKey named one machine. The scope guard answers family-vs-leaf as no-conflict, so a later `--platform macos` walked past it and beat the iOS device's lease under a selector naming a different machine. Binding a device is the moment the family is decided: write the leaf the deviceKey speaks.
1 parent 5760965 commit 8e894a0

2 files changed

Lines changed: 95 additions & 1 deletion

File tree

‎src/__tests__/remote-connection-platform-axis.test.ts‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,91 @@ test('proxy install records the public platform and the next command reuses that
321321
assert.equal(reused.flags.leaseId, 'ios-lease-1');
322322
fs.rmSync(tempRoot, { recursive: true, force: true });
323323
});
324+
325+
// A connection opened with the `apple` family selector records that alias before any device is
326+
// bound. Once a command resolves one specific device, the record must collapse to that device's
327+
// leaf: keeping the alias would let a later command name the OTHER leaf of the same family —
328+
// macOS against an iOS-bound lease — pass the scope guard, because family and leaf never
329+
// conflict, and take the device's lease under a selector that names a different machine.
330+
test('a connection opened on the apple family collapses to the bound device leaf', async () => {
331+
const { tempRoot, stateDir, remoteConfigPath } = connectionWorkspace(
332+
'agent-device-connect-proxy-apple-family-',
333+
);
334+
fs.writeFileSync(remoteConfigPath, JSON.stringify({ daemonBaseUrl: 'https://daemon.example' }));
335+
seedConnectionState({
336+
stateDir,
337+
state: {
338+
session: 'adc-proxy',
339+
remoteConfigPath,
340+
daemon: { baseUrl: 'https://daemon.example' },
341+
tenant: 'proxy',
342+
runId: 'proxy-client-1',
343+
leaseProvider: 'proxy',
344+
clientId: 'client-1',
345+
platform: 'apple',
346+
leaseBackend: 'ios-instance',
347+
},
348+
});
349+
const allocate = recordedLeaseAllocate({ leaseId: 'ios-lease-1', backend: 'ios-instance' });
350+
const client = createTestClient({
351+
listDevices: async () => [
352+
{
353+
platform: 'ios',
354+
target: 'mobile',
355+
kind: 'simulator',
356+
id: 'SIM-001',
357+
name: 'iPhone 16',
358+
booted: true,
359+
identifiers: { udid: 'SIM-001' },
360+
ios: { udid: 'SIM-001' },
361+
},
362+
{
363+
platform: 'macos',
364+
target: 'desktop',
365+
kind: 'device',
366+
id: 'MAC-1',
367+
name: 'Mac',
368+
booted: true,
369+
identifiers: {},
370+
},
371+
],
372+
allocate: allocate.stub,
373+
});
374+
const install = (platform: 'apple' | 'ios' | 'macos') =>
375+
materializeRemoteConnectionForCommand({
376+
command: 'install',
377+
flags: {
378+
json: true,
379+
help: false,
380+
version: false,
381+
stateDir,
382+
remoteConfig: remoteConfigPath,
383+
daemonBaseUrl: 'https://daemon.example',
384+
tenant: 'proxy',
385+
runId: 'proxy-client-1',
386+
session: 'adc-proxy',
387+
platform,
388+
},
389+
client,
390+
});
391+
392+
const materialized = await install('ios');
393+
assert.equal(materialized.flags.leaseId, 'ios-lease-1');
394+
assert.equal(materialized.flags.udid, 'SIM-001');
395+
396+
const state = readRemoteConnectionState({ stateDir, session: 'adc-proxy' });
397+
assert.equal(state?.platform, 'ios');
398+
assert.equal(state?.deviceKey, 'ios:mobile:SIM-001');
399+
400+
// macOS shares the `apple` family with the bound iOS simulator, so only the collapsed leaf
401+
// above — not the family alias — refuses this request before it touches the lease.
402+
await assert.rejects(
403+
async () => await install('macos'),
404+
(error: unknown) =>
405+
error instanceof AppError &&
406+
error.code === 'INVALID_ARGS' &&
407+
error.details?.session === 'adc-proxy' &&
408+
error.details?.platform === 'ios',
409+
);
410+
fs.rmSync(tempRoot, { recursive: true, force: true });
411+
});

‎src/cli/commands/connection-runtime.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,13 @@ async function resolveProxyLeaseState(options: {
874874
...options.state,
875875
deviceKey: buildConnectionDeviceKey(scope),
876876
leaseBackend: options.state.leaseBackend ?? options.leaseBackend ?? scope.leaseBackend,
877-
platform: options.state.platform ?? scope.platform,
877+
// A recorded `apple` was a family selection made before any device was bound. Once this
878+
// command binds one, the record must name the leaf its own `deviceKey` speaks; keeping the
879+
// alias would let a later leaf on the same family pass the scope guard against this device.
880+
platform:
881+
options.state.platform === 'apple'
882+
? scope.platform
883+
: (options.state.platform ?? scope.platform),
878884
target: options.state.target ?? scope.target,
879885
updatedAt: new Date().toISOString(),
880886
},

0 commit comments

Comments
 (0)