an open-source alternative to the dots, bots and muses of the world, run an office of claude code/codex like agents on your laptop, sandboxes or anywhere, uses your existing subscriptions
HTML agent-harnessagent-orchestrationagentsai-agentsautonomous-agentsclaude-codecodexcoding-agentsdesktop-appdeveloper-toolselectrongemini-cliharnessharness-engineeringlocal-firstmemorymulti-agentopencodeorchestrationtypescript
fix(focus mode): survive closing an agent, and remember it as the default view
Chaitanya Giri09ed0f3dd2f5
diff --git a/src/renderer/src/store/focusMode.ts b/src/renderer/src/store/focusMode.tsnew file mode 100644index 00000000..9786a12e--- /dev/null+++ b/src/renderer/src/store/focusMode.ts@@ -0,0 +1,55 @@+/**+ * Focus mode: which agent the full-window terminal is showing, and whether the+ * user wants that view by default.+ *+ * Kept out of the store, and structural rather than typed against `Agent`, so+ * the rules below are testable without dragging zustand into a unit test. Same+ * reasoning as `hooks/queueDelivery.ts`.+ */++/** The subset of an agent these rules need. */+export interface FocusCandidate {+ id: string;+}++/**+ * Keep focus mode pointed at an agent that still exists.+ *+ * Every path that removes an agent already re-homes `selectedId`, but none of+ * them re-homed the focused id, so closing the agent you were focused on left it+ * pointing at nothing. `App.tsx` then finds no agent to render and drops the+ * whole window back to the sidebar, which reads as the app deciding to leave+ * focus mode on your behalf.+ *+ * Closing an agent is not a request to leave focus mode. So follow the selection+ * instead, and only fall out once the last agent is gone.+ *+ * Applies to every removal path, including `reconcileWithLivePtys`: that one runs+ * at STARTUP and prunes agents whose PTY did not survive, so without it a+ * restored focus-mode preference would be undone by the first reconcile.+ */+export function refocusAfterRemoval(+ fullscreenAgentId: string | null,+ agents: FocusCandidate[],+ selectedId: string | null+): string | null {+ if (fullscreenAgentId === null) return null;+ if (agents.some((a) => a.id === fullscreenAgentId)) return fullscreenAgentId;+ return selectedId ?? agents[0]?.id ?? null;+}++/**+ * Resolve a persisted focus-mode preference into an agent to focus on load.+ *+ * The preference is stored as a BOOLEAN, deliberately not as the focused agent's+ * id. An id is meaningless across a restart: that agent may have been closed, or+ * its PTY may not come back, and restoring a stale one lands straight in the+ * dangling reference `refocusAfterRemoval` exists to prevent. The preference+ * means "I work in focus mode", so it resolves against whoever is selected now.+ */+export function focusOnLoad(+ prefersFocusMode: boolean,+ selectedId: string | null+): string | null {+ return prefersFocusMode ? selectedId : null;+}diff --git a/src/renderer/src/store/store.ts b/src/renderer/src/store/store.tsindex f3d55c71..ea151f9d 100644--- a/src/renderer/src/store/store.ts+++ b/src/renderer/src/store/store.ts@@ -16,6 +16,7 @@ import { DEFAULT_ORG_TRIGGER, type OrgTriggerConfig, type WebhookTrigger } fromimport { isCompactionCommand } from '@shared/providerAutomation';import { preferredAgentRole } from '@shared/agentRole';import { isInboxNudge } from '@shared/hiveNudge';+import { refocusAfterRemoval, focusOnLoad } from './focusMode';export type ToolKind =| 'Read' | 'Edit' | 'Write' | 'Bash' | 'WebFetch' | 'WebSearch'@@ -322,6 +323,7 @@ const LS_ARCHIVED = 'cth.archivedAgents';const LS_RESTORABLE = 'cth.restorableAgents';const LS_SELECTED = 'cth.selectedId';const LS_QUEUES = 'cth.messageQueues';+const LS_FOCUS_MODE = 'cth.prefersFocusMode';// Fields that are large or transient — not worth persisting across reloads.// contextTokens/contextLimit describe a LIVE session; persisting them showed a@@ -576,6 +578,21 @@ const initialSidebarTab: SidebarTab = (() => {return 'terminal';})();+/** Does the user want focus mode as their default view?+ *+ * Persisted as a BOOLEAN, deliberately not as the focused agent's id. The id is+ * meaningless across a restart: that agent may have been closed, or its PTY may+ * not come back, and restoring a stale one lands us straight in the dangling+ * reference `refocusAfterRemoval` exists to prevent. The preference is "I work+ * in focus mode", so on load we resolve it against whoever is selected now. */+const initialPrefersFocusMode = (() => {+ try {+ return window.localStorage.getItem(LS_FOCUS_MODE) === '1';+ } catch { /* noop */ }+ return false;+})();++const initialAgents = loadPersistedAgents();const initialArchivedAgents = loadPersistedArchived();const initialRestorableAgents = loadPersistedRestorable();@@ -615,7 +632,7 @@ export const useStore = create<State>((set) => ({ccTabRequest: null,requestCommandCenterTab: (tab) =>set((s) => ({ ccTabRequest: { tab, seq: (s.ccTabRequest?.seq ?? 0) + 1 } })),- fullscreenAgentId: null,+ fullscreenAgentId: focusOnLoad(initialPrefersFocusMode, initialSelectedId),fullscreenFilePath: null,fullscreenFileView: 'edit',ideInitialFile: null,@@ -738,9 +755,10 @@ export const useStore = create<State>((set) => ({const { [id]: _gone, ...feeds } = s.feeds;const { [id]: _queueGone, ...messageQueues } = s.messageQueues;const selectedId = s.selectedId === id ? (agents[0]?.id ?? null) : s.selectedId;+ const fullscreenAgentId = refocusAfterRemoval(s.fullscreenAgentId, agents, selectedId);persistAgents(agents, selectedId);if (_queueGone) persistQueues(messageQueues);- return { agents, feeds, selectedId, messageQueues };+ return { agents, feeds, selectedId, messageQueues, fullscreenAgentId };}),archiveAgent: (id) =>set((s) => {@@ -761,10 +779,11 @@ export const useStore = create<State>((set) => ({const { [id]: _feedGone, ...feeds } = s.feeds;const { [id]: _queueGone, ...messageQueues } = s.messageQueues;const selectedId = s.selectedId === id ? (agents[0]?.id ?? null) : s.selectedId;+ const fullscreenAgentId = refocusAfterRemoval(s.fullscreenAgentId, agents, selectedId);persistAgents(agents, selectedId);persistArchived(archivedAgents);if (_queueGone) persistQueues(messageQueues);- return { agents, archivedAgents, feeds, selectedId, messageQueues };+ return { agents, archivedAgents, feeds, selectedId, messageQueues, fullscreenAgentId };}),removeArchivedAgent: (id) =>set((s) => {@@ -912,9 +931,10 @@ export const useStore = create<State>((set) => ({const selectedId = agents.some((a) => a.id === s.selectedId)? s.selectedId: (agents[0]?.id ?? null);+ const fullscreenAgentId = refocusAfterRemoval(s.fullscreenAgentId, agents, selectedId);persistAgents(agents, selectedId);persistRestorable(restorableAgents);- return { agents, feeds, selectedId, restorableAgents };+ return { agents, feeds, selectedId, restorableAgents, fullscreenAgentId };}),setAddAgentOpen: (open) => set({ addAgentOpen: open }),hireQueue: EMPTY_HIRE_QUEUE,@@ -927,7 +947,13 @@ export const useStore = create<State>((set) => ({clearPendingHires: () => set((s) => ({hireQueue: clearHireQueue(s.hireQueue)})),- setFullscreen: (id) => set({ fullscreenAgentId: id }),+ setFullscreen: (id) => {+ // Entering focus mode makes it the default view; leaving it clears that.+ // Only an explicit toggle writes the preference, so an agent closing under+ // you never silently changes how the app opens next time.+ try { window.localStorage.setItem(LS_FOCUS_MODE, id ? '1' : '0'); } catch { /* noop */ }+ set({ fullscreenAgentId: id });+ },setFullscreenFile: (path, view) => set({ fullscreenFilePath: path, fullscreenFileView: view ?? 'edit' }),// Closing CLEARS the target: the id is scoped to one IDE session, and a stale// one left behind would silently win over the selection on the next open fromdiff --git a/test/focus-mode.test.cjs b/test/focus-mode.test.cjsnew file mode 100644index 00000000..b3b398f1--- /dev/null+++ b/test/focus-mode.test.cjs@@ -0,0 +1,56 @@+'use strict';++const test = require('node:test');+const assert = require('node:assert/strict');+const loadTs = require('./load-ts.cjs');++const { refocusAfterRemoval, focusOnLoad } =+ loadTs('src/renderer/src/store/focusMode.ts');++const agents = (...ids) => ids.map((id) => ({ id }));++// --- re-homing focus mode when an agent goes away --------------------------+// Closing an agent is not a request to leave focus mode. Before this, every+// removal path re-homed selectedId and left fullscreenAgentId dangling, so the+// window fell back to the sidebar the moment you closed the agent you were+// focused on.++test('focus stays put when some OTHER agent is removed', () => {+ assert.equal(refocusAfterRemoval('b', agents('a', 'b', 'c'), 'a'), 'b');+});++test('focus follows the selection when the focused agent is removed', () => {+ assert.equal(refocusAfterRemoval('b', agents('a', 'c'), 'c'), 'c',+ 'the removal path already picked a new selection, focus mode should honour it');+});++test('focus falls back to the first agent when the selection is also gone', () => {+ assert.equal(refocusAfterRemoval('b', agents('a', 'c'), null), 'a');+});++test('focus mode ends only once the last agent is gone', () => {+ assert.equal(refocusAfterRemoval('b', agents(), null), null);+});++test('a window that was not in focus mode is never dragged into it', () => {+ assert.equal(refocusAfterRemoval(null, agents('a', 'b'), 'a'), null,+ 'removing an agent must not turn focus mode ON');+});++// --- restoring the preference on load --------------------------------------+// The preference is a boolean, not an id: the previously focused agent may not+// exist next launch, and restoring a stale id recreates the dangling reference+// above.++test('focus mode is restored against whoever is selected now', () => {+ assert.equal(focusOnLoad(true, 'a'), 'a');+});++test('no preference means the sidebar, as before', () => {+ assert.equal(focusOnLoad(false, 'a'), null);+});++test('the preference resolves to nothing when there is no agent to focus', () => {+ assert.equal(focusOnLoad(true, null), null,+ 'first run, or every agent gone: nothing to show in focus mode');+});