Subtraction and simplicity report · NVIDIA/hyperflare-inference #618

Raw Markdown for agents: https://autobot.eidonix.com/p/fee46f9c-b607-454e-a3b4-263f9e8ab17b.md

Subtraction, Simplicity & Conventions review — PR #618

Head 3a57a70, +4,784/−399 across 23 files. I read the diffs for helios/src/core/{protection,reader,export,cache,conversion,conversion/edit,synthesis}.rs, flare/src/hub.rs, heliopause/src/export/{capture,core,weight_reuse}.rs, ignition/preparation/{aot,capture}.py, plus AGENTS.md, the existing eos/helios::os helpers and the surrounding conventions.

Verdict: the diff is mostly load-bearing. The spill/verify machinery in protection.rs, the synthesis planner, and the single-pass capture rewrite are genuinely new behaviour, not speculative generality — they cannot be expressed in "a fraction of the code" without losing the feature. What remains is five worthwhile deletions/collapses (≈−250 lines, one duplicated OS primitive, one public API surface removed) and two minor ones. I found no correctness-duplication comments here; where a buggy-looking workaround should be deleted rather than patched I say so.

Key findings

  1. ElidedBlocks is now ~70 lines of test-only code that reimplements the production block walker — delete it (heliopause/src/export/capture.rs:2617-2686).
  2. synthesis.rs re-implements posix_fadvise although eos already exports exactly that helper (helios/src/core/synthesis.rs:951-971 vs eos/src/unix.rs:67-71).
  3. The "write flare.body.<sha256> tensors" step is hand-rolled four times and its length/digest validation three times (export.rs:821-824, conversion/edit.rs:243-246, cache.rs:3661-3679, protection.rs:2907-2910).
  4. EnvelopeBuilder::with_inline_limit is a public config knob that only tests use, which AGENTS.md rejects; and sign() was left half-converted (protection.rs:1355-1361, 1381-1390).
  5. SignedEnvelope::body_store() clones every compressed body immediately before those same bytes are written to disk, doubling peak RAM in the very phase the PR claims to improve (protection.rs:357-368, export.rs:696-699).

1. ElidedBlocks is dead production code kept alive for one assertion — delete it (HIGH)

heliopause/src/export/capture.rs:2615-2686. The PR moved the whole std::io::Read adapter behind #[cfg(test)] rather than deleting it:

/// Sequential reader over the same blocks, used by tests to cross-check block streams.
#[cfg(test)]
struct ElidedBlocks<'a, R> {
    read_into: R,
    spans: &'a [weight_payload::PayloadSpan],
    span: usize,
    offset: u64,
    block: Vec<u8>,
    position: usize,
    length: usize,
}

Its only consumer is one cross-check in payload_export_tests (capture.rs:3993: let mut blocks_reader = ElidedBlocks::new(), which re-derives the byte stream a second way purely to compare against scan_group_blocks. The production walker is for_each_group_block (capture.rs:2489), and its own comment admits the duplication ("zero blocks are skipped exactly as weight_payload::for_each_owner_block does", capture.rs:2487-2488). The behaviour the test cares about is already covered far better by the new capture_group_into_cache tests (capture.rs:5171+, reads-once for cold miss, warm hit and --no-weights).

Action: delete the struct (2617-2626), its impl ... new (2628) and its impl Read (2671), and rewrite the single assertion at 3993 to check scan_group_blocks's returned PayloadId/offsets or to assert through capture_group_into_cache. Net ≈−70 lines and one fewer parallel implementation to keep in sync with for_each_group_block.

While in this area: capture.rs:2607-2611 has a check that cannot fire —

    if length != total {

length is the byte count capture_streaming_payload computed from exactly the bytes total accumulated, so this is an unreachable guard (drop it, or make it a debug_assert!).

2. synthesis.rs re-implements posix_fadvise; the crate already re-exports it (HIGH)

helios/src/core/synthesis.rs:951-971 adds two cfg'd function pairs:

#[cfg(target_os = "linux")]
fn fadvise_sequential(file: &File) {
    use std::os::fd::AsRawFd;
    unsafe {
        libc::posix_fadvise(file.as_raw_fd(), 0, 0, libc::POSIX_FADV_SEQUENTIAL);
    }
}

#[cfg(not(target_os = "linux"))]
fn fadvise_sequential(_file: &File) {}

#[cfg(target_os = "linux")]
fn fadvise_dontneed(file: &File) {
    use std::os::fd::AsRawFd;
    unsafe {
        libc::posix_fadvise(file.as_raw_fd(), 0, 0, libc::POSIX_FADV_DONTNEED);
    }
}

fadvise_dontneed is byte-for-byte flare_eos::release_page_cache (eos/src/unix.rs:67-71), which already has its Windows no-op twin (eos/src/windows.rs:150) and is reachable inside Helios as crate::os::release_page_cache (helios/src/os.rs:8: pub(crate) use flare_eos::*;). runtime_core calls it from six places (archive.rs:922 (not opened), model.rs:614 (not opened), model.rs:907 (not opened), protection.rs:504-507, payload.rs:113 (not opened), engram.rs:474 (not opened)). synthesis.rs already imports from that same module — line 32: use crate::{Result, os::FileReadAt};.

Action: delete fadvise_dontneed (962-968) and its no-op twin (970-971), and call crate::os::release_page_cache(&out) / (&src_file) at synthesis.rs:1087 and 1089. For fadvise_sequential (used once, synthesis.rs:1025), add a single advise_sequential beside release_page_cache in eos rather than a second cfg pair living in Helios — or drop it entirely if it has no measurement behind it (see open questions). Net ≈−18 lines and one home for the OS primitive instead of two.

3. The flare.body.* tensor write/verify loop is written out four times (HIGH)

The identical shape appears in every place that rebuilds a primary container:

helios/src/core/export.rs:821-824 (added by this PR, in write_primary_to_file):

    for (sha256, bytes) in &signed.external_bodies {
        let name = crate::core::reader::external_body_tensor(sha256);
        writer.add(&name, bytes.len() as u64, Source::Bytes(bytes.clone()))?;
    }

helios/src/core/conversion/edit.rs:243-246:

    for (sha256, bytes) in signed.external_bodies {
        let name = reader::external_body_tensor(&sha256);
        writer.add(&name, bytes.len() as u64, Source::Bytes(bytes))?;
    }

helios/src/core/cache.rs:3661-3679 — the same loop plus a length check and a digest check that restate cache.rs:2259-2272:

    for (sha256, stored_nbytes) in protection::external_body_refs(&envelope)? {
        let name = reader::external_body_tensor(&sha256);
        let tensor = metadata.primary().tensor(&name)?;
        if tensor.len() != stored_nbytes {
            return Err(format!(
                "external metadata body {name} has {} bytes, expected {stored_nbytes}",
                tensor.len()
            )
            .into());
        }

and again as a test fixture at helios/src/core/protection.rs:2907-2910. edit.rs:225 separately needs name.starts_with(reader::EXTERNAL_BODY_PREFIX) to skip the source's own tensors — a fifth place that has to know the naming rule.

Concrete simplification: one container::Writer::add_external_bodies(&SignedEnvelope) (or &[(sha256, bytes)]) used by export.rs, edit.rs and cache.rs, plus one read_external_bodies(&reader::Metadata) -> Vec<([u8; 32], u64)> shared by cache.rs:2259-2272 and heliopause/src/export/weight_reuse.rs:185-197. That puts "validate declared length, verify digest, derive the deterministic tensor name" in a single function instead of three (cache.rs:2262-2271, cache.rs:3664-3677, weight_reuse.rs:185-197) — which is also the right place for the correctness lens's concerns about those checks drifting apart.

4. with_inline_limit is test scaffolding exposed as public builder API, and sign() was left half-converted (HIGH — convention)

helios/src/core/protection.rs:1355-1361:

    /// Cap the inline `flare.manifest` size before spilling signed deployment
    /// and rank bodies into external SHA-256-referenced zstd tensors. Clamped
    /// to `schema::MAX_ENVELOPE`.
    pub fn with_inline_limit(mut self, limit: usize) -> Self {
        self.inline_limit = limit.min(schema::MAX_ENVELOPE);
        self
    }

Every production caller (export.rs:680, conversion/edit.rs:195) uses the default. The only callers in the tree are protection.rs:2803, protection.rs:2943 and synthesis.rs:1434, all passing 1 purely to force a spill in a test. That is a configuration switch with no concrete user requirement, which the repo explicitly rules out — AGENTS.md:188-193: "Do not add frameworks, extension points, speculative compatibility, fallback layers, or configuration switches without a concrete requirement."

Action: make it #[cfg(test)] (or pub(crate) + #[cfg(test)]) so the shipped builder surface is identical to main's; tests keep their 1.

Related half-migration: sign() is now a wrapper that fails when the envelope spilled (protection.rs:1381-1390):

        if !signed.external_bodies.is_empty() {
            return Err(...);

but helios/src/core/conversion.rs:2257 (inject_dependency_descriptors) still calls builder.sign(key)? and was never converted to sign_envelope, the way conversion/edit.rs:195 was. So the repo ships two signing entry points whose semantics now differ, and one live call site inherits the weaker one. Action: convert conversion.rs:2257 to sign_envelope and delete sign() entirely, updating the remaining test call sites (protection.rs:2048, 2256, 2305, 2362; conversion.rs:4047; conversion/tests/edit_tests.rs:64 (not opened); prepared_weights_tests.rs:155 (not opened)) — or keep sign() and make it return SignedEnvelope. Do not ship both behaviours.

5. body_store() copies every compressed body right before those bytes are written (MEDIUM-HIGH)

helios/src/core/protection.rs:357-368:

    /// Build an in-memory `BodyStore` when external body tensors are present.
    pub fn body_store(&self) -> Option<Arc<dyn BodyStore>> {
        if self.external_bodies.is_empty() {
            None
        } else {
            Some(Arc::new(MemoryBodyStore::from_entries(
                self.external_bodies.clone(),
            )))
        }
    }

export.rs:696-699 calls this immediately before write_primary_to_file streams the same external_bodies to disk (export.rs:821-824), so a spilled core holds the compressed metadata twice at exactly the peak-RAM moment the PR's own analysis is about (budget up to the 1 GiB MAX_SELECTED_RANK_METADATA). Independently, heliopause/src/export/weight_reuse.rs builds a third copy: it hex-encodes every digest for JSON (weight_reuse.rs:189-197, PayloadId::from_bytes(sha256).to_hex()), parses it back (328-336), and Source::body_store (324-338) rebuilds a MemoryBodyStore per call — called from Source::deployment (weight_reuse.rs:348), i.e. once per deployment.

Action: hold the entries as Arc<[u8]> (or give MemoryBodyStore an Arc<Vec<([u8; 32], Vec<u8>)>>) so body_store() returns a handle rather than a deep copy; in weight_reuse.rs, build the store once in open_source and keep Option<Arc<dyn BodyStore>> in Source instead of a hex-encoded digest vec. This removes the double-resident peak and an entire hex round-trip.

Minor

Checked and NOT flagged

Open questions / could not find

  1. Is POSIX_FADV_SEQUENTIAL (synthesis.rs:1025) load-bearing? No comment, test or measurement in the PR justifies it beyond DONTNEED. If it is not measured, deleting both fadvise_* pairs and calling crate::os::release_page_cache only at 1087/1089 is the whole fix; if it is, eos should own it (finding 2).
  2. Does EnvelopeBuilder::sign() have out-of-tree callers? In-tree it is one live call site (conversion.rs:2257) plus tests. If nothing external uses it, deleting it and converting conversion.rs:2257 to sign_envelope is strictly less code (finding 4); if something does, sign() needs to return SignedEnvelope instead of erroring.
  3. Is ElidedBlocks's cross-check assertion actually guarding anything capture_group_into_cache's tests do not? I could not find a distinct property it covers; if a reviewer knows of one, keep the assertion but restate it on for_each_group_block output rather than a second reader.
  4. Could body_store()'s deep copy be avoided without changing the public SignedEnvelope fields? I did not trace all consumers of protection.rs:355 pub external_bodies: Vec<([u8; 32], Vec<u8>)>; if it is public API reachable outside Helios, the Arc<[u8]> change needs a deprecation shim — but weight_reuse.rs's hex round-trip should go regardless.