From d0e8ba4aca208ca8abc477742b3ccac5aba7e0c6 Mon Sep 17 00:00:00 2001 From: wsp Date: Fri, 6 Nov 2026 11:17:26 +0800 Subject: [PATCH] fix(web-ui): avoid false permission read errors Treat a missing Session projection as a lifecycle gap and keep empty new projections on the default permission mode when the host has not materialized them yet. Preserve unread notifications for persisted or otherwise failed sessions, and add focused lifecycle tests for the distinction. Fixes: #3758 --- .../src/flow_chat/components/ChatInput.tsx | 37 +++++++++++++++++ .../utils/sessionPermissionMode.test.ts | 41 +++++++++++++++++++ .../flow_chat/utils/sessionPermissionMode.ts | 29 +++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 src/web-ui/src/flow_chat/utils/sessionPermissionMode.test.ts create mode 100644 src/web-ui/src/flow_chat/utils/sessionPermissionMode.ts diff --git a/src/web-ui/src/flow_chat/components/ChatInput.tsx b/src/web-ui/src/flow_chat/components/ChatInput.tsx index a2add38c86..1cfa42fd54 100644 --- a/src/web-ui/src/flow_chat/components/ChatInput.tsx +++ b/src/web-ui/src/flow_chat/components/ChatInput.tsx @@ -246,6 +246,10 @@ import { isPeerDeviceModeActive } from '@/infrastructure/peer-device/peerModeFla import { usePeerDeviceModeOptional } from '@/infrastructure/peer-device/peerDeviceContextState'; import { isBtwSessionDraft } from '../utils/modelSelectionTarget'; import { hasSessionStarted, isSessionBindingLocked } from '../utils/sessionLifecycle'; +import { + canTreatMissingPermissionModeAsDefault, + isSessionNotFoundError, +} from '../utils/sessionPermissionMode'; import { prepareSessionDraftForCommand } from '../services/sessionDraftService'; import { useSessionWorkspaceSelection } from '../hooks/useSessionWorkspaceSelection'; import { SubagentAvatar, resolveSubagentNameKey } from '../subagent-identity'; @@ -717,6 +721,7 @@ export const ChatInput: React.FC = ({ const effectiveTargetSession = effectiveTargetSessionId ? flowChatState.sessions.get(effectiveTargetSessionId) : undefined; + const hasEffectiveTargetSession = effectiveTargetSession !== undefined; const peer = usePeerDeviceModeOptional(); const isBtwDraftTarget = isBtwSessionDraft(effectiveTargetSession); const btwDraftSettingsInherited = isBtwDraftTarget && Boolean(peer?.peerMode.active) @@ -2595,6 +2600,11 @@ export const ChatInput: React.FC = ({ setActiveTurnPermissionMode(null); return undefined; } + if (!hasEffectiveTargetSession) { + setSessionPermissionMode(null); + setSessionPermissionModeUnread(false); + return undefined; + } if (effectiveTargetSession?.draft) { setSessionPermissionMode(effectiveTargetSession.draft.permissionMode ?? null); setSessionPermissionModeUnread(false); @@ -2625,6 +2635,28 @@ export const ChatInput: React.FC = ({ } } catch (error) { log.warn('Failed to read session permission mode', error); + if ( + permissionModeRequestGenerationRef.current === generation + && isSessionNotFoundError(error) + && canTreatMissingPermissionModeAsDefault( + hasEffectiveTargetSession + ? { + dialogTurns: effectiveTargetSession?.dialogTurns ?? [], + historyState: effectiveTargetSession?.historyState, + isHistorical: effectiveTargetSession?.isHistorical, + persistedTurnCount: effectiveTargetSession?.persistedTurnCount, + } + : undefined, + ) + ) { + // A newly reserved empty Session is intentionally host-less until + // its first message. Keep the default mode without reporting a + // failed read; later materialization reruns this effect. + setSessionPermissionMode(null); + setSessionPermissionModeUnread(false); + permissionModeUnreadNotifiedRef.current = null; + return; + } // Falling back to the global default is the safe read: it never shows a // wider mode than the session actually runs with. Report the fallback // once per Session so it cannot pass for that Session's own selection. @@ -2649,6 +2681,10 @@ export const ChatInput: React.FC = ({ sessionOwningId, sessionOwningPath, effectiveTargetSession?.workspacePath, + effectiveTargetSession?.dialogTurns, + effectiveTargetSession?.historyState, + effectiveTargetSession?.isHistorical, + effectiveTargetSession?.persistedTurnCount, effectiveTargetSession?.remoteConnectionId, effectiveTargetSession?.remoteSshHost, effectiveTargetSession?.parentSessionId, @@ -2658,6 +2694,7 @@ export const ChatInput: React.FC = ({ isBtwDraftTarget, isAcpTargetSession, t, + hasEffectiveTargetSession, ]); const applySessionPermissionMode = useCallback(async ( diff --git a/src/web-ui/src/flow_chat/utils/sessionPermissionMode.test.ts b/src/web-ui/src/flow_chat/utils/sessionPermissionMode.test.ts new file mode 100644 index 0000000000..379a918489 --- /dev/null +++ b/src/web-ui/src/flow_chat/utils/sessionPermissionMode.test.ts @@ -0,0 +1,41 @@ +import { describe, expect, it } from 'vitest'; +import type { Session } from '../types/flow-chat'; +import { + canTreatMissingPermissionModeAsDefault, + isSessionNotFoundError, +} from './sessionPermissionMode'; + +const emptyNewSession = { + dialogTurns: [], + historyState: 'new' as const, + isHistorical: false, + persistedTurnCount: undefined, +}; + +describe('session permission mode lifecycle', () => { + it('does not classify a missing projection as a host-record failure', () => { + expect(canTreatMissingPermissionModeAsDefault(undefined)).toBe(false); + }); + + it('treats an empty new local projection as inheriting the default', () => { + expect(canTreatMissingPermissionModeAsDefault(emptyNewSession)).toBe(true); + }); + + it.each([ + ['a historical session', { ...emptyNewSession, isHistorical: true }], + ['a hydrated session', { ...emptyNewSession, historyState: 'ready' as const }], + ['a session with persisted turns', { ...emptyNewSession, persistedTurnCount: 1 }], + ['a session with projected turns', { + ...emptyNewSession, + dialogTurns: [{ id: 'turn-1' }] as Session['dialogTurns'], + }], + ])('does not hide missing records for %s', (_label, session) => { + expect(canTreatMissingPermissionModeAsDefault(session)).toBe(false); + }); + + it('requires the specific missing-session error before suppressing the notification', () => { + expect(isSessionNotFoundError(new Error('Not found: Session not found: session-1'))).toBe(true); + expect(isSessionNotFoundError(new Error('Host offline'))).toBe(false); + expect(isSessionNotFoundError(new Error('Permission denied'))).toBe(false); + }); +}); diff --git a/src/web-ui/src/flow_chat/utils/sessionPermissionMode.ts b/src/web-ui/src/flow_chat/utils/sessionPermissionMode.ts new file mode 100644 index 0000000000..ac10329da4 --- /dev/null +++ b/src/web-ui/src/flow_chat/utils/sessionPermissionMode.ts @@ -0,0 +1,29 @@ +import type { Session } from '../types/flow-chat'; + +type PermissionModeSessionState = Pick< + Session, + 'dialogTurns' | 'historyState' | 'isHistorical' | 'persistedTurnCount' +>; + +/** + * A newly reserved local session can have a UI projection before its host + * directory exists. A missing host record is expected only while that empty + * session is still in its initial local state. + */ +export function canTreatMissingPermissionModeAsDefault( + session: PermissionModeSessionState | undefined, +): boolean { + return Boolean( + session + && session.isHistorical !== true + && session.historyState === 'new' + && session.dialogTurns.length === 0 + && session.persistedTurnCount == null, + ); +} + +/** Recognize the host error emitted when the session directory is not present yet. */ +export function isSessionNotFoundError(error: unknown): boolean { + const message = error instanceof Error ? error.message : String(error); + return /session not found/i.test(message); +}