The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(secret-scope): .env memo notices a same-size .env edit inside one timestamp tick
Tekniumbe22525da49b
diff --git a/agent/secret_scope.py b/agent/secret_scope.pyindex dfe185a2882..e6fb7ed47a5 100644--- a/agent/secret_scope.py+++ b/agent/secret_scope.py@@ -20,8 +20,6 @@ from contextvars import ContextVar, Tokenfrom pathlib import Pathfrom typing import Dict, Mapping, NamedTuple, Optional, Tuple-from utils import file_signature-# Process-global (describes the deployment mode, not a per-task value): set once# at gateway startup when gateway.multiplex_profiles is true.@@ -299,15 +297,15 @@ def _parse_env_value(raw_value: str) -> str:# turn, cron fire, MCP/browser adoption and housekeeping drain, and each call used to re-read and# re-parse the whole file.#-# FRESHNESS: every call still OPENS the file and keys on ``utils.file_signature`` of that-# descriptor's ``fstat`` (mtime_ns, size, inode, ctime_ns — ctime can't be backdated, so a pinned--# timestamp rewrite is still seen), re-checked after the read. The open keeps close-to-open-# revalidation on NFS, a vanished/unreadable file fails the open and is never cached (a transient-# EACCES must not become "this profile has no secrets"), and the descriptor pins one inode so a-# symlink repointed mid-read can't file one file's contents under another's identity.-# ``invalidate_env_file_cache()`` is the explicit knob; ``hermes_cli.config.invalidate_env_cache()``-# calls it for Hermes's own .env writers.-_ENV_FILE_CACHE: OrderedDict[str, tuple[tuple, dict[str, str]]] = OrderedDict()+# FRESHNESS: every call still OPENS and READS the file (a few hundred bytes) and keys on its exact+# bytes; only the decode + parse is skipped. A stat identity is not enough: a kernel that stamps+# mtime/ctime from the coarse (jiffy) clock leaves (mtime, size, inode, ctime) unchanged across an+# in-place same-size rewrite inside one tick, and ``/reload`` then served the old value (CI run+# 39146191939). The open keeps close-to-open revalidation on NFS, and a vanished/unreadable file+# fails the open and is never cached (a transient EACCES must not become "this profile has no+# secrets"). ``invalidate_env_file_cache()`` is the explicit knob; ``hermes_cli.config.+# invalidate_env_cache()`` calls it for Hermes's own .env writers.+_ENV_FILE_CACHE: OrderedDict[str, tuple[bytes, dict[str, str]]] = OrderedDict()_ENV_FILE_CACHE_LOCK = threading.Lock()_ENV_FILE_CACHE_MAX = 64 # one entry per profile home in practice@@ -355,35 +353,30 @@ def load_env_file(env_path: Path) -> dict[str, str]:Invalid UTF-8 decodes as latin-1, exactly like ``env_loader._load_dotenv_with_fallback`` installs itinto ``os.environ``. Absent/unreadable → ``{}``.- Memoised per path on the open descriptor's stat identity (see the cache comment above). Always+ Memoised per path on the file's exact bytes (see the cache comment above). Alwaysreturns a fresh dict: callers mutate what they get back (``build_profile_secret_scope`` layersexternal secrets over it)."""key = str(env_path)try:with open(env_path, "rb") as handle:- fingerprint = file_signature(os.fstat(handle.fileno()))- with _ENV_FILE_CACHE_LOCK:- cached = _ENV_FILE_CACHE.get(key)- if cached is not None and cached[0] == fingerprint:- _ENV_FILE_CACHE.move_to_end(key)- return dict(cached[1])raw = handle.read()- # Same descriptor: a rewrite that landed between the fstat and the read is parsed but not- # stored under the pre-write fingerprint.- settled = file_signature(os.fstat(handle.fileno())) == fingerprintexcept OSError:# Gone or unreadable: drop any entry so a stale map cannot outlive the file.invalidate_env_file_cache(env_path)return {}- secrets = _parse_env_text(_decode_env_bytes(raw))- if settled:- with _ENV_FILE_CACHE_LOCK:- _ENV_FILE_CACHE[key] = (fingerprint, dict(secrets))+ with _ENV_FILE_CACHE_LOCK:+ cached = _ENV_FILE_CACHE.get(key)+ if cached is not None and cached[0] == raw:_ENV_FILE_CACHE.move_to_end(key)- while len(_ENV_FILE_CACHE) > _ENV_FILE_CACHE_MAX:- _ENV_FILE_CACHE.popitem(last=False)+ return dict(cached[1])+ secrets = _parse_env_text(_decode_env_bytes(raw))+ with _ENV_FILE_CACHE_LOCK:+ _ENV_FILE_CACHE[key] = (raw, dict(secrets))+ _ENV_FILE_CACHE.move_to_end(key)+ while len(_ENV_FILE_CACHE) > _ENV_FILE_CACHE_MAX:+ _ENV_FILE_CACHE.popitem(last=False)return secretsdiff --git a/tests/agent/test_secret_scope_env_cache.py b/tests/agent/test_secret_scope_env_cache.pyindex c0f77e46c35..a5a6f9924de 100644--- a/tests/agent/test_secret_scope_env_cache.py+++ b/tests/agent/test_secret_scope_env_cache.py@@ -77,3 +77,22 @@ def test_edits_and_deletion_are_seen(tmp_path, monkeypatch):env.write_bytes(b"KEY=ccc\n")assert ss.load_env_file(env) == {"KEY": "ccc"}assert len(calls) == 3+++def test_same_size_rewrite_inside_one_timestamp_tick_is_seen(tmp_path, monkeypatch):+ """On a kernel stamping mtime/ctime from the coarse (jiffy) clock, an in-place same-size rewrite+ in the same tick leaves (mtime, size, inode, ctime) unchanged. Simulate that clock: every stat+ identity collides, so only the bytes tell the versions apart (CI run 39146191939, where+ ``reload.env`` reported ``{'updated': 0}`` after ``.env`` went from ``one`` to ``two``).+ """+ monkeypatch.setattr(ss, "file_signature", lambda st: (0, 0, 0, 0), raising=False)+ env = tmp_path / ".env"+ env.write_bytes(b"TUI_RELOAD_PROBE=one\n")+ calls = _count_parses(monkeypatch)+ assert ss.load_env_file(env) == {"TUI_RELOAD_PROBE": "one"}+ assert ss.load_env_file(env) == {"TUI_RELOAD_PROBE": "one"}+ assert len(calls) == 1 # unchanged bytes still skip the parse++ env.write_bytes(b"TUI_RELOAD_PROBE=two\n")+ assert ss.load_env_file(env) == {"TUI_RELOAD_PROBE": "two"}+ assert len(calls) == 2diff --git a/tests/gateway/test_session_tui_ops.py b/tests/gateway/test_session_tui_ops.pyindex 59d180a38fa..c18511166f8 100644--- a/tests/gateway/test_session_tui_ops.py+++ b/tests/gateway/test_session_tui_ops.py@@ -1,6 +1,7 @@"""Ink's /save, /reload, /agents pause and /rollback on the shared owner (main's sidecar parity)."""import asyncioimport json+import osfrom pathlib import Pathimport pytest@@ -42,7 +43,13 @@ def test_reload_env_rereads_the_profile_dotenv_for_a_controlled_session(tmp_pathfirst = await rpc(ws, 'reload.env', session_id=sid)assert first.get('result', {}).get('updated', 0) >= 1, firstassert (await rpc(ws, 'reload.env', session_id=sid))['result'] == {'updated': 0}+ # Same size, same inode, pinned mtime: only the bytes distinguish the versions. On a+ # kernel with coarse ctime a same-tick rewrite also kept ctime, and a stat-keyed .env+ # memo served the old value (CI run 39146191939); the memo now keys on content.+ pinned = env.stat()await asyncio.to_thread(env.write_text, 'TUI_RELOAD_PROBE=two\n')+ os.utime(env, ns=(pinned.st_atime_ns, pinned.st_mtime_ns))+ assert env.stat().st_size == pinned.st_sizeassert (await rpc(ws, 'reload.env', session_id=sid))['result'] == {'updated': 1}# A bare frame never rewrites the shared owner's environment.for params, reason in [({}, 'invalid_params'), ({'session_id': sid, 'profile': 'foreign'}, 'profile_mismatch'),