common/parser: fix nasty bug causing subtle corruption of generation prompt (#20825)

This commit is contained in:
Piotr Wilkin (ilintar)
2026-03-21 00:19:04 +01:00
committed by GitHub
parent e6ec21e62f
commit b1c70e2e54
4 changed files with 52 additions and 1 deletions
+15
View File
@@ -188,6 +188,21 @@ diff_split calculate_diff_split(const std::string & left, const std::string & ri
result.suffix = "";
// pick prefix = all as representation
}
// When left has no unique content (result.left is empty), left is entirely
// shared with right. The simultaneous prefix/suffix segment matching can
// incorrectly consume trailing segments of left as suffix when those same
// segments also appear at the end of right (e.g. "\n" at the end of both
// the shared content and the generation prompt). This rotates the diff.
// Fix: if left is a prefix of right, enforce that directly.
if (result.left.empty() && !result.right.empty() &&
left.size() <= right.size() &&
right.substr(0, left.size()) == left) {
result.prefix = left;
result.suffix = "";
result.right = right.substr(left.size());
}
return result;
}