From 04dfa29dcb3850b7986420e5c98e40915d2bdc89 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 26 Sep 2026 20:42:52 +1000 Subject: [PATCH] fix(webhook): reject rate-limited deliveries with 429 instead of chat prose or a false 202 Router.Route returned the chat-facing rate-limit prose with a nil error, so the webhook handler had no way to tell a genuine dispatch outcome from a rejection: DeliverTo "origin" echoed the prose as a 200 reply, and any other route got a 202 for an event that was actually dropped before reaching the LLM, so the source never retries it. ChatReply now carries RateLimited, set by a new Router.RouteResult (Route is now a thin wrapper over it) so chat channels that render Text to a human are unaffected. The webhook handler checks it and answers 429, matching the precedent internal/webhookguard.RateLimiter's other callers already set. --- internal/channels/webhook/webhook.go | 11 +++++++++ internal/channels/webhook/webhook_test.go | 29 +++++++++++++++++++++++ internal/domain/messaging/chat_stream.go | 6 +++++ internal/gateway/chat_local.go | 4 ++-- internal/gateway/gateway.go | 16 +++++++++++-- 5 files changed, 62 insertions(+), 4 deletions(-) diff --git a/internal/channels/webhook/webhook.go b/internal/channels/webhook/webhook.go index 2725b0cba..ed958878c 100644 --- a/internal/channels/webhook/webhook.go +++ b/internal/channels/webhook/webhook.go @@ -195,6 +195,17 @@ func (g *Gateway) handleWebhook(route *RouteConfig) http.HandlerFunc { return } + // A rate-limited delivery was dropped before reaching the LLM, not + // completed: it must not be echoed back as a successful "origin" reply + // (chat prose, meant for a human) or 202'd as accepted (indistinguishable + // from a delivered event, so the source would never retry it). 429 lets + // the source back off and retry, the way internal/webhookguard.RateLimiter's + // other callers already respond (archie-core-1173). + if reply.RateLimited { + http.Error(w, "rate limited", http.StatusTooManyRequests) + return + } + if route.DeliverTo == "origin" && reply.Text != "" { w.Header().Set("Content-Type", "text/plain") _, _ = w.Write([]byte(reply.Text)) diff --git a/internal/channels/webhook/webhook_test.go b/internal/channels/webhook/webhook_test.go index 71f31e1a1..6be25ca60 100644 --- a/internal/channels/webhook/webhook_test.go +++ b/internal/channels/webhook/webhook_test.go @@ -120,6 +120,35 @@ func TestWebhookHandlerRoute(t *testing.T) { } } +// TestWebhookHandlerRateLimitedDeliveryIsRejected pins archie-core-1173: a +// rate-limited webhook delivery must be surfaced to the origin as an +// observable rejection (429), never as chat prose echoed back with a 200, and +// never as a 202 that claims a dropped event was accepted (the origin would +// then never retry it). +func TestWebhookHandlerRateLimitedDeliveryIsRejected(t *testing.T) { + for _, deliverTo := range []string{"origin", ""} { + t.Run("DeliverTo="+deliverTo, func(t *testing.T) { + log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelWarn})) + g := New("", 0, []RouteConfig{{Path: "/hook", DeliverTo: deliverTo}}, log) + g.client = &fakeChatContract{ + routeFunc: func(context.Context, messaging.Inbound) (messaging.ChatReply, error) { + return messaging.ChatReply{Text: "You're sending messages too quickly. Please wait a moment and try again.", RateLimited: true}, nil + }, + } + + handler := g.WebhookHandler() + rec := httptest.NewRecorder() + handler.ServeHTTP(rec, httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/hook", strings.NewReader(`{"text":"hello"}`))) + if rec.Code != http.StatusTooManyRequests { + t.Errorf("status = %d, want 429 so the origin can back off and retry", rec.Code) + } + if strings.Contains(rec.Body.String(), "too quickly") { + t.Errorf("body = %q, want no chat prose echoed to a webhook origin", rec.Body.String()) + } + }) + } +} + func TestWebhookHandlerInvalidSignature(t *testing.T) { g := New("", 0, []RouteConfig{{Path: "/hook", Secret: "secret"}}, slog.Default()) g.client = &fakeChatContract{} diff --git a/internal/domain/messaging/chat_stream.go b/internal/domain/messaging/chat_stream.go index b190fd412..b7eb4745f 100644 --- a/internal/domain/messaging/chat_stream.go +++ b/internal/domain/messaging/chat_stream.go @@ -24,6 +24,12 @@ type ChatSnapshot struct { type ChatReply struct { Text string SessionID string + // RateLimited reports whether Text is the inbound rate-limit reply rather + // than a real dispatch outcome. A human-facing channel can ignore it and + // send Text as usual; a channel with no human reading replies (a webhook) + // must not treat it as a normal reply or a delivered event -- see + // archie-core-1173. + RateLimited bool } // ChatCancellation is what Cancel returns. diff --git a/internal/gateway/chat_local.go b/internal/gateway/chat_local.go index 218351df5..003f0b9c1 100644 --- a/internal/gateway/chat_local.go +++ b/internal/gateway/chat_local.go @@ -89,12 +89,12 @@ func (a *LocalChatAdapter) RecentTurns(ctx context.Context, id string, n int) ([ } func (a *LocalChatAdapter) Route(ctx context.Context, in Inbound) (ChatReply, error) { - reply, err := a.Router.Route(ctx, in) + reply, rateLimited, err := a.Router.RouteResult(ctx, in) if err != nil { return ChatReply{}, err } id, err := a.Router.ResolveSessionKey(ctx, in) - return ChatReply{Text: reply, SessionID: id}, err + return ChatReply{Text: reply, SessionID: id, RateLimited: rateLimited}, err } func (a *LocalChatAdapter) Cancel(ctx context.Context, id string) (ChatCancellation, error) { diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index f1fc6160a..18ee77569 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -269,10 +269,22 @@ func inboundBudgetKey(in Inbound) string { // Route dispatches msg and returns the reply. Gateway-local commands // are handled directly; everything else goes to the LLM responder. func (r *Router) Route(ctx context.Context, in Inbound) (string, error) { + reply, _, err := r.RouteResult(ctx, in) + return reply, err +} + +// RouteResult is Route, plus whether the sender was blocked by its inbound +// rate limit. A caller with a human reading the reply (chat channels) can +// ignore the flag and treat the reply text as usual; a caller with no human +// there (a webhook) needs it to answer with a proper rejection instead of +// echoing the rate-limit prose as a successful delivery, or accepting an +// event that was actually dropped (archie-core-1173). +func (r *Router) RouteResult(ctx context.Context, in Inbound) (reply string, rateLimited bool, err error) { if r.checkRateLimit(in) { - return rateLimitReply, nil + return rateLimitReply, true, nil } - return r.route(ctx, in) + reply, err = r.route(ctx, in) + return reply, false, err } // route is Route's continuation once the caller has already cleared the