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): actually come back in focus mode after a restart
Chaitanya Giridea56b63388e
diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsxindex 088ac0b9..2d7c2e76 100644--- a/src/renderer/src/App.tsx+++ b/src/renderer/src/App.tsx@@ -218,6 +218,20 @@ export function App() {return () => { cancelled = true; };}, [config?.onboardingComplete]);+ // Re-apply the persisted focus-mode preference as the roster fills in.+ //+ // Not a one-shot at store construction: at launch every restored agent still+ // carries the PREVIOUS session's PTY id, so the reconcile above prunes the lot+ // and correctly drops focus mode to null before god has respawned. The+ // preference therefore has to be re-checked once agents with live terminals+ // actually exist. `restoreFocusMode` is a no-op unless the preference is on and+ // focus mode is currently off, so re-running it on every roster change is safe+ // and pressing Esc stays sticky.+ useEffect(() => {+ if (!config?.onboardingComplete) return;+ useStore.getState().restoreFocusMode();+ }, [config?.onboardingComplete, agents]);+// Track viewport width for splitter clampinguseEffect(() => {const onResize = () => setVpWidth(window.innerWidth);diff --git a/src/renderer/src/components/FullscreenTerminal.tsx b/src/renderer/src/components/FullscreenTerminal.tsxindex 28187002..39ae1023 100644--- a/src/renderer/src/components/FullscreenTerminal.tsx+++ b/src/renderer/src/components/FullscreenTerminal.tsx@@ -258,11 +258,16 @@ export function FullscreenTerminal({ config }: FullscreenTerminalProps) {// agent rather than dropping the user out; leave only when nothing is left.// In an effect, not in render: setState during render is a React anti-pattern,// and hard-nulling here defeated the store's re-homing the same way onKill did.+ // `refocusFullscreen`, NOT `setFullscreen`: this is the app following the user,+ // not the user telling the app what they want. Going through the explicit+ // toggle here wrote `prefersFocusMode = false` every time an agent went away,+ // which is the same "fix the store, then overwrite it from a call site" trap+ // that broke closing an agent in focus mode.useEffect(() => {if (agent && agent.ptyId) return;const s = useStore.getState();const next = s.agents.find((a) => a.id !== agent?.id && a.ptyId);- s.setFullscreen(next?.id ?? null);+ s.refocusFullscreen(next?.id ?? null);}, [agent]);if (!agent || !agent.ptyId) return null;diff --git a/src/renderer/src/store/focusMode.ts b/src/renderer/src/store/focusMode.tsindex 9786a12e..a0127573 100644--- a/src/renderer/src/store/focusMode.ts+++ b/src/renderer/src/store/focusMode.ts@@ -10,6 +10,9 @@/** The subset of an agent these rules need. */export interface FocusCandidate {id: string;+ /** Present once the agent has a terminal. Absent for synthetic agents and for+ * a persisted agent whose PTY has not been re-established yet. */+ ptyId?: string;}/**@@ -53,3 +56,37 @@ export function focusOnLoad(): string | null {return prefersFocusMode ? selectedId : null;}++/**+ * Re-enter focus mode once there is something to focus on.+ *+ * `focusOnLoad` alone was not enough and the reason is a startup ordering one.+ * The store resolves the preference ONCE, while it is being constructed, against+ * the roster read from disk. Every agent in that roster still carries the PTY id+ * it had in the PREVIOUS session, and none of those PTYs exist yet, so the first+ * `reconcileWithLivePtys` correctly prunes the lot and `refocusAfterRemoval`+ * correctly returns null. By the time god respawns with a live terminal the+ * preference has already been read and discarded, so the app opens in the+ * sidebar with `cth.prefersFocusMode` still set to 1.+ *+ * So the preference has to be re-checked whenever the roster changes, not once+ * at construction.+ *+ * Restores only onto an agent that HAS a terminal. `FullscreenTerminal` renders+ * nothing without one and its own re-home effect would immediately bounce us+ * back out, which is a loop rather than a restore.+ *+ * Returns the current value unchanged when there is nothing to do, so callers+ * can compare by identity and skip the state write.+ */+export function restoreFocus(+ prefersFocusMode: boolean,+ fullscreenAgentId: string | null,+ agents: FocusCandidate[],+ selectedId: string | null+): string | null {+ if (!prefersFocusMode) return fullscreenAgentId;+ if (fullscreenAgentId) return fullscreenAgentId;+ const selected = agents.find((a) => a.id === selectedId && a.ptyId);+ return selected?.id ?? agents.find((a) => a.ptyId)?.id ?? null;+}diff --git a/src/renderer/src/store/store.ts b/src/renderer/src/store/store.tsindex ea151f9d..f390ed0e 100644--- a/src/renderer/src/store/store.ts+++ b/src/renderer/src/store/store.ts@@ -16,7 +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';+import { refocusAfterRemoval, focusOnLoad, restoreFocus } from './focusMode';export type ToolKind =| 'Read' | 'Edit' | 'Write' | 'Bash' | 'WebFetch' | 'WebSearch'@@ -171,6 +171,10 @@ interface State {feeds: Record<string, string[]>;addAgentOpen: boolean;fullscreenAgentId: string | null;+ /** Does the user work in focus mode by default? Persisted as a boolean, and+ * written ONLY by an explicit toggle. Kept in the store rather than read once+ * at construction because the roster arrives after the store does. */+ prefersFocusMode: boolean;fullscreenFilePath: string | null;/** How the fullscreen file overlay renders (v0.3.4): raw editor or rendered* markdown preview. Preview is the default when opened from a terminal link. */@@ -302,6 +306,12 @@ interface State {finishPendingHire: () => void;clearPendingHires: () => void;setFullscreen: (id: string | null) => void;+ /** Move focus mode WITHOUT touching the preference. For the paths that re-home+ * a focused agent that went away: the app is following the user, not being+ * told what the user wants. `setFullscreen` is the explicit toggle. */+ refocusFullscreen: (id: string | null) => void;+ /** Re-apply the persisted preference now that the roster has changed. */+ restoreFocusMode: () => void;setFullscreenFile: (path: string | null, view?: 'edit' | 'preview') => void;/** Open/close the IDE. `agentId` names the agent whose workspace it should* show; omit it only when the caller truly has no specific agent (the IDE@@ -633,6 +643,7 @@ export const useStore = create<State>((set) => ({requestCommandCenterTab: (tab) =>set((s) => ({ ccTabRequest: { tab, seq: (s.ccTabRequest?.seq ?? 0) + 1 } })),fullscreenAgentId: focusOnLoad(initialPrefersFocusMode, initialSelectedId),+ prefersFocusMode: initialPrefersFocusMode,fullscreenFilePath: null,fullscreenFileView: 'edit',ideInitialFile: null,@@ -950,10 +961,17 @@ export const useStore = create<State>((set) => ({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.+ // you never silently changes how the app opens next time. Every non-explicit+ // mover goes through `refocusFullscreen` instead.try { window.localStorage.setItem(LS_FOCUS_MODE, id ? '1' : '0'); } catch { /* noop */ }- set({ fullscreenAgentId: id });+ set({ fullscreenAgentId: id, prefersFocusMode: !!id });},+ refocusFullscreen: (id) => set({ fullscreenAgentId: id }),+ restoreFocusMode: () =>+ set((s) => {+ const id = restoreFocus(s.prefersFocusMode, s.fullscreenAgentId, s.agents, s.selectedId);+ return id === s.fullscreenAgentId ? s : { 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.cjsindex b3b398f1..2acad007 100644--- a/test/focus-mode.test.cjs+++ b/test/focus-mode.test.cjs@@ -4,7 +4,7 @@ const test = require('node:test');const assert = require('node:assert/strict');const loadTs = require('./load-ts.cjs');-const { refocusAfterRemoval, focusOnLoad } =+const { refocusAfterRemoval, focusOnLoad, restoreFocus } =loadTs('src/renderer/src/store/focusMode.ts');const agents = (...ids) => ids.map((id) => ({ id }));@@ -54,3 +54,47 @@ 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');});++// --- re-applying the preference once the roster is live --------------------+// `focusOnLoad` runs once, while the store is being built. At that moment every+// restored agent still carries the PREVIOUS session's PTY id, so the startup+// reconcile prunes them all and focus mode is correctly nulled before god has+// respawned. Nothing re-checked the preference afterwards, so the app opened in+// the sidebar with the flag still set to 1. That was the actual "closing and+// opening did not open in focus mode" bug.++const live = (...ids) => ids.map((id) => ({ id, ptyId: `pty-${id}` }));++test('focus mode re-enters once an agent with a terminal shows up', () => {+ assert.equal(restoreFocus(true, null, live('god'), 'god'), 'god');+});++test('the restore waits for a LIVE terminal, it does not latch onto a corpse', () => {+ assert.equal(restoreFocus(true, null, agents('god'), 'god'), null,+ 'a restored agent still holding last session PTY id is not focusable yet');+});++test('no preference means the roster changing never opens focus mode', () => {+ assert.equal(restoreFocus(false, null, live('a', 'b'), 'a'), null);+});++test('an explicit exit stays exited even as the roster keeps changing', () => {+ // Esc / the exit button write prefersFocusMode = false, so every later roster+ // change is a no-op. Without that the restore would fight the user.+ assert.equal(restoreFocus(false, null, live('a'), 'a'), null);+});++test('already in focus mode is left alone', () => {+ assert.equal(restoreFocus(true, 'b', live('a', 'b'), 'a'), 'b',+ 'must not yank the view onto the selected agent');+});++test('the restore prefers the selected agent, then falls back to any live one', () => {+ assert.equal(restoreFocus(true, null, live('a', 'b'), 'b'), 'b');+ assert.equal(restoreFocus(true, null, live('a', 'b'), 'missing'), 'a');+ assert.equal(restoreFocus(true, null, live('a', 'b'), null), 'a');+});++test('nothing live yet returns null, and the preference survives for next time', () => {+ assert.equal(restoreFocus(true, null, [], null), null);+});