Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop-app): r2-l3 guard the chatViewTabs non-empty invariant
Diego Zacariasdb6a7313b193
diff --git a/desktop-app/ui/src/lib/__tests__/chatViewTabs.test.ts b/desktop-app/ui/src/lib/__tests__/chatViewTabs.test.tsindex 213bdccdc..6b1b36bf5 100644--- a/desktop-app/ui/src/lib/__tests__/chatViewTabs.test.ts+++ b/desktop-app/ui/src/lib/__tests__/chatViewTabs.test.ts@@ -1,11 +1,11 @@import { describe, expect, it } from 'vitest'import fc from 'fast-check'import {+ activeChatViewTab,addBlankChatViewTab,closeChatViewTab,createChatViewTabsState,cycleChatViewTab,- activeChatViewTab,focusBlankChatViewTab,openPersistedChatViewTab,reconcileChatViewTabs,@@ -181,4 +181,20 @@ describe('reconcileChatViewTabs (minispec 04 §4 invariant — property-based)',}))})++ // R2-L3: the non-empty-tabs invariant is enforced across the module, but the+ // accessors that leaned on it used non-null assertions — a silent `undefined`+ // (activeChatViewTab) or a cryptic `.id of undefined` (selectLastChatViewTab)+ // if a future refactor ever produced an empty list. The named guard makes that+ // a diagnosable failure. (The empty state is unreachable by construction, so+ // it is built directly here to exercise the guard.)+ describe('empty-tabs invariant guard', () => {+ it('activeChatViewTab throws a named invariant error instead of returning undefined', () => {+ expect(() => activeChatViewTab({ tabs: [], activeTabId: 'x' })).toThrow(/invariant/)+ })++ it('selectLastChatViewTab throws a named invariant error instead of a cryptic TypeError', () => {+ expect(() => selectLastChatViewTab({ tabs: [], activeTabId: 'x' })).toThrow(/invariant/)+ })+ })})diff --git a/desktop-app/ui/src/lib/chatViewTabs.ts b/desktop-app/ui/src/lib/chatViewTabs.tsindex f1acbaa82..7d4d565c4 100644--- a/desktop-app/ui/src/lib/chatViewTabs.ts+++ b/desktop-app/ui/src/lib/chatViewTabs.ts@@ -1,5 +1,24 @@import type { ChatViewTab, ChatViewTabsState, PersistedChatView } from './chatViewTabs.types'+/**+ * Module invariant: the tab list is never empty — `createChatViewTabsState`+ * seeds one tab and `closeChatViewTab` re-seeds when closing the last one. These+ * accessors turn a would-be silent `undefined` (or a cryptic `.id of undefined`)+ * into a named error if a future refactor ever breaks that invariant, instead of+ * the non-null assertions they replace.+ */+function firstTab(tabs: ChatViewTab[]): ChatViewTab {+ const tab = tabs[0]+ if (!tab) throw new Error('chatViewTabs invariant violated: tab list must never be empty')+ return tab+}++function lastTab(tabs: ChatViewTab[]): ChatViewTab {+ const tab = tabs.at(-1)+ if (!tab) throw new Error('chatViewTabs invariant violated: tab list must never be empty')+ return tab+}+export function createChatViewTabsState(id: string,agentRef: string | null = null@@ -11,7 +30,7 @@ export function createChatViewTabsState(}export function activeChatViewTab(state: ChatViewTabsState): ChatViewTab {- return state.tabs.find(tab => tab.id === state.activeTabId) ?? state.tabs[0]!+ return state.tabs.find(tab => tab.id === state.activeTabId) ?? firstTab(state.tabs)}export function addBlankChatViewTab(@@ -145,7 +164,7 @@ export function selectChatViewTabAt(state: ChatViewTabsState, index: number): Ch}export function selectLastChatViewTab(state: ChatViewTabsState): ChatViewTabsState {- return { ...state, activeTabId: state.tabs.at(-1)!.id }+ return { ...state, activeTabId: lastTab(state.tabs).id }}export function cycleChatViewTab(