Files
hermes-agent/hermes_cli/sizefmt.py
kshitij 973c14b57c refactor: fold simplify findings — 6th copy in update_cmd, drop dead wrapper + speculative kwarg, behavior-contract tests
- Migrate the missed 6th inline formatter (update_cmd.py backup-size
  display) to the shared helper.
- checkpoints._fmt_bytes: plain alias instead of a None-guard wrapper —
  every caller feeds ints from checkpoint_manager (all size fields
  initialize to 0), so the None path was dead defensive code.
- Drop the fallback= kwarg (zero production callers; '?' default is the
  real inherited contract and stays).
- curator_backup + context_references: call format_bytes directly (single
  internal call site each, zero external importers — alias was churn
  avoidance with nothing to avoid). backup/_format_size and
  doctor/_human_bytes keep their aliases (claw.py + tests pin the former;
  three call sites use the latter).
- Reshape the loop so the trailing TB return is reachable (no dead line).
- Tests: replace alias-identity assertions (ossified the delegation
  mechanism) with behavior-contract equality over a value sweep;
  mutation-checked red-green.
- update_cmd parity: byte-identical B-GB vs the old inline loop; gains
  the TB tier.
2026-08-08 15:10:35 +05:30

37 lines
1.3 KiB
Python

"""Small shared size-formatting helpers for CLI/agent output.
Sibling of ``hermes_cli.timefmt`` (same extraction rationale: a tiny
purpose-named module lightweight consumers can import without dragging in
the CLI surface). Replaces six near-identical private byte formatters.
Two in-repo formatters intentionally do NOT delegate here:
* ``hermes_cli/session_recovery.py`` uses binary suffixes (KiB/MiB/GiB)
throughout its recovery report — a deliberate, self-consistent style.
* ``gateway/platforms/qqbot/chunked_upload.py`` renders bytes with one
decimal ("100.0 B", pinned by tests) inside a self-contained upload
protocol module.
"""
from __future__ import annotations
def format_bytes(n) -> str:
"""1234567 -> '1.2 MB' (B/KB/MB/GB/TB; integer bytes, one decimal above).
Accepts anything ``float()`` accepts; returns ``"?"`` for None or
unparseable input so display call sites never raise (contract inherited
from doctor's original copy — its stats dict tolerates None fields).
"""
try:
size = float(n)
except (TypeError, ValueError):
return "?"
if size < 1024:
return f"{int(size)} B"
for unit in ("KB", "MB", "GB"):
size /= 1024.0
if size < 1024:
return f"{size:.1f} {unit}"
return f"{size / 1024.0:.1f} TB"