claudetui: a reply is held from its message's row on, not from when send sees it (#6887)

TestShimCutMessageIsUnpastable (new in efb62fe90) was red on ubuntu and
macOS CI at a24a5f6af, 6cc7402bd and ac4a4cf84: a cut message's reply
got out, and its Unpastable error came after it.

The transcript's reader counts a message's row (sawRow) and goes
straight on to the reply's row and emits it. send only began holding
output (checkWhole's hold) once it woke and saw the count, so a reader
that got there first let the reply out before the message was checked
whole. On a busy Windows 10 machine the model got the cut tail and the
user got its answer, which is what the check is for.

sawRow now begins the hold itself while a message is being checked
(said isn't nil), on the reader's own goroutine, before any later row
is read. hold keeps a buffer sawRow began, and send lets out what is
held if it fails after a row was counted but before its check.

Run (mica; mote's load was 37 of 18 cores):
- the old code with time.Sleep(50ms) between send seeing the count and
  checkWhole: TestShimCutMessageIsUnpastable -count=3, red 3 of 3, the
  same output as CI's ("a cut message ended [init, assistant echo: er
  words … K-TAIL]").
- this change, the same 50ms injected: -count=5 ok; 500ms: -count=3 ok.
- injection removed: go test -race ./internal/claudetui -count=2 ok
  (380s).
- Windows (xiaomi-gpu, win-run): go test ./internal/claudetui: only
  TestShimFirstMessageWaitsForTools red, 3 of 3 the same at origin/main
  1c59acd36 (#4810, open).
Not run: the full suite, and a real Windows 10 console.
This commit is contained in:
yetone
2026-11-17 01:33:13 +08:00
parent 1c59acd36d
commit 4c6c3a453e
2 changed files with 29 additions and 1 deletions
+11
View File
@@ -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:
+18 -1
View File
@@ -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).