Skip to content

Commit 597cb16

Browse files
committed
fix: address new Simulator AX bridge review comments
1 parent cbd94e4 commit 597cb16

21 files changed

Lines changed: 452 additions & 242 deletions

‎packages/platform-apple/src/snapshot-source/adapter.test.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ test('the Simulator AX source returns raw acquisition facts and discloses unsupp
4747
hint,
4848
});
4949
assert.equal(fixture.builds, 1);
50+
assert.equal(fixture.runs, 6);
5051
assert.equal(result.stage, 'acquired');
5152
assert.equal(result.acquisition.producer, 'simulator-ax-bridge');
5253
assert.equal(result.acquisition.intent, 'full');
@@ -77,6 +78,7 @@ test('the Simulator AX source returns raw acquisition facts and discloses unsupp
7778
});
7879
assert.equal(outcome.stage, 'failed');
7980
if (outcome.stage === 'failed') assert.equal(outcome.failure.kind, 'stale-target');
81+
assert.equal(fixture.runs, 6);
8082
} finally {
8183
await source.close();
8284
await rm(root, { recursive: true, force: true });
@@ -114,6 +116,7 @@ test('preparation consumes the same acquisition deadline as bridge I/O', async (
114116
type AdapterFixture = {
115117
host: SnapshotSourceHost;
116118
builds: number;
119+
runs: number;
117120
responsePid: number;
118121
};
119122

@@ -127,10 +130,16 @@ function targetForTest() {
127130

128131
function createAdapterHost(buildDelayMs = 0): AdapterFixture {
129132
const realHost = createSnapshotSourceHost();
130-
const fixture: AdapterFixture = { host: undefined as never, builds: 0, responsePid: 321 };
133+
const fixture: AdapterFixture = {
134+
host: undefined as never,
135+
builds: 0,
136+
runs: 0,
137+
responsePid: 321,
138+
};
131139
const host: SnapshotSourceHost = {
132140
...realHost,
133141
run: async (command, args) => {
142+
fixture.runs += 1;
134143
if (command === 'xcrun' && args.includes('clang')) {
135144
fixture.builds += 1;
136145
if (buildDelayMs > 0) await new Promise((resolve) => setTimeout(resolve, buildDelayMs));

‎packages/platform-apple/src/snapshot-source/adapter.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { decodeSnapshotBridgeTree } from './tree.ts';
1010
import { createSnapshotSourceHost } from './host.ts';
1111
import type {
1212
SnapshotSourceHost,
13+
SnapshotSourceBridgeBinary,
1314
SnapshotSourceLimits,
1415
SnapshotSourceOutcome,
1516
SnapshotSourceRequest,
@@ -34,6 +35,7 @@ export function createSimulatorSnapshotSource(
3435
): SimulatorSnapshotSource {
3536
const host = options.host ?? createSnapshotSourceHost();
3637
const manager = new SnapshotBridgeManager(host);
38+
const preparedBinaries = new Map<string, SnapshotSourceBridgeBinary>();
3739
let closed = false;
3840

3941
const prepare = async (
@@ -44,7 +46,9 @@ export function createSimulatorSnapshotSource(
4446
}>,
4547
) => {
4648
if (closed) throw snapshotSourceError('unsupported', 'source-closed');
47-
return await host.withDiagnosticTimer(
49+
const prepared = preparedBinaries.get(input.runtime);
50+
if (prepared) return prepared;
51+
const completed = await host.withDiagnosticTimer(
4852
'ios.snapshot-source.prepare',
4953
async () =>
5054
await ensureSnapshotBridgeBinary({
@@ -57,6 +61,8 @@ export function createSimulatorSnapshotSource(
5761
}),
5862
{ producer: SNAPSHOT_SOURCE_PRODUCER },
5963
);
64+
preparedBinaries.set(input.runtime, completed);
65+
return completed;
6066
};
6167

6268
const acquire = async (request: SnapshotSourceRequest): Promise<SnapshotSourceOutcome> => {

‎packages/platform-apple/src/snapshot-source/cache.test.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path from 'node:path';
55
import { test } from 'vitest';
66
import { createSnapshotSourceHost } from './host.ts';
77
import { ensureSnapshotBridgeBinary } from './cache.ts';
8+
import { createSnapshotSourceDeadline } from './deadline.ts';
89
import { DEFAULT_SNAPSHOT_SOURCE_LIMITS } from './limits.ts';
910
import type { SnapshotSourceHost } from './types.ts';
1011

@@ -34,6 +35,7 @@ test('snapshot bridge preparation is cold-once, atomic, and invalidates corrupt
3435
host,
3536
runtime: 'iOS 26.2',
3637
limits: DEFAULT_SNAPSHOT_SOURCE_LIMITS,
38+
deadline: testDeadline(),
3739
sourceRoot,
3840
cacheRoot,
3941
});
@@ -44,6 +46,7 @@ test('snapshot bridge preparation is cold-once, atomic, and invalidates corrupt
4446
host,
4547
runtime: 'iOS 26.2',
4648
limits: DEFAULT_SNAPSHOT_SOURCE_LIMITS,
49+
deadline: testDeadline(),
4750
sourceRoot,
4851
cacheRoot,
4952
});
@@ -55,6 +58,7 @@ test('snapshot bridge preparation is cold-once, atomic, and invalidates corrupt
5558
host,
5659
runtime: 'iOS 26.2',
5760
limits: { ...DEFAULT_SNAPSHOT_SOURCE_LIMITS, maxNodes: 200, maxTraversalDepth: 12 },
61+
deadline: testDeadline(),
5862
sourceRoot,
5963
cacheRoot,
6064
});
@@ -65,6 +69,7 @@ test('snapshot bridge preparation is cold-once, atomic, and invalidates corrupt
6569
host,
6670
runtime: 'iOS 26.2',
6771
limits: DEFAULT_SNAPSHOT_SOURCE_LIMITS,
72+
deadline: testDeadline(),
6873
sourceRoot,
6974
cacheRoot,
7075
});
@@ -80,6 +85,7 @@ test('snapshot bridge preparation is cold-once, atomic, and invalidates corrupt
8085
host,
8186
runtime: 'iOS 26.2',
8287
limits: DEFAULT_SNAPSHOT_SOURCE_LIMITS,
88+
deadline: testDeadline(),
8389
sourceRoot,
8490
cacheRoot,
8591
});
@@ -90,6 +96,7 @@ test('snapshot bridge preparation is cold-once, atomic, and invalidates corrupt
9096
host,
9197
runtime: 'iOS 26.2',
9298
limits: DEFAULT_SNAPSHOT_SOURCE_LIMITS,
99+
deadline: testDeadline(),
93100
sourceRoot,
94101
cacheRoot,
95102
});
@@ -101,6 +108,7 @@ test('snapshot bridge preparation is cold-once, atomic, and invalidates corrupt
101108
host,
102109
runtime: 'iOS 26.2',
103110
limits: DEFAULT_SNAPSHOT_SOURCE_LIMITS,
111+
deadline: testDeadline(),
104112
sourceRoot,
105113
cacheRoot,
106114
});
@@ -133,6 +141,7 @@ test('concurrent snapshot bridge preparation publishes one cache entry', async (
133141
host,
134142
runtime: 'iOS 26.2',
135143
limits: DEFAULT_SNAPSHOT_SOURCE_LIMITS,
144+
deadline: testDeadline(),
136145
sourceRoot,
137146
cacheRoot,
138147
}),
@@ -172,7 +181,7 @@ test('an aborted cache waiter does not cancel an independent preparation', async
172181
host,
173182
runtime: 'iOS 26.2',
174183
limits: { ...DEFAULT_SNAPSHOT_SOURCE_LIMITS, maxDurationMs: 300 },
175-
signal: controller.signal,
184+
deadline: testDeadline(300, controller.signal),
176185
sourceRoot,
177186
cacheRoot,
178187
});
@@ -182,6 +191,7 @@ test('an aborted cache waiter does not cancel an independent preparation', async
182191
host,
183192
runtime: 'iOS 26.2',
184193
limits: DEFAULT_SNAPSHOT_SOURCE_LIMITS,
194+
deadline: testDeadline(),
185195
sourceRoot,
186196
cacheRoot,
187197
});
@@ -205,6 +215,13 @@ async function expectRejectedCancellation(value: Promise<unknown>): Promise<void
205215
});
206216
}
207217

218+
function testDeadline(
219+
timeoutMs = DEFAULT_SNAPSHOT_SOURCE_LIMITS.maxDurationMs,
220+
signal?: AbortSignal,
221+
) {
222+
return createSnapshotSourceDeadline(timeoutMs, signal);
223+
}
224+
208225
function createFakeBuildHost(
209226
binary: string | (() => string | Promise<string>),
210227
getXcode: () => string = () => 'Xcode 16.4\nBuild version 16F6',

‎packages/platform-apple/src/snapshot-source/cache.ts‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createHash } from 'node:crypto';
22
import path from 'node:path';
33
import { SnapshotSourceError, snapshotSourceError } from './errors.ts';
4-
import { createSnapshotSourceDeadline, remainingSnapshotSourceMs } from './deadline.ts';
4+
import { remainingSnapshotSourceMs, type SnapshotSourceDeadline } from './deadline.ts';
55
import {
66
fingerprintSnapshotBridgeSource,
77
readSnapshotSourceToolchain,
@@ -36,14 +36,12 @@ export async function ensureSnapshotBridgeBinary(
3636
host: SnapshotSourceHost;
3737
runtime: string;
3838
limits: SnapshotSourceLimits;
39-
signal?: AbortSignal;
40-
deadline?: import('./deadline.ts').SnapshotSourceDeadline;
39+
deadline: SnapshotSourceDeadline;
4140
sourceRoot?: string;
4241
cacheRoot?: string;
4342
}>,
4443
): Promise<SnapshotSourceBridgeBinary> {
45-
const deadline =
46-
input.deadline ?? createSnapshotSourceDeadline(input.limits.maxDurationMs, input.signal);
44+
const deadline = input.deadline;
4745
const sourceRoot = input.sourceRoot ?? resolveSnapshotBridgeSourceRoot(input.host);
4846
const sourceHash = await fingerprintSnapshotBridgeSource(input.host, sourceRoot, deadline);
4947
const toolchain = await readSnapshotSourceToolchain(input.host, input.runtime, deadline);
@@ -186,7 +184,7 @@ async function readValidCache(
186184
cacheKey: string;
187185
toolchain: SnapshotSourceToolchainIdentity;
188186
}>,
189-
deadline: import('./deadline.ts').SnapshotSourceDeadline,
187+
deadline: SnapshotSourceDeadline,
190188
): Promise<SnapshotSourceBridgeBinary | undefined> {
191189
const binaryPath = path.join(entryPath, BRIDGE_FILENAME);
192190
if (!host.exists(binaryPath) || !host.exists(path.join(entryPath, MANIFEST_FILENAME))) {
@@ -229,9 +227,9 @@ async function readValidCache(
229227
async function sha256File(
230228
host: SnapshotSourceHost,
231229
filePath: string,
232-
deadline?: import('./deadline.ts').SnapshotSourceDeadline,
230+
deadline: SnapshotSourceDeadline,
233231
): Promise<string> {
234-
if (deadline) remainingSnapshotSourceMs(deadline, 'native-cache-hash-deadline');
232+
remainingSnapshotSourceMs(deadline, 'native-cache-hash-deadline');
235233
return createHash('sha256')
236234
.update(await host.readBinary(filePath))
237235
.digest('hex');

‎packages/platform-apple/src/snapshot-source/deadline.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { snapshotSourceError } from './errors.ts';
33

44
export type SnapshotSourceDeadline = Readonly<{
55
clock: Deadline;
6-
signal?: AbortSignal;
6+
signal: AbortSignal | undefined;
77
}>;
88

99
export function createSnapshotSourceDeadline(
@@ -41,5 +41,6 @@ export async function waitForSnapshotSourceDelay(
4141
action();
4242
};
4343
deadline.signal?.addEventListener('abort', onAbort, { once: true });
44+
if (deadline.signal?.aborted) onAbort();
4445
});
4546
}

‎packages/platform-apple/src/snapshot-source/fixtures/native-wire-golden.json‎

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"protocolVersion": 1,
3+
"sourceVersion": "agent-device-simulator-ax-v1.5.3",
4+
"requestKeys": [
5+
"verb",
6+
"requestId",
7+
"pid",
8+
"generation",
9+
"snapshotTree",
10+
"automationMode",
11+
"maxDepth",
12+
"maxNodes",
13+
"maxDurationMs",
14+
"maxResponseBytes"
15+
],
16+
"responseKeys": [
17+
"protocolVersion",
18+
"sourceVersion",
19+
"requestId",
20+
"generation",
21+
"ok",
22+
"pid",
23+
"tree",
24+
"truncated",
25+
"automationEnabled",
26+
"error_kind",
27+
"error_code",
28+
"error"
29+
],
30+
"attributeKeys": [
31+
"XC_kAXXCAttributeElementType",
32+
"XC_kAXXCAttributeElementBaseType",
33+
"XC_kAXXCAttributeLabel",
34+
"XC_kAXXCAttributeValue",
35+
"XC_kAXXCAttributeIdentifier",
36+
"XC_kAXXCAttributeFrame",
37+
"XC_kAXXCAttributeAutomationType",
38+
"XC_kAXXCAttributeChildren"
39+
]
40+
}

0 commit comments

Comments
 (0)