From 050185bc5cc143faa4bd3ae601a3789ae5dfb634 Mon Sep 17 00:00:00 2001 From: Artem Diadiushko Date: Sun, 6 Dec 2026 10:16:06 +0200 Subject: [PATCH] fix(ios): confirm keyboard absence before settle diff --- .../runtime/settle-keyboard-chrome.test.ts | 37 +++++++++- .../runtime/stable-capture.test.ts | 71 +++++++++++++++++++ .../interaction/runtime/stable-capture.ts | 38 +++++++++- 3 files changed, 143 insertions(+), 3 deletions(-) diff --git a/packages/command-runtime/src/commands/interaction/runtime/settle-keyboard-chrome.test.ts b/packages/command-runtime/src/commands/interaction/runtime/settle-keyboard-chrome.test.ts index 59245aef6c2..d227a7ab8c3 100644 --- a/packages/command-runtime/src/commands/interaction/runtime/settle-keyboard-chrome.test.ts +++ b/packages/command-runtime/src/commands/interaction/runtime/settle-keyboard-chrome.test.ts @@ -3,7 +3,7 @@ import { test } from 'vitest'; import { makeSnapshotState } from '@agent-device/capture-kit/snapshot-state-fixtures'; import { iosInteractiveKeyboardCaptureNodes } from '@agent-device/capture-kit/snapshot-chrome-fixtures'; import { selector } from './selector-read-utils.ts'; -import { createSettleDevice } from './__tests__/settle-device-fixtures.ts'; +import { createFakeClock, createSettleDevice } from './__tests__/settle-device-fixtures.ts'; // #3469: `--settle` on iOS captured with `interactiveOnly: true`, whose // projection flattened the keyboard beside app content, so @@ -56,3 +56,38 @@ test('the settled diff after a fill shows the keyboard as one line, not one per assert.equal(diff.summary.additions, 3); assert.equal(diff.summary.removals, 0); }); + +test('fill --settle does not report keyboard removal when a missing window returns', async () => { + const keyboardUp = makeSnapshotState(iosInteractiveKeyboardCaptureNodes(), { + backend: 'xctest', + snapshotQuality: { state: 'healthy', backend: 'tree' }, + }); + const missingKeyboard = makeSnapshotState(appOnlyNodes(), { + backend: 'xctest', + snapshotQuality: { state: 'healthy', backend: 'tree' }, + keyboard: { kind: 'absent', treeBound: true }, + }); + const clock = createFakeClock(); + let captures = 0; + const device = createSettleDevice({ + stored: keyboardUp, + clock, + captureSnapshot: () => { + captures += 1; + return { snapshot: captures === 1 || clock.now() >= 900 ? keyboardUp : missingKeyboard }; + }, + }); + + const result = await device.interactions.fill(selector('id=catalog-search'), 'kit', { + session: 'default', + settle: {}, + }); + + assert.equal(result.settle?.settled, true); + assert.ok((result.settle?.waitedMs ?? 0) >= 900); + assert.ok( + !result.settle?.diff?.lines.some( + (line) => line.kind === 'removed' && /\[keyboard\]|\[scroll-area\]/.test(line.text), + ), + ); +}); diff --git a/packages/command-runtime/src/commands/interaction/runtime/stable-capture.test.ts b/packages/command-runtime/src/commands/interaction/runtime/stable-capture.test.ts index 81f975f0860..c6ac7f80971 100644 --- a/packages/command-runtime/src/commands/interaction/runtime/stable-capture.test.ts +++ b/packages/command-runtime/src/commands/interaction/runtime/stable-capture.test.ts @@ -594,6 +594,77 @@ test('settle keeps the default quiet window when only keyboard chrome arrives', assert.ok(outcome.waitedMs < 800, `keyboard arrival settled after ${outcome.waitedMs}ms`); }); +test('settle waits through a transient missing keyboard window', async () => { + const { runtime, keyboardUp } = keyboardDisappearanceRuntime(900); + const outcome = await runStableCaptureLoop( + runtime, + { session: 'default' }, + { + quietMs: 500, + timeoutMs: 5_000, + broadTransitionBaseline: { nodes: keyboardUp.nodes }, + }, + ); + + assert.equal(outcome.settled, true); + assert.ok(outcome.waitedMs >= 900, `settled on a missing keyboard after ${outcome.waitedMs}ms`); + assert.ok(outcome.lastCapture?.snapshot.nodes.some((node) => node.type === 'Keyboard')); +}); + +test('settle still confirms a real keyboard dismissal', async () => { + const { runtime, keyboardUp } = keyboardDisappearanceRuntime(Number.POSITIVE_INFINITY); + const outcome = await runStableCaptureLoop( + runtime, + { session: 'default' }, + { + quietMs: 500, + timeoutMs: 5_000, + broadTransitionBaseline: { nodes: keyboardUp.nodes }, + }, + ); + + assert.equal(outcome.settled, true); + assert.ok(outcome.waitedMs >= 1_500, `dismissal settled after ${outcome.waitedMs}ms`); + assert.equal( + outcome.lastCapture?.snapshot.nodes.some((node) => node.type === 'Keyboard'), + false, + ); +}); + +function keyboardDisappearanceRuntime(keyboardReturnsAtMs: number) { + const keyboardUp = makeSnapshotState(iosInteractiveKeyboardCaptureNodes(), { + backend: 'xctest', + snapshotQuality: { state: 'healthy', backend: 'tree' }, + }); + const missingKeyboard = makeSnapshotState( + keyboardUp.nodes.filter((node) => node.index <= 30), + { + backend: 'xctest', + snapshotQuality: { state: 'healthy', backend: 'tree' }, + keyboard: { kind: 'absent', treeBound: true }, + }, + ); + let elapsedMs = 0; + const runtime = createAgentDevice({ + backend: { + platform: 'ios', + captureSnapshot: async () => ({ + snapshot: elapsedMs < keyboardReturnsAtMs ? missingKeyboard : keyboardUp, + }), + } satisfies AgentDeviceBackend, + artifacts: createLocalArtifactAdapter(), + sessions: createMemorySessionStore([{ name: 'default', snapshot: keyboardUp }]), + policy: localCommandPolicy(), + clock: { + now: () => elapsedMs, + sleep: async (ms: number) => { + elapsedMs += ms; + }, + }, + }); + return { runtime, keyboardUp }; +} + function staticSnapshotRuntime(snapshot: SnapshotState) { let elapsedMs = 0; const runtime = createAgentDevice({ diff --git a/packages/command-runtime/src/commands/interaction/runtime/stable-capture.ts b/packages/command-runtime/src/commands/interaction/runtime/stable-capture.ts index f8b8d514d26..aa37bbf35d4 100644 --- a/packages/command-runtime/src/commands/interaction/runtime/stable-capture.ts +++ b/packages/command-runtime/src/commands/interaction/runtime/stable-capture.ts @@ -6,7 +6,7 @@ import type { } from '@agent-device/kernel/snapshot'; import { postActionPresentation } from '@agent-device/capture-kit/snapshot-baseline'; import type { SnapshotState } from '@agent-device/contracts/snapshot-model'; -import { isViewportRootNode } from '@agent-device/contracts/snapshot'; +import { isViewportRootNode, normalizeType } from '@agent-device/contracts/snapshot'; import { snapshotProducerFacts } from '@agent-device/contracts/snapshot-producers'; import type { AgentDeviceRuntime, CommandContext } from '@agent-device/contracts/runtime-contract'; import type { SurfaceScopedNodes } from '@agent-device/contracts/interaction'; @@ -52,6 +52,7 @@ const QUIET_DEADLINE_EPSILON_MS = 2; // transitional AX tree for the default quiet window. Confirm those transitions longer without // adding latency to local mutations whose pre/post trees substantially overlap. const BROAD_TRANSITION_CONFIRMATION_QUIET_MS = 1_500; +const KEYBOARD_DISMISSAL_CONFIRMATION_QUIET_MS = 1_500; export const DEFAULT_STABLE_QUIET_MS = 500; /** * The default quiet window a capture earns, keyed on the producer and not the channel: the bridge @@ -123,11 +124,22 @@ export async function runStableCaptureLoop( // therefore shorten the window; a confirmed broad replacement never drops below its own floor. // A window the caller asked for is fixed. let quietMs = params.quietMs ?? DEFAULT_STABLE_QUIET_MS; - const confirmation = broadTransitionConfirmation({ + const broadConfirmation = broadTransitionConfirmation({ baseline: params.broadTransitionBaseline, platform: runtime.backend.platform, appBundleId: session?.appBundleId, }); + const keyboardConfirmation = keyboardDisappearanceConfirmation({ + baseline: params.broadTransitionBaseline, + platform: runtime.backend.platform, + }); + const confirmation: StableCaptureConfirmation = { + requiredQuietMs: (snapshot, currentQuietMs) => + Math.max( + broadConfirmation.requiredQuietMs(snapshot, currentQuietMs), + keyboardConfirmation.requiredQuietMs(snapshot, currentQuietMs), + ), + }; // The first capture is routed afresh rather than pinned to the producer of the session's last // read. That read may come from a runner fallback the route no longer needs, such as a system // surface since dismissed, and a pin taken from it would keep every later settle on the runner, @@ -407,6 +419,28 @@ function broadTransitionConfirmation(params: { }; } +function keyboardDisappearanceConfirmation(params: { + baseline: SurfaceScopedNodes | undefined; + platform: AgentDeviceRuntime['backend']['platform']; +}): StableCaptureConfirmation { + const hadKeyboard = params.baseline?.nodes.some( + (node) => normalizeType(node.type ?? '') === 'keyboard', + ); + return { + requiredQuietMs(snapshot, currentQuietMs) { + // A captured tree can miss the keyboard window while it remains visible (#5417). + if ( + !hadKeyboard || + !isBoundIosXCTestCapture(snapshot.backend, params.platform) || + snapshot.nodes.some((node) => normalizeType(node.type ?? '') === 'keyboard') + ) { + return currentQuietMs; + } + return Math.max(currentQuietMs, KEYBOARD_DISMISSAL_CONFIRMATION_QUIET_MS); + }, + }; +} + type StableCaptureConfirmation = { /** The quiet window this capture demands, at least the loop's current one. */ requiredQuietMs: (