Repository navigation
Expand file tree
/
Copy pathowned-choice-export.go.txt
More file actions
292 lines (282 loc) · 12.7 KB
/
Copy pathowned-choice-export.go.txt
File metadata and controls
292 lines (282 loc) · 12.7 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
package production
import (
"bytes"
"fmt"
"reflect"
"testing"
"time"
"cloud-clicker/server/decimal"
"cloud-clicker/server/routes"
"cloud-clicker/server/save"
)
type manualChoiceFamilyResearch struct {
branches map[any]*ManualOwnedBranchResearch
queries, points, actions, rates, forks, banks int
}
// This type is exported only in the production test build.
type ManualOwnedBranchResearch struct {
t *testing.T
owner *endpointActionOwnerResearch
family *manualChoiceFamilyResearch
view []byte
}
type ManualOwnedChoiceStepResearch struct {
ID string
At time.Time
Revision int64
State *save.State
ParentIdentity, ChildIdentity any
Request IntentRequest
Decision save.IntentDecision
Bank *ManualOwnedBankResearch
}
type ManualOwnedBankStageResearch struct {
Kind string
Evaluation EvaluationResult
}
type ManualOwnedBankResearch struct {
Stages []ManualOwnedBankStageResearch
Events []save.EventWrite
Roles []RoleActivation
}
func (branch *ManualOwnedBranchResearch) Identity() any { return branch.owner.context }
func (branch *ManualOwnedBranchResearch) Head() []byte {
if branch.view != nil {
return append([]byte(nil), branch.view...)
}
return append([]byte(nil), branch.owner.context.calculation.origin...)
}
func (branch *ManualOwnedBranchResearch) State() *save.State {
return restoreActionStateResearch(branch.t, branch.owner.context.calculation.bundle, branch.Head())
}
func (branch *ManualOwnedBranchResearch) Revision() int64 {
return branch.owner.context.calculation.revision.Number
}
func (branch *ManualOwnedBranchResearch) Time() time.Time { return branch.State().EvaluatedThrough }
func (branch *ManualOwnedBranchResearch) originTime() time.Time {
return restoreActionStateResearch(branch.t, branch.owner.context.calculation.bundle, branch.owner.context.calculation.origin).EvaluatedThrough
}
// Separate real constructors, not a forged identity label or a bank-view rebase.
func (branch *ManualOwnedBranchResearch) IndependentBeginResearch() (*ManualOwnedBranchResearch, error) {
if branch.view != nil {
return nil, fmt.Errorf("independent control requires an action origin")
}
ctx := branch.owner.context.calculation
projection, err := newEndpointOwnerResearch(branch.t, ctx.bundle, branch.State(), ctx.revision, ctx.mode, ctx.mask, ctx.external, ctx.weight, ctx.batch)
if err != nil {
return nil, err
}
owner, err := newEndpointActionOwnerResearch(projection, branch.owner.context.carry, branch.owner.context.wireVersion)
if err != nil {
return nil, err
}
family := &manualChoiceFamilyResearch{branches: map[any]*ManualOwnedBranchResearch{}}
next := &ManualOwnedBranchResearch{t: branch.t, owner: owner, family: family}
family.branches[next.Identity()] = next
return next, nil
}
func (branch *ManualOwnedBranchResearch) Case() ManualOwnedCaseResearch {
ctx := branch.owner.context.calculation
gate, ok := ctx.bundle.Routes.Gate("gate.t2_to_t3")
if !ok || len(gate.Requirement) != 1 {
branch.t.Fatal("choice objective changed")
}
return ManualOwnedCaseResearch{Catalog: ctx.bundle.Economy, Routes: ctx.bundle.Routes, Hash: ctx.bundle.ConstantsHash, Target: gate.Requirement[0].Amount.String()}
}
// Gate progression has a conjunction, not the single-resource ranking objective
// asserted by Case. Return only the owned catalog inputs needed by that caller.
func (branch *ManualOwnedBranchResearch) GateInputsResearch() ManualOwnedCaseResearch {
ctx := branch.owner.context.calculation
return ManualOwnedCaseResearch{Catalog: ctx.bundle.Economy, Routes: ctx.bundle.Routes, Hash: ctx.bundle.ConstantsHash}
}
func (branch *ManualOwnedBranchResearch) Fork(state *save.State) (*ManualOwnedBranchResearch, error) {
if !bytes.Equal(mustEncodeState(branch.t, state), branch.Head()) {
return nil, fmt.Errorf("clone cannot manufacture a new action origin")
}
branch.family.forks++
return &ManualOwnedBranchResearch{t: branch.t, owner: branch.owner.fork(), family: branch.family, view: append([]byte(nil), branch.view...)}, nil
}
func (branch *ManualOwnedBranchResearch) Query(wait int64, quote decimal.Decimal, budget int) (int64, int, error) {
from := branch.Time().Sub(branch.originTime()).Milliseconds()
ceiling := int(2*(wait+1) + 1)
if budget < ceiling {
ceiling = budget
}
answer, err := (&endpointOwnerResearch{context: branch.owner.context.calculation}).queryBoundFrom(from, from+wait, quote, ceiling)
branch.family.queries++
points := answer.points + answer.preparationZeroPoints
branch.family.points += points
if err != nil {
return -1, points, err
}
if answer.at >= 0 {
if answer.at < from {
return -1, points, fmt.Errorf("query escaped retained-view floor")
}
return answer.at - from, points, nil
}
return -1, points, nil
}
func (branch *ManualOwnedBranchResearch) QueryAll(wait int64, requirements []routes.Requirement, budget int) (int64, int, error) {
from := branch.Time().Sub(branch.originTime()).Milliseconds()
answer, err := (&endpointOwnerResearch{context: branch.owner.context.calculation}).queryRequirementsResearch(from, from+wait, requirements, budget)
points := answer.points + answer.preparationZeroPoints
branch.family.queries++
branch.family.points += points
if err != nil {
return -1, points, err
}
if answer.at < 0 {
return -1, points, nil
}
return answer.at - from, points, nil
}
func (branch *ManualOwnedBranchResearch) Apply(request IntentRequest, state *save.State, at time.Time) (SimulationResult, *ManualOwnedBranchResearch, error) {
parsed, err := candidateRequestResearch(request)
if err != nil {
return SimulationResult{}, nil, err
}
answer, err := branch.owner.apply(branch.t, parsed, at.Sub(branch.originTime()).Milliseconds())
branch.family.actions++
if err != nil {
return SimulationResult{}, nil, err
}
result := SimulationResult{Decision: answer.decision, RoleActivations: answer.roles}
if answer.next == nil {
return result, nil, nil
}
*state = *answer.state
next := &ManualOwnedBranchResearch{t: branch.t, owner: answer.next, family: branch.family}
branch.family.branches[next.Identity()] = next
return result, next, nil
}
func (branch *ManualOwnedBranchResearch) Bank(state *save.State, at time.Time) (ManualOwnedBankResearch, *ManualOwnedBranchResearch, error) {
if !bytes.Equal(mustEncodeState(branch.t, state), branch.Head()) {
return ManualOwnedBankResearch{}, nil, fmt.Errorf("bank head does not bind retained context")
}
point, err := (&endpointOwnerResearch{context: branch.owner.context.calculation}).project(at.Sub(branch.originTime()).Milliseconds())
branch.family.banks++
if err != nil {
return ManualOwnedBankResearch{}, nil, err
}
*state = *point.state
next := &ManualOwnedBranchResearch{t: branch.t, owner: branch.owner.fork(), family: branch.family, view: mustEncodeState(branch.t, point.state)}
observation := ManualOwnedBankResearch{Events: copyPrivateWaitEventsResearch(point.events), Roles: append([]RoleActivation(nil), point.roles...)}
for _, stage := range point.stages {
observation.Stages = append(observation.Stages, ManualOwnedBankStageResearch{stage.kind, stage.evaluation})
}
return observation, next, nil
}
func (branch *ManualOwnedBranchResearch) Rate(state *save.State, mask AblationMask) (decimal.Decimal, error) {
if !bytes.Equal(mustEncodeState(branch.t, state), branch.Head()) {
return decimal.NaN, fmt.Errorf("rate read from unowned head")
}
branch.family.rates++
return candidateRateResearch(branch.owner.context.calculation, state, mask)
}
func RunManualOwnedChoicesResearch(t *testing.T, run func(*ManualOwnedBranchResearch, string) ([]ManualOwnedChoiceStepResearch, error)) {
t.Helper()
completed, delayed, verified, bankPopulations := 0, 0, 0, 0
for _, mode := range []EvaluationMode{ModeOnline, ModeOffline} {
for _, cashName := range []string{"available", "deficit", "bank"} {
t.Run(fmt.Sprintf("%s/%s", mode, cashName), func(t *testing.T) {
fixture := loadEndpointFixtureResearch(t, "combined", mode)
price, err := fixture.bundle.Economy.BulkCost("generator.beige_tower", fixture.state.GeneratorCounts["generator.beige_tower"], 1)
if err != nil {
t.Fatal(err)
}
seed := price.Sub(decimal.One)
if cashName == "available" {
second, err := fixture.bundle.Economy.BulkCost("generator.beige_tower", fixture.state.GeneratorCounts["generator.beige_tower"]+1, 1)
if err != nil {
t.Fatal(err)
}
seed = price.Add(second).Add(decimal.One)
} else if cashName == "bank" {
seed = decimal.Zero
}
setCash(t, fixture.state, seed.String())
projection, err := newEndpointOwnerResearch(t, fixture.bundle, fixture.state, fixture.revision, mode, AblationMask{}, fixture.external, fixture.weight, fixture.batch)
if err != nil {
t.Fatal(err)
}
var resolved replayAccrualResolved
if err := decodeReplayStrict(fixture.wire.Resolved, &resolved); err != nil || resolved.FounderCarry == nil {
t.Fatal(err)
}
owner, err := newEndpointActionOwnerResearch(projection, *resolved.FounderCarry, fixture.wire.Version)
if err != nil {
t.Fatal(err)
}
family := &manualChoiceFamilyResearch{branches: map[any]*ManualOwnedBranchResearch{}}
root := &ManualOwnedBranchResearch{t: t, owner: owner, family: family}
family.branches[root.Identity()] = root
origin := root.Head()
steps, err := run(root, "")
if cashName == "bank" {
if err != nil || len(steps) != 2 || family.actions != 0 || family.banks != 2 {
t.Fatal("retained bank views failed or became actions", err)
}
for _, step := range steps {
if step.ID != "~bank" || step.ParentIdentity != root.Identity() || step.ChildIdentity != root.Identity() || step.Revision != 1 || step.Bank == nil {
t.Fatal("bank renewed/lost origin")
}
}
bankPopulations++
return
}
if err != nil || len(steps) != 2 {
t.Fatal("actual two-choice run incomplete", len(steps), err)
}
parentID := root.Identity()
previous := append([]byte(nil), origin...)
for index, step := range steps {
parent := family.branches[parentID]
if step.ParentIdentity != parentID || step.ChildIdentity == parentID || parent == nil || !bytes.Equal(parent.Head(), previous) {
t.Fatal("selection lost/rebased its real parent context")
}
parsed, err := candidateRequestResearch(step.Request)
if err != nil {
t.Fatal(err)
}
quote, err := fixture.bundle.Economy.BulkCost(step.ID, parent.State().GeneratorCounts[step.ID], 1)
if err != nil || step.ID != "generator.beige_tower" {
t.Fatal("diagnostic did not select real generator", err)
}
ref, err := (&endpointOwnerResearch{context: parent.owner.context.calculation}).queryReference(2001, quote, 2002)
if err != nil || ref.at < 0 || step.At.Sub(parent.Time()).Milliseconds() != ref.at {
t.Fatal("selected child lost earliest actual window", ref.at, err)
}
row := actionRecordFromOwnerResearch(t, parent.owner, parsed, ref.at)
want := restoreActionStateResearch(t, fixture.bundle, parent.Head())
logged, err := ApplyLogged(want, row.Payload, fixture.bundle, row.Inputs)
if err != nil || logged.Outcome != save.IntentApplied || !bytes.Equal(mustEncodeState(t, step.State), mustEncodeState(t, want)) || !bytes.Equal(step.Decision.Receipt, logged.Receipt) || !reflect.DeepEqual(step.Decision.Events, logged.Events) || step.Revision != int64(index+2) {
t.Fatal("actual selected chain differs from independently logged action", index, err)
}
if ref.at > 0 {
delayed++
}
previous = mustEncodeState(t, want)
parentID = step.ChildIdentity
verified++
}
if !bytes.Equal(origin, root.Head()) || family.forks == 0 {
t.Fatal("choice consumed parent or skipped real clone propagation")
}
if cashName == "available" && mode == ModeOnline {
for _, fault := range []string{"identity", "typed_nil", "head", "clone"} {
if result, err := run(root, fault); err == nil || len(result) != 0 {
t.Fatal("invalid/missing context fell back", fault, err)
}
}
}
t.Logf("two choices queries%d points%d actions%d rate-reads%d forks%d; includes sibling probe, excludes reference/setup/inner work, not R11/workload acceptance", family.queries, family.points, family.actions, family.rates, family.forks)
completed++
})
}
}
if completed != 4 || verified != 8 || delayed < 2 || bankPopulations != 2 {
t.Fatal("incomplete selected-child population", completed, verified, delayed, bankPopulations)
}
t.Logf("selected-child populations%d independently-logged-actions%d delayed%d supported-bank-populations%d; beam/cache/complete retained-view lifetime remains open", completed, verified, delayed, bankPopulations)
}