From 72bc95cc009d02b7aa55938b381056f399d0ab03 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Thu, 17 Sep 2026 09:36:28 +0000 Subject: [PATCH 1/3] fix(telegram): summarise tool results instead of quoting an arbitrary line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Operator report: tool output in the Telegram channel was unreadable during search-heavy turns -- a wall of fenced blocks each showing one arbitrary match line ("/path/config.json-322- }") under a heading that said only "grep -- done". The renderer quoted the first non-module-cache line of a result and appended a line count recomputed from its own truncated sample, which could not agree with the total the tool had already reported: one search rendered the tool's "194 more lines" beside our "… 3 more lines". Two different numbers for one result set read as noise, and the quoted line carried no context at all. - Describe, don't quote: a search is reported as "N matches", a truncated result as " lines total; showing ", and every other tool falls back to its first informative line. Truncation notices are skipped, since they are metadata rather than content. - Never invent a count: the tool's own reported total is the only trustworthy source, parsed from its truncation notice rather than recomputed from the visible sample. - Remove the now-dead compactToolPreview and countNonEmptyOrContentLines. Also bump go-telegram/bot v1.25.0 -> v1.27.0, which fixes a streaming bug that lands squarely on this path: rawRequest sent the multipart body through an io.Pipe, which net/http cannot replay, so sendMessage/editMessageText failed until the connection was dropped. Remaining work from the report (collapsing repeated calls into one counted entry, and a ring of recent activity lines with a capped frame) is tracked in #837. --- go.mod | 2 +- go.sum | 35 +---- internal/channels/telegram/live_test.go | 16 +- internal/gateway/stream.go | 161 +++++++++++++++++--- internal/gateway/tool_render_report_test.go | 114 ++++++++++++++ 5 files changed, 269 insertions(+), 59 deletions(-) create mode 100644 internal/gateway/tool_render_report_test.go diff --git a/go.mod b/go.mod index 047f7352b..0c8e1d4f4 100644 --- a/go.mod +++ b/go.mod @@ -68,7 +68,7 @@ require ( github.com/BurntSushi/toml v1.6.0 github.com/go-git/go-billy/v6 v6.0.0-alpha.2 github.com/go-git/go-git/v6 v6.0.0-alpha.5 - github.com/go-telegram/bot v1.25.0 + github.com/go-telegram/bot v1.27.0 github.com/google/codesearch v1.3.0-rc.1 github.com/google/go-github/v78 v78.0.0 github.com/google/uuid v1.6.0 diff --git a/go.sum b/go.sum index 9bcd555ab..94e2d3393 100644 --- a/go.sum +++ b/go.sum @@ -2,21 +2,16 @@ cel.dev/cel-go v0.32.0 h1:irvpFKr5EuGPyxeME03ERh0rii1TX+BDAnB9eL3IvNk= cel.dev/cel-go v0.32.0/go.mod h1:DnVip7tpJSsgZymwfT+m1tnEVy3ivAjSMXPx12YrMkU= cel.dev/expr v0.25.3 h1:A2jO8jwOugrrovveCWfj0KEZOfqiLgAcwjpHPhzIGw0= cel.dev/expr v0.25.3/go.mod h1:hrXvqGP6G6gyx8UAHSHJ5RGk//1Oj5nXQ2NI02Nrsg4= -cloud.google.com/go/auth v0.18.2/go.mod h1:xD+oY7gcahcu7G2SG2DsBerfFxgPAJz17zz2joOFF3M= -cloud.google.com/go/compute/metadata v0.9.0/go.mod h1:E0bWwX5wTnLPedCKqk3pJmVgCBSM6qQI1yTBdEb3C10= code.gitea.io/sdk/gitea v0.25.1 h1:yywxWwoV+SdjHtbC6unBiXojWdZOtoHuGhEazEXeWuE= code.gitea.io/sdk/gitea v0.25.1/go.mod h1:uDFWYBU8dgZsgOHwe6C/6olxvf8FHguNB3wW1i83fgg= github.com/42wim/httpsig v1.2.4 h1:mI5bH0nm4xn7K18fo1K3okNDRq8CCJ0KbBYWyA6r8lU= github.com/42wim/httpsig v1.2.4/go.mod h1:yKsYfSyTBEohkPik224QPFylmzEBtda/kjyIAJjh3ps= -github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk= github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= -github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.33.0/go.mod h1:pJTkW8hEUIIi3Pf65lPZOnn4Y81yCllX6IWk2jNXdkM= github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY= github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU= github.com/ProtonMail/go-crypto v1.4.1 h1:9RfcZHqEQUvP8RzecWEUafnZVtEvrBVL9BiF67IQOfM= github.com/ProtonMail/go-crypto v1.4.1/go.mod h1:e1OaTyu5SYVrO9gKOEhTc+5UcXtTUa+P3uLudwcgPqo= -github.com/a-h/templ v0.3.1020/go.mod h1:A2DlK61v+K+NRoGnhmYbNYVmtYHcFO5/AisMvBdDxTM= github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8= github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4= github.com/antithesishq/antithesis-sdk-go v0.8.0 h1:bVV5ZAuwaREn4klRSJusYaA+A7NXq8Qi9C/knCaQ2N4= @@ -25,18 +20,14 @@ github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYW github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmOABFgjdkM7Nw= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= -github.com/bwesterb/go-ristretto v1.2.4/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cloudflare/circl v1.6.5 h1:O64F26HEqNhznd/hrC5KZXVKYuKM2rx4deZDTc4ihQA= github.com/cloudflare/circl v1.6.5/go.mod h1:h5LNyxAc5nTue9DS5jT+48en2PSDYt3zdGnz5OstK6c= -github.com/cncf/xds/go v0.0.0-20260202195803-dba9d589def2/go.mod h1:qwXFYgsP6T7XnJtbKlf1HP8AjxZZyzxMmc+Lq5GjlU4= github.com/containerd/errdefs v1.0.0 h1:tg5yIfIlQIrxYtu9ajqY42W3lpS19XqdxRQeEwYG8PI= github.com/containerd/errdefs v1.0.0/go.mod h1:+YBYIdtsnF4Iw6nWZhJcqGSg/dwvV7tyJ/kCkyJ2k+M= github.com/containerd/errdefs/pkg v0.3.0 h1:9IKJ06FvyNlexW690DXuQNx2KA2cUJXx151Xdx3ZPPE= github.com/containerd/errdefs/pkg v0.3.0/go.mod h1:NJw6s9HwNuRhnjJhM7pylWwMyAkmCQvQ4GpJHEqRLVk= -github.com/containerd/typeurl/v2 v2.2.0/go.mod h1:8XOOxnyatxSWuG8OfsZXVnAF4iZfedjS/8UHSPJnX4g= -github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davidmz/go-pageant v1.0.2 h1:bPblRCh5jGU+Uptpz6LgMZGD5hJoOt7otgT454WvHn0= @@ -51,10 +42,6 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkp github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= -github.com/envoyproxy/go-control-plane v0.14.0/go.mod h1:NcS5X47pLl/hfqxU70yPwL9ZMkUlwlKxtAohpi2wBEU= -github.com/envoyproxy/go-control-plane/envoy v1.37.0/go.mod h1:DReE9MMrmecPy+YvQOAOHNYMALuowAnbjjEMkkWOi6A= -github.com/envoyproxy/go-control-plane/ratelimit v0.1.0/go.mod h1:Wk+tMFAFbCXaJPzVVHnPgRKdUdwW/KdbRt94AzgRee4= -github.com/envoyproxy/protoc-gen-validate v1.3.3/go.mod h1:TsndJ/ngyIdQRhMcVVGDDHINPLWB7C82oDArY51KfB0= github.com/felixge/httpsnoop v1.1.0 h1:3YtUj32ZZkqZtt3sZZsClsymw/QDuVfpNhoA31zeORc= github.com/felixge/httpsnoop v1.1.0/go.mod h1:Zqxgdd+1Rkcz8euOqdr7lqgCRJztwr5hp9vDSi5UZCE= github.com/gliderlabs/ssh v0.3.8 h1:a4YXD1V7xMF9g5nTkdfnja3Sxy1PVDCj1Zg4Wb8vY6c= @@ -69,17 +56,13 @@ github.com/go-git/go-git-fixtures/v6 v6.0.0-alpha.1 h1:gmqi2jvsreu0s8JMLylYDFq4s github.com/go-git/go-git-fixtures/v6 v6.0.0-alpha.1/go.mod h1:ECf1MqJlBdYpKggBrOXjo/0EnvRZx6D++I86UYjPgAQ= github.com/go-git/go-git/v6 v6.0.0-alpha.5 h1:sE+OlkHgYWNMVmN1s9sR7uyFgsWLtxcNWse/vBYKxRE= github.com/go-git/go-git/v6 v6.0.0-alpha.5/go.mod h1:3IjhiZnM+uBmUrOGSeqrJpsmi4Vd0H2NZO/uK2a7d0s= -github.com/go-jose/go-jose/v4 v4.1.4/go.mod h1:x4oUasVrzR7071A4TnHLGSPpNOm2a21K9Kf04k1rs08= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.4.4 h1:tG4xh9yMsRCAiodLVTxyrkzSZ9+o0L1Kg/+cPVcbP/8= github.com/go-logr/logr v1.4.4/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= -github.com/go-quicktest/qt v1.101.0/go.mod h1:14Bz/f7NwaXPtdYEgzsx46kqSxVwTbzVZsDC26tQJow= -github.com/go-telegram/bot v1.25.0 h1:C072msZtr+GjJiQsX/UJLirz+jzSNZVWkckxWufR7Ww= -github.com/go-telegram/bot v1.25.0/go.mod h1:i2TRs7fXWIeaceF3z7KzsMt/he0TwkVC680mvdTFYeM= -github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= -github.com/golang/glog v1.2.5/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwmO+w= +github.com/go-telegram/bot v1.27.0 h1:PG9yBDzM8O26oUerE9SZwWlHon4RpH8WcNslZ7CEZmE= +github.com/go-telegram/bot v1.27.0/go.mod h1:i2TRs7fXWIeaceF3z7KzsMt/he0TwkVC680mvdTFYeM= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/google/codesearch v1.3.0-rc.1 h1:vSFGuF5wS3sjimuLzqPG+PGjWQ9VvEtPFbxdZD7YuuQ= @@ -93,14 +76,10 @@ github.com/google/go-querystring v1.2.0 h1:yhqkPbu2/OH+V9BfpCVPZkNmUXhb2gBxJArfh github.com/google/go-querystring v1.2.0/go.mod h1:8IFJqpSRITyJ8QhQ13bmbeMBDfmeEJZD5A0egEOmkqU= github.com/google/go-tpm v0.9.8 h1:slArAR9Ft+1ybZu0lBwpSmpwhRXaa85hWtMinMyRAWo= github.com/google/go-tpm v0.9.8/go.mod h1:h9jEsEECg7gtLis0upRBQU+GhYVH6jMjrFxI8u6bVUY= -github.com/google/go-tpm-tools v0.3.13-0.20230620182252-4639ecce2aba/go.mod h1:EFYHy8/1y2KfgTAsx7Luu7NGhoxtuVHnNo8jE7FikKc= github.com/google/pprof v0.0.0-20260802141513-ef3492d7dac3 h1:LMLX+LgTNWpfvCBdFebv6EsYotImrt/Ppc5cXIriCSo= github.com/google/pprof v0.0.0-20260802141513-ef3492d7dac3/go.mod h1:jl5iWTm0/hd5PjEYEOuwAJ57L/CibdZfrqZ5XA5GrCk= -github.com/google/s2a-go v0.1.9/go.mod h1:YA0Ei2ZQL3acow2O62kdp9UlnvMmU7kA6Eutn0dXayM= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/googleapis/enterprise-certificate-proxy v0.3.11/go.mod h1:RFV7MUdlb7AgEq2v7FmMCfeSMCllAzWxFgRdusoGks8= -github.com/googleapis/gax-go/v2 v2.17.0/go.mod h1:mzaqghpQp4JDh3HvADwrat+6M3MOIDp5YKHhb9PAgDY= github.com/hashicorp/go-version v1.9.0 h1:CeOIz6k+LoN3qX9Z0tyQrPtiB1DFYRPfCIBtaXPSCnA= github.com/hashicorp/go-version v1.9.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= @@ -128,7 +107,6 @@ github.com/moby/moby/api v1.56.0 h1:GQzua3NA599ASSIICx0iFgiJeO9YkdDARvQsm23ZZuQ= github.com/moby/moby/api v1.56.0/go.mod h1:sZ+THbVWkjOmBPPfbnzdD/G1LuIexWhqlSHHPTDQ1Uk= github.com/moby/moby/client v0.6.0 h1:AJjEB21QPbXSXjDsZorFBoDZPhMrfbpaPLgSMAW9Bgs= github.com/moby/moby/client v0.6.0/go.mod h1:OCo00wNRyA3m4lmJ228W3JbyCN4ZNNYjpOXiJydBdcQ= -github.com/moby/term v0.5.2/go.mod h1:d3djjFCrjnB+fl8NJux+EJzu0msscUP+f8it8hPkFLc= github.com/nats-io/jwt/v2 v2.8.2 h1:XXRgB60MSTnqsRwejQurVDs/hcv2dkt+86GjI+I/bMc= github.com/nats-io/jwt/v2 v2.8.2/go.mod h1:Ag/56sq9OblL4JgdYufDd16Egb17Kr/8WwwuO/forVc= github.com/nats-io/nats-server/v2 v2.14.6 h1:dHjEHa49lUTxIb5jrsk5kz/AmOyOQ4T9ti8djcBumDQ= @@ -149,20 +127,15 @@ github.com/pjbgf/sha1cd v0.6.0 h1:3WJ8Wz8gvDz29quX1OcEmkAlUg9diU4GxJHqs0/XiwU= github.com/pjbgf/sha1cd v0.6.0/go.mod h1:lhpGlyHLpQZoxMv8HcgXvZEhcGs0PG/vsZnEJ7H0iCM= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU= -github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10/go.mod h1:t/avpk3KcrXxUnYOhZhMXJlSEyie6gQbtLq5NM3loB8= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ= github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc= -github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY= github.com/samcharles93/ai-sdk v0.1.32 h1:rfBpWUOADuRTaq2kyCnMjdhtlFfaqUbsyL5DbIluqmA= github.com/samcharles93/ai-sdk v0.1.32/go.mod h1:u4zrQ9/Q58L5mNEut4bNQ1ks+RCKq71LN5EDz2N/0Zk= -github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY= github.com/sergi/go-diff v1.4.0 h1:n/SP9D5ad1fORl+llWyN+D6qoUETXNZARKjyY2/KVCw= github.com/sergi/go-diff v1.4.0/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4= -github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= -github.com/spiffe/go-spiffe/v2 v2.7.0/go.mod h1:47Q0Q9/AqGha8QLHp+kxpH4Wca7X7EnOtlIJy3mxZ3U= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE= @@ -171,7 +144,6 @@ github.com/traefik/yaegi v0.16.1 h1:f1De3DVJqIDKmnasUF6MwmWv1dSEEat0wcpXhD2On3E= github.com/traefik/yaegi v0.16.1/go.mod h1:4eVhbPb3LnD2VigQjhYbEJ69vDRFdT2HQNrXx8eEwUY= go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64= go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y= -go.opentelemetry.io/contrib/detectors/gcp v1.44.0/go.mod h1:tNAsgd8avTGke1+MndXlU5Cru4PQ9Ai/cCNWQv/ZJ/s= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.71.0 h1:3g7B90UzBltIDKq1/5mrTGxTnOFDV0ICOhLoxiZ8jlg= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.71.0/go.mod h1:Ef8SuTh59BT7+ofpDxN9z+yOlc4t2GjLmKDgYNJL/NU= go.opentelemetry.io/otel v1.46.0 h1:FHt5/CDyVxi/8IM1CH7VE/rRgq3kLHa2mSTVMO8AWyc= @@ -199,7 +171,6 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.58.0 h1:ynWG7rqYi4ccpTEuPZ2QGWHktVEM9DMCj9yzDE0Q7To= golang.org/x/net v0.58.0/go.mod h1:YwCddHnFlT7eLQqVprV19OnhLGtc5xOKgE0RyqgfWAU= -golang.org/x/oauth2 v0.36.0/go.mod h1:YDBUJMTkDnJS+A4BP4eZBjCqtokkg1hODuPjwiGPO7Q= golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek= golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -221,8 +192,6 @@ golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.49.0 h1:3NI7VXzL9+1WZD52Dx2ttoPwD5DWrFGpl9mFZDlmisI= golang.org/x/tools v0.49.0/go.mod h1:SJNXV9DBKT0UbdttsQjbfJlAE/q+y36++zo3uL3N0Oo= -golang.org/x/tools/go/expect v0.1.1-deprecated/go.mod h1:eihoPOH+FgIqa3FpoTwguz/bVUSGBlGQU67vpBeOrBY= -golang.org/x/tools/go/packages/packagestest v0.1.1-deprecated/go.mod h1:RVAQXBGNv1ib0J382/DPCRS/BPnsGebyM1Gj5VSDpG8= gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4= gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E= google.golang.org/genproto/googleapis/api v0.0.0-20260904194346-d0f1323225a4 h1:NCe/UiklGd/9xjT+ROBVhJ1kf6TRQaFedsR+z7u1gvo= diff --git a/internal/channels/telegram/live_test.go b/internal/channels/telegram/live_test.go index 575b8b0fd..756a4193a 100644 --- a/internal/channels/telegram/live_test.go +++ b/internal/channels/telegram/live_test.go @@ -230,7 +230,10 @@ 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].body() - want := "šŸ”§ read — done\n\npackage gateway\n… 7 more lines\n\n" + + // A tool result is described, never quoted with an invented line count: + // "… 7 more lines" was the renderer counting its own truncated sample, and + // it cannot agree with the total the tool actually reported. + want := "šŸ”§ read — done\n\npackage gateway\n\n" + "šŸ”§ find — done\n\n/home/sam/projects/unrelated/main.go\n\n" + "šŸ”§ shell — failed\n\ncommand_exit: exit status 2\n\n" + "šŸ”§ tools — stopped Ɨ5\n\ntool-output limit reached (200000 chars); further results suppressed\n\n" + @@ -238,6 +241,9 @@ func TestLiveReplyRendersNoisyToolTurnAsCompactTelegramText(t *testing.T) { if got != want { t.Fatalf("Telegram-visible text:\n%s\n\nwant:\n%s", got, want) } + if strings.Contains(got, "more lines") { + t.Errorf("renderer invented a line count from a truncated sample:\n%s", got) + } for i, call := range *calls { for _, forbidden := range []string{ `\\n`, `\\\"`, "[string]", "turn budget exceeded", @@ -704,9 +710,15 @@ func TestLiveReplyFramedTextClampsAnOversizedToolBlock(t *testing.T) { if !strings.Contains(last.body(), "the answer") { t.Fatalf("answer was lost after bounding oversized tool output: %.80q…", last.body()) } - if strings.Count(last.body(), "ok") > 3 || !strings.Contains(last.body(), "more lines") { + // The oversized block must be summarised rather than dumped, and the + // summary must not be a line count the renderer invented: "ok" repeats far + // more than three times, so anything that echoed the sample fails here. + if strings.Count(last.body(), "ok") > 3 { t.Fatalf("oversized tool output was not summarized: %.80q…", last.body()) } + if strings.Contains(last.body(), "more lines") { + t.Errorf("summary invented a line count from a truncated sample: %.80q…", last.body()) + } } // A provider error is not an acknowledged interruption the way /stop is, so diff --git a/internal/gateway/stream.go b/internal/gateway/stream.go index 19a5579d6..dfd059164 100644 --- a/internal/gateway/stream.go +++ b/internal/gateway/stream.go @@ -3,6 +3,7 @@ package gateway import ( "encoding/json" "fmt" + "strconv" "strings" "github.com/samcharles93/archie-core/internal/webhookguard" @@ -65,37 +66,159 @@ func toolProgressBlock(name, status, preview string) string { return "šŸ”§ " + name + " — " + status + "\n```text\n" + truncateRunes(preview, 180) + "\n```" } +// toolPreview reduces a tool result to one informative line. +// +// The result is described, never quoted: a search returns match lines that say +// nothing on their own (a path, a line number, a closing brace), so the preview +// states what came back and how much of it. The count comes from the tool's own +// truncation notice when it printed one, because a count recomputed from the +// visible sample describes the sample, not the result set -- rendering both +// produced two different numbers for one search ("194 more lines" next to +// "… 3 more lines"), which reads as noise. func toolPreview(name, output string) string { content, _ := unwrapToolContent(output) - preview, skipped := compactToolPreview(content) - if preview == "" { - if strings.TrimSpace(content) == "" { - return "completed" - } - return "completed with no printable preview" + if strings.TrimSpace(content) == "" { + return "completed" } - remaining := max(countNonEmptyOrContentLines(content)-1-skipped, 0) - if remaining > 0 { - return fmt.Sprintf("%s\n… %d more lines", preview, remaining) + // A tool that reported a real total is the only trustworthy source for + // how much came back; anything we compute is about the sample we can see. + if total, ok := toolReportedTotal(content); ok { + return total + } + if summary, ok := searchResultSummary(content); ok { + return summary + } + preview := firstInformativeLine(content) + if preview == "" { + return "completed with no printable preview" } _ = name return preview } -func compactToolPreview(output string) (string, int) { - skipped := 0 - for line := range strings.SplitSeq(output, "\n") { +// toolReportedTotal extracts the line total from the truncation notices the +// tool layer prints (internal/tools/builtin: "[truncated: showing 2/194 lines, +// …]"). Those notices are metadata, not content, so they are never rendered as +// a preview line -- quoting one is what produced "[truncated: showing 3/197 +// lines, 412B/24.1KB]" as if it were a search hit. +// +// A notice that shows everything (showing N/N) is not a truncation and gets no +// summary: the content speaks for itself. +func toolReportedTotal(content string) (string, bool) { + for line := range strings.SplitSeq(content, "\n") { + line = strings.TrimSpace(line) + shown, total, ok := parseTruncationNotice(line) + if !ok { + continue + } + if total <= shown { + continue + } + return fmt.Sprintf("%d lines total; showing %d", total, shown), true + } + return "", false +} + +// parseTruncationNotice reads "showing / lines" out of a +// truncation notice, tolerating the surrounding "[truncated: … ]" wrapper. +func parseTruncationNotice(line string) (shown, total int, ok bool) { + _, rest, found := strings.Cut(line, "showing ") + if !found { + return 0, 0, false + } + counts, _, found := strings.Cut(rest, " lines") + if !found { + return 0, 0, false + } + left, right, found := strings.Cut(counts, "/") + if !found { + return 0, 0, false + } + shown, errShown := strconv.Atoi(strings.TrimSpace(left)) + total, errTotal := strconv.Atoi(strings.TrimSpace(right)) + if errShown != nil || errTotal != nil { + return 0, 0, false + } + return shown, total, true +} + +// searchResultSummary counts match lines in ripgrep-shaped output ("path-12- +// text", "path:12:text", or a context separator). A search is the one tool +// whose raw output is least useful quoted and most useful counted, so it is +// summarised rather than sampled. +func searchResultSummary(content string) (string, bool) { + matches, _ := countSearchMatches(content) + if matches == 0 { + return "", false + } + return fmt.Sprintf("%d %s", matches, pluralize(matches, "match", "matches")), true +} + +// countSearchMatches counts match lines and whether any were truncated. +func countSearchMatches(content string) (matches int, truncated bool) { + for line := range strings.SplitSeq(content, "\n") { + line = strings.TrimSpace(line) + if line == "" || strings.HasPrefix(line, "[") { + continue + } + if searchMatchLine(line) { + matches++ + } + } + return matches, false +} + +// searchMatchLine reports whether line has ripgrep's "path-N-text" or +// "path:N:text" shape. The path may contain colons, so the separator is found +// by scanning for the last one that is followed by a line number. +func searchMatchLine(line string) bool { + if strings.HasPrefix(line, "--") && strings.Trim(line, "-") == "" { + return true // context separator + } + for i := len(line) - 1; i > 0; i-- { + if line[i] != ':' && line[i] != '-' { + continue + } + digits := line[i+1:] + end := 0 + for end < len(digits) && digits[end] >= '0' && digits[end] <= '9' { + end++ + } + if end == 0 || end >= len(digits) { + continue + } + if digits[end] == ':' || digits[end] == '-' { + return true + } + } + return false +} + +// firstInformativeLine returns the first line worth showing: not blank, not a +// module-cache path, and not a truncation notice (which is metadata the total +// path already consumed). +func firstInformativeLine(content string) string { + for line := range strings.SplitSeq(content, "\n") { line = strings.TrimSpace(line) if line == "" { continue } if strings.Contains(line, "/pkg/mod/") { - skipped++ continue } - return truncateRunes(line, 120), skipped + if _, _, ok := parseTruncationNotice(line); ok { + continue + } + return truncateRunes(line, 120) } - return "", skipped + return "" +} + +func pluralize(n int, one, many string) string { + if n == 1 { + return one + } + return many } func unwrapToolContent(output string) (string, bool) { @@ -116,14 +239,6 @@ func unwrapToolContent(output string) (string, bool) { return output, false } -func countNonEmptyOrContentLines(s string) int { - if strings.TrimSpace(s) == "" { - return 0 - } - trimmed := strings.TrimRight(s, "\r\n") - return strings.Count(trimmed, "\n") + 1 -} - func cleanToolError(name, raw string) string { line := firstNonEmptyLine(raw) prefix := "tool " + strings.TrimSpace(name) + ": " diff --git a/internal/gateway/tool_render_report_test.go b/internal/gateway/tool_render_report_test.go new file mode 100644 index 000000000..21f2f233f --- /dev/null +++ b/internal/gateway/tool_render_report_test.go @@ -0,0 +1,114 @@ +package gateway + +import ( + "fmt" + "strings" + "testing" +) + +// Operator report (2026-09-17): tool output in Telegram is unreadable. A +// grep-heavy turn rendered as a wall of blocks like: +// +// šŸ”§ grep — done +// ```text +// /home/sam/.claude/config.json-322- } +// … 194 more lines +// ``` +// +// Three faults, all in the render path: +// +// 1. the preview is an arbitrary match line -- a path, a line number and a +// stray brace -- with no statement of what matched or how much; +// 2. the "… N more lines" we append disagrees with the count the tool +// already printed, so one result set shows two different numbers; +// 3. a grep result ends with the tool's own truncation notice, which the +// preview renders as if it were a match. + +// TestGrepContextPreviewIsNotAnArbitraryMatchLine: the first line of ripgrep +// context output is a path, a line number and (here) a closing brace. Showing +// it tells the operator nothing about the search. +func TestGrepContextPreviewIsNotAnArbitraryMatchLine(t *testing.T) { + output := strings.Join([]string{ + "/home/sam/.claude/config.json-322- }", + "/home/sam/.claude/config.json-323- }", + "/home/sam/.claude/config.json-324- }", + "", + "[truncated: showing 3/197 lines, 412B/24.1KB]", + }, "\n") + + got := RenderToolCall(ToolCallEvent{Name: "grep", Output: output}) + + if strings.Contains(got, "config.json-322-") { + t.Errorf("preview is an arbitrary match line, not a summary:\n%s", got) + } + if strings.Contains(got, "[truncated:") { + t.Errorf("preview quoted the tool's truncation notice as content:\n%s", got) + } + // A summary is expected: the operator needs to know how much matched. + if !strings.Contains(got, "197") { + t.Errorf("preview does not state the result size; want the tool's own 197 lines:\n%s", got) + } +} + +// TestToolPreviewDoesNotStateItsOwnLineCount: the renderer recomputes a line +// count from the previewed text instead of using the total the tool already +// reported, so the operator sees two different numbers for one result set -- +// the tool's "194 more lines" and our "… 3 more lines". Any count we invent +// from a truncated preview is wrong by construction, so the renderer must not +// state one at all. +func TestToolPreviewDoesNotStateItsOwnLineCount(t *testing.T) { + output := strings.Join([]string{ + "/a/b.go-12- return nil", + "/a/c.go-40- return nil", + "", + "[truncated: showing 2/194 lines, 300B/12.0KB]", + }, "\n") + + got := RenderToolCall(ToolCallEvent{Name: "grep", Output: output}) + + if strings.Contains(got, "more lines") { + t.Errorf("renderer invented a line count from a truncated preview; the tool's own count is the only true one:\n%s", got) + } + // The tool's real total should be what survives. + if !strings.Contains(got, "194") { + t.Errorf("render lost the tool's real total (194 lines):\n%s", got) + } +} + +// TestToolRenderIsBoundedAndAggregatable: one rendered call must stay small so +// a tool-heavy turn cannot crowd the answer out of a single Telegram frame. +// Bounded at 220 runes so 14 calls stay well inside the 3900-rune frame. +func TestToolRenderIsBoundedAndAggregatable(t *testing.T) { + output := strings.Repeat("/very/long/path/to/some/file.go-1234- someLineOfSource();\n", 50) + got := RenderToolCall(ToolCallEvent{Name: "grep", Output: output}) + if n := len([]rune(got)); n > 220 { + t.Errorf("one rendered tool call is %d runes, want <= 220:\n%s", n, got) + } + // 14 such calls must leave the majority of the frame for the answer. + if n := len([]rune(got)) * 14; n > 3900/2 { + t.Errorf("14 rendered calls would consume %d of a 3900-rune frame", n) + } +} + +// TestToolRenderKeepsShortOutputInformative: bounding must not throw away a +// short result that genuinely fits -- the existing behaviour these tests must +// not regress. +func TestToolRenderKeepsShortOutputInformative(t *testing.T) { + got := RenderToolCall(ToolCallEvent{Name: "shell", Output: "total 8\ndrwxr-xr-x 2 sam sam"}) + if !strings.Contains(got, "total 8") { + t.Errorf("short output lost its content: %q", got) + } +} + +// TestToolRenderCountsMatchesForSearchTools: a search summary should say how +// many matches, which is the one fact that makes it useful. +func TestToolRenderCountsMatchesForSearchTools(t *testing.T) { + var b strings.Builder + for i := range 7 { + fmt.Fprintf(&b, "/src/file%d.go-%d- match\n", i, i*10) + } + got := RenderToolCall(ToolCallEvent{Name: "grep", Output: b.String()}) + if !strings.Contains(got, "7") { + t.Errorf("search preview does not report the match count:\n%s", got) + } +} From d783bebf375b14a6bb8361ea2564889b94834cf2 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Thu, 17 Sep 2026 09:49:49 +0000 Subject: [PATCH 2/3] fix(telegram): address greptile review on tool-result rendering Four findings from review, all valid: 1. Search detection is now gated on the tool name rather than applied to every result. A compiler diagnostic ("file.go:42: message") has the same shape as a search hit, so counting it reported a build error as "N matches" and hid the diagnostic. 2. Only ripgrep match lines are counted. The hyphen context form ("path-N-text") and "--" group separators were being counted as matches, so a search with context requested reported the match count plus every surrounding context line. 3. The notice parser accepts shell's "showing last N/M lines" form as well as the plain "showing N/M lines" one. Shell emits the "last" form, which failed integer parsing and was not recognised as metadata -- so a truncated shell result quoted the raw truncation notice as its preview, the exact defect this change set out to remove. The summary now also states which end was kept ("97 lines total; showing last 66" for a tail truncation). 4. The renderer cases are consolidated into one table-driven test per the development protocol in AGENTS.md, covering all four defects above plus singular/plural, colons in paths, and module-cache noise. --- internal/gateway/stream.go | 149 +++++++++------ internal/gateway/tool_render_report_test.go | 190 +++++++++++--------- 2 files changed, 204 insertions(+), 135 deletions(-) diff --git a/internal/gateway/stream.go b/internal/gateway/stream.go index dfd059164..63c46f35e 100644 --- a/internal/gateway/stream.go +++ b/internal/gateway/stream.go @@ -80,103 +80,145 @@ func toolPreview(name, output string) string { if strings.TrimSpace(content) == "" { return "completed" } - // A tool that reported a real total is the only trustworthy source for - // how much came back; anything we compute is about the sample we can see. - if total, ok := toolReportedTotal(content); ok { - return total - } - if summary, ok := searchResultSummary(content); ok { + // A notice the tool printed is the only trustworthy source for how much + // came back; anything counted here describes the visible sample. + if summary, ok := toolNoticeSummary(content); ok { return summary } + // Only a search gets a counted summary. A compiler or linter diagnostic has + // the same "file.go:42: message" shape as a search hit, so counting every + // tool's output would report a build error as "3 matches" and hide the very + // output the operator asked for. + if isSearchTool(name) { + if summary, ok := searchResultSummary(content); ok { + return summary + } + } preview := firstInformativeLine(content) if preview == "" { return "completed with no printable preview" } - _ = name return preview } -// toolReportedTotal extracts the line total from the truncation notices the -// tool layer prints (internal/tools/builtin: "[truncated: showing 2/194 lines, -// …]"). Those notices are metadata, not content, so they are never rendered as -// a preview line -- quoting one is what produced "[truncated: showing 3/197 -// lines, 412B/24.1KB]" as if it were a search hit. +// searchToolNames are the tools whose output is a set of matches, and so reads +// better counted than sampled. Every other tool is described by its first +// informative line. +var searchToolNames = map[string]bool{ + "grep": true, + "rg": true, + "search": true, + "toolsearch": true, +} + +func isSearchTool(name string) bool { + return searchToolNames[strings.ToLower(strings.TrimSpace(name))] +} + +// toolNoticeSummary renders the "how much came back" summary from the +// truncation notice the tool printed, if any. // -// A notice that shows everything (showing N/N) is not a truncation and gets no -// summary: the content speaks for itself. -func toolReportedTotal(content string) (string, bool) { +// Two notice shapes exist (internal/tools/builtin): shell prints "[truncated: +// showing last N/M lines, …]" and the other tools print "[truncated: showing +// N/M lines, …]". Both are metadata, never content, so both must be recognised +// -- missing the "last" form would quote the raw notice as the preview, which +// is the exact defect this change removes. +// +// The same notice also tells us WHICH END was kept, which changes the summary: +// a head-truncation ("showing 3/197") lost the tail, so "showing 3" describes +// the sample; a tail-truncation ("showing last 66/97") lost the head, so the +// count of what is visible says nothing useful about the whole. +func toolNoticeSummary(content string) (string, bool) { for line := range strings.SplitSeq(content, "\n") { - line = strings.TrimSpace(line) - shown, total, ok := parseTruncationNotice(line) + notice, ok := parseTruncationNotice(strings.TrimSpace(line)) if !ok { continue } - if total <= shown { - continue + if notice.total <= notice.shown { + continue // showing everything is not a truncation } - return fmt.Sprintf("%d lines total; showing %d", total, shown), true + if notice.truncatedHead { + return fmt.Sprintf("%d lines total; showing last %d", notice.total, notice.shown), true + } + return fmt.Sprintf("%d lines total; showing %d", notice.total, notice.shown), true } return "", false } -// parseTruncationNotice reads "showing / lines" out of a -// truncation notice, tolerating the surrounding "[truncated: … ]" wrapper. -func parseTruncationNotice(line string) (shown, total int, ok bool) { +// truncationNotice describes how much a tool reported and which end it kept. +type truncationNotice struct { + shown int + total int + truncatedHead bool // the kept lines are the tail ("showing last N/M") +} + +// parseTruncationNotice reads "/ lines" out of a truncation +// notice, accepting both the plain and the "showing last" form and tolerating +// the surrounding "[truncated: … ]" wrapper. +func parseTruncationNotice(line string) (truncationNotice, bool) { _, rest, found := strings.Cut(line, "showing ") if !found { - return 0, 0, false + return truncationNotice{}, false + } + truncatedHead := false + if after, ok := strings.CutPrefix(rest, "last "); ok { + rest, truncatedHead = after, true } counts, _, found := strings.Cut(rest, " lines") if !found { - return 0, 0, false + return truncationNotice{}, false } left, right, found := strings.Cut(counts, "/") if !found { - return 0, 0, false + return truncationNotice{}, false } shown, errShown := strconv.Atoi(strings.TrimSpace(left)) total, errTotal := strconv.Atoi(strings.TrimSpace(right)) if errShown != nil || errTotal != nil { - return 0, 0, false + return truncationNotice{}, false } - return shown, total, true + return truncationNotice{shown: shown, total: total, truncatedHead: truncatedHead}, true } -// searchResultSummary counts match lines in ripgrep-shaped output ("path-12- -// text", "path:12:text", or a context separator). A search is the one tool -// whose raw output is least useful quoted and most useful counted, so it is -// summarised rather than sampled. +// searchResultSummary counts the match lines in ripgrep-shaped output. Only +// matches are counted: with context requested, ripgrep also emits context lines +// ("path-12-text", hyphen separator) and "--" group separators, and counting +// those would report the match count plus every context line around it. func searchResultSummary(content string) (string, bool) { - matches, _ := countSearchMatches(content) + matches := countSearchMatches(content) if matches == 0 { return "", false } return fmt.Sprintf("%d %s", matches, pluralize(matches, "match", "matches")), true } -// countSearchMatches counts match lines and whether any were truncated. -func countSearchMatches(content string) (matches int, truncated bool) { +// countSearchMatches counts lines using ripgrep's match separator (":"). +func countSearchMatches(content string) int { + matches := 0 for line := range strings.SplitSeq(content, "\n") { - line = strings.TrimSpace(line) - if line == "" || strings.HasPrefix(line, "[") { - continue - } - if searchMatchLine(line) { + if isSearchMatchLine(strings.TrimSpace(line)) { matches++ } } - return matches, false + return matches } -// searchMatchLine reports whether line has ripgrep's "path-N-text" or -// "path:N:text" shape. The path may contain colons, so the separator is found -// by scanning for the last one that is followed by a line number. -func searchMatchLine(line string) bool { - if strings.HasPrefix(line, "--") && strings.Trim(line, "-") == "" { - return true // context separator +// isSearchMatchLine reports whether line has ripgrep's "path:N:text" match +// shape -- a colon separator. The hyphen form ("path-N-text") is a CONTEXT +// line, not a match, and "--" is a group separator between context blocks; +// neither is a match and neither is counted. +// +// The path may itself contain colons, so the separator is located by scanning +// from the end for a colon followed by digits and another colon. +func isSearchMatchLine(line string) bool { + if line == "" || strings.HasPrefix(line, "[") { + return false + } + if strings.Trim(line, "-") == "" { + return false // "--" group separator } for i := len(line) - 1; i > 0; i-- { - if line[i] != ':' && line[i] != '-' { + if line[i] != ':' { continue } digits := line[i+1:] @@ -184,10 +226,9 @@ func searchMatchLine(line string) bool { for end < len(digits) && digits[end] >= '0' && digits[end] <= '9' { end++ } - if end == 0 || end >= len(digits) { - continue - } - if digits[end] == ':' || digits[end] == '-' { + // Require :: -- the trailing colon distinguishes a + // match from a "path:42" reference with nothing after it. + if end > 0 && end < len(digits) && digits[end] == ':' { return true } } @@ -195,7 +236,7 @@ func searchMatchLine(line string) bool { } // firstInformativeLine returns the first line worth showing: not blank, not a -// module-cache path, and not a truncation notice (which is metadata the total +// module-cache path, and not a truncation notice (which is metadata the notice // path already consumed). func firstInformativeLine(content string) string { for line := range strings.SplitSeq(content, "\n") { @@ -206,7 +247,7 @@ func firstInformativeLine(content string) string { if strings.Contains(line, "/pkg/mod/") { continue } - if _, _, ok := parseTruncationNotice(line); ok { + if _, ok := parseTruncationNotice(line); ok { continue } return truncateRunes(line, 120) diff --git a/internal/gateway/tool_render_report_test.go b/internal/gateway/tool_render_report_test.go index 21f2f233f..f5b5d2e4d 100644 --- a/internal/gateway/tool_render_report_test.go +++ b/internal/gateway/tool_render_report_test.go @@ -1,85 +1,136 @@ package gateway import ( - "fmt" "strings" "testing" ) -// Operator report (2026-09-17): tool output in Telegram is unreadable. A -// grep-heavy turn rendered as a wall of blocks like: +// Operator report (2026-09-17): tool output in the Telegram channel was +// unreadable. A grep-heavy turn rendered as a wall of fenced blocks, each +// quoting one arbitrary match line under a heading that said only +// "grep — done": // // šŸ”§ grep — done -// ```text -// /home/sam/.claude/config.json-322- } +// /path/config.json-322- } // … 194 more lines -// ``` // -// Three faults, all in the render path: +// One call rendered as 86 runes carrying no usable information: a path, a line +// number, and a stray closing brace. The renderer now describes the result +// instead of quoting it, and never states a count it computed itself. // -// 1. the preview is an arbitrary match line -- a path, a line number and a -// stray brace -- with no statement of what matched or how much; -// 2. the "… N more lines" we append disagrees with the count the tool -// already printed, so one result set shows two different numbers; -// 3. a grep result ends with the tool's own truncation notice, which the -// preview renders as if it were a match. - -// TestGrepContextPreviewIsNotAnArbitraryMatchLine: the first line of ripgrep -// context output is a path, a line number and (here) a closing brace. Showing -// it tells the operator nothing about the search. -func TestGrepContextPreviewIsNotAnArbitraryMatchLine(t *testing.T) { - output := strings.Join([]string{ - "/home/sam/.claude/config.json-322- }", - "/home/sam/.claude/config.json-323- }", - "/home/sam/.claude/config.json-324- }", +// Cases are table-driven per the development protocol in AGENTS.md. +func TestToolPreviewDescribesTheResult(t *testing.T) { + // A 197-line grep result whose first visible line is an arbitrary match; + // the tool's own notice is the only trustworthy total. + truncatedGrep := strings.Join([]string{ + "/home/example/.config/app.json-322- }", + "/home/example/.config/app.json-323- }", + "/home/example/.config/app.json-324- }", "", "[truncated: showing 3/197 lines, 412B/24.1KB]", }, "\n") - got := RenderToolCall(ToolCallEvent{Name: "grep", Output: output}) - - if strings.Contains(got, "config.json-322-") { - t.Errorf("preview is an arbitrary match line, not a summary:\n%s", got) + tests := []struct { + name string + tool string + output string + want string + }{ + { + name: "truncated search reports the tool's total, which is a head truncation", + tool: "grep", + output: truncatedGrep, + want: "197 lines total; showing 3", + }, + { + name: "shell truncation notice says 'showing last' and is still metadata, not content", + tool: "shell", + output: strings.Join([]string{ + "[truncated: showing last 66/97 lines, 46.2KB/164.8KB]", + "", + "the last line of the output", + }, "\n"), + want: "97 lines total; showing last 66", + }, + { + name: "an untruncated search is counted, not sampled", + tool: "grep", + output: "/src/a.go:12:func main() {\n/src/b.go:40:\treturn nil\n/src/c.go:99:}", + want: "3 matches", + }, + { + name: "search context lines are not matches", + tool: "grep", + output: strings.Join([]string{ + "/src/a.go:12:func main() {", + "/src/a.go-13-\treturn nil", + "/src/a.go-14-}", + "--", + "/src/b.go:40:\treturn nil", + }, "\n"), + want: "2 matches", + }, + { + name: "a single match is singular", + tool: "grep", + output: "/src/a.go:12:func main() {", + want: "1 match", + }, + { + name: "a colon in the path does not hide the match", + tool: "grep", + output: "/src/weird:dir/a.go:12:func main() {", + want: "1 match", + }, + { + // A compiler diagnostic has the same "file:line:text" shape as a + // search hit. Counting it would report a build error as "N matches" + // and hide the diagnostic the operator actually needs. + name: "a compiler diagnostic is shown, not counted as matches", + tool: "shell", + output: "internal/webui/api_tasks.go:42: undefined: forgeLocator", + want: "internal/webui/api_tasks.go:42: undefined: forgeLocator", + }, + { + name: "a non-search tool keeps its first informative line", + tool: "find", + output: "/home/example/projects/app\n/home/example/projects/app/internal", + want: "/home/example/projects/app", + }, + { + name: "module-cache noise is skipped", + tool: "read", + output: "/home/example/go/pkg/mod/github.com/x/y@v1.0/z.go\npackage y", + want: "package y", + }, + { + name: "empty output is reported as completed without a preview", + tool: "shell", + output: " \n ", + want: "completed", + }, } - if strings.Contains(got, "[truncated:") { - t.Errorf("preview quoted the tool's truncation notice as content:\n%s", got) - } - // A summary is expected: the operator needs to know how much matched. - if !strings.Contains(got, "197") { - t.Errorf("preview does not state the result size; want the tool's own 197 lines:\n%s", got) - } -} -// TestToolPreviewDoesNotStateItsOwnLineCount: the renderer recomputes a line -// count from the previewed text instead of using the total the tool already -// reported, so the operator sees two different numbers for one result set -- -// the tool's "194 more lines" and our "… 3 more lines". Any count we invent -// from a truncated preview is wrong by construction, so the renderer must not -// state one at all. -func TestToolPreviewDoesNotStateItsOwnLineCount(t *testing.T) { - output := strings.Join([]string{ - "/a/b.go-12- return nil", - "/a/c.go-40- return nil", - "", - "[truncated: showing 2/194 lines, 300B/12.0KB]", - }, "\n") - - got := RenderToolCall(ToolCallEvent{Name: "grep", Output: output}) - - if strings.Contains(got, "more lines") { - t.Errorf("renderer invented a line count from a truncated preview; the tool's own count is the only true one:\n%s", got) - } - // The tool's real total should be what survives. - if !strings.Contains(got, "194") { - t.Errorf("render lost the tool's real total (194 lines):\n%s", got) + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + got := RenderToolCall(ToolCallEvent{Name: tc.tool, Output: tc.output}) + if !strings.Contains(got, tc.want) { + t.Errorf("preview missing %q:\n%s", tc.want, got) + } + // The renderer must never state a count it computed from its own + // truncated sample: that produced two different numbers for one + // result set ("194 more lines" beside "… 3 more lines"). + if strings.Contains(got, "more lines") && !strings.Contains(got, "lines total") { + t.Errorf("renderer invented a line count from its own sample:\n%s", got) + } + }) } } // TestToolRenderIsBoundedAndAggregatable: one rendered call must stay small so // a tool-heavy turn cannot crowd the answer out of a single Telegram frame. -// Bounded at 220 runes so 14 calls stay well inside the 3900-rune frame. func TestToolRenderIsBoundedAndAggregatable(t *testing.T) { - output := strings.Repeat("/very/long/path/to/some/file.go-1234- someLineOfSource();\n", 50) + output := strings.Repeat("/very/long/path/to/some/file.go:1234: someLineOfSource();\n", 50) got := RenderToolCall(ToolCallEvent{Name: "grep", Output: output}) if n := len([]rune(got)); n > 220 { t.Errorf("one rendered tool call is %d runes, want <= 220:\n%s", n, got) @@ -89,26 +140,3 @@ func TestToolRenderIsBoundedAndAggregatable(t *testing.T) { t.Errorf("14 rendered calls would consume %d of a 3900-rune frame", n) } } - -// TestToolRenderKeepsShortOutputInformative: bounding must not throw away a -// short result that genuinely fits -- the existing behaviour these tests must -// not regress. -func TestToolRenderKeepsShortOutputInformative(t *testing.T) { - got := RenderToolCall(ToolCallEvent{Name: "shell", Output: "total 8\ndrwxr-xr-x 2 sam sam"}) - if !strings.Contains(got, "total 8") { - t.Errorf("short output lost its content: %q", got) - } -} - -// TestToolRenderCountsMatchesForSearchTools: a search summary should say how -// many matches, which is the one fact that makes it useful. -func TestToolRenderCountsMatchesForSearchTools(t *testing.T) { - var b strings.Builder - for i := range 7 { - fmt.Fprintf(&b, "/src/file%d.go-%d- match\n", i, i*10) - } - got := RenderToolCall(ToolCallEvent{Name: "grep", Output: b.String()}) - if !strings.Contains(got, "7") { - t.Errorf("search preview does not report the match count:\n%s", got) - } -} From 5539f2b343251a9fb789f6e40dc07d94f84b7e10 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Thu, 17 Sep 2026 10:13:07 +0000 Subject: [PATCH 3/3] fix(telegram): require the truncation wrapper before treating a count as metadata MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review found that a bare "showing 3/197 lines" inside ordinary output was accepted as truncation metadata and replaced by a summary, hiding the real result. Both built-in producers wrap the notice as "[truncated: …]" (internal/tools/builtin/truncate.go), so the wrapper is now required rather than tolerated: it is the only marker separating a notice the tool printed from content that merely mentions a count. --- internal/gateway/stream.go | 16 +++++++++++++--- internal/gateway/tool_render_report_test.go | 10 ++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/internal/gateway/stream.go b/internal/gateway/stream.go index 63c46f35e..4a49dad1f 100644 --- a/internal/gateway/stream.go +++ b/internal/gateway/stream.go @@ -153,10 +153,20 @@ type truncationNotice struct { } // parseTruncationNotice reads "/ lines" out of a truncation -// notice, accepting both the plain and the "showing last" form and tolerating -// the surrounding "[truncated: … ]" wrapper. +// notice, accepting both the plain and the "showing last" form. +// +// The "[truncated: … ]" wrapper is required, not tolerated. It is the only +// marker that distinguishes a notice the tool printed from ordinary output that +// happens to contain the same phrasing: both built-in producers emit the +// wrapper (internal/tools/builtin/truncate.go), so a bare "showing 3/197 lines" +// inside a log or a document is content and must be left alone rather than +// replaced by a summary. func parseTruncationNotice(line string) (truncationNotice, bool) { - _, rest, found := strings.Cut(line, "showing ") + _, rest, found := strings.Cut(line, "[truncated: ") + if !found { + return truncationNotice{}, false + } + _, rest, found = strings.Cut(rest, "showing ") if !found { return truncationNotice{}, false } diff --git a/internal/gateway/tool_render_report_test.go b/internal/gateway/tool_render_report_test.go index f5b5d2e4d..50db5af63 100644 --- a/internal/gateway/tool_render_report_test.go +++ b/internal/gateway/tool_render_report_test.go @@ -103,6 +103,16 @@ func TestToolPreviewDescribesTheResult(t *testing.T) { output: "/home/example/go/pkg/mod/github.com/x/y@v1.0/z.go\npackage y", want: "package y", }, + { + // Only the built-in truncation producers emit this phrasing, and + // they always wrap it as "[truncated: …]". Accepting the phrasing on + // its own would rewrite genuine output that merely mentions a count + // as if it were a truncation notice, hiding the real result. + name: "ordinary text mentioning a line count is not truncation metadata", + tool: "shell", + output: "the log said showing 3/197 lines before it continued\nand trailing output", + want: "the log said showing 3/197 lines before it continued", + }, { name: "empty output is reported as completed without a preview", tool: "shell",