Skip to content

node: type-encode the seed-leech cap units (Bytes/Percent newtypes) to close the config→LeechCaps confusion seam #894

Closed

Description

@thiras

Follow-up from the #893 review (implements #856). Non-blocking; latent-only today.

Problem

The three seed-leech cap knobs are bare u64s carrying two different units, distinguished only by field name, and they are threaded through a second three-u64 struct and wired name-by-name:

  • ResolvedCache.{pull_ahead_bytes, max_unrecouped_leech_bytes, pull_share_ratio_percent} — crates/common/src/config/resolved.rs:218-228
  • → LeechCaps.{initial_allowance_bytes, max_unrecouped_leech_bytes, share_ratio_percent} — crates/node/src/leech_governor.rs:43-57
  • wired at crates/node/src/runtime/mod.rs:960-964

Two are byte quantities, one is a percent (scale 100, SHARE_RATIO_SCALE). A transposition (bytes↔percent) compiles, runs, and does not panic (the governor's saturating arithmetic absorbs it), silently mis-pricing the abuse caps. The allowance formula mixes a bytes field and a percent field in one expression (leech_governor.rs:138-143).

Proposal

Introduce Bytes(u64) / Percent(u64) newtypes (ideally in config-types, matching the existing OriginUrl/Hash/PinnedHashes instinct), with Percent owning SHARE_RATIO_SCALE. Thread them through CacheConfig (serde transparent), ResolvedCache, and LeechCaps so a unit transposition becomes a compile error end-to-end.

Scope / why deferred

Touches the serde config surface broadly; a Bytes wrapper ripples into byte arithmetic across leech_governor.rs and client.rs. The type-design review explicitly recommended it as its own small PR rather than bloating #893.

Filed from the #893 multi-agent review.

Activity

  1. added
    kind:feature
    rust
    prio:low
    on Jun 13, 2026
  2. added a commit that references this issue on Jun 13, 2026
  3. github-actions commented on Jun 13, 2026

    @github-actions

    🎯 Triage report

    A type-safety refactoring filed from the #893 review: the three seed-leech cap fields are bare u64s with mixed units (bytes and percent-×100), allowing a silent bytes↔percent transposition that compiles, runs without panic, and mis-prices abuse caps. The fix is to introduce Bytes(u64) / Percent(u64) newtypes in config-types and thread them through CacheConfig → ResolvedCache → LeechCaps so a transposition becomes a compile error. Scope is well-scoped to ~5 files; explicitly extracted from #893 to stay focused.

    📊 Assessment

    Dimension Value Reasoning
    Type Task Pure refactoring for type-safety; no new user-visible behaviour
    Labels unchanged (enhancement, technical, rust, low) Already accurately set; no additions warranted
    Coding agent Suitable Requirements, affected files, arithmetic formula locations, and the implementation pattern (mirror OriginUrl/Hash/PinnedHashes in config-types) are all specified. Success criterion is clear: cargo build rejects a bytes↔percent transposition.
    💡 Notes and suggestions

    Ordering note: this issue was filed against code in the open PR #893. Implement after #893 merges to avoid a rebase conflict on the same three struct fields.

    Implementation checklist:

    • Add Bytes(u64) to crates/config-types/src/lib.rs — serde transparent, derive Copy/Clone/Debug/PartialEq/Eq/PartialOrd/Ord; expose .0 or impl From/Into<u64> for arithmetic sites
    • Add Percent(u64) to same crate — same derives; move SHARE_RATIO_SCALE: u64 = 100 constant into it (e.g., Percent::SCALE) so callers can't construct a raw-scale value without importing the type
    • Update CacheConfig (wherever it lives in common) to use Bytes / Percent for the three fields; verify serde(transparent) round-trips against existing TOML fixtures
    • Update ResolvedCache fields at crates/common/src/config/resolved.rs:218-228
    • Update LeechCaps fields at crates/node/src/leech_governor.rs:43-57
    • Fix the wiring at crates/node/src/runtime/mod.rs:960-964 (the name-by-name assignment will now type-check instead of relying on field naming)
    • Fix the allowance formula at leech_governor.rs:138-143 — likely needs .0 unwraps or arithmetic impl on the newtypes; keep the saturation properties intact
    • Audit crates/node/src/client.rs for any u64 byte arithmetic on these values
    • Run cargo clippy --workspace --all-targets and cargo nextest run to confirm no regressions

    Design tip: if arithmetic across the two types is needed (e.g., comparing a Bytes field to a Percent-scaled value), make that an explicit named function on Percent (e.g., fn apply_to(self, bytes: Bytes) -> Bytes) rather than impl'ing Mul, to preserve the "this is a deliberate unit cross" intent at the call site in leech_governor.rs:138-143.

    Generated by Agentic Triage for issue #894 · ◷

    Add this agentic workflows to your repo

    To install this agentic workflow, run

    gh aw add githubnext/agentics/workflows/issue-triage.md@94ec5db57374c5b04a4b8eef8b4413f9af44d63f
    
  4. self-assigned this
    on Jun 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind:featureprio:lowrust

Type

No type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions