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(codex): address round-3 review — active_requests race, stale turn guards

cyhff928a82c900
diff --git a/modules/agents/codex/agent.py b/modules/agents/codex/agent.py
index dd6768bd5..bd3fee7fd 100644
--- a/modules/agents/codex/agent.py
+++ b/modules/agents/codex/agent.py
@@ -73,9 +73,6 @@ class CodexAgent(BaseAgent):
await self._remove_ack_reaction(request)
return
- # Store the active request so notifications can route to the right context
- self._active_requests[request.base_session_id] = request
-
# Track settings_key for scoped clear
self._session_mgr.set_settings_key(request.base_session_id, request.settings_key)
@@ -86,6 +83,9 @@ class CodexAgent(BaseAgent):
self._session_locks[request.base_session_id] = asyncio.Lock()
async with self._session_locks[request.base_session_id]:
+ # Set active request INSIDE the lock to prevent race where
+ # request B overwrites while request A's turn is still completing
+ self._active_requests[request.base_session_id] = request
try:
# Get or create thread (with resume support)
thread_id = self._session_mgr.get_thread_id(request.base_session_id)
diff --git a/modules/agents/codex/event_handler.py b/modules/agents/codex/event_handler.py
index 7273c841a..1bcfe57b8 100644
--- a/modules/agents/codex/event_handler.py
+++ b/modules/agents/codex/event_handler.py
@@ -87,27 +87,38 @@ class CodexEventHandler:
# Only clean up active request if this turn is still the active one
# (avoids race where a new request already replaced it)
current_turn = self._agent._session_mgr.get_active_turn(request.base_session_id)
- if current_turn == turn_id or not current_turn:
+ is_current = current_turn == turn_id or not current_turn
+ if is_current:
self._agent._session_mgr.clear_active_turn(request.base_session_id)
self._agent._active_requests.pop(request.base_session_id, None)
if status == "interrupted":
# Turn was interrupted — discard pending text, no result message
self._pending_assistant.pop(turn_id, None)
- await self._agent._remove_ack_reaction(request)
+ if is_current:
+ await self._agent._remove_ack_reaction(request)
return
if status == "failed":
# Turn failed — emit error, discard pending text
self._pending_assistant.pop(turn_id, None)
- error_obj = turn_obj.get("error", {}) if isinstance(turn_obj, dict) else {}
- error_msg = error_obj.get("message", "Unknown error") if isinstance(error_obj, dict) else "Unknown error"
- await self._agent.controller.emit_agent_message(
- request.context,
- "notify",
- f"❌ Codex turn failed: {error_msg}",
- )
- await self._agent._remove_ack_reaction(request)
+ if is_current:
+ error_obj = turn_obj.get("error", {}) if isinstance(turn_obj, dict) else {}
+ error_msg = (
+ error_obj.get("message", "Unknown error") if isinstance(error_obj, dict) else "Unknown error"
+ )
+ await self._agent.controller.emit_agent_message(
+ request.context,
+ "notify",
+ f"❌ Codex turn failed: {error_msg}",
+ )
+ await self._agent._remove_ack_reaction(request)
+ return
+
+ # Stale turn completion — discard pending text, no side effects
+ if not is_current:
+ self._pending_assistant.pop(turn_id, None)
+ logger.debug("Ignoring stale turn/completed for turn %s (current: %s)", turn_id, current_turn)
return
pending = self._pending_assistant.pop(turn_id, None)
@@ -136,6 +147,12 @@ class CodexEventHandler:
item_type = item.get("type")
turn_id = params.get("turnId", "")
+ # Ignore items from stale turns to avoid leaking output into a new turn
+ current_turn = self._agent._session_mgr.get_active_turn(request.base_session_id)
+ if current_turn and turn_id and current_turn != turn_id:
+ logger.debug("Ignoring stale item/%s for turn %s (current: %s)", item_type, turn_id, current_turn)
+ return
+
if item_type == "agentMessage":
text = item.get("text", "")
if text: