From c816b6f7024f899890cb046cfb09e2cc274f5aff Mon Sep 17 00:00:00 2001 From: Open Date: Mon, 1 Jun 2026 06:04:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AE=BE=E7=BD=AE=E9=A1=B5?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E6=95=B0=E5=AD=97=E5=AD=97=E6=AE=B5=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E4=BF=9D=E5=AD=98=E5=A4=B1=E8=B4=A5=20(#90)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 48 +++++++++++++++++++++++++++------------ tests/test_vibe_status.py | 23 +++++++++++++------ 2 files changed, 49 insertions(+), 22 deletions(-) diff --git a/app.py b/app.py index 376ee13..953637f 100644 --- a/app.py +++ b/app.py @@ -376,24 +376,44 @@ def settings_config_from_params(params: Dict[str, list], base_config: Dict[str, # Update refresh settings if "refresh_interval" in params: - interval = int(params["refresh_interval"][0]) - updated["refresh"]["interval_seconds"] = max(30, min(3600, interval)) + current_interval = refresh_interval_seconds(updated["refresh"].get("interval_seconds")) + updated["refresh"]["interval_seconds"] = clamp_int_range( + params["refresh_interval"][0], + current_interval, + 30, + 3600, + ) if "refresh_page" in params: - page_refresh = int(params["refresh_page"][0]) - updated["refresh"]["auto_refresh_page_ms"] = max(30, min(3600, page_refresh)) * 1000 + current_page_refresh = page_refresh_seconds(updated["refresh"].get("auto_refresh_page_ms")) + updated["refresh"]["auto_refresh_page_ms"] = clamp_int_range( + params["refresh_page"][0], + current_page_refresh, + 30, + 3600, + ) * 1000 # Update Codex settings updated["codex"]["enabled"] = "codex_enabled" in params if "codex_source" in params: updated["codex"]["source"] = params["codex_source"][0] if "session_limit" in params: - limit = int(params["session_limit"][0]) - updated["codex"]["session_file_limit"] = max(1, min(100, limit)) + current_limit = codex_session_file_limit(updated["codex"].get("session_file_limit")) + updated["codex"]["session_file_limit"] = clamp_int_range( + params["session_limit"][0], + current_limit, + 1, + 100, + ) # Update status board settings if "stale_after_seconds" in params: - stale_after = int(params["stale_after_seconds"][0]) - updated["status"]["stale_after_seconds"] = max(60, min(86400, stale_after)) + current_stale_after = status_stale_after_seconds(updated["status"].get("stale_after_seconds")) + updated["status"]["stale_after_seconds"] = clamp_int_range( + params["stale_after_seconds"][0], + current_stale_after, + 60, + 86400, + ) updated.pop("vibe", None) # Update display settings @@ -494,15 +514,13 @@ def redact_sensitive_request_line(request_line: str) -> str: return " ".join(parts) -def status_stale_after_seconds() -> int: +def status_stale_after_seconds(value: Any = None) -> int: """Return the heartbeat freshness threshold.""" - value = config_section("status").get("stale_after_seconds") if value is None: - value = config_section("vibe").get("stale_after_seconds", 900) - try: - return max(60, int(value)) - except (TypeError, ValueError, OverflowError): - return 900 + value = config_section("status").get("stale_after_seconds") + if value is None: + value = config_section("vibe").get("stale_after_seconds", 900) + return clamp_int_range(value, 900, 60, 86400) def codex_session_file_limit(value: Any = None) -> int: diff --git a/tests/test_vibe_status.py b/tests/test_vibe_status.py index 70baf28..8cdb0fd 100644 --- a/tests/test_vibe_status.py +++ b/tests/test_vibe_status.py @@ -391,20 +391,29 @@ def test_runtime_helpers_tolerate_invalid_config_sections(self): self.assertIn("InkDash", main_html) self.assertIn("设置", settings_html) - def test_settings_config_from_params_does_not_mutate_base_on_error(self): + def test_settings_config_from_params_preserves_base_on_bad_numeric_fields(self): base_config = app.merge_configs(app.DEFAULT_CONFIG, { "server": {"port": 8080}, - "refresh": {"interval_seconds": 300}, + "refresh": {"interval_seconds": 600, "auto_refresh_page_ms": 120000}, + "codex": {"session_file_limit": 7}, + "status": {"stale_after_seconds": 1200}, }) original = copy.deepcopy(base_config) - with self.assertRaises(ValueError): - app.settings_config_from_params({ - "port": ["9090"], - "refresh_interval": ["bad-value"], - }, base_config) + updated = app.settings_config_from_params({ + "port": ["9090"], + "refresh_interval": ["bad-value"], + "refresh_page": [""], + "session_limit": ["bad-value"], + "stale_after_seconds": ["bad-value"], + }, base_config) self.assertEqual(base_config, original) + self.assertEqual(updated["server"]["port"], 9090) + self.assertEqual(updated["refresh"]["interval_seconds"], 600) + self.assertEqual(updated["refresh"]["auto_refresh_page_ms"], 120000) + self.assertEqual(updated["codex"]["session_file_limit"], 7) + self.assertEqual(updated["status"]["stale_after_seconds"], 1200) def test_settings_config_from_params_returns_validated_copy(self): base_config = app.merge_configs(app.DEFAULT_CONFIG, {