fix(telegram): send undelivered-media fallback after finalize as its own message

Closes archie-core-atv2.

Media delivery is fire-and-forget with a 30s budget, far longer than
the turn's text usually finishes -- so a large upload failing (or
timing out) after doFinalize has already retired the renderer goroutine
is the normal case, not an edge one. appendFallbackLine was still
writing into toolLines and calling requestRender, a non-blocking send
into a channel nothing reads any more once stopRendering has returned:
the line was silently dropped and the finalized message stood
unchanged, with nothing telling the user or the agent the file never
arrived.

stopRendering is the single point every finalize/abandon path funnels
through to retire the renderer, so it's the right place to mark the
reply finalized. appendFallbackLine now checks that flag and, once
set, sends the fallback as its own follow-up message instead of
appending to a buffer nothing will render again.
This commit is contained in:
sam committed 2026-09-02 01:18:54 +10:00
1 parent f603305a6a
commit b966f6bd03
2 files changed
+65

No files matched your search

+23
View File
@@ -116,6 +116,15 @@ type liveReply struct {
rendered string
messageID int
last time.Time
// finalized is set once stopRendering returns, the single point every
// finalize/abandon path funnels through to retire the renderer
// goroutine. After that point requestRender is a silent no-op (a
// non-blocking send into a channel nothing reads any more), so any
// caller still trying to append to the live message after this point
// -- Media's fire-and-forget delivery has its own 30s budget, far
// longer than the turn's text usually takes -- must send its own
// follow-up message instead of writing into a buffer nothing renders.
finalized bool
// terminal guards finalize and abandon so exactly one of them actually
// runs its body. Without this, a turn finishing naturally (finalize)
@@ -289,6 +298,9 @@ func (l *liveReply) flushRendering() {
func (l *liveReply) stopRendering() {
l.cancelRender()
<-l.renderDone
l.mu.Lock()
l.finalized = true
l.mu.Unlock()
}
// Delta appends the next fragment of assistant text. It satisfies
@@ -431,6 +443,17 @@ func (l *liveReply) appendFallbackLine(att gateway.MediaAttachment) {
}
l.mu.Lock()
if l.finalized {
// The renderer that would have picked up toolLines is already
// gone -- requestRender below would be a silent no-op, and the
// live message it would have edited is done being edited. Send
// this as its own message instead of dropping it.
l.mu.Unlock()
ctx, cancel := context.WithTimeout(context.Background(), mediaSendTimeout)
defer cancel()
l.g.sendMessage(ctx, l.b, l.chatID, l.messageThreadID, line)
return
}
l.toolLines = append(l.toolLines, line)
l.mu.Unlock()
l.requestRender()
+42
View File
@@ -1093,3 +1093,45 @@ func TestLiveReplyMediaFallbackReportsUndeliveredLocalFile(t *testing.T) {
t.Errorf("fallback lines = %q, want them to name the file", lines)
}
}
// TestLiveReplyMediaFallbackAfterFinalizeSendsFollowUp is the regression
// case for archie-core-atv2: Media is fire-and-forget with a 30s budget,
// far longer than the turn's text usually takes, so a delivery failing
// after doFinalize has already run and torn down the renderer is the
// normal case for a large upload, not an edge one. Appending to toolLines
// and calling requestRender at that point is a silent no-op -- the
// renderer goroutine is gone and nothing will ever read it -- so the
// failure must reach the user as its own message instead.
func TestLiveReplyMediaFallbackAfterFinalizeSendsFollowUp(t *testing.T) {
live, calls := newTestLiveReply(t, false)
live.newMediaSender = func(*bot.Bot, int64, int) gateway.MediaSender { return failingSender{} }
live.Delta("done")
live.doFinalize(context.Background(), "done")
before := len(*calls)
live.Media(gateway.MediaEvent{
ToolName: "send_file",
Attachment: gateway.MediaAttachment{Type: "document", Path: "/tmp/report.pdf", FileName: "report.pdf"},
})
live.waitMedia()
live.mu.Lock()
stillBuffered := strings.Join(live.toolLines, "\n")
live.mu.Unlock()
if stillBuffered != "" {
t.Errorf("toolLines = %q, want empty: nothing will ever render this buffer again", stillBuffered)
}
after := *calls
if len(after) != before+1 {
t.Fatalf("api calls after finalize = %d, want exactly one follow-up (had %d before)", len(after), before)
}
got := after[len(after)-1]
if got.method != "sendRichMessage" {
t.Fatalf("follow-up method = %q, want sendRichMessage", got.method)
}
if !strings.Contains(got.body(), "could not send") || !strings.Contains(got.body(), bot.EscapeMarkdown("report.pdf")) {
t.Errorf("follow-up body = %q, want it to report the undelivered file", got.body())
}
}