Fix freeze countdown exceeding configured cooloff hours.

Clamp future last_close anchors, cap remaining time server-side, prefer freeze_remaining_sec in the badge JS, and auto-repair stale DB rows on read.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-18 22:11:09 +08:00
parent deb240d4eb
commit 9330e356fc
9 changed files with 148 additions and 24 deletions
+23
View File
@@ -296,6 +296,29 @@ class AccountRiskLibTests(unittest.TestCase):
row = conn.execute("SELECT cooloff_until_ms FROM account_risk_state WHERE id=1").fetchone()
self.assertIsNone(row["cooloff_until_ms"])
def test_remaining_never_exceeds_configured_hours(self):
conn = _mem_conn()
now = datetime(2026, 6, 18, 22, 0, 0)
now_ms = _local_ms(now)
future_close = now_ms + 49 * 60 * 1000
conn.execute(
"""UPDATE account_risk_state SET
trading_day='2026-06-18',
manual_close_count=1,
cooloff_until_ms=?,
cooloff_hours=4,
last_close_at_ms=?,
daily_frozen=0
WHERE id=1""",
(future_close + 4 * 3600 * 1000, future_close),
)
st = compute_account_risk_status(conn, trading_day="2026-06-18", now=now)
self.assertEqual(st["status"], STATUS_FREEZE_4H)
self.assertLessEqual(st["freeze_remaining_sec"], 4 * 3600 + 2)
self.assertGreater(st["freeze_remaining_sec"], 3 * 3600 + 58 * 60)
row = conn.execute("SELECT last_close_at_ms, cooloff_until_ms FROM account_risk_state WHERE id=1").fetchone()
self.assertLessEqual(int(row["last_close_at_ms"]), now_ms + 60 * 1000)
def test_legacy_naive_utc_ms_countdown_normalized(self):
conn = _mem_conn()
now = datetime(2026, 6, 14, 12, 0, 0)