fix(telegram): corrected tool outputs to keep context of tool calls (#609)

This commit is contained in:
sam authored and GitHub committed 2026-08-24 07:46:42 +10:00
1 parent b223b3031c
commit 3421be48a4
3 files changed
+58 -39

No files matched your search

+5 -5
View File
@@ -216,7 +216,7 @@ func TestLiveReplyToolCallEscapesMarkdownMetacharacters(t *testing.T) {
live.finalize(context.Background(), "**the answer**")
last := (*calls)[len(*calls)-1]
if !strings.Contains(last.markdown, "```text\n1 matches found; excerpts hidden\n```") {
if !strings.Contains(last.markdown, "```text\nfound `*bold*` and _italic_ markers\n```") {
t.Fatalf("tool output was not summarized in a fenced block: %q", last.markdown)
}
if !strings.Contains(last.markdown, "**the answer**") {
@@ -259,8 +259,8 @@ func TestLiveReplyRendersNoisyToolTurnAsCompactTelegramText(t *testing.T) {
live.finalize(context.Background(), "I found the relevant renderer and stopped after the output-volume cap.")
got := (*calls)[len(*calls)-1].markdown
want := "🔧 read — done\n```text\n8 lines read; content hidden\n```\n" +
"🔧 find — done\n```text\n3 paths found; listing hidden\n```\n" +
want := "🔧 read — done\n```text\npackage gateway\n… 7 more lines\n```\n" +
"🔧 find — done\n```text\n/home/sam/projects/unrelated/main.go\n```\n" +
"🔧 shell — failed\n```text\ncommand_exit: exit status 2\n```\n" +
"🔧 tools — stopped ×5\n```text\ntool-output limit reached (200000 chars); further results suppressed\n```\n\n" +
"I found the relevant renderer and stopped after the output-volume cap."
@@ -269,7 +269,7 @@ func TestLiveReplyRendersNoisyToolTurnAsCompactTelegramText(t *testing.T) {
}
for i, call := range *calls {
for _, forbidden := range []string{
"package gateway", `\\n`, `\\\"`, "pkg/mod", "projects/unrelated", "[string]", "turn budget exceeded",
`\\n`, `\\\"`, "[string]", "turn budget exceeded",
} {
if strings.Contains(call.markdown, forbidden) {
t.Fatalf("Telegram frame %d leaked %q:\n%s", i, forbidden, call.markdown)
@@ -764,7 +764,7 @@ func TestLiveReplyFramedTextClampsAnOversizedToolBlock(t *testing.T) {
if !strings.Contains(last.markdown, "the answer") {
t.Fatalf("answer was lost after bounding oversized tool output: %.80q…", last.markdown)
}
if strings.Count(last.markdown, "ok") > 0 || !strings.Contains(last.markdown, "output lines hidden") {
if strings.Count(last.markdown, "ok") > 3 || !strings.Contains(last.markdown, "more lines") {
t.Fatalf("oversized tool output was not summarized: %.80q…", last.markdown)
}
}
+29 -29
View File
@@ -89,33 +89,39 @@ func toolProgressBlock(name, status, preview string) string {
}
func toolPreview(name, output string) string {
content, structured := unwrapToolContent(output)
trimmed := strings.TrimSpace(content)
lowerName := strings.ToLower(strings.TrimSpace(name))
lines := countNonEmptyOrContentLines(content)
content, _ := unwrapToolContent(output)
preview, skipped := compactToolPreview(content)
if preview == "" {
if strings.TrimSpace(content) == "" {
return "completed"
}
return "completed with no printable preview"
}
remaining := countNonEmptyOrContentLines(content) - 1 - skipped
if remaining < 0 {
remaining = 0
}
if remaining > 0 {
return fmt.Sprintf("%s\n… %d more lines", preview, remaining)
}
_ = name
return preview
}
switch {
case strings.Contains(lowerName, "read"):
if lines == 0 {
return "file read completed"
func compactToolPreview(output string) (string, int) {
skipped := 0
for _, line := range strings.Split(output, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
return fmt.Sprintf("%d lines read; content hidden", lines)
case strings.Contains(lowerName, "find") || strings.Contains(lowerName, "list"):
return fmt.Sprintf("%d paths found; listing hidden", lines)
case strings.Contains(lowerName, "grep") || strings.Contains(lowerName, "search"):
return fmt.Sprintf("%d matches found; excerpts hidden", lines)
case strings.Contains(lowerName, "shell") || strings.Contains(lowerName, "terminal"):
if lines > 1 || looksLikeSourceOrListing(trimmed) || structured {
return fmt.Sprintf("command completed; %d output lines hidden", lines)
if strings.Contains(line, "/pkg/mod/") {
skipped++
continue
}
return truncateRunes(line, 120), skipped
}
if structured {
return "structured result received; details hidden"
}
if looksLikeSourceOrListing(trimmed) {
return fmt.Sprintf("%d output lines hidden", lines)
}
return firstNonEmptyLine(trimmed)
return "", skipped
}
func unwrapToolContent(output string) (string, bool) {
@@ -144,12 +150,6 @@ func countNonEmptyOrContentLines(s string) int {
return strings.Count(trimmed, "\n") + 1
}
func looksLikeSourceOrListing(s string) bool {
return strings.Contains(s, "\n") && (strings.Contains(s, "package ") ||
strings.Contains(s, "module ") || strings.Contains(s, "/pkg/mod/") ||
strings.Contains(s, "/home/") || strings.Contains(s, "func "))
}
func cleanToolError(name, raw string) string {
line := firstNonEmptyLine(raw)
prefix := "tool " + strings.TrimSpace(name) + ": "
+24 -5
View File
@@ -10,9 +10,28 @@ func TestToolCallEventRenderToolCallUsesCompactSafeOutput(t *testing.T) {
if strings.Contains(got, "schema placeholder") || strings.Contains(got, "Parameters") {
t.Fatalf("render leaked input/schema noise: %q", got)
}
want := "🔧 shell — done\n```text\ncommand completed; 2 output lines hidden\n```"
if got != want {
t.Fatalf("render = %q, want %q", got, want)
if !strings.Contains(got, "line one") || strings.Contains(got, "hidden") {
t.Fatalf("short shell output should be previewed, got %q", got)
}
}
func TestToolCallEventRenderToolCallShowsUsefulPreviewWithoutDumpingSource(t *testing.T) {
source := "package agentexec\n\nimport (\n \"context\"\n)\n\ntype AgentRequestMessage struct {\n Workflow string\n}\n"
got := (ToolCallEvent{Name: "read", Output: source}).RenderToolCall()
if strings.Contains(got, "type AgentRequestMessage") || strings.Contains(got, "content hidden") {
t.Fatalf("read preview should show a short lead-in, not dump or hide everything: %q", got)
}
if !strings.Contains(got, "package agentexec") {
t.Fatalf("read preview missing first useful line: %q", got)
}
listing := "/home/sam/projects/archie-core\n/home/sam/projects/archie-core/internal/gateway\n/home/sam/go/pkg/mod/github.com/samcharles93/ai-sdk@v0.1.21/prompt/prompt.go\n"
got = (ToolCallEvent{Name: "find", Output: listing}).RenderToolCall()
if strings.Contains(got, "listing hidden") || strings.Contains(got, "/pkg/mod/") {
t.Fatalf("find preview should show a short local path, not hide or dump module cache: %q", got)
}
if !strings.Contains(got, "/home/sam/projects/archie-core") {
t.Fatalf("find preview missing first path: %q", got)
}
}
@@ -21,8 +40,8 @@ func TestToolCallEventRenderToolCallBoundsOutputAndPreservesFailure(t *testing.T
if len([]rune(got)) > 520 {
t.Fatalf("render has %d runes, want bounded output", len([]rune(got)))
}
if strings.Contains(got, strings.Repeat("x", 20)) || !strings.Contains(got, "content hidden") {
t.Fatalf("file contents were not summarized: %q", got)
if strings.Contains(got, strings.Repeat("x", 200)) {
t.Fatalf("oversized contents were not bounded: %q", got)
}
failed := (ToolCallEvent{Name: "shell", Output: "ignored", Err: "command_exit: exit status 2"}).RenderToolCall()
if failed != "🔧 shell — failed\n```text\ncommand_exit: exit status 2\n```" {