diff --git a/internal/channels/telegram/live.go b/internal/channels/telegram/live.go index 301895ba6..a773089c0 100644 --- a/internal/channels/telegram/live.go +++ b/internal/channels/telegram/live.go @@ -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() diff --git a/internal/channels/telegram/live_test.go b/internal/channels/telegram/live_test.go index 56e88609b..7fce387f0 100644 --- a/internal/channels/telegram/live_test.go +++ b/internal/channels/telegram/live_test.go @@ -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()) + } +}