Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/web-ui/src/flow_chat/components/ChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -717,6 +721,7 @@ export const ChatInput: React.FC<ChatInputProps> = ({
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)
Expand Down Expand Up @@ -2595,6 +2600,11 @@ export const ChatInput: React.FC<ChatInputProps> = ({
setActiveTurnPermissionMode(null);
return undefined;
}
if (!hasEffectiveTargetSession) {
setSessionPermissionMode(null);
setSessionPermissionModeUnread(false);
return undefined;
}
if (effectiveTargetSession?.draft) {
setSessionPermissionMode(effectiveTargetSession.draft.permissionMode ?? null);
setSessionPermissionModeUnread(false);
Expand Down Expand Up @@ -2625,6 +2635,28 @@ export const ChatInput: React.FC<ChatInputProps> = ({
}
} 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.
Expand All @@ -2649,6 +2681,10 @@ export const ChatInput: React.FC<ChatInputProps> = ({
sessionOwningId,
sessionOwningPath,
effectiveTargetSession?.workspacePath,
effectiveTargetSession?.dialogTurns,
effectiveTargetSession?.historyState,
effectiveTargetSession?.isHistorical,
effectiveTargetSession?.persistedTurnCount,
effectiveTargetSession?.remoteConnectionId,
effectiveTargetSession?.remoteSshHost,
effectiveTargetSession?.parentSessionId,
Expand All @@ -2658,6 +2694,7 @@ export const ChatInput: React.FC<ChatInputProps> = ({
isBtwDraftTarget,
isAcpTargetSession,
t,
hasEffectiveTargetSession,
]);

const applySessionPermissionMode = useCallback(async (
Expand Down
41 changes: 41 additions & 0 deletions src/web-ui/src/flow_chat/utils/sessionPermissionMode.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
29 changes: 29 additions & 0 deletions src/web-ui/src/flow_chat/utils/sessionPermissionMode.ts
Original file line number Diff line number Diff line change
@@ -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);
}
Loading