From 3e49834c9daa0a965316a1c26bbb9df7e0b09908 Mon Sep 17 00:00:00 2001 From: Tam Nhu Tran Date: Fri, 14 Aug 2026 17:57:39 -0400 Subject: [PATCH] fix(delegation): flush stdout before exit to prevent piped output truncation console.log followed immediately by process.exit truncated piped output at the pipe buffer (about 64 KiB) because stdout writes to pipes are asynchronous. Interactive TTY runs were unaffected, so the bug only surfaced for callers consuming ccs delegation output through a pipe, such as scripts parsing --output-format json results from thinking heavy GLM sessions. Await an explicit stdout write callback before exiting in both route() and _handleContinue(). --- src/delegation/delegation-handler.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/delegation/delegation-handler.ts b/src/delegation/delegation-handler.ts index df9f4c6a..58994328 100644 --- a/src/delegation/delegation-handler.ts +++ b/src/delegation/delegation-handler.ts @@ -35,6 +35,17 @@ function isFlag(arg: string): boolean { return arg.startsWith('-'); } +/** + * Write to stdout and wait for the flush to complete. + * process.exit() right after a large piped write truncates output at the + * pipe buffer (~64 KiB), so delegation results must be drained first. + */ +function writeStdoutAndFlush(text: string): Promise { + return new Promise((resolve) => { + process.stdout.write(text + '\n', () => resolve()); + }); +} + function isInlineCcsValueFlag(arg: string): boolean { return Array.from(CCS_FLAGS_WITH_VALUE).some( (flag) => flag.startsWith('--') && arg.startsWith(`${flag}=`) @@ -147,7 +158,7 @@ export class DelegationHandler { // 5. Format and display results const formatted = await ResultFormatter.format(result); - console.log(formatted); + await writeStdoutAndFlush(formatted); // 6. Exit with proper code process.exit(result.exitCode || 0); @@ -192,7 +203,7 @@ export class DelegationHandler { }); const formatted = await ResultFormatter.format(result); - console.log(formatted); + await writeStdoutAndFlush(formatted); process.exit(result.exitCode || 0); }