Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop): classify upstream errors and keep deleted chats deleted
Alfredo Lopez2c6befe9be11
diff --git a/desktop-app/src/__tests__/appService.chatStoreTeamHop.test.ts b/desktop-app/src/__tests__/appService.chatStoreTeamHop.test.tsnew file mode 100644index 000000000..972519648--- /dev/null+++ b/desktop-app/src/__tests__/appService.chatStoreTeamHop.test.ts@@ -0,0 +1,120 @@+/**+ * A transient `runWithTeamContext` hop installs another team's token (and `me`)+ * for the duration of one operation and then restores the original team. The+ * chat store scope and the delete-fence authority must NOT follow that hop: a+ * confirmed deletion is bound to the store's scope, and a store rebound to the+ * hop team would accept a late append to a chat the user already deleted.+ */+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'+import fs from 'node:fs/promises'+import os from 'node:os'+import path from 'node:path'+import { AppService } from '../appService.js'+import {+ __setChatStoreBaseDirForTests,+ requireChatStore,+ unbindChatStore,+} from '../chatStoreBinding.js'+import type { ChatMessage } from '../types.js'++let base: string++beforeEach(async () => {+ base = await fs.mkdtemp(path.join(os.tmpdir(), 'chat-store-team-hop-'))+ __setChatStoreBaseDirForTests(base)+})++afterEach(async () => {+ unbindChatStore()+ __setChatStoreBaseDirForTests(null)+ await fs.rm(base, { recursive: true, force: true })+})++const member = (teamId: string) => ({+ id: 'user-1',+ email: '[email protected]',+ name: 'Test User',+ picture: null,+ teamId,+ teamName: teamId,+ role: 'member',+})++/** An AppService signed in on team-a whose auth client answers team switches. */+function signedInService(getMeAnswers: string[]): AppService {+ const service = new AppService() as unknown as Record<string, unknown>+ service.sessionToken = 'team-a-token'+ service.me = member('team-a')+ const getMe = vi.fn()+ for (const teamId of getMeAnswers) getMe.mockResolvedValueOnce(member(teamId))+ service.authClient = {+ getMe,+ switchTeam: vi.fn(async (_token: string, teamId: string) => ({+ token: `${teamId}-token`,+ team: { id: teamId, name: teamId, role: 'member' },+ })),+ }+ service.tokenStore = { setSessionToken: vi.fn() }+ return service as unknown as AppService+}++const lateMessage = (): ChatMessage => ({+ id: 'late',+ role: 'user',+ content: 'late append',+ timestamp: Date.now(),+})++describe('chat store scope across a transient team hop', () => {+ it('keeps the store scope and the delete authority on the home team for the whole hop', async () => {+ const service = signedInService(['team-b', 'team-a'])+ const internals = service as unknown as {+ bindCurrentChatStore(userId: string): Promise<void>+ runWithTeamContext<T>(teamId: string, op: (token: string) => Promise<T>): Promise<T>+ me: { teamId: string }+ }+ await internals.bindCurrentChatStore('user-1')+ const store = requireChatStore()+ const fence = service.getChatDeletionFenceAuthority()+ await store.createChat('agent-1', 'c1')+ await store.deleteChat('agent-1', 'c1', fence.authorityScope)+ // Liveness witness: the deletion took effect in team-a.+ expect(await store.loadMessages('agent-1', 'c1')).toEqual([])++ const during: { sessionTeam?: string; store?: unknown; fence?: unknown; loaded?: string[] } = {}+ await internals.runWithTeamContext('team-b', async () => {+ during.sessionTeam = internals.me.teamId+ during.store = store.getAuthorityScope()+ during.fence = service.getChatDeletionFenceAuthority().authorityScope+ await store.appendMessages('agent-1', 'c1', [lateMessage()])+ during.loaded = (await store.loadMessages('agent-1', 'c1')).map(m => m.id)+ })++ // Liveness witness: the session really was on team-b during the operation.+ expect(during.sessionTeam).toBe('team-b')+ expect(during.store).toMatchObject({ userId: 'user-1', teamId: 'team-a' })+ expect(during.fence).toMatchObject({ userId: 'user-1', teamId: 'team-a' })+ // The late append to the deleted chat was rejected, not stored.+ expect(during.loaded).toEqual([])+ // Restored: same team, and the chat is still gone.+ expect(internals.me.teamId).toBe('team-a')+ expect(store.getAuthorityScope()?.teamId).toBe('team-a')+ expect(service.getChatDeletionFenceAuthority().authorityScope.teamId).toBe('team-a')+ expect(await store.loadMessages('agent-1', 'c1')).toEqual([])+ })++ it('still rebinds the store when the team changes without a restoring hop', async () => {+ const service = signedInService(['team-b'])+ const internals = service as unknown as {+ bindCurrentChatStore(userId: string): Promise<void>+ switchSessionToTeam(teamId: string): Promise<string>+ }+ await internals.bindCurrentChatStore('user-1')+ expect(requireChatStore().getAuthorityScope()?.teamId).toBe('team-a')++ await internals.switchSessionToTeam('team-b')++ expect(requireChatStore().getAuthorityScope()?.teamId).toBe('team-b')+ expect(service.getChatDeletionFenceAuthority().authorityScope.teamId).toBe('team-b')+ })+})diff --git a/desktop-app/src/__tests__/chatStore.test.ts b/desktop-app/src/__tests__/chatStore.test.tsindex 870ff8b14..c9a960279 100644--- a/desktop-app/src/__tests__/chatStore.test.ts+++ b/desktop-app/src/__tests__/chatStore.test.ts@@ -257,18 +257,49 @@ describe('deleteChat', () => {expect(lastActive).toBeNull()})- it('keeps new tombstones scoped to the full authority identity', async () => {- await store.createChat('agent-1', 'team-owned-chat')- await store.deleteChat('agent-1', 'team-owned-chat', TEAM_A_SCOPE)+ it('keeps a confirmed deletion across a session-team rebind for the same user and Host', async () => {+ const agentRef = 'agent-1'+ const chatId = 'team-owned-chat'+ const message: ChatMessage = { id: 'late', role: 'user', content: 'late', timestamp: 1 }+ await store.createChat(agentRef, chatId)+ await store.saveMessages(agentRef, chatId, [+ { id: 'original', role: 'user', content: 'original', timestamp: 0 },+ ])+ // Liveness witness: the chat existed on disk before the delete.+ await expect(fs.access(chatCacheDir(chatId))).resolves.toBeUndefined()+ await store.deleteChat(agentRef, chatId, TEAM_A_SCOPE)+ await expect(store.createChat(agentRef, chatId)).rejects.toThrow('Chat was deleted locally')+ expect(await store.loadMessages(agentRef, chatId)).toEqual([])++ // The store scope is rebound from `me.teamId` on every team switch.+ store.setAuthorityScope(TEAM_B_SCOPE)++ await expect(store.createChat(agentRef, chatId)).rejects.toThrow('Chat was deleted locally')+ await store.appendMessages(agentRef, chatId, [message])+ await store.replaceMessages(agentRef, chatId, [message])+ await store.setLastActiveChatId(agentRef, chatId)+ expect(await store.loadMessages(agentRef, chatId)).toEqual([])+ expect(await store.getLastActiveChatId(agentRef)).toBeNull()+ expect(await store.listChats(agentRef)).toEqual([])+ await expect(fs.access(chatCacheDir(chatId))).rejects.toThrow()+ })++ it('binds a tombstone to environment and user, not to the session team', async () => {+ await store.createChat('agent-1', 'identity-chat')+ await store.deleteChat('agent-1', 'identity-chat', TEAM_A_SCOPE)- const teamBStore = new ChatStore(tempDir)- teamBStore.setAuthorityScope(TEAM_B_SCOPE)- await expect(teamBStore.createChat('agent-1', 'team-owned-chat')).resolves.toMatchObject({- id: 'team-owned-chat',+ // The persisted tombstone keeps its full authority scope (format unchanged).+ expect((await store.getIndex('agent-1')).deletedChatTombstones).toContainEqual({+ chatId: 'identity-chat',+ authorityScope: TEAM_A_SCOPE,+ })+ // Same store directory but a different user identity is a different Host+ // identity: the deletion does not apply to it.+ const otherUser = new ChatStore(tempDir)+ otherUser.setAuthorityScope({ ...TEAM_A_SCOPE, userId: 'user-b' })+ await expect(otherUser.createChat('agent-1', 'identity-chat')).resolves.toMatchObject({+ id: 'identity-chat',})- await expect(store.createChat('agent-1', 'team-owned-chat')).rejects.toThrow(- 'Chat was deleted locally'- )})it('keeps cleanup queued after a failure and retries it only in the owning scope', async () => {@@ -288,7 +319,9 @@ describe('deleteChat', () => {})await expect(fs.access(chatCacheDir('retry-cleanup'))).resolves.toBeUndefined()- store.setAuthorityScope(TEAM_B_SCOPE)+ // A caller holding the previous user's scope is stale once the session user+ // changed: the retry refuses instead of running for the wrong identity.+ store.setAuthorityScope({ ...TEAM_A_SCOPE, userId: 'user-b' })await expect(store.retryPendingDeleteCleanups(TEAM_A_SCOPE)).rejects.toThrow('Chat deletion authority scope changed')@@ -299,6 +332,255 @@ describe('deleteChat', () => {expect((await store.getIndex('agent-1')).pendingChatCleanup).toEqual([])await expect(fs.access(chatCacheDir('retry-cleanup'))).rejects.toThrow()})++ it('retries a cleanup left pending in team A while the session is in team B (same environment and user)', async () => {+ await store.createChat('agent-1', 'team-switch-cleanup')+ await store.saveMessages('agent-1', 'team-switch-cleanup', [+ { id: 'message', role: 'user', content: 'private', timestamp: 1 },+ ])+ vi.spyOn(fs, 'readdir').mockRejectedValueOnce(+ Object.assign(new Error('permission denied'), { code: 'EACCES' })+ )+ vi.spyOn(console, 'warn').mockImplementation(() => undefined)+ await expect(store.deleteChat('agent-1', 'team-switch-cleanup', TEAM_A_SCOPE)).resolves.toEqual(+ { cleanupPending: true }+ )+ // Liveness witness: the entry is really queued and the artifacts really exist.+ expect((await store.getIndex('agent-1')).pendingChatCleanup).toEqual([+ { chatId: 'team-switch-cleanup', authorityScope: TEAM_A_SCOPE },+ ])+ await expect(fs.access(chatCacheDir('team-switch-cleanup'))).resolves.toBeUndefined()++ store.setAuthorityScope(TEAM_B_SCOPE)+ await store.retryPendingDeleteCleanups(TEAM_B_SCOPE)++ expect((await store.getIndex('agent-1')).pendingChatCleanup).toEqual([])+ await expect(fs.access(chatCacheDir('team-switch-cleanup'))).rejects.toThrow()+ // The tombstone keeps holding in the new team: the chat is not resurrected.+ expect((await store.getIndex('agent-1')).deletedChatTombstones).toContainEqual({+ chatId: 'team-switch-cleanup',+ authorityScope: TEAM_A_SCOPE,+ })+ })++ it.each([+ ['a different user', { ...TEAM_A_SCOPE, userId: 'user-b' }],+ ['a different environment', { ...TEAM_A_SCOPE, environmentKey: 'env-b' }],+ ])('does not retry a cleanup pending for %s', async (_label, otherScope) => {+ vi.spyOn(console, 'warn').mockImplementation(() => undefined)+ const readdir = vi.spyOn(fs, 'readdir')+ // agent-1: a cleanup left pending by the original identity.+ await store.createChat('agent-1', 'foreign-cleanup')+ await store.saveMessages('agent-1', 'foreign-cleanup', [+ { id: 'message', role: 'user', content: 'private', timestamp: 1 },+ ])+ readdir.mockRejectedValueOnce(Object.assign(new Error('denied'), { code: 'EACCES' }))+ await store.deleteChat('agent-1', 'foreign-cleanup', TEAM_A_SCOPE)+ // agent-2: a cleanup left pending by the other identity, which the retry must process.+ store.setAuthorityScope(otherScope)+ await store.createChat('agent-2', 'own-cleanup')+ await store.saveMessages('agent-2', 'own-cleanup', [+ { id: 'message', role: 'user', content: 'mine', timestamp: 1 },+ ])+ readdir.mockRejectedValueOnce(Object.assign(new Error('denied'), { code: 'EACCES' }))+ await store.deleteChat('agent-2', 'own-cleanup', otherScope)+ expect((await store.getIndex('agent-2')).pendingChatCleanup).toHaveLength(1)++ await store.retryPendingDeleteCleanups(otherScope)++ // Liveness witness: the retry ran and finished the entry that belongs to this identity.+ expect((await store.getIndex('agent-2')).pendingChatCleanup).toEqual([])+ await expect(fs.access(join(tempDir, 'agent-2', 'chats', 'own-cleanup'))).rejects.toThrow()+ // The other identity's entry was left alone: still queued, artifacts still on disk.+ expect((await store.getIndex('agent-1')).pendingChatCleanup).toEqual([+ { chatId: 'foreign-cleanup', authorityScope: TEAM_A_SCOPE },+ ])+ await expect(fs.access(chatCacheDir('foreign-cleanup'))).resolves.toBeUndefined()+ })+})++// ── tombstone durability ─────────────────────────────────────────────────────++describe('tombstone durability', () => {+ const tombstone = (chatId: string) => ({ chatId, authorityScope: TEAM_A_SCOPE })++ it('drops only a malformed tombstone entry and keeps the catalog and other tombstones', async () => {+ await store.createChat('agent-1', 'kept-chat')+ await store.createChat('agent-1', 'gone-1')+ await store.deleteChat('agent-1', 'gone-1', TEAM_A_SCOPE)+ const index = await readJsonFile<Record<string, unknown>>(agentPath('index.json'))+ index.deletedChatTombstones = [+ { chatId: 'malformed-entry', authorityScope: { environmentKey: '', userId: 7 } },+ ...(index.deletedChatTombstones as unknown[]),+ ]+ await fs.writeFile(agentPath('index.json'), JSON.stringify(index))+ // The sidecar must not paper over the malformed entry in this scenario.+ await fs.rm(agentPath('.tombstones.json'), { force: true })+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)++ const restarted = new ChatStore(tempDir)+ restarted.setAuthorityScope(TEAM_A_SCOPE)+ const first = await restarted.getIndex('agent-1')+ await restarted.getIndex('agent-1')++ expect(first.chats.map(chat => chat.id)).toEqual(['kept-chat'])+ expect(first.deletedChatTombstones).toEqual([tombstone('gone-1')])+ // Not quarantined: the catalog stays in place.+ await expect(fs.access(agentPath('index.json'))).resolves.toBeUndefined()+ await expect(fs.access(agentPath('.corrupt'))).rejects.toThrow()+ // Reported once, not once per read.+ const dropped = warn.mock.calls.filter(call => String(call[0]).includes('malformed tombstone'))+ expect(dropped).toHaveLength(1)+ await expect(restarted.createChat('agent-1', 'gone-1')).rejects.toThrow(+ 'Chat was deleted locally'+ )+ })++ it('restores tombstones from the sidecar when a corrupt catalog is quarantined', async () => {+ await store.createChat('agent-1', 'kept-chat')+ await store.createChat('agent-1', 'deleted-chat')+ await store.saveMessages('agent-1', 'deleted-chat', [+ { id: 'm1', role: 'user', content: 'private', timestamp: 1 },+ ])+ // Cleanup fails, so the chat stays in pendingChatCleanup as well.+ vi.spyOn(fs, 'rm').mockRejectedValue(Object.assign(new Error('denied'), { code: 'EACCES' }))+ vi.spyOn(console, 'warn').mockImplementation(() => undefined)+ await expect(store.deleteChat('agent-1', 'deleted-chat', TEAM_A_SCOPE)).resolves.toEqual({+ cleanupPending: true,+ })+ vi.restoreAllMocks()+ vi.spyOn(console, 'warn').mockImplementation(() => undefined)+ // Liveness witness: the catalog held the tombstone before it was corrupted.+ expect((await store.getIndex('agent-1')).deletedChatTombstones).toEqual([+ tombstone('deleted-chat'),+ ])++ await fs.writeFile(agentPath('index.json'), '{"version":2,"chats":[')++ const restarted = new ChatStore(tempDir)+ restarted.setAuthorityScope(TEAM_A_SCOPE)+ const recovered = await restarted.getIndex('agent-1')+ expect(recovered.chats).toEqual([])+ expect(recovered.deletedChatTombstones).toEqual([tombstone('deleted-chat')])+ expect(recovered.pendingChatCleanup).toEqual([tombstone('deleted-chat')])+ // The corrupt bytes stay recoverable in quarantine.+ const quarantined = await fs.readdir(agentPath('.corrupt'))+ expect(quarantined).toHaveLength(1)+ expect(await fs.readFile(agentPath('.corrupt', quarantined[0]!), 'utf-8')).toBe(+ '{"version":2,"chats":['+ )+ // The catalog is gone from its writable path, yet the deletion still holds.+ await expect(fs.access(agentPath('index.json'))).rejects.toThrow()+ expect((await restarted.getIndex('agent-1')).deletedChatTombstones).toEqual([+ tombstone('deleted-chat'),+ ])+ await expect(restarted.createChat('agent-1', 'deleted-chat')).rejects.toThrow(+ 'Chat was deleted locally'+ )+ await expect(restarted.createChat('agent-1', 'kept-chat')).resolves.toMatchObject({+ id: 'kept-chat',+ })+ })++ it('does not write a tombstone sidecar for an agent that never deleted a chat', async () => {+ await store.createChat('agent-1', 'never-deleted')+ expect((await store.listChats('agent-1')).map(chat => chat.id)).toEqual(['never-deleted'])+ await expect(fs.access(agentPath('.tombstones.json'))).rejects.toThrow()+ })++ it('reports an unreadable sidecar once and returns the empty catalog', async () => {+ await store.createChat('agent-1', 'c')+ await store.deleteChat('agent-1', 'c', TEAM_A_SCOPE)+ await fs.writeFile(agentPath('index.json'), 'not json')+ await fs.writeFile(agentPath('.tombstones.json'), 'not json either')+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)++ const restarted = new ChatStore(tempDir)+ restarted.setAuthorityScope(TEAM_A_SCOPE)+ expect((await restarted.getIndex('agent-1')).deletedChatTombstones).toBeUndefined()+ await restarted.getIndex('agent-1')++ const unreadable = warn.mock.calls.filter(call => String(call[0]).includes('tombstone sidecar'))+ expect(unreadable).toHaveLength(1)+ })+})++describe('deleted-chat cleanup failure reporting', () => {+ it('reports a permanent cleanup failure once per agent, chat and code', async () => {+ await store.createChat('agent-1', 'stuck-chat')+ await store.saveMessages('agent-1', 'stuck-chat', [+ { id: 'm1', role: 'user', content: 'x', timestamp: 1 },+ ])+ await store.createChat('agent-2', 'clean-chat')+ await store.saveMessages('agent-2', 'clean-chat', [+ { id: 'm1', role: 'user', content: 'x', timestamp: 1 },+ ])+ const realRm = fs.rm.bind(fs)+ // Only agent-1's directory is unwritable; agent-2 cleans normally.+ vi.spyOn(fs, 'rm').mockImplementation(async (path, options) => {+ if (String(path).includes(join('agent-1'))) {+ throw Object.assign(new Error('permission denied'), { code: 'EACCES' })+ }+ return realRm(path, options)+ })+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)++ await expect(store.deleteChat('agent-1', 'stuck-chat', TEAM_A_SCOPE)).resolves.toEqual({+ cleanupPending: true,+ })+ await expect(store.deleteChat('agent-2', 'clean-chat', TEAM_A_SCOPE)).resolves.toEqual({+ cleanupPending: false,+ })++ await store.retryPendingDeleteCleanups(TEAM_A_SCOPE)+ await store.retryPendingDeleteCleanups(TEAM_A_SCOPE)++ const stuckReports = warn.mock.calls.filter(+ call =>+ String(call[0]).includes('Deleted-chat cleanup failed') &&+ String(call[0]).includes('"agent-1"') &&+ String(call[0]).includes('"stuck-chat"') &&+ String(call[0]).includes('EACCES')+ )+ expect(stuckReports).toHaveLength(1)+ // The failing agent stays queued; the healthy one finished on its own.+ expect((await store.getIndex('agent-1')).pendingChatCleanup).toEqual([+ { chatId: 'stuck-chat', authorityScope: TEAM_A_SCOPE },+ ])+ expect((await store.getIndex('agent-2')).pendingChatCleanup).toEqual([])+ })++ it('reports an unreadable agent catalog during retry and still cleans the other agents', async () => {+ await store.createChat('agent-2', 'clean-chat')+ await store.saveMessages('agent-2', 'clean-chat', [+ { id: 'm1', role: 'user', content: 'x', timestamp: 1 },+ ])+ const realRm = fs.rm.bind(fs)+ vi.spyOn(console, 'warn').mockImplementation(() => undefined)+ const rm = vi+ .spyOn(fs, 'rm')+ .mockRejectedValue(Object.assign(new Error('permission denied'), { code: 'EACCES' }))+ await store.deleteChat('agent-2', 'clean-chat', TEAM_A_SCOPE)+ rm.mockImplementation((path, options) => realRm(path, options))+ // Sorts before agent-2, so the retry hits it first.+ await fs.mkdir(join(tempDir, 'agent-0-broken'), { recursive: true })+ await fs.writeFile(+ join(tempDir, 'agent-0-broken', 'index.json'),+ JSON.stringify({ version: 99, chats: [] })+ )+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)++ await store.retryPendingDeleteCleanups(TEAM_A_SCOPE)+ await store.retryPendingDeleteCleanups(TEAM_A_SCOPE)++ const brokenReports = warn.mock.calls.filter(+ call =>+ String(call[0]).includes('Deleted-chat cleanup failed') &&+ String(call[0]).includes('"agent-0-broken"')+ )+ expect(brokenReports).toHaveLength(1)+ expect((await store.getIndex('agent-2')).pendingChatCleanup).toEqual([])+ })})// ── messages ─────────────────────────────────────────────────────────────────diff --git a/desktop-app/src/__tests__/httpClient.upstreamErrors.test.ts b/desktop-app/src/__tests__/httpClient.upstreamErrors.test.tsnew file mode 100644index 000000000..58a02362e--- /dev/null+++ b/desktop-app/src/__tests__/httpClient.upstreamErrors.test.ts@@ -0,0 +1,183 @@+import { afterEach, describe, expect, it, vi } from 'vitest'+import { ApiError, requestJson } from '../httpClient.js'+import { RpcProxyClient } from '../rpcProxyClient.js'+import {+ ERROR_EXCERPT_MAX_CHARS,+ HOST_ACCESS_DENIED_MESSAGE,+ HOST_ACCESS_REVOKED_MESSAGE,+ boundedErrorExcerpt,+ hostAccessDenialMessage,+} from '../upstreamErrors.js'++vi.mock('../config.js', () => ({+ config: {+ rpcProxyBaseUrl: 'http://proxy',+ externalRestApiBaseUrl: 'http://rest',+ enableDevLoginUi: false,+ requestTimeoutMs: 60000,+ appName: 'test',+ },+}))++afterEach(() => {+ vi.unstubAllGlobals()+})++function stubResponse(status: number, statusText: string, body: string, headers = {}) {+ const fetchMock = vi.fn(async () => new Response(body, { status, statusText, headers }))+ vi.stubGlobal('fetch', fetchMock)+ return fetchMock+}++async function rejection(promise: Promise<unknown>): Promise<ApiError> {+ const error = await promise.then(+ () => null,+ (e: unknown) => e+ )+ expect(error).toBeInstanceOf(ApiError)+ return error as ApiError+}++// The rpc-proxy own-denial body, exactly as the cross-layer contract defines it.+const denialBody = (code?: string) =>+ JSON.stringify({ error: 'Forbidden: user cannot access this host', ...(code ? { code } : {}) })++describe('requestJson Host-access 403 mapping', () => {+ it('maps code host_access_revoked to the single confirmed-revocation message', async () => {+ const fetchMock = stubResponse(403, 'Forbidden', denialBody('host_access_revoked'), {+ 'retry-after': '7',+ })++ const error = await rejection(requestJson('GET', 'http://proxy/x'))++ expect(fetchMock).toHaveBeenCalledTimes(1)+ expect(error.message).toBe(HOST_ACCESS_REVOKED_MESSAGE)+ expect(error.message).toBe('403 Forbidden: host_access_revoked')+ expect(error.status).toBe(403)+ expect(error.bodyText).toBe(denialBody('host_access_revoked'))+ expect(error.retryAfter).toBe('7')+ })++ it('maps code host_access_denied to a message that is not a confirmed revocation', async () => {+ stubResponse(403, 'Forbidden', denialBody('host_access_denied'))++ const error = await rejection(requestJson('GET', 'http://proxy/x'))++ expect(error.message).toBe(HOST_ACCESS_DENIED_MESSAGE)+ expect(error.message).toBe('403 Forbidden: host_access_denied')+ expect(error.message).not.toContain('host_access_revoked')+ })++ it.each([+ [+ 'a JSON body whose error is the revoked token but has no code',+ '{"error":"host_access_revoked"}',+ ],+ ['the pre-contract denial body without a code', denialBody()],+ ['a text body ending in the revoked token', 'Forbidden: host_access_revoked'],+ ['a JSON message field carrying the revoked token', '{"message":"host_access_revoked"}'],+ ['a code of another kind', '{"code":"missing_scope","error":"host_access_revoked"}'],+ ])('never forges a confirmed revocation from %s', async (_label, body) => {+ const fetchMock = stubResponse(403, 'Forbidden', body)++ const error = await rejection(requestJson('GET', 'http://proxy/x'))++ // Liveness witness: the 403 path ran and produced the ordinary status text.+ expect(fetchMock).toHaveBeenCalledTimes(1)+ expect(error.message.startsWith('403 Forbidden: ')).toBe(true)+ expect(error.status).toBe(403)+ expect(error.message).not.toContain('host_access_revoked')+ })++ it('leaves a non-403 body that mentions the token as an ordinary failure', async () => {+ stubResponse(502, 'Bad Gateway', '{"code":"host_access_revoked"}')++ const error = await rejection(requestJson('GET', 'http://proxy/x'))++ expect(error.message.startsWith('502 Bad Gateway: ')).toBe(true)+ expect(error.message).not.toContain('host_access_revoked')+ })+})++describe('bounded upstream error excerpts', () => {+ const HUGE = 'y'.repeat(100 * 1024)++ it('keeps a 100 KB body out of the requestJson message but on bodyText, status first', async () => {+ const body = JSON.stringify({ detail: HUGE })+ stubResponse(500, 'Internal Server Error', body)++ const error = await rejection(requestJson('GET', 'http://proxy/x'))++ expect(error.message.startsWith('500 Internal Server Error: ')).toBe(true)+ expect(error.message.length).toBeLessThan(600)+ expect(error.bodyText).toBe(body)+ expect(error.status).toBe(500)+ expect(/^(\d{3}) /.exec(error.message)?.[1]).toBe('500')+ })++ it('bounds an oversized error string and message field of a JSON body', async () => {+ stubResponse(400, 'Bad Request', JSON.stringify({ error: HUGE, message: HUGE }))++ const error = await rejection(requestJson('GET', 'http://proxy/x'))++ expect(error.message.startsWith('400 Bad Request: ')).toBe(true)+ expect(error.message.length).toBeLessThan(600)+ })++ it.each([+ ['prewarmHost', (c: RpcProxyClient) => c.prewarmHost('t', 'host'), 'Prewarm failed (500): '],+ [+ 'approveToolCall',+ (c: RpcProxyClient) => c.approveToolCall('t', 'host', 'task', 'tool'),+ 'Approve failed (500): ',+ ],+ [+ 'listArtifacts',+ (c: RpcProxyClient) => c.listArtifacts('t', 'host'),+ 'List artifacts failed (500): ',+ ],+ [+ 'getHostModels',+ (c: RpcProxyClient) => c.getHostModels('t', 'host', 'chat'),+ 'Get host models failed (500): ',+ ],+ [+ 'setHostModel',+ (c: RpcProxyClient) => c.setHostModel('t', 'host', 'chat', 'model'),+ 'Set host model failed (500): ',+ ],+ ])('bounds the raw-fetch message of %s', async (_name, call, prefix) => {+ const fetchMock = stubResponse(500, 'Internal Server Error', HUGE)++ const error = await rejection(call(new RpcProxyClient()))++ expect(fetchMock).toHaveBeenCalledTimes(1)+ expect(error.message.startsWith(prefix)).toBe(true)+ expect(error.message.length).toBeLessThan(600)+ expect(error.bodyText).toBe(HUGE)+ expect(error.status).toBe(500)+ })+})++describe('upstreamErrors helpers', () => {+ it('collapses whitespace, cuts at the bound with an ellipsis and defuses reserved tokens', () => {+ expect(boundedErrorExcerpt(' a\n\n b\t c ')).toBe('a b c')+ const cut = boundedErrorExcerpt('z'.repeat(ERROR_EXCERPT_MAX_CHARS + 50))+ expect(cut).toBe(`${'z'.repeat(ERROR_EXCERPT_MAX_CHARS)}…`)+ const defused = boundedErrorExcerpt('a host_access_revoked and HOST_ACCESS_DENIED here')+ expect(defused).toBe('a host-access-revoked and host-access-DENIED here')+ })++ it('decides Host-access messages from the parsed code of a 403 only', () => {+ expect(hostAccessDenialMessage(403, '{"code":"host_access_revoked"}')).toBe(+ HOST_ACCESS_REVOKED_MESSAGE+ )+ expect(hostAccessDenialMessage(403, '{"code":"host_access_denied"}')).toBe(+ HOST_ACCESS_DENIED_MESSAGE+ )+ expect(hostAccessDenialMessage(401, '{"code":"host_access_revoked"}')).toBeNull()+ expect(hostAccessDenialMessage(403, 'host_access_revoked')).toBeNull()+ expect(hostAccessDenialMessage(403, '{"error":"host_access_revoked"}')).toBeNull()+ expect(hostAccessDenialMessage(403, '')).toBeNull()+ })+})diff --git a/desktop-app/src/__tests__/ipc.chatGetIndex.test.ts b/desktop-app/src/__tests__/ipc.chatGetIndex.test.tsnew file mode 100644index 000000000..48cfe2523--- /dev/null+++ b/desktop-app/src/__tests__/ipc.chatGetIndex.test.ts@@ -0,0 +1,148 @@+/**+ * `chat:getIndex` must keep returning the index WITH its tombstones when the+ * deleted-chat cleanup retry that precedes it fails (scope changed mid-flight,+ * no session, unreadable chats directory). The renderer filters deleted chats+ * with those tombstones; a rejected read leaves them visible.+ */+import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from 'vitest'+import { promises as fs } from 'node:fs'+import { tmpdir } from 'node:os'+import { join } from 'node:path'+import type { AppService } from '../appService.js'+import type { ChatIndex } from '../types.js'++type Handler = (event: unknown, payload: unknown) => Promise<unknown>++const electron = vi.hoisted(() => ({ handlers: new Map<string, Handler>() }))++vi.mock('electron', () => ({+ BrowserWindow: { fromWebContents: vi.fn(), getAllWindows: vi.fn(() => []) },+ Notification: vi.fn(),+ app: { on: vi.fn(), getPath: vi.fn(() => '/nonexistent'), getVersion: vi.fn(), isPackaged: true },+ clipboard: { writeText: vi.fn() },+ dialog: { showSaveDialog: vi.fn() },+ ipcMain: {+ handle: vi.fn((channel: string, handler: Handler) => {+ electron.handlers.set(channel, handler)+ }),+ on: vi.fn(),+ removeHandler: vi.fn(),+ },+}))+// ipc.ts only uses AppService as a type at registration.+vi.mock('../appService.js', () => ({ AppService: class {} }))++const TEAM_A = { environmentKey: 'env-a', userId: 'user-a', teamId: 'team-a' }+const TEAM_B = { ...TEAM_A, teamId: 'team-b' }+const OTHER_USER = { ...TEAM_A, userId: 'user-b' }++let authority: { authorityScope: typeof TEAM_A; sessionGeneration: number } | Error = {+ authorityScope: TEAM_A,+ sessionGeneration: 1,+}+const service = {+ getChatDeletionFenceAuthority: vi.fn(() => {+ if (authority instanceof Error) throw authority+ return authority+ }),+}+const trusted = { senderFrame: { url: 'file:///app/index.html' } }+const call = (channel: string, payload: unknown) => {+ const handler = electron.handlers.get(channel)+ if (!handler) throw new Error(`no IPC handler registered for ${channel}`)+ return handler(trusted, payload)+}+let base: string++beforeAll(async () => {+ base = await fs.mkdtemp(join(tmpdir(), 'ipc-chat-getindex-'))+ const binding = await import('../chatStoreBinding.js')+ binding.__setChatStoreBaseDirForTests(base)+ await binding.bindChatStoreForUser('user-a', 'env-a', { teamId: 'team-a' })+ const { registerIpcHandlers } = await import('../ipc.js')+ registerIpcHandlers(service as unknown as AppService)+ await call('chat:create', { agentRef: 'agent-1', chatId: 'deleted-chat' })+ const fence = await call('chat:captureDeleteFence', { expectedAuthorityScope: TEAM_A })+ await call('chat:delete', { version: 3, fence, agentRef: 'agent-1', chatId: 'deleted-chat' })+})++afterEach(() => {+ authority = { authorityScope: TEAM_A, sessionGeneration: 1 }+ vi.restoreAllMocks()+})++afterAll(async () => {+ const binding = await import('../chatStoreBinding.js')+ binding.__setChatStoreBaseDirForTests(null)+ binding.unbindChatStore()+ await fs.rm(base, { recursive: true, force: true })+})++const tombstones = (index: unknown) =>+ (index as ChatIndex).deletedChatTombstones?.map(item => item.chatId)++describe('chat:getIndex', () => {+ it('returns the tombstones when the cleanup retry runs normally', async () => {+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)++ const index = await call('chat:getIndex', { agentRef: 'agent-1' })++ expect(tombstones(index)).toEqual(['deleted-chat'])+ expect(service.getChatDeletionFenceAuthority).toHaveBeenCalled()+ expect(warn).not.toHaveBeenCalled()+ })++ it('still returns the tombstones when the session scope changed during the retry', async () => {+ // A transient identity change: the main service reports another user while+ // the store is still bound to user-a, so the retry rejects with a+ // scope-changed error. (A team hop alone no longer rejects: the retry is+ // bound to environment + user.)+ authority = { authorityScope: OTHER_USER, sessionGeneration: 2 }+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)++ const index = await call('chat:getIndex', { agentRef: 'agent-1' })++ // Liveness witness: the retry ran and failed, and the failure was reported.+ const report = warn.mock.calls.find(entry => String(entry[0]).includes('[chat:getIndex]'))+ expect(String(report?.[0])).toContain('agent "agent-1"')+ expect(String((report?.[1] as Error).message)).toContain('scope changed')+ expect(tombstones(index)).toEqual(['deleted-chat'])+ })++ it('runs the cleanup retry without a failure report after a team switch of the same user', async () => {+ authority = { authorityScope: TEAM_B, sessionGeneration: 2 }+ service.getChatDeletionFenceAuthority.mockClear()+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)++ const index = await call('chat:getIndex', { agentRef: 'agent-1' })++ // Liveness witness: the retry consulted the authority (it did run) and returned the index.+ expect(service.getChatDeletionFenceAuthority).toHaveBeenCalledTimes(1)+ expect(tombstones(index)).toEqual(['deleted-chat'])+ expect(warn).not.toHaveBeenCalled()+ })++ it('still returns the tombstones when there is no authenticated session', async () => {+ authority = new Error('Not authenticated')+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)++ const index = await call('chat:getIndex', { agentRef: 'agent-1' })++ const report = warn.mock.calls.find(entry => String(entry[0]).includes('[chat:getIndex]'))+ expect(String((report?.[1] as Error).message)).toBe('Not authenticated')+ expect(tombstones(index)).toEqual(['deleted-chat'])+ })++ it('still returns the tombstones and reports the code when the chats directory is unreadable', async () => {+ vi.spyOn(fs, 'readdir').mockRejectedValueOnce(+ Object.assign(new Error('permission denied'), { code: 'EACCES' })+ )+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)++ const index = await call('chat:getIndex', { agentRef: 'agent-1' })++ const report = warn.mock.calls.find(entry => String(entry[0]).includes('[chat:getIndex]'))+ expect(String(report?.[0])).toContain('(EACCES)')+ expect(tombstones(index)).toEqual(['deleted-chat'])+ })+})diff --git a/desktop-app/src/__tests__/rpcProxyClient.test.ts b/desktop-app/src/__tests__/rpcProxyClient.test.tsindex 88ffea848..05796eebf 100644--- a/desktop-app/src/__tests__/rpcProxyClient.test.ts+++ b/desktop-app/src/__tests__/rpcProxyClient.test.ts@@ -44,15 +44,20 @@ describe('RpcProxyClient.listSessions', () => {})- it('projects the exact Host access denial from catalog and transcript reads', async () => {- vi.stubGlobal(- 'fetch',- vi.fn().mockResolvedValue({- ok: false,- status: 403,- text: async () => JSON.stringify({ error: 'Forbidden: user cannot access this host' }),- })- )+ // The rpc-proxy denial bodies exactly as the contract defines them: status 403,+ // the human `error` text, and a machine `code` that says whether access was+ // actually removed (`host_access_revoked`) or merely refused (`host_access_denied`).+ const rpcProxyDenial = (code?: string) =>+ JSON.stringify({ error: 'Forbidden: user cannot access this host', ...(code ? { code } : {}) })++ const stubForbidden = (body: string) => {+ const fetchMock = vi.fn().mockResolvedValue({ ok: false, status: 403, text: async () => body })+ vi.stubGlobal('fetch', fetchMock)+ return fetchMock+ }++ it('projects a confirmed revocation (code host_access_revoked) from every raw-fetch site', async () => {+ const fetchMock = stubForbidden(rpcProxyDenial('host_access_revoked'))await expect(client.listSessions('token', 'host')).rejects.toThrow('403 Forbidden: host_access_revoked'@@ -60,6 +65,75 @@ describe('RpcProxyClient.listSessions', () => {await expect(client.loadSessionMessages('token', 'host', 'agent-a', 'chat-a')).rejects.toThrow('403 Forbidden: host_access_revoked')+ await expect(client.renameSession('token', 'host', 'agent-a', 'chat-a', 'T')).rejects.toThrow(+ '403 Forbidden: host_access_revoked'+ )+ expect(fetchMock).toHaveBeenCalledTimes(3)+ })++ it('projects an own-denial without revocation (code host_access_denied) as its own message', async () => {+ const fetchMock = stubForbidden(rpcProxyDenial('host_access_denied'))++ for (const call of [+ () => client.listSessions('token', 'host'),+ () => client.loadSessionMessages('token', 'host', 'agent-a', 'chat-a'),+ () => client.renameSession('token', 'host', 'agent-a', 'chat-a', 'T'),+ ]) {+ const error = await call().then(+ () => null,+ (e: unknown) => e as Error+ )+ expect(error?.message).toBe('403 Forbidden: host_access_denied')+ expect(error?.message).not.toContain('host_access_revoked')+ }+ expect(fetchMock).toHaveBeenCalledTimes(3)+ })++ it.each([+ ['the pre-contract body (error text only, no code)', rpcProxyDenial()],+ [+ 'a JSON body whose error is the revoked token but has no code',+ '{"error":"host_access_revoked"}',+ ],+ [+ 'a JSON body carrying the revoked token in a non-code field',+ '{"reason":"host_access_revoked"}',+ ],+ ['a text body ending in the revoked token', 'Forbidden: host_access_revoked'],+ ['a code of another kind', '{"code":"missing_scope","error":"host_access_revoked"}'],+ ])('does not forge a confirmed revocation from %s', async (_label, body) => {+ const fetchMock = stubForbidden(body)++ for (const call of [+ () => client.listSessions('token', 'host'),+ () => client.loadSessionMessages('token', 'host', 'agent-a', 'chat-a'),+ () => client.renameSession('token', 'host', 'agent-a', 'chat-a', 'T'),+ ]) {+ const error = await call().then(+ () => null,+ (e: unknown) => e as Error & { status?: number }+ )+ // Liveness witness: the failure path ran and kept the plain 403 text.+ expect(error?.message).toMatch(/failed \(403\)/)+ expect(error?.status).toBe(403)+ expect(error?.message).not.toContain('host_access_revoked')+ }+ expect(fetchMock).toHaveBeenCalledTimes(3)+ })++ it('bounds an oversized upstream body in the message while ApiError keeps it whole', async () => {+ const huge = `{"detail":"${'x'.repeat(100 * 1024)}"}`+ stubForbidden(huge)++ const error = (await client.listSessions('token', 'host').then(+ () => null,+ (e: unknown) => e+ )) as Error & { status?: number; bodyText?: string }+ expect(error).toBeInstanceOf(Error)+ // Status stays first so the renderer's status parsing keeps working.+ expect(error.message.startsWith('List sessions failed (403): ')).toBe(true)+ expect(error.message.length).toBeLessThan(600)+ expect(error.bodyText).toBe(huge)})it('keeps a generic catalog 403 uncertain', async () => {diff --git a/desktop-app/src/appService.ts b/desktop-app/src/appService.tsindex c8f045452..c6289e002 100644--- a/desktop-app/src/appService.ts+++ b/desktop-app/src/appService.ts@@ -872,6 +872,12 @@ export class AppService {// token in sessionToken. Existing GFS jobs keep their captured token and// scope, but new GFS operations must not capture that temporary token.private gfsTransientTeamHopDepth = 0+ // While a `runWithTeamContext` hop that will restore the original team is in+ // flight, `me.teamId` holds the borrowed team. The chat store (and the delete+ // fence derived from it) stays on the original team: rebinding it for the hop+ // and back would expose the deliberate team's chats to the borrowed scope for+ // the length of an RPC.+ private chatStoreHomeTeamId: string | null = null// A finite RPC may temporarily borrow another team session. Keep the// deliberate user-owned GFS scope stable across that hop so in-flight jobs// are not invalidated by the transient `me` value.@@ -1009,10 +1015,16 @@ export class AppService {}}+ /** The team the chat store and the delete fence are scoped to (see chatStoreHomeTeamId). */+ private chatStoreTeamId(): string | null {+ if (this.chatStoreHomeTeamId !== null) return this.chatStoreHomeTeamId+ return this.me?.teamId ?? null+ }+private async bindCurrentChatStore(userId: string): Promise<void> {await bindChatStoreForUser(userId, getActiveEnvKey(), {legacyEnvKeys: getActiveLegacyEnvKeys(),- teamId: this.me?.id === userId ? this.me.teamId : null,+ teamId: this.me?.id === userId ? this.chatStoreTeamId() : null,})}@@ -1026,7 +1038,7 @@ export class AppService {authorityScope: {environmentKey: getActiveEnvKey(),userId: this.me.id,- teamId: String(this.me.teamId || '').trim() || null,+ teamId: String(this.chatStoreTeamId() || '').trim() || null,},sessionGeneration: this.sessionGeneration,}@@ -1171,6 +1183,7 @@ export class AppService {const shouldSwitch = originalTeamId !== targetTeamIdconst shouldRestore = Boolean(originalTeamId && shouldSwitch)const releaseTransientHop = shouldSwitch ? this.enterGfsTransientTeamHop() : undefined+ if (shouldRestore) this.chatStoreHomeTeamId = originalTeamIdtry {if (shouldSwitch) {@@ -1196,6 +1209,7 @@ export class AppService {}}} finally {+ if (shouldRestore) this.chatStoreHomeTeamId = nullreleaseTransientHop?.()}} finally {diff --git a/desktop-app/src/authClient.ts b/desktop-app/src/authClient.tsindex 74901a6c3..ec20e13ed 100644--- a/desktop-app/src/authClient.ts+++ b/desktop-app/src/authClient.ts@@ -25,6 +25,7 @@ import {WorkflowRunCompletedNotification,WorkflowRunsResult,} from './types.js'+import { boundedErrorExcerpt } from './upstreamErrors.js'function url(path: string): string {return `${config.externalRestApiBaseUrl.replace(/\/+$/, '')}${path}`@@ -463,7 +464,7 @@ export class AuthClient {if (!response.ok) {const body = await readErrorBody(response)throw new ApiError(- `Notification stream failed (${response.status}): ${body || response.statusText}`,+ `Notification stream failed (${response.status}): ${boundedErrorExcerpt(body) || response.statusText}`,response.status,body)@@ -602,7 +603,7 @@ export class AuthClient {if (!response.ok) {const body = await readErrorBody(response)throw new ApiError(- `Download workflow artifact failed (${response.status}): ${body || response.statusText}`,+ `Download workflow artifact failed (${response.status}): ${boundedErrorExcerpt(body) || response.statusText}`,response.status,body)diff --git a/desktop-app/src/chatStore.ts b/desktop-app/src/chatStore.tsindex f7570a045..ce3ed2ba8 100644--- a/desktop-app/src/chatStore.ts+++ b/desktop-app/src/chatStore.ts@@ -98,6 +98,17 @@ function sameChatAuthorityScope(left: ChatAuthorityScope, right: ChatAuthoritySc)}+/**+ * The identity a local deletion is bound to: environment + user. The store is+ * rooted at `<env>/<user>/<agentRef>`, so together with the per-agent index this+ * is the Host/agent identity the chat belongs to. The session team is NOT part+ * of it: it is rebound from `me.teamId` on every team switch, and a deletion the+ * user confirmed for a Host's chat must keep holding after that switch.+ */+function sameChatDeletionIdentity(left: ChatAuthorityScope, right: ChatAuthorityScope): boolean {+ return left.environmentKey === right.environmentKey && left.userId === right.userId+}+function normalizePositiveInteger(value: number | undefined, fallback: number): number {if (value === undefined) return fallbackif (!Number.isFinite(value)) return fallback@@ -178,7 +189,13 @@ function samePageEntry(left: ChatPageEntry, right: ChatPageEntry): boolean {)}-function parseChatIndex(raw: string): ChatIndex {+interface ParsedChatIndex {+ index: ChatIndex+ /** Tombstone / pending-cleanup entries dropped because they were malformed. */+ droppedTombstoneEntries: number+}++function parseChatIndex(raw: string): ParsedChatIndex {const parsed = JSON.parse(raw) as unknownif (!parsed || typeof parsed !== 'object') {throw new Error('Invalid chat index')@@ -197,11 +214,8 @@ function parseChatIndex(raw: string): ChatIndex {typeof candidate.onboardingDismissed !== 'boolean' ||!Array.isArray(candidate.chats) ||(candidate.deletedChatTombstones !== undefined &&- (!Array.isArray(candidate.deletedChatTombstones) ||- !candidate.deletedChatTombstones.every(isChatDeleteTombstone))) ||- (candidate.pendingChatCleanup !== undefined &&- (!Array.isArray(candidate.pendingChatCleanup) ||- !candidate.pendingChatCleanup.every(isChatDeleteTombstone)))+ !Array.isArray(candidate.deletedChatTombstones)) ||+ (candidate.pendingChatCleanup !== undefined && !Array.isArray(candidate.pendingChatCleanup))) {throw new Error('Invalid chat index')}@@ -222,7 +236,62 @@ function parseChatIndex(raw: string): ChatIndex {throw new Error('Invalid chat index entry')}}- return { ...candidate, version: INDEX_VERSION } as unknown as ChatIndex+ // One malformed tombstone must not condemn the whole catalog (and with it every+ // other tombstone): drop only the malformed entries and report the count.+ let droppedTombstoneEntries = 0+ const keepValidTombstones = (+ entries: unknown[] | undefined+ ): ChatDeleteTombstone[] | undefined => {+ if (entries === undefined) return undefined+ const valid = entries.filter(isChatDeleteTombstone)+ droppedTombstoneEntries += entries.length - valid.length+ return valid+ }+ const deletedChatTombstones = keepValidTombstones(candidate.deletedChatTombstones as unknown[])+ const pendingChatCleanup = keepValidTombstones(candidate.pendingChatCleanup as unknown[])+ const index = { ...candidate, version: INDEX_VERSION } as unknown as ChatIndex+ if (deletedChatTombstones) index.deletedChatTombstones = deletedChatTombstones+ if (pendingChatCleanup) index.pendingChatCleanup = pendingChatCleanup+ return { index, droppedTombstoneEntries }+}++/**+ * Durable copy of the tombstone and pending-cleanup lists, written next to+ * `index.json`. A corrupt catalog is quarantined and replaced by an empty one;+ * without this copy every confirmed local deletion of that agent would be+ * forgotten and the deleted chats could be recreated from the server.+ */+const TOMBSTONE_SIDECAR_VERSION = 1++interface TombstoneSidecar {+ version: typeof TOMBSTONE_SIDECAR_VERSION+ deletedChatTombstones: ChatDeleteTombstone[]+ pendingChatCleanup: ChatDeleteTombstone[]+}++/**+ * `deletedChatTombstones` are intentionally unbounded and never pruned: a+ * deleted chat is never reopened, and the tombstone is what keeps a late write+ * (a server session sync, a delayed append) from resurrecting it. An entry costs+ * ~209 bytes (measured), so the growth is negligible. Do not add a cap or an+ * expiry: evicting a tombstone would let exactly that late write bring the+ * deleted chat back. Only `pendingChatCleanup` entries are removed, once the+ * chat's artifacts are gone.+ */+function mergeTombstones(+ primary: ChatDeleteTombstone[] | undefined,+ recovered: ChatDeleteTombstone[]+): ChatDeleteTombstone[] {+ const merged = [...(primary ?? [])]+ for (const candidate of recovered) {+ const present = merged.some(+ existing =>+ existing.chatId === candidate.chatId &&+ sameChatAuthorityScope(existing.authorityScope, candidate.authorityScope)+ )+ if (!present) merged.push(candidate)+ }+ return merged}class UnsupportedChatIndexVersionError extends Error {@@ -350,13 +419,26 @@ export class ChatStore {return current}+ /**+ * Same check as `requireAuthorityScope` but on the deletion identity+ * (environment + user): pending-cleanup retries must keep working after a team+ * switch, and must still refuse another user or environment.+ */+ private requireDeletionIdentity(expected: ChatAuthorityScope): ChatAuthorityScope {+ const current = this.authorityScope+ if (!current || !sameChatDeletionIdentity(current, expected)) {+ throw new Error('Chat deletion authority scope changed')+ }+ return current+ }+private isDeletedInScope(index: ChatIndex, chatId: string): boolean {return Boolean(index.deletedChatTombstones?.some(tombstone =>tombstone.chatId === chatId &&(!this.authorityScope ||- sameChatAuthorityScope(tombstone.authorityScope, this.authorityScope))+ sameChatDeletionIdentity(tombstone.authorityScope, this.authorityScope))))}@@ -369,6 +451,9 @@ export class ChatStore {*/private indexChains = new Map<string, Promise<unknown>>()private chatChains = new Map<string, Promise<unknown>>()+ private tombstoneSidecarSerialized = new Map<string, string>()+ private warnedTombstoneAgents = new Set<string>()+ private warnedCleanupFailures = new Set<string>()private cleanedSnapshotSiblingKeys = new Set<string>()private compatibilityCheckedKeys = new Set<string>()private compatibilityWindows = new Map<string, ChatMessage[]>()@@ -436,6 +521,14 @@ export class ChatStore {return join(this.agentDir(agentRef), 'index.json')}+ /**+ * Dot-prefixed, so it can never equal `${chatId}.json` (chat ids may not start+ * with a dot) nor the `index.json` catalog.+ */+ private tombstoneSidecarPath(agentRef: string): string {+ return join(this.agentDir(agentRef), '.tombstones.json')+ }+private corruptIndexPath(agentRef: string): string {return join(this.agentDir(agentRef), '.corrupt', `index-${randomUUID()}.json`)}@@ -1503,22 +1596,91 @@ export class ChatStore {return pruned.meta}+ /**+ * The tombstones persisted in the sidecar, or null when there is none.+ * An unreadable sidecar is reported (once per agent) and treated as absent:+ * the catalog it protects is being rebuilt from an empty one already, and the+ * next tombstone write replaces it.+ */+ private async readTombstoneSidecar(agentRef: string): Promise<TombstoneSidecar | null> {+ let raw: string+ try {+ raw = await fs.readFile(this.tombstoneSidecarPath(agentRef), 'utf-8')+ } catch (error) {+ if (isNotFoundError(error)) return null+ throw error+ }+ try {+ const parsed = JSON.parse(raw) as Record<string, unknown>+ if (+ parsed?.version !== TOMBSTONE_SIDECAR_VERSION ||+ !Array.isArray(parsed.deletedChatTombstones) ||+ !Array.isArray(parsed.pendingChatCleanup)+ ) {+ throw new Error('Invalid tombstone sidecar')+ }+ return {+ version: TOMBSTONE_SIDECAR_VERSION,+ deletedChatTombstones: parsed.deletedChatTombstones.filter(isChatDeleteTombstone),+ pendingChatCleanup: parsed.pendingChatCleanup.filter(isChatDeleteTombstone),+ }+ } catch (error) {+ if (!this.warnedTombstoneAgents.has(`sidecar:${agentRef}`)) {+ this.warnedTombstoneAgents.add(`sidecar:${agentRef}`)+ console.warn(+ `[chatStore] Unreadable tombstone sidecar for "${agentRef}"; deleted-chat tombstones cannot be recovered`,+ error+ )+ }+ return null+ }+ }++ /** Adds the sidecar's tombstones (deduplicated) to an index read from disk. */+ private async withRecoveredTombstones(agentRef: string, index: ChatIndex): Promise<ChatIndex> {+ const sidecar = await this.readTombstoneSidecar(agentRef)+ if (!sidecar) return index+ const deletedChatTombstones = mergeTombstones(+ index.deletedChatTombstones,+ sidecar.deletedChatTombstones+ )+ const pendingChatCleanup = mergeTombstones(index.pendingChatCleanup, sidecar.pendingChatCleanup)+ if (deletedChatTombstones.length > 0) index.deletedChatTombstones = deletedChatTombstones+ if (pendingChatCleanup.length > 0) index.pendingChatCleanup = pendingChatCleanup+ return index+ }+async getIndex(agentRef: string): Promise<ChatIndex> {let raw: stringtry {raw = await fs.readFile(this.indexPath(agentRef), 'utf-8')} catch (error) {- if (isNotFoundError(error)) return emptyIndex()+ // A missing catalog (fresh agent, or one quarantined earlier) still owes+ // the tombstones its sidecar holds.+ if (isNotFoundError(error)) {+ return this.withRecoveredTombstones(agentRef, emptyIndex())+ }throw error}try {- return parseChatIndex(raw)+ const { index, droppedTombstoneEntries } = parseChatIndex(raw)+ if (droppedTombstoneEntries === 0) return index+ const warnKey = `dropped:${agentRef}`+ if (!this.warnedTombstoneAgents.has(warnKey)) {+ this.warnedTombstoneAgents.add(warnKey)+ console.warn(+ `[chatStore] Dropped ${droppedTombstoneEntries} malformed tombstone entr${droppedTombstoneEntries === 1 ? 'y' : 'ies'} from the chat index of "${agentRef}"`+ )+ }+ return this.withRecoveredTombstones(agentRef, index)} catch (error) {if (error instanceof UnsupportedChatIndexVersionError) throw error// Preserve unreadable catalog bytes for bounded recovery/diagnostics, but// detach them from the writable path so a torn index cannot brick every// chat mutation. Paged transcripts live under chats/ and remain untouched;// the catalog can be repopulated by the server as normal writes resume.+ // The tombstone sidecar survives the quarantine, so confirmed local+ // deletions keep holding on the rebuilt catalog.const source = this.indexPath(agentRef)const target = this.corruptIndexPath(agentRef)await fs.mkdir(dirname(target), { recursive: true, mode: 0o700 })@@ -1533,7 +1695,7 @@ export class ChatStore {})}}- return emptyIndex()+ return this.withRecoveredTombstones(agentRef, emptyIndex())}}@@ -1550,6 +1712,26 @@ export class ChatStore {// Visibility atomicity alone does not make the catalog durable across power loss.const target = this.indexPath(agentRef)await this.writeJsonAtomic(target, normalized, 2)+ await this.saveTombstoneSidecar(agentRef, normalized)+ }++ /**+ * Mirrors the tombstone lists into the sidecar (atomically, after the index)+ * whenever they change. Skipped for an agent that never had a tombstone.+ */+ private async saveTombstoneSidecar(agentRef: string, index: ChatIndex): Promise<void> {+ const sidecar: TombstoneSidecar = {+ version: TOMBSTONE_SIDECAR_VERSION,+ deletedChatTombstones: index.deletedChatTombstones ?? [],+ pendingChatCleanup: index.pendingChatCleanup ?? [],+ }+ const serialized = JSON.stringify(sidecar)+ const known = this.tombstoneSidecarSerialized.get(agentRef)+ const nothingToProtect =+ sidecar.deletedChatTombstones.length === 0 && sidecar.pendingChatCleanup.length === 0+ if (known === serialized || (known === undefined && nothingToProtect)) return+ await this.writeJsonAtomic(this.tombstoneSidecarPath(agentRef), sidecar)+ this.tombstoneSidecarSerialized.set(agentRef, serialized)}async listChats(agentRef: string): Promise<ChatMetadata[]> {@@ -1657,6 +1839,22 @@ export class ChatStore {return { cleanupPending: !cleaned }}+ /**+ * A deleted chat's cleanup is retried on every catalog read, so a permanent+ * failure (EACCES on the chat dir) would repeat forever. Report it once per+ * (agent, chat, error code) instead of on every retry, and never hide it.+ */+ private warnCleanupFailureOnce(agentRef: string, chatId: string | null, error: unknown): void {+ const code = (error as NodeJS.ErrnoException | undefined)?.code ?? 'UNKNOWN'+ const key = JSON.stringify([agentRef, chatId, code])+ if (this.warnedCleanupFailures.has(key)) return+ this.warnedCleanupFailures.add(key)+ console.warn(+ `[chatStore] Deleted-chat cleanup failed for agent "${agentRef}"${chatId ? ` chat "${chatId}"` : ''} (${code}); it stays queued and is retried`,+ error+ )+ }+private async removeChatArtifacts(agentRef: string,chatId: string,@@ -1668,9 +1866,12 @@ export class ChatStore {corruptLegacyEntries = await fs.readdir(corruptLegacyDir)} catch (error) {if ((error as NodeJS.ErrnoException).code === 'ENOENT') corruptLegacyEntries = []- else return false+ else {+ this.warnCleanupFailureOnce(agentRef, chatId, error)+ return false+ }}- if (!this.authorityScope || !sameChatAuthorityScope(this.authorityScope, authorityScope)) {+ if (!this.authorityScope || !sameChatDeletionIdentity(this.authorityScope, authorityScope)) {return false}const paths = [@@ -1686,26 +1887,28 @@ export class ChatStore {]let allRemoved = truefor (const target of paths) {- if (!this.authorityScope || !sameChatAuthorityScope(this.authorityScope, authorityScope)) {+ if (!this.authorityScope || !sameChatDeletionIdentity(this.authorityScope, authorityScope)) {return false}try {await fs.rm(target.path, { recursive: target.recursive, force: true })- } catch {+ } catch (error) {+ this.warnCleanupFailureOnce(agentRef, chatId, error)allRemoved = false}}- if (!this.authorityScope || !sameChatAuthorityScope(this.authorityScope, authorityScope)) {+ if (!this.authorityScope || !sameChatDeletionIdentity(this.authorityScope, authorityScope)) {return false}try {await this.removePagedChatSnapshotSiblings(agentRef, chatId)- } catch {+ } catch (error) {+ this.warnCleanupFailureOnce(agentRef, chatId, error)allRemoved = false}return (allRemoved &&- Boolean(this.authorityScope && sameChatAuthorityScope(this.authorityScope, authorityScope))+ Boolean(this.authorityScope && sameChatDeletionIdentity(this.authorityScope, authorityScope)))}@@ -1715,7 +1918,7 @@ export class ChatStore {): Promise<boolean> {if (!this.authorityScope ||- !sameChatAuthorityScope(this.authorityScope, tombstone.authorityScope)+ !sameChatDeletionIdentity(this.authorityScope, tombstone.authorityScope)) {return false}@@ -1726,35 +1929,37 @@ export class ChatStore {tombstone.chatId,tombstone.authorityScope)- } catch {+ } catch (error) {+ this.warnCleanupFailureOnce(agentRef, tombstone.chatId, error)return false}if (!artifactsRemoved ||!this.authorityScope ||- !sameChatAuthorityScope(this.authorityScope, tombstone.authorityScope)+ !sameChatDeletionIdentity(this.authorityScope, tombstone.authorityScope)) {return false}try {await this.serializeIndex(agentRef, async () => {- this.requireAuthorityScope(tombstone.authorityScope)+ this.requireDeletionIdentity(tombstone.authorityScope)const index = await this.getIndex(agentRef)index.pendingChatCleanup = (index.pendingChatCleanup ?? []).filter(existing =>existing.chatId !== tombstone.chatId ||- !sameChatAuthorityScope(existing.authorityScope, tombstone.authorityScope)+ !sameChatDeletionIdentity(existing.authorityScope, tombstone.authorityScope))await this.saveIndex(agentRef, index)})return true- } catch {+ } catch (error) {+ this.warnCleanupFailureOnce(agentRef, tombstone.chatId, error)return false}}async retryPendingDeleteCleanups(authorityScope: ChatAuthorityScope): Promise<void> {- const scope = this.requireAuthorityScope(authorityScope)+ const scope = this.requireDeletionIdentity(authorityScope)let entriestry {entries = await fs.readdir(this.baseDir, { withFileTypes: true })@@ -1764,21 +1969,23 @@ export class ChatStore {}for (const entry of entries) {if (!entry.isDirectory()) continue- let agentRef = entry.name+ const agentRef = entry.nametry {const index = await this.getIndex(agentRef)const pending = (index.pendingChatCleanup ?? []).filter(item =>- sameChatAuthorityScope(item.authorityScope, scope)+ sameChatDeletionIdentity(item.authorityScope, scope))for (const tombstone of pending) {- if (!this.authorityScope || !sameChatAuthorityScope(this.authorityScope, scope)) return+ if (!this.authorityScope || !sameChatDeletionIdentity(this.authorityScope, scope)) returnawait this.serializeChat(agentRef, tombstone.chatId, () =>this.finishPendingDeleteCleanup(agentRef, tombstone))}- } catch {+ } catch (error) {// An unrelated or unreadable agent index must not prevent other durable- // cleanup entries from being retried on the next authorized read.+ // cleanup entries from being retried on the next authorized read, but the+ // failure is reported (once per agent and code), never swallowed.+ this.warnCleanupFailureOnce(agentRef, null, error)}}}diff --git a/desktop-app/src/httpClient.ts b/desktop-app/src/httpClient.tsindex f90eef145..a7f637bbe 100644--- a/desktop-app/src/httpClient.ts+++ b/desktop-app/src/httpClient.ts@@ -1,4 +1,5 @@import { config } from './config.js'+import { boundedErrorExcerpt, hostAccessDenialMessage } from './upstreamErrors.js'export class ApiError extends Error {status: number@@ -141,6 +142,11 @@ export async function requestJson<T>(const raw = await response.text()if (!response.ok) {+ const retryAfter = response.headers.get('retry-after')+ const hostAccessMessage = hostAccessDenialMessage(response.status, raw)+ if (hostAccessMessage) {+ throw new ApiError(hostAccessMessage, response.status, raw, retryAfter)+ }let msg = raw || response.statusTexttry {const parsed = JSON.parse(raw) as { error?: unknown; message?: unknown }@@ -149,11 +155,12 @@ export async function requestJson<T>(} catch {// Keep raw text as message.}+ // The full body stays on `bodyText`; the message carries a bounded excerpt.throw new ApiError(- `${response.status} ${response.statusText}: ${msg}`,+ `${response.status} ${response.statusText}: ${boundedErrorExcerpt(msg)}`,response.status,raw,- response.headers.get('retry-after')+ retryAfter)}diff --git a/desktop-app/src/ipc.ts b/desktop-app/src/ipc.tsindex 87c9d5b5e..622e4f3b2 100644--- a/desktop-app/src/ipc.ts+++ b/desktop-app/src/ipc.ts@@ -1707,8 +1707,21 @@ export function registerIpcHandlers(service: AppService): void {const agentRef = sanitizeString(payload?.agentRef)if (!agentRef) throw new Error('agentRef is required')const store = requireChatStore()- const authority = service.getChatDeletionFenceAuthority()- await store.retryPendingDeleteCleanups(authority.authorityScope)+ // The catalog read must never depend on the deleted-chat artifact cleanup:+ // a cleanup failure (scope changed mid-flight, no session, an unreadable+ // chats directory) would otherwise reject the read and leave the renderer+ // without the tombstones it filters deleted chats with. The failure is+ // reported with its code and the cleanup stays queued for the next read.+ try {+ const authority = service.getChatDeletionFenceAuthority()+ await store.retryPendingDeleteCleanups(authority.authorityScope)+ } catch (error) {+ const code = (error as NodeJS.ErrnoException | undefined)?.code+ console.warn(+ `[chat:getIndex] Deleted-chat cleanup retry failed for agent "${agentRef}"${code ? ` (${code})` : ''}; returning the index with its tombstones`,+ error+ )+ }return store.getIndex(agentRef)})diff --git a/desktop-app/src/rpcProxyClient.ts b/desktop-app/src/rpcProxyClient.tsindex de5292bfe..6d0ea772a 100644--- a/desktop-app/src/rpcProxyClient.ts+++ b/desktop-app/src/rpcProxyClient.ts@@ -23,6 +23,7 @@ import {SessionsListResult,SetHostModelResult,} from './types.js'+import { boundedErrorExcerpt, hostAccessDenialMessage } from './upstreamErrors.js'export type { SandboxUiApp } from './types.js'@@ -425,7 +426,9 @@ export class SandboxUiSessionError extends Error {readonly status: numberreadonly body: stringconstructor(status: number, body: string) {- super(`sandbox-ui session mint failed (${status}): ${body || '<empty body>'}`)+ super(+ `sandbox-ui session mint failed (${status}): ${boundedErrorExcerpt(body) || '<empty body>'}`+ )this.status = statusthis.body = bodythis.name = 'SandboxUiSessionError'@@ -456,31 +459,14 @@ function errorCodeFromBody(body: string): string | null {}}-function exactHostAccessRevokedError(status: number, body: string): ApiError | null {- const denied =- status === 403 && errorCodeFromBody(body) === 'Forbidden: user cannot access this host'- return denied ? new ApiError('403 Forbidden: host_access_revoked', 403, body) : null-}--function projectHostAccessRevoked(error: unknown): unknown {- if (!(error instanceof ApiError)) return error- return exactHostAccessRevokedError(error.status, error.bodyText) ?? error-}--async function projectHostAccessRevokedInPromise<T>(promise: Promise<T>): Promise<T> {- try {- return await promise- } catch (error) {- throw projectHostAccessRevoked(error)- }-}--function requestJsonProjected<T>(- method: Parameters<typeof requestJson>[0],- url: Parameters<typeof requestJson>[1],- options: Parameters<typeof requestJson>[2]-): Promise<T> {- return projectHostAccessRevokedInPromise(requestJson<T>(method, url, options))+/**+ * The single producer of Host-access denial errors for the raw-`fetch` methods:+ * the message strings and the `code` rule live in `upstreamErrors.ts` and are+ * shared with `httpClient.requestJson`.+ */+function hostAccessDenialError(status: number, body: string): ApiError | null {+ const message = hostAccessDenialMessage(status, body)+ return message ? new ApiError(message, status, body) : null}export class RpcProxyClient {@@ -540,7 +526,7 @@ export class RpcProxyClient {options?: { async?: boolean }): Promise<HostMessageResponse> {const query = options?.async ? '?async=true' : ''- return requestJsonProjected<HostMessageResponse>(+ return requestJson<HostMessageResponse>('POST',url(`/api/v1/rpc/hosts/${encodeURIComponent(hostRef)}/messages${query}`),{@@ -555,7 +541,7 @@ export class RpcProxyClient {hostRef: string,taskId: string): Promise<HostMessageResponse> {- return requestJsonProjected<HostMessageResponse>(+ return requestJson<HostMessageResponse>('GET',url(`/api/v1/rpc/hosts/${encodeURIComponent(hostRef)}/tasks/${encodeURIComponent(taskId)}/result`@@ -601,7 +587,11 @@ export class RpcProxyClient {return { status }}const body = await readErrorBody(response)- throw new ApiError(`Prewarm failed (${response.status}): ${body}`, response.status, body)+ throw new ApiError(+ `Prewarm failed (${response.status}): ${boundedErrorExcerpt(body)}`,+ response.status,+ body+ )}async getHostHealth(rpcAccessToken: string, hostRef: string): Promise<HostRuntimeHealth> {@@ -712,7 +702,7 @@ export class RpcProxyClient {if (!response.ok) {const body = await response.text()throw new Error(- `sandbox-ui authorize-url request failed (${response.status}): ${body || '<empty>'}`+ `sandbox-ui authorize-url request failed (${response.status}): ${boundedErrorExcerpt(body) || '<empty>'}`)}const json = (await response.json()) as { authorizeUrl?: unknown }@@ -759,7 +749,7 @@ export class RpcProxyClient {if (!response.ok) {const body = await response.text()throw new ApiError(- `mcp-oauth authorize-url request failed (${response.status}): ${body || '<empty>'}`,+ `mcp-oauth authorize-url request failed (${response.status}): ${boundedErrorExcerpt(body) || '<empty>'}`,response.status,body)@@ -792,7 +782,9 @@ export class RpcProxyClient {})if (!response.ok) {const body = await response.text()- throw new Error(`Host stream failed (${response.status}): ${body || response.statusText}`)+ throw new Error(+ `Host stream failed (${response.status}): ${boundedErrorExcerpt(body) || response.statusText}`+ )}if (!response.body) {throw new Error('Host stream missing response body')@@ -914,7 +906,11 @@ export class RpcProxyClient {const body = await response.text()// ApiError (not a bare Error) so `AppService.shouldRefreshRpcToken` can see// the 401/403 status and drive the retry-after-refresh (§4.5-7).- throw new ApiError(`Approve failed (${response.status}): ${body}`, response.status, body)+ throw new ApiError(+ `Approve failed (${response.status}): ${boundedErrorExcerpt(body)}`,+ response.status,+ body+ )}return parseApprovalDecisionResponse(response)}@@ -940,7 +936,11 @@ export class RpcProxyClient {)if (!response.ok) {const body = await response.text()- throw new ApiError(`Deny failed (${response.status}): ${body}`, response.status, body)+ throw new ApiError(+ `Deny failed (${response.status}): ${boundedErrorExcerpt(body)}`,+ response.status,+ body+ )}return parseApprovalDecisionResponse(response)}@@ -961,7 +961,11 @@ export class RpcProxyClient {)if (!response.ok) {const body = await response.text()- throw new ApiError(`cancelTask failed (${response.status}): ${body}`, response.status, body)+ throw new ApiError(+ `cancelTask failed (${response.status}): ${boundedErrorExcerpt(body)}`,+ response.status,+ body+ )}}@@ -981,7 +985,7 @@ export class RpcProxyClient {if (!response.ok) {const body = await response.text()throw new ApiError(- `List artifacts failed (${response.status}): ${body}`,+ `List artifacts failed (${response.status}): ${boundedErrorExcerpt(body)}`,response.status,body)@@ -1002,7 +1006,7 @@ export class RpcProxyClient {)if (!response.ok) {const body = await response.text()- throw new Error(`Download artifact failed (${response.status}): ${body}`)+ throw new Error(`Download artifact failed (${response.status}): ${boundedErrorExcerpt(body)}`)}return Buffer.from(await response.arrayBuffer())}@@ -1023,10 +1027,10 @@ export class RpcProxyClient {})if (!response.ok) {const body = await response.text()- const hostAccessRevoked = exactHostAccessRevokedError(response.status, body)- if (hostAccessRevoked) throw hostAccessRevoked+ const hostAccessDenial = hostAccessDenialError(response.status, body)+ if (hostAccessDenial) throw hostAccessDenialthrow new ApiError(- `List sessions failed (${response.status}): ${body}`,+ `List sessions failed (${response.status}): ${boundedErrorExcerpt(body)}`,response.status,body)@@ -1067,10 +1071,10 @@ export class RpcProxyClient {}if (!response.ok) {const body = await response.text()- const hostAccessRevoked = exactHostAccessRevokedError(response.status, body)- if (hostAccessRevoked) throw hostAccessRevoked+ const hostAccessDenial = hostAccessDenialError(response.status, body)+ if (hostAccessDenial) throw hostAccessDenialthrow new ApiError(- `Load session messages failed (${response.status}): ${body}`,+ `Load session messages failed (${response.status}): ${boundedErrorExcerpt(body)}`,response.status,body)@@ -1113,7 +1117,7 @@ export class RpcProxyClient {if (!response.ok) {const body = await response.text()throw new ApiError(- `Get context breakdown failed (${response.status}): ${body}`,+ `Get context breakdown failed (${response.status}): ${boundedErrorExcerpt(body)}`,response.status,body)@@ -1156,7 +1160,7 @@ export class RpcProxyClient {if (!response.ok) {const body = await readErrorBody(response)throw new ApiError(- `Get host models failed (${response.status}): ${body}`,+ `Get host models failed (${response.status}): ${boundedErrorExcerpt(body)}`,response.status,body)@@ -1216,7 +1220,7 @@ export class RpcProxyClient {)}throw new ApiError(- `Set host model failed (${response.status}): ${body}`,+ `Set host model failed (${response.status}): ${boundedErrorExcerpt(body)}`,response.status,body)@@ -1260,20 +1264,9 @@ export class RpcProxyClient {// Body may echo the invalid title; read it for the ApiError payload but// keep the raw title out of the human-facing message.const body = await readErrorBody(response)- let hostAccessRevoked = false- if (response.status === 403) {- try {- const parsed = JSON.parse(body) as { error?: unknown }- hostAccessRevoked = parsed?.error === 'Forbidden: user cannot access this host'- } catch {- // A non-JSON or interposed 403 is not an authoritative Host denial.- }- }- throw new ApiError(- `Rename session failed (${response.status})${hostAccessRevoked ? ': host_access_revoked' : ''}`,- response.status,- body- )+ const hostAccessDenial = hostAccessDenialError(response.status, body)+ if (hostAccessDenial) throw hostAccessDenial+ throw new ApiError(`Rename session failed (${response.status})`, response.status, body)}const parsed = (await response.json().catch(() => ({}))) as { title?: unknown }// Re-sanitize the server's echoed title on read (defense in depth, A14 rule).@@ -1316,7 +1309,7 @@ export class RpcProxyClient {if (!res.ok) {const text = await readErrorBody(res)throw new ApiError(- `Desktop session exchange failed: ${res.status} ${text || res.statusText}`,+ `Desktop session exchange failed: ${res.status} ${boundedErrorExcerpt(text) || res.statusText}`,res.status,text)diff --git a/desktop-app/src/sharedFilesClient.ts b/desktop-app/src/sharedFilesClient.tsindex 3f574e47e..c86a806c5 100644--- a/desktop-app/src/sharedFilesClient.ts+++ b/desktop-app/src/sharedFilesClient.ts@@ -1,5 +1,6 @@import { config } from './config.js'import { ApiError, requestJson } from './httpClient.js'+import { boundedErrorExcerpt } from './upstreamErrors.js'export type ContextSharedFilesystemSummary = {name: string@@ -96,7 +97,7 @@ export class SharedFilesClient {if (!response.ok) {const body = await response.text()throw new ApiError(- `${response.status} ${response.statusText}: ${body}`,+ `${response.status} ${response.statusText}: ${boundedErrorExcerpt(body)}`,response.status,body)diff --git a/desktop-app/src/upstreamErrors.ts b/desktop-app/src/upstreamErrors.tsnew file mode 100644index 000000000..e738391a4--- /dev/null+++ b/desktop-app/src/upstreamErrors.ts@@ -0,0 +1,72 @@+/**+ * Shared vocabulary for the errors Desktop main builds from upstream (rpc-proxy,+ * control-api, mcp-host) HTTP responses.+ *+ * Two concerns live here so every call site agrees on them:+ *+ * 1. The Host-access denial messages the renderer matches on+ * (`ui/src/lib/format.ts isConfirmedHostAccessRevoked`). The strings are+ * defined ONCE; `httpClient.requestJson` and the raw-`fetch` methods of+ * `rpcProxyClient` all build them through {@link hostAccessDenialMessage}.+ * 2. A bounded, single-line excerpt of an upstream body for error messages+ * ({@link boundedErrorExcerpt}), so a hostile or misbehaving upstream cannot+ * push an unbounded payload through `Error.message` into logs, IPC and UI.+ */++/** Machine `code` rpc-proxy sets when the user's access to the Host was removed. */+export const HOST_ACCESS_REVOKED_CODE = 'host_access_revoked'+/** Machine `code` rpc-proxy sets for every other denial it authors itself. */+export const HOST_ACCESS_DENIED_CODE = 'host_access_denied'++/** The only message that lets the renderer treat a 403 as confirmed revocation. */+export const HOST_ACCESS_REVOKED_MESSAGE = `403 Forbidden: ${HOST_ACCESS_REVOKED_CODE}`+/** A 403 rpc-proxy authored that does NOT confirm revocation (renderer: generic 403). */+export const HOST_ACCESS_DENIED_MESSAGE = `403 Forbidden: ${HOST_ACCESS_DENIED_CODE}`++/** Upper bound (characters) of the upstream body excerpt embedded in a message. */+export const ERROR_EXCERPT_MAX_CHARS = 512++const RESERVED_HOST_ACCESS_TOKEN = /host_access_(revoked|denied)/gi++/**+ * A short single-line excerpt of an upstream body for an error message.+ *+ * Whitespace is collapsed, the result is cut at {@link ERROR_EXCERPT_MAX_CHARS}+ * characters (an ellipsis marks the cut), and the reserved Host-access tokens are+ * defused (`host_access_revoked` -> `host-access-revoked`): only+ * {@link hostAccessDenialMessage} may emit them, so a body that merely contains+ * the words can never forge a confirmed revocation.+ */+export function boundedErrorExcerpt(text: string): string {+ const collapsed = text+ .replace(/\s+/g, ' ')+ .trim()+ .replace(RESERVED_HOST_ACCESS_TOKEN, 'host-access-$1')+ return collapsed.length > ERROR_EXCERPT_MAX_CHARS+ ? `${collapsed.slice(0, ERROR_EXCERPT_MAX_CHARS)}…`+ : collapsed+}++/** The `code` string of a JSON error body, or null for non-JSON / code-less bodies. */+function machineCodeFromBody(body: string): string | null {+ try {+ const parsed = JSON.parse(body) as { code?: unknown }+ return typeof parsed?.code === 'string' ? parsed.code : null+ } catch {+ return null+ }+}++/**+ * The fixed Host-access message for a 403 response, or null when the response is+ * not one rpc-proxy authored with a Host-access `code`. Decided ONLY from the+ * parsed JSON `code` field: an `error` string, a text body or an interposed+ * proxy's page never qualifies.+ */+export function hostAccessDenialMessage(status: number, body: string): string | null {+ if (status !== 403) return null+ const code = machineCodeFromBody(body)+ if (code === HOST_ACCESS_REVOKED_CODE) return HOST_ACCESS_REVOKED_MESSAGE+ if (code === HOST_ACCESS_DENIED_CODE) return HOST_ACCESS_DENIED_MESSAGE+ return null+}diff --git a/desktop-app/test/e2e-playwright/human-stateless-continuity.spec.ts b/desktop-app/test/e2e-playwright/human-stateless-continuity.spec.tsindex f19d24b63..e8ec0b697 100644--- a/desktop-app/test/e2e-playwright/human-stateless-continuity.spec.ts+++ b/desktop-app/test/e2e-playwright/human-stateless-continuity.spec.ts@@ -1,3 +1,20 @@+/**+ * Recorded human journey: two Hosts (stateful + stateless), Desktop restart,+ * cache, and two verified stateless wake episodes. It mutates the cluster+ * (suspends and wakes Hosts) and sends four real model turns.+ *+ * Environment (no repository doc covers these; this header is the reference):+ * - `HUMAN_E2E_STATELESS_CONTINUITY=1` opts in. Without exactly `1` the whole+ * file is skipped (see the `test.skip` below), so the default lane never+ * mutates a cluster.+ * - `HUMAN_E2E_KUBE_CONTEXT=<context>` is required and must equal the+ * global-setup context (`E2E_K8S_CONTEXT` / `KUBECONTEXT` / `K8S_CONTEXT`,+ * default `clerum-test`), so the journey cannot mutate a different cluster+ * than the one the suite was set up against.+ * - `QA_RECORDER_CONFIRM_CHAT` acknowledges the model cost.+ * - Optional Host overrides: `HUMAN_E2E_STATEFUL_HOST`, `HUMAN_E2E_STATELESS_HOST`,+ * `HUMAN_E2E_STATEFUL_HOST_RESOURCE`, `HUMAN_E2E_STATELESS_HOST_RESOURCE`.+ */import { type ElectronApplication, type Locator, type Page, expect, test } from '@playwright/test'import { type ChildProcess, execFile } from 'node:child_process'import { writeFileSync } from 'node:fs'@@ -406,6 +423,16 @@ test('human journey — two Hosts, restart, cache, and two verified stateless walet app: ElectronApplication | undefinedlet page: Page | undefined+ // The handles are cleared while Electron is closed between steps; a step that+ // uses one while it is cleared must fail loudly, not act on `undefined`.+ const currentApp = (): ElectronApplication => {+ if (!app) throw new Error('Electron app is not running at this step')+ return app+ }+ const currentPage = (): Page => {+ if (!page) throw new Error('Desktop page is not open at this step')+ return page+ }let firstProfilePath = ''const metrics: JourneyMetrics = {@@ -558,7 +585,7 @@ test('human journey — two Hosts, restart, cache, and two verified stateless waawait test.step('exit Electron and prove a real replicas-0 suspension', async () => {const closeStarted = Date.now()- const electronChild = app.process()+ const electronChild = currentApp().process()await finalizeRecording(app, page)await closeElectron(app, electronChild)metrics.first_close_ms = Date.now() - closeStarted@@ -619,8 +646,8 @@ test('human journey — two Hosts, restart, cache, and two verified stateless wa})await test.step('prepare and send from a second verified cold stateless state', async () => {- await openExactSession(page, statelessTitle, STATELESS_HOST)- const composer = page.getByRole('textbox', { name: 'Agent message composer' })+ await openExactSession(currentPage(), statelessTitle, STATELESS_HOST)+ const composer = currentPage().getByRole('textbox', { name: 'Agent message composer' })await composer.fill('')const secondSuspended = await waitForStatelessSuspended(SUSPEND_TIMEOUT_MS)metrics.second_suspend_ms = secondSuspended.elapsedMs@@ -630,7 +657,7 @@ test('human journey — two Hosts, restart, cache, and two verified stateless waconst followPrompt = `Earlier I gave you a token. Reply with exactly that token followed by ${statelessFollowMarker}.`await humanType(composer, followPrompt)- await expect(page.getByTestId('send-button')).toBeEnabled()+ await expect(currentPage().getByTestId('send-button')).toBeEnabled()// The catalog prewarm after reopen is measured separately by// second_launch_to_stateless_ready_ms. This fresh snapshot proves that// the second send is itself a cold wake intent, not a continuation of@@ -650,16 +677,16 @@ test('human journey — two Hosts, restart, cache, and two verified stateless wareadyObserved = truereturn { result, observedAt: Date.now() }})- const taskStepper = page.getByTestId('progress-stepper')+ const taskStepper = currentPage().getByTestId('progress-stepper')const stepperBaseline = await taskStepper.count()const sendStarted = Date.now()- await page.getByTestId('send-button').click()- await expect(page.getByTestId('message-list')).toContainText(statelessFollowMarker, {+ await currentPage().getByTestId('send-button').click()+ await expect(currentPage().getByTestId('message-list')).toContainText(statelessFollowMarker, {timeout: 30_000,})metrics.cold_send_to_user_message_visible_ms = Date.now() - sendStarted- const followResponse = page+ const followResponse = currentPage().getByTestId('agent-response').filter({ hasText: statelessFollowMarker })const stepperWatch = expect@@ -688,7 +715,7 @@ test('human journey — two Hosts, restart, cache, and two verified stateless wa).toBe(true)const outcome = await resolveColdSend(- page,+ currentPage(),statelessFollowMarker,statelessMarker,sendStarted@@ -698,10 +725,14 @@ test('human journey — two Hosts, restart, cache, and two verified stateless wametrics.cold_send_to_response_ms = Date.now() - sendStartedmetrics.cold_send_retries = outcome.retriesawait expect(composer).toHaveValue('', { timeout: 150_000 })- await expect(page.getByTestId('send-button')).toHaveAttribute('aria-label', 'Send message', {- timeout: 150_000,- })- await expect(page.getByTestId('send-button')).toBeDisabled({ timeout: 150_000 })+ await expect(currentPage().getByTestId('send-button')).toHaveAttribute(+ 'aria-label',+ 'Send message',+ {+ timeout: 150_000,+ }+ )+ await expect(currentPage().getByTestId('send-button')).toBeDisabled({ timeout: 150_000 })metrics.cold_send_to_composer_idle_ms = Date.now() - sendStartedawait stepperWatch@@ -712,31 +743,35 @@ test('human journey — two Hosts, restart, cache, and two verified stateless waexpect(readyAfterSend.result.snapshot.readyReplicas).toBeGreaterThan(0)expect(readyAfterSend.result.snapshot.imageIds).toEqual(metrics.stateless_image_ids_before)await expect(- page.getByTestId('agent-response').filter({ hasText: statelessFollowMarker })+ currentPage().getByTestId('agent-response').filter({ hasText: statelessFollowMarker })).toContainText(statelessFollowMarker, { timeout: 30_000 })- await screenshotAndLog(page, testInfo, '07-stateless-context-continuity-after-cold-send')+ await screenshotAndLog(+ currentPage(),+ testInfo,+ '07-stateless-context-continuity-after-cold-send'+ )})await test.step('stateful control remains continuous and responsive', async () => {- await openExactSession(page, statefulTitle, STATEFUL_HOST)- await expect(page.getByTestId('message-list')).toContainText(statefulMarker, {+ await openExactSession(currentPage(), statefulTitle, STATEFUL_HOST)+ await expect(currentPage().getByTestId('message-list')).toContainText(statefulMarker, {timeout: 30_000,})const statefulTimings = await sendMarker(- page,+ currentPage(),statefulFollowMarker,`What was the earlier token in this conversation? Reply with that token followed by ${statefulFollowMarker}.`)metrics.stateful_follow_up_to_user_message_visible_ms = statefulTimings.toUserMessageMsmetrics.stateful_follow_up_to_response_ms = statefulTimings.toResponseMsawait expect(- page.getByTestId('agent-response').filter({ hasText: statefulFollowMarker })+ currentPage().getByTestId('agent-response').filter({ hasText: statefulFollowMarker })).toContainText(statefulMarker)const statefulAfter = await readHostSnapshot(STATEFUL_DEPLOYMENT, STATEFUL_HOST_RESOURCE)metrics.stateful_pod_after = statefulAfter.podNamesexpect(statefulAfter.readyReplicas).toBeGreaterThan(0)expect(statefulAfter.podNames.join(',')).toBe(metrics.stateful_pod_before.join(','))- await screenshotAndLog(page, testInfo, '08-stateful-control-continuity')+ await screenshotAndLog(currentPage(), testInfo, '08-stateful-control-continuity')})} finally {await finalizeRecording(app, page).catch(() => undefined)diff --git a/desktop-app/test/rpcProxyClient.test.ts b/desktop-app/test/rpcProxyClient.test.tsindex 913b9de14..2a0b8a3e8 100644--- a/desktop-app/test/rpcProxyClient.test.ts+++ b/desktop-app/test/rpcProxyClient.test.ts@@ -210,9 +210,26 @@ describe('RpcProxyClient — renameSession() (spec 15 Fase B)', () => {it.each([{status: 403,- body: JSON.stringify({ error: 'Forbidden: user cannot access this host' }),+ body: JSON.stringify({+ error: 'Forbidden: user cannot access this host',+ code: 'host_access_revoked',+ }),marked: true,},+ {+ status: 403,+ body: JSON.stringify({+ error: 'Forbidden: user cannot access this host',+ code: 'host_access_denied',+ }),+ marked: false,+ },+ {+ status: 403,+ body: JSON.stringify({ error: 'Forbidden: user cannot access this host' }),+ marked: false,+ },+ { status: 403, body: JSON.stringify({ error: 'host_access_revoked' }), marked: false },{status: 403,body: JSON.stringify({ error: 'Forbidden: missing rename scope' }),@@ -221,11 +238,14 @@ describe('RpcProxyClient — renameSession() (spec 15 Fase B)', () => {{ status: 403, body: 'Forbidden: user cannot access this host', marked: false },{status: 503,- body: JSON.stringify({ error: 'Forbidden: user cannot access this host' }),+ body: JSON.stringify({+ error: 'Forbidden: user cannot access this host',+ code: 'host_access_revoked',+ }),marked: false,},])(- 'marks exact Host-wide denial only for authoritative 403 body ($status, $marked)',+ 'marks Host-wide revocation only for a 403 whose body carries code host_access_revoked ($status, $marked)',async ({ status, body, marked }) => {vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(body, { status })))const client = new RpcProxyClient()@@ -233,6 +253,8 @@ describe('RpcProxyClient — renameSession() (spec 15 Fase B)', () => {.renameSession('rpc-token', 'chatllm', 'chatllm', 'chat-1', 'name').catch((caught: Error) => caught)expect(error).toBeInstanceOf(Error)+ // Liveness witness: the request reached the HTTP-error path and reported its status.+ expect((error as Error).message).toContain(String(status))expect((error as Error).message.includes('host_access_revoked')).toBe(marked)})