carlos / platform Public

console: refuse a held platform ref before finishing other promises

Task 8 fix round 3. Create finishes the requested ref's own pending
promise alone, before the platform's others, so a create refused for a
held ref writes nothing (Astra). The held-ref test asserts again that
finishing a held promise writes no manifest carrying its mint id
(Fable, the A3 witness), and a helper comment names its function.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 573ddb146f53e2c01cc45113eb547a95b2717c97 parent 6206f11
2 files changed, +95 −15
  • internal/console/apiplatformcustomers.go +23 −12
  • internal/console/apiplatformcustomers_test.go +72 −3
diff --git a/internal/console/apiplatformcustomers.go b/internal/console/apiplatformcustomers.go
index 32f67ab50..2fb235c78 100644
--- a/internal/console/apiplatformcustomers.go
+++ b/internal/console/apiplatformcustomers.go
@@ -370,8 +370,10 @@ type createPlatformCustomerRequest struct {
// platform and its grant under it: a platform deleted or revoked while
// this request waited gets nothing, and the plan and the cap are the
// grant's as it stands now;
-// 3. finish the platform's pending mints, so a retry after a failed finish
-// lands on the account the first attempt minted;
+// 3. finish the requested ref's own pending mint, alone, so a retry after
+// a failed finish lands on the account the first attempt minted, and a
+// held one is refused before anything else is written; then the
+// platform's other pending mints;
// 4. a ref a live customer holds answers 200 with it, spending nothing;
// 5. the create budget, then the cap, pending mints included;
// 6. promise the customer (ReservePlatformMint) before the bucket is
@@ -445,13 +447,10 @@ func (s *Server) handlePlatformCustomerCreate(w http.ResponseWriter, r *http.Req
return
}
- // Step 3. A held promise is passed over (Task 8 fix round 2), so it
- // fails only a create for its own ref, here: that create writes nothing.
- if err := s.finishPendingMints(ctx, platform); err != nil {
- log.Printf("console: platform %s: create customer: finish pending creates: %v", platform.Sqid, err)
- s.platformDoorFailed(w, c)
- return
- }
+ // The requested ref's own promise first, alone (fix round 3): a retry
+ // after a failed finish lands on the account its first attempt minted,
+ // and a held promise refuses here, before anything else is finished, so
+ // the refused create writes nothing.
if req.Ref != "" {
pending, err := s.store.PendingPlatformMints(ctx, platform.Sqid)
if err != nil {
@@ -460,15 +459,27 @@ func (s *Server) handlePlatformCustomerCreate(w http.ResponseWriter, r *http.Req
return
}
for _, m := range pending {
- if m.Ref == req.Ref {
- log.Printf("console: platform %s: create customer for ref %q refused: pending create %s holds the ref "+
- "and is held until an operator repairs it", platform.Sqid, req.Ref, m.MintID)
+ if m.Ref != req.Ref {
+ continue
+ }
+ if _, err := s.finishPlatformMint(ctx, platform, m); err != nil && !errors.Is(err, errPlatformMintEnded) {
+ log.Printf("console: platform %s: create customer for ref %q refused: its pending create %s "+
+ "did not finish: %v", platform.Sqid, req.Ref, m.MintID, err)
s.platformDoorFailed(w, c)
return
}
+ break
}
}
+ // Step 3. The platform's other promises; a held one is passed over
+ // (fix round 2) and fails only a create for its own ref, above.
+ if err := s.finishPendingMints(ctx, platform); err != nil {
+ log.Printf("console: platform %s: create customer: finish pending creates: %v", platform.Sqid, err)
+ s.platformDoorFailed(w, c)
+ return
+ }
+
// Step 4.
if req.Ref != "" {
held, err := s.store.LinkedCustomerByRef(ctx, platform.Sqid, req.Ref)
diff --git a/internal/console/apiplatformcustomers_test.go b/internal/console/apiplatformcustomers_test.go
index 0832374ce..ad52064fd 100644
--- a/internal/console/apiplatformcustomers_test.go
+++ b/internal/console/apiplatformcustomers_test.go
@@ -916,8 +916,8 @@ func TestPlatformCredentialRefusedAtOrdinaryDoors(t *testing.T) {
}
}
-// heldPromise is what a refused finish must leave: one pending mint for
-// ref, counted, and no manifest naming the platform anywhere.
+// wantHeldPromise fails unless a refused finish left one pending mint for
+// ref, counted, and count-1 manifests naming the platform.
func wantHeldPromise(t *testing.T, fx *consoleFixture, platform, ref string, count int) {
t.Helper()
if p := pendingMints(t, fx.st, platform); len(p) != 1 || p[0].Ref != ref {
@@ -1081,10 +1081,24 @@ func TestCustomerHeldPromiseFailsOnlyItsOwnRef(t *testing.T) {
strings.Repeat("cd", 16), time.Now()); err != nil {
t.Fatal(err)
}
- // The gate on every door goes on past it.
+ // The gate on every door goes on past it, and finishing it writes
+ // nothing: no manifest carries its mint id (the ref is refused before
+ // the mint, Astra A3).
+ bucketBefore, err := fx.rel.List(ctx, "accounts/")
+ if err != nil {
+ t.Fatal(err)
+ }
if err := fx.srv.finishPendingMints(ctx, acct); err != nil {
t.Fatalf("finishing with a held promise = %v, want nil", err)
}
+ if after, err := fx.rel.List(ctx, "accounts/"); err != nil || !slices.Equal(after, bucketBefore) {
+ t.Fatalf("bucket accounts/ after the held finish %v %v, was %v: want nothing written", after, err, bucketBefore)
+ }
+ for _, m := range platformManifests(t, fx.rel, acct.Sqid) {
+ if m.PlatformMint == strings.Repeat("ab", 16) {
+ t.Fatalf("manifest %+v carries the held promise's mint id", m)
+ }
+ }
// A create for another ref succeeds.
status, out, raw := createCustomer(t, fx.ts.URL, acct.Sqid, tok, map[string]any{"name": "cafe", "ref": "4813"})
@@ -1294,3 +1308,58 @@ func TestCustomerCreateFinishesAPromiseMadeAfterItsGate(t *testing.T) {
t.Fatalf("pending mints %+v, want none", p)
}
}
+
+// TestCustomerHeldRefRefusalFinishesNothingElse is fix round 3 (Astra): a
+// create for a held promise's ref is refused before the platform's other
+// promises are finished, so the refused request writes nothing.
+func TestCustomerHeldRefRefusalFinishesNothingElse(t *testing.T) {
+ fx := newConsoleFixture(t)
+ acct := planAccount(t, fx, 501)
+ grantPlatformAccount(t, fx, acct.Sqid, 5)
+ tok := platformToken(t, fx, acct.ID, "cms")
+ ctx := context.Background()
+ by := "service:" + acct.Sqid + "/cms"
+ // A: held, because a live customer holds its ref. B: finishable.
+ for _, m := range []PendingMint{
+ {MintID: strings.Repeat("ab", 16), Platform: acct.Sqid, Ref: "4812", Name: "shop", By: by, CreatedAt: time.Now()},
+ {MintID: strings.Repeat("ef", 16), Platform: acct.Sqid, Ref: "4900", Name: "cafe", By: by, CreatedAt: time.Now()},
+ } {
+ if err := fx.st.ReservePlatformMint(ctx, m); err != nil {
+ t.Fatal(err)
+ }
+ }
+ holder := testAccountID()
+ if _, err := fx.st.EnsureLinkedAccount(holder, domains.Sqid(holder), "held", acct.Sqid, "4812",
+ strings.Repeat("cd", 16), time.Now()); err != nil {
+ t.Fatal(err)
+ }
+ snapshot := func() string {
+ t.Helper()
+ keys, err := fx.rel.List(ctx, "accounts/")
+ if err != nil {
+ t.Fatal(err)
+ }
+ var accounts, plans, audit int
+ for q, n := range map[string]*int{
+ `SELECT COUNT(*) FROM accounts`: &accounts,
+ `SELECT COUNT(*) FROM account_plans`: &plans,
+ `SELECT COUNT(*) FROM audit`: &audit,
+ } {
+ if err := fx.st.db.QueryRowContext(ctx, q).Scan(n); err != nil {
+ t.Fatal(err)
+ }
+ }
+ return fmt.Sprintf("manifests %v, accounts %d, plans %d, audit rows %d", keys, accounts, plans, audit)
+ }
+ before := snapshot()
+
+ status, out, _ := createCustomer(t, fx.ts.URL, acct.Sqid, tok, map[string]any{"name": "shop", "ref": "4812"})
+ wantPlatformError(t, "create for the held ref", status, out, http.StatusBadGateway, "create.failed")
+ if after := snapshot(); after != before {
+ t.Fatalf("the refused create wrote: %s, was %s", after, before)
+ }
+ p := pendingMints(t, fx.st, acct.Sqid)
+ if len(p) != 2 {
+ t.Fatalf("pending mints %+v, want A and B both still pending", p)
+ }
+}