Skip to content
Open
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
43 changes: 43 additions & 0 deletions packages/core/src/utils/checkpointUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,32 @@ describe('checkpoint utils', () => {
expect(fileContent.messageId).toBe('p1');
});

it('keeps the .json in the checkpoint name when editing a .json file', async () => {
const toolCalls = [
{
callId: '1',
name: 'replace',
args: { file_path: 'config.json' },
prompt_id: 'p1',
isClientInitiated: false,
},
] as ToolCallRequestInfo[];

(mockGitService.createFileSnapshot as Mock).mockResolvedValue('hash123');
(mockGeminiClient.getHistory as Mock).mockReturnValue([]);

const { toolCallToCheckpointMap } = await processRestorableToolCalls(
toolCalls,
mockGitService,
mockGeminiClient,
'history-data',
);

expect(toolCallToCheckpointMap.get('1')).toMatch(
/-config\.json-replace$/,
);
});

it('should handle git snapshot failure by using current commit hash', async () => {
const toolCalls = [
{
Expand Down Expand Up @@ -280,6 +306,23 @@ describe('checkpoint utils', () => {
expect(actual).toEqual(expected);
});

it('strips only the trailing .json when the edited file is itself .json', () => {
const checkpointFiles = new Map([
[
'2025-01-01T12-00-00_000Z-config.json-replace.json',
JSON.stringify({ messageId: 'msg1' }),
],
]);

const actual = getCheckpointInfoList(checkpointFiles);
expect(actual).toEqual([
{
messageId: 'msg1',
checkpoint: '2025-01-01T12-00-00_000Z-config.json-replace',
},
]);
});

it('should ignore files with invalid JSON', () => {
const checkpointFiles = new Map([
['checkpoint1.json', JSON.stringify({ messageId: 'msg1' })],
Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/utils/checkpointUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ export async function processRestorableToolCalls<HistoryType>(
checkpointsToWrite.set(fileName, JSON.stringify(checkpointData, null, 2));
toolCallToCheckpointMap.set(
toolCall.callId,
fileName.replace('.json', ''),
// Use the base name directly; fileName.replace('.json', '') would strip
// the first ".json" instead of the extension when the edited file is
// itself a .json file (e.g. "...-config.json-replace.json").
checkpointFileName,
);
} catch (error) {
errors.push(
Expand Down Expand Up @@ -176,7 +179,10 @@ export function getCheckpointInfoList(
if (result.success) {
checkpointInfoList.push({
messageId: result.data.messageId,
checkpoint: file.replace('.json', ''),
// basename(file, '.json') strips only the trailing extension; a
// plain replace('.json', '') would corrupt names from edited .json
// files (e.g. "...-config.json-replace.json").
checkpoint: path.basename(file, '.json'),
Comment on lines +182 to +185

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Using path.basename(file, '.json') will strip any directory components from the path (e.g., subdir/file.json becomes file). If the checkpoint files contain directory paths, this will corrupt the checkpoint name. Additionally, path.basename is platform-dependent and may behave inconsistently on Windows vs. POSIX environments depending on the path separators used.

Using a regular expression like file.replace(/\.json$/, '') is safer as it only strips the trailing .json extension while preserving any directory structure and avoiding platform-specific path issues.

Suggested change
// basename(file, '.json') strips only the trailing extension; a
// plain replace('.json', '') would corrupt names from edited .json
// files (e.g. "...-config.json-replace.json").
checkpoint: path.basename(file, '.json'),
// Strip only the trailing .json extension while preserving any directory structure.
checkpoint: file.replace(/\.json$/, ''),

});
}
} catch {
Expand Down