plans 6a: Astra round 4. A successful discovery clears its marker and resolves its discovery_failing problem in one transaction
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+134
−13
docs/superpowers/specs/CODEX-FINDINGS-PLANS-6A-CODE-R4.md+68 −0internal/console/billingpass.go+6 −8internal/console/billingrecovery_test.go+40 −0internal/console/billingstore.go+20 −5
diff --git a/docs/superpowers/specs/CODEX-FINDINGS-PLANS-6A-CODE-R4.md b/docs/superpowers/specs/CODEX-FINDINGS-PLANS-6A-CODE-R4.md| new file mode 100644 |
| index 000000000..7dfd690e8 |
| --- /dev/null |
| +++ b/docs/superpowers/specs/CODEX-FINDINGS-PLANS-6A-CODE-R4.md |
| @@ -0,0 +1,68 @@ |
| +**Verdict: accept with fixes** at `af8cc0e1c4b72f755ee9791a1edf2171f3f8daa6`. |
| + |
| +One new **Minor** finding. No Critical or Important findings. Reviewed `0a9b202a..HEAD`, excluding changes taken unchanged from main. |
| + |
| +**Round 3’s finding: fixed.** [billingpass.go:142](/home/paulca/.cache/tmp/claude-1001/plans-6-checkout/internal/console/billingpass.go:142) persists the discovery marker before listing or syncing candidates and skips the customer if persistence fails. After ended subscription A commits, a restart before processing paid B leaves `discover_retry=1`; discovery selects the account regardless of its recorded subscription or age. A failed completion update also preserves the marker. |
| + |
| +`TestDiscoveryMarkerIsDurableBeforeACandidateCommits` and `TestDiscoverySkipsACustomerWhoseMarkerCannotBeWritten` cover the interruption point and failed marker write. I inspected these tests; Go execution was blocked. |
| + |
| +**Minor — successful discovery can leave `discovery_failing` permanently open.** |
| + |
| +At [billingpass.go:158](/home/paulca/.cache/tmp/claude-1001/plans-6-checkout/internal/console/billingpass.go:158), `EndBillingDiscovery` clears the retry marker before `ResolveBillingProblem` closes the alert at line 169. |
| + |
| +Concrete scenario: 24 failed passes open the problem. The next pass successfully discovers and commits the paid subscription, then clears the marker. The console stops before closing the problem, or that update fails. The account now has a recorded subscription and no retry marker, so discovery excludes it. Ordinary subscription sync only resolves `subscription_missing`; this alert remains open indefinitely. |
| + |
| +Entitlement is correct and an operator can close the alert, so this is Minor. Commit successful discovery completion and problem resolution together, or preserve retry eligibility until both succeed. Add a failure-injection regression. |
| + |
| +An in-memory SQLite reproduction using the implementation’s SQL confirmed this failure. It also confirmed the normal lifecycle: failure 24 opens one problem, failure 25 does not duplicate it, and success resets the counter and closes the problem. |
| + |
| +Other checks: |
| + |
| +- A customer’s failure does not stop later customers: the account cursor advances before processing, and customer-level failures return to that loop. Provider requests remain bounded and timed out; a slow customer can delay later ones but does not create an endless retry within the pass. |
| +- The merge preserves the earlier fixes. Pending replacement handling, transactional billing/plan/lapse writes, ownership guards, missing-subscription closure locking and the operator-inclusive deadlock test are unchanged from round 3. |
| +- Both `billing` and main’s new `explain` dispatch remain present. |
| + |
| +**Census verdicts** |
| + |
| +These concern read and authority boundaries independently of the functional finding. |
| + |
| +| Unit or envelope | Verdict | |
| +|---|---| |
| +| [Store.migrateBillingAccounts:65](/home/paulca/.cache/tmp/claude-1001/plans-6-checkout/internal/console/billingstore.go:65) | **Approve.** Adds a fixed counter column to the console-owned schema. | |
| +| [Store.MarkBillingDiscovery:418](/home/paulca/.cache/tmp/claude-1001/plans-6-checkout/internal/console/billingstore.go:418) | **Approve.** Parameterized update of one account’s discovery flag. | |
| +| [Store.EndBillingDiscovery:426](/home/paulca/.cache/tmp/claude-1001/plans-6-checkout/internal/console/billingstore.go:426) | **Approve.** Parameterized flag/counter updates on the same table; no new privileged input. | |
| +| Removed `Store.SetBillingDiscoveryRetry` | **Approve removal.** Declaration and overlay entries are gone; its operations are replaced by the two units above. | |
| +| [main.go:121](/home/paulca/.cache/tmp/claude-1001/plans-6-checkout/main.go:121), record-write digest | **Approve.** Main’s `explain` dispatch accounts for the movement. The inventory correctly layers on `edgeProtection2WriteCensusInventory`; digest `e639a4e3…` matches. This is root `main.go:main`, not `cmd/console/main.go:serve`. | |
| +| `internal/console` envelope | **Approve.** Changed discovery functions retain console-owned SQL, existing sync transitions and account scope. No new root reader or weakened authority check. | |
| +| `.` envelope | **Approve.** Merged CLI dispatch preserves billing; no additional privileged reader. | |
| + |
| +The other twenty handwritten units, all six census-propose units and the other six envelope hashes are unchanged from round 3; their prior verdicts stand. |
| + |
| +Direct recomputation matched **29 declaration digests**, legacy fingerprints, **eight package envelopes**, all **three record-write digests**, and both cumulative seals: |
| + |
| +- `plans-6a-units`, 23 units: `61c5a2d77fa39607d7ce7224a8914c3bed2de78ad38819ef4ff1665bb6d8848f` |
| +- `plans-6-checkout`: `ec611e77887f625ca08877e6f1457cbf904b9efac0964172f0e162ff49c8f824` |
| + |
| +The typed census scanner and targeted Go tests could not start because the sandbox refused creation of Go’s build directory. These hash and SQL checks do not substitute for them. No verdict was posted to #280. |
| + |
| +**CI:** I found no causal path from this branch to `TestAStartWhoseRequestEndsWhileWaitingForTheLockLaysNothingOut`. `internal/instancectl` is identical to main `96564cf3`, and the billing changes do not participate in its lock/start path. I could not independently reproduce the reported main failure. |
| + |
| +--- |
| + |
| +**FIRST PROMPT:** |
| +“Follow the brief docs/superpowers/specs/CODEX-REVIEW-BRIEF-PLANS-6A-CODE-R4.md exactly, at the branch's current HEAD.” |
| + |
| +**WHAT:** |
| +Review the round 4 fix, merge and census. |
| + |
| +**STATUS:** |
| +Complete: accept with fixes. One Minor finding; census hashes match; Go execution blocked. |
| + |
| +**NEEDED:** |
| +Nothing to complete this review. |
| + |
| +**RECOMMENDED:** |
| +Make discovery completion and alert closure atomic, add the regression, then run the targeted tests. |
| + |
| +**SESSION:** |
| +Safe to close. HEAD, index and working tree unchanged; no files created or processes left running. |
| \ No newline at end of file |
diff --git a/internal/console/billingpass.go b/internal/console/billingpass.go| index 195b7b799..9408fdccd 100644 |
| --- a/internal/console/billingpass.go |
| +++ b/internal/console/billingpass.go |
| @@ -137,7 +137,8 @@ func (s *Server) billingDiscoveryPass(ctx context.Context, now time.Time) { |
| // (Astra rounds 2 and 3). Only a pass in which every candidate succeeds |
| // clears the marker. After billingDiscoveryProblemAfter consecutive failed |
| // passes a discovery_failing problem tells an operator, and retries carry |
| -// on hourly; a pass that succeeds resolves it. |
| +// on hourly; a pass that succeeds resolves it in the same commit that |
| +// clears the marker (Astra round 4). |
| func (s *Server) discoverCustomer(ctx context.Context, b billingAccount, now time.Time) { |
| if err := s.store.MarkBillingDiscovery(b.AccountID); err != nil { |
| log.Printf("console: billing discovery: account %d: marking: %v; skipped this pass", b.AccountID, err) |
| @@ -155,22 +156,19 @@ func (s *Server) discoverCustomer(ctx context.Context, b billingAccount, now tim |
| log.Printf("console: billing discovery: account %d: %v", b.AccountID, err) |
| } |
| } |
| - n, err := s.store.EndBillingDiscovery(b.AccountID, failed) |
| + n, err := s.store.EndBillingDiscovery(b.AccountID, failed, now) |
| if err != nil { |
| // The marker stays down: the next pass tries again. |
| log.Printf("console: billing discovery: account %d: %v", b.AccountID, err) |
| return |
| } |
| + if !failed { |
| + return // marker cleared and any discovery_failing problem resolved, together |
| + } |
| sqid := "" |
| if a, err := s.store.AccountByID(b.AccountID); err == nil && a != nil { |
| sqid = a.Sqid |
| } |
| - if !failed { |
| - if err := s.store.ResolveBillingProblem(b.AccountID, "", problemDiscoveryFailing, "discovery succeeded", now); err != nil { |
| - log.Printf("console: billing discovery: account %d: %v", b.AccountID, err) |
| - } |
| - return |
| - } |
| if n >= billingDiscoveryProblemAfter { |
| if err := s.openBillingProblem(b.AccountID, sqid, "", problemDiscoveryFailing, |
| fmt.Sprintf("discovery of customer %s's subscriptions has failed %d passes in a row; it is retried every hour", b.Customer, n), now); err != nil { |
diff --git a/internal/console/billingrecovery_test.go b/internal/console/billingrecovery_test.go| index df3511b60..55297e89d 100644 |
| --- a/internal/console/billingrecovery_test.go |
| +++ b/internal/console/billingrecovery_test.go |
| @@ -414,3 +414,43 @@ func TestPersistentDiscoveryFailureOpensAProblem(t *testing.T) { |
| t.Fatalf("discovery_failing not resolved: %+v", open) |
| } |
| } |
| + |
| +// Astra round 4: a successful discovery clears its marker and resolves its |
| +// discovery_failing problem in one commit. If resolving fails, the marker |
| +// stays, so the next pass finishes the job rather than leaving the alert |
| +// open for ever. |
| +func TestDiscoverySuccessResolvesItsProblemWithTheMarker(t *testing.T) { |
| + bf := newBillingFixture(t) |
| + bf.ready(t) |
| + bf.put(bf.personalSub("sub_b", "active", nil)) |
| + bf.fake.SubErr = map[string]error{"sub_b": errors.New("timeout")} |
| + pass := billingNow |
| + for i := 0; i < billingDiscoveryProblemAfter; i++ { |
| + pass = pass.Add(time.Hour) |
| + bf.srv.billingSyncPass(context.Background(), pass) |
| + } |
| + if open, _ := bf.st.BillingProblems(false); len(open) != 1 || open[0].Reason != problemDiscoveryFailing { |
| + t.Fatalf("problems = %+v", open) |
| + } |
| + bf.fake.SubErr = nil |
| + if _, err := bf.st.db.Exec(`CREATE TRIGGER fail_resolve BEFORE UPDATE ON billing_problems BEGIN SELECT RAISE(ABORT, 'injected'); END`); err != nil { |
| + t.Fatal(err) |
| + } |
| + pass = pass.Add(time.Hour) |
| + bf.srv.billingSyncPass(context.Background(), pass) |
| + if p := bf.plan(t); p != "personal" { |
| + t.Fatalf("the purchase did not sync: plan %q", p) |
| + } |
| + if _, err := bf.st.db.Exec(`DROP TRIGGER fail_resolve`); err != nil { |
| + t.Fatal(err) |
| + } |
| + bf.srv.billingSyncPass(context.Background(), pass.Add(time.Hour)) |
| + if open, _ := bf.st.BillingProblems(false); len(open) != 0 { |
| + t.Fatalf("discovery_failing left open after a failed resolve: %+v", open) |
| + } |
| + bf.fake.CustomerCalls = 0 |
| + bf.srv.billingSyncPass(context.Background(), pass.Add(2*time.Hour)) |
| + if bf.fake.CustomerCalls != 0 { |
| + t.Fatalf("customer still in discovery after both succeeded: %d calls", bf.fake.CustomerCalls) |
| + } |
| +} |
diff --git a/internal/console/billingstore.go b/internal/console/billingstore.go| index d5bbff43c..b7778cd9d 100644 |
| --- a/internal/console/billingstore.go |
| +++ b/internal/console/billingstore.go |
| @@ -420,12 +420,27 @@ func (s *Store) MarkBillingDiscovery(accountID int64) error { |
| } |
| // EndBillingDiscovery records the outcome of one discovery pass over the |
| -// account. Success clears the marker and the failure count; a failure keeps |
| -// the marker and returns the number of consecutive failed passes. |
| -func (s *Store) EndBillingDiscovery(accountID int64, failed bool) (int, error) { |
| +// account. Success clears the marker and the failure count and resolves the |
| +// account's open discovery_failing problem in one transaction (Astra round |
| +// 4), so a failed resolve keeps the marker and the next pass finishes the |
| +// job. A failure keeps the marker and returns the number of consecutive |
| +// failed passes. |
| +func (s *Store) EndBillingDiscovery(accountID int64, failed bool, now time.Time) (int, error) { |
| if !failed { |
| - _, err := s.db.Exec(`UPDATE billing_accounts SET discover_retry = 0, discover_failures = 0 WHERE account_id = ?`, accountID) |
| - return 0, err |
| + tx, err := s.db.Begin() |
| + if err != nil { |
| + return 0, err |
| + } |
| + defer tx.Rollback() |
| + if _, err := tx.Exec(`UPDATE billing_accounts SET discover_retry = 0, discover_failures = 0 WHERE account_id = ?`, accountID); err != nil { |
| + return 0, err |
| + } |
| + if _, err := tx.Exec(`UPDATE billing_problems SET closed_at = ?, closed_by = ?, note = ? |
| + WHERE account_id = ? AND subscription = '' AND reason = ? AND closed_at = 0`, |
| + now.Unix(), planBilling, "discovery succeeded", accountID, problemDiscoveryFailing); err != nil { |
| + return 0, err |
| + } |
| + return 0, tx.Commit() |
| } |
| var n int |
| err := s.db.QueryRow(`UPDATE billing_accounts SET discover_failures = discover_failures + 1 WHERE account_id = ? |