refactor(internal/app): strip ANSI escape sequences from commit messages - #25
Merged
Merged
Conversation
Add a regular expression to remove ANSI escape sequences from commit messag�[6D�[K messages, ensuring that sanitized messages are clean and free of color code�[4D�[K codes or cursor movements. This improves readability and consistency in log�[3D�[K log output. - Added `regexp.MustCompile` for matching and removing ANSI escape sequence�[8D�[K sequences. - Updated `sanitizeMessage` function to include the new regex logic. - Created a test function `TestSanitizeMessageStripANSIEscapeSequences` to �[K validate the functionality with various scenarios, including cursor backspa�[7D�[K backspace, SGR color codes, and messages without any escape sequences.
Owner
|
Thanks for the fix! The approach looks good — defensive sanitization at the app layer is the right call while ollama/ollama#14571 remains open. One thing: the commit message body itself contains the ANSI artifacts ( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Commit messages generated via
git ai-commithave been getting polluted with ANSI escape sequences like[8D[K,[4D[K, etc. The root cause isollama run's word-wrap feature: even when stdout is a pipe (non-TTY), ollama emits cursor-back (ESC[nD) and erase-to-end-of-line (ESC[K) sequences to reflow words at the terminal width. SinceCLI.Generate()pipes ollama's stdout straight into abytes.Buffer, those control codes end up inside the final commit message.Solution
Defensive sanitization at the app layer.
sanitizeMessage()now strips any ANSI CSI sequence matching\x1b\[[0-9;]*[A-Za-z]before the existing code-fence / backtick cleanup runs. Engine-agnostic — any future engine that leaks control codes will also produce clean messages.The regex is intentionally scoped to CSI (
ESC [) only. OSC, DECSC/DECRC, and single-shift families are left unhandled until observed in practice, to keep the scope narrow and justifiable.Changes
internal/app/app.go— add package-levelansiEscapeReand apply it at the top ofsanitizeMessage()internal/app/app_test.go— addTestSanitizeMessageStripANSIEscapeSequencescovering cursor-back + erase, multiple consecutive sequences, SGR color codes, ollama wordwrap-with-newline, and a no-op case