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): a per-agent contract for resuming a coding session
Ruben Lütkeacc12a8e8e18
diff --git a/jarvis/agentic_ide/agent_sessions.py b/jarvis/agentic_ide/agent_sessions.pynew file mode 100644index 0000000000..8e1d50d660--- /dev/null+++ b/jarvis/agentic_ide/agent_sessions.py@@ -0,0 +1,369 @@+"""How each coding CLI names, keeps and reopens one of its conversations.++A pane of the Agentic IDE is not the conversation it shows. The conversation+lives inside the agent's own CLI — Claude Code and Codex each keep their own+history on disk — and all this module does is hold the one string needed to+point back at it: a *resume handle*.++That indirection is what makes a workspace survivable. Closing the browser kills+every agent process (deliberately: an unwatched agent burns tokens invisibly),+and a restarted app forgets the workspace entirely. With a handle per pane, both+can be undone — the pane comes back and so does what it already knows.++The two CLIs do NOT work the same way, and the difference decides the design:++* **Claude Code** accepts ``--session-id <uuid>``, so the id can be *assigned*+ at launch. We mint one per pane and hand it over; resuming is then simply+ ``--resume <uuid>``. Unambiguous, no guessing, no filesystem involved.+* **Codex** has no such flag. Its id can only be *discovered* afterwards, from+ the rollout file it writes per session. ``codex resume --last`` looks like a+ shortcut and is a trap: it means "the newest session in this folder", so three+ Codex panes in one repository would all resume the SAME conversation and two+ users' work would silently vanish. Every pane must carry its own id.++Callers never branch on an agent name (AP-21). They ask this module whether a+handle can be minted, spent, or found, and an agent that answers "no" degrades+to a fresh start with an honest message — which is also what any coding CLI+added later gets for free.++Cross-platform: nothing here is OS-specific. ``CODEX_HOME`` is honoured when+set, otherwise the CLI's conventional home directory is used, and paths are+compared through ``os.path.normcase`` so a drive-letter difference on Windows+does not hide a pane's own session from it.+"""++from __future__ import annotations++import json+import os+import time+from collections.abc import Callable, Collection+from dataclasses import dataclass+from datetime import UTC, datetime+from pathlib import Path+from typing import Any+from uuid import uuid4++from loguru import logger++# How far BEFORE a pane's recorded start a session file may claim to have been+# created and still be counted as that pane's. The CLI always stamps itself+# AFTER we read our clock, so this only absorbs clock jitter — and it must stay+# small, because panes opened in one batch are seconds apart and a generous+# window would let one pane claim its neighbour's conversation.+_SKEW_S = 1.5++# Upper bound on how many rollout files one discovery may open. A heavy Codex+# user accumulates thousands; reading the first line of every one of them would+# turn a pane restart into a disk crawl. The newest files are tried first, and a+# pane's own session is by construction among the newest.+_MAX_CANDIDATES = 400+++@dataclass(frozen=True, slots=True)+class ResumeHandle:+ """The one string that points back at a coding CLI's own conversation.++ ``kind`` is not decoration: spending a Codex id on Claude Code would at best+ fail the launch and at worst reopen a stranger's conversation, so the kind is+ checked before the handle is ever turned into arguments.+ """++ kind: str+ id: str+ captured_at: float++ def to_dict(self) -> dict[str, Any]:+ return {"kind": self.kind, "id": self.id, "captured_at": self.captured_at}++ @staticmethod+ def from_dict(data: Any) -> ResumeHandle | None:+ """Rebuild a handle from stored JSON, or None if it is not one.++ Defensive by design: this reads a file that survived a crash, an app+ upgrade, or a hand edit. Anything unrecognisable means "no handle",+ which costs a fresh conversation — never an exception on a path the user+ is waiting on.+ """+ if not isinstance(data, dict):+ return None+ kind = str(data.get("kind") or "").strip()+ session_id = str(data.get("id") or "").strip()+ if not kind or not session_id:+ return None+ try:+ captured_at = float(data.get("captured_at") or 0.0)+ except (TypeError, ValueError):+ captured_at = 0.0+ return ResumeHandle(kind=kind, id=session_id, captured_at=captured_at)+++@dataclass(frozen=True, slots=True)+class _Adapter:+ """What one coding CLI can do with its own sessions."""++ kind: str+ # Extra argv for a FRESH start, plus the handle that start will produce.+ launch: Callable[[], tuple[tuple[str, ...], ResumeHandle | None]]+ # Extra argv that reopens the conversation behind an id.+ resume: Callable[[str], tuple[str, ...]]+ # Finds the id afterwards, for CLIs that cannot be told one. The third+ # argument is the set of ids other panes already hold — see `discover`.+ discover: Callable[[str, float, Collection[str]], ResumeHandle | None] | None+++def _claude_launch() -> tuple[tuple[str, ...], ResumeHandle | None]:+ # Claude Code requires a valid UUID here and refuses anything else.+ session_id = str(uuid4())+ return (+ ("--session-id", session_id),+ ResumeHandle(kind="claude_session", id=session_id, captured_at=time.time()),+ )+++def _codex_launch() -> tuple[tuple[str, ...], ResumeHandle | None]:+ # Nothing can be passed: the id is Codex's to choose and ours to find later.+ return ((), None)+++_ADAPTERS: dict[str, _Adapter] = {+ "claude": _Adapter(+ kind="claude_session",+ launch=_claude_launch,+ resume=lambda session_id: ("--resume", session_id),+ discover=None,+ ),+ "codex": _Adapter(+ kind="codex_rollout",+ launch=_codex_launch,+ # `codex resume <id>` is a subcommand, so it comes before nothing else —+ # it is simply appended to the binary the pane already resolved.+ resume=lambda session_id: ("resume", session_id),+ # Late-bound on purpose: the discovery function is defined further down,+ # next to the file-format knowledge it needs.+ discover=lambda cwd, started, taken: _discover_codex(cwd, started, taken),+ ),+}+++def can_resume(agent: str) -> bool:+ """True when this coding CLI can reopen one of its own conversations."""+ return agent in _ADAPTERS+++def launch_extra(agent: str) -> tuple[tuple[str, ...], ResumeHandle | None]:+ """Extra argv for a fresh start, plus the handle it will be reachable by.++ A ``None`` handle does not mean "not resumable" — Codex simply cannot be+ told its id, so the handle arrives later through :func:`discover`.+ """+ adapter = _ADAPTERS.get(agent)+ if adapter is None:+ return ((), None)+ return adapter.launch()+++def resume_argv(agent: str, handle: ResumeHandle | None) -> tuple[str, ...] | None:+ """Extra argv that reopens ``handle``'s conversation, or None if it cannot.++ None is the answer for an unknown agent, a missing handle, and a handle of+ the wrong kind. Every one of those means the same thing to the caller: start+ fresh and say so.+ """+ if handle is None:+ return None+ adapter = _ADAPTERS.get(agent)+ if adapter is None or adapter.kind != handle.kind:+ return None+ return adapter.resume(handle.id)+++def discover(+ agent: str,+ cwd: str,+ started_at: float,+ taken: Collection[str] = (),+) -> ResumeHandle | None:+ """Find the session a pane started at ``started_at`` in ``cwd`` created.++ ``taken`` is what makes this safe for a whole workspace rather than a single+ pane: the ids the OTHER panes already hold. Time alone cannot separate panes+ opened in one batch — a coding CLI needs a second or two to write its+ session file, which is longer than the gap between five panes opening, so+ "the first session after I started" points several panes at the same+ conversation. An id that already belongs to a pane is simply not offered+ again, and the timestamps then only decide the order.++ Blocking filesystem work — callers run it in a worker thread. Returns None+ for every agent that does not need discovery (its handle already exists) and+ whenever nothing convincing is found.+ """+ adapter = _ADAPTERS.get(agent)+ if adapter is None or adapter.discover is None:+ return None+ try:+ return adapter.discover(cwd, started_at, taken)+ except Exception as exc: # noqa: BLE001 - discovery is a convenience+ logger.warning("Agentic IDE: {} session discovery failed: {}", agent, exc)+ return None+++# --------------------------------------------------------------------- Codex+def _codex_home() -> Path:+ raw = os.environ.get("CODEX_HOME")+ return Path(raw).expanduser() if raw else Path.home() / ".codex"+++def _same_folder(left: str, right: str) -> bool:+ """Path equality that survives Windows drive-letter and separator drift."""+ try:+ a = os.path.normcase(str(Path(left).expanduser().resolve()))+ b = os.path.normcase(str(Path(right).expanduser().resolve()))+ except OSError:+ return False+ return a == b+++def _parse_utc(value: Any) -> float | None:+ """ISO-8601 (with or without a trailing ``Z``) as a POSIX timestamp."""+ if not isinstance(value, str) or not value:+ return None+ text = value.strip()+ if text.endswith(("Z", "z")):+ text = f"{text[:-1]}+00:00"+ try:+ parsed = datetime.fromisoformat(text)+ except ValueError:+ return None+ if parsed.tzinfo is None:+ parsed = parsed.replace(tzinfo=UTC)+ return parsed.timestamp()+++def _is_top_level_session(payload: dict[str, Any]) -> bool:+ """True unless the record explicitly says it is a subagent's own thread.++ Fail-open on purpose. A filter that must recognise every future Codex record+ shape in order to accept one is a filter that eventually rejects everything —+ the failure mode that made a wake word go deaf (AP-27). Only an explicit+ subagent marker disqualifies a session.+ """+ if payload.get("thread_source") == "subagent":+ return False+ source = payload.get("source")+ return not (isinstance(source, dict) and "subagent" in source)+++def _read_meta(path: Path) -> dict[str, Any] | None:+ """The ``session_meta`` record Codex writes as the FIRST line of a rollout.++ Only that first line is read: these files grow to megabytes as the+ conversation does, and everything identifying is in the header.+ """+ try:+ with path.open("r", encoding="utf-8", errors="replace") as handle:+ first = handle.readline()+ except OSError:+ return None+ if not first.strip():+ return None+ try:+ record = json.loads(first)+ except ValueError:+ return None+ if not isinstance(record, dict):+ return None+ payload = record.get("payload")+ if not isinstance(payload, dict):+ return None+ return payload+++def _candidate_files(root: Path, started_at: float) -> list[Path]:+ """Rollout files worth opening, newest first and bounded.++ Codex files them under ``sessions/YYYY/MM/DD``. The day folder is named in+ LOCAL time while the record inside is UTC, so a machine far from UTC can+ file a session under the neighbouring day — hence the one-day margin on+ either side rather than an exact date match.+ """+ sessions = root / "sessions"+ if not sessions.is_dir():+ return []+ started_day = datetime.fromtimestamp(started_at, tz=UTC).date()+ keep: list[Path] = []+ for day_dir in sessions.glob("*/*/*"):+ if not day_dir.is_dir():+ continue+ try:+ parts = day_dir.parts[-3:]+ day = datetime(int(parts[0]), int(parts[1]), int(parts[2])).date()+ except (ValueError, IndexError):+ continue+ if (day - started_day).days < -1:+ continue+ keep.extend(day_dir.glob("rollout-*.jsonl"))++ def _mtime(path: Path) -> float:+ try:+ return path.stat().st_mtime+ except OSError:+ return 0.0++ keep.sort(key=_mtime, reverse=True)+ return keep[:_MAX_CANDIDATES]+++def _discover_codex(+ cwd: str, started_at: float, taken: Collection[str] = ()+) -> ResumeHandle | None:+ """The Codex session a pane in ``cwd`` started at ``started_at`` created.++ Matching is on *(folder, not already claimed, earliest moment at or after+ the pane launched)*. The claim check carries the correctness: Codex takes a+ beat to write its session file, so several panes opened together all see+ each other's files as "the first one after I started". Skipping ids that+ other panes already hold turns that ambiguity into a queue.++ The timestamp comes from INSIDE the file, never from its name. Codex stamps+ the filename in local time and the record in UTC; on a machine at UTC+10 the+ two are ten hours apart, so a filename-based match would place every session+ in the future and find nothing. That is the kind of bug that works perfectly+ on the machine it was written on and nowhere else.+ """+ claimed = {str(t) for t in taken}+ best: tuple[float, str] | None = None+ for path in _candidate_files(_codex_home(), started_at):+ payload = _read_meta(path)+ if payload is None:+ continue+ folder = payload.get("cwd")+ if not isinstance(folder, str) or not _same_folder(folder, cwd):+ continue+ if not _is_top_level_session(payload):+ continue+ session_id = str(payload.get("id") or payload.get("session_id") or "").strip()+ if not session_id or session_id in claimed:+ continue+ stamp = _parse_utc(payload.get("timestamp"))+ if stamp is None:+ try:+ stamp = path.stat().st_mtime+ except OSError:+ continue+ if stamp < started_at - _SKEW_S:+ continue+ if best is None or stamp < best[0]:+ best = (stamp, session_id)+ if best is None:+ return None+ return ResumeHandle(kind="codex_rollout", id=best[1], captured_at=best[0])+++__all__ = [+ "ResumeHandle",+ "can_resume",+ "discover",+ "launch_extra",+ "resume_argv",+]diff --git a/tests/unit/agentic_ide/test_agent_sessions.py b/tests/unit/agentic_ide/test_agent_sessions.pynew file mode 100644index 0000000000..ec9db05796--- /dev/null+++ b/tests/unit/agentic_ide/test_agent_sessions.py@@ -0,0 +1,298 @@+"""The per-agent resume contract: minting, spending and finding a handle.++The point of these tests is that several coding panes in ONE folder must never+collide. Codex's own ``--last`` shortcut would give all of them the same+conversation, so discovery has to tell them apart — and it has to do so on a+machine in any timezone, which is where the interesting failure lives.+"""+from __future__ import annotations++import json+from datetime import datetime+from pathlib import Path++import pytest++from jarvis.agentic_ide import agent_sessions as sessions+++def _epoch(iso: str) -> float:+ """A UTC ISO string as a POSIX timestamp, computed rather than hardcoded."""+ return datetime.fromisoformat(iso.replace("Z", "+00:00")).timestamp()+++# ------------------------------------------------------------------- minting+def test_claude_mints_a_session_id_at_launch() -> None:+ argv, handle = sessions.launch_extra("claude")+ assert argv[0] == "--session-id"+ assert handle is not None and handle.id == argv[1]+ # Claude Code rejects anything that is not a UUID.+ assert len(handle.id) == 36 and handle.id.count("-") == 4+++def test_claude_resumes_exactly_that_id() -> None:+ _, handle = sessions.launch_extra("claude")+ assert handle is not None+ assert sessions.resume_argv("claude", handle) == ("--resume", handle.id)+++def test_two_panes_never_share_a_minted_id() -> None:+ """Otherwise the second pane would overwrite the first one's conversation."""+ _, first = sessions.launch_extra("claude")+ _, second = sessions.launch_extra("claude")+ assert first is not None and second is not None and first.id != second.id+++def test_codex_cannot_be_told_an_id_but_can_resume_one() -> None:+ argv, handle = sessions.launch_extra("codex")+ assert argv == () and handle is None+ stored = sessions.ResumeHandle(kind="codex_rollout", id="abc-123", captured_at=1.0)+ assert sessions.resume_argv("codex", stored) == ("resume", "abc-123")+++# ------------------------------------------------------------------ refusals+def test_an_unknown_agent_never_claims_it_can_resume() -> None:+ """A coding CLI added later degrades to a fresh start, never to a crash."""+ assert sessions.can_resume("some-future-cli") is False+ assert sessions.launch_extra("some-future-cli") == ((), None)+ handle = sessions.ResumeHandle(kind="claude_session", id="x", captured_at=0.0)+ assert sessions.resume_argv("some-future-cli", handle) is None+++def test_a_handle_of_the_wrong_kind_is_refused() -> None:+ """A Codex id must never be spent on Claude Code."""+ codex = sessions.ResumeHandle(kind="codex_rollout", id="x", captured_at=0.0)+ assert sessions.resume_argv("claude", codex) is None+++def test_no_handle_means_no_resume_arguments() -> None:+ assert sessions.resume_argv("claude", None) is None+++# ------------------------------------------------------------- codex lookup+def _write_rollout(+ root: Path,+ *,+ day: str,+ name: str,+ cwd: str,+ utc: str,+ session_id: str,+ extra: dict | None = None,+) -> Path:+ folder = root / "sessions" / day+ folder.mkdir(parents=True, exist_ok=True)+ target = folder / name+ payload = {+ "id": session_id,+ "session_id": session_id,+ "timestamp": utc,+ "cwd": cwd,+ "originator": "codex-tui",+ }+ payload.update(extra or {})+ record = {"timestamp": utc, "type": "session_meta", "payload": payload}+ # A real rollout has thousands of lines after the header; only the first+ # one may ever be read.+ target.write_text(+ json.dumps(record) + "\n" + '{"type":"turn"}\n' * 50, encoding="utf-8"+ )+ return target+++def test_codex_discovery_picks_the_pane_s_own_session(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """Three panes in one folder must not all resume the same conversation."""+ monkeypatch.setenv("CODEX_HOME", str(tmp_path))+ cwd = str(tmp_path / "repo")+ (tmp_path / "repo").mkdir()+ for stamp, ident in (+ ("2026-07-25T12:00:00Z", "first"),+ ("2026-07-25T12:00:10Z", "second"),+ ("2026-07-25T12:00:20Z", "third"),+ ):+ _write_rollout(+ tmp_path,+ day="2026/07/25",+ name=f"rollout-{ident}.jsonl",+ cwd=cwd,+ utc=stamp,+ session_id=ident,+ )+ found = sessions.discover("codex", cwd, _epoch("2026-07-25T12:00:05Z"))+ assert found is not None and found.id == "second"+++def test_panes_opened_in_one_batch_do_not_claim_the_same_session(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """The collision that time alone cannot resolve.++ Codex needs a beat to write its session file, and opening five panes takes+ less time than that — so every pane sees every file as "the first one after+ I started". Only the claim check keeps them apart.+ """+ monkeypatch.setenv("CODEX_HOME", str(tmp_path))+ cwd = str(tmp_path / "repo")+ (tmp_path / "repo").mkdir()+ for stamp, ident in (+ ("2026-07-25T12:00:02Z", "pane-one-session"),+ ("2026-07-25T12:00:03Z", "pane-two-session"),+ ):+ _write_rollout(+ tmp_path,+ day="2026/07/25",+ name=f"rollout-{ident}.jsonl",+ cwd=cwd,+ utc=stamp,+ session_id=ident,+ )+ # Both panes launched within the same second, before either file existed.+ first = sessions.discover("codex", cwd, _epoch("2026-07-25T12:00:00Z"))+ assert first is not None and first.id == "pane-one-session"+ second = sessions.discover(+ "codex", cwd, _epoch("2026-07-25T12:00:00Z"), taken={first.id}+ )+ assert second is not None and second.id == "pane-two-session"+++def test_codex_discovery_ignores_a_session_from_another_folder(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ monkeypatch.setenv("CODEX_HOME", str(tmp_path))+ (tmp_path / "repo").mkdir()+ (tmp_path / "elsewhere").mkdir()+ _write_rollout(+ tmp_path,+ day="2026/07/25",+ name="rollout-x.jsonl",+ cwd=str(tmp_path / "elsewhere"),+ utc="2026-07-25T12:00:10Z",+ session_id="stranger",+ )+ assert (+ sessions.discover(+ "codex", str(tmp_path / "repo"), _epoch("2026-07-25T12:00:05Z")+ )+ is None+ )+++def test_codex_discovery_ignores_a_session_that_predates_the_pane(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """Yesterday's conversation in this folder is not this pane's."""+ monkeypatch.setenv("CODEX_HOME", str(tmp_path))+ cwd = str(tmp_path / "repo")+ (tmp_path / "repo").mkdir()+ _write_rollout(+ tmp_path,+ day="2026/07/24",+ name="rollout-old.jsonl",+ cwd=cwd,+ utc="2026-07-24T09:00:00Z",+ session_id="yesterday",+ )+ assert sessions.discover("codex", cwd, _epoch("2026-07-25T12:00:05Z")) is None+++def test_codex_discovery_survives_a_filename_in_a_foreign_timezone(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """The trap this test exists for.++ Codex stamps the FILENAME in local time and the record in UTC. A machine at+ UTC+10 writes ``rollout-2026-07-25T22-00-10-...`` for a session that+ happened at 12:00:10Z, and files it under the local day. Matching on the+ filename would place that session ten hours in the future and never find it;+ matching on the record works in every timezone.+ """+ monkeypatch.setenv("CODEX_HOME", str(tmp_path))+ cwd = str(tmp_path / "repo")+ (tmp_path / "repo").mkdir()+ _write_rollout(+ tmp_path,+ day="2026/07/26", # local date is already the next day+ name="rollout-2026-07-25T22-00-10-tz.jsonl",+ cwd=cwd,+ utc="2026-07-25T12:00:10Z",+ session_id="right-one",+ )+ found = sessions.discover("codex", cwd, _epoch("2026-07-25T12:00:05Z"))+ assert found is not None and found.id == "right-one"+++def test_codex_discovery_skips_a_subagent_thread(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """A reviewer subagent writes its own rollout; the pane is the parent."""+ monkeypatch.setenv("CODEX_HOME", str(tmp_path))+ cwd = str(tmp_path / "repo")+ (tmp_path / "repo").mkdir()+ _write_rollout(+ tmp_path,+ day="2026/07/25",+ name="rollout-sub.jsonl",+ cwd=cwd,+ utc="2026-07-25T12:00:08Z",+ session_id="subagent-thread",+ extra={"thread_source": "subagent"},+ )+ _write_rollout(+ tmp_path,+ day="2026/07/25",+ name="rollout-main.jsonl",+ cwd=cwd,+ utc="2026-07-25T12:00:12Z",+ session_id="the-pane",+ )+ found = sessions.discover("codex", cwd, _epoch("2026-07-25T12:00:05Z"))+ assert found is not None and found.id == "the-pane"+++def test_a_corrupt_rollout_file_is_skipped_not_raised(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ monkeypatch.setenv("CODEX_HOME", str(tmp_path))+ folder = tmp_path / "sessions" / "2026" / "07" / "25"+ folder.mkdir(parents=True)+ (folder / "rollout-broken.jsonl").write_text("{not json", encoding="utf-8")+ (tmp_path / "repo").mkdir()+ assert (+ sessions.discover(+ "codex", str(tmp_path / "repo"), _epoch("2026-07-25T12:00:05Z")+ )+ is None+ )+++def test_discovery_without_a_home_directory_returns_nothing(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """A fresh machine that has never run Codex — not an error."""+ monkeypatch.setenv("CODEX_HOME", str(tmp_path / "does-not-exist"))+ assert sessions.discover("codex", str(tmp_path), 0.0) is None+++def test_claude_needs_no_discovery() -> None:+ """It was told its id at launch, so there is nothing to look for."""+ assert sessions.discover("claude", "/tmp", 0.0) is None+++# ------------------------------------------------------------------ storage+def test_handle_round_trips_through_a_dict() -> None:+ handle = sessions.ResumeHandle(kind="claude_session", id="u", captured_at=3.5)+ assert sessions.ResumeHandle.from_dict(handle.to_dict()) == handle+++def test_a_damaged_stored_handle_reads_as_no_handle() -> None:+ """The file it comes from survived a crash or a hand edit."""+ assert sessions.ResumeHandle.from_dict({"kind": "x"}) is None+ assert sessions.ResumeHandle.from_dict({"id": "x"}) is None+ assert sessions.ResumeHandle.from_dict("nonsense") is None+ assert sessions.ResumeHandle.from_dict(None) is None+ salvaged = sessions.ResumeHandle.from_dict(+ {"kind": "claude_session", "id": "u", "captured_at": "not a number"}+ )+ assert salvaged is not None and salvaged.captured_at == 0.0