@devries-cameron20 / composio-1 / commits / 7ad88415c
Fork 0

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

Clack's spinner erases the previous frame by re-wrapping the raw message, but each frame actually renders three extra prefix columns plus up to three animated dots. Once those extras push the rendered frame across a wrap boundary — e.g. the ~90-column upgrade message in a narrower terminal — the erase under-counts lines and every 80ms tick leaks one, scrolling 'New version available: …' forever. Clamp live spinner messages (start and message updates) to a single terminal row so the redraw arithmetic cannot diverge. Stop/error messages are single writes and stay untouched. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

sjd9021 committed Aug 27, 2026 at 01:30 UTC 7ad88415c7071d5366a30e99d31d7130f388bdfd
2 files changed +100 -6
ts/packages/cli/src/services/terminal-ui.ts
+28 -5
@@ -1,5 +1,6 @@
1 import process from 'node:process';
2 import type { Writable } from 'node:stream';
3 +import { getColumns } from '@clack/core';
4 import * as p from '@clack/prompts';
5 import { Context, Effect, Exit, Layer } from 'effect';
6
@@ -187,14 +188,34 @@ type TerminalUIStreams = {
188 readonly stderr: TerminalWritable;
189 };
190
191 +// Clack's spinner erases the previous frame by re-wrapping the raw message,
192 +// but it renders `${frame} ${message}${dots}` — three prefix columns plus up
193 +// to three animated dots the erase math never sees. Once those extras push the
194 +// rendered frame across a wrap boundary, the erase under-counts lines and
195 +// every tick leaks one: an endless scroll in terminals narrower than the
196 +// message. Keep live spinner messages to a single terminal row so the redraw
197 +// arithmetic cannot diverge.
198 +const SPINNER_RENDER_OVERHEAD = 7; // frame + two spaces (3) + animated dots (3) + last-column safety (1)
199 +const MIN_SPINNER_MESSAGE_COLUMNS = 8;
200 +
201 +export const clampSpinnerMessage = (output: Writable, message: string): string => {
202 + const singleLine = message.replace(/\s*\r?\n\s*/g, ' ');
203 + if (singleLine.includes('\u001b')) {
204 + return singleLine; // truncating could split an ANSI escape sequence
205 + }
206 + const budget = Math.max(getColumns(output) - SPINNER_RENDER_OVERHEAD, MIN_SPINNER_MESSAGE_COLUMNS);
207 + return singleLine.length > budget ? `${singleLine.slice(0, budget - 1)}…` : singleLine;
208 +};
209 +
210 function createClackSpinnerHandle(
211 s: p.SpinnerResult,
192 - defaultMessage: string
212 + defaultMessage: string,
213 + clamp: (msg: string) => string
214 ): { handle: SpinnerHandle; isStopped: () => boolean } {
215 let stopped = false;
216 return {
217 handle: {
197 - message: (msg: string) => Effect.sync(() => s.message(msg)),
218 + message: (msg: string) => Effect.sync(() => s.message(clamp(msg))),
219 stop: (msg?: string) =>
220 Effect.sync(() => {
221 stopped = true;
@@ -309,7 +330,7 @@ export const makeTerminalUI = (streams: TerminalUIStreams): TerminalUI => {
330 return Effect.acquireUseRelease(
331 Effect.sync(() => {
332 const s = p.spinner({ output: stderr });
312 - s.start(message);
333 + s.start(clampSpinnerMessage(stderr, message));
334 return s;
335 }),
336 () => effect,
@@ -339,8 +360,10 @@ export const makeTerminalUI = (streams: TerminalUIStreams): TerminalUI => {
360 return Effect.acquireUseRelease(
361 Effect.sync(() => {
362 const s = p.spinner({ output: stderr });
342 - s.start(message);
343 - const { handle, isStopped } = createClackSpinnerHandle(s, message);
363 + s.start(clampSpinnerMessage(stderr, message));
364 + const { handle, isStopped } = createClackSpinnerHandle(s, message, msg =>
365 + clampSpinnerMessage(stderr, msg)
366 + );
367 return { raw: s, handle, isStopped };
368 }),
369 ({ handle }) => use(handle),
ts/packages/cli/test/src/services/terminal-ui.test.ts
+72 -1
@@ -3,7 +3,12 @@ import { describe, expect, it, layer } from '@effect/vitest';
3 import { beforeEach, vi } from 'vitest';
4 import * as p from '@clack/prompts';
5 import { Array as Arr, Data, Effect, Exit, pipe } from 'effect';
6 -import { getTerminalCapabilities, makeTerminalUI, TerminalUI } from 'src/services/terminal-ui';
6 +import {
7 + clampSpinnerMessage,
8 + getTerminalCapabilities,
9 + makeTerminalUI,
10 + TerminalUI,
11 +} from 'src/services/terminal-ui';
12 import { TestLive, MockConsole } from 'test/__utils__';
13
14 vi.mock('@clack/prompts', async importOriginal => {
@@ -166,6 +171,72 @@ describe('TerminalUI', () => {
171 );
172 });
173
174 + describe('clampSpinnerMessage keeps live spinner messages on one terminal row', () => {
175 + const streamWithColumns = (columns?: number) =>
176 + columns === undefined ? makeSink(true) : Object.assign(makeSink(true), { columns });
177 +
178 + it('leaves a message that fits untouched', () => {
179 + expect(clampSpinnerMessage(streamWithColumns(80), 'Checking for updates')).toBe(
180 + 'Checking for updates'
181 + );
182 + });
183 +
184 + it('truncates a message longer than the row budget with an ellipsis', () => {
185 + const clamped = clampSpinnerMessage(streamWithColumns(40), 'x'.repeat(100));
186 + expect(clamped).toBe(`${'x'.repeat(32)}…`);
187 + expect(clamped).toHaveLength(40 - 7);
188 + });
189 +
190 + it('collapses embedded newlines into spaces', () => {
191 + expect(clampSpinnerMessage(streamWithColumns(80), 'line one\nline two')).toBe(
192 + 'line one line two'
193 + );
194 + });
195 +
196 + it('never slices a message containing ANSI escape sequences', () => {
197 + const colored = `\u001b[32m${'x'.repeat(100)}\u001b[0m`;
198 + expect(clampSpinnerMessage(streamWithColumns(40), colored)).toBe(colored);
199 + });
200 +
201 + it('falls back to 80 columns when the stream reports no width', () => {
202 + const clamped = clampSpinnerMessage(streamWithColumns(undefined), 'x'.repeat(100));
203 + expect(clamped).toHaveLength(80 - 7);
204 + });
205 +
206 + it('keeps a minimum budget on absurdly narrow terminals', () => {
207 + const clamped = clampSpinnerMessage(streamWithColumns(4), 'x'.repeat(100));
208 + expect(clamped).toBe(`${'x'.repeat(7)}…`);
209 + });
210 +
211 + it('renders live spinner frames without wrapping in a narrow terminal', async () => {
212 + vi.useFakeTimers();
213 + try {
214 + const stdout = makeSink(true);
215 + const stderr = Object.assign(makeSink(true), { columns: 40 });
216 + const ui = makeTerminalUI({ stdin: { isTTY: true }, stdout, stderr });
217 +
218 + await Effect.runPromise(
219 + ui.useMakeSpinner(
220 + 'New version available: @composio/cli@0.4.0 (current: @composio/cli@0.3.2). Downloading...',
221 + spinner =>
222 + Effect.gen(function* () {
223 + yield* Effect.sync(() => vi.advanceTimersByTime(150));
224 + yield* spinner.stop('Upgrade completed!');
225 + })
226 + )
227 + );
228 +
229 + const frame = stderr.chunks.find(chunk => chunk.includes('…'));
230 + expect(frame).toBeDefined();
231 + expect(frame).toContain('New version available: @composio');
232 + // A wrapped frame is what leaks lines on every redraw tick.
233 + expect(frame).not.toContain('\n');
234 + } finally {
235 + vi.useRealTimers();
236 + }
237 + });
238 + });
239 +
240 layer(TestLive())(it => {
241 // -----------------------------------------------------------------------
242 // Data output