Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
fix(ok): open the Agents panel into a ready conversation (PRD-7838) (#3334)
serafin-garcia820936f98d87
diff --git a/.changeset/agents-panel-new-conversation.md b/.changeset/agents-panel-new-conversation.mdnew file mode 100644index 000000000..06fd8ed0e--- /dev/null+++ b/.changeset/agents-panel-new-conversation.md@@ -0,0 +1,7 @@+---+"@inkeep/open-knowledge": patch+---++Opening the Agents panel with no conversations in it now starts one for you, using the same agent the New button leads with — so a conversation is open and waiting instead of an empty panel. If no agent is available the panel simply stays empty; a passive reveal never pops open Configure agents.++Starting an agent without naming one now leads with the same agent every picker already shows, rather than only a previously saved choice. This mattered most on a cold start: while the agent list was still loading, "Start an agent" used to send you to Configure agents and leave you there even once an agent became available, and revealing the panel in that window left it empty for good. Both now wait for the agent list and then do what you asked.diff --git a/packages/app/src/components/SessionsHost.agents.dom.test.tsx b/packages/app/src/components/SessionsHost.agents.dom.test.tsxindex fd552ac7a..cb9d2cf04 100644--- a/packages/app/src/components/SessionsHost.agents.dom.test.tsx+++ b/packages/app/src/components/SessionsHost.agents.dom.test.tsx@@ -16,11 +16,17 @@import type { ThreadInfo } from '@inkeep/open-knowledge-core/acp/thread-protocol';import { act, cleanup, render, screen, waitFor } from '@testing-library/react';import userEvent from '@testing-library/user-event';-import { useState, useSyncExternalStore } from 'react';+import { useEffect, useState, useSyncExternalStore } from 'react';import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';import { TooltipProvider } from '@/components/ui/tooltip';+import {+ inAppEnabledKey,+ reloadEnabledAgentsFromStorage,+ setAgentEnabled,+} from '@/lib/acp/enabled-agents';import { subscribeStagedThreadDraft } from '@/lib/acp/thread-draft-staging';import type { OkDesktopBridge } from '@/lib/desktop-bridge-types';+import type { ThreadLaunchIntent } from './EditorPane';import { requestPreferredSession } from './handoff/preferred-session-events';import { requestActiveTerminalInput } from './handoff/terminal-input-events';import { subscribeToTerminalLaunchRequests } from './handoff/terminal-launch-events';@@ -95,27 +101,80 @@ vi.doMock('@/components/acp/ThreadView', () => ({}));/**- * The registered in-app agent, when a test wants one. Null (the default) models a- * machine with no ACP agent set up, so the launcher falls through to the CLI /- * bare-terminal families.+ * `supported` mirrors the real registered-agent shape: the catalog sets it false+ * when no launchable build exists for this host, and the enabled filter drops+ * such an agent before the resolver ever sees it. Omitting it from the mock left+ * that branch permanently unexercised, since `undefined` is not `false`.*/-let mockRegisteredAgent: { source: 'registry' | 'custom'; id: string; name: string } | null = null;+type MockAgent = {+ source: 'registry' | 'custom';+ id: string;+ name: string;+ supported?: boolean;+};++/**+ * The agent the store presents — an explicit registration or a detected+ * suggestion. Null (the default) models a machine with no ACP agent set up, so the+ * launcher falls through to the CLI / bare-terminal families. For the brand-new+ * user, set this to the detected agent and leave `mockPersistedDefaultAgent` null:+ * `useDefaultRegisteredAgent` returns null and `pickEffectiveDefaultAgent` still+ * leads with that agent (the first enabled one).+ */+let mockRegisteredAgent: MockAgent | null = null;+/** The explicitly persisted default, when a test wants one distinct from above. */+let mockPersistedDefaultAgent: MockAgent | null = null;+const registerAgent = vi.fn((_agent: MockAgent) => {});++// The resolver is a pure function already unit-tested in registered-agents.test.ts.+// Stubbing it here would make every default-resolution assertion tautological —+// the enabled-filter that keeps a DISABLED agent from leading is precisely what+// these tests need to exercise — so the real one is used.+const { pickEffectiveDefaultAgent } = await vi.importActual<+ typeof import('@/lib/acp/registered-agents')+>('@/lib/acp/registered-agents');++/**+ * What the store presents, in order. The persisted default is appended when it is+ * a DIFFERENT agent, so a test can put it second and prove the resolver actually+ * consults it — with one agent standing for both, "persisted wins" would hold even+ * if `useDefaultRegisteredAgent` were never forwarded.+ */+function presentedAgents(): MockAgent[] {+ const list = mockRegisteredAgent === null ? [] : [mockRegisteredAgent];+ const persisted = mockPersistedDefaultAgent;+ if (+ persisted !== null &&+ !list.some((a) => a.source === persisted.source && a.id === persisted.id)+ )+ list.push(persisted);+ return list;+}vi.doMock('@/lib/acp/registered-agents', () => ({- useRegisteredAgents: () => (mockRegisteredAgent === null ? [] : [mockRegisteredAgent]),- useDefaultRegisteredAgent: () => mockRegisteredAgent,- getDefaultRegisteredAgent: () => mockRegisteredAgent,- registerAgent: () => {},+ useRegisteredAgents: presentedAgents,+ useDefaultRegisteredAgent: () => mockPersistedDefaultAgent,+ getDefaultRegisteredAgent: () => mockPersistedDefaultAgent,+ registerAgent,// Real code loaded here imports these too (SessionsHost →// pickEffectiveDefaultAgent; catalog → hydrateRegisteredAgentMeta). A// mock.module replaces the whole module, so any omitted export becomes an// unresolved import that fails the file (and can cascade to siblings).- pickEffectiveDefaultAgent: () => mockRegisteredAgent,+ pickEffectiveDefaultAgent,hydrateRegisteredAgentMeta: () => {},}));-const launchAgentThread = vi.fn(() => {});-vi.doMock('@/lib/acp/launch-agent-thread', () => ({ launchAgentThread }));+// Mirrors the real module: a fired launch is "in flight" until its createThread+// settles. Tests drive the settle — a success lands a thread (setOpenThreads), a+// failure just clears the set — by flipping this back to false.+let mockInflightLaunch = false;+const launchAgentThread = vi.fn(() => {+ mockInflightLaunch = true;+});+vi.doMock('@/lib/acp/launch-agent-thread', () => ({+ launchAgentThread,+ hasInflightThreadLaunch: () => mockInflightLaunch,+}));let catalogData: unknown;// The host and New split-button share the catalog query; keep it controllable.@@ -156,17 +215,46 @@ function makeTerminalBridge(): OkDesktopBridge {} as unknown as OkDesktopBridge;}+type HarnessControl = {+ setVisible: (v: boolean) => void;+ setThreadLaunch: (t: ThreadLaunchIntent | null) => void;+ /**+ * Force a re-render without changing any input. The registry hooks are mocked+ * as plain reads of module-scope state, so this is how a test plays out the+ * async agent catalog landing: null at first paint, an agent a beat later.+ */+ rerender: () => void;+};++function makeControl(): { current: HarnessControl | null } {+ return { current: null };+}+function Harness({bridge = null,initialVisible = true,onVisibleChange,+ threadLaunch: initialThreadLaunch = null,+ control,}: {bridge?: OkDesktopBridge | null;initialVisible?: boolean;onVisibleChange?: (v: boolean) => void;+ threadLaunch?: ThreadLaunchIntent | null;+ control?: { current: HarnessControl | null };}) {const [container, setContainer] = useState<HTMLDivElement | null>(null);const [visible, setVisible] = useState(initialVisible);+ const [threadLaunch, setThreadLaunch] = useState(initialThreadLaunch);+ const [, setTick] = useState(0);+ // Hand a test the raw setters so it can reproduce EditorPane's transitions —+ // notably setting visibility and a launch intent in ONE commit, which is what+ // makes the reveal effect see the launch as still-pending and stand its seed+ // down instead of creating a second conversation.+ useEffect(() => {+ if (control != null)+ control.current = { setVisible, setThreadLaunch, rerender: () => setTick((n) => n + 1) };+ }, [control]);return (<TooltipProvider><div ref={setContainer} data-testid="dock-container" />@@ -178,6 +266,7 @@ function Harness({// which hands both hosts the same whole-app fact.terminalCapable={bridge != null}visible={visible}+ threadLaunch={threadLaunch}onVisibleChange={(v) => {onVisibleChange?.(v);setVisible(v);@@ -201,8 +290,15 @@ describe('SessionsHost — agents panel (web / no bridge)', () => {openArchivedThread.mockClear();deleteThread.mockClear();launchAgentThread.mockClear();+ mockInflightLaunch = false;+ registerAgent.mockClear();catalogData = undefined;mockRegisteredAgent = null;+ mockPersistedDefaultAgent = null;+ // The enabled-agents store persists to localStorage and caches at module+ // scope, so a per-test override outlives the test without this reload.+ localStorage.clear();+ reloadEnabledAgentsFromStorage();});afterEach(() => {cleanup();@@ -663,6 +759,374 @@ describe('SessionsHost — agents panel (web / no bridge)', () => {});});+ // A launch intent whose agentId is the empty-string sentinel (Ask AI, the command+ // palette, a handoff — anything that says "resolve the default for me") must lead+ // with the SAME effective default the New picker shows, so a brand-new user whose+ // machine has a detected agent lands in a thread instead of the settings dialog.+ describe('pickerless thread launch', () => {+ const sentinelLaunch = (nonce: number): ThreadLaunchIntent => ({+ agentSource: 'registry',+ agentId: '',+ prompt: null,+ docName: null,+ titleHint: null,+ nonce,+ });++ test('with no persisted default it starts the effective default agent, not Settings', () => {+ window.location.hash = '';+ // A detected suggestion is the effective default; nothing is persisted yet.+ mockRegisteredAgent = { source: 'registry', id: 'claude-acp', name: 'Claude Agent' };+ render(<Harness threadLaunch={sentinelLaunch(1)} />);++ expect(launchAgentThread).toHaveBeenCalledTimes(1);+ expect(launchAgentThread.mock.calls[0]).toEqual([+ { source: 'registry', id: 'claude-acp' },+ null,+ null,+ null,+ ]);+ expect(window.location.hash).toBe('');+ // The suggestion is launched, never written back as an explicit registration.+ expect(registerAgent).not.toHaveBeenCalled();+ });++ test('with nothing enabled it still opens Configure agents', () => {+ window.location.hash = '';+ mockRegisteredAgent = null; // nothing detected or enabled to lead with+ render(<Harness threadLaunch={sentinelLaunch(1)} />);++ expect(launchAgentThread).not.toHaveBeenCalled();+ expect(window.location.hash).toBe('#settings/configure-agents');+ });++ test('an agent the user disabled is never what a pickerless launch leads with', () => {+ window.location.hash = '';+ // Present in the store, but switched off in Configure agents — the enabled+ // filter must drop it before the resolver ever sees it.+ mockRegisteredAgent = { source: 'registry', id: 'claude-acp', name: 'Claude Agent' };+ setAgentEnabled(inAppEnabledKey('registry', 'claude-acp'), false);+ render(<Harness threadLaunch={sentinelLaunch(1)} />);++ expect(launchAgentThread).not.toHaveBeenCalled();+ expect(window.location.hash).toBe('#settings/configure-agents');+ });++ test('an agent with no launchable build on this host is not what a launch leads with', () => {+ window.location.hash = '';+ // Distinct from the user-disabled case above: nothing is toggled off in+ // Configure agents, the CATALOG reports no launchable build for this host.+ // That travels a different argument of the enabled filter, so a refactor+ // that dropped it would otherwise offer an unlaunchable agent as the lead.+ mockRegisteredAgent = {+ source: 'registry',+ id: 'claude-acp',+ name: 'Claude Agent',+ supported: false,+ };+ render(<Harness threadLaunch={sentinelLaunch(1)} />);++ expect(launchAgentThread).not.toHaveBeenCalled();+ expect(window.location.hash).toBe('#settings/configure-agents');+ });++ test('an explicitly persisted default still wins over the presented agent', () => {+ window.location.hash = '';+ // Deliberately DIFFERENT agents, with the detected one listed first: the+ // user's own pick has to beat the head of the list, so this fails if the+ // persisted default ever stops reaching the resolver.+ mockRegisteredAgent = { source: 'registry', id: 'claude-acp', name: 'Claude Agent' };+ mockPersistedDefaultAgent = { source: 'registry', id: 'codex-acp', name: 'Codex Agent' };+ render(<Harness threadLaunch={sentinelLaunch(1)} />);++ expect(launchAgentThread).toHaveBeenCalledTimes(1);+ expect(launchAgentThread.mock.calls[0][0]).toEqual({ source: 'registry', id: 'codex-acp' });+ expect(registerAgent).not.toHaveBeenCalled();+ });++ // Every surface that emits the sentinel pre-resolves with the same resolver+ // and only falls back to it when ITS resolution came back empty — so on a+ // cold start the sentinel arrives precisely while the async agent registry is+ // still landing. Burning the intent there strands the user in the settings+ // dialog permanently.+ test('an intent that arrives before an agent exists still launches once one does', () => {+ window.location.hash = '';+ mockRegisteredAgent = null; // registry has not landed yet+ const control = makeControl();+ render(<Harness threadLaunch={sentinelLaunch(1)} control={control} />);++ // Nothing to launch yet, so the user is offered the catalog.+ expect(launchAgentThread).not.toHaveBeenCalled();+ expect(window.location.hash).toBe('#settings/configure-agents');++ // The catalog lands a beat later and claude-acp becomes the lead.+ mockRegisteredAgent = { source: 'registry', id: 'claude-acp', name: 'Claude Agent' };+ act(() => control.current?.rerender());++ expect(launchAgentThread).toHaveBeenCalledTimes(1);+ expect(launchAgentThread.mock.calls[0][0]).toEqual({ source: 'registry', id: 'claude-acp' });+ });++ test('the catalog is offered once, not on every render while the registry settles', () => {+ window.location.hash = '';+ mockRegisteredAgent = null;+ const control = makeControl();+ render(<Harness threadLaunch={sentinelLaunch(1)} control={control} />);+ expect(window.location.hash).toBe('#settings/configure-agents');++ // A user who starts navigating the dialog must not have it yanked back.+ window.location.hash = '#settings/some-other-tab';+ act(() => control.current?.rerender());+ act(() => control.current?.rerender());++ expect(window.location.hash).toBe('#settings/some-other-tab');+ expect(launchAgentThread).not.toHaveBeenCalled();+ });++ test('a launch naming a concrete agent starts that agent, not the default', () => {+ window.location.hash = '';+ // An effective default exists but must be ignored when the intent names one.+ mockRegisteredAgent = { source: 'registry', id: 'claude-acp', name: 'Claude Agent' };+ render(+ <Harness+ threadLaunch={{+ agentSource: 'custom',+ agentId: 'my-agent',+ prompt: 'do the thing',+ docName: 'notes',+ titleHint: 'Notes',+ nonce: 1,+ }}+ />,+ );++ expect(launchAgentThread).toHaveBeenCalledTimes(1);+ expect(launchAgentThread.mock.calls[0]).toEqual([+ { source: 'custom', id: 'my-agent' },+ 'do the thing',+ 'notes',+ 'Notes',+ ]);+ });+ });++ // Revealing an empty agents panel starts the same lead agent the New picker+ // shows — but a passive reveal must never pop the catalog, so a panel with no+ // agent to launch simply stays empty.+ describe('seed on reveal', () => {+ test('revealing an empty panel starts one conversation when an agent resolves', () => {+ window.location.hash = '';+ mockRegisteredAgent = { source: 'registry', id: 'claude-acp', name: 'Claude Agent' };+ const control = makeControl();+ render(<Harness initialVisible={false} control={control} />);+ // Hidden + empty: nothing has launched yet.+ expect(launchAgentThread).not.toHaveBeenCalled();++ act(() => control.current?.setVisible(true));++ expect(launchAgentThread).toHaveBeenCalledTimes(1);+ expect(launchAgentThread.mock.calls[0]).toEqual([+ { source: 'registry', id: 'claude-acp' },+ null,+ null,+ null,+ ]);+ // The detected suggestion is launched, never persisted by a passive reveal.+ expect(registerAgent).not.toHaveBeenCalled();+ });++ test('a reveal before the registry lands still gets its conversation once it does', () => {+ window.location.hash = '';+ mockRegisteredAgent = null; // registry has not landed yet+ const control = makeControl();+ render(<Harness initialVisible={false} control={control} />);++ // Revealed during the window: nothing to seed, and never the catalog.+ act(() => control.current?.setVisible(true));+ expect(launchAgentThread).not.toHaveBeenCalled();+ expect(window.location.hash).toBe('');++ // The registry lands; the conversation the reveal was owed arrives.+ mockRegisteredAgent = { source: 'registry', id: 'claude-acp', name: 'Claude Agent' };+ act(() => control.current?.rerender());++ expect(launchAgentThread).toHaveBeenCalledTimes(1);+ expect(launchAgentThread.mock.calls[0][0]).toEqual({ source: 'registry', id: 'claude-acp' });+ });++ test('a reveal owed a conversation drops the debt if the panel closes first', () => {+ mockRegisteredAgent = null;+ const control = makeControl();+ render(<Harness initialVisible={false} control={control} />);+ act(() => control.current?.setVisible(true));+ expect(launchAgentThread).not.toHaveBeenCalled();++ // The user closes the panel before anything resolved — seeding into a+ // hidden panel would be a conversation they never see.+ act(() => control.current?.setVisible(false));+ mockRegisteredAgent = { source: 'registry', id: 'claude-acp', name: 'Claude Agent' };+ act(() => control.current?.rerender());++ expect(launchAgentThread).not.toHaveBeenCalled();+ });++ test('the seed keys on the reveal transition, not on being visible', () => {+ window.location.hash = '';+ mockRegisteredAgent = { source: 'registry', id: 'claude-acp', name: 'Claude Agent' };+ // Mounting already-visible presents no transition, so nothing seeds. This is+ // NOT the restore path: EditorPane starts the panel hidden and flips it from+ // an async dock-state read, which IS a transition and DOES seed (covered by+ // the reveal tests above). What this pins is that the trigger is the edge,+ // not the level — a seed keyed on `visible` alone would fire here, and on the+ // shared terminal-dock path would open a session on every cold start.+ render(<Harness initialVisible />);++ expect(launchAgentThread).not.toHaveBeenCalled();+ expect(window.location.hash).toBe('');+ });++ test('revealing an empty panel with no agent available stays empty and never opens the catalog', () => {+ window.location.hash = '';+ mockRegisteredAgent = null; // nothing detected or enabled to lead with+ const control = makeControl();+ render(<Harness initialVisible={false} control={control} />);++ act(() => control.current?.setVisible(true));++ // The surviving half of the panel's rule, and the guard against a naive+ // fix: no thread, and no Configure agents dialog either.+ expect(launchAgentThread).not.toHaveBeenCalled();+ expect(window.location.hash).toBe('');+ });++ test('revealing a panel that already has a conversation does not create another', async () => {+ mockRegisteredAgent = { source: 'registry', id: 'claude-acp', name: 'Claude Agent' };+ // Seeded before mount so the live count never rises 0→1 (no auto-reveal) —+ // the state of a user reopening a panel they already had a chat in.+ setOpenThreads([makeThread({ threadId: 't1', title: 'Existing' })]);+ const control = makeControl();+ render(<Harness initialVisible={false} control={control} />);+ await screen.findByTestId('thread-view');++ act(() => control.current?.setVisible(true));++ expect(launchAgentThread).not.toHaveBeenCalled();+ });++ test('revealing while a thread launch is pending does not seed a second conversation', () => {+ window.location.hash = '';+ mockRegisteredAgent = { source: 'registry', id: 'claude-acp', name: 'Claude Agent' };+ const control = makeControl();+ render(<Harness initialVisible={false} control={control} />);++ // EditorPane reveals the panel AND sets the launch intent together when a+ // handoff starts an agent. Set both in one commit: the pending launch owns+ // the creation, so the reveal seed must stand down.+ act(() => {+ control.current?.setThreadLaunch({+ agentSource: 'custom',+ agentId: 'handoff-agent',+ prompt: 'do it',+ docName: null,+ titleHint: null,+ nonce: 1,+ });+ control.current?.setVisible(true);+ });++ // Exactly one thread, and it is the handoff's agent — not the seed's+ // effective default, which would be the duplicate.+ expect(launchAgentThread).toHaveBeenCalledTimes(1);+ expect(launchAgentThread.mock.calls[0][0]).toEqual({ source: 'custom', id: 'handoff-agent' });+ });++ test('re-revealing while a launched thread is still being created does not seed a duplicate', () => {+ window.location.hash = '';+ // The effective default a reveal would seed differs from the concrete agent+ // the handoff launches, so the per-agent inflight dedup cannot drop the+ // duplicate — only standing the reveal seed down can.+ mockRegisteredAgent = { source: 'registry', id: 'claude-acp', name: 'Claude Agent' };+ const control = makeControl();+ render(<Harness initialVisible={false} control={control} />);++ // Handoff: reveal + concrete launch in one commit. The thread starts, but its+ // createThread is async (npx install + handshake), so nothing lands yet.+ act(() => {+ control.current?.setThreadLaunch({+ agentSource: 'custom',+ agentId: 'handoff-agent',+ prompt: 'do it',+ docName: null,+ titleHint: null,+ nonce: 1,+ });+ control.current?.setVisible(true);+ });+ expect(launchAgentThread).toHaveBeenCalledTimes(1);++ // The user toggles the panel hidden then visible again (⌘L twice) while the+ // thread is still being created: sessions is still empty and the launch nonce+ // is already handled, so `threadLaunchPending` reads false. The reveal must+ // still not seed a second, wrong-agent conversation on top of the pending one.+ act(() => control.current?.setVisible(false));+ act(() => control.current?.setVisible(true));++ expect(launchAgentThread).toHaveBeenCalledTimes(1);+ expect(launchAgentThread.mock.calls[0][0]).toEqual({ source: 'custom', id: 'handoff-agent' });+ });++ test('a launch that fails to land re-enables reveal-seed once it is no longer in flight', () => {+ window.location.hash = '';+ mockRegisteredAgent = { source: 'registry', id: 'claude-acp', name: 'Claude Agent' };+ const control = makeControl();+ render(<Harness initialVisible={false} control={control} />);++ // Handoff launches a concrete agent; its create is in flight.+ act(() => {+ control.current?.setThreadLaunch({+ agentSource: 'custom',+ agentId: 'handoff-agent',+ prompt: null,+ docName: null,+ titleHint: null,+ nonce: 1,+ });+ control.current?.setVisible(true);+ });+ expect(launchAgentThread).toHaveBeenCalledTimes(1);++ // createThread rejects (server down): no session ever lands, but the launch+ // leaves the in-flight set. Revealing an empty panel must seed again rather+ // than stay disabled for the rest of the session.+ mockInflightLaunch = false;+ act(() => control.current?.setVisible(false));+ act(() => control.current?.setVisible(true));++ expect(launchAgentThread).toHaveBeenCalledTimes(2);+ expect(launchAgentThread.mock.calls[1][0]).toEqual({ source: 'registry', id: 'claude-acp' });+ });++ test('a conversation created on one reveal is not duplicated on the next reveal', async () => {+ window.location.hash = '';+ mockRegisteredAgent = { source: 'registry', id: 'claude-acp', name: 'Claude Agent' };+ const control = makeControl();+ render(<Harness initialVisible={false} control={control} />);++ // First reveal seeds exactly one conversation.+ act(() => control.current?.setVisible(true));+ expect(launchAgentThread).toHaveBeenCalledTimes(1);++ // That launch lands as a live thread (what the server round-trip does).+ setOpenThreads([makeThread({ threadId: 't1', title: 'Seeded' })]);+ await screen.findByTestId('thread-view');++ // Hide, then reveal again: the panel is no longer empty, so no second seed.+ act(() => control.current?.setVisible(false));+ act(() => control.current?.setVisible(true));+ expect(launchAgentThread).toHaveBeenCalledTimes(1);+ });+ });+test('a dropped WS shows the reconnecting banner above the active thread', async () => {render(<Harness />);setOpenThreads([makeThread({ threadId: 't1', title: 'Work' })]);diff --git a/packages/app/src/components/SessionsHost.tsx b/packages/app/src/components/SessionsHost.tsxindex fd7519801..a24c4bd6e 100644--- a/packages/app/src/components/SessionsHost.tsx+++ b/packages/app/src/components/SessionsHost.tsx@@ -20,14 +20,13 @@ import { publishReusableSession } from '@/components/reusable-session-store';import { TabsContent } from '@/components/ui/tabs';import { isInAppAgentEnabled } from '@/lib/acp/agent-visibility';import { useEnabledOverrides } from '@/lib/acp/enabled-agents';-import { launchAgentThread } from '@/lib/acp/launch-agent-thread';+import { hasInflightThreadLaunch, launchAgentThread } from '@/lib/acp/launch-agent-thread';import {enabledTerminalClis,type LauncherSelection,resolveLauncherSelection,} from '@/lib/acp/launcher-selection';import {- getDefaultRegisteredAgent,pickEffectiveDefaultAgent,type RegisteredAgent,registerAgent,@@ -434,6 +433,15 @@ export function SessionsHost({coldSeedTerminal && launch ? launch.nonce : null,);const lastHandledThreadNonceRef = useRef<number | null>(null);+ // Which nonce already sent the user to Configure agents. Separate from the+ // handled-nonce above so an intent that arrived before the agent registry+ // settled stays live — it opens the catalog once, then still launches if an+ // agent shows up — instead of being burned on a "nothing enabled" that was+ // only ever "nothing enabled YET".+ const settingsShownForNonceRef = useRef<number | null>(null);+ // A reveal that found no agent to seed. Held so the conversation the user was+ // owed still arrives when the registry settles a beat later.+ const seedOwedRef = useRef(false);const lastHandledCommandNonceRef = useRef<number | null>(null);const prevVisibleRef = useRef(isWindow ? false : visible);const ptyIdBySessionRef = useRef(new Map<string, string>());@@ -605,14 +613,33 @@ export function SessionsHost({else openAgentSettings();}- // Seed an empty terminal dock on reveal by repeating the New primary's pick —- // a bare shell or the picked CLI. The agents panel stays empty until the user- // explicitly starts an agent: a passive reveal must not auto-open a thread or- // the catalog. A terminal host with nothing enabled likewise just shows empty.- function seedOnReveal() {- if (!hostTerminals) return;+ // Seed an empty dock on reveal by repeating the New primary's pick. On the+ // agents panel that starts the effective default agent's thread — but only when+ // one resolves: a passive reveal must never pop the catalog, so nothing enabled+ // leaves the panel empty rather than opening Configure agents. That is this+ // branch mirroring the New primary MINUS its nothing-resolved openAgentSettings+ // fallthrough. A terminal host repeats a bare shell or the picked CLI; nothing+ // enabled there likewise just shows empty.+ //+ // Returns false ONLY on the agents panel when no agent resolved yet — the+ // registry fills from an async catalog fetch, so a panel revealed during that+ // window would otherwise silently lose its conversation, and the caller holds+ // the intent open to retry once an agent appears. A terminal host always returns+ // true (it seeds a shell or the picked CLI), so false is never its signal.+ function seedOnReveal(): boolean {+ if (hostThreads) {+ if (newSessionChoice.kind !== 'agent' || newSessionChoice.agent == null) return false;+ launchAgentThread(+ { source: newSessionChoice.agent.source, id: newSessionChoice.agent.id },+ null,+ null,+ null,+ );+ return true;+ }if (newSessionChoice.kind === 'terminal') openSession(null);else if (newSessionChoice.kind === 'cli') openNewChatSession(newSessionChoice.cli);+ return true;}/**@@ -1139,10 +1166,17 @@ export function SessionsHost({}, [sessions, activeSessionId]);// Open/launch lifecycle: a fresh terminal launch intent opens its own tab;- // otherwise an open-from-hidden transition seeds the dock — UNLESS a fresh thread- // launch is being handled this cycle (the thread launch effect owns the seed- // then, so we don't open a bare terminal alongside the agent). Defined before the- // thread launch effect so `threadLaunchPending` reads the not-yet-handled nonce.+ // otherwise an open-from-hidden transition seeds the dock — UNLESS a thread+ // launch is still outstanding. That covers two windows: its nonce is unhandled+ // this cycle (`threadLaunchPending`, the thread launch effect owns the seed+ // then), or it was handled and its async createThread is still in flight+ // (`hasInflightThreadLaunch`, which clears on both success and failure so a+ // failed launch re-enables seeding). Seeding across either opens a duplicate+ // beside the agent. Both are agents-panel concerns, so both are gated on+ // `hostThreads` — the launch set is window-wide, and an ungated read lets an+ // agent-thread launch swallow the TERMINAL dock's shell on an unrelated+ // reveal. Defined before the thread launch effect so `threadLaunchPending`+ // reads the not-yet-handled nonce.useEffect(() => {if (!rehydrationSettled) return;const wasVisible = prevVisibleRef.current;@@ -1164,9 +1198,27 @@ export function SessionsHost({hostThreads &&threadLaunch != null &&threadLaunch.nonce !== lastHandledThreadNonceRef.current;- if (visible && !wasVisible && sessions.length === 0 && !threadLaunchPending) {- seedOnRevealRef.current();+ if (+ visible &&+ !wasVisible &&+ sessions.length === 0 &&+ !threadLaunchPending &&+ !(hostThreads && hasInflightThreadLaunch())+ ) {+ seedOwedRef.current = !seedOnRevealRef.current();+ return;+ }+ // A reveal that landed before the agent registry settled is still owed its+ // conversation; retry now that something changed. Anything that resolves the+ // debt another way — the panel closing, a conversation arriving, an explicit+ // launch taking over — drops it rather than seeding on top.+ if (!seedOwedRef.current) return;+ if (!visible || sessions.length > 0 || threadLaunchPending) {+ seedOwedRef.current = false;+ return;}+ if (effectiveDefaultAgent === null) return; // still nothing to seed with+ if (seedOnRevealRef.current()) seedOwedRef.current = false;}, [visible,launch,@@ -1175,31 +1227,46 @@ export function SessionsHost({sessions.length,rehydrationSettled,hostThreads,+ effectiveDefaultAgent,]);- // "Start an agent" launch intent (agents panel): resolve the agent (concrete /- // default-registered) and start a thread. Each new nonce opens its own thread- // tab; the store reconcile + auto-reveal bring it to front + reveal. When no- // agent resolves (nothing enabled yet), open Configure agents so the user can- // enable one — the retired agent catalog's replacement.+ // "Start an agent" launch intent (agents panel): resolve the agent and start a+ // thread. An intent may name a concrete agent; the empty-string sentinel instead+ // defers to the SAME effective default the New picker leads with (the enabled+ // registered default, else the first enabled agent — for a fresh user that is the+ // top detected suggestion), so a pickerless launch and the on-screen picker never+ // disagree. Each new nonce opens its own thread tab; the store reconcile ++ // auto-reveal bring it to front + reveal.+ //+ // The nonce is spent only on a launch. Every surface that emits the sentinel+ // pre-resolves with the same resolver and falls back to it exactly when ITS+ // resolution came back empty — which on a cold start means the async agent+ // registry simply has not landed yet, not that the user has nothing. Marking+ // the intent handled there would strand them in Configure agents for good, so+ // the intent stays live and this effect re-runs when an agent appears.useEffect(() => {if (!hostThreads) return;if (threadLaunch == null || threadLaunch.nonce === lastHandledThreadNonceRef.current) return;- lastHandledThreadNonceRef.current = threadLaunch.nonce;let agent: { source: 'registry' | 'custom'; id: string } | null =threadLaunch.agentId === ''? null: { source: threadLaunch.agentSource, id: threadLaunch.agentId };- if (agent === null) {- const fallback = getDefaultRegisteredAgent();- if (fallback !== null) agent = { source: fallback.source, id: fallback.id };+ if (agent === null && effectiveDefaultAgent !== null) {+ agent = { source: effectiveDefaultAgent.source, id: effectiveDefaultAgent.id };}if (agent === null) {- openAgentSettings();+ // Once per intent: a user with genuinely nothing enabled needs the catalog,+ // but re-opening it on every re-render while the registry settles would+ // fight whatever they are doing in it.+ if (settingsShownForNonceRef.current !== threadLaunch.nonce) {+ settingsShownForNonceRef.current = threadLaunch.nonce;+ openAgentSettings();+ }return;}+ lastHandledThreadNonceRef.current = threadLaunch.nonce;launchAgentThread(agent, threadLaunch.prompt, threadLaunch.docName, threadLaunch.titleHint);- }, [threadLaunch, hostThreads]);+ }, [threadLaunch, hostThreads, effectiveDefaultAgent]);// Reload rehydration (capable bridge only): rebuild one terminal tab per PTY// survivor and read the persisted unified order + active key, so restored tabsdiff --git a/packages/app/src/components/TerminalDock.dom.test.tsx b/packages/app/src/components/TerminalDock.dom.test.tsxindex 1ee4b082b..314fab51b 100644--- a/packages/app/src/components/TerminalDock.dom.test.tsx+++ b/packages/app/src/components/TerminalDock.dom.test.tsx@@ -160,6 +160,15 @@ vi.doMock('./TerminalGate', () => ({},}));+// The agent-thread launch set is window-wide, so the dock reads it too. Held+// here so a test can assert the dock's shell does not depend on whether an+// agents-panel launch happens to be in flight.+let mockInflightThreadLaunch = false;+vi.doMock('@/lib/acp/launch-agent-thread', () => ({+ launchAgentThread: () => {},+ hasInflightThreadLaunch: () => mockInflightThreadLaunch,+}));+vi.doMock('@/lib/terminal-height-store', () => ({getInitialTerminalHeight: () => 240,writeTerminalHeight: () => {},@@ -1337,6 +1346,25 @@ describe('TerminalDock extraction pins', () => {expect(view.create).toHaveBeenCalledTimes(1);});+ test('pin: an in-flight AGENT-thread launch does not swallow the dock shell', () => {+ // The agents panel skips its reveal-seed while its own createThread is in+ // flight, so it cannot open a conversation beside the one already coming.+ // That set is window-wide; the dock hosts no agent threads, so the same+ // reveal must still give the user their shell.+ mockInflightThreadLaunch = true;+ try {+ const view = renderDock(false);+ expect(screen.queryAllByTestId('terminal-session')).toHaveLength(0);++ act(() => view.rerender(true));++ expect(screen.getAllByTestId('terminal-session')).toHaveLength(1);+ expect(view.create).toHaveBeenCalledTimes(1);+ } finally {+ mockInflightThreadLaunch = false;+ }+ });+test('pin: a repeated launch nonce does not open a second tab', () => {const view = renderDock(true);diff --git a/packages/app/src/lib/acp/launch-agent-thread.ts b/packages/app/src/lib/acp/launch-agent-thread.tsindex 93e7f1511..a8d6a43a5 100644--- a/packages/app/src/lib/acp/launch-agent-thread.ts+++ b/packages/app/src/lib/acp/launch-agent-thread.ts@@ -75,3 +75,16 @@ export function launchAgentThread(inflightLaunches.delete(launchKey);});}++/**+ * Whether any agent-thread launch is still mid-flight in this window. `createThread`+ * resolves the new thread into the shared store seconds later, so a caller that+ * auto-creates a conversation on some UI transition (e.g. the sessions dock seeding+ * an empty panel on reveal) would open a duplicate if it fired during that gap.+ * Gate such seeding on this. Because the set clears in the `finally` above on BOTH+ * success and failure, a launch that never lands re-enables seeding rather than+ * disabling it for good.+ */+export function hasInflightThreadLaunch(): boolean {+ return inflightLaunches.size > 0;+}