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): handle resumed image turns

cyh3b03ebe08f53
diff --git a/modules/agents/codex/agent.py b/modules/agents/codex/agent.py
index 2f30de4d7..e28eb110e 100644
--- a/modules/agents/codex/agent.py
+++ b/modules/agents/codex/agent.py
@@ -443,7 +443,6 @@ class CodexAgent(BaseAgent):
fallback_platform=platform,
)
)
- instruction_parts.append(_CODEX_GENERATED_IMAGE_PROMPT)
if instruction_parts:
params["developerInstructions"] = "\n\n".join(part for part in instruction_parts if part)
@@ -617,7 +616,7 @@ class CodexAgent(BaseAgent):
items: list[Dict[str, Any]] = []
# Text input
- message = request.message
+ message = self._build_turn_message(request.message)
if request.files:
# Append file info like Claude agent does
file_lines = ["", "[User Attachments]"]
@@ -644,6 +643,15 @@ class CodexAgent(BaseAgent):
return items
+ def _build_turn_message(self, message: str) -> str:
+ if not message:
+ return message
+ controller = getattr(self, "controller", None)
+ config = getattr(controller, "config", None)
+ if not getattr(config, "reply_enhancements", True):
+ return message
+ return f"{_CODEX_GENERATED_IMAGE_PROMPT}\n\n{message}"
+
# ------------------------------------------------------------------
# Callback handlers (wired to transport)
# ------------------------------------------------------------------
diff --git a/modules/agents/codex/event_handler.py b/modules/agents/codex/event_handler.py
index b7c3489e3..e448ff4a1 100644
--- a/modules/agents/codex/event_handler.py
+++ b/modules/agents/codex/event_handler.py
@@ -51,6 +51,9 @@ class CodexEventHandler:
pending = self._pending_image_snapshots_by_session.get(base_session_id)
if not pending:
return
+ is_active_turn = getattr(self._agent._turn_registry, "is_active_turn", None)
+ if callable(is_active_turn) and not is_active_turn(turn_id):
+ return
pending_thread_id, _ = pending
if pending_thread_id != thread_id:
return
diff --git a/tests/test_codex_agent.py b/tests/test_codex_agent.py
index 13e08067c..726db113f 100644
--- a/tests/test_codex_agent.py
+++ b/tests/test_codex_agent.py
@@ -675,7 +675,7 @@ class CodexAgentPayloadTests(unittest.IsolatedAsyncioTestCase):
},
)
- async def test_start_thread_adds_codex_generated_image_prompt_only_to_codex_developer_instructions(self):
+ async def test_start_thread_does_not_add_codex_generated_image_prompt_to_thread_instructions(self):
agent = object.__new__(CodexAgent)
agent.controller = SimpleNamespace(config=SimpleNamespace(platform="slack", reply_enhancements=True))
agent.settings_manager = SimpleNamespace(get_channel_settings=lambda session_key: None)
@@ -702,8 +702,18 @@ class CodexAgentPayloadTests(unittest.IsolatedAsyncioTestCase):
await agent._start_thread(transport, request)
params = transport.send_request.await_args.args[1]
- self.assertIn("If you generate an image with Codex", params["developerInstructions"])
- self.assertIn("![generated image](file:///absolute/path/to/image.png)", params["developerInstructions"])
+ self.assertNotIn("If you generate an image with Codex", params["developerInstructions"])
+
+ def test_build_input_adds_codex_generated_image_prompt_to_each_turn(self):
+ agent = object.__new__(CodexAgent)
+ agent.controller = SimpleNamespace(config=SimpleNamespace(reply_enhancements=True))
+ request = SimpleNamespace(message="hello", files=None)
+
+ items = agent._build_input(request)
+
+ self.assertTrue(items[0]["text"].startswith("If you generate an image with Codex"))
+ self.assertIn("![generated image](file:///absolute/path/to/image.png)", items[0]["text"])
+ self.assertTrue(items[0]["text"].endswith("hello"))
async def test_start_turn_uses_sandbox_policy_object(self):
agent = object.__new__(CodexAgent)
diff --git a/tests/test_codex_event_handler.py b/tests/test_codex_event_handler.py
index ffe02776a..b4ef43c2b 100644
--- a/tests/test_codex_event_handler.py
+++ b/tests/test_codex_event_handler.py
@@ -75,6 +75,12 @@ class _StubTurnRegistry:
return False
return state.visible_to_user and self._active_turns.get(state.request.base_session_id) == turn_id
+ def is_active_turn(self, turn_id: str) -> bool:
+ state = self._turns.get(turn_id)
+ if not state:
+ return False
+ return self._active_turns.get(state.request.base_session_id) == turn_id
+
class _StubAgent:
def __init__(self):
@@ -358,6 +364,42 @@ class CodexEventHandlerTests(unittest.IsolatedAsyncioTestCase):
assert f"![generated image](file://{new_image.resolve()})" in result_text
assert str(before_second_turn.resolve()) not in result_text
+ async def test_stale_turn_started_does_not_steal_pending_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)
+ handler.clear_pending("turn-1")
+ agent._turn_registry.register_turn("turn-2", 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)
+ handler.snapshot_generated_images("thread-1", "session-1")
+
+ await handler._on_turn_started(
+ {
+ "threadId": "thread-1",
+ "turn": {"id": "turn-1"},
+ },
+ 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"![generated image](file://{new_image.resolve()})" in result_text
+
async def test_empty_success_result_does_not_guess_without_image_snapshot(self):
agent = _StubAgent()
handler = CodexEventHandler(agent)