feat(admin): enable 30 day default retention for chat history & remove warning (#20853)

This commit is contained in:
Shreya Keshive
2026-03-02 17:44:49 -05:00
committed by GitHub
parent 3f7ef816f1
commit 06ddfa5c4c
14 changed files with 16 additions and 623 deletions

View File

@@ -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);
});
});
});

View File

@@ -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 };
}