The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(terminal): terminal-cache cleanup re-checks each bg group before deleting and counts only real removals (#132401)
Teknium16365682ddb9
diff --git a/tests/tools/test_local_temp_dir.py b/tests/tools/test_local_temp_dir.pyindex e0307730f49..f2db760bc74 100644--- a/tests/tools/test_local_temp_dir.py+++ b/tests/tools/test_local_temp_dir.py@@ -123,5 +123,47 @@ def test_cleanup_terminal_temp_cache(tmp_path, monkeypatch):assert not (root / "hermes_bg_dead1.log").exists()+def test_cleanup_terminal_temp_cache_revalidates_before_each_delete(tmp_path, monkeypatch):+ """The age snapshot is a candidate list: a bg session whose .log is written after it+ keeps its remaining files, and a directory rmtree could not remove is not counted."""+ import pathlib+ import time++ from tools.environments import local as local_mod++ root = tmp_path / "cache" / "terminal"+ root.mkdir(parents=True)+ monkeypatch.setattr(local_mod, "_default_terminal_temp_dir", lambda: root)+ old = time.time() - 100 * 3600+ for suffix in ("pid", "log", "exit"):+ f = root / f"hermes_bg_resumed.{suffix}"+ f.write_text("x")+ os.utime(f, (old, old))+ stuck = root / "stuck-dir"+ stuck.mkdir()+ os.utime(stuck, (old, old))++ real_unlink = pathlib.Path.unlink++ fired = []++ def unlink_then_session_writes(self, *args, **kwargs):+ real_unlink(self, *args, **kwargs)+ if self.name.startswith("hermes_bg_resumed.") and not fired:+ fired.append(self.name) # the session writes its log right after the first delete+ (root / "hermes_bg_resumed.log").write_text("RESUMED OUTPUT\n")++ monkeypatch.setattr(pathlib.Path, "unlink", unlink_then_session_writes)+ monkeypatch.setattr(local_mod.shutil, "rmtree", lambda *a, **k: None)++ removed = local_mod.cleanup_terminal_temp_cache(max_age_hours=24)+ survivors = {p.name for p in root.glob("hermes_bg_resumed.*")}+ # Only the member deleted before the write is gone (iterdir order picks which one).+ expected = {f"hermes_bg_resumed.{s}" for s in ("pid", "log", "exit")} - (set(fired) - {"hermes_bg_resumed.log"})+ assert survivors == expected, (fired, survivors)+ assert stuck.exists()+ assert removed == len(set(fired) - {"hermes_bg_resumed.log"}) # never the stuck dir++if __name__ == "__main__":sys.exit(pytest.main([__file__, "-q"]))diff --git a/tools/environments/local.py b/tools/environments/local.pyindex c2baf9daa10..bcdff2daf36 100644--- a/tools/environments/local.py+++ b/tools/environments/local.py@@ -83,19 +83,34 @@ def cleanup_terminal_temp_cache(max_age_hours: float = TERMINAL_TEMP_MAX_IDLE_HOif m := _BG_GROUP_RE.match(f.name):group_newest[m.group(1)] = max(group_newest.get(m.group(1), 0.0), mt)+ def group_touched(stem: str) -> bool:+ # Re-stat every member right before each delete: the snapshot above is a candidate+ # list, and a session that writes its .log after it must keep the whole group.+ for sfx in ("log", "pid", "exit"):+ try:+ if (root / f"{stem}.{sfx}").stat().st_mtime >= cutoff:+ return True+ except FileNotFoundError:+ continue+ except OSError:+ return True+ return False+removed = 0- for f, mt in mtimes.items():+ for f in mtimes:m = _BG_GROUP_RE.match(f.name)if m:- if group_newest[m.group(1)] >= cutoff:+ if group_newest[m.group(1)] >= cutoff or group_touched(m.group(1)):continueelif subtree_touched_since(f, cutoff):continuetry:shutil.rmtree(f, ignore_errors=True) if f.is_dir() else f.unlink()- removed += 1except OSError:continue+ # rmtree ignores errors: only a path that is really gone counts as removed.+ if not os.path.lexists(f):+ removed += 1return removeddiff --git a/website/docs/user-guide/configuration.md b/website/docs/user-guide/configuration.mdindex e40a2188eeb..097ea77fbbc 100644--- a/website/docs/user-guide/configuration.md+++ b/website/docs/user-guide/configuration.md@@ -261,7 +261,10 @@ stays as long as anything anywhere inside it was written in the last day, and gothe last write. Before an idle entry is deleted, Hermes also stops any process still runningwith its working directory inside that entry (or inside a scratch path that no longer exists,such as a headless browser left behind by a test run) and drops any `git worktree`-registration that pointed into it. `hermes doctor` reports the directory and its size, and+registration that pointed into it. Each entry is re-checked right before it is deleted, so+one written to while the prune was stopping processes is kept. Every removal, stopped+process and kept entry is recorded in `~/.hermes/logs/scratch-prune.log`. `hermes doctor`+reports the directory and its size, andwarns about directories over 1 GB elsewhere under `cache/` that no pruner covers.`desktop.font_family` sets the font for chat and the rest of the Hermes Desktop interface (the terminal pane has its own key above). Give it one installed family name (for example, `OpenDyslexic` or `Atkinson Hyperlegible`) or a CSS font stack; Hermes keeps the active theme's own stack behind it so CJK and emoji glyphs still resolve, and an empty value uses the theme's font. Edit it in **Settings → Appearance → Chat Font**.