Red builds are opportunities.
Traditionally, a failed test meant a Developer context-switch. They have to pull the branch, run the test, finding the typo.
Self-Healing Pipelines intercept the error output, specificially the Stack Trace, and ask an LLM: "Given this code and this error, what is the fix?"
02. Authentication Loop
The keys is to pass the file content and the error message. The LLM returns a git-patch.
Deep Dive: The Agent's Prompt
You must constrain the agent to be chirurgic.
System: You are a CI Repair Bot.
Input: [Bad Code] + [Stack Trace]
Output: ONLY the unified diff patch. Do not output markdown. Do not rewrite the whole file.
code: readFileSync('app.ts'),
error: stderr
{'}'});
applyPatch(patch);
rerunTests();
{'}'}
04. The Senior Engineer's Take
Don't Auto-Merge
AI fixes are often correct in syntax but wrong in logic (e.g., deleting the test to make it pass).
Rule: The AI commits the fix as a "Suggestion" commit. A human must still press the "Merge" button.
The Flaky Test Trap
If your test suite is flaky (fails 1% of the time randomly), do not use self-healing agents. The agent will hallucinate a "fix" for code that wasn't broken, introducing zombie code into your repo. Fix the tests first.