Files
archie/internal/app/archiemessaging/telegram_features.go
T
sam 6985be0abd fix(messaging): report the daemon version this process was built from
The Telegram update-report relay verified every pending report against a
nil running-version map, so a successful update always ended with "I still
can't confirm which version is actually running" and read as a failure.

The 2026-09-19 extraction decision left RunningVersions nil because the
Messaging Service could obtain only another binary's stamp. 6a3b3509
(archie-core-k94o) then gave every host binary, archie-messaging included,
a buildinfo.Version self-report stamped with the archied release tag, so
the process now has a truthful source for its own running version: report
daemon = buildinfo.Version and leave the agent Unverified, since every
archie-agent process is task-scoped and observed only through
daemon.AgentStatus. A relaying process that the update did not replace
still reports its old build, which Verify reads as drift.
2026-10-02 10:16:22 +10:00

142 lines
5.9 KiB
Go

package archiemessaging
import (
"context"
"crypto/sha256"
"fmt"
"log/slog"
"path/filepath"
"time"
"github.com/samcharles93/archie-core/internal/buildinfo"
"github.com/samcharles93/archie-core/internal/channels/telegram"
"github.com/samcharles93/archie-core/internal/domain/messaging"
"github.com/samcharles93/archie-core/internal/installtype"
"github.com/samcharles93/archie-core/internal/releaseupdate"
)
// configureTelegram wires the operator seams telegram.Gateway leaves to its
// composition root. Which process may satisfy each one is settled in
// docs/architecture/migration-decisions.md ("Telegram operator surface after
// extraction"); notably ReleaseAnnouncements and Dangerous stay nil, because
// nothing this process can observe would make them true. RunningVersions is
// supplied here from this process's own build stamp; see
// messagingRunningVersions.
func configureTelegram(ctx context.Context, g *telegram.Gateway, cfg ResolvedConfig, chat messaging.ChatContract, log *slog.Logger) {
g.Version = gatewayVersionReporter(ctx, chat, cfg.Options.DependencyTimeout)
g.SetShowToolCalls(cfg.ShowToolCalls)
g.Reload = telegramReloader(cfg.Options, log)
g.RunningVersions = messagingRunningVersions
if updates := updateService(cfg); updates != nil {
g.Updates = updates
}
if cfg.WorkDir != "" {
g.UpdateReportPath = identityStatePath(cfg.WorkDir, "update-report", cfg.BotUser)
}
}
// messagingRunningVersions reports the component versions this process can
// vouch for, for checking a relayed update report against
// (releaseupdate.Report.Verify).
//
// archie-messaging is one of the processes in the archied release set (the
// installer's GATEWAY_SERVICES), built from the same archied/v* tag and stamped
// with buildinfo.Version by every build site (install.sh, the updater, the
// release workflow and Taskfile). Its own compiled-in build is therefore
// evidence that the release the installer claims actually reached this host: a
// binary the update did not replace still reports the version it was built
// from, which Verify reads as drift rather than false success. Reading the
// version from the Gateway instead would report a different process's stamp,
// which is the failure mode the check exists to catch (see
// docs/architecture/migration-decisions.md).
//
// The agent is deliberately absent: every archie-agent process is task-scoped
// and observed only through daemon.AgentStatus, which this process does not
// hold, so it must report Unverified rather than be guessed.
func messagingRunningVersions() map[string]string {
return map[string]string{releaseupdate.ComponentDaemon: buildinfo.Version}
}
// gatewayVersionReporter renders /version from the Gateway's own build block.
// It is read per call rather than captured at compose so an upgraded Gateway
// is reported without restarting this process, and bounded by the same
// dependency timeout the readiness probe uses so a hung Gateway answers the
// command instead of leaving the operator with no reply at all. The service
// context bounds it too, so a shutdown does not wait on an in-flight call.
func gatewayVersionReporter(ctx context.Context, chat messaging.ChatContract, timeout time.Duration) func() string {
return func() string {
callCtx := ctx
if timeout > 0 {
var cancel context.CancelFunc
callCtx, cancel = context.WithTimeout(callCtx, timeout)
defer cancel()
}
snapshot, err := chat.Snapshot(callCtx)
if err != nil {
return "Version information is unavailable: the Gateway could not be reached."
}
if snapshot.Version == "" {
return "Version information is unavailable: the Gateway reported no build."
}
return snapshot.Version
}
}
// updateService builds /update from this service's own [chat.telegram]
// commands, or nil when none is configured, which leaves the channel reporting
// the command as unconfigured.
//
// Enrich is deliberately unset: the daemon filled it from [nats], which this
// service must not decode, so the check command's own reported install type
// stands.
func updateService(cfg ResolvedConfig) *releaseupdate.Service {
if len(cfg.Telegram.UpdateCheckCommand) == 0 {
return nil
}
updates := &releaseupdate.Service{
Catalog: releaseupdate.CommandCatalog{Command: cfg.Telegram.UpdateCheckCommand},
StatePath: filepath.Join(cfg.WorkDir, "telegram-update-deferrals.json"),
InstallType: installtype.Type(),
}
if len(cfg.Telegram.UpdateInstallCommand) != 0 {
updates.Installer = releaseupdate.CommandInstaller{
Command: cfg.Telegram.UpdateInstallCommand,
HealthURL: cfg.HealthURL,
}
}
return updates
}
// telegramReloader re-resolves the token and allowlist from this service's own
// configuration on /restart. A reload that cannot produce a token is refused so
// the running bot keeps the credential it is already authenticated with.
func telegramReloader(o Options, log *slog.Logger) func(*telegram.Gateway) error {
return func(g *telegram.Gateway) error {
reloaded, err := Resolve(o, log)
if err != nil {
return fmt.Errorf("reload config: %w", err)
}
if reloaded.TelegramToken == "" {
return fmt.Errorf("reload config: chat.telegram token is empty")
}
g.Token = reloaded.TelegramToken
g.AllowedUserIDs = reloaded.Telegram.AllowedUserIDs
g.SetShowToolCalls(reloaded.ShowToolCalls)
log.Info("telegram config reloaded",
"allowed_user_ids", len(g.AllowedUserIDs),
"show_tool_calls", g.ShowToolCalls())
return nil
}
}
// identityStatePath names a per-identity state file under workDir. The identity
// is hashed so several identities sharing one work directory (see
// docs/architecture/identity.md) never collide. The scheme is the daemon's,
// unchanged: a different path would replay every release announcement the
// operator has already been shown.
func identityStatePath(workDir, kind, identity string) string {
identityHash := sha256.Sum256([]byte(identity))
return filepath.Join(workDir, fmt.Sprintf("%s-%x.json", kind, identityHash[:8]))
}