diff --git a/config.example.toml b/config.example.toml index a278cc71f..025f80fdc 100644 --- a/config.example.toml +++ b/config.example.toml @@ -166,6 +166,14 @@ class = "ollama" # inbound HTTP concerns that happen to share the word "webhook". # webhook_addr = "0.0.0.0:8646" # under [chat], not [chat.telegram] +# Per-(channel, sender) inbound rate limiting, shared by every chat channel: +# Telegram keys by numeric user ID, email by SMTP from address, webhook by +# its configured route path. Off by default -- both fields must be set to a +# positive value. +# [chat.rate_limit] +# window = "1m" +# max_requests = 20 + # External MCP tools are launched and managed by Archie's MCP client. A # command of "npx" gets a persistent package cache under work_dir # (top of this file) automatically, so a daemon restart reuses it instead diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index 1dc56ff38..8fb4143cd 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -57,6 +57,7 @@ import ( "github.com/samcharles93/archie-core/internal/memory" "github.com/samcharles93/archie-core/internal/plugin" "github.com/samcharles93/archie-core/internal/plugin/pluginextract" + "github.com/samcharles93/archie-core/internal/ratelimit" "github.com/samcharles93/archie-core/internal/releaseupdate" "github.com/samcharles93/archie-core/internal/secret" "github.com/samcharles93/archie-core/internal/skill" @@ -124,6 +125,10 @@ type boot struct { bus *events.Bus restartTelegram func() error channelManager *status.Manager + // rateLimiter is the shared per-(channel, sender) inbound budget every + // chat Router is given. Nil when [chat.rate_limit] is not configured, + // which leaves rate limiting off. + rateLimiter *ratelimit.Limiter // cfgHolder is the daemon's one configuration Holder. Boot owns it and // the daemon reads through it, so a reload swaps one snapshot and every // reader sees it: there is no second holder to keep in step. @@ -572,6 +577,34 @@ func (b *boot) setupLLMAndChat() error { return nil } +// rateLimiterEvictInterval is how often an active Limiter sweeps entries +// whose hits have all aged out of the window, per internal/ratelimit's own +// documented ticker contract. +const rateLimiterEvictInterval = time.Minute + +// startRateLimiter constructs b.rateLimiter from cfg when configured, and +// drives its documented EvictStale ticker for the life of ctx. Leaves +// b.rateLimiter nil (rate limiting off) when cfg is not enabled. +func (b *boot) startRateLimiter(ctx context.Context, cfg config.RateLimitConfig) { + if !cfg.Enabled() { + return + } + b.rateLimiter = ratelimit.New(cfg.Window, cfg.MaxRequests) + limiter := b.rateLimiter + go func() { + ticker := time.NewTicker(rateLimiterEvictInterval) + defer ticker.Stop() + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + limiter.EvictStale() + } + } + }() +} + // setupGateways assembles the Telegram, email and webhook gateways. It // returns false when the Telegram gateway could not start, which the // caller treats as a fatal boot error. @@ -579,6 +612,7 @@ func (b *boot) setupLLMAndChat() error { // Multi-agent collaboration PRD phase C (docs/prds/multi-agent-collaboration.md). func (b *boot) setupGateways(ctx context.Context, cfgPath, overlayPath string) bool { cfg, log := b.cfg, b.log + b.startRateLimiter(ctx, cfg.Chat.RateLimit) start, ok := setupTelegramGateway(ctx, telegramSetup{ Cfg: config.NewHolder(cfg), CfgPath: cfgPath, OverlayPath: overlayPath, St: b.stateStore, LLM: b.llm, ChatModels: b.chatModels, ToolReg: b.toolReg, @@ -602,6 +636,7 @@ func (b *boot) setupGateways(ctx context.Context, cfgPath, overlayPath string) b Bus: b.bus, RegisterRestart: func(request func() error) { b.restartTelegram = request }, Log: log, ChannelManager: b.channelManager, AgentStatus: b.agentStatus, + RateLimiter: b.rateLimiter, }) if !ok { return false @@ -610,58 +645,70 @@ func (b *boot) setupGateways(ctx context.Context, cfgPath, overlayPath string) b b.startGateways = append(b.startGateways, start) } - // ── Email gateway (optional) ─────────────────────────────────── - if cfg.Chat.Email.ListenAddr != "" { - em := email.New(cfg.Chat.Email.ListenAddr, cfg.Chat.Email.RelayAddr, log) - emRouter := gateway.NewRouter(b.stateStore, nil, "email") - configureTaskCommands(emRouter, b.chatTasks, b.chatController, chatTaskListerAdapter{tasks: b.stateStore.Tasks}, b.defaultChatIdentity) - b.startGateways = append(b.startGateways, func() { - go func() { - lifecycle := gateway.Lifecycle{ - Starting: func() { b.channelManager.MarkStarting("email") }, - Running: func() { - b.channelManager.MarkRunning("email") - log.Info("email gateway started", "addr", cfg.Chat.Email.ListenAddr) - }, - } - if err := em.Start(ctx, emRouter, lifecycle); err != nil && ctx.Err() == nil { - b.channelManager.MarkFailed("email", err.Error()) - log.Error("email gateway stopped", "err", err) - } - }() - }) - } - - // ── Webhook gateway (optional) ───────────────────────────────── - // Enabled when chat.webhook is set to a host:port listen address. - if cfg.Chat.WebhookAddr != "" { - host, port := parseListenAddr(cfg.Chat.WebhookAddr, "0.0.0.0", 8644) - wh := webhook.New( - host, port, - []webhook.RouteConfig{{Path: "/webhook"}}, - log, - ) - whRouter := gateway.NewRouter(b.stateStore, nil, "webhook") - configureTaskCommands(whRouter, b.chatTasks, b.chatController, chatTaskListerAdapter{tasks: b.stateStore.Tasks}, b.defaultChatIdentity) - b.startGateways = append(b.startGateways, func() { - go func() { - lifecycle := gateway.Lifecycle{ - Starting: func() { b.channelManager.MarkStarting("webhook") }, - Running: func() { - b.channelManager.MarkRunning("webhook") - log.Info("webhook gateway started", "addr", fmt.Sprintf("%s:%d", host, port)) - }, - } - if err := wh.Start(ctx, whRouter, lifecycle); err != nil && ctx.Err() == nil { - b.channelManager.MarkFailed("webhook", err.Error()) - log.Error("webhook gateway stopped", "err", err) - } - }() - }) - } + b.setupEmailGateway(ctx, cfg, log) + b.setupWebhookGateway(ctx, cfg, log) return true } +// setupEmailGateway registers the optional inbound email gateway when +// chat.email.listen_addr is configured. +func (b *boot) setupEmailGateway(ctx context.Context, cfg config.Config, log *slog.Logger) { + if cfg.Chat.Email.ListenAddr == "" { + return + } + em := email.New(cfg.Chat.Email.ListenAddr, cfg.Chat.Email.RelayAddr, log) + emRouter := gateway.NewRouter(b.stateStore, nil, "email") + emRouter.Limiter = b.rateLimiter + configureTaskCommands(emRouter, b.chatTasks, b.chatController, chatTaskListerAdapter{tasks: b.stateStore.Tasks}, b.defaultChatIdentity) + b.startGateways = append(b.startGateways, func() { + go func() { + lifecycle := gateway.Lifecycle{ + Starting: func() { b.channelManager.MarkStarting("email") }, + Running: func() { + b.channelManager.MarkRunning("email") + log.Info("email gateway started", "addr", cfg.Chat.Email.ListenAddr) + }, + } + if err := em.Start(ctx, emRouter, lifecycle); err != nil && ctx.Err() == nil { + b.channelManager.MarkFailed("email", err.Error()) + log.Error("email gateway stopped", "err", err) + } + }() + }) +} + +// setupWebhookGateway registers the optional inbound webhook gateway when +// chat.webhook_addr is configured. +func (b *boot) setupWebhookGateway(ctx context.Context, cfg config.Config, log *slog.Logger) { + if cfg.Chat.WebhookAddr == "" { + return + } + host, port := parseListenAddr(cfg.Chat.WebhookAddr, "0.0.0.0", 8644) + wh := webhook.New( + host, port, + []webhook.RouteConfig{{Path: "/webhook"}}, + log, + ) + whRouter := gateway.NewRouter(b.stateStore, nil, "webhook") + whRouter.Limiter = b.rateLimiter + configureTaskCommands(whRouter, b.chatTasks, b.chatController, chatTaskListerAdapter{tasks: b.stateStore.Tasks}, b.defaultChatIdentity) + b.startGateways = append(b.startGateways, func() { + go func() { + lifecycle := gateway.Lifecycle{ + Starting: func() { b.channelManager.MarkStarting("webhook") }, + Running: func() { + b.channelManager.MarkRunning("webhook") + log.Info("webhook gateway started", "addr", fmt.Sprintf("%s:%d", host, port)) + }, + } + if err := wh.Start(ctx, whRouter, lifecycle); err != nil && ctx.Err() == nil { + b.channelManager.MarkFailed("webhook", err.Error()) + log.Error("webhook gateway stopped", "err", err) + } + }() + }) +} + // loadWorkflows builds the workflow registry from the skill catalog. // Plugin-defined workflows override built-ins of the same name; // built-ins fill gaps. diff --git a/internal/app/archied/telegram_setup.go b/internal/app/archied/telegram_setup.go index c77017764..7fa8a1169 100644 --- a/internal/app/archied/telegram_setup.go +++ b/internal/app/archied/telegram_setup.go @@ -22,6 +22,7 @@ import ( "github.com/samcharles93/archie-core/internal/gateway" "github.com/samcharles93/archie-core/internal/infrastructure/configuration" "github.com/samcharles93/archie-core/internal/installtype" + "github.com/samcharles93/archie-core/internal/ratelimit" "github.com/samcharles93/archie-core/internal/releaseannounce" "github.com/samcharles93/archie-core/internal/releaseupdate" "github.com/samcharles93/archie-core/internal/secret" @@ -65,6 +66,9 @@ type telegramSetup struct { // Nil disables the agent component of RunningVersions. AgentStatus *daemon.AgentStatus Secrets *secret.Registry + // RateLimiter budgets inbound messages per sender when set. Nil + // leaves rate limiting off. + RateLimiter *ratelimit.Limiter } func resolveTelegramToken(cfg config.TelegramConfig, registry *secret.Registry) (string, error) { @@ -283,6 +287,7 @@ func buildTelegramRouter(ctx context.Context, tg *telegram.Gateway, s telegramSe // a closure -- but this way the dependency is visible rather than // captured by reference. router := gateway.NewRouter(s.St, nil, "telegram") + router.Limiter = s.RateLimiter router.Models = s.ChatModels router.Updates = s.Updates router.Personas = s.Personas diff --git a/internal/channels/email/email.go b/internal/channels/email/email.go index 9011a9a6a..16f707d27 100644 --- a/internal/channels/email/email.go +++ b/internal/channels/email/email.go @@ -205,6 +205,7 @@ func (g *Gateway) processMessage(ctx context.Context, from, to, raw string) { msg := gateway.Inbound{Message: messaging.Message{ ConversationID: messaging.ConversationID{ChannelID: to}, Sender: from, + SenderID: from, Role: messaging.RoleUser, Text: text, }} diff --git a/internal/channels/email/email_test.go b/internal/channels/email/email_test.go index 7197e2074..00d4be007 100644 --- a/internal/channels/email/email_test.go +++ b/internal/channels/email/email_test.go @@ -69,6 +69,9 @@ func TestSMTPReceiveAndRoute(t *testing.T) { if in.Message.Sender != "sender@test.com" { return "", fmt.Errorf("unexpected sender: %s", in.Message.Sender) } + if in.Message.SenderID != "sender@test.com" { + return "", fmt.Errorf("unexpected SenderID: %s", in.Message.SenderID) + } return "got it", nil }, "email") diff --git a/internal/channels/telegram/telegram.go b/internal/channels/telegram/telegram.go index 3f6763127..8e9799fd6 100644 --- a/internal/channels/telegram/telegram.go +++ b/internal/channels/telegram/telegram.go @@ -616,6 +616,7 @@ func (g *Gateway) submitTurn(ctx context.Context, b *bot.Bot, msg *models.Messag SourceID: fmt.Sprintf("%d", msg.ID), ConversationID: conversationID(msg), Sender: msg.From.Username, + SenderID: fmt.Sprintf("%d", msg.From.ID), Role: messaging.RoleUser, Text: msg.Text, At: time.Unix(int64(msg.Date), 0).UTC(), diff --git a/internal/channels/webhook/webhook.go b/internal/channels/webhook/webhook.go index f74579f43..2980f1585 100644 --- a/internal/channels/webhook/webhook.go +++ b/internal/channels/webhook/webhook.go @@ -173,8 +173,12 @@ func (g *Gateway) handleWebhook(route *RouteConfig) http.HandlerFunc { msg := gateway.Inbound{Message: messaging.Message{ ConversationID: messaging.ConversationID{ChannelID: route.Path}, Sender: "webhook", - Role: messaging.RoleUser, - Text: text, + // Webhooks have no per-caller identity; the configured route + // is the closest thing to one, since it corresponds to a + // single external source. + SenderID: route.Path, + Role: messaging.RoleUser, + Text: text, }} reply, err := router.Route(r.Context(), msg) if err != nil { diff --git a/internal/channels/webhook/webhook_test.go b/internal/channels/webhook/webhook_test.go index 7114c2d20..cf91a74b5 100644 --- a/internal/channels/webhook/webhook_test.go +++ b/internal/channels/webhook/webhook_test.go @@ -80,7 +80,11 @@ func TestWebhookHandlerNotStarted(t *testing.T) { func TestWebhookHandlerRoute(t *testing.T) { log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelWarn})) g := New("", 0, []RouteConfig{{Path: "/hook"}}, log) - g.router = gateway.NewRouter(nil, nil, "webhook") + var gotSenderID string + g.router = gateway.NewRouter(nil, func(ctx context.Context, in gateway.Inbound) (string, error) { + gotSenderID = in.Message.SenderID + return "ok", nil + }, "webhook") handler := g.WebhookHandler() payload := `{"text":"hello from webhook"}` @@ -89,6 +93,11 @@ func TestWebhookHandlerRoute(t *testing.T) { if rec.Code != http.StatusAccepted { t.Errorf("status = %d, want 202", rec.Code) } + // Webhook has no native per-caller identity, so the configured route + // path is used to key rate limiting instead (see internal/ratelimit). + if gotSenderID != "/hook" { + t.Errorf("SenderID = %q, want the route path", gotSenderID) + } } func TestWebhookHandlerInvalidSignature(t *testing.T) { diff --git a/internal/config/config.go b/internal/config/config.go index 3a3a99f8c..4d5c30583 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -910,6 +910,30 @@ type ChatConfig struct { // Empty disables the webhook gateway. WebhookAddr string `toml:"webhook_addr" yaml:"webhook_addr"` Telegram TelegramConfig `toml:"telegram" yaml:"telegram"` + // RateLimit budgets inbound messages per (channel, sender) across + // every chat channel. Zero MaxRequests (the default) leaves rate + // limiting off entirely -- it is an opt-in control, not a default + // throttle that could surprise an existing deployment. + RateLimit RateLimitConfig `toml:"rate_limit" yaml:"rate_limit"` +} + +// RateLimitConfig configures the sliding-window inbound rate limiter +// (internal/ratelimit) shared by every chat channel. A sender is +// identified per channel: Telegram by numeric user ID, email by the SMTP +// from address, webhook by the configured route path. Disabled unless +// both fields are set to a positive value. +type RateLimitConfig struct { + // Window is the rolling interval MaxRequests is budgeted over. + Window time.Duration `toml:"window" yaml:"window"` + // MaxRequests is the most inbound messages one sender may send in + // Window. Zero (the default) disables rate limiting. + MaxRequests int `toml:"max_requests" yaml:"max_requests"` +} + +// Enabled reports whether RateLimit is configured to actually limit +// anything. +func (c RateLimitConfig) Enabled() bool { + return c.Window > 0 && c.MaxRequests > 0 } // TelegramConfig configures the Telegram Bot API channel. diff --git a/internal/config/rate_limit_test.go b/internal/config/rate_limit_test.go new file mode 100644 index 000000000..7d4bca5c6 --- /dev/null +++ b/internal/config/rate_limit_test.go @@ -0,0 +1,30 @@ +package config + +import ( + "testing" + "time" +) + +// TestRateLimitConfigEnabled pins Enabled as opt-in: the zero value (an +// absent [chat.rate_limit] table) must leave rate limiting off, and both +// fields must be positive before it counts as configured. +func TestRateLimitConfigEnabled(t *testing.T) { + tests := []struct { + name string + cfg RateLimitConfig + want bool + }{ + {"zero value", RateLimitConfig{}, false}, + {"window only", RateLimitConfig{Window: time.Minute}, false}, + {"max requests only", RateLimitConfig{MaxRequests: 20}, false}, + {"both set", RateLimitConfig{Window: time.Minute, MaxRequests: 20}, true}, + {"negative max requests", RateLimitConfig{Window: time.Minute, MaxRequests: -1}, false}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + if got := tc.cfg.Enabled(); got != tc.want { + t.Errorf("Enabled() = %v, want %v", got, tc.want) + } + }) + } +} diff --git a/internal/domain/messaging/message.go b/internal/domain/messaging/message.go index 563aad010..dc57d616b 100644 --- a/internal/domain/messaging/message.go +++ b/internal/domain/messaging/message.go @@ -80,6 +80,14 @@ type Message struct { // so history search and transcript rendering keep working. Sender string + // SenderID is the channel-native stable identifier for the party that + // sent this message (e.g. a Telegram numeric user ID, an SMTP from + // address, or a webhook route path), distinct from Sender's + // human-readable display form. Empty when the channel has no stable + // per-sender identity to offer. Used to key per-identity inbound + // controls such as rate limiting. + SenderID string + Role Role Text string diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 2d2f65adb..8f0b82c01 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -21,6 +21,7 @@ import ( "unicode" "github.com/samcharles93/archie-core/internal/domain/messaging" + "github.com/samcharles93/archie-core/internal/ratelimit" "github.com/samcharles93/archie-core/internal/releaseupdate" "github.com/samcharles93/archie-core/internal/taskstate" ) @@ -231,7 +232,12 @@ type Router struct { // Log is the optional logger for the best-effort background title // path. Nil drops those diagnostics silently; nothing on this path is // important enough to fail the turn over. - Log *slog.Logger + Log *slog.Logger + // Limiter enforces a per-(gateway, sender) inbound budget when set. + // Nil disables rate limiting entirely. Messages with no SenderID + // (channels that cannot supply a stable per-sender identity) are + // never limited, since there is no key to charge them against. + Limiter *ratelimit.Limiter sessionTracker *sessionTracker gatewayName string // titlingMu guards titling, the set of sessions with a title proposal @@ -260,9 +266,34 @@ func (r *Router) SessionTracker() *sessionTracker { return r.sessionTracker } +// rateLimitReply is returned to a sender who has exceeded their inbound +// budget, in place of normal dispatch. +const rateLimitReply = "You're sending messages too quickly. Please wait a moment and try again." + +// checkRateLimit reports whether msg is over its sender's inbound budget. +// A nil Limiter (rate limiting not configured) or an empty SenderID +// (the channel has no stable per-sender identity to charge) always +// allows. +func (r *Router) checkRateLimit(msg messaging.Message) (blocked bool) { + if r.Limiter == nil || msg.SenderID == "" { + return false + } + return !r.Limiter.Allow(r.gatewayName, msg.SenderID) +} + // 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) { + if r.checkRateLimit(in.Message) { + return rateLimitReply, nil + } + return r.route(ctx, in) +} + +// route is Route's continuation once the caller has already cleared the +// rate-limit check (or is deliberately skipping it, as RouteStream's +// local-command fallthrough does to avoid charging one message twice). +func (r *Router) route(ctx context.Context, in Inbound) (string, error) { text := strings.TrimSpace(in.Message.Text) cmd, _ := parseCmd(text, r.gatewayName) @@ -449,9 +480,12 @@ func (r *Router) RouteStream(ctx context.Context, in Inbound, stream TurnStream) if r.LLMStream == nil || stream == nil { return r.Route(ctx, in) } + if r.checkRateLimit(in.Message) { + return rateLimitReply, nil + } cmd, _ := parseCmd(strings.TrimSpace(in.Message.Text), r.gatewayName) if isLocalCommand(cmd) || strings.HasPrefix(cmd, "/") { - return r.Route(ctx, in) + return r.route(ctx, in) } reply, err := r.LLMStream(ctx, in, stream) if err == nil { diff --git a/internal/gateway/gateway_ratelimit_test.go b/internal/gateway/gateway_ratelimit_test.go new file mode 100644 index 000000000..a0cf4261a --- /dev/null +++ b/internal/gateway/gateway_ratelimit_test.go @@ -0,0 +1,145 @@ +package gateway + +import ( + "context" + "testing" + "time" + + "github.com/samcharles93/archie-core/internal/ratelimit" +) + +// senderInbound is inbound with SenderID set, the field the rate limiter +// keys on. +func senderInbound(senderID, text string) Inbound { + in := inbound("chat-1", text) + in.Message.SenderID = senderID + return in +} + +func TestRouteRateLimitBlocksOverBudget(t *testing.T) { + r := NewRouter(nil, fakeLLM, "test") + r.Limiter = ratelimit.New(time.Minute, 1) + + reply, err := r.Route(context.Background(), senderInbound("u1", "hello")) + if err != nil { + t.Fatalf("Route: %v", err) + } + if reply != "llm: hello" { + t.Fatalf("first message reply = %q, want it to reach the LLM", reply) + } + + reply, err = r.Route(context.Background(), senderInbound("u1", "hello again")) + if err != nil { + t.Fatalf("Route: %v", err) + } + if reply != rateLimitReply { + t.Fatalf("second message reply = %q, want the rate-limit reply", reply) + } +} + +func TestRouteRateLimitIsPerSender(t *testing.T) { + r := NewRouter(nil, fakeLLM, "test") + r.Limiter = ratelimit.New(time.Minute, 1) + + if _, err := r.Route(context.Background(), senderInbound("u1", "hi")); err != nil { + t.Fatalf("Route: %v", err) + } + reply, err := r.Route(context.Background(), senderInbound("u2", "hi")) + if err != nil { + t.Fatalf("Route: %v", err) + } + if reply != "llm: hi" { + t.Fatalf("a different sender's message reply = %q, want it unaffected by u1's budget", reply) + } +} + +func TestRouteNoLimiterConfiguredNeverBlocks(t *testing.T) { + r := NewRouter(nil, fakeLLM, "test") + + for i := range 5 { + reply, err := r.Route(context.Background(), senderInbound("u1", "hello")) + if err != nil { + t.Fatalf("Route: %v", err) + } + if reply != "llm: hello" { + t.Fatalf("call %d reply = %q, want it to reach the LLM with no limiter configured", i, reply) + } + } +} + +// TestRouteEmptySenderIDNeverLimited pins the deliberate fail-open for +// channels (webhook routes with no configured path identity, e.g.) that +// have no stable per-sender identity to charge: there is no shared key to +// throttle everyone under, so they are left unlimited rather than blocked. +func TestRouteEmptySenderIDNeverLimited(t *testing.T) { + r := NewRouter(nil, fakeLLM, "test") + r.Limiter = ratelimit.New(time.Minute, 1) + + for i := range 3 { + reply, err := r.Route(context.Background(), inbound("chat-1", "hello")) + if err != nil { + t.Fatalf("Route: %v", err) + } + if reply != "llm: hello" { + t.Fatalf("call %d reply = %q, want it to reach the LLM with no SenderID", i, reply) + } + } +} + +// TestRouteStreamLocalCommandChargesExactlyOnce pins RouteStream's local +// command fallthrough (into route, not Route) against double-charging the +// budget: a status query must consume the same one unit whether it goes +// through Route or RouteStream. +func TestRouteStreamLocalCommandChargesExactlyOnce(t *testing.T) { + r := NewRouter(&fakeStore{counts: map[string]int{}}, nil, "test") + r.Limiter = ratelimit.New(time.Minute, 2) + r.LLMStream = func(context.Context, Inbound, TurnStream) (string, error) { + return "streamed", nil + } + + if _, err := r.RouteStream(context.Background(), senderInbound("u1", "/status"), DeltaFunc(func(string) {})); err != nil { + t.Fatalf("RouteStream: %v", err) + } + + // Budget was 2; the local command must have consumed exactly one, so + // exactly one more free-text message should still get through. + reply, err := r.RouteStream(context.Background(), senderInbound("u1", "hello"), DeltaFunc(func(string) {})) + if err != nil { + t.Fatalf("RouteStream: %v", err) + } + if reply != "streamed" { + t.Fatalf("second call reply = %q, want it to have budget left", reply) + } + + reply, err = r.RouteStream(context.Background(), senderInbound("u1", "hello again"), DeltaFunc(func(string) {})) + if err != nil { + t.Fatalf("RouteStream: %v", err) + } + if reply != rateLimitReply { + t.Fatalf("third call reply = %q, want the budget exhausted after 2 charged calls", reply) + } +} + +func TestRouteStreamRateLimitBlocksStreamedReply(t *testing.T) { + r := NewRouter(nil, nil, "test") + r.Limiter = ratelimit.New(time.Minute, 1) + streamCalls := 0 + r.LLMStream = func(context.Context, Inbound, TurnStream) (string, error) { + streamCalls++ + return "streamed", nil + } + + if _, err := r.RouteStream(context.Background(), senderInbound("u1", "hello"), DeltaFunc(func(string) {})); err != nil { + t.Fatalf("RouteStream: %v", err) + } + reply, err := r.RouteStream(context.Background(), senderInbound("u1", "hello again"), DeltaFunc(func(string) {})) + if err != nil { + t.Fatalf("RouteStream: %v", err) + } + if reply != rateLimitReply { + t.Fatalf("reply = %q, want the rate-limit reply", reply) + } + if streamCalls != 1 { + t.Fatalf("LLMStream called %d times, want exactly 1 (the blocked call must not reach it)", streamCalls) + } +} diff --git a/internal/ratelimit/limiter_test.go b/internal/ratelimit/limiter_test.go index e85f1db3c..0b6cdd9c9 100644 --- a/internal/ratelimit/limiter_test.go +++ b/internal/ratelimit/limiter_test.go @@ -379,3 +379,50 @@ func TestEvictStaleConcurrentWithAllow(t *testing.T) { t.Errorf("hits map has %d entries, want 1 (only the fresh key); stale entries were not evicted", len(l.hits)) } } + +// TestEvictStaleDrivenByTicker exercises the ticker-driven usage the +// package doc mandates ("a long-lived limiter should have EvictStale +// driven on a ticker") end to end with a real time.Ticker, rather than +// every other test's direct call against a frozen fake clock. It is the +// only test in the suite that actually starts a ticker goroutine. +func TestEvictStaleDrivenByTicker(t *testing.T) { + const window = 15 * time.Millisecond + l := New(window, 1) + + l.Allow("telegram", "u1") + if len(l.hits) != 1 { + t.Fatalf("hits map has %d entries after Allow, want 1", len(l.hits)) + } + + stop := make(chan struct{}) + done := make(chan struct{}) + go func() { + defer close(done) + ticker := time.NewTicker(5 * time.Millisecond) + defer ticker.Stop() + for { + select { + case <-stop: + return + case <-ticker.C: + l.EvictStale() + } + } + }() + t.Cleanup(func() { + close(stop) + <-done + }) + + deadline := time.Now().Add(2 * time.Second) + for time.Now().Before(deadline) { + l.mu.Lock() + n := len(l.hits) + l.mu.Unlock() + if n == 0 { + return + } + time.Sleep(5 * time.Millisecond) + } + t.Fatal("ticker-driven EvictStale never emptied the hits map for a stale entry") +}