"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(cli): switch to the next unit when formatted numbers round up (#1926)
Rudy Mizrahi Celeklicafa597b1fb3
diff --git a/agent/cli/utils/format.py b/agent/cli/utils/format.pyindex 07985e09d..ad1300162 100644--- a/agent/cli/utils/format.py+++ b/agent/cli/utils/format.py@@ -55,9 +55,11 @@ def format_duration(value: Number | None, *, unit: str = "ms") -> str:else:raise ValueError(f"unit must be 'ms' or 's', got {unit!r}")- if seconds < 1.0:+ # Compare the value as it will be printed, so 999.6ms reads "1.0s" rather+ # than "1000ms" and 59.96s reads "1m 00s" rather than "60.0s".+ if round(seconds * 1000) < 1000:return f"{int(round(seconds * 1000))}ms"- if seconds < 60:+ if round(seconds, 1) < 60:# 1.4s, 12.0s — keep one decimal to communicate sub-second jitterreturn f"{seconds:.1f}s"minutes, rem = divmod(int(round(seconds)), 60)@@ -93,9 +95,9 @@ def format_tokens(count: Number | None) -> str:if n < 1_000:return f"{n} tokens"- if n < 1_000_000:+ if round(n / 1_000, 1) < 1_000:return f"{n / 1_000:.1f}k tokens"- if n < 1_000_000_000:+ if round(n / 1_000_000, 1) < 1_000:return f"{n / 1_000_000:.1f}M tokens"return f"{n / 1_000_000_000:.1f}B tokens"@@ -148,7 +150,7 @@ def abbreviate_num(value: Number | None, *, currency: str | None = None) -> str:return f"{sign}{prefix}{int(n_abs)}"for unit, divisor in (("k", 1_000), ("M", 1_000_000), ("B", 1_000_000_000), ("T", 1_000_000_000_000)):- if n_abs < divisor * 1_000:+ if round(n_abs / divisor, 1) < 1_000:return f"{sign}{prefix}{n_abs / divisor:.1f}{unit}"return f"{sign}{prefix}{n_abs:.1e}"diff --git a/agent/tests/test_cli_format_unit_boundaries.py b/agent/tests/test_cli_format_unit_boundaries.pynew file mode 100644index 000000000..2fa3b2c7f--- /dev/null+++ b/agent/tests/test_cli_format_unit_boundaries.py@@ -0,0 +1,34 @@+"""CLI formatters must switch unit when rounding reaches the next unit."""++from __future__ import annotations++import pytest++from cli.utils.format import abbreviate_num, format_duration, format_tokens+++ "count, expected",+ [(999_950, "1.0M tokens"), (999_949, "999.9k tokens"), (1_200, "1.2k tokens"),+ (999_950_000, "1.0B tokens"), (452, "452 tokens")],+)+def test_format_tokens_unit_boundary(count: int, expected: str) -> None:+ assert format_tokens(count) == expected+++ "value, expected",+ [(999_960, "1.0M"), (999_940, "999.9k"), (12_400, "12.4k"), (-999_960, "-1.0M"),+ (999_960_000, "1.0B")],+)+def test_abbreviate_num_unit_boundary(value: float, expected: str) -> None:+ assert abbreviate_num(value) == expected+++ "ms, expected",+ [(999.6, "1.0s"), (999.4, "999ms"), (59_960, "1m 00s"), (59_940, "59.9s"),+ (1_400, "1.4s"), (230, "230ms"), (252_000, "4m 12s")],+)+def test_format_duration_unit_boundary(ms: float, expected: str) -> None:+ assert format_duration(ms) == expected