Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop,hcc): preserve pending stateless conversations
Alfredo Lopeza7d2e5da86d8
diff --git a/Makefile b/Makefileindex de28a4e65..1f036f4c1 100644--- a/Makefile+++ b/Makefile@@ -1386,12 +1386,12 @@ test-e2e-stateless-durability: ## Run stateless host durability E2E gate (Phase.PHONY: test-e2e-stateless-suspend-waketest-e2e-stateless-suspend-wake: ## Run stateless suspend/wake E2E gate (Phase 1 API + Phase 2 Desktop Playwright; needs port-forwards + seeded chatllm-stateless)@echo "Running stateless suspend/wake E2E gate..."- KUBECONTEXT=$(E2E_KUBECONTEXT) bash scripts/e2e/e2e-stateless-suspend-wake.sh+ E2E_ALLOW_STATELESS_CADENCE_ACCELERATION=1 KUBECONTEXT=$(E2E_KUBECONTEXT) bash scripts/e2e/e2e-stateless-suspend-wake.sh.PHONY: test-e2e-stateless-wake-recoverytest-e2e-stateless-wake-recovery: ## Run stateless wake-recovery latency gate (R1 warm draining / R2 cold suspended / R3 drained-window p95 budgets; needs port-forwards + seeded chatllm-stateless)@echo "Running stateless wake-recovery latency gate..."- KUBECONTEXT=$(E2E_KUBECONTEXT) bash scripts/e2e/e2e-stateless-wake-recovery.sh+ E2E_ALLOW_STATELESS_CADENCE_ACCELERATION=1 KUBECONTEXT=$(E2E_KUBECONTEXT) bash scripts/e2e/e2e-stateless-wake-recovery.sh.PHONY: test-e2e-hcc-communicationchannel-watch-recoverytest-e2e-hcc-communicationchannel-watch-recovery: ## Run isolated minikube HCC watch-recovery fault-injection gatediff --git a/deploy/base/control-plane/host-context-controller.yaml b/deploy/base/control-plane/host-context-controller.yamlindex d6c0fa1e5..ec7a3d9f7 100644--- a/deploy/base/control-plane/host-context-controller.yaml+++ b/deploy/base/control-plane/host-context-controller.yaml@@ -76,6 +76,16 @@ spec:value: "8081"- name: CONTEXT_MAPPER_NAMESPACEvalue: mcp-server+ # Product baseline for stateless Hosts. HCC uses the effective idle window+ # max(idleMinutes, idleFloorMinutes), then drains active work before scale-to-zero.+ - name: CONTEXT_MAPPER_STATELESS_IDLE_MINUTES+ value: "30"+ - name: CONTEXT_MAPPER_STATELESS_IDLE_FLOOR_MINUTES+ value: "15"+ - name: CONTEXT_MAPPER_STATELESS_DRAIN_GRACE_MS+ value: "60000"+ - name: CONTEXT_MAPPER_HEARTBEAT_POLL_MS+ value: "10000"- name: CONTEXT_MAPPER_HOST_NAMESPACEvalue: mcp-host- name: CONTEXT_MAPPER_HOST_IMAGEdiff --git a/deploy/overlays/minikube/kustomization.yaml b/deploy/overlays/minikube/kustomization.yamlindex 3e2098779..912e2dee3 100644--- a/deploy/overlays/minikube/kustomization.yaml+++ b/deploy/overlays/minikube/kustomization.yaml@@ -83,6 +83,7 @@ images:newTag: testpatchesStrategicMerge:+ - patches/hcc-stateless-lifecycle.yaml- patches/hcc-policy-resync.yaml- patches/resource-limits.yaml- patches/dynamic-images.yamldiff --git a/deploy/overlays/minikube/patches/hcc-stateless-lifecycle.yaml b/deploy/overlays/minikube/patches/hcc-stateless-lifecycle.yamlnew file mode 100644index 000000000..79bba8029--- /dev/null+++ b/deploy/overlays/minikube/patches/hcc-stateless-lifecycle.yaml@@ -0,0 +1,22 @@+# Local Minikube mirrors the product stateless idle policy explicitly. This is+# also the baseline that pre-gate reconciliation restores after an accelerated+# lifecycle test mutates the live Deployment.+apiVersion: apps/v1+kind: Deployment+metadata:+ name: host-context-controller+ namespace: control-plane+spec:+ template:+ spec:+ containers:+ - name: host-context-controller+ env:+ - name: CONTEXT_MAPPER_STATELESS_IDLE_MINUTES+ value: '30'+ - name: CONTEXT_MAPPER_STATELESS_IDLE_FLOOR_MINUTES+ value: '15'+ - name: CONTEXT_MAPPER_STATELESS_DRAIN_GRACE_MS+ value: '60000'+ - name: CONTEXT_MAPPER_HEARTBEAT_POLL_MS+ value: '10000'diff --git a/desktop-app/src/__tests__/chatStore.test.ts b/desktop-app/src/__tests__/chatStore.test.tsindex d19c2e9e8..1674a5866 100644--- a/desktop-app/src/__tests__/chatStore.test.ts+++ b/desktop-app/src/__tests__/chatStore.test.ts@@ -342,6 +342,19 @@ describe('messages', () => {expect(messages[1]!.content).toBe('hello')})+ it('upserts an optimistic outgoing message and later updates it by the same id', async () => {+ await store.createChat('agent-1', 'upsert-send')+ const optimistic = { id: 'outgoing-1', role: 'user' as const, content: 'hello', timestamp: 1 }+ await store.upsertMessages('agent-1', 'upsert-send', [optimistic])+ await store.upsertMessages('agent-1', 'upsert-send', [+ { ...optimistic, task_id: 'task-1', timestamp: 2 },+ ])++ await expect(store.loadMessages('agent-1', 'upsert-send')).resolves.toEqual([+ expect.objectContaining({ id: 'outgoing-1', task_id: 'task-1' }),+ ])+ })+it('pagination with limit and offset', async () => {await store.createChat('agent-1', 'pag-1')const msgs = Array.from({ length: 10 }, (_, i) => ({diff --git a/desktop-app/src/chatStore.ts b/desktop-app/src/chatStore.tsindex cf12f6535..f12e9e1b6 100644--- a/desktop-app/src/chatStore.ts+++ b/desktop-app/src/chatStore.ts@@ -1954,6 +1954,31 @@ export class ChatStore {})}+ /**+ * Insert or update messages by reconciled identity before a send's task id is+ * known. A send is first persisted optimistically, then updated in place after+ * its acknowledgement; append-only writes would duplicate the same user row.+ */+ async upsertMessages(agentRef: string, chatId: string, messages: ChatMessage[]): Promise<void> {+ if (!messages.length) return+ return this.serializeChat(agentRef, chatId, async () => {+ const index = await this.getIndex(agentRef)+ if (this.isDeletedInScope(index, chatId)) return+ const existingMeta =+ (await this.readOrMigratePagedChatUnlocked(agentRef, chatId)) ??+ (await this.writePagedChatUnlocked(agentRef, chatId, []))+ const existingMessages = await this.readMessagesFromPagedMeta(agentRef, chatId, existingMeta)+ const merged = mergeReconciledMessages(existingMessages, messages)+ const indexedCount = await this.indexedMessageCount(agentRef, chatId)+ await this.writePagedChatUnlocked(agentRef, chatId, merged, {+ messageCount: Math.max(merged.length, indexedCount ?? 0),+ })+ await this.updateChatIndexFromMessages(agentRef, chatId, merged, {+ preserveExistingTotals: true,+ })+ })+ }+/*** Flag that a task terminated while the chat was not the active view (D.5).* Idempotent: a no-op when already flagged, so a noisy task can't spam FS writes.diff --git a/desktop-app/src/chatStoreBinding.ts b/desktop-app/src/chatStoreBinding.tsindex 67c52a491..5e9ca2764 100644--- a/desktop-app/src/chatStoreBinding.ts+++ b/desktop-app/src/chatStoreBinding.ts@@ -2,6 +2,7 @@ import { app } from 'electron'import { promises as fs } from 'node:fs'import { join } from 'node:path'import { ChatStore } from './chatStore.js'+import { resolveDevIsolation } from './devIsolation.js'import { assertSafeFilesystemSegment } from './pathSafety.js'import type { ChatAuthorityScope } from './types.js'@@ -14,12 +15,14 @@ const PREVIOUS_PAGED_INDEX_VERSION = 3const LEGACY_INDEX_VERSIONS = new Set([1])const CORRUPT_QUARANTINE_RETENTION_MS = 30 * 24 * 60 * 60 * 1000-// Default base dir derived from Electron's userData path. Tests override via+// Default base dir derived from the user's home. Tests override via// __setChatStoreBaseDirForTests to point at a tmpdir, which means the// app.getPath call below is only reached in production.let baseDirOverride: string | null = nullfunction currentBaseDir(): string {if (baseDirOverride !== null) return baseDirOverride+ const isolation = resolveDevIsolation(process.env, process.argv, app.isPackaged)+ if (isolation.mode === 'isolated') return join(isolation.plan.runDir, 'chats')return join(app.getPath('home'), '.clerum', 'chats')}diff --git a/desktop-app/src/ipc.ts b/desktop-app/src/ipc.tsindex b142d00b1..87c9d5b5e 100644--- a/desktop-app/src/ipc.ts+++ b/desktop-app/src/ipc.ts@@ -1573,6 +1573,22 @@ export function registerIpcHandlers(service: AppService): void {})+ ipcMain.handle(+ 'chat:upsertMessages',+ async (event, payload: { agentRef: string; chatId: string; messages: unknown[] }) => {+ assertTrustedSender(event)+ const agentRef = sanitizeString(payload?.agentRef)+ const chatId = sanitizeString(payload?.chatId)+ if (!agentRef || !chatId) throw new Error('agentRef and chatId are required')+ const messages = Array.isArray(payload?.messages) ? payload.messages : []+ await requireChatStore().upsertMessages(+ agentRef,+ chatId,+ messages as import('./types.js').ChatMessage[]+ )+ }+ )+ipcMain.handle('chat:replaceMessages',async (diff --git a/desktop-app/src/preload.ts b/desktop-app/src/preload.tsindex a87185b12..d9ce05167 100644--- a/desktop-app/src/preload.ts+++ b/desktop-app/src/preload.ts@@ -589,6 +589,8 @@ const clerum = Object.freeze({ipcRenderer.invoke('chat:loadMessages', { agentRef, chatId, limit, offset }),appendMessages: (agentRef: string, chatId: string, messages: unknown[]) =>ipcRenderer.invoke('chat:appendMessages', { agentRef, chatId, messages }),+ upsertMessages: (agentRef: string, chatId: string, messages: unknown[]) =>+ ipcRenderer.invoke('chat:upsertMessages', { agentRef, chatId, messages }),replaceMessages: (agentRef: string,chatId: string,diff --git a/desktop-app/src/renderer.d.ts b/desktop-app/src/renderer.d.tsindex a17977719..23e5ecaff 100644--- a/desktop-app/src/renderer.d.ts+++ b/desktop-app/src/renderer.d.ts@@ -718,6 +718,7 @@ declare global {offset?: number) => Promise<ChatMessage[]>appendMessages: (agentRef: string, chatId: string, messages: ChatMessage[]) => Promise<void>+ upsertMessages: (agentRef: string, chatId: string, messages: ChatMessage[]) => Promise<void>replaceMessages: (agentRef: string,chatId: string,diff --git a/desktop-app/ui/src/hooks/domain/__tests__/__fixtures__/mockClerum.ts b/desktop-app/ui/src/hooks/domain/__tests__/__fixtures__/mockClerum.tsindex 585983158..f9381498e 100644--- a/desktop-app/ui/src/hooks/domain/__tests__/__fixtures__/mockClerum.ts+++ b/desktop-app/ui/src/hooks/domain/__tests__/__fixtures__/mockClerum.ts@@ -26,6 +26,7 @@ interface ChatMock {delete: FnloadMessages: FnappendMessages: Fn+ upsertMessages: FnreplaceMessages: FnmarkUnreadTerminal: FnclearUnreadTerminal: Fn@@ -65,6 +66,7 @@ export interface MockClerum {export function installMockClerum(): MockClerum {const progressHandlers = new Map<string, Handler>()const activityHandlers = new Map<string, Handler>()+ const upsertedMessageIds = new Set<string>()const isoNow = () => new Date().toISOString()@@ -88,6 +90,15 @@ export function installMockClerum(): MockClerum {delete: vi.fn(async () => ({ cleanupPending: false })),loadMessages: vi.fn(async () => []),appendMessages: vi.fn(async () => undefined),+ upsertMessages: vi.fn(async (agentRef: string, chatId: string, messages: unknown[]) => {+ const unseen = messages.filter(message => {+ const id = (message as { id?: unknown }).id+ if (typeof id !== 'string' || upsertedMessageIds.has(id)) return false+ upsertedMessageIds.add(id)+ return true+ })+ if (unseen.length) await chat.appendMessages(agentRef, chatId, unseen)+ }),replaceMessages: vi.fn(async () => undefined),markUnreadTerminal: vi.fn(async () => undefined),clearUnreadTerminal: vi.fn(async () => undefined),diff --git a/desktop-app/ui/src/hooks/domain/__tests__/imageInputJourney.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/imageInputJourney.test.tsxindex f3f1d9027..89abdfec5 100644--- a/desktop-app/ui/src/hooks/domain/__tests__/imageInputJourney.test.tsx+++ b/desktop-app/ui/src/hooks/domain/__tests__/imageInputJourney.test.tsx@@ -331,6 +331,7 @@ describe('#654 visual send and recovery', () => {})expect(result.current.failedAgentSend).toBeNull()expect(result.current.agentError).toBeNull()- expect(bridge.chat.appendMessages).not.toHaveBeenCalled()+ expect(bridge.chat.upsertMessages).toHaveBeenCalledTimes(1)+ expect(bridge.chat.appendMessages).toHaveBeenCalledTimes(1)})})diff --git a/desktop-app/ui/src/hooks/domain/__tests__/switchToChat.unified.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/switchToChat.unified.test.tsxindex 142ef9c82..30d09eff6 100644--- a/desktop-app/ui/src/hooks/domain/__tests__/switchToChat.unified.test.tsx+++ b/desktop-app/ui/src/hooks/domain/__tests__/switchToChat.unified.test.tsx@@ -58,6 +58,8 @@ function deferred<T>() {return { promise, resolve }}+type SessionResult = Awaited<ReturnType<typeof clerum.rpc.loadSessionMessages>>+describe('switchToChat (unified, D.4)', () => {it('Phase 1 renders the cache, Phase 2 reconciles, and preserves the cache when there is no diff', async () => {const cached = [@@ -84,7 +86,7 @@ describe('switchToChat (unified, D.4)', () => {'agent-x','c1',undefined,- { limit: 40, afterTurn: 1 }+ { limit: 40, afterTurn: 0 })expect(clerum.chat.replaceMessages).not.toHaveBeenCalled()expect(result.current.chatMessages).toHaveLength(2)@@ -380,7 +382,7 @@ describe('switchToChat (unified, D.4)', () => {'agent-x','delta-pages',undefined,- { limit: 40, afterTurn: 1 }+ { limit: 40, afterTurn: 0 })expect(clerum.rpc.loadSessionMessages).toHaveBeenNthCalledWith(2,@@ -463,6 +465,46 @@ describe('switchToChat (unified, D.4)', () => {])})+ it('preserves turnless legacy history outside a partial authoritative window', async () => {+ const legacyMessages = Array.from({ length: 100 }, (_, index) => ({+ id: `legacy-${index + 1}`,+ role: index % 2 === 0 ? ('user' as const) : ('assistant' as const),+ content: `legacy-${index + 1}`,+ timestamp: index + 1,+ }))+ clerum.chat.loadMessages.mockResolvedValue(legacyMessages)+ clerum.rpc.loadSessionMessages.mockResolvedValue({+ agent: 'agent-x',+ chatId: 'legacy-partial-window',+ state: 'idle',+ totalTurns: 50,+ oldestTurnNumber: 11,+ latestTurnNumber: 50,+ hasMoreBefore: true,+ hasMoreAfter: false,+ turns: Array.from({ length: 40 }, (_, index) =>+ turn(index + 11, `server-q${index + 11}`, `server-a${index + 11}`)+ ),+ })+ const { result } = renderController()+ await settleMount()++ await act(async () => {+ await result.current.switchToChat('agent-x', 'legacy-partial-window')+ })++ expect(result.current.chatMessages.map(message => message.id)).toContain('legacy-1')+ expect(result.current.chatMessages.map(message => message.id)).toContain('turn-50-assistant')+ expect(clerum.chat.replaceMessages).toHaveBeenCalledWith(+ 'agent-x',+ 'legacy-partial-window',+ expect.arrayContaining([+ expect.objectContaining({ id: 'legacy-1' }),+ expect.objectContaining({ id: 'turn-11-user' }),+ ])+ )+ })+it('caps server delta pagination so reconcile cannot walk an unbounded cursor chain', async () => {clerum.chat.loadMessages.mockResolvedValue([{ id: 'turn-1-user', role: 'user' as const, content: 'q1', timestamp: 1 },@@ -518,8 +560,6 @@ describe('switchToChat (unified, D.4)', () => {'turn-4-assistant','turn-5-user','turn-5-assistant',- 'turn-6-user',- 'turn-6-assistant','turn-999-user','turn-999-assistant',])@@ -889,7 +929,7 @@ describe('switchToChat (unified, D.4)', () => {'agent-x','synced-chat',undefined,- { limit: 40, afterTurn: 1 }+ { limit: 40, afterTurn: 0 })expect(result.current.chatMessages.map(message => message.id)).toEqual(['turn-1-user',@@ -1054,6 +1094,73 @@ describe('switchToChat (unified, D.4)', () => {})+ it('re-fetches the newest cached turn so a response completed after caching is recovered', async () => {+ clerum.chat.loadMessages.mockResolvedValue([+ { id: 'turn-1-user', role: 'user' as const, content: 'q', timestamp: 1, serverTurnNumber: 1 },+ ])+ clerum.rpc.loadSessionMessages.mockResolvedValue({+ agent: 'agent-x',+ chatId: 'partial-turn',+ state: 'idle',+ totalTurns: 1,+ turns: [turn(1, 'q', 'completed answer')],+ })+ const { result } = renderController()+ await settleMount()++ await act(async () => {+ await result.current.switchToChat('agent-x', 'partial-turn')+ })++ expect(clerum.rpc.loadSessionMessages).toHaveBeenCalledWith(+ 'agent-x',+ 'agent-x',+ 'partial-turn',+ undefined,+ { limit: 40, afterTurn: 0 }+ )+ expect(result.current.chatMessages.map(message => message.content)).toEqual([+ 'q',+ 'completed answer',+ ])+ })++ it('reuses the in-flight selection when the same chat is reopened', async () => {+ const response = deferred<SessionResult>()+ clerum.chat.loadMessages.mockResolvedValue([])+ clerum.rpc.loadSessionMessages.mockImplementationOnce(() => response.promise)+ const { result } = renderController()+ await settleMount()++ let firstSwitch!: Promise<void>+ await act(async () => {+ firstSwitch = result.current.switchToChat('agent-x', 'same-chat')+ await waitFor(() => expect(clerum.rpc.loadSessionMessages).toHaveBeenCalledTimes(1))+ })+ let secondSwitch!: Promise<void>+ await act(async () => {+ secondSwitch = result.current.switchToChat('agent-x', 'same-chat')+ await Promise.resolve()+ })++ response.resolve({+ agent: 'agent-x',+ chatId: 'same-chat',+ state: 'idle',+ totalTurns: 1,+ turns: [turn(1, 'question', 'fresh answer')],+ })+ await act(async () => {+ await Promise.all([firstSwitch, secondSwitch])+ })++ expect(clerum.rpc.loadSessionMessages).toHaveBeenCalledTimes(1)+ expect(result.current.chatMessages.map(message => message.content)).toEqual([+ 'question',+ 'fresh answer',+ ])+ })+it('ignores a send 403 from the prior team scope', async () => {const revoked = new Set<string>()let rejectSend!: (reason: unknown) => voiddiff --git a/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.crossChat.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.crossChat.test.tsxindex 78a6b3628..80b572cb4 100644--- a/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.crossChat.test.tsx+++ b/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.crossChat.test.tsx@@ -45,14 +45,26 @@ function createChatMeta(chatId: string) {function installClerumHarness() {let taskIndex = 0- const chats: Array<ReturnType<typeof createChatMeta>> = []+ const chatsByAgent = new Map<string, Array<ReturnType<typeof createChatMeta>>>()const deletedChatIds = new Set<string>()- const messagesByChat = new Map<string, unknown[]>()+ const messagesByAgentChat = new Map<string, unknown[]>()+ const messageKey = (agentRef: string, chatId: string) => `${agentRef}\u0000${chatId}`const progressHandlers = new Map<string, ProgressHandler>()const loadSessionMessages = vi.fn(async () => ({ agent: 'trader', chatId: '', turns: [] }))const listSessions = vi.fn(async () => ({ items: [] }))- const appendMessages = vi.fn(async (_agentRef: string, chatId: string, messages: unknown[]) => {- messagesByChat.set(chatId, [...(messagesByChat.get(chatId) || []), ...messages])+ const appendMessages = vi.fn(async (agentRef: string, chatId: string, messages: unknown[]) => {+ const key = messageKey(agentRef, chatId)+ messagesByAgentChat.set(key, [...(messagesByAgentChat.get(key) || []), ...messages])+ })+ const upsertMessagesIntoMap = (agentRef: string, chatId: string, messages: unknown[]) => {+ const key = messageKey(agentRef, chatId)+ const existing = messagesByAgentChat.get(key) || []+ const byId = new Map(existing.map(message => [(message as { id: string }).id, message]))+ for (const message of messages) byId.set((message as { id: string }).id, message)+ messagesByAgentChat.set(key, [...byId.values()])+ }+ const upsertMessages = vi.fn(async (agentRef: string, chatId: string, messages: unknown[]) => {+ upsertMessagesIntoMap(agentRef, chatId, messages)})const invokeHostMessage = vi.fn(async (_agentRef: string, _payload: { threadId?: string }) => {@@ -65,12 +77,13 @@ function installClerumHarness() {writable: true,value: {chat: {- list: vi.fn(async () => chats),+ list: vi.fn(async (agentRef: string) => chatsByAgent.get(agentRef) || []),create: vi.fn(async (_agentRef: string, chatId: string) => {const meta = createChatMeta(chatId)- deletedChatIds.delete(chatId)- chats.push(meta)- messagesByChat.set(chatId, [])+ const agentChats = chatsByAgent.get(_agentRef) || []+ agentChats.push(meta)+ chatsByAgent.set(_agentRef, agentChats)+ messagesByAgentChat.set(messageKey(_agentRef, chatId), [])return meta}),rename: vi.fn(async () => undefined),@@ -83,23 +96,29 @@ function installClerumHarness() {})),delete: vi.fn(async (_agentRef: string, chatId: string) => {deletedChatIds.add(chatId)+ const chats = chatsByAgent.get(_agentRef) || []const index = chats.findIndex(chat => chat.id === chatId)if (index >= 0) chats.splice(index, 1)- messagesByChat.delete(chatId)+ messagesByAgentChat.delete(messageKey(_agentRef, chatId))return { cleanupPending: false }}),loadMessages: vi.fn(- async (_agentRef: string, chatId: string) => messagesByChat.get(chatId) || []+ async (agentRef: string, chatId: string) =>+ messagesByAgentChat.get(messageKey(agentRef, chatId)) || []),appendMessages,- replaceMessages: vi.fn(async (_agentRef: string, chatId: string, messages: unknown[]) => {- messagesByChat.set(chatId, [...messages])+ upsertMessages,+ replaceMessages: vi.fn(async (agentRef: string, chatId: string, messages: unknown[]) => {+ messagesByAgentChat.set(messageKey(agentRef, chatId), [...messages])}),markUnreadTerminal: vi.fn(async () => undefined),clearUnreadTerminal: vi.fn(async () => undefined),getLastActive: vi.fn(async () => null),setLastActive: vi.fn(async () => undefined),- getIndex: vi.fn(async () => ({ chats, deletedChatIds: [...deletedChatIds] })),+ getIndex: vi.fn(async (agentRef: string) => ({+ chats: chatsByAgent.get(agentRef) || [],+ deletedChatIds: [...deletedChatIds],+ })),dismissOnboarding: vi.fn(async () => undefined),},rpc: {@@ -126,11 +145,12 @@ function installClerumHarness() {return {invokeHostMessage,appendMessages,+ upsertMessages,progressHandlers,loadSessionMessages,listSessions,seedMessage: (chatId: string, content: string) =>- messagesByChat.set(chatId, [+ messagesByAgentChat.set(messageKey('trader', chatId), [{ id: `message-${chatId}`, role: 'user', content, createdAt: new Date().toISOString() },]),}@@ -139,9 +159,10 @@ function installClerumHarness() {function AgentChatHarness() {const [selectionState, setSelectionState] = React.useState('idle')const [sendState, setSendState] = React.useState('idle')+ const [selectedAgent, setSelectedAgent] = React.useState('trader')const vm = useAgentChatController({- selectedAgent: 'trader',- agentNames: ['trader'],+ selectedAgent,+ agentNames: ['trader', 'chatllm-stateless'],currentTeamId: 'team-1',currentEnvironmentKey: 'env-test',currentTeamName: 'Team One',@@ -184,6 +205,17 @@ function AgentChatHarness() {<button type="button" onClick={() => void vm.handleCreateChat()}>Create chat</button>+ {(['trader', 'chatllm-stateless'] as const).map(agent => (+ <button+ key={agent}+ type="button"+ onClick={() => {+ setSelectedAgent(agent)+ }}+ >+ Select agent {agent}+ </button>+ ))}<buttontype="button"onClick={() => {@@ -283,6 +315,59 @@ describe('useAgentChatController (cross-chat, migrated)', () => {await waitFor(() => expect(progressHandlers.size).toBe(0))})+ it('keeps an outgoing message visible across a Host switch while its POST is pending', async () => {+ const { invokeHostMessage, upsertMessages } = installClerumHarness()+ const persistOutgoingToMap = upsertMessages.getMockImplementation()+ let persistOutgoing!: () => void+ upsertMessages.mockImplementationOnce(+ (agentRef: string, chatId: string, messages: unknown[]) =>+ new Promise(resolve => {+ persistOutgoing = () => {+ persistOutgoingToMap?.(agentRef, chatId, messages)+ resolve(undefined)+ }+ })+ )+ let finishInvoke!: (result: { taskId: string; status: string }) => void+ invokeHostMessage.mockImplementationOnce(+ () =>+ new Promise(resolve => {+ finishInvoke = resolve+ })+ )+ render(+ <AgentTaskTrackerProvider>+ <AgentChatHarness />+ </AgentTaskTrackerProvider>+ )++ fireEvent.click(screen.getByRole('button', { name: 'Create chat' }))+ await waitFor(() => expect(screen.getByTestId('active-chat-id').textContent).not.toBe(''))+ const chatId = screen.getByTestId('active-chat-id').textContent || ''+ fireEvent.click(screen.getByRole('button', { name: 'Send message' }))+ await waitFor(() => expect(upsertMessages).toHaveBeenCalledTimes(1))++ fireEvent.click(screen.getByRole('button', { name: 'Select agent chatllm-stateless' }))+ await waitFor(() => expect(screen.getByTestId('active-chat-id').textContent).not.toBe(chatId))+ persistOutgoing()+ await waitFor(() => expect(invokeHostMessage).toHaveBeenCalledTimes(1))+ expect(screen.getByTestId('chat-message-count').textContent).toBe('0')+ fireEvent.click(screen.getByRole('button', { name: 'Select agent trader' }))+ await waitFor(() => expect(screen.getByTestId('active-chat-id').textContent).toBe(chatId))+ await waitFor(() => expect(screen.getByTestId('chat-message-count').textContent).toBe('1'))++ finishInvoke({ taskId: 'late-task', status: 'pending' })+ await waitFor(() => expect(screen.getByTestId('send-state').textContent).toBe('settled'))+ expect(screen.getByTestId('chat-message-count').textContent).toBe('1')+ expect(upsertMessages).toHaveBeenCalledTimes(2)+ expect(upsertMessages.mock.calls[0]?.[2]?.[0]).toMatchObject({ role: 'user' })+ expect(upsertMessages.mock.calls[0]?.[2]?.[0]).not.toHaveProperty('task_id')+ expect(upsertMessages.mock.calls[1]?.[2]?.[0]).toMatchObject({+ role: 'user',+ task_id: 'late-task',+ })+ })+it('keeps composer drafts scoped to their chat session', async () => {installClerumHarness()@@ -479,9 +564,6 @@ describe('useAgentChatController (cross-chat, migrated)', () => {fireEvent.click(screen.getByRole('button', { name: 'Create chat' }))await waitFor(() => expect(screen.getByTestId('active-chat-id').textContent).not.toBe(''))const deletedChatId = screen.getByTestId('active-chat-id').textContent || ''- await waitFor(() =>- expect(screen.getByTestId('latest-list-ids').textContent).toContain(deletedChatId)- )fireEvent.click(screen.getByRole('button', { name: 'Send message' }))await waitFor(() => expect(invokeHostMessage).toHaveBeenCalledTimes(1))fireEvent.change(screen.getByTestId('draft-input'), {@@ -499,7 +581,9 @@ describe('useAgentChatController (cross-chat, migrated)', () => {finishInvoke({ taskId: 'late-deleted-task', status: 'pending' })await waitFor(() => expect(screen.getByTestId('send-state').textContent).toBe('settled'))expect(screen.getByTestId('chat-list-ids').textContent).not.toContain(deletedChatId)- expect(screen.getByTestId('latest-list-ids').textContent).not.toContain(deletedChatId)+ await waitFor(() =>+ expect(screen.getByTestId('latest-list-ids').textContent).not.toContain(deletedChatId)+ )expect(getComposerDraft(deletedChatId)).toBe('')expect(appendMessages.mock.calls.length).toBe(writesBeforeLateAck)diff --git a/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.pendingModel.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.pendingModel.test.tsxindex 8f5638af5..2efc854f1 100644--- a/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.pendingModel.test.tsx+++ b/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.pendingModel.test.tsx@@ -81,6 +81,12 @@ function installClerumHarness() {appendMessages: vi.fn(async (_agentRef: string, chatId: string, messages: unknown[]) => {messagesByChat.set(chatId, [...(messagesByChat.get(chatId) || []), ...messages])}),+ upsertMessages: vi.fn(async (_agentRef: string, chatId: string, messages: unknown[]) => {+ const existing = messagesByChat.get(chatId) || []+ const byId = new Map(existing.map(message => [(message as { id: string }).id, message]))+ for (const message of messages) byId.set((message as { id: string }).id, message)+ messagesByChat.set(chatId, [...byId.values()])+ }),replaceMessages: vi.fn(async (_agentRef: string, chatId: string, messages: unknown[]) => {messagesByChat.set(chatId, [...messages])}),diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.tsindex a0d9dbc1a..1a9dfdee4 100644--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts@@ -583,6 +583,7 @@ export function useAgentChatController({selectedAgent,})const activeChatSwitchRequestRef = useRef<symbol | null>(null)+ const activeChatSwitchKeyRef = useRef<string | null>(null)// Latest `pushToast` for the hoisted tracker callbacks (onTrackerTerminal reads// it) so that `tracker.setCallbacks` can run once (cross-ref D.3 M1) without// closing over a stale toast fn. Assigned in an effect, not the render body@@ -917,13 +918,21 @@ export function useAgentChatController({const switchToChat = useCallback(async (agentRef: string, chatId: string) => {if (isHostAccessBlocked(agentRef) || isChatDeleted(agentRef, chatId)) return- const switchRequest = Symbol(`${agentRef}:${chatId}`)- activeChatSwitchRequestRef.current = switchRequestconst key = makeTaskKey(agentRef, chatId)const visibleBeforeSwitch = activeChatVisibilityRef.currentconst reopeningActiveChat =visibleBeforeSwitch.selectedAgent === agentRef &&visibleBeforeSwitch.activeChatId === chatId+ const switchKey = `${currentTeamId}\u0000${key}`+ const canReuseActiveSwitch =+ reopeningActiveChat && activeChatSwitchKeyRef.current === switchKey+ const switchRequest = canReuseActiveSwitch+ ? activeChatSwitchRequestRef.current!+ : Symbol(`${agentRef}:${chatId}`)+ if (!canReuseActiveSwitch) {+ activeChatSwitchRequestRef.current = switchRequest+ activeChatSwitchKeyRef.current = switchKey+ }// A same-chat click must coalesce onto an already-running loud recovery.// Superseding it would discard the stream-loss caller that owns the Resend// or durable-result UX. Genuine chat changes still cancel stale work.@@ -935,11 +944,13 @@ export function useAgentChatController({selectedAgent: agentRef,}setActiveChatId(chatId)- setChatMessages([])- setChatMessagesLoading(true)- setHasOlderMessages(false)- cancelOlderMessagesLoad()- loadedLocalMessageCountRef.current = 0+ if (!canReuseActiveSwitch) {+ setChatMessages([])+ setChatMessagesLoading(true)+ setHasOlderMessages(false)+ cancelOlderMessagesLoad()+ loadedLocalMessageCountRef.current = 0+ }// The error/resend banner is per-chat view state, not global: clear it on// every chat switch (same-agent switches don't trip the selectedAgent// cleanup effect, so without this a stream error from chat A would bleed@@ -969,7 +980,13 @@ export function useAgentChatController({// below requests a delta after the newest cached server turn.let cached: Awaited<ReturnType<typeof chatStore.loadMessages>> = []try {- cached = await chatStore.loadMessages(agentRef, chatId, LOCAL_MESSAGE_PAGE_SIZE)+ cached = canReuseActiveSwitch+ ? ([...chatMessagesRef.current] as AgentChatMessage[])+ : ((await chatStore.loadMessages(+ agentRef,+ chatId,+ LOCAL_MESSAGE_PAGE_SIZE+ )) as AgentChatMessage[])} catch (error) {console.warn('[chat-history] failed to load local messages', { agentRef, chatId, error })}@@ -982,7 +999,7 @@ export function useAgentChatController({if (!isStillActive()) returnloadedLocalMessageCountRef.current = cached.lengthsetHasOlderMessages(hasOlderLocalMessages || hasOlderServerMessages)- setChatMessages(cached as AgentChatMessage[])+ if (!canReuseActiveSwitch) setChatMessages(cached as AgentChatMessage[])// Phase 1 rendered the cache → clear the blocking spinner. The reconcile// runs under the `syncing` indicator (RECONCILE_STARTED, dispatched by the// reconcile gate) instead.@@ -1022,13 +1039,15 @@ export function useAgentChatController({// own `isActive()` guard still prevents rendering into the wrong chat.const zombieBefore = tracker.get(key)const latestCachedTurn = latestServerTurnNumber(cached as AgentChatMessage[])+ const reconcileFromTurn =+ latestCachedTurn === undefined ? undefined : Math.max(0, latestCachedTurn - 1)await reconcileChat(key, {reason: 'switch_to_chat',taskIdHint: zombieBefore?.taskId,isRelevant: isStillActive,messagesQuery: {limit: SERVER_TURN_PAGE_SIZE,- ...(latestCachedTurn !== undefined ? { afterTurn: latestCachedTurn } : {}),+ ...(reconcileFromTurn !== undefined ? { afterTurn: reconcileFromTurn } : {}),},})},@@ -1634,9 +1653,9 @@ export function useAgentChatController({const localHasServerTurns = localMessages.some(message => serverTurnNumber(message) !== undefined)+ const responseContainsAllTurns =+ resp.totalTurns !== undefined ? resp.turns.length >= resp.totalTurns : !resp.hasMoreBeforeif (!localHasServerTurns && localMessages.length > 0) {- const responseContainsAllTurns =- resp.totalTurns === undefined || resp.totalTurns === resp.turns.lengthif (responseContainsAllTurns && hydrated.length <= localMessages.length) {return { rendered: localMessages, replaced: false, cached: localMessages, stale: false }}@@ -1655,7 +1674,10 @@ export function useAgentChatController({// of persisting both copies. A reported live task keeps the merge path so// its optimistic bubbles remain visible until task-scoped reconciliation.const replaceSettledLegacyCache =- localMessages.length > 0 && !localHasServerTurns && !resp.activeTaskId+ localMessages.length > 0 &&+ !localHasServerTurns &&+ !resp.activeTaskId &&+ responseContainsAllTurnsconst rendered =localMessages.length > 0? mergeServerMessages(@@ -1984,9 +2006,11 @@ export function useAgentChatController({return []})) as AgentChatMessage[]const latestCachedTurn = latestServerTurnNumber(cached)+ const reconcileFromTurn =+ latestCachedTurn === undefined ? undefined : Math.max(0, latestCachedTurn - 1)requestedQuery = {...requestedQuery,- ...(latestCachedTurn !== undefined ? { afterTurn: latestCachedTurn } : {}),+ ...(reconcileFromTurn !== undefined ? { afterTurn: reconcileFromTurn } : {}),}}let response = await chatStore.loadSessionMessages(@@ -2772,6 +2796,9 @@ export function useAgentChatController({sendScopeIdentity === currentAuthScopeRef.current &&!isHostAccessBlocked(sendAgent) &&(!sendChatId || !isChatDeleted(sendAgent, sendChatId))+ const sendChatIsVisible = () =>+ activeChatVisibilityRef.current.selectedAgent === sendAgent &&+ activeChatVisibilityRef.current.activeChatId === sendChatId// Captured for the retention snapshot: the model this attempt actually// asked for (issue #654), readable from the catch below.let requestModelForRetention: string | undefined@@ -2781,6 +2808,7 @@ export function useAgentChatController({// the freshly-created meta so the lookup can fall back to it.let autoCreatedMeta: Awaited<ReturnType<typeof chatStore.createChat>> | undefinedif (!sendChatId) {+ const wasSendChatVisible = sendChatIsVisible()try {const chatId = crypto.randomUUID()const meta = await chatStore.createChat(sendAgent, chatId)@@ -2803,13 +2831,15 @@ export function useAgentChatController({chatStore.setPendingModel(sendAgent, chatId, preChatModel)chatStore.clearPreChatModel(sendAgent)}- activeChatVisibilityRef.current = {- ...activeChatVisibilityRef.current,- activeChatId: chatId,- selectedAgent: sendAgent,+ if (wasSendChatVisible) {+ activeChatVisibilityRef.current = {+ ...activeChatVisibilityRef.current,+ activeChatId: chatId,+ selectedAgent: sendAgent,+ }+ setActiveChatId(chatId)+ setChatMessages([])}- setActiveChatId(chatId)- setChatMessages([])await chatStore.setLastActive(sendAgent, chatId)if (!sendStillAuthorized()) {releaseSendSetup()@@ -2826,6 +2856,10 @@ export function useAgentChatController({return}}+ if (!sendStillAuthorized()) {+ releaseSendSetup()+ return+ }const userMessageId = crypto.randomUUID()const baseContentForRequest =trimmedContent ||@@ -2847,15 +2881,34 @@ export function useAgentChatController({timestamp: Date.now(),...(displayAttachments.length ? { attachments: displayAttachments } : {}),}+ // Persist before the async POST so a Host wake, approval, tab switch, or+ // Desktop restart cannot erase the only copy of the user's outgoing turn.+ // The acknowledgement below updates this same ID with its task id.+ if (sendChatId) {+ try {+ await chatStore.upsertMessages(sendAgent, sendChatId, [userMessage])+ } catch (error) {+ console.warn('[chat-history] failed to persist outgoing message before send', {+ agentRef: sendAgent,+ chatId: sendChatId,+ error,+ })+ pushToast('Could not save this message locally. Please retry.', 'error')+ releaseSendSetup()+ return+ }+ }// Sidebar freshness (dev): bump this chat's updatedAt/messageCount so the// cross-agent list and the chat list re-sort to the top on send. The user- // message itself is persisted once, later, carrying its task_id (post-D.3),- // so we do NOT eager-persist here (that would double-write without task_id).+ // message is already durably staged by the upsert above and is updated in+ // place when the send acknowledgement carries its task_id.if (selectedAgent && sendChatId) {const updatedAt = new Date(userMessage.timestamp).toISOString()bumpActivity(selectedAgent, sendChatId, updatedAt)}- setChatMessages(prev => [...prev, userMessage])+ if (sendChatIsVisible()) {+ setChatMessages(prev => [...prev, userMessage])+ }// Auto-title on first messageif (selectedAgent && sendChatId) {// B10: fall back to the meta just returned by createChat — the closure's@@ -3036,7 +3089,7 @@ export function useAgentChatController({}),}if (sendChatId) {- await chatStore.appendMessages(sendAgent, sendChatId, [userMessage])+ await chatStore.upsertMessages(sendAgent, sendChatId, [userMessage])}await appendAssistantMessage(sendAgent, sendChatId, assistantMessage)if (isErrorResponse) {@@ -3096,9 +3149,13 @@ export function useAgentChatController({// Async task accepted. Persist the user message once, now carrying its// task_id (schema v2), then hand the SSE lifecycle to the tracker.const persistedUserMessage: AgentChatMessage = { ...userMessage, task_id: taskId }- setChatMessages(prev => prev.map(m => (m.id === userMessageId ? persistedUserMessage : m)))+ if (sendChatIsVisible()) {+ setChatMessages(prev =>+ prev.map(m => (m.id === userMessageId ? persistedUserMessage : m))+ )+ }if (sendChatId) {- await chatStore.appendMessages(sendAgent, sendChatId, [persistedUserMessage])+ await chatStore.upsertMessages(sendAgent, sendChatId, [persistedUserMessage])}attachTaskIdToRetainedSend(sendAgent, sendChatId ?? null, userMessageId, taskId)activityTaskToMessageByAgentRef.current[sendAgent] = {@@ -3219,10 +3276,12 @@ export function useAgentChatController({if (sendChatId) {try {const failedUserMessage: AgentChatMessage = { ...userMessage, preserveLocal: true }- setChatMessages(prev =>- prev.map(message => (message.id === userMessageId ? failedUserMessage : message))- )- await chatStore.appendMessages(sendAgent, sendChatId, [failedUserMessage])+ if (sendChatIsVisible()) {+ setChatMessages(prev =>+ prev.map(message => (message.id === userMessageId ? failedUserMessage : message))+ )+ }+ await chatStore.upsertMessages(sendAgent, sendChatId, [failedUserMessage])} catch {// persistence is best-effort; the in-memory view still shows it}diff --git a/desktop-app/ui/src/hooks/domain/useChatListController.ts b/desktop-app/ui/src/hooks/domain/useChatListController.tsindex df239312d..1c531ee8d 100644--- a/desktop-app/ui/src/hooks/domain/useChatListController.ts+++ b/desktop-app/ui/src/hooks/domain/useChatListController.ts@@ -208,6 +208,7 @@ export function useChatListController({// Cross-agent "Latest sessions" list (badges live in the FSM, seeded below).const [latestChatSessions, setLatestChatSessions] = useState<LatestSidebarChatEntry[]>([])const [latestChatSessionsLoading, setLatestChatSessionsLoading] = useState(false)+ const [latestCatalogMutationRevision, setLatestCatalogMutationRevision] = useState(0)// Selection requested for an agent before its chats have loaded, consumed by// the parent's agent-selection effect. Ref (not state): imperative, per-agent.const pendingChatSelectionByAgentRef = useRef<Record<string, PendingChatSelection>>({})@@ -349,6 +350,15 @@ export function useChatListController({)}, [])+ // A catalog request can resolve with a pre-delete snapshot just as its owning+ // component commit lands. Tombstones are imperative truth, so reproject them+ // onto the published list after every confirmed destructive mutation.+ useEffect(() => {+ setLatestChatSessions(previous =>+ previous.filter(item => !deletedChatIdsByAgentRef.current.get(item.agentRef)?.has(item.id))+ )+ }, [latestCatalogMutationRevision])+// ─── chatList loader (agent-scoped) ───const loadChatListOnce = useCallback(@@ -1254,6 +1264,7 @@ export function useChatListController({const deleted = deletedChatIdsByAgentRef.current.get(agentRef) ?? new Set<string>()deleted.add(chatId)deletedChatIdsByAgentRef.current.set(agentRef, deleted)+ setLatestCatalogMutationRevision(value => value + 1)chatStore.clearCachedRemoteData()host.current?.clearComposerDraft(chatId)removeLatestChatSession(agentRef, chatId)diff --git a/desktop-app/ui/src/hooks/useChatStore.ts b/desktop-app/ui/src/hooks/useChatStore.tsindex bcfd120f2..57dcbe10a 100644--- a/desktop-app/ui/src/hooks/useChatStore.ts+++ b/desktop-app/ui/src/hooks/useChatStore.ts@@ -91,6 +91,11 @@ export function useChatStore() {window.clerum.chat.appendMessages(agentRef, chatId, messages),[])+ const upsertMessages = useCallback(+ (agentRef: string, chatId: string, messages: ChatMessage[]) =>+ window.clerum.chat.upsertMessages(agentRef, chatId, messages),+ []+ )const backfillCounters = useCallback((agentRef: string, chatId: string, messages: ChatMessage[]) => {const backfill = window.clerum.chat.backfillCounters@@ -258,6 +263,7 @@ export function useChatStore() {deleteChat,loadMessages,appendMessages,+ upsertMessages,replaceMessages,backfillCounters,markUnreadTerminal,diff --git a/scripts/e2e/e2e-stateless-cron-block.sh b/scripts/e2e/e2e-stateless-cron-block.shindex f13a66b3c..d3a260d9c 100755--- a/scripts/e2e/e2e-stateless-cron-block.sh+++ b/scripts/e2e/e2e-stateless-cron-block.sh@@ -5,6 +5,8 @@# because tool selection is model-driven in the live chat path. This T2 gate# proves the runtime lifecycle contract with deterministic heartbeat evidence:# activeCronSchedules blocks suspension, and clearing it releases suspension.+# It accelerates HCC cadences only with E2E_ALLOW_STATELESS_CADENCE_ACCELERATION=1+# and fails cleanup if the saved values are not restored and read back exactly.## Registered gate: e2e-stateless-cron-block (run directly; not wired to Make).set -euo pipefail@@ -184,6 +186,10 @@ probe_pod_cron_policy_artifacts() {}save_and_set_hcc_cadences() {+ if [ "${E2E_ALLOW_STATELESS_CADENCE_ACCELERATION:-0}" != 1 ]; then+ fail "refusing to accelerate stateless lifecycle cadences; set E2E_ALLOW_STATELESS_CADENCE_ACCELERATION=1 on an owned profile"+ return 1+ fiheader "PRECONDITION (labeled setup) -- HCC test cadences (idle=${TEST_IDLE_MINUTES}min, drain=${TEST_DRAIN_GRACE_MS}ms)"local keys=(CONTEXT_MAPPER_STATELESS_IDLE_MINUTES CONTEXT_MAPPER_STATELESS_IDLE_FLOOR_MINUTES \CONTEXT_MAPPER_STATELESS_DRAIN_GRACE_MS CONTEXT_MAPPER_HEARTBEAT_POLL_MS)@@ -208,14 +214,32 @@ save_and_set_hcc_cadences() {restore_hcc_cadences() {[ -n "$HCC_ENV_SAVED" ] || return 0log "restoring HCC cadence env"- local args=() line k v+ local args=() line k v actualwhile IFS= read -r line; do[ -z "$line" ] && continuek="${line%%=*}"; v="${line#*=}"if [ -n "$v" ]; then args+=("${k}=${v}"); else args+=("${k}-"); fidone <<< "$HCC_ENV_SAVED"- [ ${#args[@]} -gt 0 ] && kctl set env "deployment/${HCC_DEPLOY}" -n "$HCC_NS" "${args[@]}" >/dev/null 2>&1 || true- kctl rollout status "deployment/${HCC_DEPLOY}" -n "$HCC_NS" --timeout=120s >/dev/null 2>&1 || true+ if [ ${#args[@]} -eq 0 ]; then return 0; fi+ if ! kctl set env "deployment/${HCC_DEPLOY}" -n "$HCC_NS" "${args[@]}" >/dev/null 2>&1; then+ fail "failed to restore HCC cadence env"+ return 1+ fi+ if ! kctl rollout status "deployment/${HCC_DEPLOY}" -n "$HCC_NS" --timeout=120s >/dev/null 2>&1; then+ fail "HCC rollout did not settle after cadence restore"+ return 1+ fi+ while IFS= read -r line; do+ [ -z "$line" ] && continue+ k="${line%%=*}"; v="${line#*=}"+ actual="$(kctl get "deployment/${HCC_DEPLOY}" -n "$HCC_NS" \+ -o jsonpath="{.spec.template.spec.containers[0].env[?(@.name=='${k}')].value}" 2>/dev/null || true)"+ if [ "$actual" != "$v" ]; then+ fail "HCC cadence restore readback mismatch for ${k}: expected '${v}', got '${actual}'"+ return 1+ fi+ done <<< "$HCC_ENV_SAVED"+ HCC_ENV_SAVED=""}restore_cron_allow_env() {@@ -255,7 +279,7 @@ cleanup() {local rc=$?stop_active_cron_pin "$CRON_POD_UID" >/dev/null 2>&1 || truerestore_cron_allow_env- restore_hcc_cadences+ restore_hcc_cadences || rc=1print_resultsexit "$rc"}diff --git a/scripts/e2e/e2e-stateless-suspend-wake.sh b/scripts/e2e/e2e-stateless-suspend-wake.shindex 937543a29..d6cfa2980 100755--- a/scripts/e2e/e2e-stateless-suspend-wake.sh+++ b/scripts/e2e/e2e-stateless-suspend-wake.sh@@ -53,6 +53,8 @@# CONTEXT_MAPPER_STATELESS_IDLE_MINUTES = 1 (minimum legal T_idle; tracker rejects 0)# CONTEXT_MAPPER_STATELESS_DRAIN_GRACE_MS = 20000# CONTEXT_MAPPER_HEARTBEAT_POLL_MS = 5000+# Acceleration is opt-in with E2E_ALLOW_STATELESS_CADENCE_ACCELERATION=1, and+# cleanup fails loud if kubectl set env, rollout, or exact readback fails.## Prereqs (each a HARD FAIL with the concrete reason):# - Host CR chatllm-stateless with spec.lifecycle.stateless=true + pod Ready@@ -61,7 +63,8 @@# - host image present on the node (pod already Ready proves this)## Usage:-# KUBECONTEXT=clerum-test bash scripts/e2e/e2e-stateless-suspend-wake.sh+# E2E_ALLOW_STATELESS_CADENCE_ACCELERATION=1 \+# KUBECONTEXT=clerum-test bash scripts/e2e/e2e-stateless-suspend-wake.sh# E2E_SKIP_DESKTOP_PHASE=1 ... # opt out of the Playwright phase (exit 3)# ======================================================================set -euo pipefail@@ -128,17 +131,31 @@ STATELESS_DISABLED=0restore_hcc_env() {[ -n "$HCC_ENV_SAVED" ] || return 0- local args=() key val+ local args=() key val actualwhile IFS='=' read -r key val; do[ -n "$key" ] || continueif [ -n "$val" ]; then args+=("${key}=${val}"); else args+=("${key}-"); fidone <<< "$HCC_ENV_SAVED"[ ${#args[@]} -gt 0 ] || return 0log "Restoring HCC cadence env on deployment/${HCC_DEPLOY}"- kctl set env "deployment/${HCC_DEPLOY}" -n "$HCC_NS" "${args[@]}" >/dev/null 2>&1 || \- warn "failed to restore HCC env (manual check advised)"- kctl rollout status "deployment/${HCC_DEPLOY}" -n "$HCC_NS" --timeout=180s >/dev/null 2>&1 || \+ if ! kctl set env "deployment/${HCC_DEPLOY}" -n "$HCC_NS" "${args[@]}" >/dev/null 2>&1; then+ warn "failed to restore HCC env (manual check required)"+ return 1+ fi+ if ! kctl rollout status "deployment/${HCC_DEPLOY}" -n "$HCC_NS" --timeout=180s >/dev/null 2>&1; thenwarn "HCC rollout did not settle after env restore"+ return 1+ fi+ while IFS='=' read -r key val; do+ [ -n "$key" ] || continue+ actual="$(kctl get "deployment/${HCC_DEPLOY}" -n "$HCC_NS" \+ -o jsonpath="{.spec.template.spec.containers[0].env[?(@.name=='${key}')].value}" 2>/dev/null || true)"+ if [ "$actual" != "$val" ]; then+ warn "HCC restore readback mismatch for ${key}: expected '${val}', got '${actual}'"+ return 1+ fi+ done <<< "$HCC_ENV_SAVED"+ HCC_ENV_SAVED=""}cleanup_on_exit() {@@ -172,7 +189,7 @@ cleanup_on_exit() {--ignore-not-found --grace-period=0 --force >/dev/null 2>&1fiif [ -n "$CORDONED_NODE" ]; then kctl uncordon "$CORDONED_NODE" >/dev/null 2>&1; fi- restore_hcc_env+ restore_hcc_env || status=1exit "$status"}trap cleanup_on_exit EXIT@@ -554,6 +571,10 @@ force_idle_and_suspend() {}save_and_set_hcc_cadences() {+ if [ "${E2E_ALLOW_STATELESS_CADENCE_ACCELERATION:-0}" != 1 ]; then+ fail "refusing to accelerate stateless lifecycle cadences; set E2E_ALLOW_STATELESS_CADENCE_ACCELERATION=1 on an owned profile"+ return 1+ fiheader "Setting HCC test cadences (idle-immediate, drain=${TEST_DRAIN_GRACE_MS}ms, poll=${TEST_POLL_MS}ms)"local keys=(CONTEXT_MAPPER_STATELESS_IDLE_MINUTES CONTEXT_MAPPER_STATELESS_IDLE_FLOOR_MINUTES \CONTEXT_MAPPER_STATELESS_DRAIN_GRACE_MS CONTEXT_MAPPER_HEARTBEAT_POLL_MS)diff --git a/scripts/e2e/e2e-stateless-wake-recovery.sh b/scripts/e2e/e2e-stateless-wake-recovery.shindex 4f8dea47a..924e04193 100755--- a/scripts/e2e/e2e-stateless-wake-recovery.sh+++ b/scripts/e2e/e2e-stateless-wake-recovery.sh@@ -119,6 +119,9 @@# CONTEXT_MAPPER_STATELESS_IDLE_MINUTES = 1# CONTEXT_MAPPER_STATELESS_DRAIN_GRACE_MS = 20000# CONTEXT_MAPPER_HEARTBEAT_POLL_MS = 5000+# Acceleration is opt-in with E2E_ALLOW_STATELESS_CADENCE_ACCELERATION=1.+# Cleanup fails loud if kubectl set env, rollout, or exact readback fails;+# a nominal test result can never hide a leaked one-minute profile policy.# Reaching state=draining after a served turn therefore needs idle# floor (60s) + one emitter tick (30s) + one HCC poll (5s) + slack =># DRAINING_WAIT default 150s. Full suspend adds drain grace (20s) =>@@ -134,7 +137,8 @@# - state.db row observable resolvable (exactly-once assertion in R3)## Usage:-# KUBECONTEXT=clerum-test bash scripts/e2e/e2e-stateless-wake-recovery.sh+# E2E_ALLOW_STATELESS_CADENCE_ACCELERATION=1 \+# KUBECONTEXT=clerum-test bash scripts/e2e/e2e-stateless-wake-recovery.sh# RECOVERY_CYCLES=5 WARM_RECOVERY_BUDGET_MS=3000 ...# ======================================================================set -euo pipefail@@ -185,24 +189,38 @@ HCC_ENV_SAVED=""restore_hcc_env() {[ -n "$HCC_ENV_SAVED" ] || return 0- local args=() key val+ local args=() key val actualwhile IFS='=' read -r key val; do[ -n "$key" ] || continueif [ -n "$val" ]; then args+=("${key}=${val}"); else args+=("${key}-"); fidone <<< "$HCC_ENV_SAVED"[ ${#args[@]} -gt 0 ] || return 0log "Restoring HCC cadence env on deployment/${HCC_DEPLOY}"- kctl set env "deployment/${HCC_DEPLOY}" -n "$HCC_NS" "${args[@]}" >/dev/null 2>&1 || \- warn "failed to restore HCC env (manual check advised)"- kctl rollout status "deployment/${HCC_DEPLOY}" -n "$HCC_NS" --timeout=180s >/dev/null 2>&1 || \+ if ! kctl set env "deployment/${HCC_DEPLOY}" -n "$HCC_NS" "${args[@]}" >/dev/null 2>&1; then+ warn "failed to restore HCC env (manual check required)"+ return 1+ fi+ if ! kctl rollout status "deployment/${HCC_DEPLOY}" -n "$HCC_NS" --timeout=180s >/dev/null 2>&1; thenwarn "HCC rollout did not settle after env restore"+ return 1+ fi+ while IFS='=' read -r key val; do+ [ -n "$key" ] || continue+ actual="$(kctl get "deployment/${HCC_DEPLOY}" -n "$HCC_NS" \+ -o jsonpath="{.spec.template.spec.containers[0].env[?(@.name=='${key}')].value}" 2>/dev/null || true)"+ if [ "$actual" != "$val" ]; then+ warn "HCC restore readback mismatch for ${key}: expected '${val}', got '${actual}'"+ return 1+ fi+ done <<< "$HCC_ENV_SAVED"+ HCC_ENV_SAVED=""}cleanup_on_exit() {local status=$?set +erm -f "$CYCLES_FILE" >/dev/null 2>&1- restore_hcc_env+ restore_hcc_env || status=1exit "$status"}trap cleanup_on_exit EXIT@@ -490,6 +508,10 @@ force_idle_and_suspend() {}save_and_set_hcc_cadences() {+ if [ "${E2E_ALLOW_STATELESS_CADENCE_ACCELERATION:-0}" != 1 ]; then+ fail "refusing to accelerate stateless lifecycle cadences; set E2E_ALLOW_STATELESS_CADENCE_ACCELERATION=1 on an owned profile"+ return 1+ fiheader "PRECONDITION (labeled setup) -- HCC test cadences (idle=1min, drain=${TEST_DRAIN_GRACE_MS}ms, poll=${TEST_POLL_MS}ms)"local keys=(CONTEXT_MAPPER_STATELESS_IDLE_MINUTES CONTEXT_MAPPER_STATELESS_IDLE_FLOOR_MINUTES \CONTEXT_MAPPER_STATELESS_DRAIN_GRACE_MS CONTEXT_MAPPER_HEARTBEAT_POLL_MS)diff --git a/scripts/minikube/pre-gate-incremental.sh b/scripts/minikube/pre-gate-incremental.shindex 5e88eb151..fca58419d 100755--- a/scripts/minikube/pre-gate-incremental.sh+++ b/scripts/minikube/pre-gate-incremental.sh@@ -445,8 +445,8 @@ incremental_build_images() {log "Building all images because the change cannot be safely targeted"(cd "${PROJECT_DIR}"- make minikube-build-images- make minikube-verify-images+ T2_SKIP_LOCK=true T2_LOCK_TOKEN="${T2_LOCK_TOKEN}" make minikube-build-images+ T2_SKIP_LOCK=true T2_LOCK_TOKEN="${T2_LOCK_TOKEN}" make minikube-verify-images)return 0fi@@ -479,8 +479,8 @@ incremental_build_images_ghcr() {log "Re-pulling the release image set (${IMAGE_TAG}) before shadowing the changed images"(cd "${PROJECT_DIR}"- make minikube-pull-images- make minikube-verify-images+ T2_SKIP_LOCK=true T2_LOCK_TOKEN="${T2_LOCK_TOKEN}" make minikube-pull-images+ T2_SKIP_LOCK=true T2_LOCK_TOKEN="${T2_LOCK_TOKEN}" make minikube-verify-images)fidiff --git a/scripts/tests/test-minikube-pre-gate-shadow.sh b/scripts/tests/test-minikube-pre-gate-shadow.shindex 7f1f3d948..c667b2192 100755--- a/scripts/tests/test-minikube-pre-gate-shadow.sh+++ b/scripts/tests/test-minikube-pre-gate-shadow.sh@@ -127,7 +127,9 @@ exit 0STUBcat > "$d/bin/make" <<'STUB'#!/usr/bin/env bash-printf 'make %s\n' "$*" >>"${TEST_LOG_FILE:?}"+printf 'make %s T2_SKIP_LOCK=%s T2_LOCK_TOKEN_SET=%s\n' \+ "$*" "${T2_SKIP_LOCK:-false}" "$([ -n "${T2_LOCK_TOKEN:-}" ] && printf true || printf false)" \+ >>"${TEST_LOG_FILE:?}"exit 0STUBchmod +x "$d/bin/docker" "$d/bin/minikube" "$d/bin/kubectl" "$d/bin/make"@@ -366,12 +368,13 @@ assert_a_full_sync_in_ghcr_mode_repulls_instead_of_building_everything() {prepare_repo "$d"out="$(PATH="$d/bin:$PATH" TEST_LOG_FILE="$d/ops.log" \- IMAGE_SOURCE=ghcr IMAGE_TAG="$PIN_TAG" \+ IMAGE_SOURCE=ghcr IMAGE_TAG="$PIN_TAG" T2_LOCK_TOKEN=test-token \run_incremental "$d" 'INCREMENTAL_REPULL_ALL=true; INCREMENTAL_TARGETS=("control-api|control-plane|control-api"); incremental_build_images')"rc=$?if [ "$rc" -eq 0 ] \&& grep -Fq "make minikube-pull-images" "$d/ops.log" \+ && grep -Fq 'make minikube-pull-images T2_SKIP_LOCK=true T2_LOCK_TOKEN_SET=true' "$d/ops.log" \&& ! grep -Fq "make minikube-build-images" "$d/ops.log" \&& grep -Fq "docker tag $(local_ref_for_image control-api) ghcr.io/evenfire-ai/control-api:${PIN_TAG}" "$d/ops.log"; thenpass "a ghcr full sync re-pulls the release set, then shadows the changed set on top"@@ -388,12 +391,13 @@ assert_local_mode_still_builds_everything_on_a_full_image_build() {prepare_repo "$d"out="$(PATH="$d/bin:$PATH" TEST_LOG_FILE="$d/ops.log" \- IMAGE_SOURCE=local IMAGE_TAG="" \+ IMAGE_SOURCE=local IMAGE_TAG="" T2_LOCK_TOKEN=test-token \run_incremental "$d" 'INCREMENTAL_FULL_IMAGE_BUILD=true; incremental_build_images')"rc=$?if [ "$rc" -eq 0 ] \&& grep -Fq "make minikube-build-images" "$d/ops.log" \+ && grep -Fq 'make minikube-build-images T2_SKIP_LOCK=true T2_LOCK_TOKEN_SET=true' "$d/ops.log" \&& ! grep -Fq "make minikube-pull-images" "$d/ops.log"; thenpass "local mode still builds every image when the change cannot be targeted"else