fix(telegram): keep every tool line that fits and mark every answer cut
Two defects in the activity/answer budget, both found in review. The tool block returned as soon as the newest line alone fit, so a turn with several short tool calls discarded history that was never over budget and reported a "+N earlier" that was untrue -- those lines had room. The window now widens from the newest line backwards while the result still fits, so every line that fits is kept. The indicator appears only when something is genuinely left out. The opening frame sliced the answer to its budget without an ellipsis, so a large first chunk -- or a turn that never opened a reply, leaving messageID 0 -- rendered text that appeared to begin mid-sentence, while later frames marked the same cut. Telegram messages are immutable, so it could never be corrected. Every frame now marks the cut; the special case only ever bought one rune. TestLiveReplyFramesStayWithinOneMessage asserted the ellipsis on the last frame only, which is what let the unmarked opening frame through. It now checks every frame, and the count bound follows: identical frames are deduplicated by render(), so an unmarked opening frame was previously a distinct body where a marked one is not. Verified by mutation -- restoring an unmarked opening cut fails the test on frame 0.
This commit is contained in:
1 parent
9bbce39df2
commit
3b266c71ce
2 files changed
+107
-44
No files matched your search
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user