Skip to content

Commit 86c4c7f

Browse files
thymikeeclaude
andcommitted
refactor(revyl): share the AX viewport rule and the loopback ranges
capture-kit's resolveIosAxTreeViewport now owns the whole rule both providers use (the tree root when usable, else the device's screen box), so Limrun and Revyl no longer each spell it. Revyl's loopback check uses the same net.BlockList ranges as the daemon client, and the fixture ends a session exactly when the client accepts a /stop, with a test for a refused 200. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 861ed36 commit 86c4c7f

7 files changed

Lines changed: 81 additions & 54 deletions

File tree

‎packages/capture-kit/src/ios-ax-tree.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,21 @@ test('a root whose frame is not finite reports no viewport instead of a usable o
8080
resolveIosAxTreeViewport([{ type: 'Application', frame: { x: 0, y: 0, width: 0, height: 0 } }]),
8181
).toEqual({ kind: 'missing', reason: 'invalid' });
8282
});
83+
84+
test('without a usable root the device screen is the viewport, and a bad screen is invalid', () => {
85+
const rootless = [{ type: 'Application' }];
86+
expect(resolveIosAxTreeViewport(rootless, { width: 440, height: 956 })).toEqual({
87+
kind: 'reported',
88+
rect: { x: 0, y: 0, width: 440, height: 956 },
89+
});
90+
expect(resolveIosAxTreeViewport(rootless, { width: 0, height: 956 })).toEqual({
91+
kind: 'missing',
92+
reason: 'invalid',
93+
});
94+
expect(
95+
resolveIosAxTreeViewport(
96+
[{ type: 'Application', frame: { x: 0, y: 0, width: 320, height: 240 } }],
97+
{ width: 440, height: 956 },
98+
),
99+
).toEqual({ kind: 'derived', rect: { x: 0, y: 0, width: 320, height: 240 } });
100+
});

‎packages/capture-kit/src/ios-ax-tree.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,28 @@ export function readIosNodeChildren(node: IosTreeNode): IosTreeNode[] {
142142
}
143143

144144
/**
145-
* The viewport the tree's own application or window root reports, which is the box every node rect
146-
* in the same tree was measured in. Undefined when the tree has no such root.
145+
* The viewport an AX tree acquisition uses: the tree's own application or window root when it reports a
146+
* usable box, because every node rect in the tree was measured in it (`derived`); otherwise the screen
147+
* size the provider reported for the device (`reported`), in the same points. Without either, the
148+
* tree's own reason the box is missing.
147149
*/
148150
export function resolveIosAxTreeViewport(
149151
tree: IosTreeNode | IosTreeNode[],
150-
): IosViewportEvidence | undefined {
151-
return resolveIosViewportEvidenceFromRoots(
152+
screen?: Readonly<{ width?: number; height?: number }>,
153+
): IosViewportEvidence {
154+
const evidence = resolveIosViewportEvidenceFromRoots(
152155
(Array.isArray(tree) ? tree : [tree]).map(viewportRoot),
153156
);
157+
if (evidence?.kind === 'reported') return { kind: 'derived', rect: evidence.rect };
158+
if (screen?.width !== undefined && screen.height !== undefined) {
159+
return isFiniteNumber(screen.width) &&
160+
isFiniteNumber(screen.height) &&
161+
screen.width > 0 &&
162+
screen.height > 0
163+
? { kind: 'reported', rect: { x: 0, y: 0, width: screen.width, height: screen.height } }
164+
: { kind: 'missing', reason: 'invalid' };
165+
}
166+
return evidence ?? { kind: 'missing', reason: 'not-provided' };
154167
}
155168

156169
/**

‎packages/provider-limrun/src/ios-snapshot-adapter.ts‎

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ export async function captureLimrunIosSnapshot(
1414
session: Pick<LimrunIosSession, 'client' | 'instanceId'>,
1515
): Promise<SnapshotRuntimeAcquiredResult> {
1616
const reported = (await session.client.elementTree()) as IosTreeNode | IosTreeNode[];
17-
const viewport = readLimrunViewport(reported, session.client.deviceInfo);
17+
const deviceInfo = session.client.deviceInfo;
18+
const viewport = resolveIosAxTreeViewport(
19+
reported,
20+
deviceInfo && { width: deviceInfo.screenWidth, height: deviceInfo.screenHeight },
21+
);
1822
const { tree, probeFailure } = await expandLimrunCollapsedTabBar(
1923
reported,
2024
viewport.kind === 'missing' ? null : viewport.rect,
@@ -32,32 +36,3 @@ export async function captureLimrunIosSnapshot(
3236
...(probeFailure ? { collapsedTabBarProbeFailure: probeFailure } : {}),
3337
});
3438
}
35-
36-
function readLimrunViewport(
37-
tree: IosTreeNode | IosTreeNode[],
38-
deviceInfo: { screenWidth?: number; screenHeight?: number } | undefined,
39-
) {
40-
const treeEvidence = resolveIosAxTreeViewport(tree);
41-
if (treeEvidence?.kind === 'reported') {
42-
return { kind: 'derived' as const, rect: treeEvidence.rect };
43-
}
44-
return readLimrunDeviceInfoViewport(deviceInfo);
45-
}
46-
47-
function readLimrunDeviceInfoViewport(
48-
deviceInfo: { screenWidth?: number; screenHeight?: number } | undefined,
49-
) {
50-
const width = deviceInfo?.screenWidth;
51-
const height = deviceInfo?.screenHeight;
52-
if (width === undefined || height === undefined) {
53-
return { kind: 'missing' as const, reason: 'not-provided' as const };
54-
}
55-
if (!isPositiveFiniteNumber(width) || !isPositiveFiniteNumber(height)) {
56-
return { kind: 'missing' as const, reason: 'invalid' as const };
57-
}
58-
return { kind: 'reported' as const, rect: { x: 0, y: 0, width, height } };
59-
}
60-
61-
function isPositiveFiniteNumber(value: number): boolean {
62-
return Number.isFinite(value) && value > 0;
63-
}

‎packages/provider-revyl/src/revyl-backend.fixtures.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,11 @@ function fixtureRoute(url: string): string {
310310
return 'unknown';
311311
}
312312

313-
/** A lifecycle answer ends its session only when Revyl did not say it refused, as the client reads it. */
313+
/** A `/stop` or run cancel ends its session exactly when the client accepts the answer. */
314314
function lifecycleAccepted(body: unknown): boolean {
315-
if (body === null || typeof body !== 'object') return true;
315+
if (body === null || typeof body !== 'object') return false;
316316
const verdict = body as { success?: unknown; request_accepted?: unknown };
317-
return verdict.success !== false && verdict.request_accepted !== false;
317+
return verdict.success === true && (verdict.request_accepted ?? true) === true;
318318
}
319319

320320
/** Revyl routes every device action below the run's device-proxy prefix. */

‎packages/provider-revyl/src/revyl-client.ts‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { AppError, type AppErrorCode } from '@agent-device/kernel/errors';
22
import { isRecord } from '@agent-device/kernel/record';
3+
import net from 'node:net';
34
import { setTimeout as sleep } from 'node:timers/promises';
45
import type {
56
RevylDeviceSessionStopResponse,
@@ -520,17 +521,18 @@ export function isRevylSessionGone(error: unknown): boolean {
520521
return error instanceof AppError && error.details?.reason === 'provider-session-gone';
521522
}
522523

523-
/**
524-
* Whether a WHATWG-normalized hostname is loopback: `localhost`, `[::1]`, any of `127.0.0.0/8`, or that
525-
* range IPv4-mapped (`[::ffff:127.0.0.2]` normalizes to `[::ffff:7f00:2]`).
526-
*/
524+
/** The loopback ranges, the same set the daemon client trusts for a local daemon. */
525+
const LOOPBACK_BLOCK_LIST = new net.BlockList();
526+
LOOPBACK_BLOCK_LIST.addSubnet('127.0.0.0', 8, 'ipv4');
527+
LOOPBACK_BLOCK_LIST.addAddress('::1', 'ipv6');
528+
LOOPBACK_BLOCK_LIST.addSubnet('::ffff:127.0.0.0', 104, 'ipv6');
529+
530+
/** Whether a WHATWG-normalized hostname (IPv6 in brackets) names a loopback address. */
527531
function isLoopbackHostname(hostname: string): boolean {
528-
return (
529-
hostname === 'localhost' ||
530-
hostname === '[::1]' ||
531-
/^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(hostname) ||
532-
/^\[::ffff:7f[0-9a-f]{2}:[0-9a-f]{1,4}\]$/.test(hostname)
533-
);
532+
if (hostname === 'localhost') return true;
533+
const address = hostname.startsWith('[') ? hostname.slice(1, -1) : hostname;
534+
const family = net.isIP(address);
535+
return family !== 0 && LOOPBACK_BLOCK_LIST.check(address, family === 6 ? 'ipv6' : 'ipv4');
534536
}
535537

536538
/** HTTPS everywhere; plain HTTP only on loopback, and never with credentials, query, or fragment. */

‎packages/provider-revyl/src/runtime.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,31 @@ test('a stop Revyl refuses keeps the lease for a retry and reports the session i
309309
assert.equal(stops(fixture).length, 2);
310310
});
311311

312+
test('a stop Revyl refuses inside a 200 keeps the lease, and the retry that is accepted ends it', async () => {
313+
const stopPath = `POST /api/v1/execution/device/sessions/${revylSessionIdForLease('lease-1')}/stop`;
314+
let stopsAnswered = 0;
315+
const fixture = await backend({
316+
sessionExistsOnceStarted: true,
317+
respond: {
318+
[stopPath]: () =>
319+
(stopsAnswered += 1) === 1
320+
? { body: { success: false, request_accepted: false, message: 'worker busy' } }
321+
: { body: { success: true, request_accepted: true } },
322+
},
323+
});
324+
const runtime = fixture.createRuntime();
325+
const lease = revylLease('ios');
326+
await runtime.leaseLifecycle?.allocate?.(lease);
327+
328+
await assert.rejects(
329+
async () => await runtime.leaseLifecycle?.release?.(lease),
330+
(error: unknown) =>
331+
error instanceof AppError && error.details?.reason === 'provider-cleanup-pending',
332+
);
333+
await runtime.leaseLifecycle?.release?.(lease);
334+
assert.equal(stops(fixture).length, 2);
335+
});
336+
312337
test('a stop Revyl confirms releases the lease, and a repeat release asks Revyl nothing', async () => {
313338
const fixture = await backend();
314339
const runtime = fixture.createRuntime();

‎packages/provider-revyl/src/snapshot.ts‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,7 @@ async function captureRevylIosSnapshot(
6464
import('@agent-device/capture-kit/ios-snapshot-acquisition'),
6565
]);
6666
const tree = parseIosTree(hierarchy);
67-
const reported = resolveIosAxTreeViewport(tree);
68-
const viewport =
69-
reported?.kind === 'reported'
70-
? { kind: 'derived' as const, rect: reported.rect }
71-
: screen !== undefined
72-
? { kind: 'reported' as const, rect: { x: 0, y: 0, ...screen } }
73-
: (reported ?? { kind: 'missing' as const, reason: 'not-provided' as const });
67+
const viewport = resolveIosAxTreeViewport(tree, screen);
7468
return acquisition.createIosSnapshotAcquisition({
7569
producer: 'revyl-ios-tree',
7670
...acquireProviderKeyboard({

0 commit comments

Comments
 (0)