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.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
fix(codex): scope image snapshots to turns
cyh95f9ab471690
diff --git a/modules/agents/codex/agent.py b/modules/agents/codex/agent.pyindex 533882e73..0dc945aba 100644--- a/modules/agents/codex/agent.py+++ b/modules/agents/codex/agent.py@@ -568,14 +568,15 @@ class CodexAgent(BaseAgent):if effective_effort:turn_params["effort"] = effective_effort+ self._turn_registry.begin_turn_start(request, thread_id)+ event_handler = getattr(self, "_event_handler", None)snapshot_generated_images = getattr(- getattr(self, "_event_handler", None),+ event_handler,"snapshot_generated_images",None,)if callable(snapshot_generated_images):- snapshot_generated_images(thread_id)- self._turn_registry.begin_turn_start(request, thread_id)+ snapshot_generated_images(thread_id, request.base_session_id)resp = await transport.send_request("turn/start", turn_params)turn_id = resp.get("id", "")@@ -589,6 +590,9 @@ class CodexAgent(BaseAgent):raise RuntimeError("Codex turn/start returned no turn id")turn_state = self._turn_registry.finalize_turn_start_response(turn_id, request)+ bind_generated_image_snapshot = getattr(event_handler, "bind_generated_image_snapshot", None)+ if callable(bind_generated_image_snapshot):+ bind_generated_image_snapshot(thread_id, turn_id, request.base_session_id)logger.info("Codex turn started: thread=%s turn=%s session=%s state=%s",thread_id,diff --git a/modules/agents/codex/event_handler.py b/modules/agents/codex/event_handler.pyindex 02358ce45..9bb9e6e92 100644--- a/modules/agents/codex/event_handler.py+++ b/modules/agents/codex/event_handler.py@@ -27,12 +27,34 @@ class CodexEventHandler:def __init__(self, agent: Any) -> None:self._agent = agent- self._image_snapshots_by_thread: dict[str, set[Path]] = {}+ self._image_snapshots_by_turn: dict[str, tuple[str, set[Path]]] = {}+ self._pending_image_snapshots_by_session: dict[str, tuple[str, set[Path]]] = {}- def snapshot_generated_images(self, thread_id: str) -> None:+ def snapshot_generated_images(self, thread_id: str, base_session_id: str) -> None:"""Record generated images present before a Codex turn starts."""- if thread_id:- self._image_snapshots_by_thread[thread_id] = self._list_generated_images(thread_id)+ if thread_id and base_session_id:+ self._pending_image_snapshots_by_session[base_session_id] = (+ thread_id,+ self._list_generated_images(thread_id),+ )++ def bind_generated_image_snapshot(+ self,+ thread_id: str,+ turn_id: str,+ base_session_id: str,+ ) -> None:+ """Bind a pre-turn image snapshot to the concrete Codex turn id."""+ if not thread_id or not turn_id or not base_session_id:+ return+ pending = self._pending_image_snapshots_by_session.get(base_session_id)+ if not pending:+ return+ pending_thread_id, _ = pending+ if pending_thread_id != thread_id:+ return+ self._image_snapshots_by_turn[turn_id] = pending+ self._pending_image_snapshots_by_session.pop(base_session_id, None)# ------------------------------------------------------------------# Public entry point@@ -82,6 +104,7 @@ class CodexEventHandler:async def _on_turn_started(self, params: dict[str, Any], request: AgentRequest) -> None:turn_obj = params.get("turn", {})turn_id = turn_obj.get("id", "") if isinstance(turn_obj, dict) else ""+ self._claim_generated_image_snapshot(params, request)logger.info("Codex turn started: thread=%s turn=%s",params.get("threadId"),@@ -322,13 +345,15 @@ class CodexEventHandler:params: dict[str, Any],request: AgentRequest,) -> str | None:- thread_id = self._extract_thread_id(params)- if not thread_id:+ self._claim_generated_image_snapshot(params, request)+ turn_id = self._extract_turn_id(params)+ if not turn_id:return None- before = self._image_snapshots_by_thread.pop(thread_id, None)- if before is None:+ snapshot = self._image_snapshots_by_turn.pop(turn_id, None)+ if snapshot is None:return None+ thread_id, before = snapshotcurrent = self._list_generated_images(thread_id)generated = sorted(@@ -343,9 +368,16 @@ class CodexEventHandler:return f"{heading}\n\n{links}"def _clear_generated_image_snapshot(self, params: dict[str, Any]) -> None:- thread_id = self._extract_thread_id(params)- if thread_id:- self._image_snapshots_by_thread.pop(thread_id, None)+ turn_id = self._extract_turn_id(params)+ if turn_id:+ self._image_snapshots_by_turn.pop(turn_id, None)++ def _claim_generated_image_snapshot(self, params: dict[str, Any], request: AgentRequest) -> None:+ self.bind_generated_image_snapshot(+ self._extract_thread_id(params),+ self._extract_turn_id(params),+ request.base_session_id,+ )def _list_generated_images(self, thread_id: str) -> set[Path]:thread_dir = self._generated_images_dir(thread_id)@@ -379,6 +411,14 @@ class CodexEventHandler:thread_id = thread_obj.get("id", "")return thread_id+ def _extract_turn_id(self, params: dict[str, Any]) -> str:+ turn_id = params.get("turnId", "")+ if not turn_id:+ turn_obj = params.get("turn")+ if isinstance(turn_obj, dict):+ turn_id = turn_obj.get("id", "")+ return turn_id+def _t(self, key: str, request: AgentRequest) -> str:controller = getattr(self._agent, "controller", None)translate = getattr(controller, "_t", None)diff --git a/tests/test_codex_event_handler.py b/tests/test_codex_event_handler.pyindex a75a26fb7..4aae454bd 100644--- a/tests/test_codex_event_handler.py+++ b/tests/test_codex_event_handler.py@@ -268,7 +268,8 @@ class CodexEventHandlerTests(unittest.IsolatedAsyncioTestCase):thread_dir.mkdir(parents=True)old_image = thread_dir / "old.png"old_image.write_bytes(b"old")- handler.snapshot_generated_images("thread-1")+ handler.snapshot_generated_images("thread-1", "session-1")+ handler.bind_generated_image_snapshot("thread-1", "turn-1", "session-1")new_image = thread_dir / "new.png"new_image.write_bytes(b"new")@@ -287,6 +288,48 @@ class CodexEventHandlerTests(unittest.IsolatedAsyncioTestCase):assert f"})" in result_textassert str(old_image.resolve()) not in result_text+ async def test_stale_interrupted_turn_does_not_clear_new_turn_image_snapshot(self):+ agent = _StubAgent()+ handler = CodexEventHandler(agent)+ request = SimpleNamespace(base_session_id="session-1", context=object(), started_at=0)+ agent._turn_registry.register_turn("turn-1", request)++ with tempfile.TemporaryDirectory() as tmpdir, patch.dict(os.environ, {"CODEX_HOME": tmpdir}):+ thread_dir = Path(tmpdir) / "generated_images" / "thread-1"+ thread_dir.mkdir(parents=True)+ before_second_turn = thread_dir / "before-second-turn.png"+ before_second_turn.write_bytes(b"old")++ handler.snapshot_generated_images("thread-1", "session-1")+ handler.bind_generated_image_snapshot("thread-1", "turn-1", "session-1")++ agent._turn_registry.register_turn("turn-2", request)+ handler.snapshot_generated_images("thread-1", "session-1")++ await handler._on_turn_completed(+ {+ "threadId": "thread-1",+ "turn": {"id": "turn-1", "status": "interrupted"},+ },+ request,+ )++ new_image = thread_dir / "new-for-turn-2.png"+ new_image.write_bytes(b"new")+ handler.bind_generated_image_snapshot("thread-1", "turn-2", "session-1")++ await handler._on_turn_completed(+ {+ "threadId": "thread-1",+ "turn": {"id": "turn-2", "status": "completed"},+ },+ request,+ )++ result_text = agent.emit_result_message.await_args.args[1]+ assert f"})" in result_text+ assert str(before_second_turn.resolve()) not in result_text+async def test_empty_success_result_does_not_guess_without_image_snapshot(self):agent = _StubAgent()handler = CodexEventHandler(agent)