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.

fix(modes): the coding character lets go when the workspace does

Haraldcad8fce6977b
diff --git a/jarvis/agentic_ide/session.py b/jarvis/agentic_ide/session.py
index b7d4192e5f..1b542c229f 100644
--- a/jarvis/agentic_ide/session.py
+++ b/jarvis/agentic_ide/session.py
@@ -1897,6 +1897,10 @@ class Registry:
"""Bring ``session`` to the front. Caller holds the lock."""
self._active = session.id
session.last_active_at = time.time()
+ # A different workspace at the front can mean a different answer to
+ # "is coding mode on" — the flag is per workspace — so the persona
+ # follows the front, not only the toggle.
+ self._sync_coding_character()
async def _open_locked(
self,
@@ -2217,6 +2221,9 @@ class Registry:
async with self._lock:
if workspace_id is None:
self._active = None
+ # No workspace on screen is no coding mode, whatever the flags
+ # on the workspaces behind the wizard say.
+ self._sync_coding_character()
return None
session = self._sessions.get(workspace_id)
if session is None:
@@ -2425,6 +2432,12 @@ class Registry:
self._active = survivor.id if survivor else None
if survivor is not None:
self._focus_locked(survivor)
+ else:
+ # The last workspace is gone; the coding character must go
+ # with it. This is the second life of the bug this override was
+ # built against — coding mode stuck on for the whole session
+ # after the workspace it belonged to had been closed.
+ self._sync_coding_character()
# Deliberately NOT re-written here. The restore point is refreshed by
# activity — opening a workspace, adding a pane, connecting one — and
# closing is not activity. Rewriting on close made the offer shrink one
@@ -2449,21 +2462,30 @@ class Registry:
if session is None:
if enabled:
raise SessionError("No Agentic-IDE session is running — open one first.")
- self._sync_coding_character(False)
+ self._sync_coding_character()
return False
session.focus_mode = bool(enabled)
logger.info("Agentic IDE focus mode {}", "on" if enabled else "off")
- self._sync_coding_character(session.focus_mode)
+ self._sync_coding_character()
return session.focus_mode
- @staticmethod
- def _sync_coding_character(enabled: bool) -> None:
- """Point the persona layer at the ``coding`` mode while focus mode is on.
+ def _sync_coding_character(self) -> None:
+ """Point the persona layer at the ``coding`` mode exactly while coding
+ mode is in effect — and at nothing the moment it is not.
+
+ Derived from the registry's own state (the workspace at the front AND
+ its focus flag, the same two halves ``coding_mode_active`` reads) rather
+ than passed in by the caller, so no transition can leave the persona
+ saying "coding" while the predicate says "no workspace". Called from
+ every place that state changes: the toggle, a workspace coming to the
+ front, the front being cleared, and the last workspace closing.
Best-effort by design: the workspace context block is the part users
depend on, so a missing or renamed ``coding`` mode costs the tone and
nothing else.
"""
+ session = self.session
+ enabled = session is not None and bool(session.focus_mode)
try:
from jarvis.brain import modes
diff --git a/jarvis/brain/modes.py b/jarvis/brain/modes.py
index fbf9f1dfc8..b47b458ee4 100644
--- a/jarvis/brain/modes.py
+++ b/jarvis/brain/modes.py
@@ -361,6 +361,21 @@ def _configured_slug() -> str:
return slug or DEFAULT_MODE
+def chosen_slug() -> str:
+ """The user's own, sticky choice — what ``[persona] active_mode`` names.
+
+ Distinct from :func:`active_slug` on purpose: while a section override is
+ in force the two differ, and a UI that can only show the one in force has no
+ way to confirm that a switch was stored ("you picked Friend; Coding is on
+ until you leave the IDE"). Falls back to the default when the stored slug
+ names a mode that no longer exists, for the same reason ``active_slug`` does.
+ """
+ candidate = _configured_slug()
+ if get_mode(candidate) is not None:
+ return candidate
+ return DEFAULT_MODE
+
+
def active_slug() -> str:
"""The slug in force for this turn — section override first, then config.
@@ -695,6 +710,7 @@ __all__ = [
"active_prompt_block",
"active_slug",
"active_voice",
+ "chosen_slug",
"delete_mode",
"get_mode",
"has_user_copy",
diff --git a/jarvis/ui/web/modes_routes.py b/jarvis/ui/web/modes_routes.py
index 5ca43f77d1..dbab12c57b 100644
--- a/jarvis/ui/web/modes_routes.py
+++ b/jarvis/ui/web/modes_routes.py
@@ -73,6 +73,11 @@ def _payload() -> dict[str, object]:
return {
"modes": [m.to_payload() for m in modes.list_modes()],
"active": active,
+ # The user's own choice, which is what a switch on this screen writes.
+ # Equal to ``active`` except while a section override is in force —
+ # and then it is the only way the UI can confirm the switch was
+ # stored rather than silently swallowed.
+ "chosen": modes.chosen_slug(),
# What the section override is doing, if anything. The UI needs to be
# able to say "coding mode is on because you are in the Agentic IDE"
# rather than showing a switch the user did not touch and cannot
@@ -109,7 +114,9 @@ async def set_active(body: ActiveModeBody) -> dict[str, object]:
mode = modes.set_active(body.slug)
except modes.ModeError as exc:
raise HTTPException(status_code=404, detail=str(exc)) from exc
- return {"ok": True, "chosen": mode.slug, "restart_required": False, **_payload()}
+ # ``chosen`` after the payload on purpose: this handler KNOWS what was just
+ # written, and must not be second-guessed by a config read that could lag.
+ return {"ok": True, **_payload(), "chosen": mode.slug, "restart_required": False}
@router.post("", summary="Create or replace a mode")
diff --git a/tests/integration/test_modes_routes.py b/tests/integration/test_modes_routes.py
index 62f21a6173..484ee4125b 100644
--- a/tests/integration/test_modes_routes.py
+++ b/tests/integration/test_modes_routes.py
@@ -111,5 +111,19 @@ def test_the_response_reports_the_mode_actually_in_force(client: TestClient) ->
assert body["chosen"] == "friend"
assert body["active"] == modes.MODE_CODING
assert body["section_override"] == modes.MODE_CODING
+ # And a plain read afterwards still tells the two apart: the switch was
+ # STORED (chosen) even though something else is in force (active). A
+ # payload that only carried ``active`` left the modes screen unable to
+ # confirm a click had done anything at all.
+ listing = client.get("/api/modes").json()
+ assert listing["chosen"] == "friend"
+ assert listing["active"] == modes.MODE_CODING
finally:
modes.set_section_override(None)
+
+
+def test_chosen_equals_active_when_nothing_overrides(client: TestClient) -> None:
+ client.put("/api/modes/active", json={"slug": "coach"})
+ body = client.get("/api/modes").json()
+ assert body["chosen"] == body["active"] == "coach"
+ assert body["section_override"] == ""
diff --git a/tests/unit/agentic_ide/test_coding_mode.py b/tests/unit/agentic_ide/test_coding_mode.py
index 4717daa15d..489b00f788 100644
--- a/tests/unit/agentic_ide/test_coding_mode.py
+++ b/tests/unit/agentic_ide/test_coding_mode.py
@@ -116,3 +116,89 @@ async def test_event_reaches_the_ui_as_a_ws_envelope(
assert envelope["event_name"] == "AgenticIdeCodingModeChanged"
assert envelope["payload"]["enabled"] is True
+
+
+# --------------------------------------------------- the persona follows the mode
+#
+# Coding mode also swaps the assistant's CHARACTER (the ``coding`` mode) through
+# an in-memory section override on the persona layer. That override must agree
+# with ``coding_mode_active`` after EVERY transition, not only after the toggle:
+# left behind by a closed or switched-away workspace it kept the assistant in
+# coding character for the rest of the process, and — because the override
+# outranks the user's choice — made the modes screen look as if switching modes
+# did nothing at all.
+
+
+def _persona_override() -> str | None:
+ from jarvis.brain import modes
+
+ return modes.section_override()
+
+
[email protected](autouse=True)
+def _clear_persona_override():
+ from jarvis.brain import modes
+
+ modes.set_section_override(None)
+ yield
+ modes.set_section_override(None)
+
+
+async def test_the_persona_override_tracks_the_toggle(
+ wired: Registry, tmp_path: Path
+) -> None:
+ await wired.start(str(tmp_path), [{"agent": "claude"}])
+ assert _persona_override() is None
+ wired.set_focus_mode(True)
+ assert _persona_override() == "coding"
+ wired.set_focus_mode(False)
+ assert _persona_override() is None
+
+
+async def test_closing_the_last_workspace_drops_the_persona_override(
+ wired: Registry, tmp_path: Path
+) -> None:
+ """The exact bug: coding character stuck on after the workspace was closed."""
+ await wired.start(str(tmp_path), [{"agent": "claude"}])
+ wired.set_focus_mode(True)
+ await wired.end()
+ assert coding_mode_active() is False
+ assert _persona_override() is None
+
+
+async def test_clearing_the_front_drops_the_persona_override(
+ wired: Registry, tmp_path: Path
+) -> None:
+ """The wizard for another workspace takes the front away — and the mode."""
+ await wired.start(str(tmp_path), [{"agent": "claude"}])
+ wired.set_focus_mode(True)
+ await wired.activate(None)
+ assert coding_mode_active() is False
+ assert _persona_override() is None
+
+
+async def test_switching_workspaces_moves_the_persona_override_with_the_flag(
+ wired: Registry, tmp_path: Path
+) -> None:
+ """The flag is per workspace, so the persona follows whichever is in front."""
+ (tmp_path / "a").mkdir()
+ (tmp_path / "b").mkdir()
+ first = await wired.start(str(tmp_path / "a"), [{"agent": "claude"}])
+ wired.set_focus_mode(True)
+ second = await wired.start(str(tmp_path / "b"), [{"agent": "claude"}])
+ # A freshly opened workspace is at the front with the mode OFF.
+ assert wired.session is second
+ assert coding_mode_active() is False
+ assert _persona_override() is None
+
+ await wired.activate(first.id)
+ assert coding_mode_active() is True
+ assert _persona_override() == "coding"
+
+ # Closing the front workspace hands the front to the survivor, whose flag
+ # is off — the override must not linger from the one that just closed.
+ await wired.end(first.id)
+ assert wired.session is second
+ assert coding_mode_active() is False
+ assert _persona_override() is None
+