Fix: Enforce session capacity on restore and prevent session-creation race

#41
No description provided.
omshrivastava changed pull request title from Fix Session Capacity Bug: Prevent Unlimited Session Restoration to 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.

omshrivastava changed pull request status to open
Cannot merge
This branch has merge conflicts in the following files:
  • backend/session_manager.py

Sign up or log in to comment