Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
fix(chat): retain native generated images as chat artifacts
rubenluetke10-beep4333dea07117
diff --git a/jarvis/agent_chat/generated_images.py b/jarvis/agent_chat/generated_images.pynew file mode 100644index 0000000000..a74ad48e1c--- /dev/null+++ b/jarvis/agent_chat/generated_images.py@@ -0,0 +1,95 @@+"""Retain native CLI images as ordinary, safely served chat artifacts.++Some CLI JSON streams omit native image-generation extension items even though+the CLI saves their PNGs. Import only fresh files from that exact conversation's+generated-images directory. The chat event remains ordinary Markdown: no base64+in the event log, no private local paths in URLs, and no new serving endpoint.+"""++from __future__ import annotations++import hashlib+import os+import tempfile+from dataclasses import dataclass+from datetime import UTC, datetime+from pathlib import Path+from uuid import UUID++from jarvis.missions.standalone_run import write_marker++MAX_IMAGE_BYTES = 16 * 1024 * 1024+PNG_SIGNATURE = b"\x89PNG\r\n\x1a\n"+++@dataclass(frozen=True)+class GeneratedImage:+ message_id: str+ markdown: str+ path: Path+++def collect_generated_images(+ *, codex_home: Path, thread_id: str, since: float, outputs_root: Path+) -> list[GeneratedImage]:+ """Archive this turn's completed PNG files, leaving source files untouched.++ UUID validation and resolved-path checks keep both thread and file symlinks+ from importing another conversation's data. Missing image support is a+ normal empty result; unreadable or invalid generated files raise so the+ runner can show a visible delivery error instead of claiming success.+ """+ thread = str(UUID(thread_id))+ home = codex_home.expanduser().resolve()+ images_root = home / "generated_images"+ source = images_root / thread+ if not source.exists():+ return []+ if images_root.is_symlink() or source.is_symlink() or source.resolve() != source:+ raise ValueError("Generated image directory must belong to the current conversation")++ candidates: list[tuple[float, Path]] = []+ for image in source.iterdir():+ if image.suffix.lower() != ".png":+ continue+ if image.is_symlink() or image.resolve().parent != source:+ raise ValueError("Generated image must stay inside its conversation directory")+ if image.is_file() and (modified := image.stat().st_mtime) >= since:+ candidates.append((modified, image))++ delivered: list[GeneratedImage] = []+ for modified, image in sorted(candidates):+ with image.open("rb") as handle:+ data = handle.read(MAX_IMAGE_BYTES + 1)+ if len(data) > MAX_IMAGE_BYTES or not data.startswith(PNG_SIGNATURE):+ raise ValueError("Generated image is not a supported PNG or exceeds 16 MiB")+ # Stable across recovery/replay, distinct even when two threads produce+ # identical pixels. Never use a provider-controlled filename as a URL.+ identity = hashlib.sha256(thread.encode() + image.name.encode() + data).hexdigest()[:20]+ stamp = datetime.fromtimestamp(modified, UTC).strftime("%Y%m%dT%H%M%S")+ slug = f"{stamp}__generated-image__{identity[:16]}"+ run_dir = outputs_root / slug+ files = run_dir / "tasks" / "chat" / "artifacts" / "files"+ if run_dir.resolve() != outputs_root.resolve() / slug or not files.resolve().is_relative_to(+ run_dir.resolve()+ ):+ raise ValueError("Generated image destination must stay inside its artifact directory")+ files.mkdir(parents=True, exist_ok=True)+ target = files / "generated-image.png"+ with tempfile.TemporaryDirectory(prefix=".image-", dir=run_dir) as scratch:+ staged = Path(scratch) / "image.png"+ staged.write_bytes(data)+ os.replace(staged, target)+ write_marker(run_dir, kind="chat_image", title="Generated image")+ url = f"/api/outputs/{slug}/files/tasks/chat/artifacts/files/generated-image.png/download"+ delivered.append(+ GeneratedImage(+ message_id=f"generated-image-{identity}",+ markdown=(+ f"\n\n"+ f"[generated-image.png]({url})"+ ),+ path=target,+ )+ )+ return delivereddiff --git a/jarvis/agent_chat/runner_cli.py b/jarvis/agent_chat/runner_cli.pyindex 0665d12374..1707467b19 100644--- a/jarvis/agent_chat/runner_cli.py+++ b/jarvis/agent_chat/runner_cli.py@@ -2670,9 +2670,59 @@ async def _run_cli_once())+ if plan.shape == "codex" and vendor_session:+ delivered = await _deliver_codex_images(handle, plan.env, vendor_session, started_at)+ if not delivered and status == "done":+ status = "error"+ error_text = "A generated image could not be added to the chat artifacts."+return _Outcome(status, error_text, usage, cost_usd, vendor_session)+async def _deliver_codex_images(+ handle: TurnHandle, env: dict[str, str], thread_id: str, since: float+) -> bool:+ """Recover native visual results omitted by the CLI's text event stream."""+ from jarvis.agent_chat.generated_images import collect_generated_images+ from jarvis.core.paths import repo_root+ from jarvis.missions.isolation.worktree import resolve_outputs_root++ raw_home = env.get("CODEX_HOME", "")+ home = Path(raw_home) if raw_home else Path.home() / ".codex"+ try:+ images = await asyncio.to_thread(+ collect_generated_images,+ codex_home=home,+ thread_id=thread_id,+ since=since,+ outputs_root=resolve_outputs_root(repo_root()),+ )+ except (OSError, ValueError):+ log.warning("agent chat: generated image delivery failed", exc_info=True)+ await handle.emit(+ make_event(+ "error",+ {+ "turn_id": handle.turn_id,+ "message": "A generated image could not be added to the chat artifacts.",+ },+ )+ )+ return False+ for image in images:+ await handle.emit(+ make_event(+ "assistant_text",+ {+ "turn_id": handle.turn_id,+ "message_id": image.message_id,+ "text": image.markdown,+ },+ )+ )+ return True++def _kill(proc: asyncio.subprocess.Process) -> None:if proc.returncode is not None:returndiff --git a/tests/unit/agent_chat/test_generated_images.py b/tests/unit/agent_chat/test_generated_images.pynew file mode 100644index 0000000000..e1d1ef7dbb--- /dev/null+++ b/tests/unit/agent_chat/test_generated_images.py@@ -0,0 +1,156 @@+"""Native images omitted by the CLI stream still reach the chat and archive."""++from __future__ import annotations++import base64+import os+import sys+from pathlib import Path++import pytest+from fastapi import FastAPI+from fastapi.testclient import TestClient++from jarvis.agent_chat import generated_images as images+from jarvis.agent_chat import runner_cli as rc+from jarvis.missions.standalone_run import read_marker+from jarvis.ui.web.outputs_routes import router+from tests.unit.agent_chat.test_ide_cli_seats import _turn++THREAD = "a2d21c42-3c21-4bde-b602-a8a3dc99440a"+OTHER = "a2d21c42-3c21-4bde-b602-a8a3dc99440b"+PNG = base64.b64decode(+ "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aRZkAAAAASUVORK5CYII="+)+++def saved(home: Path, name: str = "image.png", *, thread: str = THREAD, at: float = 200) -> Path:+ file = home / "generated_images" / thread / name+ file.parent.mkdir(parents=True, exist_ok=True)+ file.write_bytes(PNG)+ os.utime(file, (at, at))+ return file+++def collect(home: Path, root: Path, since: float = 100) -> list[images.GeneratedImage]:+ return images.collect_generated_images(+ codex_home=home, thread_id=THREAD, since=since, outputs_root=root+ )+++def test_imports_only_this_turn_and_thread(tmp_path: Path) -> None:+ home, root = tmp_path / "seat", tmp_path / "outputs"+ current = saved(home)+ saved(home, "old.png", at=50)+ saved(home, thread=OTHER)+ result = collect(home, root)+ assert len(result) == 1+ assert result[0].path.read_bytes() == current.read_bytes() == PNG+ assert str(home) not in result[0].markdown+ assert "base64" not in result[0].markdown+ assert read_marker(result[0].path.parents[4])["kind"] == "chat_image"+++def test_recovery_is_idempotent_and_keeps_the_original(tmp_path: Path) -> None:+ home, root = tmp_path / "seat", tmp_path / "outputs"+ original = saved(home)+ first = collect(home, root)+ assert collect(home, root) == first+ assert len(list(root.iterdir())) == 1+ assert original.read_bytes() == PNG+++def test_missing_image_capability_is_an_empty_result(tmp_path: Path) -> None:+ assert collect(tmp_path / "seat", tmp_path / "outputs") == []+ assert not (tmp_path / "outputs").exists()+++def test_refuses_path_traversal_thread(tmp_path: Path) -> None:+ with pytest.raises(ValueError):+ images.collect_generated_images(+ codex_home=tmp_path, thread_id="../../other", since=0, outputs_root=tmp_path / "out"+ )++[email protected]("data", [b"<html>not an image</html>", b"not png"])+def test_refuses_non_image_payload(tmp_path: Path, data: bytes) -> None:+ file = saved(tmp_path / "seat")+ file.write_bytes(data)+ with pytest.raises(ValueError, match="supported PNG"):+ collect(tmp_path / "seat", tmp_path / "out")+++def test_bounds_image_size(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:+ saved(tmp_path / "seat")+ monkeypatch.setattr(images, "MAX_IMAGE_BYTES", 8)+ with pytest.raises(ValueError, match="exceeds"):+ collect(tmp_path / "seat", tmp_path / "out")+++def test_rejects_cross_thread_file_links(tmp_path: Path) -> None:+ home = tmp_path / "seat"+ foreign = saved(home, thread=OTHER)+ ours = home / "generated_images" / THREAD+ ours.mkdir()+ try:+ (ours / "foreign.png").symlink_to(foreign)+ except OSError:+ pytest.skip("Creating symlinks is unavailable for this test account")+ with pytest.raises(ValueError, match="conversation"):+ collect(home, tmp_path / "out")+++def test_existing_download_route_serves_the_png_and_rejects_escape(tmp_path: Path) -> None:+ home, root = tmp_path / "seat", tmp_path / "outputs"+ saved(home)+ result = collect(home, root)[0]+ url = result.markdown.split("](", 1)[1].split(")", 1)[0]+ app = FastAPI()+ app.state.outputs_root = root+ app.include_router(router)+ with TestClient(app) as client:+ response = client.get(url)+ assert response.status_code == 200+ assert response.content == PNG+ assert response.headers["content-type"] == "image/png"+ assert response.headers["x-content-type-options"] == "nosniff"+ assert client.get(url.replace("tasks/chat/artifacts/files", "other")).status_code == 404++[email protected]("content", [PNG, b"invalid image", None])+def test_cli_pump_delivers_native_image_or_reports_delivery_failure(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, content: bytes | None+) -> None:+ home, root = tmp_path / "seat", tmp_path / "outputs"+ monkeypatch.setenv("JARVIS_ISOLATION_ROOT", str(root))+ target = home / "generated_images" / THREAD / "native.png"+ started = {"type": "thread.started", "thread_id": THREAD}+ # An actual lightweight subprocess writes the file without an image event,+ # reproducing the native CLI's dropped extension item.+ write_image = (+ f"p=Path({str(target)!r}); p.parent.mkdir(parents=True); p.write_bytes({content!r}); "+ if content is not None+ else ""+ )+ body = (+ "import json; from pathlib import Path; "+ + write_image+ + f"print(json.dumps({started!r})); "+ "print(json.dumps({'type':'turn.completed','usage':{}}))"+ )+ plan = rc.CliPlan(+ [sys.executable, "-c", body], {**os.environ, "CODEX_HOME": str(home)}, None, "codex", None+ )+ vendor, events = _turn(monkeypatch, tmp_path, "codex-cli", plan, "openai-codex")+ assert vendor == THREAD+ if content is None:+ assert [event["kind"] for event in events] == ["turn_finished"]+ assert events[-1]["payload"]["status"] == "done"+ return+ if content != PNG:+ assert [event["kind"] for event in events] == ["error", "turn_finished"]+ assert events[-1]["payload"]["status"] == "error"+ return+ assert [event["kind"] for event in events] == ["assistant_text", "turn_finished"]+ assert "![generated-image.png](/api/outputs/" in events[0]["payload"]["text"]+ assert events[-1]["payload"]["status"] == "done"