From a9621b91cdf3ac77aaa56e3eba8fdee75650879e Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 14 Nov 2026 14:19:02 +1100 Subject: [PATCH] fix(archiemessaging): enabling or disabling a channel extension applies without a restart (#1586) --- internal/app/archiemessaging/compose.go | 5 + .../app/archiemessaging/extension_channels.go | 209 ++++++++++++++---- internal/app/archiemessaging/reconcile.go | 4 +- internal/app/archiemessaging/supervise.go | 3 +- 4 files changed, 178 insertions(+), 43 deletions(-) diff --git a/internal/app/archiemessaging/compose.go b/internal/app/archiemessaging/compose.go index c34a1d887..27d4b0208 100644 --- a/internal/app/archiemessaging/compose.go +++ b/internal/app/archiemessaging/compose.go @@ -63,6 +63,8 @@ type channelInstance struct { chat messaging.ChatContract // plan is what an org's channel was built from. plan *orgChannelPlan + // extension is what an instance-level channel extension was built from. + extension *extensionPlan // needs names the required settings an enabled channel lacks; such a // channel is reported stopped with it rather than started to fail. needs string @@ -228,6 +230,9 @@ func (s *Service) Start(ctx context.Context) error { } s.announceReady(ctx, s.awaitedChannels()) + if s.extensions != nil { + go s.extensionChannelLoop(ctx) + } if s.orgs != nil { go s.orgChannelLoop(ctx) } diff --git a/internal/app/archiemessaging/extension_channels.go b/internal/app/archiemessaging/extension_channels.go index 1d8cd2d8b..6197d7155 100644 --- a/internal/app/archiemessaging/extension_channels.go +++ b/internal/app/archiemessaging/extension_channels.go @@ -9,9 +9,11 @@ import ( "slices" "strconv" "strings" + "time" "github.com/samcharles93/archie-core/internal/app/servicekit" "github.com/samcharles93/archie-core/internal/channels" + "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/domain/storepkg" "github.com/samcharles93/archie-core/internal/infrastructure/channelext" "github.com/samcharles93/archie-core/internal/infrastructure/configuration" @@ -68,31 +70,35 @@ func (f *channelFactory) channelPackage(ctx context.Context, name string) (store return storepkg.Installed{}, "", false } -// openExtensionChannels builds a channel for each installed channel package -// whose authority is accepted and that extension-settings enables. The -// host passes the telegram channel the show_tool_calls session default. -// Changing the set takes a restart, as every channel binding does. Running -// extensions stop with ctx. -func openExtensionChannels(ctx context.Context, source extension.Source, secrets *secret.Registry, log *slog.Logger, showToolCalls bool) ([]*channelInstance, *channelFactory) { - cache, err := configuration.DefaultCacheDir() - if err != nil { - log.WarnContext(ctx, "channel extensions unavailable: no cache directory", "err", err) - return nil, nil - } +// extensionPlan is one channel extension as the extension settings and the +// installed packages currently resolve it. A plan with needs set is enabled but +// cannot start. +type extensionPlan struct { + name string + pkg storepkg.Installed + file string + settings map[string]string + needs string +} + +// same reports whether next needs no rebuild of the running channel. +func (p extensionPlan) same(next extensionPlan) bool { + return p.needs == next.needs && p.pkg.Digest == next.pkg.Digest && maps.Equal(p.settings, next.settings) +} + +// planExtensionChannels resolves each enabled channel package to a plan, keyed +// by name. A package that is not installed, not accepted, or not enabled has no +// plan, which is what a disabled channel is. +func planExtensionChannels(ctx context.Context, log *slog.Logger, source extension.Source) (map[string]extensionPlan, error) { settings, _, err := source.Enabled(ctx) if err != nil { - log.WarnContext(ctx, "channel extensions unavailable: extension settings", "err", err) - return nil, nil + return nil, err } installed, err := source.Packages.ListInstalled(ctx) if err != nil { - log.WarnContext(ctx, "channel extensions unavailable: installed packages", "err", err) - return nil, nil + return nil, fmt.Errorf("list installed packages: %w", err) } - host := extension.NewHost(log) - factory := &channelFactory{host: host, source: source, cacheDir: filepath.Join(cache, "extensions", "channels"), secrets: secrets} - var instances []*channelInstance - built := map[string]bool{} + plans := map[string]extensionPlan{} for _, pkg := range installed { setting, ok := settings[pkg.Name] if !ok || !setting.Enabled || pkg.AcceptedAuthority == nil { @@ -102,35 +108,66 @@ func openExtensionChannels(ctx context.Context, source extension.Source, secrets if index < 0 { continue } + plan := extensionPlan{name: pkg.Name, pkg: pkg, file: pkg.Descriptor.Contributes.Extensions[index].Path, settings: setting.Settings} if missing := pkg.Descriptor.MissingSettings(setting.Settings); len(missing) > 0 { - needs := "Missing " + strings.Join(missing, ", ") + plan.needs = "Missing " + strings.Join(missing, ", ") log.WarnContext(ctx, "channel extension not started: required settings missing", "channel", pkg.Name, "missing", missing) - instances = append(instances, &channelInstance{name: pkg.Name, needs: needs}) - built[pkg.Name] = true - continue } - values := setting.Settings - if pkg.Name == telegramChannel { - values = maps.Clone(values) - if values == nil { - values = map[string]string{} - } - values["show_tool_calls"] = strconv.FormatBool(showToolCalls) - } - ch, err := factory.build(ctx, pkg, pkg.Descriptor.Contributes.Extensions[index].Path, pkg.Name, values) - if err != nil { - log.WarnContext(ctx, "channel extension not started", "channel", pkg.Name, "err", err) - continue - } - built[pkg.Name] = true - instances = append(instances, &channelInstance{name: pkg.Name, channel: ch}) + plans[pkg.Name] = plan } if setting := settings[telegramChannel]; setting.Enabled && !slices.ContainsFunc(installed, func(pkg storepkg.Installed) bool { return pkg.Name == telegramChannel }) { // Report it as a channel, so the dashboard shows why the enabled // bot is not running instead of showing nothing. - const needs = "The Telegram channel extension is not installed" log.WarnContext(ctx, "telegram is enabled but its channel extension is not installed") - instances = append(instances, &channelInstance{name: telegramChannel, needs: needs}) + plans[telegramChannel] = extensionPlan{name: telegramChannel, needs: "The Telegram channel extension is not installed"} + } + return plans, nil +} + +// buildPlan launches the channel a plan describes; a plan that needs something +// has no channel. The host passes the telegram channel the show_tool_calls +// session default. +func (f *channelFactory) buildPlan(ctx context.Context, plan extensionPlan, showToolCalls bool) (channels.Channel, error) { + if plan.needs != "" { + return nil, nil + } + values := plan.settings + if plan.name == telegramChannel { + values = maps.Clone(values) + if values == nil { + values = map[string]string{} + } + values["show_tool_calls"] = strconv.FormatBool(showToolCalls) + } + return f.build(ctx, plan.pkg, plan.file, plan.name, values) +} + +// openExtensionChannels builds a channel for each installed channel package +// whose authority is accepted and that extension-settings enables. Later +// changes to the set are applied by syncExtensionChannels. Running extensions +// stop with ctx. +func openExtensionChannels(ctx context.Context, source extension.Source, secrets *secret.Registry, log *slog.Logger, showToolCalls bool) ([]*channelInstance, *channelFactory) { + cache, err := configuration.DefaultCacheDir() + if err != nil { + log.WarnContext(ctx, "channel extensions unavailable: no cache directory", "err", err) + return nil, nil + } + plans, err := planExtensionChannels(ctx, log, source) + if err != nil { + log.WarnContext(ctx, "channel extensions unavailable", "err", err) + return nil, nil + } + host := extension.NewHost(log) + factory := &channelFactory{host: host, source: source, cacheDir: filepath.Join(cache, "extensions", "channels"), secrets: secrets} + var instances []*channelInstance + for _, name := range slices.Sorted(maps.Keys(plans)) { + plan := plans[name] + ch, err := factory.buildPlan(ctx, plan, showToolCalls) + if err != nil { + log.WarnContext(ctx, "channel extension not started", "channel", name, "err", err) + continue + } + instances = append(instances, &channelInstance{name: name, channel: ch, needs: plan.needs, extension: &plan}) } go func() { <-ctx.Done() @@ -139,6 +176,98 @@ func openExtensionChannels(ctx context.Context, source extension.Source, secrets return instances, factory } +// extensionChannelLoop applies changes to the extension settings and installed +// packages to the running channels until ctx ends. +func (s *Service) extensionChannelLoop(ctx context.Context) { + ticker := time.NewTicker(s.reconcileInterval) + defer ticker.Stop() + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + if err := s.syncExtensionChannels(ctx); err != nil { + s.log.WarnContext(ctx, "channel extensions not synced", "err", err) + } + } + } +} + +// syncExtensionChannels starts, replaces or stops each instance-level channel +// extension to match its plan. A channel that no longer has a plan is stopped +// but keeps its supervisor, so enabling it again starts it without a restart. +func (s *Service) syncExtensionChannels(ctx context.Context) error { + plans, err := planExtensionChannels(ctx, s.log, s.extensions.source) + if err != nil { + return err + } + show := s.currentConfig().ShowToolCalls + for _, name := range slices.Sorted(maps.Keys(plans)) { + s.applyExtensionPlan(ctx, plans[name], show) + } + for _, instance := range s.instances() { + if _, still := plans[instance.name]; still || instance.owner != storecontract.DefaultOrgID || instance.currentExtension() == nil { + continue + } + instance.setExtension(nil, "") + if err := s.setChannelState(instance, nil); err != nil { + s.log.WarnContext(ctx, "channel extension not stopped", "channel", instance.name, "err", err) + continue + } + s.status.Declare(instance.name, false, false) + } + s.signalPublish() + return nil +} + +func (s *Service) applyExtensionPlan(ctx context.Context, plan extensionPlan, show bool) { + instance := s.systemInstance(plan.name) + if instance != nil { + if previous := instance.currentExtension(); previous != nil && previous.same(plan) { + return + } + } + ch, err := s.extensions.buildPlan(ctx, plan, show) + if err != nil { + s.log.WarnContext(ctx, "channel extension not started", "channel", plan.name, "err", err) + return + } + if instance == nil { + instance = &channelInstance{name: plan.name, kind: plan.name, owner: storecontract.DefaultOrgID, channel: ch, needs: plan.needs, extension: &plan, notify: make(chan struct{}, 1)} + s.addChannel(ctx, instance) + return + } + instance.setExtension(&plan, plan.needs) + if err := s.setChannelState(instance, ch); err != nil { + s.log.WarnContext(ctx, "channel extension not restarted", "channel", plan.name, "err", err) + return + } + s.status.Declare(instance.name, ch != nil, channelReloadable(ch)) +} + +// systemInstance finds the instance-level channel of that name; an org's +// channel of the same kind is a different instance. +func (s *Service) systemInstance(name string) *channelInstance { + for _, instance := range s.instances() { + if instance.name == name && instance.owner == storecontract.DefaultOrgID { + return instance + } + } + return nil +} + +func (c *channelInstance) currentExtension() *extensionPlan { + c.mu.Lock() + defer c.mu.Unlock() + return c.extension +} + +func (c *channelInstance) setExtension(plan *extensionPlan, needs string) { + c.mu.Lock() + defer c.mu.Unlock() + c.extension, c.needs = plan, needs +} + func buildExtensionChannel(ctx context.Context, host *extension.Host, source extension.Source, cacheDir string, pkg storepkg.Installed, file, name string, settings map[string]string, secrets *secret.Registry) (channels.Channel, error) { binary, sum, err := extension.Materialize(ctx, source.Packages, cacheDir, pkg, file) if err != nil { diff --git a/internal/app/archiemessaging/reconcile.go b/internal/app/archiemessaging/reconcile.go index 61de9e42d..61aa5cbf9 100644 --- a/internal/app/archiemessaging/reconcile.go +++ b/internal/app/archiemessaging/reconcile.go @@ -33,8 +33,8 @@ func (s *Service) reconcileLoop(ctx context.Context) { } // reconcileOnce applies changed channel settings and records the outcome in -// apply status. Channels are composed at startup -- extension channels included -// -- so the live path today carries chat-level settings (show_tool_calls) to +// apply status. Enabling, disabling and rebuilding channels is the extension +// sync loop's job; this path carries chat-level settings (show_tool_calls) to // the running channels that accept them. func (s *Service) reconcileOnce(ctx context.Context) error { base, err := Resolve(s.currentConfig().Options, s.log) diff --git a/internal/app/archiemessaging/supervise.go b/internal/app/archiemessaging/supervise.go index f0210a42f..84082ee5f 100644 --- a/internal/app/archiemessaging/supervise.go +++ b/internal/app/archiemessaging/supervise.go @@ -24,8 +24,9 @@ func (s *Service) runChannel(ctx context.Context, c *channelInstance) { ch := c.channel if ch == nil { // Disabled: there is no channel to run until an enable builds one. + needs := c.needs c.mu.Unlock() - s.status.MarkStopped(c.name, c.needs) + s.status.MarkStopped(c.name, needs) if s.awaitChannel(ctx, c, 0) == channelShutdown { return }