Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
),
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: hadKeyboard only recognizes a type: Keyboard node, while the settle chrome classifier also recognizes role-only iOS keyboard surfaces. Include those role-based shapes here so their disappearance receives the 1.5-second confirmation window too.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/command-runtime/src/commands/interaction/runtime/stable-capture.ts, line 427:

<comment>`hadKeyboard` only recognizes a `type: Keyboard` node, while the settle chrome classifier also recognizes role-only iOS keyboard surfaces. Include those role-based shapes here so their disappearance receives the 1.5-second confirmation window too.</comment>

<file context>
@@ -407,6 +419,28 @@ function broadTransitionConfirmation(params: {
+  platform: AgentDeviceRuntime['backend']['platform'];
+}): StableCaptureConfirmation {
+  const hadKeyboard = params.baseline?.nodes.some(
+    (node) => normalizeType(node.type ?? '') === 'keyboard',
+  );
+  return {
</file context>

);
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: (
Expand Down
Loading