windmill-labs/windmill · error

Failed to apply code changes: ${errorMessage}

Error message

Failed to apply code changes: ${errorMessage}

What it means

Catch-all wrapper around the edit-code apply phase: any error thrown while saving/applying diffs (including the old_string-not-found error above) is caught, surfaced via tool status, and rethrown as 'Failed to apply code changes: <original message>'.

Source

Thrown at frontend/src/lib/components/copilot/chat/script/core.ts:833

			// Apply the code changes directly
			await helpers.applyCode(updatedCode, { applyAll: true, mode: 'apply' })

			// Show revert mode
			await helpers.applyCode(oldCode, { mode: 'revert' })

			toolCallbacks.setToolStatus(toolId, {
				content: `Code changes applied`,
				result: 'Success'
			})
			return `Applied changes to the script editor.`
		} catch (error) {
			const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'
			toolCallbacks.setToolStatus(toolId, {
				content: 'Failed to apply code changes',
				error: errorMessage
			})
			throw new Error(`Failed to apply code changes: ${errorMessage}`)
		}
	}
}

export const editCodeTool: Tool<ScriptChatHelpers> = {
	def: EDIT_CODE_TOOL,
	streamArguments: true,
	showDetails: true,
	showFade: true,
	fn: async function ({ args, helpers, toolCallbacks, toolId }) {
		const scriptOptions = helpers.getScriptOptions()

		if (!scriptOptions) {
			toolCallbacks.setToolStatus(toolId, {
				content: 'No script available to edit',
				error: 'No script found in current context'
			})
			throw new Error(

View on GitHub (pinned to e474e8803c)

Solutions

  1. Read the original message after the colon — fix that underlying cause (usually index N old_string mismatch)
  2. Re-read current code and retry with refreshed exact diffs
  3. Check for concurrent edits/saves on the same script while the tool runs
  4. If saving failed, verify write permissions and backend connectivity, then retry

Example fix

// diagnose: 'Failed to apply code changes: Diff at index 1: old_string "..." not found'
// -> fix diff[1] to exactly match current code, then re-run the tool
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await editCode({ diffs, scriptOptions })
} catch (e) {
  const cause = e.message.replace('Failed to apply code changes: ', '')
  if (/not found in code/.test(cause)) { /* refresh diffs and retry */ }
  else { /* surface cause; check save permissions/connectivity */ }
}

Prevention

When it happens

Trigger: Any exception inside the apply/save block — unmatched old_string, exception writing the updated code back via scriptOptions/SaveService, or unexpected runtime error during sequential replacement.

Common situations: Chained diff failures; concurrent edits to the same script; storage/save errors when persisting the new code; the original cause is always embedded in the message, so read the suffix first.

Related errors


AI-assisted analysis of windmill-labs/windmill@e474e8803c (2026-09-03). Data as JSON: /api/errors/fea5b2d00b9ffa3e. Report an issue: GitHub.