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): stop spinner from scrolling endlessly in narrow terminals

sjd90217ad88415c707
diff --git a/ts/packages/cli/src/services/terminal-ui.ts b/ts/packages/cli/src/services/terminal-ui.ts
index d4b42f63b..003dc9839 100644
--- a/ts/packages/cli/src/services/terminal-ui.ts
+++ b/ts/packages/cli/src/services/terminal-ui.ts
@@ -1,5 +1,6 @@
import process from 'node:process';
import type { Writable } from 'node:stream';
+import { getColumns } from '@clack/core';
import * as p from '@clack/prompts';
import { Context, Effect, Exit, Layer } from 'effect';
@@ -187,14 +188,34 @@ type TerminalUIStreams = {
readonly stderr: TerminalWritable;
};
+// Clack's spinner erases the previous frame by re-wrapping the raw message,
+// but it renders `${frame} ${message}${dots}` — three prefix columns plus up
+// to three animated dots the erase math never sees. Once those extras push the
+// rendered frame across a wrap boundary, the erase under-counts lines and
+// 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.
+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;
+};
+
function createClackSpinnerHandle(
s: p.SpinnerResult,
- defaultMessage: string
+ defaultMessage: string,
+ clamp: (msg: string) => string
): { handle: SpinnerHandle; isStopped: () => boolean } {
let stopped = false;
return {
handle: {
- message: (msg: string) => Effect.sync(() => s.message(msg)),
+ message: (msg: string) => Effect.sync(() => s.message(clamp(msg))),
stop: (msg?: string) =>
Effect.sync(() => {
stopped = true;
@@ -309,7 +330,7 @@ export const makeTerminalUI = (streams: TerminalUIStreams): TerminalUI => {
return Effect.acquireUseRelease(
Effect.sync(() => {
const s = p.spinner({ output: stderr });
- s.start(message);
+ s.start(clampSpinnerMessage(stderr, message));
return s;
}),
() => effect,
@@ -339,8 +360,10 @@ export const makeTerminalUI = (streams: TerminalUIStreams): TerminalUI => {
return Effect.acquireUseRelease(
Effect.sync(() => {
const s = p.spinner({ output: stderr });
- s.start(message);
- const { handle, isStopped } = createClackSpinnerHandle(s, message);
+ s.start(clampSpinnerMessage(stderr, message));
+ const { handle, isStopped } = createClackSpinnerHandle(s, message, msg =>
+ clampSpinnerMessage(stderr, msg)
+ );
return { raw: s, handle, isStopped };
}),
({ handle }) => use(handle),
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 045e105da..8608e92ef 100644
--- a/ts/packages/cli/test/src/services/terminal-ui.test.ts
+++ b/ts/packages/cli/test/src/services/terminal-ui.test.ts
@@ -3,7 +3,12 @@ import { describe, expect, it, layer } from '@effect/vitest';
import { beforeEach, vi } from 'vitest';
import * as p from '@clack/prompts';
import { Array as Arr, Data, Effect, Exit, pipe } from 'effect';
-import { getTerminalCapabilities, makeTerminalUI, TerminalUI } from 'src/services/terminal-ui';
+import {
+ clampSpinnerMessage,
+ getTerminalCapabilities,
+ makeTerminalUI,
+ TerminalUI,
+} from 'src/services/terminal-ui';
import { TestLive, MockConsole } from 'test/__utils__';
vi.mock('@clack/prompts', async importOriginal => {
@@ -166,6 +171,72 @@ describe('TerminalUI', () => {
);
});
+ describe('clampSpinnerMessage keeps live spinner messages on one terminal row', () => {
+ const streamWithColumns = (columns?: number) =>
+ columns === undefined ? makeSink(true) : Object.assign(makeSink(true), { columns });
+
+ it('leaves a message that fits untouched', () => {
+ expect(clampSpinnerMessage(streamWithColumns(80), 'Checking for updates')).toBe(
+ 'Checking for updates'
+ );
+ });
+
+ it('truncates a message longer than the row budget with an ellipsis', () => {
+ const clamped = clampSpinnerMessage(streamWithColumns(40), 'x'.repeat(100));
+ expect(clamped).toBe(`${'x'.repeat(32)}…`);
+ expect(clamped).toHaveLength(40 - 7);
+ });
+
+ it('collapses embedded newlines into spaces', () => {
+ expect(clampSpinnerMessage(streamWithColumns(80), 'line one\nline two')).toBe(
+ 'line one line two'
+ );
+ });
+
+ it('never slices a message containing ANSI escape sequences', () => {
+ const colored = `\u001b[32m${'x'.repeat(100)}\u001b[0m`;
+ expect(clampSpinnerMessage(streamWithColumns(40), colored)).toBe(colored);
+ });
+
+ it('falls back to 80 columns when the stream reports no width', () => {
+ const clamped = clampSpinnerMessage(streamWithColumns(undefined), 'x'.repeat(100));
+ 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('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!');
+ })
+ )
+ );
+
+ 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();
+ }
+ });
+ });
+
layer(TestLive())(it => {
// -----------------------------------------------------------------------
// Data output