Spaces:
Running on CPU Upgrade
Fix: Enforce session capacity on restore and prevent session-creation race
Summary: Prevent unlimited session restoration from persistence and racey session creation that could exceed MAX_SESSIONS. Also unload long-idle sessions to free capacity.
Root cause:ensure_session_loaded() restored persisted sessions without capacity checks;create_session() checked capacity but did not reserve a slot while performing blocking setup, allowing concurrent creators to slip past the limit.
Changes:
Added capacity check to ensure_session_loaded();
reserve session slot inside create_session() under self._lock (placeholder AgentSession) and perform blocking I/O afterwards;
update _start_agent_session() to replace placeholders;
add last_access tracking and background _unload_inactive_sessions_loop() to persist+unload sessions idle >24h.
Notes for reviewer:
I avoided changing create_session() semantics beyond reservation. Consider adding a metric for denied restores in a follow-up.