diff --git a/internal/channels/telegram/live.go b/internal/channels/telegram/live.go index 62ad78c45..bd43a9e38 100644 --- a/internal/channels/telegram/live.go +++ b/internal/channels/telegram/live.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "slices" "strings" "sync" "time" @@ -675,54 +676,63 @@ func (l *liveReply) framedText(answer string) string { return block + "\n\n" + l.clampAnswer(answer, budget) } -// clampAnswer lets the opening frame use its entire answer budget. Later -// frames mark a cut with an ellipsis, while still retaining the newest text. +// clampAnswer cuts the answer to its budget, retaining the newest text and +// marking the cut with a leading ellipsis. +// +// The marker matters on every frame, including the opening one. A turn that +// streams a large first chunk, or that never opens a reply at all (so +// messageID stays 0), would otherwise render text that appears to begin +// mid-sentence with nothing to say a cut was made -- and Telegram messages are +// immutable, so it can never be corrected afterwards. func (l *liveReply) clampAnswer(answer string, budget int) string { - if l.messageID == 0 && utf8.RuneCountInString(answer) > budget { - runes := []rune(answer) - return string(runes[len(runes)-budget:]) - } return clampToRunes(answer, budget) } // toolBlock returns the most recent activity that fits its deliberately -// smaller frame budget. A count makes discarded activity explicit, while -// keeping the newest line visible even when that line is unusually large. +// smaller frame budget, prefixed with a count of what was left out. +// +// The search runs from the newest line backwards, widening the window while +// the result still fits, so EVERY line that fits is kept: returning as soon as +// the newest line alone fits would discard history that was never over budget +// and report a "+N earlier" that is simply untrue. +// // The caller holds the lock. func (l *liveReply) toolBlock() string { if len(l.toolLines) == 0 { return "" } - for count := 1; count <= len(l.toolLines); count++ { - start := len(l.toolLines) - count - omitted := start - prefix := "" - if omitted > 0 { - prefix = fmt.Sprintf("+%d earlier\n\n", omitted) - } - candidate := prefix + strings.Join(l.toolLines[start:], "\n") - if utf8.RuneCountInString(candidate) <= liveToolMaxRunes { - return candidate - } - // Adding older lines cannot make this candidate smaller. If the - // newest line itself is too large, truncate only that line below. - if count == 1 { - // The latest line is oversized; older lines are omitted too, - // and must be reflected in the indicator even though the line - // itself is the only one being retained. - omitted := len(l.toolLines) - 1 - prefix = "" - if omitted > 0 { - prefix = fmt.Sprintf("+%d earlier\n\n", omitted) - } - available := liveToolMaxRunes - utf8.RuneCountInString(prefix) - if available <= 0 { - return clampToRunes(prefix, liveToolMaxRunes) - } - return prefix + clampToRunes(l.toolLines[len(l.toolLines)-1], available) + best := -1 + for start := range slices.Backward(l.toolLines) { + if utf8.RuneCountInString(l.toolBlockWith(start)) > liveToolMaxRunes { + break } + best = start } - return clampToRunes(l.toolLines[len(l.toolLines)-1], liveToolMaxRunes) + if best >= 0 { + return l.toolBlockWith(best) + } + // Even the newest line alone overflows, so it is the only line retained + // and every older line must be reflected in the indicator. + omitted := len(l.toolLines) - 1 + prefix := "" + if omitted > 0 { + prefix = fmt.Sprintf("+%d earlier\n\n", omitted) + } + available := liveToolMaxRunes - utf8.RuneCountInString(prefix) + if available <= 0 { + return clampToRunes(prefix, liveToolMaxRunes) + } + return prefix + clampToRunes(l.toolLines[len(l.toolLines)-1], available) +} + +// toolBlockWith renders the activity from index start to the newest line, +// including the "+N earlier" indicator for whatever precedes start. +func (l *liveReply) toolBlockWith(start int) string { + prefix := "" + if omitted := start; omitted > 0 { + prefix = fmt.Sprintf("+%d earlier\n\n", omitted) + } + return prefix + strings.Join(l.toolLines[start:], "\n") } // clampToRunes cuts s to its last maxRunes runes, marking the cut with a diff --git a/internal/channels/telegram/live_test.go b/internal/channels/telegram/live_test.go index 4ce5f9697..456f385a8 100644 --- a/internal/channels/telegram/live_test.go +++ b/internal/channels/telegram/live_test.go @@ -647,20 +647,20 @@ func TestLiveReplyFramesStayWithinOneMessage(t *testing.T) { // Telegram accepts fails this test instead of moving with it. const telegramMessageMaxRunes = 4096 - if len(*calls) < 3 { - t.Fatalf("calls = %d, want a frame per delta plus the abandon edit", len(*calls)) + if len(*calls) < 2 { + t.Fatalf("calls = %d, want at least the opening frame and the abandon edit", len(*calls)) } for i, call := range *calls { if n := len([]rune(call.body())); n > telegramMessageMaxRunes { t.Fatalf("frame %d is %d runes, want at most %d -- Telegram rejects it whole", i, n, telegramMessageMaxRunes) } - } - // The tail is what the user is watching being written, so that is the - // end that survives the cut. - last := (*calls)[len(*calls)-1] - if !strings.HasPrefix(last.body(), "…") { - t.Fatalf("truncated frame = %.20q…, want a leading ellipsis marking the cut", last.body()) + // Every frame that had to cut text must say so. An unmarked cut is + // indistinguishable from a reply that genuinely starts that way, and + // the message can never be corrected afterwards. + if !strings.HasPrefix(call.body(), "…") { + t.Fatalf("frame %d = %.20q…, want a leading ellipsis marking the cut", i, call.body()) + } } } @@ -1183,3 +1183,56 @@ func TestLiveReplyMediaFallbackAfterFinalizeSendsFollowUp(t *testing.T) { t.Errorf("follow-up body = %q, want it to report the undelivered file", got.body()) } } + +// A turn with several short tool calls must keep all of them while they fit +// the activity budget. Returning the newest line alone discards history that +// was never over budget, and reports a "+N earlier" that is not true -- the +// frame has room for those lines. +func TestLiveReplyToolBlockKeepsEveryLineThatFits(t *testing.T) { + live, _ := newTestLiveReply(t, true) + + for _, name := range []string{"read", "grep", "find"} { + live.ToolCall(toolEvent(name, "output for "+name, "")) + } + + block := live.toolBlock() + for _, name := range []string{"read", "grep", "find"} { + if !strings.Contains(block, "🔧 "+name) { + t.Errorf("tool block dropped %s while every line still fits the %d-rune budget:\n%s", + name, liveToolMaxRunes, block) + } + } + if strings.Contains(block, "earlier") { + t.Errorf("tool block claims omitted activity although all of it fits:\n%s", block) + } +} + +// The truncation marker on the answer budget must appear whether the cut is +// made on the opening frame or a later one. The opening frame previously +// sliced without one, so a large first chunk (or a turn that never opened a +// reply) looked like it began mid-sentence, while later frames showed the cut. +func TestLiveReplyClampAnswerAlwaysMarksTheCut(t *testing.T) { + live, _ := newTestLiveReply(t, true) + + for _, tc := range []struct { + name string + messageID int + }{ + {name: "opening frame", messageID: 0}, + {name: "later frame", messageID: 99}, + } { + t.Run(tc.name, func(t *testing.T) { + live.messageID = tc.messageID + got := live.clampAnswer(strings.Repeat("a", 200)+"TAIL", 50) + if !strings.HasPrefix(got, "…") { + t.Errorf("clamped answer does not mark the cut with an ellipsis: %.60q", got) + } + if !strings.HasSuffix(got, "TAIL") { + t.Errorf("clamped answer dropped the newest text: %.60q", got) + } + if n := len([]rune(got)); n > 50 { + t.Errorf("clamped answer is %d runes, over the 50-rune budget", n) + } + }) + } +}