Skip to content

The agent that grows with you

fix(tui): profile roster never memoises a racily fresh session store

Maciej Lisiewski7dafcf895ae8
diff --git a/tests/tui_gateway/test_profiles_list_session_field_cache.py b/tests/tui_gateway/test_profiles_list_session_field_cache.py
index 3e224319a1f..0ba9d37e0d0 100644
--- a/tests/tui_gateway/test_profiles_list_session_field_cache.py
+++ b/tests/tui_gateway/test_profiles_list_session_field_cache.py
@@ -7,6 +7,9 @@ roster silently paints a stale preview.
"""
from __future__ import annotations
+import os
+import time
+
import pytest
import tui_gateway.server as srv
@@ -74,3 +77,35 @@ def test_a_profile_without_a_store_is_not_memoised_and_picks_one_up(home):
_seed(bare, "20260920_000003_c", "now it exists")
assert _row("bare")["last_session"]["title"] == "now it exists"
+
+def _store_files(profile_dir, mtime_ns: int) -> None:
+ for name in ("state.db", "state.db-wal"):
+ (profile_dir / name).write_bytes(b"\0" * 4096)
+ os.utime(profile_dir / name, ns=(mtime_ns, mtime_ns))
+
+def test_a_same_tick_same_size_wal_rewrite_is_not_masked(tmp_path):
+ """A commit in the mtime tick the memo was filled in, rewriting the WAL in place at the same
+ size (after a restart checkpoint), leaves every ``(mtime_ns, size)`` unchanged: a store that
+ fresh must not be memoised."""
+ now = time.time_ns()
+ _store_files(tmp_path, now)
+ assert cache.cached_session_fields(tmp_path, lambda: {"session_count": 1}) == {"session_count": 1}
+
+ (tmp_path / "state.db-wal").write_bytes(b"\1" * 4096)
+ os.utime(tmp_path / "state.db-wal", ns=(now, now))
+
+ assert cache.cached_session_fields(tmp_path, lambda: {"session_count": 2}) == {"session_count": 2}
+
+def test_a_settled_store_is_served_from_the_memo(tmp_path):
+ _store_files(tmp_path, time.time_ns() - 60 * 1_000_000_000)
+ calls = []
+
+ def compute():
+ calls.append(1)
+ return {"session_count": len(calls)}
+
+ first = cache.cached_session_fields(tmp_path, compute)
+ second = cache.cached_session_fields(tmp_path, compute)
+
+ assert second == first
+ assert len(calls) == 1
diff --git a/tui_gateway/profile_roster_cache.py b/tui_gateway/profile_roster_cache.py
index 557867487b5..403b5a1ec31 100644
--- a/tui_gateway/profile_roster_cache.py
+++ b/tui_gateway/profile_roster_cache.py
@@ -30,6 +30,8 @@ import copy
from pathlib import Path
from typing import Any, Callable, Optional
+from utils import mtime_is_racy
+
# Each keyed by resolved profile path. One entry per profile the roster has painted; a removed
# profile leaves one stale entry, so a memo is dropped whole once it grows past any plausible fleet.
_SESSION_CACHE: dict[str, tuple[tuple, dict]] = {}
@@ -65,13 +67,17 @@ def _cached(cache: dict, key: str, signature: Optional[tuple], compute: Callable
def store_signature(profile_path: str | Path) -> Optional[tuple]:
"""``(name, mtime_ns, size)`` per session-store file, or None when the profile has no store.
- The pair the change watcher already trusts for ``sessions.changed``.
+ The pair the change watcher already trusts for ``sessions.changed``. None too while a file's
+ mtime is fresh enough that a same-tick, same-size WAL rewrite could still hide behind it.
"""
base = Path(profile_path)
parts = [p for p in (_file_parts(base / name) for name in _STORE_FILES) if p is not None]
# Our own read-only open creates an EMPTY -wal sidecar on a store that never had one; it
# carries no frames, so it must not read as "the store moved" on the very next poll.
- return tuple(p for p in parts if not (p[0].endswith("-wal") and p[2] == 0)) or None
+ parts = [p for p in parts if not (p[0].endswith("-wal") and p[2] == 0)]
+ if any(mtime_is_racy(p[1]) for p in parts):
+ return None
+ return tuple(parts) or None
def profile_yaml_signature(profile_dir: str | Path) -> Optional[tuple]: