refactor(apple): give snapshot and fold one native-build owner

Merged
TH
2mo ago
+991-701

Summary

Moves the TS native-build cache, lock, toolchain-identity, and build-host code out of snapshot-source into a domain-neutral packages/platform-apple/src/native-build module group, so fold-helper-cache.ts no longer imports the snapshot bridge's deadlines, errors, host construction, or host types to compile a binary that needs none of them.

  • native-build/{cache,deadline,host,toolchain-identity,errors}.ts carry the one cache, one lock, and one toolchain-identity probe every runtime clang build in this package shares. NativeBuildHost exposes only file access, command execution, locking, and process identity — no bridge socket start/connect, no target-process inspection.
  • NativeBuildError is domain-neutral (cancelled/timeout/unsupported); snapshot-source/errors.ts and deadline.ts map it onto SnapshotSourceError at the boundary, and foldable/fold-helper-cache.ts maps it onto its own fold-helper-build-failed AppError, preserving both public error shapes byte for byte.
  • fold-helper-cache.ts drops constructing a full SnapshotSourceHost (and overriding run) for createNativeBuildHost(runAppleToolCommand), the narrow host the compile actually needs.
  • snapshot-source/host.ts drops its own copy of lock acquisition for the shared one; cache-identity.ts keeps only the bridge's simulatorRuntime addition, with retry/probe/cancellation logic moved to native-build/toolchain-identity.ts along with its tests.
  • No Swift changes; cache-key inputs, hash algorithm, manifest schema, and lock semantics are unchanged — only file/module ownership moved.

Closes #2970. 14 files touched.

Validation

Tested commit: baa48fa8ba5f10cff86a2aa69483bf5c399d3f0f.

pnpm check:affected --run: 277 test files / 1823 tests passed, including the Darwin conformance suites (native-runtime.test.ts, fold-helper's -Werror compile gate) that do genuine xcrun/clang compiles against the real toolchain.

Live device validation (production code path, no simulator needed — compilation targets the iphonesimulator SDK, not a booted device):

  • ensureSnapshotBridgeBinary: cache MISS build 1773ms, then cache HIT reuse 95ms, identical output path both times.
  • ensureFoldHelperBinary: cache MISS build 297ms, then cache HIT reuse 102ms, identical output path both times.
  • Cancellation contract: a pre-aborted ensureFoldHelperBinary call rejects with isRequestCanceledError(error) === true (this is the fix in this commit: a fold or toolchain-probe cancellation now keeps reason: 'request_canceled' the way it did before the native-build split).

No simulator was created: the change doesn't alter when the native artifact is built or reused (cache-key inputs, hash algorithm, manifest schema, and lock semantics are preserved unchanged — only module ownership moved), confirmed by inspection and by the unit + real-compile tests above producing byte-identical cache behavior on both paths.

No unresolved risk.