diff --git a/internal/claudetui/tui.go b/internal/claudetui/tui.go index 72991c1d9..54cbdbd1e 100644 --- a/internal/claudetui/tui.go +++ b/internal/claudetui/tui.go @@ -1273,6 +1273,8 @@ func (s *shim) send(pieces []piece) error { s.said = []heardRow{} // kept from now on (heard), till send returns s.rowsMu.Unlock() defer s.heardAll() + // a row counted (sawRow) while send failed before its check is let out + defer s.letHeld() var pasted strings.Builder // the words pasted, to check them whole images := 0 marker := 0 @@ -1553,11 +1555,20 @@ func (s *shim) transcript() string { // result (renderParts). const userMark = "[user message]\n" +// sawRow counts a message's row. While one sent is checked (said isn't +// nil), what comes after the row is held from here, on the transcript's +// own reader: the reply is read right after its message's row, and held +// only once send saw the count, a reply got out before its message was +// checked whole (#6887). func (s *shim) sawRow() { s.rowsMu.Lock() s.rows++ s.busy = true + checking := s.said != nil s.rowsMu.Unlock() + if checking { + s.hold() + } select { case s.rowsCh <- struct{}{}: default: diff --git a/internal/claudetui/whole.go b/internal/claudetui/whole.go index 827da48f0..fab7352e2 100644 --- a/internal/claudetui/whole.go +++ b/internal/claudetui/whole.go @@ -457,12 +457,29 @@ func (s *shim) tell(v any) { // quick model answered words cut, its result let out, within the time // waited for the message's other rows (wholeWait): the check's error came // after the turn had ended (Haiku, 64K on Windows 10's console). +// +// What sawRow began holding is kept: a reply read before the check began +// stays in it. What a failed check dropped (io.Discard) is held anew. func (s *shim) hold() { s.outMu.Lock() - s.held = new(bytes.Buffer) + if _, ok := s.held.(*bytes.Buffer); !ok { + s.held = new(bytes.Buffer) + } s.outMu.Unlock() } +// letHeld lets out what is held for a message that was never checked: +// send failed after sawRow began holding (a check's own end, unhold, has +// already ended the hold either way). +func (s *shim) letHeld() { + s.outMu.Lock() + defer s.outMu.Unlock() + if b, ok := s.held.(*bytes.Buffer); ok { + _, _ = s.out.Write(b.Bytes()) + s.held = nil + } +} + // unhold lets out what was held when the message was whole (err nil), // and drops it, and all after it, when it wasn't: the turn's failure is // told past it (tell).