-
Notifications
You must be signed in to change notification settings - Fork 13.2k
feat(cli): allow -i/--prompt-interactive with piped stdin #23414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
GoBeromsu
wants to merge
4
commits into
google-gemini:main
Choose a base branch
from
GoBeromsu:feat/headless-interactive
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e33073b
feat(cli): add readStdinLines utility for safe piped stdin reading
GoBeromsu d6b6ea8
feat(cli): allow -i/--prompt-interactive to work with piped stdin
GoBeromsu 0b1d608
fix(cli): use byte-accurate UTF-8 limits in readStdinLines
GoBeromsu f9ab613
fix(cli): truncate before checking totalSize in EOF flush path
GoBeromsu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { vi, describe, expect, it } from 'vitest'; | ||
| import { readStdinLines } from './readStdinLines.js'; | ||
| import { PassThrough } from 'node:stream'; | ||
|
|
||
| vi.mock('@google/gemini-cli-core', () => ({ | ||
| debugLogger: { | ||
| warn: vi.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| /** Helper: collect all values from the async generator. */ | ||
| async function collect(gen: AsyncGenerator<string>): Promise<string[]> { | ||
| const results: string[] = []; | ||
| for await (const line of gen) { | ||
| results.push(line); | ||
| } | ||
| return results; | ||
| } | ||
|
|
||
| /** Helper: create a PassThrough stream and push lines into it. */ | ||
| function createStream(lines: string[]): PassThrough { | ||
| const stream = new PassThrough(); | ||
| for (const line of lines) { | ||
| stream.write(line); | ||
| } | ||
| stream.end(); | ||
| return stream; | ||
| } | ||
|
|
||
| describe('readStdinLines', () => { | ||
| it('should yield each non-empty line from piped input', async () => { | ||
| const stream = createStream(['hello\n', 'world\n']); | ||
| const result = await collect(readStdinLines(stream)); | ||
| expect(result).toEqual(['hello', 'world']); | ||
| }); | ||
|
|
||
| it('should skip empty lines', async () => { | ||
| const stream = createStream(['hello\n', '\n', '\n', 'world\n']); | ||
| const result = await collect(readStdinLines(stream)); | ||
| expect(result).toEqual(['hello', 'world']); | ||
| }); | ||
|
|
||
| it('should trim whitespace from lines', async () => { | ||
| const stream = createStream([' hello \n', ' world \n']); | ||
| const result = await collect(readStdinLines(stream)); | ||
| expect(result).toEqual(['hello', 'world']); | ||
| }); | ||
|
|
||
| it('should handle input without trailing newline', async () => { | ||
| const stream = createStream(['hello\n', 'world']); | ||
| const result = await collect(readStdinLines(stream)); | ||
| expect(result).toEqual(['hello', 'world']); | ||
| }); | ||
|
|
||
| it('should yield nothing for empty stream', async () => { | ||
| const stream = createStream([]); | ||
| const result = await collect(readStdinLines(stream)); | ||
| expect(result).toEqual([]); | ||
| }); | ||
|
|
||
| it('should handle multi-byte UTF-8 characters (CJK)', async () => { | ||
| const stream = createStream(['한글 테스트\n', '日本語\n']); | ||
| const result = await collect(readStdinLines(stream)); | ||
| expect(result).toEqual(['한글 테스트', '日本語']); | ||
| }); | ||
|
|
||
| it('should handle emoji (4-byte UTF-8)', async () => { | ||
| const stream = createStream(['hello 😀🎉\n', 'world 🚀\n']); | ||
| const result = await collect(readStdinLines(stream)); | ||
| expect(result).toEqual(['hello 😀🎉', 'world 🚀']); | ||
| }); | ||
|
|
||
| it('should handle chunks split across multiple writes', async () => { | ||
| const stream = new PassThrough(); | ||
| stream.write('hel'); | ||
| stream.write('lo\nwor'); | ||
| stream.write('ld\n'); | ||
| stream.end(); | ||
| const result = await collect(readStdinLines(stream)); | ||
| expect(result).toEqual(['hello', 'world']); | ||
| }); | ||
|
|
||
| it('should stop reading when total size exceeds cumulative limit', async () => { | ||
| // Create lines that accumulate past the 8MB total limit. | ||
| // Use a small stream with known byte sizes to verify the check fires. | ||
| const oneMB = 'a'.repeat(1024 * 1024); | ||
| const lines: string[] = []; | ||
| for (let i = 0; i < 10; i++) { | ||
| lines.push(oneMB + '\n'); | ||
| } | ||
| const stream = createStream(lines); | ||
| const result = await collect(readStdinLines(stream)); | ||
| // 8 lines of ~1MB each should fit; the 9th should be rejected | ||
| expect(result.length).toBeLessThanOrEqual(8); | ||
| expect(result.length).toBeGreaterThanOrEqual(7); | ||
| }); | ||
|
|
||
| it('should truncate oversized lines at valid UTF-8 boundary', async () => { | ||
| // A line of ~9MB of 3-byte Korean characters | ||
| const bigLine = '한'.repeat(3 * 1024 * 1024) + '\n'; // 9MB in UTF-8 | ||
| const stream = createStream([bigLine]); | ||
| const result = await collect(readStdinLines(stream)); | ||
| expect(result.length).toBe(1); | ||
| // The truncated line should be valid UTF-8 and <= 8MB | ||
| const resultBytes = Buffer.byteLength(result[0], 'utf8'); | ||
| expect(resultBytes).toBeLessThanOrEqual(8 * 1024 * 1024); | ||
| // Should not end with a broken character (no replacement chars) | ||
| expect(result[0]).not.toContain('\uFFFD'); | ||
| }); | ||
|
|
||
| it('should handle oversized buffer without newline (flush path)', async () => { | ||
| // Write >8MB without any newline to trigger the flush path | ||
| const bigChunk = 'x'.repeat(9 * 1024 * 1024); | ||
| const stream = createStream([bigChunk]); | ||
| const result = await collect(readStdinLines(stream)); | ||
| expect(result.length).toBe(1); | ||
| const resultBytes = Buffer.byteLength(result[0], 'utf8'); | ||
| expect(resultBytes).toBeLessThanOrEqual(8 * 1024 * 1024); | ||
| }); | ||
|
|
||
| it('should track totalSize consistently with post-truncation bytes', async () => { | ||
| // A 9MB CJK line followed by a small line. | ||
| // With post-truncation tracking, the truncated 8MB line leaves 0 budget, | ||
| // so the second line should be dropped. | ||
| const bigLine = '한'.repeat(3 * 1024 * 1024) + '\n'; // ~9MB UTF-8 | ||
| const smallLine = 'hello\n'; | ||
| const stream = createStream([bigLine, smallLine]); | ||
| const result = await collect(readStdinLines(stream)); | ||
| // First line truncated to ~8MB, second should be dropped (totalSize exceeded) | ||
| expect(result.length).toBe(1); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, the new piped stdin path has no error handling for broken pipes
Since this PR specifically targets long-running programmatic sessions, the pipe can break at any point - e.g., the parent process crashes, or the network drops in a remote session.
The existing
readStdin.tsalready handles this with an explicitonErrorcallback (lines 53-56) and a safety net for late errors (lines 70-72). But the newreadStdinLines.tsuses a barefor await...ofatline 49with no error handling:The consumer in
gemini.tsxalso has no try-catch around the loop atline 659:So when the pipe breaks, the error flies past the cleanup code at
lines 690-692(runExitCleanup()+process.exit), which means telemetry doesn't flush, SessionEnd hooks don't fire, and temp files aren't cleaned up.A lightweight fix would be wrapping the loop in
gemini.tsx:This way, a broken pipe logs the error and falls through to the existing cleanup path - same graceful behavior that
readStdin.tsalready provides.