mirror of
https://github.com/nesquena/hermes-webui.git
synced 2026-05-28 20:50:16 +00:00
Make session index pruning explicit
This commit is contained in:
+31
-1
@@ -244,7 +244,37 @@ def prune_session_from_index(session_id: str) -> None:
|
||||
sid = str(session_id or "")
|
||||
if not sid or not SESSION_INDEX_FILE.exists():
|
||||
return
|
||||
_write_session_index(updates=[])
|
||||
_tmp = SESSION_INDEX_FILE.with_suffix(f'.tmp.{os.getpid()}.{threading.current_thread().ident}')
|
||||
|
||||
_fallback = False
|
||||
with _INDEX_WRITE_LOCK:
|
||||
try:
|
||||
with LOCK:
|
||||
existing = json.loads(SESSION_INDEX_FILE.read_text(encoding='utf-8'))
|
||||
if not isinstance(existing, list):
|
||||
raise ValueError("session index must be a list")
|
||||
pruned = [e for e in existing if e.get('session_id') != sid]
|
||||
if len(pruned) == len(existing):
|
||||
return
|
||||
_payload = json.dumps(pruned, ensure_ascii=False, indent=2)
|
||||
|
||||
try:
|
||||
with open(_tmp, 'w', encoding='utf-8') as f:
|
||||
f.write(_payload)
|
||||
f.flush()
|
||||
os.fsync(f.fileno())
|
||||
os.replace(_tmp, SESSION_INDEX_FILE)
|
||||
except Exception:
|
||||
try:
|
||||
_tmp.unlink(missing_ok=True)
|
||||
except Exception:
|
||||
pass
|
||||
raise
|
||||
except Exception:
|
||||
_fallback = True
|
||||
|
||||
if _fallback:
|
||||
_write_session_index(updates=None)
|
||||
|
||||
|
||||
def _active_stream_ids():
|
||||
|
||||
@@ -325,10 +325,8 @@ def test_server_delete_prunes_session_index(cleanup_test_sessions):
|
||||
)
|
||||
if delete_idx >= 0:
|
||||
delete_block = text[delete_idx:delete_idx+1800]
|
||||
assert "prune_session_from_index" in delete_block, \
|
||||
assert "prune_session_from_index(sid)" in delete_block, \
|
||||
f"{label} session/delete must prune SESSION_INDEX_FILE"
|
||||
assert "SESSION_INDEX_FILE.unlink" not in delete_block, \
|
||||
f"{label} session/delete must not discard the whole session index"
|
||||
return
|
||||
assert False, "session/delete handler not found in server.py or api/routes.py"
|
||||
|
||||
|
||||
@@ -83,20 +83,21 @@ def test_compact_exposes_last_message_at_from_message_timestamp():
|
||||
assert compact["last_message_at"] == 200.0
|
||||
|
||||
|
||||
def test_prune_session_from_index_removes_only_deleted_row():
|
||||
def test_prune_session_from_index_removes_requested_row_only():
|
||||
index_file = models.SESSION_INDEX_FILE
|
||||
s_a = _make_session("sess_a", "A", updated_at=100)
|
||||
s_b = _make_session("sess_b", "B", updated_at=200)
|
||||
s_a.save()
|
||||
s_b.save()
|
||||
|
||||
s_a.path.unlink()
|
||||
prune_session_from_index("sess_a")
|
||||
|
||||
index = _read_index(index_file)
|
||||
ids = [entry["session_id"] for entry in index]
|
||||
assert ids == ["sess_b"]
|
||||
assert index_file.exists()
|
||||
assert s_a.path.exists()
|
||||
assert s_b.path.exists()
|
||||
|
||||
|
||||
def test_all_sessions_backfills_last_message_at_for_legacy_index_rows():
|
||||
|
||||
Reference in New Issue
Block a user