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(agentic-ide): coding mode is scoped to its section again

rubenluetke10-beepaa7121e5b8d4
diff --git a/jarvis/agentic_ide/session.py b/jarvis/agentic_ide/session.py
index 1d70301680..b7d4192e5f 100644
--- a/jarvis/agentic_ide/session.py
+++ b/jarvis/agentic_ide/session.py
@@ -2436,16 +2436,41 @@ class Registry:
logger.info("Agentic IDE session ended: {}", session.id)
def set_focus_mode(self, enabled: bool) -> bool:
- """Turn the focused coding mode on/off. Returns the resulting state."""
+ """Turn the focused coding mode on/off. Returns the resulting state.
+
+ Also swaps the assistant's *character* to the built-in ``coding`` mode,
+ because "focused coding mode" was only ever half a mode: it added the
+ workspace facts to the prompt but left the tone alone. It is applied as
+ a SECTION OVERRIDE, which is in-memory and never persisted — so this
+ cannot overwrite the mode the user actually chose, and cannot survive a
+ restart the way the old sticky focus flag did.
+ """
session = self.session
if session is None:
if enabled:
raise SessionError("No Agentic-IDE session is running — open one first.")
+ self._sync_coding_character(False)
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)
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.
+
+ 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.
+ """
+ try:
+ from jarvis.brain import modes
+
+ modes.set_section_override(modes.MODE_CODING if enabled else None)
+ except Exception as exc: # noqa: BLE001 - never fail a toggle on the tone
+ logger.debug("Coding-mode character not applied: {}", exc)
+
def set_surface_context(
self,
*,
diff --git a/jarvis/ui/web/frontend/src/views/AgenticIdeView.test.tsx b/jarvis/ui/web/frontend/src/views/AgenticIdeView.test.tsx
index 933897db0a..43e1801097 100644
--- a/jarvis/ui/web/frontend/src/views/AgenticIdeView.test.tsx
+++ b/jarvis/ui/web/frontend/src/views/AgenticIdeView.test.tsx
@@ -841,6 +841,74 @@ describe("Agentic IDE running workspace", () => {
);
});
+ /*
+ * The mode is scoped to this section, and the signal is `onScreen`.
+ *
+ * This view is sticky — MainView keeps it mounted so a workspace survives a
+ * trip to Settings — so there is no unmount to clean up on. That is exactly
+ * how the mode came to be permanently on: this view switched it on and
+ * nothing ever switched it off, leaving the assistant in coding mode on every
+ * screen for the rest of the session.
+ */
+ it("hands an auto-enabled coding mode back when the section is left", async () => {
+ vi.mocked(api.fetchIdeState).mockResolvedValue(
+ stateWith(sessionWith(["Mika"])),
+ );
+ vi.mocked(api.setFocusMode).mockResolvedValue(true);
+ const { rerender } = render(<AgenticIdeView onScreen />);
+
+ await waitFor(() => expect(api.setFocusMode).toHaveBeenCalledWith(true));
+
+ vi.mocked(api.setFocusMode).mockResolvedValue(false);
+ rerender(<AgenticIdeView onScreen={false} />);
+ await waitFor(() => expect(api.setFocusMode).toHaveBeenLastCalledWith(false));
+ });
+
+ it("switches the mode back on when the section is returned to", async () => {
+ vi.mocked(api.fetchIdeState).mockResolvedValue(
+ stateWith(sessionWith(["Mika"])),
+ );
+ vi.mocked(api.setFocusMode).mockResolvedValue(true);
+ const { rerender } = render(<AgenticIdeView onScreen />);
+ await waitFor(() => expect(api.setFocusMode).toHaveBeenCalledWith(true));
+
+ vi.mocked(api.setFocusMode).mockResolvedValue(false);
+ rerender(<AgenticIdeView onScreen={false} />);
+ await waitFor(() => expect(api.setFocusMode).toHaveBeenLastCalledWith(false));
+
+ // The cached session still claims focus_mode is on, which is precisely why
+ // the auto-enable gate reads the local flag instead of the session.
+ vi.mocked(api.setFocusMode).mockResolvedValue(true);
+ rerender(<AgenticIdeView onScreen />);
+ await waitFor(() => expect(api.setFocusMode).toHaveBeenLastCalledWith(true));
+ });
+
+ it("leaves a hand-toggled coding mode alone when the section is left", async () => {
+ vi.mocked(api.fetchIdeState).mockResolvedValue(
+ stateWith(sessionWith(["Mika"], true)),
+ );
+ vi.mocked(api.setFocusMode).mockResolvedValue(true);
+ const { rerender } = render(<AgenticIdeView onScreen />);
+
+ const toggle = await screen.findByTestId("agentic-focus-toggle");
+ // Off by hand, then on by hand: now the mode is the user's, not the screen's,
+ // so walking away must not take it back. This is what keeps "ask Jarvis
+ // about my terminals from another room" working.
+ vi.mocked(api.setFocusMode).mockResolvedValue(false);
+ fireEvent.click(toggle);
+ await waitFor(() => expect(api.setFocusMode).toHaveBeenLastCalledWith(false));
+ vi.mocked(api.setFocusMode).mockResolvedValue(true);
+ fireEvent.click(toggle);
+ await waitFor(() => expect(api.setFocusMode).toHaveBeenLastCalledWith(true));
+
+ const callsBeforeLeaving = vi.mocked(api.setFocusMode).mock.calls.length;
+ rerender(<AgenticIdeView onScreen={false} />);
+ await Promise.resolve();
+ expect(vi.mocked(api.setFocusMode).mock.calls.length).toBe(
+ callsBeforeLeaving,
+ );
+ });
+
it("sends a prompt to the selected terminal through the same endpoint voice uses", async () => {
vi.mocked(api.fetchIdeState).mockResolvedValue(
stateWith(sessionWith(["Mika", "Nova"])),
diff --git a/jarvis/ui/web/frontend/src/views/AgenticIdeView.tsx b/jarvis/ui/web/frontend/src/views/AgenticIdeView.tsx
index 008c094991..22fb7f2538 100644
--- a/jarvis/ui/web/frontend/src/views/AgenticIdeView.tsx
+++ b/jarvis/ui/web/frontend/src/views/AgenticIdeView.tsx
@@ -343,6 +343,21 @@ export function AgenticIdeView({ onScreen = true }: AgenticIdeViewProps) {
* assistant for a minute" works and does not get overridden a second later.
*/
const optedOutRef = useRef(false);
+ /*
+ * Was the mode switched on BY this screen, or by the user?
+ *
+ * It decides what happens when the section is left. An auto-enabled mode is
+ * on loan — it belongs to this screen and goes back when you walk away. A
+ * mode the user switched on by hand is theirs, and stays on everywhere, which
+ * is how "ask Jarvis about my terminals from the kitchen" keeps working.
+ *
+ * Without this distinction the mode simply never turned off: this view
+ * switched it on, nothing switched it off, and because the view is sticky
+ * (see MainView) there was not even an unmount to hang the cleanup on. The
+ * result was an assistant permanently in coding mode on every screen, with
+ * only a small badge admitting it.
+ */
+ const autoEnabledRef = useRef(false);
const [modeIntroFor, setModeIntroFor] = useState<string | null>(null);
/*
@@ -380,13 +395,21 @@ export function AgenticIdeView({ onScreen = true }: AgenticIdeViewProps) {
}, [session, loading]);
useEffect(() => {
- if (!session || session.focus_mode || optedOutRef.current) return;
+ // Gated on the LOCAL flag, not `session.focus_mode`: `session` is only
+ // refetched by `refresh`, so after this effect's counterpart hands the mode
+ // back on leaving, the cached session still claims the mode is on and
+ // returning to the section would never switch it on again. The local flag
+ // is the one that tracks what actually happened — and it is trustworthy
+ // here because `session` is non-null only once a refresh has landed, and
+ // that same refresh sets the flag from the backend.
+ if (!onScreen || !session || focusMode || optedOutRef.current) return;
let cancelled = false;
void (async () => {
try {
const on = await setFocusMode(true);
if (cancelled) return;
setFocus(on);
+ autoEnabledRef.current = on;
// First workspace on this machine: say once what just changed. A mode
// that switches silently is indistinguishable from a bug.
if (on && !hasSeenModeIntro()) setModeIntroFor(session.id);
@@ -397,7 +420,33 @@ export function AgenticIdeView({ onScreen = true }: AgenticIdeViewProps) {
return () => {
cancelled = true;
};
- }, [session]);
+ // `onScreen` belongs here: this view is sticky and stays mounted, so coming
+ // back to the section is a prop change, not a remount. Without it the mode
+ // would be given back on leaving and never picked up again on return.
+ }, [session, onScreen, focusMode]);
+
+ /*
+ * Leaving the section gives the mode back.
+ *
+ * The signal is `onScreen`, not an unmount — MainView keeps this view mounted
+ * on purpose so a dozen terminals survive a trip to Settings, which means
+ * there is no unmount to clean up on. Only an auto-enabled mode is returned;
+ * one the user switched on by hand is left alone.
+ *
+ * Failure is deliberately silent. This runs while the user is already looking
+ * at another section, and a toast about a mode belonging to the screen they
+ * just left would be noise pointing at nothing. The app-wide badge keeps
+ * telling the truth either way, because it re-reads the backend on every
+ * section change (useCodingMode, path 4).
+ */
+ useEffect(() => {
+ if (onScreen) return;
+ if (!autoEnabledRef.current) return;
+ autoEnabledRef.current = false;
+ void setFocusMode(false)
+ .then(() => setFocus(false))
+ .catch(() => {});
+ }, [onScreen]);
// Memoised so these arrays keep a stable identity across renders — see the
// note on buildPlan for what an unstable one costs.
@@ -740,6 +789,10 @@ export function AgenticIdeView({ onScreen = true }: AgenticIdeViewProps) {
// Remember a deliberate opt-out, or the auto-enable effect above would turn
// the mode straight back on and the switch would look broken.
optedOutRef.current = !enabled;
+ // Touching the switch takes ownership of the mode either way: switching it
+ // on by hand means "keep it on when I leave", switching it off means there
+ // is nothing left for the leave-handler to undo.
+ autoEnabledRef.current = false;
try {
setFocus(await setFocusMode(enabled));
} catch (e) {