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 "math"
5 "testing"
6)
7
8func intent(key string, reserve Usage) IntentSpec {
9 return IntentSpec{Key: key, Operation: OpProviderCall, Payload: []byte(`{"task":"implement","prompt":"` + key + `"}`), Reserve: reserve}
10}
11
12func TestReservationLimitsAndOverflow(t *testing.T) {
13 e := newEnv(t)
14 _, scope := e.createJob(softwareSpec(SoftwareSteps(false)))
15 h := e.claim(scope, "reproduce", "runner-a", "ws-1")
16 _, err := e.store.Intend(e.ctx, scope, h, intent("big-in", Usage{InputTokens: 32_001, OutputTokens: 10}))
17 wantCode(t, err, CodeBudget)
18 _, err = e.store.Intend(e.ctx, scope, h, intent("big-out", Usage{InputTokens: 10, OutputTokens: 4_001}))
19 wantCode(t, err, CodeBudget)
20 _, err = e.store.Intend(e.ctx, scope, h, intent("neg", Usage{InputTokens: -1}))
21 wantCode(t, err, CodeInvalid)
22 _, err = e.store.Intend(e.ctx, scope, h, intent("overflow", Usage{InputTokens: 10, OutputTokens: 10, CostMicros: math.MaxInt64}))
23 wantCode(t, err, CodeOverflow)
24 led, _ := e.store.Ledger(e.ctx, scope)
25 if led.Reserved != (Usage{}) {
26 t.Fatalf("refused intents reserved %+v", led)
27 }
28 acts, _ := e.store.Actions(e.ctx, scope)
29 if len(acts) != 0 {
30 t.Fatalf("refused intents wrote %d actions", len(acts))
31 }
32 // The pilot ceilings: 24 calls, 200k input, 40k output.
33 for i := 0; i < 6; i++ {
34 if _, err := e.store.Intend(e.ctx, scope, h, intent("max"+itoa(i), Usage{InputTokens: 32_000, OutputTokens: 4_000})); err != nil {
35 t.Fatalf("intent %d: %v", i, err)
36 }
37 }
38 _, err = e.store.Intend(e.ctx, scope, h, intent("over-input", Usage{InputTokens: 32_000, OutputTokens: 1}))
39 wantCode(t, err, CodeBudget)
40 led, _ = e.store.Ledger(e.ctx, scope)
41 if led.Reserved.InputTokens != 192_000 || led.Reserved.Calls != 6 || led.Reserved.CostMicros != 6*(32_000*3+4_000*15+1000) {
42 t.Fatalf("ledger %+v", led)
43 }
44 // The currency cap is explicit and enforced on its own (fresh account,
45 // so the token ledger above does not interfere).
46 e.auth.Issue("grant-b", Grant{Account: acctB, Operations: map[Operation]bool{OpProviderCall: true, OpJobContinue: true, OpAttemptContinue: true}})
47 spec := softwareSpec(SoftwareSteps(false))
48 spec.Account, spec.GrantRef = acctB, "grant-b"
49 spec.Policy = DefaultPolicy(2_000, testRates())
50 _, scope2 := e.createJob(spec)
51 h2, err := e.store.Claim(e.ctx, scope2, "reproduce", "runner-b", "ws-1")
52 must(t, err)
53 _, err = e.store.Intend(e.ctx, scope2, h2, intent("costly", Usage{InputTokens: 1000, OutputTokens: 0}))
54 wantCode(t, err, CodeBudget)
55 if _, err := e.store.Intend(e.ctx, scope2, h2, intent("cheap", Usage{InputTokens: 100, OutputTokens: 10})); err != nil {
56 t.Fatal(err)
57 }
58}
59
60func TestSettlementRules(t *testing.T) {
61 e := newEnv(t)
62 _, scope := e.createJob(softwareSpec(SoftwareSteps(false)))
63 h := e.claim(scope, "reproduce", "runner-a", "ws-1")
64 id, err := e.store.Intend(e.ctx, scope, h, intent("a", Usage{InputTokens: 100, OutputTokens: 50}))
65 must(t, err)
66 d, err := e.store.Admit(e.ctx, scope, id)
67 must(t, err)
68 if d.Action != id || d.Hash == "" {
69 t.Fatalf("dispatch %+v", d)
70 }
71 // Unknown usage settles at the full reservation; the action is still done.
72 must(t, e.store.Record(e.ctx, scope, id, Receipt{ID: "r-1", Outcome: OutcomeSuccess, Provider: "fake-a"}))
73 led, _ := e.store.Ledger(e.ctx, scope)
74 if led.Reserved.Calls != 0 || led.Spent.Calls != 1 {
75 t.Fatalf("ledger %+v", led)
76 }
77 a, _ := e.store.GetAction(e.ctx, scope, id)
78 if a.State != ActionSucceeded {
79 t.Fatalf("action %s", a.State)
80 }
81 // Late usage cannot refund the conservative unknown charge.
82 usage := &Usage{Calls: 1, InputTokens: 80, OutputTokens: 40, CostMicros: 1000}
83 must(t, e.store.Record(e.ctx, scope, id, Receipt{ID: "r-1", Outcome: OutcomeSuccess, Provider: "fake-a", Usage: usage}))
84 must(t, e.store.Record(e.ctx, scope, id, Receipt{ID: "r-1", Outcome: OutcomeSuccess, Provider: "fake-a", Usage: usage}))
85 led, _ = e.store.Ledger(e.ctx, scope)
86 if led.Reserved.Calls != 0 || led.Spent.InputTokens != 100 || led.Spent.OutputTokens != 50 {
87 t.Fatalf("ledger %+v", led)
88 }
89 other := &Usage{Calls: 1, InputTokens: 81, OutputTokens: 40, CostMicros: 1000}
90 wantCode(t, e.store.Record(e.ctx, scope, id, Receipt{ID: "r-1", Outcome: OutcomeSuccess, Provider: "fake-a", Usage: other}), CodeReceiptConflict)
91 wantCode(t, e.store.Record(e.ctx, scope, id, Receipt{ID: "r-2", Outcome: OutcomeSuccess, Provider: "fake-a", Usage: usage}), CodeReceiptConflict)
92 wantCode(t, e.store.Record(e.ctx, scope, id, Receipt{ID: "r-1", Outcome: OutcomeFailure, Provider: "fake-a"}), CodeReceiptConflict)
93 wantCode(t, e.store.Record(e.ctx, scope, id, Receipt{Outcome: OutcomeSuccess}), CodeInvalid)
94 wantCode(t, e.store.Record(e.ctx, scope, id, Receipt{ID: "x", Outcome: "maybe"}), CodeUnknownKind)
95 // A receipt for an action never admitted is refused.
96 id2, err := e.store.Intend(e.ctx, scope, h, intent("b", Usage{InputTokens: 100, OutputTokens: 50}))
97 must(t, err)
98 wantCode(t, e.store.Record(e.ctx, scope, id2, Receipt{ID: "r-3", Outcome: OutcomeSuccess}), CodeState)
99}
100
101func TestLateUsageAfterCancelAndAuditedSettlement(t *testing.T) {
102 e := newEnv(t)
103 _, scope := e.createJob(softwareSpec(SoftwareSteps(false)))
104 h := e.claim(scope, "reproduce", "runner-a", "ws-1")
105 dispatched, err := e.store.Intend(e.ctx, scope, h, intent("a", Usage{InputTokens: 100, OutputTokens: 50}))
106 must(t, err)
107 _, err = e.store.Admit(e.ctx, scope, dispatched)
108 must(t, err)
109 pending, err := e.store.Intend(e.ctx, scope, h, intent("b", Usage{InputTokens: 100, OutputTokens: 50}))
110 must(t, err)
111 rep, err := e.store.Cancel(e.ctx, scope, "paul", "stop")
112 must(t, err)
113 if len(rep.InFlight) != 1 || rep.InFlight[0] != dispatched || len(rep.Revoked) != 1 || rep.Revoked[0] != pending || rep.OpenReserved.Calls != 1 {
114 t.Fatalf("cancel %+v", rep)
115 }
116 _, err = e.store.Admit(e.ctx, scope, pending)
117 wantCode(t, err, CodeState)
118 // Late receipt for the in-flight action settles; the job stays cancelled.
119 usage := &Usage{Calls: 1, InputTokens: 90, OutputTokens: 45, CostMicros: 900}
120 must(t, e.store.Record(e.ctx, scope, dispatched, Receipt{ID: "late", Outcome: OutcomeSuccess, Provider: "fake-a", Usage: usage}))
121 led, _ := e.store.Ledger(e.ctx, scope)
122 if led.Reserved != (Usage{}) || led.Spent != *usage {
123 t.Fatalf("ledger %+v", led)
124 }
125 if j := e.job(scope); j.State != JobCancelled || j.Outcome != "stop" {
126 t.Fatalf("job %+v", j)
127 }
128 // Audited settlement: only open rows, only at the reserved maximum.
129 _, scope2 := e.createJob(softwareSpec(SoftwareSteps(false)))
130 must(t, e.store.Contain(e.ctx, scope, h.Attempt, "s"))
131 h2 := e.claim(scope2, "reproduce", "runner-b", "ws-1")
132 id, err := e.store.Intend(e.ctx, scope2, h2, intent("c", Usage{InputTokens: 100, OutputTokens: 50}))
133 must(t, err)
134 _, err = e.store.Admit(e.ctx, scope2, id)
135 must(t, err)
136 must(t, e.store.Lost(e.ctx, scope2, id, "timeout"))
137 res, _ := e.store.Reservations(e.ctx, scope2)
138 _, err = e.store.SettleAudited(e.ctx, scope2, res[0].ID, "", "x")
139 wantCode(t, err, CodeInvalid)
140 _, err = e.store.SettleAudited(e.ctx, scope, res[0].ID, "ops", "wrong job")
141 wantCode(t, err, CodeScope)
142 r, err := e.store.SettleAudited(e.ctx, scope2, res[0].ID, "ops", "provider dashboard shows no record")
143 must(t, err)
144 if r.State != ReservationSettledAudited || r.Settled != r.Reserved || r.SettledBy != "operator:ops" {
145 t.Fatalf("audited %+v", r)
146 }
147 _, err = e.store.SettleAudited(e.ctx, scope2, res[0].ID, "ops", "again")
148 wantCode(t, err, CodeState)
149 // A receipt arriving after the audit cannot reset the charge.
150 wantCode(t, e.store.Record(e.ctx, scope2, id, Receipt{ID: "late", Outcome: OutcomeSuccess, Usage: &Usage{Calls: 1}}), CodeReceiptConflict)
151 // The action itself is still uncertain: the audit charged, it did not decide.
152 a, _ := e.store.GetAction(e.ctx, scope2, id)
153 if a.State != ActionUncertain {
154 t.Fatalf("action %s", a.State)
155 }
156 if !e.hasEvent(scope2, "ledger.settled_audited") {
157 t.Fatal("no audit event")
158 }
159}
160