Skip to content

Commit 27f3836

Browse files
committed
fix(apple): keep the device payload parser and its exports inside their modules
The Fallow audit that CI runs on changed files was red on this branch: - Collapse the two payload shapes CoreDevice has shipped into one pair of sections before any field is read, so the parser stops spelling the nested fallback out per field and drops below the complexity threshold. Direct values still win. - Report a reported field through one helper that omits it when absent, keeping "the device said nothing" distinct from "the device said no". - Stop exporting `readIosDeviceDetails` and `nameIosDeviceReadinessObstacle`: each has one caller, in its own module, and a caller that needs a verdict reads `readIosDeviceReadiness` or the preflight that publishes the reason. - Move the build-fixture envelope assertions out of the test body into named helpers so the recorded shapes are asserted through the same path.
1 parent b2b8694 commit 27f3836

3 files changed

Lines changed: 120 additions & 79 deletions

File tree

‎packages/platform-apple/src/core/physical-device-coredevice.ts‎

Lines changed: 75 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export async function resolveCoreDeviceTunnelIp(
150150
* that spells the command out, and an unreadable device stays unreadable instead of becoming an
151151
* assumption about what is wrong with it (#2683).
152152
*/
153-
export async function readIosDeviceDetails(
153+
async function readIosDeviceDetails(
154154
device: DeviceInfo,
155155
timeoutBudgetMs: number,
156156
signal?: AbortSignal,
@@ -239,67 +239,88 @@ export type IosDeviceDetails = {
239239
bootState?: string;
240240
};
241241

242-
export function parseIosDeviceDetailsPayload(payload: unknown): IosDeviceDetails {
243-
const result = (payload as { result?: unknown } | null | undefined)?.result;
244-
if (!result || typeof result !== 'object') return {};
245-
const direct = (
246-
result as {
247-
connectionProperties?: { tunnelState?: unknown; tunnelIPAddress?: unknown };
248-
}
249-
).connectionProperties;
250-
const nested = (
251-
result as {
252-
device?: { connectionProperties?: { tunnelState?: unknown; tunnelIPAddress?: unknown } };
253-
}
254-
).device?.connectionProperties;
255-
const tunnelState =
256-
readNonEmptyString(direct?.tunnelState) ?? readNonEmptyString(nested?.tunnelState);
257-
const tunnelIp =
258-
readNonEmptyString(direct?.tunnelIPAddress) ?? readNonEmptyString(nested?.tunnelIPAddress);
259-
const deviceProperties = readDeviceProperties(result);
260-
const developerModeStatus =
261-
readNonEmptyString(deviceProperties?.developerModeStatus) ??
262-
readNonEmptyString(nestedDevice(result)?.deviceProperties?.developerModeStatus);
263-
const developerDiskImageServicesAvailable =
264-
readBoolean(deviceProperties?.ddiServicesAvailable) ??
265-
readBoolean(nestedDevice(result)?.deviceProperties?.ddiServicesAvailable);
266-
const bootState =
267-
readNonEmptyString(deviceProperties?.bootState) ??
268-
readNonEmptyString(nestedDevice(result)?.deviceProperties?.bootState);
269-
const outcome = readNonEmptyString(
270-
(payload as { info?: { outcome?: unknown } } | null | undefined)?.info?.outcome,
271-
);
242+
/**
243+
* What one payload reports, whichever of the two shapes CoreDevice used. Fields arrive either on
244+
* `result` or nested under `result.device`, and that is a fact about the payload rather than about
245+
* any field, so the shapes are collapsed into one pair of sections before anything is read: a parser
246+
* that prefers the direct value per field has to spell the fallback out every time it grows a field,
247+
* which is how a new state ends up read from only one of the two shapes (#2683).
248+
*/
249+
/** The payload\'s two sections, after the shape difference between releases is resolved. */
250+
type ReportedSections = {
251+
connectionProperties?: Record<string, unknown>;
252+
deviceProperties?: Record<string, unknown>;
253+
};
254+
255+
function readReportedSections(result: object): ReportedSections {
256+
const source = result as {
257+
device?: unknown;
258+
connectionProperties?: unknown;
259+
deviceProperties?: unknown;
260+
};
261+
const nested = asRecord(source.device);
272262
return {
273-
...(outcome ? { outcome } : {}),
274-
...(tunnelState ? { tunnelState } : {}),
275-
...(tunnelIp ? { tunnelIp } : {}),
276-
...(developerModeStatus ? { developerModeStatus } : {}),
277-
...(developerDiskImageServicesAvailable === undefined
278-
? {}
279-
: { developerDiskImageServicesAvailable }),
280-
...(bootState ? { bootState } : {}),
263+
connectionProperties: mergeReportedSections(
264+
asRecord(source.connectionProperties),
265+
asRecord(nested?.connectionProperties),
266+
),
267+
deviceProperties: mergeReportedSections(
268+
asRecord(source.deviceProperties),
269+
asRecord(nested?.deviceProperties),
270+
),
281271
};
282272
}
283273

284-
function readDeviceProperties(result: object): DeviceProperties | undefined {
285-
const properties = (result as { deviceProperties?: unknown }).deviceProperties;
286-
return properties && typeof properties === 'object'
287-
? (properties as DeviceProperties)
288-
: undefined;
274+
/** Direct wins wherever it spelled a value; the nested section fills the fields it left blank. */
275+
function mergeReportedSections(
276+
direct: Record<string, unknown> | undefined,
277+
nested: Record<string, unknown> | undefined,
278+
): Record<string, unknown> | undefined {
279+
if (!direct) return nested;
280+
if (!nested) return direct;
281+
const merged: Record<string, unknown> = { ...nested };
282+
for (const [key, value] of Object.entries(direct)) {
283+
if (value !== undefined) merged[key] = value;
284+
}
285+
return merged;
289286
}
290287

291-
function nestedDevice(result: object): { deviceProperties?: DeviceProperties } | undefined {
292-
const device = (result as { device?: unknown }).device;
293-
return device && typeof device === 'object'
294-
? (device as { deviceProperties?: DeviceProperties })
295-
: undefined;
288+
function asRecord(value: unknown): Record<string, unknown> | undefined {
289+
return value && typeof value === 'object' ? (value as Record<string, unknown>) : undefined;
296290
}
297291

298-
type DeviceProperties = {
299-
developerModeStatus?: unknown;
300-
ddiServicesAvailable?: unknown;
301-
bootState?: unknown;
302-
};
292+
export function parseIosDeviceDetailsPayload(payload: unknown): IosDeviceDetails {
293+
const result = asRecord((payload as { result?: unknown } | undefined)?.result);
294+
if (!result) return {};
295+
const { connectionProperties, deviceProperties } = readReportedSections(result);
296+
return {
297+
...withValue(
298+
'outcome',
299+
readNonEmptyString((payload as { info?: { outcome?: unknown } })?.info?.outcome),
300+
),
301+
...withValue('tunnelState', readNonEmptyString(connectionProperties?.tunnelState)),
302+
...withValue('tunnelIp', readNonEmptyString(connectionProperties?.tunnelIPAddress)),
303+
...withValue('developerModeStatus', readNonEmptyString(deviceProperties?.developerModeStatus)),
304+
...withValue(
305+
'developerDiskImageServicesAvailable',
306+
readBoolean(deviceProperties?.ddiServicesAvailable),
307+
),
308+
...withValue('bootState', readNonEmptyString(deviceProperties?.bootState)),
309+
};
310+
}
311+
312+
/**
313+
* Reports a field only when the payload carried it. An absent state has to stay absent so the reader
314+
* can tell that the device said nothing from the device saying no.
315+
*/
316+
function withValue<K extends keyof IosDeviceDetails>(
317+
key: K,
318+
value: IosDeviceDetails[K],
319+
): Pick<IosDeviceDetails, K> {
320+
return value === undefined
321+
? ({} as Pick<IosDeviceDetails, K>)
322+
: ({ [key]: value } as Pick<IosDeviceDetails, K>);
323+
}
303324

304325
function readNonEmptyString(value: unknown): string | undefined {
305326
return typeof value === 'string' && value.trim() ? value.trim() : undefined;

‎packages/platform-apple/src/runner/__tests__/runner-startup-failure-reasons.test.ts‎

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -102,33 +102,53 @@ afterEach(() => {
102102

103103
for (const fixture of buildForTestingFixtures()) {
104104
test(`a build-for-testing failure publishes ${fixture.reason} for ${fixture.id}`, async () => {
105-
const envelope = await driveBuildFailure(fixture);
106-
107-
assert.equal(envelope.code, 'COMMAND_FAILED');
108-
assert.equal(envelope.message, 'xcodebuild build-for-testing failed');
109-
assert.equal(envelope.details?.reason, fixture.reason);
110-
assert.match(String(envelope.hint), HINT_FOR_REASON[fixture.reason]);
111-
// No `logPath` was handed to `normalizeError`: the top-level value can only be the one the
112-
// build catch wrote into the error it throws.
113-
assert.equal(envelope.logPath, logPath);
114-
assert.equal(envelope.diagnosticId, DIAGNOSTIC_ID);
115-
// normalizeError hoists these out of `details`; a caller must read them at top level.
116-
assert.equal(envelope.details?.hint, undefined);
117-
assert.equal(envelope.details?.logPath, undefined);
118-
assert.equal(envelope.details?.diagnosticId, undefined);
119-
// The tool output stays reachable for a human reading the failure. It is redacted and
120-
// length-bounded on the way out, which is another reason the reason is typed: classification
121-
// happens before the truncation a caller sees. A message-only failure carries no tool output to
122-
// reach, which is exactly why the message is part of the haystack.
123-
if ((fixture.carrier ?? 'exec-details') === 'exec-details') {
124-
const nestedDetails = envelope.details?.details as Record<string, unknown> | undefined;
125-
assert.match(String(nestedDetails?.stdout), /AgentDeviceRunner/);
126-
} else {
127-
assert.equal(envelope.details?.details, undefined);
128-
}
105+
assertFailureEnvelope(await driveBuildFailure(fixture), fixture);
129106
});
130107
}
131108

109+
/**
110+
* Every startup failure reaches a caller through one envelope: the typed reason in `details`, its hint
111+
* and the log path hoisted to top level by `normalizeError`, and the tool output still reachable
112+
* underneath for a human. The envelope is asserted per fixture rather than once because the reason and
113+
* the hint have to travel together for every recorded shape, not just for one of them.
114+
*/
115+
function assertFailureEnvelope(
116+
envelope: NormalizedError,
117+
fixture: RunnerStartupFailureFixture,
118+
): void {
119+
assert.equal(envelope.code, 'COMMAND_FAILED');
120+
assert.equal(envelope.message, 'xcodebuild build-for-testing failed');
121+
assert.equal(envelope.details?.reason, fixture.reason);
122+
assert.match(String(envelope.hint), HINT_FOR_REASON[fixture.reason]);
123+
// No `logPath` was handed to `normalizeError`: the top-level value can only be the one the
124+
// build catch wrote into the error it throws.
125+
assert.equal(envelope.logPath, logPath);
126+
assert.equal(envelope.diagnosticId, DIAGNOSTIC_ID);
127+
// normalizeError hoists these out of `details`; a caller must read them at top level.
128+
assert.equal(envelope.details?.hint, undefined);
129+
assert.equal(envelope.details?.logPath, undefined);
130+
assert.equal(envelope.details?.diagnosticId, undefined);
131+
assertToolOutputReachable(envelope, fixture);
132+
}
133+
134+
/**
135+
* The tool output stays reachable for a human reading the failure, redacted and length-bounded on the
136+
* way out — one more reason the reason is typed: classification happens before the truncation a caller
137+
* sees. A message-only failure carries no tool output to reach, which is exactly why the message is
138+
* part of the haystack.
139+
*/
140+
function assertToolOutputReachable(
141+
envelope: NormalizedError,
142+
fixture: RunnerStartupFailureFixture,
143+
): void {
144+
if ((fixture.carrier ?? 'exec-details') !== 'exec-details') {
145+
assert.equal(envelope.details?.details, undefined);
146+
return;
147+
}
148+
const nestedDetails = envelope.details?.details as Record<string, unknown> | undefined;
149+
assert.match(String(nestedDetails?.stdout), /AgentDeviceRunner/);
150+
}
151+
132152
test('every startup failure reason has a recorded fixture', () => {
133153
const reasonsWithFixtures = new Set(RUNNER_STARTUP_FAILURE_FIXTURES.map((f) => f.reason));
134154

‎packages/platform-apple/src/runner/runner-device-readiness.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ type ReadableIosDeviceReadiness = Extract<IosDeviceRunnerReadiness, { available:
7474
* and is never restated as a toggle problem. That one-way direction is what #2683 adds; tool output
7575
* has always had one line covering both states and always named the wrong one first.
7676
*/
77-
export function nameIosDeviceReadinessObstacle(
77+
function nameIosDeviceReadinessObstacle(
7878
readiness: ReadableIosDeviceReadiness,
7979
):
8080
| Readonly<{ reason: RunnerDeviceReadinessFailureReason; message: string; hint: string }>

0 commit comments

Comments
 (0)