nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] Pasting into the composer, then typing immediately, silently deletes the pasted content

Open addyCooks opened this issue 2 months ago • 1 comments

Description

Pasting a multi-line block into the composer and then typing even a single character right afterward, with no deliberate pause, causes the entire paste to silently disappear. No error, no warning; the composer just ends up containing only what you typed after the paste.

Note: This is specific to feature/new-ui's composer state handling. Please target any fix at feature/new-ui, not main.

Parent: #907

Environment

  • Reproduced via ink-testing-library against the real UserInput component (stdin.write() driving actual paste + keystroke sequences), and is expected to reproduce in a live terminal session the same way.
  • Provider/model: not relevant, this is purely composer/input state.

Steps to Reproduce

  1. Start Nanocoder on feature/new-ui (pnpm run start).
  2. Copy a multi-line block of text/code to your clipboard.
  3. Paste it into the composer.
  4. Immediately continue typing, e.g. append a short sentence right after the paste, without pausing. (This is timing-sensitive; if it doesn't reproduce on the first try, repeat a couple of times, the window is a short race, not guaranteed on every attempt.)

Expected Behavior

The pasted content stays in the composer as a [Paste #N: X chars] placeholder, with whatever you typed afterward appended to it.

Actual Behavior

The pasted content vanishes entirely. Only the text typed after the paste remains in the composer.

Captured via automated timing sweep (gap between paste and next keystroke):

gap 0ms -> "x" <- pasted content GONE gap 8ms -> "x" <- pasted content GONE gap 16ms -> "PASTED1\nPASTED2x" <- survives once state settles

The same race also swallows the first Enter pressed right after a paste (Enter appears to do nothing).

Root Cause

insertPaste (source/components/user-input.tsx:191-204) commits through pushToUndoStack, which reads a stale currentState snapshot rather than the state React has actually committed. The very next onChange event from TextInput rebuilds the input value from that stale pre-paste snapshot, overwriting the placeholder that was just inserted. See source/hooks/useInputState.ts:276-305.

Suggested Fix

Make useInputState's update path functional (setCurrentState(prev => ...)) instead of computing the next state from a captured currentState variable, so a paste commit can't be clobbered by the very next keystroke's state update.

Additional Context

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] I can reproduce this issue consistently (timing-dependent, not on every attempt)
  • [x] This issue occurs on the latest feature/new-ui

addyCooks avatar Sep 12 '26 06:09 addyCooks

Hi @akramcodez, could you assign this to me?

addyCooks avatar Sep 12 '26 06:09 addyCooks