| 1 | package team |
| 2 | |
| 3 | import ( |
| 4 | "database/sql" |
| 5 | "errors" |
| 6 | "math" |
| 7 | "sync" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func coordinatorSpec() JobSpec { |
| 12 | p := testPolicy() |
| 13 | return JobSpec{Account: acctA, Requester: "paul", GrantRef: grant, Target: TargetBinding{Kind: TargetConversation, Subject: "paul"}, Policy: p, Steps: []StepSpec{{Key: "coordinate", Capability: CapCoordinate, Version: 1}}} |
| 14 | } |
| 15 | func coordinatorLimits() Limits { |
| 16 | return Limits{Calls: 20, InputTokens: 10000, OutputTokens: 10000, Seconds: 10000} |
| 17 | } |
| 18 | func configureCoordinator(t *testing.T, e *env) { |
| 19 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, periodSet())) |
| 20 | must(t, e.store.ConfigureCoordinatorLimits(e.ctx, acctA, "paul", 0, coordinatorLimits())) |
| 21 | } |
| 22 | func TestCoordinatorNoProjectBudgetBypass(t *testing.T) { |
| 23 | if operationAllowed(CapCoordinate, OpForgePublish) { |
| 24 | t.Fatal("coordinate capability permits forge") |
| 25 | } |
| 26 | e := newEnv(t) |
| 27 | configureCoordinator(t, e) |
| 28 | ordinary := softwareSpec(SoftwareSteps(false)) |
| 29 | if _, err := e.store.CreateJob(e.ctx, ordinary); err == nil { |
| 30 | t.Fatal("ordinary Project=0 admitted") |
| 31 | } |
| 32 | spec := coordinatorSpec() |
| 33 | _, s := e.createJob(spec) |
| 34 | h := e.claim(s, "coordinate", "runner", "coordinator") |
| 35 | if _, err := e.store.Intend(e.ctx, s, h, IntentSpec{Key: "forge", Operation: OpForgePublish, Payload: []byte(`{}`)}); err == nil { |
| 36 | t.Fatal("conversation gained forge authority") |
| 37 | } |
| 38 | spec.Target.Repository = "repo" |
| 39 | if _, err := e.store.CreateJob(e.ctx, spec); err == nil { |
| 40 | t.Fatal("conversation gained repository context") |
| 41 | } |
| 42 | spec = coordinatorSpec() |
| 43 | spec.Steps[0].Capability = CapImplement |
| 44 | if _, err := e.store.CreateJob(e.ctx, spec); err == nil { |
| 45 | t.Fatal("conversation gained implement capability") |
| 46 | } |
| 47 | spec = coordinatorSpec() |
| 48 | spec.Project = 1 |
| 49 | if _, err := e.store.CreateJob(e.ctx, spec); err == nil { |
| 50 | t.Fatal("conversation used project budget") |
| 51 | } |
| 52 | if err := e.store.Complete(e.ctx, s, h, AttemptResult{Outcome: OutcomeSuccess}); err == nil { |
| 53 | t.Fatal("process completion proved delivery") |
| 54 | } |
| 55 | } |
| 56 | func TestCoordinatorBudgetSharesTeam(t *testing.T) { |
| 57 | e := newEnv(t) |
| 58 | b := periodSet() |
| 59 | b.Team.Calls = 1 |
| 60 | for id, p := range b.Projects { |
| 61 | p.Calls = 1 |
| 62 | b.Projects[id] = p |
| 63 | } |
| 64 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, b)) |
| 65 | l := coordinatorLimits() |
| 66 | l.Calls = 1 |
| 67 | must(t, e.store.ConfigureCoordinatorLimits(e.ctx, acctA, "paul", 0, l)) |
| 68 | _, s := e.createJob(coordinatorSpec()) |
| 69 | h := e.claim(s, "coordinate", "runner", "coordinator") |
| 70 | p, ph := periodJob(e, 1) |
| 71 | var wg sync.WaitGroup |
| 72 | results := make(chan error, 2) |
| 73 | for _, v := range []struct { |
| 74 | s Scope |
| 75 | h Handle |
| 76 | }{{s, h}, {p, ph}} { |
| 77 | wg.Add(1) |
| 78 | go func() { |
| 79 | defer wg.Done() |
| 80 | _, err := e.store.Intend(e.ctx, v.s, v.h, intent("turn", Usage{InputTokens: 1, OutputTokens: 1})) |
| 81 | results <- err |
| 82 | }() |
| 83 | } |
| 84 | wg.Wait() |
| 85 | close(results) |
| 86 | successes := 0 |
| 87 | for err := range results { |
| 88 | if err == nil { |
| 89 | successes++ |
| 90 | } else if !errors.Is(err, ErrBudget) { |
| 91 | t.Fatal(err) |
| 92 | } |
| 93 | } |
| 94 | if successes != 1 { |
| 95 | t.Fatalf("shared last reservation: %d successes", successes) |
| 96 | } |
| 97 | if periodUsage(e, 0).Reserved.Calls != 1 { |
| 98 | t.Fatal("team omitted coordinator reservation") |
| 99 | } |
| 100 | } |
| 101 | func TestCoordinatorBudgetUnknownRollover(t *testing.T) { |
| 102 | e := newEnv(t) |
| 103 | configureCoordinator(t, e) |
| 104 | _, s := e.createJob(coordinatorSpec()) |
| 105 | h := e.claim(s, "coordinate", "runner", "coordinator") |
| 106 | id, err := e.store.Intend(e.ctx, s, h, intent("turn", Usage{InputTokens: 10, OutputTokens: 5})) |
| 107 | must(t, err) |
| 108 | _, err = e.store.Admit(e.ctx, s, id) |
| 109 | must(t, err) |
| 110 | must(t, e.store.Lost(e.ctx, s, id, "unknown")) |
| 111 | _, err = e.store.Cancel(e.ctx, s, "paul", "cancel") |
| 112 | must(t, err) |
| 113 | must(t, e.store.ContainUntrusted(e.ctx, s, h)) |
| 114 | u, err := e.store.CoordinatorUsage(e.ctx, acctA, "paul", e.clock.Now()) |
| 115 | must(t, err) |
| 116 | if u.Spent.Calls != 1 || u.Spent.InputTokens != 10 || u.Spent.Seconds != 1200 { |
| 117 | t.Fatalf("unknown/cancelled exposure refunded: %+v", u) |
| 118 | } |
| 119 | u, err = e.store.CoordinatorUsage(e.ctx, acctA, "paul", e.clock.Now().AddDate(0, 1, 0)) |
| 120 | must(t, err) |
| 121 | if u != (PeriodUsage{}) { |
| 122 | t.Fatal("old charge leaked into next period", u) |
| 123 | } |
| 124 | must(t, e.store.tx(e.ctx, func(tx *sql.Tx) error { |
| 125 | _, err := tx.Exec("UPDATE reservations SET s_input=?", math.MaxInt64) |
| 126 | return err |
| 127 | })) |
| 128 | _, s2 := e.createJob(coordinatorSpec()) |
| 129 | _, err = e.store.Claim(e.ctx, s2, "coordinate", "runner", "coordinator-2") |
| 130 | if !errors.Is(err, ErrOverflow) && !errors.Is(err, ErrBudget) { |
| 131 | t.Fatal("overflow admitted", err) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | func TestCoordinatorMigrationCoreUpgrade(t *testing.T) { |
| 136 | e := newEnv(t) |
| 137 | must(t, e.store.tx(e.ctx, func(tx *sql.Tx) error { |
| 138 | _, err := tx.Exec("DROP TABLE coordinator_limits") |
| 139 | if err != nil { |
| 140 | return err |
| 141 | } |
| 142 | _, err = tx.Exec("UPDATE service SET schema_version=7") |
| 143 | return err |
| 144 | })) |
| 145 | must(t, e.store.Close()) |
| 146 | e.store = nil |
| 147 | e.open() |
| 148 | var version int |
| 149 | must(t, e.store.db.QueryRow("SELECT schema_version FROM service").Scan(&version)) |
| 150 | if version != 8 { |
| 151 | t.Fatal("core upgrade did not advance version", version) |
| 152 | } |
| 153 | configureCoordinator(t, e) |
| 154 | must(t, e.store.Close()) |
| 155 | e.store = nil |
| 156 | e.open() |
| 157 | l, rev, err := e.store.CoordinatorLimits(e.ctx, acctA, "paul") |
| 158 | must(t, err) |
| 159 | if rev != 1 || l != coordinatorLimits() { |
| 160 | t.Fatal("core reopen lost limits") |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | func TestCoordinatorBudgetPersonCeiling(t *testing.T) { |
| 165 | e := newEnv(t) |
| 166 | configureCoordinator(t, e) |
| 167 | l := coordinatorLimits() |
| 168 | l.Calls = 1 |
| 169 | must(t, e.store.ConfigureCoordinatorLimits(e.ctx, acctA, "paul", 1, l)) |
| 170 | _, scope := e.createJob(coordinatorSpec()) |
| 171 | h := e.claim(scope, "coordinate", "runner", "first") |
| 172 | _, err := e.store.Intend(e.ctx, scope, h, intent("one", Usage{InputTokens: 1, OutputTokens: 1})) |
| 173 | must(t, err) |
| 174 | _, err = e.store.Intend(e.ctx, scope, h, intent("two", Usage{InputTokens: 1, OutputTokens: 1})) |
| 175 | wantCode(t, err, CodeBudget) |
| 176 | } |
| 177 | |
| 178 | func TestCoordinatorBudgetSharesTeamProjectFirst(t *testing.T) { |
| 179 | e := newEnv(t) |
| 180 | b := periodSet() |
| 181 | b.Team.Calls = 1 |
| 182 | for id, p := range b.Projects { |
| 183 | p.Calls = 1 |
| 184 | b.Projects[id] = p |
| 185 | } |
| 186 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, b)) |
| 187 | l := coordinatorLimits() |
| 188 | l.Calls = 1 |
| 189 | must(t, e.store.ConfigureCoordinatorLimits(e.ctx, acctA, "paul", 0, l)) |
| 190 | project, ph := periodJob(e, 1) |
| 191 | _, scope := e.createJob(coordinatorSpec()) |
| 192 | h := e.claim(scope, "coordinate", "runner", "coordinator") |
| 193 | _, err := e.store.Intend(e.ctx, project, ph, intent("project-first", Usage{InputTokens: 1, OutputTokens: 1})) |
| 194 | must(t, err) |
| 195 | _, err = e.store.Intend(e.ctx, scope, h, intent("coordinator-second", Usage{InputTokens: 1, OutputTokens: 1})) |
| 196 | wantCode(t, err, CodeBudget) |
| 197 | } |
| 198 | |
| 199 | func TestCoordinatorNoProjectBudgetBypassRunnerPolicy(t *testing.T) { |
| 200 | e := newEnv(t) |
| 201 | spec := softwareSpec(SoftwareSteps(false)) |
| 202 | spec.Policy = runnerPolicy(t, "subscription") |
| 203 | _, err := e.store.CreateJob(e.ctx, spec) |
| 204 | wantCode(t, err, CodeBudget) |
| 205 | spec.Project = 1 |
| 206 | _, err = e.store.CreateJob(e.ctx, spec) |
| 207 | wantCode(t, err, CodeBudget) |
| 208 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, periodSet())) |
| 209 | _, err = e.store.CreateJob(e.ctx, spec) |
| 210 | must(t, err) |
| 211 | } |
| 212 | |
| 213 | func TestCoordinatorBudgetOverflow(t *testing.T) { |
| 214 | e := newEnv(t) |
| 215 | configureCoordinator(t, e) |
| 216 | _, scope := e.createJob(coordinatorSpec()) |
| 217 | h := e.claim(scope, "coordinate", "runner", "overflow") |
| 218 | _, err := e.store.Intend(e.ctx, scope, h, intent("one", Usage{InputTokens: 1, OutputTokens: 1})) |
| 219 | must(t, err) |
| 220 | must(t, e.store.tx(e.ctx, func(tx *sql.Tx) error { |
| 221 | _, err := tx.Exec("UPDATE reservations SET r_input=?", math.MaxInt64) |
| 222 | if err != nil { |
| 223 | return err |
| 224 | } |
| 225 | _, err = tx.Exec(`INSERT INTO reservations(account,job_id,step_id,attempt_id,action_id,r_calls,r_input,r_output,r_cost,s_calls,s_input,s_output,s_cost,state,receipt_id,settled_by,created_at,settled_at,project_id,period,cost_unavailable) SELECT account,job_id,step_id,attempt_id,action_id,1,1,1,0,0,0,0,0,'open','','',created_at,'',project_id,period,cost_unavailable FROM reservations LIMIT 1`) |
| 226 | return err |
| 227 | })) |
| 228 | _, err = e.store.CoordinatorUsage(e.ctx, acctA, "paul", e.clock.Now()) |
| 229 | wantCode(t, err, CodeOverflow) |
| 230 | _, err = e.store.PeriodUsage(e.ctx, acctA, 0, e.clock.Now()) |
| 231 | wantCode(t, err, CodeOverflow) |
| 232 | } |
| 233 | |