Skip to content

Commit 73fec06

Browse files
committed
refactor(capture-kit): resolve recording helper scripts from swift-cache
1 parent a798792 commit 73fec06

4 files changed

Lines changed: 93 additions & 70 deletions

File tree

‎packages/capture-kit/src/recording/__tests__/overlay.test.ts‎

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ vi.mock('../video.ts', () => ({
2828
waitForPlayableVideo: vi.fn(async () => {}),
2929
}));
3030

31-
import { buildRecordingScriptPathCandidates, overlayRecordingTouches } from '../overlay.ts';
31+
import { overlayRecordingTouches } from '../overlay.ts';
3232
import { AppError } from '@agent-device/kernel/errors';
3333
import { runCmd } from '@agent-device/host-kit/command';
3434

@@ -130,23 +130,3 @@ test('overlay forwards the requested high export preset', async () => {
130130
expect.arrayContaining(['--events', telemetryPath, '--quality', 'high']),
131131
);
132132
});
133-
134-
test('recording script candidates include packaged dist apple-runner source', () => {
135-
const packageRoot = path.join(tmpDir, 'package');
136-
const scriptPath = path.join(
137-
packageRoot,
138-
'dist/apple/runner/AgentDeviceRunner/RecordingScripts/recording-overlay.swift',
139-
);
140-
fs.mkdirSync(path.dirname(scriptPath), { recursive: true });
141-
fs.writeFileSync(scriptPath, 'print("overlay")\n');
142-
143-
const candidates = buildRecordingScriptPathCandidates(
144-
'recording-overlay.swift',
145-
path.join(packageRoot, 'dist/src'),
146-
packageRoot,
147-
tmpDir,
148-
);
149-
const firstExisting = candidates.find((candidate) => fs.existsSync(candidate));
150-
151-
expect(firstExisting).toBe(scriptPath);
152-
});

‎packages/capture-kit/src/recording/overlay.ts‎

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,19 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
3-
import { fileURLToPath } from 'node:url';
43
import { runCmd } from '@agent-device/host-kit/command';
54
import { AppError } from '@agent-device/kernel/errors';
6-
import { buildSwiftToolEnv, compileSwiftSourceFile } from './swift-cache.ts';
75
import { findProjectRoot } from '@agent-device/host-kit/version';
6+
import {
7+
buildSwiftToolEnv,
8+
compileSwiftSourceFile,
9+
resolveRecordingScriptPath,
10+
} from './swift-cache.ts';
811
import { waitForPlayableVideo, waitForStableFile } from './video.ts';
912
import {
1013
DEFAULT_RECORDING_EXPORT_QUALITY,
1114
type RecordingExportQuality,
1215
} from '@agent-device/contracts/recording';
1316

14-
export function buildRecordingScriptPathCandidates(
15-
scriptName: string,
16-
moduleDir: string,
17-
projectRoot: string,
18-
cwd: string,
19-
): string[] {
20-
const sourceScriptPath = `apple/runner/AgentDeviceRunner/RecordingScripts/${scriptName}`;
21-
const packagedScriptPath = `dist/${sourceScriptPath}`;
22-
return [
23-
path.resolve(moduleDir, scriptName),
24-
path.resolve(projectRoot, sourceScriptPath),
25-
path.resolve(moduleDir, `../${sourceScriptPath}`),
26-
path.resolve(moduleDir, `../../${sourceScriptPath}`),
27-
path.resolve(moduleDir, `../../../${sourceScriptPath}`),
28-
path.resolve(projectRoot, packagedScriptPath),
29-
path.resolve(cwd, sourceScriptPath),
30-
];
31-
}
32-
33-
function resolveRecordingScriptPath(scriptName: string): string {
34-
const moduleDir = path.dirname(fileURLToPath(import.meta.url));
35-
const scriptCandidates = buildRecordingScriptPathCandidates(
36-
scriptName,
37-
moduleDir,
38-
findProjectRoot(),
39-
process.cwd(),
40-
);
41-
42-
for (const candidate of scriptCandidates) {
43-
if (fs.existsSync(candidate)) {
44-
return candidate;
45-
}
46-
}
47-
48-
throw new AppError('COMMAND_FAILED', `Missing recording helper script: ${scriptName}`, {
49-
hint: 'Ensure apple/runner/AgentDeviceRunner/RecordingScripts is present in this checkout or bundled under dist/apple/runner in the package.',
50-
scriptName,
51-
searchedPaths: scriptCandidates,
52-
});
53-
}
54-
55-
let overlayScriptPath: string | undefined;
56-
let exportSupportScriptPath: string | undefined;
57-
5817
export function getRecordingOverlaySupportWarning(
5918
hostPlatform: NodeJS.Platform = process.platform,
6019
): string | undefined {
@@ -64,13 +23,19 @@ export function getRecordingOverlaySupportWarning(
6423
return 'touch overlay burn-in is only available on macOS hosts; returning raw video plus gesture telemetry';
6524
}
6625

26+
let overlayScriptPath: string | undefined;
27+
let exportSupportScriptPath: string | undefined;
28+
6729
function getOverlayScriptPath(): string {
68-
overlayScriptPath ??= resolveRecordingScriptPath('recording-overlay.swift');
30+
overlayScriptPath ??= resolveRecordingScriptPath('recording-overlay.swift', findProjectRoot());
6931
return overlayScriptPath;
7032
}
7133

7234
function getExportSupportScriptPath(): string {
73-
exportSupportScriptPath ??= resolveRecordingScriptPath('RecordingExportSupport.swift');
35+
exportSupportScriptPath ??= resolveRecordingScriptPath(
36+
'RecordingExportSupport.swift',
37+
findProjectRoot(),
38+
);
7439
return exportSupportScriptPath;
7540
}
7641

‎packages/capture-kit/src/recording/swift-cache.test.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ vi.mock(import('@agent-device/host-kit/command'), async (importOriginal) => ({
1414
}));
1515

1616
import { runCmd } from '@agent-device/host-kit/command';
17-
import { compileSwiftSourceFile, compileSwiftSourceText } from './swift-cache.ts';
17+
import {
18+
buildRecordingScriptPathCandidates,
19+
compileSwiftSourceFile,
20+
compileSwiftSourceText,
21+
} from './swift-cache.ts';
1822

1923
const mockRunCmd = vi.mocked(runCmd);
2024

@@ -246,3 +250,23 @@ async function expectConcurrentCacheReuse(compile: () => Promise<string>): Promi
246250
expect(fs.readFileSync(firstExecutable, 'utf8')).toBe('compiled once');
247251
expect(mockRunCmd).toHaveBeenCalledTimes(1);
248252
}
253+
254+
test('recording script candidates include packaged dist apple-runner source', () => {
255+
const packageRoot = path.join(tmpDir, 'package');
256+
const scriptPath = path.join(
257+
packageRoot,
258+
'dist/apple/runner/AgentDeviceRunner/RecordingScripts/recording-overlay.swift',
259+
);
260+
fs.mkdirSync(path.dirname(scriptPath), { recursive: true });
261+
fs.writeFileSync(scriptPath, 'print("overlay")\n');
262+
263+
const candidates = buildRecordingScriptPathCandidates(
264+
'recording-overlay.swift',
265+
path.join(packageRoot, 'dist/src'),
266+
packageRoot,
267+
tmpDir,
268+
);
269+
const firstExisting = candidates.find((candidate) => fs.existsSync(candidate));
270+
271+
expect(firstExisting).toBe(scriptPath);
272+
});

‎packages/capture-kit/src/recording/swift-cache.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createHash } from 'node:crypto';
22
import fs from 'node:fs';
33
import os from 'node:os';
44
import path from 'node:path';
5+
import { fileURLToPath } from 'node:url';
56
import { trimEdgeDashes } from '@agent-device/kernel/collections';
67
import { AppError } from '@agent-device/kernel/errors';
78
import { runCmd } from '@agent-device/host-kit/command';
@@ -14,6 +15,59 @@ import {
1415

1516
const SWIFT_CACHE_VERSION = '2';
1617
const LOCK_RETRY_DELAY_MS = 25;
18+
const RECORDING_SCRIPT_SUBDIRECTORY = 'apple/runner/AgentDeviceRunner/RecordingScripts';
19+
20+
/**
21+
* Where a recording helper script can live, in the order a checkout, a source build, and a
22+
* published package each answer from.
23+
*/
24+
export function buildRecordingScriptPathCandidates(
25+
scriptName: string,
26+
moduleDir: string,
27+
projectRoot: string,
28+
cwd: string,
29+
): string[] {
30+
const sourceScriptPath = `${RECORDING_SCRIPT_SUBDIRECTORY}/${scriptName}`;
31+
const packagedScriptPath = `dist/${sourceScriptPath}`;
32+
return [
33+
path.resolve(moduleDir, scriptName),
34+
path.resolve(projectRoot, sourceScriptPath),
35+
path.resolve(moduleDir, `../${sourceScriptPath}`),
36+
path.resolve(moduleDir, `../../${sourceScriptPath}`),
37+
path.resolve(moduleDir, `../../../${sourceScriptPath}`),
38+
path.resolve(projectRoot, packagedScriptPath),
39+
path.resolve(cwd, sourceScriptPath),
40+
];
41+
}
42+
43+
/**
44+
* Resolves one recording helper script: checkout, built package, or working-directory fallback.
45+
* The caller names the project root it trusts, so this module stays free of host metadata lookup.
46+
*/
47+
export function resolveRecordingScriptPath(
48+
scriptName: string,
49+
projectRoot: string,
50+
moduleDir = path.dirname(fileURLToPath(import.meta.url)),
51+
cwd = process.cwd(),
52+
): string {
53+
const scriptCandidates = buildRecordingScriptPathCandidates(
54+
scriptName,
55+
moduleDir,
56+
projectRoot,
57+
cwd,
58+
);
59+
for (const candidate of scriptCandidates) {
60+
if (fs.existsSync(candidate)) {
61+
return candidate;
62+
}
63+
}
64+
65+
throw new AppError('COMMAND_FAILED', `Missing recording helper script: ${scriptName}`, {
66+
hint: `Ensure ${RECORDING_SCRIPT_SUBDIRECTORY} is present in this checkout or bundled under dist/${RECORDING_SCRIPT_SUBDIRECTORY} in the package.`,
67+
scriptName,
68+
searchedPaths: scriptCandidates,
69+
});
70+
}
1771

1872
export function buildSwiftToolEnv(env: NodeJS.ProcessEnv = process.env): NodeJS.ProcessEnv {
1973
const root = getSwiftCacheRoot();

0 commit comments

Comments
 (0)