From fe8d64cc9ffbb14d17495b82505d3800db03ee53 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 19 Sep 2026 01:50:41 +1000 Subject: [PATCH] fix(chat): wire the Telegram adapter restart callback into the chat router /restart could never work in production. boot.setupGateways captured the Telegram adapter's restart callback into boot.restartTelegram, a field no code ever read, so gateway.Router.Restart was nil in every composition: handleRestartAdapter always answered "not configured" and LocalChatAdapter's snapshot always reported RestartAvailable: false. Fix at the producer: buildTelegramRouter assigns Router.Restart directly from the adapter it already holds. The dead field and the telegramSetup.RegisterRestart indirection are deleted rather than kept alive. Red-then-green: TestBuildTelegramRouterWiresRestartCallback failed with "router.Restart = nil, want the telegram adapter restart callback wired into the router" before the change. TestBuildTelegramRouterRestartUnconfigured pins the unchanged no-adapter behaviour, including the not-configured reply. --- internal/app/archied/bootstrap.go | 11 ++- internal/app/archied/telegram_setup.go | 7 +- .../archied/telegram_setup_restart_test.go | 85 +++++++++++++++++++ 3 files changed, 93 insertions(+), 10 deletions(-) create mode 100644 internal/app/archied/telegram_setup_restart_test.go diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index 78f95a167..ff05e3cf1 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -125,9 +125,8 @@ type boot struct { catalog modelcatalog.Snapshot catalogModels []string - bus *events.Bus - restartTelegram func() error - channelManager *status.Manager + bus *events.Bus + 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. @@ -647,9 +646,9 @@ func (b *boot) setupGateways(ctx context.Context, cfgPath, overlayPath string) b }, ChatPRReviewer: b.prReviewer(), DefaultChatIdentity: b.defaultChatIdentity, SessionStore: b.chatSessionStore, Updates: b.updateService, - Secrets: b.secrets, - Bus: b.bus, - RegisterRestart: func(request func() error) { b.restartTelegram = request }, Log: log, + Secrets: b.secrets, + Bus: b.bus, + Log: log, ChannelManager: b.channelManager, AgentStatus: b.agentStatus, RateLimiter: b.rateLimiter, }) diff --git a/internal/app/archied/telegram_setup.go b/internal/app/archied/telegram_setup.go index 47474d9ad..92523a167 100644 --- a/internal/app/archied/telegram_setup.go +++ b/internal/app/archied/telegram_setup.go @@ -54,7 +54,6 @@ type telegramSetup struct { SessionStore gateway.SessionStore Updates telegram.UpdateService Dangerous gateway.DangerousCommandAuthority - RegisterRestart func(func() error) // Bus carries primary-input events (archie-core-035): a completed // chat turn is published here so input-driven curators can wake. Nil // disables turn events (tests, minimal setups). @@ -118,9 +117,6 @@ func setupTelegramGateway(ctx context.Context, s telegramSetup) (start func(), o s.Log.Error("chat.telegram config invalid", "err", err) return nil, false } - if s.RegisterRestart != nil { - s.RegisterRestart(tg.RequestRestart) - } tg.Version = func() string { return fmt.Sprintf("Archie\nGateway: %s\nRuntime: %s", gatewayVersion, runtimeVersion) } @@ -310,6 +306,9 @@ 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 new file mode 100644 index 000000000..858a25165 --- /dev/null +++ b/internal/app/archied/telegram_setup_restart_test.go @@ -0,0 +1,85 @@ +package archied + +import ( + "log/slog" + "testing" + + "github.com/samcharles93/archie-core/internal/channels/telegram" + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/domain/messaging" + "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") + } + + // 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") + } + + // 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.") + } +}