@devries-cameron20 / composio Mirror · read-only / commits / 10f5e72ef
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, 04:45 UTC

fix(cli): keep going when the stdout reader goes away (#5361)

This PR: - part of [PRDE-2212](https://linear.app/composio/issue/PRDE-2212/cli-e2e-tests-fake-backend-operation-suites-and-agent-journeys-with) - builds on top of https://github.com/ComposioHQ/composio/pull/5360; split from https://github.com/ComposioHQ/composio/pull/5333 - awaits stdout writes and drops later data after EPIPE; other write failures still fail the command - stops listeners when stdout closes, drains accepted event work, and retains one stop message - replaces the EPIPE pin and expected failure with one passing operation test checking producer exit 0 and no EPIPE on stderr - passes the full CLI suite, CLI and workspace typechecks, Effect diagnostics, Oxlint, Prettier, and keyless Docker operations, whoami, and agent-signin suites The CLI test workflow runs for PRs targeting `next`, not this stack base; full CI completes after retargeting. The existing Docker scenarios cover these corrections; no recording changes are needed.

Alberto Schiabel committed Nov 16, 2026 at 13:12 UTC 10f5e72ef5846bdadee630372df6d90a39934d95
14 files changed +254 -39
ts/e2e-tests/cli/operations/README.md
+15 -15
@@ -102,21 +102,21 @@ Observed and left as is: on the backend transport an HTML error page is copied w
102
103 ### Process and streams (`process-streams.test.ts`)
104
105 -| Fault | Injected at | Must hold | Status |
106 -| -------------------------------------------------------- | ------------------------------ | -------------------------------------------------------------------------------- | ----------------------------------------------------- |
107 -| SIGINT / SIGTERM while `link` waits | `kill` once the URL is printed | exit 130 / 143; stdout empty; the link was requested once | ok |
108 -| SIGINT while `run` executes a script | `kill` once the script started | exit 130; stdout empty, also after the script would have finished | ok |
109 -| `link` off a terminal, nobody authorizes | no terminal | The authorization URL is printed before the wait | ok: on stderr, with a hint that names `--no-wait` |
110 -| `link --no-wait` | | exit 0; JSON with the URL; no status polling | ok |
111 -| Reader closes stdout before the CLI writes (`… \| true`) | pipe | CLI exits 0; no stderr | expected failure: exits 1 with an `EPIPE` stack trace |
112 -| `-d -` with JSON on stdin | pipe | Arguments read from stdin | ok |
113 -| `-d -` with nothing on stdin | `/dev/null` | exit 1 at once; stdout empty; no execution | ok |
114 -| No terminal on any stream | | stdout is the JSON; stderr empty | ok |
115 -| stdout piped, stderr a terminal | `script` | JSON in the pipe; human rendering on the terminal, not a second copy of the data | ok |
116 -| stdout a terminal, stderr redirected | `script` | No data on the terminal | ok |
117 -| Update notice pending | seeded `update-check.json` | Never on stdout; shown on a terminal stderr | ok |
118 -| Plugin hint under an agent host | `CLAUDECODE=1` | On stderr; never on stdout | ok |
119 -| `search --human` off a terminal | no terminal | Results are printed | expected failure: prints nothing, exits 0 |
105 +| Fault | Injected at | Must hold | Status |
106 +| -------------------------------------------------------- | ------------------------------ | -------------------------------------------------------------------------------- | ------------------------------------------------- |
107 +| SIGINT / SIGTERM while `link` waits | `kill` once the URL is printed | exit 130 / 143; stdout empty; the link was requested once | ok |
108 +| SIGINT while `run` executes a script | `kill` once the script started | exit 130; stdout empty, also after the script would have finished | ok |
109 +| `link` off a terminal, nobody authorizes | no terminal | The authorization URL is printed before the wait | ok: on stderr, with a hint that names `--no-wait` |
110 +| `link --no-wait` | | exit 0; JSON with the URL; no status polling | ok |
111 +| Reader closes stdout before the CLI writes (`… \| true`) | pipe | CLI exits 0; no stderr | ok |
112 +| `-d -` with JSON on stdin | pipe | Arguments read from stdin | ok |
113 +| `-d -` with nothing on stdin | `/dev/null` | exit 1 at once; stdout empty; no execution | ok |
114 +| No terminal on any stream | | stdout is the JSON; stderr empty | ok |
115 +| stdout piped, stderr a terminal | `script` | JSON in the pipe; human rendering on the terminal, not a second copy of the data | ok |
116 +| stdout a terminal, stderr redirected | `script` | No data on the terminal | ok |
117 +| Update notice pending | seeded `update-check.json` | Never on stdout; shown on a terminal stderr | ok |
118 +| Plugin hint under an agent host | `CLAUDECODE=1` | On stderr; never on stdout | ok |
119 +| `search --human` off a terminal | no terminal | Results are printed | expected failure: prints nothing, exits 0 |
120
121 ### Upgrade (`upgrade.test.ts`, and `../upgrade`)
122
ts/e2e-tests/cli/operations/process-streams.test.ts
+2 -11
@@ -153,20 +153,11 @@ describe('streams', () => {
153 const CLOSED_EARLY = `{ ${SEARCH_SLACK}; echo $? > /tmp/pipe.rc; } | true; cat /tmp/pipe.rc`;
154
155 it(
156 - 'today: a reader that closes stdout before the CLI writes makes it exit 1 with an EPIPE trace',
157 - async () => {
158 - const result = await rig.run(CLOSED_EARLY);
159 - expect(result.stdout).toBe('1\n');
160 - expect(result.stderr).toContain('EPIPE');
161 - },
162 - TEST_TIMEOUT_MS
163 - );
164 -
165 - // CLI defect: a closed stdout is reported as an EPIPE stack trace, and the command fails.
166 - it.failing(
156 'a reader that closes stdout early does not fail the command',
157 async () => {
158 const result = await rig.run(CLOSED_EARLY);
159 + expect(result.exitCode).toBe(0);
160 + expect(result.stdout).toBe('0\n');
161 expect(result.stderr).not.toContain('EPIPE');
162 },
163 TEST_TIMEOUT_MS
ts/packages/cli/AGENTS.md
+1 -1
@@ -162,7 +162,7 @@ The three streams are **independent contracts**. Each capability depends only on
162 Rules:
163
164 1. All `TerminalUI` methods **except `output()`** write to stderr via Clack's `{ output: process.stderr }`, and only when stderr is a TTY (`canDecorate`).
165 -2. `ui.output(data)` writes to stdout **only when stdout is piped** (or with explicit `force`). No other stream participates in that decision.
165 +2. `ui.output(data)` writes to stdout **only when stdout is piped** (or with explicit `force`). No other stream participates in that decision. A reader that closes stdout early (`… | head`) gets nothing more, and the command goes on as if it had read everything; a listener (`listen`, `dev triggers listen`) instead stops on `ui.stdoutClosed` and exits 0. Any other stdout write failure is a defect.
166 3. Prompts (`ui.confirm`, `ui.select`) run only when `canPrompt`; otherwise they fall back to their defaults without blocking.
167 4. Piped stdout stays clean: `composio whoami | pbcopy` puts only the key in the clipboard — decoration still renders on the terminal via stderr, and is suppressed only when stderr itself is captured.
168 5. **Data commands** (whoami, version, login, generate, etc.) call both decoration (stderr) and `ui.output()` (stdout).
ts/packages/cli/CHANGELOG.md
+9
@@ -4,6 +4,15 @@
4
5 ### Patch Changes
6
7 +- A command whose stdout reader stops reading early, as in
8 + `composio search "send a slack message" | head -1`, now drops the rest of
9 + its output and exits as it would have. It printed an `EPIPE: broken pipe`
10 + stack trace on stderr and exited 1, so a pipeline that only wanted the first
11 + lines looked like a failure. `composio listen` and `composio dev listen`
12 + stop listening at their next write once that reader is gone and exit 0,
13 + where they would keep running with nobody reading. Other stdout write
14 + failures still fail the command.
15 +
16 - `composio search` now fails with "The API response for
17 'toolRouter.session.search' is not in the shape this version of the CLI
18 expects" and `composio upgrade` as the way out when the API answers 200 with
ts/packages/cli/src/commands/listen/listen.handler.ts
+9 -1
@@ -498,12 +498,20 @@ export const runListen = (input: ListenParams) =>
498 }
499
500 // Ctrl-C or SIGTERM after the subscription is a clean stop: exit 0 after the release.
501 + // So is a reader that closed stdout (`| head -1`): nothing written after can reach it.
502 const stopReason = yield* Effect.raceAllFirst([
503 listenEffect,
504 Deferred.await(stopWhenDone),
505 Effect.as(shutdown.requested, 'signal' as const),
506 + Effect.as(ui.stdoutClosed, 'stdout-closed' as const),
507 ]);
506 - if (stopReason === 'max-events' || stopReason === 'timeout') {
508 + // Events already accepted still finish their --out and --forward steps; once stdout
509 + // is closed their own writes there are dropped.
510 + if (
511 + stopReason === 'max-events' ||
512 + stopReason === 'timeout' ||
513 + stopReason === 'stdout-closed'
514 + ) {
515 yield* finishAcceptedEvents(events, failed);
516 }
517 if (stopReason === 'max-events') {
ts/packages/cli/src/commands/triggers/commands/triggers.listen.cmd.ts
+8 -2
@@ -369,14 +369,20 @@ export const triggersCmd$Listen = Command.make(
369 const events = yield* FiberSet.make();
370 const runEvent = yield* FiberSet.runtime(events)();
371 // Without --max-events or --fail-fast nothing resolves stopWhenDone, so this waits on
372 - // the listener until Ctrl-C or SIGTERM stops it or another signal interrupts it.
372 + // the listener until Ctrl-C or SIGTERM stops it, a reader closes stdout
373 + // (`--json | head -1`), or another signal interrupts it.
374 const stopReason = yield* Effect.raceAllFirst([
375 listenEffect(runEvent),
376 Deferred.await(stopWhenDone),
377 shutdown.requested,
378 + Effect.as(ui.stdoutClosed, 'stdout-closed' as const),
379 ]);
378 - if (stopReason === 'max-events') {
380 + // Events already accepted still finish their --out and --forward steps; once stdout is
381 + // closed their own writes there are dropped.
382 + if (stopReason === 'max-events' || stopReason === 'stdout-closed') {
383 yield* finishAcceptedEvents(events, failed);
384 + }
385 + if (stopReason === 'max-events') {
386 yield* ui.outro(`Stopped after receiving ${matchingEvents} matching events.`);
387 }
388 }).pipe(
ts/packages/cli/src/services/terminal-ui.ts
+53 -6
@@ -3,7 +3,7 @@ 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, Data, Effect, Exit, Layer } from 'effect';
6 +import { Context, Data, Deferred, Effect, Exit, Layer, Predicate } from 'effect';
7
8 export type TtyLikeStream = {
9 readonly isTTY?: boolean;
@@ -99,10 +99,20 @@ export interface TerminalUI {
99 * state participates in this decision; redirecting stdin or stderr must never make
100 * data leak onto a visible stdout terminal.
101 *
102 + * It completes once the stream took the data. After a reader closed stdout early (EPIPE),
103 + * the data is dropped and the command goes on; any other write failure is a defect.
104 + *
105 * Use this for values that scripts should capture (API keys, version strings, etc.).
106 */
107 readonly output: (data: string, options?: { readonly force?: boolean }) => Effect.Effect<void>;
108
109 + /**
110 + * Completes once a reader closed stdout early (EPIPE), from which point `output` drops its
111 + * data; never completes otherwise. A command that keeps producing data until stopped (a
112 + * listener) races it, so it stops once nobody reads what it writes.
113 + */
114 + readonly stdoutClosed: Effect.Effect<void>;
115 +
116 /**
117 * Write an unformatted line to stderr even when stderr is redirected.
118 *
@@ -273,6 +283,10 @@ function createClackSpinnerHandle(
283 };
284 }
285
286 +/** Whether a write failed because the reader of the pipe went away. */
287 +const isBrokenPipe = (error: unknown): boolean =>
288 + Predicate.hasProperty(error, 'code') && error.code === 'EPIPE';
289 +
290 /** No-op spinner handle used when decoration is suppressed (stderr redirected). */
291 const silentSpinnerHandle: SpinnerHandle = {
292 message: () => Effect.void,
@@ -301,6 +315,41 @@ export const makeTerminalUI = (streams: TerminalUIStreams): TerminalUI => {
315 return Effect.sync(render);
316 };
317
318 + // Done once the reader of stdout went away: nothing written after can reach it.
319 + const stdoutClosed = Deferred.makeUnsafe<void>();
320 + // A failed write reaches its callback and is also emitted as an `error` event, which crashes
321 + // the process when nothing listens. `writeStdout` reports every failure from the callback, and
322 + // listens only once it writes, so a process that never writes data keeps the default.
323 + let ignoringStdoutErrorEvents = false;
324 +
325 + /**
326 + * Writes `chunk` to stdout once the stream took it. A reader that closed the pipe early
327 + * (EPIPE, as `head` can) chose to stop reading, so the data is dropped and the command goes
328 + * on; any other write failure is a defect.
329 + */
330 + const writeStdout = (chunk: string): Effect.Effect<void> =>
331 + Effect.suspend(() => {
332 + if (Deferred.isDoneUnsafe(stdoutClosed)) {
333 + return Effect.void;
334 + }
335 + if (!ignoringStdoutErrorEvents) {
336 + ignoringStdoutErrorEvents = true;
337 + streams.stdout.on('error', () => {});
338 + }
339 + return Effect.callback<void>(resume => {
340 + streams.stdout.write(chunk, error => {
341 + if (error === null || error === undefined) {
342 + resume(Effect.void);
343 + } else if (isBrokenPipe(error)) {
344 + Deferred.doneUnsafe(stdoutClosed, Effect.void);
345 + resume(Effect.void);
346 + } else {
347 + resume(Effect.die(error));
348 + }
349 + });
350 + });
351 + });
352 +
353 /** Start a clack spinner on stderr, clamped to a single terminal row. */
354 const startSpinner = (message: string): StartedClackSpinner => {
355 // Clack captures this width during construction and continues wrapping
@@ -320,11 +369,9 @@ export const makeTerminalUI = (streams: TerminalUIStreams): TerminalUI => {
369 capabilities: Effect.succeed(capabilities),
370
371 output: (data, options) =>
323 - Effect.sync(() => {
324 - if (options?.force || !stdoutIsTTY) {
325 - streams.stdout.write(`${data}\n`);
326 - }
327 - }),
372 + options?.force || !stdoutIsTTY ? writeStdout(`${data}\n`) : Effect.void,
373 +
374 + stdoutClosed: Deferred.await(stdoutClosed),
375
376 error: data => Effect.sync(() => stderr.write(`${data}\n`)),
377
ts/packages/cli/test/__utils__/services/redirected-terminal-ui.ts
+9 -2
@@ -2,11 +2,16 @@ import { Writable } from 'node:stream';
2 import { Effect, Layer } from 'effect';
3 import { makeTerminalUI, TerminalUI } from 'src/services/terminal-ui';
4
5 -const capture = () => {
5 +/** A redirected stream; with `readerClosed`, every write fails with EPIPE, as after `| head`. */
6 +const capture = (readerClosed = false) => {
7 const chunks: Array<string> = [];
8 const stream = Object.assign(
9 new Writable({
10 write(chunk, _encoding, callback) {
11 + if (readerClosed) {
12 + callback(Object.assign(new Error('EPIPE: broken pipe, write'), { code: 'EPIPE' }));
13 + return;
14 + }
15 chunks.push(String(chunk));
16 callback();
17 },
@@ -25,8 +30,10 @@ const capture = () => {
30 export const redirectedTerminalUI = (options?: {
31 /** Runs after a `log.warn` is logged — a signal that code behind the warn has run. */
32 readonly onWarn?: (message: string) => void;
33 + /** The reader of stdout went away before the first write (`… | head -0`). */
34 + readonly stdoutReaderClosed?: boolean;
35 }) => {
29 - const stdout = capture();
36 + const stdout = capture(options?.stdoutReaderClosed);
37 const stderr = capture();
38 const ui = makeTerminalUI({
39 stdin: { isTTY: false },
ts/packages/cli/test/__utils__/services/terminal-ui-test.ts
+1
@@ -24,6 +24,7 @@ export const terminalUITestImpl = TerminalUI.of({
24 // channel immune to it, matching production, where `ui.output()` never goes
25 // through the Effect `Console` service at all and so is never affected.
26 output: data => Console.info(data),
27 + stdoutClosed: Effect.never,
28 error: data => Console.error(data),
29
30 intro: title => Console.log(`-- ${title} --`),
ts/packages/cli/test/src/commands/connected-accounts/link/connected-accounts.link.cmd.test.ts
+1
@@ -80,6 +80,7 @@ const RecordingTerminalUI = TerminalUI.of({
80 data,
81 })
82 ),
83 + stdoutClosed: Effect.never,
84 error: data => Console.error(data),
85 intro: title => Console.log(title),
86 outro: message => Console.log(message),
ts/packages/cli/test/src/commands/listen/listen.cmd.test.ts
+14
@@ -132,6 +132,20 @@ describe('CLI: composio listen', () => {
132 })
133 );
134
135 + // No --max-events or --timeout: only the closed stdout can stop this run.
136 + it.effect('[When] the reader closes stdout [Then] it stops listening and exits 0', () =>
137 + Effect.gen(function* () {
138 + yield* enableListen;
139 + const terminal = redirectedTerminalUI({ stdoutReaderClosed: true });
140 + const exit = yield* Effect.exit(
141 + cli(['listen', 'composio.connected_account.expired']).pipe(Effect.provide(terminal.layer))
142 + );
143 +
144 + expect(exitCodeOf(exit)).toBe(0);
145 + expect(terminal.stdout()).toBe('');
146 + })
147 + );
148 +
149 it.effect('[Then] a bare --stream flag prints the whole event payload inline', () =>
150 Effect.gen(function* () {
151 yield* enableListen;
ts/packages/cli/test/src/commands/triggers/triggers.listen.cmd.test.ts
+37
@@ -336,4 +336,41 @@ describe('CLI: composio dev listen', () => {
336 })
337 );
338 });
339 +
340 + layer(
341 + TestLive({
342 + baseConfigProvider: testConfigProvider,
343 + fixture: 'global-test-user-id',
344 + realtimeData: {
345 + events: [
346 + {
347 + id: 'evt_conn_expired_1',
348 + type: 'composio.connected_account.expired',
349 + timestamp: '2026-04-06T18:00:00.000Z',
350 + metadata: {
351 + project_id: 'consumer_project_id_test',
352 + org_id: 'org_test',
353 + },
354 + data: {
355 + id: 'con_expired_1',
356 + },
357 + },
358 + ],
359 + },
360 + })
361 + )('[Given] a reader that closed stdout', it => {
362 + // No --max-events: only the closed stdout can stop this run, at the first event it writes.
363 + it.effect('[Then] it stops listening at the next write and exits 0', () =>
364 + Effect.gen(function* () {
365 + yield* enableListen;
366 + const terminal = redirectedTerminalUI({ stdoutReaderClosed: true });
367 + const exit = yield* Effect.exit(
368 + cli(['dev', 'listen', '--json']).pipe(Effect.provide(terminal.layer))
369 + );
370 +
371 + expect(exitCodeOf(exit)).toBe(0);
372 + expect(terminal.stdout()).toBe('');
373 + })
374 + );
375 + });
376 });
ts/packages/cli/test/src/services/terminal-ui.test.ts
+94 -1
@@ -4,7 +4,8 @@ import { beforeEach, vi } from 'vitest';
4 import * as p from '@clack/prompts';
5 import { CANCEL_SYMBOL } from '@clack/core';
6 import stringWidth from 'fast-string-width';
7 -import { Data, Effect } from 'effect';
7 +import { assert } from '@effect/vitest';
8 +import { Cause, Data, Effect, Exit, Fiber } from 'effect';
9 import {
10 clampSpinnerMessage,
11 getTerminalCapabilities,
@@ -185,6 +186,98 @@ describe('TerminalUI', () => {
186 );
187 });
188
189 + describe('a stdout write that fails', () => {
190 + /**
191 + * A redirected stdout whose every write fails with `code`, as the OS reports it. An `error`
192 + * event nothing listens to, which would crash the process, is recorded in `unhandled`.
193 + */
194 + const failingStdout = (code: string) => {
195 + const attempts: string[] = [];
196 + const unhandled: unknown[] = [];
197 + const sink = new Writable({
198 + write(chunk, _encoding, callback) {
199 + attempts.push(String(chunk));
200 + callback(Object.assign(new Error(`${code}: write failed`), { code }));
201 + },
202 + });
203 + const emit = sink.emit.bind(sink);
204 + sink.emit = ((event: string | symbol, ...args: unknown[]) => {
205 + if (event === 'error' && sink.listenerCount('error') === 0) {
206 + unhandled.push(args[0]);
207 + return false;
208 + }
209 + return emit(event, ...args);
210 + }) as typeof sink.emit;
211 + return Object.assign(sink, { isTTY: false, attempts, unhandled });
212 + };
213 +
214 + /** Lets the stream emit what it deferred to the next turn of the event loop. */
215 + const settle = Effect.promise(() => new Promise<void>(resolve => setImmediate(resolve)));
216 +
217 + it.effect('drops the data once the reader closed stdout (EPIPE), and the command goes on', () =>
218 + Effect.gen(function* () {
219 + const stdout = failingStdout('EPIPE');
220 + const ui = makeTerminalUI({ stdin: { isTTY: false }, stdout, stderr: makeSink(false) });
221 +
222 + yield* ui.output('{"first":true}');
223 + yield* ui.output('{"second":true}');
224 +
225 + yield* settle;
226 +
227 + // Nothing more can reach a reader that went away, and its error does not crash the CLI.
228 + expect(stdout.attempts).toEqual(['{"first":true}\n']);
229 + expect(stdout.unhandled).toEqual([]);
230 + })
231 + );
232 +
233 + it.effect('completes stdoutClosed once the reader closed stdout (EPIPE)', () =>
234 + Effect.gen(function* () {
235 + const ui = makeTerminalUI({
236 + stdin: { isTTY: false },
237 + stdout: failingStdout('EPIPE'),
238 + stderr: makeSink(false),
239 + });
240 + const closed = yield* Effect.forkChild(ui.stdoutClosed);
241 + yield* Effect.yieldNow;
242 + expect(closed.pollUnsafe()).toBeUndefined();
243 +
244 + yield* ui.output('{"first":true}');
245 +
246 + expect(Exit.isSuccess(yield* Fiber.await(closed))).toBe(true);
247 + })
248 + );
249 +
250 + it.effect('leaves stdoutClosed pending while the reader takes the data', () =>
251 + Effect.gen(function* () {
252 + const ui = makeTerminalUI({
253 + stdin: { isTTY: false },
254 + stdout: makeSink(false),
255 + stderr: makeSink(false),
256 + });
257 + const closed = yield* Effect.forkChild(ui.stdoutClosed);
258 +
259 + yield* ui.output('{"first":true}');
260 + yield* settle;
261 +
262 + expect(closed.pollUnsafe()).toBeUndefined();
263 + yield* Fiber.interrupt(closed);
264 + })
265 + );
266 +
267 + it.effect('is a defect for any other failure', () =>
268 + Effect.gen(function* () {
269 + const stdout = failingStdout('EIO');
270 + const ui = makeTerminalUI({ stdin: { isTTY: false }, stdout, stderr: makeSink(false) });
271 +
272 + const exit = yield* Effect.exit(ui.output('{"machine":"data"}'));
273 +
274 + assert(Exit.isFailure(exit));
275 + expect(Cause.hasDies(exit.cause)).toBe(true);
276 + expect(Cause.squash(exit.cause)).toMatchObject({ code: 'EIO' });
277 + })
278 + );
279 + });
280 +
281 describe('clampSpinnerMessage keeps live spinner messages on one terminal row', () => {
282 it('leaves a message that fits untouched', () => {
283 expect(clampSpinnerMessage(80, 'Checking for updates')).toBe('Checking for updates');
ts/packages/cli/test/src/services/upgrade-binary.test.ts
+1
@@ -34,6 +34,7 @@ const TerminalUINoop = Layer.succeed(
34 })
35 ),
36 output: () => Effect.void,
37 + stdoutClosed: Effect.never,
38 error: () => Effect.void,
39 intro: () => Effect.void,
40 outro: () => Effect.void,