Skip to content

Commit 302b83e

Browse files
committed
Adds hunk map validation for generateRebase
1 parent 72194de commit 302b83e

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/plus/ai/aiProviderService.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,8 +1024,35 @@ export class AIProviderService implements Disposable {
10241024
const content = rq.content.replace(/^\s*```json\s*/, '').replace(/\s*```$/, '');
10251025
// Parse the JSON content from the result
10261026
result.commits = JSON.parse(content) as AIRebaseResult['commits'];
1027-
} catch {
1027+
1028+
const inputHunkIndices = result.hunkMap.map(h => h.index);
1029+
const outputHunkIndices = new Set(result.commits.flatMap(c => c.hunks.map(h => h.hunk)));
1030+
1031+
// Find any missing or extra hunks
1032+
const missingHunks = inputHunkIndices.filter(i => !outputHunkIndices.has(i));
1033+
const extraHunks = [...outputHunkIndices].filter(i => !inputHunkIndices.includes(i));
1034+
if (missingHunks.length > 0 || extraHunks.length > 0) {
1035+
let hunksMessage = '';
1036+
if (missingHunks.length > 0) {
1037+
const pluralize = missingHunks.length > 1 ? 's' : '';
1038+
hunksMessage += ` ${missingHunks.length} missing hunk${pluralize}.`;
1039+
}
1040+
if (extraHunks.length > 0) {
1041+
const pluralize = extraHunks.length > 1 ? 's' : '';
1042+
hunksMessage += ` ${extraHunks.length} extra hunk${pluralize}.`;
1043+
}
1044+
1045+
throw new Error(
1046+
`Invalid response in generating ${
1047+
options?.generateCommits ? 'commits' : 'rebase'
1048+
} result.${hunksMessage} Try again or select a different AI model.`,
1049+
);
1050+
}
1051+
} catch (ex) {
10281052
debugger;
1053+
if (ex?.message?.includes('Invalid response in generating')) {
1054+
throw ex;
1055+
}
10291056
throw new Error(`Unable to parse ${options?.generateCommits ? 'commits' : 'rebase'} result`);
10301057
}
10311058

0 commit comments

Comments
 (0)