Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(cleanup): revalidate idle teardown state

cyhe21073fd4425
diff --git a/core/handlers/session_handler.py b/core/handlers/session_handler.py
index 5fb1f337a..ca2093677 100644
--- a/core/handlers/session_handler.py
+++ b/core/handlers/session_handler.py
@@ -767,11 +767,24 @@ class SessionHandler(BaseHandler):
if now - last_activity >= idle_timeout:
expired.append((composite_key, now - last_activity))
+ evicted = 0
for composite_key, idle_for in expired:
+ current_last_activity = self.session_last_activity.get(composite_key)
+ if composite_key not in self.claude_sessions:
+ self.session_last_activity.pop(composite_key, None)
+ self.active_sessions.discard(composite_key)
+ continue
+ if composite_key in self.active_sessions:
+ continue
+ if current_last_activity is None:
+ continue
+ if time.monotonic() - current_last_activity < idle_timeout:
+ continue
logger.info("Evicting idle Claude session %s after %.1fs idle", composite_key, idle_for)
await self.cleanup_session(composite_key)
+ evicted += 1
- return len(expired)
+ return evicted
async def handle_session_error(self, composite_key: str, context: MessageContext, error: Exception):
"""Handle session-related errors"""
diff --git a/modules/agents/codex/agent.py b/modules/agents/codex/agent.py
index 10815a655..7da81af2a 100644
--- a/modules/agents/codex/agent.py
+++ b/modules/agents/codex/agent.py
@@ -305,7 +305,6 @@ class CodexAgent(BaseAgent):
self._transports.pop(cwd, None)
self._transport_last_activity.pop(cwd, None)
- self._transport_locks.pop(cwd, None)
for base_session_id in list(self._session_mgr.sessions_for_cwd(cwd)):
session_key = self._session_mgr.get_session_key(base_session_id)
diff --git a/tests/test_claude_cli_path.py b/tests/test_claude_cli_path.py
index 7c7e32830..f5324b071 100644
--- a/tests/test_claude_cli_path.py
+++ b/tests/test_claude_cli_path.py
@@ -312,3 +312,45 @@ def test_cleanup_session_swallows_cancelled_receiver_task(monkeypatch, tmp_path:
assert client.disconnects == 1
assert composite_key not in controller.receiver_tasks
assert composite_key not in controller.claude_sessions
+
+
+def test_evict_idle_sessions_rechecks_active_state_before_cleanup(monkeypatch, tmp_path: Path) -> None:
+ class _StubClaudeSDKClient:
+ def __init__(self, options):
+ self.disconnects = 0
+
+ async def connect(self) -> None:
+ return None
+
+ async def disconnect(self) -> None:
+ self.disconnects += 1
+
+ class _FlippingActiveSet(set):
+ def __init__(self, target_key: str):
+ super().__init__()
+ self.target_key = target_key
+ self._checks = 0
+
+ def __contains__(self, item):
+ if item == self.target_key:
+ self._checks += 1
+ return self._checks >= 2
+ return super().__contains__(item)
+
+ monkeypatch.setattr(session_handler_module, "ClaudeAgentOptions", _StubClaudeAgentOptions)
+ monkeypatch.setattr(session_handler_module, "ClaudeSDKClient", _StubClaudeSDKClient)
+ monkeypatch.setattr(session_handler_module.time, "monotonic", lambda: 1000.0)
+
+ controller = _Controller(tmp_path)
+ handler = SessionHandler(controller)
+ context = MessageContext(user_id="U123", channel_id="C123")
+ client = _run_session(handler, context)
+ composite_key = f"slack_C123:{tmp_path}"
+ handler.session_last_activity[composite_key] = 0.0
+ handler.active_sessions = _FlippingActiveSet(composite_key)
+
+ evicted = asyncio.run(handler.evict_idle_sessions(600))
+
+ assert evicted == 0
+ assert client.disconnects == 0
+ assert composite_key in controller.claude_sessions
diff --git a/tests/test_codex_agent.py b/tests/test_codex_agent.py
index efec92a50..82c32a4d0 100644
--- a/tests/test_codex_agent.py
+++ b/tests/test_codex_agent.py
@@ -318,6 +318,7 @@ class CodexAgentStopTests(unittest.IsolatedAsyncioTestCase):
self.assertEqual(cleared_turns, ["session-1"])
agent.sessions.clear_agent_session_mapping.assert_called_once_with("scope-1", "codex", "session-1")
self.assertEqual(agent._transports, {})
+ self.assertIn("/tmp/work", agent._transport_locks)
self.assertEqual(agent._transport_last_activity, {})
async def test_evict_idle_transports_keeps_active_codex_runtime(self):