{"record":{"id":"40f0e4ddc58216ad","repo":"windmill-labs/windmill","slug":"diff-at-index-index-old-string-old-string","errorCode":null,"errorMessage":"Diff at index ${index}: old_string \"${old_string}\" not found in code","messagePattern":"Diff at index (.+?): old_string \"(.+?)\" not found in code","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"frontend/src/lib/components/copilot/chat/script/core.ts","lineNumber":806,"sourceCode":"\t\t\t\tcontent: 'Invalid diffs provided',\n\t\t\t\terror: 'Diffs parameter is required and must be an array'\n\t\t\t})\n\t\t\tthrow new Error('Diffs parameter is required and must be an array')\n\t\t}\n\n\t\ttoolCallbacks.setToolStatus(toolId, { content: 'Applying code changes...' })\n\n\t\ttry {\n\t\t\t// Save old code\n\t\t\tconst oldCode = scriptOptions.code\n\n\t\t\t// Apply diffs sequentially\n\t\t\tlet updatedCode = oldCode\n\t\t\tfor (const [index, diff] of args.diffs.entries()) {\n\t\t\t\tconst { old_string, new_string, replace_all = false } = diff\n\n\t\t\t\tif (!updatedCode.includes(old_string)) {\n\t\t\t\t\tthrow new Error(`Diff at index ${index}: old_string \"${old_string}\" not found in code`)\n\t\t\t\t}\n\n\t\t\t\tif (replace_all) {\n\t\t\t\t\tupdatedCode = updatedCode.replaceAll(old_string, new_string)\n\t\t\t\t} else {\n\t\t\t\t\tupdatedCode = updatedCode.replace(old_string, new_string)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Apply the code changes directly\n\t\t\tawait helpers.applyCode(updatedCode, { applyAll: true, mode: 'apply' })\n\n\t\t\t// Show revert mode\n\t\t\tawait helpers.applyCode(oldCode, { mode: 'revert' })\n\n\t\t\ttoolCallbacks.setToolStatus(toolId, {\n\t\t\t\tcontent: `Code changes applied`,\n\t\t\t\tresult: 'Success'","sourceCodeStart":788,"sourceCodeEnd":824,"githubUrl":"https://github.com/windmill-labs/windmill/blob/e474e8803ce2ff5c2df09a58dab51d45f5c922ca/frontend/src/lib/components/copilot/chat/script/core.ts#L788-L824","documentation":"Diffs are applied sequentially with String.replace against the evolving script code. If a diff's old_string does not occur in the current (partially patched) code, the tool throws, naming the diff index and the unmatched string.","triggerScenarios":"old_string doesn't byte-match the editor code — wrong whitespace/indentation, stale code (editor changed since the model read it), the string was already replaced by an earlier diff in the same batch, or the model hallucinated the snippet.","commonSituations":"Model quoting normalized/fuzzy-matched code instead of exact text; consecutive diffs whose earlier replacement altered text a later diff expected; editing a script whose saved version differs from what the model saw.","solutions":["Re-read the current script code and regenerate diffs whose old_string exactly matches it (exact whitespace and indentation)","Split large edits into smaller, uniquely-anchored diffs applied in order","Ensure earlier diffs in the array don't consume the text a later old_string expects","If replace was intended for all occurrences, set replace_all: true and confirm old_string matches every intended site"],"exampleFix":"// before — indentation mismatch\n{ old_string: 'if (x) {\\nreturn 1', new_string: '...' } // not found\n// after — exact match from current code\n{ old_string: '\\tif (x) {\\n\\t\\treturn 1', new_string: '...' }","handlingStrategy":"validation","validationCode":"const missing = diffs.findIndex(d => !updatedCode.includes(d.old_string))\nif (missing !== -1) {\n  // re-read current code and regenerate diff at index `missing`\n}","typeGuard":null,"tryCatchPattern":"try {\n  applyDiffs(code, diffs)\n} catch (e) {\n  if (/not found in code/.test(e.message)) {\n    // re-fetch current script code, rebuild exact old_string, retry once\n  }\n}","preventionTips":["Copy old_string verbatim from the current editor content including whitespace","Sequence diffs so earlier replacements don't consume later old_strings","Keep diffs small and anchored to unique snippets"],"tags":["diffs","string-matching","ai-copilot","validation"],"backgroundTag":"string-not-found","analyzedSha":"e474e8803ce2ff5c2df09a58dab51d45f5c922ca","analyzedAt":"2026-09-03T12:38:19.024Z","contentChangedAt":"2026-09-03T12:38:19.024Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}