| 1 | package team |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "os" |
| 7 | "testing" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | func TestRecoveryReleaseKeepsUnknownCharged(t *testing.T) { |
| 12 | e := newEnv(t) |
| 13 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, periodSet())) |
| 14 | scope, h := periodJob(e, 1) |
| 15 | id, err := e.store.Intend(e.ctx, scope, h, intent("unknown", Usage{InputTokens: 10})) |
| 16 | must(t, err) |
| 17 | _, err = e.store.Admit(e.ctx, scope, id) |
| 18 | must(t, err) |
| 19 | must(t, e.store.Lost(e.ctx, scope, id, "lost reply")) |
| 20 | must(t, e.store.StopAttempt(e.ctx, scope, h, "recover")) |
| 21 | must(t, e.store.Contain(e.ctx, scope, h.Attempt, "supervisor")) |
| 22 | must(t, e.store.Pause(e.ctx, "unknown work")) |
| 23 | before := periodUsage(e, 1) |
| 24 | snapshot, err := e.store.RecoverySnapshot(e.ctx) |
| 25 | must(t, err) |
| 26 | if err := e.store.ReleaseQuarantine(e.ctx, snapshot.Revision, "owner", "reviewed", snapshot.Hash); err == nil { |
| 27 | t.Fatal("release without reconciliation") |
| 28 | } |
| 29 | _, err = e.store.Resume(e.ctx) |
| 30 | must(t, err) |
| 31 | snapshot, err = e.store.RecoverySnapshot(e.ctx) |
| 32 | must(t, err) |
| 33 | if len(snapshot.Unresolved) != 1 { |
| 34 | t.Fatalf("unknown work missing: %+v", snapshot) |
| 35 | } |
| 36 | must(t, e.store.ReleaseQuarantine(e.ctx, snapshot.Revision, "owner", "reviewed unknown charge", snapshot.Hash)) |
| 37 | _, err = e.store.Resume(e.ctx) |
| 38 | must(t, err) |
| 39 | if got := periodUsage(e, 1); got != before { |
| 40 | t.Fatalf("release refunded unknown work: before=%+v after=%+v", before, got) |
| 41 | } |
| 42 | a, err := e.store.GetAction(e.ctx, scope, id) |
| 43 | must(t, err) |
| 44 | if a.State != ActionUncertain || a.Receipt != nil { |
| 45 | t.Fatalf("release invented result: %+v", a) |
| 46 | } |
| 47 | if _, err := e.store.Admit(e.ctx, scope, id); err == nil { |
| 48 | t.Fatal("release replayed unknown action") |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestContainAfterClockRollback(t *testing.T) { |
| 53 | for _, back := range []time.Duration{5 * time.Second, 30 * time.Second} { |
| 54 | t.Run(back.String(), func(t *testing.T) { |
| 55 | e := newEnv(t) |
| 56 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, periodSet())) |
| 57 | scope, h := periodJob(e, 1) |
| 58 | e.clock.Advance(10 * time.Second) |
| 59 | _, err := e.store.Renew(e.ctx, scope, h) |
| 60 | must(t, err) |
| 61 | wrong := h |
| 62 | wrong.Runner = "other" |
| 63 | wantCode(t, e.store.StopAttempt(e.ctx, scope, wrong, "wrong handle"), CodeFence) |
| 64 | e.clock.Advance(-back) |
| 65 | must(t, e.store.StopAttempt(e.ctx, scope, h, "clock rollback")) |
| 66 | steps, err := e.store.Steps(e.ctx, scope) |
| 67 | must(t, err) |
| 68 | if steps[0].Fence <= h.Fence { |
| 69 | t.Fatal("stop did not advance fence") |
| 70 | } |
| 71 | if _, err := e.store.Renew(e.ctx, scope, h); err == nil { |
| 72 | t.Fatal("stopped handle renewed") |
| 73 | } |
| 74 | must(t, e.store.Contain(e.ctx, scope, h.Attempt, "physical cleanup complete")) |
| 75 | if _, err := e.store.Renew(e.ctx, scope, h); err == nil { |
| 76 | t.Fatal("contained handle renewed") |
| 77 | } |
| 78 | usage := periodUsage(e, 1) |
| 79 | if usage.Spent.Seconds != int64(testPolicy().AttemptSeconds) { |
| 80 | t.Fatalf("rollback refunded unknown elapsed time: %+v", usage) |
| 81 | } |
| 82 | e.clock.Advance(time.Hour) |
| 83 | if got := periodUsage(e, 1); got != usage { |
| 84 | t.Fatalf("read refunded unknown time: %+v", got) |
| 85 | } |
| 86 | }) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func TestRuntimeAuthorityEditFencesOnlyChangedGrants(t *testing.T) { |
| 91 | e := newEnv(t) |
| 92 | e.auth.Issue("grant-b", Grant{Account: acctA, Operations: map[Operation]bool{OpJobContinue: true, OpAttemptContinue: true, OpProviderCall: true}}) |
| 93 | spec := softwareSpec(SoftwareSteps(false)) |
| 94 | _, a := e.createJob(spec) |
| 95 | ha := e.claim(a, "reproduce", "a", "a") |
| 96 | spec.GrantRef = "grant-b" |
| 97 | _, b := e.createJob(spec) |
| 98 | hb := e.claim(b, "reproduce", "b", "b") |
| 99 | ia, err := e.store.Intend(e.ctx, a, ha, intent("a", Usage{})) |
| 100 | must(t, err) |
| 101 | ib, err := e.store.Intend(e.ctx, b, hb, intent("b", Usage{})) |
| 102 | must(t, err) |
| 103 | e.auth.Amend(grant) |
| 104 | must(t, e.store.WithTransaction(e.ctx, func(tx *sql.Tx) error { |
| 105 | return e.store.FenceGrantsTx(e.ctx, tx, []GrantChange{{Ref: grant, Version: 2, Epoch: 1}}) |
| 106 | })) |
| 107 | if _, err := e.store.Renew(e.ctx, a, ha); err == nil { |
| 108 | t.Fatal("changed grant renewed") |
| 109 | } |
| 110 | if _, err := e.store.Admit(e.ctx, a, ia); err == nil { |
| 111 | t.Fatal("changed grant admitted") |
| 112 | } |
| 113 | action, err := e.store.GetAction(e.ctx, a, ia) |
| 114 | must(t, err) |
| 115 | if action.State != ActionRevoked { |
| 116 | t.Fatal("changed grant retained undispatched intent") |
| 117 | } |
| 118 | reservations, err := e.store.Reservations(e.ctx, a) |
| 119 | must(t, err) |
| 120 | if reservations[0].State != ReservationReleased { |
| 121 | t.Fatal("undispatched reservation retained") |
| 122 | } |
| 123 | if _, err := e.store.Claim(e.ctx, a, "reproduce", "new", "new"); err == nil { |
| 124 | t.Fatal("stale grant claimed") |
| 125 | } |
| 126 | _, err = e.store.Renew(e.ctx, b, hb) |
| 127 | must(t, err) |
| 128 | _, err = e.store.Admit(e.ctx, b, ib) |
| 129 | must(t, err) |
| 130 | _, err = e.store.Resume(e.ctx) |
| 131 | must(t, err) |
| 132 | if _, err := e.store.Claim(e.ctx, a, "reproduce", "new", "new"); err == nil { |
| 133 | t.Fatal("resume skipped containment") |
| 134 | } |
| 135 | must(t, e.store.Contain(e.ctx, a, ha.Attempt, "supervisor")) |
| 136 | _, err = e.store.Resume(e.ctx) |
| 137 | must(t, err) |
| 138 | _, err = e.store.Claim(e.ctx, a, "reproduce", "new", "new") |
| 139 | must(t, err) |
| 140 | } |
| 141 | |
| 142 | func TestRuntimeAuthorityObservedDriftFencesBeforeResume(t *testing.T) { |
| 143 | e := newEnv(t) |
| 144 | _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 145 | h := e.claim(scope, "reproduce", "runner", "workspace") |
| 146 | id, err := e.store.Intend(e.ctx, scope, h, intent("pending", Usage{})) |
| 147 | must(t, err) |
| 148 | e.auth.Amend(grant) |
| 149 | _, err = e.store.Renew(e.ctx, scope, h) |
| 150 | wantCode(t, err, CodeGrantStale) |
| 151 | attempts, err := e.store.Attempts(e.ctx, scope) |
| 152 | must(t, err) |
| 153 | if attempts[0].State != AttemptStopping { |
| 154 | t.Fatal("observed drift did not stop attempt") |
| 155 | } |
| 156 | a, err := e.store.GetAction(e.ctx, scope, id) |
| 157 | must(t, err) |
| 158 | if a.State != ActionRevoked { |
| 159 | t.Fatal("observed drift kept undispatched intent") |
| 160 | } |
| 161 | _, err = e.store.Resume(e.ctx) |
| 162 | must(t, err) |
| 163 | if _, err = e.store.Renew(e.ctx, scope, h); err == nil { |
| 164 | t.Fatal("resume revived stale handle") |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | func TestRecoveryReleaseReceiptInvalidatesSnapshot(t *testing.T) { |
| 169 | e := newEnv(t) |
| 170 | _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 171 | h := e.claim(scope, "reproduce", "runner", "workspace") |
| 172 | id, err := e.store.Intend(e.ctx, scope, h, intent("pending", Usage{InputTokens: 10, OutputTokens: 10})) |
| 173 | must(t, err) |
| 174 | _, err = e.store.Admit(e.ctx, scope, id) |
| 175 | must(t, err) |
| 176 | must(t, e.store.Lost(e.ctx, scope, id, "unknown")) |
| 177 | must(t, e.store.StopAttempt(e.ctx, scope, h, "cleanup")) |
| 178 | must(t, e.store.Contain(e.ctx, scope, h.Attempt, "supervisor")) |
| 179 | must(t, e.store.Pause(e.ctx, "recover")) |
| 180 | _, err = e.store.Resume(e.ctx) |
| 181 | must(t, err) |
| 182 | snapshot, err := e.store.RecoverySnapshot(e.ctx) |
| 183 | must(t, err) |
| 184 | must(t, e.store.Record(e.ctx, scope, id, Receipt{ID: "receipt", Outcome: OutcomeSuccess, Usage: &Usage{Calls: 1, InputTokens: 1, CostMicros: 1003}})) |
| 185 | wantCode(t, e.store.ReleaseQuarantine(e.ctx, snapshot.Revision, "owner", "reviewed", snapshot.Hash), CodeRevision) |
| 186 | next, err := e.store.RecoverySnapshot(e.ctx) |
| 187 | must(t, err) |
| 188 | wantCode(t, e.store.ReleaseQuarantine(e.ctx, next.Revision, "owner", "reviewed", next.Hash), CodeState) |
| 189 | } |
| 190 | |
| 191 | func TestContainAfterClockRollbackReopen(t *testing.T) { |
| 192 | e := newEnv(t) |
| 193 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, periodSet())) |
| 194 | scope, h := periodJob(e, 1) |
| 195 | e.clock.Advance(10 * time.Second) |
| 196 | _, err := e.store.Renew(e.ctx, scope, h) |
| 197 | must(t, err) |
| 198 | must(t, e.store.Close()) |
| 199 | e.store = nil |
| 200 | e.clock.Advance(-5 * time.Second) |
| 201 | e.openNoResume() |
| 202 | must(t, e.store.StopAttempt(e.ctx, scope, h, "physical cleanup")) |
| 203 | must(t, e.store.Contain(e.ctx, scope, h.Attempt, "supervisor")) |
| 204 | if got := periodUsage(e, 1); got.Spent.Seconds != int64(testPolicy().AttemptSeconds) { |
| 205 | t.Fatalf("reopen lost clock high water: %+v", got) |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | type recoveryBlockedAdapter struct { |
| 210 | Adapter |
| 211 | entered, release chan struct{} |
| 212 | } |
| 213 | |
| 214 | func (a recoveryBlockedAdapter) Reconcile(ctx context.Context, d Dispatch) (ReconcileResult, error) { |
| 215 | close(a.entered) |
| 216 | <-a.release |
| 217 | return a.Adapter.Reconcile(ctx, d) |
| 218 | } |
| 219 | |
| 220 | func TestRecoveryReleaseSerializesBudgetAndContain(t *testing.T) { |
| 221 | e := newEnv(t) |
| 222 | must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, periodSet())) |
| 223 | scope, h := periodJob(e, 1) |
| 224 | id, err := e.store.Intend(e.ctx, scope, h, intent("blocked", Usage{})) |
| 225 | must(t, err) |
| 226 | _, err = e.store.Admit(e.ctx, scope, id) |
| 227 | must(t, err) |
| 228 | must(t, e.store.Lost(e.ctx, scope, id, "unknown")) |
| 229 | must(t, e.store.StopAttempt(e.ctx, scope, h, "cleanup")) |
| 230 | must(t, e.store.Pause(e.ctx, "reconcile")) |
| 231 | entered, release := make(chan struct{}), make(chan struct{}) |
| 232 | e.store.adapters[OpProviderCall] = recoveryBlockedAdapter{e.store.adapters[OpProviderCall], entered, release} |
| 233 | resumed := make(chan error, 1) |
| 234 | go func() { _, err := e.store.Resume(e.ctx); resumed <- err }() |
| 235 | <-entered |
| 236 | results := make(chan error, 2) |
| 237 | started := make(chan struct{}, 2) |
| 238 | go func() { |
| 239 | started <- struct{}{} |
| 240 | b := periodSet() |
| 241 | b.Revision = 1 |
| 242 | results <- e.store.ConfigureBudgets(e.ctx, acctA, 1, b) |
| 243 | }() |
| 244 | go func() { started <- struct{}{}; results <- e.store.Contain(e.ctx, scope, h.Attempt, "supervisor") }() |
| 245 | <-started |
| 246 | <-started |
| 247 | early := false |
| 248 | select { |
| 249 | case <-results: |
| 250 | early = true |
| 251 | case <-time.After(30 * time.Millisecond): |
| 252 | } |
| 253 | close(release) |
| 254 | must(t, <-resumed) |
| 255 | count := 2 |
| 256 | if early { |
| 257 | count-- |
| 258 | } |
| 259 | for range count { |
| 260 | must(t, <-results) |
| 261 | } |
| 262 | if early { |
| 263 | t.Fatal("budget or containment overlapped reconciliation") |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | func TestRuntimeAuthorityResumeObservesFurtherEdit(t *testing.T) { |
| 268 | e := newEnv(t) |
| 269 | _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 270 | h := e.claim(scope, "reproduce", "runner", "workspace") |
| 271 | e.auth.Amend(grant) |
| 272 | _, err := e.store.Renew(e.ctx, scope, h) |
| 273 | wantCode(t, err, CodeGrantStale) |
| 274 | must(t, e.store.Contain(e.ctx, scope, h.Attempt, "supervisor")) |
| 275 | e.auth.Amend(grant) |
| 276 | _, err = e.store.Resume(e.ctx) |
| 277 | must(t, err) |
| 278 | if got := e.job(scope).Grant.Version; got != 3 { |
| 279 | t.Fatalf("resume retained obsolete grant %d", got) |
| 280 | } |
| 281 | e.claim(scope, "reproduce", "new", "new") |
| 282 | } |
| 283 | |
| 284 | func TestRecoveryReleaseRestoreAndClockRollbackKeepsIntentCharged(t *testing.T) { |
| 285 | e := newEnv(t) |
| 286 | _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 287 | h := e.claim(scope, "reproduce", "runner", "workspace") |
| 288 | e.clock.Advance(10 * time.Second) |
| 289 | id, err := e.store.Intend(e.ctx, scope, h, intent("saved", Usage{InputTokens: 10})) |
| 290 | must(t, err) |
| 291 | backup := e.snapshot() |
| 292 | _, err = e.store.Admit(e.ctx, scope, id) |
| 293 | must(t, err) |
| 294 | must(t, e.store.Close()) |
| 295 | e.store = nil |
| 296 | raw, err := os.ReadFile(backup) |
| 297 | must(t, err) |
| 298 | must(t, os.WriteFile(e.path, raw, 0600)) |
| 299 | e.clock.Advance(-5 * time.Second) |
| 300 | e.openNoResume() |
| 301 | a, err := e.store.GetAction(e.ctx, scope, id) |
| 302 | must(t, err) |
| 303 | if a.State != ActionUncertain { |
| 304 | t.Fatalf("restore plus rollback lost unknown intent: %s", a.State) |
| 305 | } |
| 306 | ledger, err := e.store.Ledger(e.ctx, scope) |
| 307 | must(t, err) |
| 308 | if ledger.Spent.Calls != 1 { |
| 309 | t.Fatalf("restore plus rollback refunded unknown action: %+v", ledger) |
| 310 | } |
| 311 | } |
| 312 | |