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
feat(restore): bring the team back on open, and drop the stale one-shot
Vyapak Goyal8e671da9c23b
diff --git a/src/renderer/src/components/AgentStrip.tsx b/src/renderer/src/components/AgentStrip.tsxindex 4dacdd89..199e0306 100644--- a/src/renderer/src/components/AgentStrip.tsx+++ b/src/renderer/src/components/AgentStrip.tsx@@ -3,8 +3,7 @@ import { AgentCard } from './AgentCard';import { PixelButton } from './PixelButton';import { Icon } from './Icon';import { useStore, type Agent } from '@/store/store';-import { buildSpawnCommand, inferAgentProvider, tokenizeCommand, type HarnessConfig } from '@/store/config';-import { OFFICE_CAST, DEFAULT_CHARACTER } from '@/scene/office/cast';+import { type HarnessConfig } from '@/store/config';import { useRestoreTeam } from '@/hooks/useRestoreTeam';export interface AgentStripProps {@@ -23,7 +22,7 @@ export function AgentStrip({ config }: AgentStripProps) {const reorderAgents = useStore(s => s.reorderAgents);const setAgentNote = useStore(s => s.setAgentNote);// Shared with the fullscreen roster so both show one restore in progress.- const { restoring, restoreNote, restoreTeam } = useRestoreTeam(config);+ const { restoring, autoRestoring, restoreNote, restoreTeam } = useRestoreTeam(config);// Drag-to-reorder the roster: dragId = the card being dragged, overId = the card// currently hovered as a drop target (drives the insertion-line cue).const [dragId, setDragId] = useState<string | null>(null);@@ -52,67 +51,6 @@ export function AgentStrip({ config }: AgentStripProps) {return () => { cancelled = true; clearInterval(iv); };}, []);- // One-shot recovery: the previous session's team (dev1-mqf3kuw8 … dev9-mqfhlff5)- // was archived at closing time and survives ONLY in the hive registry — it was- // never written into this renderer's restore list, so the normal "restore team"- // can't see it. On first load, respawn exactly those 9 by their ORIGINAL id- // (same spawn recipe as restoreTeam) so each agent's Claude session, worktree and- // memory.md reattach by id. Guarded in localStorage so it fires at most once.- useEffect(() => {- const GUARD = 'cth.autoRestore.mqfh9';- if (localStorage.getItem(GUARD)) return;- const COHORT = [- 'dev1-mqf3kuw8', 'dev2-mqf3li68', 'dev3-mqffie2v', 'dev4-mqfhcjwk', 'dev5-mqfhkaa4',- 'dev6-mqfhkpku', 'dev7-mqfhkxwd', 'dev8-mqfhl7gl', 'dev9-mqfhlff5',- ];- const ACCENTS = ['coral', 'mint', 'sky', 'lemon', 'lilac', 'peach'] as const;- let cancelled = false;- (async () => {- try {- const cfg = await window.cth.getConfig();- const reg = await window.cth.hiveRegistry();- if (cancelled || !cfg || !reg?.agents) return;- localStorage.setItem(GUARD, '1'); // one-shot, regardless of per-agent outcome- const live = new Set(useStore.getState().agents.map((a) => a.id));- for (let i = 0; i < COHORT.length; i++) {- const m = reg.agents[COHORT[i]] as (undefined | (typeof reg.agents)[string] & { model?: string });- if (!m || live.has(m.id) || !m.cwd) continue;- const provider = inferAgentProvider(undefined, m.provider);- const command = buildSpawnCommand(cfg, m.model, provider);- if (!command) continue;- const [exe, ...args] = tokenizeCommand(command);- const ptyId = `pty-${m.id}`;- // The registry cwd for these workers IS their worktree; re-enter it if it- // still exists on disk (gitIsRepo false → pruned, but spawn still cd's there).- const worktreeOk = await window.cth.gitIsRepo(m.cwd);- const res = await window.cth.spawnPty({- id: ptyId, cwd: m.cwd, command: exe, provider, args, cols: 100, rows: 30,- isolate: false, resume: true,- hive: { id: m.id, name: m.name, provider, cwd: m.cwd, role: m.role },- });- if (res.ok) {- useStore.getState().addAgent({- id: m.id, name: m.name,- character: OFFICE_CAST[i % OFFICE_CAST.length]?.name ?? DEFAULT_CHARACTER,- accent: ACCENTS[i % ACCENTS.length],- description: m.role ?? 'a fresh harness',- project: '', tmuxTarget: '', cwd: m.cwd,- status: 'idle', action: worktreeOk ? 'starting up' : 'restored (worktree pruned)',- progress: 0, currentStation: 'desk', ptyId,- command, provider, model: m.model,- worktreePath: m.cwd, recentTextTs: Date.now(),- } as Agent);- } else {- console.error('[auto-restore] spawn failed for', m.id, res.error);- }- }- } catch (e) {- console.error('[auto-restore] failed', e);- }- })();- return () => { cancelled = true; };- }, []);-return (<div style={{display: 'flex',@@ -225,6 +163,25 @@ export function AgentStrip({ config }: AgentStripProps) {)}</div>))}+ {autoRestoring && (+ // The team comes back on its own at boot, so SAY so — terminals opening+ // by themselves with no explanation reads as the app doing something+ // you didn't ask for.+ <span+ style={{+ alignSelf: 'center', flexShrink: 0,+ display: 'inline-flex', alignItems: 'center', gap: 6,+ height: 32, padding: '0 10px',+ fontFamily: 'var(--cth-font-ui)', fontSize: 12,+ color: 'var(--cth-ink-900)',+ background: 'var(--cth-status-working)',+ boxShadow: 'inset 0 0 0 1px var(--cth-ink-900)'+ }}+ title="Your previous session's agents are being respawned with their original ids, so memory and inboxes reattach."+ >+ <Icon name="play" /> restoring your team…+ </span>+ )}{restorableAgents.length > 0 && (<spanstyle={{ alignSelf: 'center', flexShrink: 0 }}diff --git a/src/renderer/src/components/FullscreenTerminal.tsx b/src/renderer/src/components/FullscreenTerminal.tsxindex 491aca41..5f5ed250 100644--- a/src/renderer/src/components/FullscreenTerminal.tsx+++ b/src/renderer/src/components/FullscreenTerminal.tsx@@ -141,7 +141,7 @@ export function FullscreenTerminal({ config }: FullscreenTerminalProps) {const updateAgent = useStore(s => s.updateAgent);// The floor strip (and with it the restore button) is hidden behind the// overlay, so the roster carries restore too.- const { restoring, restoreNote, restoreTeam } = useRestoreTeam(config);+ const { restoring, autoRestoring, restoreNote, restoreTeam } = useRestoreTeam(config);const agent = agents.find(a => a.id === fullscreenAgentId);const parser = usePtyParser(agent?.id ?? '__none__');@@ -335,11 +335,25 @@ export function FullscreenTerminal({ config }: FullscreenTerminalProps) {{/* Last session's team, same as the floor strip — pinned to the bottomso it can't be scrolled out of reach behind a long roster. */}- {(restorableAgents.length > 0 || restoreNote) && (+ {(restorableAgents.length > 0 || restoreNote || autoRestoring) && (<div style={{flexShrink: 0, padding: 8, display: 'flex', flexDirection: 'column', gap: 6,borderTop: '1px solid var(--cth-ink-300)'}}>+ {autoRestoring && (+ // Same banner as the floor strip: terminals that open by+ // themselves need to say why.+ <div style={{+ display: 'flex', alignItems: 'center', gap: 6,+ padding: '4px 8px',+ fontFamily: 'var(--cth-font-ui)', fontSize: 11,+ color: 'var(--cth-ink-900)',+ background: 'var(--cth-status-working)',+ boxShadow: 'inset 0 0 0 1px var(--cth-ink-900)'+ }}>+ <Icon name="play" /> restoring your team…+ </div>+ )}{restorableAgents.length > 0 && (<PixelButtonvariant="primary"diff --git a/src/renderer/src/hooks/useRestoreTeam.ts b/src/renderer/src/hooks/useRestoreTeam.tsindex 0c842bbf..be506c19 100644--- a/src/renderer/src/hooks/useRestoreTeam.ts+++ b/src/renderer/src/hooks/useRestoreTeam.ts@@ -1,4 +1,4 @@-import { useSyncExternalStore } from 'react';+import { useEffect, useSyncExternalStore } from 'react';import { useStore, type Agent } from '@/store/store';import { buildSpawnCommand, inferAgentProvider, tokenizeCommand, type HarnessConfig } from '@/store/config';@@ -12,8 +12,25 @@ import { buildSpawnCommand, inferAgentProvider, tokenizeCommand, type HarnessConlet restoring = false;let note: string | null = null;+/** True only while the AUTOMATIC boot restore is in flight, so the UI can say+ * "this is happening on its own" rather than looking like a click you don't+ * remember making. */+let autoRestoring = false;+/** Latched the moment the automatic restore starts. Module-level, not per+ * component: `useRestoreTeam` is mounted from both the floor strip and the+ * fullscreen rail, and without this each of them would kick off its own. */+let autoStarted = false;const listeners = new Set<() => void>();+/** How long to wait after boot before restoring on our own.+ *+ * App.tsx reconciles the persisted roster against the PTYs actually alive in+ * the main process, and that is an async round trip. Firing before it lands+ * would read a restorable list that still contains agents whose terminals are+ * already running, and try to spawn duplicates of them. The delay is also the+ * window in which you can hit a dismiss ✕ if you don't want an agent back. */+export const AUTO_RESTORE_DELAY_MS = 2500;+function emit(): void {for (const l of [...listeners]) l();}@@ -27,9 +44,13 @@ function subscribe(listener: () => void): () => void {// object each call would loop forever, so the two fields are read separately.const getRestoring = (): boolean => restoring;const getNote = (): string | null => note;+const getAutoRestoring = (): boolean => autoRestoring;export interface RestoreTeamState {restoring: boolean;+ /** True when the run in flight was started automatically at boot, not by a+ * click. Drives the "restoring your team…" banner. */+ autoRestoring: boolean;/** Outcome of the last run ("restored 3 · 1 failed — …"), or null. */restoreNote: string | null;restoreTeam: () => Promise<void>;@@ -42,6 +63,7 @@ export interface RestoreTeamState {export function useRestoreTeam(config?: HarnessConfig | null): RestoreTeamState {const isRestoring = useSyncExternalStore(subscribe, getRestoring, getRestoring);const restoreNote = useSyncExternalStore(subscribe, getNote, getNote);+ const isAutoRestoring = useSyncExternalStore(subscribe, getAutoRestoring, getAutoRestoring);/** Respawn every worker from the previous session with its ORIGINAL agent id,* cwd, model and command — the hive workspace (memory.md, inbox, registry@@ -181,5 +203,43 @@ export function useRestoreTeam(config?: HarnessConfig | null): RestoreTeamState}};- return { restoring: isRestoring, restoreNote, restoreTeam };+ // Restore the previous session's team on open, without waiting for a click.+ //+ // Deliberately driven by a store SUBSCRIPTION rather than a plain timer: the+ // restorable list is empty on the first render and only fills once App.tsx's+ // PTY reconcile resolves, so a timer started at mount would look at an empty+ // list, decide there was nothing to do, and never look again.+ //+ // Only ever fires for agents already on the restorable list — i.e. ones that+ // had a terminal open when the app last quit. Archived agents (closed tabs)+ // are never touched.+ useEffect(() => {+ if (autoStarted || !config?.onboardingComplete) return;+ let timer: ReturnType<typeof setTimeout> | null = null;++ const check = (): void => {+ if (autoStarted || restoring || timer) return;+ if (!useStore.getState().restorableAgents.length) return;+ timer = setTimeout(() => {+ timer = null;+ if (autoStarted || restoring) return;+ if (!useStore.getState().restorableAgents.length) return;+ // Latch BEFORE the await so the other mount point's timer, which may+ // fire in this same tick, sees it.+ autoStarted = true;+ autoRestoring = true;+ emit();+ void restoreTeam().finally(() => { autoRestoring = false; emit(); });+ }, AUTO_RESTORE_DELAY_MS);+ };++ check();+ const unsub = useStore.subscribe(check);+ return () => { unsub(); if (timer) clearTimeout(timer); };+ // restoreTeam is rebuilt every render but only ever called from inside the+ // timer, so it is read fresh at call time and does not belong in the deps.+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, [config?.onboardingComplete]);++ return { restoring: isRestoring, autoRestoring: isAutoRestoring, restoreNote, restoreTeam };}