Skip to content

Commit 81a00c8

Browse files
committed
fix: restore install deployment parity
1 parent 13c4451 commit 81a00c8

14 files changed

Lines changed: 302 additions & 19 deletions

File tree

‎packages/contracts/src/app-deployment-runtime.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ export type AppDeploymentRuntimeHost = Readonly<{
7676
android: Readonly<{
7777
/** Low-level boot wait; the Android deployment package decides when it is needed. */
7878
ensureBooted(device: DeviceInfo, signal: AbortSignal): Promise<void>;
79+
/** Brackets one deploy lifecycle so fuzzy target resolution is clear before and after it. */
80+
withInvalidatedAppResolutionCache<Result>(
81+
device: DeviceInfo,
82+
operation: () => Promise<Result>,
83+
): Promise<Result>;
7984
prepareArtifact(
8085
input: MaterializeAppSourceInput,
8186
options: Readonly<{ resolveIdentity?: boolean; signal: AbortSignal }>,

‎packages/platform-android/src/deployment/runtime.test.ts‎

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ const device: DeviceInfo = {
1212
booted: true,
1313
};
1414

15+
async function withoutInvalidatingAppResolutionCache<Result>(
16+
_device: DeviceInfo,
17+
operation: () => Promise<Result>,
18+
): Promise<Result> {
19+
return await operation();
20+
}
21+
1522
test.each([
1623
['emulator', device, true],
1724
['physical device', { ...device, kind: 'device' as const }, true],
@@ -40,7 +47,15 @@ test('exposes all Android deployment operations only for admitted kinds', async
4047
const appName = vi.fn(async () => 'Example');
4148
const push = vi.fn(async () => ({ action: 'com.example.app.TEST_PUSH', extrasCount: 0 }));
4249
const host = {
43-
android: { ensureBooted, prepareArtifact, install, uninstall, appName, push },
50+
android: {
51+
ensureBooted,
52+
withInvalidatedAppResolutionCache: withoutInvalidatingAppResolutionCache,
53+
prepareArtifact,
54+
install,
55+
uninstall,
56+
appName,
57+
push,
58+
},
4459
} as unknown as AppDeploymentRuntimeHost;
4560
const operations = createAndroidAppDeploymentOperations({
4661
host,
@@ -101,6 +116,7 @@ test('preserves the pre-install Android boot wait before artifact identity inspe
101116
const host = {
102117
android: {
103118
ensureBooted,
119+
withInvalidatedAppResolutionCache: withoutInvalidatingAppResolutionCache,
104120
prepareArtifact,
105121
install: async () => 'com.example.app',
106122
uninstall: async () => ({ packageName: 'com.example.app' }),
@@ -138,6 +154,7 @@ test('preserves Android reinstall partial-failure ordering', async () => {
138154
const host = {
139155
android: {
140156
ensureBooted: async () => {},
157+
withInvalidatedAppResolutionCache: withoutInvalidatingAppResolutionCache,
141158
prepareArtifact,
142159
install,
143160
uninstall,
@@ -163,11 +180,73 @@ test('preserves Android reinstall partial-failure ordering', async () => {
163180
expect(install).not.toHaveBeenCalled();
164181
});
165182

183+
test('clears cached fuzzy Android resolution before and after the whole reinstall', async () => {
184+
const cachedResolutions = new Map([['Maps', 'com.example.stale']]);
185+
const withInvalidatedAppResolutionCache = vi.fn(
186+
async <Result>(_device: DeviceInfo, operation: () => Promise<Result>): Promise<Result> => {
187+
cachedResolutions.clear();
188+
try {
189+
return await operation();
190+
} finally {
191+
cachedResolutions.clear();
192+
}
193+
},
194+
);
195+
const resolveFuzzyTarget = vi.fn(async (target: string) => {
196+
const resolved = cachedResolutions.get(target) ?? 'com.example.current';
197+
cachedResolutions.set(target, resolved);
198+
return resolved;
199+
});
200+
const uninstall = vi.fn(async (_device: DeviceInfo, app: string) => {
201+
const packageName = await resolveFuzzyTarget(app);
202+
if (packageName !== 'com.example.current') {
203+
throw new Error(`stale fuzzy target: ${packageName}`);
204+
}
205+
return { packageName };
206+
});
207+
const install = vi.fn(async () => {
208+
cachedResolutions.set('Maps', 'com.example.replacement');
209+
return 'com.example.current';
210+
});
211+
const host = {
212+
android: {
213+
ensureBooted: async () => {},
214+
prepareArtifact: async () => ({
215+
installablePath: '/tmp/replacement.apk',
216+
cleanup: async () => {},
217+
}),
218+
install,
219+
uninstall,
220+
appName: async () => 'Example',
221+
push: async () => ({}),
222+
withInvalidatedAppResolutionCache,
223+
},
224+
} as unknown as AppDeploymentRuntimeHost;
225+
const operations = createAndroidAppDeploymentOperations({
226+
host,
227+
device,
228+
signal: new AbortController().signal,
229+
});
230+
231+
await expect(
232+
operations.deployApp?.({
233+
app: 'Maps',
234+
appPath: '/tmp/replacement.apk',
235+
replaceExisting: true,
236+
}),
237+
).resolves.toEqual({ packageName: 'com.example.current', launchTarget: 'com.example.current' });
238+
239+
expect(withInvalidatedAppResolutionCache).toHaveBeenCalledOnce();
240+
expect(resolveFuzzyTarget).toHaveBeenCalledWith('Maps');
241+
expect(cachedResolutions).toEqual(new Map());
242+
});
243+
166244
test('keeps ordinary Android install successful when identity is unavailable', async () => {
167245
const appName = vi.fn(async () => 'should not be called');
168246
const host = {
169247
android: {
170248
ensureBooted: async () => {},
249+
withInvalidatedAppResolutionCache: withoutInvalidatingAppResolutionCache,
171250
prepareArtifact: async () => ({
172251
installablePath: '/tmp/app.apk',
173252
cleanup: async () => {},

‎packages/platform-android/src/deployment/runtime.ts‎

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,34 @@ async function deployAndroidApp(
5454
input: AppDeploymentInput,
5555
signal: AbortSignal,
5656
): Promise<AppDeploymentResult> {
57+
if (input.replaceExisting) {
58+
// Reinstall resolves its fuzzy target during uninstall. Keep the old whole-operation
59+
// cache boundary here in the Android deployment owner: clear before boot/resolve/uninstall
60+
// and after materialization/install, including partial failures.
61+
return await host.android.withInvalidatedAppResolutionCache(device, async () => {
62+
if (device.booted !== true) {
63+
await host.android.ensureBooted(device, signal);
64+
}
65+
const { packageName } = await host.android.uninstall(device, input.app);
66+
const artifact = await host.android.prepareArtifact(
67+
{ source: { kind: 'path', path: input.appPath } },
68+
{ resolveIdentity: false, signal },
69+
);
70+
try {
71+
await host.android.install(device, artifact.installablePath);
72+
return { packageName, launchTarget: packageName };
73+
} finally {
74+
await artifact.cleanup();
75+
}
76+
});
77+
}
78+
5779
// Existing install semantics wait for a selected Android target before taking the
5880
// before-install package inventory. Keep that platform rule here, rather than
5981
// reviving a daemon readiness adapter.
6082
if (device.booted !== true) {
6183
await host.android.ensureBooted(device, signal);
6284
}
63-
// Reinstall used to remove the resolved package before materializing the replacement. Keep
64-
// that partial-failure boundary in the Android facet rather than changing it during cutover.
65-
if (input.replaceExisting) {
66-
const { packageName } = await host.android.uninstall(device, input.app);
67-
const artifact = await host.android.prepareArtifact(
68-
{ source: { kind: 'path', path: input.appPath } },
69-
{ resolveIdentity: false, signal },
70-
);
71-
try {
72-
await host.android.install(device, artifact.installablePath);
73-
return { packageName, launchTarget: packageName };
74-
} finally {
75-
await artifact.cleanup();
76-
}
77-
}
7885

7986
const artifact = await host.android.prepareArtifact(
8087
{ source: { kind: 'path', path: input.appPath } },

‎packages/platform-android/src/network/runtime.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ function unusedAppDeploymentHost(): PlatformRuntimeHost['appDeployment'] {
244244
},
245245
android: {
246246
ensureBooted: unused,
247+
withInvalidatedAppResolutionCache: unused,
247248
prepareArtifact: unused,
248249
install: unused,
249250
uninstall: unused,

‎packages/platform-apple/src/network/runtime.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ function unusedAppDeploymentHost(): PlatformRuntimeHost['appDeployment'] {
208208
},
209209
android: {
210210
ensureBooted: unused,
211+
withInvalidatedAppResolutionCache: unused,
211212
prepareArtifact: unused,
212213
install: unused,
213214
uninstall: unused,

‎packages/platform-apple/src/runtime.fixtures.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ function unusedAppDeploymentHost(): PlatformRuntimeHost['appDeployment'] {
7171
},
7272
android: {
7373
ensureBooted: unused,
74+
withInvalidatedAppResolutionCache: unused,
7475
prepareArtifact: unused,
7576
install: unused,
7677
uninstall: unused,

‎packages/platform-web/src/runtime.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ function unusedAppDeploymentHost(): PlatformRuntimeHost['appDeployment'] {
227227
},
228228
android: {
229229
ensureBooted: unused,
230+
withInvalidatedAppResolutionCache: unused,
230231
prepareArtifact: unused,
231232
install: unused,
232233
uninstall: unused,

‎packages/provider-webdriver/src/platform-runtime.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ function unusedAppDeploymentHost(): PlatformRuntimeHost['appDeployment'] {
290290
},
291291
android: {
292292
ensureBooted: unused,
293+
withInvalidatedAppResolutionCache: unused,
293294
prepareArtifact: unused,
294295
install: unused,
295296
uninstall: unused,

‎src/daemon/handlers/__tests__/session-command-harness.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ export const mockEnsureReadyHeadlessRuntime = vi.fn(
3838
export const mockDeployAppRuntime = vi.fn(
3939
async (_input: AppDeploymentInput): Promise<AppDeploymentResult> => ({}),
4040
);
41-
const mockMaterializeAppSourceRuntime = vi.fn(
41+
export const mockMaterializeAppSourceRuntime = vi.fn(
4242
async (_input: MaterializeAppSourceInput): Promise<MaterializedAppSource> => ({
4343
installablePath: '/tmp/materialized-app',
4444
cleanup: async () => {},
4545
}),
4646
);
47-
const mockDeployMaterializedAppRuntime = vi.fn(
47+
export const mockDeployMaterializedAppRuntime = vi.fn(
4848
async (_input: DeployMaterializedAppInput): Promise<AppDeploymentResult> => ({}),
4949
);
5050
export const mockPushNotificationRuntime = vi.fn(
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import { expect, test, vi } from 'vitest';
2+
import type { InspectDeviceRuntimeFacts } from '../../request-runtime-binding.ts';
3+
import type { DaemonRequest, SessionState } from '../../types.ts';
4+
import { makeSessionStore } from '../../../__tests__/test-utils/store-factory.ts';
5+
import {
6+
handleSessionCommands,
7+
mockBindDeviceRuntime,
8+
mockDeployMaterializedAppRuntime,
9+
mockInspectDeviceRuntimeFacts,
10+
mockMaterializeAppSourceRuntime,
11+
} from './session-command-harness.ts';
12+
13+
const invoke = async (): Promise<never> => {
14+
throw new Error('install_source ref-frame tests must stay on the runtime route');
15+
};
16+
17+
test('install_source admission failure preserves an active ref frame', async () => {
18+
const sessionStore = makeSessionStore('agent-device-session-install-source-ref-admission-');
19+
const session = activeSession();
20+
sessionStore.set('default', session);
21+
const inspectFacts = vi.fn(async (device) => {
22+
const facts = await mockInspectDeviceRuntimeFacts(device);
23+
return {
24+
...facts,
25+
operations: {
26+
...facts.operations,
27+
deployMaterializedApp: {
28+
available: false as const,
29+
reason: 'owner-capability-missing' as const,
30+
},
31+
},
32+
};
33+
}) satisfies InspectDeviceRuntimeFacts;
34+
35+
const response = await dispatchInstallSource({ sessionStore, inspectFacts });
36+
37+
expect(response).toMatchObject({ ok: false, error: { code: 'UNSUPPORTED_OPERATION' } });
38+
expect(inspectFacts).toHaveBeenCalledOnce();
39+
expect(mockBindDeviceRuntime).not.toHaveBeenCalled();
40+
expect(session.refFrameState).toBe('active');
41+
expect(session.snapshotScopeSource).toBe(session.snapshot);
42+
});
43+
44+
test('install_source materialization failure preserves an active ref frame', async () => {
45+
const sessionStore = makeSessionStore('agent-device-session-install-source-ref-materialization-');
46+
const session = activeSession();
47+
sessionStore.set('default', session);
48+
mockMaterializeAppSourceRuntime.mockRejectedValueOnce(new Error('artifact download failed'));
49+
50+
const response = await dispatchInstallSource({ sessionStore });
51+
52+
expect(response).toMatchObject({ ok: false });
53+
expect(mockInspectDeviceRuntimeFacts).toHaveBeenCalledOnce();
54+
expect(mockBindDeviceRuntime).toHaveBeenCalledOnce();
55+
expect(mockMaterializeAppSourceRuntime).toHaveBeenCalledOnce();
56+
expect(mockDeployMaterializedAppRuntime).not.toHaveBeenCalled();
57+
expect(session.refFrameState).toBe('active');
58+
expect(session.snapshotScopeSource).toBe(session.snapshot);
59+
});
60+
61+
test('install_source deploy attempt expires an active ref frame when the bound operation fails', async () => {
62+
const sessionStore = makeSessionStore('agent-device-session-install-source-ref-deploy-');
63+
const session = activeSession();
64+
sessionStore.set('default', session);
65+
mockDeployMaterializedAppRuntime.mockImplementationOnce(async () => {
66+
expect(session.refFrameState).toBe('expired');
67+
expect(session.snapshotScopeSource).toBeUndefined();
68+
throw new Error('provider deployment failed');
69+
});
70+
71+
const response = await dispatchInstallSource({ sessionStore });
72+
73+
expect(response).toMatchObject({ ok: false });
74+
expect(mockInspectDeviceRuntimeFacts).toHaveBeenCalledOnce();
75+
expect(mockBindDeviceRuntime).toHaveBeenCalledOnce();
76+
expect(mockMaterializeAppSourceRuntime).toHaveBeenCalledOnce();
77+
expect(mockDeployMaterializedAppRuntime).toHaveBeenCalledOnce();
78+
expect(session.refFrameState).toBe('expired');
79+
expect(session.snapshotScopeSource).toBeUndefined();
80+
});
81+
82+
function activeSession(): SessionState {
83+
const snapshot = { createdAt: Date.now(), nodes: [] };
84+
return {
85+
name: 'default',
86+
createdAt: Date.now(),
87+
actions: [],
88+
device: {
89+
platform: 'android',
90+
id: 'emulator-5554',
91+
name: 'Pixel',
92+
kind: 'emulator',
93+
booted: true,
94+
},
95+
snapshot,
96+
snapshotScopeSource: snapshot,
97+
refFrameState: 'active',
98+
};
99+
}
100+
101+
async function dispatchInstallSource(params: {
102+
sessionStore: ReturnType<typeof makeSessionStore>;
103+
inspectFacts?: InspectDeviceRuntimeFacts;
104+
}) {
105+
const req: DaemonRequest = {
106+
token: 't',
107+
session: 'default',
108+
command: 'install_source',
109+
positionals: [],
110+
flags: {},
111+
meta: { installSource: { kind: 'path', path: '/tmp/App.apk' } },
112+
};
113+
return await handleSessionCommands({
114+
req,
115+
sessionName: 'default',
116+
logPath: '/tmp/daemon.log',
117+
sessionStore: params.sessionStore,
118+
invoke,
119+
...(params.inspectFacts ? { inspectFacts: params.inspectFacts } : {}),
120+
});
121+
}

0 commit comments

Comments
 (0)