Skip to content

The agent that grows with you

fix(cache): a frozen-config worker never opens config.yaml for a content digest; ctime marks a write recent

Teknium6f37cc8aaeda
diff --git a/hermes_cli/config_backend.py b/hermes_cli/config_backend.py
index ddd68d09ad2..22834e361f0 100644
--- a/hermes_cli/config_backend.py
+++ b/hermes_cli/config_backend.py
@@ -117,8 +117,10 @@ class FileBackend:
return self.version_path(self.config_path(home))
def cache_version(self, home: Path, version: tuple[Any, ...]) -> tuple[Any, ...]:
- from utils import racy_content_digest # utils.file_cache_version's rule, on this stat
- return (*version, racy_content_digest(self.config_path(home), version[0]))
+ # utils.file_cache_version's rule. ``version`` is config_version's opaque token, so the
+ # racy test takes the file's own signature.
+ from utils import racy_content_digest
+ return (*version, racy_content_digest(self.config_path(home), self.version(home)))
def exists(self, home: Path) -> bool:
return self.config_path(home).exists()
diff --git a/tests/hermes_cli/test_file_cache_same_tick_rewrite.py b/tests/hermes_cli/test_file_cache_same_tick_rewrite.py
index f052a6c388c..8e3dd0862b7 100644
--- a/tests/hermes_cli/test_file_cache_same_tick_rewrite.py
+++ b/tests/hermes_cli/test_file_cache_same_tick_rewrite.py
@@ -7,8 +7,11 @@ process: a revoked pairing kept its access, and a rotated key in the shared auth
the profiles reading it. ``coarse_clock`` reports both writes in one frozen tick, which is what such a
kernel returns, on this host too."""
import json
+import os
+import subprocess
import sys
import time
+from pathlib import Path
import pytest
@@ -74,3 +77,68 @@ def test_a_long_lived_signature_does_not_change_when_the_file_settles(tmp_path,
assert utils.file_cache_version(path, always_digest=True) == fresh
path.write_text("model: b\n")
assert utils.file_cache_version(path, always_digest=True) != fresh
+
+
+
+def test_an_mtime_restoring_rewrite_still_gets_a_content_digest(tmp_path):
+ """``cp -p`` / ``rsync --inplace -t`` put an old mtime back after writing; ctime still says fresh."""
+ import utils
+ path = tmp_path / "auth.json"
+ path.write_text('{"k": "aaaa"}')
+ old = time.time() - 3600
+ os.utime(path, (old, old))
+ path.write_text('{"k": "bbbb"}') # same size, in place
+ os.utime(path, (old, old))
+ assert utils.file_cache_version(path)[-1] == utils.content_digest(path.read_bytes())
+
+
+_WORKER_PROBE = r"""
+import json, os, sys
+from pathlib import Path
+import utils
+utils.RACY_MTIME_WINDOW_NS = int(sys.argv[1])
+home = Path(os.environ["HERMES_HOME"])
+(home / "config.yaml").write_text("agent:\n max_turns: 17\nskills:\n external_dirs: []\n")
+from agent.safe_worker_policy import _bind_safe_worker_policy
+_bind_safe_worker_policy(safe_mode=True, ignore_user_config=True, config={"agent": {"max_turns": 3}})
+opens, current = {}, [None]
+def audit(event, args):
+ if event == "open" and str(args[0]).endswith("config.yaml") and current[0]:
+ opens[current[0]] = opens.get(current[0], 0) + 1
+sys.addaudithook(audit)
+from agent import skill_utils
+import providers
+from hermes_cli import free_tier_bootstrap, model_switch, profiles
+calls = {
+ "skills": skill_utils.get_external_skills_dirs,
+ "named_custom": lambda: providers._has_named_custom_provider("foo", home, "k"),
+ "alias_source": model_switch._direct_alias_source_identity,
+ "free_tier": free_tier_bootstrap._config_stamp,
+ "standalone": lambda: profiles.profile_is_standalone(home),
+}
+for name, call in calls.items():
+ current[0] = name
+ opens.setdefault(name, 0)
+ call()
+current[0] = None
+print(json.dumps(opens))
+"""
+
+
+def _worker_config_opens(tmp_path: Path, window_ns: int) -> dict:
+ home = tmp_path / f"home-{window_ns}"
+ home.mkdir()
+ env = {**os.environ, "HERMES_HOME": str(home)}
+ root = Path(__file__).resolve().parents[2]
+ out = subprocess.run([sys.executable, "-c", _WORKER_PROBE, str(window_ns)], cwd=root, env=env,
+ capture_output=True, text=True, timeout=120, check=True)
+ return json.loads(out.stdout.strip().splitlines()[-1])
+
+
+def test_a_frozen_config_worker_opens_config_yaml_no_more_while_the_file_is_fresh(tmp_path):
+ """A safe worker reads its config from the bound snapshot: the content check on a just-written
+ ``config.yaml`` must not add an open of the profile file at any reader (fresh exec per leg, since
+ the policy binds once per process)."""
+ racy = _worker_config_opens(tmp_path, 10**15)
+ settled = _worker_config_opens(tmp_path, 0)
+ assert racy == settled, (racy, settled)
diff --git a/utils.py b/utils.py
index 42c56618192..0b7d4d66522 100644
--- a/utils.py
+++ b/utils.py
@@ -8,6 +8,7 @@ import os
import re
import shutil
import stat
+import sys
import tempfile
import threading
import time
@@ -66,12 +67,23 @@ def content_digest(data: bytes) -> str:
return hashlib.blake2b(data, digest_size=16).hexdigest()
-def racy_content_digest(path: str | Path, mtime_ns: int) -> str:
- """``""`` once ``mtime_ns`` has settled; while it is racy, a digest of the file's bytes. A cache
- keyed on a stat signature appends it, because two same-size in-place writes in one timestamp tick
- share mtime, ctime, size and inode (kernels without multigrain timestamps). Read before the parse
- it guards: a write in between then costs one re-parse instead of pinning the older content."""
- if not mtime_is_racy(mtime_ns):
+def _frozen_config_worker(path: str | Path) -> bool:
+ """True for a ``config.yaml`` read inside a worker whose config is a frozen snapshot: such a
+ worker never opens a profile config file, so a cache key there stays stat-only."""
+ if Path(path).name != "config.yaml":
+ return False
+ policy = sys.modules.get("agent.safe_worker_policy") # binding a snapshot imports it first
+ return policy is not None and policy.worker_config_snapshot() is not None
+
+
+def racy_content_digest(path: str | Path, sig: tuple[int, int, int, int]) -> str:
+ """``""`` once the :func:`file_signature` ``sig`` has settled; while it is racy, a digest of the
+ file's bytes. A cache keyed on a stat signature appends it, because two same-size in-place writes
+ in one timestamp tick share mtime, ctime, size and inode (kernels without multigrain timestamps).
+ Racy is judged on the later of mtime and ctime: userland can restore an older mtime after a write
+ (``cp -p``, ``rsync --inplace -t``), never ctime. Read before the parse it guards: a write in
+ between then costs one re-parse instead of pinning the older content."""
+ if not mtime_is_racy(max(sig[0], sig[3])) or _frozen_config_worker(path):
return ""
with open(path, "rb") as f:
return content_digest(f.read())
@@ -85,15 +97,16 @@ def file_cache_version(path: str | Path, st: os.stat_result | None = None, *,
re-validated on every read: a racily-stored entry costs one re-parse when the file settles.
``always_digest`` hashes on every call, for a signature stored and compared much later (a
change detector whose false "changed" restarts something, or a "still the broken file" record),
- where the racy digest turning empty would read as a change. Raises ``OSError`` like ``os.stat``;
- given the caller's ``st`` it does not: a file gone or unreadable since that stat gets the error's
- name as its digest, which no readable version matches."""
+ where the racy digest turning empty would read as a change. A worker with a frozen config
+ snapshot never hashes a ``config.yaml`` (the key is stat-only there, as before). Raises
+ ``OSError`` like ``os.stat``; given the caller's ``st`` it does not: a file gone or unreadable
+ since that stat gets the error's name as its digest, which no readable version matches."""
sig = file_signature(Path(path).stat() if st is None else st)
try:
- if always_digest:
+ if always_digest and not _frozen_config_worker(path):
with open(path, "rb") as f:
return (*sig, content_digest(f.read()))
- return (*sig, racy_content_digest(path, sig[0])) # sig[0] is st_mtime_ns
+ return (*sig, racy_content_digest(path, sig))
except OSError as exc:
if st is None:
raise