From 43d349af386836c255df234d9910fdb5272a5e32 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 23 Aug 2026 04:29:18 +1000 Subject: [PATCH] fix(update): wire Telegram personas and survive callback cancellation --- cmd/archied/telegram_setup.go | 1 + cmd/archied/telegram_setup_regression_test.go | 27 ++++++++ internal/channels/telegram/update.go | 9 ++- .../telegram/update_regression_test.go | 62 +++++++++++++++++++ scripts/archie-update-install | 6 ++ 5 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 cmd/archied/telegram_setup_regression_test.go create mode 100644 internal/channels/telegram/update_regression_test.go diff --git a/cmd/archied/telegram_setup.go b/cmd/archied/telegram_setup.go index df9c2c61a..08a2a6294 100644 --- a/cmd/archied/telegram_setup.go +++ b/cmd/archied/telegram_setup.go @@ -202,6 +202,7 @@ func buildTelegramRouter(ctx context.Context, tg *telegram.Gateway, s telegramSe router := gateway.NewRouter(s.St, nil, "telegram") router.Models = s.ChatModels router.Updates = s.Updates + router.Personas = s.Personas router.InitSessions(sessionStore) router.Titles = newChatTitleGenerator(s) router.Log = s.Log diff --git a/cmd/archied/telegram_setup_regression_test.go b/cmd/archied/telegram_setup_regression_test.go new file mode 100644 index 000000000..2746ad373 --- /dev/null +++ b/cmd/archied/telegram_setup_regression_test.go @@ -0,0 +1,27 @@ +package main + +import ( + "context" + "log/slog" + "testing" + + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/gateway" +) + +func TestBuildTelegramRouterWiresPersonas(t *testing.T) { + personas := gateway.NewPersonaRegistry(gateway.DefaultPersonas()) + sessions := gateway.NewSessionStoreMemory() + t.Cleanup(func() { _ = sessions.Close() }) + + router := buildTelegramRouter(context.Background(), nil, telegramSetup{ + Cfg: config.NewHolder(config.Config{}), + Personas: personas, + SessionStore: sessions, + Log: slog.Default(), + }, sessions) + + if router.Personas != personas { + t.Fatalf("router.Personas = %p, want the configured registry %p", router.Personas, personas) + } +} diff --git a/internal/channels/telegram/update.go b/internal/channels/telegram/update.go index f8a9776be..ec05a6829 100644 --- a/internal/channels/telegram/update.go +++ b/internal/channels/telegram/update.go @@ -147,6 +147,12 @@ func (g *Gateway) dispatchUpdateAction(ctx context.Context, b *bot.Bot, query *m } g.answerModelCallback(ctx, b, query.ID, "Starting update…", false) g.editUpdateMessage(ctx, b, query, "Update approved. Starting now…") + // The callback context belongs to Telegram's short-lived update + // handler and is cancelled as soon as this handler returns. The + // installer may build an image for several minutes, so detach only + // the install operation from that request lifetime. Progress and + // final-message edits still use ctx and may harmlessly fail when the + // callback request is gone. go g.installUpdate(ctx, b, query) default: g.answerModelCallback(ctx, b, query.ID, "That update action is no longer valid.", true) @@ -196,7 +202,8 @@ func (g *Gateway) installUpdate(ctx context.Context, b *bot.Bot, query *models.C } } - result, err := g.Updates.Install(ctx, meta, progress) + installCtx := context.WithoutCancel(ctx) + result, err := g.Updates.Install(installCtx, meta, progress) if err != nil { g.log.Error("approved update failed", "error", err) lines = append(lines, formatInstallFailure(err)) diff --git a/internal/channels/telegram/update_regression_test.go b/internal/channels/telegram/update_regression_test.go new file mode 100644 index 000000000..96efc41ab --- /dev/null +++ b/internal/channels/telegram/update_regression_test.go @@ -0,0 +1,62 @@ +package telegram + +import ( + "context" + "log/slog" + "testing" + "time" + + "github.com/go-telegram/bot/models" + + "github.com/samcharles93/archie-core/internal/releaseupdate" +) + +type contextAwareUpdateStub struct { + started chan struct{} + seen context.Context +} + +func (s *contextAwareUpdateStub) Check(context.Context, int64) (releaseupdate.Snapshot, error) { + return releaseupdate.Snapshot{}, nil +} + +func (s *contextAwareUpdateStub) Defer(context.Context, int64, releaseupdate.Snapshot) error { + return nil +} +func (s *contextAwareUpdateStub) CanInstall() bool { return true } +func (s *contextAwareUpdateStub) Install(ctx context.Context, _ releaseupdate.InstallMeta, _ func(string)) (releaseupdate.Result, error) { + s.seen = ctx + close(s.started) + return releaseupdate.Result{}, nil +} + +func TestInstallUpdateDetachesInstallerFromCallbackContext(t *testing.T) { + g := New("1:test", "", "", []int64{42}, slog.Default()) + stub := &contextAwareUpdateStub{started: make(chan struct{})} + g.Updates = stub + b, _ := newTelegramTestBot(t) + callbackCtx, cancel := context.WithCancel(context.Background()) + message := &models.Message{Chat: models.Chat{ID: 7}, ID: 100} + + done := make(chan struct{}) + go func() { + g.installUpdate(callbackCtx, b, &models.CallbackQuery{ + From: models.User{ID: 42}, + Message: models.MaybeInaccessibleMessage{Message: message}, + }) + close(done) + }() + <-stub.started + cancel() + + select { + case <-stub.seen.Done(): + t.Fatal("installer context was cancelled with the callback context") + case <-time.After(20 * time.Millisecond): + } + select { + case <-done: + case <-time.After(time.Second): + t.Fatal("installUpdate did not finish") + } +} diff --git a/scripts/archie-update-install b/scripts/archie-update-install index f6fa868cf..adf739952 100755 --- a/scripts/archie-update-install +++ b/scripts/archie-update-install @@ -35,6 +35,12 @@ PREV_RT="$(git describe --tags --match 'archie/v*' 2>/dev/null | sed 's|^archie echo "==> fetching" git pull --ff-only +# Refresh the installed adapters from the checkout just fetched. The running +# shell continues with the old script, but the next /update uses the current +# implementation even when this invocation was started by an older install. +install -m755 "$REPO/scripts/archie-update-install" "$BIN_DIR/archie-update-install" +install -m755 "$REPO/scripts/archie-update-watchdog" "$BIN_DIR/archie-update-watchdog" + GW="$(git describe --tags --match 'archied/v*' | sed 's|^archied/v||')" RT="$(git describe --tags --match 'archie/v*' | sed 's|^archie/v||')" echo "==> building archied $GW / archie-agent $RT"