Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

fix(cli): clamp spinner messages by display width

jkomyno4e45d7fabe47
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 2df1830a6..4ee546372 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1221,6 +1221,9 @@ importers:
extract-zip:
specifier: ^2.0.1
version: 2.0.1([email protected])
+ fast-string-width:
+ specifier: ^3.0.2
+ version: 3.0.2
indent-string:
specifier: ^5.0.0
version: 5.0.0
diff --git a/ts/packages/cli/CHANGELOG.md b/ts/packages/cli/CHANGELOG.md
index 36ba3c5f2..1386b6969 100644
--- a/ts/packages/cli/CHANGELOG.md
+++ b/ts/packages/cli/CHANGELOG.md
@@ -4,6 +4,10 @@
### Patch Changes
+- `composio upgrade` (and any other command with a long spinner message) no
+ longer scrolls endlessly in terminals narrower than that message. The spinner
+ printed its line hundreds of times instead of animating in place; live spinner
+ text is now clamped to a single terminal row.
- `composio upgrade` shows download progress. The archive is a few hundred
megabytes, and the command previously printed `Downloading...` once and then
said nothing for minutes, which was indistinguishable from a hang. It now
diff --git a/ts/packages/cli/package.json b/ts/packages/cli/package.json
index 9d4d5d619..710b3976d 100644
--- a/ts/packages/cli/package.json
+++ b/ts/packages/cli/package.json
@@ -90,6 +90,7 @@
"@zed-industries/codex-acp": "^0.16.0",
"effect": "catalog:",
"extract-zip": "^2.0.1",
+ "fast-string-width": "^3.0.2",
"indent-string": "^5.0.0",
"js-tiktoken": "^1.0.21",
"json5": "^2.2.3",
diff --git a/ts/packages/cli/src/services/terminal-ui.ts b/ts/packages/cli/src/services/terminal-ui.ts
index 003dc9839..6a22f29c7 100644
--- a/ts/packages/cli/src/services/terminal-ui.ts
+++ b/ts/packages/cli/src/services/terminal-ui.ts
@@ -2,6 +2,7 @@ import process from 'node:process';
import type { Writable } from 'node:stream';
import { getColumns } from '@clack/core';
import * as p from '@clack/prompts';
+import stringWidth from 'fast-string-width';
import { Context, Effect, Exit, Layer } from 'effect';
export type TtyLikeStream = {
@@ -195,16 +196,38 @@ type TerminalUIStreams = {
// every tick leaks one: an endless scroll in terminals narrower than the
// message. Keep live spinner messages to a single terminal row so the redraw
// arithmetic cannot diverge.
+//
+// The invariant holds for terminals of at least SPINNER_RENDER_OVERHEAD + 1
+// columns. Below that the frame prefix alone overflows the row and no message
+// length can keep it on one line, so the budget degrades to just the ellipsis.
const SPINNER_RENDER_OVERHEAD = 7; // frame + two spaces (3) + animated dots (3) + last-column safety (1)
-const MIN_SPINNER_MESSAGE_COLUMNS = 8;
export const clampSpinnerMessage = (output: Writable, message: string): string => {
const singleLine = message.replace(/\s*\r?\n\s*/g, ' ');
if (singleLine.includes('\u001b')) {
return singleLine; // truncating could split an ANSI escape sequence
}
- const budget = Math.max(getColumns(output) - SPINNER_RENDER_OVERHEAD, MIN_SPINNER_MESSAGE_COLUMNS);
- return singleLine.length > budget ? `${singleLine.slice(0, budget - 1)}…` : singleLine;
+
+ const budget = Math.max(getColumns(output) - SPINNER_RENDER_OVERHEAD, 1);
+ if (stringWidth(singleLine) <= budget) {
+ return singleLine;
+ }
+
+ // Measure in display columns, not UTF-16 code units: clack wraps the rendered
+ // frame by width, so a CJK character it counts as two columns would slip past
+ // a `.length` budget and wrap anyway. Accumulating whole code points also keeps
+ // the cut off a surrogate pair. One column is reserved for the ellipsis.
+ let clamped = '';
+ let width = 0;
+ for (const char of singleLine) {
+ const charWidth = stringWidth(char);
+ if (width + charWidth > budget - 1) {
+ break;
+ }
+ clamped += char;
+ width += charWidth;
+ }
+ return `${clamped}…`;
};
function createClackSpinnerHandle(
diff --git a/ts/packages/cli/test/src/services/terminal-ui.test.ts b/ts/packages/cli/test/src/services/terminal-ui.test.ts
index 8608e92ef..bedf54074 100644
--- a/ts/packages/cli/test/src/services/terminal-ui.test.ts
+++ b/ts/packages/cli/test/src/services/terminal-ui.test.ts
@@ -2,6 +2,7 @@ import { Writable } from 'node:stream';
import { describe, expect, it, layer } from '@effect/vitest';
import { beforeEach, vi } from 'vitest';
import * as p from '@clack/prompts';
+import stringWidth from 'fast-string-width';
import { Array as Arr, Data, Effect, Exit, pipe } from 'effect';
import {
clampSpinnerMessage,
@@ -203,38 +204,131 @@ describe('TerminalUI', () => {
expect(clamped).toHaveLength(80 - 7);
});
- it('keeps a minimum budget on absurdly narrow terminals', () => {
- const clamped = clampSpinnerMessage(streamWithColumns(4), 'x'.repeat(100));
- expect(clamped).toBe(`${'x'.repeat(7)}…`);
+ it('measures display columns, not UTF-16 code units', () => {
+ // 20 CJK characters are 20 code units but 40 display columns, so at 40
+ // columns they sit under a `.length` budget of 33 and over a width one.
+ // A length-based clamp would pass them through and let the frame wrap.
+ const message = '\u4f60\u597d'.repeat(10);
+ expect(message).toHaveLength(20);
+ expect(stringWidth(message)).toBe(40);
+
+ const clamped = clampSpinnerMessage(streamWithColumns(40), message);
+ expect(clamped.endsWith('…')).toBe(true);
+ expect(stringWidth(clamped)).toBeLessThanOrEqual(40 - 7);
});
- it('renders live spinner frames without wrapping in a narrow terminal', async () => {
- vi.useFakeTimers();
- try {
- const stdout = makeSink(true);
- const stderr = Object.assign(makeSink(true), { columns: 40 });
- const ui = makeTerminalUI({ stdin: { isTTY: true }, stdout, stderr });
-
- await Effect.runPromise(
- ui.useMakeSpinner(
- 'New version available: @composio/[email protected] (current: @composio/[email protected]). Downloading...',
- spinner =>
- Effect.gen(function* () {
- yield* Effect.sync(() => vi.advanceTimersByTime(150));
- yield* spinner.stop('Upgrade completed!');
- })
- )
- );
+ it('never cuts a surrogate pair in half', () => {
+ const clamped = clampSpinnerMessage(streamWithColumns(20), '\u{1f600}'.repeat(40));
+ expect(clamped).not.toMatch(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])/);
+ expect(stringWidth(clamped)).toBeLessThanOrEqual(20 - 7);
+ });
+
+ it('degrades to the ellipsis on terminals narrower than the frame overhead', () => {
+ // Below SPINNER_RENDER_OVERHEAD + 1 columns no message length fits, so the
+ // budget bottoms out at one column rather than jumping back up to eight.
+ expect(clampSpinnerMessage(streamWithColumns(4), 'x'.repeat(100))).toBe('…');
+ });
- const frame = stderr.chunks.find(chunk => chunk.includes('…'));
- expect(frame).toBeDefined();
- expect(frame).toContain('New version available: @composio');
- // A wrapped frame is what leaks lines on every redraw tick.
- expect(frame).not.toContain('\n');
- } finally {
- vi.useRealTimers();
- }
+ it('keeps the rendered frame within the row at the exact budget boundary', () => {
+ const columns = 20;
+ const clamped = clampSpinnerMessage(streamWithColumns(columns), 'x'.repeat(100));
+ // frame (1) + two spaces (2) + message + three animated dots (3).
+ expect(1 + 2 + stringWidth(clamped) + 3).toBeLessThanOrEqual(columns);
});
+
+ // Clack ticks every 80ms and grows its animated dots by 0.125 per tick, so
+ // reaching the three dots SPINNER_RENDER_OVERHEAD reserves takes 24 ticks.
+ // A shorter advance renders a frame with no dots and never exercises them.
+ const TICKS_PAST_THREE_DOTS = 2000;
+
+ const LONG_UPGRADE_MESSAGE =
+ 'New version available: @composio/[email protected] (current: @composio/[email protected]). Downloading...';
+
+ const narrowUi = (columns: number) => {
+ const stdout = makeSink(true);
+ const stderr = Object.assign(makeSink(true), { columns });
+ const ui = makeTerminalUI({ stdin: { isTTY: true }, stdout, stderr });
+ return { ui, stderr };
+ };
+
+ /** Every frame clack redrew, i.e. the writes that carry the clamped message. */
+ const clampedFrames = (stderr: { chunks: string[] }) =>
+ stderr.chunks.filter(chunk => chunk.includes('…'));
+
+ /** Drives a real clack spinner with fake timers, then always restores them. */
+ const withFakeTimers = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
+ Effect.acquireUseRelease(
+ Effect.sync(() => vi.useFakeTimers()),
+ () => effect,
+ () => Effect.sync(() => vi.useRealTimers())
+ );
+
+ it.live('renders live spinner frames without wrapping in a narrow terminal', () =>
+ withFakeTimers(
+ Effect.gen(function* () {
+ const { ui, stderr } = narrowUi(40);
+
+ yield* ui.useMakeSpinner(LONG_UPGRADE_MESSAGE, spinner =>
+ Effect.gen(function* () {
+ yield* Effect.sync(() => vi.advanceTimersByTime(TICKS_PAST_THREE_DOTS));
+ yield* spinner.stop('Upgrade completed!');
+ })
+ );
+
+ const frames = clampedFrames(stderr);
+ expect(frames.length).toBeGreaterThan(0);
+ expect(frames[0]).toContain('New version available: @composio');
+ // A wrapped frame is what leaks lines on every redraw tick.
+ for (const frame of frames) {
+ expect(frame).not.toContain('\n');
+ }
+ })
+ )
+ );
+
+ it.live('clamps live message() updates, not just the start message', () =>
+ withFakeTimers(
+ Effect.gen(function* () {
+ const { ui, stderr } = narrowUi(40);
+
+ yield* ui.useMakeSpinner('Checking for updates...', spinner =>
+ Effect.gen(function* () {
+ // The upgrade flow drives its longest text through message(), not
+ // start() — reverting the clamp there must fail this test.
+ yield* spinner.message(LONG_UPGRADE_MESSAGE);
+ yield* Effect.sync(() => vi.advanceTimersByTime(TICKS_PAST_THREE_DOTS));
+ yield* spinner.stop('Upgrade completed!');
+ })
+ );
+
+ const frames = clampedFrames(stderr);
+ expect(frames.length).toBeGreaterThan(0);
+ expect(frames[0]).toContain('New version available: @composio');
+ for (const frame of frames) {
+ expect(frame).not.toContain('\n');
+ }
+ })
+ )
+ );
+
+ it.live('clamps the withSpinner start message too', () =>
+ withFakeTimers(
+ Effect.gen(function* () {
+ const { ui, stderr } = narrowUi(40);
+
+ yield* ui.withSpinner(
+ LONG_UPGRADE_MESSAGE,
+ Effect.sync(() => vi.advanceTimersByTime(TICKS_PAST_THREE_DOTS))
+ );
+
+ const frames = clampedFrames(stderr);
+ expect(frames.length).toBeGreaterThan(0);
+ for (const frame of frames) {
+ expect(frame).not.toContain('\n');
+ }
+ })
+ )
+ );
});
layer(TestLive())(it => {