Mintplex-Labs/anything-llm · error · Error

Could not find exact match for edit:\n${edit.oldText}

Error message

Could not find exact match for edit:\n${edit.oldText}

What it means

Thrown by applyFileEdits() after it tries two matching strategies for edit.oldText and finds neither: first an exact substring match on normalized content, then a whitespace-insensitive line-by-line match (comparing trimmed lines). If both fail, the edit cannot be applied and this error reports the unmatched oldText.

Source

Thrown at server/utils/agents/aibitat/plugins/filesystem/lib.js:587

              const relativeIndent = newIndent.length - oldIndent.length;
              return (
                originalIndent +
                " ".repeat(Math.max(0, relativeIndent)) +
                line.trimStart()
              );
            }
            return line;
          });

          contentLines.splice(i, oldLines.length, ...newLines);
          modifiedContent = contentLines.join("\n");
          matchFound = true;
          break;
        }
      }

      if (!matchFound) {
        throw new Error(
          `Could not find exact match for edit:\n${edit.oldText}`
        );
      }
    }

    const diffResult = this.#createUnifiedDiff(
      content,
      modifiedContent,
      filePath
    );

    let numBackticks = 3;
    while (diffResult.includes("`".repeat(numBackticks))) {
      numBackticks++;
    }
    const formattedDiff = `${"`".repeat(numBackticks)}diff\n${diffResult}${"`".repeat(numBackticks)}\n\n`;

    if (!dryRun) {

View on GitHub (pinned to 526360e320)

Solutions

  1. Re-read the current file content and rebuild oldText from the actual current text.
  2. Use a smaller, uniquely identifiable snippet of oldText that is certain to be present.
  3. Confirm the edit targets the correct file path.
  4. Normalize line endings and check for invisible/whitespace differences.

Example fix

// before - oldText no longer matches (file changed)
edit.oldText = "function oldName() {";
// after - re-read the file and copy the exact current text
edit.oldText = "function currentName() {";
Defensive patterns

Strategy: validation

Validate before calling

// verify oldText is present in the current file before applying edits
const content = await fs.readFile(filePath, "utf-8");
for (const edit of edits) {
  if (!content.includes(edit.oldText))
    throw new Error(`oldText not found in ${filePath}; re-read and update the edit`);
}

Try / catch

try {
  await filesystem.applyFileEdits(filePath, edits, dryRun);
} catch (e) {
  if (e.message.startsWith("Could not find exact match")) {
    // re-read file, rebuild edits from current content, retry
  } else throw e;
}

Prevention

When it happens

Trigger: edit.oldText does not appear in the file content - either the content changed since oldText was captured, the case differs, line endings differ in a way the normalization does not cover, or oldText was composed for a different file. The trim-based fallback only helps with indentation differences, not content differences.

Common situations: The file was modified between the read and the edit (stale snapshot); oldText was hand-written rather than copied from the file; Unicode/encoding differences; the edit targets the wrong file.

Related errors


AI-assisted analysis of Mintplex-Labs/anything-llm@526360e320 (2026-08-13). Data as JSON: /api/errors/de3b55c28126c42f. Report an issue: GitHub.