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
feat(agentic-ide): read the agent's conversation from the CLI's own record
rubenluetke10-beepb18240971e02
diff --git a/jarvis/agentic_ide/agent_transcript.py b/jarvis/agentic_ide/agent_transcript.pynew file mode 100644index 0000000000..ace2aba6da--- /dev/null+++ b/jarvis/agentic_ide/agent_transcript.py@@ -0,0 +1,442 @@+"""What the agent actually said, read from the CLI's own record of it.++## Why not read the screen++A pane shows a TUI, and a TUI is a picture. It repaints rows in place, wraps to+the window it happened to have, draws its own logo, and prints spinners and+status lines that mean nothing an hour later. Anything that turns that picture+back into a conversation is guessing — and it guesses wrong in the way that+matters most: a line it does not recognise is either dropped (so real output+vanishes) or kept (so ``Philosophising…`` and a half-drawn banner become part of+the transcript). Both were on screen at once in the report this module answers.++But the conversation is not only on the screen. Every coding CLI worth resuming+already writes it down — that record is what :mod:`.agent_sessions` points a+resume handle at — and it is *structured*: roles are declared, tool calls carry+their name and their arguments, prose is prose. So this reads THAT, and the+guessing stops.++## What it produces++One shape for every CLI: an ordered list of :class:`Turn`, each a role, its+text, and the :class:`Step` s the agent took inside it. Callers never branch on+which product wrote the file (AP-21) — they ask for a transcript and get either+one or ``None``, and ``None`` means "this CLI keeps no readable record", which+the surface answers by showing the live pane instead. A CLI added later gets the+same deal for free by registering an adapter.++## Bounds++Two, both load-bearing. A long-running session's file reaches megabytes, so the+read starts from the END of the file and walks backwards only as far as it must+— a conversation view shows the last stretch, and paying disk for the first+thousand turns to render the last ten is how a chat surface becomes slower the+longer it is used. And each block of text is capped, because a tool result can+be a whole file and this is a conversation, not a file viewer.++Cross-platform: paths come from :mod:`.agent_sessions`, everything is read as+UTF-8 with replacement, and nothing here is OS-specific.+"""++from __future__ import annotations++import json+import re+from collections.abc import Callable+from dataclasses import dataclass, field+from pathlib import Path+from typing import Any++from loguru import logger++from . import agent_sessions++#: How many turns a transcript answers with. A conversation view is a window on+#: the recent past; scrolling further back is a different feature with a+#: different cost, and pretending otherwise makes every open slower.+MAX_TURNS = 60++#: Per-block ceiling. A tool result can be an entire file; a chat surface shows+#: what a person reads, and the pane behind it still holds the raw output.+MAX_TEXT = 4000++#: How much of the file's tail may be read to find those turns. Generous enough+#: that MAX_TURNS is reached in one pass for any normal session, bounded so a+#: pathological file cannot pull megabytes into memory.+MAX_TAIL_BYTES = 2_000_000++#: Read granularity when walking backwards from the end of the file.+_CHUNK = 256 * 1024+++@dataclass(frozen=True, slots=True)+class Step:+ """One thing the agent DID between two things it said.++ ``target`` is the single argument worth reading at a glance — the path it+ edited, the command it ran — and ``detail`` is everything else, for the+ reader who opens the step. Both may be empty: a tool nobody has taught this+ module about still shows up under its own name rather than disappearing,+ which is the fail-safe direction.+ """++ tool: str+ target: str = ""+ detail: str = ""++ def to_dict(self) -> dict[str, Any]:+ return {"tool": self.tool, "target": self.target, "detail": self.detail}+++@dataclass(slots=True)+class Turn:+ """One side of the conversation speaking once, and what it did while doing so."""++ role: str+ text: str = ""+ steps: list[Step] = field(default_factory=list)++ def to_dict(self) -> dict[str, Any]:+ return {+ "role": self.role,+ "text": self.text,+ "steps": [s.to_dict() for s in self.steps],+ }+++# --------------------------------------------------------------------------- #+# shared helpers+# --------------------------------------------------------------------------- #+++def _clip(text: str) -> str:+ """``text`` within :data:`MAX_TEXT`, keeping both ends when it is not.++ Both ends rather than the head: a truncated tool result's last lines are+ where the error is, and a truncated answer's last lines are where it says+ what it concluded.+ """+ value = str(text or "").strip()+ if len(value) <= MAX_TEXT:+ return value+ half = MAX_TEXT // 2+ return f"{value[:half]}\n\n[…]\n\n{value[-half:]}"+++def _tail_lines(path: Path, *, max_bytes: int = MAX_TAIL_BYTES) -> list[str]:+ """The last lines of ``path``, without reading what comes before them.++ Walks backwards in chunks and stops at ``max_bytes``. The first line of the+ result may be a fragment — the caller parses per line and a fragment simply+ fails to parse, which is the correct outcome for half a JSON object.+ """+ try:+ size = path.stat().st_size+ except OSError as exc:+ logger.debug("Agent transcript: cannot stat {}: {}", path.name, exc)+ return []+ want = min(size, max_bytes)+ try:+ with path.open("rb") as handle:+ handle.seek(size - want)+ blob = handle.read(want)+ except OSError as exc:+ logger.warning("Agent transcript: cannot read {}: {}", path.name, exc)+ return []+ text = blob.decode("utf-8", "replace")+ return text.splitlines()+++def _rows(path: Path) -> list[dict[str, Any]]:+ """Every JSON object in the tail of ``path``, oldest first.++ A line that does not parse is skipped in silence, and that IS the handling:+ the first line of a tail read is usually half an object, and a CLI writing+ its file while this reads it will always produce one. Nothing is lost — the+ next poll sees the complete line.+ """+ out: list[dict[str, Any]] = []+ for line in _tail_lines(path):+ stripped = line.strip()+ if not stripped or not stripped.startswith("{"):+ continue+ try:+ payload = json.loads(stripped)+ except (ValueError, TypeError):+ continue+ if isinstance(payload, dict):+ out.append(payload)+ return out+++#: Argument names worth showing as a step's headline, best first.+#:+#: Matched by NAME rather than by tool, so a tool this module has never heard of+#: still gets a readable line as long as it names its arguments the way every+#: other tool does. That is what keeps this from being a table of one product's+#: tool names — a thing that would be wrong by the next release.+_TARGET_KEYS = (+ "file_path",+ "path",+ "notebook_path",+ "command",+ "pattern",+ "query",+ "url",+ "prompt",+ "description",+ "subject",+)+++def _headline(payload: Any) -> tuple[str, str]:+ """``(target, detail)`` for a tool call's arguments.++ The target is one line — a path, a command — because a step is read at a+ glance and a wall of JSON is not read at all. The detail is the whole call,+ for whoever opens it.+ """+ if not isinstance(payload, dict):+ text = str(payload or "").strip()+ return (text.splitlines()[0] if text else "", _clip(text))+ target = ""+ for key in _TARGET_KEYS:+ value = payload.get(key)+ if isinstance(value, str) and value.strip():+ target = " ".join(value.split())+ break+ try:+ detail = json.dumps(payload, indent=2, ensure_ascii=False)+ except (TypeError, ValueError):+ detail = str(payload)+ return (target[:200], _clip(detail))+++#: An XML-ish block, opening tag through closing tag, or a lone tag.+#:+#: Not a list of the tags any one product uses — that list would be wrong by+#: the next release. The SHAPE is the signal: a coding CLI wraps everything it+#: injects into a user turn on the user's behalf in one of these, and a person+#: typing at a prompt does not write in tags.+_MACHINE_BLOCK = re.compile(+ r"<([a-zA-Z][\w:-]*)\b[^>]*>.*?</\1\s*>|<[a-zA-Z][\w:-]*\b[^>]*/?>",+ re.DOTALL,+)+++def _spoken(text: str) -> str:+ """What a PERSON said in a user record, or "" when nobody did.++ A CLI writes far more user records than the user does: tool results, session+ notifications, command echoes, reminders injected by the harness. They are+ all real records with ``role: user``, and rendering them is how a+ conversation view ends up showing ``<task-notification>`` in the reader's own+ voice — which it did.++ The test is subtractive rather than a denylist: strip the machine blocks and+ see whether a sentence remains. Nothing left means nobody spoke. Something+ left means they did, and that something is exactly what they typed — which+ is also the right answer for the common case of a person adding a line above+ a pasted block.+ """+ remainder = _MACHINE_BLOCK.sub(" ", str(text or ""))+ return remainder.strip()+++def _append(turns: list[Turn], role: str, text: str) -> None:+ """Add ``text`` to the open turn of ``role``, or open a new one.++ Consecutive blocks from one side are one turn: a CLI writes an answer as+ several records — a thought, a sentence, a tool call, another sentence — and+ rendering each as its own bubble is how a single reply becomes six.+ """+ body = _clip(_spoken(text) if role == "user" else text)+ if not body:+ return+ if turns and turns[-1].role == role:+ turns[-1].text = _clip(f"{turns[-1].text}\n\n{body}" if turns[-1].text else body)+ return+ turns.append(Turn(role=role, text=body))+++def _step(turns: list[Turn], step: Step) -> None:+ """Record a step against the open assistant turn, opening one if needed."""+ if not turns or turns[-1].role != "assistant":+ turns.append(Turn(role="assistant"))+ turns[-1].steps.append(step)+++# --------------------------------------------------------------------------- #+# Claude Code — ~/.claude/projects/<folder>/<session-id>.jsonl+# --------------------------------------------------------------------------- #+++def _claude_file(session_id: str, home: Path | None) -> Path | None:+ # Where a CLI keeps its history is `agent_sessions`' knowledge, not this+ # module's — the two answer questions about the same files and a second copy+ # of the layout would drift from the first. Same package, same layer.+ projects = agent_sessions._claude_home(home) / "projects"+ if not projects.is_dir():+ return None+ return next(projects.glob(f"*/{session_id}.jsonl"), None)+++def _claude_turns(session_id: str, home: Path | None) -> list[Turn] | None:+ path = _claude_file(session_id, home)+ if path is None:+ return None+ turns: list[Turn] = []+ for row in _rows(path):+ kind = str(row.get("type") or "")+ if kind not in ("user", "assistant"):+ # Modes, snapshots, attachments, titles — the CLI's own bookkeeping.+ continue+ message = row.get("message")+ if not isinstance(message, dict):+ continue+ role = str(message.get("role") or "")+ content = message.get("content")+ # A plain string is the short form of one text block.+ if isinstance(content, str):+ if role in ("user", "assistant"):+ _append(turns, role, content)+ continue+ if not isinstance(content, list):+ continue+ for block in content:+ if not isinstance(block, dict):+ continue+ btype = str(block.get("type") or "")+ if btype == "text" and role in ("user", "assistant"):+ _append(turns, role, str(block.get("text") or ""))+ elif btype == "tool_use":+ target, detail = _headline(block.get("input"))+ _step(+ turns,+ Step(+ tool=str(block.get("name") or "tool"),+ target=target,+ detail=detail,+ ),+ )+ # `thinking` is deliberately dropped: it is the model reasoning with+ # itself, it is not addressed to the reader, and it is the single+ # largest block in most transcripts. `tool_result` likewise — it+ # arrives as a USER record, and rendering it would put the agent's+ # own tool output in the user's voice, which is exactly the confusion+ # the screen-reading version produced.+ return turns+++# --------------------------------------------------------------------------- #+# Codex — ~/.codex/sessions/<y>/<m>/<d>/rollout-*-<session-id>.jsonl+# --------------------------------------------------------------------------- #+++def _codex_file(session_id: str, home: Path | None) -> Path | None:+ sessions = agent_sessions._codex_home(home) / "sessions"+ if not sessions.is_dir():+ return None+ return next(sessions.glob(f"*/*/*/rollout-*{session_id}*.jsonl"), None)+++def _codex_turns(session_id: str, home: Path | None) -> list[Turn] | None:+ path = _codex_file(session_id, home)+ if path is None:+ return None+ turns: list[Turn] = []+ for row in _rows(path):+ if str(row.get("type") or "") != "response_item":+ # `event_msg` duplicates the messages it announces, and the rest is+ # session metadata. Reading both would print every answer twice.+ continue+ payload = row.get("payload")+ if not isinstance(payload, dict):+ continue+ ptype = str(payload.get("type") or "")+ if ptype in ("function_call", "local_shell_call", "custom_tool_call"):+ arguments = payload.get("arguments")+ if isinstance(arguments, str):+ try:+ arguments = json.loads(arguments)+ except (ValueError, TypeError):+ pass+ target, detail = _headline(arguments)+ _step(+ turns,+ Step(+ tool=str(payload.get("name") or payload.get("type") or "tool"),+ target=target,+ detail=detail,+ ),+ )+ continue+ if ptype != "message":+ continue+ role = str(payload.get("role") or "")+ # `developer` is the harness talking to the model — instructions, not+ # conversation. Showing it would open every chat with our own preamble.+ if role not in ("user", "assistant"):+ continue+ content = payload.get("content")+ if isinstance(content, str):+ _append(turns, role, content)+ continue+ if not isinstance(content, list):+ continue+ for block in content:+ if isinstance(block, dict) and str(block.get("type") or "").endswith("text"):+ _append(turns, role, str(block.get("text") or ""))+ return turns+++# --------------------------------------------------------------------------- #+# registry+# --------------------------------------------------------------------------- #++#: Which CLIs keep a record this module can read. Absence is not a failure — a+#: CLI without an entry degrades to "watch the live pane", honestly and without+#: an error (CLAUDE.md §3).+_READERS: dict[str, Callable[[str, Path | None], list[Turn] | None]] = {+ "claude": _claude_turns,+ "codex": _codex_turns,+}+++def can_read(agent: str) -> bool:+ """Does this CLI keep a conversation this module knows how to read?"""+ return str(agent or "").strip().lower() in _READERS+++def read(agent: str, session_id: str, *, home: Path | None = None) -> list[Turn] | None:+ """The recent conversation of one session, oldest turn first.++ ``None`` — never an exception and never a lie — when this CLI keeps no+ readable record, when the session has no file yet (a pane that has just+ started), or when the file cannot be read. The caller shows the live pane in+ that case, which is always correct and never empty.+ """+ reader = _READERS.get(str(agent or "").strip().lower())+ if reader is None or not str(session_id or "").strip():+ return None+ try:+ turns = reader(session_id.strip(), home)+ except OSError as exc:+ logger.warning("Agent transcript: {} session {} unreadable: {}", agent, session_id, exc)+ return None+ if turns is None:+ return None+ # Drop turns that ended up carrying nothing — a tool-result-only record, a+ # message whose every block was filtered.+ live = [t for t in turns if t.text or t.steps]+ return live[-MAX_TURNS:]+++__all__ = [+ "MAX_TEXT",+ "MAX_TURNS",+ "Step",+ "Turn",+ "can_read",+ "read",+]diff --git a/jarvis/ui/web/agentic_ide_routes.py b/jarvis/ui/web/agentic_ide_routes.pyindex 032e6d10dd..7673c9c890 100644--- a/jarvis/ui/web/agentic_ide_routes.py+++ b/jarvis/ui/web/agentic_ide_routes.py@@ -67,6 +67,7 @@ from fastapi import (from pydantic import BaseModel, Fieldfrom jarvis.agentic_ide import (+ agent_transcript,drop_analysis,drops,interrupted,@@ -2294,6 +2295,53 @@ async def terminal_report(name: str, lines: int = 40) -> dict:raise HTTPException(status_code=404, detail=str(exc)) from exc+ "/terminals/{name}/conversation",+ summary="One terminal's conversation, as the CLI itself recorded it",+)+async def terminal_conversation(name: str) -> dict:+ """The turns of a pane's conversation — roles, prose and the steps between.++ Read from the coding CLI's OWN record rather than from the pane's screen. A+ TUI is a picture: it repaints in place, wraps to whatever width it had, and+ prints banners and spinners that mean nothing afterwards, so anything that+ reconstructs a conversation from it is guessing. The file the CLI writes to+ resume itself has none of those problems — roles are declared and tool calls+ carry their arguments (see :mod:`jarvis.agentic_ide.agent_transcript`).++ ``available: false`` is a normal answer, not an error: a CLI that keeps no+ readable record, or a pane whose conversation has not been written yet.+ The surface shows the live pane in that case, which is never wrong and never+ empty.+ """+ found = get_registry().find_terminal(name)+ if found is None:+ raise HTTPException(status_code=404, detail=f"No terminal called {name}")+ _session, term = found+ handle = term.resume+ if handle is None or not agent_transcript.can_read(term.agent):+ return {"terminal": term.name, "agent": term.agent, "available": False, "turns": []}+ # Off the event loop: this opens a file that can be megabytes, and the loop+ # it would otherwise block is the one carrying every pane's output.+ turns = await asyncio.to_thread(+ agent_transcript.read,+ term.agent,+ handle.id,+ # The history to ask is the PANE's account: each subscription of the+ # same CLI keeps its conversations in its own directory, and the machine+ # default would hand back a different account's chat.+ home=account_home(term.agent, term.account),+ )+ if turns is None:+ return {"terminal": term.name, "agent": term.agent, "available": False, "turns": []}+ return {+ "terminal": term.name,+ "agent": term.agent,+ "available": True,+ "turns": [t.to_dict() for t in turns],+ }++@router.get("/voice-attachments",summary="List every file batch waiting for a spoken terminal prompt",diff --git a/tests/unit/agentic_ide/test_agent_transcript.py b/tests/unit/agentic_ide/test_agent_transcript.pynew file mode 100644index 0000000000..a7c172aa63--- /dev/null+++ b/tests/unit/agentic_ide/test_agent_transcript.py@@ -0,0 +1,187 @@+"""The conversation reader: real records in, a readable conversation out.++The cases here are the ones that decided the design. Each fixture is written in+the shape the CLI actually writes on disk, because the whole point of this+module is that it reads THAT rather than a screen — a test against a tidied-up+shape would pass while the feature stayed broken.+"""++from __future__ import annotations++import json+from pathlib import Path++from jarvis.agentic_ide import agent_transcript+++def _write(path: Path, rows: list[dict]) -> None:+ path.parent.mkdir(parents=True, exist_ok=True)+ path.write_text(+ "\n".join(json.dumps(row, ensure_ascii=False) for row in rows) + "\n",+ encoding="utf-8",+ )+++def _claude_session(home: Path, session_id: str, rows: list[dict]) -> None:+ _write(home / "projects" / "C--some--folder" / f"{session_id}.jsonl", rows)+++def _msg(role: str, content: object) -> dict:+ return {"type": role, "message": {"role": role, "content": content}}+++class TestClaude:+ def test_reads_prose_and_tool_calls_as_one_turn(self, tmp_path: Path) -> None:+ _claude_session(+ tmp_path,+ "abc",+ [+ _msg("user", [{"type": "text", "text": "Fix the login bug"}]),+ _msg("assistant", [{"type": "thinking", "thinking": "hmm"}]),+ _msg("assistant", [{"type": "text", "text": "Looking at it now."}]),+ _msg(+ "assistant",+ [+ {+ "type": "tool_use",+ "name": "Read",+ "input": {"file_path": "src/login.ts"},+ }+ ],+ ),+ _msg("assistant", [{"type": "text", "text": "Found it."}]),+ ],+ )+ turns = agent_transcript.read("claude", "abc", home=tmp_path)+ assert turns is not None+ assert [t.role for t in turns] == ["user", "assistant"]+ assert turns[0].text == "Fix the login bug"+ # One answer, not three bubbles — a CLI writes a reply as several records.+ assert turns[1].text == "Looking at it now.\n\nFound it."+ assert [(s.tool, s.target) for s in turns[1].steps] == [("Read", "src/login.ts")]+ # Reasoning is the model talking to itself, and it is not in the answer.+ assert "hmm" not in turns[1].text++ def test_tool_results_never_speak_as_the_user(self, tmp_path: Path) -> None:+ """A tool result is a USER record. Rendering it puts the agent's own+ output in the reader's voice, which is the confusion this replaces."""+ _claude_session(+ tmp_path,+ "abc",+ [+ _msg("user", [{"type": "text", "text": "go"}]),+ _msg(+ "assistant",+ [{"type": "tool_use", "name": "Bash", "input": {"command": "ls"}}],+ ),+ _msg("user", [{"type": "tool_result", "content": "a.txt\nb.txt"}]),+ ],+ )+ turns = agent_transcript.read("claude", "abc", home=tmp_path)+ assert turns is not None+ assert [t.role for t in turns] == ["user", "assistant"]+ assert all("a.txt" not in t.text for t in turns)++ def test_harness_blocks_are_not_the_user_speaking(self, tmp_path: Path) -> None:+ """The CLI writes user records on the user's behalf all the time."""+ _claude_session(+ tmp_path,+ "abc",+ [+ _msg("user", "<task-notification><id>7</id></task-notification>"),+ _msg("user", "<system-reminder>be nice</system-reminder>"),+ _msg("user", "<caveat>ignore</caveat> But do fix the header."),+ ],+ )+ turns = agent_transcript.read("claude", "abc", home=tmp_path)+ assert turns is not None+ # Only the record with a sentence of the user's own in it survives, and+ # the machinery around that sentence is gone.+ assert len(turns) == 1+ assert turns[0].text == "But do fix the header."++ def test_missing_session_is_none_not_an_error(self, tmp_path: Path) -> None:+ assert agent_transcript.read("claude", "nope", home=tmp_path) is None++ def test_a_half_written_line_costs_only_that_line(self, tmp_path: Path) -> None:+ """The CLI writes while this reads; a torn last line must not lose the file."""+ path = tmp_path / "projects" / "p" / "abc.jsonl"+ _write(path, [_msg("user", [{"type": "text", "text": "hello"}])])+ with path.open("a", encoding="utf-8") as handle:+ handle.write('{"type": "assistant", "message": {"role": "assi')+ turns = agent_transcript.read("claude", "abc", home=tmp_path)+ assert turns is not None+ assert [t.text for t in turns] == ["hello"]+++class TestCodex:+ def _session(self, home: Path, session_id: str, rows: list[dict]) -> None:+ _write(+ home / "sessions" / "2026" / "08" / "05" / f"rollout-2026-{session_id}.jsonl",+ rows,+ )++ def test_reads_messages_and_skips_the_harness_preamble(self, tmp_path: Path) -> None:+ self._session(+ tmp_path,+ "abc",+ [+ {"type": "session_meta", "payload": {}},+ {+ "type": "response_item",+ "payload": {+ "type": "message",+ "role": "developer",+ "content": [{"type": "input_text", "text": "system rules"}],+ },+ },+ {+ "type": "response_item",+ "payload": {+ "type": "message",+ "role": "user",+ "content": [{"type": "input_text", "text": "ship it"}],+ },+ },+ {+ "type": "response_item",+ "payload": {+ "type": "function_call",+ "name": "shell",+ "arguments": '{"command": "git status"}',+ },+ },+ {+ "type": "response_item",+ "payload": {+ "type": "message",+ "role": "assistant",+ "content": [{"type": "output_text", "text": "Done."}],+ },+ },+ # `event_msg` repeats what `response_item` already carried.+ {"type": "event_msg", "payload": {"type": "agent_message", "message": "Done."}},+ ],+ )+ turns = agent_transcript.read("codex", "abc", home=tmp_path)+ assert turns is not None+ assert [t.role for t in turns] == ["user", "assistant"]+ assert turns[0].text == "ship it"+ # Said once, not twice — the duplicate event stream is skipped.+ assert turns[1].text == "Done."+ assert [(s.tool, s.target) for s in turns[1].steps] == [("shell", "git status")]+++class TestRegistry:+ def test_an_unknown_cli_degrades_rather_than_failing(self, tmp_path: Path) -> None:+ assert agent_transcript.can_read("claude") is True+ assert agent_transcript.can_read("some-new-cli") is False+ assert agent_transcript.read("some-new-cli", "abc", home=tmp_path) is None++ def test_a_long_block_keeps_both_ends(self) -> None:+ """A truncated result's END is where the error is."""+ text = "START" + ("x" * (agent_transcript.MAX_TEXT * 2)) + "END"+ clipped = agent_transcript._clip(text)+ assert clipped.startswith("START")+ assert clipped.endswith("END")+ assert len(clipped) < len(text)