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>
111 lines
4.9 KiB
TypeScript
111 lines
4.9 KiB
TypeScript
// Redirects TMPDIR to a disposable, pid-tagged directory for the duration of
|
|
// a `node --test` invocation, then removes it — the `node --test` analogue
|
|
// of scripts/vitest-tmpdir-global-setup.ts (#1595, follow-up to #1593).
|
|
//
|
|
// `node --test` has no global setup/teardown hook, so instead of a lifecycle
|
|
// callback this wraps the whole invocation as a child process: every argument
|
|
// after this script's own path is forwarded verbatim to a fresh `node`
|
|
// process (e.g. `--test test/integration/*.test.ts`), so a package.json
|
|
// script only needs this prepended to its existing `node --test ...`
|
|
// invocation:
|
|
//
|
|
// node --experimental-strip-types scripts/node-test-tmpdir.ts --test foo.test.ts
|
|
//
|
|
// os.tmpdir() reads TMPDIR on every call, so this covers every mkdtemp call
|
|
// site the child (and anything it spawns) makes — test and production code
|
|
// alike — without touching any of them, exactly like the Vitest lane.
|
|
//
|
|
// Cleanup runs from the process 'exit' event rather than only a try/finally,
|
|
// so it fires for normal completion, a thrown error, AND a signal this
|
|
// process forwards to a still-running child (Ctrl-C locally, or a CI job
|
|
// cancellation): the signal handlers below call `process.exit()`, which
|
|
// triggers 'exit' synchronously before the process actually terminates.
|
|
// Only a SIGKILL against this wrapper itself bypasses all of that; the next
|
|
// run on the host prunes what such a kill left behind (see
|
|
// pruneAbandonedRunDirectories), since both lanes share this directory's root
|
|
// and prefix.
|
|
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { runCmdBackground } from '@agent-device/host-kit/command';
|
|
import {
|
|
TEST_RUN_TMP_PREFIX,
|
|
TEST_RUN_TMP_ROOT,
|
|
pruneAbandonedRunDirectories,
|
|
reportPrunedRunDirectories,
|
|
} from './check-tmpdir-leaks-model.ts';
|
|
|
|
const forwardedArgs = process.argv.slice(2);
|
|
if (forwardedArgs.length === 0) {
|
|
throw new Error(
|
|
'Usage: node scripts/node-test-tmpdir.ts <node args...> (e.g. --test foo.test.ts)',
|
|
);
|
|
}
|
|
|
|
reportPrunedRunDirectories(pruneAbandonedRunDirectories(TEST_RUN_TMP_ROOT));
|
|
const testRunTmpDir = fs.mkdtempSync(
|
|
path.join(TEST_RUN_TMP_ROOT, `${TEST_RUN_TMP_PREFIX}${process.pid}-`),
|
|
);
|
|
|
|
const childEnv = {
|
|
...process.env,
|
|
TMPDIR: testRunTmpDir,
|
|
// Match Vitest's hermetic-env setup: integration daemons must exercise the
|
|
// real advisory-claim mechanism without scanning, locking, or pruning the
|
|
// host user's live device claims. The wrapper already owns cleanup of this
|
|
// run directory, so claim files cannot leak after the child exits.
|
|
AGENT_DEVICE_CLAIMS_DIR: path.join(testRunTmpDir, 'device-claims'),
|
|
};
|
|
|
|
// Mirrors vitest-tmpdir-global-setup.ts's own carve-out: the Swift compiler
|
|
// cache is intentionally durable across runs (rebuilding it recompiles
|
|
// AVFoundation helpers and can push integration scenarios past their
|
|
// budgets), so it must NOT follow TMPDIR into the disposable run directory
|
|
// that gets removed after every invocation. Read os.tmpdir() before the
|
|
// TMPDIR override above takes effect in the child, so this resolves the same
|
|
// real per-user temp root vitest's globalSetup uses — the two lanes share
|
|
// one cache instead of each discarding and recompiling their own.
|
|
if (!childEnv.AGENT_DEVICE_SWIFT_CACHE_DIR?.trim()) {
|
|
childEnv.AGENT_DEVICE_SWIFT_CACHE_DIR = path.join(os.tmpdir(), 'agent-device-swift-cache');
|
|
}
|
|
|
|
// This wrapper is itself a node:test file's own subprocess whenever
|
|
// node --test runs it directly (NODE_TEST_CONTEXT/NODE_TEST_WORKER_ID: set by
|
|
// node's test runner on every test-file child), and node --test treats
|
|
// seeing those vars already set as a sign the child call is a recursive
|
|
// self-invocation — it prints a warning and skips running any files
|
|
// (verified: node --test would otherwise silently exit 0 without running the
|
|
// forwarded test file, a false pass). Since this wrapper's whole point is to
|
|
// spawn a fresh `node --test`, clear them so the child always actually runs.
|
|
delete childEnv.NODE_TEST_CONTEXT;
|
|
delete childEnv.NODE_TEST_WORKER_ID;
|
|
|
|
let cleanedUp = false;
|
|
function cleanup(): void {
|
|
if (cleanedUp) return;
|
|
cleanedUp = true;
|
|
fs.rmSync(testRunTmpDir, { recursive: true, force: true });
|
|
}
|
|
// Synchronous last-resort net: fires on every exit path, including ones a
|
|
// try/finally around the await below would never reach (process.exit() calls
|
|
// from the signal handlers, an uncaught exception's default handler, ...).
|
|
process.on('exit', cleanup);
|
|
|
|
const { child, wait } = runCmdBackground(process.execPath, forwardedArgs, {
|
|
env: childEnv,
|
|
stdio: 'inherit',
|
|
captureOutput: false,
|
|
allowFailure: true,
|
|
});
|
|
|
|
for (const signal of ['SIGINT', 'SIGTERM'] as const) {
|
|
process.on(signal, () => {
|
|
child.kill(signal);
|
|
// 128 + signal number is the shell convention for a signal-terminated exit.
|
|
process.exit(signal === 'SIGINT' ? 130 : 143);
|
|
});
|
|
}
|
|
|
|
const result = await wait;
|
|
process.exitCode = result.exitCode;
|