The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(prompt): build the system prompt without waiting for the scratch prune
Eva94bb62ac47ab
diff --git a/agent/prompt_builder.py b/agent/prompt_builder.pyindex f85a291127b..d85519a4ef5 100644--- a/agent/prompt_builder.py+++ b/agent/prompt_builder.py@@ -16,7 +16,8 @@ from pathlib import Pathfrom typing import Any, Dict, Optionalfrom hermes_constants import (- get_hermes_home, get_scratch_dir, get_skills_dir, is_wsl, reset_hermes_home_override, set_hermes_home_override,+ get_hermes_home, get_scratch_dir, get_skills_dir, is_wsl, prune_scratch_dir_in_background,+ reset_hermes_home_override, set_hermes_home_override,)from agent.model_metadata import CHARS_PER_TOKEN@@ -920,7 +921,9 @@ def _local_host_hints() -> list[str]:# The model reaches for the system temp dir by reflex (tmpfs on most Linux hosts, fills RAM);# naming Hermes' scratch dir here is what makes the TMPDIR export a habit rather than a hidden default.try:- host_lines.append(f"Scratch directory: {get_scratch_dir()} (TMPDIR points here; write temporary files "+ scratch = get_scratch_dir(prune=False)+ prune_scratch_dir_in_background(scratch)+ host_lines.append(f"Scratch directory: {scratch} (TMPDIR points here; write temporary files ""and probes there, never under the system temp dir; entries idle for 24h are pruned)")except OSError:passdiff --git a/hermes_constants.py b/hermes_constants.pyindex 652f2f456fb..2f768768612 100644--- a/hermes_constants.py+++ b/hermes_constants.py@@ -9,6 +9,7 @@ import reimport shutilimport statimport sys+import threadingfrom collections.abc import Callable, Mapping, MutableMappingfrom contextvars import ContextVar, Tokenfrom pathlib import Path@@ -815,6 +816,8 @@ SCRATCH_MAX_IDLE_HOURS = 24_SCRATCH_PRUNE_STAMP = ".last_prune"_SCRATCH_PRUNE_INTERVAL_SECONDS = 3600_scratch_pruned_once = False+_scratch_prune_lock = threading.Lock()+_scratch_prune_thread: threading.Thread | None = None# AF_UNIX socket paths cap at 104 bytes (macOS) / 108 (Linux). Chrome appends# ``com.google.Chrome.XXXXXX/SingletonSocket`` (~45) and the code kernel@@ -1015,6 +1018,21 @@ def _prune_scratch_dir_once(scratch: Path) -> None:prune_scratch_dir(scratch)+def prune_scratch_dir_in_background(scratch: Path) -> None:+ """Schedule one prune using the caller's resolved home, without blocking a turn."""+ global _scratch_prune_thread+ if _scratch_pruned_once:+ return+ with _scratch_prune_lock:+ if _scratch_pruned_once or _scratch_prune_thread is not None:+ return+ _scratch_prune_thread = threading.Thread(+ target=_prune_scratch_dir_once, args=(scratch,), name="scratch-prune", daemon=True,+ )+ with contextlib.suppress(RuntimeError):+ _scratch_prune_thread.start()++def scratch_dir_usage_bytes(scratch: Path | None = None) -> int:"""Total bytes under the scratch dir (for ``hermes doctor``); 0 when unreadable."""root = scratch if scratch is not None else get_scratch_dir(prune=False)diff --git a/tests/test_scratch_dir.py b/tests/test_scratch_dir.pyindex 8917f1d2565..94b5b79afd4 100644--- a/tests/test_scratch_dir.py+++ b/tests/test_scratch_dir.py@@ -7,8 +7,10 @@ import shutilimport statimport subprocessimport sys+import threadingimport timefrom pathlib import Path+from types import SimpleNamespacefrom unittest.mock import patchfrom tests.waiting import wait_until@@ -19,6 +21,107 @@ import pytestfrom hermes_constants import apply_scratch_tmp_env, get_scratch_dir, prune_scratch_dir+def background_scratch_prune(tmp_path, monkeypatch):+ import hermes_constants as constants++ home = tmp_path / "launch"+ monkeypatch.setenv("HERMES_HOME", str(home))+ monkeypatch.setattr(constants, "_scratch_pruned_once", False)+ monkeypatch.setattr(constants, "_scratch_prune_thread", None)+ monkeypatch.setattr(constants, "_scratch_prune_lock", threading.Lock())+ calls, threads = [], []+ real_thread = threading.Thread++ def tracked_thread(*args, **kwargs):+ thread = real_thread(*args, **kwargs)+ if thread.name == "scratch-prune":+ threads.append(thread)+ return thread++ monkeypatch.setattr(constants.threading, "Thread", tracked_thread)+ monkeypatch.setattr(constants, "prune_scratch_dir", calls.append)+ token = constants.set_hermes_home_override(None)+ try:+ yield SimpleNamespace(home=home, calls=calls, threads=threads)+ finally:+ for thread in threads:+ thread.join(timeout=5)+ assert not thread.is_alive()+ constants.reset_hermes_home_override(token)+++def test_host_hints_do_not_wait_for_scratch_prune(background_scratch_prune, monkeypatch):+ import hermes_constants as constants+ from agent.prompt_builder import _local_host_hints++ state = background_scratch_prune++ def slow_prune(scratch):+ state.calls.append(scratch)+ time.sleep(2) # health: allow HX013 -- simulate the slow prune specified by the regression++ monkeypatch.setattr(constants, "prune_scratch_dir", slow_prune)+ start = time.monotonic()+ hints = _local_host_hints()+ elapsed = time.monotonic() - start+ assert elapsed < 0.5+ assert len(state.threads) == 1+ thread = state.threads[0]+ assert thread.daemon+ thread.join(timeout=5)+ assert not thread.is_alive()+ scratch = state.home / "cache" / "scratch"+ assert state.calls == [scratch]+ assert f"Scratch directory: {scratch} (TMPDIR points here; write temporary files " in hints[0]+++def test_host_hints_schedule_only_one_scratch_prune(background_scratch_prune, monkeypatch):+ import hermes_constants as constants+ from agent.prompt_builder import _local_host_hints++ state = background_scratch_prune+ release = threading.Event()+ prune_once = constants._prune_scratch_dir_once++ def delayed_prune(scratch):+ release.wait(timeout=5)+ prune_once(scratch)++ monkeypatch.setattr(constants, "_prune_scratch_dir_once", delayed_prune)+ try:+ _local_host_hints()+ _local_host_hints()+ assert len(state.threads) == 1+ finally:+ release.set()+ for thread in state.threads:+ thread.join(timeout=5)+ _local_host_hints()+ assert len(state.threads) == 1+ assert state.calls == [state.home / "cache" / "scratch"]+++def test_host_hints_scratch_prune_uses_routed_home(background_scratch_prune, tmp_path):+ import hermes_constants as constants+ from agent.prompt_builder import _local_host_hints++ state = background_scratch_prune+ routed_home = tmp_path / "routed"+ token = constants.set_hermes_home_override(routed_home)+ try:+ hints = _local_host_hints()+ finally:+ constants.reset_hermes_home_override(token)+ assert len(state.threads) == 1+ thread = state.threads[0]+ thread.join(timeout=5)+ assert not thread.is_alive()+ scratch = routed_home / "cache" / "scratch"+ assert state.calls == [scratch]+ assert f"Scratch directory: {scratch} (TMPDIR points here; write temporary files " in hints[0]++def test_scratch_env_follows_home_and_respects_user_tmpdir(tmp_path):"""Unset temp vars → scratch of env HERMES_HOME; a Hermes-exported value re-derives for a routedhome; a user/OS-set value (macOS ``/var/folders``, ``%TEMP%``) is never touched."""