mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-03-10 14:10:37 -07:00
feat(admin): enable 30 day default retention for chat history & remove warning (#20853)
This commit is contained in:
@@ -185,9 +185,6 @@ export interface SessionRetentionSettings {
|
||||
|
||||
/** Minimum retention period (safety limit, defaults to "1d") */
|
||||
minRetention?: string;
|
||||
|
||||
/** INTERNAL: Whether the user has acknowledged the session retention warning */
|
||||
warningAcknowledged?: boolean;
|
||||
}
|
||||
|
||||
export interface SettingsError {
|
||||
|
||||
@@ -339,7 +339,7 @@ const SETTINGS_SCHEMA = {
|
||||
label: 'Enable Session Cleanup',
|
||||
category: 'General',
|
||||
requiresRestart: false,
|
||||
default: false,
|
||||
default: true as boolean,
|
||||
description: 'Enable automatic session cleanup',
|
||||
showInDialog: true,
|
||||
},
|
||||
@@ -348,7 +348,7 @@ const SETTINGS_SCHEMA = {
|
||||
label: 'Keep chat history',
|
||||
category: 'General',
|
||||
requiresRestart: false,
|
||||
default: undefined as string | undefined,
|
||||
default: '30d' as string,
|
||||
description:
|
||||
'Automatically delete chats older than this time period (e.g., "30d", "7d", "24h", "1w")',
|
||||
showInDialog: true,
|
||||
@@ -372,16 +372,6 @@ const SETTINGS_SCHEMA = {
|
||||
description: `Minimum retention period (safety limit, defaults to "${DEFAULT_MIN_RETENTION}")`,
|
||||
showInDialog: false,
|
||||
},
|
||||
warningAcknowledged: {
|
||||
type: 'boolean',
|
||||
label: 'Warning Acknowledged',
|
||||
category: 'General',
|
||||
requiresRestart: false,
|
||||
default: false,
|
||||
showInDialog: false,
|
||||
description:
|
||||
'INTERNAL: Whether the user has acknowledged the session retention warning',
|
||||
},
|
||||
},
|
||||
description: 'Settings for automatic session cleanup.',
|
||||
},
|
||||
|
||||
@@ -146,7 +146,6 @@ import { requestConsentInteractive } from '../config/extensions/consent.js';
|
||||
import { useSessionBrowser } from './hooks/useSessionBrowser.js';
|
||||
import { useSessionResume } from './hooks/useSessionResume.js';
|
||||
import { useIncludeDirsTrust } from './hooks/useIncludeDirsTrust.js';
|
||||
import { useSessionRetentionCheck } from './hooks/useSessionRetentionCheck.js';
|
||||
import { isWorkspaceTrusted } from '../config/trustedFolders.js';
|
||||
import { useSettings } from './contexts/SettingsContext.js';
|
||||
import { terminalCapabilityManager } from './utils/terminalCapabilityManager.js';
|
||||
@@ -1548,28 +1547,6 @@ Logging in with Google... Restarting Gemini CLI to continue.
|
||||
|
||||
useIncludeDirsTrust(config, isTrustedFolder, historyManager, setCustomDialog);
|
||||
|
||||
const handleAutoEnableRetention = useCallback(() => {
|
||||
const userSettings = settings.forScope(SettingScope.User).settings;
|
||||
const currentRetention = userSettings.general?.sessionRetention ?? {};
|
||||
|
||||
settings.setValue(SettingScope.User, 'general.sessionRetention', {
|
||||
...currentRetention,
|
||||
enabled: true,
|
||||
maxAge: '30d',
|
||||
warningAcknowledged: true,
|
||||
});
|
||||
}, [settings]);
|
||||
|
||||
const {
|
||||
shouldShowWarning: shouldShowRetentionWarning,
|
||||
checkComplete: retentionCheckComplete,
|
||||
sessionsToDeleteCount,
|
||||
} = useSessionRetentionCheck(
|
||||
config,
|
||||
settings.merged,
|
||||
handleAutoEnableRetention,
|
||||
);
|
||||
|
||||
const tabFocusTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -2015,7 +1992,7 @@ Logging in with Google... Restarting Gemini CLI to continue.
|
||||
const nightly = props.version.includes('nightly');
|
||||
|
||||
const dialogsVisible =
|
||||
(shouldShowRetentionWarning && retentionCheckComplete) ||
|
||||
shouldShowIdePrompt ||
|
||||
shouldShowIdePrompt ||
|
||||
isFolderTrustDialogOpen ||
|
||||
isPolicyUpdateDialogOpen ||
|
||||
@@ -2202,9 +2179,7 @@ Logging in with Google... Restarting Gemini CLI to continue.
|
||||
history: historyManager.history,
|
||||
historyManager,
|
||||
isThemeDialogOpen,
|
||||
shouldShowRetentionWarning:
|
||||
shouldShowRetentionWarning && retentionCheckComplete,
|
||||
sessionsToDeleteCount: sessionsToDeleteCount ?? 0,
|
||||
|
||||
themeError,
|
||||
isAuthenticating,
|
||||
isConfigInitialized,
|
||||
@@ -2334,9 +2309,7 @@ Logging in with Google... Restarting Gemini CLI to continue.
|
||||
}),
|
||||
[
|
||||
isThemeDialogOpen,
|
||||
shouldShowRetentionWarning,
|
||||
retentionCheckComplete,
|
||||
sessionsToDeleteCount,
|
||||
|
||||
themeError,
|
||||
isAuthenticating,
|
||||
isConfigInitialized,
|
||||
|
||||
@@ -37,9 +37,6 @@ import { AdminSettingsChangedDialog } from './AdminSettingsChangedDialog.js';
|
||||
import { IdeTrustChangeDialog } from './IdeTrustChangeDialog.js';
|
||||
import { NewAgentsNotification } from './NewAgentsNotification.js';
|
||||
import { AgentConfigDialog } from './AgentConfigDialog.js';
|
||||
import { SessionRetentionWarningDialog } from './SessionRetentionWarningDialog.js';
|
||||
import { useCallback } from 'react';
|
||||
import { SettingScope } from '../../config/settings.js';
|
||||
import { PolicyUpdateDialog } from './PolicyUpdateDialog.js';
|
||||
|
||||
interface DialogManagerProps {
|
||||
@@ -62,56 +59,8 @@ export const DialogManager = ({
|
||||
terminalHeight,
|
||||
staticExtraHeight,
|
||||
terminalWidth: uiTerminalWidth,
|
||||
shouldShowRetentionWarning,
|
||||
sessionsToDeleteCount,
|
||||
} = uiState;
|
||||
|
||||
const handleKeep120Days = useCallback(() => {
|
||||
settings.setValue(
|
||||
SettingScope.User,
|
||||
'general.sessionRetention.warningAcknowledged',
|
||||
true,
|
||||
);
|
||||
settings.setValue(
|
||||
SettingScope.User,
|
||||
'general.sessionRetention.enabled',
|
||||
true,
|
||||
);
|
||||
settings.setValue(
|
||||
SettingScope.User,
|
||||
'general.sessionRetention.maxAge',
|
||||
'120d',
|
||||
);
|
||||
}, [settings]);
|
||||
|
||||
const handleKeep30Days = useCallback(() => {
|
||||
settings.setValue(
|
||||
SettingScope.User,
|
||||
'general.sessionRetention.warningAcknowledged',
|
||||
true,
|
||||
);
|
||||
settings.setValue(
|
||||
SettingScope.User,
|
||||
'general.sessionRetention.enabled',
|
||||
true,
|
||||
);
|
||||
settings.setValue(
|
||||
SettingScope.User,
|
||||
'general.sessionRetention.maxAge',
|
||||
'30d',
|
||||
);
|
||||
}, [settings]);
|
||||
|
||||
if (shouldShowRetentionWarning && sessionsToDeleteCount !== undefined) {
|
||||
return (
|
||||
<SessionRetentionWarningDialog
|
||||
onKeep120Days={handleKeep120Days}
|
||||
onKeep30Days={handleKeep30Days}
|
||||
sessionsToDeleteCount={sessionsToDeleteCount ?? 0}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
if (uiState.adminSettingsChanged) {
|
||||
return <AdminSettingsChangedDialog />;
|
||||
}
|
||||
|
||||
@@ -1,119 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2026 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* @license
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, afterEach } from 'vitest';
|
||||
import { renderWithProviders } from '../../test-utils/render.js';
|
||||
import { SessionRetentionWarningDialog } from './SessionRetentionWarningDialog.js';
|
||||
import { waitFor } from '../../test-utils/async.js';
|
||||
import { act } from 'react';
|
||||
|
||||
// Helper to write to stdin
|
||||
const writeKey = (stdin: { write: (data: string) => void }, key: string) => {
|
||||
act(() => {
|
||||
stdin.write(key);
|
||||
});
|
||||
};
|
||||
|
||||
describe('SessionRetentionWarningDialog', () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('renders correctly with warning message and session count', async () => {
|
||||
const { lastFrame, waitUntilReady } = renderWithProviders(
|
||||
<SessionRetentionWarningDialog
|
||||
onKeep120Days={vi.fn()}
|
||||
onKeep30Days={vi.fn()}
|
||||
sessionsToDeleteCount={42}
|
||||
/>,
|
||||
);
|
||||
await waitUntilReady();
|
||||
|
||||
expect(lastFrame()).toContain('Keep chat history');
|
||||
expect(lastFrame()).toContain(
|
||||
'introducing a limit on how long chat sessions are stored',
|
||||
);
|
||||
expect(lastFrame()).toContain('Keep for 30 days (Recommended)');
|
||||
expect(lastFrame()).toContain('42 sessions will be deleted');
|
||||
expect(lastFrame()).toContain('Keep for 120 days');
|
||||
expect(lastFrame()).toContain('No sessions will be deleted at this time');
|
||||
});
|
||||
|
||||
it('handles pluralization correctly for 1 session', async () => {
|
||||
const { lastFrame, waitUntilReady } = renderWithProviders(
|
||||
<SessionRetentionWarningDialog
|
||||
onKeep120Days={vi.fn()}
|
||||
onKeep30Days={vi.fn()}
|
||||
sessionsToDeleteCount={1}
|
||||
/>,
|
||||
);
|
||||
await waitUntilReady();
|
||||
|
||||
expect(lastFrame()).toContain('1 session will be deleted');
|
||||
});
|
||||
|
||||
it('defaults to "Keep for 120 days" when there are sessions to delete', async () => {
|
||||
const onKeep120Days = vi.fn();
|
||||
const onKeep30Days = vi.fn();
|
||||
|
||||
const { stdin, waitUntilReady } = renderWithProviders(
|
||||
<SessionRetentionWarningDialog
|
||||
onKeep120Days={onKeep120Days}
|
||||
onKeep30Days={onKeep30Days}
|
||||
sessionsToDeleteCount={10}
|
||||
/>,
|
||||
);
|
||||
await waitUntilReady();
|
||||
|
||||
// Initial selection should be "Keep for 120 days" (index 1) because count > 0
|
||||
// Pressing Enter immediately should select it.
|
||||
writeKey(stdin, '\r');
|
||||
|
||||
await waitFor(() => {
|
||||
expect(onKeep120Days).toHaveBeenCalled();
|
||||
expect(onKeep30Days).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('calls onKeep30Days when "Keep for 30 days" is explicitly selected (from 120 days default)', async () => {
|
||||
const onKeep120Days = vi.fn();
|
||||
const onKeep30Days = vi.fn();
|
||||
|
||||
const { stdin, waitUntilReady } = renderWithProviders(
|
||||
<SessionRetentionWarningDialog
|
||||
onKeep120Days={onKeep120Days}
|
||||
onKeep30Days={onKeep30Days}
|
||||
sessionsToDeleteCount={10}
|
||||
/>,
|
||||
);
|
||||
await waitUntilReady();
|
||||
|
||||
// Default is index 1 (120 days). Move UP to index 0 (30 days).
|
||||
writeKey(stdin, '\x1b[A'); // Up arrow
|
||||
writeKey(stdin, '\r');
|
||||
|
||||
await waitFor(() => {
|
||||
expect(onKeep30Days).toHaveBeenCalled();
|
||||
expect(onKeep120Days).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('should match snapshot', async () => {
|
||||
const { lastFrame, waitUntilReady } = renderWithProviders(
|
||||
<SessionRetentionWarningDialog
|
||||
onKeep120Days={vi.fn()}
|
||||
onKeep30Days={vi.fn()}
|
||||
sessionsToDeleteCount={123}
|
||||
/>,
|
||||
);
|
||||
await waitUntilReady();
|
||||
|
||||
// Initial render
|
||||
expect(lastFrame()).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
@@ -1,78 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2026 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { Box, Text } from 'ink';
|
||||
import { theme } from '../semantic-colors.js';
|
||||
import {
|
||||
RadioButtonSelect,
|
||||
type RadioSelectItem,
|
||||
} from './shared/RadioButtonSelect.js';
|
||||
|
||||
interface SessionRetentionWarningDialogProps {
|
||||
onKeep120Days: () => void;
|
||||
onKeep30Days: () => void;
|
||||
sessionsToDeleteCount: number;
|
||||
}
|
||||
|
||||
export const SessionRetentionWarningDialog = ({
|
||||
onKeep120Days,
|
||||
onKeep30Days,
|
||||
sessionsToDeleteCount,
|
||||
}: SessionRetentionWarningDialogProps) => {
|
||||
const options: Array<RadioSelectItem<() => void>> = [
|
||||
{
|
||||
label: 'Keep for 30 days (Recommended)',
|
||||
value: onKeep30Days,
|
||||
key: '30days',
|
||||
sublabel: `${sessionsToDeleteCount} session${
|
||||
sessionsToDeleteCount === 1 ? '' : 's'
|
||||
} will be deleted`,
|
||||
},
|
||||
{
|
||||
label: 'Keep for 120 days',
|
||||
value: onKeep120Days,
|
||||
key: '120days',
|
||||
sublabel: 'No sessions will be deleted at this time',
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<Box
|
||||
borderStyle="round"
|
||||
borderColor={theme.border.default}
|
||||
flexDirection="column"
|
||||
width="100%"
|
||||
padding={1}
|
||||
>
|
||||
<Box marginBottom={1} justifyContent="center" width="100%">
|
||||
<Text bold>Keep chat history</Text>
|
||||
</Box>
|
||||
|
||||
<Box flexDirection="column" gap={1} marginBottom={1}>
|
||||
<Text>
|
||||
To keep your workspace clean, we are introducing a limit on how long
|
||||
chat sessions are stored. Please choose a retention period for your
|
||||
existing chats:
|
||||
</Text>
|
||||
</Box>
|
||||
|
||||
<Box marginTop={1}>
|
||||
<RadioButtonSelect
|
||||
items={options}
|
||||
onSelect={(action) => action()}
|
||||
initialIndex={1}
|
||||
/>
|
||||
</Box>
|
||||
|
||||
<Box marginTop={1}>
|
||||
<Text color={theme.text.secondary}>
|
||||
Set a custom limit <Text color={theme.text.primary}>/settings</Text>{' '}
|
||||
and change "Keep chat history".
|
||||
</Text>
|
||||
</Box>
|
||||
</Box>
|
||||
);
|
||||
};
|
||||
@@ -1,21 +0,0 @@
|
||||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
||||
|
||||
exports[`SessionRetentionWarningDialog > should match snapshot 1`] = `
|
||||
"╭──────────────────────────────────────────────────────────────────────────────────────────────────╮
|
||||
│ │
|
||||
│ Keep chat history │
|
||||
│ │
|
||||
│ To keep your workspace clean, we are introducing a limit on how long chat sessions are stored. │
|
||||
│ Please choose a retention period for your existing chats: │
|
||||
│ │
|
||||
│ │
|
||||
│ 1. Keep for 30 days (Recommended) │
|
||||
│ 123 sessions will be deleted │
|
||||
│ ● 2. Keep for 120 days │
|
||||
│ No sessions will be deleted at this time │
|
||||
│ │
|
||||
│ Set a custom limit /settings and change "Keep chat history". │
|
||||
│ │
|
||||
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
|
||||
"
|
||||
`;
|
||||
@@ -107,8 +107,6 @@ export interface UIState {
|
||||
history: HistoryItem[];
|
||||
historyManager: UseHistoryManagerReturn;
|
||||
isThemeDialogOpen: boolean;
|
||||
shouldShowRetentionWarning: boolean;
|
||||
sessionsToDeleteCount: number;
|
||||
themeError: string | null;
|
||||
isAuthenticating: boolean;
|
||||
isConfigInitialized: boolean;
|
||||
|
||||
@@ -1,217 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2026 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { renderHook } from '../../test-utils/render.js';
|
||||
import { useSessionRetentionCheck } from './useSessionRetentionCheck.js';
|
||||
import { type Config } from '@google/gemini-cli-core';
|
||||
import type { Settings } from '../../config/settingsSchema.js';
|
||||
import { waitFor } from '../../test-utils/async.js';
|
||||
|
||||
// Mock utils
|
||||
const mockGetAllSessionFiles = vi.fn();
|
||||
const mockIdentifySessionsToDelete = vi.fn();
|
||||
|
||||
vi.mock('../../utils/sessionUtils.js', () => ({
|
||||
getAllSessionFiles: () => mockGetAllSessionFiles(),
|
||||
}));
|
||||
|
||||
vi.mock('../../utils/sessionCleanup.js', () => ({
|
||||
identifySessionsToDelete: () => mockIdentifySessionsToDelete(),
|
||||
DEFAULT_MIN_RETENTION: '30d',
|
||||
}));
|
||||
|
||||
describe('useSessionRetentionCheck', () => {
|
||||
const mockConfig = {
|
||||
storage: {
|
||||
getProjectTempDir: () => '/mock/project/temp/dir',
|
||||
},
|
||||
getSessionId: () => 'mock-session-id',
|
||||
} as unknown as Config;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('should show warning if enabled is true but maxAge is undefined', async () => {
|
||||
const settings = {
|
||||
general: {
|
||||
sessionRetention: {
|
||||
enabled: true,
|
||||
maxAge: undefined,
|
||||
warningAcknowledged: false,
|
||||
},
|
||||
},
|
||||
} as unknown as Settings;
|
||||
|
||||
mockGetAllSessionFiles.mockResolvedValue(['session1.json']);
|
||||
mockIdentifySessionsToDelete.mockResolvedValue(['session1.json']);
|
||||
|
||||
const { result } = renderHook(() =>
|
||||
useSessionRetentionCheck(mockConfig, settings),
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.checkComplete).toBe(true);
|
||||
expect(result.current.shouldShowWarning).toBe(true);
|
||||
expect(mockGetAllSessionFiles).toHaveBeenCalled();
|
||||
expect(mockIdentifySessionsToDelete).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('should not show warning if warningAcknowledged is true', async () => {
|
||||
const settings = {
|
||||
general: {
|
||||
sessionRetention: {
|
||||
warningAcknowledged: true,
|
||||
},
|
||||
},
|
||||
} as unknown as Settings;
|
||||
|
||||
const { result } = renderHook(() =>
|
||||
useSessionRetentionCheck(mockConfig, settings),
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.checkComplete).toBe(true);
|
||||
expect(result.current.shouldShowWarning).toBe(false);
|
||||
expect(mockGetAllSessionFiles).not.toHaveBeenCalled();
|
||||
expect(mockIdentifySessionsToDelete).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('should not show warning if retention is already enabled', async () => {
|
||||
const settings = {
|
||||
general: {
|
||||
sessionRetention: {
|
||||
enabled: true,
|
||||
maxAge: '30d', // Explicitly enabled with non-default
|
||||
},
|
||||
},
|
||||
} as unknown as Settings;
|
||||
|
||||
const { result } = renderHook(() =>
|
||||
useSessionRetentionCheck(mockConfig, settings),
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.checkComplete).toBe(true);
|
||||
expect(result.current.shouldShowWarning).toBe(false);
|
||||
expect(mockGetAllSessionFiles).not.toHaveBeenCalled();
|
||||
expect(mockIdentifySessionsToDelete).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('should show warning if sessions to delete exist', async () => {
|
||||
const settings = {
|
||||
general: {
|
||||
sessionRetention: {
|
||||
enabled: false,
|
||||
warningAcknowledged: false,
|
||||
},
|
||||
},
|
||||
} as unknown as Settings;
|
||||
|
||||
mockGetAllSessionFiles.mockResolvedValue([
|
||||
'session1.json',
|
||||
'session2.json',
|
||||
]);
|
||||
mockIdentifySessionsToDelete.mockResolvedValue(['session1.json']); // 1 session to delete
|
||||
|
||||
const { result } = renderHook(() =>
|
||||
useSessionRetentionCheck(mockConfig, settings),
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.checkComplete).toBe(true);
|
||||
expect(result.current.shouldShowWarning).toBe(true);
|
||||
expect(result.current.sessionsToDeleteCount).toBe(1);
|
||||
expect(mockGetAllSessionFiles).toHaveBeenCalled();
|
||||
expect(mockIdentifySessionsToDelete).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('should call onAutoEnable if no sessions to delete and currently disabled', async () => {
|
||||
const settings = {
|
||||
general: {
|
||||
sessionRetention: {
|
||||
enabled: false,
|
||||
warningAcknowledged: false,
|
||||
},
|
||||
},
|
||||
} as unknown as Settings;
|
||||
|
||||
mockGetAllSessionFiles.mockResolvedValue(['session1.json']);
|
||||
mockIdentifySessionsToDelete.mockResolvedValue([]); // 0 sessions to delete
|
||||
|
||||
const onAutoEnable = vi.fn();
|
||||
|
||||
const { result } = renderHook(() =>
|
||||
useSessionRetentionCheck(mockConfig, settings, onAutoEnable),
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.checkComplete).toBe(true);
|
||||
expect(result.current.shouldShowWarning).toBe(false);
|
||||
expect(onAutoEnable).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('should not show warning if no sessions to delete', async () => {
|
||||
const settings = {
|
||||
general: {
|
||||
sessionRetention: {
|
||||
enabled: false,
|
||||
warningAcknowledged: false,
|
||||
},
|
||||
},
|
||||
} as unknown as Settings;
|
||||
|
||||
mockGetAllSessionFiles.mockResolvedValue([
|
||||
'session1.json',
|
||||
'session2.json',
|
||||
]);
|
||||
mockIdentifySessionsToDelete.mockResolvedValue([]); // 0 sessions to delete
|
||||
|
||||
const { result } = renderHook(() =>
|
||||
useSessionRetentionCheck(mockConfig, settings),
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.checkComplete).toBe(true);
|
||||
expect(result.current.shouldShowWarning).toBe(false);
|
||||
expect(result.current.sessionsToDeleteCount).toBe(0);
|
||||
expect(mockGetAllSessionFiles).toHaveBeenCalled();
|
||||
expect(mockIdentifySessionsToDelete).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle errors gracefully (assume no warning)', async () => {
|
||||
const settings = {
|
||||
general: {
|
||||
sessionRetention: {
|
||||
enabled: false,
|
||||
warningAcknowledged: false,
|
||||
},
|
||||
},
|
||||
} as unknown as Settings;
|
||||
|
||||
mockGetAllSessionFiles.mockRejectedValue(new Error('FS Error'));
|
||||
|
||||
const { result } = renderHook(() =>
|
||||
useSessionRetentionCheck(mockConfig, settings),
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.checkComplete).toBe(true);
|
||||
expect(result.current.shouldShowWarning).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,70 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2026 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { useState, useEffect } from 'react';
|
||||
import { type Config } from '@google/gemini-cli-core';
|
||||
import { type Settings } from '../../config/settings.js';
|
||||
import { getAllSessionFiles } from '../../utils/sessionUtils.js';
|
||||
import { identifySessionsToDelete } from '../../utils/sessionCleanup.js';
|
||||
import path from 'node:path';
|
||||
|
||||
export function useSessionRetentionCheck(
|
||||
config: Config,
|
||||
settings: Settings,
|
||||
onAutoEnable?: () => void,
|
||||
) {
|
||||
const [shouldShowWarning, setShouldShowWarning] = useState(false);
|
||||
const [sessionsToDeleteCount, setSessionsToDeleteCount] = useState(0);
|
||||
const [checkComplete, setCheckComplete] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
// If warning already acknowledged or retention already enabled, skip check
|
||||
if (
|
||||
settings.general?.sessionRetention?.warningAcknowledged ||
|
||||
(settings.general?.sessionRetention?.enabled &&
|
||||
settings.general?.sessionRetention?.maxAge !== undefined)
|
||||
) {
|
||||
setShouldShowWarning(false);
|
||||
setCheckComplete(true);
|
||||
return;
|
||||
}
|
||||
|
||||
const checkSessions = async () => {
|
||||
try {
|
||||
const chatsDir = path.join(config.storage.getProjectTempDir(), 'chats');
|
||||
const allFiles = await getAllSessionFiles(
|
||||
chatsDir,
|
||||
config.getSessionId(),
|
||||
);
|
||||
|
||||
// Calculate how many sessions would be deleted if we applied a 30-day retention
|
||||
const sessionsToDelete = await identifySessionsToDelete(allFiles, {
|
||||
enabled: true,
|
||||
maxAge: '30d',
|
||||
});
|
||||
|
||||
if (sessionsToDelete.length > 0) {
|
||||
setSessionsToDeleteCount(sessionsToDelete.length);
|
||||
setShouldShowWarning(true);
|
||||
} else {
|
||||
setShouldShowWarning(false);
|
||||
// If no sessions to delete, safe to auto-enable retention
|
||||
onAutoEnable?.();
|
||||
}
|
||||
} catch {
|
||||
// If we can't check sessions, default to not showing the warning to be safe
|
||||
setShouldShowWarning(false);
|
||||
} finally {
|
||||
setCheckComplete(true);
|
||||
}
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
checkSessions();
|
||||
}, [config, settings.general?.sessionRetention, onAutoEnable]);
|
||||
|
||||
return { shouldShowWarning, checkComplete, sessionsToDeleteCount };
|
||||
}
|
||||
Reference in New Issue
Block a user