Skip to content

Commit f0af272

Browse files
committed
refactor(apple): parse the query rect once and pin the rescue wiring end to end
- replace the local readRect with @agent-device/kernel/record parseRect - build the runner-call options with { ...input.execution, signal } like bindAppleFindTextRuntime instead of a field-by-field copy - forward runtime.operations.querySelector directly; delete the no-op selectOffscreenTargetQuery wrappers - inline queryDirectIosSelector into the probe and drop the unused signal - let the CONDITIONAL fact be the whole rescue gate: the fact already refuses provider-owned and macOS leaves, so the isLocalIosRunnerSession check added nothing - daemon-route tests pin both sides of the conditional (live-rect rescue, zero-query refusal) and the Apple leaf table pins fact-and-binding parity; assert AbortError on the aborted query
1 parent a11b86e commit f0af272

12 files changed

Lines changed: 206 additions & 232 deletions

‎packages/platform-apple/src/__tests__/runtime-selector-query.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ test('forwards the selector, app scope, and the full runner-execution projection
5252
);
5353
});
5454

55-
test('composes the caller signal with the binding signal and refuses an already-aborted request', async () => {
55+
test('refuses an already-aborted binding signal without querying the runner', async () => {
5656
const controller = new AbortController();
5757
controller.abort();
5858

5959
await expect(
6060
bind(controller.signal).querySelector({ selector: { key: 'label', value: 'Go' } }),
61-
).rejects.toThrow();
61+
).rejects.toMatchObject({ name: 'AbortError' });
6262
expect(queryAppleRunnerSelector).not.toHaveBeenCalled();
6363
});
6464

‎packages/platform-apple/src/runtime-selector-query.ts‎

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {
55
SelectorObservationRuntimeOperations,
66
} from '@agent-device/contracts/selector-observation-runtime';
77
import type { DeviceInfo } from '@agent-device/kernel/device';
8-
import type { Rect } from '@agent-device/kernel/snapshot';
8+
import { parseRect } from '@agent-device/kernel/record';
99
import type { RunnerCallOptions } from '@agent-device/contracts/interactor-types';
1010

1111
type AppleSelectorQueryRequest = Readonly<{
@@ -32,7 +32,9 @@ export function bindAppleQuerySelectorRuntime(
3232
? AbortSignal.any([request.signal, input.signal])
3333
: request.signal;
3434
signal.throwIfAborted();
35-
const options = runnerCallOptions(input, signal);
35+
// Same projection bindAppleFindTextRuntime builds: the execution context is exactly the
36+
// runner-call fields, with the composed signal replacing the caller's own.
37+
const options: RunnerCallOptions = { ...input.execution, signal };
3638
// Deferred exactly like the retired daemon-side edge: the runner client binds its host at
3739
// evaluation, so mechanics load on the first read, never with the runtime façade.
3840
const { queryAppleRunnerSelector } = await import('./core/runner-selector-query.ts');
@@ -51,54 +53,17 @@ export function bindAppleQuerySelectorRuntime(
5153
});
5254
}
5355

54-
function runnerCallOptions(input: QuerySelectorInput, signal: AbortSignal): RunnerCallOptions {
55-
const execution = input.execution ?? {};
56-
return {
57-
signal,
58-
...(execution.verbose === undefined ? {} : { verbose: execution.verbose }),
59-
...(execution.logPath === undefined ? {} : { logPath: execution.logPath }),
60-
...(execution.traceLogPath === undefined ? {} : { traceLogPath: execution.traceLogPath }),
61-
...(execution.requestId === undefined ? {} : { requestId: execution.requestId }),
62-
...(execution.iosXctestrunFile === undefined
63-
? {}
64-
: { iosXctestrunFile: execution.iosXctestrunFile }),
65-
...(execution.iosXctestDerivedDataPath === undefined
66-
? {}
67-
: { iosXctestDerivedDataPath: execution.iosXctestDerivedDataPath }),
68-
...(execution.iosXctestEnvDir === undefined
69-
? {}
70-
: { iosXctestEnvDir: execution.iosXctestEnvDir }),
71-
...(execution.runnerLeaseContext === undefined
72-
? {}
73-
: { runnerLeaseContext: execution.runnerLeaseContext }),
74-
};
75-
}
76-
7756
function readQuerySelectorNode(
7857
data: Record<string, unknown>,
7958
): QuerySelectorNodeReading | undefined {
8059
const nodes = data.nodes;
8160
if (!Array.isArray(nodes)) return undefined;
8261
const node = nodes[0];
8362
if (!node || typeof node !== 'object') return undefined;
84-
const rect = readRect((node as { rect?: unknown }).rect);
63+
const rect = parseRect((node as { rect?: unknown }).rect);
8564
if (!rect) return undefined;
8665
return Object.freeze({
8766
rect,
8867
hittable: (node as { hittable?: unknown }).hittable === true,
8968
});
9069
}
91-
92-
function readRect(value: unknown): Rect | undefined {
93-
if (!value || typeof value !== 'object') return undefined;
94-
const { x, y, width, height } = value as Record<string, unknown>;
95-
if (
96-
typeof x !== 'number' ||
97-
typeof y !== 'number' ||
98-
typeof width !== 'number' ||
99-
typeof height !== 'number'
100-
) {
101-
return undefined;
102-
}
103-
return { x, y, width, height };
104-
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,31 @@ const leaves = {
4747
watchos: appleDevice({ appleOs: 'watchos' }),
4848
} satisfies Record<AppleOS, DeviceInfo>;
4949

50+
// #1542 wiring on the owner side: the CONDITIONAL live selector read must be advertised AND
51+
// bound on every leaf the kind cell covers, and absent on the macOS desktop. Dropping
52+
// `...selectorQueryOperations` from the binding (while the fact stays available) breaks the
53+
// second expectation here; mis-declaring the fact breaks the first.
54+
test.each([
55+
['iOS simulator', leaves.ios, true],
56+
['iOS physical device', appleDevice({ kind: 'device' }), true],
57+
['macOS host', leaves.macos, false],
58+
] as const)(
59+
'binds the querySelector read on the %s leaf per its fact',
60+
async (_name, device, advertised) => {
61+
const binding = await createApplePlatformRuntime(platformRuntimeHostFixture()).bind({
62+
device,
63+
intent: { kind: 'ordinary' },
64+
scope: {
65+
signal: new AbortController().signal,
66+
diagnostics: { emit: () => {} },
67+
progress: { report: () => {} },
68+
},
69+
});
70+
expect(binding.facts.operations.querySelector.available).toBe(advertised);
71+
expect(binding.operations.querySelector).toBeTypeOf(advertised ? 'function' : 'undefined');
72+
},
73+
);
74+
5075
test('tvOS audio capture availability follows the exact host-owned runtime fact', async () => {
5176
const binding = await createApplePlatformRuntime(platformRuntimeHostFixture()).bind({
5277
device: leaves.tvos,

‎src/daemon/__tests__/interaction-get-runtime-fixture.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ import {
2525
type TapRefInput,
2626
HOVER_UNAVAILABLE_HINT,
2727
} from '@agent-device/contracts/touch-runtime';
28+
import type {
29+
QuerySelectorInput,
30+
QuerySelectorResult,
31+
} from '@agent-device/contracts/selector-observation-runtime';
2832
import type { DeviceInfo } from '@agent-device/kernel/device';
2933
import type { DaemonPlatformServices } from '../platform-services.ts';
3034
import type { BindDeviceRuntime, InspectDeviceRuntimeFacts } from '../request-runtime-binding.ts';
@@ -90,6 +94,14 @@ const mockFillRef = vi.fn(async (input: FillRefInput) => ({
9094
}));
9195
export const mockTapElementSelector = vi.fn(async (_input: TapElementSelectorInput) => ({}));
9296

97+
/**
98+
* The CONDITIONAL live selector read (#1542 rescue). Off by default so only the tests that pin
99+
* the rescue's wiring flip it; when advertised, the binding answers through this mock.
100+
*/
101+
export const mockQuerySelector = vi.fn(
102+
async (_input: QuerySelectorInput): Promise<QuerySelectorResult> => ({ found: false }),
103+
);
104+
93105
/**
94106
* Flip to model an exact owner cell: no live element read (web, HarmonyOS, provider), no capture
95107
* at all (the watchOS sentinel, an inactive provider), which refuses admission outright, or no
@@ -101,6 +113,8 @@ export const elementReadFixtureState = {
101113
focusPointAvailable: true,
102114
typeTextAvailable: true,
103115
tapElementSelectorAvailable: true,
116+
/** Off by default: only the tests pinning the #1542 rescue wiring advertise the live read. */
117+
querySelectorAvailable: false,
104118
};
105119

106120
export function resetGetRuntimeFixture(): void {
@@ -143,6 +157,9 @@ export function resetGetRuntimeFixture(): void {
143157
elementReadFixtureState.focusPointAvailable = true;
144158
elementReadFixtureState.typeTextAvailable = true;
145159
elementReadFixtureState.tapElementSelectorAvailable = true;
160+
elementReadFixtureState.querySelectorAvailable = false;
161+
mockQuerySelector.mockReset();
162+
mockQuerySelector.mockResolvedValue({ found: false });
146163
}
147164

148165
const available = Object.freeze({ available: true } as const);
@@ -186,6 +203,7 @@ function fixtureTouchFacts(device: DeviceInfo) {
186203
tapElementSelector: elementReadFixtureState.tapElementSelectorAvailable
187204
? available
188205
: unavailable,
206+
querySelector: elementReadFixtureState.querySelectorAvailable ? available : unavailable,
189207
};
190208
}
191209

@@ -227,6 +245,9 @@ const mockBindElementReadRuntime: BindDeviceRuntime = vi.fn(async (device: Devic
227245
...(elementReadFixtureState.tapElementSelectorAvailable
228246
? { tapElementSelector: mockTapElementSelector }
229247
: {}),
248+
...(elementReadFixtureState.querySelectorAvailable
249+
? { querySelector: mockQuerySelector }
250+
: {}),
230251
}),
231252
[Symbol.asyncDispose]: async () => undefined,
232253
}) as DeviceBinding<PlatformRuntimeOperations>;

‎src/daemon/__tests__/offscreen-target-probe.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ test('confirmIosOffscreenTargetVisible: forwards the derived id/label selector a
5858
});
5959
});
6060

61+
test('confirmIosOffscreenTargetVisible: a session without an app sends no options bag', async () => {
62+
const querySelector = vi.fn<ProbeQuery>(found({ x: 1, y: 2, width: 3, height: 4 }, true));
63+
64+
await confirmIosOffscreenTargetVisible({
65+
session: makeSession(),
66+
node: { label: 'Pickup' },
67+
rootViewport: ROOT_VIEWPORT,
68+
querySelector,
69+
execution: {},
70+
});
71+
72+
assert.equal('options' in (querySelector.mock.calls[0]?.[0] ?? {}), false);
73+
});
74+
6175
test('confirmIosOffscreenTargetVisible: null when the node has no usable id/label (never calls the operation)', async () => {
6276
const querySelector = vi.fn<ProbeQuery>(found({ x: 1, y: 2, width: 3, height: 4 }, true));
6377

‎src/daemon/__tests__/selector-runtime.test.ts‎

Lines changed: 1 addition & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { daemonPlatformServicesFixture } from './platform-services-fixture.ts';
2-
import { expect, test, vi } from 'vitest';
3-
import assert from 'node:assert/strict';
4-
import { AppError } from '@agent-device/kernel/errors';
5-
import { IOS_SIMULATOR } from '../../__tests__/test-utils/device-fixtures.ts';
6-
import type { QuerySelectorInput } from '@agent-device/contracts/selector-observation-runtime';
7-
import type { SessionState } from '../session-state.ts';
2+
import { expect, test } from 'vitest';
83
import {
94
localRuntimeOwner,
105
narrowDeviceBinding,
@@ -21,12 +16,6 @@ import { makeSession as makeStoredSession } from '../../__tests__/test-utils/ses
2116
import { dispatchGetViaRuntime } from '../selector-runtime.ts';
2217
import { testCreateCommandSurface } from './command-surface-fixture.ts';
2318

24-
import { queryDirectIosSelector } from '../direct-ios-selector.ts';
25-
26-
function makeSession(): SessionState {
27-
return { name: 'default', device: IOS_SIMULATOR, createdAt: Date.now(), actions: [] };
28-
}
29-
3019
test.each(['rebuild', 'retire'] as const)(
3120
'get text records in its captured lifetime after a held native read across %s',
3221
async (transition) => {
@@ -131,94 +120,3 @@ test.each(['rebuild', 'retire'] as const)(
131120
}
132121
},
133122
);
134-
135-
// #1542: queryDirectIosSelector is the daemon-side adapter for the off-screen rescue's live
136-
// read. The runner request shape is the Apple owner's contract (its `querySelector` runtime
137-
// operation, pinned by packages/platform-apple); what the daemon owns here is the projection:
138-
// the session's app scope rides along, and the verdict passes through unchanged.
139-
140-
test('queryDirectIosSelector: forwards the selector with the session appBundleId scope', async () => {
141-
const session = { ...makeSession(), appBundleId: 'com.example.demo' };
142-
const querySelector = vi.fn(async (_input: QuerySelectorInput) => ({ found: false }) as const);
143-
144-
await queryDirectIosSelector(session, { key: 'id', value: 'submit-order' }, querySelector, {
145-
execution: { requestId: 'req-1' },
146-
});
147-
148-
assert.deepEqual(querySelector.mock.calls[0]?.[0], {
149-
selector: { key: 'id', value: 'submit-order' },
150-
options: { appBundleId: 'com.example.demo' },
151-
execution: { requestId: 'req-1' },
152-
});
153-
});
154-
155-
test('queryDirectIosSelector: a session without an app sends no options bag', async () => {
156-
let received: QuerySelectorInput | undefined;
157-
const querySelector = vi.fn(async (input: QuerySelectorInput) => {
158-
received = input;
159-
return { found: false } as const;
160-
});
161-
162-
await queryDirectIosSelector(makeSession(), { key: 'label', value: 'Pickup' }, querySelector, {
163-
execution: {},
164-
});
165-
166-
assert.ok(received);
167-
assert.equal('options' in received, false);
168-
});
169-
170-
test('queryDirectIosSelector: passes the node projection through when the owner reports one', async () => {
171-
const node = { rect: { x: 1, y: 2, width: 3, height: 4 }, hittable: true };
172-
const querySelector = vi.fn(
173-
async (_input: QuerySelectorInput) => ({ found: true, node }) as const,
174-
);
175-
176-
const result = await queryDirectIosSelector(
177-
makeSession(),
178-
{ key: 'label', value: 'Pickup' },
179-
querySelector,
180-
{
181-
execution: {},
182-
},
183-
);
184-
185-
assert.equal(result.found, true);
186-
assert.deepEqual(result.node, node);
187-
});
188-
189-
test('queryDirectIosSelector: found:false with no node reports not-found without a node', async () => {
190-
const querySelector = vi.fn(async (_input: QuerySelectorInput) => ({ found: false }) as const);
191-
192-
const result = await queryDirectIosSelector(
193-
makeSession(),
194-
{ key: 'id', value: 'missing' },
195-
querySelector,
196-
{
197-
execution: {},
198-
},
199-
);
200-
201-
assert.equal(result.found, false);
202-
assert.equal(result.node, undefined);
203-
});
204-
205-
test('queryDirectIosSelector: propagates an AMBIGUOUS_MATCH refusal from the owner rather than swallowing it', async () => {
206-
const querySelector = vi.fn(async (_input: QuerySelectorInput) => {
207-
throw new AppError('AMBIGUOUS_MATCH', 'selector matched multiple elements');
208-
});
209-
210-
await assert.rejects(
211-
() =>
212-
queryDirectIosSelector(
213-
makeSession(),
214-
{ key: 'label', value: 'Checkout form' },
215-
querySelector,
216-
{ execution: {} },
217-
),
218-
(error: unknown) => {
219-
assert.ok(error instanceof AppError);
220-
assert.equal(error.code, 'AMBIGUOUS_MATCH');
221-
return true;
222-
},
223-
);
224-
});

‎src/daemon/direct-ios-selector.ts‎

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ import type { SessionState } from './session-state.ts';
66
import { readSimpleSelectorTarget } from '@agent-device/selectors';
77
import { asAppError, isRequestCanceledError } from '@agent-device/kernel/errors';
88
import type { ElementSelectorTapOptions } from '@agent-device/contracts/interactor-types';
9-
import type {
10-
QuerySelectorNodeReading,
11-
QuerySelectorRuntimeOperations,
12-
} from '@agent-device/contracts/selector-observation-runtime';
13-
import type { SnapshotRuntimeExecution } from '@agent-device/contracts/snapshot-runtime';
149

1510
export type DirectIosSelectorTarget = ElementSelectorTapOptions & { raw: string };
1611

@@ -48,38 +43,6 @@ export function deriveDirectIosNodeSelector(
4843
return null;
4944
}
5045

51-
export type DirectIosSelectorQueryResult = {
52-
found: boolean;
53-
node?: QuerySelectorNodeReading;
54-
};
55-
56-
/**
57-
* The off-screen rescue's live read, executed on the caller's request-bound
58-
* `querySelector` operation (the same binding the command was admitted for; ADR 0019 §2). The
59-
* daemon names no platform and builds no runner request: the owner answers with the geometry it
60-
* measured right now, or its facts declared the operation absent and the hook was never installed.
61-
*/
62-
export async function queryDirectIosSelector(
63-
session: SessionState,
64-
selector: Pick<DirectIosSelectorTarget, 'key' | 'value'>,
65-
querySelector: QuerySelectorRuntimeOperations['querySelector'],
66-
input: Readonly<{
67-
execution: SnapshotRuntimeExecution;
68-
signal?: AbortSignal;
69-
}>,
70-
): Promise<DirectIosSelectorQueryResult> {
71-
const { found, node } = await querySelector({
72-
selector,
73-
...(session.appBundleId === undefined ? {} : { options: { appBundleId: session.appBundleId } }),
74-
execution: input.execution,
75-
...(input.signal === undefined ? {} : { signal: input.signal }),
76-
});
77-
return {
78-
found,
79-
...(node ? { node } : {}),
80-
};
81-
}
82-
8346
/** The runner's selector refusals: it resolved the selector and refused before any gesture. */
8447
const RUNNER_SELECTOR_REFUSAL_CODES: ReadonlySet<string> = new Set([
8548
'ELEMENT_NOT_FOUND',

0 commit comments

Comments
 (0)