Skip to content

Commit 41443e5

Browse files
shivamhwpPR Batch Testerclaude
authored
fix(server): capture iOS screenshots on Xcode 26's simctl (#28353)
Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 0a9abdd commit 41443e5

5 files changed

Lines changed: 64 additions & 14 deletions

File tree

‎apps/server/src/device/DeviceHost.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,12 @@ export interface DeviceHostReady {
162162
readonly run: (
163163
command: string,
164164
args: ReadonlyArray<string>,
165-
options?: { readonly timeoutMs?: number; readonly stdin?: string },
165+
options?: {
166+
readonly timeoutMs?: number;
167+
readonly stdin?: string;
168+
/** Local output cap; SSH output is not capped. */
169+
readonly maxOutputBytes?: number;
170+
},
166171
) => Effect.Effect<{ readonly stdout: string; readonly stderr: string; readonly code: number }>;
167172
readonly helpers: {
168173
readonly serveSimAxSettings: string | null;

‎apps/server/src/device/DeviceHubOperations.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ const LIST_TIMEOUT_SECONDS = 40;
1919
// expo-device-hub 0.19.1 reports a failed simulator listing only as text; its error ids are hashes.
2020
const SIMULATOR_LIST_FAILED =
2121
/^\[apple-utils\] (?:Failed to (?:run `xcrun simctl list devices|parse JSON output:|list devices:)|`xcrun simctl list devices --json` did not respond within )/;
22+
// The hub passes numeric display IDs that Xcode 26 rejects; iOS 27 needs them for multiple displays.
23+
const runsOnXcode26Runtime = (version: string) => Number(/^iOS (\d+)/.exec(version)?.[1]) <= 26;
24+
const DEFAULT_DISPLAY_SCREENSHOT = `f=$(mktemp -t t3-screenshot) || exit 1
25+
trap 'rm -f "$f"' 0
26+
xcrun simctl io "$1" screenshot --type=png "$f" >/dev/null && base64 -i "$f"`;
2227

2328
const HubDevice = Schema.Struct({
2429
id: Schema.String,
@@ -265,6 +270,24 @@ export const makeDeviceHubOperations = (options: {
265270
},
266271
);
267272

273+
const defaultDisplayScreenshot = Effect.fn("DeviceHubOperations.defaultDisplayScreenshot")(
274+
function* (udid: string) {
275+
const capture = yield* run("sh", ["-c", DEFAULT_DISPLAY_SCREENSHOT, "sh", udid], {
276+
timeoutMs: Duration.toMillis(SCREENSHOT_TIMEOUT),
277+
maxOutputBytes: 64 * 1024 * 1024,
278+
});
279+
if (capture.code !== 0) {
280+
return yield* new DeviceOperationError({
281+
operation: "screenshot",
282+
reason: "command_failed",
283+
exitCode: capture.code,
284+
cause: capture.stderr.trim(),
285+
});
286+
}
287+
return new Uint8Array(Buffer.from(capture.stdout, "base64"));
288+
},
289+
);
290+
268291
const screenshot: DeviceOperations["screenshot"] = (device) =>
269292
httpClient
270293
.execute(
@@ -289,6 +312,13 @@ export const makeDeviceHubOperations = (options: {
289312
cause,
290313
}),
291314
),
315+
Effect.catchIf(
316+
(error) =>
317+
device.platform === "ios" &&
318+
runsOnXcode26Runtime(device.version) &&
319+
error.reason === "hub_rejected",
320+
() => defaultDisplayScreenshot(device.id),
321+
),
292322
Effect.withSpan("DeviceHubOperations.screenshot"),
293323
);
294324

‎apps/server/src/device/DeviceHubProxy.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,14 @@ const handler = Effect.gen(function* () {
213213
upstreamSearch.delete("wsTicket");
214214
upstreamSearch.delete("hostId");
215215
const search = upstreamSearch.size > 0 ? `?${upstreamSearch.toString()}` : "";
216+
const screenshotDevice = upstreamSearch.get("device");
217+
if (!ready.upstream && hubPath === "/vendor/serve-sim/api/screenshot" && screenshotDevice) {
218+
const { png } = yield* devices.screenshot({ hostId: ready.hostId, deviceId: screenshotDevice });
219+
return HttpServerResponse.uint8Array(png, {
220+
contentType: "image/png",
221+
headers: { "cache-control": "no-store, no-transform" },
222+
});
223+
}
216224
// Hosts with several upstreams (one per cloud session) route and authenticate requests themselves.
217225
const routed = ready.upstream ? yield* ready.upstream(hubPath, upstreamSearch) : undefined;
218226
if (routed === null) {

‎apps/server/src/device/DeviceService.test.ts‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const decodeBootError = Schema.decodeUnknownSync(Schema.fromJsonString(DeviceBoo
4848
const encodeBootError = Schema.encodeSync(Schema.fromJsonString(DeviceBootError));
4949

5050
it.effect.each([400, 500])(
51-
"distinguishes screenshot HTTP rejection (%s) from transport failure",
51+
"retries a hub-rejected (%s) screenshot with simctl only on iOS 26 and older",
5252
(status) =>
5353
Effect.gen(function* () {
5454
const operations = makeDeviceHubOperations({
@@ -59,22 +59,26 @@ it.effect.each([400, 500])(
5959
HttpClientResponse.fromWeb(request, new Response("private hub output", { status })),
6060
),
6161
),
62-
run: () => Effect.die("Screenshot does not run a command"),
62+
run: () => Effect.succeed({ code: 0, stdout: "iVBORw==", stderr: "" }),
6363
platformAvailable: () => Effect.succeed(false),
6464
});
65-
const result = yield* operations
66-
.screenshot({
67-
id: "phone",
68-
name: "Phone",
69-
version: "iOS 26.3",
70-
platform: "ios",
71-
booted: true,
72-
physical: false,
73-
})
74-
.pipe(Effect.result);
75-
expect(result).toMatchObject({
65+
const screenshot = (version: string) =>
66+
operations
67+
.screenshot({
68+
id: "phone",
69+
name: "Phone",
70+
version,
71+
platform: "ios",
72+
booted: true,
73+
physical: false,
74+
})
75+
.pipe(Effect.result);
76+
expect(yield* screenshot("iOS 27.0")).toMatchObject({
7677
failure: { _tag: "DeviceOperationError", reason: "hub_rejected" },
7778
});
79+
expect(yield* screenshot("iOS 26.3")).toMatchObject({
80+
success: new Uint8Array([137, 80, 78, 71]),
81+
});
7882
}),
7983
);
8084

‎apps/server/src/device/LocalDeviceHost.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,9 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
806806
timeout: Duration.millis(options?.timeoutMs ?? 20_000),
807807
timeoutBehavior: "timedOutResult",
808808
...(options?.stdin === undefined ? {} : { stdin: options.stdin }),
809+
...(options?.maxOutputBytes === undefined
810+
? {}
811+
: { maxOutputBytes: options.maxOutputBytes }),
809812
})
810813
.pipe(
811814
Effect.map((result) => ({

0 commit comments

Comments
 (0)