Skip to content

fix(billing): re-throw on demotePublicAgentsOnTierDowngrade failure (closes #3694) - #3698

Merged
bokelley merged 1 commit into
mainfrom
bokelley/demote-throw
Apr 30, 2026
Merged

bokelley merged 1 commit into
mainfrom
bokelley/demote-throw

Conversation

@bokelley

Copy link
Copy Markdown
Contributor

Summary

Closes #3694 (the second of three triage follow-ups from #3691).

When a Stripe webhook flips an org from a tier with API access to one without, demotePublicAgentsOnTierDowngrade must demote any public-marked agents to members_only and strip them from the org's brand.json — otherwise they stay publicly listed on a tier that doesn't allow public agents (silent entitlement leak).

The call site at http.ts:3792 was wrapped in a try/catch that logged and continued. A transient DB error during demotion would silently leave stale public agents until the next tier change.

Why hoist instead of just removing the inner try/catch

The previous structure had the demote call inside the outer swallow-on-error try, so removing the inner catch alone would let errors propagate to the outer swallow. Hoists the demote call outside that block, alongside the core subscription UPDATE — they're an entitlement pair:

  • UPDATE writes the new membership_tier to the org row
  • demote enforces the corresponding agent visibility on member_profiles + brand.json

Either failing alone leaves a coherent state; both completing or both retrying is the correct behavior.

Idempotency check

Verified that demotePublicAgentsOnTierDowngrade is safe to re-run after partial completion:

  • Reads member_profiles row with FOR UPDATE
  • If no public-visibility agents remain → returns null (no-op)
  • Brand.json clear at line 119+ is also idempotent — filters by demoted URLs, no write if remaining.length === currentAgents.length

A Stripe retry that re-fires the tier-downgrade webhook will be a no-op if the previous attempt completed the agent visibility writes.

Test plan

  • npx tsc --noEmit clean
  • Existing 10 webhook integration tests still pass (admin-sync-revenue-backfill, admin-stripe-link-unlink)
  • Pre-commit hook passes
  • Manual: simulate transient pool error during demote → assert webhook returns 5xx → Stripe retries → second attempt completes successfully

Refs

🤖 Generated with Claude Code

…3694)

When a Stripe webhook flips an org from a tier with API access to one
without, demotePublicAgentsOnTierDowngrade must demote any
public-marked agents to members_only and strip them from the org's
brand.json — otherwise they stay publicly listed on a tier that
doesn't allow public agents (silent entitlement leak).

The call site at http.ts:3792 was wrapped in a try/catch that logged
and continued. A transient DB error during demotion would silently
leave stale public agents until the next tier change. Per
js-protocol-expert review on #3691: "you've got an org row claiming
the new tier with stale public agents — exactly the silent-swallow
class you're trying to retire, just one layer down."

Hoists the demote call outside the swallow-on-error block, alongside
the core subscription UPDATE. They're a pair: UPDATE writes the new
tier; demote enforces the corresponding agent visibility. Idempotent
on retry (the helper takes FOR UPDATE on member_profiles, no-ops if
no public agents remain) so a Stripe retry that re-fires the
tier-downgrade webhook does the right thing.

Closes the second of the three triage follow-ups from #3691.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@bokelley
bokelley merged commit 2ee980b into main Apr 30, 2026
13 checks passed
@bokelley
bokelley deleted the bokelley/demote-throw branch April 30, 2026 20:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

billing: demotePublicAgentsOnTierDowngrade is silently swallowed (entitlement leak on tier change)

1 participant