@devries-cameron20 / composio Mirror · read-only / commits / 4e45d7fab
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.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 13:30 UTC

fix(cli): clamp spinner messages by display width

Follow-ups from review of the narrow-terminal spinner fix. - Budget and truncate by display columns instead of UTF-16 code units. Clack wraps the rendered frame by width, so a CJK message sat under a `.length` budget and wrapped anyway, reinstating the leak. Walking whole code points also keeps the cut off a surrogate pair. - Drop MIN_SPINNER_MESSAGE_COLUMNS. The 8-column floor exceeded the row below ~15 columns and restored the scroll it was meant to prevent. The budget now degrades to the ellipsis, so the one-row invariant holds wherever it can hold at all. - Cover the paths production actually drives: live `message()` updates and `withSpinner`'s start message. Reverting either clamp previously left the whole suite green. - Advance fake timers past clack's dot animation so the three dot columns SPINNER_RENDER_OVERHEAD reserves are exercised, and run these cases through `it.live` per the package's Effect test convention. - Add the CHANGELOG entry this user-facing fix needs; `@composio/cli` is changeset-ignored, so release notes go there directly. Claude-Session: https://claude.ai/code/session_0191yFPnWgJnzPPnCYrVm6X7

jkomyno committed Aug 28, 2026 at 18:39 UTC 4e45d7fabe47c583a47dde085436dd0d401597f4
5 files changed +156 -31
pnpm-lock.yaml
+3
@@ -1221,6 +1221,9 @@ importers:
1221 extract-zip:
1222 specifier: ^2.0.1
1223 version: 2.0.1(supports-color@10.2.2)
1224 + fast-string-width:
1225 + specifier: ^3.0.2
1226 + version: 3.0.2
1227 indent-string:
1228 specifier: ^5.0.0
1229 version: 5.0.0
ts/packages/cli/CHANGELOG.md
+4
@@ -4,6 +4,10 @@
4
5 ### Patch Changes
6
7 +- `composio upgrade` (and any other command with a long spinner message) no
8 + longer scrolls endlessly in terminals narrower than that message. The spinner
9 + printed its line hundreds of times instead of animating in place; live spinner
10 + text is now clamped to a single terminal row.
11 - `composio upgrade` shows download progress. The archive is a few hundred
12 megabytes, and the command previously printed `Downloading...` once and then
13 said nothing for minutes, which was indistinguishable from a hang. It now
ts/packages/cli/package.json
+1
@@ -90,6 +90,7 @@
90 "@zed-industries/codex-acp": "^0.16.0",
91 "effect": "catalog:",
92 "extract-zip": "^2.0.1",
93 + "fast-string-width": "^3.0.2",
94 "indent-string": "^5.0.0",
95 "js-tiktoken": "^1.0.21",
96 "json5": "^2.2.3",
ts/packages/cli/src/services/terminal-ui.ts
+26 -3
@@ -2,6 +2,7 @@ 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 stringWidth from 'fast-string-width';
6 import { Context, Effect, Exit, Layer } from 'effect';
7
8 export type TtyLikeStream = {
@@ -195,16 +196,38 @@ type TerminalUIStreams = {
196 // every tick leaks one: an endless scroll in terminals narrower than the
197 // message. Keep live spinner messages to a single terminal row so the redraw
198 // arithmetic cannot diverge.
199 +//
200 +// The invariant holds for terminals of at least SPINNER_RENDER_OVERHEAD + 1
201 +// columns. Below that the frame prefix alone overflows the row and no message
202 +// length can keep it on one line, so the budget degrades to just the ellipsis.
203 const SPINNER_RENDER_OVERHEAD = 7; // frame + two spaces (3) + animated dots (3) + last-column safety (1)
199 -const MIN_SPINNER_MESSAGE_COLUMNS = 8;
204
205 export const clampSpinnerMessage = (output: Writable, message: string): string => {
206 const singleLine = message.replace(/\s*\r?\n\s*/g, ' ');
207 if (singleLine.includes('\u001b')) {
208 return singleLine; // truncating could split an ANSI escape sequence
209 }
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;
210 +
211 + const budget = Math.max(getColumns(output) - SPINNER_RENDER_OVERHEAD, 1);
212 + if (stringWidth(singleLine) <= budget) {
213 + return singleLine;
214 + }
215 +
216 + // Measure in display columns, not UTF-16 code units: clack wraps the rendered
217 + // frame by width, so a CJK character it counts as two columns would slip past
218 + // a `.length` budget and wrap anyway. Accumulating whole code points also keeps
219 + // the cut off a surrogate pair. One column is reserved for the ellipsis.
220 + let clamped = '';
221 + let width = 0;
222 + for (const char of singleLine) {
223 + const charWidth = stringWidth(char);
224 + if (width + charWidth > budget - 1) {
225 + break;
226 + }
227 + clamped += char;
228 + width += charWidth;
229 + }
230 + return `${clamped}…`;
231 };
232
233 function createClackSpinnerHandle(
ts/packages/cli/test/src/services/terminal-ui.test.ts
+122 -28
@@ -2,6 +2,7 @@ import { Writable } from 'node:stream';
2 import { describe, expect, it, layer } from '@effect/vitest';
3 import { beforeEach, vi } from 'vitest';
4 import * as p from '@clack/prompts';
5 +import stringWidth from 'fast-string-width';
6 import { Array as Arr, Data, Effect, Exit, pipe } from 'effect';
7 import {
8 clampSpinnerMessage,
@@ -203,38 +204,131 @@ describe('TerminalUI', () => {
204 expect(clamped).toHaveLength(80 - 7);
205 });
206
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)}…`);
207 + it('measures display columns, not UTF-16 code units', () => {
208 + // 20 CJK characters are 20 code units but 40 display columns, so at 40
209 + // columns they sit under a `.length` budget of 33 and over a width one.
210 + // A length-based clamp would pass them through and let the frame wrap.
211 + const message = '\u4f60\u597d'.repeat(10);
212 + expect(message).toHaveLength(20);
213 + expect(stringWidth(message)).toBe(40);
214 +
215 + const clamped = clampSpinnerMessage(streamWithColumns(40), message);
216 + expect(clamped.endsWith('…')).toBe(true);
217 + expect(stringWidth(clamped)).toBeLessThanOrEqual(40 - 7);
218 });
219
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 - );
220 + it('never cuts a surrogate pair in half', () => {
221 + const clamped = clampSpinnerMessage(streamWithColumns(20), '\u{1f600}'.repeat(40));
222 + expect(clamped).not.toMatch(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])/);
223 + expect(stringWidth(clamped)).toBeLessThanOrEqual(20 - 7);
224 + });
225 +
226 + it('degrades to the ellipsis on terminals narrower than the frame overhead', () => {
227 + // Below SPINNER_RENDER_OVERHEAD + 1 columns no message length fits, so the
228 + // budget bottoms out at one column rather than jumping back up to eight.
229 + expect(clampSpinnerMessage(streamWithColumns(4), 'x'.repeat(100))).toBe('…');
230 + });
231
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 - }
232 + it('keeps the rendered frame within the row at the exact budget boundary', () => {
233 + const columns = 20;
234 + const clamped = clampSpinnerMessage(streamWithColumns(columns), 'x'.repeat(100));
235 + // frame (1) + two spaces (2) + message + three animated dots (3).
236 + expect(1 + 2 + stringWidth(clamped) + 3).toBeLessThanOrEqual(columns);
237 });
238 +
239 + // Clack ticks every 80ms and grows its animated dots by 0.125 per tick, so
240 + // reaching the three dots SPINNER_RENDER_OVERHEAD reserves takes 24 ticks.
241 + // A shorter advance renders a frame with no dots and never exercises them.
242 + const TICKS_PAST_THREE_DOTS = 2000;
243 +
244 + const LONG_UPGRADE_MESSAGE =
245 + 'New version available: @composio/cli@0.4.0 (current: @composio/cli@0.3.2). Downloading...';
246 +
247 + const narrowUi = (columns: number) => {
248 + const stdout = makeSink(true);
249 + const stderr = Object.assign(makeSink(true), { columns });
250 + const ui = makeTerminalUI({ stdin: { isTTY: true }, stdout, stderr });
251 + return { ui, stderr };
252 + };
253 +
254 + /** Every frame clack redrew, i.e. the writes that carry the clamped message. */
255 + const clampedFrames = (stderr: { chunks: string[] }) =>
256 + stderr.chunks.filter(chunk => chunk.includes('…'));
257 +
258 + /** Drives a real clack spinner with fake timers, then always restores them. */
259 + const withFakeTimers = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
260 + Effect.acquireUseRelease(
261 + Effect.sync(() => vi.useFakeTimers()),
262 + () => effect,
263 + () => Effect.sync(() => vi.useRealTimers())
264 + );
265 +
266 + it.live('renders live spinner frames without wrapping in a narrow terminal', () =>
267 + withFakeTimers(
268 + Effect.gen(function* () {
269 + const { ui, stderr } = narrowUi(40);
270 +
271 + yield* ui.useMakeSpinner(LONG_UPGRADE_MESSAGE, spinner =>
272 + Effect.gen(function* () {
273 + yield* Effect.sync(() => vi.advanceTimersByTime(TICKS_PAST_THREE_DOTS));
274 + yield* spinner.stop('Upgrade completed!');
275 + })
276 + );
277 +
278 + const frames = clampedFrames(stderr);
279 + expect(frames.length).toBeGreaterThan(0);
280 + expect(frames[0]).toContain('New version available: @composio');
281 + // A wrapped frame is what leaks lines on every redraw tick.
282 + for (const frame of frames) {
283 + expect(frame).not.toContain('\n');
284 + }
285 + })
286 + )
287 + );
288 +
289 + it.live('clamps live message() updates, not just the start message', () =>
290 + withFakeTimers(
291 + Effect.gen(function* () {
292 + const { ui, stderr } = narrowUi(40);
293 +
294 + yield* ui.useMakeSpinner('Checking for updates...', spinner =>
295 + Effect.gen(function* () {
296 + // The upgrade flow drives its longest text through message(), not
297 + // start() — reverting the clamp there must fail this test.
298 + yield* spinner.message(LONG_UPGRADE_MESSAGE);
299 + yield* Effect.sync(() => vi.advanceTimersByTime(TICKS_PAST_THREE_DOTS));
300 + yield* spinner.stop('Upgrade completed!');
301 + })
302 + );
303 +
304 + const frames = clampedFrames(stderr);
305 + expect(frames.length).toBeGreaterThan(0);
306 + expect(frames[0]).toContain('New version available: @composio');
307 + for (const frame of frames) {
308 + expect(frame).not.toContain('\n');
309 + }
310 + })
311 + )
312 + );
313 +
314 + it.live('clamps the withSpinner start message too', () =>
315 + withFakeTimers(
316 + Effect.gen(function* () {
317 + const { ui, stderr } = narrowUi(40);
318 +
319 + yield* ui.withSpinner(
320 + LONG_UPGRADE_MESSAGE,
321 + Effect.sync(() => vi.advanceTimersByTime(TICKS_PAST_THREE_DOTS))
322 + );
323 +
324 + const frames = clampedFrames(stderr);
325 + expect(frames.length).toBeGreaterThan(0);
326 + for (const frame of frames) {
327 + expect(frame).not.toContain('\n');
328 + }
329 + })
330 + )
331 + );
332 });
333
334 layer(TestLive())(it => {