Skip to content

refactor(ape): delegate fbuild loader to running-process - #1638

Closed
zackees wants to merge 11 commits into
feat/ape-host-toolsfrom
feat/ape-core-delegation
Closed

zackees wants to merge 11 commits into
feat/ape-host-toolsfrom
feat/ape-core-delegation

Conversation

@zackees

@zackees zackees commented Oct 3, 2026 •

Copy link
Copy Markdown
Member

fbuild currently owns an APE loader and gives embedded zccache a shell shim. Delegate detection and launch planning to running-process 4.10.16, and pin zccache to the validated commit from forge-ci/zccache#1887 (merged), which uses kernal-api 0.1.26. Embedded compiles now receive the original compiler path.

The adapter preserves FBUILD_APE_LOADER, FBUILD_APE_CACHE_DIR, FBCACHE_DIR, FBUILD_CACHE_DIR and the registered application cache, including fallback order. Spawn helpers share the canonical fork lock without recursively acquiring it. Package lookup, executable permission repair and owner-thread containment from #1633 remain intact.

This is stacked on #1633 so its newer work remains in the ancestry and this PR shows only the migration.

Validation: workspace tests, workspace clippy with warnings denied, all workspace Dylints, platform-boundary checks and the single-reviewer clud-review passed. All nine adapter tests passed, including hostile environments, registered-cache fallback and 48 concurrent first launches. Both real cosmocc compiler integration tests passed with the final dependency pin, including embedded zccache cold miss/warm hit.

The six unreferenced old loader files have now been removed safely in an isolated local worktree (0841f49a); the cleanup has passed review and all nine adapter tests. These resumed commits are not pushed yet. Final validation and ci-full remain required for this platform-sensitive migration.

The first full CI run on 562a07fee23c788c06f8d1cf4edcce753460262c passed every job except Windows: https://github.com/FastLED/fbuild/actions/runs/37146132601. It exposed relative executable resolution against the parent cwd in the hostile-environment fixture.

Commit 6ff4f9074b441d3d7d922b126213a841119ffb86 resolves explicit relative paths against the requested child cwd, preserving OS symlink traversal and bare-name search. All 330 fbuild-core tests pass locally, including the unchanged hostile fixture and a real symlink regression; clippy, targeted Dylint and review pass. Windows validation on the corrected candidate: https://github.com/FastLED/fbuild/actions/runs/37148075175.

Resumed integration checkpoint (2026-10-22; local commits pending push)

The parent #1633 conflicts with current main and its old local gate failed. The final migration branch now integrates current main, retaining the parent's universal package/tool discovery and executable-permission support together with the running-process delegation. Once final review and gates pass, this draft can target main with the complete migration; #1633 can be superseded after that lands.

  • Removed six unreachable legacy loader implementations (697 lines), preserving the public adapter and its tests.
  • Resolved main's esptool test-file move while retaining find_binary_accepts_a_nested_ape_com in the current test module.
  • Regenerated and checked the enforced platform-boundary inventory after removing the old implementation.
  • All ten APE adapter tests pass on the integrated-main candidate (latest run: 1.12 seconds).
  • Updated the local verifier to qualified workflow identities with the merged shared verifier pin; existing step requirements remain.
  • Full combined-diff review and the moved esptool regression pass.
  • A new RED regression reproduced the embedded compiler losing the registered cache fallback when the preferred cache path was unusable. The fix resolves the loader through the complete fallback chain before translating it into the embedded compiler environment.
  • Both real cosmocc 4.0.2 integration tests pass. The embedded-only test also passes independently, proving a cold miss followed by a warm hit with a hostile PATH and an unusable preferred loader-cache path. On loader hosts it inspects the actual executable loader produced in the registered cache, without invoking the planner during verification.
  • Added the reviewed complete-history checkout fix required by the source-bound gate verifier.
  • Current local checkpoint: 2c9b6353. The mandatory full gate is queued behind the existing fbuild clone gate. These resumed commits are not pushed; no final-head hosted or native-platform success is claimed.

zackees added 10 commits October 2, 2026 18:15
fbuild host tools are moving to cosmocc Actually Portable Executables: one
binary per tool for Windows, macOS and Linux on x86_64 and aarch64. Unix
kernels can't exec an APE without a binfmt handler (NixOS ships none), so
every spawn failed with "Exec format error".

- platform::ape detects the APE magic and launches `<loader> <image> args`.
  fbuild provides the loader from the image itself: the Linux ELF loader,
  the macOS x86_64 Mach-O loader, or ape-m1.c compiled once with cc on Apple
  Silicon. Loaders are validated and installed content-addressed into an
  owner-only, exec-capable cache, with a sealed memfd as the last resort on
  Linux (raw syscall, so the glibc 2.17 floor holds).
- The loader is also exposed as `ape` first on the child's PATH, so APE
  programs a tool spawns itself (gcc -> cc1/as) resolve too.
- A process-wide fork lock keeps freshly written loaders from failing
  with ETXTBSY under parallel spawns.
- zccache spawns compilers itself, so FbuildZccacheService hands it a
  `#!/bin/sh` shim that keeps the compiler's name and execs the loader on
  the original image.
- Tool lookup accepts tool, tool.exe and tool.com on every host (non-native
  spellings only when they are APE). Zip extraction restores the APE exec
  bit. Registry files without `system`, and tools.json "any", count as
  universal.
- Direct Command::new tool spawns move to process::command/tokio_command.

Tests: the hello-world cosmocc fixture via run_command and process::command,
hostile envs, tampered caches, symlinked and world-writable cache dirs,
48-way concurrent first spawns, and an opt-in all-APE cosmocc gcc toolchain
through run_command and embedded zccache (cold miss then warm hit).
Validated on Ubuntu, Debian, Fedora, Alpine, Arch (x64, root and user),
aarch64 under qemu-user, and Windows tool-resolution logic under wine.

Refs #1629
…inventory, keep exact-name DLL lookup on PATH

- process::command/tokio_command carry allow-direct-spawn markers (they are
  the sanctioned constructors).
- Move the new registry-system and APE staged-install tests into their own
  files to stay under the 1000-LOC limit.
- Regenerate ci/platform_boundary_research.tsv.
- esp_qemu: find_on_path is also used for libiconv-2.dll; the tool-name
  expansion (.exe/.com/APE-only extensionless) never matches a DLL, which
  broke hydrate_windows_runtime on Windows. Keep an exact-name lookup and use
  find_tool_on_path only for the qemu binary.
…/raw prefix compares

Dylint (ban_std_pathbuf, ban_file_based_locks, ban_unrooted_tempdir,
ban_raw_path_prefix_compare) rejected the new APE and tool-lookup code.

- platform::ape and executable tool helpers carry NormalizedPath; callers in
  PathBuf-allowlisted files convert with into_path_buf().
- Loader installs use create+truncate on a pid/sequence-unique temp name in an
  owner-only dir instead of create_new.
- Drop the $TMPDIR cache candidate. Binaries register fbuild's cache root via
  ape::set_default_cache_root(get_cache_root()/ape) (daemon + CLI startup);
  XDG/~/.cache, the runtime dir and the Linux memfd remain fallbacks.
- memfd detection is an exact-identity is_anonymous() check, not a
  /proc/self prefix compare.
…, ETXTBSY retry, docs

- native shim embeds the APE image's len/mtime so an in-place compiler
  upgrade changes the shim zccache fingerprints.
- process::shared_fork_guard is public; the allowlisted direct spawns
  (riscv probe, gdb, clang tools) fork under it.
- run_command* retry ETXTBSY for APE launches (third-party spawners such as
  zccache fork without fbuild's lock).
- README/platform docs describe the macOS loaders and current cache dirs.
…ly foreign addr2line; merge main

- teensy_loader_cli lookup searches the requesting CLI's PATH first
  (deploy_teensy.rs).
- Installs extracted before APE support get their APE exec bits repaired once
  (ape_perms.rs, .fbuild-ape-perms-v1 marker); fresh installs write the marker.
- derive_addr2line_path accepts a non-native sibling only when it is an APE.
- Refresh boundary inventory after merging main.
@zackees zackees added the ci-full Run the complete release-equivalent CI matrix on this PR SHA label Oct 3, 2026
@coderabbitai

coderabbitai Bot commented Oct 3, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Oct 4, 2026
@zackees
zackees force-pushed the feat/ape-host-tools branch from dfebd9d to f8ac127 Compare November 18, 2026 04:57
@zackees

zackees commented Nov 19, 2026 •

Copy link
Copy Markdown
Member Author

Status check (2026-11-19): BLOCKED, left open as a draft. Nothing was changed.

Not actively driven. The last commit on this branch is 6ff4f907 from 2026-10-03. The local continuation origin/resume/fbuild-pr1638 (7ab97c98) was last touched on 2026-10-21. The PR's 2026-11-18T04:57 "update" was a base_ref_force_pushed event from the steward of the parent branch feat/ape-host-tools, not new work here. No local worktree is on either branch.

Why it cannot simply land:

  • The parent PR feat(core): run cosmocc APE host tools on every host fbuild supports #1633 merged on 2026-11-18 as b472acb5 / f8ac127b, so the stack is broken. This PR still targets feat/ape-host-tools, and its diff now shows 80 files because it includes the parent's content.
  • main carries feat(core): run cosmocc APE host tools on every host fbuild supports #1633's own loader (crates/fbuild-core/src/platform/ape/{mod,install,native,prologue}.rs, 1421 LOC), and it does not use running-process. The delegation this PR describes is not on main, and no successor PR exists.
  • resume/fbuild-pr1638 is 733 commits behind origin/main. A trial git merge-tree against main reports 28 content conflicts in 27 files. These include crates/fbuild-core/src/platform/ape/mod.rs, subprocess.rs, platform/process.rs, fbuild-build-engine/src/zccache_embedded.rs, fbuild-packages-fetch/src/{lib,extractor}.rs, the root and per-crate Cargo.toml/Cargo.lock, and ci/platform_boundary_research.tsv. Resolving them means redoing the migration on top of the loader as feat(core): run cosmocc APE host tools on every host fbuild supports #1633 finally merged it (after several steward merges). That is design work, not a mechanical conflict fix.
  • The PR body itself says "final validation and ci-full remain required", and the resumed commits were never validated on hosted CI.

The dependency is not the blocker. running-process 4.10.16 on crates.io publishes the ape-loader feature, and the latest is 4.10.21. main already pins running-process =4.10.16, without ape-loader, and zccache =1.15.8 from crates.io. The PR's git pin of zccache would have to move to a released zccache.

What remains to land this:

  1. Start a fresh branch from origin/main, or retarget this PR to main. Then port the delegation from resume/fbuild-pr1638 onto the merged feat(core): run cosmocc APE host tools on every host fbuild supports #1633 loader: enable running-process/ape-loader, replace ape/{install,native,prologue}.rs with the adapter, and keep the FBUILD_APE_LOADER / FBUILD_APE_CACHE_DIR / FBCACHE_DIR / FBUILD_CACHE_DIR / registered-cache fallback order.
  2. Confirm that the embedded zccache release (1.15.8 or later) includes fix(ape): load Cosmopolitan compilers through kernal-api 0.1.26 forge-ci/zccache#1887, so embedded compiles get the original compiler path without a git pin.
  3. Regenerate ci/platform_boundary_research.tsv. Rerun the adapter tests and the real cosmocc integration tests. Then run full CI, including Windows. Windows failed on 562a07fe (run 37146132601) and passed on 6ff4f907 (run 37148075175), but neither the resumed branch nor any main-integrated head has hosted CI.

A maintainer needs to decide whether to finish this migration or close it in favour of the merged #1633 loader.

@zackees

zackees commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Decision (2026-11-20): port the delegation onto main as a fresh PR, then close this one as superseded. If the port cannot be fully validated (crate tests, clippy, dylint, the real cosmocc tests, hosted CI with ci-full, merge queue), it is abandoned and this PR is closed with a tracking issue instead. It will not be left half-done.

Strategies considered

  1. Fresh small PR on top of main's loader, close refactor(ape): delegate fbuild loader to running-process #1638 as superseded (chosen).
  2. Close refactor(ape): delegate fbuild loader to running-process #1638 and file a tracking issue with acceptance criteria for a later migration (fallback if 1 fails validation).
  3. Close outright because main's loader makes the migration unnecessary (rejected, see below).

Evidence on main (cee21122)

  • The shell-shim problem is still there. crates/fbuild-build-engine/src/zccache_embedded.rs:304 swaps an APE compiler for ape::native_executable(...), a generated #!/bin/sh shim (crates/fbuild-core/src/platform/ape/native.rs). Embedded zccache therefore fingerprints and runs the shim, not the compiler; the shim needs /bin/sh, and it has to carry the image's length and mtime in a comment so an in-place compiler upgrade does not reuse old cache entries.
  • The shim is no longer needed. Cargo.lock on main already resolves zccache 1.15.8 and kernal-api 0.1.26 from crates.io. That is the kernal-api version fix(ape): load Cosmopolitan compilers through kernal-api 0.1.26 forge-ci/zccache#1887 moved to, and it plans the APE loader itself for every compiler it spawns. No git pin of zccache is required any more, which was the main reason this PR's manifests conflict.
  • Main compiles two APE loaders. kernal-api 0.1.26 enables running-process/ape-loader, so through feature unification the running-process loader (running-process-platform-internal/src/platform/ape.rs, 966 LOC plus per-OS files) is already linked into fbuild next to fbuild's own copy (platform/ape/{mod,install,native,prologue}.rs, 1421 LOC, plus platform/{linux,macos,windows}/ape.rs, 260 LOC). The two copies cover the same cases (Linux ELF, macOS x86_64 Mach-O, macOS arm64 ape-m1.c, memfd fallback, ape PATH directory).
  • They also keep two independent fork locks (FORK_LOCK in platform/process.rs and running-process's fork_guard). Each protects only its own loader writes against its own spawns.
  • That rules out strategy 3: main's loader does not make the delegation unnecessary, it makes it a duplicate.

Why a fresh PR and not a rebase of this branch

The 28 conflicts reported on 2026-11-19 come from merging the whole stale branch, including its old merges of main and the zccache git pin. Cherry-picking only the seven migration commits from resume/fbuild-pr1638 (6ea7f1b8, 9533dc7a, 0841f49a, 9f4b3223, 5435b4b7, c0d51a86, 7ab97c98) onto cee21122 conflicts only in Cargo.lock and six Cargo.toml files. All of those resolve to main's side; the only manifest change left is adding the ape-loader feature to the workspace running-process pin. Result: 23 files, +409 / -1740.

Risk

Process spawning on Linux, Windows and macOS, the fork lock, and containment are all touched. The real cosmocc tests are #[ignore] (they need a 440 MB toolchain) and no hosted workflow runs them, so they will be run locally on Linux x86_64. macOS and Windows get hosted ci-full only, with no real APE toolchain; that limit will be stated in the new PR.

@zackees

zackees commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Closing as superseded by #2132, which merged to main on 2026-11-21 as 141c60f7 through the merge queue.

#2132 is this migration ported onto current main: the seven migration commits from resume/fbuild-pr1638 (6ea7f1b8, 9533dc7a, 0841f49a, 9f4b3223, 5435b4b7, c0d51a86, 7ab97c98) cherry-picked with -x and original authorship, plus one lockfile and boundary-inventory refresh. The zccache git pin was dropped because main already resolves zccache 1.15.8 / kernal-api 0.1.26 from crates.io.

Validation of what landed:

  • Hosted ci-full on the PR head 22efd57b (run 44885100097) and the merge-queue run 44887974876 both passed. The APE adapter tests, including the real hello.com launches, ran green on Linux, Windows, macOS arm64 and macOS Intel.
  • Locally on Linux x86_64 (no APE binfmt_misc handler): fbuild-core and fbuild-build-engine tests, clippy -D warnings, dylint, and both real cosmocc 4.0.2 tests (ape_gcc_compiles_through_run_command_in_hostile_env, ape_gcc_compiles_through_embedded_zccache_and_hits_cache).
  • Not verified: a real cosmocc compile on macOS or Windows (no hosted lane runs those #[ignore] tests), and a local daemon end-to-end build (another agent's dev daemon owned the cache root for the whole session).

Branches:

  • feat/ape-core-delegation (tip 6ff4f9074b441d3d7d922b126213a841119ffb86) is deleted. Its head stays reachable as refs/pull/1638/head.
  • resume/fbuild-pr1638 (tip 7ab97c9861ee7790cc4435735598178babe572cb) is left in place. Besides the landed migration commits it carries two CI commits that were not part of this port and have not been checked against main: 77bea165 (verify qualified Bosn workflow identities) and 2c9b6353 (fetch PR base history for source-bound gate verification). Delete it once someone confirms those are either landed or unwanted.

@zackees zackees closed this Nov 21, 2026
@zackees
zackees deleted the feat/ape-core-delegation branch November 21, 2026 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-full Run the complete release-equivalent CI matrix on this PR SHA

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

1 participant