Skip to content

Commit 9b18dc5

Browse files
shivamhwpclaude
andcommitted
fix(server): cut the Xcode 26 screenshot fallback down to what the fix needs
The Save screenshot route now just returns the device service's PNG and lets failures surface as the 500 main already returns. The shell script drops its signal trap; the exit trap still removes the temp file. Tests are back to main's one screenshot case, extended to cover the simctl retry. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent 3ab9744 commit 9b18dc5

3 files changed

Lines changed: 62 additions & 121 deletions

File tree

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

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const SIMULATOR_LIST_FAILED =
2323
const runsOnXcode26Runtime = (version: string) => Number(/^iOS (\d+)/.exec(version)?.[1]) <= 26;
2424
const DEFAULT_DISPLAY_SCREENSHOT = `f=$(mktemp -t t3-screenshot) || exit 1
2525
trap 'rm -f "$f"' 0
26-
trap 'exit 1' HUP INT TERM
2726
xcrun simctl io "$1" screenshot --type=png "$f" >/dev/null && base64 -i "$f"`;
2827

2928
const HubDevice = Schema.Struct({
@@ -271,7 +270,25 @@ export const makeDeviceHubOperations = (options: {
271270
},
272271
);
273272

274-
const hubScreenshot: DeviceOperations["screenshot"] = (device) =>
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+
291+
const screenshot: DeviceOperations["screenshot"] = (device) =>
275292
httpClient
276293
.execute(
277294
HttpClientRequest.post(
@@ -295,37 +312,15 @@ export const makeDeviceHubOperations = (options: {
295312
cause,
296313
}),
297314
),
315+
Effect.catchIf(
316+
(error) =>
317+
device.platform === "ios" &&
318+
runsOnXcode26Runtime(device.version) &&
319+
error.reason === "hub_rejected",
320+
() => defaultDisplayScreenshot(device.id),
321+
),
322+
Effect.withSpan("DeviceHubOperations.screenshot"),
298323
);
299324

300-
const defaultDisplayScreenshot = Effect.fn("DeviceHubOperations.defaultDisplayScreenshot")(
301-
function* (udid: string) {
302-
const capture = yield* run("sh", ["-c", DEFAULT_DISPLAY_SCREENSHOT, "sh", udid], {
303-
timeoutMs: Duration.toMillis(SCREENSHOT_TIMEOUT),
304-
maxOutputBytes: 64 * 1024 * 1024,
305-
});
306-
if (capture.code !== 0) {
307-
return yield* new DeviceOperationError({
308-
operation: "screenshot",
309-
reason: "command_failed",
310-
exitCode: capture.code,
311-
cause: capture.stderr.trim(),
312-
});
313-
}
314-
return new Uint8Array(Buffer.from(capture.stdout, "base64"));
315-
},
316-
);
317-
318-
const screenshot: DeviceOperations["screenshot"] = (device) =>
319-
hubScreenshot(device).pipe(
320-
Effect.catchIf(
321-
(error) =>
322-
device.platform === "ios" &&
323-
runsOnXcode26Runtime(device.version) &&
324-
error.reason === "hub_rejected",
325-
() => defaultDisplayScreenshot(device.id),
326-
),
327-
Effect.withSpan("DeviceHubOperations.screenshot"),
328-
);
329-
330325
return { list, boot, attach, shutdown, screenshot };
331326
};

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

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -214,34 +214,9 @@ const handler = Effect.gen(function* () {
214214
upstreamSearch.delete("hostId");
215215
const search = upstreamSearch.size > 0 ? `?${upstreamSearch.toString()}` : "";
216216
const screenshotDevice = upstreamSearch.get("device");
217-
if (
218-
!ready.upstream &&
219-
request.method === "POST" &&
220-
hubPath === "/vendor/serve-sim/api/screenshot" &&
221-
screenshotDevice &&
222-
upstreamSearch.size === 1
223-
) {
224-
return yield* devices.screenshot({ hostId: ready.hostId, deviceId: screenshotDevice }).pipe(
225-
Effect.map(({ png }) =>
226-
HttpServerResponse.uint8Array(png, {
227-
contentType: "image/png",
228-
headers: { "cache-control": "no-store" },
229-
}),
230-
),
231-
Effect.catch((error) =>
232-
Effect.logWarning("Device screenshot failed", {
233-
errorTag: error._tag,
234-
...(error._tag === "DeviceOperationError" ? { reason: error.reason } : {}),
235-
}).pipe(
236-
Effect.as(
237-
HttpServerResponse.text(
238-
error._tag === "DeviceNotFoundError" ? "Device not found" : "Screenshot failed",
239-
{ status: error._tag === "DeviceNotFoundError" ? 404 : 502 },
240-
),
241-
),
242-
),
243-
),
244-
);
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, { contentType: "image/png" });
245220
}
246221
// Hosts with several upstreams (one per cloud session) route and authenticate requests themselves.
247222
const routed = ready.upstream ? yield* ready.upstream(hubPath, upstreamSearch) : undefined;

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

Lines changed: 32 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -47,68 +47,39 @@ const decodeJson = Schema.decodeUnknownSync(Schema.fromJsonString(Schema.Unknown
4747
const decodeBootError = Schema.decodeUnknownSync(Schema.fromJsonString(DeviceBootError));
4848
const encodeBootError = Schema.encodeSync(Schema.fromJsonString(DeviceBootError));
4949

50-
const PNG = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]);
51-
52-
it.effect.each([
53-
{
54-
name: "keeps the hub's rejection for Android",
55-
platform: "android" as const,
56-
expected: { failure: { _tag: "DeviceOperationError", reason: "hub_rejected" } },
57-
},
58-
{
59-
name: "keeps the hub's rejection on iOS 27 runtimes, which can have several displays",
60-
version: "iOS 27.2.iPhone19.4",
61-
expected: { failure: { _tag: "DeviceOperationError", reason: "hub_rejected" } },
62-
},
63-
{
64-
name: "captures the default display with simctl when the hub rejects an iOS screenshot",
65-
capture: { code: 0, stdout: `${Buffer.from(PNG).toString("base64")}\n`, stderr: "" },
66-
expected: { success: PNG },
67-
},
68-
{
69-
name: "reports simctl's own failure when the iOS fallback fails",
70-
capture: { code: 1, stdout: "", stderr: "Error: Device is not booted" },
71-
expected: {
72-
failure: {
73-
_tag: "DeviceOperationError",
74-
reason: "command_failed",
75-
exitCode: 1,
76-
cause: "Error: Device is not booted",
77-
},
78-
},
79-
},
80-
])("screenshot $name", ({ platform = "ios" as const, version, capture, expected }) =>
81-
Effect.gen(function* () {
82-
const operations = makeDeviceHubOperations({
83-
hostId: LOCAL_DEVICE_HOST_ID,
84-
origin: "http://device.test",
85-
http: HttpClient.make((request) =>
86-
Effect.succeed(
87-
HttpClientResponse.fromWeb(request, new Response("private hub output", { status: 500 })),
50+
it.effect.each([400, 500])(
51+
"retries a hub-rejected (%s) screenshot with simctl only on iOS 26 and older",
52+
(status) =>
53+
Effect.gen(function* () {
54+
const operations = makeDeviceHubOperations({
55+
hostId: LOCAL_DEVICE_HOST_ID,
56+
origin: "http://device.test",
57+
http: HttpClient.make((request) =>
58+
Effect.succeed(
59+
HttpClientResponse.fromWeb(request, new Response("private hub output", { status })),
60+
),
8861
),
89-
),
90-
run: (command, args) =>
91-
command === "sh" && args.at(-1) === "phone" && capture
92-
? Effect.sync(() => {
93-
expect(args[1]).toContain('simctl io "$1" screenshot ');
94-
expect(args[1]).not.toContain("--display");
95-
return capture;
96-
})
97-
: Effect.die(`Unexpected command: ${command} ${args.join(" ")}`),
98-
platformAvailable: () => Effect.succeed(false),
99-
});
100-
const result = yield* operations
101-
.screenshot({
102-
id: "phone",
103-
name: "Phone",
104-
version: version ?? (platform === "ios" ? "iOS 26.2" : "Android 15"),
105-
platform,
106-
booted: true,
107-
physical: false,
108-
})
109-
.pipe(Effect.result);
110-
expect(result).toMatchObject(expected);
111-
}),
62+
run: () => Effect.succeed({ code: 0, stdout: "iVBORw==", stderr: "" }),
63+
platformAvailable: () => Effect.succeed(false),
64+
});
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({
77+
failure: { _tag: "DeviceOperationError", reason: "hub_rejected" },
78+
});
79+
expect(yield* screenshot("iOS 26.3")).toMatchObject({
80+
success: new Uint8Array([137, 80, 78, 71]),
81+
});
82+
}),
11283
);
11384

11485
const fixture = Effect.fn("fixture")(function* (

0 commit comments

Comments
 (0)