Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🎨 refactor(local-system): preserve ANSI escape codes in command output (#15529)

Arvin Xudf72bc335edb
diff --git a/packages/local-file-shell/src/shell/__tests__/utils.test.ts b/packages/local-file-shell/src/shell/__tests__/utils.test.ts
index 3c71649be70..bd4e7ffa852 100644
--- a/packages/local-file-shell/src/shell/__tests__/utils.test.ts
+++ b/packages/local-file-shell/src/shell/__tests__/utils.test.ts
@@ -1,35 +1,6 @@
import { describe, expect, it } from 'vitest';
-import { getShellConfig, MAX_OUTPUT_LENGTH, stripAnsi, truncateOutput } from '../utils';
-
-describe('stripAnsi', () => {
- it('should strip ANSI color codes', () => {
- expect(stripAnsi('\x1B[31mred\x1B[0m')).toBe('red');
- });
-
- it('should strip complex ANSI sequences', () => {
- expect(stripAnsi('\x1B[38;5;250mβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—\x1B[0m')).toBe('β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—');
- });
-
- it('should strip bold/bright codes', () => {
- expect(stripAnsi('\x1B[1;32mSuccess\x1B[0m')).toBe('Success');
- });
-
- it('should handle string without ANSI codes', () => {
- expect(stripAnsi('plain text')).toBe('plain text');
- });
-
- it('should handle empty string', () => {
- expect(stripAnsi('')).toBe('');
- });
-
- it('should strip multiple ANSI sequences', () => {
- const input = '\x1B[33mwarning:\x1B[0m something \x1B[31mhappened\x1B[0m';
- expect(input).toContain('\x1B[');
- expect(stripAnsi(input)).toBe('warning: something happened');
- expect(stripAnsi(input)).not.toContain('\x1B[');
- });
-});
+import { getShellConfig, MAX_OUTPUT_LENGTH, truncateOutput } from '../utils';
describe('truncateOutput', () => {
it('should return string as-is when within limit', () => {
@@ -45,10 +16,42 @@ describe('truncateOutput', () => {
expect(result).toContain('more characters');
});
- it('should strip ANSI before checking length', () => {
+ it('should preserve ANSI escape codes so the client can render colors', () => {
const colored = '\x1B[31m' + 'x'.repeat(50) + '\x1B[0m';
const result = truncateOutput(colored, 100);
- expect(result).toBe('x'.repeat(50));
+ expect(result).toBe(colored);
+ expect(result).toContain('\x1B[');
+ });
+
+ it('should reset an open SGR state before the truncation notice', () => {
+ // A long colored line whose closing \x1B[0m falls beyond the cut boundary.
+ const colored = '\x1B[31m' + 'x'.repeat(200) + '\x1B[0m';
+ const result = truncateOutput(colored, 100);
+
+ expect(result).toContain('truncated');
+ // The reset must sit right before the notice so the color cannot bleed into it.
+ expect(result).toContain('\x1B[0m\n... [truncated');
+ // Everything after the reset (the notice) carries no further escape codes.
+ const notice = result.slice(result.indexOf('\x1B[0m\n'));
+ expect(notice.slice('\x1B[0m'.length)).not.toContain('\x1B[');
+ });
+
+ it('should drop a partial escape sequence left dangling at the cut boundary', () => {
+ // maxLength lands in the middle of the second color sequence (\x1B[32 has no final byte yet).
+ const input = '\x1B[31mred' + '\x1B[32mgreen';
+ const cutInsideEscape = ('\x1B[31mred' + '\x1B[32').length;
+ const result = truncateOutput(input, cutInsideEscape);
+
+ // The incomplete \x1B[32 must be removed, not carried into the output.
+ expect(result).not.toContain('\x1B[32');
+ expect(result.startsWith('\x1B[31mred')).toBe(true);
+ expect(result).toContain('\x1B[0m\n... [truncated');
+ });
+
+ it('should not inject an ANSI reset into plain (non-colored) output', () => {
+ const result = truncateOutput('x'.repeat(200), 100);
+ expect(result).not.toContain('\x1B');
+ expect(result).toBe('x'.repeat(100) + '\n... [truncated, 100 more characters]');
});
it('should use MAX_OUTPUT_LENGTH as default', () => {
diff --git a/packages/local-file-shell/src/shell/index.ts b/packages/local-file-shell/src/shell/index.ts
index d88610afb41..16dd56f59f1 100644
--- a/packages/local-file-shell/src/shell/index.ts
+++ b/packages/local-file-shell/src/shell/index.ts
@@ -2,4 +2,4 @@ export type { ShellProcess } from './process-manager';
export { ShellProcessManager } from './process-manager';
export type { RunCommandOptions } from './runner';
export { runCommand } from './runner';
-export { getShellConfig, MAX_OUTPUT_LENGTH, stripAnsi, truncateOutput } from './utils';
+export { getShellConfig, MAX_OUTPUT_LENGTH, truncateOutput } from './utils';
diff --git a/packages/local-file-shell/src/shell/utils.ts b/packages/local-file-shell/src/shell/utils.ts
index 11fa4ee9ee1..672070e57d0 100644
--- a/packages/local-file-shell/src/shell/utils.ts
+++ b/packages/local-file-shell/src/shell/utils.ts
@@ -1,22 +1,38 @@
/** Maximum output length to prevent context explosion */
export const MAX_OUTPUT_LENGTH = 80_000;
-/** Strip ANSI escape codes from terminal output */
-// eslint-disable-next-line no-control-regex, regexp/no-obscure-range
-const ANSI_REGEX = /\u001B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])/g;
+/** ANSI SGR reset, closes any open color/style state */
+const ANSI_RESET = '\u001B[0m';
-export const stripAnsi = (str: string): string => str.replaceAll(ANSI_REGEX, '');
+/** Matches a complete ANSI escape sequence anchored at the start of the string */
+// eslint-disable-next-line no-control-regex, regexp/no-obscure-range
+const ANSI_ESCAPE_AT_START = /^\u001B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])/;
-/** Truncate string to max length with indicator */
+/**
+ * Truncate string to max length with indicator.
+ *
+ * ANSI escape codes are preserved so the client can render colored output, but a
+ * naive slice can (a) cut across an escape sequence and (b) stop while a color or
+ * style is still open. Either would bleed styling into the truncation notice and
+ * anything the client renders afterwards. So we drop a dangling partial sequence
+ * at the cut boundary and append a reset before the notice.
+ */
export const truncateOutput = (str: string, maxLength: number = MAX_OUTPUT_LENGTH): string => {
- const cleaned = stripAnsi(str);
- if (cleaned.length <= maxLength) return cleaned;
- return (
- cleaned.slice(0, maxLength) +
- '\n... [truncated, ' +
- (cleaned.length - maxLength) +
- ' more characters]'
- );
+ if (str.length <= maxLength) return str;
+
+ let slice = str.slice(0, maxLength);
+
+ // Drop a partial escape sequence left dangling at the cut boundary, otherwise
+ // it would consume the leading characters of the truncation notice.
+ const lastEsc = slice.lastIndexOf('\u001B');
+ if (lastEsc !== -1 && !ANSI_ESCAPE_AT_START.test(slice.slice(lastEsc))) {
+ slice = slice.slice(0, lastEsc);
+ }
+
+ // Reset any still-open SGR state so it cannot leak into the notice.
+ const reset = slice.includes('\u001B') ? ANSI_RESET : '';
+
+ return slice + reset + '\n... [truncated, ' + (str.length - maxLength) + ' more characters]';
};
/** Get cross-platform shell configuration */