fix(core): ensure chat compression summary persists correctly without data loss

- Modified ChatRecordingService.initialize to accept an explicit overwriteHistory flag.
- When overwriteHistory is true (e.g., after chat compression), it overwrites disk messages; otherwise, it preserves existing historical metadata (IDs/timestamps).
- Updated GeminiChat and GeminiClient.startChat to propagate this flag, ensuring it is only true during the compression flow.
- Refactored apiContentToMessageRecords for full type safety by removing unsafe type assertions and adding part fallbacks, as suggested in PR review.
- Updated unit tests in chatRecordingService.test.ts and client.test.ts to verify the new behavior and fix regression.
- Verified all workspace and integration tests pass via preflight.

Fixes #21335
This commit is contained in:
Abhijit Balaji
2026-03-05 17:13:34 -08:00
parent 663048f2f8
commit 6f4382901e
5 changed files with 65 additions and 7 deletions
@@ -123,7 +123,7 @@ describe('ChatRecordingService', () => {
expect(conversation.sessionId).toBe('old-session-id');
});
it('should overwrite existing messages if initialHistory is provided', () => {
it('should overwrite existing messages if overwriteHistory is true', () => {
const chatsDir = path.join(testTempDir, 'chats');
fs.mkdirSync(chatsDir, { recursive: true });
const sessionFile = path.join(chatsDir, 'session-overwrite.json');
@@ -155,6 +155,7 @@ describe('ChatRecordingService', () => {
},
'main',
newHistory,
true, // overwriteHistory = true
);
const conversation = JSON.parse(
@@ -166,6 +167,49 @@ describe('ChatRecordingService', () => {
]);
expect(conversation.messages[0].type).toBe('user');
});
it('should NOT overwrite existing messages if overwriteHistory is false', () => {
const chatsDir = path.join(testTempDir, 'chats');
fs.mkdirSync(chatsDir, { recursive: true });
const sessionFile = path.join(chatsDir, 'session-no-overwrite.json');
const initialData = {
sessionId: 'test-session-id',
projectHash: 'test-project-hash',
messages: [
{
id: 'msg-1',
type: 'user',
content: 'Old Message',
timestamp: new Date().toISOString(),
},
],
};
fs.writeFileSync(sessionFile, JSON.stringify(initialData));
const newHistory: Content[] = [
{
role: 'user',
parts: [{ text: 'Some New Context' }],
},
];
chatRecordingService.initialize(
{
filePath: sessionFile,
conversation: initialData as ConversationRecord,
},
'main',
newHistory,
false, // overwriteHistory = false
);
const conversation = JSON.parse(
fs.readFileSync(sessionFile, 'utf8'),
) as ConversationRecord;
expect(conversation.messages).toHaveLength(1);
expect(conversation.messages[0].content).toBe('Old Message');
expect(conversation.messages[0].id).toBe('msg-1');
});
});
describe('recordMessage', () => {