feat(cache): streaming ifc-build-cache CLI with atomic finalize

Merged
NI
9d ago
+1830-349

Part of #9584 (slice 3 of 3: streaming CLI, atomic finalize, bounds, RSS evidence, docs).

Status (2026-11-20)

  • Slice 2 (#10356) is on main as 4504e7045. This branch is rebased onto it, so the diff is slice 3 alone: 31 files, +1828/−349.
  • Merge base ee77162e03a93591765ea574392a02ba7e66da08, head 48a4873a0175cdcde959d8389a6572d80dff2223.
  • Outstanding: the fleet-admitted paired RSS run. The harness (#10751, scripts/perf/cache-stream-ab.mjs) is on main as 0f22ecfe1. It builds both refs in throwaway worktrees, so it runs from main against this PR's refs without moving the head being measured:
    node scripts/perf/cache-stream-ab.mjs \
      --base ee77162e03a93591765ea574392a02ba7e66da08 \
      --head 48a4873a0175cdcde959d8389a6572d80dff2223 \
      --iters 5 --json cache-rss.json
    
    The run report and its verdict get attached here, and the verdict goes into the scripts/perf/README.md ledger entry.
  • CI on this head: one red lane, Viewer E2E smoke (5/8), where reopen-held-pick.e2e.spec.ts (#10218) timed out waiting for the model load. The same lane passed on d4658dc8c, and the only commit since changes rust/cache/src/stream_tests.rs; this PR touches no viewer file. It needs a maintainer re-run.
  • The RSS table below is local, diagnostic evidence. It is not the acceptance result.

Streamed output is byte-identical to the in-memory writer's, so slice 2's conformance against SPEC.md §12 (#10349) applies to the CLI's output as well.

What

  • ifc-build-cache [--no-geometry] [--omit-source-hash] [--no-compress] <input.ifc> <output.ifclite>: a thin CLI over the library. cargo install --locked --path rust/cache; cargo only, no release wiring. Prints CacheStats as JSON; exit 0 ok, 1 failure, 2 usage.
  • build_cache_to_path(source, output, options), beside the unchanged in-memory build_cache.
  • Streaming with canonical order. An incremental Grouper in chunks.rs now drives both writers, so there is one grouping implementation (first-insertion cell order; a cell reopened after the cap keeps its slot). A chunk closed by the soft cap is serialized and appended to a spool at once; open chunks flush in slot order at the end. RAM holds the 44-byte directory entries and the open cells. Mesh retention is off. No format or order change was needed.
  • Memory bound. Each open cell holds under the 8 MiB soft cap plus at most one more mesh of any size, so the open chunks are bounded by chunkCells × 8 MiB plus the largest mesh in each cell. CacheStats reports chunkCells and peakOpenChunkBytes.
  • Bounds. Every u32 offset and length goes through a checked conversion; past the limit the build fails with CacheError::TooLarge, never wraps. A chunk's forced head bytes (fixed head fields and its directory entry) are counted before the chunk is written; finish keeps the exact size check for the remaining metadata.
  • Atomic finalize. Temp file in the destination directory, fsync, rename, parent-dir fsync on unix. A build that fails with an error removes its temp file and spool (Drop), and only files it created.
  • Killed builds. A build that is killed, or that panics under the release profile's panic = "abort", leaves .<output>.<pid>.<n>.tmp / .spool behind. Those files never replace the output. A later build skips a taken staging name and moves to the next sequence (up to 1024) without touching the leftover; both names are reserved with create_new before the build starts. There is no sweep, because a sweep could delete a concurrent build's live spool. The guide documents the leftover pattern.
  • Section writer in its own module. ChunkBody, GeometryHead and SectionWriter live in rust/cache/src/geometry/section.rs, re-exported from geometry/mod.rs.

Tests

ClaimTest
Streamed bytes == in-memory bytes (and equal stats, nothing else left in the dir)tests/streamed_matches_in_memory.rs: issue_9584_committed_samples_stream_byte_identical_under_every_option, issue_9584_catalogued_fixtures_stream_byte_identical (catalogued models when fetched)
Independent of pipeline batch sizestream_tests::streamed_bytes_do_not_depend_on_the_job_chunk_size (1, 7, 1024 jobs)
CLI output == build_cache over a stale existing output, no staging file leftscripts/cache-native-conformance.mjs (CLI check in scripts/lib/cache-native-cli.mjs)
A chunk past the limit is refused before it is written, counting the head bytes it forcessection_tests::issue_9584_a_chunk_that_crosses_the_limit_is_refused_before_it_is_written, …_the_bytes_a_chunk_forces_into_the_head_are_counted_before_it_is_written
The exact size check in finish (exactly-at-limit passes, one under is refused)section_tests::issue_9584_a_head_that_crosses_the_limit_is_refused_at_finish, …_a_head_larger_than_the_smallest_is_refused_at_finish (optional CoordinateInfo fields, so only the finished head crosses the limit)
The limit counts from the section startsection_tests::the_limit_counts_from_the_section_start
A cache past the limit is refused and leaves nothingstream_tests::issue_9584_a_cache_past_the_limit_is_refused_and_leaves_nothing (injected Limits, not a 4 GiB write)
Mid-geometry failure leaves no output, temp or spool; an existing output keeps its bytesstream_tests::issue_9584_an_injected_mid_geometry_failure_leaves_no_output_temp_or_spool (fails after a chunk is already spooled), a_missing_output_directory_is_an_error_and_creates_nothing
A failure inside the commit, after the temp file is fully written, removes itstream_tests::issue_9584_a_failed_rename_removes_the_written_temp_file (a directory at the output path fails the rename)
Staging removes only files it created; a taken name is left for its ownerstream_tests::issue_9584_a_taken_staging_name_is_not_removed
A leftover staging name from a killed build is skipped, not fatal, and survivesstream_tests::issue_9584_a_leftover_staging_name_is_skipped_not_fatal
The site rotation is restored identically when streaming (on_frame)tests/streamed_matches_in_memory.rs on rotated_site_90.ifc and issue_8190_slab_12381.ifc

RSS: local evidence, not acceptance

Apple M2 Pro, 10 cores, 16 GB, macOS, shared host at load 4–6; release; /usr/bin/time -l; 2 interleaved runs each. Base = slice 2 before the rebase (5093d52f3) in-memory, not the merge base ee77162e0; the fleet run measures against the latter. Peak RSS in MiB. Floor = geometry pass alone with meshes dropped (examples/pipeline_floor.rs); data model = --no-geometry.

Model (source MB)MeshesFloorData modelBase in-memBranch in-memStreamed
AC20-FZK-Haus (2.5)2852014272728
ISSUE_129 (12)1,40210684158153159
dental_clinic (13)5,92787120177174170
FM_ARC_DigitalHub (14)4,05810077134150149
01_BIMcollab_Example_ARC (18)7,84610693167163162
advanced_model (35)11,472165230463475401
schependomlaan (49)6,062128245322246242
ISSUE_068 (56)16,777277377646628505
Holter_Tower_10 (177)110,6335579071,4921,4701,098
  • Output bytes identical across base in-memory, branch in-memory and streamed on all 9 models, every run.
  • Retention is gone: streamed peak stays at or below data model + floor (8 of 9; 6 MiB over on advanced_model). In-memory exceeds it by the retained mesh set (~370 MiB on Holter).
  • Not flat in absolute terms: the data-model phase dominates large models (907 MiB alone on Holter). Running geometry before the data model did not help (one Holter run, 1,110 MiB). No wall-time claim: moves were within noise on this host.
  • The scripts/perf/README.md ledger entry is headed "Measured locally, acceptance pending" and carries the same caveat about the base column. The verdict is recorded there once the fleet run is attached.

Docs

docs/guide/native-cache.md (nav after "Server Guide"): install, flags mapped to BinaryCacheWriter options, guarantees (including what a killed build leaves behind), the per-cell memory bound, and what a native cache leaves out. It links packages/cache/SPEC.md (#10349, merged).

Revert oracle

Slice 2's eight surgical mutations under tests/evidence/cache-native-*-9584/ are already on main. This PR adds two, cache-native-stream-bounds-9584 (the per-chunk u32 bound) and cache-native-stream-cleanup-9584 (staging cleanup), and re-targets two, cache-native-chunks-9584 at the Grouper and cache-native-rotation-9584 at the streaming pipeline.

A hand-written mutation is admitted only for a production file the PR adds. After the rebase geometry/mod.rs is modified rather than added, which is why the section writer moved to the new geometry/section.rs: the bounds mutation targets that file.

Notes

  • New dependency memmap2 = "0.5", already in Cargo.lock via the workspace (+1 lock line).
  • examples/pipeline_floor.rs exists only to make the floor column reproducible; happy to drop it.
  • CacheError is now #[non_exhaustive] with an Io variant (crate is publish = false).
  • Not tried on Windows.

Checks

  • cargo test -p ifc-lite-cache: 50 unit, 5 self-reproducible, 2 streamed-vs-in-memory.
  • cargo clippy -p ifc-lite-cache --all-targets -- -D warnings: clean at d4658dc8c; the one commit since (48a4873a0) only changes stream_tests.rs.
  • Native cache conformance: 12/12 fixtures in CI at e8bd18eea, including AC20-FZK-Haus, the georeferenced MiniBIM model and the IFC5 bridge deck.
  • node scripts/pre-handoff.mjs: 17 gates pass, 1 skipped (retained-free viewer: no viewer build), at d4658dc8c.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added a native command-line tool for building .ifclite cache files and reporting build statistics.
    • Added a streaming cache option that writes directly to a file and produces output matching the in-memory builder.
    • Cache files are committed through atomic replacement; temporary files created during an unsuccessful build are cleaned up.
    • Build options can omit geometry or the source hash, or disable geometry compression.
    • Cache statistics now include geometry chunk-cell counts and peak open-chunk memory.
    • The command-line tool can be installed with Cargo; prebuilt binaries are not currently provided.
  • Documentation
    • Added a guide to installing and using the native cache builder, including its options, output, and limitations.