Skip to content

refactor(internal/app): strip ANSI escape sequences from commit messages - #25

Merged
takai merged 1 commit into
takai:mainfrom
asonas:fix/strip-ansi-escape-sequences
Apr 13, 2026
Merged

refactor(internal/app): strip ANSI escape sequences from commit messages#25
takai merged 1 commit into
takai:mainfrom
asonas:fix/strip-ansi-escape-sequences

Conversation

@asonas

@asonas asonas commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

Problem

Commit messages generated via git ai-commit have been getting polluted with ANSI escape sequences like [8D[K, [4D[K, etc. The root cause is ollama 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. Since CLI.Generate() pipes ollama's stdout straight into a bytes.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-level ansiEscapeRe and apply it at the top of sanitizeMessage()
  • internal/app/app_test.go — add TestSanitizeMessageStripANSIEscapeSequences covering cursor-back + erase, multiple consecutive sequences, SGR color codes, ollama wordwrap-with-newline, and a no-op case

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.
@takai

takai commented Apr 13, 2026

Copy link
Copy Markdown
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 (^[[6D^[[K etc.), which is ironic. I'll squash merge this to clean that up.

@takai
takai merged commit eca44df into takai:main Apr 13, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants