Skip to content

Non-invasive MCP accelerator plugin for Google Antigravity (CLI/Desktop/IDE): cuts LLM turns (-34%), token quota (-31%), and upload traffic (-40%) without affecting native UI or diffs.

fix(apply-edits): reject ambiguous self-overlapping targets consistently

acrot0bfafdbdf73ca
diff --git a/dist/mcp-server.mjs b/dist/mcp-server.mjs
index 0f2e711..f8d3a00 100755
--- a/dist/mcp-server.mjs
+++ b/dist/mcp-server.mjs
@@ -2893,6 +2893,11 @@ function findClosestRegionSnippet(normalizedContent, normTarget, rangeStartLine,
Closest match near line ${bestStart + 1} (lines ${from}-${to}):
${snippet.join("\n")}`;
}
+function hasOverlappingMatch(content, target, offset, limitEnd) {
+ if (target.length <= 1) return false;
+ const next = content.indexOf(target, offset + 1);
+ return next !== -1 && next + target.length <= limitEnd;
+}
function planHunksForContent(normalizedContent, hunks, displayPath) {
const lineStarts = computeLineStarts(normalizedContent);
if (normalizedContent.length > 0 && lineStarts.at(-1) === normalizedContent.length) {
@@ -2956,7 +2961,13 @@ function planHunksForContent(normalizedContent, hunks, displayPath) {
gPos = found + Math.max(1, normTarget.length);
}
if (globalOffsets.length > 0) {
- if (!hunk.replaceAll && globalOffsets.length === 1) {
+ const hasSingleUnambiguousGlobalMatch = globalOffsets.length === 1 && !hasOverlappingMatch(
+ normalizedContent,
+ normTarget,
+ globalOffsets[0],
+ normalizedContent.length
+ );
+ if (!hunk.replaceAll && hasSingleUnambiguousGlobalMatch) {
const uniqueOffset = globalOffsets[0];
const matchStartLine = offsetToLine(lineStarts, uniqueOffset);
const matchEndOffset = normTarget.length > 0 ? uniqueOffset + normTarget.length - 1 : uniqueOffset;
@@ -2983,13 +2994,13 @@ function planHunksForContent(normalizedContent, hunks, displayPath) {
}
const hitLines = globalOffsets.map((o) => formatMatchLineRange(lineStarts, o, normTarget.length)).join(", ");
let action = "choose bounds covering the intended matches.";
- if (globalOffsets.length === 1 && !hunk.replaceAll) {
+ if (hasSingleUnambiguousGlobalMatch && !hunk.replaceAll) {
const start = globalOffsets[0];
const firstLine = offsetToLine(lineStarts, start);
const lastLine = offsetToLine(lineStarts, start + normTarget.length - 1);
action = `use startLine: ${firstLine}, endLine: ${lastLine} or omit both bounds.`;
} else if (!hunk.replaceAll) {
- action = "narrow the bounds to one match or include more surrounding text.";
+ action = globalOffsets.length === 1 ? "include more surrounding text to disambiguate overlapping matches." : "narrow the bounds to one match or include more surrounding text.";
}
hint = ` Exact match found outside range at ${hitLines} \u2014 check 'path', then ${action}`;
} else {
@@ -3018,11 +3029,22 @@ function planHunksForContent(normalizedContent, hunks, displayPath) {
);
continue;
}
- if (foundOffsets.length > 1 && !hunk.replaceAll) {
+ if (!hunk.replaceAll && (foundOffsets.length > 1 || foundOffsets.length === 1 && hasOverlappingMatch(
+ normalizedContent,
+ normTarget,
+ foundOffsets[0],
+ windowEnd
+ ))) {
const lines = foundOffsets.slice(0, 10).map((o) => formatMatchLineRange(lineStarts, o, normTarget.length, "")).join(", ");
- errors.push(
- `${displayPath} hunk #${idx + 1}: target matched ${foundOffsets.length} times (lines: ${lines}). Narrow the line bounds or include more surrounding text. Set replaceAll: true only if every match in scope should change.`
- );
+ if (foundOffsets.length === 1) {
+ errors.push(
+ `${displayPath} hunk #${idx + 1}: target has overlapping matches (line: ${lines}). Include more surrounding text to make the match unambiguous. Set replaceAll: true only if non-overlapping matches in scope should all change.`
+ );
+ } else {
+ errors.push(
+ `${displayPath} hunk #${idx + 1}: target matched ${foundOffsets.length} times (lines: ${lines}). Narrow the line bounds or include more surrounding text. Set replaceAll: true only if every match in scope should change.`
+ );
+ }
continue;
}
for (const start of foundOffsets) {
diff --git a/src/tools/apply-edits.ts b/src/tools/apply-edits.ts
index 30cd3f3..bfbb61e 100644
--- a/src/tools/apply-edits.ts
+++ b/src/tools/apply-edits.ts
@@ -367,6 +367,17 @@ function findClosestRegionSnippet(
return `\n Closest match near line ${bestStart + 1} (lines ${from}-${to}):\n${snippet.join("\n")}`;
}
+function hasOverlappingMatch(
+ content: string,
+ target: string,
+ offset: number,
+ limitEnd: number,
+): boolean {
+ if (target.length <= 1) return false;
+ const next = content.indexOf(target, offset + 1);
+ return next !== -1 && next + target.length <= limitEnd;
+}
+
function planHunksForContent(
normalizedContent: string,
hunks: EditHunk[],
@@ -456,10 +467,18 @@ function planHunksForContent(
gPos = found + Math.max(1, normTarget.length);
}
if (globalOffsets.length > 0) {
+ const hasSingleUnambiguousGlobalMatch =
+ globalOffsets.length === 1 &&
+ !hasOverlappingMatch(
+ normalizedContent,
+ normTarget,
+ globalOffsets[0]!,
+ normalizedContent.length,
+ );
// Auto-recover when the target has a single globally unique exact match within a
// tight line-drift margin (and replaceAll is not requested), eliminating wasted
// retry turns caused by minor line shifts from earlier edits.
- if (!hunk.replaceAll && globalOffsets.length === 1) {
+ if (!hunk.replaceAll && hasSingleUnambiguousGlobalMatch) {
const uniqueOffset = globalOffsets[0]!;
const matchStartLine = offsetToLine(lineStarts, uniqueOffset);
const matchEndOffset =
@@ -503,13 +522,16 @@ function planHunksForContent(
.map((o) => formatMatchLineRange(lineStarts, o, normTarget.length))
.join(", ");
let action = "choose bounds covering the intended matches.";
- if (globalOffsets.length === 1 && !hunk.replaceAll) {
+ if (hasSingleUnambiguousGlobalMatch && !hunk.replaceAll) {
const start = globalOffsets[0]!;
const firstLine = offsetToLine(lineStarts, start);
const lastLine = offsetToLine(lineStarts, start + normTarget.length - 1);
action = `use startLine: ${firstLine}, endLine: ${lastLine} or omit both bounds.`;
} else if (!hunk.replaceAll) {
- action = "narrow the bounds to one match or include more surrounding text.";
+ action =
+ globalOffsets.length === 1
+ ? "include more surrounding text to disambiguate overlapping matches."
+ : "narrow the bounds to one match or include more surrounding text.";
}
hint = ` Exact match found outside range at ${hitLines} — check 'path', then ${action}`;
} else {
@@ -546,14 +568,30 @@ function planHunksForContent(
continue;
}
- if (foundOffsets.length > 1 && !hunk.replaceAll) {
+ if (
+ !hunk.replaceAll &&
+ (foundOffsets.length > 1 ||
+ (foundOffsets.length === 1 &&
+ hasOverlappingMatch(
+ normalizedContent,
+ normTarget,
+ foundOffsets[0]!,
+ windowEnd,
+ )))
+ ) {
const lines = foundOffsets
.slice(0, 10)
.map((o) => formatMatchLineRange(lineStarts, o, normTarget.length, ""))
.join(", ");
- errors.push(
- `${displayPath} hunk #${idx + 1}: target matched ${foundOffsets.length} times (lines: ${lines}). Narrow the line bounds or include more surrounding text. Set replaceAll: true only if every match in scope should change.`,
- );
+ if (foundOffsets.length === 1) {
+ errors.push(
+ `${displayPath} hunk #${idx + 1}: target has overlapping matches (line: ${lines}). Include more surrounding text to make the match unambiguous. Set replaceAll: true only if non-overlapping matches in scope should all change.`,
+ );
+ } else {
+ errors.push(
+ `${displayPath} hunk #${idx + 1}: target matched ${foundOffsets.length} times (lines: ${lines}). Narrow the line bounds or include more surrounding text. Set replaceAll: true only if every match in scope should change.`,
+ );
+ }
continue;
}
diff --git a/test/apply-edits.test.ts b/test/apply-edits.test.ts
index eb4e8f4..d3aea25 100644
--- a/test/apply-edits.test.ts
+++ b/test/apply-edits.test.ts
@@ -2695,6 +2695,64 @@ describe("apply_edits", () => {
assert.match(invertedRes.text, /Invalid line range: startLine \(50\) > endLine \(40\)/);
assert.equal(await fs.readFile(shortFile, "utf8"), "const s1 = 1;\nconst s2 = 2;\nconst s3 = 3;\n");
+ // 6b. Overlapping self-matches (e.g. "aa" in "aaa") must be rejected consistently across
+ // drift recovery, bounded direct matching, and unbounded direct matching (without emitting
+ // a contradictory retry hint), while replaceAll: true still applies deterministically.
+ const selfOverlapFile = path.join(driftDir, "self-overlap.txt");
+ await fs.writeFile(selfOverlapFile, "// hdr\n// sub\naaa\n", "utf8");
+
+ const driftOverlapRes = await runApplyEdits({
+ cwd: driftDir,
+ edits: [
+ {
+ path: "self-overlap.txt",
+ hunks: [{ startLine: 1, endLine: 2, target: "aa", replacement: "bb" }],
+ },
+ ],
+ });
+ assert.equal(driftOverlapRes.isError, true);
+ assert.match(driftOverlapRes.text, /include more surrounding text to disambiguate overlapping matches/i);
+ assert.doesNotMatch(driftOverlapRes.text, /use startLine: 3, endLine: 3/);
+ assert.equal(await fs.readFile(selfOverlapFile, "utf8"), "// hdr\n// sub\naaa\n");
+
+ const boundedOverlapRes = await runApplyEdits({
+ cwd: driftDir,
+ edits: [
+ {
+ path: "self-overlap.txt",
+ hunks: [{ startLine: 3, endLine: 3, target: "aa", replacement: "bb" }],
+ },
+ ],
+ });
+ assert.equal(boundedOverlapRes.isError, true);
+ assert.match(boundedOverlapRes.text, /overlapping matches/i);
+ assert.equal(await fs.readFile(selfOverlapFile, "utf8"), "// hdr\n// sub\naaa\n");
+
+ const unboundedOverlapRes = await runApplyEdits({
+ cwd: driftDir,
+ edits: [
+ {
+ path: "self-overlap.txt",
+ hunks: [{ target: "aa", replacement: "bb" }],
+ },
+ ],
+ });
+ assert.equal(unboundedOverlapRes.isError, true);
+ assert.match(unboundedOverlapRes.text, /overlapping matches/i);
+ assert.equal(await fs.readFile(selfOverlapFile, "utf8"), "// hdr\n// sub\naaa\n");
+
+ const replaceAllOverlapRes = await runApplyEdits({
+ cwd: driftDir,
+ edits: [
+ {
+ path: "self-overlap.txt",
+ hunks: [{ target: "aa", replacement: "bb", replaceAll: true }],
+ },
+ ],
+ });
+ assert.equal(replaceAllOverlapRes.isError, false, replaceAllOverlapRes.text);
+ assert.equal(await fs.readFile(selfOverlapFile, "utf8"), "// hdr\n// sub\nbba\n");
+
// 7. Omitted endLine with startLine > totalLines recovers cleanly with non-inverted L30+ label
const openEndedRes = await runApplyEdits({
cwd: driftDir,