-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogram.go
More file actions
264 lines (246 loc) · 10.1 KB
/
Copy pathprogram.go
File metadata and controls
264 lines (246 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package kernel
import (
"fmt"
"sort"
)
type Transition struct {
ID string `json:"id"`
SourceModes []string `json:"source_modes"`
TargetMode string `json:"target_mode"`
ObjectiveScope ObjectiveScope `json:"objective_scope"`
ObjectiveMutation ObjectiveMutation `json:"objective_mutation"`
RequiredCapabilities []Capability `json:"required_capabilities"`
OwnedFacets []string `json:"owned_facets"`
Operation string `json:"operation"`
Priority int `json:"priority"`
Recovers []string `json:"recovers,omitempty"`
}
type ObjectiveMutation string
const (
PreserveObjective ObjectiveMutation = "preserve"
BindObjectiveMutation ObjectiveMutation = "bind"
ClearObjectiveMutation ObjectiveMutation = "clear"
)
func (m ObjectiveMutation) valid() bool {
return m == PreserveObjective || m == BindObjectiveMutation || m == ClearObjectiveMutation
}
func (t Transition) validate() error {
if !qualifiedSemanticID.MatchString(t.ID) || len(t.SourceModes) == 0 || t.TargetMode == "" || !t.ObjectiveScope.Valid() || !t.ObjectiveMutation.valid() || !qualifiedSemanticID.MatchString(t.Operation) || t.Priority < 1 {
return fmt.Errorf("transition %q has incomplete identity, modes, objective scope, operation, or priority", t.ID)
}
if len(t.RequiredCapabilities) == 0 || len(t.OwnedFacets) == 0 {
return fmt.Errorf("transition %q requires explicit capabilities and owned facets", t.ID)
}
if _, err := normalizeCapabilities(t.RequiredCapabilities); err != nil {
return fmt.Errorf("transition %q: %w", t.ID, err)
}
seen := map[string]bool{}
for _, facet := range t.OwnedFacets {
if !semanticID.MatchString(facet) || seen[facet] {
return fmt.Errorf("transition %q has invalid or duplicate owned facet %q", t.ID, facet)
}
seen[facet] = true
}
if t.ObjectiveMutation != PreserveObjective && (t.ObjectiveScope != ObjectiveNone || !contains(t.OwnedFacets, "supervisor.objective") || !containsCapability(t.RequiredCapabilities, "objective.bind")) {
return fmt.Errorf("transition %q objective mutation requires NONE scope, supervisor.objective ownership, and objective.bind capability", t.ID)
}
for _, recovered := range t.Recovers {
if !qualifiedSemanticID.MatchString(recovered) {
return fmt.Errorf("transition %q has invalid recovery target %q", t.ID, recovered)
}
}
return nil
}
func (t Transition) canonical() (Transition, error) {
copy := t
var err error
if copy.SourceModes, err = canonicalIDs(copy.SourceModes, "source mode"); err != nil {
return Transition{}, fmt.Errorf("transition %q: %w", copy.ID, err)
}
if copy.RequiredCapabilities, err = normalizeCapabilities(copy.RequiredCapabilities); err != nil {
return Transition{}, fmt.Errorf("transition %q: %w", copy.ID, err)
}
if copy.OwnedFacets, err = canonicalIDs(copy.OwnedFacets, "owned facet"); err != nil {
return Transition{}, fmt.Errorf("transition %q: %w", copy.ID, err)
}
if copy.Recovers, err = canonicalQualifiedIDs(copy.Recovers, "recovery target"); err != nil {
return Transition{}, fmt.Errorf("transition %q: %w", copy.ID, err)
}
return copy, nil
}
type Program struct {
SchemaVersion int `json:"schema_version"`
ID string `json:"id"`
Version string `json:"version"`
RuntimeCompatibility string `json:"runtime_compatibility"`
DomainContractFingerprint string `json:"domain_contract_fingerprint,omitempty"`
InitialMode string `json:"initial_mode"`
MarkedModes []string `json:"marked_modes"`
Transitions []Transition `json:"transitions"`
Fingerprint string `json:"fingerprint"`
byID map[string]Transition
}
func CompileProgram(id, version, runtimeCompatibility, initialMode string, markedModes []string, transitions []Transition) (Program, error) {
return CompileDomainProgram(id, version, runtimeCompatibility, "", initialMode, markedModes, transitions)
}
// CompileDomainProgram binds the complete domain contract fingerprint into the
// same executable Program identity used by the kernel. Domain-specific ABI
// fields remain outside the kernel, but changing any of them changes the
// program fingerprint and invalidates prior prescriptions.
func CompileDomainProgram(id, version, runtimeCompatibility, domainContractFingerprint, initialMode string, markedModes []string, transitions []Transition) (Program, error) {
program := Program{SchemaVersion: ProgramSchemaVersion, ID: id, Version: version, RuntimeCompatibility: runtimeCompatibility, DomainContractFingerprint: domainContractFingerprint, InitialMode: initialMode, MarkedModes: append([]string(nil), markedModes...), Transitions: append([]Transition(nil), transitions...)}
if err := program.prepare(); err != nil {
return Program{}, err
}
identity := program
identity.Fingerprint = ""
identity.byID = nil
fingerprint, err := contentHash(identity)
if err != nil {
return Program{}, err
}
program.Fingerprint = fingerprint
return program, nil
}
func (p *Program) prepare() error {
if p.SchemaVersion != ProgramSchemaVersion || !semanticID.MatchString(p.ID) || p.Version == "" || p.RuntimeCompatibility == "" || p.InitialMode == "" || len(p.MarkedModes) == 0 || len(p.Transitions) == 0 {
return fmt.Errorf("program requires schema, identity, compatibility, initial mode, marked modes, and transitions")
}
if p.DomainContractFingerprint != "" && len(p.DomainContractFingerprint) != 64 {
return fmt.Errorf("program domain contract fingerprint must be an exact sha256 identity")
}
markedModes, err := canonicalIDs(p.MarkedModes, "marked mode")
if err != nil {
return err
}
p.MarkedModes = markedModes
p.Transitions = append([]Transition(nil), p.Transitions...)
for index, transition := range p.Transitions {
canonical, err := transition.canonical()
if err != nil {
return err
}
p.Transitions[index] = canonical
}
sort.Slice(p.Transitions, func(i, j int) bool {
if p.Transitions[i].Priority != p.Transitions[j].Priority {
return p.Transitions[i].Priority < p.Transitions[j].Priority
}
return p.Transitions[i].ID < p.Transitions[j].ID
})
p.byID = make(map[string]Transition, len(p.Transitions))
for _, transition := range p.Transitions {
if err := transition.validate(); err != nil {
return err
}
if _, exists := p.byID[transition.ID]; exists {
return fmt.Errorf("program duplicates transition %q", transition.ID)
}
p.byID[transition.ID] = transition
}
for _, transition := range p.Transitions {
if len(transition.Recovers) != 0 && (transition.ObjectiveScope == ObjectiveBoundExact || transition.ObjectiveMutation != PreserveObjective) {
return fmt.Errorf("recovery transition %q must preserve objective state without requiring an exact objective", transition.ID)
}
for _, recovered := range transition.Recovers {
recoveredTransition, exists := p.byID[recovered]
if !exists {
return fmt.Errorf("transition %q recovers unknown transition %q", transition.ID, recovered)
}
for _, sourceMode := range recoveredTransition.SourceModes {
if !contains(transition.SourceModes, sourceMode) {
return fmt.Errorf("transition %q cannot recover %q from source mode %q", transition.ID, recovered, sourceMode)
}
}
}
}
recovered := make(map[string]bool, len(p.Transitions))
for _, transition := range p.Transitions {
for _, recoveredID := range transition.Recovers {
recovered[recoveredID] = true
}
}
for _, transition := range p.Transitions {
if len(transition.Recovers) == 0 && !recovered[transition.ID] {
return fmt.Errorf("transition %q has no declared recovery", transition.ID)
}
}
return nil
}
func canonicalIDs(values []string, label string) ([]string, error) {
result := append([]string(nil), values...)
sort.Strings(result)
for index, value := range result {
if !semanticID.MatchString(value) {
return nil, fmt.Errorf("%s %q is not a semantic identifier", label, value)
}
if index > 0 && value == result[index-1] {
return nil, fmt.Errorf("%s %q is duplicated", label, value)
}
}
return result, nil
}
func canonicalQualifiedIDs(values []string, label string) ([]string, error) {
result := append([]string(nil), values...)
sort.Strings(result)
for index, value := range result {
if !qualifiedSemanticID.MatchString(value) {
return nil, fmt.Errorf("%s %q is not a qualified semantic identifier", label, value)
}
if index > 0 && value == result[index-1] {
return nil, fmt.Errorf("%s %q is duplicated", label, value)
}
}
return result, nil
}
func (p Program) Validate() error {
copy := p
want := copy.Fingerprint
copy.Fingerprint = ""
copy.byID = nil
if err := copy.prepare(); err != nil {
return err
}
got, err := contentHash(copy)
if err != nil || want == "" || got != want {
return fmt.Errorf("program fingerprint does not identify its canonical executable representation")
}
return nil
}
func (p Program) Identity() ProgramIdentity {
return ProgramIdentity{ID: p.ID, Version: p.Version, Fingerprint: p.Fingerprint}
}
func (p Program) Transition(id string) (Transition, bool) {
if p.byID == nil {
copy := p
if copy.prepare() != nil {
return Transition{}, false
}
return copy.Transition(id)
}
transition, ok := p.byID[id]
return transition, ok
}
func (p Program) Marked(mode string) bool {
for _, candidate := range p.MarkedModes {
if candidate == mode {
return true
}
}
return false
}
func (p Program) Clone() Program {
copy := p
copy.MarkedModes = append([]string(nil), p.MarkedModes...)
copy.Transitions = append([]Transition(nil), p.Transitions...)
copy.byID = make(map[string]Transition, len(copy.Transitions))
for index, transition := range copy.Transitions {
transition.SourceModes = append([]string(nil), transition.SourceModes...)
transition.RequiredCapabilities = append([]Capability(nil), transition.RequiredCapabilities...)
transition.OwnedFacets = append([]string(nil), transition.OwnedFacets...)
transition.Recovers = append([]string(nil), transition.Recovers...)
copy.Transitions[index] = transition
copy.byID[transition.ID] = transition
}
return copy
}