console: count app-gone release failures; reuse equal saved contacts
A failed release on the app-gone path now bumps Attempts with the same CAS move the attach path uses, records reason release_failed, and pages the operator once at the tenth attempt naming the release that failed. A purchase started with a new contact equal field for field to one the account has saved uses that saved contact instead of adding a duplicate, freezing its version and registrar handle as a saved selection does.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+136
−8
internal/console/domainbuy.go+35 −6internal/console/domainbuy_account_test.go+101 −2
CI failed — run details
diff --git a/internal/console/domainbuy.go b/internal/console/domainbuy.go| index 07e10186b..af561cf8a 100644 |
| --- a/internal/console/domainbuy.go |
| +++ b/internal/console/domainbuy.go |
| @@ -280,8 +280,9 @@ func (s *Server) startDomainPurchase(ctx context.Context, acct *Account, app, na |
| // that is gone or being deleted writes nothing. So a purchase row either |
| // exists before deletion checks for one, or is never written. |
| // |
| -// The purchase freezes its contact (R12): a new contact is saved first and |
| -// the purchase takes that copy; a saved one is copied with its version and, |
| +// The purchase freezes its contact (R12): a new contact is saved first |
| +// (unless an equal one is already saved, which is used instead) and the |
| +// purchase takes that copy; a saved one is copied with its version and, |
| // when it has one, its registrar handle. Later steps read only the copy. |
| func (s *Server) insertDomainPurchaseLocked(ctx context.Context, acct *Account, app, principal string, q domainreg.Quote, c registrar.Contact, saved *SavedContact) (*DomainPurchase, error) { |
| if domainPurchaseBeforeInsert != nil { |
| @@ -304,6 +305,21 @@ func (s *Server) insertDomainPurchaseLocked(ctx context.Context, acct *Account, |
| } |
| details := purchaseDetails{Principal: principal, Contact: c} |
| added := false |
| + if saved == nil { |
| + // A new contact equal, field for field, to a saved one is that one: |
| + // the purchase freezes its version and handle instead of saving a |
| + // duplicate. |
| + contacts, err := s.store.SavedContacts(ctx, acct.ID) |
| + if err != nil { |
| + return nil, err |
| + } |
| + for i := range contacts { |
| + if contacts[i].Contact == c { |
| + saved = &contacts[i] |
| + break |
| + } |
| + } |
| + } |
| if saved == nil { |
| sc, err := s.store.AddSavedContact(ctx, acct.ID, c, now) |
| if err != nil { |
| @@ -763,13 +779,26 @@ func (s *Server) capturedPurchase(ctx context.Context, acct *Account, p *DomainP |
| // claims and any first-route record behind them, through detach's own |
| // removal, which never touches another holder's claim. Then it unpoints the |
| // zone and finishes with no app. Any failure leaves the purchase captured |
| -// for the next pass. Buying is off under host claims, so this never runs |
| -// against an armed gate. |
| +// for the next pass; a failed release counts as an attempt, as a failed |
| +// attach does, and the tenth pages the operator. Buying is off under host |
| +// claims, so this never runs against an armed gate. |
| func (s *Server) finishPurchaseUnpointed(ctx context.Context, acct *Account, p *DomainPurchase) error { |
| for _, host := range []string{p.Domain, "www." + p.Domain} { |
| - if ref := s.releaseDomainHost(ctx, acct, p.App, host, p.Principal); ref != nil && ref.Status != http.StatusNotFound { |
| - return fmt.Errorf("console: purchase %s: releasing %s from %s: %d %s", p.ID, host, p.App, ref.Status, ref.Msg) |
| + ref := s.releaseDomainHost(ctx, acct, p.App, host, p.Principal) |
| + if ref == nil || ref.Status == http.StatusNotFound { |
| + continue |
| + } |
| + failed := fmt.Errorf("console: purchase %s: releasing %s from %s: %d %s", p.ID, host, p.App, ref.Status, ref.Msg) |
| + if err := s.movePurchase(ctx, p, p.State, func(p *DomainPurchase) { |
| + p.Reason = "release_failed" |
| + p.Attempts++ |
| + }); err != nil { |
| + return errors.Join(failed, err) |
| + } |
| + if p.Attempts == 10 { |
| + s.purchaseAlert(ctx, acct, p.Domain, p.ID, "release of "+host+" from "+p.App+" failed: "+ref.Msg) |
| } |
| + return failed |
| } |
| if err := s.cfg.ZoneHost.SetManaged(ctx, zonehost.Zone{ID: p.ZoneID, Domain: p.Domain}, zonehost.ManagedSet{}); err != nil { |
| return err |
diff --git a/internal/console/domainbuy_account_test.go b/internal/console/domainbuy_account_test.go| index b2032aead..9542bc7f5 100644 |
| --- a/internal/console/domainbuy_account_test.go |
| +++ b/internal/console/domainbuy_account_test.go |
| @@ -160,15 +160,17 @@ func TestPurchaseAppPurgedMidwayFinishesUnpointed(t *testing.T) { |
| } |
| } |
| -// failClaimDeleteStore fails the first Delete of one key. |
| +// failClaimDeleteStore fails the first Delete of one key, or every one |
| +// when always is set. |
| type failClaimDeleteStore struct { |
| release.Store |
| key string |
| + always bool |
| failed bool |
| } |
| func (s *failClaimDeleteStore) Delete(ctx context.Context, key string) error { |
| - if key == s.key && !s.failed { |
| + if key == s.key && (s.always || !s.failed) { |
| s.failed = true |
| return errors.New("bucket unavailable") |
| } |
| @@ -590,3 +592,100 @@ func (f *purchaseFixture) addContact(c registrar.Contact) error { |
| _, err := f.st.AddSavedContact(context.Background(), f.acct.ID, c, time.Now()) |
| return err |
| } |
| + |
| +// partialAttachTrashed is a captured purchase whose apex attached and whose |
| +// www attach was refused by a foreign claim (Attempts 1), with its app then |
| +// moved to Trash. |
| +func partialAttachTrashed(t *testing.T, f *purchaseFixture) *DomainPurchase { |
| + t.Helper() |
| + ctx := context.Background() |
| + p := f.start(t) |
| + f.authorise(t, p) |
| + f.until(t, p.ID, PurchaseRegistered) |
| + foreign := domains.Claim{Schema: 1, Host: "www." + p.Domain, Account: "foreign", App: "other", CreatedBy: "other"} |
| + if _, _, err := domains.AttachClaim(ctx, f.rel, foreign); err != nil { |
| + t.Fatal(err) |
| + } |
| + f.advance(t, p.ID) |
| + p = f.advance(t, p.ID) |
| + if p.State != PurchaseCaptured || p.Attempts != 1 { |
| + t.Fatalf("partial attach: %+v", p) |
| + } |
| + if err := f.st.TrashAppRow(f.acct.ID, "kass", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + return p |
| +} |
| + |
| +func TestPurchaseAppGoneReleaseFailureAlertsAtTen(t *testing.T) { |
| + f := newPurchaseFixture(t) |
| + p := partialAttachTrashed(t, f) |
| + var alerts []string |
| + pageDomainPurchase = func(_ context.Context, _, _, body string) error { alerts = append(alerts, body); return nil } |
| + failing := &failClaimDeleteStore{Store: f.srv.cfg.Releases, key: domains.ClaimKey(p.Domain), always: true} |
| + f.srv.cfg.Releases = failing |
| + for pass := 2; pass <= 12; pass++ { |
| + if err := f.srv.advancePurchase(context.Background(), p.ID); err == nil { |
| + t.Fatalf("pass %d: a failed release must be an error for the pass", pass) |
| + } |
| + got := f.read(t, p.ID) |
| + if got.State != PurchaseCaptured || got.App != "kass" || got.Attempts != pass || got.Reason != "release_failed" { |
| + t.Fatalf("pass %d: %+v", pass, got) |
| + } |
| + want := 0 |
| + if pass >= 10 { |
| + want = 1 |
| + } |
| + if len(alerts) != want { |
| + t.Fatalf("pass %d: %d alerts %q", pass, len(alerts), alerts) |
| + } |
| + } |
| + if !strings.Contains(alerts[0], "release") || !strings.Contains(alerts[0], p.Domain) { |
| + t.Fatalf("alert does not name the failed release: %q", alerts[0]) |
| + } |
| + if f.sent(billing.PreparedCapture) != 1 { |
| + t.Fatalf("captures: %v", f.fake.Sent) |
| + } |
| + failing.always = false |
| + if p = f.advance(t, p.ID); p.State != PurchaseDone || p.Reason != "app_gone" { |
| + t.Fatalf("finish after the release works: %+v", p) |
| + } |
| +} |
| + |
| +func TestNewContactEqualToSavedReusesIt(t *testing.T) { |
| + f := newPurchaseFixture(t) |
| + ctx := context.Background() |
| + f.reg.Available["other.com"] = registrar.Availability{Available: true, Cost: registrar.Money{Currency: "usd", Minor: 900}} |
| + first := f.startWith(t, "kass", purchaseContactSelection{New: newContact()}) |
| + f.authorise(t, first) |
| + first = f.until(t, first.ID, PurchaseDone) |
| + contacts, err := f.st.SavedContacts(ctx, f.acct.ID) |
| + if err != nil || len(contacts) != 1 || contacts[0].ID != first.ContactID || contacts[0].RegistrarHandle != first.OwnerHandle || first.OwnerHandle == "" { |
| + t.Fatalf("after the first purchase: %+v %v (purchase %+v)", contacts, err, first.purchaseDetails) |
| + } |
| + calls := purchaseCalls(f.reg.Calls, "contact:") |
| + second, url, ref := startQuotedSelection(f.srv, ctx, f.acct, "kass", "other.com", purchaseContactSelection{New: newContact()}, "IE", "buyer@example.test") |
| + if ref != nil || second == nil || url == "" { |
| + t.Fatalf("second start: %+v %q %+v", second, url, ref) |
| + } |
| + if second.ContactID != first.ContactID || second.ContactVersion != 1 || second.OwnerHandle != first.OwnerHandle || second.Contact != purchaseContact() { |
| + t.Fatalf("second froze %+v, want the first's contact %q handle %q", second.purchaseDetails, first.ContactID, first.OwnerHandle) |
| + } |
| + if purchaseCalls(f.reg.Calls, "contact:") != calls { |
| + t.Fatalf("second purchase minted a handle: %v", f.reg.Calls) |
| + } |
| + if contacts, err := f.st.SavedContacts(ctx, f.acct.ID); err != nil || len(contacts) != 1 { |
| + t.Fatalf("saved contacts: %+v %v", contacts, err) |
| + } |
| + // A contact differing in any field is a new one. |
| + other := purchaseContact() |
| + other.Company = "Shop Ltd" |
| + f.reg.Available["third.com"] = registrar.Availability{Available: true, Cost: registrar.Money{Currency: "usd", Minor: 900}} |
| + f.srv.cfg.DomainCatalog.DailyCap = 5 |
| + if _, _, ref := startQuotedSelection(f.srv, ctx, f.acct, "kass", "third.com", purchaseContactSelection{New: &other}, "IE", "buyer@example.test"); ref != nil { |
| + t.Fatalf("third start: %+v", ref) |
| + } |
| + if contacts, err := f.st.SavedContacts(ctx, f.acct.ID); err != nil || len(contacts) != 2 { |
| + t.Fatalf("saved contacts after a different contact: %+v %v", contacts, err) |
| + } |
| +} |