The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tools): share the idle-expiry criteria between acquire and the reaper; plain once-latch
teknium131d237cab9d3
diff --git a/tests/tools/test_code_kernel.py b/tests/tools/test_code_kernel.pyindex 055752b27e7..03fbb80d53f 100644--- a/tests/tools/test_code_kernel.py+++ b/tests/tools/test_code_kernel.py@@ -552,15 +552,6 @@ class TestBackgroundIdleReaper(unittest.TestCase):finally:kernel.attached -= 1- def test_spawn_starts_the_reaper_thread(self):- import threading-- with _kernel_config():- self._run_as("conv-a", "x = 1", task_id="turn-1")- self.assertTrue(any(t.name == "hermes-kernel-idle-reaper"- for t in threading.enumerate()))--class TestStaleStagingDirSweep(unittest.TestCase):def test_week_old_kernel_dirs_go_and_fresh_ones_stay(self):import time as time_modulediff --git a/tools/code_kernel.py b/tools/code_kernel.pyindex a63b7237787..8da86292975 100644--- a/tools/code_kernel.py+++ b/tools/code_kernel.py@@ -656,6 +656,13 @@ def _spawn(kernel: SessionKernel, *, child_python: str, child_cwd: str,_ensure_background_reaper()+def _pop_idle_expired(now: float, idle_timeout: float) -> List[SessionKernel]:+ """Pop (caller holds ``_REGISTRY.lock``) every kernel idle past *idle_timeout*. Kernels with+ attached cells are skipped: the last cell out tears them down."""+ return [_KERNELS.pop(k) for k in list(_KERNELS)+ if _KERNELS[k].attached == 0 and now - _KERNELS[k].last_used > idle_timeout]++def _acquire_kernel(key: Tuple, reset: bool, *, pinned: bool = False) -> Tuple[SessionKernel, bool]:"""Look up or register the kernel for *key*; returns (kernel, state_reset). Every entry alsosweeps idle-expired kernels and enforces the process-wide LRU cap (doomed kernels are popped@@ -664,10 +671,7 @@ def _acquire_kernel(key: Tuple, reset: bool, *, pinned: bool = False) -> Tuple[S``shutdown_kernels_for_delegated_child``, so the cap has nothing to bound for them."""cap, idle_timeout = _lifecycle_limits()with _REGISTRY.lock:- now = time.monotonic()- # Reaping and eviction skip kernels with attached cells (the last cell out tears them down).- expired = [_KERNELS.pop(k) for k in list(_KERNELS)- if _KERNELS[k].attached == 0 and now - _KERNELS[k].last_used > idle_timeout]+ expired = _pop_idle_expired(time.monotonic(), idle_timeout)kernel = _KERNELS.get(key)state_reset = kernel is not None and (reset or kernel.dead())if state_reset:@@ -697,7 +701,7 @@ def _acquire_kernel(key: Tuple, reset: bool, *, pinned: bool = False) -> Tuple[S# outlived a host which died without cleanup (SIGKILL / container restart)._STALE_STAGING_DIR_AGE = 7 * 86400_REAPER_INTERVAL_FLOOR, _REAPER_INTERVAL_CEIL = 30.0, 300.0-_REAPER_GUARD = threading.Lock()+_REAPER_STARTED = Falsedef _sweep_stale_staging_dirs(now: Optional[float] = None) -> int:@@ -724,9 +728,7 @@ def _reap_once() -> None:"""One background pass: the acquire-path idle criteria, then the stale-dir sweep."""_, idle_timeout = _lifecycle_limits()with _REGISTRY.lock:- now = time.monotonic()- expired = [_KERNELS.pop(k) for k in list(_KERNELS)- if _KERNELS[k].attached == 0 and now - _KERNELS[k].last_used > idle_timeout]+ expired = _pop_idle_expired(time.monotonic(), idle_timeout)for doomed in expired:doomed.teardown()_sweep_stale_staging_dirs()@@ -734,9 +736,13 @@ def _reap_once() -> None:def _ensure_background_reaper() -> None:"""Start the reaper once per process (on the first kernel spawn)."""- if _REAPER_GUARD.acquire(blocking=False):- threading.Thread(target=_background_reaper, daemon=True,- name="hermes-kernel-idle-reaper").start()+ global _REAPER_STARTED+ with _REGISTRY.lock:+ if _REAPER_STARTED:+ return+ _REAPER_STARTED = True+ threading.Thread(target=_background_reaper, daemon=True,+ name="hermes-kernel-idle-reaper").start()def _background_reaper() -> None: