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(agentic-ide): bring remote panes back safely and fork them whole
rubenluetke10-beep93005c9f71ac
diff --git a/jarvis/agentic_ide/session.py b/jarvis/agentic_ide/session.pyindex 04c4e4e0f8..efac1055b3 100644--- a/jarvis/agentic_ide/session.py+++ b/jarvis/agentic_ide/session.py@@ -1959,6 +1959,21 @@ def _copy_key(local: str) -> tuple[str, Path | None]:return os.path.normcase(os.path.realpath(str(top) if top else local)), top+def _changed_since(top: Path, snapshot: str) -> bool:+ """Whether repo ``top``'s working tree differs from offload ``snapshot``.++ Blocking (git). The same comparison ``remote.pull_code`` makes before it+ applies a copy's work; a snapshot git cannot read counts as unchanged,+ as joining did before the check existed.+ """+ try:+ offload_tree = remote._git(top, "rev-parse", f"{snapshot}^{{tree}}")+ return remote.working_tree_id(top) != offload_tree+ except (remote.MoveError, OSError) as exc:+ logger.info("Agentic IDE: offload snapshot {} unreadable: {}", snapshot[:12], exc)+ return False++class Registry:"""Process-wide holder of the open Agentic-IDE workspaces.@@ -5174,6 +5189,8 @@ class Registry:permission_mode: str | None = None,computer_id: Any = INHERIT_PLACEMENT,folder: str = "",+ fork_from: ResumeHandle | None = None,+ branch: str = "",) -> Terminal:"""Open one more terminal in the running workspace.@@ -5215,7 +5232,9 @@ class Registry:of it runs on one computer. A pane for a computer is set up therebefore it can start (:meth:`_place_new`) and removed again if thatfails. ``folder`` opens it in another folder than the workspace's (a- worktree fork).+ worktree fork); ``fork_from`` and ``branch`` make the pane a fork+ (:meth:`fork_terminal`) from the moment it exists, so no viewer can+ spawn it as a fresh chat first."""selected_id = workspace_id or self.active_idasync with self._lock:@@ -5311,6 +5330,8 @@ class Registry:effort=launch_picks.normalize_effort(chosen, effort),permission_mode=launch_picks.normalize_permission(chosen, permission_mode),folder=folder,+ branch=branch,+ fork_from=fork_from,computer_id=target,placing=self._placing_note(target) if target else "",)@@ -5419,12 +5440,20 @@ class Registry:f"{term.name} runs on {self._computer_label(term.computer_id)}. ""Bring it back to this computer first.")+ if not target:+ back, failures, messages = await self._bring_back_group([(session, term)])+ for failed, error in failures:+ if failed is term:+ raise PlacementError(error)+ for other in back:+ if other is not term:+ messages.append(+ f"{other.name} came back with it: they worked in one copy there."+ )+ return {"moved": True, "message": " ".join(messages), "terminal": term.to_dict()}async with term.attach_lock:was_live = bool(term.pty_id)- if target:- message = await self._offload_locked(session, term, target, {})- else:- message = await self._bring_back_locked(session, term)+ message = await self._offload_locked(session, term, target, {})if was_live:await self._restart_in_place(session, term)await self._persist()@@ -5472,42 +5501,91 @@ class Registry:return {"moved": moved, "messages": messages}async def _bring_workspace_back(self, session: Session) -> dict[str, Any]:- """Bring every remote pane of ``session`` home, one folder transfer per folder.+ """Bring every remote pane of ``session`` home (see :meth:`_bring_back_group`).++ Fails only when no pane came back; otherwise the messages name each+ pane that stayed on its computer and why.+ """+ away = [(session, t) for t in session.terminals if t.computer_id and not t.placing]+ back, failures, messages = await self._bring_back_group(away)+ if failures and not back:+ raise PlacementError(" ".join(messages))+ return {"moved": [t.key for t in back], "messages": messages}++ async def _bring_back_group(+ self, panes: list[tuple[Session, Terminal]]+ ) -> tuple[list[Terminal], list[tuple[Terminal, str]], list[str]]:+ """Bring ``panes`` home together, one folder transfer per shared copy.++ Returns ``(back, failures, messages)``. Every pane that works in the+ same copy as one of ``panes`` comes along, from any workspace: a copy+ is one folder, and returning it for one pane applied a sibling's+ half-done work here while the sibling kept working there (#253).Every agent stops BEFORE any folder is packed — a sibling still writingwhile the first pane's copy came home lost its last edits — and thepanes are gated meanwhile so no viewer restarts one there. Each(computer, folder) comes back once however many panes shared it: onereturn per pane used to make a second branch, collide on its name and- leave the workspace half moved.+ leave the workspace half moved. A pane whose return fails stays on its+ computer and runs there again if it was running; one failure used to+ abort the loop and strand every later pane stopped on the server (#252)."""- away = [t for t in session.terminals if t.computer_id and not t.placing]- moved: list[str] = []+ panes = await self._with_copy_sharers(panes)+ back: list[Terminal] = []+ failures: list[tuple[Terminal, str]] = []messages: list[str] = []- live: set[str] = set()+ live: set[int] = set()returns: dict[tuple[str, str], remote.Return] = {}- for term in away:+ for _session, term in panes:term.placing = "Bringing the work back to this computer…"try:- for term in away:+ for _session, term in panes:async with term.attach_lock:if term.pty_id:- live.add(term.key)+ live.add(id(term))await self._stop_for_move(term, self._pool(term))- for term in away:+ for session, term in panes:async with term.attach_lock:- message = await self._bring_back_locked(session, term, returns)+ where = self._computer_label(term.computer_id)+ try:+ message = await self._bring_back_locked(session, term, returns)+ except Exception as exc: # noqa: BLE001 - reported per pane, the rest go on+ logger.warning("Agentic IDE: {} stays on {}: {}", term.name, where, exc)+ message = f"{term.name} stays on {where}: {exc}"+ failures.append((term, str(exc)))+ else:+ back.append(term)term.placing = ""- if term.key in live:+ if id(term) in live:await self._restart_in_place(session, term)- moved.append(term.key)if message and message not in messages:messages.append(message)finally:- for term in away:+ for _session, term in panes:term.placing = ""await self._persist()- return {"moved": moved, "messages": messages}+ return back, failures, messages++ async def _with_copy_sharers(+ self, panes: list[tuple[Session, Terminal]]+ ) -> list[tuple[Session, Terminal]]:+ """``panes`` plus every other remote pane that works in one of their copies."""+ copies: set[tuple[str, str]] = set()+ for session, term in panes:+ key, _top = await self._copy_root(term.cwd(session.folder))+ copies.add((term.computer_id, key))+ chosen = {id(term) for _session, term in panes}+ result = list(panes)+ for session in list(self._sessions.values()):+ for other in list(session.terminals):+ if id(other) in chosen or not other.remote_folder or other.placing:+ continue+ key, _top = await self._copy_root(other.cwd(session.folder))+ if (other.computer_id, key) in copies:+ result.append((session, other))+ chosen.add(id(other))+ return resultasync def _stop_for_move(self, term: Terminal, pool: Any) -> None:"""End the pane's current process and wait until its exit is recorded."""@@ -5644,8 +5722,24 @@ class Registry:Caller holds the copy lock for ``key``. The same repo seen from anothersubfolder maps to the matching subfolder of the copy.++ A copy another pane made earlier is joined only while this folder is+ still what that copy started from: after local edits it would run on+ old code without anyone saying so, and a refresh would rewrite files+ under the agent working there (#253)."""- known = placements.get(key) or await self._sibling_placement(computer_id, key, exclude)+ known = placements.get(key)+ if known is None:+ known = await self._sibling_placement(computer_id, key, exclude)+ snapshot = known[0].offload_snapshot if known is not None else None+ if top is not None and snapshot and await asyncio.to_thread(+ _changed_since, top, snapshot+ ):+ raise remote.MoveError(+ f"This folder changed since the copy on {self._computer_label(computer_id)} "+ "was made, and another pane still works in that copy. Bring that pane "+ "back first, then move this one."+ )if known is None:placement = await remote.push_code(pool, Path(local))placements[key] = (placement, local)@@ -5844,13 +5938,12 @@ class Registry:effort=source.effort,permission_mode=source.permission_mode,folder=folder,+ # Born a fork: a pane for a computer opens its ``placing`` gate+ # and persists before add_terminal returns, so a viewer can spawn+ # it inside that await (#254).+ fork_from=source.resume,+ branch=branch,)- # No await between the pane's creation and these, so no viewer can- # attach (and spawn) before the pane knows it is a fork. (A pane for- # a computer was gated by ``placing`` until add_terminal returned.)- term.branch = branch- term.fork_from = source.resume- await self._persist()logger.info("Agentic IDE: forked {} into {}{}",source.name,diff --git a/tests/unit/agentic_ide/test_remote_placement.py b/tests/unit/agentic_ide/test_remote_placement.pyindex 25ec424f3e..d4ade48b4f 100644--- a/tests/unit/agentic_ide/test_remote_placement.py+++ b/tests/unit/agentic_ide/test_remote_placement.py@@ -392,3 +392,151 @@ async def test_a_subfolder_of_the_same_repo_joins_the_running_copy(assert len(_pushes(moves)) == 1, "one repo, one copy — a second send reset it"assert inner.remote_folder == "/home/u/jarvis-workspaces/app-abc123/pkg"+++# -- bringing panes back (#252, #253) and forking on a computer (#254) ------------+++async def test_one_failed_return_does_not_strand_the_other_panes(+ pools: tuple[FakePtyManager, RemotePool, list[Any]],+ tmp_path: Path,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """#252: every agent stops before any folder is packed; one pane's failed+ return used to abort the loop and leave the rest stopped on the server."""+ local, far, moves = pools+ other = tmp_path / "other"+ other.mkdir()++ async def pull_code(_pool: object, folder: Path, rf: str, snap: str | None, name: str) -> Any:+ moves.append(("pull", folder))+ if folder == Path(str(tmp_path)):+ raise remote.MoveError("The server's copy of this folder is gone.")+ return remote.Return(branch="jarvis/vps/1", applied=True, message="back")++ monkeypatch.setattr(remote, "pull_code", pull_code)+ registry = ide.Registry(pty_manager=local)+ session = await registry.start(str(tmp_path), [{"agent": "claude"}], computer_id="c_1")+ await registry.add_terminal(workspace_id=session.id, computer_id="c_1", folder=str(other))+ first, second = session.terminals+ for term in session.terminals:+ await registry.attach(term.key, 100, 30, _sink, _gone, workspace_id=session.id)+ remote_spawns = len(far.spawns)++ result = await registry.place_workspace(session.id, computer_id=None)++ assert second.computer_id == "", "the healthy pane still came back"+ assert first.computer_id == "c_1", "the failed pane stays where its work is"+ assert first.pty_id and len(far.spawns) == remote_spawns + 1, "and runs there again"+ assert first.placing == "" and second.placing == ""+ assert any("stays on" in m and "is gone" in m for m in result["messages"])+ assert result["moved"] == [second.key]+++async def test_a_workspace_return_that_fails_everywhere_raises(+ pools: tuple[FakePtyManager, RemotePool, list[Any]],+ tmp_path: Path,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ local, _far, _moves = pools++ async def pull_code(*_args: Any) -> Any:+ raise remote.MoveError("The server's copy of this folder is gone.")++ monkeypatch.setattr(remote, "pull_code", pull_code)+ registry = ide.Registry(pty_manager=local)+ session = await registry.start(str(tmp_path), [{"agent": "claude"}], computer_id="c_1")++ with pytest.raises(ide.PlacementError, match="is gone"):+ await registry.place_workspace(session.id, computer_id=None)+ assert session.terminals[0].computer_id == "c_1"+++async def test_bringing_back_one_pane_of_a_shared_copy_brings_back_all_sharers(+ pools: tuple[FakePtyManager, RemotePool, list[Any]], tmp_path: Path+) -> None:+ """#253: one copy is one folder; returning it for T1 alone applied T2's+ half-done work here while T2 kept working there."""+ local, far, moves = pools+ registry = ide.Registry(pty_manager=local)+ session = await registry.start(+ str(tmp_path), [{"agent": "claude"}, {"agent": "claude"}], computer_id="c_1"+ )+ elsewhere = await registry.start(str(tmp_path), [{"agent": "claude"}], computer_id="c_1")+ first, second = session.terminals+ third = elsewhere.terminals[0]+ await registry.attach(second.key, 100, 30, _sink, _gone, workspace_id=session.id)+ second_remote = second.pty_id++ result = await registry.place_terminal(first.key, workspace_id=session.id, computer_id=None)++ assert [m for m in moves if m[0] == "pull"] == [+ ("pull", "/home/u/jarvis-workspaces/app-abc123", "f" * 40)+ ], "one return for the one copy"+ assert first.computer_id == second.computer_id == third.computer_id == ""+ assert second_remote in far.closed, "the sharer stopped before its copy was packed"+ assert second.pty_id and local.spawns, "and runs here again"+ assert f"{second.name} came back with it" in result["message"]+++async def test_a_new_pane_never_joins_a_copy_older_than_the_folder(+ pools: tuple[FakePtyManager, RemotePool, list[Any]],+ tmp_path: Path,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """#253: a pane added after local edits joined the stale copy and ran on+ old code without being told."""+ import subprocess++ local, _far, moves = pools+ subprocess.run(["git", "init", "-q", str(tmp_path)], check=True) # noqa: S603, S607, ASYNC221+ (tmp_path / "app.py").write_text("print('v1')\n", encoding="utf-8")++ async def push_code(_pool: object, folder: Path) -> remote.Placement:+ moves.append(("push", folder))+ snap, _tree, _held = remote.snapshot_commit(folder, "offload")+ return remote.Placement("/home/u/jarvis-workspaces/app-abc123", snap)++ monkeypatch.setattr(remote, "push_code", push_code)+ registry = ide.Registry(pty_manager=local)+ session = await registry.start(str(tmp_path), [{"agent": "claude"}], computer_id="c_1")++ unchanged = await registry.add_terminal(workspace_id=session.id, computer_id="c_1")+ assert unchanged.remote_folder and len(_pushes(moves)) == 1, "an unchanged folder joins"++ (tmp_path / "app.py").write_text("print('v2')\n", encoding="utf-8")+ with pytest.raises(ide.PlacementError, match="changed since the copy"):+ await registry.add_terminal(workspace_id=session.id, computer_id="c_1")+ assert len(session.terminals) == 2 and len(_pushes(moves)) == 1+++async def test_a_fork_on_a_computer_is_a_fork_before_its_gate_opens(+ pools: tuple[FakePtyManager, RemotePool, list[Any]],+ tmp_path: Path,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """#254: ``_place_new`` opens the ``placing`` gate and awaits a persist+ before ``add_terminal`` returns; a viewer attaching there spawned the fork+ as a fresh chat."""+ from jarvis.agentic_ide.agent_sessions import ResumeHandle++ local, _far, _moves = pools+ registry = ide.Registry(pty_manager=local)+ session = await registry.start(str(tmp_path), [{"agent": "claude"}], computer_id="c_1")+ source = session.terminals[0]+ source.resume = ResumeHandle(kind="claude_session", id="source-id", captured_at=1.0)+ seen: list[tuple[str, bool]] = []+ persist = registry._persist++ async def watching_persist() -> None:+ for term in session.terminals:+ if term is not source and not term.placing:+ seen.append((term.name, term.fork_from is not None))+ await persist()++ monkeypatch.setattr(registry, "_persist", watching_persist)++ forked = await registry.fork_terminal(source.name)++ assert forked.computer_id == "c_1"+ assert seen and all(is_fork for _name, is_fork in seen), seen