Skip to content

The agent that grows with you

fix(cron): preserve suspend credit across idle sampling

unsupportedpastelsc6293dcd5759
diff --git a/agent/session_activity.py b/agent/session_activity.py
index 079a4a461ac..1a3ec55a766 100644
--- a/agent/session_activity.py
+++ b/agent/session_activity.py
@@ -135,8 +135,10 @@ class AwakeIdleMeter:
def measure(self, idle_s: float) -> float:
"""Return *idle_s* minus the sleep observed since the agent's last activity."""
wall, mono = time.time(), time.monotonic()
- self._asleep_s += max(0.0, (wall - self._wall) - (mono - self._mono))
+ slept_s = max(0.0, (wall - self._wall) - (mono - self._mono))
+ self._asleep_s += slept_s
self._wall, self._mono = wall, mono
- # New activity pulls idle below the credit, which then drops with it (to within one poll).
- self._asleep_s = max(0.0, min(self._asleep_s, idle_s))
- return idle_s - self._asleep_s
+ # The idle sample may predate sleep observed by these clock reads. Keep that new
+ # credit until the next poll; only older credit can be trimmed by renewed activity.
+ self._asleep_s = max(0.0, min(self._asleep_s, idle_s + slept_s))
+ return max(0.0, idle_s - self._asleep_s)
diff --git a/tests/agent/test_turn_liveness_watchdog.py b/tests/agent/test_turn_liveness_watchdog.py
index cd19ba7fb42..9e4cd92fad6 100644
--- a/tests/agent/test_turn_liveness_watchdog.py
+++ b/tests/agent/test_turn_liveness_watchdog.py
@@ -755,7 +755,8 @@ def test_declined_abort_does_not_cancel_pending_compression_commit():
fence.finish_commit()
-def test_host_sleep_is_not_a_stalled_turn(monkeypatch):
[email protected]("during_sample", [False, True])
+def test_host_sleep_is_not_a_stalled_turn(monkeypatch, during_sample):
"""A host that sleeps mid-turn wakes with a wall-clock activity stamp as old as the nap.
The nap is not a stall; awake silence after it still aborts at the bound."""
from types import SimpleNamespace
@@ -777,9 +778,21 @@ def test_host_sleep_is_not_a_stalled_turn(monkeypatch):
is_turn_active=lambda: True, commit_abort=abort, deactivate_turn=MagicMock(),
)
+ pending_sleep = [0.0]
+ original_sample = watchdog._sample
+
+ def sample():
+ snapshot = original_sample()
+ clock.wall += pending_sleep[0]
+ pending_sleep[0] = 0.0
+ return snapshot
+
+ monkeypatch.setattr(watchdog, "_sample", sample)
+
def poll(asleep=0.0):
clock.mono += 15
- clock.wall += 15 + asleep
+ clock.wall += 15 + (0.0 if during_sample else asleep)
+ pending_sleep[0] = asleep if during_sample else 0.0
return watchdog._tick()
poll()
diff --git a/tests/cron/test_cron_inactivity_timeout.py b/tests/cron/test_cron_inactivity_timeout.py
index b3a6ac03153..567d949be62 100644
--- a/tests/cron/test_cron_inactivity_timeout.py
+++ b/tests/cron/test_cron_inactivity_timeout.py
@@ -4,6 +4,8 @@ import threading
import time
from types import SimpleNamespace
+import pytest
+
class TestInactivityWatchdogLoop:
"""The daemon-thread inactivity helper must not depend on the caller thread."""
@@ -79,7 +81,8 @@ class TestInactivityWatchdogLoop:
class TestHostSleep:
"""A sleeping host freezes the whole job, so the nap is not job inactivity."""
- def test_sleep_is_not_idle_time_but_awake_idle_still_fires(self, monkeypatch):
+ @pytest.mark.parametrize("during_sample", [False, True])
+ def test_sleep_is_not_idle_time_but_awake_idle_still_fires(self, monkeypatch, during_sample):
from agent import session_activity
from cron.scheduler import _inactivity_watchdog_loop
@@ -95,12 +98,18 @@ class TestHostSleep:
polls.append(timeout)
clock.wall += timeout
clock.mono += timeout
- if len(polls) == 3:
+ if len(polls) == 3 and not during_sample:
clock.wall += 900.0 # the laptop sleeps for 15 minutes mid-job
return len(polls) > 1000
+ def read_idle():
+ idle = clock.wall - last_activity_wall
+ if during_sample and len(polls) == 3:
+ clock.wall += 900.0 # suspend after the activity read, before meter clocks
+ return idle
+
fired = _inactivity_watchdog_loop(
- get_idle_seconds=lambda: clock.wall - last_activity_wall,
+ get_idle_seconds=read_idle,
limit_s=600.0,
poll_s=5.0,
stop=_Stop(),
diff --git a/website/docs/developer-guide/cron-internals.md b/website/docs/developer-guide/cron-internals.md
index 610f6f26642..65954980117 100644
--- a/website/docs/developer-guide/cron-internals.md
+++ b/website/docs/developer-guide/cron-internals.md
@@ -306,7 +306,7 @@ The script timeout defaults to 3600 seconds (1 hour). `_get_script_timeout()` re
3. **Config** — `cron.script_timeout_seconds` in `config.yaml` (read via `load_config()`)
4. **Default** — 3600 seconds (1 hour)
-This timeout bounds the **pre-run script only**, not the agent. Skill-based / LLM-driven jobs run on a separate *inactivity*-based budget (`HERMES_CRON_TIMEOUT`, default 600s of idle time, `0` = unlimited) — they can run for hours as long as they keep calling tools or streaming tokens, and are only killed after the configured idle period with no activity. Time the host spends asleep does not count toward that idle period. Scripts are dispatched to a persistent thread pool (not held under the tick lock), so a long-running script does not block other due jobs from firing.
+This timeout bounds the **pre-run script only**, not the agent. Skill-based / LLM-driven jobs run on a separate *inactivity*-based budget (`HERMES_CRON_TIMEOUT`, default 600s of idle time, `0` = unlimited) — they can run for hours as long as they keep calling tools or streaming tokens, and are only killed after the configured idle period with no activity. On platforms where Python's monotonic clock excludes suspend (macOS and Linux), time the host spends asleep does not count toward that idle period; Windows behavior is unchanged. Scripts are dispatched to a persistent thread pool (not held under the tick lock), so a long-running script does not block other due jobs from firing.
On timeout or ownership cancellation, `cron.scheduler_script` uses the shared
`agent.deadline.kill_process_tree` hard-kill path. On POSIX it briefly stops and