oficina / alta Public

Clone
git clone https://amadan.net/oficina/alta

Plain git — no account needed to clone.

Download

Download this file

1// This file is the runner's delivery check of one app after its restart:
2// the delivery evidence recorded in place of 522 Plan 2 (Paul,
3// 2026-11-17, Home discussion 103, replacing his witnessed sign-in).
4package adopt
5
6import (
7 "context"
8 "crypto/sha256"
9 "encoding/hex"
10 "errors"
11 "fmt"
12 "net/http"
13 "strings"
14 "time"
15
16 "amadan.net/oficina/alta/carlosop"
17 "amadan.net/oficina/alta/provision"
18)
19
20// CheckDelivery checks that app's restart delivered its new configuration
21// and records the check. artifact is the app's reviewed artifact SHA-256,
22// 64 lowercase hex (the config's artifacts). It refuses, recording
23// nothing, unless all of these hold:
24//
25// - artifact is a reviewed SHA-256 (check-artifact-unreviewed), the
26// after snapshot exists (check-after-missing), and so does the
27// baseline it is measured against (check-baseline-missing): all
28// checked before anything is read;
29// - a fresh observation's probe passes Delivered against the baseline
30// (check-not-delivered): the canonical HTTPS /api/version body and
31// X-Carlos-Version unchanged, no redirect, /healthz 200, and GET
32// /api/v1/operations moved as the app's must: a 404 to agent-access's
33// challenge or list, Slides', Calls' and Equipo's own 401 to its
34// challenge, Base's challenge held (BaselineOps);
35// - the served release, X-Carlos-Version, hashes to artifact
36// (check-artifact-mismatch);
37// - the fresh identity equals the after snapshot's
38// (check-identity-<code>, one per SameIdentity code);
39// - the fresh config equals the after snapshot's, every key's digest,
40// kind and source and every layer's version
41// (check-config-<code>, one per sameConfig code).
42//
43// The check stores the fresh identity and delivery and the after
44// snapshot's write-layer version: it is bound to what was observed, not
45// to the clock, and a later change in any of them voids it. Run again on
46// an unchanged app, it records the check again.
47//
48// The reviewed release is checked before anything is written too:
49// configure's write refuses it (WriteAgentKeys), so this check only
50// catches a release that changed after the write.
51//
52// One release change is not refused: a redeploy Paul approved, after the
53// write landed and the after snapshot was captured (Home
54// existing-team-adoption.md, "Approved redeploy after configure"). When
55// the served release (X-Carlos-Version) is no longer the baseline's, the
56// check is made against the new release instead, and passes only if
57// redeployCodes finds nothing: the release is the approved artifact, the
58// body and header name it, /healthz is 200 and the operations door gives
59// agent-access's answer; the write landed and both agent keys hold its
60// sent values in the write layer; everything else is preserved from the
61// baseline and equals the after snapshot; the identity is unchanged.
62// The check it records carries Redeploy, from the baseline's release to
63// the new one, which the gate holds it to. Any code refuses with today's
64// check-not-delivered refusal, the codes after it, and records nothing.
65// This path reads only: like every check, it writes nothing to CARLOS
66// and restarts nothing.
67func CheckDelivery(ctx context.Context, s *Store, c *carlosop.Client, h *http.Client, app provision.App, artifact string) error {
68 if s == nil {
69 return errors.New("adopt: no adoption journal")
70 }
71 if !artifactShape.MatchString(artifact) {
72 return errors.New("adopt: check-artifact-unreviewed")
73 }
74 after, ok, err := s.After(ctx, app.Slug)
75 if err != nil {
76 return err
77 }
78 if !ok {
79 return errors.New("adopt: check-after-missing")
80 }
81 base, ok, err := s.Baseline(ctx, app.Slug)
82 if err != nil {
83 return err
84 }
85 if !ok {
86 return errors.New("adopt: check-baseline-missing")
87 }
88 fresh, err := Observe(ctx, c, h, app)
89 if err != nil {
90 return err
91 }
92 if err := Delivered(app, base.Delivery, fresh.Delivery); err != nil {
93 if !releaseChanged(base.Delivery, fresh.Delivery) {
94 return fmt.Errorf("adopt: check-not-delivered: %w", err)
95 }
96 return checkRedeploy(ctx, s, app, base, after, fresh, artifact, err)
97 }
98 if code := ReviewedRelease(fresh.Delivery, artifact); code != "" {
99 return errors.New("adopt: check-" + code)
100 }
101 if codes := SameIdentity(after.Identity, fresh.Identity); len(codes) > 0 {
102 for i, code := range codes {
103 codes[i] = "check-identity-" + code
104 }
105 return errors.New("adopt: " + strings.Join(codes, ", "))
106 }
107 if codes := sameConfig(after, fresh); len(codes) > 0 {
108 for i, code := range codes {
109 codes[i] = "check-config-" + code
110 }
111 return errors.New("adopt: " + strings.Join(codes, ", "))
112 }
113 return s.SaveDeliveryCheck(ctx, DeliveryCheck{
114 App: app.Slug,
115 Identity: fresh.Identity, Delivery: fresh.Delivery,
116 AfterVersion: after.Layer.Version,
117 At: time.Now().UTC(),
118 })
119}
120
121// checkRedeploy is CheckDelivery's approved-redeploy path, for a fresh
122// observation whose release is not the baseline's. refused is Delivered's
123// error, today's refusal, which leads any refusal here.
124func checkRedeploy(ctx context.Context, s *Store, app provision.App, base, after, fresh Snapshot, artifact string, refused error) error {
125 w, landed, err := s.Write(ctx, app.Slug)
126 if err != nil {
127 return err
128 }
129 if codes := redeployCodes(app, base, after, fresh, w, landed, artifact); len(codes) > 0 {
130 for i, code := range codes {
131 codes[i] = "redeploy-" + code
132 }
133 return fmt.Errorf("adopt: check-not-delivered: %w (%s)", refused, strings.Join(codes, ", "))
134 }
135 return s.SaveDeliveryCheck(ctx, DeliveryCheck{
136 App: app.Slug,
137 Identity: fresh.Identity, Delivery: fresh.Delivery,
138 AfterVersion: after.Layer.Version,
139 At: time.Now().UTC(),
140 Redeploy: &Redeploy{
141 From: Release{base.Delivery.Version, base.Delivery.Header},
142 To: Release{fresh.Delivery.Version, fresh.Delivery.Header},
143 Artifact: artifact,
144 Reason: RedeployApproved,
145 },
146 })
147}
148
149// ReviewedRelease is "" when d serves the reviewed release: artifact is
150// 64 lowercase hex (artifact-unreviewed otherwise) and the SHA-256 of
151// d's X-Carlos-Version is artifact (artifact-mismatch otherwise). It is
152// the one comparison of a served release with the config's artifacts.
153func ReviewedRelease(d Delivery, artifact string) string {
154 if !artifactShape.MatchString(artifact) {
155 return "artifact-unreviewed"
156 }
157 sum := sha256.Sum256([]byte(d.Header))
158 if hex.EncodeToString(sum[:]) != artifact {
159 return "artifact-mismatch"
160 }
161 return ""
162}
163
164// sameConfig lists, sorted, how b's config differs from a's, with key
165// and layer names only: added:<KEY>, gone:<KEY>, kind:<KEY>,
166// changed:<KEY> (digest) and from:<KEY> (source layer) over both the
167// effective config and the write layer's stored vars; layer:<name> for
168// any layer whose version moved, appeared or went (the write layer's
169// stored version included); write-layer when the write layer itself
170// differs. Unlike Preserved, no key is exempt: the delivery check
171// describes the after snapshot's config exactly.
172func sameConfig(a, b Snapshot) []string {
173 var out []string
174 if a.WriteLayer != b.WriteLayer || a.Layer.Bundle != b.Layer.Bundle {
175 out = append(out, "write-layer")
176 }
177 if a.Layer.Version != b.Layer.Version {
178 out = append(out, "layer:"+layerName(a.WriteLayer))
179 }
180 for name := range a.Layers {
181 if bv, ok := b.Layers[name]; !ok || bv != a.Layers[name] {
182 out = append(out, "layer:"+name)
183 }
184 }
185 for name := range b.Layers {
186 if _, ok := a.Layers[name]; !ok {
187 out = append(out, "layer:"+name)
188 }
189 }
190 type entry struct{ kind, digest, from string }
191 compare := func(am, bm map[string]entry) {
192 for k, av := range am {
193 bv, ok := bm[k]
194 switch {
195 case !ok:
196 out = append(out, "gone:"+k)
197 continue
198 case av.kind != bv.kind:
199 out = append(out, "kind:"+k)
200 }
201 if av.digest != bv.digest {
202 out = append(out, "changed:"+k)
203 }
204 if av.from != bv.from {
205 out = append(out, "from:"+k)
206 }
207 }
208 for k := range bm {
209 if _, ok := am[k]; !ok {
210 out = append(out, "added:"+k)
211 }
212 }
213 }
214 effective := func(s Snapshot) map[string]entry {
215 m := map[string]entry{}
216 for _, v := range s.Vars {
217 m[v.Key] = entry{v.Kind, v.Digest, v.From}
218 }
219 return m
220 }
221 stored := func(s Snapshot) map[string]entry {
222 m := map[string]entry{}
223 for _, v := range s.Layer.Vars {
224 m[v.Key] = entry{v.Kind, v.Digest, ""}
225 }
226 return m
227 }
228 compare(effective(a), effective(b))
229 compare(stored(a), stored(b))
230 return sortedSet(out)
231}
232