// This file is the activation gate: an adopted app's re-observation // immediately before Home activates it. Nothing activates without the // journal's complete evidence and a fresh read that still matches it. package adopt import ( "context" "crypto/sha256" "encoding/base64" "encoding/hex" "errors" "net/http" "regexp" "strings" "time" "amadan.net/oficina/alta/carlosop" "amadan.net/oficina/alta/provision" ) // Refusal is the gate's refusal: fixed codes, sorted, carrying key names, // layer names and identity fields only. type Refusal struct{ Codes []string } func (r *Refusal) Error() string { return "adopt: observation refused: " + strings.Join(r.Codes, ", ") } // RefusalCodes answers the codes to provision's engine, which parks an // adopted app's activation with them (provision.ObserverRefusal). func (r *Refusal) RefusalCodes() []string { return append([]string(nil), r.Codes...) } var _ provision.ObserverRefusal = (*Refusal)(nil) func refuse(codes ...string) error { return &Refusal{Codes: sortedSet(codes)} } // appVersion is the app_version Home's directory.activate-consumer // admits, as provision's step 10 mirrors it (Home 0cee9ba, // internal/operator/verification.go observedVersion). var appVersion = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._+-]{0,127}$`) // artifactShape is a reviewed artifact SHA-256: 64 lowercase hex. var artifactShape = regexp.MustCompile(`^[0-9a-f]{64}$`) // Observer is the CARLOS half's provision.Observer. Artifacts holds each // app's reviewed artifact SHA-256 as 64 lowercase hex: the SHA-256 of the // release name Paul reviewed, Alta's artifact scheme (provision // artifactDigest). Now stamps the fresh delivery; it is no age limit. type Observer struct { C *carlosop.Client H *http.Client S *Store Artifacts map[string]string Now func() time.Time } var _ provision.Observer = (*Observer)(nil) func (o *Observer) now() time.Time { if o.Now == nil { return time.Now().UTC() } return o.Now().UTC() } // Observe re-observes app and answers its activation evidence. It reads // and probes; it writes nothing, to CARLOS or the journal. It refuses // (a *Refusal) unless every one of these holds: // // - evidence: the pre-plan-a Home preflight, the baseline, a landed // write, the after snapshot and the delivery check (missing-); no // rollback (rollback-recorded); // - the write sent Issuer and teamSQID (issuer-mismatch, // team-mismatch); // - Plan C: S.CanaryGate passes (canary-not-active); // - the app's reviewed artifact SHA-256 is configured // (artifact-unreviewed); // - identity: a fresh read's equals the baseline's, the after // snapshot's and the delivery check's (identity-); // - preservation: Preserved(baseline, after) is empty // (after-not-preserved), and so is Preserved(baseline, fresh) // (drift-); // - config: the fresh read equals the after snapshot, every key's // digest, kind and source and every layer's version // (config-); no team-mode key (team-mode-); the Home // issuer and consumer are still the ones the app must trust // (home-issuer-mismatch, home-consumer-mismatch); both agent // keys are plain vars from the write layer carrying the sent // digests (agent-key-); // - the delivery check is current: its after version is the fresh // write layer's, and its delivered version and header are the fresh // ones (delivery-check-superseded). There is no wall-clock limit; // - delivery: the fresh probe passes Delivered against the baseline // (not-delivered); or, when the delivery check records an approved // redeploy, it is that redeploy, from the baseline's release to the // check's, under the approved artifact, and the fresh probe passes // redelivered (not-delivered: checkDelivered); the version body and X-Carlos-Version name the // same release, SameRelease (version-header-mismatch), and Home // admits the body // (version-unacceptable); // - artifact: the SHA-256 of the served release name (X-Carlos-Version) // is the reviewed one (artifact-mismatch). // // Stored evidence is checked before anything is read. A read that fails // is an error, not a refusal. func (o *Observer) Observe(ctx context.Context, app, teamSQID string) (provision.Observation, error) { if o == nil || o.C == nil || o.H == nil || o.S == nil { return provision.Observation{}, errors.New("adopt: the observer needs a console, a client and the journal") } a, ok := provision.AppBySlug(app) if !ok { return provision.Observation{}, refuse("unknown-app") } if !sqidShape.MatchString(teamSQID) { return provision.Observation{}, refuse("team-not-sqid") } ev, err := o.evidence(ctx, a, teamSQID) if err != nil { return provision.Observation{}, err } fresh, err := Observe(ctx, o.C, o.H, a) if err != nil { return provision.Observation{}, err } fresh.Delivery.At = o.now() var codes []string addAll := func(prefix string, cs ...string) { for _, c := range cs { codes = append(codes, prefix+c) } } add := func(code string) { codes = append(codes, code) } keys := AgentKeys(a) addAll("identity-", SameIdentity(ev.base.Identity, fresh.Identity)...) addAll("identity-", SameIdentity(ev.after.Identity, fresh.Identity)...) addAll("identity-", SameIdentity(ev.check.Identity, fresh.Identity)...) if len(Preserved(ev.base, ev.after, keys)) > 0 { add("after-not-preserved") } addAll("drift-", Preserved(ev.base, fresh, keys)...) addAll("config-", sameConfig(ev.after, fresh)...) addAll("team-mode-", TeamModeKeys(a, fresh.Vars)...) addAll("", homeCodes(a, fresh)...) for _, k := range keys { v := findVar(fresh.Vars, k) if v == nil || v.Kind != "env" || v.From != layerName(ev.base.WriteLayer) || v.Digest != carlosop.EnvDigest(ev.write.Sent[k]) { add("agent-key-" + k) } } chk := ev.check if chk.AfterVersion != fresh.Layer.Version || chk.Delivery.Version != fresh.Delivery.Version || chk.Delivery.Header != fresh.Delivery.Header { add("delivery-check-superseded") } d := fresh.Delivery if checkDelivered(a, ev.base.Delivery, chk, ev.artifact, d) != nil { add("not-delivered") } if !SameRelease(d.Version, d.Header) { add("version-header-mismatch") } if !appVersion.MatchString(d.Version) { add("version-unacceptable") } // The served release name is X-Carlos-Version, the release the edge // served from. Controller amendment (Task 3 ruling, 2026-11-16): // ChannelDigest is dropped and artifact_digest is Alta's existing // scheme, base64url SHA-256 of the served release name, as ordinary // activation sends it (provision/steps.go:700 records serving, :780 artifactDigest hashes // it, :816 stepActivate sends it). sum := sha256.Sum256([]byte(d.Header)) artifact := hex.EncodeToString(sum[:]) if artifact != ev.artifact { add("artifact-mismatch") } if len(codes) > 0 { return provision.Observation{}, refuse(codes...) } return provision.Observation{ AppVersion: d.Version, ArtifactDigest: base64.RawURLEncoding.EncodeToString(sum[:]), EnvironmentBinding: EnvironmentBinding(BindingInput{ Before: ev.base.Identity, After: ev.after.Identity, BeforeVars: ev.base.Vars, AfterVars: ev.after.Vars, BeforeLayers: ev.base.Layers, AfterLayers: ev.after.Layers, TeamSQID: teamSQID, Issuer: Issuer, ArtifactSHA256: artifact, Version: d.Version, BeforeDelivery: ev.base.Delivery, AfterDelivery: ev.after.Delivery, NowDelivery: d, Check: chk, }), }, nil } // gateEvidence is the journal's records for one app's activation. type gateEvidence struct { base, after Snapshot write WriteRecord check DeliveryCheck artifact string } // evidence reads and checks the stored evidence, before anything is // read from CARLOS or the app. func (o *Observer) evidence(ctx context.Context, a provision.App, teamSQID string) (gateEvidence, error) { var ev gateEvidence var missing []string have := func(ok bool, err error, what string) error { if err == nil && !ok { missing = append(missing, "missing-"+what) } return err } _, ok, err := o.S.HomePreflight(ctx, PhasePrePlanA) if err := have(ok, err, "home-preflight"); err != nil { return ev, err } base, ok, err := o.S.Baseline(ctx, a.Slug) if err := have(ok, err, "baseline"); err != nil { return ev, err } write, ok, err := o.S.Write(ctx, a.Slug) if err := have(ok && write.State == "landed", err, "write"); err != nil { return ev, err } after, ok, err := o.S.After(ctx, a.Slug) if err := have(ok, err, "after"); err != nil { return ev, err } check, ok, err := o.S.DeliveryCheck(ctx, a.Slug) if err := have(ok, err, "delivery-check"); err != nil { return ev, err } if len(missing) > 0 { return ev, refuse(missing...) } if _, ok, err := o.S.Rollback(ctx, a.Slug); err != nil { return ev, err } else if ok { return ev, refuse("rollback-recorded") } keys := AgentKeys(a) var codes []string if write.Sent[keys[0]] != Issuer { codes = append(codes, "issuer-mismatch") } if write.Sent[keys[1]] != teamSQID { codes = append(codes, "team-mismatch") } if len(codes) > 0 { return ev, refuse(codes...) } if err := o.S.CanaryGate(ctx, a.Slug); err != nil { if errors.Is(err, ErrCanaryNotActive) { return ev, refuse("canary-not-active") } return ev, err } artifact := o.Artifacts[a.Slug] if !artifactShape.MatchString(artifact) { return ev, refuse("artifact-unreviewed") } return gateEvidence{base: base, after: after, write: write, check: check, artifact: artifact}, nil }