Skip to content

Commit f13251f

Browse files
thymikeeclaude
andcommitted
fix(apple): clean the runner derived path the runner uses
pnpm clean:xcuitest ignored AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH and cleaned the shared default directory. Resolve the override through the runner's own resolver, and clean only transient entries of that directory. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 9935ef5 commit f13251f

9 files changed

Lines changed: 96 additions & 15 deletions

File tree

‎CONTRIBUTING.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ Append `:clean` to any platform build when DerivedData may be stale, for example
5555
`pnpm build:xcuitest:macos:clean`. `pnpm build:xcuitest` remains the shared iOS-and-macOS gate for
5656
changes that affect both runners; it is not an all-platform build.
5757

58+
`pnpm clean:xcuitest[:ios|:macos|:tvos|:visionos]` cleans the runner derived-data directory and prints
59+
it. When `AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH` is set, the scripts clean only that directory, for
60+
every platform, and keep entries that are not build output. Otherwise they clean the default
61+
directory under `~/.agent-device/apple-runner/derived`.
62+
5863
Android and macOS helper builds remain separate because they require their native toolchains:
5964

6065
```bash

‎packages/platform-apple/src/core/runner-client.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export {
5151
} from '../runner/runner-cache-metadata.ts';
5252
export {
5353
isRunnerXcuitestScriptPlatform,
54+
resolveRunnerDerivedOverride,
5455
resolveRunnerScriptDevice,
5556
} from '../runner/apple-runner-platform.ts';
5657
export {

‎packages/platform-apple/src/runner-operations-facade.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export {
1919
resolveRunnerPerformanceBuildSettings,
2020
resolveRunnerSandboxBuildArgs,
2121
isRunnerXcuitestScriptPlatform,
22+
resolveRunnerDerivedOverride,
2223
resolveRunnerScriptDevice,
2324
resolveRunnerSigningBuildSettings,
2425
requireCertifiedRunnerCacheArtifacts,

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'node:path';
12
import { AppError } from '@agent-device/kernel/errors';
23
import {
34
isMacOs,
@@ -136,6 +137,16 @@ const RUNNER_PLATFORM_PROFILES: Record<RunnerApplePlatformName, RunnerPlatformPr
136137
},
137138
};
138139

140+
/**
141+
* The derived-data directory an operator pinned with `AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH`, or
142+
* `undefined` when the managed cache layout applies. One path serves every Apple platform, so the
143+
* runner, its cache retention, and the clean script all read the override here.
144+
*/
145+
export function resolveRunnerDerivedOverride(env: NodeJS.ProcessEnv): string | undefined {
146+
const override = env.AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH?.trim();
147+
return override ? path.resolve(override) : undefined;
148+
}
149+
139150
const RUNNER_XCUITEST_SCRIPT_PLATFORMS = ['ios', 'macos', 'tvos', 'visionos'] as const;
140151
export type RunnerXcuitestScriptPlatform = (typeof RUNNER_XCUITEST_SCRIPT_PLATFORMS)[number];
141152

‎packages/platform-apple/src/runner/runner-cache-metadata.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { hostEnvironment, isEnvTruthy, findProjectRoot, readVersion } from './ho
88
import {
99
resolveRunnerBuildDestinationFamily,
1010
resolveRunnerDerivedBaseName,
11+
resolveRunnerDerivedOverride,
1112
resolveRunnerPlatformName,
1213
resolveRunnerSdkName,
1314
} from './apple-runner-platform.ts';
@@ -149,11 +150,10 @@ export function resolveRunnerDerivedPath(
149150
device: DeviceInfo,
150151
metadata: RunnerXctestrunCacheMetadata,
151152
): string {
152-
const override = hostEnvironment().env.AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH?.trim();
153-
if (override) {
154-
return path.resolve(override);
155-
}
156-
return resolveRunnerKeyedDerivedPath(device, metadata);
153+
return (
154+
resolveRunnerDerivedOverride(hostEnvironment().env) ??
155+
resolveRunnerKeyedDerivedPath(device, metadata)
156+
);
157157
}
158158

159159
export function resolveRunnerKeyedDerivedPath(

‎packages/platform-apple/src/runner/runner-cache-retention.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
3+
import { resolveRunnerDerivedOverride } from './apple-runner-platform.ts';
34
import { hostEnvironment, emitDiagnostic } from './host.ts';
45
import {
56
acquireRunnerXctestrunCacheLock,
@@ -39,7 +40,7 @@ export async function evictStaleRunnerCaches(
3940
nowMs: number = Date.now(),
4041
): Promise<string[]> {
4142
const keep = resolveRunnerCacheKeepCount(env);
42-
if (keep === 0 || env.AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH?.trim()) return [];
43+
if (keep === 0 || resolveRunnerDerivedOverride(env)) return [];
4344
const base = path.dirname(currentDerived);
4445
const candidates = listCacheKeyDirectories(base)
4546
.filter((entry) => entry.derived !== currentDerived)

‎packages/platform-apple/src/runner/runner-cache.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
33
import { AppError } from '@agent-device/kernel/errors';
4+
import { resolveRunnerDerivedOverride } from './apple-runner-platform.ts';
45
import {
56
emitDiagnostic,
67
readProcessStartTime,
@@ -295,8 +296,7 @@ export function assertSafeDerivedCleanup(
295296
derivedPath: string,
296297
env: NodeJS.ProcessEnv = hostEnvironment().env,
297298
): void {
298-
const override = env.AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH?.trim();
299-
if (!override) {
299+
if (!resolveRunnerDerivedOverride(env)) {
300300
return;
301301
}
302302
if (isPathInsideProjectTmp(derivedPath)) {

‎scripts/clean-xcuitest-derived.mjs‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
import fs from 'node:fs';
33
import os from 'node:os';
44
import path from 'node:path';
5+
import { resolveRunnerDerivedOverride } from '@agent-device/platform-apple/runner/operations';
56

7+
const DERIVED_OVERRIDE = resolveRunnerDerivedOverride(process.env);
68
const DERIVED_ROOT = path.join(os.homedir(), '.agent-device', 'apple-runner', 'derived');
79
const ROOT_TRANSIENT_ENTRY_NAMES = new Set([
810
'.agent-device-runner-cache.json',
@@ -19,11 +21,12 @@ const ROOT_TRANSIENT_ENTRY_NAMES = new Set([
1921
const platforms = process.argv.slice(2);
2022
const requested = platforms.length > 0 ? platforms : ['ios', 'macos', 'tvos', 'visionos'];
2123
const supported = new Set(['ios', 'macos', 'tvos', 'visionos']);
24+
// An override pins one directory for every platform, as it does for the runner build.
2225
const DERIVED_PATHS = new Map([
23-
['ios', DERIVED_ROOT],
24-
['macos', path.join(DERIVED_ROOT, 'macos')],
25-
['tvos', path.join(DERIVED_ROOT, 'tvos')],
26-
['visionos', path.join(DERIVED_ROOT, 'visionos')],
26+
['ios', DERIVED_OVERRIDE ?? DERIVED_ROOT],
27+
['macos', DERIVED_OVERRIDE ?? path.join(DERIVED_ROOT, 'macos')],
28+
['tvos', DERIVED_OVERRIDE ?? path.join(DERIVED_ROOT, 'tvos')],
29+
['visionos', DERIVED_OVERRIDE ?? path.join(DERIVED_ROOT, 'visionos')],
2730
]);
2831
const PLATFORM_LABELS = new Map([
2932
['ios', 'iOS'],
@@ -55,7 +58,7 @@ function cleanDerivedPath(platform, targetPath) {
5558
if (!fs.existsSync(targetPath)) {
5659
return { status: 'skipped', reason: 'not-found' };
5760
}
58-
if (platform !== 'ios') {
61+
if (platform !== 'ios' && !DERIVED_OVERRIDE) {
5962
fs.rmSync(targetPath, { recursive: true, force: true });
6063
return { status: 'removed' };
6164
}
@@ -85,7 +88,8 @@ function cleanDerivedPath(platform, targetPath) {
8588

8689
function formatCleanupResult(platform, targetPath, result) {
8790
const platformLabel = resolvePlatformLabel(platform);
88-
if (platform !== 'ios') return formatWholeDerivedCleanupResult(platformLabel, targetPath, result);
91+
if (platform !== 'ios' && !DERIVED_OVERRIDE)
92+
return formatWholeDerivedCleanupResult(platformLabel, targetPath, result);
8993
return formatIosCleanupResult(platformLabel, targetPath, result);
9094
}
9195

‎test/integration/smoke-clean-xcuitest-derived.test.ts‎

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,59 @@ test('clean-xcuitest macos removes the entire platform directory when present',
8484
}
8585
});
8686

87+
for (const platform of ['ios', 'macos']) {
88+
test(`clean-xcuitest ${platform} cleans the override directory and leaves the default untouched`, () => {
89+
const homeDir = fs.mkdtempSync(
90+
path.join(os.tmpdir(), `agent-device-clean-xcuitest-override-${platform}-`),
91+
);
92+
try {
93+
const defaultRoot = path.join(homeDir, '.agent-device', 'apple-runner', 'derived');
94+
const defaultPlatformDir = path.join(defaultRoot, 'macos');
95+
fs.mkdirSync(path.join(defaultRoot, 'Build'), { recursive: true });
96+
fs.mkdirSync(defaultPlatformDir, { recursive: true });
97+
const overrideRoot = path.join(homeDir, 'worktree', '.tmp', 'ios-runner-derived');
98+
fs.mkdirSync(path.join(overrideRoot, 'Build'), { recursive: true });
99+
fs.mkdirSync(path.join(overrideRoot, 'Logs'), { recursive: true });
100+
fs.writeFileSync(path.join(overrideRoot, '.agent-device-runner-cache.json'), '{}', 'utf8');
101+
fs.mkdirSync(path.join(overrideRoot, 'user-data'), { recursive: true });
102+
103+
const result = runCleanXcuitest(homeDir, platform, {
104+
env: { AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH: ` ${overrideRoot}/ ` },
105+
});
106+
const label = platform === 'ios' ? 'iOS' : 'macOS';
107+
assert.equal(result.exitCode, 0, result.stderr);
108+
assert.equal(
109+
result.stdout.trim(),
110+
`Removed ${label} XCTest transient entries under ${overrideRoot}: .agent-device-runner-cache.json, Build, Logs; kept user-data`,
111+
);
112+
assert.deepEqual(fs.readdirSync(overrideRoot), ['user-data']);
113+
assert.equal(fs.existsSync(path.join(defaultRoot, 'Build')), true);
114+
assert.equal(fs.existsSync(defaultPlatformDir), true);
115+
} finally {
116+
fs.rmSync(homeDir, { recursive: true, force: true });
117+
}
118+
});
119+
}
120+
121+
test('clean-xcuitest ignores a blank override and keeps the default directory', () => {
122+
const homeDir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-clean-xcuitest-blank-'));
123+
try {
124+
const derivedRoot = path.join(homeDir, '.agent-device', 'apple-runner', 'derived');
125+
fs.mkdirSync(path.join(derivedRoot, 'Build'), { recursive: true });
126+
127+
const result = runCleanXcuitest(homeDir, 'ios', {
128+
env: { AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH: ' ' },
129+
});
130+
assert.equal(result.exitCode, 0, result.stderr);
131+
assert.equal(
132+
result.stdout.trim(),
133+
`Removed iOS XCTest transient entries under ${derivedRoot}: Build`,
134+
);
135+
} finally {
136+
fs.rmSync(homeDir, { recursive: true, force: true });
137+
}
138+
});
139+
87140
// Root bypasses directory permissions, so chmod cannot force the cleanup
88141
// failure this test provokes (common in containerized agent environments).
89142
const isRoot = process.getuid?.() === 0;
@@ -112,15 +165,20 @@ test(
112165
},
113166
);
114167

115-
function runCleanXcuitest(homeDir: string, ...args: Array<string | { allowFailure?: boolean }>) {
168+
function runCleanXcuitest(
169+
homeDir: string,
170+
...args: Array<string | { allowFailure?: boolean; env?: Record<string, string> }>
171+
) {
116172
const lastArg = args.at(-1);
117173
const options = typeof lastArg === 'object' ? lastArg : {};
118174
const platforms = (options === lastArg ? args.slice(0, -1) : args) as string[];
119175
return runCmdSync(process.execPath, [scriptPath, ...platforms], {
120176
cwd: repoRoot,
121177
env: {
122178
...process.env,
179+
AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH: undefined,
123180
HOME: homeDir,
181+
...options?.env,
124182
},
125183
allowFailure: options?.allowFailure,
126184
timeoutMs: 30_000,

0 commit comments

Comments
 (0)