From b703c3663bb10a5688deeed27957befc3899522f Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 22 Sep 2026 08:15:32 +1000 Subject: [PATCH] feat(messaging): route a channel reload, and refuse one that cannot reload archie-core-np7l's service half -- the reload action and its truthful refusal. The request surface that carries the instruction from the dashboard is its own change, because it is its own object with its own lifetime (the reasoning is recorded in docs/architecture/migration-decisions.md). Service.ReloadChannel routes to the channel's own seam rather than reimplementing it: the telegram front-end re-reads its token and allowlist, which is what its own /restart path calls. A channel that cannot reload is refused with a reason instead of accepted and ignored, because the dashboard offers the action on the strength of the capability the descriptor declares, and an accepted-then-nothing reload is a lie the operator cannot see through. The capability and the action now come from one source. Before this a descriptor declared ReloadSupported by NAME, so a channel could announce reload support and have nothing behind it; reloadable() decides both, and the descriptor test builds a real gateway with a seam rather than a fake named "telegram". Verified: task check exit 0. Coverage: the refusal names the reason for a channel that declares no support and for an unknown channel; the service reaches telegram's own reload seam and the re-read config lands; and the declaration and the action cannot drift, which is asserted by reloading every composed channel and requiring the refusal specifically to be "does not support reload" -- a declared reload may still fail on the configuration it re-reads, and that is a different fact. --- .../archiemessaging/channel_reload_test.go | 109 ++++++++++++++++++ .../app/archiemessaging/channel_status.go | 45 +++++++- .../archiemessaging/channel_status_test.go | 8 +- 3 files changed, 156 insertions(+), 6 deletions(-) create mode 100644 internal/app/archiemessaging/channel_reload_test.go diff --git a/internal/app/archiemessaging/channel_reload_test.go b/internal/app/archiemessaging/channel_reload_test.go new file mode 100644 index 000000000..cabfa56e1 --- /dev/null +++ b/internal/app/archiemessaging/channel_reload_test.go @@ -0,0 +1,109 @@ +package archiemessaging + +import ( + "log/slog" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/domain/messaging" +) + +// TestReloadChannelRefusesWhatCannotReload: the dashboard offers the action on the +// strength of the capability its descriptor declares, so a channel that cannot +// reload must be refused with a reason. Accepting and doing nothing is the failure +// an operator cannot see through. +func TestReloadChannelRefusesWhatCannotReload(t *testing.T) { + srv, err := compose(t.Context(), deps{ + Config: ResolvedConfig{ + WebhookAddr: "127.0.0.1:0", + Webhook: config.WebhookRoute{Path: "/smoke"}, + }, + Log: slog.New(slog.DiscardHandler), + Chat: &recordingChatContract{routes: make(chan messaging.Inbound, 1), reply: "ok"}, + }) + if err != nil { + t.Fatalf("compose: %v", err) + } + + err = srv.ReloadChannel(t.Context(), "webhook") + if err == nil { + t.Fatal("ReloadChannel(webhook) = nil, want a refusal: webhook declares no reload support") + } + if !strings.Contains(err.Error(), "does not support reload") { + t.Errorf("error = %v, want it to say the channel does not support reload", err) + } + + if err := srv.ReloadChannel(t.Context(), "nope"); err == nil || !strings.Contains(err.Error(), "no channel") { + t.Errorf("ReloadChannel(unknown) = %v, want it to name the missing channel", err) + } +} + +// TestReloadChannelReachesTheChannelsOwnSeam: the service routes a reload to the +// channel's own implementation rather than reimplementing it, so the operator's +// action and the front-end's own /restart path do the same thing. +func TestReloadChannelReachesTheChannelsOwnSeam(t *testing.T) { + dir := t.TempDir() + cfgPath := filepath.Join(dir, "config.toml") + write := func(userIDs string) { + content := "bot_user = \"testbot\"\n\n[chat.telegram]\ntoken_env = \"TEST_TG_TOKEN\"\nallowed_user_ids = " + userIDs + "\n" + if err := os.WriteFile(cfgPath, []byte(content), 0o600); err != nil { + t.Fatal(err) + } + } + write("[1]") + t.Setenv("TEST_TG_TOKEN", "first-token") + + resolved, err := Resolve(Options{Config: cfgPath}, slog.New(slog.DiscardHandler)) + if err != nil { + t.Fatalf("Resolve: %v", err) + } + srv, err := compose(t.Context(), deps{Config: resolved, Log: slog.New(slog.DiscardHandler), Chat: &recordingChatContract{}}) + if err != nil { + t.Fatalf("compose: %v", err) + } + gateway := telegramChannel(t, srv) + + write("[1, 2]") + t.Setenv("TEST_TG_TOKEN", "second-token") + if err := srv.ReloadChannel(t.Context(), "telegram"); err != nil { + t.Fatalf("ReloadChannel(telegram): %v", err) + } + if len(gateway.AllowedUserIDs) != 2 || gateway.Token != "second-token" { + t.Fatalf("after reload: allowlist %v, token %q; want the re-read config", gateway.AllowedUserIDs, gateway.Token) + } +} + +// TestReloadCapabilityAndActionShareOneSource pins the property that keeps the +// dashboard honest: whatever a descriptor declares reloadable must be reloadable, +// and vice versa, because both come from reloadable(). +func TestReloadCapabilityAndActionShareOneSource(t *testing.T) { + srv, err := compose(t.Context(), deps{ + Config: ResolvedConfig{ + TelegramToken: "token-123", + Telegram: config.TelegramConfig{TokenEnv: "TEST_TG_TOKEN", AllowedUserIDs: []int64{1}}, + WebhookAddr: "127.0.0.1:0", + Webhook: config.WebhookRoute{Path: "/smoke"}, + }, + Log: slog.New(slog.DiscardHandler), + Chat: &recordingChatContract{routes: make(chan messaging.Inbound, 1), reply: "ok"}, + }) + if err != nil { + t.Fatalf("compose: %v", err) + } + for _, report := range srv.ChannelStatus() { + err := srv.ReloadChannel(t.Context(), report.ID) + // A declared reload can still fail on the configuration it re-reads -- + // telegram refuses a config that lost its token, and the existing test for + // that pins it. What the capability claims is that the ACTION exists and is + // routed, so the property tested here is that the refusal is specifically + // "does not support reload". + unsupported := err != nil && strings.Contains(err.Error(), "does not support reload") + if report.ReloadSupported == unsupported { + t.Errorf("%s: ReloadSupported=%v but reload returned %v; the declaration and the action have drifted", + report.ID, report.ReloadSupported, err) + } + } +} diff --git a/internal/app/archiemessaging/channel_status.go b/internal/app/archiemessaging/channel_status.go index a2a562934..d9ab65e1c 100644 --- a/internal/app/archiemessaging/channel_status.go +++ b/internal/app/archiemessaging/channel_status.go @@ -2,10 +2,12 @@ package archiemessaging import ( "context" + "fmt" "time" "github.com/samcharles93/archie-core/internal/channels" "github.com/samcharles93/archie-core/internal/channels/status" + "github.com/samcharles93/archie-core/internal/channels/telegram" "github.com/samcharles93/archie-core/internal/domain/storecontract" ) @@ -20,16 +22,49 @@ func channelDescriptors(instances []channelInstance) []status.Descriptor { ID: instance.name, Name: instance.name, Configured: true, - // Telegram is the only front-end that carries a reload seam today - // (archiemessaging.telegramFeatures rewires it on request). Declaring - // the others would invite the dashboard to offer a button that - // answers "reloaded" and changed nothing. - ReloadSupported: instance.name == "telegram", + // The capability comes from the same place the action does, so a + // channel cannot declare reload support and then answer nothing. + ReloadSupported: reloadable(instance), }) } return descriptors } +// reloadable reports whether this channel can re-read its configuration. It is +// the single source for both the capability a descriptor declares and the action +// a reload performs; deriving them separately is how a dashboard ends up offering +// a button that reports success and changes nothing (archie-core-np7l). +func reloadable(instance channelInstance) bool { + gateway, ok := instance.channel.(*telegram.Gateway) + return ok && gateway.Reload != nil +} + +// ReloadChannel asks one channel to re-read its configuration. A channel that +// does not support reload is refused with a reason rather than accepted and +// ignored: the dashboard offers the action on the strength of the capability its +// descriptor declares, so an accepted-then-nothing reload is a lie the operator +// has no way to see through. +// +// The reload itself is the channel's own seam -- the telegram front-end re-reads +// its token and allowlist -- so this function adds routing and a truthful +// refusal, not a second implementation. +func (s *Service) ReloadChannel(_ context.Context, id string) error { + for _, instance := range s.channels { + if instance.name != id { + continue + } + gateway, ok := instance.channel.(*telegram.Gateway) + if !ok || gateway.Reload == nil { + return fmt.Errorf("messaging: channel %q does not support reload", id) + } + if err := gateway.Reload(gateway); err != nil { + return fmt.Errorf("messaging: reload %q: %w", id, err) + } + return nil + } + return fmt.Errorf("messaging: no channel %q", id) +} + // lifecycleFor returns the report a channel writes its own state through. Each // channel gets its own closure, so one channel's failure marks only itself -- // which is the reason the manager is keyed by channel rather than being one flag diff --git a/internal/app/archiemessaging/channel_status_test.go b/internal/app/archiemessaging/channel_status_test.go index cdf67cf48..4bb58ab89 100644 --- a/internal/app/archiemessaging/channel_status_test.go +++ b/internal/app/archiemessaging/channel_status_test.go @@ -11,6 +11,7 @@ import ( "github.com/samcharles93/archie-core/internal/channels" "github.com/samcharles93/archie-core/internal/channels/status" + "github.com/samcharles93/archie-core/internal/channels/telegram" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/messaging" ) @@ -138,8 +139,13 @@ func TestServiceRecordsAChannelThatCannotStart(t *testing.T) { // acts on. Reload is the one with a real consequence: a channel declaring it // invites a button that must do something. func TestChannelDescriptorsDeclareCapabilities(t *testing.T) { + // Telegram is a real gateway with a reload seam: the capability now derives + // from the channel and its seam, not from its name, so a fakeChannel named + // "telegram" correctly declares nothing. + withReload := telegram.New("token-123", []int64{1}, slog.New(slog.DiscardHandler)) + withReload.Reload = func(*telegram.Gateway) error { return nil } descriptors := channelDescriptors([]channelInstance{ - {name: "telegram", channel: fakeChannel{}}, + {name: "telegram", channel: withReload}, {name: "email", channel: fakeChannel{}}, {name: "webhook", channel: fakeChannel{}}, })