From 4c624e2f832bb91c3bce0498a0a18f2257f1ae0b Mon Sep 17 00:00:00 2001 From: Open Date: Mon, 1 Jun 2026 02:32:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=9D=9E=E6=9C=89=E9=99=90?= =?UTF-8?q?=E7=94=A8=E9=87=8F=E6=95=B0=E5=80=BC=E5=AF=BC=E8=87=B4=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - percent_left_from_used() / token_count_value() 捕获 OverflowError - Infinity/NaN 按无效值处理:百分比返回 None,token 回退 0 - 补充 inf/nan 断言到现有回归测试 --- app.py | 8 ++++++-- tests/test_vibe_status.py | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index 29ef1b7..231d6a0 100644 --- a/app.py +++ b/app.py @@ -492,7 +492,7 @@ def percent_left_from_used(value: Any) -> Optional[int]: """Convert a used percentage into a clamped remaining percentage.""" try: used_percent = int(value) - except (TypeError, ValueError): + except (TypeError, ValueError, OverflowError): return None return max(0, min(100, 100 - used_percent)) @@ -676,7 +676,11 @@ def token_count_value(value: Any) -> int: """Return a non-negative token count from a session usage value.""" if not isinstance(value, (int, float)): return 0 - return max(0, int(value)) + try: + count = int(value) + except (ValueError, OverflowError): + return 0 + return max(0, count) def parse_codex_timestamp(value: Any) -> Optional[datetime]: diff --git a/tests/test_vibe_status.py b/tests/test_vibe_status.py index 18ae319..4142c45 100644 --- a/tests/test_vibe_status.py +++ b/tests/test_vibe_status.py @@ -818,12 +818,16 @@ def test_percent_left_from_used_clamps_and_rejects_invalid_values(self): self.assertEqual(app.percent_left_from_used(-10), 100) self.assertEqual(app.percent_left_from_used(999), 0) self.assertIsNone(app.percent_left_from_used("bad-value")) + self.assertIsNone(app.percent_left_from_used(float("inf"))) + self.assertIsNone(app.percent_left_from_used(float("nan"))) def test_token_count_value_clamps_negative_and_rejects_non_numeric_values(self): self.assertEqual(app.token_count_value(42), 42) self.assertEqual(app.token_count_value(3.8), 3) self.assertEqual(app.token_count_value(-5), 0) self.assertEqual(app.token_count_value("5"), 0) + self.assertEqual(app.token_count_value(float("inf")), 0) + self.assertEqual(app.token_count_value(float("nan")), 0) def test_dict_or_empty_rejects_non_object_values(self): payload = {"ok": True}