// Package adopt is the CARLOS half of the existing-team adoption runner. // This file holds the types the later tasks store and pass around. package adopt import ( "bytes" "encoding/json" "sort" "time" "amadan.net/oficina/alta/carlosop" "amadan.net/oficina/alta/provision" ) // Issuer is the connection issuer the apps are told to trust. const Issuer = "https://connect.oficina.works" // AgentKeys are the two keys adoption writes: issuer first, team second. // They are written and unset together, never one alone. func AgentKeys(app provision.App) [2]string { return [2]string{app.Prefix + "_AGENT_ISSUER", app.Prefix + "_AGENT_TEAM"} } // CanonicalHost is the app's one public host, from Alta's own helper. func CanonicalHost(app provision.App) string { return provision.PublicHost(provision.AdoptionLabel, app.Slug) } // Identity is what must not change while adoption runs. An instance's // persistent storage is identified by Slug, CreatedAt, ReplicaAssignment // and Seed: recreating the instance or swapping its storage gives a new // record or a new assignment, and a move changes Region and sets Move. // ReplicaAssignment and Seed are canonical JSON ("" when absent). // // Domain and Route are the claim that makes RecordHost the canonical // one: the canonical domain, and the record host it routes to. A domain // re-pointed at another record is a changed identity even before the // record it left changes. type Identity struct { Account, App, RecordHost, Slug, Environment, Channel, Region string Domain, Route string Owned, Moving bool CreatedAt time.Time RecordSeq uint64 ReplicaAssignment, Seed string } // IdentityOf describes an instance record, the domain claim routed to it // and its effective view. It never errors: JSON it cannot canonicalise is // kept as its raw bytes, so a change still shows. func IdentityOf(d carlosop.Domain, in carlosop.Instance, eff carlosop.Effective) Identity { return Identity{ Account: in.Account, App: in.App, RecordHost: in.Host, Domain: d.Host, Route: d.Route, Slug: in.Slug, Environment: in.Environment, Channel: in.Channel, Region: in.Region, Owned: eff.Owned, Moving: present(in.Move), CreatedAt: in.CreatedAt, RecordSeq: in.RecordSeq, ReplicaAssignment: canonical(in.ReplicaAssignment), Seed: canonical(in.Seed), } } func present(raw json.RawMessage) bool { t := bytes.TrimSpace(raw) return len(t) > 0 && !bytes.Equal(t, []byte("null")) } // canonical re-encodes JSON with sorted keys and no spaces. func canonical(raw json.RawMessage) string { if !present(raw) { return "" } var v any dec := json.NewDecoder(bytes.NewReader(raw)) dec.UseNumber() if err := dec.Decode(&v); err != nil { return string(raw) } out, err := json.Marshal(v) if err != nil { return string(raw) } return string(out) } // SameIdentity returns the sorted, distinct codes of what differs: // storage (slug, created_at, replica, seed), instance (record host, and // account or app), alias (the canonical domain or the route it is // claimed for), environment, owned, channel, // region, moving and record (the record's sequence). // // record stays in (Task 7 step 0, read at carlos/platform config-receipts-1 // 8e229fdb): record_seq advances only through provision.WriteRecord, and // neither of the runner's own writes calls it. PUT /config // (console/apiconfig.go handleAPIConfigPut) writes the config bundle and // bumps the serial; POST /restart/scope (console/apirestartscope.go) // appends to the app's restart-scope.json log and bumps the serial, and // the boxes cycle units without writing the record (internal/host). So a // moved record_seq means some other writer touched the record. func SameIdentity(a, b Identity) []string { set := map[string]bool{} flag := func(differs bool, code string) { if differs { set[code] = true } } flag(a.Slug != b.Slug || !a.CreatedAt.Equal(b.CreatedAt) || a.ReplicaAssignment != b.ReplicaAssignment || a.Seed != b.Seed, "storage") flag(a.RecordHost != b.RecordHost || a.Account != b.Account || a.App != b.App, "instance") flag(a.Domain != b.Domain || a.Route != b.Route, "alias") flag(a.Environment != b.Environment, "environment") flag(a.Owned != b.Owned, "owned") flag(a.Channel != b.Channel, "channel") flag(a.Region != b.Region, "region") flag(a.Moving != b.Moving, "moving") flag(a.RecordSeq != b.RecordSeq, "record") codes := make([]string, 0, len(set)) for c := range set { codes = append(codes, c) } sort.Strings(codes) return codes } // Snapshot is one observation of an app's stored and effective config. // Values appear nowhere: Layers and the vars carry versions and digests, // so a Snapshot is sealed before it is stored. type Snapshot struct { Identity Identity WriteLayer string Layer carlosop.Layer Layers map[string]string Vars []carlosop.EffectiveVar HomeIssuer, HomeConsumer string Delivery Delivery At time.Time } // DeliveryCheck is the runner's own check that one app's restart // delivered the new configuration, recorded in place of 522 Plan 2 (Paul, // 2026-11-17, Home discussion 103). It is bound to what was observed: // AfterVersion is the after snapshot's write-layer version, sealed, and // any change in Identity, Delivery or the config voids it. // // Redeploy is nil for a check on the baseline's release. It is set only // for a check made on another release, after the write landed and the // after snapshot was captured, because the app was redeployed to a // release Paul approved (CheckDelivery's approved-redeploy path): it is // the journal's record of why the release moved. type DeliveryCheck struct { App string Identity Identity Delivery Delivery AfterVersion string At time.Time Redeploy *Redeploy `json:",omitempty"` } // Release is a served release: the /api/version body and // X-Carlos-Version. type Release struct{ Version, Header string } // RedeployApproved is the one reason a delivery check is made on a // release other than the baseline's: Paul approved the redeploy, and the // config's artifact for the app is the new release's. const RedeployApproved = "approved-redeploy" // Redeploy is why a delivery check was made on another release than the // baseline's: the release changed From the baseline's To the checked // one, whose SHA-256 is Artifact, the config's approved artifact, for // Reason (RedeployApproved). type Redeploy struct { From, To Release Artifact string Reason string } // HomePreflight is Home's state before a phase, from signed inspects and // Paul's read-only counts. Phase is "pre-initialize" or "pre-plan-a". type HomePreflight struct { Phase string CredentialSeq int64 DirectorySeq int64 CredentialHead string Generation *string ProposedID int64 ProposedSQID string Pending, Open bool Counts map[string]int64 AttestedAt time.Time } // WriteRecord journals one app's write. State is sent, landed or stopped. type WriteRecord struct { App, State string Sent map[string]string Receipt *carlosop.Receipt Code string } // RollbackRecord journals one app's rollback. State is sent, unset, // restarted or stopped. type RollbackRecord struct { App, State string Receipt *carlosop.Receipt Code string }