-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinteger.go
More file actions
598 lines (549 loc) · 21.5 KB
/
Copy pathinteger.go
File metadata and controls
598 lines (549 loc) · 21.5 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
package conformance
import (
"context"
"encoding/json"
"fmt"
"sync"
"testing"
"time"
"github.com/operatorstack/boatstack/boatstack/kernel"
)
const (
integerDomainContractFingerprint = "61af3c7a11200a92087cbff475230ff6b19bb0440e3ba77dd0247ea60227f6b8"
alternateIntegerDomainContractFingerprint = "af13a3dd586f031d960a72ef1dfa58e0cef139b91a334b38aab39b3af772073f"
)
// IntegerDomain is the reference non-software domain.
type IntegerDomain struct {
mu sync.Mutex
value int
executions map[string]int
interruptNextIncrement bool
panicNextIncrement bool
}
func (d *IntegerDomain) Observe(context.Context, string) (kernel.Observation, error) {
d.mu.Lock()
defer d.mu.Unlock()
return kernel.NewObservation(struct {
Value int `json:"value"`
}{d.value})
}
func (d *IntegerDomain) Admissible(_ context.Context, evaluation kernel.Evaluation) (bool, string, error) {
var observed struct {
Value int `json:"value"`
}
if err := json.Unmarshal(evaluation.Observation.Value, &observed); err != nil {
return false, "", err
}
switch evaluation.Transition.Operation {
case "objective.bind":
return evaluation.State.ObjectiveBinding == nil && evaluation.Objective != nil, "objective is not yet bound", nil
case "counter.increment":
return evaluation.Objective != nil && observed.Value < 2, "exact objective is present and value is below target", nil
case "counter.reset":
return observed.Value > 0 || evaluation.State.Recovery != nil, "value is nonzero or recovery remains active", nil
case "counter.hold":
return true, "hold preserves the counter and is admissible from every declared source mode", nil
default:
return false, "unknown transition", nil
}
}
func (d *IntegerDomain) Verify(_ context.Context, evaluation kernel.Evaluation, _ kernel.Effect, target kernel.Observation) error {
var before, after struct {
Value int `json:"value"`
}
if err := json.Unmarshal(evaluation.Observation.Value, &before); err != nil {
return err
}
if err := json.Unmarshal(target.Value, &after); err != nil {
return err
}
switch evaluation.Transition.Operation {
case "objective.bind":
if before.Value != after.Value {
return fmt.Errorf("objective binding changed domain state")
}
case "counter.increment":
if after.Value != before.Value+1 {
return fmt.Errorf("increment postcondition failed")
}
case "counter.reset":
if after.Value != 0 {
return fmt.Errorf("reset postcondition failed")
}
case "counter.hold":
if before.Value != after.Value {
return fmt.Errorf("hold changed domain state")
}
}
return nil
}
func (d *IntegerDomain) changeObservation() {
d.mu.Lock()
defer d.mu.Unlock()
d.value++
}
func (d *IntegerDomain) interruptNext() {
d.mu.Lock()
defer d.mu.Unlock()
d.interruptNextIncrement = true
}
func (d *IntegerDomain) panicNext() {
d.mu.Lock()
defer d.mu.Unlock()
d.panicNextIncrement = true
}
func (d *IntegerDomain) effectCounts() map[string]int {
d.mu.Lock()
defer d.mu.Unlock()
copy := make(map[string]int, len(d.executions))
for transition, count := range d.executions {
copy[transition] = count
}
return copy
}
// IntegerOperator applies the reference domain operations.
type IntegerOperator struct{ Domain *IntegerDomain }
func (o IntegerOperator) Execute(_ context.Context, operation kernel.Operation) (kernel.Effect, error) {
o.Domain.mu.Lock()
defer o.Domain.mu.Unlock()
o.Domain.executions[operation.Transition.ID]++
switch operation.Transition.Operation {
case "objective.bind":
case "counter.increment":
o.Domain.value++
if o.Domain.interruptNextIncrement {
o.Domain.interruptNextIncrement = false
return kernel.Effect{}, fmt.Errorf("simulated interrupted operator")
}
if o.Domain.panicNextIncrement {
o.Domain.panicNextIncrement = false
panic("simulated process panic")
}
case "counter.reset":
o.Domain.value = 0
case "counter.hold":
default:
return kernel.Effect{}, fmt.Errorf("unknown operation")
}
facet := "counter.value"
if operation.Transition.Operation == "objective.bind" {
facet = "supervisor.objective"
}
return kernel.Effect{Facts: []kernel.EffectFact{{Facet: facet, Operation: operation.Transition.Operation, Fingerprint: fmt.Sprintf("value-%d", o.Domain.value)}}}, nil
}
// IntegerCapabilities classifies the reference operations.
type IntegerCapabilities struct{}
func (IntegerCapabilities) RequiredCapabilities(transition kernel.Transition) ([]kernel.Capability, error) {
switch transition.Operation {
case "objective.bind":
return []kernel.Capability{"objective.bind"}, nil
case "counter.increment":
return []kernel.Capability{"counter.increment"}, nil
case "counter.reset":
return []kernel.Capability{"counter.reset"}, nil
case "counter.hold":
return []kernel.Capability{"counter.hold"}, nil
default:
return nil, fmt.Errorf("unclassified operation %q", transition.Operation)
}
}
// MemoryReceipts records committed receipts for one control instance in the
// reference store.
type MemoryReceipts struct {
mu sync.Mutex
values []kernel.Receipt
}
func (r *MemoryReceipts) append(receipt kernel.Receipt) {
r.mu.Lock()
defer r.mu.Unlock()
r.values = append(r.values, receipt)
}
func (r *MemoryReceipts) snapshot() []kernel.Receipt {
r.mu.Lock()
defer r.mu.Unlock()
return append([]kernel.Receipt(nil), r.values...)
}
// memoryInstance is one durable instance record in the reference store.
type memoryInstance struct {
state kernel.ControlState
receipts *MemoryReceipts
commits int
}
// MemoryStateStore is the keyed multi-instance, revision-CAS reference
// Store. Every operation routes by the explicit instance identity; state,
// receipts, revisions, and compare-and-swap are local to one instance.
type MemoryStateStore struct {
mu sync.Mutex
instances map[string]*memoryInstance
current string
commitFailures int
}
// NewMemoryStateStore returns an empty multi-instance reference store.
func NewMemoryStateStore() *MemoryStateStore {
return &MemoryStateStore{instances: map[string]*memoryInstance{}}
}
func (s *MemoryStateStore) Create(_ context.Context, instanceID string, initial kernel.ControlState) error {
s.mu.Lock()
defer s.mu.Unlock()
if initial.InstanceID != instanceID {
return fmt.Errorf("initial control state belongs to %q, not %q", initial.InstanceID, instanceID)
}
if _, ok := s.instances[instanceID]; ok {
return kernel.InstanceExistsError{InstanceID: instanceID}
}
s.instances[instanceID] = &memoryInstance{state: cloneState(initial), receipts: &MemoryReceipts{}}
if s.current == "" {
s.current = instanceID
}
return nil
}
func (s *MemoryStateStore) Load(_ context.Context, instanceID string) (kernel.InstanceRecord, error) {
s.mu.Lock()
defer s.mu.Unlock()
instance, ok := s.instances[instanceID]
if !ok {
return kernel.InstanceRecord{}, kernel.InstanceNotFoundError{InstanceID: instanceID}
}
return kernel.InstanceRecord{State: cloneState(instance.state), Receipts: instance.receipts.snapshot()}, nil
}
func (s *MemoryStateStore) BeginEffect(_ context.Context, instanceID string, revision uint64, attempt kernel.ControlState) error {
s.mu.Lock()
defer s.mu.Unlock()
instance, ok := s.instances[instanceID]
if !ok {
return kernel.InstanceNotFoundError{InstanceID: instanceID}
}
if instance.state.Revision != revision {
return fmt.Errorf("stale revision")
}
instance.state = cloneState(attempt)
return nil
}
func (s *MemoryStateStore) CommitTransition(_ context.Context, instanceID string, revision uint64, target kernel.ControlState, receipt kernel.Receipt) error {
s.mu.Lock()
defer s.mu.Unlock()
instance, ok := s.instances[instanceID]
if !ok {
return kernel.InstanceNotFoundError{InstanceID: instanceID}
}
if s.commitFailures > 0 {
s.commitFailures--
return fmt.Errorf("simulated atomic transaction failure")
}
if instance.state.Revision != revision {
return fmt.Errorf("stale revision")
}
instance.state = cloneState(target)
instance.commits++
instance.receipts.append(receipt)
return nil
}
// currentInstance returns the fixture's active instance record. Fixture
// hooks act on this record; the kernel-facing Store methods never use it.
func (s *MemoryStateStore) currentInstance() *memoryInstance {
instance, ok := s.instances[s.current]
if !ok {
panic(fmt.Sprintf("memory store has no current instance %q", s.current))
}
return instance
}
func (s *MemoryStateStore) snapshot() (kernel.ControlState, int) {
s.mu.Lock()
defer s.mu.Unlock()
instance := s.currentInstance()
return cloneState(instance.state), instance.commits
}
// instanceReceipts exposes the active instance's receipt log so white-box
// counterexample tests can rewrite committed history.
func (s *MemoryStateStore) instanceReceipts() *MemoryReceipts {
s.mu.Lock()
defer s.mu.Unlock()
return s.currentInstance().receipts
}
func (s *MemoryStateStore) failNextCommit() {
s.mu.Lock()
defer s.mu.Unlock()
s.commitFailures++
}
// retarget provisions a separate control instance whose state mirrors the
// active one and makes it the fixture's active instance, so laws can address
// a prescription minted for one instance against another.
func (s *MemoryStateStore) retarget(instanceID string) {
s.mu.Lock()
defer s.mu.Unlock()
state := cloneState(s.currentInstance().state)
state.InstanceID = instanceID
s.instances[instanceID] = &memoryInstance{state: state, receipts: &MemoryReceipts{}}
s.current = instanceID
}
func (s *MemoryStateStore) isolate() {
s.mu.Lock()
defer s.mu.Unlock()
s.currentInstance().state.Mode = "isolated"
}
func (s *MemoryStateStore) bumpRevision() {
s.mu.Lock()
defer s.mu.Unlock()
s.currentInstance().state.Revision++
}
func (s *MemoryStateStore) retargetProgram(program kernel.ProgramIdentity) {
s.mu.Lock()
defer s.mu.Unlock()
s.currentInstance().state.Program = program
}
func (s *MemoryStateStore) rebind(objective kernel.Objective) {
s.mu.Lock()
defer s.mu.Unlock()
binding, err := kernel.BindObjective(objective)
if err != nil {
panic(err)
}
s.currentInstance().state.ObjectiveBinding = &binding
}
// forceState overwrites the requested instance's state without any
// compare-and-swap, for dishonest white-box store variants.
func (s *MemoryStateStore) forceState(instanceID string, state kernel.ControlState) {
s.mu.Lock()
defer s.mu.Unlock()
s.instances[instanceID].state = cloneState(state)
}
// forceCommit commits state plus receipt without any compare-and-swap, for
// dishonest white-box store variants.
func (s *MemoryStateStore) forceCommit(instanceID string, target kernel.ControlState, receipt kernel.Receipt) {
s.mu.Lock()
defer s.mu.Unlock()
instance := s.instances[instanceID]
instance.state = cloneState(target)
instance.commits++
instance.receipts.append(receipt)
}
// forceMode tears a commit by mutating only the mode of the requested
// instance, for dishonest white-box store variants.
func (s *MemoryStateStore) forceMode(instanceID, mode string) {
s.mu.Lock()
defer s.mu.Unlock()
s.instances[instanceID].state.Mode = mode
}
// MemoryLocker serializes one control instance.
type MemoryLocker struct{ mu sync.Mutex }
func (l *MemoryLocker) Acquire(context.Context, string) (kernel.Lock, error) {
l.mu.Lock()
return memoryLock{mu: &l.mu}, nil
}
type memoryLock struct{ mu *sync.Mutex }
func (l memoryLock) Unlock() error {
l.mu.Unlock()
return nil
}
// FixedClock returns one deterministic, test-controlled time.
type FixedClock struct {
mu sync.Mutex
Time time.Time
}
func (c *FixedClock) Now() time.Time {
c.mu.Lock()
defer c.mu.Unlock()
return c.Time
}
func (c *FixedClock) advance(duration time.Duration) {
c.mu.Lock()
defer c.mu.Unlock()
c.Time = c.Time.Add(duration)
}
// IntegerProgram compiles the reference control program.
func IntegerProgram() (kernel.Program, error) {
return kernel.CompileDomainProgram("integer-control", "1.0.0", "kernel-v1", integerDomainContractFingerprint, "unbound", []string{"two"}, []kernel.Transition{
{ID: "objective.bind", SourceModes: []string{"unbound"}, TargetMode: "zero", ObjectiveScope: kernel.ObjectiveNone, ObjectiveMutation: kernel.BindInitialObjective, RequiredCapabilities: []kernel.Capability{"objective.bind"}, OwnedFacets: []string{"supervisor.objective"}, Operation: "objective.bind", SelectionRank: 1, Selection: kernel.SelectionImplicit, Priority: 5},
{ID: "counter.increment-first", SourceModes: []string{"zero"}, TargetMode: "one", ObjectiveScope: kernel.ObjectiveBoundExact, ObjectiveMutation: kernel.PreserveObjective, RequiredCapabilities: []kernel.Capability{"counter.increment"}, OwnedFacets: []string{"counter.value"}, Operation: "counter.increment", SelectionRank: 1, Selection: kernel.SelectionImplicit, Priority: 10},
{ID: "counter.increment-second", SourceModes: []string{"one"}, TargetMode: "two", ObjectiveScope: kernel.ObjectiveBoundExact, ObjectiveMutation: kernel.PreserveObjective, RequiredCapabilities: []kernel.Capability{"counter.increment"}, OwnedFacets: []string{"counter.value"}, Operation: "counter.increment", SelectionRank: 1, Selection: kernel.SelectionImplicit, Priority: 10},
{ID: "counter.reset", SourceModes: []string{"one", "two"}, TargetMode: "zero", ObjectiveScope: kernel.ObjectiveOptionalPreserve, ObjectiveMutation: kernel.PreserveObjective, RequiredCapabilities: []kernel.Capability{"counter.reset"}, OwnedFacets: []string{"counter.value"}, Operation: "counter.reset", SelectionRank: 1, Selection: kernel.SelectionImplicit, Priority: 20},
{ID: "counter.hold", SourceModes: []string{"isolated", "zero"}, TargetMode: "zero", ObjectiveScope: kernel.ObjectiveOptionalPreserve, ObjectiveMutation: kernel.PreserveObjective, RequiredCapabilities: []kernel.Capability{"counter.hold"}, OwnedFacets: []string{"counter.value"}, Operation: "counter.hold", SelectionRank: 6, Selection: kernel.SelectionExplicitOnly, Priority: 30},
{ID: "objective.recover", SourceModes: []string{"unbound"}, TargetMode: "unbound", ObjectiveScope: kernel.ObjectiveOptionalPreserve, ObjectiveMutation: kernel.PreserveObjective, RequiredCapabilities: []kernel.Capability{"counter.reset"}, OwnedFacets: []string{"counter.value"}, Operation: "counter.reset", SelectionRank: 1, Selection: kernel.SelectionImplicit, Priority: 1, Recovers: []string{"objective.bind"}},
{ID: "counter.recover", SourceModes: []string{"isolated", "zero", "one", "two"}, TargetMode: "zero", ObjectiveScope: kernel.ObjectiveOptionalPreserve, ObjectiveMutation: kernel.PreserveObjective, RequiredCapabilities: []kernel.Capability{"counter.reset"}, OwnedFacets: []string{"counter.value"}, Operation: "counter.reset", SelectionRank: 1, Selection: kernel.SelectionImplicit, Priority: 1, Recovers: []string{"counter.hold", "counter.increment-first", "counter.increment-second", "counter.reset"}},
})
}
// IntegerFixture returns the reusable reference conformance suite.
func IntegerFixture() KernelConformance {
return newIntegerFixture(SetupUnbound)
}
func newIntegerFixture(setup Setup) KernelConformance {
if !setup.valid() {
panic(invalidSetup(setup))
}
program, err := IntegerProgram()
if err != nil {
panic(err)
}
objective, err := kernel.NewObjective("reach-two", 1, map[string]int{"value": 2})
if err != nil {
panic(err)
}
revised, err := kernel.NewObjective("reach-two", 2, map[string]int{"value": 3})
if err != nil {
panic(err)
}
conflicting, err := kernel.NewObjective("other", 1, map[string]int{"value": 0})
if err != nil {
panic(err)
}
alternateProgram, err := kernel.CompileDomainProgram(
program.ID,
program.Version,
program.RuntimeCompatibility,
alternateIntegerDomainContractFingerprint,
program.InitialMode,
program.MarkedModes,
program.Transitions,
)
if err != nil {
panic(err)
}
state := kernel.ControlState{InstanceID: "counter-fixture", Program: program.Identity(), Mode: "unbound", Revision: 1}
value := 0
switch setup {
case SetupConcurrentSameBase:
binding, bindErr := kernel.BindObjective(objective)
if bindErr != nil {
panic(bindErr)
}
state.Mode, state.ObjectiveBinding = "zero", &binding
case SetupMaintenanceAbsent:
state.Mode, value = "one", 1
case SetupMaintenanceBound:
binding, bindErr := kernel.BindObjective(objective)
if bindErr != nil {
panic(bindErr)
}
state.Mode, state.ObjectiveBinding, value = "one", &binding, 1
}
now := time.Date(2026, 8, 12, 10, 0, 0, 0, time.UTC)
authority := kernel.Authority{Receipts: []kernel.AuthorityReceipt{{ID: "human-counter", Subject: "fixture", Fingerprint: "fixture-authority", Capabilities: []kernel.Capability{"counter.audit", "counter.hold", "counter.increment", "counter.reset", "objective.bind"}, IssuedAt: now.Add(-time.Minute), ExpiresAt: now.Add(24 * time.Hour)}}}
domain := &IntegerDomain{value: value, executions: map[string]int{}}
store := NewMemoryStateStore()
if err := store.Create(context.Background(), state.InstanceID, state); err != nil {
panic(err)
}
clock := &FixedClock{Time: now}
fixture := KernelConformance{
Domain: domain,
Operator: IntegerOperator{Domain: domain},
CapabilityClassifier: IntegerCapabilities{},
Store: store,
Locker: &MemoryLocker{},
Clock: clock,
Program: program,
}
fixture.Scenario = Scenario{
InstanceID: state.InstanceID,
Objective: objective,
RevisedObjective: revised,
ConflictingObjective: conflicting,
AlternateProgram: alternateProgram,
Authority: authority,
BindTransition: "objective.bind",
AdvanceTransitions: []string{"counter.increment-first", "counter.increment-second"},
MaintenanceTransition: "counter.reset",
ExplicitOnlyTransition: "counter.hold",
IsolateExplicitOnly: store.isolate,
RecoveryTransition: "counter.recover",
RecoveryCapability: "counter.reset",
ExtraCapability: "counter.audit",
ChangeObservation: domain.changeObservation,
RebindObjective: store.rebind,
BumpStateRevision: store.bumpRevision,
RetargetProgram: store.retargetProgram,
AdvanceClock: clock.advance,
IndependentLocker: func() kernel.Locker { return &MemoryLocker{} },
VerifyCommitted: func(before, after Snapshot, receipt kernel.Receipt) error {
return verifyIntegerCommitted(program, before, after, receipt)
},
InterruptNextOperator: domain.interruptNext,
PanicNextOperator: domain.panicNext,
FailNextCommit: store.failNextCommit,
RetargetInstance: store.retarget,
Snapshot: func() Snapshot {
current, commits := store.snapshot()
observation, observeErr := domain.Observe(context.Background(), current.InstanceID)
if observeErr != nil {
panic(observeErr)
}
return Snapshot{State: current, Observation: observation, Effects: domain.effectCounts(), Receipts: store.instanceReceipts().snapshot(), CommitCount: commits}
},
}
fixture.New = func(_ testing.TB, requested Setup) KernelConformance {
return newIntegerFixture(requested)
}
if setup == SetupBound {
runtime, runtimeErr := kernel.NewRuntime(program, fixture.Domain, fixture.Operator, fixture.CapabilityClassifier, fixture.Store, fixture.Locker, fixture.Clock)
if runtimeErr != nil {
panic(runtimeErr)
}
for _, transition := range []string{fixture.Scenario.BindTransition, fixture.Scenario.AdvanceTransitions[0], fixture.Scenario.MaintenanceTransition} {
request := kernel.ResolveRequest{InstanceID: state.InstanceID, Objective: &objective, Authority: authority, Requested: transition}
resolution, resolveErr := runtime.Resolve(context.Background(), request)
if resolveErr != nil || resolution.Prescription == nil {
panic(fmt.Sprintf("seed bound fixture %s: decision=%#v error=%v", transition, resolution.Decision, resolveErr))
}
if _, applyErr := runtime.Apply(context.Background(), kernel.ApplyRequest{ResolveRequest: request, Prescription: *resolution.Prescription}); applyErr != nil {
panic(applyErr)
}
}
}
return fixture
}
func verifyIntegerCommitted(program kernel.Program, before, after Snapshot, receipt kernel.Receipt) error {
var prior, result struct {
Value int `json:"value"`
}
if err := json.Unmarshal(before.Observation.Value, &prior); err != nil {
return err
}
if err := json.Unmarshal(after.Observation.Value, &result); err != nil {
return err
}
transition, ok := program.Transition(receipt.TransitionID)
if !ok {
return fmt.Errorf("unknown transition %q", receipt.TransitionID)
}
facet := "counter.value"
if transition.Operation == "objective.bind" {
facet = "supervisor.objective"
}
expectedFact := kernel.EffectFact{Facet: facet, Operation: transition.Operation, Fingerprint: fmt.Sprintf("value-%d", result.Value)}
if len(receipt.Effects) != 1 || receipt.Effects[0] != expectedFact {
return fmt.Errorf("receipt effect facts %#v differ from independent evidence %#v", receipt.Effects, expectedFact)
}
switch transition.Operation {
case "objective.bind":
if result.Value != prior.Value {
return fmt.Errorf("objective binding changed value from %d to %d", prior.Value, result.Value)
}
case "counter.increment":
if result.Value != prior.Value+1 {
return fmt.Errorf("increment changed value from %d to %d", prior.Value, result.Value)
}
case "counter.reset":
if result.Value != 0 {
return fmt.Errorf("reset left value at %d", result.Value)
}
case "counter.hold":
if result.Value != prior.Value {
return fmt.Errorf("hold changed value from %d to %d", prior.Value, result.Value)
}
default:
return fmt.Errorf("unsupported operation %q", transition.Operation)
}
return nil
}
func cloneState(state kernel.ControlState) kernel.ControlState {
copy := state
if state.ObjectiveBinding != nil {
binding := *state.ObjectiveBinding
copy.ObjectiveBinding = &binding
}
if state.Recovery != nil {
recovery := *state.Recovery
copy.Recovery = &recovery
}
return copy
}