oficina / equipo Public

fix: persist authority denials outside rejected runner transactions

codex agent, operated by paul@keymail.dev pushed by claude agent, operated by paul@keymail.dev 74acd92eb6e97bba4d06623c3468951f8ba2078a parent 6d854f0
6 files changed, +114 −3
  • internal/equipo/team/actions.go +1 −1
  • internal/equipo/team/admission.go +9 −1
  • internal/equipo/team/authority_denial.go +43 −0
  • internal/equipo/team/final_atomic_authority_test.go +57 −0
  • internal/equipo/team/scheduler.go +1 −1
  • internal/equipo/team/stop.go +3 −0
diff --git a/internal/equipo/team/actions.go b/internal/equipo/team/actions.go
index 52acb4b6..f8588082 100644
--- a/internal/equipo/team/actions.go
+++ b/internal/equipo/team/actions.go
@@ -343,7 +343,7 @@ func (s *Store) Admit(ctx context.Context, scope Scope, id ActionID) (Dispatch,
adm, err := s.auth.Introspect(ctx, AdmissionRequest{Grant: job.Grant.Ref, Account: job.Account, Project: job.Project, Target: job.Target, Operation: pre.Operation})
if err != nil {
if errors.Is(err, ErrGrantRevoked) {
- s.revoke(ctx, scope, "admit: "+err.Error())
+ return Dispatch{}, s.observedRevocation(ctx, scope, err)
}
return Dispatch{}, err
}
diff --git a/internal/equipo/team/admission.go b/internal/equipo/team/admission.go
index 4acec03a..01fec7b0 100644
--- a/internal/equipo/team/admission.go
+++ b/internal/equipo/team/admission.go
@@ -52,9 +52,14 @@ func (s *Store) Atomic(ctx context.Context, fn func(context.Context, *sql.Tx) er
if tx := s.bound(ctx); tx != nil {
return fn(ctx, tx)
}
- return s.WithTransaction(ctx, func(tx *sql.Tx) error {
+ err := s.WithTransaction(ctx, func(tx *sql.Tx) error {
return fn(context.WithValue(ctx, transactionKey{}, transactionBinding{s, tx}), tx)
})
+ var denial *authorityDenial
+ if errors.As(err, &denial) {
+ return s.commitAuthorityDenial(ctx, denial)
+ }
+ return err
}
func (s *Store) AdmitOnce(ctx context.Context, scope Scope, h Handle, id ActionID, requestID string) (AdmissionTicket, error) {
@@ -126,6 +131,9 @@ func (s *Store) AdmitOnce(ctx context.Context, scope Scope, h Handle, id ActionI
}
adm, err := s.auth.Introspect(ctx, AdmissionRequest{Grant: hd.job.Grant.Ref, Account: hd.job.Account, Project: hd.job.Project, Target: hd.job.Target, Operation: a.Operation})
if err != nil {
+ return s.observedRevocation(ctx, scope, err)
+ }
+ if err := s.fenceObserved(ctx, hd.job.Grant, adm); err != nil {
return err
}
if err := checkAdmission(hd.job, adm, s.clock.Now()); err != nil {
diff --git a/internal/equipo/team/authority_denial.go b/internal/equipo/team/authority_denial.go
new file mode 100644
index 00000000..486bbaa2
--- /dev/null
+++ b/internal/equipo/team/authority_denial.go
@@ -0,0 +1,43 @@
+package team
+
+import (
+ "context"
+ "database/sql"
+ "errors"
+ "time"
+)
+
+type authorityDenial struct {
+ cause error
+ change *GrantChange
+ scope Scope
+}
+
+func (e *authorityDenial) Error() string { return e.cause.Error() }
+func (e *authorityDenial) Unwrap() error { return e.cause }
+func (s *Store) observedRevocation(ctx context.Context, scope Scope, err error) error {
+ if !errors.Is(err, ErrGrantRevoked) {
+ return err
+ }
+ denial := &authorityDenial{cause: err, scope: scope}
+ if s.bound(ctx) != nil {
+ return denial
+ }
+ return s.commitAuthorityDenial(ctx, denial)
+}
+func (s *Store) commitAuthorityDenial(ctx context.Context, d *authorityDenial) error {
+ cleanup, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
+ defer cancel()
+ err := s.WithTransaction(cleanup, func(tx *sql.Tx) error {
+ if d.change != nil {
+ return s.FenceGrantsTx(cleanup, tx, []GrantChange{*d.change})
+ }
+ j, err := loadJob(cleanup, tx, d.scope)
+ if err != nil || j.State.Terminal() {
+ return err
+ }
+ _, err = terminate(cleanup, tx, j, JobCancelled, "authority", "grant revoked", s.clock.Now())
+ return err
+ })
+ return errors.Join(d.cause, err)
+}
diff --git a/internal/equipo/team/final_atomic_authority_test.go b/internal/equipo/team/final_atomic_authority_test.go
new file mode 100644
index 00000000..6fa3e587
--- /dev/null
+++ b/internal/equipo/team/final_atomic_authority_test.go
@@ -0,0 +1,57 @@
+package team
+
+import (
+ "context"
+ "database/sql"
+ "errors"
+ "testing"
+)
+
+func TestAtomicAuthorityDenialPersists(t *testing.T) {
+ for _, op := range []string{"renew", "admit"} {
+ for _, revoked := range []bool{false, true} {
+ t.Run(op+map[bool]string{false: "/stale", true: "/revoked"}[revoked], func(t *testing.T) {
+ e := newEnv(t)
+ _, scope := e.createJob(softwareSpec(SoftwareSteps(false)))
+ h := e.claim(scope, "reproduce", "runner", "ws")
+ id, err := e.store.Intend(e.ctx, scope, h, intent("turn", Usage{InputTokens: 1}))
+ must(t, err)
+ want := ErrGrantStale
+ if revoked {
+ e.auth.Revoke(grant)
+ want = ErrGrantRevoked
+ } else {
+ e.auth.Amend(grant)
+ }
+ err = e.store.Atomic(e.ctx, func(ctx context.Context, tx *sql.Tx) error {
+ if _, err := tx.ExecContext(ctx, "UPDATE jobs SET outcome='must rollback' WHERE id=?", scope.Job); err != nil {
+ return err
+ }
+ if op == "renew" {
+ _, err := e.store.Renew(ctx, scope, h)
+ return err
+ }
+ _, err := e.store.Admit(ctx, scope, id)
+ return err
+ })
+ if !errors.Is(err, want) {
+ t.Fatal("wrong denial", err)
+ }
+ var stale int
+ var state, outcome string
+ must(t, e.store.db.QueryRow("SELECT grant_stale,state,outcome FROM jobs WHERE id=?", scope.Job).Scan(&stale, &state, &outcome))
+ if outcome == "must rollback" {
+ t.Fatal("denial committed unrelated writes")
+ }
+ if revoked && state != "cancelled" || !revoked && stale != 1 {
+ t.Fatal("denial rolled back authority effect", state, stale)
+ }
+ var attempt string
+ must(t, e.store.db.QueryRow("SELECT state FROM attempts WHERE id=?", h.Attempt).Scan(&attempt))
+ if attempt != "stopping" {
+ t.Fatal("denial did not stop attempt", attempt)
+ }
+ })
+ }
+ }
+}
diff --git a/internal/equipo/team/scheduler.go b/internal/equipo/team/scheduler.go
index 69875f37..38c43e9a 100644
--- a/internal/equipo/team/scheduler.go
+++ b/internal/equipo/team/scheduler.go
@@ -227,7 +227,7 @@ func (s *Store) Renew(ctx context.Context, scope Scope, h Handle) (time.Time, er
adm, err := s.auth.Introspect(ctx, AdmissionRequest{Grant: job.Grant.Ref, Account: job.Account, Project: job.Project, Target: job.Target, Operation: OpAttemptContinue})
if err != nil {
if errors.Is(err, ErrGrantRevoked) {
- s.revoke(ctx, scope, "renew: "+err.Error())
+ return time.Time{}, s.observedRevocation(ctx, scope, err)
}
return time.Time{}, err
}
diff --git a/internal/equipo/team/stop.go b/internal/equipo/team/stop.go
index a9a432b1..6bd3fea1 100644
--- a/internal/equipo/team/stop.go
+++ b/internal/equipo/team/stop.go
@@ -236,6 +236,9 @@ func (s *Store) fenceObserved(ctx context.Context, grant GrantSnapshot, adm Admi
if adm.Version == grant.Version && adm.Epoch == grant.Epoch {
return nil
}
+ if s.bound(ctx) != nil {
+ return &authorityDenial{cause: ErrGrantStale, change: &GrantChange{Ref: adm.Grant, Version: adm.Version, Epoch: adm.Epoch}}
+ }
if err := s.WithTransaction(ctx, func(tx *sql.Tx) error {
return s.FenceGrantsTx(ctx, tx, []GrantChange{{Ref: adm.Grant, Version: adm.Version, Epoch: adm.Epoch}})
}); err != nil {