From 8dd8841a7a02d7af6981a0d30ebcbc45f86076ad Mon Sep 17 00:00:00 2001 From: samcharles93 Date: Thu, 17 Sep 2026 11:23:54 +0000 Subject: [PATCH 1/3] Telegram tool-activity block is uncapped and crowds out the answer (archie) Implements #839 --- internal/channels/telegram/live.go | 87 +++++++++++++++++++++++------- 1 file changed, 67 insertions(+), 20 deletions(-) diff --git a/internal/channels/telegram/live.go b/internal/channels/telegram/live.go index 2e27e76e8..c1a34ba94 100644 --- a/internal/channels/telegram/live.go +++ b/internal/channels/telegram/live.go @@ -41,6 +41,11 @@ const ( // Only the live frame is bounded. finalize sends the whole reply, // split across as many messages as it needs. liveBodyMaxRunes = 3900 + + // Keep tool activity to a minority of a live frame so the answer remains + // readable even when a turn makes many calls. The remainder is reserved + // for the answer (and the cursor). + liveToolMaxRunes = (liveBodyMaxRunes - 2) / 3 ) // liveReply renders one chat turn into a single Telegram message as it is @@ -653,32 +658,71 @@ func (l *liveReply) body() string { // framedText composes tool activity ahead of the answer text and clamps the // whole thing to fit one Telegram message. The caller holds the lock. -// -// The tool block is kept whole rather than clamped along with the answer: -// tools run before the answer in an agentic turn, so a single clamp over the -// concatenation would cut the tool block first as the answer grows past the -// bound, and the user watching mid-turn would see tool activity silently -// vanish partway through, then reappear once finalize leads with it again. -// Keeping the block outside the clamp makes the live frame, an abandoned -// frame, and the finished message agree on shape throughout the turn. func (l *liveReply) framedText(answer string) string { if len(l.toolLines) == 0 { - return clampToOneMessage(answer) + return l.clampAnswer(answer, liveBodyMaxRunes-2) } - block := strings.Join(l.toolLines, "\n") + block := l.toolBlock() if answer == "" { - return clampToOneMessage(block) + return block } - // Only the answer tail is clamped, to whatever is left of the budget - // once the tool block is accounted for. A tool block alone big enough - // to exhaust the budget is the one case where it gets clamped too -- - // exceedingly unlikely given how short a rendered tool line is, but - // still a single Telegram message rather than a rejected edit. - budget := liveBodyMaxRunes - utf8.RuneCountInString(block) - 2 + // Reserve the larger share of the frame for the answer. The two extra + // runes are the separator and the cursor appended by body. + budget := liveBodyMaxRunes - 2 - utf8.RuneCountInString(block) - 2 if budget <= 0 { - return clampToOneMessage(block) + return block } - return block + "\n\n" + clampToRunes(answer, budget) + 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. +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. +// 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) + } + } + return clampToRunes(l.toolLines[len(l.toolLines)-1], liveToolMaxRunes) } // clampToOneMessage cuts s to the tail that fits in a single Telegram @@ -696,7 +740,10 @@ func clampToRunes(s string, maxRunes int) string { if len(runes) <= maxRunes { return s } - return "…" + string(runes[len(runes)-maxRunes:]) + if maxRunes <= 1 { + return "…" + } + return "…" + string(runes[len(runes)-(maxRunes-1):]) } // finalText composes the finished message: the tool activity in the order it From 9bbce39df2d2aaf562f30e0f9f00255f968bcf54 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Thu, 17 Sep 2026 11:31:05 +0000 Subject: [PATCH 2/3] chore(telegram): resolve the state-store test conflict and drop dead clamp helper Rebased onto main, which now carries an equivalent STATE_STORE_TOKEN isolation guard in state_store_client_test.go from an independent baseline repair. The two changes were the same fix expressed twice, so main's wording is kept and this branch's duplicate is dropped -- the file is now identical to main and no longer appears in the diff. That leaves one commit, touching only internal/channels/telegram/live.go. Removing clampToOneMessage, which this change orphaned: all three of its call sites were replaced with clampToRunes directly, and `unused` flagged it. Dead code left behind by a rewrite does not ship. --- internal/channels/telegram/live.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/internal/channels/telegram/live.go b/internal/channels/telegram/live.go index c1a34ba94..62ad78c45 100644 --- a/internal/channels/telegram/live.go +++ b/internal/channels/telegram/live.go @@ -725,14 +725,6 @@ func (l *liveReply) toolBlock() string { return clampToRunes(l.toolLines[len(l.toolLines)-1], liveToolMaxRunes) } -// clampToOneMessage cuts s to the tail that fits in a single Telegram -// message, marking the cut with a leading ellipsis. Counting runes rather -// than bytes matches how Telegram measures the limit and keeps the cut off a -// character boundary. -func clampToOneMessage(s string) string { - return clampToRunes(s, liveBodyMaxRunes) -} - // clampToRunes cuts s to its last maxRunes runes, marking the cut with a // leading ellipsis. func clampToRunes(s string, maxRunes int) string { From 3b266c71ce23bd24c89a7878738122dd61fcb25b Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Thu, 17 Sep 2026 11:40:08 +0000 Subject: [PATCH 3/3] 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. --- internal/channels/telegram/live.go | 82 ++++++++++++++----------- internal/channels/telegram/live_test.go | 69 ++++++++++++++++++--- 2 files changed, 107 insertions(+), 44 deletions(-) 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) + } + }) + } +}