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

Raw Markdown for agents: https://autobot.eidonix.com/p/c504d067-1db6-4b37-9751-42423045a26a.md

#1 [high] Research summary

Subtraction, Simplicity & Conventions review — PR #618

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,weight_reuse}.rs, ignition/preparation/{aot,capture}.py, plus AGENTS.md and the existing eos/helios::os helpers and docs/design-review.md (not opened) conventions. The PR is large (+4.8k) but most of it is load-bearing. Below are the five deletions/collapses I think are worth acting on, ordered by value; two minor ones follow.

1. ElidedBlocks is now ~70 lines of test-only code that duplicates the production block walker — delete it

heliopause/src/export/capture.rs:2615-2686 retains the whole Read adapter, now behind #[cfg(test)]:

/// 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 assertion in payload_export_tests (capture.rs:3991-4003) that re-derives the byte stream a second way to compare against scan_group_blocks. for_each_group_block (capture.rs:2489-2517) already walks the same blocks with the same elision rule; the test can assert on PayloadId/offsets from scan_group_blocks plus the new capture_group_into_cache (which the PR already tests exhaustively at capture.rs:5171+). Deleting ElidedBlocks and its std::io::Read impl removes 70 lines of parallel implementation that must be kept in sync with for_each_group_block (the comment even admits the duplication: "zero blocks are skipped exactly as weight_payload::for_each_owner_block does", capture.rs:2487-2488).

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

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

#[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);
    }
}

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

flare_eos::release_page_cache (eos/src/unix.rs:67-71) is exactly this, already has the Windows no-op twin (eos/src/windows.rs:150 (not opened)), and is reachable inside Helios as crate::os::release_page_cache (helios/src/os.rs:8) — runtime_core uses it in six places (archive.rs:922 (not opened), model.rs:614 (not opened), 907, …). synthesis.rs already imports from the same module (use crate::{Result, os::FileReadAt}, line 32). Action: delete fadvise_dontneed and call crate::os::release_page_cache at synthesis.rs:1087 and 1089; if POSIX_FADV_SEQUENTIAL is genuinely needed, add one advise_sequential to eos beside the existing helper instead of a second cfg pair in Helios. Net −18 lines and one OS-primitive home instead of two.

3. The "write flare.body.* tensors" loop is written out four times

The identical 5-8 line loop appears in every place that rebuilds a primary:

edit.rs:224 separately needs name.starts_with(reader::EXTERNAL_BODY_PREFIX) to skip the source's tensors. One helper — e.g. container::Writer::add_external_bodies(&signed) plus a read_external_bodies(&Metadata) -> Vec<(...)> used by cache/weight-reuse — collapses four copies into one and puts the "validate length, verify digest, name deterministically" rule in a single place (it is currently restated in cache.rs:2262-2271, cache.rs:3664-3677, weight_reuse.rs:185-197).

4. with_inline_limit is a public config knob that only tests use

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 non-test caller (export.rs:680, conversion/edit.rs:195) uses the default; the only callers are protection.rs:2803, 2943 and synthesis.rs:1434, all passing 1 to force a spill. That is test scaffolding exposed as public builder API, which AGENTS.md:188-193 rejects ("Do not add frameworks, extension points, speculative compatibility, fallback layers, or configuration switches without a concrete requirement"). Make it #[cfg(test)] (or pub(crate) + cfg(test)) so the shipped builder surface is unchanged from main. Related: sign() (protection.rs:1381-1390) is now a wrapper that fails when the envelope spilled — it is only kept so older callers still compile (conversion.rs:2257, three test modules). conversion.rs:2257 in inject_dependency_descriptors has not been converted to sign_envelope, so it will now error on a spilled core; either convert that call site to sign_envelope (as edit.rs:195 already does) or keep sign() but return SignedEnvelope — don't ship both behaviours.

5. SignedEnvelope::body_store() clones every compressed body just before writing them

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 writes the same external_bodies, so a spilled core holds the compressed bodies twice (up to the 1 GiB selected-rank budget) at exactly the peak-RAM moment the PR claims to improve; heliopause/src/export/weight_reuse.rs:324-338 then rebuilds a fresh MemoryBodyStore — cloning all entries again — inside Source::deployment, i.e. once per deployment. Storing entries as Arc<[u8]> (or MemoryBodyStore holding Arc<Vec<([u8;32], Vec<u8>)>>) and building the store once in open_source removes both duplications; weight_reuse.rs can hold Option<Arc<dyn BodyStore>> instead of re-encoding sha256 to hex and re-parsing it (weight_reuse.rs:189-197, 328-336).

Minor

No convention violations found beyond item 4: the new module respects the model-agnostic rule (synthesis.rs:6-16 matches views to layouts and declared sources, no model branches), the new test names are descriptive, and both design docs are updated in the same PR.

#2 [high] PR #618 Subtraction/Simplicity lens — all five findings with citations

LENS: Subtraction, Simplicity & Conventions for PR #618 (head 3a57a70, +4784/-399, 23 files). Conclusion: mostly load-bearing diff, 5 worthwhile deletions/collapses + 2 minor. Nothing about correctness duplication (Correctness lens owns that).

=== 1. ElidedBlocks is now ~70 lines of test-only code duplicating for_each_group_block — delete it (HIGH) === File: heliopause/src/export/capture.rs:2615-2686. The PR added #[cfg(test)] to both the struct and its impls but kept the whole Read adapter: /// 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, position: usize, length: usize, } Only consumer: capture.rs:3980-4003 in payload_export_tests (re-derives the stream to cross-check scan_group_blocks). Production walker is for_each_group_block at capture.rs:2489-2517, whose doc comment at 2487-2488 already admits the duplication: "zero blocks are skipped exactly as weight_payload::for_each_owner_block does". capture_group_into_cache already has exhaustive tests at capture.rs:5171+ (reads-once for miss/hit/no-weights). Action: delete ElidedBlocks + its Read impl (2615-2686) and rewrite that one assertion on scan_group_blocks PayloadId/offsets, or on capture_group_into_cache.

=== 2. synthesis.rs re-implements posix_fadvise; Helios already re-exports eos's helper (HIGH) === File: helios/src/core/synthesis.rs:951-971, two cfg'd pairs: #[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); } }

#[cfg(not(target_os = "linux"))] fn fadvise_dontneed(_file: &File) {} Existing helper: eos/src/unix.rs:67-71 pub fn release_page_cache(file: &File) = same syscall; Windows no-op twin at eos/src/windows.rs:150. Reachable in Helios via pub(crate) use flare_eos::*; at helios/src/os.rs:8, i.e. crate::os::release_page_cache. Used by runtime_core in six places (archive.rs:922, model.rs:614, model.rs:907, protection.rs:504-507, payload.rs:113, engram.rs:474). synthesis.rs already imports from that module: line 32 use crate::{Result, os::FileReadAt};. Action: delete fadvise_dontneed (958-971) and call crate::os::release_page_cache at synthesis.rs:1087 and 1089; if POSIX_FADV_SEQUENTIAL (951-957) is really needed, add ONE advise_sequential to eos beside the existing helper rather than a second cfg pair in Helios. Net -18 lines, one home for the OS primitive.

=== 3. The "write flare.body.* tensors" loop is written out four times (HIGH) ===

=== 4. with_inline_limit is a public config knob only tests use; sign() left behind half-converted (HIGH) === File: 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 } Production callers export.rs:680 and conversion/edit.rs:195 never call it; only callers are protection.rs:2803, protection.rs:2943, synthesis.rs:1434, all passing 1 to force a spill. AGENTS.md:188-193 rejects "configuration switches ... without a concrete requirement". Action: make it #[cfg(test)] / pub(crate)+cfg(test) so the shipped builder surface matches main. Related: sign() at protection.rs:1381-1390 is now a wrapper that errors when the envelope spilled ("core envelope exceeds its inline limit and requires external body tensors"). conversion.rs:2257 (inject_dependency_descriptors) still calls builder.sign(key)? and was NOT converted to sign_envelope (edit.rs:195 was), so that path now errors on a spilled core. Either convert conversion.rs:2257 to sign_envelope or delete sign() entirely and update the remaining test call sites (protection.rs:2048,2256,2305,2362; conversion.rs:4047; conversion/tests/edit_tests.rs:64; prepared_weights_tests.rs:155). Do not ship both behaviours.

=== 5. SignedEnvelope::body_store() clones every compressed body right before writing it (MEDIUM-HIGH) === File: 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> { if self.external_bodies.is_empty() { None } else { Some(Arc::new(MemoryBodyStore::from_entries( self.external_bodies.clone(), ))) } } export.rs:696-699 calls it immediately before write_primary_to_file writes the same external_bodies, so a spilled core holds the compressed bodies twice at the exact peak-RAM moment the PR claims to improve (budget up to 1 GiB selected-rank metadata). Separately heliopause/src/export/weight_reuse.rs:324-338 (Source::body_store) rebuilds a MemoryBodyStore by cloning every entry, and is called from Source::deployment (weight_reuse.rs:348) i.e. once per deployment; it also round-trips through hex: weight_reuse.rs:189-197 encodes PayloadId::from_bytes(sha256).to_hex(), 328-336 parses it back with PayloadId::parse. Action: store entries as Arc/Vec shared (or MemoryBodyStore holding Arc<Vec<...>>), build the store once in open_source and keep Option<Arc<dyn BodyStore>> in Source instead of the hex-encoded external_bodies vec.

=== MINOR A. Triple validation per body === protection.rs:501-512 resolve_body starts with let _ = declared_body_nbytes(inline, external, what)?; and throws the result away, after signed_deployment_entry (protection.rs:1171-1185) / signed_rank_entry (1198-1205) already validated the same fields, and before decompress_external_body (protection.rs:456) validates them a third time. Replace with one validate_external_body call returning (digest, stored, decoded) threaded through.

=== MINOR B. Redundant enum + unreachable check === synthesis.rs:944-949 ExecuteOutcome{Synthesized, AlreadyPresent, Mismatch, Cancelled}: Synthesized and AlreadyPresent are handled identically at both call sites (synthesis.rs:1155, 1212-1213), and Mismatch/Cancelled/Err(()) all mean "no object" (match at 1098-1109). Result<Option<PayloadObject>> deletes the enum, the 1098-1109 match, and the inner Result<ExecuteOutcome, ()> at 995. Also capture.rs:2607-2611 if length != total cannot fire: length is the byte count capture_streaming_payload computed from exactly the bytes total counted.

=== Things checked and NOT flagged ===