mirror of
https://github.com/callstack/agent-device.git
synced 2026-09-14 20:06:34 +08:00
e832325e87
* refactor: split generic host mechanics into @agent-device/host-kit (#2082 W1) The shared src/utils closure that blocked the platform-family moves lands on declared owners: generic host mechanics form a new private @agent-device/host-kit package between kernel and capture-kit, and capture-kit keeps capture, snapshot, and recording behavior, depending on host-kit for the mechanics it needs. tar-stream and yauzl move with the archive code. Every seam's exported subpaths are pinned in package-boundaries.test.ts, the layering model ranks the new zone, R13's allow-list names it, and each seam carries an exact eager-closure row. ADR-0019's substrate amendment describes the layout. Tests that mocked two of the moved modules separately became duplicate same-seam vi.mock factories, where the second silently replaced the first; those are merged, and the mocks that production code reaches past are pinned at their injection points instead. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018VngeKZH6zBuJzNBk5YzUH * refactor(host-kit): one narrow capability port per export The four technical barrels (exec/fs/values/request) grouped by category rather than by capability, so a consumer needing one mechanic evaluated unrelated ones. Each export is now a single capability over the host machine: command, process, diagnostics, retry, archive, file, request, version. A port re-exports only what a consumer of that capability uses, and every port carries its own eager-closure row. Most of the old values barrel was never host mechanics. Pure record readers, config-source values, result text, memoization, async scoping, coordinate validation, and device-scope parsing touch no process, file, or environment, so they join kernel's other primitives instead. Closures fall accordingly: capture-kit's png-worker-client from 20 to 10, png-resize from 28 to 18, session-teardown from 79 to 68, and the CLI from 386 to 380. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018VngeKZH6zBuJzNBk5YzUH * chore: drop the migration inventories and trim the touched comments Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018VngeKZH6zBuJzNBk5YzUH * docs: trim the touched host-kit and mutation-lane comments Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018VngeKZH6zBuJzNBk5YzUH * docs: keep tool directives only in the touched files Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018VngeKZH6zBuJzNBk5YzUH * docs: keep tool directives only across the touched tree Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018VngeKZH6zBuJzNBk5YzUH * fix: point the Swift parity comment at the real TS twin and test The W1 move rewrote this citation to packages/contracts/src/mobile-snapshot-semantics.ts, which does not exist: the module went to capture-kit while isTapPointInsideViewport itself went to packages/contracts/src/snapshot-visibility.ts. The TS test line was left pointing at the pre-move path. Both now resolve. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018VngeKZH6zBuJzNBk5YzUH * fix: repoint comment citations at the homes this refactor moved them to The W1 move left ~20 comment citations pointing at src/utils/*.ts and src/request/*.ts paths that no longer exist. Each now names the capability port that owns the symbol, which survives further file moves: exec -> host-kit/command host-process, owner-identity -> host-kit/process diagnostics -> host-kit/diagnostics atomic-file, process-lock -> host-kit/file retry -> host-kit/retry request progress/cancel -> host-kit/request version -> host-kit/version ttl-memo, source-value, parsing, device-isolation, keyed-lock, success-text -> kernel subpaths Comment-only; no closure, budget, or behavior change. ADR citations are left as written, being dated records of the decision rather than live references. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018VngeKZH6zBuJzNBk5YzUH --------- Co-authored-by: Claude <noreply@anthropic.com>
219 lines
7.3 KiB
TypeScript
219 lines
7.3 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { test } from 'node:test';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { runCmd, runCmdBackground } from '@agent-device/host-kit/command';
|
|
|
|
const REPOSITORY_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
const WRAPPER = path.join(REPOSITORY_ROOT, 'scripts', 'swift-toolchain-tmpdir.ts');
|
|
|
|
async function runProbe(exitCode: number): Promise<{
|
|
childTmpDir: string;
|
|
resultExitCode: number;
|
|
}> {
|
|
const evidenceRoot = fs.mkdtempSync(
|
|
path.join(os.tmpdir(), 'swift-toolchain-tmpdir-lifecycle-test-'),
|
|
);
|
|
const evidencePath = path.join(evidenceRoot, 'child-tmpdir.txt');
|
|
let childTmpDir: string | undefined;
|
|
|
|
try {
|
|
const probe = `
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
fs.writeFileSync(${JSON.stringify(evidencePath)}, process.env.TMPDIR);
|
|
const leaked = path.join(process.env.TMPDIR, 'TemporaryDirectory.probe');
|
|
fs.mkdirSync(leaked);
|
|
fs.writeFileSync(path.join(leaked, '.keep-directory'), '');
|
|
process.exit(${exitCode});
|
|
`;
|
|
const result = await runCmd(
|
|
process.execPath,
|
|
['--experimental-strip-types', WRAPPER, process.execPath, '-e', probe],
|
|
{
|
|
cwd: REPOSITORY_ROOT,
|
|
timeoutMs: 30_000,
|
|
allowFailure: true,
|
|
},
|
|
);
|
|
|
|
childTmpDir = fs.readFileSync(evidencePath, 'utf8');
|
|
assert.match(path.basename(childTmpDir), /^agent-device-swift-toolchain-/);
|
|
assert.equal(fs.existsSync(childTmpDir), false, `wrapper left behind: ${childTmpDir}`);
|
|
return { childTmpDir, resultExitCode: result.exitCode };
|
|
} finally {
|
|
if (childTmpDir) fs.rmSync(childTmpDir, { recursive: true, force: true });
|
|
fs.rmSync(evidenceRoot, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
test('the Swift toolchain wrapper removes its TMPDIR after success', async () => {
|
|
const result = await runProbe(0);
|
|
assert.equal(result.resultExitCode, 0);
|
|
});
|
|
|
|
test('the Swift toolchain wrapper cleans up and forwards a failure', async () => {
|
|
const result = await runProbe(17);
|
|
assert.equal(result.resultExitCode, 17);
|
|
});
|
|
|
|
test('the Swift toolchain wrapper keeps TMPDIR until a signaled child exits', async () => {
|
|
const evidenceRoot = fs.mkdtempSync(
|
|
path.join(os.tmpdir(), 'swift-toolchain-tmpdir-signal-test-'),
|
|
);
|
|
const readyPath = path.join(evidenceRoot, 'ready.json');
|
|
const shutdownPath = path.join(evidenceRoot, 'shutdown.json');
|
|
let childTmpDir: string | undefined;
|
|
|
|
try {
|
|
const probe = `
|
|
const fs = require('node:fs');
|
|
fs.writeFileSync(${JSON.stringify(readyPath)}, JSON.stringify({ tmpdir: process.env.TMPDIR }));
|
|
process.on('SIGTERM', () => {
|
|
setTimeout(() => {
|
|
fs.writeFileSync(
|
|
${JSON.stringify(shutdownPath)},
|
|
JSON.stringify({ tmpdirExisted: fs.existsSync(process.env.TMPDIR) }),
|
|
);
|
|
process.exit(0);
|
|
}, 200);
|
|
});
|
|
setInterval(() => {}, 1_000);
|
|
`;
|
|
const background = runCmdBackground(
|
|
process.execPath,
|
|
['--experimental-strip-types', WRAPPER, process.execPath, '-e', probe],
|
|
{
|
|
cwd: REPOSITORY_ROOT,
|
|
allowFailure: true,
|
|
},
|
|
);
|
|
|
|
await waitForFile(readyPath);
|
|
childTmpDir = (JSON.parse(fs.readFileSync(readyPath, 'utf8')) as { tmpdir: string }).tmpdir;
|
|
background.child.kill('SIGTERM');
|
|
|
|
const result = await background.wait;
|
|
assert.equal(result.exitCode, 143);
|
|
assert.equal(
|
|
(JSON.parse(fs.readFileSync(shutdownPath, 'utf8')) as { tmpdirExisted: boolean })
|
|
.tmpdirExisted,
|
|
true,
|
|
'the child must retain TMPDIR until its delayed shutdown completes',
|
|
);
|
|
assert.equal(fs.existsSync(childTmpDir), false, `wrapper left behind: ${childTmpDir}`);
|
|
} finally {
|
|
if (childTmpDir) fs.rmSync(childTmpDir, { recursive: true, force: true });
|
|
fs.rmSync(evidenceRoot, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test(
|
|
'the Swift toolchain wrapper keeps TMPDIR until signaled descendants exit',
|
|
{ skip: process.platform === 'win32' },
|
|
async () => {
|
|
const evidenceRoot = fs.mkdtempSync(
|
|
path.join(os.tmpdir(), 'swift-toolchain-tmpdir-descendant-test-'),
|
|
);
|
|
const readyPath = path.join(evidenceRoot, 'ready.json');
|
|
const shutdownPath = path.join(evidenceRoot, 'shutdown.json');
|
|
let childTmpDir: string | undefined;
|
|
let descendantPid: number | undefined;
|
|
|
|
try {
|
|
const descendantProbe = `
|
|
const fs = require('node:fs');
|
|
fs.writeFileSync(
|
|
${JSON.stringify(readyPath)},
|
|
JSON.stringify({ pid: process.pid, tmpdir: process.env.TMPDIR }),
|
|
);
|
|
process.on('SIGTERM', () => {
|
|
setTimeout(() => {
|
|
fs.writeFileSync(
|
|
${JSON.stringify(shutdownPath)},
|
|
JSON.stringify({ tmpdirExisted: fs.existsSync(process.env.TMPDIR) }),
|
|
);
|
|
process.exit(0);
|
|
}, 200);
|
|
});
|
|
setInterval(() => {}, 1_000);
|
|
`;
|
|
const directChildProbe = `
|
|
const { spawn } = require('node:child_process');
|
|
process.on('SIGTERM', () => process.exit(0));
|
|
const descendant = spawn(process.execPath, ['-e', ${JSON.stringify(descendantProbe)}], {
|
|
env: process.env,
|
|
stdio: 'ignore',
|
|
});
|
|
descendant.unref();
|
|
setInterval(() => {}, 1_000);
|
|
`;
|
|
const background = runCmdBackground(
|
|
process.execPath,
|
|
['--experimental-strip-types', WRAPPER, process.execPath, '-e', directChildProbe],
|
|
{
|
|
cwd: REPOSITORY_ROOT,
|
|
allowFailure: true,
|
|
},
|
|
);
|
|
|
|
await waitForFile(readyPath);
|
|
const ready = JSON.parse(fs.readFileSync(readyPath, 'utf8')) as {
|
|
pid: number;
|
|
tmpdir: string;
|
|
};
|
|
descendantPid = ready.pid;
|
|
childTmpDir = ready.tmpdir;
|
|
background.child.kill('SIGTERM');
|
|
|
|
const result = await background.wait;
|
|
assert.equal(result.exitCode, 143);
|
|
if (!fs.existsSync(shutdownPath)) process.kill(descendantPid, 'SIGTERM');
|
|
await waitForFile(shutdownPath);
|
|
assert.equal(
|
|
(JSON.parse(fs.readFileSync(shutdownPath, 'utf8')) as { tmpdirExisted: boolean })
|
|
.tmpdirExisted,
|
|
true,
|
|
'a delayed descendant must retain TMPDIR until its shutdown completes',
|
|
);
|
|
assert.equal(fs.existsSync(childTmpDir), false, `wrapper left behind: ${childTmpDir}`);
|
|
} finally {
|
|
if (descendantPid) {
|
|
try {
|
|
process.kill(descendantPid, 'SIGKILL');
|
|
} catch {}
|
|
}
|
|
if (childTmpDir) fs.rmSync(childTmpDir, { recursive: true, force: true });
|
|
fs.rmSync(evidenceRoot, { recursive: true, force: true });
|
|
}
|
|
},
|
|
);
|
|
|
|
test('Apple build lanes route toolchain commands through the cleanup wrapper', () => {
|
|
const manifest = JSON.parse(
|
|
fs.readFileSync(path.join(REPOSITORY_ROOT, 'package.json'), 'utf8'),
|
|
) as { scripts?: Record<string, string> };
|
|
const scripts = manifest.scripts ?? {};
|
|
assert.match(scripts['build:macos-helper'] ?? '', /swift-toolchain-tmpdir\.ts.*swift build/);
|
|
assert.match(
|
|
scripts['build:macos-helper:clean'] ?? '',
|
|
/swift-toolchain-tmpdir\.ts.*swift package/,
|
|
);
|
|
|
|
const xcodeBuildScript = fs.readFileSync(
|
|
path.join(REPOSITORY_ROOT, 'scripts', 'build-xcuitest-apple.sh'),
|
|
'utf8',
|
|
);
|
|
assert.match(xcodeBuildScript, /swift-toolchain-tmpdir\.ts xcodebuild build-for-testing/);
|
|
});
|
|
|
|
async function waitForFile(filePath: string): Promise<void> {
|
|
for (let attempt = 0; attempt < 100; attempt += 1) {
|
|
if (fs.existsSync(filePath)) return;
|
|
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
}
|
|
throw new Error(`Timed out waiting for ${filePath}`);
|
|
}
|