refactor: consolidate five duplicate byte formatters into hermes_cli.sizefmt

Five modules each carried a private near-identical human-readable byte
formatter (backup._format_size, checkpoints._fmt_bytes,
doctor._human_bytes, context_references._human_bytes,
curator_backup.format_size). Three of them silently topped out at GB and
rendered a 1 TiB value as '1024.0 GB'. All five now alias one shared
format_bytes in hermes_cli/sizefmt.py (sibling of timefmt.py, same
zero-dependency rationale), keeping each module's established local name
so no caller churns.

Deliberately NOT migrated (behavior differs on purpose):
- session_recovery._format_bytes: binary suffixes (KiB/MiB/GiB)
- qqbot chunked_upload.format_size: '100.0 B' one-decimal style, pinned
  by its protocol tests

Net -33 production LOC before the new module; parity verified over a
16-value corpus against all five verbatim originals (only divergence:
the TB tier fix). Contract tests mutation-checked red-green.
This commit is contained in:
kshitij
2026-08-08 14:41:36 +05:30
parent 4af8fb2148
commit 7289898494
7 changed files with 120 additions and 48 deletions
+7 -10
View File
@@ -27,17 +27,14 @@ from datetime import datetime
from pathlib import Path
from typing import Any, Optional
from hermes_cli.sizefmt import format_bytes
def _fmt_bytes(n: int) -> str:
units = ("B", "KB", "MB", "GB", "TB")
size = float(n or 0)
for unit in units:
if size < 1024 or unit == units[-1]:
if unit == "B":
return f"{int(size)} {unit}"
return f"{size:.1f} {unit}"
size /= 1024
return f"{size:.1f} TB"
def _fmt_bytes(n: Optional[int]) -> str:
# Delegates to the shared formatter; ``or 0`` preserves this module's
# historical None/0 -> "0 B" display (the shared helper renders None
# as "?", which is wrong for a size total that is genuinely zero).
return format_bytes(n or 0)
def _fmt_ts(ts: Any) -> str: