Skip to content

Commit 1b0d9a0

Browse files
fix(remote): bind the host-allocated macos-app lease on connect
1 parent 937471b commit 1b0d9a0

2 files changed

Lines changed: 119 additions & 9 deletions

File tree

‎src/__tests__/remote-connection-macos-app.test.ts‎

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { expect, test } from 'vitest';
1+
import { expect, test, vi } from 'vitest';
22
import fs from 'node:fs';
33
import { AppError } from '@agent-device/kernel/errors';
44
import { connectionWorkspace, createTestClient } from './remote-connection.fixtures.ts';
5+
import type { AgentDeviceClient } from '../agent-device-client.ts';
6+
import { connectCommand } from '../cli/commands/connection.ts';
57
import { materializeRemoteConnectionForCommand } from '../cli/commands/connection-runtime.ts';
68
import { readRemoteConnectionState } from '../remote/remote-connection-state.ts';
79
import { LeaseRegistry } from '../daemon/lease-registry.ts';
@@ -99,3 +101,92 @@ test('an inactive host lease is reported, never replaced by a tenant allocation'
99101
).rejects.toMatchObject({ code: 'UNAUTHORIZED', details: { reason: 'LEASE_NOT_FOUND' } });
100102
expect(allocations).toBe(0);
101103
});
104+
105+
async function connectHostedMacosApp(
106+
workspace: { stateDir: string; remoteConfigPath: string },
107+
overrides: { leaseId?: string; force?: boolean } = {},
108+
release: AgentDeviceClient['leases']['release'] = async () => ({ released: true }),
109+
): Promise<void> {
110+
const stdout = vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
111+
try {
112+
await connectCommand({
113+
positionals: [],
114+
flags: {
115+
...flagsFor(workspace.stateDir, workspace.remoteConfigPath),
116+
daemonBaseUrl: 'https://host.example/agent/',
117+
tenant: 'stim',
118+
runId: 'session-1',
119+
leaseId: LEASE_ID,
120+
leaseBackend: 'macos-app',
121+
platform: 'macos',
122+
...overrides,
123+
},
124+
client: createTestClient({
125+
release,
126+
listDevices: async () => {
127+
throw new Error('a macos-app connection resolves no device into a new key');
128+
},
129+
allocate: async () => {
130+
throw new Error('a macos-app connection never allocates');
131+
},
132+
}),
133+
});
134+
} finally {
135+
stdout.mockRestore();
136+
}
137+
}
138+
139+
test('connect binds the lease and device a macos-app remote config names, and open uses them', async () => {
140+
const workspace = connectionWorkspace('macos-app-connect-');
141+
writeHostIssuedConfig(workspace.remoteConfigPath);
142+
await connectHostedMacosApp(workspace);
143+
expect(
144+
readRemoteConnectionState({ stateDir: workspace.stateDir, session: 'hosted' }),
145+
).toMatchObject({
146+
leaseId: LEASE_ID,
147+
leaseBackend: 'macos-app',
148+
deviceKey: 'com.example.app@4242',
149+
});
150+
151+
const materialized = await materializeRemoteConnectionForCommand({
152+
command: 'open',
153+
positionals: ['com.example.app'],
154+
flags: { ...flagsFor(workspace.stateDir, workspace.remoteConfigPath), platform: 'macos' },
155+
client: createTestClient({
156+
allocate: async () => {
157+
throw new Error('a macos-app connection never allocates');
158+
},
159+
}),
160+
});
161+
expect(materialized.flags).toMatchObject({ leaseId: LEASE_ID, leaseBackend: 'macos-app' });
162+
});
163+
164+
test('connect with an explicit --lease-id binds that lease over the one in the config', async () => {
165+
const workspace = connectionWorkspace('macos-app-connect-flag-');
166+
writeHostIssuedConfig(workspace.remoteConfigPath);
167+
const flagLeaseId = 'ffffffffffffffffffffffffffffffff';
168+
await connectHostedMacosApp(workspace, { leaseId: flagLeaseId });
169+
expect(
170+
readRemoteConnectionState({ stateDir: workspace.stateDir, session: 'hosted' }),
171+
).toMatchObject({
172+
leaseId: flagLeaseId,
173+
deviceKey: 'com.example.app@4242',
174+
});
175+
});
176+
177+
test('a forced reconnect keeps the host lease it binds again', async () => {
178+
const workspace = connectionWorkspace('macos-app-connect-force-');
179+
writeHostIssuedConfig(workspace.remoteConfigPath);
180+
await connectHostedMacosApp(workspace);
181+
const released: string[] = [];
182+
await connectHostedMacosApp(workspace, { force: true }, async (request) => {
183+
released.push(request.leaseId);
184+
return { released: true };
185+
});
186+
expect(released).toEqual([]);
187+
expect(
188+
readRemoteConnectionState({ stateDir: workspace.stateDir, session: 'hosted' }),
189+
).toMatchObject({
190+
leaseId: LEASE_ID,
191+
});
192+
});

‎src/cli/commands/connection.ts‎

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export const connectCommand: ClientCommandHandler = async ({ positionals, flags,
8989
stateDir,
9090
connectFlags,
9191
context.previous,
92-
state.daemon?.baseUrl,
92+
state,
9393
);
9494
const runtimePreparation = buildRuntimePreparationNotice(connectFlags, state);
9595
const readiness = presentConnectReadiness(state, verification);
@@ -185,23 +185,41 @@ function buildConnectedState(options: {
185185
};
186186
}
187187

188+
type ConnectionLeaseBinding = Pick<
189+
RemoteConnectionState,
190+
'clientId' | 'deviceKey' | 'leaseBackend' | 'leaseId' | 'leaseProvider'
191+
>;
192+
188193
function buildConnectionLeaseBinding(
189194
flags: CliFlags,
190195
previous: RemoteConnectionState | null,
191196
connectionMetadata: RemoteConnectionRequestMetadata | undefined,
192-
): Pick<
193-
RemoteConnectionState,
194-
'clientId' | 'deviceKey' | 'leaseBackend' | 'leaseId' | 'leaseProvider'
195-
> {
197+
): ConnectionLeaseBinding {
196198
const connection = mergeRemoteConnectionRequestMetadata(connectionMetadata ?? {}, previous ?? {});
199+
const leaseBackend = previous?.leaseBackend ?? resolveRequestedLeaseBackend(flags);
200+
if (leaseBackend === 'macos-app') {
201+
return buildHostAllocatedLeaseBinding(flags, previous, connection);
202+
}
197203
return {
198204
leaseId: previous?.leaseId,
199-
leaseBackend: previous?.leaseBackend ?? resolveRequestedLeaseBackend(flags),
205+
leaseBackend,
200206
...connection,
201207
deviceKey: previous?.deviceKey ?? connection.deviceKey,
202208
};
203209
}
204210

211+
function buildHostAllocatedLeaseBinding(
212+
flags: CliFlags,
213+
previous: RemoteConnectionState | null,
214+
connection: RemoteConnectionRequestMetadata,
215+
): ConnectionLeaseBinding {
216+
return {
217+
leaseId: flags.leaseId ?? previous?.leaseId,
218+
leaseBackend: 'macos-app',
219+
...connection,
220+
};
221+
}
222+
205223
/**
206224
* Writes what a reused connection is bound to on the platform axis.
207225
*
@@ -252,13 +270,14 @@ async function cleanupForcedPreviousConnection(
252270
stateDir: string,
253271
flags: CliFlags,
254272
previous: RemoteConnectionState | null,
255-
nextDaemonBaseUrl: string | undefined,
273+
next: RemoteConnectionState,
256274
): Promise<PreviousLeaseReleaseNotice | undefined> {
257275
if (!previous || !flags.force) return undefined;
258276
await stopMetroCleanup(previous.metro);
259277
await stopReactDevtoolsCleanup({ stateDir, state: previous });
278+
if (previous.leaseId && previous.leaseId === next.leaseId) return undefined;
260279
return await releasePreviousLease(client, previous, {
261-
nextDaemonBaseUrl,
280+
nextDaemonBaseUrl: next.daemon?.baseUrl,
262281
ambientDaemonAuthToken: flags.daemonAuthToken,
263282
cwd: process.cwd(),
264283
env: process.env,

0 commit comments

Comments
 (0)