fix(memory): add session-refresh guard to maybe_consolidate_by_tokens

When background consolidation runs with a stale session reference (captured
before AutoCompact replaced the session via compact_idle_session), it could
operate on outdated data. Now, after acquiring the per-session lock, the
method refreshes its session reference from SessionManager.get_or_create().
If the session was replaced, it swaps in the fresh reference before doing
any consolidation work.

This prevents a race where AutoCompact truncates an idle session while a
background maybe_consolidate_by_tokens call is in flight with the old
session object.
This commit is contained in:
chengyongru
2026-05-18 01:16:47 +08:00
committed by Xubin Ren
parent 48d35bd2d9
commit 888d54790d
2 changed files with 69 additions and 0 deletions
+5
View File
@@ -683,6 +683,11 @@ class Consolidator:
lock = self.get_lock(session.key)
async with lock:
# Refresh session reference: AutoCompact may have replaced it.
fresh = self.sessions.get_or_create(session.key)
if fresh is not session:
session = fresh
budget = self._input_token_budget
target = int(budget * self.consolidation_ratio)
last_summary = await self._consolidate_replay_overflow(