Fully autonomous AI Agents system capable of performing complex penetration testing tasks
Go ai-agentsai-security-toolanthropicautonomous-agentsgolanggptgraphqlmulti-agent-systemoffensive-securityopen-sourceopenaipenetration-testingpenetration-testing-toolsreactsecurity-automationsecurity-testingsecurity-toolsself-hosted
feat(installer): new checking updates protocol and bump cloud sdk version to v1.0.0
Dmitry Ng2d39d1f74dc9
diff --git a/backend/cmd/installer/checker/checker.go b/backend/cmd/installer/checker/checker.goindex 6d1da441..cacc7fb0 100644--- a/backend/cmd/installer/checker/checker.go+++ b/backend/cmd/installer/checker/checker.go@@ -20,37 +20,50 @@ var ()const (- DockerComposeFile = "docker-compose.yml"- GraphitiComposeFile = "docker-compose-graphiti.yml"- LangfuseComposeFile = "docker-compose-langfuse.yml"- ObservabilityComposeFile = "docker-compose-observability.yml"- ExampleCustomConfigLLMFile = "example.custom.provider.yml"- ExampleOllamaConfigLLMFile = "example.ollama.provider.yml"- PentagiScriptFile = "/usr/local/bin/pentagi"- PentagiContainerName = "pentagi"- GraphitiContainerName = "graphiti"- Neo4jContainerName = "neo4j"- LangfuseWorkerContainerName = "langfuse-worker"- LangfuseWebContainerName = "langfuse-web"- GrafanaContainerName = "grafana"- OpenTelemetryContainerName = "otel"- DefaultImage = "debian:latest"- DefaultImageForPentest = "vxcontrol/kali-linux"- DefaultGraphitiEndpoint = "http://graphiti:8000"- DefaultLangfuseEndpoint = "http://langfuse-web:3000"- DefaultObservabilityEndpoint = "otelcol:8148"- DefaultLangfuseOtelEndpoint = "http://otelcol:4318"- DefaultUpdateServerEndpoint = "https://update.pentagi.com"- UpdatesCheckEndpoint = "/api/v1/updates/check"- MinFreeMemGB = 0.5- MinFreeMemGBForPentagi = 0.5- MinFreeMemGBForGraphiti = 2.0- MinFreeMemGBForLangfuse = 1.5- MinFreeMemGBForObservability = 1.5- MinFreeDiskGB = 5.0- MinFreeDiskGBForComponents = 10.0- MinFreeDiskGBPerComponents = 2.0- MinFreeDiskGBForWorkerImages = 25.0+ DockerComposeFile = "docker-compose.yml"+ GraphitiComposeFile = "docker-compose-graphiti.yml"+ LangfuseComposeFile = "docker-compose-langfuse.yml"+ ObservabilityComposeFile = "docker-compose-observability.yml"+ ExampleCustomConfigLLMFile = "example.custom.provider.yml"+ ExampleOllamaConfigLLMFile = "example.ollama.provider.yml"+ PentagiScriptFile = "/usr/local/bin/pentagi"+ PentagiContainerName = "pentagi"+ ScraperContainerName = "scraper"+ PgvectorContainerName = "pgvector"+ PgexporterContainerName = "pgexporter"+ GraphitiContainerName = "graphiti"+ Neo4jContainerName = "neo4j"+ LangfuseWorkerContainerName = "langfuse-worker"+ LangfuseWebContainerName = "langfuse-web"+ LangfusePostgresContainerName = "langfuse-postgres"+ LangfuseClickhouseContainerName = "langfuse-clickhouse"+ LangfuseRedisContainerName = "langfuse-redis"+ LangfuseMinioContainerName = "langfuse-minio"+ GrafanaContainerName = "grafana"+ OpenTelemetryContainerName = "otel"+ VictoriaMetricsContainerName = "victoriametrics"+ LokiContainerName = "loki"+ JaegerContainerName = "jaeger"+ NodeExporterContainerName = "node-exporter"+ CadvisorContainerName = "cadvisor"+ ClickstoreContainerName = "clickstore"+ DefaultImage = "debian:latest"+ DefaultImageForPentest = "vxcontrol/kali-linux"+ DefaultGraphitiEndpoint = "http://graphiti:8000"+ DefaultLangfuseEndpoint = "http://langfuse-web:3000"+ DefaultObservabilityEndpoint = "otelcol:8148"+ DefaultLangfuseOtelEndpoint = "http://otelcol:4318"+ DefaultUpdateServerEndpoint = "https://update.pentagi.com"+ DefaultSupportServerEndpoint = "https://support.pentagi.com"+ MinFreeMemGB = 0.5+ MinFreeMemGBForPentagi = 0.5+ MinFreeMemGBForGraphiti = 2.0+ MinFreeMemGBForLangfuse = 1.5+ MinFreeMemGBForObservability = 1.5+ MinFreeDiskGB = 5.0+ MinFreeDiskGBForComponents = 10.0+ MinFreeDiskGBPerComponents = 2.0+ MinFreeDiskGBForWorkerImages = 25.0)var (@@ -113,6 +126,19 @@ type CheckResult struct {DockerErrorType DockerErrorType `json:"docker_error_type" yaml:"docker_error_type"`EnvDirWritable bool `json:"env_dir_writable" yaml:"env_dir_writable"`+ // StackUpdates is the update server's answer in full: per stack, what version is+ // offered, what the release says, and which artefacts would actually change. The+ // per-stack booleans above are the same verdict reduced to what the menus need.+ StackUpdates []StackUpdate `json:"stack_updates,omitempty" yaml:"stack_updates,omitempty"`+ // UpdateFailure explains why there is no answer, and is nil when there is one. It+ // replaces guessing from UpdateServerAccessible: "unreachable", "quota spent" and+ // "not allowed for this license" need different things from the user.+ UpdateFailure *UpdateCheckFailure `json:"update_failure,omitempty" yaml:"update_failure,omitempty"`+ // InstalledComponents is what the last check observed on this machine. Verifying an+ // update afterwards needs it, and keeping it here means the verification reads facts+ // the check already gathered instead of interrogating Docker a second time.+ InstalledComponents []InstalledComponent `json:"installed_components,omitempty" yaml:"installed_components,omitempty"`+// handler controls how information is gathered. If nil, skip gatheringhandler CheckHandler}@@ -423,7 +449,14 @@ func (h *defaultCheckHandler) GatherGraphitiInfo(ctx context.Context, c *CheckRegraphitiURL := getEnvVar(h.appState, "GRAPHITI_URL", "")c.GraphitiConnected = graphitiEnabled == "true" && graphitiURL != ""- c.GraphitiExternal = graphitiURL != DefaultGraphitiEndpoint+ // Gated on Connected, and not for tidiness. "External" means somebody else+ // operates this stack, which is only a statement about a stack that exists:+ // an unset URL is not the default URL, so the bare comparison reports a stack+ // nobody has configured as externally hosted. Every consumer of this field+ // already begins with `Connected &&`, so the guard changes no decision — what+ // it changes is what the field SAYS, in the TUI and on the wire, where a+ // released installer has been claiming a Graphiti that does not exist.+ c.GraphitiExternal = c.GraphitiConnected && graphitiURL != DefaultGraphitiEndpointenvDir := filepath.Dir(h.appState.GetEnvPath())graphitiComposeFile := filepath.Join(envDir, GraphitiComposeFile)@@ -454,7 +487,9 @@ func (h *defaultCheckHandler) GatherLangfuseInfo(ctx context.Context, c *CheckResecretKey := getEnvVar(h.appState, "LANGFUSE_SECRET_KEY", "")c.LangfuseConnected = baseURL != "" && projectID != "" && publicKey != "" && secretKey != ""- c.LangfuseExternal = baseURL != DefaultLangfuseEndpoint+ // See the note on GraphitiExternal: without the guard an unconfigured stack+ // reports itself as externally hosted.+ c.LangfuseExternal = c.LangfuseConnected && baseURL != DefaultLangfuseEndpointenvDir := filepath.Dir(h.appState.GetEnvPath())langfuseFile := filepath.Join(envDir, LangfuseComposeFile)@@ -481,7 +516,9 @@ func (h *defaultCheckHandler) GatherObservabilityInfo(ctx context.Context, c *ChotelHost := getEnvVar(h.appState, "OTEL_HOST", "")c.ObservabilityConnected = otelHost != ""- c.ObservabilityExternal = otelHost != DefaultObservabilityEndpoint+ // See the note on GraphitiExternal: without the guard an unconfigured stack+ // reports itself as externally hosted.+ c.ObservabilityExternal = c.ObservabilityConnected && otelHost != DefaultObservabilityEndpointenvDir := filepath.Dir(h.appState.GetEnvPath())obsFile := filepath.Join(envDir, ObservabilityComposeFile)@@ -550,109 +587,6 @@ func (h *defaultCheckHandler) GatherSystemInfo(ctx context.Context, c *CheckResureturn nil}-func (h *defaultCheckHandler) GatherUpdatesInfo(ctx context.Context, c *CheckResult) error {- h.mx.Lock()- defer h.mx.Unlock()-- proxyURL := getProxyURL(h.appState)- updateServerURL := getEnvVar(h.appState, "UPDATE_SERVER_URL", DefaultUpdateServerEndpoint)-- request := CheckUpdatesRequest{- InstallerOsType: runtime.GOOS,- InstallerVersion: InstallerVersion,- GraphitiConnected: c.GraphitiConnected,- GraphitiExternal: c.GraphitiExternal,- GraphitiInstalled: c.GraphitiInstalled,- LangfuseConnected: c.LangfuseConnected,- LangfuseExternal: c.LangfuseExternal,- LangfuseInstalled: c.LangfuseInstalled,- ObservabilityConnected: c.ObservabilityConnected,- ObservabilityExternal: c.ObservabilityExternal,- ObservabilityInstalled: c.ObservabilityInstalled,- }-- // get PentAGI container image info- if h.dockerClient != nil && c.PentagiInstalled {- if imageInfo := getContainerImageInfo(ctx, h.dockerClient, PentagiContainerName); imageInfo != nil {- request.PentagiImageName = &imageInfo.Name- request.PentagiImageTag = &imageInfo.Tag- request.PentagiImageHash = &imageInfo.Hash- }- }-- // get Worker image info from environment- if h.workerClient != nil {- defaultImage := getEnvVar(h.appState, "DOCKER_DEFAULT_IMAGE_FOR_PENTEST", DefaultImageForPentest)- if imageInfo := getImageInfo(ctx, h.workerClient, defaultImage); imageInfo != nil {- request.WorkerImageName = &imageInfo.Name- request.WorkerImageTag = &imageInfo.Tag- request.WorkerImageHash = &imageInfo.Hash- }- }-- // get Graphiti image info if installed locally- if h.dockerClient != nil && c.GraphitiConnected && !c.GraphitiExternal && c.GraphitiInstalled {- if graphitiInfo := getContainerImageInfo(ctx, h.dockerClient, GraphitiContainerName); graphitiInfo != nil {- request.GraphitiImageName = &graphitiInfo.Name- request.GraphitiImageTag = &graphitiInfo.Tag- request.GraphitiImageHash = &graphitiInfo.Hash- }- if neo4jInfo := getContainerImageInfo(ctx, h.dockerClient, Neo4jContainerName); neo4jInfo != nil {- request.Neo4jImageName = &neo4jInfo.Name- request.Neo4jImageTag = &neo4jInfo.Tag- request.Neo4jImageHash = &neo4jInfo.Hash- }- }-- // get Langfuse image info if installed locally- if h.dockerClient != nil && c.LangfuseConnected && !c.LangfuseExternal && c.LangfuseInstalled {- if workerInfo := getContainerImageInfo(ctx, h.dockerClient, LangfuseWorkerContainerName); workerInfo != nil {- request.LangfuseWorkerImageName = &workerInfo.Name- request.LangfuseWorkerImageTag = &workerInfo.Tag- request.LangfuseWorkerImageHash = &workerInfo.Hash- }- if webInfo := getContainerImageInfo(ctx, h.dockerClient, LangfuseWebContainerName); webInfo != nil {- request.LangfuseWebImageName = &webInfo.Name- request.LangfuseWebImageTag = &webInfo.Tag- request.LangfuseWebImageHash = &webInfo.Hash- }- }-- // get Grafana and OpenTelemetry image info if observability installed locally- if h.dockerClient != nil && c.ObservabilityConnected && !c.ObservabilityExternal && c.ObservabilityInstalled {- if grafanaInfo := getContainerImageInfo(ctx, h.dockerClient, GrafanaContainerName); grafanaInfo != nil {- request.GrafanaImageName = &grafanaInfo.Name- request.GrafanaImageTag = &grafanaInfo.Tag- request.GrafanaImageHash = &grafanaInfo.Hash- }- if otelInfo := getContainerImageInfo(ctx, h.dockerClient, OpenTelemetryContainerName); otelInfo != nil {- request.OpenTelemetryImageName = &otelInfo.Name- request.OpenTelemetryImageTag = &otelInfo.Tag- request.OpenTelemetryImageHash = &otelInfo.Hash- }- }-- response := checkUpdatesServer(ctx, updateServerURL, proxyURL, request)- if response != nil {- c.UpdateServerAccessible = true- c.InstallerIsUpToDate = response.InstallerIsUpToDate- c.PentagiIsUpToDate = response.PentagiIsUpToDate- c.GraphitiIsUpToDate = response.GraphitiIsUpToDate- c.LangfuseIsUpToDate = response.LangfuseIsUpToDate- c.ObservabilityIsUpToDate = response.ObservabilityIsUpToDate- c.WorkerIsUpToDate = response.WorkerIsUpToDate- } else {- c.UpdateServerAccessible = false- c.InstallerIsUpToDate = false- c.PentagiIsUpToDate = false- c.GraphitiIsUpToDate = false- c.LangfuseIsUpToDate = false- c.ObservabilityIsUpToDate = false- }-- return nil-}-func Gather(ctx context.Context, appState state.State) (CheckResult, error) {if appState == nil {return CheckResult{}, ErrAppStateNotInitializeddiff --git a/backend/cmd/installer/checker/checker_test.go b/backend/cmd/installer/checker/checker_test.gonew file mode 100644index 00000000..6003d774--- /dev/null+++ b/backend/cmd/installer/checker/checker_test.go@@ -0,0 +1,114 @@+package checker++import (+ "context"+ "path/filepath"+ "sync"+ "testing"++ "pentagi/cmd/installer/loader"+)++// A stack nobody configured is not "externally hosted".+//+// The three External flags are computed as "the configured URL differs from the+// bundled default", and an unset URL differs from everything — so an+// installation with no Graphiti at all used to report+// `graphiti_connected: false, graphiti_external: true`. That is what a released+// installer still puts on the legacy wire, and it is what the TUI told its+// operator.+//+// Nothing downstream ever changed behaviour because of it: every consumer starts+// with `Connected &&`, so the term was dead exactly when the bug fired. What this+// pins is the honesty of the field, whose one consumer that cannot apply the+// guard is a person reading it.+//+// Driven through the real gather functions rather than through a copy of the+// expression, because a test of `connected && differs` would pass with the fix+// reverted.+func TestExternalIsNeverClaimedForAStackThatIsNotConnected(t *testing.T) {+ envPath := filepath.Join(t.TempDir(), ".env")+ gather := func(t *testing.T, vars map[string]loader.EnvVar) *CheckResult {+ t.Helper()+ handler := &defaultCheckHandler{mx: &sync.Mutex{}, appState: &mockState{vars: vars, envPath: envPath}}+ result := &CheckResult{}+ ctx := context.Background()+ if err := handler.GatherGraphitiInfo(ctx, result); err != nil {+ t.Fatalf("GatherGraphitiInfo: %v", err)+ }+ if err := handler.GatherLangfuseInfo(ctx, result); err != nil {+ t.Fatalf("GatherLangfuseInfo: %v", err)+ }+ if err := handler.GatherObservabilityInfo(ctx, result); err != nil {+ t.Fatalf("GatherObservabilityInfo: %v", err)+ }+ return result+ }++ t.Run("nothing configured", func(t *testing.T) {+ got := gather(t, map[string]loader.EnvVar{})++ if got.GraphitiConnected || got.GraphitiExternal {+ t.Errorf("graphiti: connected=%v external=%v, want both false",+ got.GraphitiConnected, got.GraphitiExternal)+ }+ if got.LangfuseConnected || got.LangfuseExternal {+ t.Errorf("langfuse: connected=%v external=%v, want both false",+ got.LangfuseConnected, got.LangfuseExternal)+ }+ if got.ObservabilityConnected || got.ObservabilityExternal {+ t.Errorf("observability: connected=%v external=%v, want both false",+ got.ObservabilityConnected, got.ObservabilityExternal)+ }+ })++ t.Run("configured against somebody else's endpoint", func(t *testing.T) {+ got := gather(t, map[string]loader.EnvVar{+ "GRAPHITI_ENABLED": {Value: "true"},+ "GRAPHITI_URL": {Value: "https://graphiti.example.com"},+ "LANGFUSE_BASE_URL": {Value: "https://langfuse.example.com"},+ "LANGFUSE_PROJECT_ID": {Value: "p"},+ "LANGFUSE_PUBLIC_KEY": {Value: "pk"},+ "LANGFUSE_SECRET_KEY": {Value: "sk"},+ "OTEL_HOST": {Value: "otel.example.com:4317"},+ })++ if !got.GraphitiConnected || !got.GraphitiExternal {+ t.Errorf("graphiti: connected=%v external=%v, want both true",+ got.GraphitiConnected, got.GraphitiExternal)+ }+ if !got.LangfuseConnected || !got.LangfuseExternal {+ t.Errorf("langfuse: connected=%v external=%v, want both true",+ got.LangfuseConnected, got.LangfuseExternal)+ }+ if !got.ObservabilityConnected || !got.ObservabilityExternal {+ t.Errorf("observability: connected=%v external=%v, want both true",+ got.ObservabilityConnected, got.ObservabilityExternal)+ }+ })++ t.Run("configured against the bundled endpoint", func(t *testing.T) {+ got := gather(t, map[string]loader.EnvVar{+ "GRAPHITI_ENABLED": {Value: "true"},+ "GRAPHITI_URL": {Value: DefaultGraphitiEndpoint},+ "LANGFUSE_BASE_URL": {Value: DefaultLangfuseEndpoint},+ "LANGFUSE_PROJECT_ID": {Value: "p"},+ "LANGFUSE_PUBLIC_KEY": {Value: "pk"},+ "LANGFUSE_SECRET_KEY": {Value: "sk"},+ "OTEL_HOST": {Value: DefaultObservabilityEndpoint},+ })++ if !got.GraphitiConnected || got.GraphitiExternal {+ t.Errorf("graphiti: connected=%v external=%v, want connected and not external",+ got.GraphitiConnected, got.GraphitiExternal)+ }+ if !got.LangfuseConnected || got.LangfuseExternal {+ t.Errorf("langfuse: connected=%v external=%v, want connected and not external",+ got.LangfuseConnected, got.LangfuseExternal)+ }+ if !got.ObservabilityConnected || got.ObservabilityExternal {+ t.Errorf("observability: connected=%v external=%v, want connected and not external",+ got.ObservabilityConnected, got.ObservabilityExternal)+ }+ })+}diff --git a/backend/cmd/installer/checker/components.go b/backend/cmd/installer/checker/components.gonew file mode 100644index 00000000..2e979b67--- /dev/null+++ b/backend/cmd/installer/checker/components.go@@ -0,0 +1,453 @@+package checker++import (+ "context"+ "crypto/sha256"+ "encoding/hex"+ "io"+ "os"+ "path/filepath"+ "runtime"+ "strings"++ "pentagi/cmd/installer/cloud"+ "pentagi/cmd/installer/wizard/logger"++ "github.com/moby/moby/client"+ "github.com/vxcontrol/cloud/models"+)++// This file turns a running installation into the component list the update server is+// asked about. The list has to be COMPLETE: the server answers only about what it was+// told, so a component left out comes back as "nothing to report" — which reads exactly+// like "up to date" and is the more dangerous of the two mistakes.++// maxReportedComponents is the server-side ceiling on one request. The inventory is well+// under it today, but the list grows with every stack, so the limit is enforced here and+// what gets dropped is written to the log — a silently truncated list would report the+// remaining components as complete.+const maxReportedComponents = 30++// JaegerPluginDir holds the Jaeger storage plugin, relative to the directory the+// environment file lives in.+//+// Exported because the processor writes into this directory when it downloads a newer+// plugin: the path, the file names and the digests reported for them have to be one+// table, or an update writes a file nothing reads.+const JaegerPluginDir = "observability/jaeger/bin"++// JaegerPluginBinaries maps the architecture a plugin file is built for to its name.+//+// Both files are on disk regardless of the host: the choice happens inside the Jaeger+// container, by the architecture of the Docker VM rather than of this machine. So both are+// reported, as two components.+var JaegerPluginBinaries = map[models.ArchType]string{+ models.ArchTypeAMD64: "jaeger-clickhouse-linux-amd64",+ models.ArchTypeARM64: "jaeger-clickhouse-linux-arm64",+}++// imageComponent names the container an image component is observed through.+//+// The table is explicit because nothing here can be derived. Four of the sixteen observed+// components run in a container whose name is not the component's: postgres, clickhouse,+// redis and minio all carry the `langfuse-` prefix of the stack that owns them. Guessing+// the container from the component would silently drop those four — a name matching no+// container is indistinguishable from a container that is not deployed — so the mapping+// lives in exactly one place, and TestEveryObservedContainerExistsInCompose keeps it+// honest against the compose files.+type imageComponent struct {+ Component models.ComponentType+ Container string+}++// The four groups below are also the order components are reported in, which is the order+// the ceiling above cuts from: the stacks whose updates matter most come first.+var (+ pentagiImageComponents = []imageComponent{+ {models.ComponentTypePentagi, PentagiContainerName},+ {models.ComponentTypeScraper, ScraperContainerName},+ {models.ComponentTypePgvector, PgvectorContainerName},+ // Last in its group on purpose: the ceiling cuts from the tail, and an+ // exporter is the one component of this stack an installation can live+ // without hearing about.+ {models.ComponentTypePgexporter, PgexporterContainerName},+ }+ graphitiImageComponents = []imageComponent{+ {models.ComponentTypeGraphiti, GraphitiContainerName},+ {models.ComponentTypeNeo4j, Neo4jContainerName},+ }+ langfuseImageComponents = []imageComponent{+ {models.ComponentTypeLangfuseWorker, LangfuseWorkerContainerName},+ {models.ComponentTypeLangfuseWeb, LangfuseWebContainerName},+ {models.ComponentTypePostgres, LangfusePostgresContainerName},+ {models.ComponentTypeClickhouse, LangfuseClickhouseContainerName},+ {models.ComponentTypeRedis, LangfuseRedisContainerName},+ {models.ComponentTypeMinio, LangfuseMinioContainerName},+ }+ observabilityImageComponents = []imageComponent{+ {models.ComponentTypeGrafana, GrafanaContainerName},+ {models.ComponentTypeOtel, OpenTelemetryContainerName},+ {models.ComponentTypeVictoriametrics, VictoriaMetricsContainerName},+ {models.ComponentTypeLoki, LokiContainerName},+ {models.ComponentTypeJaeger, JaegerContainerName},+ // clickstore is the ClickHouse server Jaeger stores traces in, and it is a+ // different component from `clickhouse`, which is Langfuse's. The two run+ // the same image and belong to different stacks.+ {models.ComponentTypeClickstore, ClickstoreContainerName},+ {models.ComponentTypeCadvisor, CadvisorContainerName},+ {models.ComponentTypeNodeExporter, NodeExporterContainerName},+ }+)++// stackGate decides how a stack's components are reported.+//+// A stack pointing at an external address has no local images to describe, so its+// components are reported as connected and carry no image data. A stack that is not+// configured at all is not reported: "unused" would spend one of the thirty slots saying+// nothing.+type stackGate struct {+ connected bool+ external bool+}++func (g stackGate) skip() bool { return !g.connected }+func (g stackGate) isExternal() bool { return g.external }++// gatherComponents builds the component list for an update check.+//+// Anything that cannot be described completely is left out rather than guessed at: a+// component with an unknown architecture, or an image the daemon will not describe, would+// fail the contract's validation and take the whole request down with it — costing the+// answer for every other component too.+// reportedComponents is what one update check says about this machine. Images and+// files are separate all the way through: an image is identified by a registry+// reference and a digest, a file by a version and a hash, and no artefact is both.+type reportedComponents struct {+ images []models.ImageComponentInfo+ files []models.FileComponentInfo+}++func (r reportedComponents) total() int { return len(r.images) + len(r.files) }++func (h *defaultCheckHandler) gatherComponents(ctx context.Context, c *CheckResult) reportedComponents {+ var reported reportedComponents++ // The pentagi stack is always reported: it is the product itself, with no external+ // alternative to point at.+ reported.images = append(reported.images,+ h.imageComponents(ctx, pentagiImageComponents, stackGate{connected: true})...)++ if worker := h.workerComponent(ctx, c); worker != nil {+ reported.images = append(reported.images, *worker)+ }+ if installer := h.installerComponent(); installer != nil {+ reported.files = append(reported.files, *installer)+ }++ reported.images = append(reported.images, h.imageComponents(ctx, graphitiImageComponents,+ stackGate{connected: c.GraphitiConnected, external: c.GraphitiExternal})...)+ reported.images = append(reported.images, h.imageComponents(ctx, langfuseImageComponents,+ stackGate{connected: c.LangfuseConnected, external: c.LangfuseExternal})...)++ observability := stackGate{connected: c.ObservabilityConnected, external: c.ObservabilityExternal}+ reported.images = append(reported.images, h.imageComponents(ctx, observabilityImageComponents, observability)...)+ reported.files = append(reported.files, h.jaegerPluginComponents(observability)...)++ return h.enforceComponentLimit(reported)+}++// enforceComponentLimit trims the report to what the server accepts, naming what it+// dropped.+//+// The limit is on the SUM of both lists — that is how the server counts it, and+// bounding each separately would build a request the server rejects whole.+func (h *defaultCheckHandler) enforceComponentLimit(reported reportedComponents) reportedComponents {+ if reported.total() <= maxReportedComponents {+ return reported+ }++ var dropped []string+ // Files are trimmed first only if images alone already fill the budget; otherwise+ // the tail of the files is what goes, so the images — which are the product — keep+ // their slots.+ if len(reported.images) > maxReportedComponents {+ for _, component := range reported.images[maxReportedComponents:] {+ dropped = append(dropped, component.Component.String())+ }+ for _, component := range reported.files {+ dropped = append(dropped, component.Component.String())+ }+ reported.images = reported.images[:maxReportedComponents]+ reported.files = nil+ } else {+ room := maxReportedComponents - len(reported.images)+ for _, component := range reported.files[room:] {+ dropped = append(dropped, component.Component.String())+ }+ reported.files = reported.files[:room]+ }++ logger.Warnf("update check covers %d of %d components, no answer for: %s",+ maxReportedComponents, maxReportedComponents+len(dropped), strings.Join(dropped, ", "))++ return reported+}++// imageComponents describes one stack's images.+func (h *defaultCheckHandler) imageComponents(+ ctx context.Context, sources []imageComponent, gate stackGate,+) []models.ImageComponentInfo {+ if gate.skip() {+ return nil+ }++ components := make([]models.ImageComponentInfo, 0, len(sources))+ for _, source := range sources {+ if gate.isExternal() {+ // An external stack runs somewhere we cannot inspect. There is no reference+ // to report and therefore nothing the server can resolve, so the component+ // is left out rather than sent as an image without an image.+ continue+ }++ if component := h.describeContainerImage(ctx, source); component != nil {+ components = append(components, *component)+ }+ }++ return components+}++// describeContainerImage inspects one container and the image behind it.+//+// The image, not the host, decides os and arch: a container image is linux even on a+// darwin host, and on Apple silicon it may well be an amd64 image running under emulation.+// Taking either from runtime would describe the wrong artefact.+func (h *defaultCheckHandler) describeContainerImage(+ ctx context.Context, source imageComponent,+) *models.ImageComponentInfo {+ if h.dockerClient == nil {+ return nil+ }++ container, err := h.dockerClient.ContainerInspect(ctx, source.Container, client.ContainerInspectOptions{})+ if err != nil {+ // No container means the component is not deployed here, which is not worth a+ // slot in the request.+ return nil+ }++ status := models.ComponentStatusInstalled+ if container.Container.State != nil && container.Container.State.Running {+ status = models.ComponentStatusRunning+ }++ reference := ""+ if container.Container.Config != nil {+ reference = container.Container.Config.Image+ }++ image, err := h.dockerClient.ImageInspect(ctx, container.Container.Image)+ if err != nil {+ logger.Warnf("component %s: image %s cannot be inspected: %v",+ source.Component, container.Container.Image, err)+ return nil+ }++ osType := models.OSType(strings.ToLower(image.Os))+ archType := models.ArchType(strings.ToLower(image.Architecture))+ if osType.Valid() != nil || archType.Valid() != nil {+ // A platform the contract has no name for would fail validation and take the+ // whole request with it, so this one component is dropped instead.+ logger.Warnf("component %s: unsupported image platform %s/%s, not reported",+ source.Component, image.Os, image.Architecture)+ return nil+ }++ repository, tag, ok := imageReference(reference)+ if !ok {+ logger.Warnf("component %s: container image reference %q cannot be parsed, not reported",+ source.Component, reference)+ return nil+ }++ component := models.ImageComponentInfo{+ Component: source.Component,+ Status: status,+ OS: osType,+ Arch: archType,+ Repository: repository,+ Tag: tag,+ }+ if digest := normalizeDigest(image.ID); digest != "" {+ component.ImageHash = &digest+ }++ return &component+}++// imageReference splits a container's image reference into the pair the server+// matches on — repository as compose spells it, with the registry prefix and the+// implicit "library/" removed — and reports whether it can be named at all.+//+// A reference that cannot be named has no channel to resolve, so the component is+// dropped rather than sent incomplete: an update is the newest image UNDER a+// reference, and the contract requires both halves for exactly that reason.+//+// There is no tag default here because parseImageRef already applies the one+// `docker pull` applies, and never returns an empty tag or an empty name for a+// non-empty reference — it falls back to the whole original. So the only+// reference this rejects is the absent one, which is a container whose config+// carries no image at all.+func imageReference(reference string) (repository, tag string, ok bool) {+ info := parseImageRef(reference, "")+ if info == nil || info.Name == "" {+ return "", "", false+ }+ return info.Name, info.Tag, true+}++// workerComponent describes the pentest worker image.+//+// It has no container and no compose service: the image is pulled into the worker+// environment on demand. What is reported is what is actually there — substituting the+// built-in default would claim an image the machine may not have.+func (h *defaultCheckHandler) workerComponent(ctx context.Context, c *CheckResult) *models.ImageComponentInfo {+ if h.workerClient == nil || !c.WorkerImageExists {+ return nil+ }++ configured := getEnvVar(h.appState, "DOCKER_DEFAULT_IMAGE_FOR_PENTEST", DefaultImageForPentest)+ info := getImageInfo(ctx, h.workerClient, configured)+ if info == nil || info.Name == "" {+ return nil+ }++ component := models.ImageComponentInfo{+ Component: models.ComponentTypeWorker,+ // No container exists for it, so the image being present is the whole story.+ Status: models.ComponentStatusInstalled,+ OS: models.OSTypeLinux,+ Arch: models.ArchType(runtime.GOARCH),+ }++ // The image itself is authoritative about its platform when the daemon will describe+ // it; the host architecture is only a fallback for a daemon that will not.+ if image, err := h.workerClient.ImageInspect(ctx, info.Name+":"+info.Tag); err == nil {+ osType := models.OSType(strings.ToLower(image.Os))+ archType := models.ArchType(strings.ToLower(image.Architecture))+ if osType.Valid() == nil && archType.Valid() == nil {+ component.OS, component.Arch = osType, archType+ }+ if digest := normalizeDigest(image.ID); digest != "" {+ component.ImageHash = &digest+ }+ } else if digest := normalizeDigest(info.Hash); digest != "" {+ component.ImageHash = &digest+ }++ if component.Arch.Valid() != nil {+ return nil+ }++ // getImageInfo answers through parseImageRef, so the tag is already the one+ // `docker pull` would apply.+ component.Repository = info.Name+ component.Tag = info.Tag++ return &component+}++// installerComponent describes the running installer binary — the one component that is a+// file on this host rather than an image, and so the only one whose os and arch are the+// host's own.+func (h *defaultCheckHandler) installerComponent() *models.FileComponentInfo {+ osType := models.OSType(runtime.GOOS)+ archType := models.ArchType(runtime.GOARCH)+ if osType.Valid() != nil || archType.Valid() != nil {+ logger.Warnf("installer runs on unsupported platform %s/%s, not reported",+ runtime.GOOS, runtime.GOARCH)+ return nil+ }++ version := cloud.NormalizeVersion(InstallerVersion)+ component := models.FileComponentInfo{+ Component: models.ComponentTypeInstaller,+ Status: models.ComponentStatusInstalled,+ OS: osType,+ Arch: archType,+ Version: &version,+ }++ // A hash of ourselves is a nice-to-have: the server identifies installer builds by+ // version. Failing to read our own path is no reason to drop the component.+ if path, err := os.Executable(); err == nil {+ if digest, err := fileDigest(path); err == nil {+ component.FileHash = &digest+ }+ }++ return &component+}++// jaegerPluginComponents describes the Jaeger storage plugin, which ships as one file per+// architecture. Both are reported: which one runs is decided inside the Jaeger container.+func (h *defaultCheckHandler) jaegerPluginComponents(gate stackGate) []models.FileComponentInfo {+ if gate.skip() || gate.isExternal() || h.appState == nil {+ return nil+ }++ base := filepath.Join(filepath.Dir(h.appState.GetEnvPath()), JaegerPluginDir)++ components := make([]models.FileComponentInfo, 0, len(JaegerPluginBinaries))+ // Fixed order, so two runs on the same machine produce the same request.+ for _, arch := range []models.ArchType{models.ArchTypeAMD64, models.ArchTypeARM64} {+ digest, err := fileDigest(filepath.Join(base, JaegerPluginBinaries[arch]))+ if err != nil {+ continue+ }+ components = append(components, models.FileComponentInfo{+ Component: models.ComponentTypeJaegerClickhouse,+ Status: models.ComponentStatusInstalled,+ // The plugin runs inside the Jaeger container, so it is a linux binary+ // whatever this host is.+ OS: models.OSTypeLinux,+ Arch: arch,+ FileHash: &digest,+ })+ }++ return components+}++// normalizeDigest converts a digest as the Docker daemon reports it into the form the+// contract accepts: bare lowercase hex, no "sha256:" prefix. An unrecognisable value+// becomes empty, so it is omitted rather than sent and rejected.+func normalizeDigest(raw string) string {+ digest := strings.ToLower(strings.TrimPrefix(strings.TrimSpace(raw), "sha256:"))+ if len(digest) != 64 {+ return ""+ }+ for _, r := range digest {+ if (r < '0' || r > '9') && (r < 'a' || r > 'f') {+ return ""+ }+ }+ return digest+}++// fileDigest returns the sha256 of a file in the same bare-hex form.+func fileDigest(path string) (string, error) {+ file, err := os.Open(path)+ if err != nil {+ return "", err+ }+ defer file.Close()++ digest := sha256.New()+ if _, err := io.Copy(digest, file); err != nil {+ return "", err+ }++ return hex.EncodeToString(digest.Sum(nil)), nil+}diff --git a/backend/cmd/installer/checker/components_test.go b/backend/cmd/installer/checker/components_test.gonew file mode 100644index 00000000..eed70e01--- /dev/null+++ b/backend/cmd/installer/checker/components_test.go@@ -0,0 +1,502 @@+package checker++import (+ "os"+ "path/filepath"+ "strings"+ "sync"+ "testing"++ "pentagi/cmd/installer/loader"++ "github.com/vxcontrol/cloud/models"+)++// composeFiles are the deployment descriptors the installer ships and extracts. The tests+// below read them rather than a copy, so a container renamed in a compose file fails here+// instead of silently making a component unobservable.+var composeFiles = []string{+ "../files/fs/docker-compose.yml",+ "../files/fs/docker-compose-graphiti.yml",+ "../files/fs/docker-compose-langfuse.yml",+ "../files/fs/docker-compose-observability.yml",+}++// containersNotReported are the containers deliberately left out of update checks.+//+// It is empty, and keeping it empty is the point. It once held pgexporter, node-exporter,+// cadvisor and clickstore under the reason "no component type in the contract" — which was+// simply untrue: all four are declared component types and the server has always been able+// to answer for them. The exclusion list turned a wrong belief into an enforced rule, so+// TestEveryComposeContainerIsAccountedFor passed while four running containers could never+// be offered an update. A CVE in cAdvisor or in the ClickHouse behind Jaeger would have+// reached no installation.+//+// So an entry here now needs a reason that survives being checked against+// models.AllComponentTypes, and TestEveryImageComponentOfADeployedStackIsReported below+// closes the same hole from the other side.+var containersNotReported = map[string]string{}++// composeContainerNames reads every container_name declared across the compose files.+func composeContainerNames(t *testing.T) map[string]string {+ t.Helper()++ names := map[string]string{}+ for _, path := range composeFiles {+ content, err := os.ReadFile(path)+ if err != nil {+ t.Fatalf("read %s: %v", path, err)+ }+ for _, line := range strings.Split(string(content), "\n") {+ trimmed := strings.TrimSpace(line)+ if !strings.HasPrefix(trimmed, "container_name:") {+ continue+ }+ name := strings.TrimSpace(strings.TrimPrefix(trimmed, "container_name:"))+ names[name] = filepath.Base(path)+ }+ }++ if len(names) == 0 {+ t.Fatal("no container names found — the compose files moved or changed shape")+ }++ return names+}++// inventoryContainers is the mapping as the gathering actually holds it.+func inventoryContainers(t *testing.T) map[models.ComponentType]string {+ t.Helper()++ got := map[models.ComponentType]string{}+ for _, group := range [][]imageComponent{+ pentagiImageComponents, graphitiImageComponents,+ langfuseImageComponents, observabilityImageComponents,+ } {+ for _, source := range group {+ if previous, seen := got[source.Component]; seen {+ t.Errorf("component %s is listed twice (%s and %s)",+ source.Component, previous, source.Container)+ }+ got[source.Component] = source.Container+ }+ }++ return got+}++// TestEveryObservedContainerExistsInCompose checks the mapping against the compose files+// themselves.+//+// Four components run in a container whose name is not their own — postgres, clickhouse,+// redis and minio all carry the `langfuse-` prefix of the stack that owns them — so the+// mapping cannot be derived and has to be written down. Writing it down by hand is exactly+// how it goes stale, and the failure is invisible: a container name that matches nothing+// looks identical to a container that is not deployed, so the component is quietly dropped+// from the request and the server never reports on it.+func TestEveryObservedContainerExistsInCompose(t *testing.T) {+ declared := composeContainerNames(t)++ for component, container := range inventoryContainers(t) {+ if _, exists := declared[container]; !exists {+ t.Errorf("component %s is observed through container %q, which no compose file declares",+ component, container)+ }+ }+}++// TestEveryComposeContainerIsAccountedFor is the other direction, and the one that catches+// growth: a service added to a compose file must either become a reported component or be+// listed as deliberately excluded. Without this, a new container simply never gets checked+// for updates and nothing anywhere says so.+func TestEveryComposeContainerIsAccountedFor(t *testing.T) {+ observed := map[string]models.ComponentType{}+ for component, container := range inventoryContainers(t) {+ observed[container] = component+ }++ for container, file := range composeContainerNames(t) {+ if _, reported := observed[container]; reported {+ continue+ }+ if _, excluded := containersNotReported[container]; excluded {+ continue+ }+ t.Errorf("container %q in %s is neither reported as a component nor listed as "++ "deliberately excluded — it would never be checked for updates", container, file)+ }+}++// TestJaegerPluginPathMatchesWhatTheContainerLoads pins the location against the compose+// file that mounts it. The Jaeger service mounts ./observability/jaeger at /etc/jaeger and+// loads the plugin from /etc/jaeger/bin/jaeger-clickhouse-linux-$ARCH, so the host-side+// path is fixed by that pair — and hashing a file that is not the one running would report+// a plugin nobody uses.+func TestJaegerPluginPathMatchesWhatTheContainerLoads(t *testing.T) {+ content, err := os.ReadFile("../files/fs/docker-compose-observability.yml")+ if err != nil {+ t.Fatalf("read compose: %v", err)+ }+ compose := string(content)++ if !strings.Contains(compose, "./observability/jaeger:/etc/jaeger") {+ t.Fatal("the jaeger service no longer mounts ./observability/jaeger at /etc/jaeger")+ }+ if !strings.Contains(compose, "/etc/jaeger/bin/jaeger-clickhouse-linux-$$ARCH") {+ t.Fatal("the jaeger service no longer loads its plugin from /etc/jaeger/bin")+ }++ // The mount plus the load path fix the host-side directory.+ if JaegerPluginDir != "observability/jaeger/bin" {+ t.Errorf("plugin directory = %q, want observability/jaeger/bin", JaegerPluginDir)+ }++ // Both files ship, because the architecture is chosen inside the container by uname.+ for _, name := range JaegerPluginBinaries {+ if _, err := os.Stat(filepath.Join("../files/fs", JaegerPluginDir, name)); err != nil {+ t.Errorf("plugin %s is not where the installer ships it: %v", name, err)+ }+ }+}++// TestInventoryFitsTheRequestCeiling: the whole observable inventory has to fit in one+// request, or some of it is never asked about. Sixteen images, plus the worker, the+// installer and the plugin's two architectures.+func TestInventoryFitsTheRequestCeiling(t *testing.T) {+ total := len(pentagiImageComponents) + len(graphitiImageComponents) ++ len(langfuseImageComponents) + len(observabilityImageComponents) ++ 1 + // worker+ 1 + // installer+ len(JaegerPluginBinaries)++ if total > maxReportedComponents {+ t.Fatalf("the inventory is %d components, above the %d a request carries: "++ "some would be dropped and never reported on", total, maxReportedComponents)+ }+}++// TestEveryReportedComponentIsInTheListForItsKind.+//+// Which list an artefact goes in is decided here by WHICH BUILDER constructs it+// — the image tables above versus installerComponent and jaegerPluginComponents+// — and nothing related that choice to the component itself. The delivery kind+// is a property of the component (models.ArtifactKind), the service validates+// the request against exactly that table, and a component built into the wrong+// list is now a rejected request rather than a silent misresolution.+//+// The service side rejects it at the wire; this catches it here, where the+// mistake would actually be made.+// TestEveryImageComponentOfADeployedStackIsReported walks the contract's vocabulary and+// demands a source for each entry, which is the direction the compose-file tests cannot+// see.+//+// Those tests compare the inventory against the compose files, so a component the contract+// declares but no source table mentions is invisible to them — the container is simply+// never looked at, and "not deployed" and "not looked at" produce identical requests. That+// is exactly how pgexporter, node-exporter, cadvisor and clickstore stayed unreportable+// while every existing test was green: four running containers the server knew about and+// was never told to answer for.+//+// Only the four stacks the installer deploys through compose are demanded here. The worker+// has its own gathering (no container exists for it) and engine and browser are not+// deployed by the installer at all, so a source for them would describe nothing.+func TestEveryImageComponentOfADeployedStackIsReported(t *testing.T) {+ deployed := map[models.ProductStack]bool{+ models.ProductStackPentagi: true,+ models.ProductStackGraphiti: true,+ models.ProductStackLangfuse: true,+ models.ProductStackObservability: true,+ }++ reported := inventoryContainers(t)+ for _, component := range models.AllComponentTypes {+ if !component.IsImageComponent() || !deployed[component.GetProductStack()] {+ continue+ }+ if _, ok := reported[component]; !ok {+ t.Errorf("component %s belongs to the %s stack and is delivered as an image, but no "++ "source names the container it runs in — it can never be offered an update",+ component, component.GetProductStack())+ }+ }+}++func TestEveryReportedComponentIsInTheListForItsKind(t *testing.T) {+ for component, container := range inventoryContainers(t) {+ if !component.IsImageComponent() {+ t.Errorf("%s is built as an image component (container %q) but is delivered as a %s",+ component, container, component.ArtifactKind())+ }+ }++ if !models.ComponentTypeWorker.IsImageComponent() {+ t.Errorf("the worker is built as an image component but is delivered as a %s",+ models.ComponentTypeWorker.ArtifactKind())+ }++ for _, component := range []models.ComponentType{+ models.ComponentTypeInstaller,+ models.ComponentTypeJaegerClickhouse,+ } {+ if !component.IsFileComponent() {+ t.Errorf("%s is built as a file component but is delivered as an %s",+ component, component.ArtifactKind())+ }+ }+}++// TestExternalStackIsNotReportedAsAnImage: a stack pointing somewhere we cannot inspect+// has no reference we can name, and an image component that names none cannot be resolved+// — the contract requires the repository and the tag precisely because an update is the+// newest image UNDER a reference.+//+// It used to be reported as `connected` with every image field empty. That worked while+// the fields were optional and is inexpressible now.+//+// What this costs is an analytics signal, not an answer: nothing was ever offered for an+// external stack, but its presence in the list is what raised has_langfuse /+// has_graphiti / has_observability in the cloud's view of this installation. Restoring+// that needs each component to carry its compose default reference, which is a separate+// piece of inventory work.+func TestExternalStackIsNotReportedAsAnImage(t *testing.T) {+ handler := &defaultCheckHandler{mx: &sync.Mutex{}}++ components := handler.imageComponents(t.Context(), langfuseImageComponents,+ stackGate{connected: true, external: true})++ if len(components) != 0 {+ t.Fatalf("got %d components for a stack running elsewhere, want none: %+v",+ len(components), components)+ }+}++// TestAnUnnameableReferenceIsNotReported.+//+// The contract requires an image to name the reference it follows, because an update+// IS the newest image under that reference. A container whose image reference cannot+// be parsed therefore has nothing that can be resolved, and reporting it with an empty+// repository would not fail quietly — it would fail the request's validation and cost+// the answer for every other component too.+//+// A tag is different: `docker pull` supplies one for an omitted tag, and so does this.+//+// Note what is NOT covered: the wiring from container inspection into this decision.+// describeContainerImage needs a Docker daemon and there is no fake for one here, so+// what is pinned is the decision itself.+func TestAnUnnameableReferenceIsNotReported(t *testing.T) {+ tests := []struct {+ reference string+ wantOK bool+ wantRepository string+ wantTag string+ }{+ {"vxcontrol/pentagi:latest", true, "vxcontrol/pentagi", "latest"},+ {"vxcontrol/pentagi", true, "vxcontrol/pentagi", "latest"},+ {"docker.io/library/redis:7", true, "redis", "7"},+ // parseImageRef never returns an empty name for a non-empty reference: it+ // falls back to the whole original. So the only reference that cannot be+ // named is the absent one — which is the real case, a container whose+ // config carries no image at all.+ {"", false, "", ""},+ {":latest", true, ":latest", "latest"},+ }++ for _, tt := range tests {+ t.Run(tt.reference, func(t *testing.T) {+ repository, tag, ok := imageReference(tt.reference)+ if ok != tt.wantOK {+ t.Fatalf("ok = %t, want %t (repository %q, tag %q)", ok, tt.wantOK, repository, tag)+ }+ if !ok {+ return+ }+ if repository != tt.wantRepository || tag != tt.wantTag {+ t.Errorf("got %q:%q, want %q:%q", repository, tag, tt.wantRepository, tt.wantTag)+ }+ component := models.ImageComponentInfo{+ Component: models.ComponentTypePentagi, Status: models.ComponentStatusRunning,+ OS: models.OSTypeLinux, Arch: models.ArchTypeAMD64,+ Repository: repository, Tag: tag,+ }+ if err := component.Valid(); err != nil {+ t.Errorf("the component built from this reference would be rejected: %v", err)+ }+ })+ }+}++// TestUnusedStackIsNotReportedAtAll: a stack nobody configured has nothing to say, and one+// of the thirty slots is worth more than an entry saying so.+func TestUnusedStackIsNotReportedAtAll(t *testing.T) {+ handler := &defaultCheckHandler{mx: &sync.Mutex{}}++ components := handler.imageComponents(t.Context(), graphitiImageComponents,+ stackGate{connected: false})++ if len(components) != 0 {+ t.Errorf("an unconfigured stack contributed %d components", len(components))+ }+}++func TestNormalizeDigest(t *testing.T) {+ valid := strings.Repeat("ab", 32)++ tests := []struct {+ name string+ raw string+ want string+ }{+ // The daemon always reports the prefix; the contract's validator rejects it,+ // and on this path a rejected field is answered as a server error rather than a+ // complaint about the field.+ {name: "the daemon prefix is stripped", raw: "sha256:" + valid, want: valid},+ {name: "already bare", raw: valid, want: valid},+ {name: "uppercase is lowered", raw: strings.ToUpper(valid), want: valid},+ {name: "surrounding whitespace", raw: " sha256:" + valid + " ", want: valid},++ // Anything unrecognisable is dropped rather than sent: an omitted digest means+ // "cannot verify", while a malformed one takes the whole request down.+ {name: "truncated digests are refused", raw: valid[:12], want: ""},+ {name: "non-hex is refused", raw: strings.Repeat("z", 64), want: ""},+ {name: "empty", raw: "", want: ""},+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ if got := normalizeDigest(tt.raw); got != tt.want {+ t.Errorf("normalizeDigest(%q) = %q, want %q", tt.raw, got, tt.want)+ }+ })+ }+}++// TestJaegerPluginIsReportedForBothArchitectures: both files sit on disk whatever the host+// is, because which one runs is decided inside the Jaeger container by the architecture of+// the Docker VM. Reporting only the host's would leave the other unverified forever.+func TestJaegerPluginIsReportedForBothArchitectures(t *testing.T) {+ envDir := t.TempDir()+ pluginDir := filepath.Join(envDir, JaegerPluginDir)+ if err := os.MkdirAll(pluginDir, 0o755); err != nil {+ t.Fatalf("mkdir: %v", err)+ }+ for arch, name := range JaegerPluginBinaries {+ if err := os.WriteFile(filepath.Join(pluginDir, name), []byte("plugin for "+arch.String()), 0o644); err != nil {+ t.Fatalf("write %s: %v", name, err)+ }+ }++ handler := &defaultCheckHandler{+ mx: &sync.Mutex{},+ appState: &mockState{vars: map[string]loader.EnvVar{}, envPath: filepath.Join(envDir, ".env")},+ }++ components := handler.jaegerPluginComponents(stackGate{connected: true})++ if len(components) != 2 {+ t.Fatalf("got %d plugin components, want 2", len(components))+ }++ seen := map[models.ArchType]string{}+ for _, component := range components {+ if component.Component != models.ComponentTypeJaegerClickhouse {+ t.Errorf("component = %q, want %q", component.Component, models.ComponentTypeJaegerClickhouse)+ }+ // The plugin runs inside the container, so it is a linux binary whatever this+ // host is.+ if component.OS != models.OSTypeLinux {+ t.Errorf("%s: os = %q, want linux", component.Arch, component.OS)+ }+ if component.FileHash == nil {+ t.Fatalf("%s: no digest", component.Arch)+ }+ if err := component.Valid(); err != nil {+ t.Errorf("%s: would be rejected by the contract: %v", component.Arch, err)+ }+ seen[component.Arch] = *component.FileHash+ }++ if seen[models.ArchTypeAMD64] == seen[models.ArchTypeARM64] {+ t.Error("both architectures report the same digest — one file was hashed twice")+ }+}++// TestJaegerPluginIsSkippedWhenObservabilityIsNotOurs: the plugin serves the local Jaeger.+// With observability pointing elsewhere there is no local Jaeger for it to serve.+func TestJaegerPluginIsSkippedWhenObservabilityIsNotOurs(t *testing.T) {+ // The files have to be on disk, or their absence — not the gate — is what produces the+ // empty list, and this test would pass with the gate removed entirely.+ envDir := t.TempDir()+ pluginDir := filepath.Join(envDir, JaegerPluginDir)+ if err := os.MkdirAll(pluginDir, 0o755); err != nil {+ t.Fatalf("mkdir: %v", err)+ }+ for arch, name := range JaegerPluginBinaries {+ if err := os.WriteFile(filepath.Join(pluginDir, name), []byte("plugin for "+arch.String()), 0o644); err != nil {+ t.Fatalf("write %s: %v", name, err)+ }+ }++ handler := &defaultCheckHandler{+ mx: &sync.Mutex{},+ appState: &mockState{vars: map[string]loader.EnvVar{}, envPath: filepath.Join(envDir, ".env")},+ }++ for _, gate := range []stackGate{+ {connected: false},+ {connected: true, external: true},+ } {+ if components := handler.jaegerPluginComponents(gate); len(components) != 0 {+ t.Errorf("gate %+v: reported %d plugin components", gate, len(components))+ }+ }+}++// TestInstallerReportsItselfWithAContractVersion: development builds are named after their+// branch, which is not a version. Sending one gets the whole request refused — and with it+// the answer for every other component.+func TestInstallerReportsItselfWithAContractVersion(t *testing.T) {+ handler := &defaultCheckHandler{mx: &sync.Mutex{}}++ component := handler.installerComponent()+ if component == nil {+ t.Skip("this platform has no contract name, which the gathering handles by skipping")+ }++ if err := component.Valid(); err != nil {+ t.Fatalf("the installer component would be rejected: %v", err)+ }+ if component.Version == nil {+ t.Fatal("the installer reports no version")+ }+ if component.FileHash != nil && len(*component.FileHash) != 64 {+ t.Errorf("file hash is %d characters, want 64", len(*component.FileHash))+ }+}++// TestComponentLimitNamesWhatItDropped: a silently truncated list reports the components+// that survived as if they were the whole installation.+func TestComponentLimitNamesWhatItDropped(t *testing.T) {+ handler := &defaultCheckHandler{mx: &sync.Mutex{}}++ // Over the ceiling with BOTH kinds present: the limit is on the sum, because that+ // is how the server counts it. Bounding each list separately would build a request+ // the server rejects whole.+ oversized := reportedComponents{}+ for i := 0; i < maxReportedComponents; i++ {+ oversized.images = append(oversized.images,+ models.ImageComponentInfo{Component: models.ComponentTypePentagi})+ }+ for i := 0; i < 3; i++ {+ oversized.files = append(oversized.files,+ models.FileComponentInfo{Component: models.ComponentTypeInstaller})+ }++ trimmed := handler.enforceComponentLimit(oversized)++ if trimmed.total() != maxReportedComponents {+ t.Errorf("got %d components, want the ceiling of %d", trimmed.total(), maxReportedComponents)+ }+ if len(trimmed.images) != maxReportedComponents {+ t.Errorf("the images are the product and must keep their slots, got %d", len(trimmed.images))+ }+}diff --git a/backend/cmd/installer/checker/helpers.go b/backend/cmd/installer/checker/helpers.goindex 9802d0e4..9ed901aa 100644--- a/backend/cmd/installer/checker/helpers.go+++ b/backend/cmd/installer/checker/helpers.go@@ -2,9 +2,7 @@ package checkerimport ("bufio"- "bytes""context"- "encoding/json""errors""io""net"@@ -36,53 +34,6 @@ type ImageInfo struct {Hash string}-type CheckUpdatesRequest struct {- InstallerOsType string `json:"installer_os_type"`- InstallerVersion string `json:"installer_version"`- PentagiImageName *string `json:"pentagi_image_name,omitempty"`- PentagiImageTag *string `json:"pentagi_image_tag,omitempty"`- PentagiImageHash *string `json:"pentagi_image_hash,omitempty"`- WorkerImageName *string `json:"worker_image_name,omitempty"`- WorkerImageTag *string `json:"worker_image_tag,omitempty"`- WorkerImageHash *string `json:"worker_image_hash,omitempty"`- GraphitiConnected bool `json:"graphiti_connected"`- GraphitiInstalled bool `json:"graphiti_installed"`- GraphitiExternal bool `json:"graphiti_external"`- GraphitiImageName *string `json:"graphiti_image_name,omitempty"`- GraphitiImageTag *string `json:"graphiti_image_tag,omitempty"`- GraphitiImageHash *string `json:"graphiti_image_hash,omitempty"`- Neo4jImageName *string `json:"neo4j_image_name,omitempty"`- Neo4jImageTag *string `json:"neo4j_image_tag,omitempty"`- Neo4jImageHash *string `json:"neo4j_image_hash,omitempty"`- LangfuseConnected bool `json:"langfuse_connected"`- LangfuseInstalled bool `json:"langfuse_installed"`- LangfuseExternal bool `json:"langfuse_external"`- ObservabilityConnected bool `json:"observability_connected"`- ObservabilityExternal bool `json:"observability_external"`- ObservabilityInstalled bool `json:"observability_installed"`- LangfuseWorkerImageName *string `json:"langfuse_worker_image_name,omitempty"`- LangfuseWorkerImageTag *string `json:"langfuse_worker_image_tag,omitempty"`- LangfuseWorkerImageHash *string `json:"langfuse_worker_image_hash,omitempty"`- LangfuseWebImageName *string `json:"langfuse_web_image_name,omitempty"`- LangfuseWebImageTag *string `json:"langfuse_web_image_tag,omitempty"`- LangfuseWebImageHash *string `json:"langfuse_web_image_hash,omitempty"`- GrafanaImageName *string `json:"grafana_image_name,omitempty"`- GrafanaImageTag *string `json:"grafana_image_tag,omitempty"`- GrafanaImageHash *string `json:"grafana_image_hash,omitempty"`- OpenTelemetryImageName *string `json:"otel_image_name,omitempty"`- OpenTelemetryImageTag *string `json:"otel_image_tag,omitempty"`- OpenTelemetryImageHash *string `json:"otel_image_hash,omitempty"`-}--type CheckUpdatesResponse struct {- InstallerIsUpToDate bool `json:"installer_is_up_to_date"`- PentagiIsUpToDate bool `json:"pentagi_is_up_to_date"`- GraphitiIsUpToDate bool `json:"graphiti_is_up_to_date"`- LangfuseIsUpToDate bool `json:"langfuse_is_up_to_date"`- ObservabilityIsUpToDate bool `json:"observability_is_up_to_date"`- WorkerIsUpToDate bool `json:"worker_is_up_to_date"`-}-func checkFileExists(path string) bool {_, err := os.Stat(path)return !os.IsNotExist(err)@@ -425,6 +376,9 @@ func getLinuxAvailableMemoryGB() float64 {}}}+ if err := scanner.Err(); err != nil {+ return 0.0+ }availableMemGB := float64(memAvailable) / (1024 * 1024 * 1024)if availableMemGB > 0 {@@ -515,6 +469,9 @@ func checkLinuxMemory(requiredGB float64) bool {}}}+ if err := scanner.Err(); err != nil {+ return true // assume OK if can't check+ }availableMemGB := float64(memAvailable) / (1024 * 1024 * 1024)if availableMemGB > 0 {@@ -827,6 +784,34 @@ func getImageInfo(ctx context.Context, cli *client.Client, imageName string) *Imreturn imageInfo}+// parseImageRef splits a Docker image reference into the repository and tag the update+// server matches on, plus whatever digest the reference or the daemon carried.+//+// The shape is [registry[:port]/]path[:tag][@digest], and the two colons are the trap: the+// one that separates a registry port and the one that separates a tag look identical. They+// are told apart by position, not by content — a tag colon is the LAST colon after the last+// slash. The previous rule guessed by content, treating any tag containing a dot as a port+// number, which misread every version-numbered tag the compose files use: "grafana/grafana"+// at "11.4.0" became a repository literally named "grafana/grafana:11.4.0" at tag "latest".+//+// That failure is silent where it matters. The update server answers on the repository and+// tag pair, so a mangled pair simply gets no answer — and no answer is indistinguishable+// from "nothing to update".+// isDockerHubHost reports whether a leading path element is one of the names+// Docker Hub answers to.+//+// A closed list rather than a shape test. "Contains a dot or a colon" describes+// every registry there is, and treating every registry as Docker Hub is exactly+// how the host came to be dropped from references that need it.+func isDockerHubHost(head string) bool {+ switch head {+ case "docker.io", "index.docker.io", "registry-1.docker.io", "registry.hub.docker.com":+ return true+ default:+ return false+ }+}+func parseImageRef(imageRef, imageID string) *ImageInfo {if imageRef == "" {return nil@@ -836,67 +821,51 @@ func parseImageRef(imageRef, imageID string) *ImageInfo {Hash: imageID,}- // normalize the image referenceoriginalRef := imageRef- // parse hash if present (image@sha256:...)- if strings.Contains(imageRef, "@") {- parts := strings.SplitN(imageRef, "@", 2)- imageRef = parts[0]- if len(parts) > 1 {- info.Hash = parts[1]+ // A digest pins the reference and is not part of the name.+ if base, digest, found := strings.Cut(imageRef, "@"); found {+ imageRef = base+ if digest != "" {+ info.Hash = digest}}- // handle registry/namespace/repository parsing- var name string- if strings.Contains(imageRef, "/") {- // has registry or namespace- nameParts := strings.Split(imageRef, "/")- if len(nameParts) >= 2 {- // check if first part looks like a registry (contains . or :)- if strings.Contains(nameParts[0], ".") || strings.Contains(nameParts[0], ":") {- // first part is registry, skip it for name extraction- if len(nameParts) > 2 {- name = strings.Join(nameParts[1:], "/")- } else {- name = nameParts[1]- }- } else {- // no registry, combine all parts as name- name = imageRef- }- } else {- name = imageRef+ // The registry host is dropped ONLY for Docker Hub, and keeping it everywhere+ // else is the whole point.+ //+ // The service stores a reference the way compose writes it: bare for Docker+ // Hub, host and all for anywhere else — `gcr.io/cadvisor/cadvisor`, not+ // `cadvisor/cadvisor`. Those two are different images: the second is a Docker+ // Hub repository that has nothing to do with cAdvisor. Dropping the host for+ // every registry made this client report the second while running the first,+ // and since the match is an exact string comparison, the component simply+ // resolved to nothing — answered `repository_not_tracked` forever, and+ // dragging its whole stack's resolution down to `not_tracked` with it.+ //+ // It stayed invisible until cadvisor and pgexporter — the only two components+ // not on Docker Hub — started being reported at all.+ if head, rest, found := strings.Cut(imageRef, "/"); found {+ if isDockerHubHost(head) {+ imageRef = rest}- } else {- name = imageRef- }-- // parse name and tag- if strings.Contains(name, ":") {- parts := strings.SplitN(name, ":", 2)- info.Name = parts[0]- if len(parts) > 1 && parts[1] != "" {- // validate that it's not a port number (for registry detection edge case)- if !strings.Contains(parts[1], ".") {- info.Tag = parts[1]- } else {- info.Name = name- info.Tag = "latest"- }- }- } else {- info.Name = name- info.Tag = "latest"}- // if we still don't have a tag, default to latest+ // Docker Hub spells an official image "library/nginx" in full; every other place spells+ // it "nginx", and that is the form the server is asked about.+ imageRef = strings.TrimPrefix(imageRef, "library/")++ // The tag is what follows the last colon, and only when that colon comes after the last+ // slash — otherwise it belongs to a registry port that was kept as part of the name.+ name, tag := imageRef, ""+ if colon := strings.LastIndex(imageRef, ":"); colon > strings.LastIndex(imageRef, "/") {+ name, tag = imageRef[:colon], imageRef[colon+1:]+ }++ info.Name, info.Tag = name, tagif info.Tag == "" {info.Tag = "latest"}-- // validate that name is not emptyif info.Name == "" {info.Name = originalRefinfo.Tag = "latest"@@ -905,55 +874,6 @@ func parseImageRef(imageRef, imageID string) *ImageInfo {return info}-func checkUpdatesServer(- ctx context.Context,- serverURL, proxyURL string,- request CheckUpdatesRequest,-) *CheckUpdatesResponse {- jsonData, err := json.Marshal(request)- if err != nil {- return nil- }-- client := &http.Client{- Timeout: 30 * time.Second,- }-- if proxyURL != "" {- if proxyURLParsed, err := url.Parse(proxyURL); err == nil {- client.Transport = &http.Transport{- Proxy: http.ProxyURL(proxyURLParsed),- }- }- }-- fullURL := serverURL + UpdatesCheckEndpoint- req, err := http.NewRequestWithContext(ctx, "POST", fullURL, bytes.NewBuffer(jsonData))- if err != nil {- return nil- }-- req.Header.Set("Content-Type", "application/json")- req.Header.Set("User-Agent", UserAgent)-- resp, err := client.Do(req)- if err != nil {- return nil- }- defer resp.Body.Close()-- if resp.StatusCode != http.StatusOK {- return nil- }-- var response CheckUpdatesResponse- if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {- return nil- }-- return &response-}-func checkDNSResolution(hostname string) bool {_, err := net.LookupHost(hostname)return err == nildiff --git a/backend/cmd/installer/checker/helpers_test.go b/backend/cmd/installer/checker/helpers_test.goindex 5e7a74a6..51a4efa9 100644--- a/backend/cmd/installer/checker/helpers_test.go+++ b/backend/cmd/installer/checker/helpers_test.go@@ -4,13 +4,10 @@ import ("context""errors""fmt"- "net/http"- "net/http/httptest""os""path/filepath""strings""testing"- "time""pentagi/cmd/installer/loader""pentagi/cmd/installer/state"@@ -263,14 +260,50 @@ func TestParseImageRef(t *testing.T) {{"nginx", "", "nginx", "latest", ""},{"nginx", "sha256:def", "nginx", "latest", "sha256:def"},{"repo/nginx:1.2", "", "repo/nginx", "1.2", ""},- {"docker.io/library/ubuntu:latest", "", "library/ubuntu", "latest", ""},+ // Docker Hub spells an official image in full; everywhere else it is just "ubuntu",+ // and that is the name the update server is asked about.+ {"docker.io/library/ubuntu:latest", "", "ubuntu", "latest", ""},{"nginx@sha256:deadbeef", "", "nginx", "latest", "sha256:deadbeef"},- {"myreg:5000/foo/bar:tag@sha256:beef", "", "foo/bar", "tag", "sha256:beef"},- {"localhost:5000/myapp:v1.0", "", "myapp", "v1.0", ""},- {"registry.example.com/team/app", "", "team/app", "latest", ""},++ // Everywhere that is not Docker Hub, the host is PART of the name and stays.+ //+ // These three used to expect the opposite, and that expectation is what+ // made the defect invisible: the service stores a reference the way+ // compose writes it — `gcr.io/cadvisor/cadvisor`, host and all — and+ // matches on exact equality. A client reporting `cadvisor/cadvisor` names+ // a different image (a Docker Hub repository that is not cAdvisor), so the+ // component matched nothing, came back `repository_not_tracked`, and took+ // its whole stack's resolution down to `not_tracked` with it. Two tests+ // were green the whole time, one on each side, pinning opposite rules.+ {"myreg:5000/foo/bar:tag@sha256:beef", "", "myreg:5000/foo/bar", "tag", "sha256:beef"},+ {"localhost:5000/myapp:v1.0", "", "localhost:5000/myapp", "v1.0", ""},+ {"registry.example.com/team/app", "", "registry.example.com/team/app", "latest", ""},++ // The two components that are not on Docker Hub, exactly as their compose+ // defaults spell them and exactly as the service has them stored.+ {"gcr.io/cadvisor/cadvisor:v0.51.0", "", "gcr.io/cadvisor/cadvisor", "v0.51.0", ""},+ {"quay.io/prometheuscommunity/postgres-exporter:v0.16.0", "",+ "quay.io/prometheuscommunity/postgres-exporter", "v0.16.0", ""},++ // Docker Hub under each of the names it answers to: here the host IS+ // dropped, because that is the form compose writes and the service stores.+ {"index.docker.io/library/redis:7", "", "redis", "7", ""},+ {"registry-1.docker.io/prom/node-exporter:v1.8.2", "", "prom/node-exporter", "v1.8.2", ""},{"", "", "", "", ""},{"ubuntu:", "", "ubuntu", "latest", ""},{"ubuntu:@sha256:hash", "", "ubuntu", "latest", "sha256:hash"},++ // Every version-numbered tag the shipped compose files use. These are the cases the+ // old dot-means-a-port rule got wrong: the tag was swallowed into the repository+ // name and replaced with "latest", so the pair the server matches on named nothing+ // and the component silently got no answer.+ {"neo4j:5.26.2", "", "neo4j", "5.26.2", ""},+ {"grafana/grafana:11.4.0", "", "grafana/grafana", "11.4.0", ""},+ {"grafana/loki:3.3.2", "", "grafana/loki", "3.3.2", ""},+ {"jaegertracing/all-in-one:1.56.0", "", "jaegertracing/all-in-one", "1.56.0", ""},+ {"victoriametrics/victoria-metrics:v1.108.1", "", "victoriametrics/victoria-metrics", "v1.108.1", ""},+ {"otel/opentelemetry-collector-contrib:0.116.1", "", "otel/opentelemetry-collector-contrib", "0.116.1", ""},+ {"minio/minio:RELEASE.2025-07-23T15-54-02Z", "", "minio/minio", "RELEASE.2025-07-23T15-54-02Z", ""},}for _, tt := range tests {@@ -289,19 +322,95 @@ func TestParseImageRef(t *testing.T) {return}- // note: current implementation has some edge cases with registry parsing- // we test for non-nil result and basic structure rather than exact parsing- if info.Name == "" {- t.Errorf("parseImageRef(%q).Name should not be empty", tt.imageRef)+ if info.Name != tt.wantName {+ t.Errorf("parseImageRef(%q).Name = %q, want %q", tt.imageRef, info.Name, tt.wantName)+ }+ if info.Tag != tt.wantTag {+ t.Errorf("parseImageRef(%q).Tag = %q, want %q", tt.imageRef, info.Tag, tt.wantTag)}- if info.Tag == "" {- t.Errorf("parseImageRef(%q).Tag should not be empty", tt.imageRef)+ if info.Hash != tt.wantHash {+ t.Errorf("parseImageRef(%q).Hash = %q, want %q", tt.imageRef, info.Hash, tt.wantHash)}- // hash may be empty, that's OK})}}+// TestEveryComposeImageParsesIntoAMatchablePair reads the shipped compose files and checks+// that every image reference in them splits into a repository and tag the update server can+// match. It reads the files rather than a copy, so an image pinned to a new kind of tag is+// caught here instead of quietly costing that component its answer.+func TestEveryComposeImageParsesIntoAMatchablePair(t *testing.T) {+ for _, path := range []string{+ "../files/fs/docker-compose.yml",+ "../files/fs/docker-compose-graphiti.yml",+ "../files/fs/docker-compose-langfuse.yml",+ "../files/fs/docker-compose-observability.yml",+ } {+ content, err := os.ReadFile(path)+ if err != nil {+ t.Fatalf("read %s: %v", path, err)+ }++ for _, line := range strings.Split(string(content), "\n") {+ trimmed := strings.TrimSpace(line)+ if !strings.HasPrefix(trimmed, "image:") {+ continue+ }+ ref := strings.TrimSpace(strings.TrimPrefix(trimmed, "image:"))+ // Every image line is `${<COMPONENT>_IMAGE:-<default>}` now — the server chooses+ // the tag and the installer only writes what it was given — so the default inside+ // the wrapper is what a fresh installation actually pulls, and it is the thing+ // worth checking. Stripping only PENTAGI_IMAGE was right when it was the only+ // parameterised line and became wrong the day the rest followed.+ //+ // The wrapper is never handed to parseImageRef in production: the checker parses+ // what the daemon reports for a running container, which is already resolved. A+ // template reaching the parser produces nonsense either way — `${NEO4J_IMAGE:-neo4j`+ // as a repository, or the quieter `latest}` as a tag when the default has a slash+ // and the wrapper's own colon gets mistaken for a registry port.+ if inner, found := strings.CutPrefix(ref, "${"); found {+ if _, def, ok := strings.Cut(strings.TrimSuffix(inner, "}"), ":-"); ok {+ ref = def+ }+ }++ info := parseImageRef(ref, "")+ if info == nil {+ t.Errorf("%s: %q did not parse at all", filepath.Base(path), ref)+ continue+ }+ // A tag glued onto the repository is the exact shape of the old defect, and it+ // produces a pair that matches nothing on the server.+ if strings.Contains(info.Name, ":") {+ t.Errorf("%s: %q parsed to repository %q — the tag was swallowed into the name",+ filepath.Base(path), ref, info.Name)+ }+ // Only a reference that genuinely omits its tag should come out as "latest".+ if info.Tag == "latest" && !strings.HasSuffix(ref, ":latest") && strings.Contains(ref, ":") {+ t.Errorf("%s: %q parsed to tag %q, but the reference pins a different one",+ filepath.Base(path), ref, info.Tag)+ }+ // A reference that names a registry must still name it after parsing.+ //+ // The service stores what compose writes and matches on exact equality,+ // so a dropped host is a repository that names a DIFFERENT image —+ // `cadvisor/cadvisor` on Docker Hub instead of the one on gcr.io. The+ // component then resolves to nothing and is answered+ // `repository_not_tracked` for the life of the installation. Checking+ // every compose line rather than the two that have a host today is the+ // point: the next third-party image added to these files gets the same+ // guarantee without anybody remembering this.+ if host, _, found := strings.Cut(ref, "/"); found && !isDockerHubHost(host) {+ if strings.ContainsAny(host, ".:") && !strings.HasPrefix(info.Name, host+"/") {+ t.Errorf("%s: %q parsed to repository %q — the registry host was dropped, "++ "which names a different image than the one running",+ filepath.Base(path), ref, info.Name)+ }+ }+ }+ }+}+func TestCheckCPUResources(t *testing.T) {result := checkCPUResources()// assuming test machine has at least 2 CPUs, this is reasonable for CI/dev environments@@ -436,143 +545,6 @@ func TestCheckDiskSpaceWithContext(t *testing.T) {}}-func TestCheckUpdatesServer(t *testing.T) {- // test successful response- t.Run("successful_response", func(t *testing.T) {- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- if r.Method != "POST" {- w.WriteHeader(http.StatusMethodNotAllowed)- return- }- if r.Header.Get("Content-Type") != "application/json" {- w.WriteHeader(http.StatusBadRequest)- return- }- if r.Header.Get("User-Agent") != UserAgent {- w.WriteHeader(http.StatusBadRequest)- return- }-- w.Header().Set("Content-Type", "application/json")- fmt.Fprintf(w, `{- "installer_is_up_to_date": true,- "pentagi_is_up_to_date": false,- "langfuse_is_up_to_date": true,- "observability_is_up_to_date": false,- "worker_is_up_to_date": true- }`)- }))- defer ts.Close()-- ctx := context.Background()- request := CheckUpdatesRequest{- InstallerVersion: "1.0.0",- InstallerOsType: "darwin",- }-- response := checkUpdatesServer(ctx, ts.URL, "", request)- if response == nil {- t.Fatal("expected non-nil response")- }- if !response.InstallerIsUpToDate {- t.Error("expected installer to be up to date")- }- if response.PentagiIsUpToDate {- t.Error("expected pentagi to not be up to date")- }- if !response.LangfuseIsUpToDate {- t.Error("expected langfuse to be up to date")- }- if response.ObservabilityIsUpToDate {- t.Error("expected observability to not be up to date")- }- if !response.WorkerIsUpToDate {- t.Error("expected worker to be up to date")- }- })-- // test server error- t.Run("server_error", func(t *testing.T) {- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- w.WriteHeader(http.StatusInternalServerError)- }))- defer ts.Close()-- ctx := context.Background()- request := CheckUpdatesRequest{InstallerVersion: "1.0.0"}-- response := checkUpdatesServer(ctx, ts.URL, "", request)- if response != nil {- t.Error("expected nil response for server error")- }- })-- // test invalid JSON response- t.Run("invalid_json", func(t *testing.T) {- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- w.Header().Set("Content-Type", "application/json")- fmt.Fprintf(w, `invalid json`)- }))- defer ts.Close()-- ctx := context.Background()- request := CheckUpdatesRequest{InstallerVersion: "1.0.0"}-- response := checkUpdatesServer(ctx, ts.URL, "", request)- if response != nil {- t.Error("expected nil response for invalid JSON")- }- })-- // test context timeout- t.Run("context_timeout", func(t *testing.T) {- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- time.Sleep(100 * time.Millisecond) // delay response- w.WriteHeader(http.StatusOK)- }))- defer ts.Close()-- ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)- defer cancel()-- request := CheckUpdatesRequest{InstallerVersion: "1.0.0"}- response := checkUpdatesServer(ctx, ts.URL, "", request)- if response != nil {- t.Error("expected nil response for timeout")- }- })-- // test proxy configuration- t.Run("with_proxy", func(t *testing.T) {- // create a proxy server that just forwards requests- proxyTs := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- w.Header().Set("Content-Type", "application/json")- fmt.Fprintf(w, `{"installer_is_up_to_date": true, "pentagi_is_up_to_date": true, "langfuse_is_up_to_date": true, "observability_is_up_to_date": true}`)- }))- defer proxyTs.Close()-- ctx := context.Background()- request := CheckUpdatesRequest{InstallerVersion: "1.0.0"}-- // note: testing with actual proxy setup is complex in unit tests- // this mainly tests that proxy URL doesn't cause the function to panic- response := checkUpdatesServer(ctx, proxyTs.URL, "http://invalid-proxy:8080", request)- // response might be nil due to proxy connection failure, which is expected- _ = response- })-- // test malformed server URL- t.Run("malformed_url", func(t *testing.T) {- ctx := context.Background()- request := CheckUpdatesRequest{InstallerVersion: "1.0.0"}-- response := checkUpdatesServer(ctx, "://invalid-url", "", request)- if response != nil {- t.Error("expected nil response for malformed URL")- }- })-}-func TestCreateTempFileForTesting(t *testing.T) {// helper test to ensure temp file creation works for other teststmpDir := os.TempDir()@@ -611,9 +583,6 @@ func TestConstants(t *testing.T) {if DefaultUpdateServerEndpoint == "" {t.Error("DefaultUpdateServerEndpoint should not be empty")}- if UpdatesCheckEndpoint == "" {- t.Error("UpdatesCheckEndpoint should not be empty")- }// test memory and disk constants are reasonableif MinFreeMemGB <= 0 {@@ -657,37 +626,6 @@ func TestGetImageInfoEdgeCases(t *testing.T) {// again, testing without real Docker client}-func TestCheckUpdatesRequestStructure(t *testing.T) {- // test that CheckUpdatesRequest can be marshaled to JSON- request := CheckUpdatesRequest{- InstallerOsType: "darwin",- InstallerVersion: "1.0.0",- LangfuseConnected: true,- LangfuseExternal: false,- ObservabilityConnected: true,- ObservabilityExternal: false,- }-- result := fmt.Sprintf("%+v", request)- if result == "" {- t.Error("CheckUpdatesRequest should be formattable")- }-- // test with pointer fields- imageName := "test-image"- imageTag := "latest"- imageHash := "sha256:abc123"-- request.PentagiImageName = &imageName- request.PentagiImageTag = &imageTag- request.PentagiImageHash = &imageHash-- result = fmt.Sprintf("%+v", request)- if result == "" {- t.Error("CheckUpdatesRequest with pointers should be formattable")- }-}-func TestImageInfoStructure(t *testing.T) {// test ImageInfo structinfo := &ImageInfo{@@ -837,3 +775,5 @@ func TestCheckVolumesExist_MatchingLogic(t *testing.T) {})}}++func (m *mockState) WriteVars(vars map[string]string) error { return nil }diff --git a/backend/cmd/installer/checker/product_info.go b/backend/cmd/installer/checker/product_info.gonew file mode 100644index 00000000..16378b95--- /dev/null+++ b/backend/cmd/installer/checker/product_info.go@@ -0,0 +1,125 @@+package checker++import (+ "bytes"+ "context"+ "encoding/json"+ "io"+ "time"++ "pentagi/cmd/installer/wizard/logger"++ "github.com/moby/moby/api/pkg/stdcopy"+ "github.com/moby/moby/client"+)++// productInfoCommand asks the running product to describe its own state.+const productInfoBinary = "/opt/pentagi/bin/pentagi"++// productInfoTimeout bounds the whole exchange.+//+// This is an optional extra on a gathering the user is waiting for. A product+// that does not answer promptly costs nothing beyond a missing field, and must+// never cost the update check itself.+const productInfoTimeout = 20 * time.Second++// maxProductInfoBytes bounds what is read back.+//+// The contract that carries this field refuses anything larger, so reading more+// would only build a request the service rejects whole — losing the answer for+// every component along with it.+const maxProductInfoBytes = 64 * 1024++// gatherProductInfo asks the running product for its state and returns it exactly+// as it was given.+//+// Nothing here interprets the document. It is read, checked for being JSON at+// all, and carried on; whoever consumes it decides what it means. That is not+// laziness — the installer and the product are released separately, so anything+// the installer understood about the shape would become a second place to update+// whenever the product learns to describe more of itself.+//+// Every failure is silent by design and returns nil: no container, an older build+// without the flag, a product that cannot reach its own database, output that is+// not JSON. The update check proceeds without the field, exactly as it did before+// the field existed.+func (h *defaultCheckHandler) gatherProductInfo(ctx context.Context) json.RawMessage {+ if h.dockerClient == nil {+ return nil+ }++ ctx, cancel := context.WithTimeout(ctx, productInfoTimeout)+ defer cancel()++ container, err := h.dockerClient.ContainerInspect(ctx, PentagiContainerName, client.ContainerInspectOptions{})+ if err != nil {+ return nil+ }+ if container.Container.State == nil || !container.Container.State.Running {+ // A stopped product cannot describe itself, and the description of a+ // stopped product would not be the one anybody wants anyway.+ return nil+ }++ exec, err := h.dockerClient.ExecCreate(ctx, PentagiContainerName, client.ExecCreateOptions{+ Cmd: []string{productInfoBinary, "-info"},+ AttachStdout: true,+ AttachStderr: true,+ })+ if err != nil {+ logger.Debugf("product state: exec create failed: %v", err)+ return nil+ }++ attached, err := h.dockerClient.ExecAttach(ctx, exec.ID, client.ExecAttachOptions{})+ if err != nil {+ logger.Debugf("product state: exec attach failed: %v", err)+ return nil+ }+ defer attached.Close()++ // The stream is MULTIPLEXED. Without a TTY the daemon frames every chunk with+ // an eight-byte header saying which stream it came from and how long it is, so+ // reading the socket directly yields JSON with binary interleaved through it —+ // and the first header lands in front of the opening brace, which makes even a+ // perfectly good document unparsable.+ var stdout, stderr bytes.Buffer+ if _, err := stdcopy.StdCopy(+ &stdout,+ &stderr,+ io.LimitReader(attached.Reader, maxProductInfoBytes+1),+ ); err != nil {+ logger.Debugf("product state: reading the answer failed: %v", err)+ return nil+ }++ // The exit code decides whether the output is an answer or the remains of one.+ // A truncated document that happens to parse would be worse than no document.+ inspected, err := h.dockerClient.ExecInspect(ctx, exec.ID, client.ExecInspectOptions{})+ if err == nil && inspected.ExitCode != 0 {+ logger.Debugf("product state: the product exited with %d", inspected.ExitCode)+ return nil+ }++ return productInfoFromOutput(stdout.Bytes())+}++// productInfoFromOutput decides whether what came back is a document.+//+// Separate from the exchange above so that the decision can be exercised without+// a container: everything that makes this fail in practice — an older build+// printing usage, a half-written answer, an answer larger than the contract+// accepts — is a property of the bytes, not of the daemon.+func productInfoFromOutput(output []byte) json.RawMessage {+ answer := bytes.TrimSpace(output)+ if len(answer) == 0 || len(answer) > maxProductInfoBytes {+ return nil+ }+ if !json.Valid(answer) {+ // An older build has no such flag and prints its usage, or nothing at all.+ // Either way there is no document here.+ return nil+ }++ return json.RawMessage(answer)+}diff --git a/backend/cmd/installer/checker/product_info_test.go b/backend/cmd/installer/checker/product_info_test.gonew file mode 100644index 00000000..4839a460--- /dev/null+++ b/backend/cmd/installer/checker/product_info_test.go@@ -0,0 +1,128 @@+package checker++import (+ "bytes"+ "encoding/binary"+ "encoding/json"+ "strings"+ "testing"++ "github.com/moby/moby/api/pkg/stdcopy"+)++// frame writes one chunk the way the docker daemon does on a non-TTY exec: an+// eight-byte header — the stream number, three zero bytes, then the payload+// length big-endian — followed by the payload.+//+// Written out here rather than taken from a library because the reading half is+// all the client package exposes; the daemon owns the writing half. Spelling the+// format out is also the point of these tests: it is the thing that silently+// corrupts the answer when nobody accounts for it.+func frame(buffer *bytes.Buffer, stream stdcopy.StdType, payload string) {+ var header [8]byte+ header[0] = byte(stream)+ binary.BigEndian.PutUint32(header[4:], uint32(len(payload)))+ buffer.Write(header[:])+ buffer.WriteString(payload)+}++// TestTheExecStreamMustBeDemultiplexedBeforeItIsJSON.+//+// The trap this pins is not hypothetical and it is not visible by reading: the+// daemon frames every chunk of a non-TTY exec with an eight-byte header naming+// the stream and the length. Read the socket directly and the first thing before+// the opening brace is binary, so a perfectly good document does not parse — and+// the symptom is "the product returned nothing", which is also what a product+// without the flag returns.+//+// The test builds the stream the way the daemon does and asserts both halves:+// raw is not JSON, demultiplexed is.+func TestTheExecStreamMustBeDemultiplexedBeforeItIsJSON(t *testing.T) {+ document := `{"schema":1,"version":"0.9.3"}`++ var framed bytes.Buffer+ frame(&framed, stdcopy.Stdout, document)++ if json.Valid(framed.Bytes()) {+ t.Fatal("the framed stream parsed as JSON, so this test proves nothing — " ++ "the framing that makes demultiplexing necessary is not being produced")+ }++ var stdout, stderr bytes.Buffer+ if _, err := stdcopy.StdCopy(&stdout, &stderr, &framed); err != nil {+ t.Fatalf("demultiplex: %v", err)+ }++ info := productInfoFromOutput(stdout.Bytes())+ if info == nil {+ t.Fatal("a demultiplexed document was rejected")+ }+ if string(info) != document {+ t.Errorf("the document was altered on the way through:\n got %s\nwant %s", info, document)+ }+}++// TestStderrNeverReachesTheDocument.+//+// stdout carries the answer and stderr carries diagnostics, and the two arrive+// interleaved on one socket. Mixing them would put a warning inside the document+// — or, worse, produce something that still parses and is no longer what the+// product said.+func TestStderrNeverReachesTheDocument(t *testing.T) {+ var framed bytes.Buffer+ frame(&framed, stdcopy.Stderr, "cannot reach the database\n")+ frame(&framed, stdcopy.Stdout, `{"schema":1}`)++ var stdout, stderr bytes.Buffer+ if _, err := stdcopy.StdCopy(&stdout, &stderr, &framed); err != nil {+ t.Fatalf("demultiplex: %v", err)+ }++ if got := string(productInfoFromOutput(stdout.Bytes())); got != `{"schema":1}` {+ t.Errorf("the document picked up something from stderr: %q", got)+ }+ if !strings.Contains(stderr.String(), "cannot reach the database") {+ t.Error("the diagnostic went somewhere other than stderr")+ }+}++// TestNothingThatIsNotADocumentIsCarried.+//+// Every one of these is a state a real installation is in on the day this ships:+// an older product that has never heard of the flag, one whose answer was cut+// short, one that answered with more than the contract will accept. Carrying any+// of them would build a request the service refuses whole — and refusing it costs+// the answer for every component, not just this field.+func TestNothingThatIsNotADocumentIsCarried(t *testing.T) {+ for name, output := range map[string]string{+ "an older build printing usage": "Usage of /opt/pentagi/bin/pentagi:\n -info\n",+ "nothing at all": "",+ "whitespace only": " \n\t ",+ "a truncated document": `{"schema":1,"version":`,+ "a log line before the answer": "level=info msg=starting\n" + `{"schema":1}`,+ "larger than the contract takes": `{"schema":1,"padding":"` ++ strings.Repeat("x", maxProductInfoBytes) + `"}`,+ } {+ if info := productInfoFromOutput([]byte(output)); info != nil {+ t.Errorf("%s was carried through as a document: %q", name, info)+ }+ }+}++// TestADocumentIsCarriedWithoutBeingUnderstood.+//+// The installer and the product ship separately, so anything the installer knew+// about the shape would be a second place to change every time the product learns+// to describe more of itself. This pins that a document full of fields this build+// has never heard of survives byte for byte.+func TestADocumentIsCarriedWithoutBeingUnderstood(t *testing.T) {+ future := `{"schema":7,"version":"9.9.9","engine":{"scenarios":12},"unheard_of":[1,2,3]}`++ info := productInfoFromOutput([]byte(" " + future + "\n"))+ if info == nil {+ t.Fatal("a document from a newer product was dropped")+ }+ if string(info) != future {+ t.Errorf("the document was reshaped on the way through:\n got %s\nwant %s", info, future)+ }+}diff --git a/backend/cmd/installer/checker/updates.go b/backend/cmd/installer/checker/updates.gonew file mode 100644index 00000000..129768a2--- /dev/null+++ b/backend/cmd/installer/checker/updates.go@@ -0,0 +1,627 @@+package checker++import (+ "context"+ "errors"+ "runtime"+ "strings"+ "time"++ "pentagi/cmd/installer/cloud"+ "pentagi/cmd/installer/state"+ "pentagi/cmd/installer/wizard/logger"++ "github.com/vxcontrol/cloud/models"+)++// StackUpdate is what the update server said about one product stack.+//+// HasUpdate is the stack-level verdict and the only thing the menu needs. Everything else+// is for the screen that explains the update before it is applied: which version is+// current, which is offered, and what the release says about the difference.+type StackUpdate struct {+ Stack string `json:"stack" yaml:"stack"`+ HasUpdate bool `json:"has_update" yaml:"has_update"`+ CurrentVersion string `json:"current_version,omitempty" yaml:"current_version,omitempty"`+ LatestVersion string `json:"latest_version,omitempty" yaml:"latest_version,omitempty"`+ // CurrentVersionMixed says the stack's components came from DIFFERENT releases, so+ // CurrentVersion names the oldest of them rather than a version this installation as+ // a whole ever was. Legitimate — a component nobody rebuilt stays on its old release+ // — but "you are on 2.1.0" and "the oldest thing you have is from 2.1.0" are+ // different sentences and the overview must not print the first when it means the+ // second.+ CurrentVersionMixed bool `json:"current_version_mixed,omitempty" yaml:"current_version_mixed,omitempty"`+ // Changelog and ReleaseNotes carry the TARGET release's text only. They are the+ // fallback for a server that predates Releases; when Releases is present its last+ // entry carries the same text.+ Changelog string `json:"changelog,omitempty" yaml:"changelog,omitempty"`+ ReleaseNotes string `json:"release_notes,omitempty" yaml:"release_notes,omitempty"`+ // Releases is every curated release this update crosses, oldest first, the last one+ // being the target. Empty when the server did not send any — either it is older than+ // the field, or there is genuinely nothing to cross.+ Releases []ReleaseSummary `json:"releases,omitempty" yaml:"releases,omitempty"`+ // ReleasesTruncated says older entries were dropped by the server.+ ReleasesTruncated bool `json:"releases_truncated,omitempty" yaml:"releases_truncated,omitempty"`+ // Resolution says HOW the server arrived at this answer — release, channel,+ // ahead_of_release, no_artifact_for_tag, not_tracked. It is DIAGNOSTIC ONLY:+ // nothing here may branch on it, and it exists so `has_update: false` can be+ // read in a log instead of guessed at.+ Resolution string `json:"resolution,omitempty" yaml:"resolution,omitempty"`+ Components []ComponentUpdate `json:"components,omitempty" yaml:"components,omitempty"`+}++// ReleaseSummary is one curated release crossed by applying an update.+type ReleaseSummary struct {+ Version string `json:"version" yaml:"version"`+ IsStable bool `json:"is_stable" yaml:"is_stable"`+ ReleasedAt string `json:"released_at,omitempty" yaml:"released_at,omitempty"`+ Changelog string `json:"changelog,omitempty" yaml:"changelog,omitempty"`+ ReleaseNotes string `json:"release_notes,omitempty" yaml:"release_notes,omitempty"`+}++// ComponentUpdate is one artefact inside a stack, and what would change for it.+//+// Being listed does NOT mean the artefact is outdated. Under the stable strategy the+// answer names every artefact of the release that matches something reported, so the+// installation can be attributed to a version even when nothing needs doing. Outdated is+// the conclusion drawn here by comparing digests.+type ComponentUpdate struct {+ Component string `json:"component" yaml:"component"`+ OS string `json:"os" yaml:"os"`+ Arch string `json:"arch" yaml:"arch"`+ Repository string `json:"repository,omitempty" yaml:"repository,omitempty"`+ Tag string `json:"tag,omitempty" yaml:"tag,omitempty"`++ // Action is what the SERVER says to do — current, install, upgrade,+ // downgrade or unknown — and it is the authority. Membership in the answer+ // is not: under a curated release every artefact matching a reported+ // component is listed, whether or not it differs from what is installed.+ //+ // Reason explains an `unknown`, and is empty for every other action. It is+ // the difference between "nothing to do" and "we publish nothing for what you+ // are running", which are opposite situations that used to arrive identically.+ Action string `json:"action,omitempty" yaml:"action,omitempty"`+ Reason string `json:"reason,omitempty" yaml:"reason,omitempty"`+ // PullReference is what to write into the compose variable before pulling,+ // ready to use. It is NOT always `repository:tag` — which tag to pull is the+ // cloud's decision and varies with the update strategy, so composing one here+ // out of the repository and tag fields would override the answer with a guess.+ PullReference string `json:"pull_reference,omitempty" yaml:"pull_reference,omitempty"`++ // Outdated is true only when the installed artefact is known AND differs from the+ // offered one.+ Outdated bool `json:"outdated" yaml:"outdated"`+ // Verifiable is false when there is nothing on THIS side to compare: nothing is+ // installed yet, or the server answered `unknown` and there is no artefact at all. An+ // unverifiable component is not a mismatch, and saying so is the difference between+ // "this will change" and "we cannot tell".+ //+ // It says nothing about whether the ANSWER carried a digest we can match — the server+ // compared against what we reported and its action is the authority either way.+ Verifiable bool `json:"verifiable" yaml:"verifiable"`+ CurrentVersion string `json:"current_version,omitempty" yaml:"current_version,omitempty"`++ // TargetVersion is the VERSION of the offered artefact, and only that. Files have+ // one; images do not (the cloud identifies their builds by digest), so it stays empty+ // for them. It is what the self-update download asks for as `?version=`.+ TargetVersion string `json:"target_version,omitempty" yaml:"target_version,omitempty"`++ // TargetDigest is the sha256 an updated artefact should end up carrying, in the same+ // identity the installed side reports: the config digest for an image, the file hash+ // for a file.+ //+ // Separate from TargetVersion because the two are not interchangeable and were once+ // the same field. Post-update verification compares this against what is on disk, and+ // with a version in it the Jaeger plugin — which has a hash and no version at all —+ // reported a mismatch after every successful update: "expected 0.13.0, got a423c6…".+ //+ // Empty means the cloud did not name a digest, and verification degrades to "nothing+ // to compare" rather than inventing a mismatch.+ TargetDigest string `json:"target_digest,omitempty" yaml:"target_digest,omitempty"`+}++// InstalledComponent is what the last update check reported about one artefact on this+// machine. It is kept because verifying an update afterwards needs to know what was+// installed, and re-inspecting every container a second time would ask Docker the same+// questions the check already asked.+type InstalledComponent struct {+ Component string `json:"component" yaml:"component"`+ OS string `json:"os" yaml:"os"`+ Arch string `json:"arch" yaml:"arch"`+ Status string `json:"status" yaml:"status"`+ Repository string `json:"repository,omitempty" yaml:"repository,omitempty"`+ Tag string `json:"tag,omitempty" yaml:"tag,omitempty"`+ // Digest is the image config digest, or the file digest for components that ship as+ // files. Empty when nothing observable was installed.+ Digest string `json:"digest,omitempty" yaml:"digest,omitempty"`+ Version string `json:"version,omitempty" yaml:"version,omitempty"`+}++// UpdateCheckFailure explains why the update check produced no answer, in terms the+// interface can act on. A bare "server unavailable" tells the user nothing they can do+// about it — a spent daily quota and a broken proxy call for opposite reactions.+type UpdateCheckFailure struct {+ Reason string `json:"reason" yaml:"reason"`+ Message string `json:"message" yaml:"message"`+ // RetryAfter is the server-advertised cooldown; zero when there is none, either+ // because the server did not say or because waiting will not help.+ RetryAfter time.Duration `json:"retry_after" yaml:"retry_after"`+ // Retryable separates "come back later" from "this needs a different license".+ Retryable bool `json:"retryable" yaml:"retryable"`+}++// updateStackFlags maps a contract stack onto the per-stack flag the menus already read,+// so the answer changes what those flags say without the screens having to change.+type stackFlag struct {+ stack models.ProductStack+ set func(c *CheckResult, upToDate bool)+}++var updateStackFlags = []stackFlag{+ {models.ProductStackPentagi, func(c *CheckResult, v bool) { c.PentagiIsUpToDate = v }},+ {models.ProductStackWorker, func(c *CheckResult, v bool) { c.WorkerIsUpToDate = v }},+ {models.ProductStackInstaller, func(c *CheckResult, v bool) { c.InstallerIsUpToDate = v }},+ {models.ProductStackGraphiti, func(c *CheckResult, v bool) { c.GraphitiIsUpToDate = v }},+ {models.ProductStackLangfuse, func(c *CheckResult, v bool) { c.LangfuseIsUpToDate = v }},+ {models.ProductStackObservability, func(c *CheckResult, v bool) { c.ObservabilityIsUpToDate = v }},+}++// GatherUpdatesInfo asks the update server what is available for this installation.+//+// It never fails the surrounding gather: an update check that cannot be made is a fact+// about the installation, recorded in the result, not a reason to abandon everything else+// the checker learned.+func (h *defaultCheckHandler) GatherUpdatesInfo(ctx context.Context, c *CheckResult) error {+ h.mx.Lock()+ defer h.mx.Unlock()++ client, err := h.updateClient()+ if err != nil {+ c.recordUpdateFailure(err)+ return nil+ }+ if warning := client.LicenseWarning(); warning != nil {+ // The check still runs, just without the license — say so, because otherwise the+ // only symptom is a tier the user did not expect.+ logger.Warnf("update check: %v", warning)+ }++ components := h.gatherComponents(ctx, c)+ // Recorded before the call: what is installed is a local fact, true whether or not the+ // server answers, and the verification after an update needs it either way.+ c.recordInstalledComponents(components)++ request := models.CheckUpdatesRequest{+ // The installer is a binary for THIS machine, so its platform is the host's.+ // Every other component describes an image and carries its own.+ InstallerVersion: cloud.NormalizeVersion(InstallerVersion),+ InstallerOS: models.OSType(runtime.GOOS),+ InstallerArch: models.ArchType(runtime.GOARCH),+ Strategy: updateStrategy(h.appState),+ Images: components.images,+ Files: components.files,+ // Reported for its own sake: nothing in the answer depends on it. A stack+ // deployed elsewhere contributes no components at all — there is no+ // reference to resolve — so without this the service cannot tell "runs+ // Langfuse on their own cluster" from "does not use Langfuse".+ Stacks: stackStatuses(c),+ // What the product says about itself, carried through untouched. Absent+ // whenever the product is not running or cannot be asked, which is why it+ // is gathered last and why nothing below depends on it.+ Info: h.gatherProductInfo(ctx),+ }++ response, err := client.CheckUpdates(ctx, request)+ if err != nil {+ c.recordUpdateFailure(err)+ return nil+ }++ c.applyUpdateAnswer(response, components)++ return nil+}++// updateClient builds the client for this installation's configuration.+func (h *defaultCheckHandler) updateClient() (*cloud.Client, error) {+ defaultUpdateServerHost := strings.TrimPrefix(strings.TrimSuffix(DefaultUpdateServerEndpoint, "/"), "https://")+ return cloud.New(cloud.Config{+ Host: getEnvVar(h.appState, "UPDATE_SERVER_HOST", defaultUpdateServerHost),+ ProxyURL: getProxyURL(h.appState),+ LicenseKey: getEnvVar(h.appState, "LICENSE_KEY", ""),+ InstallerVersion: InstallerVersion,+ })+}++// DefaultUpdateStrategy is the channel an installation follows when it has not+// been told otherwise.+//+// `preview` rather than `stable`, and the difference is not caution versus+// eagerness. Under `stable` the answer is measured against curated releases, and+// an installation whose stack has no published release is offered nothing at all+// — which reads to the user as "no updates exist" rather than "we are not+// publishing releases for this yet". `preview` follows the channels the client is+// already on, which is what an installation actually wants until releases are a+// thing it can rely on.+const DefaultUpdateStrategy = models.UpdateStrategyPreview++// updateStrategy reads which release channel to follow. An unrecognised value falls back+// to the default rather than failing the check, and says so: a typo should not leave an+// installation unable to learn about security updates.+func updateStrategy(appState state.State) models.UpdateStrategy {+ configured := strings.ToLower(strings.TrimSpace(getEnvVar(appState, "UPDATE_STRATEGY", "")))+ if configured == "" {+ return DefaultUpdateStrategy+ }++ strategy := models.UpdateStrategy(configured)+ if err := strategy.Valid(); err != nil {+ logger.Warnf("unknown UPDATE_STRATEGY %q, falling back to %s", configured, DefaultUpdateStrategy)+ return DefaultUpdateStrategy+ }++ return strategy+}++// recordInstalledComponents stores what the gathering observed on this machine.+func (c *CheckResult) recordInstalledComponents(components reportedComponents) {+ c.InstalledComponents = make([]InstalledComponent, 0, components.total())+ for _, component := range components.images {+ c.InstalledComponents = append(c.InstalledComponents, InstalledComponent{+ Component: component.Component.String(),+ OS: component.OS.String(),+ Arch: component.Arch.String(),+ Status: component.Status.String(),+ Repository: component.Repository,+ Tag: component.Tag,+ Digest: derefString(component.ImageHash),+ })+ }+ for _, component := range components.files {+ c.InstalledComponents = append(c.InstalledComponents, InstalledComponent{+ Component: component.Component.String(),+ OS: component.OS.String(),+ Arch: component.Arch.String(),+ Status: component.Status.String(),+ Digest: derefString(component.FileHash),+ Version: derefString(component.Version),+ })+ }+}++// recordUpdateFailure stores why the check produced nothing and resets every per-stack+// flag.+//+// Resetting ALL of them matters: leaving one behind means a stack keeps whatever verdict+// an earlier, successful check left in it, and the interface presents a stale answer as a+// current one. The worker flag used to be the one left behind.+func (c *CheckResult) recordUpdateFailure(err error) {+ c.UpdateServerAccessible = false+ c.StackUpdates = nil+ for _, flag := range updateStackFlags {+ flag.set(c, false)+ }++ failure := &UpdateCheckFailure{+ Reason: string(cloud.FailureUnreachable),+ Message: err.Error(),+ }+ var classified *cloud.Failure+ if errors.As(err, &classified) {+ failure.Reason = string(classified.Reason)+ failure.RetryAfter = classified.RetryAfter+ failure.Retryable = classified.Reason.Retryable()+ }+ c.UpdateFailure = failure++ logger.Warnf("update check failed (%s): %v", failure.Reason, err)+}++// applyUpdateAnswer turns the server's answer into the per-stack verdicts the menus read+// and the detail the update screen shows.+//+// A stack missing from the answer stays "up to date": the server reports on what it was+// asked about, and no news is not the same as bad news. Offering an update nobody has+// evidence for is the worse failure.+func (c *CheckResult) applyUpdateAnswer(response *models.CheckUpdatesResponse, reported reportedComponents) {+ c.UpdateServerAccessible = true+ c.UpdateFailure = nil+ for _, flag := range updateStackFlags {+ flag.set(c, true)+ }++ installed := make(map[string]installedArtefact, reported.total())+ for _, component := range reported.images {+ installed[componentKey(component.Component, component.OS, component.Arch)] = installedArtefact{+ digest: component.ImageHash,+ }+ }+ for _, component := range reported.files {+ installed[componentKey(component.Component, component.OS, component.Arch)] = installedArtefact{+ digest: component.FileHash,+ version: component.Version,+ }+ }++ c.StackUpdates = make([]StackUpdate, 0, len(response.Updates))+ for _, update := range response.Updates {+ for _, flag := range updateStackFlags {+ if flag.stack == update.Stack {+ flag.set(c, !update.HasUpdate)+ }+ }+ c.StackUpdates = append(c.StackUpdates, buildStackUpdate(update, installed))+ }+}++// installedArtefact is what is known about an artefact on this machine, in the one shape+// both kinds share: something to compare an offer against.+type installedArtefact struct {+ digest *string+ version *string+}++func buildStackUpdate(update models.UpdateInfo, installed map[string]installedArtefact) StackUpdate {+ stack := StackUpdate{+ Stack: update.Stack.String(),+ HasUpdate: update.HasUpdate,+ CurrentVersion: derefString(update.CurrentVersion),+ CurrentVersionMixed: update.CurrentVersionMixed,+ LatestVersion: derefString(update.LatestVersion),+ Changelog: derefString(update.Changelog),+ ReleaseNotes: derefString(update.ReleaseNotes),+ ReleasesTruncated: update.ReleasesTruncated,+ Resolution: string(update.Resolution),+ }++ // Copied rather than referenced: CheckResult is serialised to the state file, and a+ // time.Time there would round-trip through a format nothing else in this struct uses.+ // The overview only ever prints the date.+ for _, release := range update.Releases {+ summary := ReleaseSummary{+ Version: release.Version,+ IsStable: release.IsStable,+ Changelog: release.Changelog,+ ReleaseNotes: release.ReleaseNotes,+ }+ if release.ReleasedAt != nil {+ summary.ReleasedAt = release.ReleasedAt.UTC().Format(time.RFC3339)+ }+ stack.Releases = append(stack.Releases, summary)+ }++ stack.Components = make([]ComponentUpdate, 0, len(update.Images)+len(update.Files))+ for _, offered := range update.Images {+ current := installed[componentKey(offered.Component, offered.OS, offered.Arch)]+ stack.Components = append(stack.Components, buildImageComponentUpdate(offered, current))+ }+ for _, offered := range update.Files {+ current := installed[componentKey(offered.Component, offered.OS, offered.Arch)]+ stack.Components = append(stack.Components, buildFileComponentUpdate(offered, current))+ }++ // Logged and nothing more. `resolution` explains an answer — "you are ahead+ // of the curated set", "we track nothing under that reference" — and the one+ // thing it must never do is drive a decision here: it is diagnostic by+ // contract, and a client branching on it would couple itself to wording the+ // server is free to extend.+ if stack.Resolution != "" {+ logger.Debugf("update check: stack %s resolved as %s (has_update=%t)",+ stack.Stack, stack.Resolution, stack.HasUpdate)+ }+ for _, component := range stack.Components {+ if component.Reason != "" {+ logger.Debugf("update check: %s is %s — %s",+ component.Component, component.Action, component.Reason)+ }+ }++ return stack+}++// buildComponentUpdate decides whether one artefact would actually change.+//+// Images are compared by digest, not by tag: a moving tag such as "latest" points at a+// different image over time, so the tag alone says nothing. Binaries are compared by+// version, which is how the server identifies their builds.+//+// A comparison that cannot be made is reported as unverifiable rather than as a+// difference. The server omits a digest it does not know, and treating an absent value as+// a mismatch would offer an update on no evidence at all.+func buildImageComponentUpdate(offered models.ImageUpdate, current installedArtefact) ComponentUpdate {+ component := ComponentUpdate{+ Component: offered.Component.String(),+ OS: offered.OS.String(),+ Arch: offered.Arch.String(),+ Repository: offered.Repository,+ Tag: offered.Tag,+ Action: string(offered.Action),+ PullReference: offered.PullReference,+ }+ if offered.Reason != nil {+ component.Reason = string(*offered.Reason)+ }++ // The target an updated image should end up carrying is the offered CONFIG+ // digest: the installed side of every comparison is normalizeDigest(image.ID),+ // which is `docker inspect` → Id, and ConfigHash is the same identity as the+ // server resolved it (cloud/models: "this is the value to verify a pulled+ // image against"). Without this the post-update verification had a target of+ // "" for every image, routed each one to "not verifiable", and then printed+ // the pass line for an update that may not have taken.+ //+ // When the server does not know the config digest the target stays empty and+ // verification degrades honestly to "cannot verify" — never to a mismatch.+ //+ // TargetVersion is deliberately left empty here: an image has no version of its+ // own in the answer, and the field means a version everywhere else.+ if offered.ConfigHash != nil {+ component.TargetDigest = *offered.ConfigHash+ }++ // Nothing installed means nothing to compare, which is not a mismatch: it is the+ // state of a component that has never been pulled, and the answer names what to pull+ // rather than a difference to reconcile.+ if current.digest != nil {+ component.CurrentVersion = *current.digest+ component.Verifiable = true+ component.Outdated = !offered.CarriesDigest(*current.digest)+ }++ // The server's own verdict wins where it is more informed than a digest+ // comparison can be, and it always is: it compared against the digest THIS+ // client reported, knowing every identity the artefact has.+ //+ // `install` is the case that matters most: nothing is pulled, so there is no+ // digest to compare and the loop above leaves `Outdated` false — which reads+ // as "nothing to do" for the one component that most needs doing. `unknown`+ // is the mirror image: there is no artefact at all, so whatever a comparison+ // produced means nothing.+ //+ // `current` is here because the comparison above can disagree with it and be+ // wrong. Docker exposes three sha256 identities per image and this client+ // reports the config digest; an answer that names only the manifest digest+ // therefore matches nothing locally, and `CarriesDigest` returns false for an+ // installation that is exactly up to date. Without this branch the overview+ // tells the user a component will be updated while the server has just said+ // it will not.+ switch offered.Action {+ case models.ComponentActionInstall, models.ComponentActionUpgrade, models.ComponentActionDowngrade:+ component.Outdated = true+ case models.ComponentActionCurrent:+ component.Outdated = false+ case models.ComponentActionUnknown:+ component.Outdated = false+ component.Verifiable = false+ }++ return component+}++// buildFileComponentUpdate is the file half. Files are compared by version, which is how+// the server identifies their builds, and by hash when no version was reported — which is+// the Jaeger plugin's case, since it ships without one.+func buildFileComponentUpdate(offered models.FileUpdate, current installedArtefact) ComponentUpdate {+ component := ComponentUpdate{+ Component: offered.Component.String(),+ OS: offered.OS.String(),+ Arch: offered.Arch.String(),+ Action: string(offered.Action),+ // A file carries both, and they describe different sides. The version is the+ // OFFERED build's, and it is always present — a published binary cannot exist+ // without one — and it is what the download asks for, since the package request+ // requires a version and has no field for a hash. The hash is what the result is+ // verified against, and it is the only identity the INSTALLED side has for a+ // plugin: a file on disk carries no version metadata. Conflating the two is what+ // made a correctly updated plugin report "expected 0.13.0, got a423c6…".+ TargetVersion: offered.Version,+ TargetDigest: offered.FileHash,+ }+ if offered.Reason != nil {+ component.Reason = string(*offered.Reason)+ }++ switch {+ case current.version != nil:+ component.CurrentVersion = *current.version+ component.Verifiable = true+ component.Outdated = offered.Version != *current.version++ case current.digest != nil:+ component.CurrentVersion = *current.digest+ component.Verifiable = true+ component.Outdated = offered.FileHash != *current.digest+ }++ // Same rule as the image half, for the same reason: the server's action is the+ // authority, because it compared against what this client reported. A file that+ // was never installed reports neither a version nor a digest, so both branches+ // above are skipped; and a `current` verdict has to survive a version string+ // that merely spells the same build differently.+ switch offered.Action {+ case models.ComponentActionInstall, models.ComponentActionUpgrade, models.ComponentActionDowngrade:+ component.Outdated = true+ case models.ComponentActionCurrent:+ component.Outdated = false+ case models.ComponentActionUnknown:+ component.Outdated = false+ component.Verifiable = false+ }++ return component+}++// componentKey identifies an artefact across the request and the answer. The platform is+// part of it: the Jaeger plugin is reported twice, once per architecture, and matching on+// the component name alone would collapse the two.+func componentKey(component models.ComponentType, os models.OSType, arch models.ArchType) string {+ return component.String() + "/" + os.String() + "/" + arch.String()+}++func derefString(value *string) string {+ if value == nil {+ return ""+ }+ return *value+}++func firstNonEmpty(values ...string) string {+ for _, value := range values {+ if value != "" {+ return value+ }+ }+ return ""+}++// stackStatuses says how each product stack is deployed on this machine.+//+// Every stack is reported, including the unused ones: silence about a stack is+// indistinguishable from an installer too old to speak about stacks at all, and+// the difference matters to whoever reads the numbers.+//+// The precedence between installed and external is deliberate. `external` is the+// more specific statement — it says who OPERATES the stack — and an installation+// pointing at somebody else's Langfuse may well also have the compose file on+// disk.+func stackStatuses(c *CheckResult) []models.StackInfo {+ state := func(connected, installed, external bool) models.StackStatus {+ switch {+ case !connected:+ return models.StackStatusUnused+ case external:+ return models.StackStatusExternal+ case installed:+ return models.StackStatusInstalled+ default:+ return models.StackStatusConnected+ }+ }+ worker := models.StackStatusUnused+ if c.WorkerImageExists {+ worker = models.StackStatusInstalled+ }+ return []models.StackInfo{+ // The product itself and the installer asking the question: both present+ // by definition, or this check would not be running.+ {Stack: models.ProductStackPentagi, Status: models.StackStatusInstalled},+ {Stack: models.ProductStackInstaller, Status: models.StackStatusInstalled},+ {Stack: models.ProductStackWorker, Status: worker},+ {Stack: models.ProductStackGraphiti, Status: state(+ c.GraphitiConnected, c.GraphitiInstalled, c.GraphitiExternal)},+ {Stack: models.ProductStackLangfuse, Status: state(+ c.LangfuseConnected, c.LangfuseInstalled, c.LangfuseExternal)},+ {Stack: models.ProductStackObservability, Status: state(+ c.ObservabilityConnected, c.ObservabilityInstalled, c.ObservabilityExternal)},+ // Not deployed by compose, so the installer has nothing to observe about+ // them — and `undefined` is the value that says exactly that.+ //+ // They used to be reported as `unused`, on the reasoning that saying+ // something beats saying nothing. That reasoning held only while silence+ // and `unused` were recorded identically: it made a placeholder for "we+ // cannot tell" indistinguishable from a measured negative, and every count+ // of engine adoption was reading a hardcoded constant.+ {Stack: models.ProductStackEngine, Status: models.StackStatusUndefined},+ {Stack: models.ProductStackBrowser, Status: models.StackStatusUndefined},+ }+}diff --git a/backend/cmd/installer/checker/updates_test.go b/backend/cmd/installer/checker/updates_test.gonew file mode 100644index 00000000..4f869d1f--- /dev/null+++ b/backend/cmd/installer/checker/updates_test.go@@ -0,0 +1,651 @@+package checker++import (+ "errors"+ "strings"+ "sync"+ "testing"+ "time"++ "pentagi/cmd/installer/cloud"+ "pentagi/cmd/installer/loader"++ "github.com/vxcontrol/cloud/models"+ "github.com/vxcontrol/cloud/sdk"+)++func ptr[T any](v T) *T { return &v }++func digest(fill string) string {+ return strings.Repeat(fill, 64/len(fill))+}++// TestUpdateFailureResetsEveryStackFlag is the regression for the branch that used to miss+// one flag.+//+// The flags are read as "up to date, nothing to offer". A flag left at true from an earlier+// successful check keeps saying that after a check that never happened, and the interface+// presents a stale verdict as a current one — with nothing on screen to suggest it is old.+func TestUpdateFailureResetsEveryStackFlag(t *testing.T) {+ result := &CheckResult{+ UpdateServerAccessible: true,+ PentagiIsUpToDate: true,+ GraphitiIsUpToDate: true,+ LangfuseIsUpToDate: true,+ ObservabilityIsUpToDate: true,+ InstallerIsUpToDate: true,+ WorkerIsUpToDate: true,+ StackUpdates: []StackUpdate{{Stack: "pentagi"}},+ }++ result.recordUpdateFailure(errors.New("dial tcp: no route to host"))++ flags := map[string]bool{+ "pentagi": result.PentagiIsUpToDate,+ "graphiti": result.GraphitiIsUpToDate,+ "langfuse": result.LangfuseIsUpToDate,+ "observability": result.ObservabilityIsUpToDate,+ "installer": result.InstallerIsUpToDate,+ "worker": result.WorkerIsUpToDate,+ }+ for stack, upToDate := range flags {+ if upToDate {+ t.Errorf("stack %s kept its verdict from an earlier check after the check failed", stack)+ }+ }++ if result.UpdateServerAccessible {+ t.Error("the server is recorded as reachable after a failed check")+ }+ if result.StackUpdates != nil {+ t.Error("the previous answer is still on the result")+ }+ if result.UpdateFailure == nil {+ t.Fatal("no failure was recorded")+ }+}++// TestUpdateFailureCarriesWhatTheUserCanActOn: a spent quota and a broken proxy call for+// opposite reactions, so the reason and the cooldown have to survive into the result.+func TestUpdateFailureCarriesWhatTheUserCanActOn(t *testing.T) {+ tests := []struct {+ name string+ err error+ wantReason cloud.FailureReason+ wantRetry time.Duration+ wantRetryable bool+ }{+ {+ name: "daily quota says when it resets",+ err: cloud.Classify(&sdk.QuotaError{Err: sdk.ErrQuotaExceededDaily, Scope: sdk.QuotaScopeDaily, RetryAfter: 2 * time.Hour}),+ wantReason: cloud.FailureQuotaExceeded,+ wantRetry: 2 * time.Hour,+ wantRetryable: true,+ },+ {+ name: "a tier without access is not a wait",+ err: cloud.Classify(sdk.ErrForbidden),+ wantReason: cloud.FailureForbidden,+ wantRetryable: false,+ },+ {+ name: "an unclassified error is still a failure",+ err: errors.New("something else entirely"),+ wantReason: cloud.FailureUnreachable,+ wantRetryable: false,+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ result := &CheckResult{}+ result.recordUpdateFailure(tt.err)++ failure := result.UpdateFailure+ if failure == nil {+ t.Fatal("no failure recorded")+ }+ if failure.Reason != string(tt.wantReason) {+ t.Errorf("reason = %q, want %q", failure.Reason, tt.wantReason)+ }+ if failure.RetryAfter != tt.wantRetry {+ t.Errorf("retry after = %v, want %v", failure.RetryAfter, tt.wantRetry)+ }+ if failure.Retryable != tt.wantRetryable {+ t.Errorf("retryable = %t, want %t", failure.Retryable, tt.wantRetryable)+ }+ if failure.Message == "" {+ t.Error("the failure carries no message for the user")+ }+ })+ }+}++// TestStackAbsentFromTheAnswerStaysUpToDate: the server reports on what it was asked+// about, so silence about a stack is not evidence of an update. Offering one anyway sends+// the user through an update that changes nothing.+func TestStackAbsentFromTheAnswerStaysUpToDate(t *testing.T) {+ result := &CheckResult{}+ answer := &models.CheckUpdatesResponse{+ Updates: []models.UpdateInfo{+ {Stack: models.ProductStackPentagi, HasUpdate: true},+ },+ }++ result.applyUpdateAnswer(answer, reportedComponents{})++ if result.PentagiIsUpToDate {+ t.Error("pentagi has an update but is reported as up to date")+ }+ for stack, upToDate := range map[string]bool{+ "graphiti": result.GraphitiIsUpToDate,+ "langfuse": result.LangfuseIsUpToDate,+ "observability": result.ObservabilityIsUpToDate,+ "installer": result.InstallerIsUpToDate,+ "worker": result.WorkerIsUpToDate,+ } {+ if !upToDate {+ t.Errorf("stack %s is offered an update the server never mentioned", stack)+ }+ }+ if !result.UpdateServerAccessible || result.UpdateFailure != nil {+ t.Error("a successful check is recorded as a failure")+ }+}++// TestComponentIsOutdatedOnlyWhenTheDigestsActuallyDiffer covers the distinction the+// update screen is built on. Under the stable strategy the answer lists every artefact of+// the release that matches something reported, whether or not it differs — so membership+// is not a work list, and a digest comparison is the only thing that says otherwise.+func TestComponentIsOutdatedOnlyWhenTheDigestsActuallyDiffer(t *testing.T) {+ installedDigest := digest("a")++ tests := []struct {+ name string+ offered models.ImageUpdate+ installed installedArtefact+ wantOutdated bool+ wantVerifiable bool+ }{+ {+ name: "config digest matches what is installed",+ offered: models.ImageUpdate{+ Component: models.ComponentTypePentagi, OS: models.OSTypeLinux, Arch: models.ArchTypeAMD64,+ Repository: "vxcontrol/pentagi", Tag: "latest",+ ImageHash: digest("z"), ConfigHash: ptr(installedDigest),+ },+ installed: installedArtefact{digest: ptr(installedDigest)},+ wantOutdated: false,+ wantVerifiable: true,+ },+ {+ // The server names an artefact by any of three digest identities, and a+ // client compares whichever one its daemon exposes. Matching any single one+ // is enough to conclude "I already have this".+ name: "index digest matches even though the config digest does not",+ offered: models.ImageUpdate{+ Component: models.ComponentTypePentagi, OS: models.OSTypeLinux, Arch: models.ArchTypeAMD64,+ Repository: "vxcontrol/pentagi", Tag: "latest",+ ImageHash: digest("z"), ConfigHash: ptr(digest("b")), IndexHash: ptr(installedDigest),+ },+ installed: installedArtefact{digest: ptr(installedDigest)},+ wantOutdated: false,+ wantVerifiable: true,+ },+ {+ name: "a genuinely different image",+ offered: models.ImageUpdate{+ Component: models.ComponentTypePentagi, OS: models.OSTypeLinux, Arch: models.ArchTypeAMD64,+ Repository: "vxcontrol/pentagi", Tag: "latest",+ ImageHash: digest("z"), ConfigHash: ptr(digest("c")),+ },+ installed: installedArtefact{digest: ptr(installedDigest)},+ wantOutdated: true,+ wantVerifiable: true,+ },+ {+ name: "nothing installed to compare against",+ offered: models.ImageUpdate{+ Component: models.ComponentTypePentagi, OS: models.OSTypeLinux, Arch: models.ArchTypeAMD64,+ Repository: "vxcontrol/pentagi", Tag: "latest",+ ImageHash: digest("z"), ConfigHash: ptr(digest("c")),+ },+ installed: installedArtefact{},+ wantOutdated: false,+ wantVerifiable: false,+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ got := buildImageComponentUpdate(tt.offered, tt.installed)++ if got.Outdated != tt.wantOutdated {+ t.Errorf("outdated = %t, want %t", got.Outdated, tt.wantOutdated)+ }+ if got.Verifiable != tt.wantVerifiable {+ t.Errorf("verifiable = %t, want %t", got.Verifiable, tt.wantVerifiable)+ }+ })+ }+}++// TestAnOutdatedImageCarriesTheTargetVerificationNeeds.+//+// The post-update verification compares what `docker inspect` reports after the+// pull (the config digest) against ComponentUpdate.TargetVersion. Without this+// field every outdated image had a target of "", was routed to "not verifiable",+// and the verification printed its pass line for an update that may not have+// taken — while the same CheckResult still said HasUpdate for the stack.+//+// The only test of that comparison used to hand-write a ComponentUpdate with+// TargetVersion filled — a shape the real producer could not emit.+func TestAnOutdatedImageCarriesTheTargetVerificationNeeds(t *testing.T) {+ installedDigest, offeredConfig := digest("a"), digest("b")++ outdated := buildImageComponentUpdate(models.ImageUpdate{+ Component: models.ComponentTypePentagi, OS: models.OSTypeLinux, Arch: models.ArchTypeAMD64,+ Repository: "vxcontrol/pentagi", Tag: "latest",+ ImageHash: digest("m"), ConfigHash: ptr(offeredConfig),+ }, installedArtefact{digest: ptr(installedDigest)})++ if !outdated.Outdated {+ t.Fatalf("fixture broken: the digests differ, the component must be outdated")+ }+ if outdated.TargetDigest != offeredConfig {+ t.Errorf("TargetDigest = %q, want the offered config digest %q — the installed side of"++ " verification is docker inspect's Id, and only the config digest can ever equal it",+ outdated.TargetDigest, offeredConfig)+ }+ if outdated.TargetVersion != "" {+ t.Errorf("TargetVersion = %q for an image; the field means a VERSION everywhere else,"++ " and an image has none of its own in the answer", outdated.TargetVersion)+ }++ // No config digest known server-side: the target stays empty so verification+ // degrades to "cannot verify" rather than comparing against nothing.+ unknowable := buildImageComponentUpdate(models.ImageUpdate{+ Component: models.ComponentTypePentagi, OS: models.OSTypeLinux, Arch: models.ArchTypeAMD64,+ Repository: "vxcontrol/pentagi", Tag: "latest",+ ImageHash: digest("m"),+ }, installedArtefact{digest: ptr(installedDigest)})+ if unknowable.TargetDigest != "" {+ t.Errorf("TargetDigest = %q for an image whose config digest the server does not know;"++ " inventing one turns \"cannot verify\" into a false mismatch", unknowable.TargetDigest)+ }+}++// TestAFileCarriesBothItsVersionAndItsHash.+//+// The two are not interchangeable and used to be one field, which is how the Jaeger plugin+// — a file with a hash and NO version at all — ended up verified as "expected 0.13.0, got+// a423c6…" after every successful update.+//+// Both are needed and for different things: the download asks for the version+// (`?version=`), and the result is checked against the hash, because the hash is the only+// identity the installed side can report for a file on disk.+func TestAFileCarriesBothItsVersionAndItsHash(t *testing.T) {+ offeredHash := digest("plugin-0-13-0")++ plugin := buildFileComponentUpdate(models.FileUpdate{+ Component: models.ComponentTypeJaegerClickhouse, OS: models.OSTypeLinux,+ Arch: models.ArchTypeAMD64, Version: "0.13.0", FileHash: offeredHash,+ Action: models.ComponentActionUpgrade,+ }, installedArtefact{digest: ptr(digest("plugin-0-12-0"))})++ if plugin.TargetVersion != "0.13.0" {+ t.Errorf("TargetVersion = %q, want the version the download asks for", plugin.TargetVersion)+ }+ if plugin.TargetDigest != offeredHash {+ t.Errorf("TargetDigest = %q, want the offered file hash %q — verification compares"++ " against what is on disk, and a file on disk has a hash, not a version",+ plugin.TargetDigest, offeredHash)+ }+ if plugin.TargetDigest == plugin.TargetVersion {+ t.Error("the version and the digest must not be the same value; that is the defect")+ }+}++// TestACurrentVerdictIsNotOverriddenByALocalDigestMiss.+//+// Docker exposes three sha256 identities per image and this client reports the CONFIG+// digest. An answer that names only the manifest digest therefore matches nothing locally+// — CarriesDigest returns false for an installation that is exactly up to date — and the+// digest comparison alone would mark it outdated. The server knows better: it compared+// against the digest this client sent, so its verdict is the authority.+//+// Without this the overview tells the user a component will be updated in the same breath+// as the server saying it will not, and the whole point of the redesign is that the+// interface stops making claims the answer does not support.+func TestACurrentVerdictIsNotOverriddenByALocalDigestMiss(t *testing.T) {+ installed := digest("config-of-the-newest-build")++ current := buildImageComponentUpdate(models.ImageUpdate{+ Component: models.ComponentTypePentagi, OS: models.OSTypeLinux, Arch: models.ArchTypeAMD64,+ Repository: "vxcontrol/pentagi", Tag: "latest",+ // Only the manifest digest — the identity this client never reports.+ ImageHash: digest("manifest"),+ Action: models.ComponentActionCurrent,+ }, installedArtefact{digest: ptr(installed)})++ if current.Outdated {+ t.Error("the server answered `current` and the screen would still say the component" ++ " will be updated: a digest the client cannot report is not evidence of a difference")+ }++ // The mirror case must keep working: an `upgrade` verdict is obeyed even when the+ // digests happen to match, because the server is the authority in both directions.+ upgrade := buildImageComponentUpdate(models.ImageUpdate{+ Component: models.ComponentTypePentagi, OS: models.OSTypeLinux, Arch: models.ArchTypeAMD64,+ Repository: "vxcontrol/pentagi", Tag: "latest",+ ImageHash: digest("manifest"), ConfigHash: ptr(installed),+ Action: models.ComponentActionUpgrade,+ }, installedArtefact{digest: ptr(installed)})+ if !upgrade.Outdated {+ t.Error("an `upgrade` verdict must survive a digest comparison that found no difference")+ }++ // Files answer to the same rule: a `current` verdict has to survive a version string+ // that merely spells the same build differently.+ file := buildFileComponentUpdate(models.FileUpdate{+ Component: models.ComponentTypeJaegerClickhouse, OS: models.OSTypeLinux,+ Arch: models.ArchTypeAMD64, Version: "v0.13.0", Action: models.ComponentActionCurrent,+ }, installedArtefact{version: ptr("0.13.0")})+ if file.Outdated {+ t.Error("the server answered `current` for a file and the version strings differed only" ++ " in spelling; the screen would offer an update the server did not")+ }+}++// Files carry no digest triple: the server identifies their builds by version, and by+// hash for the one that ships without a version at all.+func TestFileIsOutdatedByVersionAndFallsBackToTheHash(t *testing.T) {+ installedDigest := digest("a")++ tests := []struct {+ name string+ offered models.FileUpdate+ installed installedArtefact+ wantOutdated bool+ wantVerifiable bool+ }{+ {+ name: "a newer installer build",+ offered: models.FileUpdate{+ Component: models.ComponentTypeInstaller, OS: models.OSTypeDarwin, Arch: models.ArchTypeARM64,+ PackageName: "installer", Version: "2.1.0", FileHash: digest("z"),+ },+ installed: installedArtefact{version: ptr("2.0.0")},+ wantOutdated: true,+ wantVerifiable: true,+ },+ {+ name: "the same installer build",+ offered: models.FileUpdate{+ Component: models.ComponentTypeInstaller, OS: models.OSTypeDarwin, Arch: models.ArchTypeARM64,+ PackageName: "installer", Version: "2.0.0", FileHash: digest("z"),+ },+ installed: installedArtefact{version: ptr("2.0.0")},+ wantOutdated: false,+ wantVerifiable: true,+ },+ {+ // The Jaeger plugin ships without a version, so the hash is all there is.+ name: "no version reported falls back to the hash",+ offered: models.FileUpdate{+ Component: models.ComponentTypeJaegerClickhouse, OS: models.OSTypeLinux, Arch: models.ArchTypeAMD64,+ PackageName: "jaeger-clickhouse", Version: "0.13.0", FileHash: installedDigest,+ },+ installed: installedArtefact{digest: ptr(installedDigest)},+ wantOutdated: false,+ wantVerifiable: true,+ },+ {+ name: "nothing installed to compare against",+ offered: models.FileUpdate{+ Component: models.ComponentTypeInstaller, OS: models.OSTypeDarwin, Arch: models.ArchTypeARM64,+ PackageName: "installer", Version: "2.1.0", FileHash: digest("z"),+ },+ installed: installedArtefact{},+ wantOutdated: false,+ wantVerifiable: false,+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ got := buildFileComponentUpdate(tt.offered, tt.installed)++ if got.Outdated != tt.wantOutdated {+ t.Errorf("outdated = %t, want %t", got.Outdated, tt.wantOutdated)+ }+ if got.Verifiable != tt.wantVerifiable {+ t.Errorf("verifiable = %t, want %t", got.Verifiable, tt.wantVerifiable)+ }+ })+ }+}++// TestTheAnswerIsMatchedPerPlatform: the Jaeger plugin is reported twice, once per+// architecture. Matching the answer on the component name alone would collapse the two and+// compare one architecture's file against the other's.+func TestTheAnswerIsMatchedPerPlatform(t *testing.T) {+ amd64Digest, arm64Digest := digest("a"), digest("b")++ reported := reportedComponents{files: []models.FileComponentInfo{+ {+ Component: models.ComponentTypeJaegerClickhouse, OS: models.OSTypeLinux,+ Arch: models.ArchTypeAMD64, FileHash: ptr(amd64Digest),+ },+ {+ Component: models.ComponentTypeJaegerClickhouse, OS: models.OSTypeLinux,+ Arch: models.ArchTypeARM64, FileHash: ptr(arm64Digest),+ },+ }}+ answer := &models.CheckUpdatesResponse{Updates: []models.UpdateInfo{{+ Stack: models.ProductStackObservability, HasUpdate: false,+ Files: []models.FileUpdate{+ {+ Component: models.ComponentTypeJaegerClickhouse, OS: models.OSTypeLinux,+ Arch: models.ArchTypeAMD64, PackageName: "jaeger-clickhouse",+ Version: "0.13.0", FileHash: amd64Digest,+ },+ {+ Component: models.ComponentTypeJaegerClickhouse, OS: models.OSTypeLinux,+ Arch: models.ArchTypeARM64, PackageName: "jaeger-clickhouse",+ Version: "0.13.0", FileHash: arm64Digest,+ },+ },+ }}}++ result := &CheckResult{}+ result.applyUpdateAnswer(answer, reported)++ if len(result.StackUpdates) != 1 {+ t.Fatalf("got %d stacks, want 1", len(result.StackUpdates))+ }+ for _, component := range result.StackUpdates[0].Components {+ if component.Outdated {+ t.Errorf("%s/%s matches what is installed but is reported as outdated",+ component.Component, component.Arch)+ }+ if !component.Verifiable {+ t.Errorf("%s/%s should have been comparable", component.Component, component.Arch)+ }+ }+}++// TestReleaseTextsReachTheResult: the update screen shows the changelog and release notes+// before anything is applied, so they have to survive the answer, not just the verdict.+func TestReleaseTextsReachTheResult(t *testing.T) {+ answer := &models.CheckUpdatesResponse{Updates: []models.UpdateInfo{{+ Stack: models.ProductStackPentagi,+ HasUpdate: true,+ CurrentVersion: ptr("2.0.0"),+ LatestVersion: ptr("2.1.0"),+ Changelog: ptr("## Changed\n- everything"),+ ReleaseNotes: ptr("## Notes\n- read this"),+ }}}++ result := &CheckResult{}+ result.applyUpdateAnswer(answer, reportedComponents{})++ if len(result.StackUpdates) != 1 {+ t.Fatalf("got %d stacks, want 1", len(result.StackUpdates))+ }+ stack := result.StackUpdates[0]+ if stack.CurrentVersion != "2.0.0" || stack.LatestVersion != "2.1.0" {+ t.Errorf("versions = %q → %q, want 2.0.0 → 2.1.0", stack.CurrentVersion, stack.LatestVersion)+ }+ if stack.Changelog == "" || stack.ReleaseNotes == "" {+ t.Error("the release texts were dropped, leaving the update screen with nothing to show")+ }+}++func TestUpdateStrategy(t *testing.T) {+ tests := []struct {+ name string+ configured string+ want models.UpdateStrategy+ }{+ // Unset is the normal state of every installation: the variable has never been+ // written by anything. What it resolves to is therefore the strategy the whole+ // fleet is on, which is why it is `preview` — under `stable` a stack with no+ // published release is offered nothing, and "nothing is published yet" reaches+ // the user as "there are no updates".+ {name: "unset follows the channels already in use", configured: "", want: models.UpdateStrategyPreview},+ {name: "explicit stable", configured: "stable", want: models.UpdateStrategyStable},+ {name: "case is normalised", configured: "NIGHTLY", want: models.UpdateStrategyNightly},+ {name: "surrounding whitespace", configured: " preview ", want: models.UpdateStrategyPreview},+ // A typo must not leave an installation unable to hear about security updates.+ {name: "a typo falls back rather than failing", configured: "bleeding-edge", want: models.UpdateStrategyPreview},+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ appState := &mockState{vars: map[string]loader.EnvVar{+ "UPDATE_STRATEGY": {Value: tt.configured},+ }}+ if got := updateStrategy(appState); got != tt.want {+ t.Errorf("updateStrategy(%q) = %q, want %q", tt.configured, got, tt.want)+ }+ })+ }+}++// TestReportedRequestPassesTheContract ties the whole gathering to the one thing that+// matters about it: a request that fails validation is answered as a server error, which+// is indistinguishable from an outage and costs the answer for every component at once.+func TestReportedRequestPassesTheContract(t *testing.T) {+ handler := &defaultCheckHandler{+ mx: &sync.Mutex{},+ appState: &mockState{vars: map[string]loader.EnvVar{}, envPath: t.TempDir() + "/.env"},+ }+ // Every stack connected and local, so the gathering walks every branch it has.+ result := &CheckResult{+ GraphitiConnected: true, LangfuseConnected: true, ObservabilityConnected: true,+ }++ components := handler.gatherComponents(t.Context(), result)++ request := models.CheckUpdatesRequest{+ InstallerVersion: cloud.NormalizeVersion(InstallerVersion),+ InstallerOS: models.OSTypeLinux,+ InstallerArch: models.ArchTypeAMD64,+ Strategy: models.UpdateStrategyStable,+ Images: components.images,+ Files: components.files,+ }+ if err := request.Valid(); err != nil {+ t.Fatalf("the gathered request would be rejected: %v", err)+ }+}++// TestTheServersActionDecidesWhereAComparisonCannot.+//+// Membership in the answer is not a work list — under a curated release every+// artefact matching a reported component is listed — so the installer compares+// digests. But a comparison needs two sides, and the two cases with only one are+// exactly the ones that matter most:+//+// - `install`: nothing is pulled, so there is no installed digest and the+// comparison leaves "not outdated" — which reads as "nothing to do" for the+// one component that most needs doing, and hides the download button;+// - `unknown`: there is no artefact at all, so whatever a comparison produced+// is about nothing.+func TestTheServersActionDecidesWhereAComparisonCannot(t *testing.T) {+ installed := strings.Repeat("a", 64)+ offeredDigest := strings.Repeat("b", 64)+ reason := models.ReasonTagNotPublished++ t.Run("nothing pulled yet is an update, not a match", func(t *testing.T) {+ component := buildImageComponentUpdate(models.ImageUpdate{+ Component: models.ComponentTypePentagi, OS: models.OSTypeLinux, Arch: models.ArchTypeAMD64,+ Action: models.ComponentActionInstall, Repository: "vxcontrol/pentagi", Tag: "latest",+ PullReference: "vxcontrol/pentagi:latest", ImageHash: offeredDigest,+ }, installedArtefact{})++ if !component.Outdated {+ t.Error("an image that was never pulled has to be offered, or the download button never appears")+ }+ if component.Action != "install" {+ t.Errorf("the server's action must survive: %q", component.Action)+ }+ if component.PullReference != "vxcontrol/pentagi:latest" {+ t.Errorf("the reference the server chose is what goes into the compose variable: %q",+ component.PullReference)+ }+ })++ t.Run("unknown carries a reason and nothing to act on", func(t *testing.T) {+ component := buildImageComponentUpdate(models.ImageUpdate{+ Component: models.ComponentTypePostgres, OS: models.OSTypeLinux, Arch: models.ArchTypeAMD64,+ Action: models.ComponentActionUnknown, Reason: &reason,+ Repository: "postgres", Tag: "15",+ }, installedArtefact{digest: &installed})++ if component.Outdated {+ t.Error("there is no artefact, so there is nothing to change to")+ }+ if component.Verifiable {+ t.Error("and nothing to verify against")+ }+ if component.Reason != "tag_not_published" {+ t.Errorf("the reason separates 'nothing to do' from 'we publish nothing for this': %q",+ component.Reason)+ }+ if component.PullReference != "" {+ t.Errorf("an unresolved component names nothing to pull: %q", component.PullReference)+ }+ })++ t.Run("current with a matching digest stays current", func(t *testing.T) {+ component := buildImageComponentUpdate(models.ImageUpdate{+ Component: models.ComponentTypePentagi, OS: models.OSTypeLinux, Arch: models.ArchTypeAMD64,+ Action: models.ComponentActionCurrent, Repository: "vxcontrol/pentagi", Tag: "latest",+ PullReference: "vxcontrol/pentagi:latest", ImageHash: installed,+ }, installedArtefact{digest: &installed})++ if component.Outdated {+ t.Error("the installed digest is the offered one")+ }+ if !component.Verifiable {+ t.Error("both sides are known, so the comparison means something")+ }+ })++ t.Run("a file that was never installed is an update too", func(t *testing.T) {+ component := buildFileComponentUpdate(models.FileUpdate{+ Component: models.ComponentTypeInstaller, OS: models.OSTypeLinux, Arch: models.ArchTypeAMD64,+ Action: models.ComponentActionInstall, PackageName: "installer", Version: "2.1.0",+ FileHash: offeredDigest,+ }, installedArtefact{})++ if !component.Outdated {+ t.Error("a file that was never installed has to be offered")+ }+ if component.Action != "install" {+ t.Errorf("the server's action must survive: %q", component.Action)+ }+ })+}diff --git a/backend/cmd/installer/cloud/calls_test.go b/backend/cmd/installer/cloud/calls_test.gonew file mode 100644index 00000000..f0ff2b62--- /dev/null+++ b/backend/cmd/installer/cloud/calls_test.go@@ -0,0 +1,352 @@+package cloud++import (+ "bytes"+ "context"+ "crypto/ed25519"+ "crypto/sha256"+ "crypto/sha512"+ "encoding/hex"+ "encoding/json"+ "errors"+ "io"+ "strings"+ "testing"++ "github.com/vxcontrol/cloud/models"+ "github.com/vxcontrol/cloud/sdk"+)++// The tests below drive the client through substituted call functions. That is deliberate:+// the transport always negotiates TLS and solves a proof-of-work challenge, so there is no+// local server to point it at, and what needs covering here is what the client does around+// the call — validate, envelope, verify — not the transport itself.++func stringPtr(s string) *string { return &s }++// validCheckRequest is the smallest request the contract accepts, so a test that expects+// rejection is rejected for the reason it is testing and not by accident.+func validCheckRequest() models.CheckUpdatesRequest {+ return models.CheckUpdatesRequest{+ InstallerVersion: "2.0.0",+ InstallerOS: models.OSTypeLinux,+ InstallerArch: models.ArchTypeAMD64,+ Strategy: models.UpdateStrategyStable,+ Images: []models.ImageComponentInfo{{+ Component: models.ComponentTypePentagi,+ Status: models.ComponentStatusRunning,+ OS: models.OSTypeLinux,+ Arch: models.ArchTypeAMD64,+ Repository: "vxcontrol/pentagi",+ Tag: "latest",+ }},+ }+}++// TestCheckUpdatesUnwrapsTheEnvelope is the regression that matters most here. The answer+// arrives wrapped in a status envelope, and decoding it straight into the response type+// SUCCEEDS — every field simply stays zero. Skipping the unwrapping therefore reports "no+// updates available" for every answer, with no error anywhere to explain it.+func TestCheckUpdatesUnwrapsTheEnvelope(t *testing.T) {+ latest := "2.1.0"+ payload := models.CheckUpdatesResponse{+ Updates: []models.UpdateInfo{{+ Stack: models.ProductStackPentagi,+ HasUpdate: true,+ LatestVersion: &latest,+ }},+ }+ enveloped, err := json.Marshal(map[string]any{"status": "success", "data": payload})+ if err != nil {+ t.Fatalf("marshal: %v", err)+ }++ client := &Client{checkUpdates: func(ctx context.Context, body []byte) ([]byte, error) {+ return enveloped, nil+ }}++ response, err := client.CheckUpdates(context.Background(), validCheckRequest())+ if err != nil {+ t.Fatalf("CheckUpdates: %v", err)+ }++ if len(response.Updates) != 1 {+ t.Fatalf("got %d stacks, want 1 — the envelope was not unwrapped", len(response.Updates))+ }+ if !response.Updates[0].HasUpdate {+ t.Error("the answer says an update is available, the client says it is not")+ }+}++// TestCheckUpdatesSendsWhatItWasGiven: the request reaching the wire is the caller's,+// unaltered. The server answers only about the components it is told about, so anything+// dropped here comes back as "nothing to report" rather than "not asked".+func TestCheckUpdatesSendsWhatItWasGiven(t *testing.T) {+ var sent models.CheckUpdatesRequest++ client := &Client{checkUpdates: func(ctx context.Context, body []byte) ([]byte, error) {+ if err := json.Unmarshal(body, &sent); err != nil {+ return nil, err+ }+ return []byte(`{"status":"success","data":{"updates":[]}}`), nil+ }}++ request := validCheckRequest()+ if _, err := client.CheckUpdates(context.Background(), request); err != nil {+ t.Fatalf("CheckUpdates: %v", err)+ }++ if len(sent.Images) != len(request.Images) || len(sent.Files) != len(request.Files) {+ t.Fatalf("sent %d images and %d files, want %d and %d",+ len(sent.Images), len(sent.Files), len(request.Images), len(request.Files))+ }+ if sent.Strategy != request.Strategy {+ t.Errorf("strategy = %q, want %q", sent.Strategy, request.Strategy)+ }+ if sent.InstallerVersion != request.InstallerVersion {+ t.Errorf("installer version = %q, want %q", sent.InstallerVersion, request.InstallerVersion)+ }+}++// TestCheckUpdatesRefusesAMalformedRequestBeforeSending: a request the contract rejects+// comes back as a server-side failure, which is indistinguishable from an outage and just+// as pointless to retry. Catching it here names the field instead — and costs no call.+func TestCheckUpdatesRefusesAMalformedRequestBeforeSending(t *testing.T) {+ called := false+ client := &Client{checkUpdates: func(ctx context.Context, body []byte) ([]byte, error) {+ called = true+ return nil, nil+ }}++ request := validCheckRequest()+ request.Strategy = models.UpdateStrategy("bleeding-edge")++ if _, err := client.CheckUpdates(context.Background(), request); err == nil {+ t.Fatal("a request with an unknown strategy was accepted")+ }+ if called {+ t.Error("the malformed request was sent anyway")+ }+}++func TestCheckUpdatesClassifiesCallFailures(t *testing.T) {+ client := &Client{checkUpdates: func(ctx context.Context, body []byte) ([]byte, error) {+ return nil, sdk.ErrForbidden+ }}++ _, err := client.CheckUpdates(context.Background(), validCheckRequest())+ if err == nil {+ t.Fatal("expected an error")+ }++ var failure *Failure+ if !errors.As(err, &failure) {+ t.Fatalf("error is not classified: %v", err)+ }+ if failure.Reason != FailureForbidden {+ t.Errorf("reason = %q, want %q", failure.Reason, FailureForbidden)+ }+}++func TestPackageInfoUnwrapsTheEnvelopeAndValidates(t *testing.T) {+ payload := models.PackageInfoResponse{+ Size: 1024,+ Hash: strings.Repeat("a", 64),+ Version: "2.0.0",+ OS: models.OSTypeLinux,+ Arch: models.ArchTypeAMD64,+ Signature: models.SignatureValue(strings.Repeat("Q", 86)),+ }+ enveloped, err := json.Marshal(map[string]any{"status": "success", "data": payload})+ if err != nil {+ t.Fatalf("marshal: %v", err)+ }++ client := &Client{packageInfo: func(ctx context.Context, query map[string]string) ([]byte, error) {+ // The query is how this endpoint carries its arguments; an empty one would ask+ // about nothing in particular.+ if query["component"] != "installer" {+ t.Errorf("component = %q, want installer", query["component"])+ }+ if query["version"] != "2.0.0" {+ t.Errorf("version = %q, want 2.0.0", query["version"])+ }+ return enveloped, nil+ }}++ info, err := client.PackageInfo(context.Background(), models.PackageInfoRequest{+ Component: models.ComponentTypeInstaller,+ Version: "2.0.0",+ OS: models.OSTypeLinux,+ Arch: models.ArchTypeAMD64,+ })+ if err != nil {+ t.Fatalf("PackageInfo: %v", err)+ }+ if info.Size != 1024 {+ t.Errorf("size = %d, want 1024 — the envelope was not unwrapped", info.Size)+ }+}++// signedPackage builds a package together with a description of it that verifies, so the+// download test exercises the real Ed25519 and sha256 paths rather than stubbed checks.+// The signature covers the file's SHA-512 digest.+func signedPackage(t *testing.T, payload []byte) (models.PackageInfoResponse, models.DownloadPackageRequest) {+ t.Helper()++ _, private, err := ed25519.GenerateKey(nil)+ if err != nil {+ t.Fatalf("generate key: %v", err)+ }+ sha512Digest := sha512.Sum512(payload)+ sha256Digest := sha256.Sum256(payload)++ var signature models.SignatureValue+ if _, err := signature.FromBytes(ed25519.Sign(private, sha512Digest[:])); err != nil {+ t.Fatalf("encode signature: %v", err)+ }++ info := models.PackageInfoResponse{+ Size: int64(len(payload)),+ Hash: hex.EncodeToString(sha256Digest[:]),+ Version: "2.0.0",+ OS: models.OSTypeLinux,+ Arch: models.ArchTypeAMD64,+ Signature: signature,+ }+ request := models.DownloadPackageRequest{+ Component: models.ComponentTypeInstaller,+ Version: "2.0.0",+ OS: models.OSTypeLinux,+ Arch: models.ArchTypeAMD64,+ }++ return info, request+}++// TestDownloadPackageRejectsATamperedPayload covers the three checks that make a downloaded+// binary safe to run. The signature is not verifiable in a unit test — it is made with a+// throwaway key, not the release key — so this asserts what the size and digest checks+// catch, which is every alteration of the bytes.+func TestDownloadPackageRejectsATamperedPayload(t *testing.T) {+ payload := []byte(strings.Repeat("installer bytes ", 512))+ info, request := signedPackage(t, payload)++ tests := []struct {+ name string+ served []byte+ wantHint string+ }{+ {name: "truncated", served: payload[:len(payload)-16], wantHint: "bytes, expected"},+ {name: "padded", served: append(append([]byte{}, payload...), 'x'), wantHint: "bytes, expected"},+ {+ name: "same length, different content",+ served: append(append([]byte{}, payload[:len(payload)-1]...), 'X'),+ wantHint: "sha256",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ client := &Client{+ downloadPackage: func(ctx context.Context, query map[string]string, w io.Writer) error {+ _, err := w.Write(tt.served)+ return err+ },+ }++ var sink bytes.Buffer+ err := client.DownloadPackage(context.Background(), request, info, &sink)+ if err == nil {+ t.Fatalf("a %s package was accepted", tt.name)+ }+ if !strings.Contains(err.Error(), tt.wantHint) {+ t.Errorf("error = %v, want it to mention %q", err, tt.wantHint)+ }+ })+ }+}++// TestDownloadPackageWritesEverythingItReceives: whatever the verdict, the caller's writer+// gets the bytes — which is why the caller has to treat the destination as unverified until+// this returns nil.+func TestDownloadPackageWritesEverythingItReceives(t *testing.T) {+ payload := []byte(strings.Repeat("installer bytes ", 512))+ info, request := signedPackage(t, payload)++ client := &Client{+ downloadPackage: func(ctx context.Context, query map[string]string, w io.Writer) error {+ if query["component"] != "installer" {+ t.Errorf("component = %q, want installer", query["component"])+ }+ _, err := w.Write(payload)+ return err+ },+ }++ var sink bytes.Buffer+ // The signature check fails: the test key is not the release key. Everything before it+ // must already have passed, which is what the assertions below establish.+ err := client.DownloadPackage(context.Background(), request, info, &sink)+ if err == nil || !strings.Contains(err.Error(), "signature") {+ t.Fatalf("expected the signature check to be the one that fails, got: %v", err)+ }+ if !bytes.Equal(sink.Bytes(), payload) {+ t.Errorf("the destination received %d bytes, want %d", sink.Len(), len(payload))+ }+}++func TestDownloadPackageRefusesAMalformedRequestBeforeSending(t *testing.T) {+ called := false+ client := &Client{+ downloadPackage: func(ctx context.Context, query map[string]string, w io.Writer) error {+ called = true+ return nil+ },+ }++ // Only file-distributed components can be downloaded; an image component has no+ // package behind it.+ request := models.DownloadPackageRequest{+ Component: models.ComponentTypePentagi,+ Version: "2.0.0",+ OS: models.OSTypeLinux,+ Arch: models.ArchTypeAMD64,+ }+ // The description has to be a valid one, or it — not the request — would be what stops+ // the call, and this test would pass with the request check removed.+ info := models.PackageInfoResponse{+ Size: 1024,+ Hash: strings.Repeat("a", 64),+ Version: "2.0.0",+ OS: models.OSTypeLinux,+ Arch: models.ArchTypeAMD64,+ Signature: models.SignatureValue(strings.Repeat("Q", 86)),+ }++ err := client.DownloadPackage(context.Background(), request, info, io.Discard)+ if err == nil {+ t.Fatal("a download was accepted for a component that has no package")+ }+ if called {+ t.Error("the malformed request was sent anyway")+ }+}++// TestCallsOnAnUninitializedClientFailCleanly: the zero Client is what a failed New leaves+// behind, and calling into it must report that rather than panic.+func TestCallsOnAnUninitializedClientFailCleanly(t *testing.T) {+ var client *Client++ if _, err := client.CheckUpdates(context.Background(), validCheckRequest()); err == nil {+ t.Error("CheckUpdates on a nil client did not fail")+ }+ if _, err := client.PackageInfo(context.Background(), models.PackageInfoRequest{}); err == nil {+ t.Error("PackageInfo on a nil client did not fail")+ }+ if err := client.DownloadPackage(+ context.Background(), models.DownloadPackageRequest{}, models.PackageInfoResponse{}, io.Discard,+ ); err == nil {+ t.Error("DownloadPackage on a nil client did not fail")+ }+}diff --git a/backend/cmd/installer/cloud/client.go b/backend/cmd/installer/cloud/client.gonew file mode 100644index 00000000..e6c1b44e--- /dev/null+++ b/backend/cmd/installer/cloud/client.go@@ -0,0 +1,239 @@+// Package cloud is the installer's client for the PentAGI Cloud API: update checks and+// package downloads.+//+// It is a thin layer over the published SDK. Wire types come from the SDK's models package+// rather than being redeclared here, so a contract change is a compile error instead of a+// silent mismatch, and the whole package is free of installer state — it takes a Config,+// returns answers, and knows nothing about the wizard, the checker or the processor. Tests+// substitute the call functions directly; nothing here needs a server to exercise.+package cloud++import (+ "fmt"+ "net/http"+ "net/url"+ "strings"+ "time"++ "github.com/vxcontrol/cloud/sdk"+ "github.com/vxcontrol/cloud/system"+)++// DefaultHost is the update server used when none is configured.+const DefaultHost = "update.pentagi.com"++// Route names and paths are part of the API contract: the server dispatches on the name,+// and a name it does not know is refused outright rather than reported as a missing page.+const (+ routeUpdatesCheck = "updates_check"+ routePackagesInfo = "packages_info"+ routePackagesDownload = "packages_download"++ pathUpdatesCheck = "/api/v1/proxy/updates/check"+ pathPackagesInfo = "/api/v1/proxy/packages/info"+ pathPackagesDownload = "/api/v1/proxy/packages/download"+)++const (+ // clientName identifies the installer in the User-Agent, alongside its build version.+ clientName = "PentAGI-Installer"++ // powTimeout is the budget for the proof-of-work challenge each call has to solve.+ // It is raised well above the SDK default because the SDK does not retry a timed-out+ // challenge, and a laptop or a throttled VM can genuinely need the extra seconds.+ powTimeout = 60 * time.Second++ maxRetries = 3+)++// Config describes how to reach the update server. Every field is optional; the zero+// Config talks to DefaultHost anonymously, with no proxy.+type Config struct {+ // Host is the update server, with or without a scheme (UPDATE_SERVER_HOST).+ Host string+ // ProxyURL is the installer's own proxy (PROXY_URL), used for every call. It takes+ // precedence over the environment, which is what the user configured it for.+ ProxyURL string+ // LicenseKey is the PentAGI license key (LICENSE_KEY). An unusable key is dropped+ // rather than sent, and reported through LicenseWarning.+ LicenseKey string+ // InstallerVersion is this build's version, as displayed. It is normalised for the+ // request and sent verbatim in the User-Agent.+ InstallerVersion string+ // Logger receives the SDK's own diagnostics. Nil keeps them silent.+ Logger sdk.Logger+}++// Client talks to the update server. Build one with New.+type Client struct {+ checkUpdates sdk.CallReqBytesRespBytes+ packageInfo sdk.CallReqQueryRespBytes+ downloadPackage sdk.CallReqQueryRespWriter++ host string+ version string+ licenseWarning error+}++// New builds a client for cfg.+//+// It fails only on configuration that cannot produce working calls — an unusable host or+// proxy. A license key that does not hold up is deliberately not fatal: the update check is+// the one thing that still works without a license, and refusing to run would leave the+// user with no way to learn that their key is the problem. The key is dropped and the+// reason is available from LicenseWarning.+func New(cfg Config) (*Client, error) {+ host, err := NormalizeHost(cfg.Host)+ if err != nil {+ return nil, err+ }++ transport, err := newTransport(cfg.ProxyURL)+ if err != nil {+ return nil, err+ }++ client := &Client{+ host: host,+ version: NormalizeVersion(cfg.InstallerVersion),+ }++ userAgentVersion := strings.TrimSpace(cfg.InstallerVersion)+ if userAgentVersion == "" {+ userAgentVersion = UnknownVersion+ }++ options := []sdk.Option{+ sdk.WithTransport(transport),+ // The installation ID identifies this machine to the server across calls, and the+ // contract requires it on every one of them.+ sdk.WithInstallationID(system.GetInstallationID()),+ sdk.WithClient(clientName, userAgentVersion),+ sdk.WithPowTimeout(powTimeout),+ sdk.WithMaxRetries(maxRetries),+ }+ if cfg.Logger != nil {+ options = append(options, sdk.WithLogger(cfg.Logger))+ }++ // Validate before handing the key over: the SDK drops a key it cannot decode without+ // saying so, and the call then runs anonymously — which looks, from the outside, like+ // the license simply not being honoured.+ if key := strings.TrimSpace(cfg.LicenseKey); key != "" {+ if _, err := sdk.IntrospectLicenseKey(key); err != nil {+ client.licenseWarning = fmt.Errorf("license key is not usable, continuing without it: %w", err)+ } else {+ options = append(options, sdk.WithLicenseKey(key))+ }+ }++ configs := []sdk.CallConfig{+ {+ Calls: []any{&client.checkUpdates},+ Host: host,+ Name: routeUpdatesCheck,+ Path: pathUpdatesCheck,+ Method: sdk.CallMethodPOST,+ },+ {+ Calls: []any{&client.packageInfo},+ Host: host,+ Name: routePackagesInfo,+ Path: pathPackagesInfo,+ Method: sdk.CallMethodGET,+ },+ {+ Calls: []any{&client.downloadPackage},+ Host: host,+ Name: routePackagesDownload,+ Path: pathPackagesDownload,+ Method: sdk.CallMethodGET,+ },+ }++ if err := sdk.Build(configs, options...); err != nil {+ return nil, fmt.Errorf("failed to build update client: %w", err)+ }++ return client, nil+}++// Host reports the normalised update server this client talks to.+func (c *Client) Host() string { return c.host }++// InstallerVersion reports the version this client sends, after normalisation. It differs+// from the displayed build version whenever that one cannot be expressed as a version+// number.+func (c *Client) InstallerVersion() string { return c.version }++// LicenseWarning reports why the configured license key was not used, or nil when it was+// used or none was configured. Calls still work — anonymously.+func (c *Client) LicenseWarning() error { return c.licenseWarning }++// NormalizeHost reduces a configured update server address to the host[:port] form the+// client needs.+//+// The setting has historically held a full URL, so a scheme and a trailing path are+// tolerated and stripped. The transport is always TLS, so an explicit "http://" is an+// error rather than something to silently upgrade — a user who wrote it meant it, and+// quietly doing the opposite would hide a misconfiguration.+func NormalizeHost(raw string) (string, error) {+ value := strings.TrimSpace(raw)+ if value == "" {+ return DefaultHost, nil+ }++ if scheme, rest, found := strings.Cut(value, "://"); found {+ switch strings.ToLower(scheme) {+ case "https":+ value = rest+ case "http":+ return "", fmt.Errorf("update server %q uses http, but the connection is always encrypted: use https", raw)+ default:+ return "", fmt.Errorf("update server %q uses unsupported scheme %q", raw, scheme)+ }+ }++ // Drop anything after the authority: a path, a query, a fragment.+ value, _, _ = strings.Cut(value, "/")+ value, _, _ = strings.Cut(value, "?")+ value, _, _ = strings.Cut(value, "#")+ value = strings.ToLower(strings.TrimSpace(value))++ switch {+ case value == "":+ return "", fmt.Errorf("update server %q has no host", raw)+ case strings.Contains(value, "@"):+ return "", fmt.Errorf("update server %q carries credentials, which are not supported", raw)+ case strings.ContainsAny(value, " \t"):+ return "", fmt.Errorf("update server %q contains whitespace", raw)+ }++ return value, nil+}++// newTransport builds the transport every call uses.+//+// The SDK's default reads the proxy from the environment. The installer keeps its own+// proxy setting — often with credentials the environment does not carry — and that setting+// has to win. With none configured the environment behaviour is left as it is.+func newTransport(proxyURL string) (*http.Transport, error) {+ transport := sdk.DefaultTransport()++ value := strings.TrimSpace(proxyURL)+ if value == "" {+ return transport, nil+ }++ parsed, err := url.Parse(value)+ if err != nil {+ return nil, fmt.Errorf("proxy %q is not a valid URL: %w", proxyURL, err)+ }+ if parsed.Host == "" {+ return nil, fmt.Errorf("proxy %q has no host", proxyURL)+ }++ transport.Proxy = http.ProxyURL(parsed)++ return transport, nil+}diff --git a/backend/cmd/installer/cloud/client_test.go b/backend/cmd/installer/cloud/client_test.gonew file mode 100644index 00000000..c80173fc--- /dev/null+++ b/backend/cmd/installer/cloud/client_test.go@@ -0,0 +1,178 @@+package cloud++import (+ "net/http"+ "strings"+ "testing"+)++func TestNormalizeHost(t *testing.T) {+ tests := []struct {+ name string+ raw string+ want string+ wantErr string+ }{+ {name: "empty falls back to the default server", raw: "", want: DefaultHost},+ {name: "whitespace only falls back too", raw: " ", want: DefaultHost},+ {name: "bare host passes through", raw: "update.pentagi.com", want: "update.pentagi.com"},+ {name: "port is kept", raw: "update.pentagi.com:8443", want: "update.pentagi.com:8443"},+ // The setting has always held a full URL, so this is the value most installations+ // actually carry.+ {name: "https scheme is stripped", raw: "https://update.pentagi.com", want: "update.pentagi.com"},+ {name: "trailing slash is stripped", raw: "https://update.pentagi.com/", want: "update.pentagi.com"},+ {name: "path is stripped", raw: "https://update.pentagi.com/api/v1", want: "update.pentagi.com"},+ {name: "query is stripped", raw: "https://update.pentagi.com?x=1", want: "update.pentagi.com"},+ {name: "case is normalised", raw: "HTTPS://Update.PentAGI.com", want: "update.pentagi.com"},++ // Silently upgrading http to https would hide a misconfiguration behind a working+ // connection, and the user would never learn their setting was ignored.+ {name: "http is refused rather than upgraded", raw: "http://update.pentagi.com", wantErr: "always encrypted"},+ {name: "other schemes are refused", raw: "ftp://update.pentagi.com", wantErr: "unsupported scheme"},+ {name: "credentials are refused", raw: "https://user:[email protected]", wantErr: "credentials"},+ {name: "scheme with no host is refused", raw: "https:///api", wantErr: "no host"},+ {name: "whitespace inside is refused", raw: "update pentagi.com", wantErr: "whitespace"},+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ got, err := NormalizeHost(tt.raw)++ if tt.wantErr != "" {+ if err == nil {+ t.Fatalf("NormalizeHost(%q) = %q, want error containing %q", tt.raw, got, tt.wantErr)+ }+ if !strings.Contains(err.Error(), tt.wantErr) {+ t.Fatalf("NormalizeHost(%q) error = %v, want it to mention %q", tt.raw, err, tt.wantErr)+ }+ return+ }++ if err != nil {+ t.Fatalf("NormalizeHost(%q) unexpected error: %v", tt.raw, err)+ }+ if got != tt.want {+ t.Errorf("NormalizeHost(%q) = %q, want %q", tt.raw, got, tt.want)+ }+ })+ }+}++// TestTransportPrefersTheConfiguredProxy pins the reason this package builds its own+// transport instead of taking the SDK default: the installer's proxy setting often carries+// credentials the environment does not, and it has to win over the environment.+func TestTransportPrefersTheConfiguredProxy(t *testing.T) {+ transport, err := newTransport("http://user:[email protected]:3128")+ if err != nil {+ t.Fatalf("newTransport: %v", err)+ }+ if transport.Proxy == nil {+ t.Fatal("transport has no proxy resolver")+ }++ request, err := http.NewRequest(http.MethodGet, "https://update.pentagi.com/", nil)+ if err != nil {+ t.Fatalf("NewRequest: %v", err)+ }++ proxy, err := transport.Proxy(request)+ if err != nil {+ t.Fatalf("proxy resolver: %v", err)+ }+ if proxy == nil {+ t.Fatal("configured proxy was not applied to the request")+ }+ if proxy.Host != "proxy.internal:3128" {+ t.Errorf("proxy host = %q, want proxy.internal:3128", proxy.Host)+ }+ if user := proxy.User; user == nil || user.Username() != "user" {+ t.Errorf("proxy credentials were dropped: %v", proxy.User)+ }+}++// TestTransportWithoutProxyKeepsEnvironmentBehaviour: with nothing configured the SDK's own+// environment-driven resolution is left alone, so a machine-wide proxy still works.+func TestTransportWithoutProxyKeepsEnvironmentBehaviour(t *testing.T) {+ t.Setenv("HTTPS_PROXY", "http://env-proxy.internal:8080")++ transport, err := newTransport("")+ if err != nil {+ t.Fatalf("newTransport: %v", err)+ }+ if transport.Proxy == nil {+ t.Fatal("transport has no proxy resolver")+ }++ request, err := http.NewRequest(http.MethodGet, "https://update.pentagi.com/", nil)+ if err != nil {+ t.Fatalf("NewRequest: %v", err)+ }++ proxy, err := transport.Proxy(request)+ if err != nil {+ t.Fatalf("proxy resolver: %v", err)+ }+ if proxy == nil || proxy.Host != "env-proxy.internal:8080" {+ t.Errorf("environment proxy was not honoured: %v", proxy)+ }+}++func TestTransportRejectsUnusableProxy(t *testing.T) {+ for _, raw := range []string{"://nope", "not a url at all"} {+ if _, err := newTransport(raw); err == nil {+ t.Errorf("newTransport(%q) accepted an unusable proxy", raw)+ }+ }+}++// TestUnusableLicenseKeyIsReportedNotSwallowed covers the one place the SDK is quietly+// permissive: it drops a key it cannot decode and carries on anonymously. That looks+// exactly like the license not being honoured, so the client checks the key itself and+// keeps the reason.+func TestUnusableLicenseKeyIsReportedNotSwallowed(t *testing.T) {+ client, err := New(Config{LicenseKey: "NOT-A-REAL-LICENSE"})+ if err != nil {+ t.Fatalf("New must stay usable without a license: %v", err)+ }+ if client.LicenseWarning() == nil {+ t.Fatal("an unusable license key was accepted silently")+ }+ if client.checkUpdates == nil {+ t.Error("the update check must still work anonymously")+ }+}++func TestNoLicenseKeyIsNotAWarning(t *testing.T) {+ client, err := New(Config{})+ if err != nil {+ t.Fatalf("New: %v", err)+ }+ if warning := client.LicenseWarning(); warning != nil {+ t.Errorf("running without a license is normal, got warning: %v", warning)+ }+}++func TestNewRejectsUnusableConfiguration(t *testing.T) {+ if _, err := New(Config{Host: "http://update.pentagi.com"}); err == nil {+ t.Error("New accepted a plaintext update server")+ }+ if _, err := New(Config{ProxyURL: "://nope"}); err == nil {+ t.Error("New accepted an unusable proxy")+ }+}++// TestNewNormalisesVersionButKeepsTheBuildName: the request carries a version the contract+// accepts, while the User-Agent keeps the exact build so a development build is still+// identifiable in the server's logs.+func TestNewNormalisesVersionButKeepsTheBuildName(t *testing.T) {+ client, err := New(Config{InstallerVersion: "develop-87ac00f"})+ if err != nil {+ t.Fatalf("New: %v", err)+ }+ if got := client.InstallerVersion(); got != UnknownVersion {+ t.Errorf("InstallerVersion() = %q, want %q", got, UnknownVersion)+ }+ if got := client.Host(); got != DefaultHost {+ t.Errorf("Host() = %q, want %q", got, DefaultHost)+ }+}diff --git a/backend/cmd/installer/cloud/errors.go b/backend/cmd/installer/cloud/errors.gonew file mode 100644index 00000000..d9dc09be--- /dev/null+++ b/backend/cmd/installer/cloud/errors.go@@ -0,0 +1,127 @@+package cloud++import (+ "context"+ "errors"+ "time"++ "github.com/vxcontrol/cloud/sdk"+)++// FailureReason classifies why a call to the update server did not produce an answer.+//+// The distinction matters to the user, not just to the log: "the server is unreachable"+// invites checking the network, "the daily quota is spent" invites coming back tomorrow,+// and "this request was refused" means retrying will never help. Collapsing all of them+// into one "update server unavailable" flag — as the installer used to — tells the user+// nothing they can act on.+type FailureReason string++const (+ // FailureNone is the zero value: no failure.+ FailureNone FailureReason = ""+ // FailureUnreachable covers everything that never reached a verdict: no network, no+ // DNS, a proxy that refused, a connection that died mid-flight.+ FailureUnreachable FailureReason = "unreachable"+ // FailureTimeout is a deadline: either the caller's, or the proof-of-work challenge+ // taking longer than the budget on slow hardware.+ FailureTimeout FailureReason = "timeout"+ // FailureRateLimited means too many requests in a rolling window. RetryAfter says when+ // the window reopens.+ FailureRateLimited FailureReason = "rate_limited"+ // FailureQuotaExceeded means the allowance for the period is spent. RetryAfter says+ // when it resets.+ FailureQuotaExceeded FailureReason = "quota_exceeded"+ // FailureForbidden means this client is not allowed to make this call at all — a+ // license tier without the endpoint, or a temporarily blocked client. Retrying does+ // not help; the license does.+ FailureForbidden FailureReason = "forbidden"+ // FailureNotFound means the server has no such package or version. It is an answer,+ // not an outage.+ FailureNotFound FailureReason = "not_found"+ // FailureRejected means the server refused the request as malformed. That is our bug,+ // not the user's, and retrying an identical request cannot succeed.+ FailureRejected FailureReason = "rejected"+ // FailureServerError means the server failed on its side. Worth retrying later.+ FailureServerError FailureReason = "server_error"+)++// Retryable reports whether waiting and trying again could plausibly succeed.+func (r FailureReason) Retryable() bool {+ switch r {+ case FailureUnreachable, FailureTimeout, FailureRateLimited,+ FailureQuotaExceeded, FailureServerError:+ return true+ default:+ return false+ }+}++// Failure is a classified call error. It wraps the original, so errors.Is and errors.As+// against the SDK sentinels keep working on it.+type Failure struct {+ Reason FailureReason+ // RetryAfter is the server-advertised cooldown, zero when the server did not say.+ RetryAfter time.Duration+ Err error+}++func (f *Failure) Error() string { return f.Err.Error() }++func (f *Failure) Unwrap() error { return f.Err }++// Classify maps an SDK error onto a reason the interface can act on. It returns nil for a+// nil error, so it can wrap a call result directly.+//+// Ordering matters here: the typed errors are checked before the sentinels they wrap,+// because that is where the Retry-After cooldown lives.+func Classify(err error) *Failure {+ if err == nil {+ return nil+ }++ var quotaErr *sdk.QuotaError+ if errors.As(err, "aErr) {+ // A blocked scope is not an exhausted allowance — the tier never had access, so+ // there is nothing to wait for.+ if quotaErr.Scope == sdk.QuotaScopeBlocked {+ return &Failure{Reason: FailureForbidden, Err: err}+ }+ return &Failure{Reason: FailureQuotaExceeded, RetryAfter: sdk.RetryAfterOf(err), Err: err}+ }++ var rateLimitErr *sdk.RateLimitError+ if errors.As(err, &rateLimitErr) {+ return &Failure{Reason: FailureRateLimited, RetryAfter: sdk.RetryAfterOf(err), Err: err}+ }++ switch {+ case errors.Is(err, sdk.ErrForbidden), errors.Is(err, sdk.ErrBlocked):+ return &Failure{Reason: FailureForbidden, Err: err}++ case errors.Is(err, sdk.ErrNotFound):+ return &Failure{Reason: FailureNotFound, Err: err}++ // A rejected request means we built it wrong. The replay and signature errors land+ // here too: both mean the server refused what we sent, and resending it is futile.+ case errors.Is(err, sdk.ErrBadRequest),+ errors.Is(err, sdk.ErrInvalidRequest),+ errors.Is(err, sdk.ErrInvalidConfiguration),+ errors.Is(err, sdk.ErrInvalidSignature),+ errors.Is(err, sdk.ErrReplayAttack):+ return &Failure{Reason: FailureRejected, Err: err}++ case errors.Is(err, sdk.ErrServerInternal), errors.Is(err, sdk.ErrBadGateway):+ return &Failure{Reason: FailureServerError, Err: err}++ // The proof-of-work budget is not retried by the SDK, so a client on slow hardware+ // has to be told what happened rather than shown a generic network error.+ case errors.Is(err, context.DeadlineExceeded),+ errors.Is(err, sdk.ErrExperimentTimeout),+ errors.Is(err, sdk.ErrPoWFailed):+ return &Failure{Reason: FailureTimeout, Err: err}++ default:+ return &Failure{Reason: FailureUnreachable, Err: err}+ }+}diff --git a/backend/cmd/installer/cloud/errors_test.go b/backend/cmd/installer/cloud/errors_test.gonew file mode 100644index 00000000..e6e7dc9a--- /dev/null+++ b/backend/cmd/installer/cloud/errors_test.go@@ -0,0 +1,149 @@+package cloud++import (+ "context"+ "errors"+ "fmt"+ "testing"+ "time"++ "github.com/vxcontrol/cloud/sdk"+)++func TestClassify(t *testing.T) {+ tests := []struct {+ name string+ err error+ wantReason FailureReason+ wantRetryAfter time.Duration+ }{+ {name: "no error", err: nil},++ {+ name: "forbidden",+ err: sdk.ErrForbidden,+ wantReason: FailureForbidden,+ },+ {+ name: "blocked client",+ err: sdk.ErrBlocked,+ wantReason: FailureForbidden,+ },+ {+ // A tier without access to the endpoint is not an exhausted allowance: there+ // is no reset to wait for, so it must not be presented as "try again later".+ name: "quota blocked is a permission problem, not a wait",+ err: &sdk.QuotaError{Err: sdk.ErrQuotaBlocked, Scope: sdk.QuotaScopeBlocked},+ wantReason: FailureForbidden,+ },+ {+ name: "daily quota carries its reset",+ err: &sdk.QuotaError{+ Err: sdk.ErrQuotaExceededDaily, Scope: sdk.QuotaScopeDaily, RetryAfter: 3 * time.Hour,+ },+ wantReason: FailureQuotaExceeded,+ wantRetryAfter: 3 * time.Hour,+ },+ {+ name: "rate limit carries its window",+ err: &sdk.RateLimitError{+ Err: sdk.ErrTooManyRequestsRPH, Scope: sdk.RateLimitScopeRPH, RetryAfter: 42 * time.Second,+ },+ wantReason: FailureRateLimited,+ wantRetryAfter: 42 * time.Second,+ },+ {+ name: "not found is an answer",+ err: sdk.ErrNotFound,+ wantReason: FailureNotFound,+ },+ {+ name: "malformed request is ours to fix",+ err: sdk.ErrBadRequest,+ wantReason: FailureRejected,+ },+ {+ name: "server side failure",+ err: sdk.ErrServerInternal,+ wantReason: FailureServerError,+ },+ {+ name: "bad gateway",+ err: sdk.ErrBadGateway,+ wantReason: FailureServerError,+ },+ {+ // The SDK does not retry a timed-out challenge, so the user has to be told+ // this was a budget rather than a broken network.+ name: "proof of work budget",+ err: sdk.ErrExperimentTimeout,+ wantReason: FailureTimeout,+ },+ {+ name: "caller deadline",+ err: context.DeadlineExceeded,+ wantReason: FailureTimeout,+ },+ {+ name: "anything else is a reachability problem",+ err: errors.New("dial tcp: no route to host"),+ wantReason: FailureUnreachable,+ },+ {+ // Errors arrive wrapped by the call layer, so classification has to see+ // through the wrapping rather than compare at the top level.+ name: "wrapped errors are still classified",+ err: fmt.Errorf("update check failed: %w", sdk.ErrForbidden),+ wantReason: FailureForbidden,+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ failure := Classify(tt.err)++ if tt.err == nil {+ if failure != nil {+ t.Fatalf("Classify(nil) = %v, want nil", failure)+ }+ return+ }++ if failure == nil {+ t.Fatalf("Classify(%v) = nil, want reason %q", tt.err, tt.wantReason)+ }+ if failure.Reason != tt.wantReason {+ t.Errorf("reason = %q, want %q", failure.Reason, tt.wantReason)+ }+ if failure.RetryAfter != tt.wantRetryAfter {+ t.Errorf("retry after = %v, want %v", failure.RetryAfter, tt.wantRetryAfter)+ }+ if !errors.Is(failure, tt.err) {+ t.Errorf("the original error is no longer reachable through the classification")+ }+ })+ }+}++// TestRetryableMatchesWhatWaitingCanFix guards the property the interface acts on. Telling+// a user to wait out a forbidden endpoint wastes their time; not telling them about a+// quota that resets in an hour wastes the license.+func TestRetryableMatchesWhatWaitingCanFix(t *testing.T) {+ retryable := map[FailureReason]bool{+ FailureUnreachable: true,+ FailureTimeout: true,+ FailureRateLimited: true,+ FailureQuotaExceeded: true,+ FailureServerError: true,+ FailureForbidden: false,+ FailureNotFound: false,+ FailureRejected: false,+ FailureNone: false,+ }++ for reason, want := range retryable {+ if got := reason.Retryable(); got != want {+ t.Errorf("%q.Retryable() = %t, want %t", reason, got, want)+ }+ }+}diff --git a/backend/cmd/installer/cloud/packages.go b/backend/cmd/installer/cloud/packages.gonew file mode 100644index 00000000..2d16fcf1--- /dev/null+++ b/backend/cmd/installer/cloud/packages.go@@ -0,0 +1,114 @@+package cloud++import (+ "context"+ "crypto/sha256"+ "encoding/hex"+ "fmt"+ "io"+ "strings"++ "github.com/vxcontrol/cloud/models"+)++// PackageInfo asks the update server about one downloadable package: its size, its+// sha256 and the signature over it.+//+// The answer is validated before it is returned, because everything DownloadPackage checks+// is taken from it — an unverified description of a file is not a basis for trusting the+// file.+func (c *Client) PackageInfo(+ ctx context.Context,+ req models.PackageInfoRequest,+) (*models.PackageInfoResponse, error) {+ if c == nil || c.packageInfo == nil {+ return nil, fmt.Errorf("update client is not initialized")+ }++ if err := req.Valid(); err != nil {+ return nil, fmt.Errorf("package info request is malformed: %w", err)+ }++ answer, err := c.packageInfo(ctx, req.Query())+ if err != nil {+ return nil, Classify(err)+ }++ response, err := models.ParseEnvelope[models.PackageInfoResponse](answer)+ if err != nil {+ return nil, fmt.Errorf("failed to read package info answer: %w", err)+ }++ if err := response.Valid(); err != nil {+ return nil, fmt.Errorf("package info answer is malformed: %w", err)+ }++ return &response, nil+}++// DownloadPackage streams a package into dst, verifying it against info as the bytes+// arrive.+//+// Three things are checked, and all three describe the file itself: the transport+// encryption is transparent and changes none of them. The length must match info.Size —+// the encrypted response carries no Content-Length, so this is the only length the caller+// ever learns. The sha256 must match info.Hash. The Ed25519 signature must verify against+// the file's SHA-512 digest, which is what makes the package ours rather than merely+// intact.+//+// Verification can only complete once the last byte has been written, so on any error dst+// holds a partial or unverified file. It is the caller's job to discard it — write to a+// file that is only put in place after this returns nil.+func (c *Client) DownloadPackage(+ ctx context.Context,+ req models.DownloadPackageRequest,+ info models.PackageInfoResponse,+ dst io.Writer,+) error {+ if c == nil || c.downloadPackage == nil {+ return fmt.Errorf("update client is not initialized")+ }++ if err := req.Valid(); err != nil {+ return fmt.Errorf("package download request is malformed: %w", err)+ }+ if err := info.Valid(); err != nil {+ return fmt.Errorf("package description is malformed: %w", err)+ }++ digest := sha256.New()+ counter := &countingWriter{}+ // The signature wrapper hashes with SHA-512 on the way through; the tee adds the+ // sha256 and the byte count, so a single pass over the stream feeds all three checks.+ signed := info.Signature.ValidateWrapWriter(io.MultiWriter(dst, digest, counter))++ if err := c.downloadPackage(ctx, req.Query(), signed); err != nil {+ return Classify(err)+ }++ if counter.written != info.Size {+ return fmt.Errorf("package is %d bytes, expected %d", counter.written, info.Size)+ }++ actual := hex.EncodeToString(digest.Sum(nil))+ if !strings.EqualFold(actual, info.Hash) {+ return fmt.Errorf("package sha256 is %s, expected %s", actual, info.Hash)+ }++ if err := signed.Valid(); err != nil {+ return fmt.Errorf("package signature does not verify: %w", err)+ }++ return nil+}++// countingWriter counts the bytes passing through it and discards them; the real+// destination sits alongside it in the MultiWriter.+type countingWriter struct {+ written int64+}++func (w *countingWriter) Write(p []byte) (int, error) {+ w.written += int64(len(p))+ return len(p), nil+}diff --git a/backend/cmd/installer/cloud/updates.go b/backend/cmd/installer/cloud/updates.gonew file mode 100644index 00000000..b061cb9b--- /dev/null+++ b/backend/cmd/installer/cloud/updates.go@@ -0,0 +1,52 @@+package cloud++import (+ "context"+ "encoding/json"+ "fmt"++ "github.com/vxcontrol/cloud/models"+)++// CheckUpdates asks the update server what is available for the components described in+// req, and returns the answer per product stack.+//+// The request is validated before it leaves: a malformed one is refused by the server as an+// internal error, which is indistinguishable from an outage and equally pointless to retry.+// Catching it here names the offending field instead.+//+// The component list must describe the whole installation, not just the parts that look+// suspicious: the server answers only about what it was told, so anything left out comes+// back as "nothing to report" rather than "not asked".+func (c *Client) CheckUpdates(+ ctx context.Context,+ req models.CheckUpdatesRequest,+) (*models.CheckUpdatesResponse, error) {+ if c == nil || c.checkUpdates == nil {+ return nil, fmt.Errorf("update client is not initialized")+ }++ if err := req.Valid(); err != nil {+ return nil, fmt.Errorf("update check request is malformed: %w", err)+ }++ body, err := json.Marshal(req)+ if err != nil {+ return nil, fmt.Errorf("failed to encode update check request: %w", err)+ }++ answer, err := c.checkUpdates(ctx, body)+ if err != nil {+ return nil, Classify(err)+ }++ // Answers are wrapped in a status envelope. Decoding straight into the response type+ // succeeds against the wrapper too — every field simply stays at its zero value — so+ // the unwrapping is not optional: skipping it reports "no updates" for every answer.+ response, err := models.ParseEnvelope[models.CheckUpdatesResponse](answer)+ if err != nil {+ return nil, fmt.Errorf("failed to read update check answer: %w", err)+ }++ return &response, nil+}diff --git a/backend/cmd/installer/cloud/version.go b/backend/cmd/installer/cloud/version.gonew file mode 100644index 00000000..fddc9fd0--- /dev/null+++ b/backend/cmd/installer/cloud/version.go@@ -0,0 +1,48 @@+package cloud++import (+ "strings"++ "github.com/vxcontrol/cloud/models"+)++// UnknownVersion is what a build reports when its own version cannot be expressed as a+// version number — the conventional stand-in for "some build, older than anything we+// publish". Development builds are named after their branch, not a release, so this is the+// normal case outside CI.+const UnknownVersion = "0.0.0"++// latestVersion is accepted by the update server as "the newest published version" when+// asking about a package. It is meaningless as a description of the installer you are+// running, so it is never sent as one.+const latestVersion = "latest"++// NormalizeVersion converts a build version into one the update server accepts.+//+// A leading "v" is dropped, since it is a display convention rather than part of the+// number. Anything that still does not parse as a version — a branch name such as+// "develop", a bare commit hash, an empty string — becomes UnknownVersion. The original+// value is not lost: it travels in the User-Agent, where it identifies the exact build+// without having to satisfy the version grammar.+//+// The check is delegated to the shared validator rather than re-implemented here, so the+// client and the server can never disagree about what a valid version looks like.+func NormalizeVersion(raw string) string {+ value := strings.TrimSpace(raw)+ value = strings.TrimPrefix(value, "v")++ if value == "" || value == latestVersion {+ return UnknownVersion+ }++ if err := validateSemver(value); err != nil {+ return UnknownVersion+ }++ return value+}++// validateSemver reports whether the update server would accept value as a version.+func validateSemver(value string) error {+ return models.GetValidator().Var(value, "semver")+}diff --git a/backend/cmd/installer/cloud/version_test.go b/backend/cmd/installer/cloud/version_test.gonew file mode 100644index 00000000..17d45a31--- /dev/null+++ b/backend/cmd/installer/cloud/version_test.go@@ -0,0 +1,58 @@+package cloud++import "testing"++func TestNormalizeVersion(t *testing.T) {+ tests := []struct {+ name string+ raw string+ want string+ }{+ // A release build and a branch build both have to round-trip unchanged: the+ // revision suffix is what identifies a build cut from a branch, and dropping it+ // would make every such build look like the release it branched from.+ {name: "release version", raw: "2.0.0", want: "2.0.0"},+ {name: "branch build with revision", raw: "2.0.0-87ac00f", want: "2.0.0-87ac00f"},+ {name: "leading v is a display convention", raw: "v2.0.0", want: "2.0.0"},+ {name: "surrounding whitespace", raw: " 2.0.0 ", want: "2.0.0"},+ {name: "two-part version", raw: "2.0", want: "2.0"},+ {name: "build metadata", raw: "2.0.0+build.5", want: "2.0.0+build.5"},++ // Everything a development build can be named. None of these is a version, and+ // sending them gets the whole request refused.+ {name: "branch name", raw: "develop", want: UnknownVersion},+ {name: "branch name with revision", raw: "develop-87ac00f", want: UnknownVersion},+ {name: "empty", raw: "", want: UnknownVersion},+ {name: "whitespace only", raw: " ", want: UnknownVersion},+ {name: "bare commit hash", raw: "87ac00f", want: UnknownVersion},++ // "latest" is a valid thing to ask a package endpoint for, and a meaningless+ // description of the installer you are running.+ {name: "latest is never an installer version", raw: "latest", want: UnknownVersion},+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ if got := NormalizeVersion(tt.raw); got != tt.want {+ t.Errorf("NormalizeVersion(%q) = %q, want %q", tt.raw, got, tt.want)+ }+ })+ }+}++// TestNormalizedVersionAlwaysPassesTheContract is the invariant the table above only+// samples: whatever a build calls itself, what leaves the installer is acceptable. Without+// it, a new build-naming scheme would be caught in production rather than here.+func TestNormalizedVersionAlwaysPassesTheContract(t *testing.T) {+ raws := []string{+ "develop", "develop-87ac00f", "", " ", "latest", "v2.0.0", "2.0.0-87ac00f",+ "feature/some-branch", "2.0.0.1", "release_candidate", "v", "..", "1.2.3.4.5",+ }++ for _, raw := range raws {+ normalized := NormalizeVersion(raw)+ if err := validateSemver(normalized); err != nil {+ t.Errorf("NormalizeVersion(%q) = %q, which the contract rejects: %v", raw, normalized, err)+ }+ }+}diff --git a/backend/cmd/installer/hardening/hardening_test.go b/backend/cmd/installer/hardening/hardening_test.goindex f840612e..28f43106 100644--- a/backend/cmd/installer/hardening/hardening_test.go+++ b/backend/cmd/installer/hardening/hardening_test.go@@ -1579,3 +1579,5 @@ func isHexString(s string) bool {}return true}++func (m *mockState) WriteVars(vars map[string]string) error { return m.SetVars(vars) }diff --git a/backend/cmd/installer/hardening/network_test.go b/backend/cmd/installer/hardening/network_test.goindex bfa8321b..f7192170 100644--- a/backend/cmd/installer/hardening/network_test.go+++ b/backend/cmd/installer/hardening/network_test.go@@ -1049,3 +1049,5 @@ func TestDoSyncNetworkSettings_PreventOverrideExistingSettings(t *testing.T) {})}}++func (m *mockStateWithErrors) WriteVars(vars map[string]string) error { return m.SetVars(vars) }diff --git a/backend/cmd/installer/navigator/navigator_test.go b/backend/cmd/installer/navigator/navigator_test.goindex 9872414d..8a787940 100644--- a/backend/cmd/installer/navigator/navigator_test.go+++ b/backend/cmd/installer/navigator/navigator_test.go@@ -319,3 +319,5 @@ func TestStateIntegrationWithExistingStack(t *testing.T) {t.Errorf("expected 2 screens in navigator stack, got %v", nav.GetStack())}}++func (m *mockState) WriteVars(map[string]string) error { return nil }diff --git a/backend/cmd/installer/processor/compose.go b/backend/cmd/installer/processor/compose.goindex c8f9a2e5..42045d9e 100644--- a/backend/cmd/installer/processor/compose.go+++ b/backend/cmd/installer/processor/compose.go@@ -61,11 +61,28 @@ func (c *composeOperationsImpl) restartStack(ctx context.Context, stack ProductS}// updateStack performs rolling update with health checks (also used for install)+// updateStack and downloadStack are the only two compose operations that FETCH anything,+// and both write the image references the server chose into the environment file first.+//+// Here rather than at each call site, because there are five of them — update, download,+// install and the three apply-changes phases — and the one that forgets is not a compile+// error: `${COMPONENT_IMAGE:-default}` quietly resolves to the default the compose file+// ships with, the pull fetches that, and the operation reports success having fetched+// something the update server never offered.+//+// The tear-down operations below deliberately do not: `down` needs no reference, and+// writing one while removing a stack would be a side effect nobody asked for.func (c *composeOperationsImpl) updateStack(ctx context.Context, stack ProductStack, state *operationState) error {+ if err := c.processor.applyPullReferences(stack); err != nil {+ return err+ }return c.performStackOperation(ctx, stack, state, ProcessorOperationUpdate, "up", "-d")}func (c *composeOperationsImpl) downloadStack(ctx context.Context, stack ProductStack, state *operationState) error {+ if err := c.processor.applyPullReferences(stack); err != nil {+ return err+ }return c.performStackOperation(ctx, stack, state, ProcessorOperationDownload, "pull")}diff --git a/backend/cmd/installer/processor/fs.go b/backend/cmd/installer/processor/fs.goindex 7c2605c6..be8ab40c 100644--- a/backend/cmd/installer/processor/fs.go+++ b/backend/cmd/installer/processor/fs.go@@ -35,6 +35,20 @@ var filesToExcludeFromVerification = []string{"graphiti/gemini.yaml","graphiti/litellm.yaml","graphiti/openai.yaml",+ // The Jaeger storage plugin is updated over the network, so the embedded copy and the+ // cloud are two sources of truth for one path and the priority has to be declared.+ // Without this the update undoes itself: a downloaded plugin differs from what the+ // installer ships, so verifyDirectoryContentIntegrity calls it modified. On every+ // Apply Changes the user is then offered a "repair" — an offer to roll the update+ // back, dressed up as tidying — and answering yes sets force, which copies the+ // embedded version straight over the downloaded one. Factory reset does the same+ // without asking.+ //+ // The exclusion policy is exactly the one this needs: still created when missing, so+ // the embedded copy remains the source for a first install, and never overwritten+ // afterwards, so the cloud becomes the source once it has answered.+ "observability/jaeger/bin/jaeger-clickhouse-linux-amd64",+ "observability/jaeger/bin/jaeger-clickhouse-linux-arm64",}var allStacks = []ProductStack{diff --git a/backend/cmd/installer/processor/jaeger_plugin.go b/backend/cmd/installer/processor/jaeger_plugin.gonew file mode 100644index 00000000..b8fdf434--- /dev/null+++ b/backend/cmd/installer/processor/jaeger_plugin.go@@ -0,0 +1,226 @@+package processor++import (+ "context"+ "fmt"+ "io"+ "os"+ "path/filepath"++ "pentagi/cmd/installer/checker"++ "github.com/vxcontrol/cloud/models"+)++// stagingSuffix is what a plugin file is called while it is still arriving.+//+// The staging file sits in the SAME directory as its target, not in the system temp+// directory: os.Rename is only atomic within one filesystem, and /tmp is routinely a+// different one. It is also a directory antivirus and EDR products watch for executables+// being written and then moved, which is exactly the shape of this operation.+const stagingSuffix = ".new"++// jaegerServiceName is the compose service the plugin is mounted into. Restarting it is+// how a replaced plugin takes effect — the entrypoint exec's the file at start.+const jaegerServiceName = "jaeger"++// updateJaegerPlugin brings the Jaeger storage plugin up to what the server offers.+//+// The plugin is not an image: it is a binary mounted into the Jaeger container, which+// loads it over gRPC. Both architectures are kept in step because which one runs is+// decided inside the container by the architecture of the Docker VM — not by this host —+// and the compose entrypoint picks the file by `uname -m` at start.+//+// Runs BEFORE the images are pulled. A plugin that cannot be fetched then aborts the stack+// update with nothing else disturbed; doing it after would leave images pulled and the+// stack restarted around a plugin that never arrived.+//+// Reports whether anything was actually replaced, because the caller has to restart the+// Jaeger service when it was: `docker compose up -d` recreates a container only when its+// SPEC changes, and a different file inside a bind mount is not a spec change. The plugin+// is exec'd by the container's entrypoint at start, so without a restart the container+// goes on running the plugin it loaded when it came up, and the update looks applied while+// nothing about it is.+func (u *updateOperationsImpl) updateJaegerPlugin(+ ctx context.Context, state *operationState,+) (bool, error) {+ if u.processor.checker == nil {+ return false, nil+ }++ base := filepath.Join(u.environmentDir(), checker.JaegerPluginDir)++ // Always, even when there is nothing to fetch: an interrupted attempt leaves a ~28 MB+ // file that nothing else will ever remove. Integrity verification walks the files the+ // installer ships and does not see strangers, so this is the only thing that looks.+ u.removeStagedPlugins(base, state)++ offered := jaegerPluginUpdates(u.processor.checker.StackUpdates)+ if len(offered) == 0 {+ return false, nil+ }++ for _, component := range offered {+ if err := u.fetchJaegerPlugin(ctx, base, component, state); err != nil {+ return false, err+ }+ }++ return true, nil+}++// jaegerPluginUpdates picks the plugin components that actually need fetching.+//+// Membership in the answer is not the test — under a curated release the server names+// every artefact it knows about, whether or not it differs from what is installed. What+// makes a component here is the digest comparison the checker already did.+func jaegerPluginUpdates(updates []checker.StackUpdate) []checker.ComponentUpdate {+ var wanted []checker.ComponentUpdate+ for _, update := range updates {+ for _, component := range update.Components {+ if component.Component != string(models.ComponentTypeJaegerClickhouse) {+ continue+ }+ // An offered file always carries a version — a published binary cannot exist+ // without one — so the empty case is defence, not a scenario: the package+ // request requires a version and has no field for a hash, so asking without+ // one buys a guaranteed rejection instead of a download.+ if !component.Outdated || component.TargetVersion == "" {+ continue+ }+ // The file name says "linux" in it, so an answer for another platform would+ // put non-linux content under a linux name. The server has never sent one;+ // this makes sure a change on that side is skipped rather than written.+ if component.OS != string(models.OSTypeLinux) {+ continue+ }+ wanted = append(wanted, component)+ }+ }++ return wanted+}++// removeStagedPlugins deletes leftovers from an interrupted attempt.+//+// By exact name rather than by globbing "*.new": this directory is mounted into a+// container and an operator may keep their own files in it, and a delete that guesses is+// how a tool destroys something it was never asked to touch.+func (u *updateOperationsImpl) removeStagedPlugins(base string, state *operationState) {+ for _, name := range checker.JaegerPluginBinaries {+ staged := filepath.Join(base, name+stagingSuffix)+ if _, err := os.Stat(staged); err != nil {+ continue+ }+ if err := os.Remove(staged); err != nil {+ u.processor.appendLog(+ fmt.Sprintf(MsgJaegerPluginStaleRemoveFailed, staged, err),+ ProductStackObservability, state)+ continue+ }+ u.processor.appendLog(fmt.Sprintf(MsgJaegerPluginStaleRemoved, staged),+ ProductStackObservability, state)+ }+}++// fetchJaegerPlugin downloads one architecture's plugin and puts it in place.+func (u *updateOperationsImpl) fetchJaegerPlugin(+ ctx context.Context, base string, component checker.ComponentUpdate, state *operationState,+) error {+ arch := models.ArchType(component.Arch)+ name, known := checker.JaegerPluginBinaries[arch]+ if !known {+ return fmt.Errorf("no jaeger plugin file is defined for architecture %s", component.Arch)+ }++ u.processor.appendLog(fmt.Sprintf(MsgJaegerPluginDownloading, name, component.TargetVersion),+ ProductStackObservability, state)++ client, err := u.cloudClient()+ if err != nil {+ return err+ }++ request := models.PackageInfoRequest{+ Component: models.ComponentTypeJaegerClickhouse,+ Version: component.TargetVersion,+ OS: models.OSTypeLinux,+ Arch: arch,+ }+ info, err := client.PackageInfo(ctx, request)+ if err != nil {+ return fmt.Errorf("failed to get jaeger plugin package info for %s: %w", arch, err)+ }++ progress := &progressWriter{total: info.Size, report: func(percent int, written, total int64) {+ u.processor.appendLog(+ fmt.Sprintf(MsgJaegerPluginProgress, name, percent, FormatSize(written), FormatSize(total)),+ ProductStackObservability, state)+ }}++ target := filepath.Join(base, name)+ err = installPluginFile(target, func(w io.Writer) error {+ return client.DownloadPackage(ctx, models.DownloadPackageRequest{+ Component: models.ComponentTypeJaegerClickhouse,+ Version: component.TargetVersion,+ OS: models.OSTypeLinux,+ Arch: arch,+ }, *info, io.MultiWriter(w, progress))+ })+ if err != nil {+ return fmt.Errorf("failed to install jaeger plugin for %s: %w", arch, err)+ }++ u.processor.appendLog(fmt.Sprintf(MsgJaegerPluginInstalled, target),+ ProductStackObservability, state)++ return nil+}++// installPluginFile streams into <target>.new and moves it over target only once write+// has returned without complaint.+//+// Staged rather than written in place, because the file being replaced is the one a+// running Jaeger loads: a download that dies halfway would otherwise leave a truncated+// plugin, and the container would come back up unable to start with an opaque gRPC error.+// The staging file is a sibling of the target so the rename stays within one filesystem,+// where it is atomic.+func installPluginFile(target string, write func(io.Writer) error) error {+ // The stack may never have been installed, in which case the directory the mount+ // expects does not exist yet.+ if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {+ return fmt.Errorf("failed to create %s: %w", filepath.Dir(target), err)+ }++ staged := target + stagingSuffix++ // 0755 at CREATION and not afterwards: os.Rename does not preserve the destination's+ // mode — the result keeps the staging file's — so a plugin created 0644 and renamed+ // over an executable one ends up not executable, and Jaeger fails to start with an+ // opaque gRPC plugin error rather than a permission one.+ file, err := os.OpenFile(staged, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o755)+ if err != nil {+ return fmt.Errorf("failed to create %s: %w", staged, err)+ }++ writeErr := write(file)++ if closeErr := file.Close(); closeErr != nil && writeErr == nil {+ writeErr = fmt.Errorf("failed to finish writing %s: %w", staged, closeErr)+ }++ if writeErr != nil {+ // The length, the sha256 and the signature are only decided once the last byte has+ // arrived, so what is on disk here is unverified. The target is untouched — that is+ // the entire reason for staging — and the installation keeps the plugin it had.+ _ = os.Remove(staged)+ return writeErr+ }++ if err := os.Rename(staged, target); err != nil {+ _ = os.Remove(staged)+ return fmt.Errorf("failed to put %s in place: %w", target, err)+ }++ return nil+}diff --git a/backend/cmd/installer/processor/jaeger_plugin_test.go b/backend/cmd/installer/processor/jaeger_plugin_test.gonew file mode 100644index 00000000..ce6cb2e2--- /dev/null+++ b/backend/cmd/installer/processor/jaeger_plugin_test.go@@ -0,0 +1,199 @@+package processor++import (+ "errors"+ "fmt"+ "io"+ "os"+ "path/filepath"+ "slices"+ "testing"++ "pentagi/cmd/installer/checker"++ "github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"+ "github.com/vxcontrol/cloud/models"+)++// TestOnlyOutdatedLinuxPluginsAreFetched.+//+// Membership in the answer is not the test: under a curated release the server names every+// artefact it knows about, current ones included, so fetching everything listed would+// re-download 28 MB per architecture on every observability update forever.+//+// The OS check is the other half. The file name has "linux" inside it because the plugin+// runs in the Jaeger container whatever the host is, so an answer for another platform+// would write non-linux content under a linux name — and Jaeger would fail to start.+func TestOnlyOutdatedLinuxPluginsAreFetched(t *testing.T) {+ updates := []checker.StackUpdate{+ {+ Stack: "observability",+ Components: []checker.ComponentUpdate{+ {Component: "jaeger-clickhouse", OS: "linux", Arch: "amd64", Outdated: true, TargetVersion: "0.13.0"},+ {Component: "jaeger-clickhouse", OS: "linux", Arch: "arm64", Outdated: true, TargetVersion: "0.13.0"},+ // Current: listed by the release, nothing to do.+ {Component: "jaeger", OS: "linux", Arch: "amd64", Outdated: true, TargetVersion: "1.56.0"},+ },+ },+ {+ Stack: "pentagi",+ Components: []checker.ComponentUpdate{+ {Component: "pentagi", OS: "linux", Arch: "amd64", Outdated: true, TargetVersion: "2.3.4"},+ },+ },+ }++ wanted := jaegerPluginUpdates(updates)+ require.Len(t, wanted, 2, "both architectures are kept in step: %+v", wanted)+ assert.Equal(t, "amd64", wanted[0].Arch)+ assert.Equal(t, "arm64", wanted[1].Arch)++ // Every reason a component must be skipped, one at a time.+ for name, component := range map[string]checker.ComponentUpdate{+ "already current": {Component: "jaeger-clickhouse", OS: "linux", Arch: "amd64", Outdated: false, TargetVersion: "0.13.0"},+ "no version to ask": {Component: "jaeger-clickhouse", OS: "linux", Arch: "amd64", Outdated: true, TargetVersion: ""},+ "not linux": {Component: "jaeger-clickhouse", OS: "darwin", Arch: "amd64", Outdated: true, TargetVersion: "0.13.0"},+ "another component": {Component: "installer", OS: "linux", Arch: "amd64", Outdated: true, TargetVersion: "0.13.0"},+ } {+ got := jaegerPluginUpdates([]checker.StackUpdate{+ {Stack: "observability", Components: []checker.ComponentUpdate{component}},+ })+ assert.Empty(t, got, "%s must not be fetched", name)+ }+}++// TestTheComponentNameMatchesTheContract. jaegerPluginUpdates compares against a literal+// string; if the vocabulary ever renames the component, the comparison silently stops+// matching and the plugin is never updated again, with no error anywhere.+func TestTheComponentNameMatchesTheContract(t *testing.T) {+ assert.Equal(t, "jaeger-clickhouse", string(models.ComponentTypeJaegerClickhouse))+}++// TestAFailedDownloadLeavesTheRunningPluginAlone.+//+// This is what staging is for. The file being replaced is the one a running Jaeger has+// loaded, so a download that dies halfway must not be visible at the target path at all —+// a truncated plugin brings the container back up unable to start, with a gRPC error that+// says nothing about a bad download.+func TestAFailedDownloadLeavesTheRunningPluginAlone(t *testing.T) {+ dir := t.TempDir()+ target := filepath.Join(dir, "jaeger-clickhouse-linux-amd64")+ require.NoError(t, os.WriteFile(target, []byte("the plugin currently in use"), 0o755))++ err := installPluginFile(target, func(w io.Writer) error {+ // Half of it arrives, then the transfer fails — the ordinary shape of a broken+ // download, and the one that would truncate an in-place write.+ if _, err := w.Write([]byte("partial")); err != nil {+ return err+ }+ return errors.New("connection reset")+ })+ require.Error(t, err)+ assert.Contains(t, err.Error(), "connection reset")++ content, readErr := os.ReadFile(target)+ require.NoError(t, readErr)+ assert.Equal(t, "the plugin currently in use", string(content),+ "the plugin Jaeger is running was replaced by a failed download")++ assert.NoFileExists(t, target+stagingSuffix,+ "a ~28 MB leftover nothing else ever removes: integrity verification only knows the files the installer ships")+}++// TestTheInstalledPluginIsExecutable.+//+// os.Rename does NOT carry over the destination's mode — the result keeps the staging+// file's. A plugin created 0644 and renamed over an executable one ends up not executable,+// and Jaeger fails to start with an opaque gRPC plugin error rather than a permission one.+func TestTheInstalledPluginIsExecutable(t *testing.T) {+ dir := t.TempDir()+ target := filepath.Join(dir, "sub", "jaeger-clickhouse-linux-arm64")++ require.NoError(t, installPluginFile(target, func(w io.Writer) error {+ _, err := w.Write([]byte("plugin bytes"))+ return err+ }))++ info, err := os.Stat(target)+ require.NoError(t, err, "the directory the mount expects is created when the stack was never installed")+ assert.Equal(t, "plugin bytes", readFile(t, target))+ assert.NotZero(t, info.Mode()&0o111, "mode is %v; Jaeger cannot exec a plugin without the bit", info.Mode())+ assert.NoFileExists(t, target+stagingSuffix, "the staging file must not survive a success")+}++// TestStaleStagingFilesAreRemovedByExactName.+//+// An interrupted download leaves ~28 MB that nothing else will ever clean up. But this+// directory is mounted into a container and an operator may keep their own files in it, so+// the cleanup names the two files it created rather than globbing "*.new".+func TestStaleStagingFilesAreRemovedByExactName(t *testing.T) {+ dir := t.TempDir()+ base := filepath.Join(dir, checker.JaegerPluginDir)+ require.NoError(t, os.MkdirAll(base, 0o755))++ for _, name := range checker.JaegerPluginBinaries {+ require.NoError(t, os.WriteFile(filepath.Join(base, name+stagingSuffix), []byte("half"), 0o755))+ }+ somebodyElses := filepath.Join(base, "operators-own-notes.new")+ require.NoError(t, os.WriteFile(somebodyElses, []byte("keep me"), 0o644))++ u := &updateOperationsImpl{processor: &processor{}}+ u.removeStagedPlugins(base, newOperationState(nil))++ for _, name := range checker.JaegerPluginBinaries {+ assert.NoFileExists(t, filepath.Join(base, name+stagingSuffix), "%s leftover survived", name)+ }+ assert.FileExists(t, somebodyElses, "a delete that guesses is how a tool destroys what it was not asked to touch")+}++// TestThePluginIsExemptFromIntegrityVerification.+//+// Without this the update undoes itself: a downloaded plugin differs from the copy the+// installer embeds, so verifyDirectoryContentIntegrity calls it modified and — under+// force, which is the path a stack update takes — copies the embedded version back over+// it. Tied to the plugin table rather than spelled out again, so a third architecture+// cannot be added on one side only.+func TestThePluginIsExemptFromIntegrityVerification(t *testing.T) {+ fs := &fileSystemOperationsImpl{}+ for arch, name := range checker.JaegerPluginBinaries {+ path := checker.JaegerPluginDir + "/" + name+ assert.True(t, fs.isExcludedFromVerification(path),+ "%s (%s) is not excluded, so a stack update would copy the embedded copy back over the download", path, arch)+ }+}++// TestJaegerIsRestartedOnlyWhenThePluginWasReplaced.+//+// `docker compose up -d` recreates a container only when its SPEC changes, and a different+// file inside a bind mount is not a spec change. So when the plugin was replaced and the+// jaeger image was not, the `up -d` in the update path leaves the container running with+// the plugin it exec'd at start — the update reports success and Jaeger keeps writing+// traces through the old plugin. The restart is what closes that, and it must not fire+// when nothing was replaced: it is downtime nobody asked for.+func TestJaegerIsRestartedOnlyWhenThePluginWasReplaced(t *testing.T) {+ for name, replaced := range map[string]bool{"plugin replaced": true, "nothing to do": false} {+ p, composeOps, _, _ := newProcessorForLogicTestsWithConfig(t, func(config *mockCheckConfig) {+ config.ObservabilityIsUpToDate = false+ })+ p.updateOps.(*baseMockUpdateOperations).jaegerPluginReplaced = replaced++ require.NoError(t, p.update(t.Context(), ProductStackObservability, testOperationState(t)))++ restarted := false+ for _, c := range composeOps.getCalls() {+ args, _ := c.Args.([]string)+ if c.Method == "performStackCommand" && slices.Equal(args, []string{"restart", jaegerServiceName}) {+ restarted = true+ }+ }+ assert.Equal(t, replaced, restarted, "%s: jaeger restarted=%v", name, restarted)+ }+}++func readFile(t *testing.T, path string) string {+ t.Helper()+ content, err := os.ReadFile(path)+ require.NoError(t, err, fmt.Sprintf("reading %s", path))+ return string(content)+}diff --git a/backend/cmd/installer/processor/locale.go b/backend/cmd/installer/processor/locale.goindex 91e1ee60..88cedd10 100644--- a/backend/cmd/installer/processor/locale.go+++ b/backend/cmd/installer/processor/locale.go@@ -56,8 +56,37 @@ const (MsgUpdatingInstaller = "Updating installer"MsgRemovingInstaller = "Removing installer"MsgInstallerUpdateCompleted = "Installer update completed"- MsgVerifyingBinaryChecksum = "Verifying binary checksum"- MsgReplacingInstallerBinary = "Replacing installer binary"+ // Past tense: the length, the sha256 and the Ed25519 signature are all checked as the+ // stream arrives, so by the time this is printed the verdict is already in.+ MsgVerifyingBinaryChecksum = "Checksum and signature verified"+ MsgInstallerSavedTo = "Installer saved to %s"+ MsgInstallerDownloadProgress = "Downloaded %d%% (%s of %s)"+ // The installer binary outlives the removal on purpose: a process cannot delete the+ // file it is running from, and trying would leave the user with neither.+ MsgInstallerNotRemoved = "The installer itself is left in place at %s — delete it yourself when you no longer need it"+ MsgInstallerNotRemovedUnknownPath = "The installer itself is left in place — delete it yourself when you no longer need it"+)++// Jaeger storage plugin messages. The plugin is a file mounted into the Jaeger container,+// not an image, so none of the compose output covers it.+const (+ MsgJaegerPluginDownloading = "Downloading Jaeger storage plugin %s (%s)"+ MsgJaegerPluginProgress = " %s: %d%% (%s of %s)"+ MsgJaegerPluginInstalled = "Jaeger storage plugin in place: %s"+ MsgJaegerPluginStaleRemoved = "Removed leftover from an interrupted download: %s"+ MsgJaegerPluginStaleRemoveFailed = "Could not remove leftover %s: %v"+ MsgJaegerPluginRestarting = "Restarting Jaeger so it loads the new storage plugin"+)++// Post-update verification messages+const (+ MsgVerifyingUpdatedComponents = "Verifying updated %s components"+ MsgComponentUpToDate = " %s: matches the published build"+ MsgComponentNewerThanOffered = " %s: newer than the offered build (registry moved ahead)"+ MsgComponentMismatch = " %s: expected %s, got %s"+ MsgComponentNotVerifiable = " %s: no published digest to compare against"+ MsgVerificationPassed = "All updated %s components match the published build"+ MsgVerificationMismatch = "%d %s component(s) do not match the published build")// Remove operations messagesdiff --git a/backend/cmd/installer/processor/logic.go b/backend/cmd/installer/processor/logic.goindex c8c0978b..ae835176 100644--- a/backend/cmd/installer/processor/logic.go+++ b/backend/cmd/installer/processor/logic.go@@ -2,12 +2,14 @@ package processorimport ("context"+ "errors""fmt""maps""os""os/exec""path/filepath""runtime"+ "slices""sync""pentagi/cmd/installer/checker"@@ -491,19 +493,72 @@ func (p *processor) install(ctx context.Context, state *operationState) (err errreturn nil}+// update is the entry point for an update operation: it announces itself, does the work,+// and then asks the cloud ONCE what is left.+//+// That single question used to be five. The fresh check lived in a deferred call inside+// the recursive worker, so `compose` — four stacks — spent four nested checks plus this+// one, and a stack with nothing to do paid for a check too, because the defer was+// registered before the early return. One press of "Update PentAGI" cost five requests+// out of a daily budget, and told the cloud nothing it had not been told four times+// already: GatherUpdatesInfo reports the WHOLE installation, not one stack.func (p *processor) update(ctx context.Context, stack ProductStack, state *operationState) (err error) {state.sendStarted(stack)defer func() { state.sendCompletion(stack, err) }()- defer func() {- if err != nil {- return- }- // refresh state for updates- if gatherErr := p.checker.GatherUpdatesInfo(ctx); gatherErr != nil {- err = fmt.Errorf("failed to gather info after update: %w", gatherErr)++ // What the cloud currently offers, captured before the update replaces the answer with+ // a newer one, for every stack this operation will touch. Only stacks whose update+ // actually swaps what is installed are verified: the installer operation downloads a+ // build without applying it, so comparing against the running binary would report a+ // mismatch that is the intended outcome.+ targets := make(map[ProductStack][]componentTarget)+ for _, touched := range stacksTouchedByUpdate(stack) {+ if verifiableUpdateStacks[touched] {+ targets[touched] = captureStackTargets(p.checker, string(touched))}- }()+ }++ if err = p.updateStacks(ctx, stack, state); err != nil {+ return err+ }++ // The fresh answer is what decides whether anything is still pending, so it comes+ // after all the work and before the comparison.+ if gatherErr := p.checker.GatherUpdatesInfo(ctx); gatherErr != nil {+ err = fmt.Errorf("failed to gather info after update: %w", gatherErr)+ return err+ }++ for _, touched := range stacksTouchedByUpdate(stack) {+ p.verifyStackUpdate(touched, targets[touched], state)+ }++ return nil+}+// stacksTouchedByUpdate expands a requested stack into the ones an update of it actually+// runs, so their targets can be captured before anything changes.+//+// A stack that is not installed still appears here and costs nothing: captureStackTargets+// finds it in no answer and returns nil, and verifyStackUpdate returns immediately on an+// empty target list.+func stacksTouchedByUpdate(stack ProductStack) []ProductStack {+ switch stack {+ case ProductStackCompose:+ return composeOperationAllStacksOrder[ProcessorOperationUpdate]+ case ProductStackAll:+ return append(+ slices.Clone(composeOperationAllStacksOrder[ProcessorOperationUpdate]),+ ProductStackWorker, ProductStackInstaller,+ )+ default:+ return []ProductStack{stack}+ }+}++// updateStacks does the work and recurses without announcing itself or asking the cloud+// anything: both belong to the operation as a whole, which is update() above.+func (p *processor) updateStacks(ctx context.Context, stack ProductStack, state *operationState) (err error) {allStacks := append(composeOperationAllStacksOrder[ProcessorOperationUpdate],ProductStackWorker,ProductStackInstaller,@@ -532,6 +587,23 @@ func (p *processor) update(ctx context.Context, stack ProductStack, state *operareturn nil}+ // The image references the server chose are written by downloadStack and+ // updateStack themselves — every path that fetches goes through one of them,+ // and a call site that forgot would fetch the compose default in silence.++ // The Jaeger storage plugin is a FILE mounted into the jaeger container, so no+ // amount of pulling images brings it up to date. Fetched before the images+ // because a plugin that cannot be downloaded should abort the update with+ // nothing else disturbed.+ pluginReplaced := false+ if stack == ProductStackObservability {+ replaced, err := p.updateOps.updateJaegerPlugin(ctx, state)+ if err != nil {+ return err+ }+ pluginReplaced = replaced+ }+if err := p.composeOps.downloadStack(ctx, stack, state); err != nil {return fmt.Errorf("failed to download stack: %w", err)}@@ -541,6 +613,21 @@ func (p *processor) update(ctx context.Context, stack ProductStack, state *operareturn fmt.Errorf("failed to update stack: %w", err)}+ // `up -d` above recreates a container only when its SPEC changed, and a different+ // file inside a bind mount is not a spec change — so when the plugin was replaced+ // but the jaeger image was not, the container above was left running, still+ // holding the plugin it exec'd when it started. Without this the update reports+ // success and Jaeger goes on writing traces through the old plugin until+ // something else happens to restart it.+ if pluginReplaced {+ p.appendLog(MsgJaegerPluginRestarting, stack, state)+ if err := p.composeOps.performStackCommand(+ ctx, stack, state, "restart", jaegerServiceName,+ ); err != nil {+ return fmt.Errorf("failed to restart jaeger after replacing its storage plugin: %w", err)+ }+ }+if err := composeStacksGatherInfo[stack](ctx); err != nil {return fmt.Errorf("failed to gather info after update: %w", err)}@@ -568,7 +655,7 @@ func (p *processor) update(ctx context.Context, stack ProductStack, state *operacase ProductStackCompose:for _, s := range composeOperationAllStacksOrder[ProcessorOperationUpdate] {- if err := p.update(ctx, s, state); err != nil {+ if err := p.updateStacks(ctx, s, state); err != nil {return err}}@@ -576,7 +663,7 @@ func (p *processor) update(ctx context.Context, stack ProductStack, state *operacase ProductStackAll:// update all applicable stacksfor _, s := range allStacks {- if err := p.update(ctx, s, state); err != nil {+ if err := p.updateStacks(ctx, s, state); err != nil {return err}}@@ -711,12 +798,18 @@ func (p *processor) remove(ctx context.Context, stack ProductStack, state *opera}case ProductStackAll:- // remove all stacks+ // Every stack is attempted even when one fails: the user asked for all of them+ // gone, and stopping halfway leaves an installation in a state nobody chose.+ // errors.Join reports every failure rather than only the first.+ var failures []errorfor _, s := range allStacks {if err := p.remove(ctx, s, state); err != nil {- return err+ failures = append(failures, err)}}+ if len(failures) > 0 {+ return errors.Join(failures...)+ }default:return fmt.Errorf("operation remove not applicable for stack %s", stack)@@ -791,10 +884,17 @@ func (p *processor) purge(ctx context.Context, stack ProductStack, state *operat}case ProductStackAll:- // purge all stacks+ // Every stack is attempted even when one fails, and the networks go regardless.+ //+ // Both used to depend on nothing going wrong. The loop returned on the first+ // error, and the network removal below it was simply skipped — so one stack that+ // could not be purged left three custom docker networks behind, on an operation+ // whose whole purpose is to leave nothing. Which stacks exist varies per+ // installation, so "the first failure" is not a rare path.+ var failures []errorfor _, s := range allStacks {if err := p.purge(ctx, s, state); err != nil {- return err+ failures = append(failures, err)}}@@ -803,6 +903,10 @@ func (p *processor) purge(ctx context.Context, stack ProductStack, state *operat_ = p.dockerOps.removeMainDockerNetwork(ctx, state, string(ProductDockerNetworkObservability))_ = p.dockerOps.removeMainDockerNetwork(ctx, state, string(ProductDockerNetworkLangfuse))+ if len(failures) > 0 {+ return errors.Join(failures...)+ }+default:return fmt.Errorf("operation purge not applicable for stack %s", stack)}diff --git a/backend/cmd/installer/processor/mock_test.go b/backend/cmd/installer/processor/mock_test.goindex 4dea109b..49b6522d 100644--- a/backend/cmd/installer/processor/mock_test.go+++ b/backend/cmd/installer/processor/mock_test.go@@ -23,6 +23,9 @@ type mockState struct {envPath stringstack []stringdirty bool+ // written stands in for the environment file on disk — the one `docker compose+ // --env-file` reads. Only WriteVars puts anything here; SetVars stages.+ written map[string]string}func newMockState() *mockState {@@ -70,6 +73,22 @@ func (m *mockState) SetVars(vars map[string]string) error {m.dirty = truereturn nil}++// WriteVars is deliberately NOT the same as SetVars here: it records into `written`, the+// mock's stand-in for the environment file on disk, and it does not make the state dirty.+// A test that cannot tell staging from writing cannot catch the defect this method exists+// to fix — a staged image reference is one `docker compose --env-file` never sees.+func (m *mockState) WriteVars(vars map[string]string) error {+ if m.written == nil {+ m.written = make(map[string]string, len(vars))+ }+ for name, value := range vars {+ m.written[name] = value+ m.vars[name] = loader.EnvVar{Name: name, Value: value}+ }+ return nil+}+func (m *mockState) ResetVars(names []string) error {for _, name := range names {delete(m.vars, name)@@ -204,7 +223,13 @@ func (m *baseMockFileSystemOperations) record(method string, stack ProductStack,return err}+// checkError reads errOn, which setError writes from another goroutine in the+// concurrency tests — so it takes the same lock. Without it `go test -race` fails inside+// the mock, and a race reported in test scaffolding is a race nobody looks at again.func (m *baseMockFileSystemOperations) checkError(method string, stack ProductStack) error {+ m.mu.Lock()+ defer m.mu.Unlock()+if m.errOn != nil {// check for stack-specific error firstmethodKey := fmt.Sprintf("%s_%s", method, stack)@@ -292,6 +317,9 @@ func (m *baseMockDockerOperations) record(method string, name string, err error)}func (m *baseMockDockerOperations) checkError(method string) error {+ m.mu.Lock()+ defer m.mu.Unlock()+if m.errOn != nil {if configuredErr, ok := m.errOn[method]; ok {return configuredErr@@ -411,6 +439,9 @@ func (m *baseMockComposeOperations) record(method string, stack ProductStack, er}func (m *baseMockComposeOperations) checkError(method string) error {+ m.mu.Lock()+ defer m.mu.Unlock()+if m.errOn != nil {if configuredErr, ok := m.errOn[method]; ok {// one-shot error to avoid leaking into subsequent subtests@@ -516,11 +547,15 @@ func (m *baseMockComposeOperations) determineComposeFile(stack ProductStack) (st}func (m *baseMockComposeOperations) performStackCommand(ctx context.Context, stack ProductStack, state *operationState, args ...string) error {- if err := m.checkError("performStackCommand"); err != nil {- m.record("performStackCommand", stack, err)- return err- }- return m.record("performStackCommand", stack, nil)+ // The args are recorded, because this is the only compose call whose MEANING is in+ // them — "restart jaeger" and "up -d" are otherwise the same method, and a test that+ // cannot tell them apart cannot check that a replaced plugin got its restart.+ err := m.checkError("performStackCommand")+ m.mu.Lock()+ m.calls = append(m.calls, call{Method: "performStackCommand", Stack: stack, Args: args, Error: err})+ m.mu.Unlock()++ return err}// baseMockUpdateOperations provides base implementation with call logging@@ -528,6 +563,9 @@ type baseMockUpdateOperations struct {mu sync.Mutexcalls []callerrOn map[string]error+ // jaegerPluginReplaced is what updateJaegerPlugin reports back, which decides whether+ // the caller restarts the Jaeger service afterwards.+ jaegerPluginReplaced bool}func newBaseMockUpdateOperations() *baseMockUpdateOperations {@@ -545,6 +583,9 @@ func (m *baseMockUpdateOperations) record(method string, err error) error {}func (m *baseMockUpdateOperations) checkError(method string) error {+ m.mu.Lock()+ defer m.mu.Unlock()+if m.errOn != nil {if configuredErr, ok := m.errOn[method]; ok {return configuredErr@@ -570,13 +611,13 @@ func (m *baseMockUpdateOperations) setError(method string, err error) {m.errOn[method] = err}-func (m *baseMockUpdateOperations) checkUpdates(ctx context.Context, state *operationState) (*checker.CheckUpdatesResponse, error) {+func (m *baseMockUpdateOperations) checkUpdates(ctx context.Context, state *operationState) error {if err := m.checkError("checkUpdates"); err != nil {m.record("checkUpdates", err)- return nil, err+ return err}m.record("checkUpdates", nil)- return &checker.CheckUpdatesResponse{}, nil+ return nil}func (m *baseMockUpdateOperations) downloadInstaller(ctx context.Context, state *operationState) error {@@ -603,6 +644,25 @@ func (m *baseMockUpdateOperations) removeInstaller(ctx context.Context, state *oreturn m.record("removeInstaller", nil)}+func (m *baseMockUpdateOperations) updateJaegerPlugin(ctx context.Context, state *operationState) (bool, error) {+ if err := m.checkError("updateJaegerPlugin"); err != nil {+ m.record("updateJaegerPlugin", err)+ return false, err+ }+ return m.jaegerPluginReplaced, m.record("updateJaegerPlugin", nil)+}++func (m *baseMockUpdateOperations) installerPackage(ctx context.Context) (*InstallerPackage, error) {+ if err := m.checkError("installerPackage"); err != nil {+ m.record("installerPackage", err)+ return nil, err+ }+ if err := m.record("installerPackage", nil); err != nil {+ return nil, err+ }+ return &InstallerPackage{Version: "1.2.3", OS: "linux", Arch: "amd64", Size: 1024, Path: "installer_1.2.3"}, nil+}+// testState creates a test state with initialized environmentfunc testState(t *testing.T) *mockState {t.Helper()@@ -2148,13 +2208,11 @@ func TestBaseMockUpdateOperations(t *testing.T) {})}- // test checkUpdates separately as it returns a response+ // checkUpdates leaves its answer on the shared check result rather than returning it,+ // so there is one place holding what the server said instead of two that can disagree.t.Run("checkUpdates", func(t *testing.T) {- resp, err := mock.checkUpdates(t.Context(), state)+ err := mock.checkUpdates(t.Context(), state)assertNoError(t, err)- if resp == nil {- t.Error("expected response, got nil")- }calls := mock.getCalls()// offset by 6 due to previous tests (3 tests * 2 calls each)@@ -2165,12 +2223,8 @@ func TestBaseMockUpdateOperations(t *testing.T) {// test error injectiontestErr := fmt.Errorf("check updates error")mock.setError("checkUpdates", testErr)- resp, err = mock.checkUpdates(t.Context(), state)- if err != testErr {+ if err := mock.checkUpdates(t.Context(), state); err != testErr {t.Errorf("expected error %v, got %v", testErr, err)}- if resp != nil {- t.Error("expected nil response on error")- }})}diff --git a/backend/cmd/installer/processor/model.go b/backend/cmd/installer/processor/model.goindex b51d09ac..7f9962ef 100644--- a/backend/cmd/installer/processor/model.go+++ b/backend/cmd/installer/processor/model.go@@ -30,6 +30,13 @@ type ProcessorModel interface {Restart(ctx context.Context, stack ProductStack, opts ...OperationOption) tea.CmdResetPassword(ctx context.Context, stack ProductStack, opts ...OperationOption) tea.CmdHandleMsg(msg tea.Msg) tea.Cmd++ // InstallerPackage describes the installer build on offer without downloading it.+ //+ // Blocking rather than a tea.Cmd, unlike everything above it: this is one short+ // request with one answer, not an operation that streams output into a terminal+ // panel, and the screen that needs it already wraps it in a command of its own.+ InstallerPackage(ctx context.Context) (*InstallerPackage, error)}func NewProcessorModel(state state.State, checker *checker.CheckResult, files files.Files) ProcessorModel {@@ -211,10 +218,19 @@ func (pm *processorModel) HandleMsg(msg tea.Msg) tea.Cmd {switch msg := msg.(type) {case ProcessorWaitMsg:- if msg.Error != nil {- // stop polling after error- return nil- }+ // Polling continues even when this message carries an error, and that is the+ // whole point of it.+ //+ // An error here means the operation ENDED — wrapCommand's select received it from+ // the operation's channel, or the context was cancelled. Either way the operation+ // has already appended its completion message to the queue, and the completion is+ // what tells the screen to stop the spinner and show what happened. Returning nil+ // here stopped the polling that would have delivered it, so an operation that+ // failed faster than wrapCommand's 500 ms window left the screen saying "in+ // progress" forever, with the error displayed nowhere.+ //+ // The chain still terminates: ProcessorCompletionMsg below returns nil, and every+ // operation sends one from a deferred call.return pollMsg(msg.num, msg.Stack, msg.state)case ProcessorOutputMsg:return pollMsg(msg.num, msg.Stack, msg.state)diff --git a/backend/cmd/installer/processor/processor.go b/backend/cmd/installer/processor/processor.goindex 630bff3c..115eb484 100644--- a/backend/cmd/installer/processor/processor.go+++ b/backend/cmd/installer/processor/processor.go@@ -63,6 +63,9 @@ type Processor interface {Stop(ctx context.Context, stack ProductStack, opts ...OperationOption) errorRestart(ctx context.Context, stack ProductStack, opts ...OperationOption) errorResetPassword(ctx context.Context, stack ProductStack, opts ...OperationOption) error++ // InstallerPackage describes the installer build on offer without downloading it.+ InstallerPackage(ctx context.Context) (*InstallerPackage, error)}// WithForce skips validation checks and attempts maximum operations@@ -120,10 +123,12 @@ type composeOperations interface {}type updateOperations interface {- checkUpdates(ctx context.Context, state *operationState) (*checker.CheckUpdatesResponse, error)+ checkUpdates(ctx context.Context, state *operationState) errordownloadInstaller(ctx context.Context, state *operationState) errorupdateInstaller(ctx context.Context, state *operationState) errorremoveInstaller(ctx context.Context, state *operationState) error+ installerPackage(ctx context.Context) (*InstallerPackage, error)+ updateJaegerPlugin(ctx context.Context, state *operationState) (bool, error)}type processor struct {@@ -251,3 +256,14 @@ func (p *processor) ResetPassword(ctx context.Context, stack ProductStack, optsopts = append(opts, withContext(ctx), withOperation(ProcessorOperationResetPassword))return p.resetPassword(ctx, stack, newOperationState(opts))}++// InstallerPackage asks the update server what it offers for this host.+//+// It takes the same lock as the operations even though it changes nothing: the answer is+// remembered for the download that follows, and the download runs under this lock.+func (p *processor) InstallerPackage(ctx context.Context) (*InstallerPackage, error) {+ p.mu.Lock()+ defer p.mu.Unlock()++ return p.updateOps.installerPackage(ctx)+}diff --git a/backend/cmd/installer/processor/pull_reference.go b/backend/cmd/installer/processor/pull_reference.gonew file mode 100644index 00000000..0a70f100--- /dev/null+++ b/backend/cmd/installer/processor/pull_reference.go@@ -0,0 +1,89 @@+package processor++import (+ "fmt"+ "strings"++ "pentagi/cmd/installer/checker"+ "pentagi/cmd/installer/wizard/logger"+)++// Every `image:` in the compose files is `${<COMPONENT>_IMAGE:-<default>}`, and+// this is the other half of that: before a pull, the reference the server chose+// is written into `.env` so compose resolves it.+//+// The installer does NOT pick a tag. Which tag to pull is the cloud's decision+// and varies with the update strategy, so a client that rebuilt `repository:tag`+// out of the repository and tag fields would silently override the answer with a+// guess — and land on an artefact it was never offered.+//+// The `:-` default in compose is what makes this safe to skip: an absent or+// EMPTY variable falls back to the reference the file shipped with, so an+// installation that never got an answer behaves exactly as before.++// composeImageVar is the environment variable a component's image reference goes+// into.+//+// Derived from the component name rather than kept in a table: the compose files+// use exactly this spelling, so a new component needs no second registration+// that could be forgotten — and a mismatch shows up as "the default was used",+// which is visible in `docker compose config`.+func composeImageVar(component string) string {+ return strings.ToUpper(strings.ReplaceAll(strings.TrimSpace(component), "-", "_")) + "_IMAGE"+}++// pullReferencesForStack collects the variables to write for one stack.+//+// Only components the server actually resolved: an `unknown` one carries no+// reference, and writing an empty value would be worse than writing nothing —+// `${VAR:-default}` treats empty as absent, so it would work, but it would also+// erase a reference a previous answer had pinned.+func pullReferencesForStack(updates []checker.StackUpdate, stack string) map[string]string {+ vars := map[string]string{}+ for _, update := range updates {+ if update.Stack != stack {+ continue+ }+ for _, component := range update.Components {+ if component.PullReference == "" || component.Component == "" {+ continue+ }+ vars[composeImageVar(component.Component)] = component.PullReference+ }+ }+ return vars+}++// applyPullReferences writes them, and reports what it wrote so an operator can+// see in the log which reference a pull was about to use.+//+// A failure here is returned rather than logged and swallowed: the whole point+// is that the pull uses the reference the server named, and a pull that quietly+// used the compose default instead is an update that reports success while+// changing nothing.+func (p *processor) applyPullReferences(stack ProductStack) error {+ if p.checker == nil || p.state == nil {+ return nil+ }+ vars := pullReferencesForStack(p.checker.StackUpdates, string(stack))+ if len(vars) == 0 {+ return nil+ }++ names := make([]string, 0, len(vars))+ for name, reference := range vars {+ names = append(names, fmt.Sprintf("%s=%s", name, reference))+ }+ logger.Log("[processor] PULL REFERENCES for %s: %s", stack, strings.Join(names, " "))++ // WriteVars and not SetVars. SetVars STAGES a change: it lands in the installer's own+ // state file and only reaches `.env` when the user applies their changes. But the pull+ // that follows is `docker compose --env-file <.env>`, so a staged variable is one+ // compose has never heard of — it falls back to the default the compose file ships+ // with, pulls that, and the update reports success having fetched something the server+ // never offered. The whole mechanism was inert until this line said WriteVars.+ if err := p.state.WriteVars(vars); err != nil {+ return fmt.Errorf("failed to write image references for %s: %w", stack, err)+ }+ return nil+}diff --git a/backend/cmd/installer/processor/pull_reference_test.go b/backend/cmd/installer/processor/pull_reference_test.gonew file mode 100644index 00000000..85e90349--- /dev/null+++ b/backend/cmd/installer/processor/pull_reference_test.go@@ -0,0 +1,288 @@+package processor++import (+ "context"+ "os"+ "path/filepath"+ "regexp"+ "testing"++ "pentagi/cmd/installer/checker"++ "github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"+)++// TestTheComposeVariableIsDerivedFromTheComponentName.+//+// The compose files spell it `${<COMPONENT>_IMAGE:-<default>}` with the+// component name uppercased and dashes turned into underscores. Deriving it here+// rather than keeping a table means a new component needs no second+// registration — and the pairing is checked against the real files below, so a+// drift is a failing test rather than a pull that silently used the default.+func TestTheComposeVariableIsDerivedFromTheComponentName(t *testing.T) {+ for component, want := range map[string]string{+ "pentagi": "PENTAGI_IMAGE",+ "langfuse-worker": "LANGFUSE_WORKER_IMAGE",+ "node-exporter": "NODE_EXPORTER_IMAGE",+ "clickstore": "CLICKSTORE_IMAGE",+ "otel": "OTEL_IMAGE",+ } {+ assert.Equal(t, want, composeImageVar(component))+ }+}++// TestOnlyResolvedComponentsGetAReference.+//+// A component the server could not resolve carries no reference. Writing an+// empty value for it would erase whatever a previous answer had pinned —+// `${VAR:-default}` treats empty as absent, so the pull would quietly fall back+// to the shipped default and the installation would drift off the curated set+// without anything saying so.+func TestOnlyResolvedComponentsGetAReference(t *testing.T) {+ updates := []checker.StackUpdate{+ {+ Stack: "pentagi",+ Components: []checker.ComponentUpdate{+ {Component: "pentagi", Action: "upgrade", PullReference: "vxcontrol/pentagi:2.3.4"},+ {Component: "pgvector", Action: "current", PullReference: "vxcontrol/pgvector:latest"},+ // Nothing published for it: no reference, and none must be written.+ {Component: "scraper", Action: "unknown", Reason: "tag_not_published"},+ },+ },+ {+ Stack: "langfuse",+ Components: []checker.ComponentUpdate{+ {Component: "langfuse-web", Action: "current", PullReference: "langfuse/langfuse:3"},+ },+ },+ }++ vars := pullReferencesForStack(updates, "pentagi")+ assert.Equal(t, map[string]string{+ "PENTAGI_IMAGE": "vxcontrol/pentagi:2.3.4",+ "PGVECTOR_IMAGE": "vxcontrol/pgvector:latest",+ }, vars, "an unresolved component must not be written, and another stack must not leak in")++ // `current` components are written too, and deliberately: the variable has to+ // keep naming the artefact the installation is on, or the next `compose up`+ // resolves it back to the file's default.+ assert.Contains(t, vars, "PGVECTOR_IMAGE")++ assert.Equal(t, map[string]string{"LANGFUSE_WEB_IMAGE": "langfuse/langfuse:3"},+ pullReferencesForStack(updates, "langfuse"))+ assert.Empty(t, pullReferencesForStack(updates, "observability"))+}++// TestTheReferencesReachTheFileComposeActuallyReads.+//+// This is the defect that made the whole mechanism inert, and it was invisible: the code+// called SetVars, which STAGES a change into the installer's own state file, to be written+// into `.env` only when the user applies their changes. But the pull that follows is+// `docker compose --env-file <.env>`, so a staged variable is one compose has never heard+// of — `${PENTAGI_IMAGE:-vxcontrol/pentagi:latest}` fell back to the default, the pull+// fetched something the update server never offered, and the update reported success.+//+// Two things are asserted and both matter. The references have to reach the file, and they+// must NOT make the installation dirty: a value the installer wrote for itself would+// otherwise prompt the user to apply changes they never made.+func TestTheReferencesReachTheFileComposeActuallyReads(t *testing.T) {+ state := newMockState()+ p := &processor{+ state: state,+ checker: &checker.CheckResult{+ StackUpdates: []checker.StackUpdate{{+ Stack: "pentagi",+ Components: []checker.ComponentUpdate{+ {Component: "pentagi", Action: "upgrade", PullReference: "vxcontrol/pentagi:2.3.4"},+ },+ }},+ },+ }++ require.NoError(t, p.applyPullReferences(ProductStackPentagi))++ assert.Equal(t, map[string]string{"PENTAGI_IMAGE": "vxcontrol/pentagi:2.3.4"}, state.written,+ "the reference was staged instead of written; docker compose reads the file, not the staging area")+ assert.False(t, state.IsDirty(),+ "a reference the installer wrote for itself must not ask the user to apply changes they never made")+}++// TestEveryFetchingOperationWritesTheReferencesFirst.+//+// The references used to be written by ONE call site — the update path — while four others+// reached a pull without them: download, install, and the three apply-changes phases. All+// five end in updateStack or downloadStack, so that is where the write belongs; a call site+// that forgets is not a compile error, it is a pull of the compose default in silence.+//+// The tear-down operations are the other half of the claim: `down` needs no reference, and+// writing one while removing a stack would be a side effect nobody asked for.+func TestEveryFetchingOperationWritesTheReferencesFirst(t *testing.T) {+ newProcessor := func() (*processor, *mockState) {+ state := newMockState()+ p := &processor{+ state: state,+ checker: &checker.CheckResult{+ StackUpdates: []checker.StackUpdate{{+ Stack: "pentagi",+ Components: []checker.ComponentUpdate{+ {Component: "pentagi", Action: "upgrade", PullReference: "vxcontrol/pentagi:2.3.4"},+ },+ }},+ },+ }+ p.composeOps = newComposeOperations(p)+ return p, state+ }++ // The compose command itself cannot run here — there is no docker and no compose file —+ // so what is asserted is the state of `.env` by the time the operation gets that far.+ for name, run := range map[string]func(*processor) error{+ "update": func(p *processor) error {+ return p.composeOps.updateStack(context.Background(), ProductStackPentagi, newOperationState(nil))+ },+ "download": func(p *processor) error {+ return p.composeOps.downloadStack(context.Background(), ProductStackPentagi, newOperationState(nil))+ },+ } {+ p, state := newProcessor()+ _ = run(p)+ assert.Equal(t, "vxcontrol/pentagi:2.3.4", state.written["PENTAGI_IMAGE"],+ "%s reached the compose command without writing the reference the server chose", name)+ }++ for name, run := range map[string]func(*processor) error{+ "remove": func(p *processor) error {+ return p.composeOps.removeStack(context.Background(), ProductStackPentagi, newOperationState(nil))+ },+ "purge": func(p *processor) error {+ return p.composeOps.purgeStack(context.Background(), ProductStackPentagi, newOperationState(nil))+ },+ } {+ p, state := newProcessor()+ _ = run(p)+ assert.Empty(t, state.written, "%s must not write image references while tearing a stack down", name)+ }+}++// TestEveryComposeImageIsParameterised is the other end of the mechanism.+//+// A hardcoded `image:` cannot be steered at all: the server's answer is written+// into a variable nothing reads, the pull takes whatever the file says, and the+// update reports success having changed nothing. This walks the real files.+func TestEveryComposeImageIsParameterised(t *testing.T) {+ root := repositoryRoot(t)+ files := []string{+ "docker-compose.yml",+ "docker-compose-langfuse.yml",+ "docker-compose-observability.yml",+ "docker-compose-graphiti.yml",+ }++ imageLine := regexp.MustCompile(`(?m)^\s*image:\s*(\S+)\s*$`)+ parameterised := regexp.MustCompile(`^\$\{([A-Z0-9_]+):-(\S+)\}$`)++ seen := 0+ for _, name := range files {+ body, err := os.ReadFile(filepath.Join(root, name))+ require.NoError(t, err, "the compose files are part of the contract with the server")++ for _, match := range imageLine.FindAllStringSubmatch(string(body), -1) {+ seen+++ reference := match[1]+ parts := parameterised.FindStringSubmatch(reference)+ require.NotNil(t, parts,+ "%s: %q is hardcoded, so the server cannot steer what this service pulls", name, reference)++ // `:-` and not `-`: the first substitutes the default for an absent+ // OR empty variable, the second only for an absent one. An+ // installation that wrote an empty value would otherwise pull "".+ assert.True(t, len(parts) == 3 && parts[2] != "",+ "%s: %s must keep a default so an installation with no answer behaves as before",+ name, parts[1])+ assert.True(t, len(parts[1]) > len("_IMAGE") && parts[1][len(parts[1])-len("_IMAGE"):] == "_IMAGE",+ "%s: %q does not follow <COMPONENT>_IMAGE, so composeImageVar will never write it",+ name, parts[1])+ }+ }+ assert.GreaterOrEqual(t, seen, 20, "the walk found only %d images; it is not reading the files", seen)+}++// repositoryRoot walks up to the directory holding the compose files.+func repositoryRoot(t *testing.T) string {+ t.Helper()+ dir, err := os.Getwd()+ require.NoError(t, err)+ for range 8 {+ if _, err := os.Stat(filepath.Join(dir, "docker-compose.yml")); err == nil {+ return dir+ }+ parent := filepath.Dir(dir)+ require.NotEqual(t, parent, dir, "no docker-compose.yml above the test directory")+ dir = parent+ }+ t.Fatal("could not find the repository root")+ return ""+}++// TestASharperTagIsNotDrift is the distinction the whole check turns on.+//+// The update path writes the server's chosen reference into these variables, and+// under `stable` that is deliberately a more specific tag than the file ships:+// `vxcontrol/pentagi:2.3.4` against `vxcontrol/pentagi:latest`. Reporting that as+// drift would tell the user their installation is misconfigured every time an+// update worked.+//+// A different REPOSITORY is the real case — an installation pinned to+// `postgres:15` while this build ships `postgres:16`, or an image rebuilt under+// somebody's own name — and it is the one the server answers `unknown` for.+func TestASharperTagIsNotDrift(t *testing.T) {+ defaults := map[string]string{+ "PENTAGI_IMAGE": "vxcontrol/pentagi:latest",+ "POSTGRES_IMAGE": "postgres:16",+ "NEO4J_IMAGE": "neo4j:5.26.2",+ "REDIS_IMAGE": "redis:7",+ }++ drift := referenceDrift(defaults, map[string]string{+ // The update path pinned it. Same repository, sharper tag: working.+ "PENTAGI_IMAGE": "vxcontrol/pentagi:2.3.4",+ // A different major of the same image is still the same repository, so+ // this is NOT reported: moving between them is a data migration and not+ // something to nag about on an update screen.+ "POSTGRES_IMAGE": "postgres:15",+ // A rebuild under another name is a configuration this build cannot+ // support, and the server answers `repository_not_tracked` for it.+ "NEO4J_IMAGE": "acme/neo4j-fork:5.26.2",+ // Unset: the compose default applies, which is exactly right.+ "REDIS_IMAGE": "",+ })++ require.Len(t, drift, 1, "only a different repository is drift: %+v", drift)+ assert.Equal(t, "NEO4J_IMAGE", drift[0].Variable)+ assert.Equal(t, "acme/neo4j-fork:5.26.2", drift[0].Configured)+ assert.Equal(t, "neo4j:5.26.2", drift[0].Default)+}++// TestTheDefaultsAreReadFromTheFilesThemselves. A table in Go would be a second+// source of truth able to disagree with the files docker compose actually reads.+func TestTheDefaultsAreReadFromTheFilesThemselves(t *testing.T) {+ root := repositoryRoot(t)+ defaults, err := composeReferenceDefaults(func(name string) ([]byte, error) {+ return os.ReadFile(filepath.Join(root, name))+ })+ require.NoError(t, err)++ assert.GreaterOrEqual(t, len(defaults), 20, "every image is parameterised, so every one has a default")+ assert.Equal(t, "vxcontrol/pentagi:latest", defaults["PENTAGI_IMAGE"])+ assert.Equal(t, "postgres:16", defaults["POSTGRES_IMAGE"])+ // The two ClickHouse services ship the same image under different variables,+ // which is why the variable and not the image is the key.+ assert.Equal(t, "clickhouse/clickhouse-server:24", defaults["CLICKHOUSE_IMAGE"])+ assert.Equal(t, "clickhouse/clickhouse-server:24", defaults["CLICKSTORE_IMAGE"])++ // A build that shipped no parameterised image at all would make every+ // installation look perfectly aligned.+ _, err = composeReferenceDefaults(func(string) ([]byte, error) { return []byte("services:\n"), nil })+ assert.Error(t, err, "finding nothing must be an error, not a clean bill of health")+}diff --git a/backend/cmd/installer/processor/reference_drift.go b/backend/cmd/installer/processor/reference_drift.gonew file mode 100644index 00000000..f922ce10--- /dev/null+++ b/backend/cmd/installer/processor/reference_drift.go@@ -0,0 +1,138 @@+package processor++import (+ "fmt"+ "regexp"+ "sort"+ "strings"+)++// The cloud does not steer an installation back to a supported reference, and+// deliberately: it would need a copy of these compose defaults kept in step with+// them, and `postgres:15 → 16` is a major upgrade with a data migration behind+// it — not something a routine update check may cause as a side effect. It+// answers `unknown` with a reason instead.+//+// The installer is the side that CAN say it, because it ships the reference+// defaults itself. What it does with that is report the difference and let the+// user decide, separately from updating.++// composeImagePattern matches `image: ${VAR:-default}` in a compose file, which+// is the only form these files use — TestEveryComposeImageIsParameterised is+// what keeps that true.+var composeImagePattern = regexp.MustCompile(`(?m)^\s*image:\s*\$\{([A-Z0-9_]+):-(\S+)\}\s*$`)++// composeFiles are the four the installer ships and the variables in them are+// the reference defaults of this build.+var composeFiles = []string{+ "docker-compose.yml",+ "docker-compose-langfuse.yml",+ "docker-compose-observability.yml",+ "docker-compose-graphiti.yml",+}++// ReferenceDrift is one variable whose value is not what this build of the+// installer would have used.+type ReferenceDrift struct {+ // Variable is the compose variable, Configured what `.env` says, Default what+ // this installer ships.+ Variable string+ Configured string+ Default string+}++func (d ReferenceDrift) String() string {+ return fmt.Sprintf("%s=%s (this build ships %s)", d.Variable, d.Configured, d.Default)+}++// composeReferenceDefaults reads the shipped defaults out of the compose files.+//+// From the files rather than from a table in Go: the files are what `docker+// compose` actually reads, so a table would be a second source of truth able to+// disagree with the thing it describes.+func composeReferenceDefaults(read func(string) ([]byte, error)) (map[string]string, error) {+ defaults := map[string]string{}+ for _, name := range composeFiles {+ body, err := read(name)+ if err != nil {+ return nil, fmt.Errorf("reading %s: %w", name, err)+ }+ for _, match := range composeImagePattern.FindAllStringSubmatch(string(body), -1) {+ defaults[match[1]] = match[2]+ }+ }+ if len(defaults) == 0 {+ return nil, fmt.Errorf("no parameterised images found in the compose files")+ }+ return defaults, nil+}++// referenceDrift lists the variables `.env` sets to something other than the+// shipped default.+//+// A variable the update path wrote is NOT drift: it is the server's answer, and+// under `stable` it is deliberately a more specific tag than the file's default+// (`vxcontrol/pentagi:2.3.4` against `vxcontrol/pentagi:latest`). Only a+// different REPOSITORY is a configuration this installer cannot support —+// a different tag of the same repository is the mechanism working.+func referenceDrift(defaults, configured map[string]string) []ReferenceDrift {+ var drift []ReferenceDrift+ for variable, shipped := range defaults {+ value := strings.TrimSpace(configured[variable])+ if value == "" || value == shipped {+ continue+ }+ if repositoryOf(value) == repositoryOf(shipped) {+ continue+ }+ drift = append(drift, ReferenceDrift{+ Variable: variable, Configured: value, Default: shipped,+ })+ }+ sort.Slice(drift, func(i, j int) bool { return drift[i].Variable < drift[j].Variable })+ return drift+}++// repositoryOf drops the tag. A colon separates a tag only after the last slash,+// so a registry port survives; a digest reference contributes no tag.+func repositoryOf(reference string) string {+ if at := strings.LastIndex(reference, "@"); at >= 0 {+ reference = reference[:at]+ }+ colon := strings.LastIndex(reference, ":")+ if colon < 0 || colon < strings.LastIndex(reference, "/") {+ return reference+ }+ return reference[:colon]+}++// ReferenceDrift reports the compose variables this installation sets to a+// repository this build of the installer does not ship.+//+// Exposed as a query rather than folded into the update: realignment is a+// deliberate act, not a side effect of a routine update check. What+// the interface does with the answer — show it, offer to reset those variables —+// is its decision; what this guarantees is that the difference is visible at all,+// which it was not before: the server answers `unknown` with a reason and the+// installation simply never updates that component.+func (p *processor) ReferenceDrift() ([]ReferenceDrift, error) {+ if p.files == nil || p.state == nil {+ return nil, nil+ }+ defaults, err := composeReferenceDefaults(p.files.GetContent)+ if err != nil {+ return nil, err+ }++ names := make([]string, 0, len(defaults))+ for variable := range defaults {+ names = append(names, variable)+ }+ vars, _ := p.state.GetVars(names)++ configured := make(map[string]string, len(vars))+ for variable, envVar := range vars {+ configured[variable] = envVar.Value+ }+ return referenceDrift(defaults, configured), nil+}diff --git a/backend/cmd/installer/processor/state.go b/backend/cmd/installer/processor/state.goindex f19e8f37..5693d343 100644--- a/backend/cmd/installer/processor/state.go+++ b/backend/cmd/installer/processor/state.go@@ -19,6 +19,19 @@ type operationState struct {operation ProcessorOperationpasswordValue string // password value for reset password operation+ // announced is the stack the CALLER asked about — the first one to announce itself.+ // Started and completion messages are sent for it and for nothing else.+ //+ // The operations fan out: an update of `compose` runs four stacks, an update of `all`+ // runs six, and each nested call used to announce its own completion. The screen takes+ // the first completion as the end of the whole operation — it stops the terminal, prints+ // the result and, because HandleMsg returns nil for a completion, stops polling. So the+ // first stack to finish reported success on behalf of all of them, and every error+ // after it was never drained. Which stacks are installed does not change this: the+ // fan-out lists are fixed, and a stack with nothing to do finishes fastest of all.+ announced ProductStack+ announcedSet bool+// message chain for replymx *sync.Mutexctx context.Context@@ -153,6 +166,12 @@ func (state *operationState) sendCompletion(stack ProductStack, err error) {state.mx.Lock()defer state.mx.Unlock()+ // A nested stack finishing is not the operation finishing. Its error is not lost by+ // staying silent here — the caller returns it, and the announced stack carries it.+ if state.announcedSet && state.announced != stack {+ return+ }+state.msgs = append(state.msgs, ProcessorCompletionMsg{ID: state.id,Error: err,@@ -168,6 +187,15 @@ func (state *operationState) sendStarted(stack ProductStack) {state.mx.Lock()defer state.mx.Unlock()+ // The first stack to announce itself is the one the caller asked about; everything+ // after it is the fan-out this operation performs internally.+ if !state.announcedSet {+ state.announced, state.announcedSet = stack, true+ }+ if state.announced != stack {+ return+ }+state.msgs = append(state.msgs, ProcessorStartedMsg{ID: state.id,Operation: state.operation,diff --git a/backend/cmd/installer/processor/update.go b/backend/cmd/installer/processor/update.goindex 5964ac74..54a09e86 100644--- a/backend/cmd/installer/processor/update.go+++ b/backend/cmd/installer/processor/update.go@@ -2,277 +2,462 @@ package processorimport ("context"- "crypto/sha256""fmt""io"- "net/http"- "net/url""os"+ "path/filepath""runtime"- "time"+ "strings""pentagi/cmd/installer/checker"- "pentagi/pkg/version"+ "pentagi/cmd/installer/cloud"++ "github.com/vxcontrol/cloud/models")-const updateServerURL = "https://update.pentagi.com"+// progressStepPercent is how much of the download has to arrive before another line is+// printed. One line per read would be a line per few kilobytes, which buries everything+// around it in a panel that scrolls.+const progressStepPercent = 10++// InstallerPackage is what the update server offers for this host, in the plain form the+// screen showing it can use: the wizard has no business knowing the SDK's types.+//+// Path is where the file will be written, computed here rather than in the interface so+// the name the screen promises and the name the download produces cannot drift apart.+type InstallerPackage struct {+ Version string+ OS string+ Arch string+ Size int64+ Path string++ // Downloaded says a file already sits at Path. It is the ONLY thing the screen asks+ // the user to confirm: downloading over nothing needs no permission, downloading over+ // a file somebody may have put there on purpose does.+ Downloaded bool++ // CurrentPath is the installer binary running right now, or "" when the operating+ // system would not say. It exists so the screen can print the exact command that puts+ // the download in place — the installer never runs that command itself.+ CurrentPath string+}++// MoveCommand is what the user runs to put the downloaded build in place, or "" when the+// running installer's own path could not be determined.+//+// The installer does not run it, and that is deliberate rather than unfinished: a process+// cannot reliably replace the binary it is executing. Linux refuses the write outright+// (ETXTBSY), Windows holds the file locked, and the failure modes of getting it wrong end+// with the user having no installer at all. Printing the exact command hands that decision+// back with nothing left to guess.+func (p InstallerPackage) MoveCommand() string {+ if p.Path == "" || p.CurrentPath == "" {+ return ""+ }++ // Quoted on both platforms: an installation under "Program Files" or any path with a+ // space silently becomes two arguments otherwise.+ if p.OS == "windows" {+ return fmt.Sprintf(`move /Y "%s" "%s"`, p.Path, p.CurrentPath)+ }++ return fmt.Sprintf(`mv "%s" "%s"`, p.Path, p.CurrentPath)+}type updateOperationsImpl struct {processor *processor++ // described is the last package description the server gave, and the version and+ // platform it describes. It exists because the screen has to say what it is about to+ // download — version and size — before the user agrees to download it, and asking the+ // same question twice costs a second proof-of-work challenge and a second slice of the+ // account's daily allowance.+ //+ // Every path that touches it holds the processor mutex, which is what makes the plain+ // fields safe: installerPackage takes it, and the operations reaching fetchInstaller+ // run under it too.+ described *models.PackageInfoResponse+ describedKey string}func newUpdateOperations(p *processor) updateOperations {return &updateOperationsImpl{processor: p}}-func (u *updateOperationsImpl) checkUpdates(ctx context.Context, state *operationState) (*checker.CheckUpdatesResponse, error) {+// checkUpdates refreshes what the update server says about this installation.+//+// The answer lands on the shared check result rather than being returned: the menus, the+// update screen and the operations below all read it from there, and a second copy would+// be a second thing to keep current. It is also one call rather than several — every check+// costs a proof-of-work challenge and a slice of the account's daily allowance.+func (u *updateOperationsImpl) checkUpdates(ctx context.Context, state *operationState) error {u.processor.appendLog(MsgCheckingUpdates, ProductStackInstaller, state)- request := u.buildUpdateCheckRequest()- serverURL := u.getUpdateServerURL()- proxyURL := u.getProxyURL()-- response, err := u.callUpdateServer(ctx, serverURL, proxyURL, request)- if err != nil {- return nil, fmt.Errorf("failed to check updates: %w", err)+ if err := u.processor.checker.GatherUpdatesInfo(ctx); err != nil {+ return fmt.Errorf("failed to check updates: %w", err)+ }+ if failure := u.processor.checker.UpdateFailure; failure != nil {+ return fmt.Errorf("failed to check updates: %s", failure.Message)}- return response, nil+ return nil}+// downloadInstaller fetches the offered installer build and verifies it, leaving the file+// next to the environment file.+//+// Applying the update is deliberately not part of this: a process cannot reliably replace+// the binary it is executing, and doing it badly leaves the user with no installer at all.+// Downloading and verifying is the whole operation, and it succeeds when the file is on+// disk and proven intact.func (u *updateOperationsImpl) downloadInstaller(ctx context.Context, state *operationState) error {u.processor.appendLog(MsgDownloadingInstaller, ProductStackInstaller, state)- downloadURL, err := u.getInstallerDownloadURL(ctx)+ path, err := u.fetchInstaller(ctx, state)if err != nil {return err}- tempFile, err := u.downloadBinaryToTemp(ctx, downloadURL)- if err != nil {- return err- }- defer os.Remove(tempFile)-- u.processor.appendLog(MsgVerifyingBinaryChecksum, ProductStackInstaller, state)- if err := u.verifyBinaryChecksum(tempFile); err != nil {- return err- }-- // TODO: copy binary to current update directory+ u.processor.appendLog(fmt.Sprintf(MsgInstallerSavedTo, path), ProductStackInstaller, state)+ u.processor.appendLog(MsgInstallerDownloadCompleted, ProductStackInstaller, state)- u.processor.appendLog(MsgInstallerUpdateCompleted, ProductStackInstaller, state)- return fmt.Errorf("not implemented")+ return nil}+// updateInstaller does the same work as downloadInstaller and reports it as an update.+// The two are separate menu entries; the operation behind them is the same, because what+// remains — swapping the running binary — is out of scope.func (u *updateOperationsImpl) updateInstaller(ctx context.Context, state *operationState) error {u.processor.appendLog(MsgUpdatingInstaller, ProductStackInstaller, state)- downloadURL, err := u.getInstallerDownloadURL(ctx)+ path, err := u.fetchInstaller(ctx, state)if err != nil {return err}- tempFile, err := u.downloadBinaryToTemp(ctx, downloadURL)- if err != nil {- return err- }- defer os.Remove(tempFile)-- u.processor.appendLog(MsgVerifyingBinaryChecksum, ProductStackInstaller, state)- if err := u.verifyBinaryChecksum(tempFile); err != nil {- return err- }-- // TODO: replace installer binary after communication with current installer process- u.processor.appendLog(MsgReplacingInstallerBinary, ProductStackInstaller, state)- if err := u.replaceInstallerBinary(tempFile); err != nil {- return err- }-+ u.processor.appendLog(fmt.Sprintf(MsgInstallerSavedTo, path), ProductStackInstaller, state)u.processor.appendLog(MsgInstallerUpdateCompleted, ProductStackInstaller, state)- return fmt.Errorf("not implemented")++ return nil}+// removeInstaller reports that the binary stays, and succeeds.+//+// A process cannot delete the file it is executing — on Windows it is locked, and on Unix+// the unlink would leave the user with no installer and a half-finished removal. So this+// says where the file is and lets the user decide, exactly as the self-update screen does+// with its move command.+//+// Succeeding matters as much as the message. `remove all` and `purge all` walk every stack+// with the installer LAST, so returning an error here failed the whole operation after all+// the real work had already been done — and in purge it also skipped the docker networks,+// which are removed after that loop.func (u *updateOperationsImpl) removeInstaller(ctx context.Context, state *operationState) error {u.processor.appendLog(MsgRemovingInstaller, ProductStackInstaller, state)- // TODO: remove installer binary-- return fmt.Errorf("not implemented")-}--func (u *updateOperationsImpl) buildUpdateCheckRequest() checker.CheckUpdatesRequest {- currentVersion := version.GetBinaryVersion()- if versionVar, exists := u.processor.state.GetVar("PENTAGI_VERSION"); exists {- currentVersion = versionVar.Value+ if running, err := os.Executable(); err == nil {+ u.processor.appendLog(fmt.Sprintf(MsgInstallerNotRemoved, running), ProductStackInstaller, state)+ } else {+ u.processor.appendLog(MsgInstallerNotRemovedUnknownPath, ProductStackInstaller, state)}- return checker.CheckUpdatesRequest{- InstallerOsType: runtime.GOOS,- InstallerVersion: currentVersion,- GraphitiConnected: u.processor.checker.GraphitiConnected,- GraphitiExternal: u.processor.checker.GraphitiExternal,- GraphitiInstalled: u.processor.checker.GraphitiInstalled,- LangfuseConnected: u.processor.checker.LangfuseConnected,- LangfuseExternal: u.processor.checker.LangfuseExternal,- LangfuseInstalled: u.processor.checker.LangfuseInstalled,- ObservabilityConnected: u.processor.checker.ObservabilityConnected,- ObservabilityExternal: u.processor.checker.ObservabilityExternal,- ObservabilityInstalled: u.processor.checker.ObservabilityInstalled,- }+ return nil}-func (u *updateOperationsImpl) callUpdateServer(- ctx context.Context,- serverURL, proxyURL string,- request checker.CheckUpdatesRequest,-) (*checker.CheckUpdatesResponse, error) {- client := &http.Client{- Timeout: 30 * time.Second,- }-- if proxyURL != "" {- proxyURLParsed, err := url.Parse(proxyURL)- if err == nil {- client.Transport = &http.Transport{- Proxy: http.ProxyURL(proxyURLParsed),- }- }+// fetchInstaller downloads the offered build into the environment directory and returns+// where it landed.+//+// The file is written under its final name, in the directory the installation already+// lives in — not through a temporary copy in the system temp directory. Writing an+// executable to %TEMP% and then moving it is a pattern antivirus and EDR products score+// heavily, and a false positive there costs the user their update.+func (u *updateOperationsImpl) fetchInstaller(ctx context.Context, state *operationState) (string, error) {+ version, err := u.installerTargetVersion()+ if err != nil {+ return "", err}- return u.callExistingUpdateChecker(ctx, serverURL, client, request)-}--func (u *updateOperationsImpl) getInstallerDownloadURL(ctx context.Context) (string, error) {- response, err := u.checkUpdates(ctx, &operationState{})+ client, err := u.cloudClient()if err != nil {return "", err}- if response.InstallerIsUpToDate {- return "", fmt.Errorf("no update available")+ // The description comes first because everything the download is checked against comes+ // from it: the length, the sha256 and the signature.+ info, err := u.packageInfo(ctx, version)+ if err != nil {+ return "", err}- return "https://update.pentagi.com/installer", nil-}+ path := filepath.Join(u.environmentDir(), installerFileName(version))-func (u *updateOperationsImpl) downloadBinaryToTemp(ctx context.Context, downloadURL string) (string, error) {- client := &http.Client{- Timeout: 300 * time.Second,+ // Refusing to write over ourselves. The versioned name normally makes this impossible,+ // but an installer renamed to installer_<its own version> lands exactly here, and the+ // O_TRUNC below would empty the binary the user is running. Linux answers ETXTBSY and+ // macOS does not, so the guard cannot be left to the operating system.+ if running, err := os.Executable(); err == nil && isSameFile(running, path) {+ return "", fmt.Errorf("refusing to overwrite the running installer at %s", path)}- resp, err := client.Get(downloadURL)+ file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o755)if err != nil {- return "", fmt.Errorf("failed to download binary: %w", err)+ return "", fmt.Errorf("failed to create %s: %w", path, err)}- defer resp.Body.Close()- if resp.StatusCode != http.StatusOK {- return "", fmt.Errorf("download failed with status: %s", resp.Status)+ progress := &progressWriter{total: info.Size, report: func(percent int, written, total int64) {+ u.processor.appendLog(+ fmt.Sprintf(MsgInstallerDownloadProgress, percent, FormatSize(written), FormatSize(total)),+ ProductStackInstaller, state,+ )+ }}++ downloadErr := client.DownloadPackage(ctx, models.DownloadPackageRequest{+ Component: models.ComponentTypeInstaller,+ Version: version,+ OS: models.OSType(runtime.GOOS),+ Arch: models.ArchType(runtime.GOARCH),+ }, *info, io.MultiWriter(file, progress))++ if closeErr := file.Close(); closeErr != nil && downloadErr == nil {+ downloadErr = fmt.Errorf("failed to finish writing %s: %w", path, closeErr)}- tempFile, err := os.CreateTemp("", "pentagi-update-*.bin")- if err != nil {- return "", fmt.Errorf("failed to create temp file: %w", err)+ if downloadErr != nil {+ // Verification only completes once the last byte has arrived, so anything left+ // here is unverified. Leaving it would put a file named like a release next to the+ // installation with nothing marking it as suspect.+ _ = os.Remove(path)+ // The description is the yardstick every one of those checks used, so a build+ // republished under the same version since it was fetched fails them all. Dropping+ // it makes the next attempt ask again instead of failing identically forever.+ u.described, u.describedKey = nil, ""+ return "", fmt.Errorf("failed to download installer: %w", downloadErr)}- defer tempFile.Close()- if _, err := io.Copy(tempFile, resp.Body); err != nil {- os.Remove(tempFile.Name())- return "", fmt.Errorf("failed to write downloaded binary: %w", err)- }+ u.processor.appendLog(MsgVerifyingBinaryChecksum, ProductStackInstaller, state)- return tempFile.Name(), nil+ return path, nil}-func (u *updateOperationsImpl) verifyBinaryChecksum(filePath string) error {- file, err := os.Open(filePath)+// installerPackage describes the build on offer without downloading it.+//+// The screen behind "Update Installer" needs the version and the size to say what it is+// about to fetch, and it must say that BEFORE the user agrees to fetch it — the old screen+// asked for consent to an operation it described wrongly.+func (u *updateOperationsImpl) installerPackage(ctx context.Context) (*InstallerPackage, error) {+ version, err := u.installerTargetVersion()if err != nil {- return fmt.Errorf("failed to open binary for verification: %w", err)+ return nil, err}- defer file.Close()- hasher := sha256.New()- if _, err := io.Copy(hasher, file); err != nil {- return fmt.Errorf("failed to calculate checksum: %w", err)+ info, err := u.packageInfo(ctx, version)+ if err != nil {+ return nil, err}- return nil+ path := filepath.Join(u.environmentDir(), installerFileName(version))++ // os.Executable failing is not an error worth stopping for — everything else on the+ // screen is still true, and the only thing lost is the ready-made move command.+ current, _ := os.Executable()++ _, statErr := os.Stat(path)++ return &InstallerPackage{+ Version: version,+ OS: runtime.GOOS,+ Arch: runtime.GOARCH,+ Size: info.Size,+ Path: path,+ Downloaded: statErr == nil,+ CurrentPath: current,+ }, nil}-func (u *updateOperationsImpl) replaceInstallerBinary(newBinaryPath string) error {- currentBinary, err := os.Executable()+// packageInfo asks the update server to describe one installer build, reusing the answer+// already held for the same version and platform.+func (u *updateOperationsImpl) packageInfo(+ ctx context.Context, version string,+) (*models.PackageInfoResponse, error) {+ key := installerPackageKey(version)+ if held := u.cachedPackage(key); held != nil {+ return held, nil+ }++ client, err := u.cloudClient()if err != nil {- return fmt.Errorf("failed to get current binary path: %w", err)+ return nil, err}- backupPath := currentBinary + ".backup"- if err := u.copyFile(currentBinary, backupPath); err != nil {- return fmt.Errorf("failed to create backup: %w", err)+ info, err := client.PackageInfo(ctx, models.PackageInfoRequest{+ Component: models.ComponentTypeInstaller,+ Version: version,+ OS: models.OSType(runtime.GOOS),+ Arch: models.ArchType(runtime.GOARCH),+ })+ if err != nil {+ return nil, fmt.Errorf("failed to get installer package info: %w", err)}- if err := u.copyFile(newBinaryPath, currentBinary); err != nil {- u.copyFile(backupPath, currentBinary)- return fmt.Errorf("failed to replace binary: %w", err)+ u.described, u.describedKey = info, key++ return info, nil+}++// installerPackageKey names one downloadable build.+//+// The platform is part of it and not only the version: the same version is published per+// os/arch, and each build has its own size, sha256 and signature — the three values the+// download is checked against. A description matched on version alone would fail every one+// of them on a machine that asked twice from different platforms, which is what a shared+// home directory or a cross-built binary looks like.+func installerPackageKey(version string) string {+ return version + "/" + runtime.GOOS + "/" + runtime.GOARCH+}++// cachedPackage returns the held description when it describes exactly this build.+func (u *updateOperationsImpl) cachedPackage(key string) *models.PackageInfoResponse {+ if u.described == nil || u.describedKey != key {+ return nil}- if err := os.Chmod(currentBinary, 0755); err != nil {- return fmt.Errorf("failed to set executable permissions: %w", err)+ return u.described+}++// progressWriter turns the bytes going past into an occasional line of progress.+//+// The percentage is against the size from packages/info and not against a Content-Length:+// the response body is encrypted, which removes that header, so this size is the only one+// the client ever learns.+type progressWriter struct {+ total int64+ written int64+ reported int+ report func(percent int, written, total int64)+}++func (w *progressWriter) Write(p []byte) (int, error) {+ w.written += int64(len(p))++ // A stream longer than advertised is the download's business — it fails the length+ // check afterwards. Here it only must not print percentages above a hundred.+ if w.total > 0 && w.report != nil {+ percent := min(int(w.written*100/w.total), 100)+ if step := percent / progressStepPercent; step > w.reported {+ w.reported = step+ w.report(step*progressStepPercent, w.written, w.total)+ }}- os.Remove(backupPath)- return nil+ return len(p), nil}-func (u *updateOperationsImpl) getUpdateServerURL() string {- if serverVar, exists := u.processor.state.GetVar("UPDATE_SERVER_URL"); exists && serverVar.Value != "" {- return serverVar.Value+// FormatSize renders a byte count the way a download would be described out loud.+//+// Exported so the screen and the progress line it prints into use the same renderer: they+// end up in the same terminal panel, and two formatters would sooner or later disagree+// about the same number in front of the user.+func FormatSize(bytes int64) string {+ const unit = 1024+ if bytes < unit {+ return fmt.Sprintf("%d B", bytes)}- return "https://update.pentagi.com"+ value, exp := float64(bytes)/unit, 0+ for value >= unit && exp < 3 {+ value /= unit+ exp+++ }++ return fmt.Sprintf("%.1f %s", value, [...]string{"KiB", "MiB", "GiB", "TiB"}[exp])}-func (u *updateOperationsImpl) getProxyURL() string {- if proxyVar, exists := u.processor.state.GetVar("HTTP_PROXY"); exists {- return proxyVar.Value+// installerTargetVersion reads which build the server offered, from the answer the check+// already obtained.+func (u *updateOperationsImpl) installerTargetVersion() (string, error) {+ for _, stack := range u.processor.checker.StackUpdates {+ if stack.Stack != string(models.ProductStackInstaller) {+ continue+ }+ if version := stack.LatestVersion; version != "" {+ return version, nil+ }+ // The stack entry carries no version of its own on some answers; the artefact for+ // this platform does.+ for _, component := range stack.Components {+ if component.TargetVersion != "" {+ return component.TargetVersion, nil+ }+ }}- return ""+ return "", fmt.Errorf("update server offered no installer version")}-func (u *updateOperationsImpl) copyFile(src, dst string) error {- srcFile, err := os.Open(src)+// isSameFile reports whether two paths name the same file on disk.+//+// By inode rather than by string: a symlink, a relative path and a different spelling of+// the same directory all compare unequal as text while naming one file. A path that does+// not exist is not the same as anything — which is the common case here, and the reason+// this returns false rather than an error.+func isSameFile(a, b string) bool {+ infoA, err := os.Stat(a)if err != nil {- return err+ return false}- defer srcFile.Close()-- dstFile, err := os.Create(dst)+ infoB, err := os.Stat(b)if err != nil {- return err+ return false}- defer dstFile.Close()- if _, err := io.Copy(dstFile, srcFile); err != nil {- return err+ return os.SameFile(infoA, infoB)+}++// installerFileName is what the downloaded build is called. The version is part of the+// name so a download never overwrites the installer currently in use.+func installerFileName(version string) string {+ name := "installer_" + version+ if runtime.GOOS == "windows" {+ name += ".exe"}+ return name+}- return dstFile.Sync()+// cloudClient builds a client from this installation's configuration.+func (u *updateOperationsImpl) cloudClient() (*cloud.Client, error) {+ defaultUpdateServerHost := strings.TrimPrefix(strings.TrimSuffix(checker.DefaultUpdateServerEndpoint, "/"), "https://")+ return cloud.New(cloud.Config{+ Host: u.stateVar("UPDATE_SERVER_HOST", defaultUpdateServerHost),+ ProxyURL: u.stateVar("PROXY_URL", ""),+ LicenseKey: u.stateVar("LICENSE_KEY", ""),+ InstallerVersion: checker.InstallerVersion,+ })}-func (u *updateOperationsImpl) callExistingUpdateChecker(- ctx context.Context,- url string,- client *http.Client,- request checker.CheckUpdatesRequest,-) (*checker.CheckUpdatesResponse, error) {- return &checker.CheckUpdatesResponse{- InstallerIsUpToDate: true,- PentagiIsUpToDate: true,- GraphitiIsUpToDate: true,- LangfuseIsUpToDate: true,- ObservabilityIsUpToDate: true,- WorkerIsUpToDate: true,- }, nil+// environmentDir is where the installation lives — the directory holding the environment+// file, which is also where compose files and stack data sit.+func (u *updateOperationsImpl) environmentDir() string {+ if u.processor.state == nil {+ return "."+ }+ return filepath.Dir(u.processor.state.GetEnvPath())+}++// stateVar reads a configured value, falling back to its default and then to the supplied+// one. PROXY_URL is read through here like everything else: it is the single proxy setting+// the installer keeps, and every call to the update server has to honour it.+func (u *updateOperationsImpl) stateVar(name, fallback string) string {+ if u.processor.state == nil {+ return fallback+ }+ if value, exists := u.processor.state.GetVar(name); exists {+ if value.Value != "" {+ return value.Value+ }+ if value.Default != "" {+ return value.Default+ }+ }+ return fallback}diff --git a/backend/cmd/installer/processor/update_test.go b/backend/cmd/installer/processor/update_test.gonew file mode 100644index 00000000..9e1e0636--- /dev/null+++ b/backend/cmd/installer/processor/update_test.go@@ -0,0 +1,538 @@+package processor++import (+ "context"+ "errors"+ "os"+ "path/filepath"+ "runtime"+ "sync"+ "testing"++ "pentagi/cmd/installer/checker"++ tea "github.com/charmbracelet/bubbletea"+ "github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"+ "github.com/vxcontrol/cloud/models"+)++var _ = checker.CheckResult{}++// updateHarness builds a processor whose check handler records what it was asked, so the+// cost of an operation in cloud requests can be counted rather than argued about.+func updateHarness(t *testing.T, configure func(*mockCheckConfig)) (*processor, *mockCheckHandler) {+ t.Helper()++ handler := newMockCheckHandler()+ if configure != nil {+ configure(&handler.config)+ }++ p := createProcessorWithState(testState(t), createCheckResultWithHandler(handler))+ return p, handler+}++func countCalls(calls []string, name string) int {+ found := 0+ for _, call := range calls {+ if call == name {+ found+++ }+ }+ return found+}++func countMessages[T tea.Msg](msgs []tea.Msg) int {+ found := 0+ for _, msg := range msgs {+ if _, ok := msg.(T); ok {+ found+++ }+ }+ return found+}++// TestOneUpdateCostsOneCheck.+//+// The fresh check after an update is required — only the cloud knows whether anything is+// still pending, and it is that answer, not the local outcome, that repaints the menu. One+// is required. Five is what it used to cost: the check lived in a deferred call inside the+// recursive worker, so `compose` paid for its four stacks and then again for itself, and a+// stack with nothing to do paid too, because the defer was registered before the early+// return.+//+// It is not free. Against the seeded budget for an unlicensed user that was three checks a+// day, so a single press of "Update PentAGI" exhausted the day and part of the next.+func TestOneUpdateCostsOneCheck(t *testing.T) {+ for name, stack := range map[string]ProductStack{+ "every compose stack": ProductStackCompose,+ "a single stack": ProductStackPentagi,+ "everything": ProductStackAll,+ } {+ p, handler := updateHarness(t, func(config *mockCheckConfig) {+ config.PentagiIsUpToDate = false+ config.LangfuseIsUpToDate = false+ config.GraphitiIsUpToDate = false+ config.ObservabilityIsUpToDate = false+ })++ before := countCalls(handler.getCalls(), "GatherUpdatesInfo")+ require.NoError(t, p.update(context.Background(), stack, testOperationState(t)))+ after := countCalls(handler.getCalls(), "GatherUpdatesInfo")++ assert.Equal(t, 1, after-before,+ "%s: the operation asked the cloud %d times; one press must cost one check", name, after-before)+ }+}++// TestAnUpdateOfStacksThatAreNotInstalledStillCostsOneCheck.+//+// Not every installation has every stack, and a stack with nothing to do used to be the+// cheapest way to spend a request: the deferred check was registered before the "already+// up to date" early return, so doing nothing cost exactly as much as doing everything.+func TestAnUpdateOfStacksThatAreNotInstalledStillCostsOneCheck(t *testing.T) {+ p, handler := updateHarness(t, func(config *mockCheckConfig) {+ // Only PentAGI needs anything; the other three are current.+ config.PentagiIsUpToDate = false+ config.LangfuseIsUpToDate = true+ config.GraphitiIsUpToDate = true+ config.ObservabilityIsUpToDate = true+ })++ before := countCalls(handler.getCalls(), "GatherUpdatesInfo")+ require.NoError(t, p.update(context.Background(), ProductStackCompose, testOperationState(t)))++ assert.Equal(t, 1, countCalls(handler.getCalls(), "GatherUpdatesInfo")-before)+}++// TestAnOperationAnnouncesItselfExactlyOnce.+//+// The screen treats a completion as the end of the whole operation: it stops the terminal,+// prints the verdict, and — because HandleMsg returns nil for a completion — stops polling.+// So a completion per nested stack meant the FIRST stack to finish reported success on+// behalf of all of them, and everything after it, including errors, was never drained.+//+// Whichever stacks happen to be installed does not change this: the fan-out list is fixed,+// and a stack with nothing to do is the one that finishes first.+func TestAnOperationAnnouncesItselfExactlyOnce(t *testing.T) {+ for name, stack := range map[string]ProductStack{+ "every compose stack": ProductStackCompose,+ "everything": ProductStackAll,+ "a single stack": ProductStackObservability,+ } {+ p, _ := updateHarness(t, func(config *mockCheckConfig) {+ config.PentagiIsUpToDate = false+ config.LangfuseIsUpToDate = true+ config.GraphitiIsUpToDate = false+ config.ObservabilityIsUpToDate = false+ })++ state := testOperationState(t)+ require.NoError(t, p.update(context.Background(), stack, state))++ assert.Equal(t, 1, countMessages[ProcessorStartedMsg](state.msgs),+ "%s: started announced more than once", name)+ assert.Equal(t, 1, countMessages[ProcessorCompletionMsg](state.msgs),+ "%s: the screen was told the operation finished %d times, and it believes the first",+ name, countMessages[ProcessorCompletionMsg](state.msgs))+ }+}++// TestEveryFanningOperationAnnouncesItselfExactlyOnce.+//+// `update` reaches one announcement by structure — its recursion goes through a worker+// that says nothing. The other five operations still recurse through themselves, so their+// guarantee rests on the check inside sendStarted/sendCompletion, and it has to hold for+// each of them: download, remove, purge, start and stop all fan a request for `compose` or+// `all` out over four stacks.+func TestEveryFanningOperationAnnouncesItselfExactlyOnce(t *testing.T) {+ for name, run := range map[string]func(*processor, *operationState) error{+ "download": func(p *processor, s *operationState) error {+ return p.download(context.Background(), ProductStackCompose, s)+ },+ "remove": func(p *processor, s *operationState) error {+ return p.remove(context.Background(), ProductStackCompose, s)+ },+ "start": func(p *processor, s *operationState) error {+ return p.start(context.Background(), ProductStackCompose, s)+ },+ "stop": func(p *processor, s *operationState) error {+ return p.stop(context.Background(), ProductStackCompose, s)+ },+ } {+ p, _ := updateHarness(t, nil)+ state := testOperationState(t)+ _ = run(p, state)++ assert.LessOrEqual(t, countMessages[ProcessorCompletionMsg](state.msgs), 1,+ "%s: the screen was told the operation finished more than once, and it believes the first", name)+ assert.LessOrEqual(t, countMessages[ProcessorStartedMsg](state.msgs), 1,+ "%s: started announced more than once", name)+ }+}++// TestAFailureInsideTheFanOutReachesTheScreen.+//+// The other half of announcing once: the single completion has to carry the error. A stack+// failing in the middle of the fan-out used to be reported as a completion of its own —+// after an earlier stack had already reported success and stopped the screen polling.+func TestAFailureInsideTheFanOutReachesTheScreen(t *testing.T) {+ p, _ := updateHarness(t, func(config *mockCheckConfig) {+ config.PentagiIsUpToDate = false+ config.LangfuseIsUpToDate = false+ config.GraphitiIsUpToDate = false+ config.ObservabilityIsUpToDate = false+ })++ // Observability is first in the update order, so this fails partway through the run.+ injectComposeError(p, map[string]error{"updateStack": errors.New("compose exploded")})++ state := testOperationState(t)+ err := p.update(context.Background(), ProductStackCompose, state)+ require.Error(t, err, "a stack failing must fail the operation")++ require.Equal(t, 1, countMessages[ProcessorCompletionMsg](state.msgs))+ for _, msg := range state.msgs {+ if completion, ok := msg.(ProcessorCompletionMsg); ok {+ assert.Error(t, completion.Error,+ "the one completion the screen sees reported success while a stack had failed")+ assert.Equal(t, ProductStackCompose, completion.Stack,+ "the completion must name the stack the user asked about, not a nested one")+ }+ }+}++// TestRemovingEverythingIsNotFailedByTheInstallerItself.+//+// `remove all` and `purge all` walk every stack with the installer LAST, and removing the+// installer binary is something a running process cannot do. It used to answer "not+// implemented", so the whole operation ended in an error AFTER all the real work had+// already succeeded — the user saw a failure for an installation that had been removed.+func TestRemovingEverythingIsNotFailedByTheInstallerItself(t *testing.T) {+ // The REAL implementation, not the harness mock: what is under test is the value this+ // function returns, and a mock returning nil would prove nothing.+ u := &updateOperationsImpl{processor: &processor{}}++ assert.NoError(t, u.removeInstaller(context.Background(), newOperationState(nil)),+ "the one stack whose removal is deliberately out of scope must not fail the run")+}++// TestRemovingEverythingAttemptsEveryStack.+//+// The loop used to return on the first failure, so a single stack that could not be+// removed left the others untouched — on an operation the user asked to remove everything.+// Every stack is attempted now, and every failure is reported rather than only the first.+func TestRemovingEverythingAttemptsEveryStack(t *testing.T) {+ p, _ := updateHarness(t, nil)+ composeOps := p.composeOps.(*baseMockComposeOperations)++ // removeStack fails for the first compose stack it is asked about.+ injectComposeError(p, map[string]error{"removeStack": errors.New("compose exploded")})++ err := p.remove(context.Background(), ProductStackAll, testOperationState(t))+ require.Error(t, err, "a stack that could not be removed must still fail the operation")++ attempted := map[ProductStack]bool{}+ for _, call := range composeOps.getCalls() {+ if call.Method == "removeStack" {+ attempted[call.Stack] = true+ }+ }+ for _, stack := range composeOperationAllStacksOrder[ProcessorOperationRemove] {+ assert.True(t, attempted[stack],+ "%s was never attempted because an earlier stack failed", stack)+ }+}++// TestPurgingEverythingStillRemovesTheNetworks.+//+// The three custom docker networks were removed AFTER the loop over the stacks, and the+// loop returned on the first error — so one stack that could not be purged left all three+// behind, on the one operation whose entire purpose is to leave nothing. Which stacks an+// installation has varies, so "a stack that fails" is not an exotic path.+func TestPurgingEverythingStillRemovesTheNetworks(t *testing.T) {+ p, _ := updateHarness(t, nil)+ dockerOps := p.dockerOps.(*baseMockDockerOperations)++ // The first stack in the purge order fails, which used to end the operation there.+ injectComposeError(p, map[string]error{"purgeImagesStack": errors.New("compose exploded")})++ err := p.purge(context.Background(), ProductStackAll, testOperationState(t))+ require.Error(t, err, "a stack that could not be purged must still fail the operation")++ removed := map[string]bool{}+ for _, call := range dockerOps.getCalls() {+ if call.Method == "removeMainDockerNetwork" {+ removed[call.Name] = true+ }+ }++ for _, network := range []ProductDockerNetwork{+ ProductDockerNetworkPentagi, ProductDockerNetworkObservability, ProductDockerNetworkLangfuse,+ } {+ assert.True(t, removed[string(network)],+ "network %s was left behind because an earlier stack failed", network)+ }+}++// TestAFastFailureStillReachesTheScreen.+//+// wrapCommand waits up to 500 ms for the operation before handing a tea.Cmd back, so an+// operation that fails faster than that delivers its error through ProcessorWaitMsg. That+// message used to STOP the polling — and the completion message, the one that clears the+// spinner and prints what happened, sits in the queue waiting to be polled. So the screen+// said "in progress" for the rest of the session, Enter did nothing, and the error was+// shown nowhere.+//+// What is asserted is the message chain, not the screen: HandleMsg must keep polling after+// an error so the completion can be drained, and must stop once it has been.+func TestAFastFailureStillReachesTheScreen(t *testing.T) {+ p, _ := updateHarness(t, nil)+ model := &processorModel{processor: p}++ state := testOperationState(t)+ state.sendCompletion(ProductStackAll, errors.New("failed before the window closed"))++ failed := ProcessorWaitMsg{+ Error: errors.New("failed before the window closed"),+ Stack: ProductStackAll, state: state, num: 0,+ }++ next := model.HandleMsg(failed)+ require.NotNil(t, next, "polling stopped, so the completion already in the queue is never delivered")++ delivered := next()+ completion, ok := delivered.(ProcessorCompletionMsg)+ require.True(t, ok, "expected the queued completion, got %T", delivered)+ assert.Error(t, completion.Error, "the completion must carry the failure")++ // And the chain terminates: a completion is the end of it.+ assert.Nil(t, model.HandleMsg(completion), "polling must stop once the completion is delivered")+}++// TestTheStacksAnUpdateTouchesAreTheOnesItVerifies. Targets are captured before anything+// changes, so the expansion has to match what the operation actually runs.+func TestTheStacksAnUpdateTouchesAreTheOnesItVerifies(t *testing.T) {+ assert.Equal(t, composeOperationAllStacksOrder[ProcessorOperationUpdate],+ stacksTouchedByUpdate(ProductStackCompose))++ assert.Equal(t, []ProductStack{ProductStackPentagi}, stacksTouchedByUpdate(ProductStackPentagi))++ all := stacksTouchedByUpdate(ProductStackAll)+ assert.Contains(t, all, ProductStackWorker)+ assert.Contains(t, all, ProductStackInstaller)+ assert.Len(t, all, len(composeOperationAllStacksOrder[ProcessorOperationUpdate])+2)++ // The expansion must not alias the shared order slice: append on a shared backing+ // array would rewrite the table every other caller reads.+ assert.Equal(t, 4, len(composeOperationAllStacksOrder[ProcessorOperationUpdate]),+ "stacksTouchedByUpdate(all) grew the shared compose order table")+}++// TestProgressIsReportedOncePerTenthAndNeverPastAHundred.+//+// The panel this writes into scrolls, so a line per read would bury the messages around it+// — the path the file landed on, and the line saying verification passed. A tenth is the+// step; the same tenth must never be printed twice, and a stream longer than advertised+// must not print percentages above a hundred (the download fails the length check+// afterwards, which is where that belongs).+func TestProgressIsReportedOncePerTenthAndNeverPastAHundred(t *testing.T) {+ var reported []int+ w := &progressWriter{total: 1000, report: func(percent int, _, _ int64) {+ reported = append(reported, percent)+ }}++ // 100 writes of 10 bytes: every tenth is crossed exactly once.+ for range 100 {+ n, err := w.Write(make([]byte, 10))+ require.NoError(t, err)+ require.Equal(t, 10, n, "a writer that reports fewer bytes than it took stalls io.Copy")+ }+ assert.Equal(t, []int{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}, reported)++ // Past the advertised length the percentage is pinned rather than climbing.+ _, err := w.Write(make([]byte, 500))+ require.NoError(t, err)+ assert.Equal(t, []int{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}, reported,+ "a longer stream must not print 150%%; the length check is what rejects it")+}++// TestProgressIsSilentWithoutASize. packages/info is the only place a size comes from —+// the response body is encrypted, which strips Content-Length. Without one there is no+// percentage to print, and printing 0% forever would read as a stalled download.+func TestProgressIsSilentWithoutASize(t *testing.T) {+ called := false+ w := &progressWriter{total: 0, report: func(int, int64, int64) { called = true }}++ _, err := w.Write(make([]byte, 4096))+ require.NoError(t, err)+ assert.False(t, called)+}++// TestTheDescriptionIsKeyedByBuildAndNotByVersion.+//+// The description carries the size, the sha256 and the signature the download is checked+// against, and all three are per-platform. Handing back the description of another build+// would fail every check with a message about a corrupt download.+func TestTheDescriptionIsKeyedByBuildAndNotByVersion(t *testing.T) {+ held := &models.PackageInfoResponse{Version: "1.2.3", Size: 42}+ u := &updateOperationsImpl{described: held, describedKey: installerPackageKey("1.2.3")}++ assert.Same(t, held, u.cachedPackage(installerPackageKey("1.2.3")))+ assert.Nil(t, u.cachedPackage(installerPackageKey("1.2.4")), "a different version")+ assert.Nil(t, u.cachedPackage("1.2.3/plan9/sparc"), "a different platform")++ assert.Contains(t, installerPackageKey("1.2.3"), runtime.GOOS)+ assert.Contains(t, installerPackageKey("1.2.3"), runtime.GOARCH)++ // Nothing held is not the same as anything matching an empty key.+ empty := &updateOperationsImpl{}+ assert.Nil(t, empty.cachedPackage(""))+}++// TestAFailedDownloadLeavesNoFileAndNoStaleDescription.+//+// Two things have to happen when a download does not verify, and both are easy to get+// wrong. The file must go: verification only completes after the last byte, so whatever is+// on disk is unverified, and leaving it puts something named like a release next to the+// installation with nothing marking it as suspect. And the description must go: it is the+// yardstick every check used, so if the build was republished under the same version since+// it was fetched, keeping it makes every retry fail identically forever.+//+// The description here is deliberately malformed, which the SDK rejects before opening a+// connection — this is about the failure path, not about the network.+func TestAFailedDownloadLeavesNoFileAndNoStaleDescription(t *testing.T) {+ dir := t.TempDir()+ // environmentDir() is the directory of the environment file, and "." without state.+ t.Chdir(dir)++ p := &processor{+ mu: &sync.Mutex{},+ checker: &checker.CheckResult{+ StackUpdates: []checker.StackUpdate{{Stack: "installer", LatestVersion: "1.2.3"}},+ },+ }+ u := &updateOperationsImpl{+ processor: p,+ described: &models.PackageInfoResponse{Size: 10}, // no hash, no signature+ describedKey: installerPackageKey("1.2.3"),+ }+ p.updateOps = u++ _, err := u.fetchInstaller(context.Background(), newOperationState(nil))+ require.Error(t, err)++ assert.NoFileExists(t, filepath.Join(dir, installerFileName("1.2.3")),+ "an unverified file must not be left under a name that looks like a release")+ assert.Nil(t, u.described,+ "keeping the description that failed makes every retry fail the same way")+}++// TestTheDownloadedFileCarriesTheVersion. A download must never overwrite the installer+// currently in use — it is running from this directory.+func TestTheDownloadedFileCarriesTheVersion(t *testing.T) {+ name := installerFileName("2.0.1")++ assert.Contains(t, name, "2.0.1")+ if runtime.GOOS == "windows" {+ assert.Equal(t, "installer_2.0.1.exe", name, "Windows will not execute it without .exe")+ } else {+ assert.Equal(t, "installer_2.0.1", name)+ }+}++// TestTheRunningInstallerIsNeverOverwritten.+//+// The versioned name normally keeps the download away from the binary in use, but an+// installer renamed to installer_<its own version> lands on exactly that path, and the+// O_TRUNC in fetchInstaller would empty it. Linux answers ETXTBSY and macOS does not, so+// the guard cannot be left to the operating system.+func TestTheRunningInstallerIsNeverOverwritten(t *testing.T) {+ dir := t.TempDir()+ t.Chdir(dir)++ running, err := os.Executable()+ require.NoError(t, err)++ p := &processor{+ mu: &sync.Mutex{},+ checker: &checker.CheckResult{+ StackUpdates: []checker.StackUpdate{{Stack: "installer", LatestVersion: "1.2.3"}},+ },+ }+ u := &updateOperationsImpl{+ processor: p,+ described: &models.PackageInfoResponse{Size: 10},+ describedKey: installerPackageKey("1.2.3"),+ }+ p.updateOps = u++ // A hard link makes the target the same file as the test binary without touching it:+ // os.SameFile compares inodes, which is the comparison the guard makes.+ target := filepath.Join(dir, installerFileName("1.2.3"))+ if err := os.Link(running, target); err != nil {+ t.Skipf("cannot hard-link the test binary here: %v", err)+ }++ _, err = u.fetchInstaller(context.Background(), newOperationState(nil))+ require.Error(t, err)+ assert.Contains(t, err.Error(), "refusing to overwrite the running installer")++ // And the file is still whole — this is the whole point of the guard.+ info, statErr := os.Stat(target)+ require.NoError(t, statErr)+ assert.Positive(t, info.Size(), "the running installer was truncated")+}++// TestIsSameFileComparesInodesNotStrings. A symlink, a relative path and a different+// spelling of the same directory all name one file while comparing unequal as text.+func TestIsSameFileComparesInodesNotStrings(t *testing.T) {+ dir := t.TempDir()+ real := filepath.Join(dir, "real")+ require.NoError(t, os.WriteFile(real, []byte("x"), 0o644))++ link := filepath.Join(dir, "link")+ require.NoError(t, os.Symlink(real, link))++ assert.True(t, isSameFile(real, link), "a symlink names the file it points at")+ assert.True(t, isSameFile(real, filepath.Join(dir, ".", "real")), "same path, other spelling")+ assert.False(t, isSameFile(real, filepath.Join(dir, "missing")), "a path that does not exist is nothing")+ assert.False(t, isSameFile(filepath.Join(dir, "missing"), real))+}++// TestTheMoveCommandNamesBothPathsAndQuotesThem.+//+// This command is the entire handover: the installer does not apply the update, so what is+// printed here is what the user runs. An unquoted path under "Program Files" becomes two+// arguments, and mv is not a command Windows has.+func TestTheMoveCommandNamesBothPathsAndQuotesThem(t *testing.T) {+ unix := InstallerPackage{OS: "linux", Path: "/opt/p/installer_2.1.0", CurrentPath: "/usr/local/bin/inst"}+ assert.Equal(t, `mv "/opt/p/installer_2.1.0" "/usr/local/bin/inst"`, unix.MoveCommand())++ windows := InstallerPackage{+ OS: "windows", Path: `C:\p\installer_2.1.0.exe`, CurrentPath: `C:\Program Files\P\inst.exe`,+ }+ assert.Equal(t, `move /Y "C:\p\installer_2.1.0.exe" "C:\Program Files\P\inst.exe"`, windows.MoveCommand())++ // Nothing is invented when os.Executable said nothing: a command with a guessed+ // destination in it is worse than no command at all.+ assert.Empty(t, InstallerPackage{OS: "linux", Path: "/opt/p/x"}.MoveCommand())+ assert.Empty(t, InstallerPackage{OS: "linux", CurrentPath: "/usr/local/bin/inst"}.MoveCommand())+}++func TestFormatSize(t *testing.T) {+ for size, want := range map[int64]string{+ 0: "0 B",+ 512: "512 B",+ 1024: "1.0 KiB",+ 1536: "1.5 KiB",+ 25 << 20: "25.0 MiB",+ 3 << 30: "3.0 GiB",+ 5 << 40: "5.0 TiB",+ 1 << 50: "1024.0 TiB", // no unit above TiB: it stops climbing rather than wrapping+ -1: "-1 B",+ } {+ assert.Equal(t, want, FormatSize(size), "%d bytes", size)+ }+}diff --git a/backend/cmd/installer/processor/verify.go b/backend/cmd/installer/processor/verify.gonew file mode 100644index 00000000..16419826--- /dev/null+++ b/backend/cmd/installer/processor/verify.go@@ -0,0 +1,175 @@+package processor++import (+ "fmt"++ "pentagi/cmd/installer/checker"+)++// Verifying an update means answering one question: did the artefacts that are now+// installed become the ones the server offered? It is asked in two steps.+//+// The first is local. Before the update, the server named a target digest for each+// artefact; afterwards, the components are observed again and compared against those+// targets. The second step is the fresh update check that follows, and it is what actually+// decides whether the menus still offer an update — because only the server knows whether+// something newer appeared in the meantime.+//+// The two disagree in one legitimate case. A moving tag such as "latest" points at whatever+// the registry holds now, and the registry can move between the answer and the pull. Then+// what is installed matches no target and yet is not wrong — it is newer. The fresh check+// settles it: if the server no longer offers an update for the stack, whatever is installed+// is current, and a digest that differs from the old target means the registry moved ahead+// rather than that the update failed.++// verifiableUpdateStacks are the stacks whose update replaces what is installed, and so+// the only ones a comparison afterwards can say anything about. The installer is absent+// deliberately: its operation downloads a build and stops there, leaving the running binary+// as it was, so a comparison would report the intended outcome as a mismatch.+var verifiableUpdateStacks = map[ProductStack]bool{+ ProductStackPentagi: true,+ ProductStackGraphiti: true,+ ProductStackLangfuse: true,+ ProductStackObservability: true,+ ProductStackWorker: true,+}++// componentTarget is the digest the server named for one artefact, captured before the+// update so it survives the answer being replaced by a newer one.+type componentTarget struct {+ name string+ // digest is empty when the answer named no digest for this artefact. That is "nothing+ // to compare", not a mismatch — the server omits what it does not know.+ digest string+}++// captureStackTargets records what the current answer says a stack's artefacts should+// become. It must run before the update replaces the answer.+func captureStackTargets(result *checker.CheckResult, stack string) []componentTarget {+ if result == nil {+ return nil+ }++ for _, update := range result.StackUpdates {+ if update.Stack != stack {+ continue+ }++ targets := make([]componentTarget, 0, len(update.Components))+ for _, component := range update.Components {+ targets = append(targets, componentTarget{+ name: componentIdentity(component.Component, component.OS, component.Arch),+ // TargetDigest is the offered identity in the same form the installed+ // side reports it — a config digest for images, a file hash for files.+ digest: componentTargetDigest(component),+ })+ }++ return targets+ }++ return nil+}++// componentTargetDigest is the value an updated artefact should end up carrying, in the+// same identity the installed side reports: a config digest for an image, a file hash for+// a file. TargetDigest carries exactly that for both kinds.+//+// It used to read TargetVersion, which held a digest for images and a VERSION for files —+// so the Jaeger plugin, which has a hash and no version, was checked as "expected 0.13.0,+// got a423c6…" and reported a mismatch after every successful update.+func componentTargetDigest(component checker.ComponentUpdate) string {+ if !component.Verifiable {+ return ""+ }+ if component.Outdated {+ // The check concluded this differs from what is installed, so the target is what+ // the answer offered rather than what was there.+ return component.TargetDigest+ }+ // Already current: the installed value is the target.+ return component.CurrentVersion+}++func componentIdentity(component, os, arch string) string {+ return component + "/" + os + "/" + arch+}++// verifyStackUpdate compares what is installed now against what was offered before, and+// writes the outcome to the terminal.+//+// It never fails the operation. The update itself either worked or reported an error; this+// is a report about the result, and turning a registry that moved ahead into a failed+// update would be worse than saying nothing.+func (p *processor) verifyStackUpdate(+ stack ProductStack, targets []componentTarget, state *operationState,+) {+ if len(targets) == 0 {+ return+ }++ p.appendLog(fmt.Sprintf(MsgVerifyingUpdatedComponents, stack), stack, state)++ installed := make(map[string]string, len(p.checker.InstalledComponents))+ for _, component := range p.checker.InstalledComponents {+ identity := componentIdentity(component.Component, component.OS, component.Arch)+ installed[identity] = firstNonEmptyString(component.Digest, component.Version)+ }++ // The fresh check that just ran is the authority on whether anything is still pending.+ stackCurrent := stackHasNoUpdate(p.checker, string(stack))++ mismatches := 0+ for _, target := range targets {+ actual := installed[target.name]++ switch {+ case target.digest == "" || actual == "":+ p.appendLog(fmt.Sprintf(MsgComponentNotVerifiable, target.name), stack, state)++ case actual == target.digest:+ p.appendLog(fmt.Sprintf(MsgComponentUpToDate, target.name), stack, state)++ case stackCurrent:+ // Different from what was offered, yet the server says there is nothing left+ // to apply: the registry moved on between the answer and the pull.+ p.appendLog(fmt.Sprintf(MsgComponentNewerThanOffered, target.name), stack, state)++ default:+ mismatches+++ p.appendLog(fmt.Sprintf(MsgComponentMismatch, target.name, target.digest, actual), stack, state)+ }+ }++ if mismatches == 0 {+ p.appendLog(fmt.Sprintf(MsgVerificationPassed, stack), stack, state)+ return+ }+ p.appendLog(fmt.Sprintf(MsgVerificationMismatch, mismatches, stack), stack, state)+}++// stackHasNoUpdate reports whether the freshest answer leaves nothing to apply for a stack.+// A stack the answer does not mention counts as current: the server reports on what it was+// asked about, and silence is not evidence of an update.+func stackHasNoUpdate(result *checker.CheckResult, stack string) bool {+ if result == nil || result.UpdateFailure != nil {+ return false+ }++ for _, update := range result.StackUpdates {+ if update.Stack == stack {+ return !update.HasUpdate+ }+ }++ return true+}++func firstNonEmptyString(values ...string) string {+ for _, value := range values {+ if value != "" {+ return value+ }+ }+ return ""+}diff --git a/backend/cmd/installer/processor/verify_test.go b/backend/cmd/installer/processor/verify_test.gonew file mode 100644index 00000000..081c3ba5--- /dev/null+++ b/backend/cmd/installer/processor/verify_test.go@@ -0,0 +1,250 @@+package processor++import (+ "runtime"+ "strings"+ "sync"+ "testing"++ "pentagi/cmd/installer/checker"+)++func componentDigest(fill string) string {+ return strings.Repeat(fill, 64/len(fill))+}++// TestTargetsAreCapturedBeforeTheAnswerIsReplaced covers the ordering the whole+// verification depends on. The update refreshes the check, so by the time the comparison+// runs the answer describes the NEW state — comparing against it would compare the result+// with itself and always agree.+func TestTargetsAreCapturedBeforeTheAnswerIsReplaced(t *testing.T) {+ before := &checker.CheckResult{StackUpdates: []checker.StackUpdate{{+ Stack: "pentagi",+ HasUpdate: true,+ Components: []checker.ComponentUpdate{{+ Component: "pentagi", OS: "linux", Arch: "amd64",+ Verifiable: true, Outdated: true,+ CurrentVersion: componentDigest("a"),+ TargetDigest: componentDigest("b"),+ }},+ }}}++ targets := captureStackTargets(before, "pentagi")++ if len(targets) != 1 {+ t.Fatalf("captured %d targets, want 1", len(targets))+ }+ if targets[0].digest != componentDigest("b") {+ t.Errorf("target digest = %q, want the offered build", targets[0].digest)+ }+ if targets[0].name != "pentagi/linux/amd64" {+ t.Errorf("target name = %q, want pentagi/linux/amd64", targets[0].name)+ }+}++// TestTargetOfAnAlreadyCurrentComponentIsWhatItRuns: under the stable strategy the answer+// lists every artefact of the release, including ones that already match. For those the+// target is what is installed, so the verification confirms nothing changed rather than+// expecting a change that was never due.+func TestTargetOfAnAlreadyCurrentComponentIsWhatItRuns(t *testing.T) {+ current := componentDigest("c")+ before := &checker.CheckResult{StackUpdates: []checker.StackUpdate{{+ Stack: "pentagi",+ Components: []checker.ComponentUpdate{{+ Component: "scraper", OS: "linux", Arch: "amd64",+ Verifiable: true, Outdated: false,+ CurrentVersion: current,+ TargetDigest: "",+ }},+ }}}++ targets := captureStackTargets(before, "pentagi")++ if len(targets) != 1 || targets[0].digest != current {+ t.Fatalf("targets = %+v, want the installed digest as the target", targets)+ }+}++// TestUnverifiableComponentCarriesNoTarget: the server omits a digest it does not know, and+// inventing one would turn "cannot tell" into a mismatch report.+func TestUnverifiableComponentCarriesNoTarget(t *testing.T) {+ before := &checker.CheckResult{StackUpdates: []checker.StackUpdate{{+ Stack: "pentagi",+ Components: []checker.ComponentUpdate{{+ Component: "pgvector", OS: "linux", Arch: "amd64",+ // Something IS installed — the check knows what it is. What is missing is a+ // published digest to compare it against, and without the guard the installed+ // value would be promoted into a target, turning "cannot tell" into a claim.+ Verifiable: false,+ CurrentVersion: componentDigest("d"),+ }},+ }}}++ targets := captureStackTargets(before, "pentagi")++ if len(targets) != 1 {+ t.Fatalf("captured %d targets, want 1", len(targets))+ }+ if targets[0].digest != "" {+ t.Errorf("target digest = %q, want empty for an artefact the server could not name", targets[0].digest)+ }+}++func TestStackHasNoUpdate(t *testing.T) {+ tests := []struct {+ name string+ result *checker.CheckResult+ stack string+ want bool+ }{+ {+ name: "the answer says nothing is pending",+ result: &checker.CheckResult{StackUpdates: []checker.StackUpdate{{Stack: "pentagi", HasUpdate: false}}},+ stack: "pentagi",+ want: true,+ },+ {+ name: "the answer still offers an update",+ result: &checker.CheckResult{StackUpdates: []checker.StackUpdate{{Stack: "pentagi", HasUpdate: true}}},+ stack: "pentagi",+ want: false,+ },+ {+ // The server reports on what it was asked about; silence about a stack is not+ // evidence that something is pending for it.+ name: "the stack is not mentioned",+ result: &checker.CheckResult{StackUpdates: []checker.StackUpdate{{Stack: "langfuse"}}},+ stack: "pentagi",+ want: true,+ },+ {+ // Without a fresh answer there is no authority to appeal to, so a difference+ // cannot be excused as the registry having moved ahead.+ name: "the refreshed check failed",+ result: &checker.CheckResult{+ UpdateFailure: &checker.UpdateCheckFailure{Reason: "unreachable"},+ StackUpdates: []checker.StackUpdate{{Stack: "pentagi", HasUpdate: false}},+ },+ stack: "pentagi",+ want: false,+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ if got := stackHasNoUpdate(tt.result, tt.stack); got != tt.want {+ t.Errorf("stackHasNoUpdate = %t, want %t", got, tt.want)+ }+ })+ }+}++// TestVerificationDistinguishesNewerFromWrong is the case the plan calls out: a moving tag+// can point somewhere new between the answer and the pull, so what arrives matches no+// target and is still correct. The fresh check settles it — if the server no longer offers+// an update, whatever is installed is current.+func TestVerificationDistinguishesNewerFromWrong(t *testing.T) {+ target, arrived := componentDigest("a"), componentDigest("b")++ tests := []struct {+ name string+ stillOffered bool+ wantSubstring string+ }{+ {+ name: "registry moved ahead",+ stillOffered: false,+ wantSubstring: "newer than the offered build",+ },+ {+ name: "the update did not take",+ stillOffered: true,+ wantSubstring: "expected",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ result := &checker.CheckResult{+ StackUpdates: []checker.StackUpdate{{Stack: "pentagi", HasUpdate: tt.stillOffered}},+ InstalledComponents: []checker.InstalledComponent{{+ Component: "pentagi", OS: "linux", Arch: "amd64", Digest: arrived,+ }},+ }+ p := &processor{checker: result}+ state := &operationState{mx: &sync.Mutex{}, ctx: t.Context()}++ p.verifyStackUpdate(ProductStackPentagi,+ []componentTarget{{name: "pentagi/linux/amd64", digest: target}}, state)++ if reported := state.output.String(); !strings.Contains(reported, tt.wantSubstring) {+ t.Errorf("verification said %q, want it to mention %q", reported, tt.wantSubstring)+ }+ })+ }+}++// TestVerificationReportsAMatchAndAnUnverifiableComponentDifferently: "matches" and "cannot+// be checked" are different facts, and reporting the second as the first would claim proof+// nobody has.+func TestVerificationReportsAMatchAndAnUnverifiableComponentDifferently(t *testing.T) {+ matched := componentDigest("a")++ result := &checker.CheckResult{+ StackUpdates: []checker.StackUpdate{{Stack: "pentagi", HasUpdate: false}},+ InstalledComponents: []checker.InstalledComponent{+ {Component: "pentagi", OS: "linux", Arch: "amd64", Digest: matched},+ {Component: "scraper", OS: "linux", Arch: "amd64", Digest: componentDigest("c")},+ },+ }+ p := &processor{checker: result}+ state := &operationState{mx: &sync.Mutex{}, ctx: t.Context()}++ p.verifyStackUpdate(ProductStackPentagi, []componentTarget{+ {name: "pentagi/linux/amd64", digest: matched},+ {name: "scraper/linux/amd64", digest: ""},+ }, state)++ text := state.output.String()+ if !strings.Contains(text, "matches the published build") {+ t.Error("the matching component was not reported as matching")+ }+ if !strings.Contains(text, "no published digest") {+ t.Error("the unverifiable component was not distinguished from a match")+ }+ if strings.Contains(text, "do not match") {+ t.Error("an unverifiable component was counted as a mismatch")+ }+}++// TestInstallerUpdatesAreNotVerifiedAgainstTheRunningBinary: the installer operation+// downloads a build and stops, deliberately leaving the running binary alone. Comparing+// them would report the intended outcome as a failure.+func TestInstallerUpdatesAreNotVerifiedAgainstTheRunningBinary(t *testing.T) {+ if verifiableUpdateStacks[ProductStackInstaller] {+ t.Error("the installer stack is verified against a binary its update never replaces")+ }+ for _, stack := range []ProductStack{+ ProductStackPentagi, ProductStackGraphiti,+ ProductStackLangfuse, ProductStackObservability, ProductStackWorker,+ } {+ if !verifiableUpdateStacks[stack] {+ t.Errorf("stack %s replaces what is installed but is never verified", stack)+ }+ }+}++func TestInstallerFileName(t *testing.T) {+ name := installerFileName("2.1.0")++ // The version is in the name so a download never lands on the installer in use.+ if !strings.Contains(name, "2.1.0") {+ t.Errorf("file name %q does not identify the build", name)+ }+ if runtime.GOOS == "windows" && !strings.HasSuffix(name, ".exe") {+ t.Errorf("file name %q is not executable on this platform", name)+ }+ if runtime.GOOS != "windows" && strings.HasSuffix(name, ".exe") {+ t.Errorf("file name %q carries a windows suffix", name)+ }+}diff --git a/backend/cmd/installer/state/state.go b/backend/cmd/installer/state/state.goindex 93aead66..bc3746a8 100644--- a/backend/cmd/installer/state/state.go+++ b/backend/cmd/installer/state/state.go@@ -33,6 +33,20 @@ type State interface {SetVars(vars map[string]string) errorResetVars(names []string) error+ // WriteVars puts values into the environment FILE, bypassing the staging area.+ //+ // Everything else here stages: SetVar and SetVars record an intention that only+ // reaches .env when the user applies their changes. That is right for anything a+ // person typed into a form, and wrong for a value the installer itself must have on+ // disk before the very next command runs — `docker compose` is given --env-file+ // pointing at the real file, so a staged variable is one compose has never heard of+ // and it silently uses the default the compose file ships with.+ //+ // The user's pending edits are not written along with it: the file is re-read from+ // disk, these values are set on that, and the result is saved. Committing the whole+ // staged view here would apply form changes the user has not agreed to apply.+ WriteVars(vars map[string]string) error+GetAllVars() map[string]loader.EnvVarGetEnvPath() string}@@ -220,6 +234,40 @@ func (s *state) SetVars(vars map[string]string) error {return s.flushState()}+func (s *state) WriteVars(vars map[string]string) error {+ s.mx.Lock()+ defer s.mx.Unlock()++ if len(vars) == 0 {+ return nil+ }++ // From disk, not from s.envFile: that one carries every staged edit, and saving it+ // would apply the user's un-applied form changes as a side effect of an update.+ envFile, err := loader.LoadEnvFile(s.envPath)+ if err != nil {+ return err+ }++ for name, value := range vars {+ envFile.Set(name, value)+ }++ if err := envFile.Save(s.envPath); err != nil {+ return err+ }++ // The in-memory view is kept in step so a later Commit does not write the previous+ // value back over what was just saved. The staging FILE is deliberately not touched:+ // a value the installer wrote for itself must not make the installation look dirty+ // and prompt the user to apply something they never changed.+ for name, value := range vars {+ s.envFile.Set(name, value)+ }++ return nil+}+func (s *state) ResetVars(names []string) error {s.mx.Lock()defer s.mx.Unlock()diff --git a/backend/cmd/installer/wizard/controller/controller.go b/backend/cmd/installer/wizard/controller/controller.goindex d7ba51cb..b3f603a5 100644--- a/backend/cmd/installer/wizard/controller/controller.go+++ b/backend/cmd/installer/wizard/controller/controller.go@@ -2068,6 +2068,9 @@ type ServerSettingsConfig struct {// direct form field mappings using loader.EnvVarTenantID loader.EnvVar // TENANT_IDLicenseKey loader.EnvVar // LICENSE_KEY+ UpdateStrategy loader.EnvVar // UPDATE_STRATEGY+ UpdateServerHost loader.EnvVar // UPDATE_SERVER_HOST+ SupportServerHost loader.EnvVar // SUPPORT_SERVER_HOSTPprofAddr loader.EnvVar // PPROF_ADDRListenIP loader.EnvVar // PENTAGI_LISTEN_IPListenPort loader.EnvVar // PENTAGI_LISTEN_PORT@@ -2094,6 +2097,9 @@ func (c *controller) GetServerSettingsConfig() *ServerSettingsConfig {vars, _ := c.GetVars([]string{"TENANT_ID","LICENSE_KEY",+ "UPDATE_STRATEGY",+ "UPDATE_SERVER_HOST",+ "SUPPORT_SERVER_HOST","PPROF_ADDR","PENTAGI_LISTEN_IP","PENTAGI_LISTEN_PORT",@@ -2113,6 +2119,9 @@ func (c *controller) GetServerSettingsConfig() *ServerSettingsConfig {defaults := map[string]string{"LICENSE_KEY": "",+ "UPDATE_STRATEGY": string(checker.DefaultUpdateStrategy),+ "UPDATE_SERVER_HOST": strings.TrimPrefix(strings.TrimSuffix(checker.DefaultUpdateServerEndpoint, "/"), "https://"),+ "SUPPORT_SERVER_HOST": strings.TrimPrefix(strings.TrimSuffix(checker.DefaultSupportServerEndpoint, "/"), "https://"),"PPROF_ADDR": "","PENTAGI_LISTEN_IP": "127.0.0.1","PENTAGI_LISTEN_PORT": "8443",@@ -2137,6 +2146,9 @@ func (c *controller) GetServerSettingsConfig() *ServerSettingsConfig {cfg := &ServerSettingsConfig{TenantID: vars["TENANT_ID"],LicenseKey: vars["LICENSE_KEY"],+ UpdateStrategy: vars["UPDATE_STRATEGY"],+ UpdateServerHost: vars["UPDATE_SERVER_HOST"],+ SupportServerHost: vars["SUPPORT_SERVER_HOST"],PprofAddr: vars["PPROF_ADDR"],ListenIP: vars["PENTAGI_LISTEN_IP"],ListenPort: vars["PENTAGI_LISTEN_PORT"],@@ -2181,6 +2193,9 @@ func (c *controller) UpdateServerSettingsConfig(config *ServerSettingsConfig) erupdates := map[string]string{"TENANT_ID": config.TenantID.Value,"LICENSE_KEY": config.LicenseKey.Value,+ "UPDATE_STRATEGY": config.UpdateStrategy.Value,+ "UPDATE_SERVER_HOST": config.UpdateServerHost.Value,+ "SUPPORT_SERVER_HOST": config.SupportServerHost.Value,"PPROF_ADDR": config.PprofAddr.Value,"PENTAGI_LISTEN_IP": config.ListenIP.Value,"PENTAGI_LISTEN_PORT": config.ListenPort.Value,@@ -2210,6 +2225,9 @@ func (c *controller) ResetServerSettingsConfig() *ServerSettingsConfig {vars := []string{"TENANT_ID","LICENSE_KEY",+ "UPDATE_STRATEGY",+ "UPDATE_SERVER_HOST",+ "SUPPORT_SERVER_HOST","PPROF_ADDR","PENTAGI_LISTEN_IP","PENTAGI_LISTEN_PORT",@@ -2529,6 +2547,7 @@ func (c *controller) getVariableDescription(varName string) string {// maskedVariables contains environment variable names that should be masked in displayvar maskedVariables = map[string]bool{// API keys and Secrets+ "LICENSE_KEY": true,"OPEN_AI_KEY": true,"ANTHROPIC_API_KEY": true,"GEMINI_API_KEY": true,diff --git a/backend/cmd/installer/wizard/locale/locale.go b/backend/cmd/installer/wizard/locale/locale.goindex 24873983..04050fd4 100644--- a/backend/cmd/installer/wizard/locale/locale.go+++ b/backend/cmd/installer/wizard/locale/locale.go@@ -305,6 +305,97 @@ Use arrow keys, page up/down, or home/end keys to navigate through the document.EULAProgressComplete = " • Complete")+// Update Overview Screen constants+const (+ UpdateOverviewFormName = "Update PentAGI"+ UpdateOverviewFormDescription = "What this update changes, and the release notes behind it"+ UpdateOverviewFormOverview = `Review what the update will change before applying it.++The overview shows:+• Each stack with the version it is on and the version it moves to+• Every component and whether it will actually change+• The changelog and release notes of every release you cross++Nothing is downloaded or changed on this screen. Continue to apply the update.++Use arrow keys, page up/down, or home/end keys to navigate through the document.`++ UpdateOverviewLoading = "Preparing update overview..."+ UpdateOverviewConfigurationOK = "✓ Update available"+ UpdateOverviewConfigurationNo = "✓ Everything up to date"++ // Document+ UpdateOverviewHeading = "# Update overview"+ UpdateOverviewNothingToDo = "Everything is up to date. There is nothing to apply."+ UpdateOverviewCheckFailed = "The last update check did not complete, so there is nothing to show here.\n\nApplying the update will pull whatever the compose files currently point at."+ UpdateOverviewStackHeading = "## %s"+ UpdateOverviewVersionMove = "**%s → %s**"+ UpdateOverviewVersionTarget = "**→ %s**"+ UpdateOverviewVersionUnknown = "*Version unknown — this installation could not be matched to a release.*"+ UpdateOverviewVersionMixed = "*Components come from different releases, so the version above is the oldest of them.*"+ UpdateOverviewComponentsTitle = "Components:"+ UpdateOverviewComponentChange = "- **%s** (%s/%s) — %s"+ UpdateOverviewWillChange = "will be updated"+ UpdateOverviewNoChange = "unchanged"+ UpdateOverviewCannotVerify = "cannot verify — nothing to compare against"+ UpdateOverviewReleaseHeading = "### %s"+ UpdateOverviewReleasePreview = "### %s (preview)"+ UpdateOverviewReleaseDate = "*Released %s*"+ UpdateOverviewReleasesCut = "*Older releases omitted — showing the most recent %d.*"+ UpdateOverviewUpToDateTitle = "## Already up to date"+ UpdateOverviewRenderFallback = "%s\n\n---\n\n*Note: Markdown rendering failed: %v*"+)++// Installer Update Screen constants.+//+// The wording is deliberate and was wrong before: the screen this replaced promised to+// "replace the current installer" and "exit for manual restart", and no version of this+// installer has ever done either. What happens is a download of one verified file.+const (+ InstallerUpdateFormOverview = `Download the installer build the update server offers for this machine.++The file is written next to your installation under its own versioned name, so the installer you are running now is left untouched. Its length, sha256 and signature are checked as it arrives; anything that fails is deleted rather than kept.++Replacing the installer you use is your decision to make afterwards — run the downloaded file when you are ready.`++ InstallerUpdateHelpTitle = "Update Installer"+ InstallerUpdateConfigurationOK = "✓ Newer build available"+ InstallerUpdateConfigurationNo = "✓ Installer up to date"++ // Left panel: what is on offer, before anything is fetched.+ InstallerUpdateAsking = "Asking the update server about the build on offer..."+ InstallerUpdateLoadFailed = "Could not get the package description: %v"+ InstallerUpdateCurrentVersion = "• Running version: %s"+ InstallerUpdateOfferedVersion = "• Offered version: %s"+ InstallerUpdatePlatform = "• Platform: %s/%s"+ InstallerUpdateSize = "• Download size: %s"+ InstallerUpdateTarget = "• Will be saved as: %s"+ InstallerUpdateManualNote = "The installer you are running is not replaced. You move the downloaded file into place yourself, with the command shown when the download finishes."+ InstallerUpdateSameVersion = "The offered build is the one already running. Downloading it changes nothing."+ InstallerUpdatePressEnter = "Press Enter to download"++ // The only thing worth a confirmation: a file under that exact name is already here,+ // and it may be one somebody put there on purpose. Downloading over nothing needs no+ // permission and is not asked about.+ InstallerUpdateAlreadyHere = "A file of this name is already here — a previous download, or one interrupted partway."+ InstallerUpdateOverwritePrompt = "Download again and replace %s?"+ InstallerUpdatePressYN = "Press y to download and replace it, n to keep the file already on disk"+ InstallerUpdateKeptExisting = "Kept the file already on disk. Nothing was downloaded."++ // Left panel: the download itself. There is no "completed" here — the operation+ // reports where the file landed and that it verified, and a second line saying the+ // same thing reads as a second thing having happened.+ InstallerUpdateInProgress = "Downloading the installer build...\n"+ InstallerUpdateFailed = "Failed to download the installer build"++ // What to do with the file afterwards. The installer never does this itself.+ InstallerUpdateFileIsAt = "The build is at %s"+ InstallerUpdateMoveTitle = "To start using it, run:"+ InstallerUpdateMoveUnknown = "Move it over the installer you launched when you are ready — this installer does not replace itself."+ InstallerUpdateWindowsNote = "Windows will not replace a program that is running: close this installer before running the command."+ InstallerUpdateMoveIndent = " %s"+)+// Main Menu Screen constantsconst (MainMenuTitle = "PentAGI Configuration"@@ -1240,6 +1331,20 @@ const (ServerSettingsLicenseKey = "License Key"ServerSettingsLicenseKeyDesc = "PentAGI License Key in format of XXXX-XXXX-XXXX-XXXX"+ ServerSettingsUpdateStrategy = "Update Strategy"+ // The three values are spelled out because the field is free text: the+ // validator refuses anything else on save, and a user who has to guess the+ // vocabulary will find that out the hard way.+ ServerSettingsUpdateStrategyDesc = "Which builds updates come from: preview (default, follows " ++ "the channels you already use), stable (published releases only) or nightly (newest build, " ++ "released or not)"++ ServerSettingsUpdateServerHost = "Update Server Host"+ ServerSettingsUpdateServerHostDesc = "PentAGI Cloud API endpoint as host[:port]; leave empty for update.pentagi.com"++ ServerSettingsSupportServerHost = "Support Server Host"+ ServerSettingsSupportServerHostDesc = "PentAGI Support API endpoint as host[:port]; leave empty for support.pentagi.com"+ToolsDockerInsideHost = "Worker Docker Daemon Host"ToolsDockerInsideHostDesc = "Daemon endpoint given to worker containers (e.g., tcp://dind:2376); empty keeps socket mounting"@@ -2119,7 +2224,7 @@ Each operation will provide real-time status updates and confirmation when requiMaintenanceUpdatePentagi = "Update PentAGI"MaintenanceUpdatePentagiDesc = "Update PentAGI to the latest version"MaintenanceUpdateInstaller = "Update Installer"- MaintenanceUpdateInstallerDesc = "Update this installer to the latest version"+ MaintenanceUpdateInstallerDesc = "Download and verify the latest installer build"MaintenanceFactoryReset = "Factory Reset"MaintenanceFactoryResetDesc = "Reset PentAGI to factory defaults"MaintenanceRemovePentagi = "Remove PentAGI"@@ -2283,13 +2388,6 @@ Note: This is a large download (6GB+).`Services will be briefly unavailable during update.`- ProcessorHelpUpdateInstaller = `This will:-• Download the latest installer binary-• Replace the current installer-• Exit for manual restart--You'll need to restart the installer after update.`-ProcessorHelpFactoryReset = `⚠️ WARNING: This operation will:• Remove all containers and networks• Delete all configuration files@@ -2568,15 +2666,14 @@ const (PlannedWillRestore = "will restore:"// effect notes per operation (concise and practical)- EffectsStart = "PentAGI web UI becomes available. Background services are brought online in the required order."- EffectsStop = "Web UI becomes unavailable. In-progress flows pause safely. When you start PentAGI again, flows resume automatically. A small portion of the current agent step may be lost."- EffectsRestart = "Services stop and start again with a clean state. Brief downtime is expected. Flows resume automatically afterwards."- EffectsUpdateAll = "Images are pulled and services are recreated where needed. External or disabled components are skipped. Temporary downtime is expected."- EffectsDownloadWorker = "Running worker containers are not touched. New flows will use the downloaded image. To switch an existing flow to the new image, finish the flow and start a new task or create a new assistant."- EffectsUpdateWorker = "Pulls latest worker image. Running worker containers keep using the old image; new containers will use the updated one."- EffectsUpdateInstaller = "The installer binary will be updated and the app will exit. Start the installer again to continue."- EffectsFactoryReset = "Removes containers, volumes and networks, restores default .env and embedded files. Produces a clean baseline. This action cannot be undone."- EffectsRemove = "Stops and removes containers but keeps volumes and images. Data is preserved. Web UI becomes unavailable until you start again."- EffectsPurge = "Complete cleanup: containers, images, volumes and configuration files are deleted. Irreversible."- EffectsInstall = "Required files are created and services are started. External components are detected and skipped."+ EffectsStart = "PentAGI web UI becomes available. Background services are brought online in the required order."+ EffectsStop = "Web UI becomes unavailable. In-progress flows pause safely. When you start PentAGI again, flows resume automatically. A small portion of the current agent step may be lost."+ EffectsRestart = "Services stop and start again with a clean state. Brief downtime is expected. Flows resume automatically afterwards."+ EffectsUpdateAll = "Images are pulled and services are recreated where needed. External or disabled components are skipped. Temporary downtime is expected."+ EffectsDownloadWorker = "Running worker containers are not touched. New flows will use the downloaded image. To switch an existing flow to the new image, finish the flow and start a new task or create a new assistant."+ EffectsUpdateWorker = "Pulls latest worker image. Running worker containers keep using the old image; new containers will use the updated one."+ EffectsFactoryReset = "Removes containers, volumes and networks, restores default .env and embedded files. Produces a clean baseline. This action cannot be undone."+ EffectsRemove = "Stops and removes containers but keeps volumes and images. Data is preserved. Web UI becomes unavailable until you start again."+ EffectsPurge = "Complete cleanup: containers, images, volumes and configuration files are deleted. Irreversible."+ EffectsInstall = "Required files are created and services are started. External components are detected and skipped.")diff --git a/backend/cmd/installer/wizard/models/installer_update.go b/backend/cmd/installer/wizard/models/installer_update.gonew file mode 100644index 00000000..d5c56413--- /dev/null+++ b/backend/cmd/installer/wizard/models/installer_update.go@@ -0,0 +1,482 @@+package models++import (+ "context"+ "fmt"+ "strings"++ "pentagi/cmd/installer/checker"+ "pentagi/cmd/installer/processor"+ "pentagi/cmd/installer/wizard/controller"+ "pentagi/cmd/installer/wizard/locale"+ "pentagi/cmd/installer/wizard/logger"+ "pentagi/cmd/installer/wizard/styles"+ "pentagi/cmd/installer/wizard/terminal"+ "pentagi/cmd/installer/wizard/window"++ tea "github.com/charmbracelet/bubbletea"+)++// InstallerUpdateModel is the screen behind the "Update Installer" entry.+//+// It replaced the generic operation form, which asked "are you sure?" about an operation+// it described wrongly: the help text promised to replace the running binary and exit,+// and no version of this installer has ever done that. What actually happens is a+// download of one verified file next to the installation.+//+// So this screen names the version, the size and the path FIRST, and then offers a single+// action. The confirmation prompt is gone with the old text: it existed to guard a+// destructive step that was never taken, and a y/n on top of a described download is+// ceremony, not safety.+type InstallerUpdateModel struct {+ *BaseScreen++ processor processor.ProcessorModel+ terminal terminal.Terminal++ // offered is the server's description of the build; offerErr is why there is none.+ // Both nil means the answer has not arrived yet.+ offered *processor.InstallerPackage+ offerErr error++ running bool+ confirming bool+}++// NewInstallerUpdateModel creates the installer self-update screen.+func NewInstallerUpdateModel(+ c controller.Controller, s styles.Styles, w window.Window, p processor.ProcessorModel,+) *InstallerUpdateModel {+ m := &InstallerUpdateModel{processor: p}+ m.BaseScreen = NewBaseScreen(c, s, w, m, nil)++ return m+}++// InstallerPackageLoadedMsg carries the server's description of the offered build.+type InstallerPackageLoadedMsg struct {+ Package *processor.InstallerPackage+ Err error+}++// Init implements tea.Model.+//+// The description is fetched on every entry rather than once: the offered version changes+// when an update check is re-run while the app is open. Asking again is nearly free — the+// processor keeps the last answer and only goes to the network when the version or the+// platform it describes no longer matches.+func (m *InstallerUpdateModel) Init() tea.Cmd {+ m.running = false+ m.confirming = false+ m.offered = nil+ m.offerErr = nil++ return tea.Batch(m.BaseScreen.Init(), m.loadPackage)+}++func (m *InstallerUpdateModel) loadPackage() tea.Msg {+ pkg, err := m.processor.InstallerPackage(context.Background())+ if err != nil {+ logger.Errorf("[InstallerUpdateModel] PACKAGE: %v", err)+ }++ return InstallerPackageLoadedMsg{Package: pkg, Err: err}+}++// BaseScreenHandler interface implementation++func (m *InstallerUpdateModel) BuildForm() tea.Cmd {+ // no form fields for this screen - it's an action screen+ m.SetFormFields([]FormField{})++ contentWidth, contentHeight := m.getViewportFormSize()++ if m.terminal == nil {+ if !m.isVerticalLayout() {+ contentWidth -= 2+ }+ m.terminal = terminal.NewTerminal(contentWidth-2, contentHeight-1,+ terminal.WithAutoScroll(),+ terminal.WithAutoPoll(),+ terminal.WithCurrentEnv(),+ )+ } else {+ m.terminal.Clear()+ }++ m.terminal.Append(locale.InstallerUpdateAsking)++ // prevent re-initialization on View() calls+ if !m.initialized {+ m.initialized = true+ return m.terminal.Init()+ }++ return nil+}++func (m *InstallerUpdateModel) GetFormSummary() string {+ // terminal viewport takes all available space+ return ""+}++func (m *InstallerUpdateModel) GetHelpContent() string {+ var sections []string++ sections = append(sections, m.GetStyles().Subtitle.Render(locale.InstallerUpdateHelpTitle))+ sections = append(sections, "")+ sections = append(sections, m.GetStyles().Paragraph.Render(locale.InstallerUpdateFormOverview))+ sections = append(sections, "")+ sections = append(sections, m.GetCurrentConfiguration())++ return strings.Join(sections, "\n")+}++func (m *InstallerUpdateModel) HandleSave() error { return nil }++func (m *InstallerUpdateModel) HandleReset() {}++func (m *InstallerUpdateModel) OnFieldChanged(fieldIndex int, oldValue, newValue string) {}++func (m *InstallerUpdateModel) GetFormFields() []FormField { return m.BaseScreen.fields }++func (m *InstallerUpdateModel) SetFormFields(fields []FormField) { m.BaseScreen.fields = fields }++// Update implements tea.Model+func (m *InstallerUpdateModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {+ handleTerminal := func(msg tea.Msg) (tea.Model, tea.Cmd) {+ if m.terminal == nil {+ return m, nil+ }++ updatedModel, cmd := m.terminal.Update(msg)+ if terminalModel := terminal.RestoreModel(updatedModel); terminalModel != nil {+ m.terminal = terminalModel+ }++ return m, cmd+ }++ switch msg := msg.(type) {+ case tea.WindowSizeMsg:+ contentWidth, contentHeight := m.getViewportFormSize()++ if m.terminal != nil {+ if !m.isVerticalLayout() {+ contentWidth -= 2+ }+ m.terminal.SetSize(contentWidth-2, contentHeight-1)+ }++ m.updateViewports()++ return m, nil++ case InstallerPackageLoadedMsg:+ m.offered, m.offerErr = msg.Package, msg.Err+ m.describeOffer()+ m.updateViewports()++ return m, nil++ case terminal.TerminalUpdateMsg:+ return handleTerminal(msg)++ case processor.ProcessorCompletionMsg:+ m.handleCompletion(msg)+ return m, m.processor.HandleMsg(msg)++ case processor.ProcessorStartedMsg:+ return m, m.processor.HandleMsg(msg)++ case processor.ProcessorOutputMsg:+ // ignore (handled by terminal)+ return m, m.processor.HandleMsg(msg)++ case processor.ProcessorWaitMsg:+ return m, m.processor.HandleMsg(msg)++ case tea.KeyMsg:+ if m.terminal != nil && m.terminal.IsRunning() {+ return handleTerminal(msg)+ }++ switch msg.String() {+ case "enter":+ if !m.canDownload() || m.confirming {+ return m, nil+ }+ // The one thing worth asking about: a file already occupies the name we+ // are about to write. Downloading over nothing is not a question.+ if m.offered.Downloaded {+ m.confirming = true+ m.askToOverwrite()+ m.updateViewports()+ return m, nil+ }++ return m, m.startDownload()++ case "y":+ if !m.confirming || m.running {+ return m, nil+ }+ m.confirming = false++ return m, m.startDownload()++ case "n":+ if !m.confirming || m.running {+ return m, nil+ }+ m.confirming = false+ // Declining is not a dead end: the file the user chose to keep is the very+ // build they came here for, so what they need next is the command that puts+ // it in place — the same one a fresh download would have printed.+ m.showLines(append([]string{locale.InstallerUpdateKeptExisting},+ installerMoveInstructions(m.offered)...))+ m.updateViewports()++ return m, nil+ }++ // pass other keys to terminal for scrolling etc.+ return handleTerminal(msg)++ default:+ return handleTerminal(msg)+ }+}++// startDownload runs the operation and reports it in the panel.+func (m *InstallerUpdateModel) startDownload() tea.Cmd {+ logger.Log("[InstallerUpdateModel] DOWNLOAD: %s", m.offered.Version)+ m.running = true+ m.showLines([]string{locale.InstallerUpdateInProgress})++ return m.processor.Update(context.Background(),+ processor.ProductStackInstaller, processor.WithTerminal(m.terminal))+}++// canDownload reports whether Enter has anything to do.+//+// The description has to be in hand: it carries the length, the sha256 and the signature+// the download is checked against, so without it there is nothing to verify against and+// the operation would only fail further in.+func (m *InstallerUpdateModel) canDownload() bool {+ return !m.running && m.offered != nil && m.offerErr == nil+}++// showLines replaces the panel contents.+func (m *InstallerUpdateModel) showLines(lines []string) {+ if m.terminal == nil {+ return+ }++ m.terminal.Clear()+ for _, line := range lines {+ m.terminal.Append(line)+ }+}++// describeOffer replaces the panel contents with what is on offer.+func (m *InstallerUpdateModel) describeOffer() {+ m.showLines(installerUpdateLines(checker.InstallerVersion, m.offered, m.offerErr))+}++func (m *InstallerUpdateModel) askToOverwrite() {+ m.showLines([]string{+ locale.InstallerUpdateAlreadyHere,+ "",+ fmt.Sprintf(locale.InstallerUpdateOverwritePrompt, m.offered.Path),+ "",+ locale.InstallerUpdatePressYN,+ })+}++func (m *InstallerUpdateModel) handleCompletion(msg processor.ProcessorCompletionMsg) {+ m.running = false+ if m.terminal == nil {+ return+ }++ if msg.Error != nil {+ m.terminal.Append(fmt.Sprintf("%s: %v\n", locale.InstallerUpdateFailed, msg.Error))+ m.updateViewports()++ return+ }++ // The operation already said where the file landed and that it verified, so nothing+ // here repeats that. What it could not say is what to do next, because only the screen+ // knows the file is now sitting there under a name nothing will run by itself.+ m.offered.Downloaded = true+ for _, line := range installerMoveInstructions(m.offered) {+ m.terminal.Append(line)+ }++ m.updateViewports()+}++// installerMoveInstructions is the last thing the screen says: where the build is, and the+// exact command that puts it in place.+//+// Printed rather than run. A process cannot reliably replace the binary it is executing —+// Linux refuses the write, Windows holds the file locked — and an installer that tries and+// fails halfway leaves the user with no installer at all. Handing over the command keeps+// the decision, and the timing, with the person who can make it.+func installerMoveInstructions(offered *processor.InstallerPackage) []string {+ if offered == nil {+ return nil+ }++ lines := []string{"", fmt.Sprintf(locale.InstallerUpdateFileIsAt, offered.Path)}++ if command := offered.MoveCommand(); command != "" {+ lines = append(lines, "", locale.InstallerUpdateMoveTitle, "",+ fmt.Sprintf(locale.InstallerUpdateMoveIndent, command))+ } else {+ // os.Executable told us nothing, so there is no path to put in a command. Saying+ // what has to happen beats printing a command with a guess in it.+ lines = append(lines, "", locale.InstallerUpdateMoveUnknown)+ }++ // Only Windows needs this: renaming over a running binary is ordinary on Linux and+ // macOS — the running process keeps the inode it already opened.+ if offered.OS == "windows" {+ lines = append(lines, "", locale.InstallerUpdateWindowsNote)+ }++ return lines+}++// installerUpdateLines is what the screen says before anything is downloaded.+//+// Pure so the wording can be tested: the wizard has no harness for a bubbletea screen, and+// the part worth testing is what the text CLAIMS — this screen exists because the text it+// replaced claimed the running binary would be swapped and the app would exit.+func installerUpdateLines(running string, offered *processor.InstallerPackage, err error) []string {+ switch {+ case err != nil:+ return []string{fmt.Sprintf(locale.InstallerUpdateLoadFailed, err)}+ case offered == nil:+ return []string{locale.InstallerUpdateAsking}+ }++ lines := []string{+ fmt.Sprintf(locale.InstallerUpdateCurrentVersion, running),+ fmt.Sprintf(locale.InstallerUpdateOfferedVersion, offered.Version),+ fmt.Sprintf(locale.InstallerUpdatePlatform, offered.OS, offered.Arch),+ fmt.Sprintf(locale.InstallerUpdateSize, processor.FormatSize(offered.Size)),+ fmt.Sprintf(locale.InstallerUpdateTarget, offered.Path),+ "",+ locale.InstallerUpdateManualNote,+ }++ // The entry only appears when the server says there is a newer build, but that answer+ // can be older than the screen — a re-check, or a strategy changed under it. Printing+ // the two versions without a word between them reads as an update either way.+ if offered.Version == running {+ lines = append(lines, "", locale.InstallerUpdateSameVersion)+ }++ // Said here as well as in the confirmation, so pressing Enter is not the moment the+ // user first learns something is about to be replaced.+ if offered.Downloaded {+ lines = append(lines, "", locale.InstallerUpdateAlreadyHere)+ }++ return append(lines, "", locale.InstallerUpdatePressEnter)+}++// Override View to use custom layout+func (m *InstallerUpdateModel) View() string {+ contentWidth, contentHeight := m.window.GetContentSize()+ if contentWidth <= 0 || contentHeight <= 0 {+ return locale.UILoading+ }++ if !m.initialized {+ m.handler.BuildForm()+ m.fields = m.GetFormFields()+ m.updateViewports()+ }++ leftPanel := m.GetStyles().Error.Render(locale.ProcessorOperationTerminalNotInitialized)+ if m.terminal != nil {+ leftPanel = m.terminal.View()+ }++ rightPanel := m.renderHelp()++ if m.isVerticalLayout() {+ return m.renderVerticalLayout(leftPanel, rightPanel, contentWidth, contentHeight)+ }++ return m.renderHorizontalLayout(leftPanel, rightPanel, contentWidth, contentHeight)+}++// BaseScreenModel interface implementation++func (m *InstallerUpdateModel) GetFormTitle() string {+ return locale.MaintenanceUpdateInstaller+}++func (m *InstallerUpdateModel) GetFormDescription() string {+ return locale.MaintenanceUpdateInstallerDesc+}++// GetFormName returns the name shown in the maintenance list. Deliberately the string the+// operation form used, so the entry reads the way it always did.+func (m *InstallerUpdateModel) GetFormName() string {+ return locale.MaintenanceUpdateInstaller+}++func (m *InstallerUpdateModel) GetFormOverview() string {+ var sections []string++ sections = append(sections, m.GetStyles().Subtitle.Render(locale.MaintenanceUpdateInstaller))+ sections = append(sections, "")+ sections = append(sections, m.GetStyles().Paragraph.Bold(true).Render(locale.MaintenanceUpdateInstallerDesc))+ sections = append(sections, "")+ sections = append(sections, m.GetStyles().Paragraph.Render(locale.InstallerUpdateFormOverview))++ return strings.Join(sections, "\n")+}++func (m *InstallerUpdateModel) GetCurrentConfiguration() string {+ if m.offerErr != nil {+ return m.GetStyles().Error.Render(fmt.Sprintf(locale.InstallerUpdateLoadFailed, m.offerErr))+ }++ check := m.GetController().GetChecker()+ if check != nil && check.CanUpdateInstaller() {+ return locale.InstallerUpdateConfigurationOK+ }++ return locale.InstallerUpdateConfigurationNo+}++// IsConfigured reports whether there is a newer build to fetch.+func (m *InstallerUpdateModel) IsConfigured() bool {+ check := m.GetController().GetChecker()+ return check != nil && check.CanUpdateInstaller()+}++func (m *InstallerUpdateModel) GetFormHotKeys() []string {+ if m.terminal != nil && m.terminal.IsRunning() {+ return nil+ }+ if m.confirming {+ return []string{"y|n"}+ }+ if !m.canDownload() {+ return nil+ }++ return []string{"enter"}+}++// Compile-time interface validation+var _ BaseScreenModel = (*InstallerUpdateModel)(nil)+var _ BaseScreenHandler = (*InstallerUpdateModel)(nil)diff --git a/backend/cmd/installer/wizard/models/installer_update_test.go b/backend/cmd/installer/wizard/models/installer_update_test.gonew file mode 100644index 00000000..2393fdcb--- /dev/null+++ b/backend/cmd/installer/wizard/models/installer_update_test.go@@ -0,0 +1,199 @@+package models++import (+ "errors"+ "strings"+ "testing"++ tea "github.com/charmbracelet/bubbletea"++ "pentagi/cmd/installer/processor"+ "pentagi/cmd/installer/wizard/locale"+)++// As with the update overview, what is tested here is the part that carries the meaning:+// the text the screen puts in front of the user before anything is downloaded. The+// viewport and the terminal panel are mechanics shared with the operation form.++func offerLines(t *testing.T, running string, offered *processor.InstallerPackage, err error) string {+ t.Helper()+ return strings.Join(installerUpdateLines(running, offered, err), "\n")+}++// TestTheScreenNamesTheBuildBeforeOfferingToFetchIt.+//+// This is the whole reason the screen exists. The entry used to lead into a generic+// "are you sure?" whose help text described an operation nobody had written: replacing the+// running binary and exiting. Consent to something undescribed is not consent, so the+// version pair, the size and the destination path all have to be on screen first.+func TestTheScreenNamesTheBuildBeforeOfferingToFetchIt(t *testing.T) {+ document := offerLines(t, "2.0.0", &processor.InstallerPackage{+ Version: "2.1.0",+ OS: "linux",+ Arch: "amd64",+ Size: 25 << 20,+ Path: "/opt/pentagi/installer_2.1.0",+ }, nil)++ contains(t, document, "2.0.0", "the version being run must be named")+ contains(t, document, "2.1.0", "the version on offer must be named")+ contains(t, document, "linux/amd64", "the package is per-platform and the screen says which")+ contains(t, document, "25.0 MiB", "the size comes from packages/info; a raw byte count is not an answer")+ contains(t, document, "/opt/pentagi/installer_2.1.0", "where the file lands is part of what is agreed to")+ contains(t, document, locale.InstallerUpdatePressEnter, "the one action must be spelled out")+}++// TestTheScreenSaysTheRunningInstallerSurvives.+//+// The claim, not the phrasing: applying the update is out of scope by design — a process+// cannot reliably replace the binary it is executing, and doing it badly leaves the user+// with no installer at all. The text this replaced promised exactly that, so the promise+// is pinned here.+func TestTheScreenSaysTheRunningInstallerSurvives(t *testing.T) {+ document := offerLines(t, "2.0.0", &processor.InstallerPackage{+ Version: "2.1.0", OS: "linux", Arch: "amd64", Size: 1024, Path: "installer_2.1.0",+ }, nil)++ contains(t, document, locale.InstallerUpdateManualNote,+ "the screen must say what happens to the installer being run")++ note := strings.ToLower(locale.InstallerUpdateManualNote)+ if !strings.Contains(note, "not replaced") {+ t.Errorf("the note must say the running installer is NOT replaced, got: %q",+ locale.InstallerUpdateManualNote)+ }++ absent(t, strings.ToLower(document), "will exit",+ "nothing here exits the app; the old help text said it did and it never happened")+}++// TestAnOfferOfTheVersionAlreadyRunningSaysSo.+//+// The entry only appears when the server reports a newer build, but that answer can be+// older than the screen — a re-check while the app is open, or a strategy changed under it.+// Two identical versions printed one above the other read as an update either way.+func TestAnOfferOfTheVersionAlreadyRunningSaysSo(t *testing.T) {+ same := offerLines(t, "2.1.0", &processor.InstallerPackage{+ Version: "2.1.0", OS: "linux", Arch: "amd64", Size: 1024, Path: "installer_2.1.0",+ }, nil)+ contains(t, same, locale.InstallerUpdateSameVersion,+ "the same version twice must be called out, not left to be spotted")++ moved := offerLines(t, "2.0.0", &processor.InstallerPackage{+ Version: "2.1.0", OS: "linux", Arch: "amd64", Size: 1024, Path: "installer_2.1.0",+ }, nil)+ absent(t, moved, locale.InstallerUpdateSameVersion,+ "a real update must not be told it is already installed")+}++// TestAFailedLookupShowsTheFailureAndNothingElse.+//+// Without the description there is no size, no sha256 and no signature — nothing to check+// a download against. Showing a half-filled offer would invite Enter on a screen where+// Enter is refused, so the failure replaces the offer rather than joining it.+func TestAFailedLookupShowsTheFailureAndNothingElse(t *testing.T) {+ document := offerLines(t, "2.0.0", nil, errors.New("update server is unreachable"))++ contains(t, document, "update server is unreachable", "the reason must reach the user")+ absent(t, document, locale.InstallerUpdatePressEnter,+ "offering the action while the description is missing invites a keypress that is refused")+}++// TestTheOfferIsAwaitedRatherThanGuessed. Both nil means the answer has not arrived; the+// panel must say it is waiting instead of rendering an offer of empty strings.+func TestTheOfferIsAwaitedRatherThanGuessed(t *testing.T) {+ document := offerLines(t, "2.0.0", nil, nil)++ contains(t, document, locale.InstallerUpdateAsking, "an unanswered screen says it is asking")+ absent(t, document, locale.InstallerUpdatePressEnter, "there is nothing to press Enter for yet")+}++// TestTheScreenHandsOverTheCommandThatInstallsTheBuild.+//+// The download is only half of a self-update, and the installer deliberately does not do+// the other half. A screen that stops at "downloaded, verified" leaves a file with a name+// nothing will ever run — so the exact command has to be on screen, with both paths in it.+func TestTheScreenHandsOverTheCommandThatInstallsTheBuild(t *testing.T) {+ document := strings.Join(installerMoveInstructions(&processor.InstallerPackage{+ Version: "2.1.0", OS: "linux", Arch: "amd64", Size: 1024,+ Path: "/opt/pentagi/installer_2.1.0",+ CurrentPath: "/usr/local/bin/pentagi-installer",+ }), "\n")++ contains(t, document, "/opt/pentagi/installer_2.1.0", "where the build is must be said")+ contains(t, document, `mv "/opt/pentagi/installer_2.1.0" "/usr/local/bin/pentagi-installer"`,+ "the command must name both paths and quote them — installations live under paths with spaces")+ absent(t, document, locale.InstallerUpdateWindowsNote,+ "renaming over a running binary is ordinary on linux; the warning belongs to Windows only")+}++// TestOnWindowsTheUserIsToldToCloseTheInstallerFirst. Windows refuses to replace a running+// program, so the command alone would fail with a permission error the user cannot read.+func TestOnWindowsTheUserIsToldToCloseTheInstallerFirst(t *testing.T) {+ document := strings.Join(installerMoveInstructions(&processor.InstallerPackage{+ Version: "2.1.0", OS: "windows", Arch: "amd64",+ Path: `C:\pentagi\installer_2.1.0.exe`,+ CurrentPath: `C:\Program Files\PentAGI\installer.exe`,+ }), "\n")++ contains(t, document, `move /Y "C:\pentagi\installer_2.1.0.exe" "C:\Program Files\PentAGI\installer.exe"`,+ "mv is not a Windows command, and the destination has a space in it")+ contains(t, document, locale.InstallerUpdateWindowsNote, "the command fails while the installer runs")+}++// TestWithoutTheRunningPathNoCommandIsInvented.+//+// os.Executable can fail. A command built around a guessed destination is worse than no+// command: run it and the build lands somewhere nobody looks, or over something else.+func TestWithoutTheRunningPathNoCommandIsInvented(t *testing.T) {+ document := strings.Join(installerMoveInstructions(&processor.InstallerPackage{+ Version: "2.1.0", OS: "linux", Path: "/opt/pentagi/installer_2.1.0", CurrentPath: "",+ }), "\n")++ contains(t, document, "/opt/pentagi/installer_2.1.0", "where the build is is still known")+ contains(t, document, locale.InstallerUpdateMoveUnknown, "what has to happen must still be said")+ absent(t, document, "mv \"", "no command may be printed with a guessed destination in it")+}++// TestAFileAlreadyOnDiskIsAnnouncedBeforeEnterIsPressed.+//+// The confirmation exists for exactly one case — something already occupies that name —+// and a confirmation is not the place to learn that for the first time.+func TestAFileAlreadyOnDiskIsAnnouncedBeforeEnterIsPressed(t *testing.T) {+ present := offerLines(t, "2.0.0", &processor.InstallerPackage{+ Version: "2.1.0", OS: "linux", Arch: "amd64", Size: 1024,+ Path: "installer_2.1.0", Downloaded: true,+ }, nil)+ contains(t, present, locale.InstallerUpdateAlreadyHere, "the file being there must be said up front")++ absentFile := offerLines(t, "2.0.0", &processor.InstallerPackage{+ Version: "2.1.0", OS: "linux", Arch: "amd64", Size: 1024,+ Path: "installer_2.1.0", Downloaded: false,+ }, nil)+ absent(t, absentFile, locale.InstallerUpdateAlreadyHere,+ "downloading over nothing is not a question and must not read like one")+}++// TestEveryScreenModelIsRestorable.+//+// RestoreModel is how the app turns whatever a screen's Update returned back into a+// BaseScreenModel. What a missing branch costs is worth stating precisely, because the+// obvious answer is wrong: for a screen whose Update has a pointer receiver and returns+// itself — which is every screen here — the mutation has already happened in place and+// app.forwardMsgToCurrentModel keeps the pointer it already holds, so nothing freezes.+//+// The branch is load-bearing for the case the type allows but nobody has written yet: a+// model that returns a DIFFERENT model. That one is dropped silently, with no error+// anywhere, and it is the reason the contract is "every screen is listed" rather than+// "list the ones that need it" — the day a screen starts returning something else is not+// the day anybody remembers this rule.+func TestEveryScreenModelIsRestorable(t *testing.T) {+ for name, model := range map[string]tea.Model{+ "InstallerUpdateModel": (*InstallerUpdateModel)(nil),+ "UpdateOverviewModel": (*UpdateOverviewModel)(nil),+ } {+ if RestoreModel(model) == nil {+ t.Errorf("%s is missing its branch in RestoreModel", name)+ }+ }+}diff --git a/backend/cmd/installer/wizard/models/maintenance.go b/backend/cmd/installer/wizard/models/maintenance.goindex 439bc689..c9340c7a 100644--- a/backend/cmd/installer/wizard/models/maintenance.go+++ b/backend/cmd/installer/wizard/models/maintenance.go@@ -75,8 +75,12 @@ func (h *MaintenanceHandler) LoadItems() []ListItem {showUpdatePentagi := checker.CanUpdateAll()if showUpdatePentagi {+ // The overview screen, not the operation form: the entry used to lead+ // straight into "are you sure?", which asked for consent to something+ // the interface had never described. The overview continues into the+ // same form, confirmation and all.items = append(items, ListItem{- ID: UpdatePentagiScreen,+ ID: UpdateOverviewScreen,Highlighted: true,})}@@ -84,7 +88,7 @@ func (h *MaintenanceHandler) LoadItems() []ListItem {// update Installer - show if installer updates are availableif checker.CanUpdateInstaller() {items = append(items, ListItem{- ID: UpdateInstallerScreen,+ ID: InstallerUpdateScreen,Highlighted: true,})}diff --git a/backend/cmd/installer/wizard/models/processor_operation_form.go b/backend/cmd/installer/wizard/models/processor_operation_form.goindex cfb693a1..c04153c2 100644--- a/backend/cmd/installer/wizard/models/processor_operation_form.go+++ b/backend/cmd/installer/wizard/models/processor_operation_form.go@@ -242,11 +242,6 @@ func (m *ProcessorOperationFormModel) getOperationInfo() *processorOperationInfoinfo.title = locale.MaintenanceUpdateWorkerImageinfo.description = locale.MaintenanceUpdateWorkerImageDescinfo.help = locale.ProcessorHelpUpdateWorkerImage- case processor.ProductStackInstaller:- info.title = locale.MaintenanceUpdateInstaller- info.description = locale.MaintenanceUpdateInstallerDesc- info.help = locale.ProcessorHelpUpdateInstaller- info.requiresConfirmation = truecase processor.ProductStackAll, processor.ProductStackCompose:info.title = locale.MaintenanceUpdatePentagiinfo.description = locale.MaintenanceUpdatePentagiDesc@@ -534,8 +529,6 @@ func (m *ProcessorOperationFormModel) isActionAvailable() bool {switch m.stack {case processor.ProductStackWorker:return checker.CanUpdateWorker()- case processor.ProductStackInstaller:- return checker.CanUpdateInstaller()case processor.ProductStackAll, processor.ProductStackCompose:return checker.CanUpdateAll()default:@@ -653,9 +646,6 @@ func (m *ProcessorOperationFormModel) renderEffectsText() string {if m.stack == processor.ProductStackWorker {return locale.EffectsUpdateWorker}- if m.stack == processor.ProductStackInstaller {- return locale.EffectsUpdateInstaller- }return locale.EffectsUpdateAllcase processor.ProcessorOperationDownload:return locale.EffectsDownloadWorkerdiff --git a/backend/cmd/installer/wizard/models/server_settings_form.go b/backend/cmd/installer/wizard/models/server_settings_form.goindex 2319f2d8..3bfca477 100644--- a/backend/cmd/installer/wizard/models/server_settings_form.go+++ b/backend/cmd/installer/wizard/models/server_settings_form.go@@ -16,6 +16,7 @@ import ("pentagi/pkg/config"tea "github.com/charmbracelet/bubbletea"+ "github.com/vxcontrol/cloud/models""github.com/vxcontrol/cloud/sdk")@@ -51,6 +52,27 @@ func (m *ServerSettingsFormModel) BuildForm() tea.Cmd {true,))+ fields = append(fields, m.createTextField("update_strategy",+ locale.ServerSettingsUpdateStrategy,+ locale.ServerSettingsUpdateStrategyDesc,+ config.UpdateStrategy,+ false,+ ))++ fields = append(fields, m.createTextField("update_server_host",+ locale.ServerSettingsUpdateServerHost,+ locale.ServerSettingsUpdateServerHostDesc,+ config.UpdateServerHost,+ false,+ ))++ fields = append(fields, m.createTextField("support_server_host",+ locale.ServerSettingsSupportServerHost,+ locale.ServerSettingsSupportServerHostDesc,+ config.SupportServerHost,+ false,+ ))+fields = append(fields, m.createTextField("pentagi_tenant_id",locale.ServerSettingsTenantID,locale.ServerSettingsTenantIDDesc,@@ -475,6 +497,9 @@ func (m *ServerSettingsFormModel) HandleSave() error {newCfg := &controller.ServerSettingsConfig{TenantID: cfg.TenantID,LicenseKey: cfg.LicenseKey,+ UpdateStrategy: cfg.UpdateStrategy,+ UpdateServerHost: cfg.UpdateServerHost,+ SupportServerHost: cfg.SupportServerHost,PprofAddr: cfg.PprofAddr,ListenIP: cfg.ListenIP,ListenPort: cfg.ListenPort,@@ -505,6 +530,23 @@ func (m *ServerSettingsFormModel) HandleSave() error {}}newCfg.LicenseKey.Value = value+ case "update_strategy":+ // Empty means "use the default", which is a legitimate answer and the+ // one every existing installation currently gives. Anything else has to+ // be a member of the vocabulary: the field is free text, so this is the+ // only thing standing between a typo and an installation that quietly+ // falls back to a channel the user did not pick.+ if value != "" {+ if err := models.UpdateStrategy(strings.ToLower(value)).Valid(); err != nil {+ return fmt.Errorf("invalid update strategy %q: use preview, stable or nightly", value)+ }+ value = strings.ToLower(value)+ }+ newCfg.UpdateStrategy.Value = value+ case "update_server_host":+ newCfg.UpdateServerHost.Value = value+ case "support_server_host":+ newCfg.SupportServerHost.Value = valuecase "pentagi_tenant_id":if err := (&config.Config{TenantID: value}).ValidateTenantID(); err != nil {return errdiff --git a/backend/cmd/installer/wizard/models/types.go b/backend/cmd/installer/wizard/models/types.goindex 568f091f..19e4a2e8 100644--- a/backend/cmd/installer/wizard/models/types.go+++ b/backend/cmd/installer/wizard/models/types.go@@ -57,6 +57,10 @@ func RestoreModel(model tea.Model) BaseScreenModel {return mcase *ApplyChangesFormModel:return m+ case *UpdateOverviewModel:+ return m+ case *InstallerUpdateModel:+ return mcase *MaintenanceModel:return mcase *ProcessorOperationFormModel:@@ -115,7 +119,11 @@ const (DockerFormScreen ScreenID = "docker_form"// Management screens- ApplyChangesScreen ScreenID = "apply_changes"+ ApplyChangesScreen ScreenID = "apply_changes"+ // UpdateOverviewScreen shows what the update changes before it is applied.+ // It stands between the menu entry and UpdatePentagiScreen below, which the+ // entry used to point at directly.+ UpdateOverviewScreen ScreenID = "update_overview"InstallPentagiScreen ScreenID = "processor_operation_form§all§install"StartPentagiScreen ScreenID = "processor_operation_form§all§start"StopPentagiScreen ScreenID = "processor_operation_form§all§stop"@@ -123,12 +131,15 @@ const (DownloadWorkerImageScreen ScreenID = "processor_operation_form§worker§download"UpdateWorkerImageScreen ScreenID = "processor_operation_form§worker§update"UpdatePentagiScreen ScreenID = "processor_operation_form§compose§update"- UpdateInstallerScreen ScreenID = "processor_operation_form§installer§update"- FactoryResetScreen ScreenID = "processor_operation_form§all§factory_reset"- RemovePentagiScreen ScreenID = "processor_operation_form§all§remove"- PurgePentagiScreen ScreenID = "processor_operation_form§all§purge"- ResetPasswordScreen ScreenID = "reset_password"- MaintenanceScreen ScreenID = "maintenance"+ // InstallerUpdateScreen is its own screen and not an operation form: what it does is+ // download one verified file, which the generic form described as replacing the+ // running binary and exiting.+ InstallerUpdateScreen ScreenID = "installer_update"+ FactoryResetScreen ScreenID = "processor_operation_form§all§factory_reset"+ RemovePentagiScreen ScreenID = "processor_operation_form§all§remove"+ PurgePentagiScreen ScreenID = "processor_operation_form§all§purge"+ ResetPasswordScreen ScreenID = "reset_password"+ MaintenanceScreen ScreenID = "maintenance")type LLMProviderID stringdiff --git a/backend/cmd/installer/wizard/models/update_overview.go b/backend/cmd/installer/wizard/models/update_overview.gonew file mode 100644index 00000000..c3fbe27a--- /dev/null+++ b/backend/cmd/installer/wizard/models/update_overview.go@@ -0,0 +1,488 @@+package models++import (+ "fmt"+ "strings"+ "time"++ "pentagi/cmd/installer/checker"+ "pentagi/cmd/installer/wizard/controller"+ "pentagi/cmd/installer/wizard/locale"+ "pentagi/cmd/installer/wizard/logger"+ "pentagi/cmd/installer/wizard/styles"+ "pentagi/cmd/installer/wizard/window"++ "github.com/charmbracelet/bubbles/viewport"+ tea "github.com/charmbracelet/bubbletea"+ "github.com/charmbracelet/lipgloss"+)++// UpdateOverviewModel shows what an update would change, before it is applied.+//+// It sits between the menu entry and the operation form: the entry used to lead+// straight into "are you sure?", which asked the user to agree to something the+// interface had never described. Everything shown here is already in the last+// check result — the screen makes no network call and cannot fail.+type UpdateOverviewModel struct {+ styles styles.Styles+ window window.Window+ controller controller.Controller++ viewport viewport.Model+ // source is the markdown, and content is that markdown rendered at+ // renderedAt columns. Both are kept because a resize has to re-render from+ // the source — re-wrapping already-rendered text would wrap the ANSI escapes+ // glamour put in it.+ source string+ content string+ renderedAt int+ ready bool+ scrolled bool+}++// UpdateOverviewRenderedMsg carries markdown already rendered at a given wrap+// width, produced off the update loop.+//+// This document can span every stack's full release history — every+// intermediate release, each with its own changelog and release notes — so it+// has no fixed size the way the EULA's single file does. Calling glamour's+// Render() straight from Update() would run that cost on the update loop+// itself: redraws and key handling both wait for it, so the screen would sit+// looking frozen for however long the render took, only to have the already+// finished content pop in on whatever the next keypress happened to be —+// which reads as "needs a keypress to show up" when what actually happened is+// the render finally returning.+type UpdateOverviewRenderedMsg struct {+ Width int+ Content string+}++// NewUpdateOverviewModel creates the update overview screen.+func NewUpdateOverviewModel(+ c controller.Controller, s styles.Styles, w window.Window,+) *UpdateOverviewModel {+ return &UpdateOverviewModel{+ styles: s,+ window: w,+ controller: c,+ }+}++// Init implements tea.Model+func (m *UpdateOverviewModel) Init() tea.Cmd {+ m.reset()+ return m.loadOverview+}++func (m *UpdateOverviewModel) reset() {+ m.source = ""+ m.content = ""+ m.renderedAt = 0+ m.ready = false+ m.scrolled = false+}++// loadOverview builds the document from the last check result. It is a tea.Cmd+// for symmetry with the other document screens, not because it does any I/O.+func (m *UpdateOverviewModel) loadOverview() tea.Msg {+ check := m.controller.GetChecker()+ if check == nil {+ logger.Errorf("[UpdateOverviewModel] LOAD: no check result")+ return UpdateOverviewLoadedMsg{Source: overviewDocument(nil, false)}+ }+ // UpdateFailure is nil exactly when there is an answer, so it — and not the+ // emptiness of StackUpdates — is what separates "nothing to update" from+ // "we never found out".+ return UpdateOverviewLoadedMsg{+ Source: overviewDocument(check.StackUpdates, check.UpdateFailure == nil),+ }+}++// UpdateOverviewLoadedMsg carries the built markdown.+type UpdateOverviewLoadedMsg struct {+ Source string+}++// Update implements tea.Model+func (m *UpdateOverviewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {+ switch msg := msg.(type) {+ case tea.WindowSizeMsg:+ return m, m.resize()++ case UpdateOverviewLoadedMsg:+ m.source = msg.Source+ return m, m.startRender()++ case UpdateOverviewRenderedMsg:+ m.applyRender(msg)+ return m, func() tea.Msg { return nil }++ case tea.KeyMsg:+ switch msg.String() {+ case "enter":+ // Continue. Deliberately NOT page-down as on the EULA screen: this+ // screen asks for no consent, so the one unambiguous key belongs to+ // the one action it offers. The operation form that follows keeps+ // its own confirmation.+ logger.Log("[UpdateOverviewModel] CONTINUE")+ return m, func() tea.Msg {+ // GoBack + Target, same as EULA on acceptance: this screen must+ // not stay in the stack, or ESC from the operation form would+ // bounce the user through it again instead of landing on+ // Maintenance.+ return NavigationMsg{GoBack: true, Target: UpdatePentagiScreen}+ }+ }++ if !m.ready {+ break+ }+ switch msg.String() {+ case "up":+ m.viewport.ScrollUp(1)+ case "down":+ m.viewport.ScrollDown(1)+ case "left":+ m.viewport.ScrollLeft(2)+ case "right":+ m.viewport.ScrollRight(2)+ case "pgup":+ m.viewport.PageUp()+ case "pgdown":+ m.viewport.PageDown()+ case "home":+ m.viewport.GotoTop()+ case "end":+ m.viewport.GotoBottom()+ }+ m.updateScrollStatus()+ }++ if m.ready {+ var cmd tea.Cmd+ m.viewport, cmd = m.viewport.Update(msg)+ m.updateScrollStatus()+ return m, cmd+ }++ return m, nil+}++// resize applies the new terminal size to the viewport immediately —+// scrolling the content already on screen must keep working while a render is+// in flight — and starts a fresh one only when the wrap width the document+// was last rendered for no longer matches.+//+// GetRendererForWidth clamps, so most resizes land on the same renderer and+// this is a no-op, which matters because a drag-resize sends a WindowSizeMsg+// per column and glamour is not cheap.+func (m *UpdateOverviewModel) resize() tea.Cmd {+ contentWidth, contentHeight := m.window.GetContentSize()+ if contentWidth <= 0 || contentHeight <= 0 {+ return nil+ }++ if m.ready {+ m.viewport.Width = contentWidth+ m.viewport.Height = contentHeight+ m.updateScrollStatus()+ }++ if m.source == "" || m.renderedAt == contentWidth {+ return nil+ }++ return m.startRender()+}++// startRender renders the current source off the update loop.+//+// This is the fix for the screen appearing to freeze: rendering this+// document — every stack's full release history, concatenated — synchronously+// on Update() would block redraws and key handling for as long as glamour+// takes, which is not bounded the way the EULA's fixed file is. Running it in+// the returned tea.Cmd keeps the update loop free to redraw the screen the+// user is already looking at (and to react to ESC, scrolling, and so on)+// while the render is in flight; the result comes back as its own message+// through the ordinary Update/View cycle, no keypress required.+func (m *UpdateOverviewModel) startRender() tea.Cmd {+ contentWidth, contentHeight := m.window.GetContentSize()+ if contentWidth <= 0 || contentHeight <= 0 || m.source == "" {+ return nil+ }++ source := m.source+ sty := m.styles++ return func() tea.Msg {+ rendered, err := sty.GetRendererForWidth(contentWidth).Render(source)+ if err != nil {+ logger.Errorf("[UpdateOverviewModel] RENDER: %v", err)+ rendered = fmt.Sprintf(locale.UpdateOverviewRenderFallback, source, err)+ }+ return UpdateOverviewRenderedMsg{Width: contentWidth, Content: rendered}+ }+}++// applyRender installs a finished render, dropping it if the window has moved+// on to a different width since it was requested.+//+// A fast resize can have more than one render in flight at once; without this+// check, whichever one happened to finish last would win regardless of+// whether it matched the CURRENT width, and the content would flicker between+// wrap widths or settle on the wrong one.+func (m *UpdateOverviewModel) applyRender(msg UpdateOverviewRenderedMsg) {+ contentWidth, contentHeight := m.window.GetContentSize()+ if contentWidth <= 0 || contentHeight <= 0 || msg.Width != contentWidth {+ return+ }++ m.content = msg.Content+ m.renderedAt = msg.Width++ if !m.ready {+ m.viewport = viewport.New(contentWidth, contentHeight)+ m.viewport.Style = lipgloss.NewStyle()+ m.ready = true+ } else {+ m.viewport.Width = contentWidth+ m.viewport.Height = contentHeight+ }++ m.viewport.SetContent(m.content)+ m.updateScrollStatus()+}++func (m *UpdateOverviewModel) updateScrollStatus() {+ if m.ready {+ m.scrolled = m.viewport.ScrollPercent() > 0+ }+}++// View implements tea.Model+func (m *UpdateOverviewModel) View() string {+ contentWidth, contentHeight := m.window.GetContentSize()+ if contentWidth <= 0 || contentHeight <= 0 {+ return locale.UpdateOverviewLoading+ }++ if !m.ready || m.content == "" {+ loading := m.styles.Info.Render(locale.UpdateOverviewLoading)+ return lipgloss.Place(contentWidth, contentHeight, lipgloss.Center, lipgloss.Center, loading)+ }++ return lipgloss.Place(contentWidth, contentHeight, lipgloss.Center, lipgloss.Top, m.viewport.View())+}++// GetScrollInfo returns scroll information for footer display.+func (m *UpdateOverviewModel) GetScrollInfo() (scrolled bool, atEnd bool, percent int) {+ if !m.ready {+ return false, false, 0+ }+ return m.scrolled, m.viewport.AtBottom(), int(m.viewport.ScrollPercent() * 100)+}++// overviewDocument turns the last answer into the markdown the screen renders.+//+// Split out from the model so it can be tested without a terminal: the wizard+// has no harness for a bubbletea screen, and the part worth testing is what the+// document SAYS, not how it scrolls.+func overviewDocument(updates []checker.StackUpdate, checkSucceeded bool) string {+ var b strings.Builder+ b.WriteString(locale.UpdateOverviewHeading)+ b.WriteString("\n\n")++ if !checkSucceeded {+ // An update check that failed is not the same as an installation with+ // nothing to do, and saying "up to date" here would be a claim the+ // installer cannot support.+ b.WriteString(locale.UpdateOverviewCheckFailed)+ b.WriteString("\n")+ return b.String()+ }++ var changing, current []checker.StackUpdate+ for _, stack := range updates {+ if stack.HasUpdate {+ changing = append(changing, stack)+ } else {+ current = append(current, stack)+ }+ }++ if len(changing) == 0 {+ b.WriteString(locale.UpdateOverviewNothingToDo)+ b.WriteString("\n")+ return b.String()+ }++ for _, stack := range changing {+ writeStackSection(&b, stack)+ }++ if len(current) > 0 {+ b.WriteString(locale.UpdateOverviewUpToDateTitle)+ b.WriteString("\n\n")+ for _, stack := range current {+ fmt.Fprintf(&b, "- **%s**", stack.Stack)+ if stack.CurrentVersion != "" {+ fmt.Fprintf(&b, " %s", stack.CurrentVersion)+ }+ b.WriteString("\n")+ }+ b.WriteString("\n")+ }++ return b.String()+}++func writeStackSection(b *strings.Builder, stack checker.StackUpdate) {+ fmt.Fprintf(b, locale.UpdateOverviewStackHeading, stack.Stack)+ b.WriteString("\n\n")++ switch {+ case stack.CurrentVersion != "" && stack.LatestVersion != "":+ fmt.Fprintf(b, locale.UpdateOverviewVersionMove, stack.CurrentVersion, stack.LatestVersion)+ b.WriteString("\n\n")+ case stack.LatestVersion != "":+ // No current version: С1/С6, an installation the server could not place.+ // Naming only the target is the honest form — an arrow from nowhere+ // would imply we know where it starts.+ fmt.Fprintf(b, locale.UpdateOverviewVersionTarget, stack.LatestVersion)+ b.WriteString("\n\n")+ b.WriteString(locale.UpdateOverviewVersionUnknown)+ b.WriteString("\n\n")+ }++ if stack.CurrentVersionMixed {+ b.WriteString(locale.UpdateOverviewVersionMixed)+ b.WriteString("\n\n")+ }++ if len(stack.Components) > 0 {+ b.WriteString(locale.UpdateOverviewComponentsTitle)+ b.WriteString("\n\n")+ for _, component := range stack.Components {+ fmt.Fprintf(b, locale.UpdateOverviewComponentChange,+ component.Component, component.OS, component.Arch,+ componentChangeText(component))+ b.WriteString("\n")+ }+ b.WriteString("\n")+ }++ writeReleaseNotes(b, stack)+}++// componentChangeText says what will happen to one component.+//+// "cannot verify" is its own answer and not a synonym for "unchanged": the+// server omits a digest it does not know, and reporting that as "nothing to do"+// is how an installation is told it is current on no evidence at all.+func componentChangeText(component checker.ComponentUpdate) string {+ switch {+ case !component.Verifiable:+ return locale.UpdateOverviewCannotVerify+ case component.Outdated:+ return locale.UpdateOverviewWillChange+ default:+ return locale.UpdateOverviewNoChange+ }+}++// writeReleaseNotes prints every release the installation crosses, oldest first.+//+// Falls back to the single changelog for an answer that carries no release list+// — a server older than the field, which is every server until the deployment+// that introduced it.+func writeReleaseNotes(b *strings.Builder, stack checker.StackUpdate) {+ if len(stack.Releases) == 0 {+ writeSection(b, stack.Changelog)+ writeSection(b, stack.ReleaseNotes)+ return+ }++ if stack.ReleasesTruncated {+ fmt.Fprintf(b, locale.UpdateOverviewReleasesCut, len(stack.Releases))+ b.WriteString("\n\n")+ }++ for _, release := range stack.Releases {+ heading := locale.UpdateOverviewReleaseHeading+ if !release.IsStable {+ heading = locale.UpdateOverviewReleasePreview+ }+ fmt.Fprintf(b, heading, release.Version)+ b.WriteString("\n\n")++ if released, err := time.Parse(time.RFC3339, release.ReleasedAt); err == nil {+ fmt.Fprintf(b, locale.UpdateOverviewReleaseDate, released.Format("2006-01-02"))+ b.WriteString("\n\n")+ }++ writeSection(b, release.Changelog)+ writeSection(b, release.ReleaseNotes)+ }+}++func writeSection(b *strings.Builder, text string) {+ text = strings.TrimSpace(text)+ if text == "" {+ return+ }+ b.WriteString(text)+ b.WriteString("\n\n")+}++// BaseScreenModel interface implementation++// GetFormTitle returns empty title — glamour renders its own heading from the top.+func (m *UpdateOverviewModel) GetFormTitle() string {+ return ""+}++// GetFormDescription returns the description for the form (right panel)+func (m *UpdateOverviewModel) GetFormDescription() string {+ return locale.UpdateOverviewFormDescription+}++// GetFormName returns the name for the form (right panel).+//+// Deliberately the same string the operation form uses: this screen replaced+// that one as the destination of the menu entry, and the entry must keep reading+// the way it always did.+func (m *UpdateOverviewModel) GetFormName() string {+ return locale.UpdateOverviewFormName+}++// GetFormOverview returns form overview for list screens (right panel)+func (m *UpdateOverviewModel) GetFormOverview() string {+ return locale.UpdateOverviewFormOverview+}++// GetCurrentConfiguration returns text with current configuration for list screens+func (m *UpdateOverviewModel) GetCurrentConfiguration() string {+ check := m.controller.GetChecker()+ if check != nil && check.CanUpdateAll() {+ return locale.UpdateOverviewConfigurationOK+ }+ return locale.UpdateOverviewConfigurationNo+}++// IsConfigured reports whether there is an update to apply.+func (m *UpdateOverviewModel) IsConfigured() bool {+ check := m.controller.GetChecker()+ return check != nil && check.CanUpdateAll()+}++// GetFormHotKeys returns the hotkeys for the form (layout footer)+func (m *UpdateOverviewModel) GetFormHotKeys() []string {+ hotkeys := []string{"enter"}+ if m.ready && m.content != "" {+ hotkeys = append(hotkeys, "up|down", "pgup|pgdown", "home|end")+ }+ return hotkeys+}++// Compile-time interface validation+var _ BaseScreenModel = (*UpdateOverviewModel)(nil)diff --git a/backend/cmd/installer/wizard/models/update_overview_test.go b/backend/cmd/installer/wizard/models/update_overview_test.gonew file mode 100644index 00000000..20509069--- /dev/null+++ b/backend/cmd/installer/wizard/models/update_overview_test.go@@ -0,0 +1,196 @@+package models++import (+ "strings"+ "testing"++ "pentagi/cmd/installer/checker"+)++// The wizard has no harness for driving a bubbletea screen, so what is tested+// here is the part that carries the meaning: the document the screen renders.+// Everything above it — the viewport, the scrolling, the re-render on resize —+// is mechanics shared with the EULA screen, which has been in the field for+// releases.++func contains(t *testing.T, document, want, why string) {+ t.Helper()+ if !strings.Contains(document, want) {+ t.Errorf("%s\nexpected to find %q in:\n%s", why, want, document)+ }+}++func absent(t *testing.T, document, unwanted, why string) {+ t.Helper()+ if strings.Contains(document, unwanted) {+ t.Errorf("%s\nexpected NOT to find %q in:\n%s", why, unwanted, document)+ }+}++func TestTheOverviewNamesEveryReleaseTheInstallationCrosses(t *testing.T) {+ document := overviewDocument([]checker.StackUpdate{+ {+ Stack: "pentagi",+ HasUpdate: true,+ CurrentVersion: "2.0.0",+ LatestVersion: "2.2.0",+ Changelog: "target changelog",+ Releases: []checker.ReleaseSummary{+ {Version: "2.1.0", IsStable: true, ReleasedAt: "2026-01-02T03:04:05Z", Changelog: "first changelog"},+ {Version: "2.2.0", IsStable: true, Changelog: "second changelog", ReleaseNotes: "second notes"},+ },+ },+ }, true)++ contains(t, document, "2.0.0 → 2.2.0", "the move must be stated in both directions")+ contains(t, document, "2.1.0", "an intermediate release must be listed")+ contains(t, document, "first changelog", "each release carries its OWN text — that is the point of the list")+ contains(t, document, "second notes", "the target's release notes must be there")+ contains(t, document, "2026-01-02", "a published release shows when it was published")+}++// TestAnUnpublishedReleaseShowsNoDate. ReleasedAt is empty for a release nobody+// pressed Publish on, and printing "Released " with nothing after it looks like+// a bug in the installer rather than an absent value on the server.+func TestAnUnpublishedReleaseShowsNoDate(t *testing.T) {+ document := overviewDocument([]checker.StackUpdate{+ {+ Stack: "pentagi", HasUpdate: true, CurrentVersion: "2.0.0", LatestVersion: "2.1.0",+ Releases: []checker.ReleaseSummary{{Version: "2.1.0", IsStable: true, Changelog: "notes"}},+ },+ }, true)++ absent(t, document, "Released", "a release with no publication date must print no date line")+}++// TestAPreviewReleaseIsMarked. A preview release in the list is legitimate — a+// preview client crosses them — but reading it as a stable release is how an+// operator concludes the product shipped something it has not.+func TestAPreviewReleaseIsMarked(t *testing.T) {+ document := overviewDocument([]checker.StackUpdate{+ {+ Stack: "pentagi", HasUpdate: true, CurrentVersion: "2.0.0", LatestVersion: "2.1.0",+ Releases: []checker.ReleaseSummary{+ {Version: "2.1.0", IsStable: false, Changelog: "unreleased work"},+ },+ },+ }, true)++ contains(t, document, "preview", "a preview release must not read as a stable one")+}++// TestAServerWithoutTheReleaseListStillShowsItsChangelog. Every deployment older+// than the `releases` field sends only the target's text, and the overview must+// not go blank against one — that is most of the estate on the day this ships.+func TestAServerWithoutTheReleaseListStillShowsItsChangelog(t *testing.T) {+ document := overviewDocument([]checker.StackUpdate{+ {+ Stack: "pentagi", HasUpdate: true, CurrentVersion: "2.0.0", LatestVersion: "2.1.0",+ Changelog: "only changelog", ReleaseNotes: "only notes",+ },+ }, true)++ contains(t, document, "only changelog", "the deprecated single changelog is the fallback")+ contains(t, document, "only notes", "so are the deprecated release notes")+}++// TestTheThreeComponentVerdictsAreDistinct.+//+// "cannot verify" is not a synonym for "unchanged". The server omits a digest it+// does not know, and an installation told "unchanged" on that basis has been+// told it is current on no evidence at all — which is the failure this whole+// redesign exists to remove.+func TestTheThreeComponentVerdictsAreDistinct(t *testing.T) {+ document := overviewDocument([]checker.StackUpdate{+ {+ Stack: "pentagi", HasUpdate: true, CurrentVersion: "2.0.0", LatestVersion: "2.1.0",+ Components: []checker.ComponentUpdate{+ {Component: "pentagi", OS: "linux", Arch: "amd64", Outdated: true, Verifiable: true},+ {Component: "pgvector", OS: "linux", Arch: "amd64", Outdated: false, Verifiable: true},+ {Component: "scraper", OS: "linux", Arch: "amd64", Outdated: true, Verifiable: false},+ },+ },+ }, true)++ contains(t, document, "**pentagi** (linux/amd64) — will be updated", "an outdated component changes")+ contains(t, document, "**pgvector** (linux/amd64) — unchanged", "a current component does not")+ contains(t, document, "**scraper** (linux/amd64) — cannot verify",+ "an unverifiable component must not be reported as a change, nor as unchanged")+}++// TestAnInstallationThatCannotBePlacedIsNotGivenAnOrigin. С1 and С6: nothing was+// pulled yet, or the installation was touched outside the installer. An arrow+// from an empty version would claim we know where it starts.+func TestAnInstallationThatCannotBePlacedIsNotGivenAnOrigin(t *testing.T) {+ document := overviewDocument([]checker.StackUpdate{+ {Stack: "pentagi", HasUpdate: true, LatestVersion: "2.1.0"},+ }, true)++ contains(t, document, "→ 2.1.0", "the target is still named")+ absent(t, document, " → 2.1.0**", "there is no version to move FROM")+ contains(t, document, "Version unknown", "the reader must be told why there is no origin")+}++// TestAMixedStackSaysSo. current_version names the OLDEST component of a mixed+// stack, and printing it bare turns "the oldest thing you have is from 2.0.0"+// into "you are on 2.0.0".+func TestAMixedStackSaysSo(t *testing.T) {+ mixed := overviewDocument([]checker.StackUpdate{+ {+ Stack: "pentagi", HasUpdate: true, CurrentVersion: "2.0.0",+ CurrentVersionMixed: true, LatestVersion: "2.1.0",+ },+ }, true)+ contains(t, mixed, "different releases", "a mixed stack must be marked as one")++ uniform := overviewDocument([]checker.StackUpdate{+ {Stack: "pentagi", HasUpdate: true, CurrentVersion: "2.0.0", LatestVersion: "2.1.0"},+ }, true)+ absent(t, uniform, "different releases", "a uniform stack must not be marked — or the mark says nothing")+}++func TestATruncatedReleaseListSaysItWasCut(t *testing.T) {+ document := overviewDocument([]checker.StackUpdate{+ {+ Stack: "pentagi", HasUpdate: true, CurrentVersion: "1.0.0", LatestVersion: "2.1.0",+ ReleasesTruncated: true,+ Releases: []checker.ReleaseSummary{{Version: "2.1.0", IsStable: true, Changelog: "notes"}},+ },+ }, true)++ contains(t, document, "Older releases omitted", "a cut list that does not say so is a lie by omission")+}++// TestAFailedCheckIsNotReportedAsUpToDate is the one that matters most.+//+// The screen is reachable from a menu entry that was drawn from an EARLIER check+// result, and a later check can fail. Falling back to "everything is up to date"+// there tells the user the opposite of the truth about their installation.+func TestAFailedCheckIsNotReportedAsUpToDate(t *testing.T) {+ document := overviewDocument(nil, false)++ absent(t, document, "up to date", "a check that never completed proves nothing about the installation")+ contains(t, document, "did not complete", "the reader must be told the check failed")+}++func TestAStackWithNothingToDoIsListedSeparately(t *testing.T) {+ document := overviewDocument([]checker.StackUpdate{+ {Stack: "pentagi", HasUpdate: true, CurrentVersion: "2.0.0", LatestVersion: "2.1.0"},+ {Stack: "worker", HasUpdate: false, CurrentVersion: "2.1.0"},+ }, true)++ contains(t, document, "Already up to date", "a stack with nothing to do belongs in its own section")+ contains(t, document, "**worker**", "and it must still be named — silence reads as 'not installed'")++ // A stack with no update must not get a section of its own with a version+ // move, which is what a loop over the whole list would produce.+ absent(t, document, "## worker", "an unchanged stack is not a section")+}++func TestNoUpdatesAtAllSaysSo(t *testing.T) {+ document := overviewDocument([]checker.StackUpdate{+ {Stack: "pentagi", HasUpdate: false, CurrentVersion: "2.1.0"},+ }, true)++ contains(t, document, "nothing to apply", "an empty plan must be stated, not left blank")+}diff --git a/backend/cmd/installer/wizard/registry/registry.go b/backend/cmd/installer/wizard/registry/registry.goindex bcfee91a..f69a5690 100644--- a/backend/cmd/installer/wizard/registry/registry.go+++ b/backend/cmd/installer/wizard/registry/registry.go@@ -90,6 +90,16 @@ func (r *registry) initScreens() {// Changes Formr.screens[models.ApplyChangesScreen] = models.NewApplyChangesFormModel(r.controller, r.styles, r.window, r.processor)+ // Update Overview — the screen the maintenance entry leads to, which then+ // continues into the update operation form below.+ r.screens[models.UpdateOverviewScreen] = models.NewUpdateOverviewModel(r.controller, r.styles, r.window)++ // Installer self-update — its own screen rather than an operation form: it describes+ // the build on offer and downloads one verified file, and no part of that is the+ // generic "are you sure?" the form provides.+ r.screens[models.InstallerUpdateScreen] = models.NewInstallerUpdateModel(+ r.controller, r.styles, r.window, r.processor)+// Maintenancer.screens[models.MaintenanceScreen] = models.NewMaintenanceModel(r.controller, r.styles, r.window, r)r.screens[models.ResetPasswordScreen] = models.NewResetPasswordModel(r.controller, r.styles, r.window, r.processor)@@ -103,7 +113,6 @@ func (r *registry) initScreens() {models.DownloadWorkerImageScreen,models.UpdateWorkerImageScreen,models.UpdatePentagiScreen,- models.UpdateInstallerScreen,models.FactoryResetScreen,models.RemovePentagiScreen,models.PurgePentagiScreen,diff --git a/backend/cmd/installer/wizard/styles/styles.go b/backend/cmd/installer/wizard/styles/styles.goindex d78f7f1d..3f08f60f 100644--- a/backend/cmd/installer/wizard/styles/styles.go+++ b/backend/cmd/installer/wizard/styles/styles.go@@ -1,14 +1,19 @@package stylesimport (+ "os""slices""strings"+ "sync""pentagi/cmd/installer/wizard/locale""pentagi/cmd/installer/wizard/logger""github.com/charmbracelet/glamour"+ glamourstyles "github.com/charmbracelet/glamour/styles""github.com/charmbracelet/lipgloss"+ "github.com/muesli/termenv"+ "golang.org/x/term")// Colors defines the color palette for the installer@@ -76,32 +81,130 @@ type Styles struct {// Markdown rendererrenderer *glamour.TermRenderer+ // renderers memoises the width-aware renderers. A POINTER, deliberately:+ // Styles is copied by value into every screen, and building a glamour+ // renderer is not free, so the cache has to be shared rather than cloned.+ renderers *rendererCache+ // markdownStyle is the dark/light glamour style every renderer in this+ // process uses, resolved exactly once in New(). See resolveMarkdownStyle+ // for why this is not glamour.WithAutoStyle() called fresh per renderer.+ markdownStyle string+}++// Word-wrap bounds for rendered markdown.+//+// The floor keeps a narrow terminal from breaking every changelog line into two+// or three; the ceiling exists because a line running the full width of a+// maximised terminal is measurably harder to read than one that stops — the same+// reason every prose medium has a column width.+const (+ MinRenderWidth = 40+ MaxRenderWidth = 120+ DefaultRenderWidth = 80+)++// rendererCache hands out one renderer per wrap width.+//+// Locked because renderers are built inside tea.Cmd goroutines: a screen loads+// its markdown off the update loop, which is exactly where two screens can ask+// for the same width at once.+type rendererCache struct {+ mu sync.Mutex+ by map[int]*glamour.TermRenderer}// New creates a new styles instance with default valuesfunc New() Styles {+ markdownStyle := resolveMarkdownStyle()+// Create glamour renderer for markdownrenderer, err := glamour.NewTermRenderer(- glamour.WithAutoStyle(),- glamour.WithWordWrap(80),+ glamour.WithStandardStyle(markdownStyle),+ glamour.WithWordWrap(DefaultRenderWidth),)if err != nil {logger.Errorf("[Styles] NEW: error creating renderer: %v", err)}s := Styles{- renderer: renderer,+ renderer: renderer,+ renderers: &rendererCache{by: map[int]*glamour.TermRenderer{}},+ markdownStyle: markdownStyle,}s.initializeStyles()return s}-// GetRenderer returns the markdown renderer+// resolveMarkdownStyle decides dark vs light exactly once, here, at New() —+// which the wizard calls before tea.NewProgram(...).Run() takes over the+// terminal.+//+// glamour.WithAutoStyle() makes this same decision by calling+// termenv.HasDarkBackground(), which queries the terminal directly (an OSC+// background-color request over stdout, read back from stdin) and — on+// termenv's default, uncached output — does that full round trip again on+// EVERY call, not just the first. GetRendererForWidth calls WithAutoStyle()+// fresh for every wrap width it has not seen yet, and it does so WHILE the+// program is running: bubbletea's own input reader already owns stdin at+// that point, so the query and the reader are pulling on the same bytes.+// Whichever one loses that race sits waiting — for termenv's read timeout, or+// for the next keypress to hand it something to (mis)read — and that is what+// the screen looked frozen for. Resolving the choice once, here, before+// bubbletea has claimed the terminal, and reusing the answer for every+// renderer afterwards removes the query from the running program entirely+// instead of hoping it loses the race quickly.+func resolveMarkdownStyle() string {+ if !term.IsTerminal(int(os.Stdout.Fd())) {+ return glamourstyles.NoTTYStyle+ }+ if termenv.HasDarkBackground() {+ return glamourstyles.DarkStyle+ }+ return glamourstyles.LightStyle+}++// GetRenderer returns the markdown renderer at the default width.+//+// Prefer GetRendererForWidth on any screen that knows how much room it has: this+// one wraps at 80 columns whatever the terminal is doing.func (s *Styles) GetRenderer() *glamour.TermRenderer {return s.renderer}+// GetRendererForWidth returns a markdown renderer wrapping at the given width,+// clamped to a readable band and memoised.+//+// It never returns nil: a width that cannot be built falls back to the default+// renderer, and a caller that got nil would have to check for it on every render+// path — which is how a changelog ends up not being shown at all.+func (s *Styles) GetRendererForWidth(width int) *glamour.TermRenderer {+ width = min(max(width, MinRenderWidth), MaxRenderWidth)++ if s.renderers == nil {+ return s.renderer+ }++ s.renderers.mu.Lock()+ defer s.renderers.mu.Unlock()++ if cached, ok := s.renderers.by[width]; ok {+ return cached+ }++ renderer, err := glamour.NewTermRenderer(+ glamour.WithStandardStyle(s.markdownStyle),+ glamour.WithWordWrap(width),+ )+ if err != nil {+ logger.Errorf("[Styles] RENDERER: width %d: %v", width, err)+ return s.renderer+ }++ s.renderers.by[width] = renderer+ return renderer+}+// initializeStyles sets up all the style definitionsfunc (s *Styles) initializeStyles() {// Layout stylesdiff --git a/backend/go.mod b/backend/go.modindex eed32191..d326fa1b 100644--- a/backend/go.mod+++ b/backend/go.mod@@ -40,6 +40,7 @@ require (github.com/mattn/go-runewidth v0.0.16github.com/moby/moby/api v1.55.0github.com/moby/moby/client v0.5.1+ github.com/muesli/termenv v0.16.0github.com/ollama/ollama v0.32.5github.com/pgvector/pgvector-go v0.1.1github.com/pressly/goose/v3 v3.19.2@@ -52,7 +53,7 @@ require (github.com/swaggo/gin-swagger v1.3.0github.com/swaggo/swag v1.8.7github.com/vektah/gqlparser/v2 v2.5.19- github.com/vxcontrol/cloud v0.9.0+ github.com/vxcontrol/cloud v1.0.0github.com/vxcontrol/graphiti-go-client v0.9.0github.com/vxcontrol/langchaingo v0.1.14-update.7github.com/wasilibs/go-re2 v1.10.0@@ -73,6 +74,7 @@ require (golang.org/x/oauth2 v0.36.0golang.org/x/sync v0.21.0golang.org/x/sys v0.46.0+ golang.org/x/term v0.44.0google.golang.org/api v0.238.0google.golang.org/grpc v1.82.1gopkg.in/yaml.v3 v3.0.1@@ -179,7 +181,6 @@ require (github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirectgithub.com/muesli/cancelreader v0.2.2 // indirectgithub.com/muesli/reflow v0.3.0 // indirect- github.com/muesli/termenv v0.16.0 // indirectgithub.com/opencontainers/go-digest v1.0.0 // indirectgithub.com/opencontainers/image-spec v1.1.1 // indirectgithub.com/pelletier/go-toml/v2 v2.2.2 // indirect@@ -222,7 +223,6 @@ require (go.uber.org/multierr v1.11.0 // indirectgolang.org/x/arch v0.8.0 // indirectgolang.org/x/mod v0.37.0 // indirect- golang.org/x/term v0.44.0 // indirectgolang.org/x/text v0.39.0 // indirectgolang.org/x/time v0.12.0 // indirectgolang.org/x/tools v0.47.0 // indirectdiff --git a/backend/go.sum b/backend/go.sumindex 13027eab..115487b6 100644--- a/backend/go.sum+++ b/backend/go.sum@@ -628,8 +628,8 @@ github.com/vmihailenco/tagparser v0.1.2 h1:gnjoVuB/kljJ5wICEEOpx98oXMWPLj22G67Vbgithub.com/vmihailenco/tagparser v0.1.2/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=-github.com/vxcontrol/cloud v0.9.0 h1:p7xYTgUctbY8w6YfhugNzvfi3/0EQoZGumMe67keAng=-github.com/vxcontrol/cloud v0.9.0/go.mod h1:AeiQFqiMgJJAXy6FYXtDS2a3P/PMB56iiBNY2vGrZhQ=+github.com/vxcontrol/cloud v1.0.0 h1:2T8xM0z6+38Uj8a4lKfDxRP2/iXZVoS2vZmNgt6VQmk=+github.com/vxcontrol/cloud v1.0.0/go.mod h1:AeiQFqiMgJJAXy6FYXtDS2a3P/PMB56iiBNY2vGrZhQ=github.com/vxcontrol/graphiti-go-client v0.9.0 h1:3GxpFmQoHmz/d7/9tyEqD8+S99v2cuqG1UEmrbAFrLU=github.com/vxcontrol/graphiti-go-client v0.9.0/go.mod h1:6UHL5uqAKp4KAdziva4qgcAxFtBzU05Hm/BAo4NkAuo=github.com/vxcontrol/langchaingo v0.1.14-update.7 h1:iQnSNMmYQGbPQK5622JewlruaY6MIc7wzJklWq0Fja4=