diff --git a/cmd/archied/telegram_setup.go b/cmd/archied/telegram_setup.go index 3d0e72655..91d11e0f1 100644 --- a/cmd/archied/telegram_setup.go +++ b/cmd/archied/telegram_setup.go @@ -155,13 +155,28 @@ func makeTelegramLLMResponder(ctx context.Context, tg *telegram.Gateway, s teleg if err != nil { return "", fmt.Errorf("resolve chat session: %w", err) } - if err := sessionStore.SaveMessage(ctx, sk, msg); err != nil { - return "", fmt.Errorf("save inbound chat message: %w", err) - } history, err := sessionStore.RecentMessages(ctx, sk, 100) if err != nil { return "", fmt.Errorf("load chat history: %w", err) } + // A redelivered update must not be answered twice. The store already + // refuses to store the message again, but that alone changed + // nothing here: the model still ran, costing a second billed turn, + // running any tool side effects again, and appending a second reply + // (replies carry no upstream reference, so they always append). + // Replaying the answer we already gave is both cheaper and correct. + if prior := gateway.PriorReply(history, sk, msg.SourceID, s.Cfg.BotUser); prior != "" { + s.Log.Info("replaying the reply to a redelivered message", + "session", sk, "source_id", msg.SourceID) + if onDelta != nil { + onDelta(prior) + } + return prior, nil + } + if err := sessionStore.SaveMessage(ctx, sk, msg); err != nil { + return "", fmt.Errorf("save inbound chat message: %w", err) + } + history = append(history, msg) // Chat turns were entirely unlogged, so a report of lost context had // no evidence to check against: session id, history depth and the // resolved persona are the three things needed to tell "wrong diff --git a/internal/gateway/redelivery_test.go b/internal/gateway/redelivery_test.go new file mode 100644 index 000000000..0bec633e9 --- /dev/null +++ b/internal/gateway/redelivery_test.go @@ -0,0 +1,145 @@ +package gateway + +import ( + "testing" + "time" +) + +// Making the store idempotent only stopped the duplicate *record*. The chat +// path saved the inbound message, ignored the fact that nothing changed, and +// went on to call the model again -- a second billed turn, any tool side +// effects run twice, and a second assistant reply appended (the reply carries +// no SourceID, so it always gets a fresh identity and always appends). +// +// PriorReply is the check that closes that: if this upstream message has +// already been answered, the answer is already in the history. +func TestPriorReply(t *testing.T) { + const ( + sessionID = "chat-1" + identity = "archie" + ) + + // history is built from (sourceID, from, text) triples; an empty + // sourceID means a locally generated message, as replies are. + type turn struct { + sourceID string + from string + text string + } + + tests := []struct { + name string + history []turn + sourceID string + want string + }{ + { + name: "an answered message returns its reply", + history: []turn{ + {"tg-1", "alice", "ping"}, + {"", identity, "pong"}, + }, + sourceID: "tg-1", + want: "pong", + }, + { + name: "the reply for the right message is chosen", + history: []turn{ + {"tg-1", "alice", "first"}, + {"", identity, "first reply"}, + {"tg-2", "alice", "second"}, + {"", identity, "second reply"}, + }, + sourceID: "tg-2", + want: "second reply", + }, + { + name: "an unanswered message is handled normally", + history: []turn{ + {"tg-1", "alice", "ping"}, + }, + sourceID: "tg-1", + want: "", + }, + { + // The turn died before replying. Re-running is correct here: + // the user is owed an answer. + name: "a message followed only by another user turn is unanswered", + history: []turn{ + {"tg-1", "alice", "ping"}, + {"tg-2", "alice", "still there?"}, + }, + sourceID: "tg-1", + want: "", + }, + { + name: "an unseen message is handled normally", + history: []turn{ + {"tg-1", "alice", "ping"}, + {"", identity, "pong"}, + }, + sourceID: "tg-99", + want: "", + }, + { + // Without an upstream ID there is nothing to match on, so the + // message cannot be recognised as a redelivery. + name: "no source id means no dedup", + history: []turn{ + {"tg-1", "alice", "ping"}, + {"", identity, "pong"}, + }, + sourceID: "", + want: "", + }, + { + name: "empty history", + history: nil, + sourceID: "tg-1", + want: "", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + history := make([]Message, 0, len(tc.history)) + for i, h := range tc.history { + history = append(history, Message{ + MessageID: newMessageIDFor(sessionID, h.sourceID, i), + SourceID: h.sourceID, + From: h.from, + Text: h.text, + At: time.Now().Add(time.Duration(i) * time.Second), + }) + } + + got := PriorReply(history, sessionID, tc.sourceID, identity) + if got != tc.want { + t.Errorf("PriorReply = %q, want %q", got, tc.want) + } + }) + } +} + +// newMessageIDFor mirrors what the store assigns: a derived ID for messages +// carrying an upstream reference, a unique one otherwise. +func newMessageIDFor(sessionID, sourceID string, i int) string { + if sourceID == "" { + return "local-" + string(rune('a'+i)) + } + return CanonicalMessageID(sessionID, sourceID) +} + +// The canonical ID must be the same one the store derives, or the lookup +// silently never matches and the dedup quietly stops working. +func TestCanonicalMessageIDMatchesTheStore(t *testing.T) { + const sessionID = "chat-1" + got := CanonicalMessageID(sessionID, "tg-7") + want := newMessageID(sessionID, "tg-7") + if got != want { + t.Fatalf("CanonicalMessageID = %q, want the store's %q", got, want) + } + if CanonicalMessageID(sessionID, "") != "" { + t.Error("CanonicalMessageID with no source id must be empty, not a random UUID") + } +} diff --git a/internal/gateway/session_store.go b/internal/gateway/session_store.go index 520d30a51..169303bbf 100644 --- a/internal/gateway/session_store.go +++ b/internal/gateway/session_store.go @@ -873,3 +873,53 @@ func (s *nellSessionStore) SearchMessages(ctx context.Context, sessionID string, Truncated: truncated && !hasMore, }, nil } + +// CanonicalMessageID returns the identity the store assigns to a message +// carrying sourceID in this session, or "" when there is no upstream +// reference to derive one from. +// +// It exists so a caller can ask "have I already handled this upstream +// message?" without duplicating the derivation. Duplicating it would let the +// two drift, and a lookup that silently never matches looks exactly like a +// message that has never been seen. +func CanonicalMessageID(sessionID, sourceID string) string { + if sourceID == "" { + return "" + } + return newMessageID(sessionID, sourceID) +} + +// PriorReply returns the reply already produced for an upstream message, or +// "" when it has not been answered yet. +// +// Making the store idempotent stopped redelivery duplicating the inbound +// *record*, but nothing consulted that: the chat path saved the message, +// ignored the fact that nothing changed, and called the model again. That +// meant a second billed turn, tool side effects run twice, and a second +// assistant reply appended -- replies carry no upstream reference, so they +// always get a fresh identity and always append. +// +// An answered message is one immediately followed by a turn from identity. +// A message with no reply after it was never answered -- the turn may have +// died partway -- and re-running is the right outcome there, since the user +// is still owed an answer. +// +// history is the recent window the caller already loaded, so a redelivery of +// something older than that window is not recognised. Channels redeliver only +// their recent undelivered queue, so this covers the case that occurs. +func PriorReply(history []Message, sessionID, sourceID, identity string) string { + id := CanonicalMessageID(sessionID, sourceID) + if id == "" { + return "" + } + for i, m := range history { + if m.MessageID != id { + continue + } + if i+1 < len(history) && history[i+1].From == identity { + return history[i+1].Text + } + return "" + } + return "" +}