Skip to content

The agent that grows with you

fix(gateway): key the profiles.list stat caches on ctime and inode too

Tekniumf607bd2ab410
diff --git a/gateway/session_group_controls.py b/gateway/session_group_controls.py
index 2d7f1940f2d..e866013de0b 100644
--- a/gateway/session_group_controls.py
+++ b/gateway/session_group_controls.py
@@ -448,11 +448,17 @@ def _profiles(authority, actor, home, params, siblings=()):
# The Bots pane polls ``profiles.list`` every 5 s. Re-listing ``profiles/`` and re-parsing every
# sibling ``profile.yaml`` each time cost ~60 ms at 50 profiles, so both are cached by stat:
-# the directory listing by the mtimes of ``profiles/`` and each entry (a new profile, its
+# the directory listing by the stat keys of ``profiles/`` and each entry (a new profile, its
# identity marker or a tombstone each bump one of them), and a parsed ``profile.yaml`` by
-# (path, mtime_ns, size).
+# (path, stat key). A stat key carries ctime and the inode beside mtime and size: mtime alone
+# misses a change inside one coarse timestamp tick (FAT/SMB/HFS+) and can be set back with
+# utime, while ctime moves on every metadata or content change and cannot be set by callers.
_NAMED_DIRS_CACHE: dict[str, tuple[tuple, list]] = {}
-_PROFILE_META_CACHE: dict[str, tuple[tuple[int, int], object]] = {}
+_PROFILE_META_CACHE: dict[str, tuple[tuple, object]] = {}
+
+
+def _stat_key(stat):
+ return (stat.st_mtime_ns, stat.st_ctime_ns, stat.st_size, stat.st_ino)
def _named_profile_dirs():
@@ -460,8 +466,8 @@ def _named_profile_dirs():
profiles_root = _get_profiles_root()
try:
with os.scandir(profiles_root) as entries:
- signature = (os.stat(profiles_root).st_mtime_ns,
- tuple(sorted((entry.name, entry.stat().st_mtime_ns) for entry in entries)))
+ signature = (_stat_key(os.stat(profiles_root)),
+ tuple(sorted((entry.name, _stat_key(entry.stat())) for entry in entries)))
except OSError:
signature = None
cached = _NAMED_DIRS_CACHE.get(str(profiles_root))
@@ -486,7 +492,7 @@ def _discovered_profile_meta(name, meta_path):
stat = meta_path.stat()
except OSError:
return {}
- key = (stat.st_mtime_ns, stat.st_size)
+ key = _stat_key(stat)
cached = _PROFILE_META_CACHE.get(str(meta_path))
if cached and cached[0] == key:
return cached[1]
diff --git a/tests/gateway/test_session_group_controls.py b/tests/gateway/test_session_group_controls.py
index a91a358299d..a84100a275c 100644
--- a/tests/gateway/test_session_group_controls.py
+++ b/tests/gateway/test_session_group_controls.py
@@ -249,3 +249,38 @@ def test_profiles_list_reuses_its_disk_scan_until_a_profile_changes(tmp_path, mo
rows = {row['name']: row for row in _list_profiles(home)['result']['profiles']}
assert rows['writer']['display_name'] == 'Renamed Writer'
assert 'scout' in rows
+
+
+def test_profiles_list_cache_sees_changes_inside_one_mtime_tick(tmp_path, monkeypatch):
+ """Independent review on #140972: the stat caches keyed only on mtime (and size), so a change
+ inside one coarse mtime tick (FAT/SMB/HFS+, or two writes in one kernel timestamp tick) was
+ served stale. Pinning mtimes back with os.utime emulates that tick; ctime still moves."""
+ import os
+
+ from gateway import session_group_controls as g
+ home = _roster_home(tmp_path, monkeypatch)
+ root = home / 'profiles'
+ tick = 1_700_000_000_000_000_000
+
+ def pin(*paths):
+ for path in paths:
+ os.utime(path, ns=(tick, tick))
+
+ lone = root / 'lone'
+ lone.mkdir()
+ (lone / 'profile.yaml').write_text('display_name: Lone\n')
+ pin(root, lone)
+ assert 'lone' in [p.name for p in g._named_profile_dirs()]
+ # Deleting the profile's only identity file, inside the same tick, drops it from the roster.
+ (lone / 'profile.yaml').unlink()
+ pin(root, lone)
+ assert 'lone' not in [p.name for p in g._named_profile_dirs()]
+
+ # An edit that keeps both size and mtime serves the new content.
+ meta = root / 'writer' / 'profile.yaml'
+ meta.write_text('display_name: AAAA\n')
+ pin(meta)
+ assert g._discovered_profile_meta('writer', meta) == {'display_name': 'AAAA'}
+ meta.write_text('display_name: BBBB\n')
+ pin(meta)
+ assert g._discovered_profile_meta('writer', meta) == {'display_name': 'BBBB'}