The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui): give the slash catalog one session-aware owner
yoniebans6bcf096ef411
diff --git a/ui-tui/src/__tests__/slashCatalog.test.ts b/ui-tui/src/__tests__/slashCatalog.test.tsindex 7471431166c..d3bef09ee59 100644--- a/ui-tui/src/__tests__/slashCatalog.test.ts+++ b/ui-tui/src/__tests__/slashCatalog.test.ts@@ -1,8 +1,9 @@-import { describe, expect, it } from 'vitest'+import { beforeEach, describe, expect, it } from 'vitest'-import type { CommandsCatalogResponse } from '../gatewayTypes.js'import type { GatewayRpc } from '../app/interfaces.js'import { loadSlashCatalog } from '../app/slashCatalog.js'+import { patchUiState, resetUiState } from '../app/uiStore.js'+import type { CommandsCatalogResponse } from '../gatewayTypes.js'import type { SlashCatalog } from '../types.js'const REPO_SKILL = '/repo-probe'@@ -20,7 +21,18 @@ const catalogFor = (sessionId: string | undefined): CommandsCatalogResponse => {}describe('loadSlashCatalog', () => {- it("binds the catalog to the session, so each session's project skills replace the last one's", async () => {+ let catalog: null | SlashCatalog++ const setCatalog = (next: null | SlashCatalog) => {+ catalog = next+ }++ beforeEach(() => {+ resetUiState()+ catalog = null+ })++ it("binds the catalog to the live session, so each session's project skills replace the last one's", async () => {const requested: (string | undefined)[] = []const fake = async (method: string, params: { session_id?: string } = {}) => {@@ -33,21 +45,44 @@ describe('loadSlashCatalog', () => {// SAFETY: the fake answers only commands.catalog, the one method loadSlashCatalog calls, with that method's result type.const rpc = fake as GatewayRpc- let catalog: null | SlashCatalog = null- const setCatalog = (next: null | SlashCatalog) => {- catalog = next- }-await loadSlashCatalog(rpc, null, setCatalog)expect(catalog?.canon[REPO_SKILL]).toBeUndefined()+ patchUiState({ sid: 'repo' })await loadSlashCatalog(rpc, 'repo', setCatalog)expect(catalog?.canon[REPO_SKILL]).toBe(REPO_SKILL)expect(catalog?.skillCount).toBe(1)+ patchUiState({ sid: 'elsewhere' })await loadSlashCatalog(rpc, 'elsewhere', setCatalog)expect(catalog?.canon[REPO_SKILL]).toBeUndefined()expect(requested).toEqual([undefined, 'repo', 'elsewhere'])})++ it('drops a reply that arrives after the live session has changed', async () => {+ let release = () => {}++ const gate = new Promise<void>(resolve => {+ release = resolve+ })++ const fake = async (_method: string, params: { session_id?: string } = {}) => {+ await gate++ return catalogFor(params.session_id)+ }++ // SAFETY: the fake answers only commands.catalog, the one method loadSlashCatalog calls, with that method's result type.+ const rpc = fake as GatewayRpc++ patchUiState({ sid: 'repo' })+ const pending = loadSlashCatalog(rpc, 'repo', setCatalog)++ patchUiState({ sid: 'elsewhere' })+ release()+ await pending++ expect(catalog).toBeNull()+ })})diff --git a/ui-tui/src/app/createGatewayEventHandler.ts b/ui-tui/src/app/createGatewayEventHandler.tsindex aa01cc69a43..adc1a666226 100644--- a/ui-tui/src/app/createGatewayEventHandler.ts+++ b/ui-tui/src/app/createGatewayEventHandler.ts@@ -915,9 +915,10 @@ export function createGatewayEventHandler(ctx: GatewayEventHandlerContext): (ev:void rpc('wake.start', { surface: 'tui' }).catch(() => undefined)}- // Before the first session the gateway uses the same workspace it seeds a new session with;- // useMainApp reloads it bound to each session once one is live.- void loadSlashCatalog(rpc, getUiState().sid, setCatalog)+ // The pre-session catalog; useSlashCatalog owns every session-bound load, reconnects included.+ if (!getUiState().sid) {+ void loadSlashCatalog(rpc, null, setCatalog)+ }// Keep the recovery target until resume succeeds, including across a second// disconnect during setup or history loading. Recovery never resends the prompt.diff --git a/ui-tui/src/app/interfaces.ts b/ui-tui/src/app/interfaces.tsindex ad3ea5d5fd2..280164d608e 100644--- a/ui-tui/src/app/interfaces.ts+++ b/ui-tui/src/app/interfaces.ts@@ -508,7 +508,7 @@ export interface GatewayEventHandlerContext {recoverSidRef?: MutableRefObject<null | string>resetSession: () => voidresumeById: (id: string) => Promise<void>- setCatalog: StateSetter<null | SlashCatalog>+ setCatalog: (catalog: null | SlashCatalog) => void}submission: {/** Submit text literally as a prompt — no slash/!/interpolation dispatch.@@ -568,7 +568,7 @@ export interface SlashHandlerContext {getHistoryItems: () => Msg[]getLastUserMsg: () => stringmaybeWarn: (value: unknown) => void- setCatalog: StateSetter<null | SlashCatalog>+ setCatalog: (catalog: null | SlashCatalog) => void}session: {closeSession: (targetSid?: null | string, deferMessages?: boolean) => Promise<unknown>diff --git a/ui-tui/src/app/slashCatalog.ts b/ui-tui/src/app/slashCatalog.tsindex 4d2db838015..b0c2e7f10e4 100644--- a/ui-tui/src/app/slashCatalog.ts+++ b/ui-tui/src/app/slashCatalog.ts@@ -1,17 +1,23 @@+import { useStore } from '@nanostores/react'+import { atom } from 'nanostores'+import { useEffect } from 'react'+import type { CommandsCatalogResponse } from '../gatewayTypes.js'import { t } from '../i18n/runtime.js'import { rpcErrorMessage } from '../lib/rpc.js'import type { SlashCatalog } from '../types.js'-import type { GatewayRpc, StateSetter } from './interfaces.js'+import type { GatewayRpc } from './interfaces.js'import { turnController } from './turnController.js'+import { $uiState } from './uiStore.js'/** `commands.catalog` bound to `sid` when there is one: project-local skills follow the session's repo,- * so the catalog is reloaded whenever the live session changes. */-export const loadSlashCatalog = (rpc: GatewayRpc, sid: null | string, setCatalog: StateSetter<null | SlashCatalog>) =>+ * so the catalog is reloaded whenever the live session changes. A reply for a session that is no longer+ * the live one is dropped. */+export const loadSlashCatalog = (rpc: GatewayRpc, sid: null | string, setCatalog: (catalog: null | SlashCatalog) => void) =>rpc<CommandsCatalogResponse>('commands.catalog', sid ? { session_id: sid } : {}).then(r => {- if (!r?.pairs) {+ if (!r?.pairs || $uiState.get().sid !== sid) {return}@@ -28,3 +34,23 @@ export const loadSlashCatalog = (rpc: GatewayRpc, sid: null | string, setCatalog}}).catch(e => turnController.pushActivity(t('gatewayMsg.startup.commandCatalogUnavailable', rpcErrorMessage(e)), 'info'))++/** The slash catalog for the live session (or the launch workspace before one exists). */+export const $slashCatalog = atom<null | SlashCatalog>(null)++export const setSlashCatalog = (next: null | SlashCatalog) => $slashCatalog.set(next)++/** The slash catalog, reloaded for each live session while connected; the gateway-ready load covers the+ * pre-session one. */+export function useSlashCatalog(rpc: GatewayRpc) {+ const catalog = useStore($slashCatalog)+ const { gatewayConnected, sid } = useStore($uiState)++ useEffect(() => {+ if (sid && gatewayConnected !== false) {+ void loadSlashCatalog(rpc, sid, setSlashCatalog)+ }+ }, [rpc, sid, gatewayConnected])++ return catalog+}diff --git a/ui-tui/src/app/useMainApp.ts b/ui-tui/src/app/useMainApp.tsindex c438965e8d0..09aa3d92c73 100644--- a/ui-tui/src/app/useMainApp.ts+++ b/ui-tui/src/app/useMainApp.ts@@ -44,7 +44,7 @@ import { terminalParityHints } from '../lib/terminalParity.js'import { formatAbandonedClarify, sameToolTrailGroup, toolTrailLabel } from '../lib/text.js'import { estimatedMsgHeight, messageHeightKey } from '../lib/virtualHeights.js'import { onUserWidgets } from '../sdk/userWidgets.js'-import type { Msg, PanelSection, SlashCatalog } from '../types.js'+import type { Msg, PanelSection } from '../types.js'import { applyAgentSnapshot } from './agentRoster.js'import { clarifySettledRows } from './clarifyTranscript.js'@@ -62,7 +62,7 @@ import { applyProcessSnapshot, type ProcessEntry } from './processRoster.js'import { scrollWithSelectionBy } from './scroll.js'import { respondToServerRequest } from './serverRequestStore.js'import { mutateCanonicalSession } from './slash/canonicalSessionControls.js'-import { loadSlashCatalog } from './slashCatalog.js'+import { setSlashCatalog, useSlashCatalog } from './slashCatalog.js'import { captureDestination, isCurrentDestination, type SubmissionDestination } from './submissionDestination.js'import { turnController } from './turnController.js'import { patchTurnState, useTurnSelector } from './turnStore.js'@@ -307,7 +307,6 @@ export function useMainApp(gw: GatewayClient) {const [lastUserMsg, setLastUserMsg] = useState('')const [stickyPrompt, setStickyPrompt] = useState('')- const [catalog, setCatalog] = useState<null | SlashCatalog>(null)const [voiceEnabled, setVoiceEnabled] = useState(false)const [voiceTts, setVoiceTts] = useState(false)const [voiceRecording, setVoiceRecording] = useState(false)@@ -637,6 +636,7 @@ export function useMainApp(gw: GatewayClient) {)const gateway = useMemo(() => ({ gw, rpc }), [gw, rpc])+ const catalog = useSlashCatalog(rpc)const { die, dieWithCode, exitClose } = useDie(gw, exit)@@ -680,12 +680,6 @@ export function useMainApp(gw: GatewayClient) {useConfigSync({ gw, setBellOnComplete, setBellOnPrompt, setVoiceEnabled, setVoiceRecordKey, sid: ui.sid })useBatteryPoll(gw)- useEffect(() => {- if (ui.sid && ui.gatewayConnected !== false) {- void loadSlashCatalog(rpc, ui.sid, setCatalog)- }- }, [rpc, ui.sid, ui.gatewayConnected])-useEffect(() => {if (!ui.sid) {patchUiState({ liveSessionCount: 0, sessionTitle: '' })@@ -1038,7 +1032,7 @@ export function useMainApp(gw: GatewayClient) {recoverSidRef,resetSession: session.resetSession,resumeById: session.resumeById,- setCatalog+ setCatalog: setSlashCatalog},submission: { submitLiteralRef, submitRef },system: { bellOnComplete, bellOnPrompt, stdout, sys },@@ -1201,7 +1195,7 @@ export function useMainApp(gw: GatewayClient) {getHistoryItems: () => historyItemsRef.current,getLastUserMsg: () => lastUserMsgRef.current,maybeWarn,- setCatalog+ setCatalog: setSlashCatalog},session: {closeSession: session.closeSession,