Skip to content
Merged
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
10 changes: 10 additions & 0 deletions docs/adr/0025-foldable-apple-panels.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The Android emulator pose amendment is dated 2026-10-10.
The no-panel refusal correction (#3761) is dated 2026-10-18.
The guest readback amendment is dated 2026-10-26.
The runner surface amendment is dated 2026-10-31.
The pose panel acceptance amendment is dated 2026-11-23. It adds an exception to the rejected pose-to-panel map.
This ADR covers iPhone Duo (iOS 27.1, `iPhone19,4`) and Apple devices with more than one integrated CoreDevice display.
The amendment below also covers foldable Android emulators.

Expand All @@ -31,6 +32,7 @@ Only inference answers the second question.
| Density normalization | Use the captured panel's `pointScale`. For runner fallback, use that capture's reported pixels-per-point. |
| Pose report | From panel power alone, report only `closed`, `fully-open`, or `unknown`, never a narrower pose. `fold` reports an exact pose only after hinge readings verify it. |
| Pose request | `agent-device fold <closed\|half-open\|open>` sends private HID inside the simulator. Then it reads the hinge angle back inside the simulator until the angle agrees. For `half-open`, two consecutive readings must agree. If the readings never agree, refuse the pose. |
| Lit panel after a named pose | Report only a measured lit panel that the pose lights: `primary` for `closed`, the other panel for `half-open` and `open`. Read the panel table again until a short deadline. Then omit `screen`. An unresolved table, or a table without measured panel power, ends the reads. A custom keyframe angle uses one read. |
| The first hinge read returns no sample | Refuse with `fold-pose-dispatched-unverified`. Panel power does not verify the pose. |
| The hinge reaches `half-open` but keeps moving | Refuse with `fold-pose-unsettled` and the observed and previous angles. An angle inside the open interval is an observed category, not a pose that the hinge holds. |
| External display | It is not a panel. It creates neither multi-screen classification nor a pose. |
Expand Down Expand Up @@ -284,6 +286,13 @@ Before the amendment, one run read 175.1° one stream after Book and 130° two s
If the budget ends at another pose, refuse with `fold-pose-unverified` and the observed angle.
If it ends at unsettled `half-open`, refuse with `fold-pose-unsettled`.
A verified response includes the angle and lit panel's point size.
The guest switches panel power after the hinge stop, so the first panel read can name the panel lit before the fold.
Amendment (2026-11-23): for a named pose, the reported panel must be the measured panel that the pose lights.
That panel is `primary` for `closed` and the other panel for `half-open` and `open`, the only poses with panel measurements.
Panel reads repeat until a short deadline, and each read ends by that deadline. Then the response omits `screen`.
An unresolved table, or a table in which no panel reports measured power, cannot show the switch, so it ends the reads.
A custom keyframe angle keeps one panel read, because no panel measurement exists for interior angles.
This is an explicit exception to the rejected pose-to-panel map below: the pose selects among measured panels and never supplies a panel.
That point size tells an agent its refs are stale.

A single-panel simulator is refused before mutation with `single-panel-device`.
Expand Down Expand Up @@ -366,6 +375,7 @@ Rejected alternatives:
- **Keep devicectl for the default set: rejected.** It keeps a known failure on 651.13.5 and two hinge readers.
- **`simctl io enumerate` as the scoped panel table: rejected.** It reports every port as `Power state: On`, so it cannot identify the lit panel.
- **The lit panel derived from the hinge pose: rejected.** Panel identity and power must be measured. A pose-to-panel map is a second authority.
Exception (2026-11-23): after a named pose, `fold` uses the pose to reject a measured panel that the pose does not light. The pose never supplies a panel.
- **Refuse capture on a scoped multi-panel device: rejected.** The guest reports measured panel power, so the existing capture rules apply unchanged.

## Amendment: foldable rotation through guest HID (2026-10-26)
Expand Down
5 changes: 3 additions & 2 deletions packages/command-registry/src/timeout-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ export const COMPOSITE_TIMEOUT_POLICIES = {

/**
* Covers the fold route's worst-case ledger (display inventory, fold-helper preparation, HID
* dispatch, hinge settle reads, final display inventory) plus the daemon-result margin. Proven by
* the ledger test for a default-set simulator: `test/integration/provider-scenarios/ios-fold.test.ts`.
* dispatch, hinge settle reads, and the lit-panel reads, which a named pose repeats until its
* deadline) plus the daemon-result margin. Proven by the ledger test for a default-set simulator,
* for both a custom trajectory and a named pose: `test/integration/provider-scenarios/ios-fold.test.ts`.
* A scoped-set simulator reads its two display inventories through the guest helper, at most 15s
* each instead of 5s, and still fits.
*/
Expand Down
8 changes: 8 additions & 0 deletions packages/platform-apple/src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ export const IOS_FOLD_POSE_SETTLE_ATTEMPTS = 8;
export const IOS_FOLD_POSE_READ_INTERVAL_MS = 1_000;
/** Two consecutive reads this close together mean the hinge has stopped moving. */
export const IOS_FOLD_POSE_STABLE_DEGREES = 0.5;
/**
* How long a verified named pose waits for the guest panel table to light that pose's panel. The
* hinge reaches its stop before panel power switches, and the table can report both panels lit for
* a few seconds in between. Lit-panel reads repeat at the interval below. The time left on this
* deadline bounds each read, so the wait costs about one read at `IOS_APPLE_DISPLAY_PROBE_TIMEOUT_MS`.
*/
export const IOS_FOLD_PANEL_SETTLE_TIMEOUT_MS = 5_000;
export const IOS_FOLD_PANEL_READ_INTERVAL_MS = 500;
/**
* Bounded lit-panel reads cover delayed HID effects and the two-lit interval after a fold.
* A fixed picker value is sent once per lit panel; the remaining reads only observe.
Expand Down
92 changes: 87 additions & 5 deletions packages/platform-apple/src/foldable/pose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ import {
buildInventory,
queryAppleDisplayInventory,
type AppleDeviceDisplay,
type AppleDisplayInventory,
} from '../core/display-inventory.ts';
import { IOS_FOLD_POSE_READ_INTERVAL_MS, IOS_FOLD_POSE_SETTLE_ATTEMPTS } from '../core/config.ts';
import {
IOS_FOLD_PANEL_READ_INTERVAL_MS,
IOS_FOLD_PANEL_SETTLE_TIMEOUT_MS,
IOS_FOLD_POSE_READ_INTERVAL_MS,
IOS_FOLD_POSE_SETTLE_ATTEMPTS,
} from '../core/config.ts';
import { createFoldHelper } from './fold-helper.ts';
import { setAppleFoldPose } from './pose.ts';

Expand Down Expand Up @@ -86,6 +92,20 @@ function queueBudgetReadings(fill: (index: number) => number, last: readonly num
for (const angle of last) mockHinge.mockResolvedValueOnce(angle);
}

/** Runs one fold under fake timers: the lit-panel poll interval must not cost real time. */
async function foldOnFakeClock(input: Parameters<typeof setAppleFoldPose>[1]) {
vi.useFakeTimers();
try {
const outcome = setAppleFoldPose(duo, input);
await vi.advanceTimersByTimeAsync(
IOS_FOLD_PANEL_SETTLE_TIMEOUT_MS + IOS_FOLD_PANEL_READ_INTERVAL_MS,
);
return await outcome;
} finally {
vi.useRealTimers();
}
}

beforeEach(() => {
mockInventory.mockReset();
mockHinge.mockReset();
Expand Down Expand Up @@ -168,20 +188,79 @@ test("reports native panel points regardless of the display's own currentOrienta
});
});

test('omits the screen report when panel selection is ambiguous, never inventing a viewport', async () => {
// Two lit panels make the inventory ambiguous, so `readLitPanel` refuses to name one; the pose is
test('omits the screen report when panel selection stays ambiguous, never inventing a viewport', async () => {
// Two lit panels make the inventory ambiguous, so the panel read refuses to name one; the pose is
// still verified but carries no `screen` rather than guessing the app viewport.
const bothLit = buildInventory([panel({}), panel({ name: 'LCD-1', displayId: 3 })]);
expect(bothLit.ambiguous).toBe(true);
mockInventory.mockResolvedValueOnce(bothLit).mockResolvedValueOnce(bothLit);
mockInventory.mockResolvedValue(bothLit);
mockHinge.mockResolvedValue(180);

await expect(setAppleFoldPose(duo, { pose: 'open' })).resolves.toEqual({
await expect(foldOnFakeClock({ pose: 'open' })).resolves.toEqual({
pose: 'open',
hingeAngleDegrees: 180,
});
});

test.each([
['open', 180, 'outer', 'inner', { display: 'LCD-1', widthPt: 669, heightPt: 951 }],
['closed', 0, 'inner', 'outer', { display: 'LCD', widthPt: 466, heightPt: 678 }],
] as const)(
'fold %s waits past a first panel read that still names the panel lit before the fold',
async (pose, angle, before, after, screen) => {
mockInventory
.mockResolvedValueOnce(duoInventory(before))
.mockResolvedValueOnce(duoInventory(before))
.mockResolvedValueOnce(duoInventory(after));
mockHinge.mockResolvedValue(angle);

await expect(foldOnFakeClock({ pose })).resolves.toEqual({
pose,
hingeAngleDegrees: angle,
screen: { ...screen, coordinateSpace: 'native-panel' },
});
expect(mockInventory).toHaveBeenCalledTimes(3);
},
);

test('omits the screen when the panel lit before the fold is still lit at the deadline', async () => {
// The hinge verified open, but the table never lit the inner panel: the outer panel is stale
// evidence, so the pose is reported without a screen rather than with the pre-fold panel.
mockInventory.mockResolvedValue(duoInventory('outer'));
mockHinge.mockResolvedValue(180);

await expect(foldOnFakeClock({ pose: 'open' })).resolves.toEqual({
pose: 'open',
hingeAngleDegrees: 180,
});
const panelReads = mockInventory.mock.calls.length - 1;
expect(panelReads).toBe(IOS_FOLD_PANEL_SETTLE_TIMEOUT_MS / IOS_FOLD_PANEL_READ_INTERVAL_MS + 1);
});

test.each<[string, AppleDisplayInventory]>([
['unresolved', { displays: [], multiScreen: false, ambiguous: false, unresolved: true }],
[
'reporting no measured panel power',
buildInventory([
panel({ power: 'unknown' }),
panel({ name: 'LCD-1', displayId: 3, primary: false, power: 'unknown' }),
]),
],
])(
'omits the screen after one read when the panel table is %s, without waiting',
async (_label, table) => {
// Neither table can ever show the panel switch, so a further read cannot help.
mockInventory.mockResolvedValueOnce(duoInventory('outer')).mockResolvedValue(table);
mockHinge.mockResolvedValue(180);

await expect(foldOnFakeClock({ pose: 'open' })).resolves.toEqual({
pose: 'open',
hingeAngleDegrees: 180,
});
expect(mockInventory).toHaveBeenCalledTimes(2);
},
);

test('sends half-open and reports it only once the hinge has stopped', async () => {
mockInventory
.mockResolvedValueOnce(duoInventory('inner'))
Expand Down Expand Up @@ -400,6 +479,9 @@ test.each([
}),
).resolves.toMatchObject({ pose, hingeAngleDegrees: observed });
expect(mockHinge).toHaveBeenCalledTimes(2);
// Panels were measured only at the named poses, so a custom final angle reads the panel once
// and reports the measured panel without requiring the one a named pose lights.
expect(mockInventory).toHaveBeenCalledTimes(2);
},
);

Expand Down
59 changes: 54 additions & 5 deletions packages/platform-apple/src/foldable/pose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import {
import type { DeviceInfo } from '@agent-device/kernel/device';
import { AppError, type ReasonTable } from '@agent-device/kernel/errors';
import { emitDiagnostic } from '@agent-device/host-kit/diagnostics';
import { Deadline, pollUntilSettled } from '@agent-device/host-kit/retry';

import {
IOS_FOLD_PANEL_READ_INTERVAL_MS,
IOS_FOLD_PANEL_SETTLE_TIMEOUT_MS,
IOS_FOLD_POSE_READ_INTERVAL_MS,
IOS_FOLD_POSE_SETTLE_ATTEMPTS,
IOS_FOLD_POSE_STABLE_DEGREES,
Expand Down Expand Up @@ -74,7 +77,10 @@ export async function setAppleFoldPose(
});

const hingeAngleDegrees = await awaitHingePose(device, helper, pose, options.signal, targetAngle);
const litPanel = await readLitPanel(device, helper, options.signal);
// Panels were measured only at the named poses, so a custom final angle keeps one panel read.
const litPanel = intent.pose
? await awaitPoseLitPanel(device, helper, intent.pose, options.signal)
: litPanelOf(await readGuestPanelTable(device, helper, options.signal));
return {
pose: foldPoseForHingeAngle(hingeAngleDegrees)!,
hingeAngleDegrees,
Expand Down Expand Up @@ -103,7 +109,7 @@ async function dispatchedUnverifiedPoseError(
targetAngle: number | undefined,
cause: unknown,
): Promise<AppError> {
const litPanel = await readLitPanel(device, helper, signal);
const litPanel = litPanelOf(await readGuestPanelTable(device, helper, signal));
return new AppError(
'COMMAND_FAILED',
`${device.name} received the ${pose} pose request, but the first hinge-angle read returned no sample to verify it`,
Expand Down Expand Up @@ -290,16 +296,59 @@ function unsettledPoseError(
);
}

async function readLitPanel(
/**
* Reads the lit panel until it is the panel the named pose lights: the `primary` panel when closed
* and the other panel when open or half-open (ADR 0025). A pose whose panel is already lit settles
* on the first read. A table that cannot show a panel switch, because it is unresolved or no panel
* reports measured power, ends the poll at once.
*/
async function awaitPoseLitPanel(
device: DeviceInfo,
foldHelper: FoldHelper,
helper: FoldHelper,
pose: FoldPose,
signal: AbortSignal | undefined,
): Promise<AppleDeviceDisplay | undefined> {
const inventory = await queryAppleDisplayInventory(device, {
const posePanel = (inventory: AppleDisplayInventory) => {
const panel = litPanelOf(inventory);
return panel?.primary === (pose === 'closed') ? panel : undefined;
};
// CONSERVATIVE: The guest switches panel power after the hinge stop, so the first read can name
// the panel lit before the fold; wait for the pose's panel and omit `screen` at the deadline.
// Revisit only if the panel table reports the switch together with the hinge stop.
const deadline = Deadline.fromTimeoutMs(IOS_FOLD_PANEL_SETTLE_TIMEOUT_MS);
const { value } = await pollUntilSettled({
// The time left on the deadline bounds each read, so the poll costs about one full read.
probe: () =>
readGuestPanelTable(device, helper, signal, {
timeoutMs: Math.max(deadline.remainingMs(), IOS_FOLD_PANEL_READ_INTERVAL_MS),
}),
settled: (inventory) => !hasMeasuredPanelPower(inventory) || posePanel(inventory) !== undefined,
deadline,
intervalMs: IOS_FOLD_PANEL_READ_INTERVAL_MS,
signal,
});
return posePanel(value);
}

function hasMeasuredPanelPower(inventory: AppleDisplayInventory): boolean {
return !inventory.unresolved && inventory.displays.some((display) => display.power !== 'unknown');
}

async function readGuestPanelTable(
device: DeviceInfo,
foldHelper: FoldHelper,
signal: AbortSignal | undefined,
options: { timeoutMs?: number } = {},
): Promise<AppleDisplayInventory> {
return await queryAppleDisplayInventory(device, {
signal,
source: 'guest',
foldHelper,
timeoutMs: options.timeoutMs,
});
}

function litPanelOf(inventory: AppleDisplayInventory): AppleDeviceDisplay | undefined {
if (inventory.unresolved || inventory.ambiguous) return undefined;
return inventory.activeDisplay;
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/schema/cli-help-foldable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Screens are handled for you:

Changing the pose:
agent-device fold closed | half-open | open
fold sends a private HID hinge event inside the selected simulator and then reads the hinge angle back until it agrees, each read a separate in-simulator CoreMotion sample: closed is 0 degrees, open is 180, and half-open is any angle between them. An angle in that interval only proves the category, so half-open is reported once consecutive readings both fall inside it and agree. The response reports the verified pose, the hinge angle, and the panel the device now lights with its native panel point size, marked coordinateSpace "native-panel". That point size is the panel's own geometry, not the next snapshot's viewport, so it cannot place a tap: the active app window can differ (a 669x951 inner panel hosts a 951x669 window). A hinge whose last reading is some other pose fails with COMMAND_FAILED and reason fold-pose-unverified, naming the angle the simulator still reports; a hinge seen half-open but never at rest fails with reason fold-pose-unsettled, naming the observed and previous angles; a first hinge-angle read that returns no sample (for example a simulator runtime without CoreMotion hinge support) fails with reason fold-pose-dispatched-unverified, meaning the pose was sent but cannot be confirmed; hingeReadFailure holds the read's own failure details, and screen holds the lit panel's size when panel power decides one panel, so take a screenshot or snapshot to check. A single-panel simulator fails with UNSUPPORTED_OPERATION. A simulator in a scoped set selected with --ios-simulator-device-set folds the same way: the HID send, the hinge reads, and the panel reads all run through simctl --set.
fold sends a private HID hinge event inside the selected simulator and then reads the hinge angle back until it agrees, each read a separate in-simulator CoreMotion sample: closed is 0 degrees, open is 180, and half-open is any angle between them. An angle in that interval only proves the category, so half-open is reported once consecutive readings both fall inside it and agree. The response reports the verified pose, the hinge angle, and the panel the device now lights with its native panel point size, marked coordinateSpace "native-panel". The panel table can switch panels a few seconds after the hinge stops, so after closed, half-open, or open, fold keeps reading it for a few seconds until the pose's panel is lit (the primary outer panel when closed, the inner panel otherwise). A fold whose panel has not switched therefore takes a few seconds longer and then omits screen rather than name the panel lit before the fold. A custom keyframe angle reports the panel from one read. That point size is the panel's own geometry, not the next snapshot's viewport, so it cannot place a tap: the active app window can differ (a 669x951 inner panel hosts a 951x669 window). A hinge whose last reading is some other pose fails with COMMAND_FAILED and reason fold-pose-unverified, naming the angle the simulator still reports; a hinge seen half-open but never at rest fails with reason fold-pose-unsettled, naming the observed and previous angles; a first hinge-angle read that returns no sample (for example a simulator runtime without CoreMotion hinge support) fails with reason fold-pose-dispatched-unverified, meaning the pose was sent but cannot be confirmed; hingeReadFailure holds the read's own failure details, and screen holds the lit panel's size when panel power decides one panel, so take a screenshot or snapshot to check. A single-panel simulator fails with UNSUPPORTED_OPERATION. A simulator in a scoped set selected with --ios-simulator-device-set folds the same way: the HID send, the hinge reads, and the panel reads all run through simctl --set.
Expect a fold to take 2-4 seconds, longer for the first fold after a boot while the hinge settles; half-open waits for agreeing hinge reads. Re-snapshot after every fold; refs and coordinates from before it are stale, and the command's message says so.
Timed motion: fold --keyframes '[{"atMs":0,"angle":0},{"atMs":5000,"angle":180}]'. Use 2–64 frames starting at 0ms, increasing integer timestamps up to 60000ms, and angles from 0 to 180. The last timestamp sets motion duration, excluding setup and verification. Equal angles hold; cancellation stops motion. See the fold examples in the command and Node API documentation for trajectories.

Expand Down
Loading
Loading