Skip to content

fix(prompt): preserve buffered input between prompts#118

Merged
zeshi-du merged 1 commit into
TestSprite:mainfrom
cmdr-chara:fix/prompt-buffered-input
Jul 2, 2026
Merged

fix(prompt): preserve buffered input between prompts#118
zeshi-du merged 1 commit into
TestSprite:mainfrom
cmdr-chara:fix/prompt-buffered-input

Conversation

@cmdr-chara

@cmdr-chara cmdr-chara commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes prompt input handling so answers already buffered after the first newline are preserved for later prompts on the same input stream.

Why

When input arrives as one buffered chunk, such as first\nsecond\n, the prompt reader returned first and discarded the remaining text. A second prompt on the same stream would then wait for input that had already been read.

Changes

  • Preserve unconsumed text after CR/LF terminators per input stream.
  • Treat CRLF as one newline so sequential prompts do not receive an empty answer.
  • Resolve buffered tail input when the stream has already ended.
  • Add regression tests for sequential text prompts, EOF tail input, CRLF input, and secret prompts.

Testing

  • npx vitest run src/lib/prompt.test.ts --reporter=dot
  • npm run typecheck
  • npm run build

Summary by CodeRabbit

  • Bug Fixes
    • Improved prompt input handling so sequential prompts on the same stream now correctly preserve and reuse buffered answers.
    • Fixed cases where the next prompt could miss leftover input at end-of-file or when using different line endings.
    • Ensured secret prompts continue to accept buffered input across multiple prompts without echoing answers to the screen.

@coderabbitai

coderabbitai Bot commented Jul 1, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 5223582d-b60b-4936-a2e3-03472ee728d5

📥 Commits

Reviewing files that changed from the base of the PR and between 3ab8136 and 84a25d9.

📒 Files selected for processing (2)
  • src/lib/prompt.test.ts
  • src/lib/prompt.ts

Walkthrough

The readLine input handling in src/lib/prompt.ts is refactored to buffer unread stream data in a WeakMap keyed by stream, allowing sequential prompts sharing a stream to reuse previously read input. Listener cleanup is made conditional, and tests were added covering buffered reuse for promptText and promptSecret.

Changes

Sequential prompt input buffering

Layer / File(s) Summary
Pending input storage and data handling
src/lib/prompt.ts
A WeakMap (pendingPromptInput) stores unconsumed text per stream; the data handler is reworked into an indexed processText loop that saves remainder text after a newline into the pending store before finishing, and character accumulation now indexes into the processed string.
Listener lifecycle and pending input resumption
src/lib/prompt.ts
cleanup() only detaches listeners when a listening flag is active; readLine checks for and consumes pending input at start (including early finish() when the stream has ended), and a new isInputEnded helper gates resume-time control flow.
Tests for buffered sequential prompts
src/lib/prompt.test.ts
New promptText tests cover LF/CRLF-terminated sequential answers and buffered EOF tail reuse; a new promptSecret test verifies buffered secret reuse across prompts and confirms secrets are not echoed to output.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant readLine
  participant pendingPromptInput
  participant Stream

  Caller->>readLine: prompt for answer
  readLine->>pendingPromptInput: check pending text for stream
  alt pending text exists
    readLine->>readLine: process pending text, isInputEnded check
    readLine->>Caller: finish() with buffered answer
  else no pending text
    readLine->>Stream: attach data/end/error listeners
    Stream->>readLine: emit data chunk
    readLine->>readLine: processText loop, detect newline
    readLine->>pendingPromptInput: save remainder after newline
    readLine->>Caller: finish() with answer
    readLine->>Stream: cleanup() detaches listeners
  end
Loading

Related Issues: None referenced in the provided changes.

Related PRs: None referenced in the provided changes.

Suggested labels: bug, enhancement

Suggested reviewers: None determinable from the provided changes.

🐰 A prompt once lost its trailing bytes,
now tucked away for future sights,
each stream remembers what it read,
so answers queue instead of dread,
the rabbit hops through lines anew,
buffered whispers carried through.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main fix: preserving buffered prompt input across sequential prompts.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@zeshi-du zeshi-du left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed and verified — merging. Thanks for the contribution!

@zeshi-du zeshi-du merged commit dfdee5a into TestSprite:main Jul 2, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants