Skip to content

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.

feat(ide): resume every running agent after a power-off, Herdr-style

rubenluetke10-beep88d8d7986ccb
diff --git a/jarvis/agentic_ide/resume_store.py b/jarvis/agentic_ide/resume_store.py
index b9156a02fc..f69c12edca 100644
--- a/jarvis/agentic_ide/resume_store.py
+++ b/jarvis/agentic_ide/resume_store.py
@@ -106,6 +106,12 @@ class SnapshotTerminal:
# process started; kept here so a fork that never got to start before a
# restart still starts as the copy it was opened as.
fork_from: ResumeHandle | None = None
+ # Was this pane's agent running when the snapshot was written? A reboot
+ # brings back exactly those (``Registry._resume_after_reboot``); an agent
+ # that ended by itself — ``/exit``, a finished one-shot — stays ended.
+ # Missing on older snapshots means True: before this field, every pane in
+ # an open workspace was one the user expected to come back.
+ running: bool = True
def to_dict(self) -> dict[str, Any]:
return {
@@ -127,6 +133,7 @@ class SnapshotTerminal:
"folder": self.folder,
"branch": self.branch,
"fork_from": self.fork_from.to_dict() if self.fork_from else None,
+ "running": self.running,
}
@staticmethod
@@ -160,6 +167,7 @@ class SnapshotTerminal:
folder=str(data.get("folder") or "").strip(),
branch=str(data.get("branch") or "").strip(),
fork_from=ResumeHandle.from_dict(data.get("fork_from")),
+ running=data.get("running") is not False,
)
diff --git a/jarvis/agentic_ide/session.py b/jarvis/agentic_ide/session.py
index d96a6d8241..47e32e8853 100644
--- a/jarvis/agentic_ide/session.py
+++ b/jarvis/agentic_ide/session.py
@@ -1006,6 +1006,9 @@ class Terminal:
# then run on unwatched, which is the whole thing the kill prevents.
stopping: bool = False
exit_code: int | None = None
+ # Restored from the snapshot: was this pane's agent running when the app
+ # last saved? Read once, by ``_resume_after_reboot``.
+ was_running: bool = False
error: str = ""
started_at: float | None = None
last_output_at: float | None = None
@@ -1439,8 +1442,26 @@ class Terminal:
folder=self.folder,
branch=self.branch,
fork_from=self.fork_from,
+ running=self._counts_as_running(),
)
+ def _counts_as_running(self) -> bool:
+ """Should a reboot bring this pane's agent back?
+
+ A live agent, obviously. So is one that died with anything but a clean
+ exit: a host that went away with the machine reports an unknown code,
+ and a crash is exactly what recovery is for. An agent that exited 0
+ ended by itself (``/exit``, a finished task) and stays ended, and a
+ pane that never started keeps what the last snapshot said about it.
+ """
+ if self.status == "live":
+ return True
+ if self.status == "exited":
+ return self.exit_code != 0
+ if self.status == "pending":
+ return self.was_running
+ return False
+
def cwd(self, workspace_folder: str) -> str:
"""The folder this pane's agent runs in: its own worktree, else the workspace's."""
return self.folder or workspace_folder
@@ -2137,48 +2158,84 @@ class Registry:
info.meta.get("name", "unnamed"),
)
manager.kill_hosted(info.terminal_id) # type: ignore[union-attr]
- await self._continue_interrupted_after_reboot()
-
- async def _continue_interrupted_after_reboot(self) -> None:
- """Carry on with the work a reboot interrupted, in every workspace.
-
- Only panes whose agent did NOT survive (nothing in the PTY host claimed
- them — the machine restarted, or the host died) and whose last
- checkpoint saw them actively working. ``interrupted.continue_panes``
- applies the rest of the gate: a pane showing a question or waiting for
- an approval is never typed into, and a pane that finished or was
- stopped carries no ``continuation_pending`` in the first place.
-
- Their agents are started here without a viewer, so continuing does not
- depend on somebody opening each workspace; the ``continue`` is held
- until each CLI's input line is writable (``defer_continue``).
+ await self._resume_after_reboot()
+
+ async def _resume_after_reboot(self) -> None:
+ """Bring back every agent a power-off or reboot ended, in every workspace.
+
+ What Herdr calls native agent-session resume: the agents' processes died
+ with the machine, so each one is started again on ITS OWN conversation
+ (the CLI's ``--resume <id>`` and equivalents, see ``agent_sessions``) —
+ not a fresh CLI, and not only in the workspace somebody happens to open.
+
+ Which panes: every one still ``pending`` after the PTY host had its say
+ (a pane whose agent survived was re-joined and is ``live``), whose agent
+ was running at the last save (``Terminal.was_running`` — an agent that
+ ended by itself stays ended), and whose CLI really holds the
+ conversation its handle points at. A pane that was never given an
+ instruction has nothing to resume and keeps starting when it is opened.
+
+ On top of that, panes whose last checkpoint saw them WORKING are told to
+ carry on (``interrupted.continue_panes``). That gate stays strict: a
+ pane that was idle, finished or waiting for an answer or an approval is
+ resumed but never typed into.
+
+ The starts run without a viewer and go through the ordinary cold-start
+ gate, so a dozen agents come back a few at a time.
"""
from . import interrupted
- try:
- report = await interrupted.continue_panes(self)
- except Exception as exc: # noqa: BLE001 - startup must not fail on this
- logger.warning("Agentic IDE: interrupted work not continued at startup: {}", exc)
- return
- starts = [
+ pending = [
(session, term)
for session in self._sessions.values()
for term in session.terminals
- if term.status == "pending" and term.continue_when_ready
+ if term.status == "pending"
+ and term.was_running
+ and term.resume is not None
+ and accepts_prompts(term.agent)
]
+
+ def _with_conversation() -> list[tuple[Session, Terminal]]:
+ found: list[tuple[Session, Terminal]] = []
+ for session, term in pending:
+ try:
+ if has_conversation(
+ term.agent, term.resume, account_home(term.agent, term.account)
+ ):
+ found.append((session, term))
+ except Exception as exc: # noqa: BLE001 - one unreadable history skips one pane
+ logger.debug(
+ "Agentic IDE: could not check {}'s conversation: {}", term.name, exc
+ )
+ return found
+
+ resumable = await asyncio.to_thread(_with_conversation)
+ try:
+ report = await interrupted.continue_panes(self)
+ except Exception as exc: # noqa: BLE001 - startup must not fail on this
+ logger.warning("Agentic IDE: interrupted work not continued at startup: {}", exc)
+ report = None
+ starts = list(resumable)
+ chosen = {id(term) for _session, term in starts}
+ for session in self._sessions.values():
+ for term in session.terminals:
+ if term.status == "pending" and term.continue_when_ready and id(term) not in chosen:
+ starts.append((session, term))
for session, term in starts:
task = asyncio.create_task(
- self._start_unviewed(session, term), name=f"ide-continue-{term.key}"
+ self._start_unviewed(session, term), name=f"ide-resume-{term.key}"
)
self._cold_start_holds.add(task)
task.add_done_callback(self._cold_start_holds.discard)
- if starts or report.continued:
+ if starts:
logger.info(
- "Agentic IDE: continuing {} interrupted pane(s) after a restart ({} started "
- "without a viewer)",
- len(starts) + len(report.continued),
+ "Agentic IDE: resuming {} agent(s) that a reboot ended ({} of them "
+ "told to continue their work)",
len(starts),
+ sum(1 for _session, term in starts if term.continue_when_ready),
)
+ elif report is not None and report.continued:
+ logger.info("Agentic IDE: continued {} interrupted pane(s)", len(report.continued))
async def _start_unviewed(self, session: Session, term: Terminal) -> None:
"""Start a pane's agent with nobody watching (the registry records output)."""
@@ -2784,6 +2841,7 @@ class Registry:
folder=entry.folder if entry.folder and Path(entry.folder).is_dir() else "",
branch=entry.branch if entry.folder and Path(entry.folder).is_dir() else "",
fork_from=entry.fork_from,
+ was_running=entry.running,
)
terminals = [_restored(index, entry) for index, entry in enumerate(space.terminals)]
diff --git a/tests/unit/agentic_ide/test_reboot_resume.py b/tests/unit/agentic_ide/test_reboot_resume.py
new file mode 100644
index 0000000000..aa0df7a2db
--- /dev/null
+++ b/tests/unit/agentic_ide/test_reboot_resume.py
@@ -0,0 +1,190 @@
+"""RUB-102: after a power-off every running agent comes back on its own conversation.
+
+Herdr's "native agent-session resume": the agents died with the machine, so
+the app starts each one again with the CLI's resume argument, in every
+workspace, without waiting for somebody to open it. Driven against the fake
+persistent pool, so nothing real is started.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import time
+from pathlib import Path
+
+import pytest
+
+from jarvis.agentic_ide import resume_store
+from jarvis.agentic_ide import session as ide
+from jarvis.agentic_ide.agent_sessions import ResumeHandle
+from tests.unit.agentic_ide.test_pty_host_adoption import ConnectRecorder, FakeHostedPool
+
+
[email protected](autouse=True)
+def _no_real_agents(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(ide, "agent_argv", lambda name: (f"/usr/bin/{name}",))
+ # Every handle points at a conversation that exists, unless a test says not.
+ monkeypatch.setattr(ide, "has_conversation", lambda agent, handle, home=None: True)
+
+
+def _pane(index: int, *, running: bool = True, working: bool = False, resume: bool = True):
+ return resume_store.SnapshotTerminal(
+ key=f"t{index}",
+ name=f"T{index}",
+ agent="claude",
+ history_id=f"hist-{index}",
+ column=index,
+ resume=ResumeHandle("claude_session", f"conv-{index}", 1.0) if resume else None,
+ continuation_needed=working,
+ running=running,
+ )
+
+
+def _save(folder: Path, panes: list[resume_store.SnapshotTerminal], ws_id: str = "ide_reboot01"):
+ now = time.time()
+ workspace = resume_store.SnapshotWorkspace(
+ session_id=ws_id, folder=str(folder), terminals=panes, saved_at=now
+ )
+ return workspace, now
+
+
+def _store(*workspaces: resume_store.SnapshotWorkspace) -> None:
+ now = max(w.saved_at for w in workspaces)
+ resume_store.save(
+ resume_store.Snapshot(
+ saved_at=now, workspaces=list(workspaces), active_session_id=workspaces[0].session_id
+ )
+ )
+
+
+async def _boot(monkeypatch: pytest.MonkeyPatch, pool: FakeHostedPool):
+ monkeypatch.setattr(ide, "_connect_pty_host", ConnectRecorder(pool))
+ registry = ide.Registry()
+ nudged: list[str] = []
+
+ async def record_prompt(name: str, text: str, **_kwargs: object) -> None:
+ nudged.append(name)
+
+ monkeypatch.setattr(registry, "send_prompt", record_prompt)
+ await registry.boot_restore()
+ return registry, nudged
+
+
+async def _settle(pool: FakeHostedPool, want: int) -> None:
+ for _ in range(100):
+ if len(pool.metas) >= want:
+ break
+ await asyncio.sleep(0.02)
+ await asyncio.sleep(0.05)
+
+
+async def test_every_running_agent_is_resumed_in_every_workspace(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ first, second = tmp_path / "a", tmp_path / "b"
+ first.mkdir()
+ second.mkdir()
+ ws_a, _ = _save(first, [_pane(1), _pane(2)], "ide_rebootA")
+ ws_b, _ = _save(second, [_pane(3)], "ide_rebootB")
+ _store(ws_a, ws_b)
+ pool = FakeHostedPool() # a fresh host: nothing survived the power-off
+
+ registry, nudged = await _boot(monkeypatch, pool)
+ await _settle(pool, 3)
+
+ # All three came back, including the one in the workspace nobody opened ...
+ assert sorted(m["history_id"] for m in pool.metas) == ["hist-1", "hist-2", "hist-3"]
+ assert all(t.status == "live" and t.resumed for s in registry.sessions for t in s.terminals)
+ # ... on their OWN conversations, not as fresh CLIs.
+ argvs = [" ".join(spawn["argv"]) for spawn in pool.spawns]
+ for index in (1, 2, 3):
+ assert any(f"--resume conv-{index}" in argv for argv in argvs), argvs
+ # Idle agents are resumed but never typed into.
+ assert nudged == []
+
+
+async def test_working_agents_are_also_told_to_continue(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ ws, _ = _save(tmp_path, [_pane(1, working=True), _pane(2)])
+ _store(ws)
+ monkeypatch.setattr(
+ ide,
+ "_mark_restored_continuations",
+ lambda terms: [
+ setattr(t, "continuation_pending", t.resume_continuation_needed) for t in terms
+ ],
+ )
+ pool = FakeHostedPool()
+
+ registry, nudged = await _boot(monkeypatch, pool)
+ await _settle(pool, 2)
+ for _ in range(100):
+ if nudged:
+ break
+ await asyncio.sleep(0.02)
+
+ assert len(pool.metas) == 2
+ assert nudged == ["T1"]
+
+
+async def test_agents_that_ended_by_themselves_stay_ended(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ ws, _ = _save(tmp_path, [_pane(1, running=False), _pane(2)])
+ _store(ws)
+ pool = FakeHostedPool()
+
+ registry, _nudged = await _boot(monkeypatch, pool)
+ await _settle(pool, 1)
+
+ assert [m["history_id"] for m in pool.metas] == ["hist-2"]
+ assert registry.sessions[0].find("T1").status == "pending"
+
+
+async def test_panes_without_a_conversation_are_not_started(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ ws, _ = _save(tmp_path, [_pane(1, resume=False), _pane(2)])
+ _store(ws)
+ monkeypatch.setattr(
+ ide, "has_conversation", lambda agent, handle, home=None: handle.id != "conv-2"
+ )
+ pool = FakeHostedPool()
+
+ await _boot(monkeypatch, pool)
+ await _settle(pool, 1)
+
+ assert pool.metas == []
+
+
+async def test_survivors_are_rejoined_and_only_the_rest_resumed(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ ws, _ = _save(tmp_path, [_pane(1), _pane(2)])
+ _store(ws)
+ pool = FakeHostedPool()
+ pool.add_hosted("h-1", history_id="hist-1") # the app closed; this agent kept running
+
+ registry, _nudged = await _boot(monkeypatch, pool)
+ await _settle(pool, 1)
+
+ assert pool.adopted == ["h-1"]
+ assert [m["history_id"] for m in pool.metas] == ["hist-2"]
+ session = registry.sessions[0]
+ assert session.find("T1").reattached is False and session.find("T1").pty_id == "h-1"
+
+
+def test_snapshot_records_whether_an_agent_was_running() -> None:
+ term = ide.Terminal(key="t1", name="T1", agent="claude", display_name="Claude", index=0)
+ term.status = "live"
+ assert term.to_snapshot().running is True
+ term.status, term.exit_code = "exited", 0 # /exit
+ assert term.to_snapshot().running is False
+ term.exit_code = -1 # the host went away with the machine
+ assert term.to_snapshot().running is True
+ term.status, term.was_running = "pending", True
+ assert term.to_snapshot().running is True
+ # Older snapshots carry no field: every pane counts as running.
+ parsed = resume_store.SnapshotTerminal.from_dict({"name": "T1", "agent": "claude"})
+ assert parsed is not None and parsed.running is True