// This file is the runner's delivery check of one app after its restart: // the delivery evidence recorded in place of 522 Plan 2 (Paul, // 2026-11-17, Home discussion 103, replacing his witnessed sign-in). package adopt import ( "context" "crypto/sha256" "encoding/hex" "errors" "fmt" "net/http" "strings" "time" "amadan.net/oficina/alta/carlosop" "amadan.net/oficina/alta/provision" ) // CheckDelivery checks that app's restart delivered its new configuration // and records the check. artifact is the app's reviewed artifact SHA-256, // 64 lowercase hex (the config's artifacts). It refuses, recording // nothing, unless all of these hold: // // - artifact is a reviewed SHA-256 (check-artifact-unreviewed), the // after snapshot exists (check-after-missing), and so does the // baseline it is measured against (check-baseline-missing): all // checked before anything is read; // - a fresh observation's probe passes Delivered against the baseline // (check-not-delivered): the canonical HTTPS /api/version body and // X-Carlos-Version unchanged, no redirect, /healthz 200, and GET // /api/v1/operations moved as the app's must: a 404 to agent-access's // challenge or list, Slides', Calls' and Equipo's own 401 to its // challenge, Base's challenge held (BaselineOps); // - the served release, X-Carlos-Version, hashes to artifact // (check-artifact-mismatch); // - the fresh identity equals the after snapshot's // (check-identity-, one per SameIdentity code); // - the fresh config equals the after snapshot's, every key's digest, // kind and source and every layer's version // (check-config-, one per sameConfig code). // // The check stores the fresh identity and delivery and the after // snapshot's write-layer version: it is bound to what was observed, not // to the clock, and a later change in any of them voids it. Run again on // an unchanged app, it records the check again. // // The reviewed release is checked before anything is written too: // configure's write refuses it (WriteAgentKeys), so this check only // catches a release that changed after the write. // // One release change is not refused: a redeploy Paul approved, after the // write landed and the after snapshot was captured (Home // existing-team-adoption.md, "Approved redeploy after configure"). When // the served release (X-Carlos-Version) is no longer the baseline's, the // check is made against the new release instead, and passes only if // redeployCodes finds nothing: the release is the approved artifact, the // body and header name it, /healthz is 200 and the operations door gives // agent-access's answer; the write landed and both agent keys hold its // sent values in the write layer; everything else is preserved from the // baseline and equals the after snapshot; the identity is unchanged. // The check it records carries Redeploy, from the baseline's release to // the new one, which the gate holds it to. Any code refuses with today's // check-not-delivered refusal, the codes after it, and records nothing. // This path reads only: like every check, it writes nothing to CARLOS // and restarts nothing. func CheckDelivery(ctx context.Context, s *Store, c *carlosop.Client, h *http.Client, app provision.App, artifact string) error { if s == nil { return errors.New("adopt: no adoption journal") } if !artifactShape.MatchString(artifact) { return errors.New("adopt: check-artifact-unreviewed") } after, ok, err := s.After(ctx, app.Slug) if err != nil { return err } if !ok { return errors.New("adopt: check-after-missing") } base, ok, err := s.Baseline(ctx, app.Slug) if err != nil { return err } if !ok { return errors.New("adopt: check-baseline-missing") } fresh, err := Observe(ctx, c, h, app) if err != nil { return err } if err := Delivered(app, base.Delivery, fresh.Delivery); err != nil { if !releaseChanged(base.Delivery, fresh.Delivery) { return fmt.Errorf("adopt: check-not-delivered: %w", err) } return checkRedeploy(ctx, s, app, base, after, fresh, artifact, err) } if code := ReviewedRelease(fresh.Delivery, artifact); code != "" { return errors.New("adopt: check-" + code) } if codes := SameIdentity(after.Identity, fresh.Identity); len(codes) > 0 { for i, code := range codes { codes[i] = "check-identity-" + code } return errors.New("adopt: " + strings.Join(codes, ", ")) } if codes := sameConfig(after, fresh); len(codes) > 0 { for i, code := range codes { codes[i] = "check-config-" + code } return errors.New("adopt: " + strings.Join(codes, ", ")) } return s.SaveDeliveryCheck(ctx, DeliveryCheck{ App: app.Slug, Identity: fresh.Identity, Delivery: fresh.Delivery, AfterVersion: after.Layer.Version, At: time.Now().UTC(), }) } // checkRedeploy is CheckDelivery's approved-redeploy path, for a fresh // observation whose release is not the baseline's. refused is Delivered's // error, today's refusal, which leads any refusal here. func checkRedeploy(ctx context.Context, s *Store, app provision.App, base, after, fresh Snapshot, artifact string, refused error) error { w, landed, err := s.Write(ctx, app.Slug) if err != nil { return err } if codes := redeployCodes(app, base, after, fresh, w, landed, artifact); len(codes) > 0 { for i, code := range codes { codes[i] = "redeploy-" + code } return fmt.Errorf("adopt: check-not-delivered: %w (%s)", refused, strings.Join(codes, ", ")) } return s.SaveDeliveryCheck(ctx, DeliveryCheck{ App: app.Slug, Identity: fresh.Identity, Delivery: fresh.Delivery, AfterVersion: after.Layer.Version, At: time.Now().UTC(), Redeploy: &Redeploy{ From: Release{base.Delivery.Version, base.Delivery.Header}, To: Release{fresh.Delivery.Version, fresh.Delivery.Header}, Artifact: artifact, Reason: RedeployApproved, }, }) } // ReviewedRelease is "" when d serves the reviewed release: artifact is // 64 lowercase hex (artifact-unreviewed otherwise) and the SHA-256 of // d's X-Carlos-Version is artifact (artifact-mismatch otherwise). It is // the one comparison of a served release with the config's artifacts. func ReviewedRelease(d Delivery, artifact string) string { if !artifactShape.MatchString(artifact) { return "artifact-unreviewed" } sum := sha256.Sum256([]byte(d.Header)) if hex.EncodeToString(sum[:]) != artifact { return "artifact-mismatch" } return "" } // sameConfig lists, sorted, how b's config differs from a's, with key // and layer names only: added:, gone:, kind:, // changed: (digest) and from: (source layer) over both the // effective config and the write layer's stored vars; layer: for // any layer whose version moved, appeared or went (the write layer's // stored version included); write-layer when the write layer itself // differs. Unlike Preserved, no key is exempt: the delivery check // describes the after snapshot's config exactly. func sameConfig(a, b Snapshot) []string { var out []string if a.WriteLayer != b.WriteLayer || a.Layer.Bundle != b.Layer.Bundle { out = append(out, "write-layer") } if a.Layer.Version != b.Layer.Version { out = append(out, "layer:"+layerName(a.WriteLayer)) } for name := range a.Layers { if bv, ok := b.Layers[name]; !ok || bv != a.Layers[name] { out = append(out, "layer:"+name) } } for name := range b.Layers { if _, ok := a.Layers[name]; !ok { out = append(out, "layer:"+name) } } type entry struct{ kind, digest, from string } compare := func(am, bm map[string]entry) { for k, av := range am { bv, ok := bm[k] switch { case !ok: out = append(out, "gone:"+k) continue case av.kind != bv.kind: out = append(out, "kind:"+k) } if av.digest != bv.digest { out = append(out, "changed:"+k) } if av.from != bv.from { out = append(out, "from:"+k) } } for k := range bm { if _, ok := am[k]; !ok { out = append(out, "added:"+k) } } } effective := func(s Snapshot) map[string]entry { m := map[string]entry{} for _, v := range s.Vars { m[v.Key] = entry{v.Kind, v.Digest, v.From} } return m } stored := func(s Snapshot) map[string]entry { m := map[string]entry{} for _, v := range s.Layer.Vars { m[v.Key] = entry{v.Kind, v.Digest, ""} } return m } compare(effective(a), effective(b)) compare(stored(a), stored(b)) return sortedSet(out) }