oficina / alta Public

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

Plain git — no account needed to clone.

Download

Download this file

1// Package adopt is the CARLOS half of the existing-team adoption runner.
2// This file holds the types the later tasks store and pass around.
3package adopt
4
5import (
6 "bytes"
7 "encoding/json"
8 "sort"
9 "time"
10
11 "amadan.net/oficina/alta/carlosop"
12 "amadan.net/oficina/alta/provision"
13)
14
15// Issuer is the connection issuer the apps are told to trust.
16const Issuer = "https://connect.oficina.works"
17
18// AgentKeys are the two keys adoption writes: issuer first, team second.
19// They are written and unset together, never one alone.
20func AgentKeys(app provision.App) [2]string {
21 return [2]string{app.Prefix + "_AGENT_ISSUER", app.Prefix + "_AGENT_TEAM"}
22}
23
24// CanonicalHost is the app's one public host, from Alta's own helper.
25func CanonicalHost(app provision.App) string {
26 return provision.PublicHost(provision.AdoptionLabel, app.Slug)
27}
28
29// Identity is what must not change while adoption runs. An instance's
30// persistent storage is identified by Slug, CreatedAt, ReplicaAssignment
31// and Seed: recreating the instance or swapping its storage gives a new
32// record or a new assignment, and a move changes Region and sets Move.
33// ReplicaAssignment and Seed are canonical JSON ("" when absent).
34//
35// Domain and Route are the claim that makes RecordHost the canonical
36// one: the canonical domain, and the record host it routes to. A domain
37// re-pointed at another record is a changed identity even before the
38// record it left changes.
39type Identity struct {
40 Account, App, RecordHost, Slug, Environment, Channel, Region string
41 Domain, Route string
42 Owned, Moving bool
43 CreatedAt time.Time
44 RecordSeq uint64
45 ReplicaAssignment, Seed string
46}
47
48// IdentityOf describes an instance record, the domain claim routed to it
49// and its effective view. It never errors: JSON it cannot canonicalise is
50// kept as its raw bytes, so a change still shows.
51func IdentityOf(d carlosop.Domain, in carlosop.Instance, eff carlosop.Effective) Identity {
52 return Identity{
53 Account: in.Account, App: in.App, RecordHost: in.Host, Domain: d.Host, Route: d.Route,
54 Slug: in.Slug, Environment: in.Environment, Channel: in.Channel, Region: in.Region,
55 Owned: eff.Owned, Moving: present(in.Move),
56 CreatedAt: in.CreatedAt, RecordSeq: in.RecordSeq,
57 ReplicaAssignment: canonical(in.ReplicaAssignment), Seed: canonical(in.Seed),
58 }
59}
60
61func present(raw json.RawMessage) bool {
62 t := bytes.TrimSpace(raw)
63 return len(t) > 0 && !bytes.Equal(t, []byte("null"))
64}
65
66// canonical re-encodes JSON with sorted keys and no spaces.
67func canonical(raw json.RawMessage) string {
68 if !present(raw) {
69 return ""
70 }
71 var v any
72 dec := json.NewDecoder(bytes.NewReader(raw))
73 dec.UseNumber()
74 if err := dec.Decode(&v); err != nil {
75 return string(raw)
76 }
77 out, err := json.Marshal(v)
78 if err != nil {
79 return string(raw)
80 }
81 return string(out)
82}
83
84// SameIdentity returns the sorted, distinct codes of what differs:
85// storage (slug, created_at, replica, seed), instance (record host, and
86// account or app), alias (the canonical domain or the route it is
87// claimed for), environment, owned, channel,
88// region, moving and record (the record's sequence).
89//
90// record stays in (Task 7 step 0, read at carlos/platform config-receipts-1
91// 8e229fdb): record_seq advances only through provision.WriteRecord, and
92// neither of the runner's own writes calls it. PUT /config
93// (console/apiconfig.go handleAPIConfigPut) writes the config bundle and
94// bumps the serial; POST /restart/scope (console/apirestartscope.go)
95// appends to the app's restart-scope.json log and bumps the serial, and
96// the boxes cycle units without writing the record (internal/host). So a
97// moved record_seq means some other writer touched the record.
98func SameIdentity(a, b Identity) []string {
99 set := map[string]bool{}
100 flag := func(differs bool, code string) {
101 if differs {
102 set[code] = true
103 }
104 }
105 flag(a.Slug != b.Slug || !a.CreatedAt.Equal(b.CreatedAt) ||
106 a.ReplicaAssignment != b.ReplicaAssignment || a.Seed != b.Seed, "storage")
107 flag(a.RecordHost != b.RecordHost || a.Account != b.Account || a.App != b.App, "instance")
108 flag(a.Domain != b.Domain || a.Route != b.Route, "alias")
109 flag(a.Environment != b.Environment, "environment")
110 flag(a.Owned != b.Owned, "owned")
111 flag(a.Channel != b.Channel, "channel")
112 flag(a.Region != b.Region, "region")
113 flag(a.Moving != b.Moving, "moving")
114 flag(a.RecordSeq != b.RecordSeq, "record")
115 codes := make([]string, 0, len(set))
116 for c := range set {
117 codes = append(codes, c)
118 }
119 sort.Strings(codes)
120 return codes
121}
122
123// Snapshot is one observation of an app's stored and effective config.
124// Values appear nowhere: Layers and the vars carry versions and digests,
125// so a Snapshot is sealed before it is stored.
126type Snapshot struct {
127 Identity Identity
128 WriteLayer string
129 Layer carlosop.Layer
130 Layers map[string]string
131 Vars []carlosop.EffectiveVar
132 HomeIssuer, HomeConsumer string
133 Delivery Delivery
134 At time.Time
135}
136
137// DeliveryCheck is the runner's own check that one app's restart
138// delivered the new configuration, recorded in place of 522 Plan 2 (Paul,
139// 2026-11-17, Home discussion 103). It is bound to what was observed:
140// AfterVersion is the after snapshot's write-layer version, sealed, and
141// any change in Identity, Delivery or the config voids it.
142//
143// Redeploy is nil for a check on the baseline's release. It is set only
144// for a check made on another release, after the write landed and the
145// after snapshot was captured, because the app was redeployed to a
146// release Paul approved (CheckDelivery's approved-redeploy path): it is
147// the journal's record of why the release moved.
148type DeliveryCheck struct {
149 App string
150 Identity Identity
151 Delivery Delivery
152 AfterVersion string
153 At time.Time
154 Redeploy *Redeploy `json:",omitempty"`
155}
156
157// Release is a served release: the /api/version body and
158// X-Carlos-Version.
159type Release struct{ Version, Header string }
160
161// RedeployApproved is the one reason a delivery check is made on a
162// release other than the baseline's: Paul approved the redeploy, and the
163// config's artifact for the app is the new release's.
164const RedeployApproved = "approved-redeploy"
165
166// Redeploy is why a delivery check was made on another release than the
167// baseline's: the release changed From the baseline's To the checked
168// one, whose SHA-256 is Artifact, the config's approved artifact, for
169// Reason (RedeployApproved).
170type Redeploy struct {
171 From, To Release
172 Artifact string
173 Reason string
174}
175
176// HomePreflight is Home's state before a phase, from signed inspects and
177// Paul's read-only counts. Phase is "pre-initialize" or "pre-plan-a".
178type HomePreflight struct {
179 Phase string
180 CredentialSeq int64
181 DirectorySeq int64
182 CredentialHead string
183 Generation *string
184 ProposedID int64
185 ProposedSQID string
186 Pending, Open bool
187 Counts map[string]int64
188 AttestedAt time.Time
189}
190
191// WriteRecord journals one app's write. State is sent, landed or stopped.
192type WriteRecord struct {
193 App, State string
194 Sent map[string]string
195 Receipt *carlosop.Receipt
196 Code string
197}
198
199// RollbackRecord journals one app's rollback. State is sent, unset,
200// restarted or stopped.
201type RollbackRecord struct {
202 App, State string
203 Receipt *carlosop.Receipt
204 Code string
205}
206