oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package team
2
3import (
4 "database/sql"
5 "encoding/json"
6 "math"
7 "testing"
8)
9
10func runnerPolicy(t *testing.T, mode string) Policy {
11 t.Helper()
12 limits := Limits{Calls: 24, InputTokens: 1000, OutputTokens: 500}
13 perTurn := Limits{Calls: 1, InputTokens: 100, OutputTokens: 50}
14 rates := Rates{}
15 if mode == "metered" {
16 limits.CostMicros = 100000
17 perTurn.CostMicros = 10000
18 rates = testRates()
19 }
20 p, err := RunnerPolicy(mode, limits, perTurn, rates)
21 must(t, err)
22 return p
23}
24func runnerJob(e *env, p Policy) (Scope, Handle) {
25 e.t.Helper()
26 spec := softwareSpec(SoftwareSteps(false))
27 spec.Project = 1
28 spec.Policy = p
29 _, s := e.createJob(spec)
30 return s, e.claim(s, "reproduce", "runner", "workspace")
31}
32func TestSubscriptionPolicy(t *testing.T) {
33 p := runnerPolicy(t, "subscription")
34 if p.Version != 2 || p.UsageMode != "subscription" || !p.RequireDistinctReviewFamily || p.AdmissionSeconds != 5 || p.LeaseSeconds != 60 || p.AttemptSeconds != 1200 || p.JobSeconds != 2700 || p.MaxAttemptsPerStep != 2 || p.MaxConcurrentAttempts != 2 || p.MaxDepth != 0 || p.Job.Calls != 24 {
35 t.Fatalf("runner policy: %+v", p)
36 }
37 u, err := p.price(OpProviderCall, Usage{Calls: 1, InputTokens: 10, CostMicros: 900})
38 must(t, err)
39 if u.CostMicros != 0 || !u.CostUnavailable {
40 t.Fatal("subscription currency fabricated", u)
41 }
42 raw, _ := json.Marshal(p)
43 _, err = ParsePolicy(raw)
44 must(t, err)
45 legacy := testPolicy()
46 if legacy.Version != 1 {
47 t.Fatal("legacy policy version changed")
48 }
49 must(t, legacy.Validate())
50 for _, change := range []func(*Policy){func(p *Policy) { p.UsageMode = "unknown" }, func(p *Policy) { p.PerCall.InputTokens = 0 }, func(p *Policy) { p.Job.OutputTokens = 0 }, func(p *Policy) { p.Job.CostMicros = 1 }} {
51 q := p
52 change(&q)
53 if q.Validate() == nil {
54 t.Fatal("invalid subscription accepted", q)
55 }
56 }
57 metered := runnerPolicy(t, "metered")
58 metered.Rates.InputMicrosPer = 0
59 if metered.Validate() == nil {
60 t.Fatal("unpriced metered policy accepted")
61 }
62 _, err = RunnerPolicy("subscription", Limits{}, Limits{}, Rates{})
63 wantCode(t, err, CodeInvalid)
64}
65func TestMixedConnectionPolicies(t *testing.T) {
66 for _, first := range []string{"subscription", "metered"} {
67 t.Run(first, func(t *testing.T) {
68 e := newEnv(t)
69 must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, periodSet()))
70 second := "metered"
71 if first == second {
72 second = "subscription"
73 }
74 for _, mode := range []string{first, second} {
75 p := runnerPolicy(t, mode)
76 if mode == "metered" {
77 p.PerCall.InputTokens = 80
78 p.PerCall.OutputTokens = 40
79 }
80 s, h := runnerJob(e, p)
81 id, err := e.store.Intend(e.ctx, s, h, intent("turn", Usage{CostMicros: 75}))
82 must(t, err)
83 a, err := e.store.GetAction(e.ctx, s, id)
84 must(t, err)
85 var r Reservation
86 must(t, e.store.read(e.ctx, func(tx *sql.Tx) error { var err error; r, err = loadReservation(e.ctx, tx, a.Reservation); return err }))
87 if r.Reserved.InputTokens != p.PerCall.InputTokens || r.Reserved.OutputTokens != p.PerCall.OutputTokens || r.Reserved.CostUnavailable != (mode == "subscription") {
88 t.Fatal("job policy ignored", r)
89 }
90 if mode == "metered" && r.Reserved.CostMicros != 1915 {
91 t.Fatal("wrong job price", r)
92 }
93 }
94 })
95 }
96 e := newEnv(t)
97 _, _ = runnerJob(e, runnerPolicy(t, "subscription"))
98 spec := softwareSpec(SoftwareSteps(false))
99 spec.Policy = runnerPolicy(t, "metered")
100 _, err := e.store.CreateJob(e.ctx, spec)
101 wantCode(t, err, CodeBudget)
102}
103func TestLoweredBudgetBlocksReservedAdmission(t *testing.T) {
104 e := newEnv(t)
105 b := periodSet()
106 must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, b))
107 s, h := runnerJob(e, runnerPolicy(t, "subscription"))
108 id, err := e.store.Intend(e.ctx, s, h, intent("turn", Usage{}))
109 must(t, err)
110 b.Revision = 1
111 b.Team.InputTokens = 99
112 for id, p := range b.Projects {
113 p.InputTokens = 99
114 b.Projects[id] = p
115 }
116 must(t, e.store.ConfigureBudgets(e.ctx, acctA, 1, b))
117 _, err = e.store.Admit(e.ctx, s, id)
118 wantCode(t, err, CodeBudget)
119}
120func TestUnknownUsageStillCharged(t *testing.T) {
121 e := newEnv(t)
122 s, h := runnerJob(e, runnerPolicy(t, "subscription"))
123 id, err := e.store.Intend(e.ctx, s, h, intent("first", Usage{}))
124 must(t, err)
125 _, err = e.store.Admit(e.ctx, s, id)
126 must(t, err)
127 _, err = e.store.Intend(e.ctx, s, h, intent("premature", Usage{}))
128 wantCode(t, err, CodeUncertain)
129 must(t, e.store.Lost(e.ctx, s, id, "missing reply"))
130 _, err = e.store.Intend(e.ctx, s, h, intent("first", Usage{}))
131 wantCode(t, err, CodeUncertain)
132 _, err = e.store.Intend(e.ctx, s, h, intent("after lost", Usage{}))
133 wantCode(t, err, CodeUncertain)
134 must(t, e.store.Record(e.ctx, s, id, Receipt{ID: "done", Outcome: OutcomeSuccess}))
135 _, err = e.store.Intend(e.ctx, s, h, intent("follow-up", Usage{}))
136 must(t, err)
137 sum, err := e.store.Ledger(e.ctx, s)
138 must(t, err)
139 if sum.Charged.Calls != 2 || sum.Charged.InputTokens != 200 || !sum.Charged.CostUnavailable {
140 t.Fatal("unknown usage refunded", sum)
141 }
142}
143func TestBoundedTurnReservation(t *testing.T) {
144 t.Run("job headroom", func(t *testing.T) {
145 e := newEnv(t)
146 p := runnerPolicy(t, "subscription")
147 p.Job.InputTokens = 150
148 p.Job.OutputTokens = 70
149 s, h := runnerJob(e, p)
150 for _, key := range []string{"first", "second"} {
151 _, err := e.store.Intend(e.ctx, s, h, intent(key, Usage{InputTokens: 1}))
152 must(t, err)
153 }
154 rows, err := e.store.Reservations(e.ctx, s)
155 must(t, err)
156 if rows[0].Reserved.InputTokens != 100 || rows[1].Reserved.InputTokens != 50 || rows[1].Reserved.OutputTokens != 20 {
157 t.Fatal("unbounded or unclipped turn", rows)
158 }
159 _, err = e.store.Intend(e.ctx, s, h, intent("zero", Usage{}))
160 wantCode(t, err, CodeBudget)
161 })
162 t.Run("period headroom", func(t *testing.T) {
163 e := newEnv(t)
164 b := periodSet()
165 b.Team.InputTokens = 130
166 for id, p := range b.Projects {
167 p.InputTokens = 130
168 b.Projects[id] = p
169 }
170 must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, b))
171 s, h := runnerJob(e, runnerPolicy(t, "subscription"))
172 for _, key := range []string{"a", "b"} {
173 _, err := e.store.Intend(e.ctx, s, h, intent(key, Usage{}))
174 must(t, err)
175 }
176 rows, err := e.store.Reservations(e.ctx, s)
177 must(t, err)
178 if rows[1].Reserved.InputTokens != 30 {
179 t.Fatal("period not clipped", rows)
180 }
181 _, err = e.store.Intend(e.ctx, s, h, intent("c", Usage{}))
182 wantCode(t, err, CodeBudget)
183 })
184 t.Run("publication slot", func(t *testing.T) {
185 e := newEnv(t)
186 p := runnerPolicy(t, "subscription")
187 p.Job.InputTokens = 10000
188 p.Job.OutputTokens = 10000
189 p.Account = p.Job
190 spec := softwareSpec([]StepSpec{{Key: "producer", Capability: CapImplement, Version: 1}, {Key: "reviewer", Capability: CapReview, Version: 1}})
191 spec.Policy = p
192 _, s := e.createJob(spec)
193 h := e.claim(s, "producer", "runner", "produce")
194 review := e.claim(s, "reviewer", "runner", "review")
195 for i := 0; i < 23; i++ {
196 turn := h
197 if i%2 == 1 {
198 turn = review
199 }
200 _, err := e.store.Intend(e.ctx, s, turn, intent(itoa(i), Usage{}))
201 must(t, err)
202 }
203 _, err := e.store.Intend(e.ctx, s, h, intent("24th provider", Usage{}))
204 wantCode(t, err, CodeBudget)
205 must(t, e.store.tx(e.ctx, func(tx *sql.Tx) error {
206 j, err := loadJob(e.ctx, tx, s)
207 if err != nil {
208 return err
209 }
210 _, _, err = reserve(e.ctx, tx, j, 0, h.Attempt, OpForgePublish, Usage{Calls: 1}, e.clock.Now())
211 return err
212 }))
213 sum, err := e.store.Ledger(e.ctx, s)
214 must(t, err)
215 if sum.Charged.Calls != 24 {
216 t.Fatal("publication slot lost", sum)
217 }
218 })
219 t.Run("overrun", func(t *testing.T) {
220 e := newEnv(t)
221 s, h := runnerJob(e, runnerPolicy(t, "subscription"))
222 id, err := e.store.Intend(e.ctx, s, h, intent("a", Usage{}))
223 must(t, err)
224 _, err = e.store.Admit(e.ctx, s, id)
225 must(t, err)
226 must(t, e.store.Record(e.ctx, s, id, Receipt{ID: "done", Outcome: OutcomeSuccess, Usage: &Usage{Calls: 1, InputTokens: 101, OutputTokens: 1, CostMicros: 999}}))
227 sum, err := e.store.Ledger(e.ctx, s)
228 must(t, err)
229 if sum.Spent.InputTokens != 101 || sum.Spent.CostMicros != 0 {
230 t.Fatal("overrun not persisted", sum)
231 }
232 if _, err = e.store.Intend(e.ctx, s, h, intent("b", Usage{})); err == nil {
233 t.Fatal("overrun allowed another turn")
234 }
235 })
236}
237func TestCachedUsageSeparate(t *testing.T) {
238 e := newEnv(t)
239 s, h := runnerJob(e, runnerPolicy(t, "subscription"))
240 id, err := e.store.Intend(e.ctx, s, h, intent("a", Usage{}))
241 must(t, err)
242 _, err = e.store.Admit(e.ctx, s, id)
243 must(t, err)
244 rc := Receipt{ID: "done", Outcome: OutcomeSuccess, Usage: &Usage{Calls: 7, InputTokens: 20, OutputTokens: 2, CachedInputTokens: 15}}
245 must(t, e.store.Record(e.ctx, s, id, rc))
246 must(t, e.store.Record(e.ctx, s, id, rc))
247 sum, err := e.store.Ledger(e.ctx, s)
248 must(t, err)
249 if sum.Spent.Calls != 1 || sum.Spent.InputTokens != 20 || sum.Spent.CachedInputTokens != 15 {
250 t.Fatal("cached input merged", sum)
251 }
252 _, err = (Usage{CachedInputTokens: math.MaxInt64}).add(Usage{CachedInputTokens: 1})
253 wantCode(t, err, CodeOverflow)
254}
255
256func TestSubscriptionCurrencyAvailability(t *testing.T) {
257 e := newEnv(t)
258 s, _ := runnerJob(e, runnerPolicy(t, "subscription"))
259 sum, err := e.store.Ledger(e.ctx, s)
260 must(t, err)
261 if !sum.Reserved.CostUnavailable || !sum.Spent.CostUnavailable || !sum.Charged.CostUnavailable {
262 t.Fatal("empty subscription ledger claims currency availability")
263 }
264 p := runnerPolicy(t, "metered")
265 u, err := p.price(OpForgePublish, Usage{Calls: 1, CostUnavailable: true})
266 must(t, err)
267 if u.CostUnavailable {
268 t.Fatal("caller can mark metered currency unavailable")
269 }
270}
271
272func TestBudgetSyncCommittedRetry(t *testing.T) {
273 e := newEnv(t)
274 b := periodSet()
275 b.Revision = 1
276 e.store.commitHook = func(stage string) error {
277 if stage == "after_commit" {
278 return ErrUncertain
279 }
280 return nil
281 }
282 err := e.store.tx(e.ctx, func(tx *sql.Tx) error { return e.store.SyncBudgetsTx(e.ctx, tx, acctA, b) })
283 if err == nil {
284 t.Fatal("lost commit reply not injected")
285 }
286 e.crash()
287 e.openNoResume()
288 got, err := e.store.ConfiguredBudgets(e.ctx, acctA)
289 must(t, err)
290 if got.Revision != 1 {
291 t.Fatal("committed budget lost")
292 }
293 before, err := e.store.RecoverySnapshot(e.ctx)
294 must(t, err)
295 for i := 0; i < 2; i++ {
296 must(t, e.store.tx(e.ctx, func(tx *sql.Tx) error { return e.store.SyncBudgetsTx(e.ctx, tx, acctA, b) }))
297 }
298 after, err := e.store.RecoverySnapshot(e.ctx)
299 must(t, err)
300 if before.Revision != after.Revision || before.Hash != after.Hash {
301 t.Fatal("retry advanced budget recovery twice")
302 }
303 b.Team.Calls++
304 err = e.store.tx(e.ctx, func(tx *sql.Tx) error { return e.store.SyncBudgetsTx(e.ctx, tx, acctA, b) })
305 wantCode(t, err, CodeRevision)
306 _, err = e.store.db.Exec(`UPDATE period_budgets SET content_hash='corrupted'`)
307 must(t, err)
308 _, err = e.store.ConfiguredBudgets(e.ctx, acctA)
309 wantCode(t, err, CodeInvalid)
310}
311
312func TestBoundedTurnReservationPublicationOnce(t *testing.T) {
313 e := newEnv(t)
314 spec := softwareSpec([]StepSpec{{Key: "publish", Capability: CapPublish, Version: 1}})
315 spec.Policy = runnerPolicy(t, "subscription")
316 _, s := e.createJob(spec)
317 h := e.claim(s, "publish", "runner", "workspace")
318 in := IntentSpec{Key: "publication", Operation: OpTrackerPublish, Payload: []byte(`{}`)}
319 id, err := e.store.Intend(e.ctx, s, h, in)
320 must(t, err)
321 same, err := e.store.Intend(e.ctx, s, h, in)
322 must(t, err)
323 if same != id {
324 t.Fatal("publication replay duplicated")
325 }
326 in.Key = "second publication"
327 _, err = e.store.Intend(e.ctx, s, h, in)
328 wantCode(t, err, CodeBudget)
329}
330