Skip to content

Commit a30ebda

Browse files
committed
fix(remote): decide the platform axis once, as the narrowest of its three sources
`boundConnectionPlatform` collapsed `apple` only where a backend named a leaf, and the effective platform was still `nextState.platform ?? nextFlags.platform`, so a record won whenever it held one. A record reading `apple` beside `ios-simulator` — or beside no backend at all — let `--platform macos` past the guard and then rewrote it back to `apple` before the allocate payload and the daemon request: the same retargeting the PR set out to close, one lookup further along, refused at the base commit and not here. `connect --platform apple` could widen a reused `ios` record the same way, and an unplatformed record on an `ios-instance` backend allocated a macOS request against the iOS lease. One rule replaces both, in one helper: the effective platform is the narrowest of the backend's leaf, the recorded platform, and the requested selector, and two candidates that cannot name the same device are refused with typed details. A requested selector can narrow what a connection is bound to and can never widen it. Every read and write goes through it — the record, the flags, the allocate payload, the returned flags, connect's runtime binding, and the reuse check — so the axis has no remaining site that resolves it by whichever field it happened to read first. `boundConnectionPlatform` and the scope guard's separate compare are gone with it. Reading the backend also settles a family the record never wrote, which turned up a connection that had been passing for the wrong reason: a `harmonyos-instance` lease with no recorded platform accepted `--platform apple` and silently ran a HarmonyOS snapshot, and accepted `ios` and sent that. Both are now refused, which is the narrower answer the backend has always implied. `PLATFORM_BY_LEASE_BACKEND` is derived from the forward table and held in a `Map`. It was a hand-written inverse of it — one more pair of axes free to drift — and a plain object answered an on-disk `leaseBackend` of `constructor` with an inherited function, which is a platform nobody rents. `connect`'s reuse assertion now reads the typed code and details instead of the message text. `platformForLimrunLeaseBackend` stays as it is: it answers which platforms Limrun itself supports, not which platform a backend rents, and deriving it from the shared table would couple a provider's capability set to the kernel's vocabulary.
1 parent cc4c340 commit a30ebda

7 files changed

Lines changed: 410 additions & 111 deletions

File tree

‎packages/kernel/src/contracts.ts‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,20 @@ export function leaseBackendForPlatform(
9090
* A connection binds a platform at the same moment it binds a lease, and the lease is the stronger
9191
* evidence: it names the backend that is actually holding the device. `ios-simulator` maps to no
9292
* leaf because it is a runner/process guard below device leases, not a platform a selector names.
93+
*
94+
* Derived from the forward table rather than written beside it, so the two axes cannot drift, and held
95+
* in a `Map` because a `leaseBackend` reaching here can be any string an older binary left on disk: a
96+
* plain object would answer `constructor` and friends with an inherited function, which is a platform
97+
* nobody rents.
9398
*/
94-
const PLATFORM_BY_LEASE_BACKEND: Partial<Record<LeaseBackend, PublicPlatform>> = {
95-
'ios-instance': 'ios',
96-
'android-instance': 'android',
97-
'harmonyos-instance': 'harmonyos',
98-
};
99-
100-
export function platformForLeaseBackend(backend: LeaseBackend): PublicPlatform | undefined {
101-
return PLATFORM_BY_LEASE_BACKEND[backend];
99+
const PLATFORM_BY_LEASE_BACKEND = new Map<string, PublicPlatform>(
100+
Object.entries(LEASE_BACKEND_BY_PLATFORM).flatMap(([platform, backend]) =>
101+
backend === undefined ? [] : [[backend, platform as PublicPlatform] as const],
102+
),
103+
);
104+
105+
export function platformForLeaseBackend(backend: string): PublicPlatform | undefined {
106+
return PLATFORM_BY_LEASE_BACKEND.get(backend);
102107
}
103108

104109
const DAEMON_SERVER_MODES = ['socket', 'http', 'dual'] as const;

‎src/__tests__/remote-connection-harmonyos.test.ts‎

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,77 @@ test('HarmonyOS platform resolves to its proxy lease backend', () => {
2626
).toBe('harmonyos-instance');
2727
});
2828

29-
test.each(['apple', 'harmonyos', 'ios', 'android'] as const)(
30-
'stored Harmony runtime compatibility respects %s selection',
29+
test('stored Harmony runtime compatibility keeps the HarmonyOS runtime for a HarmonyOS selection', async () => {
30+
const { stateDir, remoteConfigPath } = connectionWorkspace('harmonyos-runtime-');
31+
fs.writeFileSync(remoteConfigPath, JSON.stringify({ daemonBaseUrl: 'https://daemon.example' }));
32+
seedConnectionState({
33+
stateDir,
34+
state: {
35+
session: 'runtime-compat',
36+
remoteConfigPath,
37+
daemon: { baseUrl: 'https://daemon.example' },
38+
tenant: 'proxy',
39+
runId: 'compat-run',
40+
leaseId: 'compat-existing',
41+
leaseBackend: 'harmonyos-instance',
42+
runtime: { platform: 'harmonyos', launchUrl: 'demo://open' },
43+
},
44+
});
45+
const materialized = await materializeRemoteConnectionForCommand({
46+
command: 'snapshot',
47+
client: createTestClient(),
48+
flags: {
49+
json: true,
50+
help: false,
51+
version: false,
52+
stateDir,
53+
remoteConfig: remoteConfigPath,
54+
session: 'runtime-compat',
55+
platform: 'harmonyos',
56+
},
57+
});
58+
expect(materialized.runtime?.platform).toBe('harmonyos');
59+
});
60+
61+
/**
62+
* A backend that rents HarmonyOS devices is the device this connection holds, even when the record
63+
* never wrote a platform, so any other family is refused rather than sent along the existing lease
64+
* (#2962). Before the backend was read, `apple` silently ran as a HarmonyOS snapshot and `ios` went
65+
* out as an `ios` snapshot — both against a `harmonyos-instance` lease.
66+
*/
67+
async function expectRejectedSelection(
68+
stateDir: string,
69+
remoteConfigPath: string,
70+
platform: 'apple' | 'ios' | 'android',
71+
): Promise<void> {
72+
await expect(
73+
materializeRemoteConnectionForCommand({
74+
command: 'snapshot',
75+
client: createTestClient(),
76+
flags: {
77+
json: true,
78+
help: false,
79+
version: false,
80+
stateDir,
81+
remoteConfig: remoteConfigPath,
82+
session: 'runtime-compat',
83+
platform,
84+
},
85+
}),
86+
).rejects.toMatchObject({
87+
code: 'INVALID_ARGS',
88+
details: {
89+
reason: 'CONNECTION_PLATFORM_CONFLICT',
90+
platform: 'harmonyos',
91+
requestedPlatform: platform,
92+
},
93+
});
94+
}
95+
96+
test.each(['apple', 'ios', 'android'] as const)(
97+
'a harmonyos-instance lease refuses a %s selection',
3198
async (platform) => {
32-
const { stateDir, remoteConfigPath } = connectionWorkspace('harmonyos-runtime-');
99+
const { stateDir, remoteConfigPath } = connectionWorkspace('harmonyos-runtime-reject-');
33100
fs.writeFileSync(remoteConfigPath, JSON.stringify({ daemonBaseUrl: 'https://daemon.example' }));
34101
seedConnectionState({
35102
stateDir,
@@ -44,24 +111,7 @@ test.each(['apple', 'harmonyos', 'ios', 'android'] as const)(
44111
runtime: { platform: 'harmonyos', launchUrl: 'demo://open' },
45112
},
46113
});
47-
const materialized = await materializeRemoteConnectionForCommand({
48-
command: 'snapshot',
49-
client: createTestClient(),
50-
flags: {
51-
json: true,
52-
help: false,
53-
version: false,
54-
stateDir,
55-
remoteConfig: remoteConfigPath,
56-
session: 'runtime-compat',
57-
platform,
58-
},
59-
});
60-
if (platform === 'apple' || platform === 'harmonyos') {
61-
expect(materialized.runtime?.platform).toBe('harmonyos');
62-
} else {
63-
expect(materialized.runtime).toBeUndefined();
64-
}
114+
await expectRejectedSelection(stateDir, remoteConfigPath, platform);
65115
},
66116
);
67117

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

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,9 +577,16 @@ test('connect refuses to reuse an apple-bound connection for the other leaf', as
577577
client: createTestClient(),
578578
});
579579

580+
// `connect` asks whether this is the same connection before it binds anything, so it refuses with
581+
// its own "different connection, needs --force" rather than the platform conflict a command that
582+
// does bind raises. Both refuse; only one names the axis.
580583
await assert.rejects(
581584
async () => await connect('macos'),
582-
/A different remote connection is already active/,
585+
(error: unknown) =>
586+
error instanceof AppError &&
587+
error.code === 'INVALID_ARGS' &&
588+
error.details?.session === 'adc-apple-reuse' &&
589+
error.details?.remoteConfig === remoteConfigPath,
583590
"the other leaf of the family can't ride along on this lease",
584591
);
585592
await connect('ios');
@@ -590,3 +597,133 @@ test('connect refuses to reuse an apple-bound connection for the other leaf', as
590597
);
591598
fs.rmSync(tempRoot, { recursive: true, force: true });
592599
});
600+
601+
// `connect` writes the platform it is asked for, which let a reused connection widen its own record
602+
// back to the family: an `ios` lease re-declared with `--platform apple` recorded `apple`, and the
603+
// next command could then ask for any Apple leaf and be served on the iOS device that lease holds.
604+
// The family and the leaf name the same device here, so nothing conflicts — the record simply has to
605+
// keep the leaf it already had.
606+
test('connect keeps the bound leaf when re-declared with the family selector', async () => {
607+
const { tempRoot, stateDir, remoteConfigPath } = connectionWorkspace(
608+
'agent-device-connect-apple-redeclare-',
609+
);
610+
fs.writeFileSync(remoteConfigPath, JSON.stringify({ daemonBaseUrl: 'https://daemon.example' }));
611+
seedConnectionState({
612+
stateDir,
613+
state: {
614+
session: 'adc-redeclare',
615+
remoteConfigPath,
616+
daemon: { baseUrl: 'https://daemon.example' },
617+
tenant: 'acme',
618+
runId: 'run-9',
619+
leaseId: 'ios-lease-1',
620+
leaseBackend: 'ios-instance',
621+
platform: 'ios',
622+
},
623+
});
624+
625+
await connectCommand({
626+
positionals: [],
627+
flags: {
628+
json: true,
629+
help: false,
630+
version: false,
631+
stateDir,
632+
remoteConfig: remoteConfigPath,
633+
daemonBaseUrl: 'https://daemon.example',
634+
tenant: 'acme',
635+
runId: 'run-9',
636+
session: 'adc-redeclare',
637+
platform: 'apple',
638+
leaseBackend: 'ios-instance',
639+
},
640+
client: createTestClient(),
641+
});
642+
643+
assert.equal(
644+
readRemoteConnectionState({ stateDir, session: 'adc-redeclare' })?.platform,
645+
'ios',
646+
'the family selector restates the connection, it does not widen it',
647+
);
648+
fs.rmSync(tempRoot, { recursive: true, force: true });
649+
});
650+
651+
// The two shapes where nothing in the record settles the Apple leaf, so the answer has to come from
652+
// the backend or the request and never from the wider of the two.
653+
//
654+
// - A runner-guard backend (`ios-simulator`) rents no platform, so the requested leaf is the only
655+
// leaf on the table and the recorded alias must not win it.
656+
// - A backend that rents iOS instances has decided the leaf even when the record never wrote one, and
657+
// that is a device this request cannot have.
658+
test('an undecided record narrows to the requested leaf or refuses, and never sends apple', async () => {
659+
const { tempRoot, stateDir, remoteConfigPath } = connectionWorkspace(
660+
'agent-device-connect-undecided-leaf-',
661+
);
662+
fs.writeFileSync(remoteConfigPath, JSON.stringify({ daemonBaseUrl: 'https://daemon.example' }));
663+
const command = (
664+
session: string,
665+
leaseBackend: 'ios-simulator' | 'ios-instance',
666+
platform: 'apple' | 'macos',
667+
) =>
668+
materializeRemoteConnectionForCommand({
669+
command: 'snapshot',
670+
flags: {
671+
json: true,
672+
help: false,
673+
version: false,
674+
stateDir,
675+
remoteConfig: remoteConfigPath,
676+
daemonBaseUrl: 'https://daemon.example',
677+
tenant: 'acme',
678+
runId: 'run-9',
679+
session,
680+
platform,
681+
},
682+
client: createTestClient({
683+
allocate: recordedLeaseAllocate({ leaseId: 'undecided-lease-1', backend: leaseBackend })
684+
.stub,
685+
}),
686+
});
687+
688+
seedConnectionState({
689+
stateDir,
690+
state: {
691+
session: 'adc-guard',
692+
remoteConfigPath,
693+
daemon: { baseUrl: 'https://daemon.example' },
694+
tenant: 'acme',
695+
runId: 'run-9',
696+
leaseBackend: 'ios-simulator',
697+
platform: 'apple',
698+
},
699+
});
700+
const narrowed = await command('adc-guard', 'ios-simulator', 'macos');
701+
assert.equal(
702+
narrowed.flags.platform,
703+
'macos',
704+
'a runner guard rents no leaf, so the request keeps its own',
705+
);
706+
707+
seedConnectionState({
708+
stateDir,
709+
state: {
710+
session: 'adc-unplatformed',
711+
remoteConfigPath,
712+
daemon: { baseUrl: 'https://daemon.example' },
713+
tenant: 'acme',
714+
runId: 'run-9',
715+
leaseBackend: 'ios-instance',
716+
},
717+
});
718+
await assert.rejects(
719+
async () => await command('adc-unplatformed', 'ios-instance', 'macos'),
720+
(error: unknown) =>
721+
error instanceof AppError &&
722+
error.code === 'INVALID_ARGS' &&
723+
error.details?.reason === 'CONNECTION_PLATFORM_CONFLICT' &&
724+
error.details?.platform === 'ios' &&
725+
error.details?.requestedPlatform === 'macos',
726+
'a backend that rents iOS is a bound device, alias or no alias',
727+
);
728+
fs.rmSync(tempRoot, { recursive: true, force: true });
729+
});

0 commit comments

Comments
 (0)