| 1 | package team |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestIntentKeyIsBoundToItsPayload(t *testing.T) { |
| 10 | e := newEnv(t) |
| 11 | _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 12 | h := e.claim(scope, "reproduce", "runner-a", "ws-1") |
| 13 | a, err := e.store.Intend(e.ctx, scope, h, IntentSpec{Key: "k", Operation: OpProviderCall, Payload: []byte(`{"task":"implement", "prompt": "p"}`), Reserve: Usage{InputTokens: 1, OutputTokens: 1}}) |
| 14 | must(t, err) |
| 15 | // Same payload, other spelling: same action, no second reservation. |
| 16 | b, err := e.store.Intend(e.ctx, scope, h, IntentSpec{Key: "k", Operation: OpProviderCall, Payload: []byte(`{ "prompt":"p","task":"implement" }`), Reserve: Usage{InputTokens: 5, OutputTokens: 5}}) |
| 17 | must(t, err) |
| 18 | if a != b { |
| 19 | t.Fatalf("retry made a new action %d != %d", a, b) |
| 20 | } |
| 21 | res, _ := e.store.Reservations(e.ctx, scope) |
| 22 | if len(res) != 1 || res[0].Reserved.InputTokens != 1 { |
| 23 | t.Fatalf("reservations %+v", res) |
| 24 | } |
| 25 | _, err = e.store.Intend(e.ctx, scope, h, IntentSpec{Key: "k", Operation: OpProviderCall, Payload: []byte(`{"task":"implement","prompt":"q"}`), Reserve: Usage{InputTokens: 1, OutputTokens: 1}}) |
| 26 | wantCode(t, err, CodeConflict) |
| 27 | _, err = e.store.Intend(e.ctx, scope, h, IntentSpec{Key: "k2", Operation: OpProviderCall, Payload: []byte(`["not","an","object"]`)}) |
| 28 | wantCode(t, err, CodeInvalid) |
| 29 | _, err = e.store.Intend(e.ctx, scope, h, IntentSpec{Key: "k3", Operation: "shell.exec", Payload: []byte(`{}`)}) |
| 30 | wantCode(t, err, CodeUnknownKind) |
| 31 | _, err = e.store.Intend(e.ctx, scope, h, IntentSpec{Key: "k4", Operation: OpJobContinue, Payload: []byte(`{}`)}) |
| 32 | wantCode(t, err, CodeUnknownKind) |
| 33 | _, err = e.store.Intend(e.ctx, scope, h, IntentSpec{Key: "k5", Operation: OpProviderCall, Payload: []byte(`{"task":"` + strings.Repeat("x", MaxPayloadBytes) + `"}`)}) |
| 34 | wantCode(t, err, CodeSize) |
| 35 | _, err = e.store.Intend(e.ctx, scope, h, IntentSpec{Key: "", Operation: OpProviderCall, Payload: []byte(`{}`)}) |
| 36 | wantCode(t, err, CodeInvalid) |
| 37 | // After success the key still answers the same action, and it cannot be re-admitted. |
| 38 | rc, err := e.store.Dispatch(e.ctx, scope, a) |
| 39 | must(t, err) |
| 40 | c, err := e.store.Intend(e.ctx, scope, h, IntentSpec{Key: "k", Operation: OpProviderCall, Payload: []byte(`{"task":"implement","prompt":"p"}`), Reserve: Usage{InputTokens: 1, OutputTokens: 1}}) |
| 41 | must(t, err) |
| 42 | if c != a { |
| 43 | t.Fatalf("after success key gave %d", c) |
| 44 | } |
| 45 | _, err = e.store.Admit(e.ctx, scope, a) |
| 46 | wantCode(t, err, CodeState) |
| 47 | if e.impl.Calls() != 1 || rc.ID == "" { |
| 48 | t.Fatalf("provider ran %d times", e.impl.Calls()) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestPublicationIsBoundToTargetAndCandidate(t *testing.T) { |
| 53 | e := newEnv(t) |
| 54 | _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 55 | w := e.workflow(scope) |
| 56 | repro, _, err := w.Reproduce(e.ctx, []byte(FixtureBaseSource)) |
| 57 | must(t, err) |
| 58 | cand, err := w.Implement(e.ctx, "implement", "ws-implement-1") |
| 59 | must(t, err) |
| 60 | _, err = w.ReviewCandidate(e.ctx, "ws-review-1", cand) |
| 61 | must(t, err) |
| 62 | _, err = w.Verify(e.ctx, repro, cand) |
| 63 | must(t, err) |
| 64 | h := e.claim(scope, "publish", "runner-a", "ws-publish-1") |
| 65 | try := func(repo, ref, head string) error { |
| 66 | _, err := e.store.Intend(e.ctx, scope, h, IntentSpec{Key: "pub/" + repo + ref + short(head), Operation: OpForgePublish, |
| 67 | Payload: mustJSON(publication{Repository: repo, Ref: ref, Head: head})}) |
| 68 | return err |
| 69 | } |
| 70 | wantCode(t, try("example/other", "team/fix-sum", cand.Hash), CodeScope) |
| 71 | wantCode(t, try("example/shop", "main", cand.Hash), CodeScope) |
| 72 | wantCode(t, try("example/shop", "team/fix-sum", hashBytes([]byte("something else"))), CodeEvidence) |
| 73 | _, err = e.store.Intend(e.ctx, scope, h, IntentSpec{Key: "pub/x", Operation: OpForgePublish, Payload: []byte(`{"repository":"example/shop","ref":"team/fix-sum","head":"` + cand.Hash + `","force":true}`)}) |
| 74 | wantCode(t, err, CodeInvalid) |
| 75 | must(t, try("example/shop", "team/fix-sum", cand.Hash)) |
| 76 | acts, _ := e.store.Actions(e.ctx, scope) |
| 77 | if len(acts) != 3 { // implement, review and the accepted publication |
| 78 | t.Fatalf("%d actions", len(acts)) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func TestLostReplyNeverReplaysBlind(t *testing.T) { |
| 83 | e := newEnv(t) |
| 84 | _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 85 | h := e.claim(scope, "reproduce", "runner-a", "ws-1") |
| 86 | // Paid inference (unsafe class): a lost reply stays uncertain with no |
| 87 | // definitive answer, keeps its reservation and parks the job. |
| 88 | e.impl.SetLoseReply(true) |
| 89 | id, err := e.store.Intend(e.ctx, scope, h, intent("call", Usage{InputTokens: 100, OutputTokens: 50})) |
| 90 | must(t, err) |
| 91 | _, err = e.store.Dispatch(e.ctx, scope, id) |
| 92 | wantCode(t, err, CodeUncertain) |
| 93 | if e.impl.Calls() != 1 { |
| 94 | t.Fatalf("provider ran %d times", e.impl.Calls()) |
| 95 | } |
| 96 | _, err = e.store.Reconcile(e.ctx, scope, id) |
| 97 | wantCode(t, err, CodeUncertain) |
| 98 | _, err = e.store.Dispatch(e.ctx, scope, id) |
| 99 | wantCode(t, err, CodeState) |
| 100 | _, err = e.store.Intend(e.ctx, scope, h, intent("call", Usage{InputTokens: 100, OutputTokens: 50})) |
| 101 | wantCode(t, err, CodeUncertain) |
| 102 | if j := e.job(scope); j.State != JobWaiting || j.Wait != WaitUncertainOutcome { |
| 103 | t.Fatalf("job %s/%s", j.State, j.Wait) |
| 104 | } |
| 105 | led, _ := e.store.Ledger(e.ctx, scope) |
| 106 | if led.Spent.Calls != 1 { |
| 107 | t.Fatalf("ledger %+v", led) |
| 108 | } |
| 109 | if e.impl.Calls() != 1 { |
| 110 | t.Fatalf("provider ran %d times after reconcile attempts", e.impl.Calls()) |
| 111 | } |
| 112 | // A job waiting on an uncertain outcome is not claimable either. |
| 113 | _, err = e.store.Claim(e.ctx, scope, "reproduce", "runner-b", "ws-2") |
| 114 | wantCode(t, err, CodeState) |
| 115 | // The runner cannot finish around it. |
| 116 | wantCode(t, e.store.Complete(e.ctx, scope, h, AttemptResult{Outcome: OutcomeSuccess}), CodeState) |
| 117 | wantCode(t, e.store.Release(e.ctx, scope, h, WaitProviderLimit, "x"), CodeState) |
| 118 | // Cancel reports it in flight; the late receipt still settles. |
| 119 | rep, err := e.store.Cancel(e.ctx, scope, "paul", "give up") |
| 120 | must(t, err) |
| 121 | if len(rep.InFlight) != 1 { |
| 122 | t.Fatalf("cancel %+v", rep) |
| 123 | } |
| 124 | must(t, e.store.Record(e.ctx, scope, id, Receipt{ID: "late", Outcome: OutcomeSuccess, Provider: "fake-a", Usage: &Usage{Calls: 1, InputTokens: 10, OutputTokens: 5, CostMicros: 7}})) |
| 125 | led, _ = e.store.Ledger(e.ctx, scope) |
| 126 | if led.Reserved.Calls != 0 || led.Spent.CostMicros != 2050 { |
| 127 | t.Fatalf("ledger %+v", led) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func TestReconcileWithoutReceiptNeverRedispatches(t *testing.T) { |
| 132 | e := newEnv(t) |
| 133 | _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 134 | h := e.claim(scope, "reproduce", "runner-a", "ws-1") |
| 135 | id, err := e.store.Intend(e.ctx, scope, h, intent("lost", Usage{InputTokens: 10, OutputTokens: 10})) |
| 136 | must(t, err) |
| 137 | _, err = e.store.Admit(e.ctx, scope, id) |
| 138 | must(t, err) |
| 139 | must(t, e.store.Lost(e.ctx, scope, id, "reply lost")) |
| 140 | _, err = e.store.Reconcile(e.ctx, scope, id) |
| 141 | wantCode(t, err, CodeUncertain) |
| 142 | _, err = e.store.Dispatch(e.ctx, scope, id) |
| 143 | wantCode(t, err, CodeState) |
| 144 | _, err = e.store.Cancel(e.ctx, scope, "operator", "stop") |
| 145 | must(t, err) |
| 146 | rows, err := e.store.Reservations(e.ctx, scope) |
| 147 | must(t, err) |
| 148 | if len(rows) != 1 || rows[0].State != ReservationSettled || rows[0].Settled != rows[0].Reserved { |
| 149 | t.Fatalf("lost usage was released: %+v", rows) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | func TestCancelSerialisesWithAdmission(t *testing.T) { |
| 154 | e := newEnv(t) |
| 155 | _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 156 | h := e.claim(scope, "reproduce", "runner-a", "ws-1") |
| 157 | id, err := e.store.Intend(e.ctx, scope, h, intent("a", Usage{InputTokens: 1, OutputTokens: 1})) |
| 158 | must(t, err) |
| 159 | // Cancel between the authority answer and the admission transaction: |
| 160 | // the stored revision moved, so the admission is refused. |
| 161 | e.store.auth = authorityHook{inner: e.auth, before: func() { |
| 162 | if _, err := e.store.Cancel(e.ctx, scope, "paul", "race"); err != nil { |
| 163 | t.Error(err) |
| 164 | } |
| 165 | }} |
| 166 | _, err = e.store.Admit(e.ctx, scope, id) |
| 167 | if c := CodeOf(err); c != CodeRevision && c != CodeState { |
| 168 | t.Fatalf("admit after racing cancel: %v", err) |
| 169 | } |
| 170 | a, _ := e.store.GetAction(e.ctx, scope, id) |
| 171 | if a.State != ActionRevoked { |
| 172 | t.Fatalf("action %s", a.State) |
| 173 | } |
| 174 | if e.impl.Calls() != 0 { |
| 175 | t.Fatal("provider ran") |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // authorityHook runs before between introspection and the caller's |
| 180 | // transaction, to open the race window on purpose. |
| 181 | type authorityHook struct { |
| 182 | inner Authority |
| 183 | before func() |
| 184 | } |
| 185 | |
| 186 | func (a authorityHook) Introspect(ctx context.Context, req AdmissionRequest) (Admission, error) { |
| 187 | adm, err := a.inner.Introspect(ctx, req) |
| 188 | a.before() |
| 189 | return adm, err |
| 190 | } |
| 191 | |
| 192 | func TestRevokedGrantAtAdmission(t *testing.T) { |
| 193 | e := newEnv(t) |
| 194 | _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 195 | h := e.claim(scope, "reproduce", "runner-a", "ws-1") |
| 196 | id, err := e.store.Intend(e.ctx, scope, h, intent("a", Usage{InputTokens: 1, OutputTokens: 1})) |
| 197 | must(t, err) |
| 198 | e.auth.Revoke(grant) |
| 199 | _, err = e.store.Admit(e.ctx, scope, id) |
| 200 | wantCode(t, err, CodeGrantRevoked) |
| 201 | if j := e.job(scope); j.State != JobCancelled { |
| 202 | t.Fatalf("job %s", j.State) |
| 203 | } |
| 204 | a, _ := e.store.GetAction(e.ctx, scope, id) |
| 205 | if a.State != ActionRevoked { |
| 206 | t.Fatalf("action %s", a.State) |
| 207 | } |
| 208 | led, _ := e.store.Ledger(e.ctx, scope) |
| 209 | if led.Reserved != (Usage{}) { |
| 210 | t.Fatalf("ledger %+v", led) |
| 211 | } |
| 212 | } |
| 213 | |