Skip to content

Commit c75affa

Browse files
fix(ios): a scoped rotation learns nothing from a panel that did not move
Review of callstack#4440 (cubic): the offset re-derived from the last dispatch and a same-panel reading assumed that dispatch had landed. A reading taken before it landed, or a panel whose app cannot show the target, taught a wrong offset; on the device a portrait-only app made the loop cycle through six picker values, upside-down included. A reading that still shows what the panel showed before the last dispatch now teaches nothing and gets no further dispatch: the loop re-reads, and a panel that never follows fails unverified after one dispatch. Also qualify the attempt-budget comment and note that both panels report lit for a few seconds after an unfold as well as a fold. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 09b2765 commit c75affa

4 files changed

Lines changed: 64 additions & 24 deletions

File tree

‎.changeset/foldable-scoped-rotation.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'agent-device': patch
33
---
44

5-
Fix `orientation` on a foldable iPhone simulator (iPhone Duo) in a scoped simulator set. It no longer refuses with `unsupported-device-scope`: the fold helper already reaches a scoped set, so the rotation is dispatched through it and succeeds only when the lit panel shows the requested orientation, otherwise failing with `orientation-unverified-scoped-set`. Measured on iPhone Duo, the lit panel usually reads the orientation-picker value plus a fixed per-panel offset (0 degrees on the outer panel, +90 degrees on the inner panel), and the picker value survives a fold; a panel whose foreground app cannot show that orientation keeps its previous one instead. Each dispatch is therefore the target minus the lit panel's offset, taken from the measured value at first and then recalibrated from the last dispatch and a fresh reading of the same panel, so the first command lands. The fold helper also maps `portrait-upside-down` to the picker's `pud` wire spelling, which the inner panel needs to reach landscape-left; a user target of `portrait-upside-down` stays refused.
5+
Fix `orientation` on a foldable iPhone simulator (iPhone Duo) in a scoped simulator set. It no longer refuses with `unsupported-device-scope`: the fold helper already reaches a scoped set, so the rotation is dispatched through it and succeeds only when the lit panel shows the requested orientation, otherwise failing with `orientation-unverified-scoped-set`. Measured on iPhone Duo, the lit panel usually reads the orientation-picker value plus a fixed per-panel offset (0 degrees on the outer panel, +90 degrees on the inner panel), and the picker value survives a fold; a panel whose foreground app cannot show that orientation keeps its previous one instead. Each dispatch is therefore the target minus the lit panel's offset, taken from the measured value at first and then recalibrated from the last dispatch and a fresh reading of the same panel, so the first command lands. A reading that has not moved since the last dispatch teaches nothing and is not followed by another dispatch, so a panel that cannot show the target fails after one dispatch. The fold helper also maps `portrait-upside-down` to the picker's `pud` wire spelling, which the inner panel needs to reach landscape-left; a user target of `portrait-upside-down` stays refused.

‎packages/platform-apple/src/core/config.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ export const IOS_FOLDABLE_ORIENTATION_READ_TIMEOUT_MS = 2_000;
6767
* How many read/dispatch rounds a scoped-set foldable rotation gets. A panel at its measured
6868
* picker offset lands in one dispatch, and a panel that disagrees with the measured offset in
6969
* two, the second calibrated by the first. The rest of the budget covers reads that name no
70-
* single lit panel, such as the few seconds after an iPhone Duo folds closed. Verified on
71-
* iPhone Duo with the settle delay below between a dispatch and the read that confirms it.
70+
* single lit panel, such as the few seconds after an iPhone Duo folds or unfolds, and reads taken
71+
* before a dispatch has landed. Exercised on an iPhone Duo simulator (iOS 27.3) with this loop and
72+
* the settle delay below between a dispatch and the read that confirms it.
7273
*/
7374
export const IOS_FOLDABLE_SCOPED_ORIENTATION_ATTEMPTS = 6;
7475
export const IOS_FOLDABLE_SCOPED_ORIENTATION_SETTLE_MS = 600;

‎packages/platform-apple/src/foldable/orientation.test.ts‎

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,17 @@ function fakeScopedDuo(
194194
lastShown?: Partial<Record<DuoPanel, DeviceRotation>>;
195195
/** From this panel read on (1-based, the routing probe included), both panels report lit. */
196196
bothLitFromRead?: number;
197+
/** How many panel reads after a picker event still show the panel as it was before it. */
198+
hidLagReads?: number;
197199
},
198200
) {
199201
const helpers: string[][] = [];
200202
const devicectl: string[] = [];
201203
const offsets = options.offsets ?? MEASURED_OFFSETS;
202204
let lit = options.lit;
203205
let pickerAngle = PANEL_ANGLE[options.picker];
206+
let pendingPicker: number | undefined;
207+
let lagReadsLeft = 0;
204208
let bothLitReads = 0;
205209
let panelReads = 0;
206210
const shown: Record<DuoPanel, number> = {
@@ -239,6 +243,12 @@ function fakeScopedDuo(
239243
helpers.push(mode);
240244
if (mode[0] === 'panels') {
241245
panelReads += 1;
246+
if (lagReadsLeft > 0) lagReadsLeft -= 1;
247+
else if (pendingPicker !== undefined) {
248+
pickerAngle = pendingPicker;
249+
pendingPicker = undefined;
250+
show(lit);
251+
}
242252
const bothLit =
243253
bothLitReads > 0 ||
244254
(options.bothLitFromRead !== undefined && panelReads >= options.bothLitFromRead);
@@ -247,7 +257,10 @@ function fakeScopedDuo(
247257
return { stdout: JSON.stringify({ version: 1, displays }), stderr: '', exitCode: 0 };
248258
}
249259
expect(mode[0]).toBe('rotate');
250-
if (options.followsHid !== false) {
260+
if (options.followsHid !== false && options.hidLagReads) {
261+
pendingPicker = PANEL_ANGLE[mode[1]! as DeviceRotation];
262+
lagReadsLeft = options.hidLagReads;
263+
} else if (options.followsHid !== false) {
251264
pickerAngle = PANEL_ANGLE[mode[1]! as DeviceRotation];
252265
show(lit);
253266
}
@@ -447,7 +460,7 @@ test('an undecided final read does not report an earlier reading as the current
447460
});
448461
expect((outcome as AppError).details).not.toHaveProperty('observedOrientation');
449462
expect((outcome as AppError).details).not.toHaveProperty('rawPanelOrientation');
450-
expect(sim.rotations()).toEqual(['landscape-left', 'portrait-upside-down']);
463+
expect(sim.rotations()).toEqual(['landscape-left']);
451464
});
452465

453466
test('a scoped rotation to the orientation already showing dispatches nothing', async () => {
@@ -461,6 +474,19 @@ test('a scoped rotation to the orientation already showing dispatches nothing',
461474
expect(sim.rotations()).toEqual([]);
462475
});
463476

477+
test('a reading taken before the dispatch lands teaches nothing and is not followed by another dispatch', async () => {
478+
const scoped = simulator({ simulatorSetPath: '/tmp/scoped-set' });
479+
// The first two panel reads after each picker event still show the panel as it was before it.
480+
const sim = fakeScopedDuo(scoped, { lit: 'inner', picker: 'portrait', hidLagReads: 2 });
481+
expect(sim.litScreen()).toBe('rot90');
482+
await expect(rotateScoped(sim, 'landscape-left')).resolves.toEqual({
483+
orientation: 'landscape-left',
484+
});
485+
// One exact dispatch (landscape-left - 90 = portrait-upside-down); the stale reads in between
486+
// neither re-learned an offset nor dispatched again.
487+
expect(sim.rotations()).toEqual(['portrait-upside-down']);
488+
});
489+
464490
test('a scoped rotation the lit panel never shows is refused as unverified', async () => {
465491
const scoped = simulator({ simulatorSetPath: '/tmp/scoped-set' });
466492
const sim = fakeScopedDuo(scoped, {
@@ -478,15 +504,9 @@ test('a scoped rotation the lit panel never shows is refused as unverified', asy
478504
rawPanelOrientation: 'rot90',
479505
},
480506
});
481-
// Each dispatch re-derives the offset from the unchanged reading: six rounds, then refusal.
482-
expect(sim.rotations()).toEqual([
483-
'landscape-left',
484-
'portrait-upside-down',
485-
'landscape-right',
486-
'portrait',
487-
'landscape-left',
488-
'portrait-upside-down',
489-
]);
507+
// The panel never moved after the one dispatch, so it taught no offset and nothing more was
508+
// dispatched: the loop re-read until its budget ended, then refused.
509+
expect(sim.rotations()).toEqual(['landscape-left']);
490510
expect(sim.devicectl).toEqual([]);
491511
});
492512

‎packages/platform-apple/src/foldable/orientation.ts‎

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,9 @@ export async function setAppleFoldableOrientation(
168168
* so one absolute dispatch of the target lands a quarter-turn away on the inner panel and looks
169169
* one command late. One bounded loop reads the lit panel and, until it shows the target,
170170
* dispatches `target - offset`: the offset comes from this command's last dispatch and a fresh
171-
* reading of the same panel once there is one, and from the measured table before that. A read
172-
* that names no single lit panel neither confirms nor dispatches; the loop waits for the next.
171+
* reading of the same panel that moved since it, and from the measured table before that. A read
172+
* that names no single lit panel, or a panel that has not moved since the last dispatch, neither
173+
* confirms nor dispatches; the loop waits for the next read.
173174
* A repeat of the showing orientation is a read-only success. Nothing is kept between commands:
174175
* a fold in between can light the other panel, and the measured table covers that panel.
175176
*/
@@ -185,11 +186,11 @@ async function setScopedFoldableOrientation(
185186
signal?.throwIfAborted();
186187
reading = await readLitPanel(device, helper, signal);
187188
if (reading?.rotation === rotation) return { orientation: rotation };
188-
if (reading) {
189+
if (reading && !awaitingDispatchEffect(dispatched, reading)) {
189190
const panelOffset = scopedPanelOffset(dispatched, reading);
190191
const value = pickerValueForTarget(rotation, panelOffset);
191192
await helper.rotate(value, signal);
192-
dispatched = { value, displayId: reading.panel.displayId };
193+
dispatched = { value, displayId: reading.panel.displayId, before: reading.rotation };
193194
emitScopedDispatch(device, rotation, attempt, value, panelOffset, reading);
194195
}
195196
await settleScopedRotation();
@@ -200,14 +201,32 @@ async function setScopedFoldableOrientation(
200201
throw scopedUnverifiedError(device, rotation, reading);
201202
}
202203

203-
/** The picker value this command last dispatched, and the panel that was lit for it. */
204-
type ScopedDispatch = { value: DeviceRotation; displayId: number };
204+
/**
205+
* The picker value this command last dispatched, the panel that was lit for it, and the
206+
* rotation that panel showed before the dispatch.
207+
*/
208+
type ScopedDispatch = { value: DeviceRotation; displayId: number; before: DeviceRotation };
205209

206210
type LitPanel = { panel: AppleDeviceDisplay; rotation: DeviceRotation };
207211

208212
/**
209-
* The lit panel's picker offset. After this command dispatched to the same panel, the fresh
210-
* reading and that dispatch give the offset this panel really has. Before any dispatch, or when
213+
* The same panel still shows what it showed before the last dispatch: the dispatch has not taken
214+
* effect yet, or the app in front cannot show that orientation. Such a reading teaches no offset,
215+
* so nothing is dispatched on it; the loop re-reads, and a panel that never follows ends the
216+
* command unverified after one dispatch instead of cycling through picker values.
217+
*/
218+
function awaitingDispatchEffect(
219+
dispatched: ScopedDispatch | undefined,
220+
reading: LitPanel,
221+
): boolean {
222+
return (
223+
dispatched?.displayId === reading.panel.displayId && reading.rotation === dispatched.before
224+
);
225+
}
226+
227+
/**
228+
* The lit panel's picker offset. After this command dispatched to the same panel and the panel
229+
* moved, the fresh reading and that dispatch give the offset this panel really has. Before any dispatch, or when
211230
* a fold lit another panel mid-command, the measured offset of the panel now lit applies.
212231
*/
213232
function scopedPanelOffset(
@@ -275,8 +294,8 @@ function scopedUnverifiedError(
275294
* The one lit panel and the rotation it shows, from one fresh panel-table read. Anything
276295
* less decided — an unreadable table, no lit panel, an unrecognized `rotN` — is no reading:
277296
* the rotation loop must not confirm, compensate or dispatch from a panel that is not showing.
278-
* On iPhone Duo the table reports both panels lit for a few seconds after a fold to closed, so
279-
* the first reads after that fold are no reading by design.
297+
* On iPhone Duo the table reports both panels lit for a few seconds after a pose change in either
298+
* direction, so the first reads after a fold or an unfold are no reading by design.
280299
*/
281300
async function readLitPanel(
282301
device: DeviceInfo,

0 commit comments

Comments
 (0)