Skip to content

Commit 65abe33

Browse files
committed
fix(remote): refuse a forced connect whose backend and platform name different devices
`--force` drops the previous record before the axis is decided, so the lease backend is the only thing left naming a leaf. `connect --force --platform macos --lease-backend ios-instance` reached the binding helper with a backend that rents iOS and a request that wants macOS, and the helper's fallback wrote the requested platform anyway: a connection recorded as macOS on a backend that can only ever rent iOS devices, which the next command would then read as permission to ask a macOS device of an iOS lease. That is the retargeting #2962 set out to close, arriving through a request that contradicts itself rather than through a bound record. The fallback goes: a refused narrowing is now refused from connect too. The conflict factory gains a `detail` because the advice differs — a bound connection is replaced with `--force`, and a request that names a backend's opposite leaf has no --force to reach. The thrown error keeps the same typed reason and platform fields. The pre-existing reuse refusal is untouched: it still fires from the compatibility check before anything is written.
1 parent a30ebda commit 65abe33

3 files changed

Lines changed: 98 additions & 13 deletions

File tree

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,3 +727,50 @@ test('an undecided record narrows to the requested leaf or refuses, and never se
727727
);
728728
fs.rmSync(tempRoot, { recursive: true, force: true });
729729
});
730+
731+
// `--force` drops the previous record before the platform is decided, so the backend is the only
732+
// thing left naming a leaf. A connect that pairs an `ios-instance` backend with `--platform macos`
733+
// is a contradiction on the request itself, not a reuse of a bound connection, and it must not be
734+
// written out as a connection whose record says macOS while its backend can only ever rent iOS —
735+
// the next command on that record would then ask a macOS device of an iOS lease.
736+
test('connect --force refuses a backend and platform that name different devices', async () => {
737+
const { tempRoot, stateDir, remoteConfigPath } = connectionWorkspace(
738+
'agent-device-connect-force-conflict-',
739+
);
740+
fs.writeFileSync(remoteConfigPath, JSON.stringify({ daemonBaseUrl: 'https://daemon.example' }));
741+
742+
await assert.rejects(
743+
async () =>
744+
await connectCommand({
745+
positionals: [],
746+
flags: {
747+
json: true,
748+
help: false,
749+
version: false,
750+
stateDir,
751+
remoteConfig: remoteConfigPath,
752+
daemonBaseUrl: 'https://daemon.example',
753+
tenant: 'acme',
754+
runId: 'run-9',
755+
session: 'adc-force-conflict',
756+
force: true,
757+
platform: 'macos',
758+
leaseBackend: 'ios-instance',
759+
},
760+
client: createTestClient(),
761+
}),
762+
(error: unknown) =>
763+
error instanceof AppError &&
764+
error.code === 'INVALID_ARGS' &&
765+
error.details?.reason === 'CONNECTION_PLATFORM_CONFLICT' &&
766+
error.details?.platform === 'ios' &&
767+
error.details?.requestedPlatform === 'macos',
768+
'a backend that rents iOS cannot be asked for macOS, --force or not',
769+
);
770+
assert.equal(
771+
readRemoteConnectionState({ stateDir, session: 'adc-force-conflict' }),
772+
null,
773+
'the refused connect wrote no connection state',
774+
);
775+
fs.rmSync(tempRoot, { recursive: true, force: true });
776+
});

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

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,13 @@ export async function materializeRemoteConnectionForCommand(options: {
154154
requestedPlatform: nextFlags.platform,
155155
});
156156
if (!carriedPlatform.ok) {
157-
throw connectionPlatformConflict(state, nextState.leaseBackend, carriedPlatform);
157+
throw connectionPlatformConflict({
158+
session: state.session,
159+
leaseBackend: nextState.leaseBackend,
160+
boundPlatform: carriedPlatform.boundPlatform,
161+
requestedPlatform: carriedPlatform.requestedPlatform,
162+
detail: 'bound-connection',
163+
});
158164
}
159165
nextState = { ...nextState, platform: carriedPlatform.platform };
160166
nextFlags.platform = carriedPlatform.platform;
@@ -345,7 +351,13 @@ async function materializeLeaseForCommand(options: {
345351
requestedPlatform: nextFlags.platform,
346352
});
347353
if (!platform.ok) {
348-
throw connectionPlatformConflict(state, leaseBackend, platform);
354+
throw connectionPlatformConflict({
355+
session: state.session,
356+
leaseBackend,
357+
boundPlatform: platform.boundPlatform,
358+
requestedPlatform: platform.requestedPlatform,
359+
detail: 'bound-connection',
360+
});
349361
}
350362
// Read after the platform is settled: a request that asks for another Apple leaf also asks for a
351363
// different target, and the platform is the reason it is being refused.
@@ -952,22 +964,31 @@ async function resolveSelectedDevice(
952964
);
953965
}
954966

955-
function connectionPlatformConflict(
956-
state: RemoteConnectionState,
957-
leaseBackend: LeaseBackend | undefined,
958-
conflict: Readonly<{
967+
/**
968+
* The refusal raised when the platform axis cannot be decided: two of the backend, the record, and
969+
* the request name devices that are not the same device. `detail` says which of the three disagreed,
970+
* because the advice differs — a bound connection is replaced with `--force`, and a request that
971+
* contradicts itself has no `--force` to reach.
972+
*/
973+
export function connectionPlatformConflict(
974+
options: Readonly<{
975+
session: string;
976+
leaseBackend: LeaseBackend | undefined;
959977
boundPlatform?: CliFlags['platform'];
960978
requestedPlatform?: CliFlags['platform'];
979+
detail: 'bound-connection' | 'requested-backend';
961980
}>,
962981
): AppError {
963982
return new AppError(
964983
'INVALID_ARGS',
965-
'Active remote connection is already bound to a different platform. Re-run connect --force to replace it.',
984+
options.detail === 'bound-connection'
985+
? 'Active remote connection is already bound to a different platform. Re-run connect --force to replace it.'
986+
: 'The requested platform does not match the device this lease backend rents.',
966987
{
967-
session: state.session,
968-
leaseBackend,
969-
platform: conflict.boundPlatform,
970-
requestedPlatform: conflict.requestedPlatform,
988+
session: options.session,
989+
leaseBackend: options.leaseBackend,
990+
platform: options.boundPlatform,
991+
requestedPlatform: options.requestedPlatform,
971992
reason: 'CONNECTION_PLATFORM_CONFLICT',
972993
},
973994
);

‎src/cli/commands/connection.ts‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
verifyResolvedConnectProvider,
3030
} from '../connection/connect-provider-adapters.ts';
3131
import {
32+
connectionPlatformConflict,
3233
hasDeferredMetroConfig,
3334
releaseRemoteConnectionLease,
3435
releasePreviousLease,
@@ -163,7 +164,13 @@ function buildConnectedState(options: {
163164
: null;
164165
const now = new Date().toISOString();
165166
const leaseBinding = buildConnectionLeaseBinding(flags, previous, connectionMetadata);
166-
const runtimeBinding = buildConnectionRuntimeBinding(flags, previous, now, leaseBinding);
167+
const runtimeBinding = buildConnectionRuntimeBinding(
168+
flags,
169+
previous,
170+
now,
171+
leaseBinding,
172+
context.session,
173+
);
167174
return {
168175
version: 1,
169176
session: context.session,
@@ -208,14 +215,24 @@ function buildConnectionRuntimeBinding(
208215
previous: RemoteConnectionState | null,
209216
now: string,
210217
leaseBinding: Pick<RemoteConnectionState, 'leaseBackend'>,
218+
session: string,
211219
): Pick<RemoteConnectionState, 'connectedAt' | 'metro' | 'platform' | 'runtime' | 'target'> {
212220
const platform = narrowConnectionPlatform({
213221
leaseBackend: leaseBinding.leaseBackend,
214222
recordedPlatform: previous?.platform,
215223
requestedPlatform: flags.platform,
216224
});
225+
if (!platform.ok) {
226+
throw connectionPlatformConflict({
227+
session,
228+
leaseBackend: leaseBinding.leaseBackend,
229+
boundPlatform: platform.boundPlatform,
230+
requestedPlatform: platform.requestedPlatform,
231+
detail: previous ? 'bound-connection' : 'requested-backend',
232+
});
233+
}
217234
return {
218-
platform: platform.ok ? platform.platform : flags.platform,
235+
platform: platform.platform,
219236
target: flags.target ?? previous?.target,
220237
runtime: previous?.runtime,
221238
metro: previous?.metro,

0 commit comments

Comments
 (0)