package runner import ( "context" "errors" "strconv" "testing" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/runnerproto" ) type recoveryRefusalAPI struct { *coordinatorAPIStub operation string status int refusedID string refusals, claims int offers []runnerproto.CoordinatorTurn } func (a *recoveryRefusalAPI) CoordinatorCall(ctx context.Context, op string, q runnerproto.CoordinatorRequest, out any) error { id := q.TurnID if q.Claim != nil { id = q.Claim.Turn.ID } if op == a.operation && id == a.refusedID { a.refusals++ return &RefusalError{Reason: "coordinator.fenced", Status: a.status} } if op == "poll" { *out.(*[]runnerproto.CoordinatorTurn) = a.offers return nil } if op == "claim" { a.claims++ } return a.coordinatorAPIStub.CoordinatorCall(ctx, op, q, out) } func TestCoordinatorRecoveryCallRefusalKeepsServing(t *testing.T) { for _, op := range []string{"recover", "containment"} { for _, status := range []int{401, 403, 409, 422} { t.Run(op+"/"+strconv.Itoa(status), func(t *testing.T) { ctx := context.Background() s, base, launcher, c := coordinatorServiceFixture(t) api := &recoveryRefusalAPI{coordinatorAPIStub: base, operation: op, status: status, refusedID: c.Turn.ID} s.API = api if op == "recover" { mustJournal(t, s.Journal.saveState(ctx, "coordinator-claim", c.Turn.ID, runnerproto.CoordinatorRequest{TurnID: c.Turn.ID, Revision: c.Turn.Revision})) } else { mustJournal(t, s.Journal.saveState(ctx, "coordinator", c.Turn.ID, coordinatorState{Claim: c})) } for restart := 0; restart < 2; restart++ { path := s.Journal.path mustJournal(t, s.Journal.Close()) var err error s.Journal, err = OpenJournal(ctx, path) mustJournal(t, err) defer s.Journal.Close() launcher.j = s.Journal runSpecialistAfterCoordinator(t, s) if api.refusals != 1 { t.Fatal("repeated recovery refusal", api.refusals) } var states []coordinatorState mustJournal(t, s.Journal.readStates(ctx, "coordinator", &states)) if len(states) != 1 || !states[0].Refused || !states[0].Finished { t.Fatal("recovery refusal not durably retired", states) } if states[0].RefusalOperation != op || states[0].RefusalStatus != status { t.Fatal("refusal operation/status missing", states[0]) } if op == "containment" && (states[0].Containment == nil || states[0].Containment.CheckedAt.IsZero()) { t.Fatal("refused report lost local containment proof") } if err := s.RunTurn(ctx, c); !errors.Is(err, provider.ErrUncertain) { t.Fatal("refused prompt can restart", err) } } if launcher.starts != 0 { t.Fatal("recovery resent prompt", launcher.starts) } // A stale refused offer cannot prevent a later coordinator from running. next := c next.Turn.ID = "next-turn" next.Scope.Job++ next.Handle.Attempt++ next.Admission.Scope, next.Admission.Handle = next.Scope, next.Handle base.c = next api.offers = []runnerproto.CoordinatorTurn{c.Turn, next.Turn} mustJournal(t, s.Poll(ctx)) if api.claims != 1 || launcher.starts != 1 || !base.delivery { t.Fatal("refused offer blocked another coordinator", api.claims, launcher.starts, base.delivery) } }) } } } func TestCoordinatorContainmentCallRefusalOnCleanup(t *testing.T) { s, base, launcher, c := coordinatorServiceFixture(t) api := &recoveryRefusalAPI{coordinatorAPIStub: base, operation: "containment", status: 403, refusedID: c.Turn.ID} s.API = api mustJournal(t, s.RunTurn(context.Background(), c)) mustJournal(t, s.Recover(context.Background())) if api.refusals != 1 || launcher.starts != 1 { t.Fatal("cleanup repeated refused call or prompt", api.refusals, launcher.starts) } } func TestCoordinatorRefusalInterruptedRetirement(t *testing.T) { ctx := context.Background() s, base, _, c := coordinatorServiceFixture(t) api := &recoveryRefusalAPI{coordinatorAPIStub: base, operation: "containment", status: 403, refusedID: c.Turn.ID} s.API = api mustJournal(t, s.Journal.saveState(ctx, "coordinator", c.Turn.ID, coordinatorState{Claim: c})) _, err := s.Journal.db.ExecContext(ctx, `CREATE TRIGGER interrupt_retirement BEFORE INSERT ON journal_supervisor WHEN NEW.kind='coordinator' AND json_extract(NEW.body,'$.Finished')=1 BEGIN SELECT RAISE(ABORT,'interrupted retirement'); END`) mustJournal(t, err) if err := s.Recover(ctx); err == nil { t.Fatal("fixture did not interrupt retirement") } path := s.Journal.path mustJournal(t, s.Journal.Close()) s.Journal, err = OpenJournal(ctx, path) mustJournal(t, err) defer s.Journal.Close() _, err = s.Journal.db.ExecContext(ctx, "DROP TRIGGER interrupt_retirement") mustJournal(t, err) mustJournal(t, s.Recover(ctx)) var states []coordinatorState mustJournal(t, s.Journal.readStates(ctx, "coordinator", &states)) if api.refusals != 1 || len(states) != 1 || !states[0].Finished || !states[0].Refused { t.Fatal("interrupted retirement repeated refused call", api.refusals, states) } } func TestCoordinatorRefusalRequiresLocalContainment(t *testing.T) { s, base, _, c := coordinatorServiceFixture(t) api := &recoveryRefusalAPI{coordinatorAPIStub: base, operation: "containment", status: 403, refusedID: c.Turn.ID} s.API = api s.Contain = func(context.Context, runnerproto.CoordinatorClaim) (runnerproto.ContainmentProof, error) { return runnerproto.ContainmentProof{}, provider.ErrContainment } mustJournal(t, s.Journal.saveState(context.Background(), "coordinator", c.Turn.ID, coordinatorState{Claim: c})) if err := s.Recover(context.Background()); !errors.Is(err, provider.ErrContainment) { t.Fatal("local containment failure ignored", err) } var states []coordinatorState mustJournal(t, s.Journal.readStates(context.Background(), "coordinator", &states)) if api.refusals != 0 || states[0].Finished || states[0].Refused { t.Fatal("uncontained turn retired") } }