From 267de032de078fcd1def51cf0bd61ba6be008bbd Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 19 Sep 2026 02:17:59 +1000 Subject: [PATCH] fix(chat): remove unreachable Telegram-router restart wiring Review fix for PR #873. The previous attempt wired Router.Restart on the Telegram router, but that path is unreachable: telegram.go:260 registers an exact-match /restart handler (restartHandler, restart.go:24) which consumes the update before the chat router is reached. The wiring was therefore a false affordance: it made the Telegram router report RestartAvailable: true for a callback no production path invokes, and the test only passed because it assembled a LocalChatAdapter by hand rather than driving the composition production uses. Remove it. Telegram's own restartHandler remains the sole path to RequestRestart. The removals of boot.restartTelegram and telegramSetup.RegisterRestart stand: those were written and never read, which is the real defect (channels-telegram-3). Tests are retargeted and table-driven: buildTelegramRouter leaves Restart nil with and without an adapter (regression guard against re-adding the unreachable wiring), the router's /restart route reports not-configured, and RequestRestart's queue-then-reject contract is pinned at the adapter level. This deliberately does NOT fix dashboard /restart: the web router runs in the archie-gateway process while the Telegram adapter runs in archied, and no gateway->daemon restart RPC exists. That needs its own design (see the bead referenced in the PR body). --- internal/app/archied/telegram_setup.go | 3 - .../archied/telegram_setup_restart_test.go | 108 +++++++----------- internal/channels/telegram/restart_test.go | 38 ++++++ 3 files changed, 78 insertions(+), 71 deletions(-) diff --git a/internal/app/archied/telegram_setup.go b/internal/app/archied/telegram_setup.go index 92523a167..0e07a19f9 100644 --- a/internal/app/archied/telegram_setup.go +++ b/internal/app/archied/telegram_setup.go @@ -306,9 +306,6 @@ func buildTelegramRouter(ctx context.Context, tg *telegram.Gateway, s telegramSe router.Titles = newChatTitleGenerator(s) router.Log = s.Log configureTaskCommands(router, s.ChatTasks, s.ChatController, s.ChatTaskLister, s.DefaultChatIdentity) - if tg != nil { - router.Restart = func(ctx context.Context) error { return tg.RequestRestart() } - } if s.LLM != nil { turnRunner := newChatTurnRunner(ctx, tg.Name(), s, sessionStore, router) diff --git a/internal/app/archied/telegram_setup_restart_test.go b/internal/app/archied/telegram_setup_restart_test.go index 858a25165..c70bc3a5c 100644 --- a/internal/app/archied/telegram_setup_restart_test.go +++ b/internal/app/archied/telegram_setup_restart_test.go @@ -10,76 +10,48 @@ import ( "github.com/samcharles93/archie-core/internal/gateway" ) -// TestBuildTelegramRouterWiresRestartCallback drives the same Router -// composition path setupTelegramGateway uses and pins that the Telegram -// adapter's restart callback is wired into Router.Restart. The daemon used to -// capture tg.RequestRestart into a boot field and never read it, leaving -// Router.Restart nil so /restart always answered "not configured". -func TestBuildTelegramRouterWiresRestartCallback(t *testing.T) { - ctx := t.Context() - sessions := gateway.NewSessionStoreMemory() - t.Cleanup(func() { _ = sessions.Close() }) - - tg := telegram.New("test-token", nil, slog.Default()) - router := buildTelegramRouter(ctx, tg, telegramSetup{ - Cfg: config.NewHolder(config.Config{}), - SessionStore: sessions, - Log: slog.Default(), - }, sessions) - - if router.Restart == nil { - t.Fatal("router.Restart = nil, want the telegram adapter restart callback wired into the router") +// TestBuildTelegramRouterLeavesRestartUnwired pins that the Telegram router +// never wires Router.Restart, regardless of whether a Telegram adapter is in +// scope. Telegram's /restart is served by a dedicated exact-match handler +// (registerCommandHandlers → restartHandler), which is the only path that +// reaches RequestRestart; the router's own handleRestartAdapter is never +// reached on Telegram, so wiring Router.Restart here would be unreachable in +// production. It must stay nil and the router's /restart reply must keep +// reporting not-configured. +func TestBuildTelegramRouterLeavesRestartUnwired(t *testing.T) { + tests := []struct { + name string + tg *telegram.Gateway + }{ + {name: "telegram adapter present", tg: telegram.New("test-token", nil, slog.Default())}, + {name: "no telegram adapter", tg: nil}, } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctx := t.Context() + sessions := gateway.NewSessionStoreMemory() + t.Cleanup(func() { _ = sessions.Close() }) - // Invoking the router's restart path must call the adapter's restart - // callback exactly once: the first call queues the scoped reload and - // returns nil; a second call is rejected because a restart is already - // in progress. - if err := router.Restart(ctx); err != nil { - t.Fatalf("router.Restart = %v, want nil (callback called exactly once)", err) - } - if err := router.Restart(ctx); err == nil { - t.Fatal("router.Restart second call = nil, want error: telegram gateway restart already in progress") - } + router := buildTelegramRouter(ctx, tt.tg, telegramSetup{ + Cfg: config.NewHolder(config.Config{}), + SessionStore: sessions, + Log: slog.Default(), + }, sessions) - // The wired router must also surface the capability to the local chat - // snapshot, which is what the web UI's restart affordance reads. - adapter := &gateway.LocalChatAdapter{Router: router, Sessions: sessions} - snapshot, err := adapter.Snapshot(ctx) - if err != nil { - t.Fatalf("snapshot: %v", err) - } - if !snapshot.RestartAvailable { - t.Fatal("snapshot.RestartAvailable = false, want true when the restart callback is wired") - } -} - -// TestBuildTelegramRouterRestartUnconfigured pins the unchanged behaviour -// when no Telegram adapter exists: there is no restart callback, so -// Router.Restart stays nil and /restart still reports "not configured". -func TestBuildTelegramRouterRestartUnconfigured(t *testing.T) { - ctx := t.Context() - sessions := gateway.NewSessionStoreMemory() - t.Cleanup(func() { _ = sessions.Close() }) - - router := buildTelegramRouter(ctx, nil, telegramSetup{ - Cfg: config.NewHolder(config.Config{}), - SessionStore: sessions, - Log: slog.Default(), - }, sessions) - - if router.Restart != nil { - t.Fatal("router.Restart = non-nil with no telegram adapter, want nil") - } - - reply, err := router.Route(ctx, gateway.Inbound{Message: messaging.Message{ - Role: messaging.RoleUser, - Text: "/restart", - }}) - if err != nil { - t.Fatalf("Route(/restart) error = %v", err) - } - if reply != "Chat adapter restart is not configured." { - t.Fatalf("Route(/restart) = %q, want %q", reply, "Chat adapter restart is not configured.") + if router.Restart != nil { + t.Fatalf("router.Restart = %T, want nil: Telegram /restart is served by restartHandler, not this router", router.Restart) + } + + reply, err := router.Route(ctx, gateway.Inbound{Message: messaging.Message{ + Role: messaging.RoleUser, + Text: "/restart", + }}) + if err != nil { + t.Fatalf("Route(/restart) error = %v", err) + } + if reply != "Chat adapter restart is not configured." { + t.Fatalf("Route(/restart) = %q, want %q", reply, "Chat adapter restart is not configured.") + } + }) } } diff --git a/internal/channels/telegram/restart_test.go b/internal/channels/telegram/restart_test.go index 40610fb1d..96a74927d 100644 --- a/internal/channels/telegram/restart_test.go +++ b/internal/channels/telegram/restart_test.go @@ -73,6 +73,44 @@ func TestRequestRestartQueuesScopedReload(t *testing.T) { } } +// TestRequestRestartRejectsDuplicate pins RequestRestart's dedupe contract: +// the restart channel is buffered to one, so a second request while one is +// still queued is rejected rather than silently dropped or blocking a caller +// outside Telegram. restartHandler (restart.go) is the production driver of +// this path; the error surface is what callers observe. +func TestRequestRestartRejectsDuplicate(t *testing.T) { + tests := []struct { + name string + prefill bool + wantErr bool + }{ + {name: "first request queues", wantErr: false}, + {name: "second request rejected while queued", prefill: true, wantErr: true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g := New("tok", []int64{1}, slog.Default()) + if tt.prefill { + g.restartCh <- restartRequest{} + } + + err := g.RequestRestart() + if tt.wantErr { + if err == nil || err.Error() != "telegram gateway restart already in progress" { + t.Fatalf("RequestRestart error = %v, want %q", err, "telegram gateway restart already in progress") + } + return + } + if err != nil { + t.Fatalf("RequestRestart error = %v, want nil", err) + } + if got := len(g.restartCh); got != 1 { + t.Fatalf("restart queue length = %d, want 1", got) + } + }) + } +} + // A failing Reload must not be fatal: a bad config edit should leave the // gateway running on its previous settings, not take chat down for good. func TestReloadFailureKeepsPreviousSettings(t *testing.T) {