| 1 | // This file is the delivery check's approved-redeploy path: an app |
| 2 | // redeployed, after its write landed and its after snapshot was captured, |
| 3 | // to a release Paul approved. configure, run again, records a fresh |
| 4 | // delivery check bound to the new release, writing and restarting |
| 5 | // nothing, only when the redeploy changed the release and nothing else. |
| 6 | // Home implementation/existing-team-adoption.md, "Approved redeploy after |
| 7 | // configure" (amendment, 2026-11-19). |
| 8 | package adopt |
| 9 | |
| 10 | import ( |
| 11 | "errors" |
| 12 | "net/http" |
| 13 | |
| 14 | "amadan.net/oficina/alta/carlosop" |
| 15 | "amadan.net/oficina/alta/provision" |
| 16 | ) |
| 17 | |
| 18 | // releaseChanged reports whether now serves another release than base: |
| 19 | // X-Carlos-Version, the served release name, is present and not the |
| 20 | // baseline's. Only then is the approved-redeploy path taken; a check on |
| 21 | // an unchanged release is Delivered's alone. |
| 22 | func releaseChanged(base, now Delivery) bool { |
| 23 | return now.Header != "" && now.Header != base.Header |
| 24 | } |
| 25 | |
| 26 | // redelivered lists, as fixed codes, how now fails to be app's delivery |
| 27 | // on a release other than base's that artifact approves: |
| 28 | // |
| 29 | // - release-unchanged: the baseline names no release, or the body or |
| 30 | // header is still the baseline's; |
| 31 | // - redirected, healthz (not 200), version-header-mismatch (the body |
| 32 | // and header do not name one release, SameRelease); |
| 33 | // - artifact-unreviewed, artifact-mismatch (ReviewedRelease); |
| 34 | // - operations-<shape>: the operations door does not give agent-access's |
| 35 | // answer as app's must, by the strict classifier (opsMoved), or |
| 36 | // operations-baseline when the baseline did not answer as app's does. |
| 37 | // |
| 38 | // It is Delivered with the release allowed to move to the approved one, |
| 39 | // and nothing else relaxed. |
| 40 | func redelivered(app provision.App, base, now Delivery, artifact string) []string { |
| 41 | var codes []string |
| 42 | add := func(c string) { codes = append(codes, c) } |
| 43 | if base.Version == "" || base.Header == "" || now.Version == base.Version || now.Header == base.Header { |
| 44 | add("release-unchanged") |
| 45 | } |
| 46 | if now.Redirected { |
| 47 | add("redirected") |
| 48 | } |
| 49 | if now.Health != http.StatusOK { |
| 50 | add("healthz") |
| 51 | } |
| 52 | if now.Version == "" || now.Header == "" || !SameRelease(now.Version, now.Header) { |
| 53 | add("version-header-mismatch") |
| 54 | } |
| 55 | if code := ReviewedRelease(now, artifact); code != "" { |
| 56 | add(code) |
| 57 | } |
| 58 | switch { |
| 59 | case base.Ops != BaselineOps(app): |
| 60 | add("operations-baseline") |
| 61 | case opsMoved(app, base, now) != nil: |
| 62 | add("operations-" + string(now.Ops)) |
| 63 | } |
| 64 | return sortedSet(codes) |
| 65 | } |
| 66 | |
| 67 | // redeployCodes lists every reason fresh is not app redeployed to the |
| 68 | // release artifact approves and otherwise exactly as configure left it. |
| 69 | // Empty admits it. Beyond redelivered: |
| 70 | // |
| 71 | // - write-not-landed: the journal's write is not landed with the two |
| 72 | // agent keys; |
| 73 | // - agent-key-<KEY>: a key is not the journal's sent value, a plain var |
| 74 | // from the write layer, in the effective config (as Preflight admits |
| 75 | // on a resume; sameConfig holds the stored layer to the after |
| 76 | // snapshot); |
| 77 | // - drift-<code>: Preserved(baseline, fresh), every other key, untouched |
| 78 | // layer and the identity as the baseline had them; |
| 79 | // - identity-<code>: SameIdentity with the baseline and the after |
| 80 | // snapshot: the same record, environment, storage and domain-to-route |
| 81 | // binding (a redeploy keeps the instance record); |
| 82 | // - config-<code>: sameConfig(after, fresh), so a config change a |
| 83 | // redeploy made, a new key included, is refused, as the check always |
| 84 | // refused it; |
| 85 | // - team-mode-<KEY>, home-issuer-mismatch, home-consumer-mismatch. |
| 86 | func redeployCodes(app provision.App, base, after, fresh Snapshot, w WriteRecord, landed bool, artifact string) []string { |
| 87 | codes := redelivered(app, base.Delivery, fresh.Delivery, artifact) |
| 88 | add := func(prefix string, cs ...string) { |
| 89 | for _, c := range cs { |
| 90 | codes = append(codes, prefix+c) |
| 91 | } |
| 92 | } |
| 93 | keys := AgentKeys(app) |
| 94 | if !landed || w.State != "landed" || len(w.Sent) != 2 || w.Sent[keys[0]] == "" || w.Sent[keys[1]] == "" { |
| 95 | add("", "write-not-landed") |
| 96 | } |
| 97 | for _, k := range keys { |
| 98 | v := findVar(fresh.Vars, k) |
| 99 | if v == nil || v.Kind != "env" || v.From != layerName(base.WriteLayer) || v.Digest != carlosop.EnvDigest(w.Sent[k]) { |
| 100 | add("agent-key-", k) |
| 101 | } |
| 102 | } |
| 103 | add("drift-", Preserved(base, fresh, keys)...) |
| 104 | add("identity-", SameIdentity(base.Identity, fresh.Identity)...) |
| 105 | add("identity-", SameIdentity(after.Identity, fresh.Identity)...) |
| 106 | add("config-", sameConfig(after, fresh)...) |
| 107 | add("team-mode-", TeamModeKeys(app, fresh.Vars)...) |
| 108 | add("", homeCodes(app, fresh)...) |
| 109 | return sortedSet(codes) |
| 110 | } |
| 111 | |
| 112 | // checkDelivered is the gate's delivery rule for chk. A check that |
| 113 | // records no redeploy holds now to Delivered against the baseline, as it |
| 114 | // always did. One that does holds it to the redeploy it recorded: made |
| 115 | // from the baseline's release, to the release chk observed, for the |
| 116 | // approved reason, under the artifact the config approves now; and now |
| 117 | // must pass redelivered against the baseline. The gate's freshness rule |
| 118 | // (delivery-check-superseded) separately holds now to chk's release. |
| 119 | func checkDelivered(app provision.App, base Delivery, chk DeliveryCheck, artifact string, now Delivery) error { |
| 120 | r := chk.Redeploy |
| 121 | if r == nil { |
| 122 | return Delivered(app, base, now) |
| 123 | } |
| 124 | switch { |
| 125 | case r.Reason != RedeployApproved, |
| 126 | r.From != (Release{base.Version, base.Header}), |
| 127 | r.To != (Release{chk.Delivery.Version, chk.Delivery.Header}), |
| 128 | r.Artifact != artifact: |
| 129 | return errors.New("adopt: the delivery check's redeploy is not this one") |
| 130 | } |
| 131 | if len(redelivered(app, base, now, artifact)) > 0 { |
| 132 | return errors.New("adopt: the redeployed release is not delivered") |
| 133 | } |
| 134 | return nil |
| 135 | } |
| 136 | |