Skip to content

Commit 287aa82

Browse files
committed
fix(ios): drop -Werror from runtime clang builds and cache the fold helper
Both runtime clang builds (the AX bridge and the fold helper) compiled with -Werror, so a new warning in a future Xcode SDK could break either feature on a user's machine with no way for them to fix it. Drop -Werror from both; warnings still fail this repository's own CI checks unchanged. The fold helper also recompiled apple/fold-helper/Fold.m into a fresh temp directory on every fold call. Extract the AX bridge's locked, content+toolchain-keyed build cache (packages/platform-apple/src/ snapshot-source/cache.ts) into a shared native-build-cache.ts, and build the fold helper through the same mechanism: a fold call after the first serves a cached binary instead of recompiling, and the cache key's toolchain identity (extracted from cache-identity.ts as readHostToolchainIdentity, independent of any simulator runtime) means switching DEVELOPER_DIR busts the cache instead of serving a binary built against a different SDK. Closes #2796.
1 parent 9fac266 commit 287aa82

12 files changed

Lines changed: 1103 additions & 347 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@
4444
through the refusal; the runner transport never resends it. Interactions that mutate without a
4545
leading read (`press`, a coordinate `fill`, `swipe`, `scroll`, hardware keys) keep the foreground
4646
repair and still bring a stopped app up.
47+
- Fixed (ios): runtime clang builds no longer compile with `-Werror`, so a new warning from a future
48+
Xcode SDK cannot break the AX bridge or fold on a user's machine that this repository cannot fix
49+
for them. The fold helper is now built through the same content- and toolchain-keyed build cache
50+
as the AX bridge, so a fold call after the first serves a cached binary instead of recompiling
51+
`Fold.m` on every call, and switching `DEVELOPER_DIR` busts the cache instead of serving a binary
52+
built against a different SDK. A darwin-only CI step (`.github/workflows/ios.yml`) compiles each
53+
build's production argv with `-Werror` appended whenever its sources change, so a new warning still
54+
fails CI (#2796).
4755
- Fixed (ios): a local Simulator snapshot taken through the host AX bridge once again publishes the
4856
geometric `hittable` fact, so `is hittable` and a `hittable:` selector resolve the same controls on
4957
the bridge and the XCTest runner. The snapshot capability table has declared `hittable =
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
import assert from 'node:assert/strict';
2+
import { readFile, rm, writeFile } from 'node:fs/promises';
3+
import path from 'node:path';
4+
import { describe, test } from 'vitest';
5+
import { runCmd } from '@agent-device/host-kit/command';
6+
import { mkdtempForTest } from '../__tests__/tmp-dir.ts';
7+
import { execKillTimeoutError } from '../snapshot-source/__tests__/exec-timeout-fixture.ts';
8+
import { createSnapshotSourceHost } from '../snapshot-source/host.ts';
9+
import type { SnapshotSourceHost } from '../snapshot-source/types.ts';
10+
import {
11+
buildFoldHelperCompileArgv,
12+
ensureFoldHelperBinary,
13+
FOLD_HELPER_BUILD_TIMEOUT_MS,
14+
} from './fold-helper-cache.ts';
15+
16+
function fakeFoldHelperHost(
17+
binary: () => string,
18+
xcodeVersion: () => string = () => 'Xcode 16.4\nBuild version 16F6',
19+
): SnapshotSourceHost {
20+
const real = createSnapshotSourceHost();
21+
return {
22+
...real,
23+
run: async (command, args) => {
24+
if (command === 'xcrun' && args.includes('clang')) {
25+
const outputPath = args.at(-1)!;
26+
await writeFile(outputPath, binary());
27+
return { stdout: '', stderr: '', exitCode: 0 };
28+
}
29+
const stdout =
30+
command === 'xcodebuild'
31+
? xcodeVersion()
32+
: command === 'sw_vers'
33+
? args.includes('-buildVersion')
34+
? '24G90'
35+
: '15.6'
36+
: command === 'uname'
37+
? 'arm64'
38+
: '';
39+
return { stdout, stderr: '', exitCode: 0 };
40+
},
41+
};
42+
}
43+
44+
test('a cache hit does not build, and a source or toolchain change does', async () => {
45+
const root = await mkdtempForTest('agent-device-fold-helper-cache-');
46+
const sourceRoot = path.join(root, 'source');
47+
const cacheRoot = path.join(root, 'cache');
48+
await (await import('@agent-device/host-kit/host-file')).ensureHostDirectory(sourceRoot);
49+
await writeFile(path.join(sourceRoot, 'Fold.m'), 'fold source v1');
50+
51+
let builds = 0;
52+
let xcodeVersion = 'Xcode 16.4\nBuild version 16F6';
53+
const host = fakeFoldHelperHost(
54+
() => {
55+
builds += 1;
56+
return `binary-${builds}`;
57+
},
58+
() => xcodeVersion,
59+
);
60+
61+
try {
62+
const first = await ensureFoldHelperBinary({ host, sourceRoot, cacheRoot });
63+
assert.equal(builds, 1);
64+
assert.equal(await readFile(first.path, 'utf8'), 'binary-1');
65+
66+
const hit = await ensureFoldHelperBinary({ host, sourceRoot, cacheRoot });
67+
assert.equal(hit.path, first.path);
68+
assert.equal(builds, 1);
69+
70+
await writeFile(path.join(sourceRoot, 'Fold.m'), 'fold source v2');
71+
const sourceChanged = await ensureFoldHelperBinary({ host, sourceRoot, cacheRoot });
72+
assert.notEqual(sourceChanged.path, first.path);
73+
assert.equal(builds, 2);
74+
75+
xcodeVersion = 'Xcode 16.5\nBuild version 16F5';
76+
const toolchainChanged = await ensureFoldHelperBinary({ host, sourceRoot, cacheRoot });
77+
assert.notEqual(toolchainChanged.path, sourceChanged.path);
78+
assert.equal(builds, 3);
79+
} finally {
80+
await rm(root, { recursive: true, force: true });
81+
}
82+
});
83+
84+
test('a compile exec killed at its budget reports the fold-helper build, not the exec layer', async () => {
85+
const root = await mkdtempForTest('agent-device-fold-helper-cache-stall-');
86+
const sourceRoot = path.join(root, 'source');
87+
const cacheRoot = path.join(root, 'cache');
88+
await (await import('@agent-device/host-kit/host-file')).ensureHostDirectory(sourceRoot);
89+
await writeFile(path.join(sourceRoot, 'Fold.m'), 'fold source');
90+
const okHost = fakeFoldHelperHost(() => 'binary');
91+
const host: SnapshotSourceHost = {
92+
...okHost,
93+
run: async (command, args, options) => {
94+
if (command === 'xcrun' && args.includes('clang')) throw await execKillTimeoutError();
95+
return await okHost.run(command, args, options);
96+
},
97+
};
98+
99+
try {
100+
await assert.rejects(
101+
ensureFoldHelperBinary({ host, sourceRoot, cacheRoot }),
102+
(error: unknown) => {
103+
assert.ok(error instanceof Error);
104+
assert.equal((error as { code?: string }).code, 'COMMAND_FAILED');
105+
assert.equal(
106+
(error as { details?: { reason?: string } }).details?.reason,
107+
'fold-helper-build-failed',
108+
);
109+
assert.equal(
110+
(error as { details?: { cause?: string } }).details?.cause,
111+
'native-build-stalled',
112+
);
113+
return true;
114+
},
115+
);
116+
} finally {
117+
await rm(root, { recursive: true, force: true });
118+
}
119+
});
120+
121+
test('a build failure reports COMMAND_FAILED with the fold-helper-build-failed reason', async () => {
122+
const root = await mkdtempForTest('agent-device-fold-helper-cache-failure-');
123+
const sourceRoot = path.join(root, 'source');
124+
const cacheRoot = path.join(root, 'cache');
125+
await (await import('@agent-device/host-kit/host-file')).ensureHostDirectory(sourceRoot);
126+
await writeFile(path.join(sourceRoot, 'Fold.m'), 'fold source');
127+
const okHost = fakeFoldHelperHost(() => 'binary');
128+
const host: SnapshotSourceHost = {
129+
...okHost,
130+
run: async (command, args, options) => {
131+
if (command === 'xcrun' && args.includes('clang')) {
132+
return { stdout: '', stderr: 'clang: error: build failed', exitCode: 1 };
133+
}
134+
return await okHost.run(command, args, options);
135+
},
136+
};
137+
138+
try {
139+
await assert.rejects(
140+
ensureFoldHelperBinary({ host, sourceRoot, cacheRoot }),
141+
(error: unknown) => {
142+
assert.ok(error instanceof Error);
143+
assert.equal((error as { code?: string }).code, 'COMMAND_FAILED');
144+
assert.equal(
145+
(error as { details?: { reason?: string } }).details?.reason,
146+
'fold-helper-build-failed',
147+
);
148+
return true;
149+
},
150+
);
151+
} finally {
152+
await rm(root, { recursive: true, force: true });
153+
}
154+
});
155+
156+
// #2796: the production compile drops -Werror so a stale toolchain warning cannot fail a build;
157+
// this is the gate that keeps a new Fold.m warning from passing CI unnoticed. It runs the
158+
// production argv (`buildFoldHelperCompileArgv`) against the real iphonesimulator SDK with
159+
// -Werror appended, so a warning fails here instead of nowhere.
160+
describe.skipIf(process.platform !== 'darwin')('fold helper warning gate', () => {
161+
test(
162+
'the production fold helper argv compiles clean under -Werror',
163+
async () => {
164+
const sourceRoot = path.resolve(import.meta.dirname, '../../../../apple/fold-helper');
165+
const binary = path.join(await mkdtempForTest('fold-helper-werror-'), 'fold-helper');
166+
const argv = buildFoldHelperCompileArgv({ sourceRoot, outputPath: binary });
167+
const wextraIndex = argv.indexOf('-Wextra');
168+
assert.ok(wextraIndex >= 0, 'the production argv carries -Wextra');
169+
const werrorArgv = [
170+
...argv.slice(0, wextraIndex + 1),
171+
'-Werror',
172+
...argv.slice(wextraIndex + 1),
173+
];
174+
const compiled = await runCmd('xcrun', werrorArgv, {
175+
allowFailure: true,
176+
timeoutMs: FOLD_HELPER_BUILD_TIMEOUT_MS,
177+
});
178+
assert.equal(compiled.exitCode, 0, compiled.stderr);
179+
},
180+
FOLD_HELPER_BUILD_TIMEOUT_MS + 30_000,
181+
);
182+
});
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import path from 'node:path';
2+
import { AppError } from '@agent-device/kernel/errors';
3+
import { execFailureDetails } from '@agent-device/host-kit/command';
4+
import { runAppleToolCommand } from '../core/tool-provider.ts';
5+
import { COLD_TOOLCHAIN_PROBE_TIMEOUT_MS } from '../runner/apple-runner-platform.ts';
6+
import { readHostToolchainIdentity } from '../snapshot-source/cache-identity.ts';
7+
import {
8+
createSnapshotSourceDeadline,
9+
type SnapshotSourceDeadline,
10+
} from '../snapshot-source/deadline.ts';
11+
import { SnapshotSourceError } from '../snapshot-source/errors.ts';
12+
import { createSnapshotSourceHost } from '../snapshot-source/host.ts';
13+
import {
14+
ensureNativeBuildCacheEntry,
15+
execNativeBuildClang,
16+
fingerprintNativeBuildSource,
17+
nativeBuildCacheKey,
18+
nativeBuildManifestFieldsMatch,
19+
} from '../snapshot-source/native-build-cache.ts';
20+
import type { SnapshotSourceHost } from '../snapshot-source/types.ts';
21+
22+
const FOLD_HELPER_SOURCE_FILENAME = 'Fold.m';
23+
const FOLD_HELPER_BINARY_FILENAME = 'fold-helper';
24+
const FOLD_HELPER_SCHEMA_VERSION = 1 as const;
25+
const MANIFEST_FIELDS = ['schemaVersion', 'sourceHash', 'cacheKey', 'toolchain'] as const;
26+
27+
/** Upper bound on a single fold-helper clang invocation; the same budget the prior per-call build used. */
28+
export const FOLD_HELPER_BUILD_TIMEOUT_MS = 30_000;
29+
30+
/** Ceiling on locating, probing and (if needed) building a cached fold-helper binary. */
31+
const FOLD_HELPER_PREPARATION_DEADLINE_MS =
32+
COLD_TOOLCHAIN_PROBE_TIMEOUT_MS + FOLD_HELPER_BUILD_TIMEOUT_MS;
33+
34+
/**
35+
* The fold helper binary for the host's active toolchain, building and caching it if needed. Shares
36+
* the snapshot bridge's content+toolchain-keyed build cache (`native-build-cache.ts`), so a fold
37+
* call after the first serves a cached binary instead of recompiling `Fold.m`, and a `DEVELOPER_DIR`
38+
* switch busts the cache instead of serving a binary built against a different SDK (#2796).
39+
*
40+
* Failures surface as `AppError('COMMAND_FAILED', ..., {reason: 'fold-helper-build-failed'})`, the
41+
* error shape `sendSimulatorFoldPose` reported before this cache existed.
42+
*/
43+
export async function ensureFoldHelperBinary(
44+
input: Readonly<{
45+
signal?: AbortSignal;
46+
host?: SnapshotSourceHost;
47+
cacheRoot?: string;
48+
sourceRoot?: string;
49+
}> = {},
50+
): Promise<Readonly<{ path: string }>> {
51+
const host = input.host ?? createFoldHelperCacheHost();
52+
const deadline = createSnapshotSourceDeadline(FOLD_HELPER_PREPARATION_DEADLINE_MS, input.signal);
53+
try {
54+
const sourceRoot = input.sourceRoot ?? path.join(host.projectRoot(), 'apple', 'fold-helper');
55+
const sourceHash = await fingerprintNativeBuildSource(
56+
host,
57+
sourceRoot,
58+
[FOLD_HELPER_SOURCE_FILENAME],
59+
deadline,
60+
);
61+
const toolchain = await readHostToolchainIdentity(host, deadline);
62+
const cacheKey = nativeBuildCacheKey({
63+
schemaVersion: FOLD_HELPER_SCHEMA_VERSION,
64+
sourceHash,
65+
toolchain,
66+
});
67+
const cacheRoot =
68+
input.cacheRoot ?? path.join(host.homeDirectory(), '.agent-device', 'fold-helper');
69+
const manifest = { schemaVersion: FOLD_HELPER_SCHEMA_VERSION, sourceHash, cacheKey, toolchain };
70+
return await ensureNativeBuildCacheEntry({
71+
host,
72+
deadline,
73+
cacheRoot,
74+
cacheKey,
75+
binaryFilename: FOLD_HELPER_BINARY_FILENAME,
76+
manifest,
77+
manifestMatches: (candidate) =>
78+
nativeBuildManifestFieldsMatch(candidate, manifest, MANIFEST_FIELDS),
79+
build: (outputPath) => compileFoldHelper(host, deadline, sourceRoot, outputPath),
80+
});
81+
} catch (error) {
82+
throw asFoldHelperCacheError(error);
83+
}
84+
}
85+
86+
function createFoldHelperCacheHost(): SnapshotSourceHost {
87+
const real = createSnapshotSourceHost();
88+
return {
89+
...real,
90+
// Routed through the Apple tool-provider scope, not `run`'s default `runCmd`, so a fold test
91+
// can fake every exec this cache makes the same way it fakes the simctl dispatch (#2796).
92+
run: (command, args, options) => runAppleToolCommand(command, args, options),
93+
};
94+
}
95+
96+
/**
97+
* The production `xcrun`/clang argv for the fold helper source, exposed so a darwin-only
98+
* conformance test can compile it with `-Werror` appended and a unit test can assert it never
99+
* carries `-Werror` on its own (#2796).
100+
*/
101+
export function buildFoldHelperCompileArgv(
102+
input: Readonly<{ sourceRoot: string; outputPath: string }>,
103+
): readonly string[] {
104+
return [
105+
'--sdk',
106+
'iphonesimulator',
107+
'clang',
108+
'-mios-simulator-version-min=15.0',
109+
'-fobjc-arc',
110+
'-Wall',
111+
'-Wextra',
112+
'-framework',
113+
'Foundation',
114+
'-framework',
115+
'IOKit',
116+
path.join(input.sourceRoot, FOLD_HELPER_SOURCE_FILENAME),
117+
'-o',
118+
input.outputPath,
119+
];
120+
}
121+
122+
async function compileFoldHelper(
123+
host: SnapshotSourceHost,
124+
deadline: SnapshotSourceDeadline,
125+
sourceRoot: string,
126+
outputPath: string,
127+
): Promise<void> {
128+
const result = await execNativeBuildClang({
129+
host,
130+
deadline,
131+
argv: buildFoldHelperCompileArgv({ sourceRoot, outputPath }),
132+
budgetMs: FOLD_HELPER_BUILD_TIMEOUT_MS,
133+
deadlineReason: 'fold-helper-build-deadline',
134+
label: 'fold helper',
135+
});
136+
if (result.exitCode !== 0 || !host.exists(outputPath)) {
137+
throw new AppError(
138+
'COMMAND_FAILED',
139+
'Unable to build the simulator fold helper',
140+
execFailureDetails(result, {
141+
reason: 'fold-helper-build-failed',
142+
hint: 'Select an Xcode with the iOS simulator SDK and foldable HID support using DEVELOPER_DIR.',
143+
}),
144+
);
145+
}
146+
}
147+
148+
/**
149+
* `AppError('COMMAND_FAILED', ..., {reason: 'fold-helper-build-failed'})` for every cache failure,
150+
* matching the error `sendSimulatorFoldPose` reported before this cache existed, except a genuine
151+
* cancellation: `compileFoldHelper` already throws that exact shape on a build failure, so it
152+
* passes through unchanged.
153+
*/
154+
function asFoldHelperCacheError(error: unknown): unknown {
155+
if (!(error instanceof SnapshotSourceError)) return error;
156+
if (error.failureKind === 'cancelled') return error;
157+
return new AppError(
158+
'COMMAND_FAILED',
159+
'Unable to build the simulator fold helper',
160+
{
161+
reason: 'fold-helper-build-failed',
162+
hint: 'Select an Xcode with the iOS simulator SDK and foldable HID support using DEVELOPER_DIR.',
163+
cause: error.failureCode,
164+
},
165+
error,
166+
);
167+
}

0 commit comments

Comments
 (0)