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:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user