Compare commits

..

1 Commits

Author SHA1 Message Date
Claude
7c0df70e8f fix: Strip ANSI color sequences from tool output
Add stripAnsiCodes function to sanitizer and apply it in formatResultContent
to remove terminal color escape codes (e.g., [1;33m for yellow/bold) from
tool output before displaying it in GitHub comments.

This ensures clean, readable output without raw ANSI escape sequences
appearing in the formatted tool results.
2025-12-12 02:17:56 +00:00
9 changed files with 87 additions and 80 deletions

View File

@@ -2,6 +2,7 @@
import { readFileSync, existsSync } from "fs";
import { exit } from "process";
import { stripAnsiCodes } from "../github/utils/sanitizer";
export type ToolUse = {
type: string;
@@ -172,6 +173,9 @@ export function formatResultContent(content: any): string {
contentStr = String(content).trim();
}
// Strip ANSI escape codes from terminal output
contentStr = stripAnsiCodes(contentStr);
// Truncate very long results
if (contentStr.length > 3000) {
contentStr = contentStr.substring(0, 2997) + "...";

View File

@@ -15,20 +15,6 @@ import { GITHUB_SERVER_URL } from "../github/api/config";
import { checkAndCommitOrDeleteBranch } from "../github/operations/branch-cleanup";
import { updateClaudeComment } from "../github/operations/comments/update-claude-comment";
/**
* Encodes a branch name for use in a URL, preserving forward slashes.
* GitHub expects literal slashes in branch names (e.g., /tree/feature/branch)
* but other special characters like parentheses need to be encoded.
* Note: encodeURIComponent doesn't encode ( ) ! ' * ~ per RFC 3986,
* but parentheses break markdown links so we encode them manually.
*/
function encodeBranchName(branchName: string): string {
return encodeURIComponent(branchName)
.replace(/%2F/gi, "/")
.replace(/\(/g, "%28")
.replace(/\)/g, "%29");
}
async function run() {
try {
const commentId = parseInt(process.env.CLAUDE_COMMENT_ID!);
@@ -154,7 +140,7 @@ async function run() {
const prBody = encodeURIComponent(
`This PR addresses ${entityType.toLowerCase()} #${context.entityNumber}\n\nGenerated with [Claude Code](https://claude.ai/code)`,
);
const prUrl = `${serverUrl}/${owner}/${repo}/compare/${encodeBranchName(baseBranch)}...${encodeBranchName(claudeBranch)}?quick_pull=1&title=${prTitle}&body=${prBody}`;
const prUrl = `${serverUrl}/${owner}/${repo}/compare/${baseBranch}...${claudeBranch}?quick_pull=1&title=${prTitle}&body=${prBody}`;
prLink = `\n[Create a PR](${prUrl})`;
}
} catch (error) {

View File

@@ -2,20 +2,6 @@ import type { Octokits } from "../api/client";
import { GITHUB_SERVER_URL } from "../api/config";
import { $ } from "bun";
/**
* Encodes a branch name for use in a URL, preserving forward slashes.
* GitHub expects literal slashes in branch names (e.g., /tree/feature/branch)
* but other special characters like parentheses need to be encoded.
* Note: encodeURIComponent doesn't encode ( ) ! ' * ~ per RFC 3986,
* but parentheses break markdown links so we encode them manually.
*/
function encodeBranchName(branchName: string): string {
return encodeURIComponent(branchName)
.replace(/%2F/gi, "/")
.replace(/\(/g, "%28")
.replace(/\)/g, "%29");
}
export async function checkAndCommitOrDeleteBranch(
octokit: Octokits,
owner: string,
@@ -94,7 +80,7 @@ export async function checkAndCommitOrDeleteBranch(
);
// Set branch link since we now have commits
const branchUrl = `${GITHUB_SERVER_URL}/${owner}/${repo}/tree/${encodeBranchName(claudeBranch)}`;
const branchUrl = `${GITHUB_SERVER_URL}/${owner}/${repo}/tree/${claudeBranch}`;
branchLink = `\n[View branch](${branchUrl})`;
} else {
console.log(
@@ -105,7 +91,7 @@ export async function checkAndCommitOrDeleteBranch(
} catch (gitError) {
console.error("Error checking/committing changes:", gitError);
// If we can't check git status, assume the branch might have changes
const branchUrl = `${GITHUB_SERVER_URL}/${owner}/${repo}/tree/${encodeBranchName(claudeBranch)}`;
const branchUrl = `${GITHUB_SERVER_URL}/${owner}/${repo}/tree/${claudeBranch}`;
branchLink = `\n[View branch](${branchUrl})`;
}
} else {
@@ -116,13 +102,13 @@ export async function checkAndCommitOrDeleteBranch(
}
} else {
// Only add branch link if there are commits
const branchUrl = `${GITHUB_SERVER_URL}/${owner}/${repo}/tree/${encodeBranchName(claudeBranch)}`;
const branchUrl = `${GITHUB_SERVER_URL}/${owner}/${repo}/tree/${claudeBranch}`;
branchLink = `\n[View branch](${branchUrl})`;
}
} catch (error) {
console.error("Error comparing commits on Claude branch:", error);
// If we can't compare but the branch exists remotely, include the branch link
const branchUrl = `${GITHUB_SERVER_URL}/${owner}/${repo}/tree/${encodeBranchName(claudeBranch)}`;
const branchUrl = `${GITHUB_SERVER_URL}/${owner}/${repo}/tree/${claudeBranch}`;
branchLink = `\n[View branch](${branchUrl})`;
}
}

View File

@@ -1,19 +1,5 @@
import { GITHUB_SERVER_URL } from "../api/config";
/**
* Encodes a branch name for use in a URL, preserving forward slashes.
* GitHub expects literal slashes in branch names (e.g., /tree/feature/branch)
* but other special characters like parentheses need to be encoded.
* Note: encodeURIComponent doesn't encode ( ) ! ' * ~ per RFC 3986,
* but parentheses break markdown links so we encode them manually.
*/
function encodeBranchName(branchName: string): string {
return encodeURIComponent(branchName)
.replace(/%2F/gi, "/")
.replace(/\(/g, "%28")
.replace(/\)/g, "%29");
}
export type ExecutionDetails = {
total_cost_usd?: number;
duration_ms?: number;
@@ -174,7 +160,7 @@ export function updateCommentBody(input: CommentUpdateInput): string {
// Extract owner/repo from jobUrl
const repoMatch = jobUrl.match(/github\.com\/([^\/]+)\/([^\/]+)\//);
if (repoMatch) {
branchUrl = `${GITHUB_SERVER_URL}/${repoMatch[1]}/${repoMatch[2]}/tree/${encodeBranchName(finalBranchName)}`;
branchUrl = `${GITHUB_SERVER_URL}/${repoMatch[1]}/${repoMatch[2]}/tree/${finalBranchName}`;
}
}
@@ -186,9 +172,8 @@ export function updateCommentBody(input: CommentUpdateInput): string {
}
// Add PR link (either from content or provided)
// Use greedy match with end anchor to capture full URL even if it contains parentheses
const prUrl =
prLinkFromContent || (prLink ? prLink.match(/\((.+)\)$/)?.[1] : "");
prLinkFromContent || (prLink ? prLink.match(/\(([^)]+)\)/)?.[1] : "");
if (prUrl) {
links += ` • [Create PR ➔](${prUrl})`;
}

View File

@@ -12,26 +12,12 @@ export function createJobRunLink(
return `[View job run](${jobRunUrl})`;
}
/**
* Encodes a branch name for use in a URL, preserving forward slashes.
* GitHub expects literal slashes in branch names (e.g., /tree/feature/branch)
* but other special characters like parentheses need to be encoded.
* Note: encodeURIComponent doesn't encode ( ) ! ' * ~ per RFC 3986,
* but parentheses break markdown links so we encode them manually.
*/
function encodeBranchName(branchName: string): string {
return encodeURIComponent(branchName)
.replace(/%2F/gi, "/")
.replace(/\(/g, "%28")
.replace(/\)/g, "%29");
}
export function createBranchLink(
owner: string,
repo: string,
branchName: string,
): string {
const branchUrl = `${GITHUB_SERVER_URL}/${owner}/${repo}/tree/${encodeBranchName(branchName)}`;
const branchUrl = `${GITHUB_SERVER_URL}/${owner}/${repo}/tree/${branchName}`;
return `\n[View branch](${branchUrl})`;
}

View File

@@ -1,3 +1,11 @@
export function stripAnsiCodes(content: string): string {
// Matches ANSI escape sequences:
// - \x1B[ (CSI) followed by parameters and a final byte
// - \x1B followed by single-character sequences
// Common sequences: \x1B[1;33m (colors), \x1B[0m (reset), \x1B[K (clear line)
return content.replace(/\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])/g, "");
}
export function stripInvisibleCharacters(content: string): string {
content = content.replace(/[\u200B\u200C\u200D\uFEFF]/g, "");
content = content.replace(

View File

@@ -139,21 +139,6 @@ describe("updateCommentBody", () => {
);
expect(result).not.toContain("View branch");
});
it("encodes special characters in branch names while preserving slashes", () => {
const input = {
...baseInput,
branchName: "feature/fix(issue)-test",
};
const result = updateCommentBody(input);
// Branch name display should show the original name
expect(result).toContain("`feature/fix(issue)-test`");
// URL should have encoded parentheses but preserved slashes
expect(result).toContain(
"https://github.com/owner/repo/tree/feature/fix%28issue%29-test",
);
});
});
describe("PR link", () => {

View File

@@ -111,6 +111,27 @@ describe("formatResultContent", () => {
const result = formatResultContent(JSON.stringify(structuredContent));
expect(result).toBe("**→** Hello world\n\n");
});
test("strips ANSI color codes from terminal output", () => {
// Test bold yellow warning (the issue reported: [1;33m)
const coloredOutput = "\x1B[1;33mWarning: something happened\x1B[0m";
const result = formatResultContent(coloredOutput);
expect(result).toBe("**→** Warning: something happened\n\n");
expect(result).not.toContain("\x1B");
expect(result).not.toContain("[1;33m");
});
test("strips ANSI codes from longer output in code blocks", () => {
const longColoredOutput =
"\x1B[32m✓\x1B[0m Test 1 passed\n" +
"\x1B[32m✓\x1B[0m Test 2 passed\n" +
"\x1B[31m✗\x1B[0m Test 3 failed\n" +
"Some additional output to make it longer";
const result = formatResultContent(longColoredOutput);
expect(result).toContain("✓ Test 1 passed");
expect(result).toContain("✗ Test 3 failed");
expect(result).not.toContain("\x1B");
});
});
describe("formatToolWithResult", () => {

View File

@@ -1,5 +1,6 @@
import { describe, expect, it } from "bun:test";
import {
stripAnsiCodes,
stripInvisibleCharacters,
stripMarkdownImageAltText,
stripMarkdownLinkTitles,
@@ -10,6 +11,51 @@ import {
redactGitHubTokens,
} from "../src/github/utils/sanitizer";
describe("stripAnsiCodes", () => {
it("should remove color codes", () => {
// Bold yellow text: \x1B[1;33m
expect(stripAnsiCodes("\x1B[1;33mWarning\x1B[0m")).toBe("Warning");
// Red text: \x1B[31m
expect(stripAnsiCodes("\x1B[31mError\x1B[0m")).toBe("Error");
// Green text: \x1B[32m
expect(stripAnsiCodes("\x1B[32mSuccess\x1B[0m")).toBe("Success");
});
it("should remove bold and other style codes", () => {
// Bold: \x1B[1m
expect(stripAnsiCodes("\x1B[1mBold text\x1B[0m")).toBe("Bold text");
// Underline: \x1B[4m
expect(stripAnsiCodes("\x1B[4mUnderlined\x1B[0m")).toBe("Underlined");
});
it("should remove cursor movement codes", () => {
// Clear line: \x1B[K
expect(stripAnsiCodes("Text\x1B[K")).toBe("Text");
// Cursor up: \x1B[A
expect(stripAnsiCodes("Line1\x1B[ALine2")).toBe("Line1Line2");
});
it("should handle multiple ANSI codes in one string", () => {
const input = "\x1B[1;31mError:\x1B[0m \x1B[33mWarning\x1B[0m text";
expect(stripAnsiCodes(input)).toBe("Error: Warning text");
});
it("should preserve text without ANSI codes", () => {
expect(stripAnsiCodes("Normal text")).toBe("Normal text");
expect(stripAnsiCodes("Text with [brackets]")).toBe("Text with [brackets]");
});
it("should handle empty string", () => {
expect(stripAnsiCodes("")).toBe("");
});
it("should handle complex terminal output", () => {
// Simulates npm/yarn output with colors
const input = "\x1B[2K\x1B[1G\x1B[32m✓\x1B[0m Tests passed";
expect(stripAnsiCodes(input)).toBe("✓ Tests passed");
});
});
describe("stripInvisibleCharacters", () => {
it("should remove zero-width characters", () => {
expect(stripInvisibleCharacters("Hello\u200BWorld")).toBe("HelloWorld");