Skip to content

The agent that grows with you

fix(scale-to-zero): clamp dashboard marker mtime to now

Ben Barclay59786d280f1d
diff --git a/gateway/scale_to_zero.py b/gateway/scale_to_zero.py
index bebb31fec11..e1a64159dbf 100644
--- a/gateway/scale_to_zero.py
+++ b/gateway/scale_to_zero.py
@@ -221,13 +221,16 @@ def dashboard_client_last_seen(
"""
import time
+ current = time.time() if now is None else now
p = dashboard_client_heartbeat_path() if path is None else path
try:
- return os.stat(p).st_mtime
+ # Clamp to now: a wall-clock step-back (NTP) can leave the mtime in the
+ # future, which would push idle out by the step size for no reason.
+ return min(os.stat(p).st_mtime, current)
except FileNotFoundError:
return None
except OSError:
- return time.time() if now is None else now
+ return current
def self_suspend_available(environ: Optional[dict] = None) -> bool:
diff --git a/tests/gateway/test_scale_to_zero_dashboard_client.py b/tests/gateway/test_scale_to_zero_dashboard_client.py
index e49c8c16219..93dbd73f1c9 100644
--- a/tests/gateway/test_scale_to_zero_dashboard_client.py
+++ b/tests/gateway/test_scale_to_zero_dashboard_client.py
@@ -75,6 +75,17 @@ def test_last_seen_returns_raw_mtime_without_staleness_cutoff(hermes_home):
assert s2z.dashboard_client_last_seen(now=mtime + 3600) == mtime
+def test_last_seen_future_mtime_is_clamped_to_now(hermes_home):
+ # A wall-clock step-back can leave the marker in the future; it must not
+ # extend the idle window past "now".
+ s2z.touch_dashboard_client_heartbeat()
+ p = s2z.dashboard_client_heartbeat_path()
+ future = time.time() + 600
+ os.utime(p, (future, future))
+ now = time.time()
+ assert s2z.dashboard_client_last_seen(now=now) == now
+
+
def test_last_seen_unreadable_marker_fails_awake(hermes_home, monkeypatch):
s2z.touch_dashboard_client_heartbeat()