From c8d3f8aa75e210cbf3f8cc3eaa7b15cf7a996f6b Mon Sep 17 00:00:00 2001 From: Nick Bottone Date: Thu, 28 May 2026 00:49:07 -0400 Subject: [PATCH 01/28] Reset Django migrations baseline --- .gitignore | 1 - discordoauth2/migrations/0001_initial.py | 53 +++++++++++ discordoauth2/migrations/__init__.py | 1 + events/migrations/0001_initial.py | 110 +++++++++++++++++++++++ events/migrations/__init__.py | 1 + highscores/migrations/0001_initial.py | 80 +++++++++++++++++ highscores/migrations/__init__.py | 0 home/migrations/0001_initial.py | 38 ++++++++ home/migrations/__init__.py | 1 + ladder/migrations/__init__.py | 0 ranked/migrations/0001_initial.py | 67 ++++++++++++++ ranked/migrations/__init__.py | 1 + teamleague/migrations/__init__.py | 0 13 files changed, 352 insertions(+), 1 deletion(-) create mode 100644 discordoauth2/migrations/0001_initial.py create mode 100644 discordoauth2/migrations/__init__.py create mode 100644 events/migrations/0001_initial.py create mode 100644 events/migrations/__init__.py create mode 100644 highscores/migrations/0001_initial.py create mode 100644 highscores/migrations/__init__.py create mode 100644 home/migrations/0001_initial.py create mode 100644 home/migrations/__init__.py create mode 100644 ladder/migrations/__init__.py create mode 100644 ranked/migrations/0001_initial.py create mode 100644 ranked/migrations/__init__.py create mode 100644 teamleague/migrations/__init__.py diff --git a/.gitignore b/.gitignore index c3b2e73..646d662 100644 --- a/.gitignore +++ b/.gitignore @@ -132,7 +132,6 @@ dmypy.json # Database *.sqlite3 -migrations/ .vscode/ .DS_Store diff --git a/discordoauth2/migrations/0001_initial.py b/discordoauth2/migrations/0001_initial.py new file mode 100644 index 0000000..11d0b77 --- /dev/null +++ b/discordoauth2/migrations/0001_initial.py @@ -0,0 +1,53 @@ +# Generated by Django 3.2.25 on 2026-05-28 04:36 + +import discordoauth2.managers +import django.contrib.auth.validators +import django.core.validators +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('auth', '0012_alter_user_first_name_max_length'), + ] + + operations = [ + migrations.CreateModel( + name='User', + fields=[ + ('password', models.CharField(max_length=128, verbose_name='password')), + ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), + ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), + ('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')), + ('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')), + ('display_name', models.CharField(max_length=25, null=True, validators=[django.contrib.auth.validators.ASCIIUsernameValidator(), django.core.validators.MinLengthValidator(4)], verbose_name='display name')), + ('id', models.BigIntegerField(primary_key=True, serialize=False, verbose_name='user id')), + ('username', models.CharField(max_length=100, verbose_name='username')), + ('discriminator', models.CharField(max_length=4, verbose_name='discriminator')), + ('avatar', models.URLField(verbose_name='avatar')), + ('public_flags', models.IntegerField(verbose_name='public flags')), + ('flags', models.IntegerField(verbose_name='flags')), + ('locale', models.CharField(max_length=100, verbose_name='locale')), + ('mfa_enabled', models.BooleanField(verbose_name='multi-factor authentication enabled')), + ('email', models.EmailField(max_length=254, verbose_name='email address')), + ('verified', models.BooleanField(help_text='Whether the user has verified their email address.', verbose_name='email verified')), + ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')), + ('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff')), + ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')), + ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')), + ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')), + ], + options={ + 'verbose_name': 'user', + 'verbose_name_plural': 'users', + 'abstract': False, + }, + managers=[ + ('objects', discordoauth2.managers.DiscordUserOAuth2Manager()), + ], + ), + ] diff --git a/discordoauth2/migrations/__init__.py b/discordoauth2/migrations/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/discordoauth2/migrations/__init__.py @@ -0,0 +1 @@ + diff --git a/events/migrations/0001_initial.py b/events/migrations/0001_initial.py new file mode 100644 index 0000000..8953f1a --- /dev/null +++ b/events/migrations/0001_initial.py @@ -0,0 +1,110 @@ +# Generated by Django 3.2.25 on 2026-05-28 04:36 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='ChampionshipPoints', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('player_name', models.CharField(max_length=25)), + ('event_1', models.IntegerField(default=0)), + ('event_2', models.IntegerField(default=0)), + ], + ), + migrations.CreateModel( + name='Event', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=25)), + ('start_time', models.DateTimeField()), + ('end_time', models.DateTimeField()), + ], + ), + migrations.CreateModel( + name='Ranking', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('player_name', models.CharField(max_length=25)), + ('ranking_points', models.IntegerField()), + ('time_set', models.DateTimeField(blank=True, null=True)), + ('event', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='events.event')), + ], + ), + migrations.CreateModel( + name='Player', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('player_name', models.CharField(max_length=25)), + ('event', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='events.event')), + ], + ), + migrations.CreateModel( + name='Match', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('match_number', models.IntegerField(blank=True, null=True)), + ('match_type', models.CharField(choices=[('q', 'Qualifications'), ('qf', 'Quarterfinals'), ('sf', 'Semifinals'), ('f', 'Finals')], default='q', max_length=2)), + ('red1', models.CharField(max_length=25)), + ('red2', models.CharField(max_length=25)), + ('red3', models.CharField(max_length=25)), + ('blue1', models.CharField(max_length=25)), + ('blue2', models.CharField(max_length=25)), + ('blue3', models.CharField(max_length=25)), + ('red1_redcard', models.BooleanField(default=False)), + ('red2_redcard', models.BooleanField(default=False)), + ('red3_redcard', models.BooleanField(default=False)), + ('blue1_redcard', models.BooleanField(default=False)), + ('blue2_redcard', models.BooleanField(default=False)), + ('blue3_redcard', models.BooleanField(default=False)), + ('red1_surrogate', models.BooleanField(default=False)), + ('red2_surrogate', models.BooleanField(default=False)), + ('red3_surrogate', models.BooleanField(default=False)), + ('blue1_surrogate', models.BooleanField(default=False)), + ('blue2_surrogate', models.BooleanField(default=False)), + ('blue3_surrogate', models.BooleanField(default=False)), + ('red1_contribution', models.IntegerField(blank=True, null=True)), + ('red2_contribution', models.IntegerField(blank=True, null=True)), + ('red3_contribution', models.IntegerField(blank=True, null=True)), + ('blue1_contribution', models.IntegerField(blank=True, null=True)), + ('blue2_contribution', models.IntegerField(blank=True, null=True)), + ('blue3_contribution', models.IntegerField(blank=True, null=True)), + ('red_score', models.IntegerField(blank=True, null=True)), + ('red_climb_rp', models.BooleanField(blank=True, null=True)), + ('red_wheel_rp', models.BooleanField(blank=True, null=True)), + ('red_auto_points', models.IntegerField(blank=True, null=True)), + ('red_teleop_points', models.IntegerField(blank=True, null=True)), + ('red_endgame_points', models.IntegerField(blank=True, null=True)), + ('red_power_cells', models.IntegerField(blank=True, null=True)), + ('blue_score', models.IntegerField(blank=True, null=True)), + ('blue_climb_rp', models.BooleanField(blank=True, null=True)), + ('blue_wheel_rp', models.BooleanField(blank=True, null=True)), + ('blue_auto_points', models.IntegerField(blank=True, null=True)), + ('blue_teleop_points', models.IntegerField(blank=True, null=True)), + ('blue_endgame_points', models.IntegerField(blank=True, null=True)), + ('blue_power_cells', models.IntegerField(blank=True, null=True)), + ('event', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='events.event')), + ], + ), + migrations.CreateModel( + name='ElimsAlliance', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('alliance_number', models.IntegerField(blank=True, null=True)), + ('player1', models.CharField(max_length=25)), + ('player2', models.CharField(max_length=25)), + ('player3', models.CharField(max_length=25)), + ('advancement', models.CharField(choices=[('qf', 'QF'), ('sf', 'SF'), ('f', 'F'), ('w', 'W')], default='qf', max_length=25)), + ('event', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='events.event')), + ], + ), + ] diff --git a/events/migrations/__init__.py b/events/migrations/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/events/migrations/__init__.py @@ -0,0 +1 @@ + diff --git a/highscores/migrations/0001_initial.py b/highscores/migrations/0001_initial.py new file mode 100644 index 0000000..8a57cd6 --- /dev/null +++ b/highscores/migrations/0001_initial.py @@ -0,0 +1,80 @@ +# Generated by Django 3.2.25 on 2026-05-28 04:36 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='ExemptedIP', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('ip', models.CharField(max_length=45)), + ('reason', models.CharField(blank=True, max_length=200, null=True)), + ], + ), + migrations.CreateModel( + name='Leaderboard', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=25)), + ('robot', models.CharField(max_length=25)), + ('game', models.CharField(max_length=25)), + ('game_slug', models.CharField(max_length=5)), + ('auto_or_teleop', models.CharField(default='TELE', max_length=4)), + ('message', models.CharField(blank=True, max_length=200, null=True)), + ], + ), + migrations.CreateModel( + name='Score', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('score', models.IntegerField()), + ('time_set', models.DateTimeField(auto_now_add=True)), + ('source', models.URLField()), + ('approved', models.BooleanField(default=False)), + ('clean_code', models.CharField(max_length=4000)), + ('decrypted_code', models.CharField(blank=True, max_length=2000, null=True)), + ('client_version', models.CharField(blank=True, max_length=20, null=True)), + ('time_of_score', models.CharField(blank=True, max_length=30, null=True)), + ('robot_position', models.CharField(blank=True, max_length=20, null=True)), + ('time_data', models.TextField(blank=True, null=True)), + ('ip', models.CharField(blank=True, max_length=45, null=True)), + ('leaderboard', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='highscores.leaderboard')), + ('player', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + migrations.CreateModel( + name='CleanCodeSubmission', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('clean_code', models.CharField(max_length=600)), + ('score', models.IntegerField()), + ('time_set', models.DateTimeField(auto_now_add=True)), + ('ip', models.CharField(blank=True, max_length=45, null=True)), + ('leaderboard', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='highscores.leaderboard')), + ('player', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + migrations.AddIndex( + model_name='score', + index=models.Index(fields=['leaderboard'], name='highscores__leaderb_ad4364_idx'), + ), + migrations.AddIndex( + model_name='score', + index=models.Index(fields=['player'], name='highscores__player__3776b0_idx'), + ), + migrations.AddIndex( + model_name='score', + index=models.Index(fields=['approved'], name='highscores__approve_054a97_idx'), + ), + ] diff --git a/highscores/migrations/__init__.py b/highscores/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/home/migrations/0001_initial.py b/home/migrations/0001_initial.py new file mode 100644 index 0000000..b5f9d0a --- /dev/null +++ b/home/migrations/0001_initial.py @@ -0,0 +1,38 @@ +# Generated by Django 3.2.25 on 2026-05-28 04:36 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='HistoricEvent', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=50)), + ('date', models.DateField()), + ('youtube_url', models.URLField(blank=True, null=True)), + ('first_place', models.CharField(max_length=100)), + ('second_place', models.CharField(blank=True, max_length=100, null=True)), + ], + ), + migrations.CreateModel( + name='Staff', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=50)), + ('title', models.CharField(max_length=50)), + ('image_url', models.URLField()), + ('bio', models.TextField()), + ('linkedin_url', models.URLField(blank=True, null=True)), + ('github_url', models.URLField(blank=True, null=True)), + ('email', models.EmailField(max_length=254)), + ], + ), + ] diff --git a/home/migrations/__init__.py b/home/migrations/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/home/migrations/__init__.py @@ -0,0 +1 @@ + diff --git a/ladder/migrations/__init__.py b/ladder/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ranked/migrations/0001_initial.py b/ranked/migrations/0001_initial.py new file mode 100644 index 0000000..cea68c6 --- /dev/null +++ b/ranked/migrations/0001_initial.py @@ -0,0 +1,67 @@ +# Generated by Django 3.2.25 on 2026-05-28 04:36 + +from django.conf import settings +import django.core.validators +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='GameMode', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=25, unique=True)), + ('game', models.CharField(max_length=25)), + ('players_per_alliance', models.IntegerField(validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(3)])), + ('short_code', models.CharField(max_length=10, unique=True)), + ], + ), + migrations.CreateModel( + name='PlayerElo', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('elo', models.FloatField(default=1200)), + ('matches_played', models.IntegerField(default=0)), + ('matches_won', models.IntegerField(default=0)), + ('matches_lost', models.IntegerField(default=0)), + ('matches_drawn', models.IntegerField(default=0)), + ('last_match_played_time', models.DateTimeField(blank=True, null=True)), + ('last_match_played_number', models.IntegerField(blank=True, null=True)), + ('total_score', models.IntegerField(default=0)), + ('game_mode', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ranked.gamemode')), + ('player', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + migrations.CreateModel( + name='Match', + fields=[ + ('match_number', models.AutoField(primary_key=True, serialize=False)), + ('time', models.DateTimeField(auto_now_add=True)), + ('red_score', models.IntegerField()), + ('blue_score', models.IntegerField()), + ('red_starting_elo', models.FloatField()), + ('blue_starting_elo', models.FloatField()), + ('blue_alliance', models.ManyToManyField(related_name='blue_alliance', to=settings.AUTH_USER_MODEL)), + ('game_mode', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ranked.gamemode')), + ('red_alliance', models.ManyToManyField(related_name='red_alliance', to=settings.AUTH_USER_MODEL)), + ], + ), + migrations.CreateModel( + name='EloHistory', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('match_number', models.IntegerField()), + ('elo', models.FloatField()), + ('player_elo', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ranked.playerelo')), + ], + ), + ] diff --git a/ranked/migrations/__init__.py b/ranked/migrations/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/ranked/migrations/__init__.py @@ -0,0 +1 @@ + diff --git a/teamleague/migrations/__init__.py b/teamleague/migrations/__init__.py new file mode 100644 index 0000000..e69de29 From 3c2e84ff03cd70292fab51733e6cc92208682f00 Mon Sep 17 00:00:00 2001 From: Nick Bottone Date: Thu, 28 May 2026 00:49:30 -0400 Subject: [PATCH 02/28] Add premium subscriptions and casual server orchestration --- SRCweb/settings.py | 16 +- SRCweb/urls.py | 2 + home/templates/home/base.html | 9 + subscriptions/__init__.py | 1 + subscriptions/admin.py | 66 ++ subscriptions/api/__init__.py | 1 + subscriptions/api/urls.py | 15 + subscriptions/api/views.py | 164 ++++ subscriptions/apps.py | 6 + subscriptions/management/__init__.py | 1 + subscriptions/management/commands/__init__.py | 1 + .../commands/expire_premium_sessions.py | 19 + subscriptions/migrations/0001_initial.py | 189 +++++ .../migrations/0002_seed_default_plans.py | 47 ++ subscriptions/migrations/__init__.py | 1 + subscriptions/models.py | 252 +++++++ subscriptions/orchestrator.py | 87 +++ subscriptions/services.py | 707 ++++++++++++++++++ .../templates/subscriptions/home.html | 180 +++++ subscriptions/tests.py | 403 ++++++++++ subscriptions/urls.py | 11 + subscriptions/views.py | 135 ++++ 22 files changed, 2312 insertions(+), 1 deletion(-) create mode 100644 subscriptions/__init__.py create mode 100644 subscriptions/admin.py create mode 100644 subscriptions/api/__init__.py create mode 100644 subscriptions/api/urls.py create mode 100644 subscriptions/api/views.py create mode 100644 subscriptions/apps.py create mode 100644 subscriptions/management/__init__.py create mode 100644 subscriptions/management/commands/__init__.py create mode 100644 subscriptions/management/commands/expire_premium_sessions.py create mode 100644 subscriptions/migrations/0001_initial.py create mode 100644 subscriptions/migrations/0002_seed_default_plans.py create mode 100644 subscriptions/migrations/__init__.py create mode 100644 subscriptions/models.py create mode 100644 subscriptions/orchestrator.py create mode 100644 subscriptions/services.py create mode 100644 subscriptions/templates/subscriptions/home.html create mode 100644 subscriptions/tests.py create mode 100644 subscriptions/urls.py create mode 100644 subscriptions/views.py diff --git a/SRCweb/settings.py b/SRCweb/settings.py index 97d7dbe..d08616b 100644 --- a/SRCweb/settings.py +++ b/SRCweb/settings.py @@ -37,6 +37,19 @@ # SECURITY WARNING: keep the API Key used in production secret! API_KEY = os.getenv("API_KEY") or "API_KEY" +# Polar subscriptions +POLAR_API_BASE_URL = os.getenv("POLAR_API_BASE_URL") or "https://api.polar.sh" +POLAR_ACCESS_TOKEN = os.getenv("POLAR_ACCESS_TOKEN") or "" +POLAR_WEBHOOK_SECRET = os.getenv("POLAR_WEBHOOK_SECRET") or "" +POLAR_CHECKOUT_SUCCESS_URL = os.getenv("POLAR_CHECKOUT_SUCCESS_URL") or "" +POLAR_CUSTOMER_PORTAL_URL = os.getenv("POLAR_CUSTOMER_PORTAL_URL") or "" + +# xRC server orchestrator +ORCHESTRATOR_API_BASE_URL = os.getenv("ORCHESTRATOR_API_BASE_URL") or "" +ORCHESTRATOR_API_TOKEN = os.getenv("ORCHESTRATOR_API_TOKEN") or "" +ORCHESTRATOR_REQUEST_TIMEOUT_SECONDS = float(os.getenv("ORCHESTRATOR_REQUEST_TIMEOUT_SECONDS") or 10) +SECONDWEBSITE_SERVICE_TOKEN = os.getenv("SECONDWEBSITE_SERVICE_TOKEN") or "" + # Sends an email to admins when debug = false and a 500 server error occurs ADMINS = [('Webmaster', 'webmaster@secondrobotics.org')] ADMIN_EMAILS = [email for name, email in ADMINS] @@ -73,7 +86,8 @@ # 'teamleague', # 'ladder', 'discordoauth2', - 'ranked' + 'ranked', + 'subscriptions', ] REST_FRAMEWORK = { diff --git a/SRCweb/urls.py b/SRCweb/urls.py index 31ab48c..bca3fb1 100644 --- a/SRCweb/urls.py +++ b/SRCweb/urls.py @@ -30,6 +30,8 @@ path('oauth2/', include('discordoauth2.urls')), path('ranked/', include('ranked.urls')), path('api/ranked/', include('ranked.api.urls', namespace="api-ranked")), + path('subscriptions/', include('subscriptions.urls')), + path('api/subscriptions/', include('subscriptions.api.urls', namespace="api-subscriptions")), ] if settings.DEBUG: diff --git a/home/templates/home/base.html b/home/templates/home/base.html index c19e808..970c734 100644 --- a/home/templates/home/base.html +++ b/home/templates/home/base.html @@ -243,6 +243,15 @@ + + diff --git a/subscriptions/tests.py b/subscriptions/tests.py index 16df89d..9a37db3 100644 --- a/subscriptions/tests.py +++ b/subscriptions/tests.py @@ -1,6 +1,3 @@ -import base64 -import hashlib -import hmac import json from datetime import timedelta from unittest.mock import patch @@ -18,7 +15,7 @@ process_orchestrator_event, start_server_session, stop_server_session, - verify_standard_webhook_signature, + validate_polar_webhook, ) @@ -47,17 +44,17 @@ def create_user(user_id=1001): ) -def create_entitlement(user, tier='supporter', minutes=360): +def create_entitlement(user, tier='supporter', credits=360, provider=SubscriptionEntitlement.PROVIDER_COMP): plan = SubscriptionPlan.objects.get(tier=tier) - if minutes != plan.monthly_server_minutes: - plan.monthly_server_minutes = minutes - plan.save(update_fields=['monthly_server_minutes']) + if credits != plan.monthly_credits: + plan.monthly_credits = credits + plan.save(update_fields=['monthly_credits']) now = timezone.now() return SubscriptionEntitlement.objects.create( user=user, plan=plan, - provider=SubscriptionEntitlement.PROVIDER_COMP, - provider_subscription_id=f'comp-{user.id}-{tier}', + provider=provider, + provider_subscription_id=f'{provider}-{user.id}-{tier}', status=SubscriptionEntitlement.STATUS_ACTIVE, current_period_start=now, current_period_end=now + timedelta(days=30), @@ -73,7 +70,7 @@ def test_start_requires_active_entitlement(self): def test_start_limits_session_to_plan_and_remaining_minutes(self): user = create_user() - create_entitlement(user, tier='supporter', minutes=30) + create_entitlement(user, tier='supporter', credits=30) session = start_server_session(user, requested_minutes=240, game='xRC') @@ -93,8 +90,8 @@ def test_plan_can_allow_multiple_concurrent_sessions(self): user = create_user() plan = SubscriptionPlan.objects.get(tier='supporter') plan.max_concurrent_servers = 2 - plan.monthly_server_minutes = 360 - plan.save(update_fields=['max_concurrent_servers', 'monthly_server_minutes']) + plan.monthly_credits = 360 + plan.save(update_fields=['max_concurrent_servers', 'monthly_credits']) create_entitlement(user) start_server_session(user, requested_minutes=30) @@ -107,13 +104,13 @@ def test_highest_priority_entitlement_is_used(self): supporter = SubscriptionPlan.objects.get(tier='supporter') host = SubscriptionPlan.objects.get(tier='host') supporter.entitlement_priority = 100 - supporter.monthly_server_minutes = 30 - supporter.save(update_fields=['entitlement_priority', 'monthly_server_minutes']) + supporter.monthly_credits = 30 + supporter.save(update_fields=['entitlement_priority', 'monthly_credits']) host.entitlement_priority = 10 - host.monthly_server_minutes = 1500 - host.save(update_fields=['entitlement_priority', 'monthly_server_minutes']) + host.monthly_credits = 1500 + host.save(update_fields=['entitlement_priority', 'monthly_credits']) create_entitlement(user, tier='host') - create_entitlement(user, tier='supporter', minutes=30) + create_entitlement(user, tier='supporter', credits=30) session = start_server_session(user, requested_minutes=240) @@ -131,24 +128,63 @@ def test_stop_records_rounded_usage_once(self): stop_server_session(session) usage = UsageLedgerEntry.objects.get(server_session=session) - self.assertEqual(usage.minutes, 10) + self.assertEqual(usage.credits, 10) self.assertEqual(UsageLedgerEntry.objects.count(), 1) def test_exhausted_entitlement_rejects_start(self): user = create_user() - entitlement = create_entitlement(user, minutes=30) + entitlement = create_entitlement(user, credits=30) UsageLedgerEntry.objects.create( user=user, entitlement=entitlement, billing_period_start=entitlement.current_period_start, billing_period_end=entitlement.current_period_end, - minutes=30, + credits=30, reason=UsageLedgerEntry.REASON_ADMIN_ADJUSTMENT, ) with self.assertRaises(SubscriptionError): start_server_session(user) + @patch('subscriptions.polar_gateway.get_customer_credit_balance') + def test_polar_entitlement_uses_meter_balance_for_launch(self, mock_balance): + mock_balance.return_value = 25 + user = create_user() + create_entitlement(user, provider=SubscriptionEntitlement.PROVIDER_POLAR, credits=360) + + session = start_server_session(user, requested_minutes=60) + + self.assertEqual(session.allocated_minutes, 25) + mock_balance.assert_called_once_with(user) + + @patch('subscriptions.polar_gateway.get_customer_credit_balance') + def test_polar_entitlement_rejects_exhausted_meter_balance(self, mock_balance): + mock_balance.return_value = 0 + user = create_user() + create_entitlement(user, provider=SubscriptionEntitlement.PROVIDER_POLAR, credits=360) + + with self.assertRaises(SubscriptionError): + start_server_session(user, requested_minutes=30) + + @patch('subscriptions.polar_gateway.ingest_usage_event') + @patch('subscriptions.polar_gateway.get_customer_credit_balance') + def test_polar_usage_is_ingested_when_session_stops(self, mock_balance, mock_ingest): + mock_balance.return_value = 60 + mock_ingest.return_value = type('PolarIngestResponse', (), {'inserted': 1, 'duplicates': 0})() + user = create_user() + create_entitlement(user, provider=SubscriptionEntitlement.PROVIDER_POLAR, credits=360) + session = start_server_session(user, requested_minutes=60) + session.started_at = timezone.now() - timedelta(minutes=6) + session.save(update_fields=['started_at']) + + stop_server_session(session) + stop_server_session(session) + + usage = UsageLedgerEntry.objects.get(server_session=session) + self.assertEqual(usage.credits, 10) + self.assertEqual(usage.metadata['polar_inserted'], 1) + mock_ingest.assert_called_once() + def test_expire_stale_sessions_marks_expired_and_records_usage(self): user = create_user() create_entitlement(user) @@ -162,7 +198,7 @@ def test_expire_stale_sessions_marks_expired_and_records_usage(self): self.assertEqual(len(expired), 1) session.refresh_from_db() self.assertEqual(session.status, ServerSession.STATUS_EXPIRED) - self.assertEqual(UsageLedgerEntry.objects.get(server_session=session).minutes, 20) + self.assertEqual(UsageLedgerEntry.objects.get(server_session=session).credits, 20) def test_orchestrator_ready_and_stopped_events_record_usage_once(self): user = create_user() @@ -205,7 +241,7 @@ def test_orchestrator_ready_and_stopped_events_record_usage_once(self): self.assertEqual(session.host, 'play.example.test') self.assertEqual(session.port, 11115) self.assertEqual(session.server_password, 'secret') - self.assertEqual(UsageLedgerEntry.objects.get(server_session=session).minutes, 10) + self.assertEqual(UsageLedgerEntry.objects.get(server_session=session).credits, 10) self.assertEqual(WebhookEvent.objects.filter(provider='orchestrator').count(), 2) def test_orchestrator_launch_failed_does_not_record_usage(self): @@ -349,24 +385,21 @@ def test_orchestrator_callback_accepts_api_key(self): class PolarWebhookTests(TestCase): - @override_settings(POLAR_WEBHOOK_SECRET='whsec_' + base64.b64encode(b'secret').decode('utf-8')) - def test_standard_webhook_signature_verification_accepts_valid_signature(self): + @override_settings(POLAR_WEBHOOK_SECRET='whsec_test') + @patch('subscriptions.polar_gateway.validate_event') + def test_polar_webhook_validation_uses_sdk(self, mock_validate_event): body = b'{"type":"subscription.updated"}' - webhook_id = 'evt_123' - timestamp = str(int(timezone.now().timestamp())) - signed = b'.'.join([webhook_id.encode(), timestamp.encode(), body]) - signature = 'v1,' + base64.b64encode(hmac.new(b'secret', signed, hashlib.sha256).digest()).decode('utf-8') - - verify_standard_webhook_signature( - 'whsec_' + base64.b64encode(b'secret').decode('utf-8'), - body, - webhook_id, - timestamp, - signature, - ) + payload = {'type': 'subscription.updated', 'data': {}} + mock_validate_event.return_value = payload + + result = validate_polar_webhook('whsec_test', body, {'webhook-id': 'evt_123'}) + + self.assertEqual(result, payload) + mock_validate_event.assert_called_once_with(body, {'webhook-id': 'evt_123'}, 'whsec_test') @override_settings(POLAR_WEBHOOK_SECRET='plain-secret') - def test_polar_subscription_webhook_upserts_entitlement_and_is_idempotent(self): + @patch('subscriptions.polar_gateway.validate_event') + def test_polar_subscription_webhook_upserts_entitlement_and_is_idempotent(self, mock_validate_event): user = create_user() plan = SubscriptionPlan.objects.get(tier='supporter') plan.polar_product_id = 'prod_supporter' @@ -382,11 +415,9 @@ def test_polar_subscription_webhook_upserts_entitlement_and_is_idempotent(self): 'product_id': 'prod_supporter', }, } + mock_validate_event.return_value = body raw_body = json.dumps(body).encode('utf-8') webhook_id = 'evt_123' - timestamp = str(int(timezone.now().timestamp())) - signed = b'.'.join([webhook_id.encode(), timestamp.encode(), raw_body]) - signature = 'v1,' + base64.b64encode(hmac.new(b'plain-secret', signed, hashlib.sha256).digest()).decode('utf-8') client = APIClient() response = client.post( @@ -394,8 +425,6 @@ def test_polar_subscription_webhook_upserts_entitlement_and_is_idempotent(self): data=raw_body, content_type='application/json', HTTP_WEBHOOK_ID=webhook_id, - HTTP_WEBHOOK_TIMESTAMP=timestamp, - HTTP_WEBHOOK_SIGNATURE=signature, ) self.assertEqual(response.status_code, 200) @@ -404,8 +433,6 @@ def test_polar_subscription_webhook_upserts_entitlement_and_is_idempotent(self): data=raw_body, content_type='application/json', HTTP_WEBHOOK_ID=webhook_id, - HTTP_WEBHOOK_TIMESTAMP=timestamp, - HTTP_WEBHOOK_SIGNATURE=signature, ) self.assertEqual(response.status_code, 200) diff --git a/subscriptions/views.py b/subscriptions/views.py index bc3ff3c..1fad672 100644 --- a/subscriptions/views.py +++ b/subscriptions/views.py @@ -1,5 +1,3 @@ -import json - from django.conf import settings from django.contrib import messages from django.contrib.auth.decorators import login_required @@ -18,7 +16,7 @@ launch_casual_server, process_polar_webhook, request_stop_server_session, - verify_standard_webhook_signature, + validate_polar_webhook, ) @@ -111,25 +109,15 @@ def stop_server_session_view(request, session_id): @csrf_exempt @require_POST def polar_webhook(request): - webhook_id = request.META.get('HTTP_WEBHOOK_ID', '') - webhook_timestamp = request.META.get('HTTP_WEBHOOK_TIMESTAMP', '') - webhook_signature = request.META.get('HTTP_WEBHOOK_SIGNATURE', '') - try: - verify_standard_webhook_signature( + payload = validate_polar_webhook( getattr(settings, 'POLAR_WEBHOOK_SECRET', ''), request.body, - webhook_id, - webhook_timestamp, - webhook_signature, + {key[5:].replace('_', '-').lower(): value for key, value in request.META.items() if key.startswith('HTTP_')}, ) except WebhookVerificationError as exc: return HttpResponseBadRequest(str(exc)) - try: - payload = json.loads(request.body.decode('utf-8')) - except json.JSONDecodeError: - return HttpResponseBadRequest('Invalid JSON payload.') - - event = process_polar_webhook(payload, event_id=webhook_id) + event_id = request.META.get('HTTP_WEBHOOK_ID', '') or str(payload.get('id') or '') + event = process_polar_webhook(payload, event_id=event_id) return JsonResponse({'success': True, 'status': event.status}) From 73189031ff6704e845ab5b79314f9599e11f58fb Mon Sep 17 00:00:00 2001 From: Nick Bottone Date: Tue, 7 Jul 2026 01:10:05 -0400 Subject: [PATCH 07/28] chore: hide premium nav link before launch --- home/templates/home/base.html | 8 -------- 1 file changed, 8 deletions(-) diff --git a/home/templates/home/base.html b/home/templates/home/base.html index 970c734..a9a30bc 100644 --- a/home/templates/home/base.html +++ b/home/templates/home/base.html @@ -244,14 +244,6 @@ -
    Date: Wed, 28 Oct 2026 20:26:35 -0400 Subject: [PATCH 08/28] fix: preserve pending premium billing and launch review work --- .github/workflows/django.yml | 13 +- .github/workflows/tests.yml | 4 + SRCweb/settings.py | 13 +- docs/launch-readiness-2026-10-28.md | 192 ++++++++++++++++++ subscriptions/admin.py | 4 +- .../management/commands/sync_polar_usage.py | 22 ++ .../migrations/0004_usage_polar_sync_state.py | 39 ++++ subscriptions/models.py | 16 ++ subscriptions/services.py | 65 ++++-- subscriptions/tests.py | 49 +++++ 10 files changed, 397 insertions(+), 20 deletions(-) create mode 100644 docs/launch-readiness-2026-10-28.md create mode 100644 subscriptions/management/commands/sync_polar_usage.py create mode 100644 subscriptions/migrations/0004_usage_polar_sync_state.py diff --git a/.github/workflows/django.yml b/.github/workflows/django.yml index 81ca6c4..44bd359 100644 --- a/.github/workflows/django.yml +++ b/.github/workflows/django.yml @@ -29,6 +29,7 @@ jobs: run: python manage.py makemigrations --check --dry-run env: SECRET_KEY: ci-secret-key + API_KEY: ci-api-key NEW_AES_KEY: 0123456789abcdef DISCORD_CLIENT_SECRET: ci-discord-secret @@ -36,6 +37,7 @@ jobs: run: python manage.py migrate --noinput env: SECRET_KEY: ci-secret-key + API_KEY: ci-api-key NEW_AES_KEY: 0123456789abcdef DISCORD_CLIENT_SECRET: ci-discord-secret @@ -43,6 +45,7 @@ jobs: run: python manage.py check env: SECRET_KEY: ci-secret-key + API_KEY: ci-api-key NEW_AES_KEY: 0123456789abcdef DISCORD_CLIENT_SECRET: ci-discord-secret @@ -50,6 +53,7 @@ jobs: run: pytest env: SECRET_KEY: ci-secret-key + API_KEY: ci-api-key NEW_AES_KEY: 0123456789abcdef DISCORD_CLIENT_SECRET: ci-discord-secret @@ -77,11 +81,10 @@ jobs: - name: Collect static files run: python manage.py collectstatic --noinput env: - SECRET_KEY: ${{ secrets.BASE_KEY }} - SMTP_SERVER: ${{ secrets.SMTP_SERVER }} - SMTP_PASSWORD: ${{ secrets.SMTP_PASSWORD }} - NEW_AES_KEY: ${{ secrets.NEW_AES_KEY }} - DISCORD_CLIENT_SECRET: ${{ secrets.DISCORD_CLIENT_SECRET }} + SECRET_KEY: ci-secret-key + API_KEY: ci-api-key + NEW_AES_KEY: 0123456789abcdef + DISCORD_CLIENT_SECRET: ci-discord-secret - name: Restart server run: | diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 65f92c3..dc79e5a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -26,6 +26,7 @@ jobs: run: python manage.py makemigrations --check --dry-run env: SECRET_KEY: ci-secret-key + API_KEY: ci-api-key NEW_AES_KEY: 0123456789abcdef DISCORD_CLIENT_SECRET: ci-discord-secret @@ -33,6 +34,7 @@ jobs: run: python manage.py migrate --noinput env: SECRET_KEY: ci-secret-key + API_KEY: ci-api-key NEW_AES_KEY: 0123456789abcdef DISCORD_CLIENT_SECRET: ci-discord-secret @@ -40,6 +42,7 @@ jobs: run: python manage.py check env: SECRET_KEY: ci-secret-key + API_KEY: ci-api-key NEW_AES_KEY: 0123456789abcdef DISCORD_CLIENT_SECRET: ci-discord-secret @@ -47,5 +50,6 @@ jobs: run: pytest env: SECRET_KEY: ci-secret-key + API_KEY: ci-api-key NEW_AES_KEY: 0123456789abcdef DISCORD_CLIENT_SECRET: ci-discord-secret diff --git a/SRCweb/settings.py b/SRCweb/settings.py index 1ce2cce..912c776 100644 --- a/SRCweb/settings.py +++ b/SRCweb/settings.py @@ -11,6 +11,7 @@ """ from dotenv import load_dotenv +from django.core.exceptions import ImproperlyConfigured from pathlib import Path import os @@ -34,8 +35,18 @@ DISCORD_CLIENT_SECRET = os.getenv( "DISCORD_CLIENT_SECRET") or "DISCORD_CLIENT_SECRET" + +def required_non_placeholder_env(name, *, development_default=None): + value = os.getenv(name) or development_default + if os.getenv("PYTHON_ENV") == "development": + return value + if not value or value in {name, "replace-me", "changeme", "CHANGE_ME"}: + raise ImproperlyConfigured(f"{name} must be configured in non-development environments.") + return value + + # SECURITY WARNING: keep the API Key used in production secret! -API_KEY = os.getenv("API_KEY") or "API_KEY" +API_KEY = required_non_placeholder_env("API_KEY", development_default="API_KEY") # Polar subscriptions POLAR_SERVER = os.getenv("POLAR_SERVER") or "production" diff --git a/docs/launch-readiness-2026-10-28.md b/docs/launch-readiness-2026-10-28.md new file mode 100644 index 0000000..3756bee --- /dev/null +++ b/docs/launch-readiness-2026-10-28.md @@ -0,0 +1,192 @@ +# Second Robotics premium hosting: launch readiness + +Assessment date: October 28, 2026. + +**Recommendation: do not launch paid subscriptions yet.** Most of the feature exists, but the three release candidates have not been assembled, several normal user actions fail across service boundaries, and billing/recovery paths still have correctness gaps. The next milestone should be a coordinated private beta with Polar sandbox billing, followed by a public launch after the gates below pass. + +This is a repository, history, PR, and targeted code review. It does not establish what is currently deployed or configured on production hosts. No application code was changed, no stash was applied to your checkouts, no branches were switched, and no deployment or real billing operation was performed. Tests and reproductions used disposable environments and copies where necessary. This report is the only intentional repository addition from this assessment. + +## Where the work actually is + +| Repository | Current local state | Remote/review state | What to recover or integrate | +| --- | --- | --- | --- | +| SecondWebsite | `polar-subscriptions` at `9442fed`, plus local changes | [Draft PR #62](https://github.com/SecondRobotics/SecondWebsite/pull/62), open and reported mergeable when inspected. Its committed head has a successful test check. | Local API-key guard, Polar usage retry state/command, migration `0004`, admin/test/CI changes. Integrate six newer commits from remote `main`. | +| xRCRankedBot | Clean `main` at `66a707c` | Current main includes October game/autocomplete fixes. No open migration PR; the only open PR found was dependency PR #84. | Migration is in **`stash@{0}`**, based on `orchestrator-service` at `d437db3`. Several essential new files are in the stash's untracked-files parent. Recover onto current main carefully. | +| xRCServerOrchestrator | `main` at `aa276a4`, one commit ahead of `origin/main`, plus substantial local changes | GitHub `main` remains `cda4a4e`. No non-Dependabot PR was found. | Push/review the binary-diagnostics commit and integration changes: website callbacks/auth, casual lifecycle/idempotency, games endpoint, restart default, tests. | + +Important recovery identifiers: + +```text +Bot migration stash: 42f72f2f8abb38f21942e2008350a4fdc9239736 +Bot stash untracked tree: 6b30ab4f138d2bb467109eef9dbcb0e459bc6ec4 +Bot migration base branch: d437db311117b3bd6eb906dfcbeb8b7a1ede209b +Website subscription head: 9442fed5efc63b9ca417320fed37a1fcb94e8030 +Website remote main: 0cb4eb66fd82ebd1045a0e55eeefa45b0a4a38d7 +Bot remote main: 66a707ca9ed49e0dbf70bfbe34179d169a9d0dbd +Orchestrator remote main: cda4a4e784fbdac310092648def2bc32c4639639 +``` + +The bot stash includes new `orchestrator_client.py`, `website_client.py`, `cogs/casual.py`, orchestration tests, and CI. It removes the old hangout cog and changes ranked/server flows. Checking out `orchestrator-service` alone does **not** restore the migration. The stash was reviewed by extracting both the tracked snapshot and its untracked-files tree into a temporary directory. + +The website branch is seven feature commits ahead of the common ancestor and lacks six commits now on remote main, including high-score client gates, BioBuzz ranked/high-score changes, and the wheel update. The locally cached `origin/main` is stale; its apparent two-commit divergence understates the current remote divergence. [Current website main](https://github.com/SecondRobotics/SecondWebsite/tree/main). + +## Decisions recovered from earlier chats + +Reviewed the subscription design chat, the orchestrator and bot implementation chats, and the July 7 launch review/follow-up. + +- **Polar is the v1 paid provider**, using its Python SDK and credits/meter system. GitHub Sponsors and native Discord subscriptions are future possibilities, not unfinished v1 requirements. +- **Casual hosting goes through the website**, whether initiated on the site or in Discord. The website owns entitlement checks, reservations, and usage records. The orchestrator owns actual game processes. +- Ranked hosting stays free and launches directly from the bot to the orchestrator. Admin hosting remains staff-controlled. +- Bot/orchestrator calls into the website use the existing shared `API_KEY` in `X-API-KEY`; calls to the orchestrator use bearer tokens with scopes. +- **In-memory orchestrator state is intentional.** You explicitly accepted closing all servers on restart and relaunching them afterward. Persistence is not a prerequisite; reliable shutdown, website cleanup, and Discord recovery are. +- Migration baseline repair is a known deployment task. Legacy migration files were previously inconsistent/untracked; rehearse the existing-database transition rather than blindly applying a fresh-database recipe. +- Sandbox testing was accepted. The premium navbar link was deliberately hidden for initial direct-link production testing. `/subscriptions/` remains accessible. +- Agent handoff docs were not intended to be committed merely because they exist locally. Documentation cleanup is not itself a launch blocker. + +The July follow-up implemented the API-key guard, retryable Polar usage, bot player/OPR compatibility, and the default restart mode. Those changes are present, but largely remain local or stashed. Its prior “implemented” conclusion should not be read as “integrated and launch verified.” + +## What is already implemented + +| Area | Implemented behavior | Qualification | +| --- | --- | --- | +| Plans and entitlements | Database-configured plans, price/product/role IDs, monthly allowance, session/concurrency limits, priority selection, comp/provider-neutral entitlements, Django admin | Polar products/benefits still need matching configuration. Adding a provider choice does not implement that provider's billing integration. | +| Billing integration | SDK checkout, signed webhook validation, subscription upserts, meter balance reads, usage ingestion with stable session external IDs | Existing tests mock Polar. A real sandbox billing lifecycle remains unverified here. | +| Website casual hosting | Subscription page, plan/balance/session display, launch and stop forms, authenticated/service APIs, credit reservations, lifecycle callbacks | Start/stop contracts and recovery need fixes below. The page requires refresh for updated state. | +| Usage resilience | Local ledger, one usage entry per session, rounding, pending/failed/sent fields, retry command, admin visibility | Local changes; pending usage is not currently held against the spendable Polar balance. Ingestion still runs inline; exceptions are contained. | +| Orchestrator | Process launch/stop/restart/kill, port allocation, logs/chat/player/score APIs, moderation scopes, admin UI, metrics/readiness, binary updater, runtime limits, events, website callbacks with retries | Core exists on remote; important website-integration behavior is uncommitted locally. Real binary/host behavior needs a smoke test. | +| Bot migration | Orchestrator and website clients; ranked/admin launch/stop/status/moderation paths; website-proxied casual start/status/stop; inactivity and score events; old hangouts removed | In stash. Unit tests pass, but it predates newer main changes and has stale-cache behavior. | +| Previous display fixes | Accepts orchestrator `playerName`/`ipAddress` and split `redOpr`/`blueOpr` | Present in bot stash and covered by tests. | + +The seeded tiers remain Supporter at $5/month, 360 credits and 120-minute sessions; Host at $12/month, 1,500 credits and 240-minute sessions. These are code defaults, not verification of live Polar pricing. Both default to one concurrent server, and one credit represents a runtime minute with five-minute rounding. + +## Launch blockers and required fixes + +### 1. P1 — Discord casual launch is rejected by the orchestrator + +The website constructs `actor.type = "discord_bot"` for a bot-origin launch. The orchestrator only accepts `discord_user`, `website_user`, `service`, and `system`. + +Evidence: [website launch actor](/Users/nick/GitHub/SecondWebsite/subscriptions/services.py:334), [orchestrator actor schema](/Users/nick/GitHub/xRCServerOrchestrator/src/services/launchSchemas.ts:3). + +**Reproduction:** sending the website's bot-origin payload to the actual Fastify route with the fake runner returned HTTP 400 `VALIDATION_FAILED` at `actor.type`. + +**Required:** agree on an accepted actor representation, preserve the initiating Discord identity, and test bot → website → actual orchestrator validation. Current unit mocks accept invalid payloads. + +### 2. P1 — Website-proxied stop requests omit a required actor + +Website UI/API callers invoke `request_stop_server_session` without an actor. The website client then sends only `reason`, while the orchestrator stop schema requires `actor`. This affects website stops and casual stops proxied from Discord. + +Evidence: [stop service](/Users/nick/GitHub/SecondWebsite/subscriptions/services.py:574), [outgoing stop payload](/Users/nick/GitHub/SecondWebsite/subscriptions/orchestrator.py:83), [required actor](/Users/nick/GitHub/xRCServerOrchestrator/src/services/launchSchemas.ts:68). + +**Reproduction:** a valid website-style launch returned HTTP 201; stopping that session using the website's payload returned HTTP 400 `VALIDATION_FAILED`, `actor: Required`. + +**Required:** supply the appropriate actor or define a deliberate server-side default. Test both entry points against real request validation. + +### 3. P1 — Failed Polar usage becomes spendable again + +For Polar users, `available_credits` subtracts open-session allocations from the remote meter balance, but does not subtract stopped-session usage waiting for successful Polar sync. Once a session stops, its reservation disappears even if ingestion failed. The same risk needs consideration during meter-processing delay after ingestion succeeds. + +Evidence: [available credits](/Users/nick/GitHub/SecondWebsite/subscriptions/services.py:108), [usage sync](/Users/nick/GitHub/SecondWebsite/subscriptions/services.py:410). + +**Reproduction:** a user with a remote balance of 60 consumed 60 minutes; ingestion failed and the ledger correctly recorded 60 failed credits. The website nevertheless reported 60 available credits and authorized another 60-minute session. + +**Required:** reconcile unreflected local usage with Polar balance and active reservations. Retain a hold until usage is safely reflected, without subtracting already-reflected usage twice. Cover outages, retry, delayed meter updates, and concurrent launch requests. + +### 4. P1 — Callback ordering can resurrect completed sessions + +The orchestrator sends callbacks independently with retries. A delayed `ready` or `launch_accepted` event can arrive after `stopped`; the website unconditionally sets the session back to running/launching. The launch-response handler also unconditionally writes `launching`, potentially overwriting an earlier ready/terminal callback. Event-ID deduplication does not protect against different events arriving out of order. + +Evidence: [launch response update](/Users/nick/GitHub/SecondWebsite/subscriptions/services.py:340), [callback transitions](/Users/nick/GitHub/SecondWebsite/subscriptions/services.py:490), [independent callback sends](/Users/nick/GitHub/xRCServerOrchestrator/src/services/serverManager.ts:655). + +**Reproduction:** process `stopped`, then an older `ready` with a different event ID. The website ends up `running` while a usage ledger entry already exists. + +**Required:** enforce legal/monotonic lifecycle transitions, terminal-state protection, and freshness/ordering rules. Test delayed delivery and callbacks racing launch/stop responses. + +### 5. P1 — Failed Polar webhooks are acknowledged on retry without reprocessing + +`process_polar_webhook` writes an event before processing. On failure it preserves that event as failed. A later delivery with the same ID immediately returns the existing record, and the HTTP view responds successfully. Fixing a missing plan mapping then redelivering does not repair the entitlement. + +Evidence: [webhook deduplication and failure path](/Users/nick/GitHub/SecondWebsite/subscriptions/services.py:611), [HTTP response](/Users/nick/GitHub/SecondWebsite/subscriptions/views.py:108). + +**Reproduction:** deliver a subscription event with an unmapped product, add the product mapping, then redeliver the identical event ID. It remains failed and no entitlement is created. + +**Required:** deduplicate successfully completed events, support safe retry of failed processing, and expose a replay/recovery path. Also test older subscription events arriving after newer entitlement state. + +### 6. P1 — Destructive restart recovery is incomplete + +The chosen restart policy is valid, but changing the default to `stop` does not finish recovery: + +- Startup reconciliation sends SIGTERM to matching xRC processes without waiting for exit or escalating if they ignore it. Exceptions are logged, but reconciliation is marked complete in `finally`, so readiness can be reported despite failure. +- The website's stale-session command only selects `running`. Lost stop callbacks can leave `stopping` sessions open indefinitely; lost ready callbacks/launch races can leave `launching` sessions open indefinitely. Those sessions reserve credits and block new launches. +- Running sessions are billed using cleanup time, capped by allocation, rather than a proven actual stop time. After a crash/restart this can overstate runtime. +- Callback retries are finite and in memory. Once a terminal callback exhausts retries, there is no durable delivery/reconciliation path. A cron alone does not solve all the states above. +- The bot's event consumer handles match timer/inactivity/chat events, not server termination/restart events. Existing ranked Discord state is not automatically reconciled when the orchestrator loses its old session IDs. + +Evidence: [startup reconciliation](/Users/nick/GitHub/xRCServerOrchestrator/src/services/serverManager.ts:751), [process termination](/Users/nick/GitHub/xRCServerOrchestrator/src/services/processInspector.ts:50), [website cleanup](/Users/nick/GitHub/SecondWebsite/subscriptions/services.py:592), bot stash `cogs/server.py:552`. + +**Reproduction:** stale `launching` and `stopping` sessions were both ignored by `expire_stale_sessions`. + +**Required:** verify processes have actually exited before accepting fresh work; keep readiness failed on reconciliation failure; reconcile every open website state with a defensible billing cutoff; define ranked Discord cleanup on restart. This can remain a simple destructive-restart design without adding persistent live-session storage. + +### 7. P1 — The local billing migration fails the required CI gate + +The model's unnamed `polar_status` index generates `subscriptio_polar_s_d17737_idx`; migration `0004` explicitly creates `subscriptio_polar_s_9533a3_idx`. Django detects a missing migration. + +Evidence: [model index](/Users/nick/GitHub/SecondWebsite/subscriptions/models.py:238), [migration index](/Users/nick/GitHub/SecondWebsite/subscriptions/migrations/0004_usage_polar_sync_state.py:35). + +**Reproduction:** `manage.py makemigrations --check --dry-run` exits 1 on both the old venv and a fresh Python 3.12/Django 5.2.18 environment. Django 5 proposes a `RenameIndex` migration. + +**Required:** align model/migration index names or add the appropriate migration, then pass the gate. This is separate from the already-known production baseline rehearsal. + +### 8. P1 — Assemble the release without losing newer games or the dropdown fix + +The recovered bot migration and orchestrator know game codes through `22`. Bot main also supports Override (`23`) and BioBuzz (`24`). The migration still uses fixed `@app_commands.choices` lists, including its new casual command. Blindly restoring old config loses games; adding newer games while retaining fixed choices risks reintroducing the October startup failure. + +Evidence: [orchestrator catalog](/Users/nick/GitHub/xRCServerOrchestrator/src/config/games.ts:3), [current bot catalog](/Users/nick/GitHub/xRCRankedBot/config.py:101), bot stash `cogs/casual.py:83` and its ranked/server decorators. + +**Reproduction:** actual orchestrator route requests for codes `23` and `24` both returned HTTP 400 `Unknown gameCode`. + +**Required:** recover the stash onto current bot main, preserve its autocomplete/tests/CI and game names, give the new casual command compatible selection, update the orchestrator catalog and binary, and integrate current website main. Verify slash-command registration and launching both newer games. Commit and review all three resulting release candidates. + +## Other remaining work + +- **Bot casual status cache:** `_get_active_website_session` returns a cached active record without refreshing it. After automatic expiry or a website stop, `/casual_status` can indefinitely show an old launching/running session. Query the website or use bounded cache freshness. Also review the bot's 180-minute input ceiling against Host's advertised 240-minute sessions. Evidence: bot stash `cogs/casual.py:71–89`. +- **Advertised benefits:** seeded Host copy promises saved presets/faster relaunch, but no preset implementation was found. `discord_role_id` is stored/admin-editable; no role-sync implementation was found in these repositories. Either configure/test Polar's Discord benefit externally, implement missing benefits, or change launch copy. Do not sell promises without a working delivery path. +- **Orchestrator retry test:** one complete check run failed and a rerun passed. The test waits for any two fetches, then asserts two `launch_accepted` fetches; a different callback can satisfy the wait early. Wait for the specific retried event. Evidence: [retry test](/Users/nick/GitHub/xRCServerOrchestrator/test/api.test.ts:408). +- **Deployment configuration:** explicitly configure production bearer tokens/scopes; the orchestrator has a permissive default development token. Configure matching shared keys, correct base/public host URLs, real process mode/binary, paths/permissions, ports/firewall, and exactly one chat-relay owner. `/readyz` alone does not validate billing or callback credentials. +- **Recurring jobs and observability:** install and verify `sync_polar_usage` and corrected stale-session cleanup on the actual host. Alert on failed usage, failed webhooks/callbacks, stale open sessions, and failed startup reconciliation. No live scheduler configuration was verified. +- **Dependencies:** there are open dependency PRs in all three repositories. Triage them in the release dependency review; this assessment did not perform a complete vulnerability audit. Avoid assuming every dependency bump is a required compatible change. + +## Verification performed + +| Check | Result | What it proves / limits | +| --- | --- | --- | +| Website existing pytest suite, old `.venv` | 25 passed | Old local runtime is Python 3.9/Django 3.2; this alone was insufficient. | +| Website existing pytest suite, fresh declared dependencies | **25 passed** on Python 3.12.9/Django 5.2.18 | Fresh test database migrations execute; service tests remain heavily mocked. | +| Django system checks, fresh runtime | Passed | Settings/app checks; no live service validation. | +| Missing-migration check, fresh runtime | **Failed** | Index naming drift described above. | +| Bot recovered stash unit tests | **16 passed** | Includes 14 orchestration/client/command tests and two log-regex tests. Uses existing Python 3.9 venv; not the final merged bot candidate or its CI runtime. | +| Bot current main tests in existing venv | Blocked at import: missing `psutil` | Environment deficiency; does not establish a product regression. Final merged candidate needs a fresh Python 3.10 dependency install and full tests. | +| Orchestrator admin + TypeScript build | Passed | Temporary source copy with installed local dependencies; Node 24 locally versus Node 22 in CI. | +| Orchestrator API suite | 19/20 on first check, **20/20 on rerun** | Retry-test race above. Fake processes, not real xRC. | +| Targeted website reproductions | **Four confirmed**, including on fresh runtime | Failed usage reuse; delayed-ready resurrection; failed-webhook replay; cleanup ignoring launching/stopping. Assertions intentionally check for the observed defects. | +| Actual Fastify route contract probes | **Four failures confirmed** | Invalid Discord actor, missing stop actor, missing Override, missing BioBuzz. No real game process or billing calls. | +| Website PR #62 committed-head CI | Successful `test` check at `9442fed` | Does not cover local retry/guard/migration changes. [CI run](https://github.com/SecondRobotics/SecondWebsite/actions/runs/28843125089). | +| Bot current remote main CI | Successful `test` and `check-lock` checks at `66a707c` | Does not cover the stashed migration or its eventual integration. | +| Orchestrator current remote main CI | **Failed** `build-and-test` at `cda4a4e` | [Existing failed job](https://github.com/SecondRobotics/xRCServerOrchestrator/actions/runs/26490784587/job/78077388472). Retained annotation: long-poll event test expected HTTP 200, received 404 (`test/api.test.ts:230`). Full logs have expired (HTTP 410). The local candidate results above are separate; require fresh CI on the assembled release. | + +The modern Python environment and orchestrator/bot copies were disposable. No production SQLite migration, live checkout, real Polar ingestion, Discord registration, game-client join, or remote restart was exercised. Existing green unit tests do not imply the services work together; the contract probes demonstrate why an integration test is necessary. + +## Recommended completion sequence + +1. **Recover and pin release candidates.** Preserve the bot stash, recover it onto current main, integrate website main, and put orchestrator local changes into reviewable commits. Include untracked implementation files and migration `0004`; exclude unrelated generated static files and temporary handoff docs as appropriate. Record all three SHAs. +2. **Fix the blockers above.** Start with actor contracts and migration drift, then accounting, webhook/callback ordering, and restart/reconciliation. Add targeted regression tests and one actual-schema integration test between website and orchestrator. Resolve catalog/autocomplete drift as part of assembly. +3. **Run the intended CI environments.** Website Python 3.12 with declared dependencies; bot Python 3.10 with its merged tests; orchestrator Node 22 with `npm ci` and `npm run check`. Require clean migration checks. Rehearse the baseline transition on a copy of the current production SQLite database, then verify ordinary migrations and seeded plans. Back up before production migration. +4. **Run a small sandbox acceptance matrix.** Use Polar sandbox products/meter/credits benefits and a test Discord guild. Verify checkout → signed entitlement event → launch from website and Discord → ready → client join → stop → one ledger entry → one reflected Polar charge. Confirm balance subtraction and five-minute rounding. Test no entitlement, exhausted balance, concurrency, max duration, failed launch, and subscription cancellation/end-of-period behavior. +5. **Exercise failure recovery.** Drop/reorder callbacks; make Polar unavailable while stopping; restore it and retry without overspending/double charging. Restart/kill the orchestrator with one casual and one ranked session active: all old processes must die, paid sessions must finalize once, credits/concurrency must recover, Discord match resources must be cleared, and fresh launches must succeed. Test callbacks exhausted while the website is down. +6. **Real xRC/ranked smoke.** Use the actual Linux binary: launch/join a casual session and a ranked match; inspect players, scoped IP visibility, chat relay, scores/OPR, match-end/inactivity workflows, ELO submission, and cleanup. Include Override and BioBuzz after catalog/binary alignment. +7. **Coordinated maintenance cutover.** Treat this as one release. Pause new launches and drain active legacy bot servers before starting an orchestrator configured to terminate existing xRC processes. Deploy/migrate the website, start the verified orchestrator, then switch the bot to the matching release. Enable recurring jobs and monitoring, and run direct-link acceptance with the navbar still hidden. Define rollback before beginning: stop new launches, drain processes, revert matching service versions, retain database backups, and reconcile outstanding billing rather than blindly rolling back schema/data. +8. **Public enablement.** Complete the hidden beta, confirm role benefits and tier copy, verify production Polar configuration and a controlled paid lifecycle, then expose the premium link and announce. Switching out of sandbox needs its own production product/meter/key/benefit verification. + +## Practical readiness summary + +The foundation is substantial: this is integration and reliability completion, not a project that needs to be rebuilt. The most immediate problems are concrete and reproducible. A calendar launch estimate would be premature until the bot stash is integrated and the failure-recovery fixes pass. Call the system ready only when one pinned three-repository release passes the sandbox matrix, real ranked/game smoke, migration rehearsal, and destructive-restart test. diff --git a/subscriptions/admin.py b/subscriptions/admin.py index 72c40aa..7934a61 100644 --- a/subscriptions/admin.py +++ b/subscriptions/admin.py @@ -53,8 +53,8 @@ class ServerSessionAdmin(admin.ModelAdmin): @admin.register(UsageLedgerEntry) class UsageLedgerEntryAdmin(admin.ModelAdmin): - list_display = ('user', 'entitlement', 'credits', 'reason', 'billing_period_start', 'billing_period_end', 'created_at') - list_filter = ('reason',) + list_display = ('user', 'entitlement', 'credits', 'reason', 'polar_status', 'polar_last_attempt_at', 'billing_period_start', 'billing_period_end', 'created_at') + list_filter = ('reason', 'polar_status') search_fields = ('user__username', 'user__display_name') diff --git a/subscriptions/management/commands/sync_polar_usage.py b/subscriptions/management/commands/sync_polar_usage.py new file mode 100644 index 0000000..d388b5d --- /dev/null +++ b/subscriptions/management/commands/sync_polar_usage.py @@ -0,0 +1,22 @@ +from django.core.management.base import BaseCommand + +from subscriptions.models import UsageLedgerEntry +from subscriptions.services import sync_pending_polar_usage + + +class Command(BaseCommand): + help = 'Retry pending or failed Polar usage events for premium casual server sessions.' + + def add_arguments(self, parser): + parser.add_argument( + '--limit', + type=int, + default=100, + help='Maximum number of usage entries to retry.', + ) + + def handle(self, *args, **options): + entries = sync_pending_polar_usage(limit=options['limit']) + sent = sum(1 for entry in entries if entry.polar_status == UsageLedgerEntry.POLAR_STATUS_SENT) + failed = sum(1 for entry in entries if entry.polar_status == UsageLedgerEntry.POLAR_STATUS_FAILED) + self.stdout.write(self.style.SUCCESS(f'Synced {sent} Polar usage event(s); {failed} still failed.')) diff --git a/subscriptions/migrations/0004_usage_polar_sync_state.py b/subscriptions/migrations/0004_usage_polar_sync_state.py new file mode 100644 index 0000000..7b6d28d --- /dev/null +++ b/subscriptions/migrations/0004_usage_polar_sync_state.py @@ -0,0 +1,39 @@ +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('subscriptions', '0003_use_polar_credit_units'), + ] + + operations = [ + migrations.AddField( + model_name='usageledgerentry', + name='polar_error', + field=models.TextField(blank=True), + ), + migrations.AddField( + model_name='usageledgerentry', + name='polar_last_attempt_at', + field=models.DateTimeField(blank=True, null=True), + ), + migrations.AddField( + model_name='usageledgerentry', + name='polar_status', + field=models.CharField( + choices=[ + ('not_required', 'Not Required'), + ('pending', 'Pending'), + ('sent', 'Sent'), + ('failed', 'Failed'), + ], + default='not_required', + max_length=24, + ), + ), + migrations.AddIndex( + model_name='usageledgerentry', + index=models.Index(fields=['polar_status'], name='subscriptio_polar_s_9533a3_idx'), + ), + ] diff --git a/subscriptions/models.py b/subscriptions/models.py index a75a95f..8eb03ee 100644 --- a/subscriptions/models.py +++ b/subscriptions/models.py @@ -205,11 +205,23 @@ class UsageLedgerEntry(models.Model): REASON_SERVER_SESSION = 'server_session' REASON_ADMIN_ADJUSTMENT = 'admin_adjustment' + POLAR_STATUS_NOT_REQUIRED = 'not_required' + POLAR_STATUS_PENDING = 'pending' + POLAR_STATUS_SENT = 'sent' + POLAR_STATUS_FAILED = 'failed' + REASON_CHOICES = [ (REASON_SERVER_SESSION, 'Server Session'), (REASON_ADMIN_ADJUSTMENT, 'Admin Adjustment'), ] + POLAR_STATUS_CHOICES = [ + (POLAR_STATUS_NOT_REQUIRED, 'Not Required'), + (POLAR_STATUS_PENDING, 'Pending'), + (POLAR_STATUS_SENT, 'Sent'), + (POLAR_STATUS_FAILED, 'Failed'), + ] + user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='premium_usage_entries') entitlement = models.ForeignKey(SubscriptionEntitlement, on_delete=models.CASCADE, related_name='usage_entries') server_session = models.OneToOneField(ServerSession, on_delete=models.SET_NULL, related_name='usage_entry', null=True, blank=True) @@ -217,6 +229,9 @@ class UsageLedgerEntry(models.Model): billing_period_end = models.DateTimeField() credits = models.IntegerField() reason = models.CharField(max_length=32, choices=REASON_CHOICES) + polar_status = models.CharField(max_length=24, choices=POLAR_STATUS_CHOICES, default=POLAR_STATUS_NOT_REQUIRED) + polar_error = models.TextField(blank=True) + polar_last_attempt_at = models.DateTimeField(null=True, blank=True) metadata = models.JSONField(default=dict, blank=True) created_at = models.DateTimeField(auto_now_add=True) @@ -224,6 +239,7 @@ class Meta: ordering = ['-created_at'] indexes = [ models.Index(fields=['user', 'billing_period_start', 'billing_period_end']), + models.Index(fields=['polar_status']), ] def __str__(self): diff --git a/subscriptions/services.py b/subscriptions/services.py index e4f2e02..03e70c8 100644 --- a/subscriptions/services.py +++ b/subscriptions/services.py @@ -401,6 +401,7 @@ def _rounded_runtime_minutes_from_seconds(runtime_seconds, allocated_minutes): def _create_usage_entry(session, credits, metadata): + polar_required = session.entitlement.provider == SubscriptionEntitlement.PROVIDER_POLAR usage, created = UsageLedgerEntry.objects.get_or_create( server_session=session, defaults={ @@ -410,24 +411,64 @@ def _create_usage_entry(session, credits, metadata): 'billing_period_end': session.entitlement.current_period_end, 'credits': credits, 'reason': UsageLedgerEntry.REASON_SERVER_SESSION, + 'polar_status': UsageLedgerEntry.POLAR_STATUS_PENDING if polar_required else UsageLedgerEntry.POLAR_STATUS_NOT_REQUIRED, 'metadata': metadata, }, ) - if created and session.entitlement.provider == SubscriptionEntitlement.PROVIDER_POLAR: - try: - response = polar_gateway.ingest_usage_event(session, credits, session.stopped_at or timezone.now()) - except polar_gateway.PolarGatewayError as exc: - raise SubscriptionError(str(exc), status_code=503, code='POLAR_USAGE_INGEST_FAILED') from exc - usage.metadata = { - **usage.metadata, - 'polar_event_name': getattr(settings, 'POLAR_CASUAL_SERVER_EVENT_NAME', '') or 'casual_server_credits', - 'polar_inserted': getattr(response, 'inserted', None), - 'polar_duplicates': getattr(response, 'duplicates', None), - } - usage.save(update_fields=['metadata']) + if created and polar_required: + sync_polar_usage_entry(usage) return usage +def sync_polar_usage_entry(usage): + usage = UsageLedgerEntry.objects.select_related('entitlement', 'server_session', 'user').get(pk=usage.pk) + if usage.entitlement.provider != SubscriptionEntitlement.PROVIDER_POLAR: + return usage + if usage.polar_status == UsageLedgerEntry.POLAR_STATUS_SENT: + return usage + if usage.server_session is None: + usage.polar_status = UsageLedgerEntry.POLAR_STATUS_FAILED + usage.polar_error = 'Usage entry is missing its server session.' + usage.polar_last_attempt_at = timezone.now() + usage.save(update_fields=['polar_status', 'polar_error', 'polar_last_attempt_at']) + return usage + + usage.polar_last_attempt_at = timezone.now() + try: + response = polar_gateway.ingest_usage_event( + usage.server_session, + usage.credits, + usage.server_session.stopped_at or usage.created_at or timezone.now(), + ) + except polar_gateway.PolarGatewayError as exc: + usage.polar_status = UsageLedgerEntry.POLAR_STATUS_FAILED + usage.polar_error = str(exc) + usage.save(update_fields=['polar_status', 'polar_error', 'polar_last_attempt_at']) + return usage + + usage.polar_status = UsageLedgerEntry.POLAR_STATUS_SENT + usage.polar_error = '' + usage.metadata = { + **usage.metadata, + 'polar_event_name': getattr(settings, 'POLAR_CASUAL_SERVER_EVENT_NAME', '') or 'casual_server_credits', + 'polar_inserted': getattr(response, 'inserted', None), + 'polar_duplicates': getattr(response, 'duplicates', None), + } + usage.save(update_fields=['polar_status', 'polar_error', 'polar_last_attempt_at', 'metadata']) + return usage + + +def sync_pending_polar_usage(limit=100): + pending = UsageLedgerEntry.objects.filter( + entitlement__provider=SubscriptionEntitlement.PROVIDER_POLAR, + polar_status__in=[ + UsageLedgerEntry.POLAR_STATUS_PENDING, + UsageLedgerEntry.POLAR_STATUS_FAILED, + ], + ).order_by('created_at')[:limit] + return [sync_polar_usage_entry(usage) for usage in pending] + + def _record_orchestrator_usage(session, stopped_at, runtime_seconds=None): if runtime_seconds is not None: billed_minutes = _rounded_runtime_minutes_from_seconds(runtime_seconds, session.allocated_minutes) diff --git a/subscriptions/tests.py b/subscriptions/tests.py index 9a37db3..fdfdb65 100644 --- a/subscriptions/tests.py +++ b/subscriptions/tests.py @@ -1,13 +1,16 @@ import json +from io import StringIO from datetime import timedelta from unittest.mock import patch +from django.core.management import call_command from django.test import TestCase, override_settings from django.utils import timezone from rest_framework.authtoken.models import Token from rest_framework.test import APIClient from discordoauth2.models import User +from subscriptions import polar_gateway from subscriptions.models import ServerSession, SubscriptionEntitlement, SubscriptionPlan, UsageLedgerEntry, WebhookEvent from subscriptions.services import ( SubscriptionError, @@ -182,9 +185,55 @@ def test_polar_usage_is_ingested_when_session_stops(self, mock_balance, mock_ing usage = UsageLedgerEntry.objects.get(server_session=session) self.assertEqual(usage.credits, 10) + self.assertEqual(usage.polar_status, UsageLedgerEntry.POLAR_STATUS_SENT) + self.assertEqual(usage.polar_error, '') self.assertEqual(usage.metadata['polar_inserted'], 1) mock_ingest.assert_called_once() + @patch('subscriptions.polar_gateway.ingest_usage_event') + @patch('subscriptions.polar_gateway.get_customer_credit_balance') + def test_polar_usage_failure_keeps_local_stop_finalized_for_retry(self, mock_balance, mock_ingest): + mock_balance.return_value = 60 + mock_ingest.side_effect = polar_gateway.PolarGatewayError('temporary polar outage') + user = create_user() + create_entitlement(user, provider=SubscriptionEntitlement.PROVIDER_POLAR, credits=360) + session = start_server_session(user, requested_minutes=60) + session.started_at = timezone.now() - timedelta(minutes=6) + session.save(update_fields=['started_at']) + + stop_server_session(session) + + session.refresh_from_db() + usage = UsageLedgerEntry.objects.get(server_session=session) + self.assertEqual(session.status, ServerSession.STATUS_STOPPED) + self.assertEqual(usage.credits, 10) + self.assertEqual(usage.polar_status, UsageLedgerEntry.POLAR_STATUS_FAILED) + self.assertIn('temporary polar outage', usage.polar_error) + + @patch('subscriptions.polar_gateway.ingest_usage_event') + @patch('subscriptions.polar_gateway.get_customer_credit_balance') + def test_sync_polar_usage_retries_failed_entries(self, mock_balance, mock_ingest): + mock_balance.return_value = 60 + mock_ingest.side_effect = [ + polar_gateway.PolarGatewayError('temporary polar outage'), + type('PolarIngestResponse', (), {'inserted': 1, 'duplicates': 0})(), + ] + user = create_user() + create_entitlement(user, provider=SubscriptionEntitlement.PROVIDER_POLAR, credits=360) + session = start_server_session(user, requested_minutes=60) + session.started_at = timezone.now() - timedelta(minutes=6) + session.save(update_fields=['started_at']) + stop_server_session(session) + + output = StringIO() + call_command('sync_polar_usage', stdout=output) + + usage = UsageLedgerEntry.objects.get(server_session=session) + self.assertEqual(usage.polar_status, UsageLedgerEntry.POLAR_STATUS_SENT) + self.assertEqual(usage.polar_error, '') + self.assertIn('Synced 1 Polar usage event', output.getvalue()) + self.assertEqual(mock_ingest.call_count, 2) + def test_expire_stale_sessions_marks_expired_and_records_usage(self): user = create_user() create_entitlement(user) From 8a5163dcad37eedadae9fe06470e40ec8531cac1 Mon Sep 17 00:00:00 2001 From: Nick Bottone Date: Wed, 28 Oct 2026 20:52:22 -0400 Subject: [PATCH 09/28] fix: harden premium billing and server lifecycle for launch --- conftest.py | 20 ++ docs/premium-release-checklist.md | 108 +++++++ requirements.txt | 14 +- subscriptions/admin.py | 14 +- subscriptions/api/views.py | 4 +- subscriptions/credits.py | 39 +++ .../commands/check_premium_launch.py | 30 ++ .../commands/replay_subscription_webhooks.py | 23 ++ ...rversession_billing_period_end_and_more.py | 45 +++ .../migrations/0006_correct_host_benefits.py | 13 + subscriptions/models.py | 21 +- subscriptions/orchestrator.py | 8 + subscriptions/polar_gateway.py | 8 +- subscriptions/services.py | 303 +++++++++++------- subscriptions/test_launch_readiness.py | 159 +++++++++ .../test_orchestrator_integration.py | 89 +++++ subscriptions/tests.py | 33 +- tools/rehearse_premium_migrations.py | 45 +++ 18 files changed, 829 insertions(+), 147 deletions(-) create mode 100644 conftest.py create mode 100644 docs/premium-release-checklist.md create mode 100644 subscriptions/credits.py create mode 100644 subscriptions/management/commands/check_premium_launch.py create mode 100644 subscriptions/management/commands/replay_subscription_webhooks.py create mode 100644 subscriptions/migrations/0005_serversession_billing_period_end_and_more.py create mode 100644 subscriptions/migrations/0006_correct_host_benefits.py create mode 100644 subscriptions/test_launch_readiness.py create mode 100644 subscriptions/test_orchestrator_integration.py create mode 100644 tools/rehearse_premium_migrations.py diff --git a/conftest.py b/conftest.py new file mode 100644 index 0000000..a2c0e17 --- /dev/null +++ b/conftest.py @@ -0,0 +1,20 @@ +import os +from pathlib import Path +import tempfile + +import pytest +from django.conf import settings + + +@pytest.fixture(scope='session') +def django_db_modify_db_settings(): + if not os.environ.get('ORCHESTRATOR_REPO'): + yield + return + # Real callbacks use separate HTTP/DB threads. SQLite's shared in-memory + # cache raises SQLITE_LOCKED instead of honoring the busy timeout. Exercise + # the production file-backed locking behavior in an isolated test DB. + with tempfile.TemporaryDirectory(prefix='premium-http-db-') as directory: + settings.DATABASES['default'].setdefault('TEST', {})['NAME'] = str(Path(directory) / 'test.sqlite3') + settings.DATABASES['default'].setdefault('OPTIONS', {})['timeout'] = 20 + yield diff --git a/docs/premium-release-checklist.md b/docs/premium-release-checklist.md new file mode 100644 index 0000000..df24a5f --- /dev/null +++ b/docs/premium-release-checklist.md @@ -0,0 +1,108 @@ +# Premium hosting release checklist + +This is a coordinated website, ranked-bot, and orchestrator release. Nothing in this preparation authorizes merging or deployment. Keep the premium navbar link hidden for the initial beta. + +## Prepared in code + +- Recovered the bot migration without dropping the original stash; retained Override/BioBuzz and searchable game selection. +- Aligned actual casual launch/stop contracts. Added a local HTTP integration test with real Django and Fastify routes, both launch origins, callbacks, and the two newer games. +- Protected terminal states and launch/stop response races, made failed Polar webhooks replayable, and rejected stale entitlement updates. +- Recorded billing periods on reservations, serialized reservation decisions per user (including SQLite), and kept uncertain launch outcomes reserved until reconciliation. +- Moved Polar ingestion to the scheduled worker. A durable local hold protects credits during ingestion failures and meter aggregation delay. Portal credit grants still affect available balance. +- Added conservative reconciliation for every open session state. Network failure does not prove a process stopped. A reachable orchestrator with a missing session finalizes only last-confirmed runtime. +- Added a file-backed callback outbox that survives restart, process-exit verification/escalation, failed-start cleanup, and bot cleanup of lost ranked sessions. +- Preserved previous score files in archives before port reuse. Scoped event/IP and raw-log reads. Removed the unimplemented saved-presets promise from default Host copy. + +## Actions needed from Nick, in order + +### 1. Finish the existing Polar sandbox configuration + +Local configuration already specifies `POLAR_SERVER=sandbox`, an access token, and a meter ID. Do not paste secrets into chat. Add missing values to the appropriate local/private configuration or secret store: + +1. Configure the sandbox webhook endpoint `/subscriptions/polar/webhook/` on a reachable **test** website URL and save its signing secret as `POLAR_WEBHOOK_SECRET`. A local test receiver needs a tunnel or test host; do not point sandbox events at the live production database. +2. Ensure the token can create checkouts, read customer meters, and ingest usage events. Keep all products, meter IDs, and credentials in the same sandbox organization. +3. Map each active Django `SubscriptionPlan.polar_product_id` to its sandbox product in Django admin. Configure recurring credits to match the advertised plan: Supporter 360 and Host 1,500; one credit is one runtime minute. Configure billing-cycle expiry/no rollover and no paid overage. Verify these behaviors in sandbox rather than assuming defaults. +4. Use a **dedicated meter** for this application's `casual_server_credits` events with a sum of `metadata.credits`. Do not publish unrelated usage to it or manually inject consumption into it. The reconciliation hold uses this dedicated meter's `consumed_units`; it allows credit grants in the portal and does not release holds merely because ingestion was accepted. [Polar customer meter fields](https://polar.sh/docs/api-reference/customer-meters/get). +5. Configure and test Polar's Discord role benefit for each product, or remove the corresponding role promise from the editable plan copy. The website's role ID field does not itself grant Discord roles. Saved presets are deferred and no longer advertised by the default Host plan. +6. Configure `POLAR_CUSTOMER_PORTAL_URL` and test the manage-billing flow. + +### 2. Provide an isolated Linux/Discord acceptance environment + +Use a disposable Linux machine or a test host, a current xRC Linux binary supporting Override/BioBuzz, and a test Discord guild/bot identity. Do not start this orchestrator alongside live unmanaged xRC servers: the intended startup policy terminates existing xRC processes visible to its account. Isolation by host or OS account is necessary for a safe test. + +Set up private configuration, without committing secrets: + +| Direction | Configuration | +| --- | --- | +| Bot + orchestrator → website | Matching strong `API_KEY`; header `X-API-KEY` | +| Website → orchestrator | `ORCHESTRATOR_API_BASE_URL`, `ORCHESTRATOR_API_TOKEN`; token scopes `servers:casual:create,servers:read,servers:stop` | +| Bot → orchestrator | `ORCHESTRATOR_BASE_URL`, `ORCHESTRATOR_TOKEN`; scopes `servers:ranked:create,servers:read,servers:stop` | +| Bot privileged operations | Separate moderation/admin tokens; raw logs require `logs:read` and `moderation:read`; admin operations need their corresponding admin/create/stop/restart scopes | +| Orchestrator → website | `SECONDWEBSITE_BASE_URL` pointing at the same test website/database | +| Client connections | `SERVICE_PUBLIC_HOST`, reachable server port pool and required game traffic allowed by the test host firewall | + +The local website is currently missing its orchestrator URL/token. Production mode also requires explicit non-development service tokens, a non-placeholder shared key, real process mode, and stop-on-restart reconciliation. + +Choose one chat-relay owner: orchestrator webhook **or** bot event relay. Preserve runtime log/score directories and `EVENT_LOG_DIR/website-callbacks` across service restarts. Pending callbacks include connection passwords: protect the directory and its backups. No database for live orchestrator state is needed. Periodically review archived score directories and retain/remove them according to your chosen retention policy. + +### 3. Run the sandbox acceptance gate before approving merges + +Once the environment is ready, the candidates must demonstrate: + +1. Sandbox checkout activates the correct entitlement/credits. Role benefit works and manage billing opens. Test downgrade, cancellation through period end, renewal/reset, and past-due/revoked access. +2. Website launch and Discord `/casual_start` both produce a ready real server. Join from a game client. Confirm connection/password, players, chat, and scores. Include Override and BioBuzz. +3. Stop from both entry points. Verify exactly one usage record and one Polar event per session, five-minute rounding, and eventual matching meter debit. Test max duration, exhausted balance, and concurrent launch rejection. +4. Make Polar temporarily unavailable while stopping. Confirm local finalization succeeds, credits remain held, and `sync_polar_usage` later recovers without reusing or charging the same credits twice. +5. Stop the test website long enough to exhaust immediate callback retries, restore it, and confirm the outbox replays. Restart the orchestrator with one ranked and one casual session: old processes must exit, casual usage finalizes once, Discord match resources clear, and fresh launches succeed. A mere network outage must not clear a live ranked match. +6. Complete a real ranked match: queue, roles/channels, free server launch, score/OPR display, ELO submission, inactivity behavior, and cleanup. Verify IPs are absent from ordinary event/player reads and available only to authorized moderation callers. + +The automated checks cover the service contracts and mocked failure cases. They cannot prove Discord registration, a real game-client join, or live Polar benefit/meter configuration. + +### 4. Rehearse against a fresh production database backup + +The old local SQLite copy migrated successfully with all modeled columns and no foreign-key violations. It already contained subscriptions migrations through `0003`, so this does **not** replace testing a current backup from the actual production host. + +Run against a downloaded backup, using a dependency-equipped Python 3.12 environment: + +```sh +python tools/rehearse_premium_migrations.py --source /absolute/path/to/production-backup.sqlite3 +``` + +The utility opens the source read-only, makes a disposable backup, runs `migrate --fake-initial` on the copy, and checks model columns/foreign keys. If it fails, stop and resolve the actual schema/history mismatch before approving deployment. Never delete migration records or fake missing schema changes blindly. After the baseline transition, use committed migrations normally. Back up production again immediately before any eventual migration. + +### 5. Approve the reviewed release and a later coordinated cutover + +After green CI, sandbox/real-game acceptance, and the current-backup rehearsal, review the three PRs together. Explicit approval to merge/deploy remains a separate step. At cutover: pause new launches, drain legacy servers, back up/migrate the website, start the orchestrator, then switch the bot to the matching release. Keep public discovery disabled during the initial beta. Record all deployed SHAs. + +Install and verify these recurring tasks on the actual host at cutover, using its Python environment and environment variables: + +```cron +*/1 * * * * cd /absolute/path/to/website && /absolute/path/to/python manage.py sync_polar_usage +*/2 * * * * cd /absolute/path/to/website && /absolute/path/to/python manage.py expire_premium_sessions +``` + +Avoid overlapping scheduler runs; capture output and alert on failed usage/webhooks, an accumulating callback outbox, and stale open sessions. Retry corrected failed entitlement events with `python manage.py replay_subscription_webhooks`. Run `python manage.py check_premium_launch` for secret-safe local prerequisite checks; it does not contact external services or replace acceptance testing. + +Switching from sandbox to production requires production product/meter/benefit IDs and keys, verification of a controlled paid lifecycle, and a separate enablement decision. Do not reuse sandbox IDs. Remove the navbar hiding only after the beta succeeds. + +## Developer verification + +```sh +# Website: Python 3.12 with requirements.txt installed +python manage.py makemigrations --check --dry-run +python manage.py check +python -m pytest + +# Bot: Python 3.10 with requirements.txt installed +python -m unittest discover -s tests -v +python -m unittest test_regex_log -v + +# Orchestrator: Node 22 +npm ci +npm run check + +# Local cross-service acceptance (orchestrator must already be built) +ORCHESTRATOR_REPO=/absolute/path/to/xRCServerOrchestrator python -m pytest subscriptions/test_orchestrator_integration.py -o addopts='' +``` + +Use dummy settings-import secrets for CI/local tests; no real Polar or Discord credentials are needed. The orchestrator CI also checks out a matching website candidate branch, falling back to website main once the coordinated work is integrated. Production deployment workflows are not invoked by release-branch pushes. diff --git a/requirements.txt b/requirements.txt index cb6bb46..7a9737b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,23 +1,23 @@ appdirs==1.4.4 asgiref>=3.8.1 autopep8==1.6.0 -certifi==2024.7.4 +certifi==2026.10.19 charset-normalizer==2.0.12 distlib==0.3.4 Django>=5.0,<6.0 djangorestframework>=3.15.0,<4.0 -idna==3.3 +idna==3.15 pycodestyle==2.8.0 pycryptodome==3.19.1 polar-sdk==0.31.7 -python-dotenv==0.20.0 +python-dotenv==1.2.2 pytz==2022.1 -requests==2.32.4 +requests==2.33.0 six==1.16.0 -sqlparse==0.5.0 +sqlparse==0.6.0 toml==0.10.2 -urllib3==2.5.0 +urllib3==2.8.0 wheel==0.46.2 -pytest==8.3.3 +pytest==9.0.3 pytest-django==4.9.0 pytest-cov==5.0.0 diff --git a/subscriptions/admin.py b/subscriptions/admin.py index 7934a61..3f1194d 100644 --- a/subscriptions/admin.py +++ b/subscriptions/admin.py @@ -1,6 +1,6 @@ from django.contrib import admin -from .models import ServerSession, SubscriptionEntitlement, SubscriptionPlan, UsageLedgerEntry, WebhookEvent +from .models import PolarCreditAccount, ServerSession, SubscriptionEntitlement, SubscriptionPlan, UsageLedgerEntry, WebhookEvent @admin.register(SubscriptionPlan) @@ -64,3 +64,15 @@ class WebhookEventAdmin(admin.ModelAdmin): list_filter = ('provider', 'status', 'event_type') search_fields = ('event_id', 'event_type') readonly_fields = ('received_at', 'processed_at') + + +@admin.register(PolarCreditAccount) +class PolarCreditAccountAdmin(admin.ModelAdmin): + list_display = ('user', 'meter_id', 'observed_consumed_units', 'local_credits_seen', 'unreflected_credits') + readonly_fields = list_display + + def has_add_permission(self, request): + return False + + def has_delete_permission(self, request, obj=None): + return False diff --git a/subscriptions/api/views.py b/subscriptions/api/views.py index f14f006..8565cf0 100644 --- a/subscriptions/api/views.py +++ b/subscriptions/api/views.py @@ -12,6 +12,7 @@ process_orchestrator_event, request_stop_server_session, serialize_session, + _actor_for_user, ) @@ -123,7 +124,8 @@ def stop_session(request, session_id): return Response({'success': False, 'message': 'Server session does not exist.'}, status=404) try: - session = request_stop_server_session(session, reason=(request.data or {}).get('reason', 'stopped')) + actor = _actor_for_user(session.user, 'discord_user' if _is_service_request(request) else 'website_user') + session = request_stop_server_session(session, reason=(request.data or {}).get('reason', 'stopped'), actor=actor) except SubscriptionError as exc: return Response({'success': False, 'message': str(exc), 'code': exc.code}, status=exc.status_code) return Response({'success': True, 'session': serialize_session(session)}) diff --git a/subscriptions/credits.py b/subscriptions/credits.py new file mode 100644 index 0000000..eb7e66b --- /dev/null +++ b/subscriptions/credits.py @@ -0,0 +1,39 @@ +from decimal import Decimal, ROUND_FLOOR + +from django.conf import settings +from django.db import transaction +from django.db.models import F, Sum + +from discordoauth2.models import User +from . import polar_gateway +from .models import PolarCreditAccount, SubscriptionEntitlement, UsageLedgerEntry + + +def lock_user(user_id): + # A real write also serializes reservations on SQLite, where FOR UPDATE is + # ignored. Take this lock before reading entitlement, meter, or ledger state. + User.objects.filter(pk=user_id).update(last_login=F('last_login')) + + +@transaction.atomic +def spendable_polar_balance(user): + lock_user(user.pk) + meter = polar_gateway.get_customer_meter(user) + meter_id = str(getattr(settings, 'POLAR_CASUAL_SERVER_METER_ID', '')) + consumed = Decimal(str(meter.consumed_units)) + total = UsageLedgerEntry.objects.filter( + user=user, entitlement__provider=SubscriptionEntitlement.PROVIDER_POLAR, + metadata__polar_meter_id=meter_id, + ).aggregate(total=Sum('credits'))['total'] or 0 + account, created = PolarCreditAccount.objects.select_for_update().get_or_create( + user=user, meter_id=meter_id, + defaults={'observed_consumed_units': consumed}, + ) + new_usage = max(total - account.local_credits_seen, 0) + # A reset of consumed_units is not proof that pending usage was processed. + reflected = max(consumed - account.observed_consumed_units, Decimal(0)) if not created else Decimal(0) + account.unreflected_credits = max(account.unreflected_credits + new_usage - reflected, Decimal(0)) + account.local_credits_seen = total + account.observed_consumed_units = consumed + account.save() + return max(int((Decimal(str(meter.balance)) - account.unreflected_credits).to_integral_value(rounding=ROUND_FLOOR)), 0) diff --git a/subscriptions/management/commands/check_premium_launch.py b/subscriptions/management/commands/check_premium_launch.py new file mode 100644 index 0000000..15bf753 --- /dev/null +++ b/subscriptions/management/commands/check_premium_launch.py @@ -0,0 +1,30 @@ +from django.conf import settings +from django.core.management.base import BaseCommand, CommandError +from subscriptions.models import SubscriptionPlan, UsageLedgerEntry, WebhookEvent + + +class Command(BaseCommand): + help = 'Check premium launch configuration without printing secrets or contacting services.' + + def handle(self, *args, **options): + issues = [] + for name in ['API_KEY', 'POLAR_ACCESS_TOKEN', 'POLAR_WEBHOOK_SECRET', 'POLAR_CASUAL_SERVER_METER_ID', + 'POLAR_CUSTOMER_PORTAL_URL', 'ORCHESTRATOR_API_BASE_URL', 'ORCHESTRATOR_API_TOKEN']: + value = getattr(settings, name, '') + if not value or value in {name, 'replace-me', 'changeme'}: + issues.append(f'Configure {name}.') + for plan in SubscriptionPlan.objects.filter(is_active=True): + if not plan.polar_product_id: + issues.append(f'Map the {plan.tier} plan to its Polar product in Django admin.') + if not SubscriptionPlan.objects.filter(is_active=True).exists(): + issues.append('Configure at least one active subscription plan.') + failed_usage = UsageLedgerEntry.objects.filter(polar_status='failed').count() + failed_hooks = WebhookEvent.objects.filter(status='failed').count() + if failed_usage or failed_hooks: + issues.append(f'Resolve {failed_usage} failed usage entries and {failed_hooks} failed webhook events.') + self.stdout.write(f'Polar environment: {settings.POLAR_SERVER}') + for issue in issues: + self.stderr.write(issue) + if issues: + raise CommandError(f'{len(issues)} premium launch prerequisite(s) remain.') + self.stdout.write(self.style.SUCCESS('Local prerequisites pass. Complete the sandbox and Linux/Discord acceptance checklist.')) diff --git a/subscriptions/management/commands/replay_subscription_webhooks.py b/subscriptions/management/commands/replay_subscription_webhooks.py new file mode 100644 index 0000000..75b2a63 --- /dev/null +++ b/subscriptions/management/commands/replay_subscription_webhooks.py @@ -0,0 +1,23 @@ +from django.core.management.base import BaseCommand, CommandError +from subscriptions.models import WebhookEvent +from subscriptions.services import process_polar_webhook + + +class Command(BaseCommand): + help = 'Retry failed Polar subscription webhooks after correcting configuration.' + + def add_arguments(self, parser): + parser.add_argument('--limit', type=int, default=100) + + def handle(self, *args, **options): + failed = 0 + events = list(WebhookEvent.objects.filter(provider='polar', status='failed').order_by('received_at')[:options['limit']]) + for event in events: + try: + process_polar_webhook(event.payload, event.event_id) + except Exception: + failed += 1 + self.stderr.write(f'Webhook {event.event_id} still failed; inspect its admin record.') + self.stdout.write(f'Replayed {len(events) - failed} webhook(s); {failed} still failed.') + if failed: + raise CommandError('Some Polar webhooks still need attention.') diff --git a/subscriptions/migrations/0005_serversession_billing_period_end_and_more.py b/subscriptions/migrations/0005_serversession_billing_period_end_and_more.py new file mode 100644 index 0000000..832e46a --- /dev/null +++ b/subscriptions/migrations/0005_serversession_billing_period_end_and_more.py @@ -0,0 +1,45 @@ +# Generated by Django 5.2.18 on 2026-10-29 00:30 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('subscriptions', '0004_usage_polar_sync_state'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.AddField( + model_name='serversession', + name='billing_period_end', + field=models.DateTimeField(blank=True, null=True), + ), + migrations.AddField( + model_name='serversession', + name='billing_period_start', + field=models.DateTimeField(blank=True, null=True), + ), + migrations.AddField( + model_name='serversession', + name='last_event_at', + field=models.DateTimeField(blank=True, null=True), + ), + migrations.CreateModel( + name='PolarCreditAccount', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('meter_id', models.CharField(max_length=128)), + ('observed_consumed_units', models.DecimalField(decimal_places=6, default=0, max_digits=20)), + ('local_credits_seen', models.PositiveBigIntegerField(default=0)), + ('unreflected_credits', models.DecimalField(decimal_places=6, default=0, max_digits=20)), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + options={ + 'constraints': [models.UniqueConstraint(fields=('user', 'meter_id'), name='unique_user_polar_meter')], + }, + ), + ] diff --git a/subscriptions/migrations/0006_correct_host_benefits.py b/subscriptions/migrations/0006_correct_host_benefits.py new file mode 100644 index 0000000..e4d0245 --- /dev/null +++ b/subscriptions/migrations/0006_correct_host_benefits.py @@ -0,0 +1,13 @@ +from django.db import migrations + + +def correct_unimplemented_benefit(apps, schema_editor): + plans = apps.get_model('subscriptions', 'SubscriptionPlan') + plans.objects.filter( + tier='host', benefit_lines='Higher premium Discord role\nSaved casual presets / faster relaunch flow', + ).update(benefit_lines='Higher premium Discord role\nLaunch from Discord bot or website') + + +class Migration(migrations.Migration): + dependencies = [('subscriptions', '0005_serversession_billing_period_end_and_more')] + operations = [migrations.RunPython(correct_unimplemented_benefit, migrations.RunPython.noop)] diff --git a/subscriptions/models.py b/subscriptions/models.py index 8eb03ee..4a111d1 100644 --- a/subscriptions/models.py +++ b/subscriptions/models.py @@ -166,6 +166,9 @@ class ServerSession(models.Model): launch_context = models.JSONField(default=dict, blank=True) started_at = models.DateTimeField(default=timezone.now) last_heartbeat_at = models.DateTimeField(default=timezone.now) + last_event_at = models.DateTimeField(null=True, blank=True) + billing_period_start = models.DateTimeField(null=True, blank=True) + billing_period_end = models.DateTimeField(null=True, blank=True) stopped_at = models.DateTimeField(null=True, blank=True) stop_reason = models.CharField(max_length=80, blank=True) created_at = models.DateTimeField(auto_now_add=True) @@ -239,13 +242,29 @@ class Meta: ordering = ['-created_at'] indexes = [ models.Index(fields=['user', 'billing_period_start', 'billing_period_end']), - models.Index(fields=['polar_status']), + models.Index(fields=['polar_status'], name='subscriptio_polar_s_9533a3_idx'), ] def __str__(self): return f'{self.user} - {self.credits} credits - {self.reason}' +class PolarCreditAccount(models.Model): + """Reconcile our dedicated Polar meter with locally committed usage. + + Ingestion and meter aggregation are asynchronous. Keep usage reserved until + consumed_units advances, independently of credits granted in the portal. + """ + user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) + meter_id = models.CharField(max_length=128) + observed_consumed_units = models.DecimalField(max_digits=20, decimal_places=6, default=0) + local_credits_seen = models.PositiveBigIntegerField(default=0) + unreflected_credits = models.DecimalField(max_digits=20, decimal_places=6, default=0) + + class Meta: + constraints = [models.UniqueConstraint(fields=['user', 'meter_id'], name='unique_user_polar_meter')] + + class WebhookEvent(models.Model): STATUS_PROCESSED = 'processed' STATUS_SKIPPED = 'skipped' diff --git a/subscriptions/orchestrator.py b/subscriptions/orchestrator.py index aeaed72..90177a0 100644 --- a/subscriptions/orchestrator.py +++ b/subscriptions/orchestrator.py @@ -85,3 +85,11 @@ def stop_server(orchestrator_session_id, *, reason='website_stop', actor=None): if actor: payload['actor'] = actor return _request('POST', f'/v1/servers/{orchestrator_session_id}/stop', json=payload) + + +def get_server(session_id): + return _request('GET', f'/v1/servers/{session_id}') + + +def find_website_sessions(website_session_id): + return _request('GET', '/v1/servers', params={'websiteServerSessionId': str(website_session_id)}).get('sessions', []) diff --git a/subscriptions/polar_gateway.py b/subscriptions/polar_gateway.py index 9e7cdcd..7666705 100644 --- a/subscriptions/polar_gateway.py +++ b/subscriptions/polar_gateway.py @@ -59,7 +59,7 @@ def create_checkout_url(user, plan, customer_ip_address=''): return checkout_url -def get_customer_credit_balance(user): +def get_customer_meter(user): meter_id = getattr(settings, 'POLAR_CASUAL_SERVER_METER_ID', '') if not meter_id: raise PolarGatewayError('Polar casual server meter ID is not configured.') @@ -77,7 +77,11 @@ def get_customer_credit_balance(user): items = getattr(result, 'items', None) or [] if not items: raise PolarGatewayError('Polar credit balance is not available for this customer yet.') - return int(getattr(items[0], 'balance', 0)) + return items[0] + + +def get_customer_credit_balance(user): + return int(get_customer_meter(user).balance) def ingest_usage_event(session, credits, occurred_at): diff --git a/subscriptions/services.py b/subscriptions/services.py index 03e70c8..8acadf8 100644 --- a/subscriptions/services.py +++ b/subscriptions/services.py @@ -1,7 +1,7 @@ from datetime import timedelta, timezone as datetime_timezone from django.conf import settings -from django.db import transaction +from django.db import OperationalError, transaction from django.utils import timezone from django.utils.dateparse import parse_datetime @@ -15,6 +15,7 @@ ) from . import orchestrator from . import polar_gateway +from .credits import lock_user, spendable_polar_balance ACTIVE_STATUSES = { @@ -66,6 +67,7 @@ def get_active_entitlement(user): entitlements = SubscriptionEntitlement.objects.filter( user=user, status__in=ACTIVE_STATUSES, + current_period_start__lte=now, current_period_end__gt=now, ).select_related('plan').order_by( '-plan__entitlement_priority', @@ -100,7 +102,7 @@ def locally_reserved_credits(entitlement): def _polar_credit_balance(entitlement): try: - return polar_gateway.get_customer_credit_balance(entitlement.user) + return spendable_polar_balance(entitlement.user) except polar_gateway.PolarGatewayError as exc: raise SubscriptionError(str(exc), status_code=503, code='POLAR_CREDIT_BALANCE_UNAVAILABLE') from exc @@ -114,7 +116,7 @@ def available_credits(entitlement): def credit_summary(entitlement): if entitlement.provider == SubscriptionEntitlement.PROVIDER_POLAR: try: - remaining = max(polar_gateway.get_customer_credit_balance(entitlement.user), 0) + remaining = max(spendable_polar_balance(entitlement.user) - locally_reserved_credits(entitlement), 0) monthly = entitlement.monthly_credits return { 'monthly_credits': monthly, @@ -149,6 +151,7 @@ def credit_summary(entitlement): def entitlement_payload(user): entitlement = get_active_entitlement(user) + active_session = ServerSession.objects.filter(user=user, status__in=OPEN_SESSION_STATUSES).first() if entitlement is None: return { 'active': False, @@ -163,10 +166,9 @@ def entitlement_payload(user): 'credit_balance_source': None, 'credit_balance_available': False, 'max_session_minutes': 0, - 'active_session': None, + 'active_session': serialize_session(active_session), } - active_session = ServerSession.objects.filter(user=user, status__in=OPEN_SESSION_STATUSES).first() credits = credit_summary(entitlement) return { 'active': True, @@ -218,6 +220,7 @@ def serialize_session(session): @transaction.atomic def start_server_session(user, requested_minutes=None, game='', server_identifier='', launch_context=None, status=ServerSession.STATUS_RUNNING): + lock_user(user.pk) entitlement = get_active_entitlement(user) if entitlement is None: raise SubscriptionError('An active premium subscription is required to launch a casual server.') @@ -242,7 +245,12 @@ def start_server_session(user, requested_minutes=None, game='', server_identifie except (TypeError, ValueError) as exc: raise SubscriptionError('requested_minutes must be an integer.', status_code=400, code='INVALID_REQUESTED_MINUTES') from exc requested = max(requested, 5) - allocated = min(requested, entitlement.max_session_minutes, remaining) + # Do not carry an old billing-cycle reservation into the next cycle. Leave + # a minute for the launch request and the orchestrator's timeout polling. + period_minutes = int((entitlement.current_period_end - timezone.now()).total_seconds() // 60) - 1 + if period_minutes < 5: + raise SubscriptionError('This billing period ends shortly. Try again after renewal.') + allocated = min(requested, entitlement.max_session_minutes, remaining, period_minutes) session = ServerSession.objects.create( user=user, @@ -250,6 +258,8 @@ def start_server_session(user, requested_minutes=None, game='', server_identifie status=status, requested_minutes=requested, allocated_minutes=allocated, + billing_period_start=entitlement.current_period_start, + billing_period_end=entitlement.current_period_end, game=game or '', server_identifier=server_identifier or '', launch_context=launch_context or {}, @@ -307,22 +317,23 @@ def reserve_server_session(user, requested_minutes=None, game='', game_code='', status=ServerSession.STATUS_PENDING, ) session.game_code = str(game_code or '') - session.comment = (comment or '')[:120] + session.comment = (comment or f'Casual {game_code}')[:120] session.server_password = (password or '')[:80] session.save(update_fields=['game_code', 'comment', 'server_password', 'updated_at']) return session def launch_casual_server(user, requested_minutes=None, game='', game_code='', comment='', password='', launch_source='website', launch_context=None): - session = reserve_server_session( - user, - requested_minutes=requested_minutes, - game=game, - game_code=game_code, - comment=comment, - password=password, - launch_context=launch_context, - ) + try: + session = reserve_server_session( + user, requested_minutes=requested_minutes, game=game, game_code=game_code, + comment=comment, password=password, launch_context=launch_context, + ) + except OperationalError as exc: + if 'locked' not in str(exc).lower(): + raise + raise SubscriptionError('Another session update is in progress. Try again shortly.', + status_code=409, code='SESSION_UPDATE_IN_PROGRESS') from exc try: payload = orchestrator.launch_casual( @@ -331,15 +342,29 @@ def launch_casual_server(user, requested_minutes=None, game='', game_code='', co comment=session.comment, password=session.server_password, launch_source=launch_source, - actor=_actor_for_user(user, actor_type='discord_bot' if launch_source == 'discord_bot' else 'website_user'), + actor=_actor_for_user(user, actor_type='discord_user' if launch_source == 'discord_bot' else 'website_user'), ) except orchestrator.OrchestratorError as exc: - mark_server_session_failed(session, exc.code, str(exc)) + # A timeout/5xx may follow a successful spawn. Keep the reservation so + # reconciliation can recover by websiteServerSessionId. + if (exc.status_code and 400 <= exc.status_code < 500) or exc.code == 'ORCHESTRATOR_NOT_CONFIGURED': + mark_server_session_failed(session, exc.code, str(exc)) raise SubscriptionError(str(exc), status_code=503, code=exc.code) from exc - session = ServerSession.objects.get(pk=session.pk) + return _record_launch_response(session.pk, payload) + + +@transaction.atomic +def _record_launch_response(session_id, payload): + session = ServerSession.objects.select_for_update().get(pk=session_id) + if session.status in TERMINAL_SESSION_STATUSES: + return session + previous_state = session.orchestrator_state _apply_orchestrator_payload(session, payload) - session.status = ServerSession.STATUS_LAUNCHING + if session.last_event_at: + session.orchestrator_state = previous_state + if session.status == ServerSession.STATUS_PENDING: + session.status = ServerSession.STATUS_LAUNCHING session.save(update_fields=[ 'status', 'orchestrator_session_id', @@ -355,6 +380,7 @@ def launch_casual_server(user, requested_minutes=None, game='', game_code='', co @transaction.atomic def mark_server_session_failed(session, code='', message=''): + lock_user(session.user_id) session = ServerSession.objects.select_for_update().get(pk=session.pk) if session.status in TERMINAL_SESSION_STATUSES: return session @@ -377,6 +403,7 @@ def mark_server_session_failed(session, code='', message=''): @transaction.atomic def stop_server_session(session, reason='stopped'): + lock_user(session.user_id) session = ServerSession.objects.select_for_update().get(pk=session.pk) if session.status not in OPEN_SESSION_STATUSES: return session @@ -407,16 +434,15 @@ def _create_usage_entry(session, credits, metadata): defaults={ 'user': session.user, 'entitlement': session.entitlement, - 'billing_period_start': session.entitlement.current_period_start, - 'billing_period_end': session.entitlement.current_period_end, + 'billing_period_start': session.billing_period_start or session.entitlement.current_period_start, + 'billing_period_end': session.billing_period_end or session.entitlement.current_period_end, 'credits': credits, 'reason': UsageLedgerEntry.REASON_SERVER_SESSION, 'polar_status': UsageLedgerEntry.POLAR_STATUS_PENDING if polar_required else UsageLedgerEntry.POLAR_STATUS_NOT_REQUIRED, - 'metadata': metadata, + 'metadata': {**metadata, 'polar_meter_id': getattr(settings, 'POLAR_CASUAL_SERVER_METER_ID', '')}, }, ) - if created and polar_required: - sync_polar_usage_entry(usage) + # Persist first; the scheduled worker owns network ingestion. return usage @@ -489,85 +515,67 @@ def _record_orchestrator_usage(session, stopped_at, runtime_seconds=None): @transaction.atomic def process_orchestrator_event(session_id, payload): - event_id = payload.get('event_id') - event_type = payload.get('event') - if not event_id or not event_type: - raise SubscriptionError('Orchestrator events require event_id and event.') - + event_id, event_type = payload.get('event_id'), payload.get('event') + supported = {'launch_accepted', 'ready', 'heartbeat', 'stopping', 'stopped', 'exited', 'killed', 'launch_failed'} + if not event_id or event_type not in supported: + raise SubscriptionError('Orchestrator events require an event_id and a supported event.') + owner_id = ServerSession.objects.values_list('user_id', flat=True).get(pk=session_id) + lock_user(owner_id) + session = ServerSession.objects.select_for_update().get(pk=session_id) event, created = WebhookEvent.objects.get_or_create( - provider='orchestrator', - event_id=event_id, - defaults={ - 'event_type': event_type, - 'payload': payload, - 'status': WebhookEvent.STATUS_PROCESSED, - 'processed_at': timezone.now(), - }, + provider='orchestrator', event_id=event_id, + defaults={'event_type': event_type, 'payload': {**payload, 'website_session_id': str(session_id)}, 'processed_at': timezone.now()}, ) if not created: + if str(event.payload.get('website_session_id', session_id)) != str(session_id): + raise SubscriptionError('Event belongs to another session.') return event - - session = ServerSession.objects.select_for_update().get(pk=session_id) occurred_at = _parse_datetime(payload.get('occurred_at')) or timezone.now() - - if payload.get('orchestrator_session_id'): - session.orchestrator_session_id = str(payload['orchestrator_session_id']) - session.server_identifier = str(payload['orchestrator_session_id']) - if payload.get('orchestrator_state'): - session.orchestrator_state = str(payload['orchestrator_state']) - if payload.get('host'): - session.host = str(payload['host']) - if payload.get('port') is not None: - session.port = int(payload['port']) - if payload.get('password'): - session.server_password = str(payload['password']) - + terminal = event_type in {'stopped', 'exited', 'killed', 'launch_failed'} + if session.status in TERMINAL_SESSION_STATUSES or (not terminal and session.last_event_at and occurred_at < session.last_event_at): + event.status = WebhookEvent.STATUS_SKIPPED + event.save(update_fields=['status']) + return event + if session.status == ServerSession.STATUS_STOPPING and not terminal: + # A delayed heartbeat/ready can refresh liveness, never undo a stop. + if event_type == 'heartbeat': + session.last_heartbeat_at = max(session.last_heartbeat_at, occurred_at) + session.save(update_fields=['last_heartbeat_at']) + return event + for key, field in [('orchestrator_session_id', 'orchestrator_session_id'), ('orchestrator_state', 'orchestrator_state'), + ('host', 'host'), ('port', 'port'), ('password', 'server_password')]: + if payload.get(key) is not None: + setattr(session, field, payload[key]) + if session.orchestrator_session_id: + session.server_identifier = session.orchestrator_session_id if event_type == 'launch_accepted': - session.status = ServerSession.STATUS_LAUNCHING + if session.status == ServerSession.STATUS_PENDING: + session.status = ServerSession.STATUS_LAUNCHING elif event_type == 'ready': session.status = ServerSession.STATUS_RUNNING - session.ready_at = _parse_datetime(payload.get('ready_at')) or occurred_at - session.last_heartbeat_at = occurred_at + session.ready_at = session.ready_at or _parse_datetime(payload.get('ready_at')) or occurred_at + session.last_heartbeat_at = max(session.last_heartbeat_at, occurred_at) elif event_type == 'heartbeat': - if session.status not in TERMINAL_SESSION_STATUSES: + # Starting/stopping processes also send heartbeats; they are not ready. + if payload.get('orchestrator_state') == 'running' or session.ready_at: session.status = ServerSession.STATUS_RUNNING - session.last_heartbeat_at = occurred_at + session.ready_at = session.ready_at or _parse_datetime(payload.get('ready_at')) + session.last_heartbeat_at = max(session.last_heartbeat_at, occurred_at) elif event_type == 'stopping': - if session.status not in TERMINAL_SESSION_STATUSES: - session.status = ServerSession.STATUS_STOPPING - session.stop_reason = payload.get('reason') or 'stopping' - elif event_type in {'stopped', 'exited', 'killed'}: - stopped_at = _parse_datetime(payload.get('stopped_at')) or occurred_at - session.status = ServerSession.STATUS_KILLED if event_type == 'killed' else ServerSession.STATUS_STOPPED - session.stopped_at = stopped_at - session.orchestrator_stopped_at = stopped_at + session.status = ServerSession.STATUS_STOPPING + session.stop_reason = payload.get('reason') or 'stopping' + elif terminal: + session.status = ServerSession.STATUS_FAILED if event_type == 'launch_failed' else ( + ServerSession.STATUS_KILLED if event_type == 'killed' else ServerSession.STATUS_STOPPED) + session.stopped_at = _parse_datetime(payload.get('stopped_at')) or occurred_at + session.orchestrator_stopped_at = session.stopped_at session.stop_reason = payload.get('reason') or event_type - runtime_seconds = payload.get('runtime_seconds') - _record_orchestrator_usage(session, stopped_at, runtime_seconds=runtime_seconds) - elif event_type == 'launch_failed': - session.status = ServerSession.STATUS_FAILED - session.stopped_at = occurred_at - session.stop_reason = 'launch_failed' session.launch_error_code = payload.get('error_code') or '' session.launch_error_message = payload.get('error_message') or '' - - session.save(update_fields=[ - 'status', - 'orchestrator_session_id', - 'orchestrator_state', - 'host', - 'port', - 'server_password', - 'server_identifier', - 'ready_at', - 'last_heartbeat_at', - 'stopped_at', - 'orchestrator_stopped_at', - 'stop_reason', - 'launch_error_code', - 'launch_error_message', - 'updated_at', - ]) + session.last_event_at = max(session.last_event_at or occurred_at, occurred_at) + session.save() + if terminal and event_type != 'launch_failed': + _record_orchestrator_usage(session, session.stopped_at, runtime_seconds=payload.get('runtime_seconds')) return event @@ -575,29 +583,71 @@ def request_stop_server_session(session, reason='stopped', actor=None): session = ServerSession.objects.get(pk=session.pk) if session.status in TERMINAL_SESSION_STATUSES: return session - - if session.orchestrator_session_id: - try: - orchestrator.stop_server(session.orchestrator_session_id, reason=reason, actor=actor) - except orchestrator.OrchestratorError as exc: - raise SubscriptionError(str(exc), status_code=503, code=exc.code) from exc - session.status = ServerSession.STATUS_STOPPING - session.stop_reason = reason or 'stopping' - session.save(update_fields=['status', 'stop_reason', 'updated_at']) - return session - - return stop_server_session(session, reason=reason) + if not session.orchestrator_session_id: + raise SubscriptionError('The server launch is still being confirmed. Try again shortly.', status_code=409) + try: + orchestrator.stop_server(session.orchestrator_session_id, reason=reason, + actor=actor or _actor_for_user(session.user)) + except orchestrator.OrchestratorError as exc: + raise SubscriptionError(str(exc), status_code=503, code=exc.code) from exc + # A stop callback may have arrived while the HTTP request was in flight. + with transaction.atomic(): + session = ServerSession.objects.select_for_update().get(pk=session.pk) + if session.status not in TERMINAL_SESSION_STATUSES: + session.status = ServerSession.STATUS_STOPPING + session.stop_reason = reason or 'stopping' + session.save(update_fields=['status', 'stop_reason', 'updated_at']) + return session def expire_stale_sessions(max_heartbeat_age_minutes=10): cutoff = timezone.now() - timedelta(minutes=max_heartbeat_age_minutes) - stale_sessions = ServerSession.objects.filter( - status=ServerSession.STATUS_RUNNING, - last_heartbeat_at__lt=cutoff, - ) + stale_ids = list(ServerSession.objects.filter( + status__in=OPEN_SESSION_STATUSES, last_heartbeat_at__lt=cutoff, + ).values_list('pk', flat=True)) expired = [] - for session in stale_sessions: - expired.append(stop_server_session(session, reason=ServerSession.STATUS_EXPIRED)) + for session_id in stale_ids: + session = ServerSession.objects.get(pk=session_id) + try: + if session.orchestrator_session_id: + details = orchestrator.get_server(session.orchestrator_session_id) + else: + matches = orchestrator.find_website_sessions(session.id) + details = matches[0] if matches else None + except orchestrator.OrchestratorError as exc: + if exc.status_code != 404: + # Network outage is not evidence of process death. + continue + details = None + if details: + state = details.get('state') + stopped = state in {'stopped', 'exited', 'killed', 'failed'} + process_orchestrator_event(session.id, { + 'event_id': f'reconcile:{session.id}:{details.get("id")}:{details.get("stoppedAt") or timezone.now().isoformat()}', + 'event': ('launch_failed' if state == 'failed' else 'stopped') if stopped else 'heartbeat', + 'occurred_at': details.get('stoppedAt') or timezone.now().isoformat(), + 'ready_at': details.get('readyAt'), 'stopped_at': details.get('stoppedAt'), + 'runtime_seconds': details.get('runtimeSeconds'), + 'orchestrator_session_id': details.get('id'), 'orchestrator_state': state, + 'reason': 'reconciled', + }) + if not stopped: + continue + else: + # The service is reachable and the old session no longer exists: + # destructive restart. Charge only runtime last confirmed alive. + with transaction.atomic(): + lock_user(session.user_id) + session = ServerSession.objects.select_for_update().get(pk=session_id) + if not session.is_open or session.last_heartbeat_at >= cutoff: + continue + session.status = ServerSession.STATUS_EXPIRED + session.stopped_at = min(session.last_heartbeat_at, timezone.now()) + session.stop_reason = 'orchestrator_session_lost' + session.save() + if session.ready_at: + _record_orchestrator_usage(session, session.stopped_at) + expired.append(ServerSession.objects.get(pk=session_id)) return expired @@ -620,17 +670,18 @@ def process_polar_webhook(payload, event_id): 'payload': payload, }, ) - if not created: + if not created and event.status != WebhookEvent.STATUS_FAILED: return event try: if event_type.startswith('subscription.'): - _upsert_entitlement_from_polar_subscription(data, event_id) + _upsert_entitlement_from_polar_subscription(data, event_id, payload.get('timestamp')) event.status = WebhookEvent.STATUS_PROCESSED + event.error = '' else: event.status = WebhookEvent.STATUS_SKIPPED event.processed_at = timezone.now() - event.save(update_fields=['status', 'processed_at']) + event.save(update_fields=['status', 'error', 'processed_at']) except Exception as exc: event.status = WebhookEvent.STATUS_FAILED event.error = str(exc) @@ -647,7 +698,8 @@ def create_polar_checkout_url(user, plan, customer_ip_address=''): raise SubscriptionError(str(exc)) from exc -def _upsert_entitlement_from_polar_subscription(data, event_id): +@transaction.atomic +def _upsert_entitlement_from_polar_subscription(data, event_id, event_timestamp=None): user = _user_from_polar_data(data) plan = _plan_from_polar_data(data) if user is None or plan is None: @@ -657,6 +709,16 @@ def _upsert_entitlement_from_polar_subscription(data, event_id): if not subscription_id: raise SubscriptionError('Polar subscription webhook did not include a subscription ID.') + lock_user(user.pk) + incoming_at = _parse_dt(data.get('modified_at') or event_timestamp) + previous = SubscriptionEntitlement.objects.filter( + provider=SubscriptionEntitlement.PROVIDER_POLAR, provider_subscription_id=subscription_id, + ).first() + previous_at = _parse_dt((previous.metadata or {}).get('_event_at')) if previous else None + if previous_at and (incoming_at is None or incoming_at <= previous_at): + return + data = {**data, '_event_at': incoming_at.isoformat() if incoming_at else None} + period_start = _parse_dt( data.get('current_period_start') or data.get('started_at') @@ -668,7 +730,9 @@ def _upsert_entitlement_from_polar_subscription(data, event_id): or data.get('ended_at') ) or (period_start + timedelta(days=31)) raw_status = str(data.get('status') or '').lower() - status = POLAR_STATUS_MAP.get(raw_status, SubscriptionEntitlement.STATUS_ACTIVE) + status = POLAR_STATUS_MAP.get(raw_status, SubscriptionEntitlement.STATUS_INACTIVE) + if status == SubscriptionEntitlement.STATUS_CANCELED and data.get('cancel_at_period_end') and period_end > timezone.now(): + status = SubscriptionEntitlement.STATUS_ACTIVE customer = data.get('customer') or {} customer_id = str(data.get('customer_id') or customer.get('id') or '') @@ -708,13 +772,6 @@ def _user_from_polar_data(data): def _plan_from_polar_data(data): - metadata = data.get('metadata') or {} - tier = metadata.get('tier') - if tier: - plan = SubscriptionPlan.objects.filter(tier=tier).first() - if plan: - return plan - product_id = ( data.get('product_id') or (data.get('product') or {}).get('id') @@ -722,8 +779,10 @@ def _plan_from_polar_data(data): ) if product_id: plan = SubscriptionPlan.objects.filter(polar_product_id=product_id).first() - if plan: - return plan + return plan + tier = (data.get('metadata') or {}).get('tier') + if tier: + return SubscriptionPlan.objects.filter(tier=tier).first() return None diff --git a/subscriptions/test_launch_readiness.py b/subscriptions/test_launch_readiness.py new file mode 100644 index 0000000..348c49f --- /dev/null +++ b/subscriptions/test_launch_readiness.py @@ -0,0 +1,159 @@ +from datetime import timedelta +from types import SimpleNamespace +from unittest.mock import patch + +from django.core.management import call_command +from django.test import TestCase, override_settings +from django.utils import timezone + +from subscriptions.models import ServerSession, SubscriptionEntitlement, SubscriptionPlan, UsageLedgerEntry, WebhookEvent +from subscriptions.services import ( + SubscriptionError, _record_launch_response, available_credits, expire_stale_sessions, + launch_casual_server, process_orchestrator_event, process_polar_webhook, + request_stop_server_session, start_server_session, stop_server_session, +) +from subscriptions.tests import create_user, create_entitlement +from subscriptions.polar_gateway import PolarGatewayError +from subscriptions.orchestrator import OrchestratorError + + +@override_settings(POLAR_CASUAL_SERVER_METER_ID='dedicated-meter') +class LaunchReadinessTests(TestCase): + def setUp(self): + self.user = create_user() + self.entitlement = create_entitlement(self.user) + + def callback(self, session, event, **details): + return process_orchestrator_event(session.id, { + 'event_id': f'{event}-{WebhookEvent.objects.count()}', 'event': event, **details, + }) + + @patch('subscriptions.polar_gateway.get_customer_meter') + @patch('subscriptions.polar_gateway.ingest_usage_event') + def test_usage_hold_survives_failure_ingestion_delay_and_portal_credit_grants(self, ingest, meter): + self.entitlement.provider = SubscriptionEntitlement.PROVIDER_POLAR + self.entitlement.save() + meter.return_value = SimpleNamespace(balance=60, consumed_units=100) + session = start_server_session(self.user, 60) + session.started_at = timezone.now() - timedelta(minutes=60) + session.save() + stop_server_session(session) + ingest.assert_not_called() # Network work is outside lifecycle transactions. + self.assertEqual(available_credits(self.entitlement), 0) + ingest.side_effect = PolarGatewayError('offline') + call_command('sync_polar_usage') + self.assertEqual(available_credits(self.entitlement), 0) + ingest.side_effect = None + ingest.return_value = SimpleNamespace(inserted=1, duplicates=0) + call_command('sync_polar_usage') + self.assertEqual(available_credits(self.entitlement), 0) # accepted, not aggregated + meter.return_value = SimpleNamespace(balance=0, consumed_units=160) + self.assertEqual(available_credits(self.entitlement), 0) # reflected exactly once + meter.return_value = SimpleNamespace(balance=90, consumed_units=160) + self.assertEqual(available_credits(self.entitlement), 90) # portal grants remain authoritative + self.assertEqual(UsageLedgerEntry.objects.count(), 1) + + def test_terminal_state_survives_reordered_callbacks_and_launch_response(self): + session = start_server_session(self.user, 30) + self.callback(session, 'stopped', runtime_seconds=61) + self.callback(session, 'ready', occurred_at=(timezone.now() - timedelta(minutes=2)).isoformat()) + _record_launch_response(session.pk, {'id': 'orchestrator', 'state': 'starting'}) + session.refresh_from_db() + self.assertEqual(session.status, ServerSession.STATUS_STOPPED) + self.assertEqual(UsageLedgerEntry.objects.get().credits, 5) + + def test_heartbeat_does_not_reverse_stop_or_make_starting_ready(self): + session = start_server_session(self.user, 30, status=ServerSession.STATUS_LAUNCHING) + self.callback(session, 'heartbeat', orchestrator_state='starting') + session.refresh_from_db() + self.assertEqual(session.status, ServerSession.STATUS_LAUNCHING) + self.callback(session, 'stopping') + self.callback(session, 'heartbeat', orchestrator_state='running') + session.refresh_from_db() + self.assertEqual(session.status, ServerSession.STATUS_STOPPING) + + @patch('subscriptions.orchestrator.stop_server') + def test_stop_supplies_actor_and_does_not_overwrite_terminal_callback(self, stop): + session = start_server_session(self.user, 30) + session.orchestrator_session_id = 'orch' + session.save() + stop.side_effect = lambda *a, **kw: self.callback(session, 'stopped', runtime_seconds=61) + result = request_stop_server_session(session) + self.assertEqual(result.status, ServerSession.STATUS_STOPPED) + self.assertEqual(stop.call_args.kwargs['actor']['type'], 'website_user') + + @patch('subscriptions.orchestrator.launch_casual') + def test_discord_launch_actor_and_ready_race(self, launch): + def accepted(session, **kwargs): + self.callback(session, 'ready', orchestrator_session_id='orch') + return {'id': 'orch', 'state': 'starting', 'port': 11115} + launch.side_effect = accepted + session = launch_casual_server(self.user, 30, game_code='20', launch_source='discord_bot') + self.assertEqual(launch.call_args.kwargs['actor']['type'], 'discord_user') + self.assertEqual(session.status, ServerSession.STATUS_RUNNING) + + @patch('subscriptions.orchestrator.launch_casual', side_effect=OrchestratorError('timeout')) + def test_uncertain_launch_retains_reservation(self, launch): + with self.assertRaises(SubscriptionError): + launch_casual_server(self.user, 30, game_code='20') + self.assertEqual(ServerSession.objects.get().status, ServerSession.STATUS_PENDING) + + @patch('subscriptions.orchestrator.get_server') + def test_cleanup_reconciles_all_open_states_and_uses_last_confirmed_runtime(self, get): + get.side_effect = OrchestratorError('missing', status_code=404) + for status in ['pending', 'launching', 'running', 'stopping']: + session = start_server_session(self.user, 60, status=status) + session.orchestrator_session_id = 'old' + session.started_at = timezone.now() - timedelta(minutes=60) + session.ready_at = session.started_at if status in ['running', 'stopping'] else None + session.last_heartbeat_at = session.started_at + timedelta(minutes=6) + session.save() + self.assertEqual(len(expire_stale_sessions()), 1) + session.refresh_from_db() + self.assertEqual(session.status, 'expired') + if session.ready_at: + self.assertEqual(session.usage_entry.credits, 10) + else: + self.assertFalse(UsageLedgerEntry.objects.filter(server_session=session).exists()) + + @patch('subscriptions.orchestrator.get_server', side_effect=OrchestratorError('offline')) + def test_cleanup_does_not_infer_process_death_from_network_failure(self, get): + session = start_server_session(self.user, 30) + session.orchestrator_session_id = 'orch' + session.last_heartbeat_at = timezone.now() - timedelta(days=1) + session.save() + self.assertEqual(expire_stale_sessions(), []) + self.assertFalse(UsageLedgerEntry.objects.exists()) + + def test_webhook_failure_is_retryable_and_old_updates_do_not_regress_entitlement(self): + now = timezone.now() + payload = {'type': 'subscription.updated', 'timestamp': now.isoformat(), 'data': { + 'id': 'sub-test', 'status': 'active', 'customer': {'external_id': str(self.user.pk)}, + 'product_id': 'unmapped', + }} + with self.assertRaises(SubscriptionError): + process_polar_webhook(payload, 'polar-retry') + plan = SubscriptionPlan.objects.get(tier='supporter') + plan.polar_product_id = 'unmapped' + plan.save() + self.assertEqual(process_polar_webhook(payload, 'polar-retry').status, 'processed') + older = {**payload, 'timestamp': (now - timedelta(days=1)).isoformat(), + 'data': {**payload['data'], 'status': 'revoked'}} + process_polar_webhook(older, 'old-event') + entitlement = SubscriptionEntitlement.objects.get(provider_subscription_id='sub-test') + self.assertEqual(entitlement.status, 'active') + + def test_usage_keeps_reserved_billing_period_after_entitlement_renews(self): + session = start_server_session(self.user, 30) + period = session.billing_period_start + self.entitlement.current_period_start += timedelta(days=30) + self.entitlement.current_period_end += timedelta(days=30) + self.entitlement.save() + stop_server_session(session) + self.assertEqual(session.usage_entry.billing_period_start, period) + + def test_session_reservation_cannot_cross_billing_cycle(self): + self.entitlement.current_period_end = timezone.now() + timedelta(minutes=12) + self.entitlement.save() + session = start_server_session(self.user, 120) + self.assertLessEqual(session.allocated_minutes, 11) diff --git a/subscriptions/test_orchestrator_integration.py b/subscriptions/test_orchestrator_integration.py new file mode 100644 index 0000000..9f5d7cc --- /dev/null +++ b/subscriptions/test_orchestrator_integration.py @@ -0,0 +1,89 @@ +"""Run with ORCHESTRATOR_REPO=/path/to/built/orchestrator; no external services.""" +import json +import os +from pathlib import Path +import select +import subprocess +import tempfile +import time +from unittest import skipUnless + +from django.test import LiveServerTestCase, override_settings +from rest_framework.test import APIClient + +from subscriptions.models import ServerSession, SubscriptionPlan, UsageLedgerEntry +from subscriptions.tests import create_user, create_entitlement + + +@skipUnless(os.environ.get('ORCHESTRATOR_REPO'), 'Set ORCHESTRATOR_REPO for the cross-service test.') +class OrchestratorIntegrationTests(LiveServerTestCase): + def test_discord_and_browser_launch_callbacks_and_stop_use_real_contracts(self): + repo = Path(os.environ['ORCHESTRATOR_REPO']).resolve() + with tempfile.TemporaryDirectory(prefix='second-contract-') as runtime: + script = """ +import Fastify from 'fastify'; +import app from './dist/app.js'; +const server = Fastify(); +await server.register(app); +await server.listen({ host: '127.0.0.1', port: 0 }); +console.log(JSON.stringify({ url: server.listeningOrigin })); +process.on('SIGTERM', async () => { await server.close(); process.exit(0); }); +""" + env = {key: value for key, value in os.environ.items() if key in ['PATH', 'HOME', 'TMPDIR']} + env.update({ + 'XRC_PROCESS_MODE': 'fake', 'NODE_ENV': 'test', 'API_KEY': 'integration-key', + 'SERVICE_TOKENS': 'integration-token:servers:casual:create,servers:read,servers:stop', + 'SECONDWEBSITE_BASE_URL': self.live_server_url, + 'SECONDWEBSITE_CALLBACK_RETRY_BASE_MS': '10', + 'SERVER_LOG_DIR': runtime + '/logs', 'CHAT_LOG_DIR': runtime + '/chat', + 'SCORE_DATA_DIR': runtime + '/scores', 'EVENT_LOG_DIR': runtime + '/events', + }) + child = subprocess.Popen(['node', '--input-type=module', '-e', script], cwd=repo, + env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + try: + self.assertTrue(select.select([child.stdout], [], [], 15)[0], 'Orchestrator startup timed out') + line = child.stdout.readline() + self.assertTrue(line, 'Orchestrator did not start') + url = json.loads(line)['url'] + with override_settings(ORCHESTRATOR_API_BASE_URL=url, ORCHESTRATOR_API_TOKEN='integration-token', + API_KEY='integration-key', ALLOWED_HOSTS=['localhost', '127.0.0.1', 'testserver']): + # TestCase data migrations are flushed by LiveServerTestCase. + SubscriptionPlan.objects.get_or_create(tier='supporter', defaults={ + 'name': 'Supporter', 'monthly_price_usd': 5, 'monthly_credits': 360, + 'max_session_minutes': 120, + }) + user = create_user() + create_entitlement(user) + client = APIClient() + for source, game in [('discord_bot', '23'), ('website', '24')]: + if source == 'website': + client.force_authenticate(user) + response = client.post('/api/subscriptions/server-sessions/start/', { + 'owner_discord_id': str(user.pk), 'game_code': game, + 'requested_minutes': 5, 'launch_source': source, + }, format='json', HTTP_X_API_KEY='integration-key' if source == 'discord_bot' else '') + self.assertEqual(response.status_code, 201, response.data) + session_id = response.data['session']['id'] + self.wait_for_status(session_id, 'running') + time.sleep(1.1) # One runtime second rounds to five credits. + response = client.post(f'/api/subscriptions/server-sessions/{session_id}/stop/', {}, + format='json', HTTP_X_API_KEY='integration-key' if source == 'discord_bot' else '') + self.assertEqual(response.status_code, 200, response.data) + self.wait_for_status(session_id, 'stopped') + usage = UsageLedgerEntry.objects.get(server_session_id=session_id) + self.assertEqual(usage.credits, 5) + self.assertEqual(UsageLedgerEntry.objects.count(), 2) + finally: + child.terminate() + try: + child.communicate(timeout=10) + except subprocess.TimeoutExpired: + child.kill() + child.communicate() + + def wait_for_status(self, session_id, status): + for _ in range(100): + if ServerSession.objects.get(pk=session_id).status == status: + return + time.sleep(0.05) + self.fail(f'Session did not become {status}') diff --git a/subscriptions/tests.py b/subscriptions/tests.py index fdfdb65..4e68bc1 100644 --- a/subscriptions/tests.py +++ b/subscriptions/tests.py @@ -1,5 +1,6 @@ import json from io import StringIO +from types import SimpleNamespace from datetime import timedelta from unittest.mock import patch @@ -149,9 +150,9 @@ def test_exhausted_entitlement_rejects_start(self): with self.assertRaises(SubscriptionError): start_server_session(user) - @patch('subscriptions.polar_gateway.get_customer_credit_balance') + @patch('subscriptions.polar_gateway.get_customer_meter') def test_polar_entitlement_uses_meter_balance_for_launch(self, mock_balance): - mock_balance.return_value = 25 + mock_balance.return_value = SimpleNamespace(balance=25, consumed_units=0) user = create_user() create_entitlement(user, provider=SubscriptionEntitlement.PROVIDER_POLAR, credits=360) @@ -160,9 +161,9 @@ def test_polar_entitlement_uses_meter_balance_for_launch(self, mock_balance): self.assertEqual(session.allocated_minutes, 25) mock_balance.assert_called_once_with(user) - @patch('subscriptions.polar_gateway.get_customer_credit_balance') + @patch('subscriptions.polar_gateway.get_customer_meter') def test_polar_entitlement_rejects_exhausted_meter_balance(self, mock_balance): - mock_balance.return_value = 0 + mock_balance.return_value = SimpleNamespace(balance=0, consumed_units=0) user = create_user() create_entitlement(user, provider=SubscriptionEntitlement.PROVIDER_POLAR, credits=360) @@ -170,9 +171,9 @@ def test_polar_entitlement_rejects_exhausted_meter_balance(self, mock_balance): start_server_session(user, requested_minutes=30) @patch('subscriptions.polar_gateway.ingest_usage_event') - @patch('subscriptions.polar_gateway.get_customer_credit_balance') + @patch('subscriptions.polar_gateway.get_customer_meter') def test_polar_usage_is_ingested_when_session_stops(self, mock_balance, mock_ingest): - mock_balance.return_value = 60 + mock_balance.return_value = SimpleNamespace(balance=60, consumed_units=0) mock_ingest.return_value = type('PolarIngestResponse', (), {'inserted': 1, 'duplicates': 0})() user = create_user() create_entitlement(user, provider=SubscriptionEntitlement.PROVIDER_POLAR, credits=360) @@ -182,6 +183,8 @@ def test_polar_usage_is_ingested_when_session_stops(self, mock_balance, mock_ing stop_server_session(session) stop_server_session(session) + mock_ingest.assert_not_called() + call_command('sync_polar_usage') usage = UsageLedgerEntry.objects.get(server_session=session) self.assertEqual(usage.credits, 10) @@ -191,9 +194,9 @@ def test_polar_usage_is_ingested_when_session_stops(self, mock_balance, mock_ing mock_ingest.assert_called_once() @patch('subscriptions.polar_gateway.ingest_usage_event') - @patch('subscriptions.polar_gateway.get_customer_credit_balance') + @patch('subscriptions.polar_gateway.get_customer_meter') def test_polar_usage_failure_keeps_local_stop_finalized_for_retry(self, mock_balance, mock_ingest): - mock_balance.return_value = 60 + mock_balance.return_value = SimpleNamespace(balance=60, consumed_units=0) mock_ingest.side_effect = polar_gateway.PolarGatewayError('temporary polar outage') user = create_user() create_entitlement(user, provider=SubscriptionEntitlement.PROVIDER_POLAR, credits=360) @@ -202,6 +205,7 @@ def test_polar_usage_failure_keeps_local_stop_finalized_for_retry(self, mock_bal session.save(update_fields=['started_at']) stop_server_session(session) + call_command('sync_polar_usage') session.refresh_from_db() usage = UsageLedgerEntry.objects.get(server_session=session) @@ -211,9 +215,9 @@ def test_polar_usage_failure_keeps_local_stop_finalized_for_retry(self, mock_bal self.assertIn('temporary polar outage', usage.polar_error) @patch('subscriptions.polar_gateway.ingest_usage_event') - @patch('subscriptions.polar_gateway.get_customer_credit_balance') + @patch('subscriptions.polar_gateway.get_customer_meter') def test_sync_polar_usage_retries_failed_entries(self, mock_balance, mock_ingest): - mock_balance.return_value = 60 + mock_balance.return_value = SimpleNamespace(balance=60, consumed_units=0) mock_ingest.side_effect = [ polar_gateway.PolarGatewayError('temporary polar outage'), type('PolarIngestResponse', (), {'inserted': 1, 'duplicates': 0})(), @@ -225,6 +229,7 @@ def test_sync_polar_usage_retries_failed_entries(self, mock_balance, mock_ingest session.save(update_fields=['started_at']) stop_server_session(session) + call_command('sync_polar_usage') output = StringIO() call_command('sync_polar_usage', stdout=output) @@ -234,20 +239,22 @@ def test_sync_polar_usage_retries_failed_entries(self, mock_balance, mock_ingest self.assertIn('Synced 1 Polar usage event', output.getvalue()) self.assertEqual(mock_ingest.call_count, 2) - def test_expire_stale_sessions_marks_expired_and_records_usage(self): + @patch('subscriptions.orchestrator.find_website_sessions', return_value=[]) + def test_expire_stale_sessions_marks_expired_and_records_usage(self, mock_sessions): user = create_user() create_entitlement(user) session = start_server_session(user) session.started_at = timezone.now() - timedelta(minutes=16) + session.ready_at = session.started_at session.last_heartbeat_at = timezone.now() - timedelta(minutes=12) - session.save(update_fields=['started_at', 'last_heartbeat_at']) + session.save(update_fields=['started_at', 'ready_at', 'last_heartbeat_at']) expired = expire_stale_sessions(max_heartbeat_age_minutes=10) self.assertEqual(len(expired), 1) session.refresh_from_db() self.assertEqual(session.status, ServerSession.STATUS_EXPIRED) - self.assertEqual(UsageLedgerEntry.objects.get(server_session=session).credits, 20) + self.assertEqual(UsageLedgerEntry.objects.get(server_session=session).credits, 5) def test_orchestrator_ready_and_stopped_events_record_usage_once(self): user = create_user() diff --git a/tools/rehearse_premium_migrations.py b/tools/rehearse_premium_migrations.py new file mode 100644 index 0000000..7fd21e5 --- /dev/null +++ b/tools/rehearse_premium_migrations.py @@ -0,0 +1,45 @@ +"""Migrate a disposable SQLite backup; never mutate the source database.""" +import argparse +import os +from pathlib import Path +import sqlite3 +import sys +import tempfile + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--source', required=True, type=Path) + args = parser.parse_args() + source = args.source.resolve(strict=True) + target = Path(tempfile.mkdtemp(prefix='premium-migration-rehearsal-')) / 'db.sqlite3' + with sqlite3.connect(source.as_uri() + '?mode=ro', uri=True) as original: + with sqlite3.connect(target) as backup: + original.backup(backup) + sys.path.insert(0, str(Path(__file__).resolve().parents[1])) + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'SRCweb.settings') + from django.conf import settings + settings.DATABASES['default']['NAME'] = str(target) + import django + django.setup() + from django.apps import apps + from django.core.management import call_command + from django.db import connection + call_command('migrate', fake_initial=True, interactive=False) + missing = [] + with connection.cursor() as cursor: + for model in apps.get_models(): + if model._meta.app_label not in ['discordoauth2', 'events', 'highscores', 'home', 'ranked', 'subscriptions']: + continue + columns = {column.name for column in connection.introspection.get_table_description(cursor, model._meta.db_table)} + missing.extend(f'{model._meta.db_table}.{field.column}' for field in model._meta.local_fields if field.column not in columns) + cursor.execute('PRAGMA foreign_key_check') + violations = len(cursor.fetchall()) + print(f'Rehearsal copy: {target}') + print(f'Missing model columns: {missing}; foreign-key violations: {violations}') + if missing or violations: + raise SystemExit(1) + + +if __name__ == '__main__': + main() From 76eeb24362ddfd2992dd84a41fd88f911ab8f54b Mon Sep 17 00:00:00 2001 From: Nick Bottone Date: Wed, 28 Oct 2026 20:57:11 -0400 Subject: [PATCH 10/28] docs: clarify coordinated release deployment trigger --- docs/premium-release-checklist.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/premium-release-checklist.md b/docs/premium-release-checklist.md index df24a5f..1faabf6 100644 --- a/docs/premium-release-checklist.md +++ b/docs/premium-release-checklist.md @@ -74,6 +74,8 @@ The utility opens the source read-only, makes a disposable backup, runs `migrate After green CI, sandbox/real-game acceptance, and the current-backup rehearsal, review the three PRs together. Explicit approval to merge/deploy remains a separate step. At cutover: pause new launches, drain legacy servers, back up/migrate the website, start the orchestrator, then switch the bot to the matching release. Keep public discovery disabled during the initial beta. Record all deployed SHAs. +**The existing website workflow deploys automatically after a push to `main`. Merging its PR is therefore a deployment trigger. Keep it unmerged until the coordinated cutover is authorized, or explicitly pause that workflow first.** + Install and verify these recurring tasks on the actual host at cutover, using its Python environment and environment variables: ```cron From 9d82dba1ec2873812232434f9b50a5b458acb9da Mon Sep 17 00:00:00 2001 From: Nick Bottone Date: Wed, 28 Oct 2026 21:00:01 -0400 Subject: [PATCH 11/28] fix: preserve customized subscription tiers during benefit cleanup --- docs/premium-release-checklist.md | 2 +- .../migrations/0006_correct_host_benefits.py | 12 +++++++++--- subscriptions/test_launch_readiness.py | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/docs/premium-release-checklist.md b/docs/premium-release-checklist.md index 1faabf6..f044d1f 100644 --- a/docs/premium-release-checklist.md +++ b/docs/premium-release-checklist.md @@ -21,7 +21,7 @@ Local configuration already specifies `POLAR_SERVER=sandbox`, an access token, a 1. Configure the sandbox webhook endpoint `/subscriptions/polar/webhook/` on a reachable **test** website URL and save its signing secret as `POLAR_WEBHOOK_SECRET`. A local test receiver needs a tunnel or test host; do not point sandbox events at the live production database. 2. Ensure the token can create checkouts, read customer meters, and ingest usage events. Keep all products, meter IDs, and credentials in the same sandbox organization. -3. Map each active Django `SubscriptionPlan.polar_product_id` to its sandbox product in Django admin. Configure recurring credits to match the advertised plan: Supporter 360 and Host 1,500; one credit is one runtime minute. Configure billing-cycle expiry/no rollover and no paid overage. Verify these behaviors in sandbox rather than assuming defaults. +3. Verify each active Django `SubscriptionPlan.polar_product_id` against its sandbox product in Django admin. The local database already has mapped **Supporter** (`supporter`, 360 credits, 120-minute max) and **VIP Supporter** (`vip-supporter`, 1,500 credits, 240-minute max) plans. Preserve those customized tiers; do not replace them with the fresh-database Host seed. Configure recurring credits to match the actual advertised plan; one credit is one runtime minute. Configure billing-cycle expiry/no rollover and no paid overage. Verify these behaviors in sandbox rather than assuming defaults. 4. Use a **dedicated meter** for this application's `casual_server_credits` events with a sum of `metadata.credits`. Do not publish unrelated usage to it or manually inject consumption into it. The reconciliation hold uses this dedicated meter's `consumed_units`; it allows credit grants in the portal and does not release holds merely because ingestion was accepted. [Polar customer meter fields](https://polar.sh/docs/api-reference/customer-meters/get). 5. Configure and test Polar's Discord role benefit for each product, or remove the corresponding role promise from the editable plan copy. The website's role ID field does not itself grant Discord roles. Saved presets are deferred and no longer advertised by the default Host plan. 6. Configure `POLAR_CUSTOMER_PORTAL_URL` and test the manage-billing flow. diff --git a/subscriptions/migrations/0006_correct_host_benefits.py b/subscriptions/migrations/0006_correct_host_benefits.py index e4d0245..8906971 100644 --- a/subscriptions/migrations/0006_correct_host_benefits.py +++ b/subscriptions/migrations/0006_correct_host_benefits.py @@ -3,9 +3,15 @@ def correct_unimplemented_benefit(apps, schema_editor): plans = apps.get_model('subscriptions', 'SubscriptionPlan') - plans.objects.filter( - tier='host', benefit_lines='Higher premium Discord role\nSaved casual presets / faster relaunch flow', - ).update(benefit_lines='Higher premium Discord role\nLaunch from Discord bot or website') + for plan in plans.objects.all(): + lines = plan.benefit_lines.splitlines() + if 'Saved casual presets / faster relaunch flow' not in lines: + continue + lines.remove('Saved casual presets / faster relaunch flow') + if 'Launch from Discord bot or website' not in lines: + lines.append('Launch from Discord bot or website') + plan.benefit_lines = '\n'.join(lines) + plan.save(update_fields=['benefit_lines']) class Migration(migrations.Migration): diff --git a/subscriptions/test_launch_readiness.py b/subscriptions/test_launch_readiness.py index 348c49f..18206d2 100644 --- a/subscriptions/test_launch_readiness.py +++ b/subscriptions/test_launch_readiness.py @@ -157,3 +157,18 @@ def test_session_reservation_cannot_cross_billing_cycle(self): self.entitlement.save() session = start_server_session(self.user, 120) self.assertLessEqual(session.allocated_minutes, 11) + + def test_benefit_cleanup_preserves_customized_tier_and_copy(self): + from importlib import import_module + from django.apps import apps + plan = SubscriptionPlan.objects.create( + tier='vip-supporter', name='VIP Supporter', monthly_price_usd=12, + monthly_credits=1500, max_session_minutes=240, + benefit_lines='Custom benefit\nSaved casual presets / faster relaunch flow', + ) + migration = import_module('subscriptions.migrations.0006_correct_host_benefits') + migration.correct_unimplemented_benefit(apps, None) + plan.refresh_from_db() + self.assertEqual(plan.tier, 'vip-supporter') + self.assertIn('Custom benefit', plan.benefit_lines) + self.assertNotIn('presets', plan.benefit_lines) From 7b46bfc37bcbe04e8ff0c808a9b9dd80b6f35722 Mon Sep 17 00:00:00 2001 From: Nick Bottone Date: Wed, 28 Oct 2026 23:27:33 -0400 Subject: [PATCH 12/28] fix: allow isolated Discord OAuth application for staging --- .env.example | 5 ++++- SRCweb/settings.py | 23 ++++++++++++++++----- subscriptions/test_staging_configuration.py | 21 +++++++++++++++++++ 3 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 subscriptions/test_staging_configuration.py diff --git a/.env.example b/.env.example index 13667ab..0cb560b 100644 --- a/.env.example +++ b/.env.example @@ -9,7 +9,10 @@ API_KEY=replace-me # Used in highscores clean-code decoding NEW_AES_KEY=0123456789abcdef -# Discord OAuth +# Discord OAuth (set a dedicated application ID for isolated testing) +DISCORD_CLIENT_ID=825618483957071873 +DISCORD_REDIRECT_URI= +DISCORD_API_REDIRECT_URI= DISCORD_CLIENT_SECRET=replace-me # Email diff --git a/SRCweb/settings.py b/SRCweb/settings.py index 912c776..e9f25cb 100644 --- a/SRCweb/settings.py +++ b/SRCweb/settings.py @@ -14,6 +14,7 @@ from django.core.exceptions import ImproperlyConfigured from pathlib import Path import os +from urllib.parse import urlencode load_dotenv() @@ -29,7 +30,7 @@ NEW_AES_KEY = os.getenv("NEW_AES_KEY") or "NEW_AES_KEY" # Discord Client ID (client safe) -DISCORD_CLIENT_ID = "825618483957071873" +DISCORD_CLIENT_ID = os.getenv("DISCORD_CLIENT_ID") or "825618483957071873" # SECURITY WARNING: keep the Discord Client Secret used in production secret! DISCORD_CLIENT_SECRET = os.getenv( @@ -271,19 +272,31 @@ def required_non_placeholder_env(name, *, development_default=None): STATICFILES_DIRS = [BASE_DIR / "static"] DISCORD_REDIRECT_URI = "http://localhost:8000/oauth2/login/redirect" DISCORD_API_REDIRECT_URI = "http://localhost:8000/oauth2/loginapi/redirect" - DISCORD_AUTH_URL = "https://discord.com/api/oauth2/authorize?client_id=825618483957071873&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2Foauth2%2Flogin%2Fredirect&response_type=code&scope=identify%20email" - DISCORD_AUTH_API_URL = "https://discord.com/api/oauth2/authorize?client_id=825618483957071873&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2Foauth2%2Floginapi%2Fredirect&response_type=code&scope=identify%20email" else: # production DEBUG = False ALLOWED_HOSTS = ["secondrobotics.org", "www.secondrobotics.org", "mail.secondrobotics.org"] DISCORD_REDIRECT_URI = "https://secondrobotics.org/oauth2/login/redirect" DISCORD_API_REDIRECT_URI = "https://secondrobotics.org/oauth2/loginapi/redirect" - DISCORD_AUTH_URL = "https://discord.com/api/oauth2/authorize?client_id=825618483957071873&redirect_uri=https%3A%2F%2Fsecondrobotics.org%2Foauth2%2Flogin%2Fredirect&response_type=code&scope=identify%20email" - DISCORD_AUTH_API_URL = "https://discord.com/api/oauth2/authorize?client_id=825618483957071873&redirect_uri=https%3A%2F%2Fsecondrobotics.org%2Foauth2%2Floginapi%2Fredirect&response_type=code&scope=identify%20email" CSRF_COOKIE_SECURE = True SESSION_COOKIE_SECURE = True +# A dedicated test application can share its ID with the test bot. Preserve +# production defaults while allowing isolated OAuth login and redirect URLs. +DISCORD_REDIRECT_URI = os.getenv("DISCORD_REDIRECT_URI") or DISCORD_REDIRECT_URI +DISCORD_API_REDIRECT_URI = os.getenv("DISCORD_API_REDIRECT_URI") or DISCORD_API_REDIRECT_URI + + +def discord_authorization_url(redirect_uri): + return "https://discord.com/api/oauth2/authorize?" + urlencode({ + "client_id": DISCORD_CLIENT_ID, "redirect_uri": redirect_uri, + "response_type": "code", "scope": "identify email", + }) + + +DISCORD_AUTH_URL = discord_authorization_url(DISCORD_REDIRECT_URI) +DISCORD_AUTH_API_URL = discord_authorization_url(DISCORD_API_REDIRECT_URI) + SESSION_COOKIE_AGE = 60 * 60 * 24 * 30 # 30 days MAX_UPLOAD_SIZE = "5242880" diff --git a/subscriptions/test_staging_configuration.py b/subscriptions/test_staging_configuration.py new file mode 100644 index 0000000..4d98942 --- /dev/null +++ b/subscriptions/test_staging_configuration.py @@ -0,0 +1,21 @@ +import json +import os +from pathlib import Path +import subprocess +import sys +from urllib.parse import parse_qs, urlparse + + +def test_oauth_uses_the_test_application_and_registered_redirects(): + env = {**os.environ, 'DISCORD_CLIENT_ID': '123456789', + 'DISCORD_REDIRECT_URI': 'http://localhost:8000/oauth2/login/redirect', + 'DISCORD_API_REDIRECT_URI': 'http://localhost:8000/oauth2/loginapi/redirect'} + script = ('import json; from SRCweb import settings as s; ' + 'print(json.dumps([s.DISCORD_AUTH_URL, s.DISCORD_AUTH_API_URL]))') + output = subprocess.check_output([sys.executable, '-c', script], env=env, + cwd=Path(__file__).resolve().parents[1], text=True) + for url, target in zip(json.loads(output), [env['DISCORD_REDIRECT_URI'], env['DISCORD_API_REDIRECT_URI']]): + query = parse_qs(urlparse(url).query) + assert query['client_id'] == ['123456789'] + assert query['redirect_uri'] == [target] + assert query['scope'] == ['identify email'] From 9ddf6e81daf380d98918a46d1c92360e447113ba Mon Sep 17 00:00:00 2001 From: Nick Bottone Date: Thu, 29 Oct 2026 01:59:21 -0400 Subject: [PATCH 13/28] docs: record live premium acceptance progress --- docs/premium-acceptance-2026-10-29.md | 68 +++++++++++++++++++++++++++ docs/premium-release-checklist.md | 4 ++ 2 files changed, 72 insertions(+) create mode 100644 docs/premium-acceptance-2026-10-29.md diff --git a/docs/premium-acceptance-2026-10-29.md b/docs/premium-acceptance-2026-10-29.md new file mode 100644 index 0000000..b8c7b1a --- /dev/null +++ b/docs/premium-acceptance-2026-10-29.md @@ -0,0 +1,68 @@ +# Premium acceptance evidence — 2026-10-29 + +Status: **in progress; not cleared for merge or launch**. No production deployment or merge has been performed. + +## Tested candidates and environment + +- Website: `7b46bfc37bcbe04e8ff0c808a9b9dd80b6f35722`. +- Bot: `9927f3e` (acceptance found and fixed idle event polling timeouts). +- Orchestrator: `971a3b1` (adds a host control for private discovery). +- Isolated application files: `ssh digitalocean`, `/home/nick/.local/share/second-robotics-test/releases/20261029/`. +- Test website: loopback port 8000, available locally through the SSH tunnel at `http://localhost:8000/`. +- Test orchestrator: loopback port 3300. Port 3000 belongs to an existing unrelated service and was left intact. +- Discord: guild `663201408581107712`, application `724063847958184046`. +- Fresh SQLite database; only public game-mode and plan configuration copied. Production/customer databases were not copied. +- Python 3.12.16 (website), Python 3.10.22 (bot), Node 22.23.4 (orchestrator). + +## Verified + +- Fresh migrations and Django system checks pass. +- Discord OAuth redirects persist; real browser login returned successfully and created an authenticated test session. +- Test bot connects and completes startup in the selected guild. +- Linux automated verification: website **39 tests passed**, including the real HTTP contract test; bot **31 tests plus 2 log-parser tests passed**; orchestrator **27 tests passed** before the discovery addition. The updated orchestrator suite **28 tests passed locally**, and its build/test and website-contract CI checks passed. +- The original low-memory cross-service timeout and later OOM were resolved by cleaning up the orphaned test child and adding temporary swap; the complete website rerun passed. +- Real official xRC server v21.2b reached `running` with readiness timestamps for Override (23) and BioBuzz (24). Both then reached `stopped`; their OS processes were verified absent. +- Official Polar CLI authenticated against the **sandbox** SecondRobotics organization (not the separate SecondRoboticsSandbox organization). +- Configured meter: `ae0c267f-dbbf-4e3f-9041-2ac8d43eedac`; filter selects `casual_server_credits`, aggregation sums `credits`. +- Existing meter benefits grant 360 / 1,500 units with rollover disabled. The application token can read customer meters. It lacks product/meter inspection scopes; those inspections used the authorized CLI instead. +- Signed Polar sample `order.created` was forwarded through the official CLI to the real Django webhook handler: **200 OK**. Real `checkout.created` also returned **200 OK**. +- Real website checkout creation succeeds for the isolated Supporter test product; browser checkout is awaiting user completion. + +## Acceptance fixes + +Bot event requests previously used a 15-second timeout while asking the service to wait 25 seconds. Event polls now allow the complete wait plus response overhead; Python 3.10 asyncio timeouts are translated into the normal service-unavailable error. Regression tests exercise a delayed real localhost HTTP response and Python 3.10 timeout handling. Bot PR CI passed. + +Orchestrator `SERVER_PUBLIC_DISCOVERY=false` forces `Register=Off` even if a caller requests registration. Set this during acceptance and the initial beta. Existing installations retain the prior default when unset. + +## Test runtime compatibility + +The droplet runs Ubuntu 20.04 with glibc 2.31; the current official game binary requires glibc 2.34 symbols. The test harness downloaded Ubuntu Jammy libc6 2.35-0ubuntu3.15, verified the signed Ubuntu archive index and SHA256 hashes, and extracted it into the private test runtime. Only the test executable's interpreter/RPATH were changed; its original executable is preserved as `xRC Simulator.x86_64.original`. System libc and unrelated services were not changed. + +A temporary 512 MiB swap file at `/home/nick/.local/share/second-robotics-test/acceptance.swap` is active, with root ownership and mode 600. It is not configured to survive reboot. Test startup timeout is 120 seconds. The host is shared and memory constrained; these accommodations do not establish production capacity. Confirm the production host can run the chosen official binary and sustain the planned concurrency before cutover. + +## Billing isolation + +Existing sandbox products include a Discord role benefit pointing at the main guild. To avoid changing that guild during acceptance, separate private sandbox products were created with only the matching meter-credit benefits: + +| Tier | Test product ID | +| --- | --- | +| Supporter | `4a900d30-03fb-45cb-a0c4-9053f964d966` | +| VIP Supporter | `794d2bb8-5f3b-4868-975a-fb7c181d8a70` | + +Only the test database uses these mappings. Main sandbox products and their benefits were not modified. Discord-role acceptance remains outstanding. + +The signed-in account already had an older active sandbox VIP subscription under its original email, with no customer external ID. It was left intact. Only the disposable Django account email was changed to a unique acceptance alias for fresh checkout; the real Discord identity is unchanged. A subsequent OAuth login refreshes that test profile email, so finish this run before logging in again. + +Secrets remain in private configuration on the host and the CLI's local credential store; never commit or print them. The official local CLI forwards only to `http://localhost:8000/subscriptions/polar/webhook/`, through the SSH tunnel. Its protected log and temporary files live under `/private/tmp/second-e2e-prep/`. Forwarding and the SSH tunnel must remain running for real checkout events to arrive. + +## Remaining gates, in order + +1. Complete the open **sandbox** Supporter checkout. Verify real subscription webhooks, correct entitlement, 360-credit grant, and no rollover/paid overage configuration. +2. Exercise real casual launches from the website and Discord command. Verify concurrency rejection, duration bounds, stops from both entry points, exactly one five-minute-rounded ledger charge per session, ingestion, and matching meter debit. +3. Exercise live retry/recovery: Polar unavailable at stop, callback outbox replay after website outage, orchestrator restart with live casual/ranked processes, and recovery without double charging. +4. Exercise sandbox lifecycle: upgrade/downgrade, period-end cancellation, renewal reset, past-due/revoked access, and manage-billing link. `POLAR_CUSTOMER_PORTAL_URL` remains to be configured/verified. +5. Configure and verify an isolated Discord role benefit; current test products intentionally omit the main-guild benefit. +6. User deferred real-client/multiplayer participation until automated checks are complete. Still required: client joins, password/player/chat/score verification, complete ranked match, ELO submission, inactivity and resource cleanup, and moderation/IP scoping with actual players. +7. Rehearse migrations on a fresh production backup, review coordinated PRs, and obtain separate merge/cutover authorization. + +The catalog import initially reintroduced stale editable plan copy in the test database. The existing saved-presets migration cleanup was reapplied, and main-guild role promises were removed from the credit-only test plans; do not interpret those promises as implemented features. Preserve customized plan prices and allowances. diff --git a/docs/premium-release-checklist.md b/docs/premium-release-checklist.md index f044d1f..8a6b8b7 100644 --- a/docs/premium-release-checklist.md +++ b/docs/premium-release-checklist.md @@ -1,5 +1,7 @@ # Premium hosting release checklist +Current acceptance evidence: [2026-10-29 test run](premium-acceptance-2026-10-29.md). + This is a coordinated website, ranked-bot, and orchestrator release. Nothing in this preparation authorizes merging or deployment. Keep the premium navbar link hidden for the initial beta. ## Prepared in code @@ -43,6 +45,8 @@ Set up private configuration, without committing secrets: The local website is currently missing its orchestrator URL/token. Production mode also requires explicit non-development service tokens, a non-placeholder shared key, real process mode, and stop-on-restart reconciliation. +Set orchestrator `SERVER_PUBLIC_DISCOVERY=false` for acceptance and the initial beta; this forces public game-browser registration off for all launch sources. + Choose one chat-relay owner: orchestrator webhook **or** bot event relay. Preserve runtime log/score directories and `EVENT_LOG_DIR/website-callbacks` across service restarts. Pending callbacks include connection passwords: protect the directory and its backups. No database for live orchestrator state is needed. Periodically review archived score directories and retain/remove them according to your chosen retention policy. ### 3. Run the sandbox acceptance gate before approving merges From ab431ab64252d0a5803e3825f87e10f368b54e74 Mon Sep 17 00:00:00 2001 From: Nick Bottone Date: Thu, 29 Oct 2026 02:08:43 -0400 Subject: [PATCH 14/28] fix: avoid SQLite lock upgrade when recording server stop --- subscriptions/services.py | 14 ++--- .../test_orchestrator_integration.py | 52 +++++++++++++++++++ 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/subscriptions/services.py b/subscriptions/services.py index 8acadf8..4f068aa 100644 --- a/subscriptions/services.py +++ b/subscriptions/services.py @@ -590,13 +590,13 @@ def request_stop_server_session(session, reason='stopped', actor=None): actor=actor or _actor_for_user(session.user)) except orchestrator.OrchestratorError as exc: raise SubscriptionError(str(exc), status_code=503, code=exc.code) from exc - # A stop callback may have arrived while the HTTP request was in flight. - with transaction.atomic(): - session = ServerSession.objects.select_for_update().get(pk=session.pk) - if session.status not in TERMINAL_SESSION_STATUSES: - session.status = ServerSession.STATUS_STOPPING - session.stop_reason = reason or 'stopping' - session.save(update_fields=['status', 'stop_reason', 'updated_at']) + # A callback may be writing the terminal state while this response arrives. + # A single conditional write waits for SQLite's writer without attempting a + # read-to-write transaction upgrade, and cannot overwrite a terminal state. + ServerSession.objects.filter(pk=session.pk).exclude( + status__in=TERMINAL_SESSION_STATUSES, + ).update(status=ServerSession.STATUS_STOPPING, stop_reason=reason or 'stopping', updated_at=timezone.now()) + session.refresh_from_db() return session diff --git a/subscriptions/test_orchestrator_integration.py b/subscriptions/test_orchestrator_integration.py index 9f5d7cc..db08299 100644 --- a/subscriptions/test_orchestrator_integration.py +++ b/subscriptions/test_orchestrator_integration.py @@ -87,3 +87,55 @@ def wait_for_status(self, session_id, status): return time.sleep(0.05) self.fail(f'Session did not become {status}') + + def test_stop_response_waits_for_sqlite_callback_writer(self): + import threading + from unittest.mock import patch + from django.db import close_old_connections, connection, transaction + from django.utils import timezone + from subscriptions.services import request_stop_server_session + + if connection.vendor != 'sqlite': + self.skipTest('Exercises SQLite read-to-write lock upgrades.') + SubscriptionPlan.objects.get_or_create(tier='supporter', defaults={ + 'name': 'Supporter', 'monthly_price_usd': 5, 'monthly_credits': 360, + 'max_session_minutes': 120, + }) + user = create_user() + entitlement = create_entitlement(user) + session = ServerSession.objects.create( + user=user, entitlement=entitlement, requested_minutes=5, allocated_minutes=5, + status='running', orchestrator_session_id='stop-race', + ) + writer_locked = threading.Event() + errors = [] + + def callback_writer(): + close_old_connections() + try: + with transaction.atomic(): + ServerSession.objects.filter(pk=session.pk).update( + status='stopped', stop_reason='callback', stopped_at=timezone.now(), + ) + writer_locked.set() + time.sleep(0.15) + except Exception as exc: + errors.append(exc) + finally: + close_old_connections() + + writer = threading.Thread(target=callback_writer) + + def stop_response(*args, **kwargs): + writer.start() + self.assertTrue(writer_locked.wait(5), 'Callback writer did not start') + + try: + with patch('subscriptions.orchestrator.stop_server', side_effect=stop_response): + result = request_stop_server_session(session) + self.assertEqual(result.status, 'stopped') + self.assertEqual(result.stop_reason, 'callback') + finally: + writer.join(5) + self.assertFalse(writer.is_alive()) + self.assertEqual(errors, []) From 9235683aa247a456a3785d3238564a626f332053 Mon Sep 17 00:00:00 2001 From: Nick Bottone Date: Thu, 29 Oct 2026 10:01:06 -0400 Subject: [PATCH 15/28] fix: preserve Polar webhook wire aliases --- subscriptions/polar_gateway.py | 2 +- subscriptions/tests.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/subscriptions/polar_gateway.py b/subscriptions/polar_gateway.py index 7666705..a63050c 100644 --- a/subscriptions/polar_gateway.py +++ b/subscriptions/polar_gateway.py @@ -115,5 +115,5 @@ def validate_webhook(body, headers, secret): except Exception as exc: raise PolarGatewayError('Invalid Polar webhook signature.') from exc if hasattr(event, 'model_dump'): - return event.model_dump(mode='json') + return event.model_dump(mode='json', by_alias=True) return dict(event) diff --git a/subscriptions/tests.py b/subscriptions/tests.py index 4e68bc1..71ebcd8 100644 --- a/subscriptions/tests.py +++ b/subscriptions/tests.py @@ -441,6 +441,19 @@ def test_orchestrator_callback_accepts_api_key(self): class PolarWebhookTests(TestCase): + @patch('subscriptions.polar_gateway.validate_event') + def test_sdk_event_model_preserves_wire_discriminator(self, mock_validate_event): + from polar_sdk.models.webhooksubscriptionactivepayload import WebhookSubscriptionActivePayload + from polar_sdk.models.subscription import Subscription + + event = WebhookSubscriptionActivePayload.model_construct( + timestamp=timezone.now(), data=Subscription.model_construct(id='sub-test'), + ) + mock_validate_event.return_value = event + result = validate_polar_webhook('test-secret', b'{}', {}) + self.assertEqual(result['type'], 'subscription.active') + self.assertNotIn('TYPE', result) + @override_settings(POLAR_WEBHOOK_SECRET='whsec_test') @patch('subscriptions.polar_gateway.validate_event') def test_polar_webhook_validation_uses_sdk(self, mock_validate_event): From 7baf2d1ce5eef75b0b83b939706d00d2651ca959 Mon Sep 17 00:00:00 2001 From: Nick Bottone Date: Thu, 29 Oct 2026 10:16:25 -0400 Subject: [PATCH 16/28] fix: remove Polar return credentials from URLs and logs --- SRCweb/log_filters.py | 16 ++++++++++++++++ SRCweb/settings.py | 7 ++++++- subscriptions/test_checkout_return.py | 21 +++++++++++++++++++++ subscriptions/views.py | 4 ++++ 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 SRCweb/log_filters.py create mode 100644 subscriptions/test_checkout_return.py diff --git a/SRCweb/log_filters.py b/SRCweb/log_filters.py new file mode 100644 index 0000000..df23f23 --- /dev/null +++ b/SRCweb/log_filters.py @@ -0,0 +1,16 @@ +"""Keep Polar's short-lived portal credential out of application access logs.""" +import logging +import re + + +class RedactCustomerSessionToken(logging.Filter): + def filter(self, record): + message = record.getMessage() + redacted = re.sub( + r'(customer_session_token=)[^&\s\"\x27]+', + r'\1[REDACTED]', message, flags=re.IGNORECASE, + ) + if redacted != message: + record.msg = redacted + record.args = () + return True diff --git a/SRCweb/settings.py b/SRCweb/settings.py index e9f25cb..a965e04 100644 --- a/SRCweb/settings.py +++ b/SRCweb/settings.py @@ -171,6 +171,7 @@ def required_non_placeholder_env(name, *, development_default=None): 'console': { 'class': 'logging.StreamHandler', 'formatter': 'verbose', + 'filters': ['redact_customer_session_token'], }, 'file': { 'class': 'logging.handlers.RotatingFileHandler', @@ -178,12 +179,13 @@ def required_non_placeholder_env(name, *, development_default=None): 'maxBytes': 1024 * 1024 * 5, # 5 MB 'backupCount': 5, 'formatter': 'verbose', + 'filters': ['redact_customer_session_token'], }, 'mail_admins': { 'level': 'ERROR', 'class': 'django.utils.log.AdminEmailHandler', 'include_html': True, - 'filters': ['require_debug_false'], + 'filters': ['require_debug_false', 'redact_customer_session_token'], } }, 'loggers': { @@ -209,6 +211,9 @@ def required_non_placeholder_env(name, *, development_default=None): }, }, 'filters': { + 'redact_customer_session_token': { + '()': 'SRCweb.log_filters.RedactCustomerSessionToken', + }, 'require_debug_false': { '()': 'django.utils.log.RequireDebugFalse', }, diff --git a/subscriptions/test_checkout_return.py b/subscriptions/test_checkout_return.py new file mode 100644 index 0000000..ff5b56d --- /dev/null +++ b/subscriptions/test_checkout_return.py @@ -0,0 +1,21 @@ +import logging +from django.test import RequestFactory +from subscriptions.views import subscription_home +from SRCweb.log_filters import RedactCustomerSessionToken +from types import SimpleNamespace + + +def test_checkout_return_discards_customer_session_token(): + request = RequestFactory().get('/subscriptions/?customer_session_token=secret') + request.user = SimpleNamespace(is_authenticated=True) + response = subscription_home(request) + assert response.status_code == 302 + assert response.url == '/subscriptions/' + + +def test_access_log_redacts_formatted_customer_session_token(): + record = logging.LogRecord('django.server', logging.INFO, '', 0, + '"GET %s HTTP/1.1" %s', + ('/subscriptions/?customer_session_token=secret&other=value', 302), None) + assert RedactCustomerSessionToken().filter(record) + assert record.getMessage() == '"GET /subscriptions/?customer_session_token=[REDACTED]&other=value HTTP/1.1" 302' diff --git a/subscriptions/views.py b/subscriptions/views.py index 1fad672..17896a9 100644 --- a/subscriptions/views.py +++ b/subscriptions/views.py @@ -29,6 +29,10 @@ def _get_client_ip(request): @login_required(login_url='/login') def subscription_home(request): + # Polar appends a portal credential to success URLs; this page uses the + # existing Django session and should not retain that credential in its URL. + if 'customer_session_token' in request.GET: + return redirect(request.path) plans = SubscriptionPlan.objects.filter(is_active=True).order_by('display_order', 'monthly_price_usd', 'name') entitlement = entitlement_payload(request.user) sessions = ServerSession.objects.filter(user=request.user).order_by('-started_at')[:10] From 182c917d3d68f87cf0bf0f637f9bbf284573397d Mon Sep 17 00:00:00 2001 From: Nick Bottone Date: Thu, 29 Oct 2026 10:20:47 -0400 Subject: [PATCH 17/28] fix: serialize launch responses with SQLite callbacks --- subscriptions/services.py | 6 ++++-- subscriptions/test_launch_readiness.py | 2 +- subscriptions/test_orchestrator_integration.py | 16 +++++++++++++--- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/subscriptions/services.py b/subscriptions/services.py index 4f068aa..83fb97f 100644 --- a/subscriptions/services.py +++ b/subscriptions/services.py @@ -351,11 +351,13 @@ def launch_casual_server(user, requested_minutes=None, game='', game_code='', co mark_server_session_failed(session, exc.code, str(exc)) raise SubscriptionError(str(exc), status_code=503, code=exc.code) from exc - return _record_launch_response(session.pk, payload) + return _record_launch_response(session.pk, payload, user.pk) @transaction.atomic -def _record_launch_response(session_id, payload): +def _record_launch_response(session_id, payload, user_id): + # Acquire the SQLite writer before reading a callback-updated session. + lock_user(user_id) session = ServerSession.objects.select_for_update().get(pk=session_id) if session.status in TERMINAL_SESSION_STATUSES: return session diff --git a/subscriptions/test_launch_readiness.py b/subscriptions/test_launch_readiness.py index 18206d2..cbe444b 100644 --- a/subscriptions/test_launch_readiness.py +++ b/subscriptions/test_launch_readiness.py @@ -57,7 +57,7 @@ def test_terminal_state_survives_reordered_callbacks_and_launch_response(self): session = start_server_session(self.user, 30) self.callback(session, 'stopped', runtime_seconds=61) self.callback(session, 'ready', occurred_at=(timezone.now() - timedelta(minutes=2)).isoformat()) - _record_launch_response(session.pk, {'id': 'orchestrator', 'state': 'starting'}) + _record_launch_response(session.pk, {'id': 'orchestrator', 'state': 'starting'}, self.user.pk) session.refresh_from_db() self.assertEqual(session.status, ServerSession.STATUS_STOPPED) self.assertEqual(UsageLedgerEntry.objects.get().credits, 5) diff --git a/subscriptions/test_orchestrator_integration.py b/subscriptions/test_orchestrator_integration.py index db08299..ce70174 100644 --- a/subscriptions/test_orchestrator_integration.py +++ b/subscriptions/test_orchestrator_integration.py @@ -89,11 +89,17 @@ def wait_for_status(self, session_id, status): self.fail(f'Session did not become {status}') def test_stop_response_waits_for_sqlite_callback_writer(self): + self.assert_response_waits_for_sqlite_callback_writer('stop') + + def test_launch_response_waits_for_sqlite_callback_writer(self): + self.assert_response_waits_for_sqlite_callback_writer('launch') + + def assert_response_waits_for_sqlite_callback_writer(self, response_kind): import threading from unittest.mock import patch from django.db import close_old_connections, connection, transaction from django.utils import timezone - from subscriptions.services import request_stop_server_session + from subscriptions.services import request_stop_server_session, _record_launch_response if connection.vendor != 'sqlite': self.skipTest('Exercises SQLite read-to-write lock upgrades.') @@ -131,8 +137,12 @@ def stop_response(*args, **kwargs): self.assertTrue(writer_locked.wait(5), 'Callback writer did not start') try: - with patch('subscriptions.orchestrator.stop_server', side_effect=stop_response): - result = request_stop_server_session(session) + if response_kind == 'stop': + with patch('subscriptions.orchestrator.stop_server', side_effect=stop_response): + result = request_stop_server_session(session) + else: + stop_response() + result = _record_launch_response(session.pk, {}, user.pk) self.assertEqual(result.status, 'stopped') self.assertEqual(result.stop_reason, 'callback') finally: From 020a90c906f91e4e476522ee68a5f9b2e92208a6 Mon Sep 17 00:00:00 2001 From: Nick Bottone Date: Thu, 29 Oct 2026 10:24:30 -0400 Subject: [PATCH 18/28] fix: acquire callback write lock before reading session state --- subscriptions/services.py | 10 ++++++++-- subscriptions/test_orchestrator_integration.py | 11 +++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/subscriptions/services.py b/subscriptions/services.py index 83fb97f..c9022a2 100644 --- a/subscriptions/services.py +++ b/subscriptions/services.py @@ -515,13 +515,19 @@ def _record_orchestrator_usage(session, stopped_at, runtime_seconds=None): ) -@transaction.atomic def process_orchestrator_event(session_id, payload): + # Resolve ownership outside the write transaction so SQLite does not retain + # a read lock while waiting for another callback/response writer. + owner_id = ServerSession.objects.values_list('user_id', flat=True).get(pk=session_id) + return _process_orchestrator_event(session_id, payload, owner_id) + + +@transaction.atomic +def _process_orchestrator_event(session_id, payload, owner_id): event_id, event_type = payload.get('event_id'), payload.get('event') supported = {'launch_accepted', 'ready', 'heartbeat', 'stopping', 'stopped', 'exited', 'killed', 'launch_failed'} if not event_id or event_type not in supported: raise SubscriptionError('Orchestrator events require an event_id and a supported event.') - owner_id = ServerSession.objects.values_list('user_id', flat=True).get(pk=session_id) lock_user(owner_id) session = ServerSession.objects.select_for_update().get(pk=session_id) event, created = WebhookEvent.objects.get_or_create( diff --git a/subscriptions/test_orchestrator_integration.py b/subscriptions/test_orchestrator_integration.py index ce70174..68bba1f 100644 --- a/subscriptions/test_orchestrator_integration.py +++ b/subscriptions/test_orchestrator_integration.py @@ -94,12 +94,15 @@ def test_stop_response_waits_for_sqlite_callback_writer(self): def test_launch_response_waits_for_sqlite_callback_writer(self): self.assert_response_waits_for_sqlite_callback_writer('launch') + def test_callback_waits_for_sqlite_callback_writer(self): + self.assert_response_waits_for_sqlite_callback_writer('callback') + def assert_response_waits_for_sqlite_callback_writer(self, response_kind): import threading from unittest.mock import patch from django.db import close_old_connections, connection, transaction from django.utils import timezone - from subscriptions.services import request_stop_server_session, _record_launch_response + from subscriptions.services import request_stop_server_session, _record_launch_response, process_orchestrator_event if connection.vendor != 'sqlite': self.skipTest('Exercises SQLite read-to-write lock upgrades.') @@ -140,9 +143,13 @@ def stop_response(*args, **kwargs): if response_kind == 'stop': with patch('subscriptions.orchestrator.stop_server', side_effect=stop_response): result = request_stop_server_session(session) - else: + elif response_kind == 'launch': stop_response() result = _record_launch_response(session.pk, {}, user.pk) + else: + stop_response() + process_orchestrator_event(session.pk, {'event_id': 'concurrent-callback', 'event': 'ready'}) + result = ServerSession.objects.get(pk=session.pk) self.assertEqual(result.status, 'stopped') self.assertEqual(result.stop_reason, 'callback') finally: From ce9f470274376deedf03446c56eb0c8efb24a1cd Mon Sep 17 00:00:00 2001 From: Nick Bottone Date: Thu, 29 Oct 2026 11:16:49 -0400 Subject: [PATCH 19/28] docs: record end-to-end premium acceptance evidence and remaining gates --- docs/premium-acceptance-2026-10-29.md | 140 ++++++++++++++++---------- docs/premium-release-checklist.md | 2 + 2 files changed, 88 insertions(+), 54 deletions(-) diff --git a/docs/premium-acceptance-2026-10-29.md b/docs/premium-acceptance-2026-10-29.md index b8c7b1a..0179cae 100644 --- a/docs/premium-acceptance-2026-10-29.md +++ b/docs/premium-acceptance-2026-10-29.md @@ -1,68 +1,100 @@ # Premium acceptance evidence — 2026-10-29 -Status: **in progress; not cleared for merge or launch**. No production deployment or merge has been performed. +**Status: core integration proven; remaining launch gates are listed below. Do not merge or deploy yet.** -## Tested candidates and environment +No production deployment, merge, production billing change, or main-guild role change was performed. The three coordinated PRs remain unmerged: website #67, bot #85, orchestrator #18. -- Website: `7b46bfc37bcbe04e8ff0c808a9b9dd80b6f35722`. -- Bot: `9927f3e` (acceptance found and fixed idle event polling timeouts). -- Orchestrator: `971a3b1` (adds a host control for private discovery). -- Isolated application files: `ssh digitalocean`, `/home/nick/.local/share/second-robotics-test/releases/20261029/`. -- Test website: loopback port 8000, available locally through the SSH tunnel at `http://localhost:8000/`. -- Test orchestrator: loopback port 3300. Port 3000 belongs to an existing unrelated service and was left intact. -- Discord: guild `663201408581107712`, application `724063847958184046`. -- Fresh SQLite database; only public game-mode and plan configuration copied. Production/customer databases were not copied. -- Python 3.12.16 (website), Python 3.10.22 (bot), Node 22.23.4 (orchestrator). +## Candidate verification -## Verified +| Candidate | Code revision | Verification | +| --- | --- | --- | +| Website | `020a90c` | 45 tests pass locally and on the Linux droplet, including real Django/Fastify HTTP contracts and three concurrent SQLite regressions; final code CI passes | +| Ranked bot | `9927f3e` | 31 tests plus 2 log-parser tests pass on Linux; CI passes | +| Orchestrator | `971a3b1` | 28 tests pass locally and in CI; build and website-contract CI pass; preceding 27-test suite also passed on the droplet | -- Fresh migrations and Django system checks pass. -- Discord OAuth redirects persist; real browser login returned successfully and created an authenticated test session. -- Test bot connects and completes startup in the selected guild. -- Linux automated verification: website **39 tests passed**, including the real HTTP contract test; bot **31 tests plus 2 log-parser tests passed**; orchestrator **27 tests passed** before the discovery addition. The updated orchestrator suite **28 tests passed locally**, and its build/test and website-contract CI checks passed. -- The original low-memory cross-service timeout and later OOM were resolved by cleaning up the orphaned test child and adding temporary swap; the complete website rerun passed. -- Real official xRC server v21.2b reached `running` with readiness timestamps for Override (23) and BioBuzz (24). Both then reached `stopped`; their OS processes were verified absent. -- Official Polar CLI authenticated against the **sandbox** SecondRobotics organization (not the separate SecondRoboticsSandbox organization). -- Configured meter: `ae0c267f-dbbf-4e3f-9041-2ac8d43eedac`; filter selects `casual_server_credits`, aggregation sums `credits`. -- Existing meter benefits grant 360 / 1,500 units with rollover disabled. The application token can read customer meters. It lacks product/meter inspection scopes; those inspections used the authorized CLI instead. -- Signed Polar sample `order.created` was forwarded through the official CLI to the real Django webhook handler: **200 OK**. Real `checkout.created` also returned **200 OK**. -- Real website checkout creation succeeds for the isolated Supporter test product; browser checkout is awaiting user completion. +Fresh database migrations and Django system checks pass. The test database contains copied public game/plan configuration and disposable test users; no production/customer database was copied. -## Acceptance fixes +## Live acceptance results -Bot event requests previously used a 15-second timeout while asking the service to wait 25 seconds. Event polls now allow the complete wait plus response overhead; Python 3.10 asyncio timeouts are translated into the normal service-unavailable error. Regression tests exercise a delayed real localhost HTTP response and Python 3.10 timeout handling. Bot PR CI passed. - -Orchestrator `SERVER_PUBLIC_DISCOVERY=false` forces `Register=Off` even if a caller requests registration. Set this during acceptance and the initial beta. Existing installations retain the prior default when unset. - -## Test runtime compatibility - -The droplet runs Ubuntu 20.04 with glibc 2.31; the current official game binary requires glibc 2.34 symbols. The test harness downloaded Ubuntu Jammy libc6 2.35-0ubuntu3.15, verified the signed Ubuntu archive index and SHA256 hashes, and extracted it into the private test runtime. Only the test executable's interpreter/RPATH were changed; its original executable is preserved as `xRC Simulator.x86_64.original`. System libc and unrelated services were not changed. - -A temporary 512 MiB swap file at `/home/nick/.local/share/second-robotics-test/acceptance.swap` is active, with root ownership and mode 600. It is not configured to survive reboot. Test startup timeout is 120 seconds. The host is shared and memory constrained; these accommodations do not establish production capacity. Confirm the production host can run the chosen official binary and sustain the planned concurrency before cutover. - -## Billing isolation - -Existing sandbox products include a Discord role benefit pointing at the main guild. To avoid changing that guild during acceptance, separate private sandbox products were created with only the matching meter-credit benefits: - -| Tier | Test product ID | +| Gate | Evidence | +| --- | --- | +| Discord OAuth | Both registered localhost redirects persist. Real login returned successfully and created an authenticated session for the intended Discord account. | +| Bot connectivity | The test bot connects and completes startup in Nick's Test Server. A real 25-second idle event poll completes without timing out. | +| Real game processes | Official xRC v21.2b starts Override (23) and BioBuzz (24), reaches `running` with readiness timestamps, then stops. Old OS processes were verified absent. | +| Sandbox checkout | Browser test-card checkout succeeded and created a real Supporter subscription. Initial custom-price checkout could remain in `confirmed` for several minutes before completing. | +| Entitlement and credits | Real signed subscription events activate the correct plan. The initial grant was 360 credits. | +| Service API launch | A Polar-backed real Override session launched through the bot's website API contract. Concurrent launch was rejected (HTTP 403, existing website contract), stop succeeded, and duplicate stop retained one ledger entry. | +| Website UI | A real BioBuzz session launched and stopped using the browser forms. | +| Actual Discord commands | `/casual_start` with Override autocomplete and 5 minutes created a real session; `/casual_status` reported it running; `/casual_stop` finalized it. | +| Usage billing | Three real subscriber sessions each produced one 5-credit ledger entry. Ingestion succeeded, and Polar consumption/balance matched 15 credits consumed / 345 remaining before lifecycle tests. | +| Aggregation delay | After accepted ingestion but before meter aggregation, Polar still reported 360 while the website held the charge and exposed 355. Once aggregation reached 5 consumed, website and meter both reported 355. | +| Ingestion outage | One real usage entry was synced against an intentionally unreachable Polar URL. It became failed; spendable credits stayed held. Restoring the real sandbox endpoint sent it successfully. Re-sync did not duplicate the ledger charge. | +| Exhaustion | A separate zero-credit fixture account was rejected without a reservation or process being created. | +| Duration cap | A 999-minute request was capped to a five-minute fixture plan. The real server stopped automatically with `max_runtime_elapsed`, and one 5-credit usage entry was recorded. | +| Callback outage/restart | Four callback files persisted while the configured receiver was unreachable. Restoring it and restarting the orchestrator replayed them, finalized one charge, and emptied the outbox. This used the complimentary fixture, not paid Polar usage. | +| Crash recovery | With one real ranked and one real casual process alive, the orchestrator was killed. Restart removed both old game processes; reconciliation finalized the lost casual session once. The test invoked reconciliation with zero staleness grace rather than waiting the normal ten minutes. It did not create a real Discord ranked match or prove its role/channel cleanup. | +| Period-end cancellation | A real sandbox cancellation webhook set the cancellation flag while retaining active access. Renewal was then restored. | +| Upgrade | After changing only the private acceptance products to fixed pricing, a real upgrade selected VIP from the actual product ID despite stale supporter metadata. The website showed 1,500 credits and a 240-minute limit; Polar also reported 1,500. | +| Scheduled downgrade | Polar scheduled a Supporter downgrade for the next period while current website access remained VIP. The test schedule was subsequently cleared. | +| Revocation | Immediate revocation of the newly created test subscription removed access. A launch was rejected with HTTP 403 and created no session. A fresh VIP checkout was submitted afterward; it remained confirmed without a payment or new subscription at the final provider check. | +| Billing portal entry | `https://sandbox.polar.sh/secondrobotics/portal` was verified as this organization's customer portal sign-in page and configured as the test website's Manage billing destination. Customer authentication inside that portal remains a manual gate. | + +## Defects found and fixed during acceptance + +1. **Idle bot polling:** the bot asked for a 25-second server wait with a 15-second HTTP deadline. Event requests now allow the whole wait plus overhead; Python 3.10 asyncio timeouts are translated consistently. Regression includes a delayed real localhost HTTP response. +2. **Webhook SDK aliases:** SDK `model_dump()` returned `TYPE`, so signed events were acknowledged but skipped. Serialization now uses wire aliases (`type`); a regression uses the actual SDK event model. Three stored real subscription events were recovered in the disposable test database. +3. **SQLite callback/response races:** stop-response recording, launch-response recording, and callback ownership lookup could upgrade a retained read lock while another writer was active. Stop uses a guarded SQL update; launch locks the user before reading; callback ownership is resolved before its write transaction. Three concurrent file-backed SQLite regressions pass, along with the complete Linux suite. +4. **Private discovery:** `SERVER_PUBLIC_DISCOVERY=false` forces `Register=Off` even when callers request public registration. This is enabled on the test orchestrator and documented for the initial beta. +5. **Return credential handling:** Polar appends a customer-session credential to the checkout success URL. The website discards it from the URL and redacts it from Django access logs. Reverse-proxy access logs must also avoid recording that parameter at eventual cutover. + +## Findings that still block launch clearance + +### Billing lifecycle and product configuration + +The original sandbox Supporter/VIP products use **custom/name-your-price** prices. Polar rejects changing a subscription **to** a custom-price product. Fixed-price copies support the tested upgrade and scheduled downgrade. Nick approved **fixed $5/$12 launch pricing**. Configure production products accordingly; do not retain custom-price targets if normal plan switching is promised. Original products were not modified; only the private acceptance products use fixed prices. + +A deliberately shortened billing period advanced to a new period, and the meter's consumed units reset. However, its old 360-credit benefit did **not** replenish within the observed test window; balance remained zero before the later separate VIP upgrade granted 1,500. Do not treat the successful upgrade grant as proof of monthly renewal replenishment. Resolve and retest actual renewal grants, no rollover, and no paid overage before launch. The organization's `reset` proration mode is disabled; tests used the supported `invoice` mode instead of changing organization settings. + +A real past-due/failed-renewal payment lifecycle has not been exercised. Revocation, period-end cancellation, and mocked failure behavior are covered, but they do not replace this check. + +### Discord benefit and real players + +The existing sandbox products include a role benefit pointing at the **main guild**. Acceptance products omit that benefit to avoid changing main-guild roles. If the launch promises a Discord role, install/configure Polar's role integration for the isolated guild and test grant/revocation there. Otherwise remove that promise from actual launch plan copy. + +Still required with the game client/test players: join/password verification, actual player/chat/score/OPR updates, a complete ranked queue-to-ELO match, inactivity behavior, Discord match resource cleanup, and IP scoping with actual player data. Nick deferred player participation until automated checks were complete. + +### Production prerequisites + +- Rehearse migrations on a **fresh production backup**. Only the older local copy and a fresh empty database have been rehearsed so far. +- Verify the production host can run the official binary and sustain planned concurrency. The current test host required private newer libc and temporary swap. +- Configure production product/meter/benefit IDs, webhook secret, portal URL, service tokens, and recurring workers separately. Sandbox IDs must not be reused in production. +- Review all three PRs together and obtain separate merge/cutover authorization. Website main pushes trigger automatic deployment. + +## Isolated test environment and preservation + +- SSH alias: `digitalocean`. +- Root: `/home/nick/.local/share/second-robotics-test/`; release directory: `releases/20261029/`. +- Website listens on `127.0.0.1:8000`; localhost-only SSH forwarding exposes it at `http://localhost:8000/`. +- Orchestrator listens on `127.0.0.1:3300`. Existing unrelated services on port 3000 were left intact. +- Guild: `663201408581107712`; bot application: `724063847958184046`. +- Runtimes: Python 3.12.16 / Python 3.10.22 / Node 22.23.4. +- The droplet is Ubuntu 20.04, glibc 2.31. Official xRC requires newer libc symbols. Ubuntu Jammy libc6 2.35-0ubuntu3.15 was verified using the signed archive index and SHA256 hashes and extracted privately. Only the test binary's interpreter/RPATH were changed; its original is preserved as `xRC Simulator.x86_64.original`. System libc was not replaced. +- Temporary root-owned mode-600 512 MiB swap file: `acceptance.swap`; it is not configured across reboot. Test startup timeout is 120 seconds. These accommodations do not establish production capacity. +- Private service configuration remains mode 600. Logs, callback outbox, and score archives must be preserved across test restarts. Pending callback files contain connection credentials. +- Official Polar CLI is authenticated to sandbox **SecondRobotics**, not the separate **SecondRoboticsSandbox** organization. It forwards signed events to the private website through the SSH tunnel. Private local artifacts/forwarder logs are under `/private/tmp/second-e2e-prep/`. +- The interrupted turn closed the original SSH tunnel; it was restored as a separate process. Forwarding and the CLI listener must remain running for future events. +- An older sandbox VIP subscription under the original email was left intact. Only the disposable Django profile email was changed to a unique acceptance alias to avoid that pre-existing customer conflict. Do not delete the older subscription as cleanup for this run. + +| Tier | Private acceptance product | | --- | --- | | Supporter | `4a900d30-03fb-45cb-a0c4-9053f964d966` | | VIP Supporter | `794d2bb8-5f3b-4868-975a-fb7c181d8a70` | -Only the test database uses these mappings. Main sandbox products and their benefits were not modified. Discord-role acceptance remains outstanding. - -The signed-in account already had an older active sandbox VIP subscription under its original email, with no customer external ID. It was left intact. Only the disposable Django account email was changed to a unique acceptance alias for fresh checkout; the real Discord identity is unchanged. A subsequent OAuth login refreshes that test profile email, so finish this run before logging in again. - -Secrets remain in private configuration on the host and the CLI's local credential store; never commit or print them. The official local CLI forwards only to `http://localhost:8000/subscriptions/polar/webhook/`, through the SSH tunnel. Its protected log and temporary files live under `/private/tmp/second-e2e-prep/`. Forwarding and the SSH tunnel must remain running for real checkout events to arrive. - -## Remaining gates, in order +The copied catalog's stale saved-presets promise was cleaned using the existing migration, and role promises were removed from these credit-only test plans. Preserve customized launch prices/allowances when applying the actual migration. -1. Complete the open **sandbox** Supporter checkout. Verify real subscription webhooks, correct entitlement, 360-credit grant, and no rollover/paid overage configuration. -2. Exercise real casual launches from the website and Discord command. Verify concurrency rejection, duration bounds, stops from both entry points, exactly one five-minute-rounded ledger charge per session, ingestion, and matching meter debit. -3. Exercise live retry/recovery: Polar unavailable at stop, callback outbox replay after website outage, orchestrator restart with live casual/ranked processes, and recovery without double charging. -4. Exercise sandbox lifecycle: upgrade/downgrade, period-end cancellation, renewal reset, past-due/revoked access, and manage-billing link. `POLAR_CUSTOMER_PORTAL_URL` remains to be configured/verified. -5. Configure and verify an isolated Discord role benefit; current test products intentionally omit the main-guild benefit. -6. User deferred real-client/multiplayer participation until automated checks are complete. Still required: client joins, password/player/chat/score verification, complete ranked match, ELO submission, inactivity and resource cleanup, and moderation/IP scoping with actual players. -7. Rehearse migrations on a fresh production backup, review coordinated PRs, and obtain separate merge/cutover authorization. +## State left for the next acceptance step -The catalog import initially reintroduced stale editable plan copy in the test database. The existing saved-presets migration cleanup was reapplied, and main-guild role promises were removed from the credit-only test plans; do not interpret those promises as implemented features. Preserve customized plan prices and allowances. +- Final website code is running on the droplet after the 45-test Linux pass; all code PR checks are green. No game sessions were intentionally left running. +- The post-revocation VIP checkout remained in `confirmed`, with a blank Stripe 3-D Secure challenge and no payment/subscription at the last check. Reloading it did not resolve the pending provider state. Browser control disconnected during the final UI verification. Do not submit repeated duplicate checkouts or report restoration as successful. +- To keep client acceptance usable while that checkout is pending, the disposable account has a clearly labelled **Temporary client acceptance** complimentary entitlement for two days: 1,500 credits, 240-minute maximum, priority -100. A normal active Polar plan has higher priority and automatically wins. This fixture does not constitute paid-billing or renewal evidence; its usage is not sent to Polar. +- Next owner inputs, in order: authorize/configure the isolated Polar Discord-role integration if roles remain promised; join the test game server and arrange enough people for a complete ranked match; provide a fresh production database backup through a private file path for rehearsal. These can proceed alongside resolving the remaining renewal/checkout provider issues. diff --git a/docs/premium-release-checklist.md b/docs/premium-release-checklist.md index 8a6b8b7..f77510a 100644 --- a/docs/premium-release-checklist.md +++ b/docs/premium-release-checklist.md @@ -19,6 +19,8 @@ This is a coordinated website, ranked-bot, and orchestrator release. Nothing in ### 1. Finish the existing Polar sandbox configuration +The 2026-10-29 acceptance run proves checkout, initial credits, real usage ingestion/debit, outage holds, upgrades with fixed prices, cancellation, and revocation. Fixed $5/$12 pricing is approved; configure the production products that way (Polar rejects upgrades to custom-price targets). Actual renewal replenishment and the post-revocation checkout restoration still require resolution. See the linked evidence report before repeating completed setup. + Local configuration already specifies `POLAR_SERVER=sandbox`, an access token, and a meter ID. Do not paste secrets into chat. Add missing values to the appropriate local/private configuration or secret store: 1. Configure the sandbox webhook endpoint `/subscriptions/polar/webhook/` on a reachable **test** website URL and save its signing secret as `POLAR_WEBHOOK_SECRET`. A local test receiver needs a tunnel or test host; do not point sandbox events at the live production database. From 65cd9116465f4d6e01783f1e71fcb4e032fdd2c2 Mon Sep 17 00:00:00 2001 From: Nick Bottone Date: Thu, 29 Oct 2026 21:00:04 -0400 Subject: [PATCH 20/28] docs: specify isolated Polar Discord authorization setup --- docs/premium-acceptance-2026-10-29.md | 2 ++ docs/premium-discord-test-setup.md | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 docs/premium-discord-test-setup.md diff --git a/docs/premium-acceptance-2026-10-29.md b/docs/premium-acceptance-2026-10-29.md index 0179cae..6a050b9 100644 --- a/docs/premium-acceptance-2026-10-29.md +++ b/docs/premium-acceptance-2026-10-29.md @@ -98,3 +98,5 @@ The copied catalog's stale saved-presets promise was cleaned using the existing - The post-revocation VIP checkout remained in `confirmed`, with a blank Stripe 3-D Secure challenge and no payment/subscription at the last check. Reloading it did not resolve the pending provider state. Browser control disconnected during the final UI verification. Do not submit repeated duplicate checkouts or report restoration as successful. - To keep client acceptance usable while that checkout is pending, the disposable account has a clearly labelled **Temporary client acceptance** complimentary entitlement for two days: 1,500 credits, 240-minute maximum, priority -100. A normal active Polar plan has higher priority and automatically wins. This fixture does not constitute paid-billing or renewal evidence; its usage is not sent to Polar. - Next owner inputs, in order: authorize/configure the isolated Polar Discord-role integration if roles remain promised; join the test game server and arrange enough people for a complete ranked match; provide a fresh production database backup through a private file path for rehearsal. These can proceed alongside resolving the remaining renewal/checkout provider issues. + +Discord setup is now partially automated: two zero-permission premium roles have been created in the test guild. The CLI confirms the remaining blocker is merchant-side Discord server authorization. Exact pages and IDs are in [the Discord test setup guide](premium-discord-test-setup.md). diff --git a/docs/premium-discord-test-setup.md b/docs/premium-discord-test-setup.md new file mode 100644 index 0000000..d03ffb9 --- /dev/null +++ b/docs/premium-discord-test-setup.md @@ -0,0 +1,26 @@ +# Isolated Polar Discord benefit setup + +Most setup is automated. The official Polar CLI can create Discord benefits and attach them to products, but creation currently fails with: `properties.guild_id: This organization has not connected this Discord server. Please connect it again.` + +## Required owner authorization + +1. Open https://sandbox.polar.sh/dashboard/secondrobotics/products/benefits in the **SecondRobotics sandbox** organization (ID `c8c9219f-b5eb-42cb-9c75-2d4445de23ad`). Do not use the separate SecondRoboticsSandbox organization. +2. Start adding a **Discord** benefit, then choose **Connect your Discord server**. +3. In Discord's authorization screen select **Nick's Test Server**, guild `663201408581107712`. +4. Authorize Polar's own Discord app. Polar's documentation lists Manage Roles, Kick Members, and Create Invite as required permissions. This adds Polar's role integration; the existing ranked test bot cannot substitute for it. No new developer application, client secret, or bot token needs to be created by Nick. +5. Return to Polar after authorization. Tell Codex the server is connected; Codex can create/attach the benefits through the CLI. If a role choice is required to finish the dialog, choose the Supporter role below and leave **kick member** disabled. + +Official guide: https://polar.sh/docs/features/benefits/discord-access + +## Prepared roles and products + +Both roles exist already and have permission bitfield `0`; neither has been assigned to users. + +| Tier | Discord role | Role ID | Private sandbox product | +| --- | --- | --- | --- | +| Supporter | Premium Acceptance Supporter | `1565530199838294038` | `4a900d30-03fb-45cb-a0c4-9053f964d966` | +| VIP Supporter | Premium Acceptance VIP | `1565530201893380126` | `794d2bb8-5f3b-4868-975a-fb7c181d8a70` | + +After authorization, Codex should verify the role hierarchy, create distinct benefits with `kick_member=false`, and attach each to its matching acceptance product while preserving the existing meter-credit benefit. Customer Discord connection/grant/revocation testing follows after this merchant-side connection. + +The old benefit `887e581d-f3ca-490c-9a84-cff33751d53b` targets main guild `637407041048281098`; do not edit or reuse it for this test. From 6a23d44f9cafe65500d830acd3c3bc3776f1563c Mon Sep 17 00:00:00 2001 From: Nick Bottone Date: Thu, 29 Oct 2026 21:09:21 -0400 Subject: [PATCH 21/28] docs: record connected Polar test guild and pending customer link --- docs/premium-acceptance-2026-10-29.md | 4 ++++ docs/premium-discord-test-setup.md | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/docs/premium-acceptance-2026-10-29.md b/docs/premium-acceptance-2026-10-29.md index 6a050b9..1823ad0 100644 --- a/docs/premium-acceptance-2026-10-29.md +++ b/docs/premium-acceptance-2026-10-29.md @@ -100,3 +100,7 @@ The copied catalog's stale saved-presets promise was cleaned using the existing - Next owner inputs, in order: authorize/configure the isolated Polar Discord-role integration if roles remain promised; join the test game server and arrange enough people for a complete ranked match; provide a fresh production database backup through a private file path for rehearsal. These can proceed alongside resolving the remaining renewal/checkout provider issues. Discord setup is now partially automated: two zero-permission premium roles have been created in the test guild. The CLI confirms the remaining blocker is merchant-side Discord server authorization. Exact pages and IDs are in [the Discord test setup guide](premium-discord-test-setup.md). + +### Discord integration follow-up + +Merchant-side authorization is complete. Both isolated role benefits are attached with `kick_member=false`, credit benefits are preserved, and Polar's role hierarchy is corrected. The real VIP grant is pending **customer Discord account connection**, not server setup. Exact benefit/grant/role IDs and the portal step are in the Discord test setup guide. The restoration checkout now has an active VIP subscription in Polar (`8889315a-38ce-40ee-aa7b-b7c2cd09cde2`), superseding the earlier confirmed/pending observation. diff --git a/docs/premium-discord-test-setup.md b/docs/premium-discord-test-setup.md index d03ffb9..4fcca3b 100644 --- a/docs/premium-discord-test-setup.md +++ b/docs/premium-discord-test-setup.md @@ -24,3 +24,18 @@ Both roles exist already and have permission bitfield `0`; neither has been assi After authorization, Codex should verify the role hierarchy, create distinct benefits with `kick_member=false`, and attach each to its matching acceptance product while preserving the existing meter-credit benefit. Customer Discord connection/grant/revocation testing follows after this merchant-side connection. The old benefit `887e581d-f3ca-490c-9a84-cff33751d53b` targets main guild `637407041048281098`; do not edit or reuse it for this test. + +## Merchant connection completed + +Nick connected the guild and created both benefits. Codex attached them through the CLI, preserving the credit benefits, verified `kick_member=false`, and placed Polar Sandbox's managed role above both premium roles. + +- Polar Sandbox bot: `1285600567409901569`. +- Polar managed role: `1565531587368722462` (position 2; premium roles remain below it). +- Supporter benefit: `d1e7f83d-a107-41d1-ab09-5b7a050a1f2e`. +- VIP benefit: `1bfbd1df-3201-4ed0-868f-992dd9ed4295`. +- Active restored VIP subscription: `8889315a-38ce-40ee-aa7b-b7c2cd09cde2`. +- Pending VIP Discord grant: `c9c0cebc-e752-4c90-8d27-d9086a899d64`. + +The remaining provider-reported action is **The customer needs to connect their Discord account**. Open https://sandbox.polar.sh/secondrobotics/portal, sign in using the `+premium-acceptance-20261029` checkout email alias, open the Acceptance 20261029 VIP Supporter purchase/benefits, and use **Connect Discord** for **vip discord role**. Connect Discord account `261976608519225345`. Merchant server authorization and customer identity connection are separate steps. + +The existing application token cannot create a customer-portal session (`403 insufficient_scope`), so it cannot skip the portal sign-in. No additional bot application or secret is required. After customer connection, verify the actual Discord member role via API; do not manually assign it as a substitute for Polar granting it. Then exercise benefit removal/regrant on only the private acceptance product. From ceaad195c76f637e8cef61bf9bcb65553bd25263 Mon Sep 17 00:00:00 2001 From: Nick Bottone Date: Thu, 29 Oct 2026 21:20:13 -0400 Subject: [PATCH 22/28] docs: record verified Polar VIP role grant lifecycle --- docs/premium-acceptance-2026-10-29.md | 4 ++++ docs/premium-discord-test-setup.md | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/docs/premium-acceptance-2026-10-29.md b/docs/premium-acceptance-2026-10-29.md index 1823ad0..9bed4ce 100644 --- a/docs/premium-acceptance-2026-10-29.md +++ b/docs/premium-acceptance-2026-10-29.md @@ -104,3 +104,7 @@ Discord setup is now partially automated: two zero-permission premium roles have ### Discord integration follow-up Merchant-side authorization is complete. Both isolated role benefits are attached with `kick_member=false`, credit benefits are preserved, and Polar's role hierarchy is corrected. The real VIP grant is pending **customer Discord account connection**, not server setup. Exact benefit/grant/role IDs and the portal step are in the Discord test setup guide. The restoration checkout now has an active VIP subscription in Polar (`8889315a-38ce-40ee-aa7b-b7c2cd09cde2`), superseding the earlier confirmed/pending observation. + +### VIP role lifecycle passed + +Polar's actual VIP role assignment, removal after benefit detachment, and regrant after reattachment have all been verified through Discord's member API. No manual role assignment was used, and guild membership remained intact. Supporter role acceptance is awaiting its own customer-side Discord connection; a temporary additional benefit on the private VIP test purchase exposes that connection without changing credits. Restore the normal VIP-only role benefit list after this final role test. diff --git a/docs/premium-discord-test-setup.md b/docs/premium-discord-test-setup.md index 4fcca3b..39b3edb 100644 --- a/docs/premium-discord-test-setup.md +++ b/docs/premium-discord-test-setup.md @@ -39,3 +39,9 @@ Nick connected the guild and created both benefits. Codex attached them through The remaining provider-reported action is **The customer needs to connect their Discord account**. Open https://sandbox.polar.sh/secondrobotics/portal, sign in using the `+premium-acceptance-20261029` checkout email alias, open the Acceptance 20261029 VIP Supporter purchase/benefits, and use **Connect Discord** for **vip discord role**. Connect Discord account `261976608519225345`. Merchant server authorization and customer identity connection are separate steps. The existing application token cannot create a customer-portal session (`403 insufficient_scope`), so it cannot skip the portal sign-in. No additional bot application or secret is required. After customer connection, verify the actual Discord member role via API; do not manually assign it as a substitute for Polar granting it. Then exercise benefit removal/regrant on only the private acceptance product. + +## Live VIP role lifecycle verified + +After customer connection, Polar reported the VIP grant successful and the Discord API confirmed role `1565530201893380126` on account `261976608519225345`. Removing the VIP benefit from the private product removed the actual role without removing guild membership; reattaching it restored the role. Codex did not manually assign/revoke the role. The credit benefit remained attached throughout. + +The Supporter grant separately reports `BenefitActionRequiredError: The customer needs to connect their Discord account`. Pending grant: `704f5e83-76c9-452a-8558-12d57c9cf8b9`. To finish this second role's acceptance, the private VIP product temporarily includes both role benefits plus its original credit benefit. The customer should connect **supporter discord role** in the same portal purchase. After verifying Supporter assignment, remove only the temporary Supporter benefit from the VIP product and verify that Supporter disappears while VIP remains. Normal final VIP benefits must be only `e16f5f1e-b7fe-4e6b-ade7-e56c338de4cd` and `1bfbd1df-3201-4ed0-868f-992dd9ed4295`. From 933ea21af55dc936dae5d0abd98d5a99cc9c3713 Mon Sep 17 00:00:00 2001 From: Nick Bottone Date: Thu, 29 Oct 2026 21:23:47 -0400 Subject: [PATCH 23/28] docs: record completed Discord role acceptance and cleanup --- docs/premium-acceptance-2026-10-29.md | 8 +++- docs/premium-discord-test-setup.md | 60 ++++++++++++--------------- 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/docs/premium-acceptance-2026-10-29.md b/docs/premium-acceptance-2026-10-29.md index 9bed4ce..306bd51 100644 --- a/docs/premium-acceptance-2026-10-29.md +++ b/docs/premium-acceptance-2026-10-29.md @@ -37,7 +37,7 @@ Fresh database migrations and Django system checks pass. The test database conta | Upgrade | After changing only the private acceptance products to fixed pricing, a real upgrade selected VIP from the actual product ID despite stale supporter metadata. The website showed 1,500 credits and a 240-minute limit; Polar also reported 1,500. | | Scheduled downgrade | Polar scheduled a Supporter downgrade for the next period while current website access remained VIP. The test schedule was subsequently cleared. | | Revocation | Immediate revocation of the newly created test subscription removed access. A launch was rejected with HTTP 403 and created no session. A fresh VIP checkout was submitted afterward; it remained confirmed without a payment or new subscription at the final provider check. | -| Billing portal entry | `https://sandbox.polar.sh/secondrobotics/portal` was verified as this organization's customer portal sign-in page and configured as the test website's Manage billing destination. Customer authentication inside that portal remains a manual gate. | +| Billing portal entry | `https://sandbox.polar.sh/secondrobotics/portal` was verified as this organization's customer portal sign-in page and configured as the test website's Manage billing destination. Customer portal sign-in and both Discord benefit connections were completed; actual portal UI billing changes remain separate from the API lifecycle checks. | ## Defects found and fixed during acceptance @@ -59,7 +59,7 @@ A real past-due/failed-renewal payment lifecycle has not been exercised. Revocat ### Discord benefit and real players -The existing sandbox products include a role benefit pointing at the **main guild**. Acceptance products omit that benefit to avoid changing main-guild roles. If the launch promises a Discord role, install/configure Polar's role integration for the isolated guild and test grant/revocation there. Otherwise remove that promise from actual launch plan copy. +Discord role acceptance is now complete in the isolated guild: actual VIP grant/removal/regrant and Supporter grant/removal passed. Both private products preserve their credit benefits and use `kick_member=false`. The VIP product is restored to VIP-only role benefits. The original main-guild benefit was not edited. See the Discord test setup guide for exact IDs and evidence. Still required with the game client/test players: join/password verification, actual player/chat/score/OPR updates, a complete ranked queue-to-ELO match, inactivity behavior, Discord match resource cleanup, and IP scoping with actual player data. Nick deferred player participation until automated checks were complete. @@ -108,3 +108,7 @@ Merchant-side authorization is complete. Both isolated role benefits are attache ### VIP role lifecycle passed Polar's actual VIP role assignment, removal after benefit detachment, and regrant after reattachment have all been verified through Discord's member API. No manual role assignment was used, and guild membership remained intact. Supporter role acceptance is awaiting its own customer-side Discord connection; a temporary additional benefit on the private VIP test purchase exposes that connection without changing credits. Restore the normal VIP-only role benefit list after this final role test. + +### Final Discord acceptance and restored access + +Both isolated Discord role lifecycle checks are complete. Polar granted Supporter after its customer connection, then removed it when the temporary benefit was removed; VIP and guild membership remained. The private VIP product is restored to its normal credit-plus-VIP-role benefits. Website and Polar balances both remain 1,500. The restored real Polar VIP entitlement is active, so the temporary `acceptance-client-comp` fallback was deactivated, preserving its history. Earlier notes about pending restoration or customer connections are superseded by this result. Remaining launch gates are renewal credit replenishment/failed-payment lifecycle, real-client/full-ranked acceptance, fresh production-backup rehearsal, and production configuration/capacity. diff --git a/docs/premium-discord-test-setup.md b/docs/premium-discord-test-setup.md index 39b3edb..7adbd32 100644 --- a/docs/premium-discord-test-setup.md +++ b/docs/premium-discord-test-setup.md @@ -1,47 +1,39 @@ -# Isolated Polar Discord benefit setup +# Isolated Polar Discord benefit acceptance -Most setup is automated. The official Polar CLI can create Discord benefits and attach them to products, but creation currently fails with: `properties.guild_id: This organization has not connected this Discord server. Please connect it again.` +**Status: complete.** Merchant authorization and both customer connections are complete. Real role changes were verified through Discord's member API, not inferred from Polar statuses or manually assigned. -## Required owner authorization +## Final configuration -1. Open https://sandbox.polar.sh/dashboard/secondrobotics/products/benefits in the **SecondRobotics sandbox** organization (ID `c8c9219f-b5eb-42cb-9c75-2d4445de23ad`). Do not use the separate SecondRoboticsSandbox organization. -2. Start adding a **Discord** benefit, then choose **Connect your Discord server**. -3. In Discord's authorization screen select **Nick's Test Server**, guild `663201408581107712`. -4. Authorize Polar's own Discord app. Polar's documentation lists Manage Roles, Kick Members, and Create Invite as required permissions. This adds Polar's role integration; the existing ranked test bot cannot substitute for it. No new developer application, client secret, or bot token needs to be created by Nick. -5. Return to Polar after authorization. Tell Codex the server is connected; Codex can create/attach the benefits through the CLI. If a role choice is required to finish the dialog, choose the Supporter role below and leave **kick member** disabled. - -Official guide: https://polar.sh/docs/features/benefits/discord-access - -## Prepared roles and products - -Both roles exist already and have permission bitfield `0`; neither has been assigned to users. +- Polar organization: SecondRobotics **sandbox**, `c8c9219f-b5eb-42cb-9c75-2d4445de23ad`. +- Test guild: Nick's Test Server, `663201408581107712`. +- Test customer Discord account: `261976608519225345`. +- Polar Sandbox bot: `1285600567409901569`. +- Polar managed role: `1565531587368722462`, positioned above both premium roles. +- Both premium roles have permission bitfield `0`; both benefits use `kick_member=false`. -| Tier | Discord role | Role ID | Private sandbox product | +| Tier | Role ID | Benefit ID | Private sandbox product | | --- | --- | --- | --- | -| Supporter | Premium Acceptance Supporter | `1565530199838294038` | `4a900d30-03fb-45cb-a0c4-9053f964d966` | -| VIP Supporter | Premium Acceptance VIP | `1565530201893380126` | `794d2bb8-5f3b-4868-975a-fb7c181d8a70` | +| Supporter | `1565530199838294038` | `d1e7f83d-a107-41d1-ab09-5b7a050a1f2e` | `4a900d30-03fb-45cb-a0c4-9053f964d966` | +| VIP Supporter | `1565530201893380126` | `1bfbd1df-3201-4ed0-868f-992dd9ed4295` | `794d2bb8-5f3b-4868-975a-fb7c181d8a70` | -After authorization, Codex should verify the role hierarchy, create distinct benefits with `kick_member=false`, and attach each to its matching acceptance product while preserving the existing meter-credit benefit. Customer Discord connection/grant/revocation testing follows after this merchant-side connection. +Each product retains its original meter-credit benefit. The VIP product's final benefits are exactly `e16f5f1e-b7fe-4e6b-ade7-e56c338de4cd` (credits) and `1bfbd1df-3201-4ed0-868f-992dd9ed4295` (VIP role). Its temporary Supporter benefit has been removed. -The old benefit `887e581d-f3ca-490c-9a84-cff33751d53b` targets main guild `637407041048281098`; do not edit or reuse it for this test. +## Verified behavior -## Merchant connection completed - -Nick connected the guild and created both benefits. Codex attached them through the CLI, preserving the credit benefits, verified `kick_member=false`, and placed Polar Sandbox's managed role above both premium roles. - -- Polar Sandbox bot: `1285600567409901569`. -- Polar managed role: `1565531587368722462` (position 2; premium roles remain below it). -- Supporter benefit: `d1e7f83d-a107-41d1-ab09-5b7a050a1f2e`. -- VIP benefit: `1bfbd1df-3201-4ed0-868f-992dd9ed4295`. -- Active restored VIP subscription: `8889315a-38ce-40ee-aa7b-b7c2cd09cde2`. -- Pending VIP Discord grant: `c9c0cebc-e752-4c90-8d27-d9086a899d64`. +1. Customer connected VIP in the sandbox portal; Polar granted the actual VIP Discord role. +2. Removing the VIP benefit from the private product removed the actual role, without removing guild membership. +3. Reattaching the VIP benefit restored the actual role. +4. The Supporter benefit separately required customer Discord connection. It was temporarily exposed on the private VIP purchase for this test. +5. After connection, Polar granted the actual Supporter role while VIP remained. +6. Removing only that temporary benefit removed Supporter and retained VIP. The final account has VIP only. +7. The website and Polar both retained 1,500 credits throughout the final role cleanup. -The remaining provider-reported action is **The customer needs to connect their Discord account**. Open https://sandbox.polar.sh/secondrobotics/portal, sign in using the `+premium-acceptance-20261029` checkout email alias, open the Acceptance 20261029 VIP Supporter purchase/benefits, and use **Connect Discord** for **vip discord role**. Connect Discord account `261976608519225345`. Merchant server authorization and customer identity connection are separate steps. +The active restored VIP subscription is `8889315a-38ce-40ee-aa7b-b7c2cd09cde2`. VIP grant: `c9c0cebc-e752-4c90-8d27-d9086a899d64`; Supporter test grant: `704f5e83-76c9-452a-8558-12d57c9cf8b9`. -The existing application token cannot create a customer-portal session (`403 insufficient_scope`), so it cannot skip the portal sign-in. No additional bot application or secret is required. After customer connection, verify the actual Discord member role via API; do not manually assign it as a substitute for Polar granting it. Then exercise benefit removal/regrant on only the private acceptance product. +The old benefit `887e581d-f3ca-490c-9a84-cff33751d53b` targets main guild `637407041048281098` and was not edited. No original product, main-guild role, production billing resource, merge, or deployment was changed. -## Live VIP role lifecycle verified +## Repeating the setup -After customer connection, Polar reported the VIP grant successful and the Discord API confirmed role `1565530201893380126` on account `261976608519225345`. Removing the VIP benefit from the private product removed the actual role without removing guild membership; reattaching it restored the role. Codex did not manually assign/revoke the role. The credit benefit remained attached throughout. +The CLI creates benefits and attaches them to products. The initial server connection requires Discord authorization through the [sandbox benefits page](https://sandbox.polar.sh/dashboard/secondrobotics/products/benefits). Customer connections are completed separately for each benefit in the [sandbox portal](https://sandbox.polar.sh/secondrobotics/portal), using the checkout email alias containing `+premium-acceptance-20261029`. -The Supporter grant separately reports `BenefitActionRequiredError: The customer needs to connect their Discord account`. Pending grant: `704f5e83-76c9-452a-8558-12d57c9cf8b9`. To finish this second role's acceptance, the private VIP product temporarily includes both role benefits plus its original credit benefit. The customer should connect **supporter discord role** in the same portal purchase. After verifying Supporter assignment, remove only the temporary Supporter benefit from the VIP product and verify that Supporter disappears while VIP remains. Normal final VIP benefits must be only `e16f5f1e-b7fe-4e6b-ade7-e56c338de4cd` and `1bfbd1df-3201-4ed0-868f-992dd9ed4295`. +Official setup guide: https://polar.sh/docs/features/benefits/discord-access From 7ad20febed754c3ba3c405a34e738b0a4e2dfc8c Mon Sep 17 00:00:00 2001 From: Nick Bottone Date: Thu, 29 Oct 2026 21:24:47 -0400 Subject: [PATCH 24/28] docs: refresh remaining launch gates after role acceptance --- docs/premium-acceptance-2026-10-29.md | 28 ++++++++------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/docs/premium-acceptance-2026-10-29.md b/docs/premium-acceptance-2026-10-29.md index 306bd51..53ed977 100644 --- a/docs/premium-acceptance-2026-10-29.md +++ b/docs/premium-acceptance-2026-10-29.md @@ -36,7 +36,7 @@ Fresh database migrations and Django system checks pass. The test database conta | Period-end cancellation | A real sandbox cancellation webhook set the cancellation flag while retaining active access. Renewal was then restored. | | Upgrade | After changing only the private acceptance products to fixed pricing, a real upgrade selected VIP from the actual product ID despite stale supporter metadata. The website showed 1,500 credits and a 240-minute limit; Polar also reported 1,500. | | Scheduled downgrade | Polar scheduled a Supporter downgrade for the next period while current website access remained VIP. The test schedule was subsequently cleared. | -| Revocation | Immediate revocation of the newly created test subscription removed access. A launch was rejected with HTTP 403 and created no session. A fresh VIP checkout was submitted afterward; it remained confirmed without a payment or new subscription at the final provider check. | +| Revocation | Immediate revocation of the newly created test subscription removed access. A launch was rejected with HTTP 403 and created no session. A fresh VIP checkout was initially delayed, then completed. Real Polar VIP access and its 1,500-credit balance are restored. | | Billing portal entry | `https://sandbox.polar.sh/secondrobotics/portal` was verified as this organization's customer portal sign-in page and configured as the test website's Manage billing destination. Customer portal sign-in and both Discord benefit connections were completed; actual portal UI billing changes remain separate from the API lifecycle checks. | ## Defects found and fixed during acceptance @@ -92,23 +92,11 @@ Still required with the game client/test players: join/password verification, ac The copied catalog's stale saved-presets promise was cleaned using the existing migration, and role promises were removed from these credit-only test plans. Preserve customized launch prices/allowances when applying the actual migration. -## State left for the next acceptance step +## Current state and next gates -- Final website code is running on the droplet after the 45-test Linux pass; all code PR checks are green. No game sessions were intentionally left running. -- The post-revocation VIP checkout remained in `confirmed`, with a blank Stripe 3-D Secure challenge and no payment/subscription at the last check. Reloading it did not resolve the pending provider state. Browser control disconnected during the final UI verification. Do not submit repeated duplicate checkouts or report restoration as successful. -- To keep client acceptance usable while that checkout is pending, the disposable account has a clearly labelled **Temporary client acceptance** complimentary entitlement for two days: 1,500 credits, 240-minute maximum, priority -100. A normal active Polar plan has higher priority and automatically wins. This fixture does not constitute paid-billing or renewal evidence; its usage is not sent to Polar. -- Next owner inputs, in order: authorize/configure the isolated Polar Discord-role integration if roles remain promised; join the test game server and arrange enough people for a complete ranked match; provide a fresh production database backup through a private file path for rehearsal. These can proceed alongside resolving the remaining renewal/checkout provider issues. - -Discord setup is now partially automated: two zero-permission premium roles have been created in the test guild. The CLI confirms the remaining blocker is merchant-side Discord server authorization. Exact pages and IDs are in [the Discord test setup guide](premium-discord-test-setup.md). - -### Discord integration follow-up - -Merchant-side authorization is complete. Both isolated role benefits are attached with `kick_member=false`, credit benefits are preserved, and Polar's role hierarchy is corrected. The real VIP grant is pending **customer Discord account connection**, not server setup. Exact benefit/grant/role IDs and the portal step are in the Discord test setup guide. The restoration checkout now has an active VIP subscription in Polar (`8889315a-38ce-40ee-aa7b-b7c2cd09cde2`), superseding the earlier confirmed/pending observation. - -### VIP role lifecycle passed - -Polar's actual VIP role assignment, removal after benefit detachment, and regrant after reattachment have all been verified through Discord's member API. No manual role assignment was used, and guild membership remained intact. Supporter role acceptance is awaiting its own customer-side Discord connection; a temporary additional benefit on the private VIP test purchase exposes that connection without changing credits. Restore the normal VIP-only role benefit list after this final role test. - -### Final Discord acceptance and restored access - -Both isolated Discord role lifecycle checks are complete. Polar granted Supporter after its customer connection, then removed it when the temporary benefit was removed; VIP and guild membership remained. The private VIP product is restored to its normal credit-plus-VIP-role benefits. Website and Polar balances both remain 1,500. The restored real Polar VIP entitlement is active, so the temporary `acceptance-client-comp` fallback was deactivated, preserving its history. Earlier notes about pending restoration or customer connections are superseded by this result. Remaining launch gates are renewal credit replenishment/failed-payment lifecycle, real-client/full-ranked acceptance, fresh production-backup rehearsal, and production configuration/capacity. +- The real Polar VIP subscription `8889315a-38ce-40ee-aa7b-b7c2cd09cde2` is active. Website and Polar both report **1,500 credits**. The temporary complimentary fallback has been deactivated; earlier restoration-pending observations are resolved. +- Isolated Discord role acceptance is complete: VIP grant/removal/regrant and Supporter grant/removal were verified directly in Discord. VIP and guild membership were retained during final Supporter removal. Both private products have their intended final benefits, and the temporary extra Supporter benefit was removed from VIP. [Exact configuration and evidence](premium-discord-test-setup.md). +- No production/main-guild resources were changed, and all coordinated PRs remain unmerged. +- Remaining billing gates: demonstrate actual recurring credit replenishment and a failed-renewal/past-due lifecycle. The shortened-cycle renewal observation remains unresolved; a later upgrade grant is not evidence of replenishment. +- Next owner participation: join a real test game and arrange players for a complete ranked match; provide a consistent fresh production database backup through a private file path for migration rehearsal. +- Production fixed $5/$12 product configuration, credentials, capacity validation, recurring workers, coordinated review, and separate cutover authorization remain required before launch. From 48058bdb7262bb2889f786517cc440c18c417d64 Mon Sep 17 00:00:00 2001 From: Nick Bottone Date: Thu, 29 Oct 2026 22:18:21 -0400 Subject: [PATCH 25/28] docs: record recurring billing acceptance and remaining live tests --- docs/premium-acceptance-2026-10-29.md | 22 ++++++++++---- docs/premium-live-test-sequence.md | 43 +++++++++++++++++++++++++++ docs/premium-release-checklist.md | 2 +- 3 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 docs/premium-live-test-sequence.md diff --git a/docs/premium-acceptance-2026-10-29.md b/docs/premium-acceptance-2026-10-29.md index 53ed977..c3df7d9 100644 --- a/docs/premium-acceptance-2026-10-29.md +++ b/docs/premium-acceptance-2026-10-29.md @@ -29,13 +29,14 @@ Fresh database migrations and Django system checks pass. The test database conta | Usage billing | Three real subscriber sessions each produced one 5-credit ledger entry. Ingestion succeeded, and Polar consumption/balance matched 15 credits consumed / 345 remaining before lifecycle tests. | | Aggregation delay | After accepted ingestion but before meter aggregation, Polar still reported 360 while the website held the charge and exposed 355. Once aggregation reached 5 consumed, website and meter both reported 355. | | Ingestion outage | One real usage entry was synced against an intentionally unreachable Polar URL. It became failed; spendable credits stayed held. Restoring the real sandbox endpoint sent it successfully. Re-sync did not duplicate the ledger charge. | -| Exhaustion | A separate zero-credit fixture account was rejected without a reservation or process being created. | +| Exhaustion | A separate zero-credit fixture account was rejected without a reservation or process being created. Later, the real paid VIP customer had an actual zero Polar meter balance during renewal: launch returned HTTP 403 and the server-session count was unchanged. This proves the tested launch path does not create paid overage at zero balance. | | Duration cap | A 999-minute request was capped to a five-minute fixture plan. The real server stopped automatically with `max_runtime_elapsed`, and one 5-credit usage entry was recorded. | | Callback outage/restart | Four callback files persisted while the configured receiver was unreachable. Restoring it and restarting the orchestrator replayed them, finalized one charge, and emptied the outbox. This used the complimentary fixture, not paid Polar usage. | | Crash recovery | With one real ranked and one real casual process alive, the orchestrator was killed. Restart removed both old game processes; reconciliation finalized the lost casual session once. The test invoked reconciliation with zero staleness grace rather than waiting the normal ten minutes. It did not create a real Discord ranked match or prove its role/channel cleanup. | | Period-end cancellation | A real sandbox cancellation webhook set the cancellation flag while retaining active access. Renewal was then restored. | | Upgrade | After changing only the private acceptance products to fixed pricing, a real upgrade selected VIP from the actual product ID despite stale supporter metadata. The website showed 1,500 credits and a 240-minute limit; Polar also reported 1,500. | | Scheduled downgrade | Polar scheduled a Supporter downgrade for the next period while current website access remained VIP. The test schedule was subsequently cleared. | +| Standalone renewal / no rollover | Fixed-price VIP renewal produced paid `subscription_cycle` order `60cd3321-9a77-4818-890b-1245fc3bcfa2`. After the provider's fifteen-minute order delay, both Polar and the website reported exactly 1,500 credits, consumed units zero, and the new period October 30–November 30. The previous 1,500 credits did not roll over; no webhook failures were present. | | Revocation | Immediate revocation of the newly created test subscription removed access. A launch was rejected with HTTP 403 and created no session. A fresh VIP checkout was initially delayed, then completed. Real Polar VIP access and its 1,500-credit balance are restored. | | Billing portal entry | `https://sandbox.polar.sh/secondrobotics/portal` was verified as this organization's customer portal sign-in page and configured as the test website's Manage billing destination. Customer portal sign-in and both Discord benefit connections were completed; actual portal UI billing changes remain separate from the API lifecycle checks. | @@ -53,15 +54,23 @@ Fresh database migrations and Django system checks pass. The test database conta The original sandbox Supporter/VIP products use **custom/name-your-price** prices. Polar rejects changing a subscription **to** a custom-price product. Fixed-price copies support the tested upgrade and scheduled downgrade. Nick approved **fixed $5/$12 launch pricing**. Configure production products accordingly; do not retain custom-price targets if normal plan switching is promised. Original products were not modified; only the private acceptance products use fixed prices. -A deliberately shortened billing period advanced to a new period, and the meter's consumed units reset. However, its old 360-credit benefit did **not** replenish within the observed test window; balance remained zero before the later separate VIP upgrade granted 1,500. Do not treat the successful upgrade grant as proof of monthly renewal replenishment. Resolve and retest actual renewal grants, no rollover, and no paid overage before launch. The organization's `reset` proration mode is disabled; tests used the supported `invoice` mode instead of changing organization settings. +The first deliberately shortened billing period advanced, and the meter's consumed units reset. Its old 360-credit benefit did **not** replenish before the later separate VIP upgrade granted 1,500. That upgrade was not proof of monthly replenishment. The separate follow-up below now proves standalone renewal and no rollover; a real zero-balance launch was also rejected. The organization's `reset` proration mode is disabled; tests used the supported `invoice` mode instead of changing organization settings. A real past-due/failed-renewal payment lifecycle has not been exercised. Revocation, period-end cancellation, and mocked failure behavior are covered, but they do not replace this check. +Follow-up renewal diagnosis: the sandbox organization reports `subscription_renewals=true` and zero-day benefit-revocation grace. A second test shortened the **fixed-price** restored VIP subscription to `2026-10-30T01:58:55.051431Z`. Its period advanced to November 30, but initially there was no cycle order, and both provider balance and website spendable credits became zero. Polar's current public implementation deliberately defers recurring order creation for fifteen minutes after the cutoff, then cycles credit grants after the renewal order is paid ([order task](https://github.com/polarsource/polar/blob/main/server/polar/order/tasks.py), [paid-order handling](https://github.com/polarsource/polar/blob/main/server/polar/order/service.py)). At `02:14:26Z`, the deployed sandbox was observed to have produced paid recurring order `60cd3321-9a77-4818-890b-1245fc3bcfa2`; both provider and website had exactly 1,500 credits, and no webhooks had failed. Standalone replenishment and no rollover are now proven. The temporary zero-credit renewal interval is real behavior to account for in launch messaging. + +The earlier upgrade order `bab5689d-5225-4aff-bc82-2f7342356614` has a $17 pre-tax subtotal with two lines: a $5 non-prorated Supporter cycle starting `14:15:57Z`, and a $12 VIP upgrade starting `14:24:09Z`. Thus the pending Supporter cycle was collected in the upgrade invoice; its absence as a separate cycle order does not demonstrate an uncharged renewal. The upgrade/grant still cannot prove standalone recurring credit replenishment. + +The customer portal's Add Payment Method flow initially stalled on a dimmed loading overlay. One reload and retry opened the form successfully. Stripe's documented decline-after-attachment card ending `0341` was attached in sandbox. Polar automatically made the new card the default; a subsequent API check caught this, and the successful `4242` method `07587eda-50df-4821-ac3d-bf4d313f3b19` was restored and verified as default before the pending renewal charge. The failure method `aca3b621-e73d-4619-94a0-e5f3f953a649` remains attached for the subsequent failed-renewal test; attachment alone does not prove a payment failure. See [Stripe's test-card behavior](https://docs.stripe.com/testing#declined-payments). + +After successful renewal, selecting the saved failure card showed “Failed to update default payment method / Failed to update customer.” A fresh attachment attempt stalled; refreshing returned the portal to sign-in. The session is now awaiting the owner's emailed verification code in Edge. The failure cause is not established. Final API verification still shows the successful `4242` card as default, subscription active through November 30, and no second shortened period scheduled. Resume failed-renewal setup after reauthentication; do not claim that gate passed. + ### Discord benefit and real players Discord role acceptance is now complete in the isolated guild: actual VIP grant/removal/regrant and Supporter grant/removal passed. Both private products preserve their credit benefits and use `kick_member=false`. The VIP product is restored to VIP-only role benefits. The original main-guild benefit was not edited. See the Discord test setup guide for exact IDs and evidence. -Still required with the game client/test players: join/password verification, actual player/chat/score/OPR updates, a complete ranked queue-to-ELO match, inactivity behavior, Discord match resource cleanup, and IP scoping with actual player data. Nick deferred player participation until automated checks were complete. +Still required with the game client/test players: join/password verification, actual player/chat/score/OPR updates, a complete ranked queue-to-ELO match, inactivity behavior, Discord match resource cleanup, and IP scoping with actual player data. Nick has confirmed two players can participate. The droplet initially blocked game ports; temporary source-restricted rules are now prepared as described below. A real Rebuilt 1v1 preflight reached running, listened on TCP and UDP port 11115 with public discovery off, and stopped. This is not a completed Discord ranked match or a real-client reachability test. ### Production prerequisites @@ -90,13 +99,14 @@ Still required with the game client/test players: join/password verification, ac | Supporter | `4a900d30-03fb-45cb-a0c4-9053f964d966` | | VIP Supporter | `794d2bb8-5f3b-4868-975a-fb7c181d8a70` | -The copied catalog's stale saved-presets promise was cleaned using the existing migration, and role promises were removed from these credit-only test plans. Preserve customized launch prices/allowances when applying the actual migration. +The copied catalog's stale saved-presets promise was cleaned using the existing migration. The acceptance products initially used credits only; their intended Discord benefits have since been configured and verified as described above. Preserve customized launch prices/allowances when applying the actual migration. ## Current state and next gates -- The real Polar VIP subscription `8889315a-38ce-40ee-aa7b-b7c2cd09cde2` is active. Website and Polar both report **1,500 credits**. The temporary complimentary fallback has been deactivated; earlier restoration-pending observations are resolved. +- The real Polar VIP subscription `8889315a-38ce-40ee-aa7b-b7c2cd09cde2` passed standalone renewal: both provider and website report **1,500 credits** for October 30–November 30. The temporary complimentary fallback remains deactivated. - Isolated Discord role acceptance is complete: VIP grant/removal/regrant and Supporter grant/removal were verified directly in Discord. VIP and guild membership were retained during final Supporter removal. Both private products have their intended final benefits, and the temporary extra Supporter benefit was removed from VIP. [Exact configuration and evidence](premium-discord-test-setup.md). - No production/main-guild resources were changed, and all coordinated PRs remain unmerged. -- Remaining billing gates: demonstrate actual recurring credit replenishment and a failed-renewal/past-due lifecycle. The shortened-cycle renewal observation remains unresolved; a later upgrade grant is not evidence of replenishment. +- Follow the [ordered remaining live-test sequence](premium-live-test-sequence.md), including exact test queue/channel and evidence requirements. Temporary UFW rules now allow TCP/UDP ports 11115–11117 only from the current SSH client address; a private cleanup record is stored at `acceptance-firewall.json` under the test root. A second player on another network requires a separate source rule. +- Remaining billing gate: a failed-renewal/past-due lifecycle and payment recovery. Standalone recurring credit replenishment, no rollover, and rejecting actual zero-balance launches are now proven. - Next owner participation: join a real test game and arrange players for a complete ranked match; provide a consistent fresh production database backup through a private file path for migration rehearsal. - Production fixed $5/$12 product configuration, credentials, capacity validation, recurring workers, coordinated review, and separate cutover authorization remain required before launch. diff --git a/docs/premium-live-test-sequence.md b/docs/premium-live-test-sequence.md new file mode 100644 index 0000000..20f45f3 --- /dev/null +++ b/docs/premium-live-test-sequence.md @@ -0,0 +1,43 @@ +# Remaining live acceptance sequence + +Use the isolated 20261029 release on `ssh digitalocean`, test guild +`663201408581107712`, and bot `724063847958184046`. No production deployment +or merge is authorized. Completed evidence remains in +[the acceptance report](premium-acceptance-2026-10-29.md). + +| Order | Operator action | Required evidence | +| --- | --- | --- | +| 1 — passed | Agent scheduled a short fixed-price sandbox billing period and observed provider orders, credits, signed webhooks, and website entitlement. | Paid `subscription_cycle` order `60cd3321-9a77-4818-890b-1245fc3bcfa2`; exactly 1,500 credits after the fifteen-minute billing delay, without rollover; website and provider agree. | +| 2 | Agent configures a supported sandbox payment failure and triggers another renewal. Owner handles private payment/account authentication if necessary. | Provider marks past due; website denies new paid launches; benefits follow the configured zero-day grace; restoring payment recovers access without duplicate credit or usage. | +| 3 | Owner confirms player networks and Discord accounts. Agent opens only the required test game access, checks profiles in the disposable database, and starts a casual server. | Both real clients reach the advertised address. Wrong password fails; correct password succeeds. Public discovery remains off. | +| 4 | Players join opposing alliances, send an identifiable test chat message, and play a scored game. Agent inspects API/events/logs and stops the server. | Player presence, chat, score and OPR agree with the clients; scopes protect actual player IP data; stop closes the process and debits once. | +| 5 | Both players use `/queuestandard`, selecting **Rebuilt 1v1**, in [the test queue channel](https://discord.com/channels/663201408581107712/1565227916432900126). | Bot creates private match resources, supplies a working server/password, and assigns the correct players. No production account creation or leaderboard writes. | +| 6 | Players play and submit actual red/blue scores with `/submit` in the match channel; continue until the bot reports the series complete. | Staging match rows and ELO history match submitted scores; channels/roles and game process are cleaned up after the series. Respect the command's two-minute cooldown. | +| 7 | Agent coordinates a fresh test match for idle timeout, then a separate restart/network-disruption check. Players follow the requested join/leave steps. | Idle/restart cleanup removes stale Discord resources and game processes; a transient network failure does not erase a live match; a new match can start afterward. | +| 8 | Owner supplies a working local production SSH alias and absolute SQLite path. Agent makes a consistent snapshot through a read-only SQLite connection and runs the rehearsal on a disposable copy. | Migrations, modeled columns, and foreign-key checks pass. Production database and service processes are unchanged. | +| 9 | Agent reconciles evidence, fixes findings, runs relevant checks, and updates the three draft PRs. | Every required gate has recorded evidence or a specific unresolved blocker. Merge and production cutover remain a separate owner decision. | + +## Inputs still needed + +- Whether players share a public network; if not, the second player's network must + be included in the temporary game firewall rules. Do not share SSH keys with players. +- The second player's Discord ID if they are not the existing `easierthanpai` + test-guild member. Any profile seeded for ranked acceptance is a test fixture; + it does not count as an additional OAuth login test. +- Production SSH alias and absolute database path. Keep credentials in local SSH + configuration or the SSH agent, not chat. Do not copy a live SQLite file using + plain file copying while writes may be occurring. If the database path is + unknown, provide the working alias first; the agent can locate it read-only. +- Complete the pending Polar sandbox customer-portal email verification in Edge. + The acceptance alias contains `+premium-acceptance-20261029`; its mail arrives + in the owner's usual Gmail inbox. Keep the verification code out of chat. + +Temporary UFW rules now permit game TCP/UDP ports 11115–11117 from the current +SSH client address only, in addition to the existing SSH and Nginx rules. The +private `acceptance-firewall.json` cleanup record is under the test root. A second +player on a different network needs a separate source rule. A real Rebuilt 1v1 +preflight reached running on port 11115, listened on both TCP and UDP, kept public +discovery off, and accepted a stop request. Actual client reachability remains to +be tested. +Use the current matching xRC client revision; the installed test server is v21.2b. +Connection ports and passwords come from the actual session, not this document. diff --git a/docs/premium-release-checklist.md b/docs/premium-release-checklist.md index f77510a..cf8cf4e 100644 --- a/docs/premium-release-checklist.md +++ b/docs/premium-release-checklist.md @@ -19,7 +19,7 @@ This is a coordinated website, ranked-bot, and orchestrator release. Nothing in ### 1. Finish the existing Polar sandbox configuration -The 2026-10-29 acceptance run proves checkout, initial credits, real usage ingestion/debit, outage holds, upgrades with fixed prices, cancellation, and revocation. Fixed $5/$12 pricing is approved; configure the production products that way (Polar rejects upgrades to custom-price targets). Actual renewal replenishment and the post-revocation checkout restoration still require resolution. See the linked evidence report before repeating completed setup. +The 2026-10-29 acceptance run proves checkout, initial credits, real usage ingestion/debit, outage holds, upgrades with fixed prices, cancellation, revocation, restored access after a fresh checkout, both Discord role lifecycles, and standalone recurring replenishment without rollover. Actual zero-provider-balance launches are denied without creating sessions. Fixed $5/$12 pricing is approved; configure the production products that way (Polar rejects upgrades to custom-price targets). Failed-renewal behavior and recovery still require verification. See the linked evidence report before repeating completed setup. Local configuration already specifies `POLAR_SERVER=sandbox`, an access token, and a meter ID. Do not paste secrets into chat. Add missing values to the appropriate local/private configuration or secret store: From f63a0b72c9335dac6f95bf4ee28c60b3cafb3eb6 Mon Sep 17 00:00:00 2001 From: Nick Bottone Date: Fri, 30 Oct 2026 11:21:39 -0400 Subject: [PATCH 26/28] docs: record fresh production migration rehearsal and runtime prerequisites --- docs/premium-acceptance-2026-10-29.md | 14 +++++++++++--- docs/premium-live-test-sequence.md | 16 ++++++++-------- docs/premium-release-checklist.md | 6 +++++- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/docs/premium-acceptance-2026-10-29.md b/docs/premium-acceptance-2026-10-29.md index c3df7d9..faa7795 100644 --- a/docs/premium-acceptance-2026-10-29.md +++ b/docs/premium-acceptance-2026-10-29.md @@ -12,7 +12,9 @@ No production deployment, merge, production billing change, or main-guild role c | Ranked bot | `9927f3e` | 31 tests plus 2 log-parser tests pass on Linux; CI passes | | Orchestrator | `971a3b1` | 28 tests pass locally and in CI; build and website-contract CI pass; preceding 27-test suite also passed on the droplet | -Fresh database migrations and Django system checks pass. The test database contains copied public game/plan configuration and disposable test users; no production/customer database was copied. +Fresh database migrations and Django system checks pass. The staging database contains copied public game/plan configuration and disposable test users; it does not contain production customer records. + +On October 30, a fresh production SQLite snapshot was copied into private local storage for the migration rehearsal only. It was not uploaded to the droplet, committed, or used as the staging customer database. The read-only source was `/home/bottxleg/repositories/SecondWebsite/db.sqlite3` through `ssh namecheap`; SQLite's backup API produced a consistent 142,123,008-byte snapshot and its quick check passed. The release migrated a separate disposable copy successfully, including the existing ranked schema's fake-initial baseline and subscriptions migrations through `0006`. All modeled columns were present, foreign-key violations were zero, and exact data digests/counts matched across all 22 existing application tables. The original snapshot's checksum was unchanged after rehearsal. ## Live acceptance results @@ -66,15 +68,21 @@ The customer portal's Add Payment Method flow initially stalled on a dimmed load After successful renewal, selecting the saved failure card showed “Failed to update default payment method / Failed to update customer.” A fresh attachment attempt stalled; refreshing returned the portal to sign-in. The session is now awaiting the owner's emailed verification code in Edge. The failure cause is not established. Final API verification still shows the successful `4242` card as default, subscription active through November 30, and no second shortened period scheduled. Resume failed-renewal setup after reauthentication; do not claim that gate passed. +October 30 follow-up: the owner completed fresh portal authentication, and selecting the existing failure card succeeded. The provider API verified `0341` as default. The test period was shortened to `2026-10-30T15:09:47.809701Z`; observe the failed charge after the fifteen-minute billing delay. The localhost SSH tunnel had stopped overnight while the official webhook listener remained alive, causing connection-refused forwarding failures. The tunnel was restored, its HTTP endpoint returned 200, and a metadata-only update preserving existing metadata requested a fresh signed full-state event. This local forwarding interruption must not be confused with application webhook rejection. The earlier portal/card-setting blockage is resolved; failed payment and recovery remain under observation. + ### Discord benefit and real players Discord role acceptance is now complete in the isolated guild: actual VIP grant/removal/regrant and Supporter grant/removal passed. Both private products preserve their credit benefits and use `kick_member=false`. The VIP product is restored to VIP-only role benefits. The original main-guild benefit was not edited. See the Discord test setup guide for exact IDs and evidence. Still required with the game client/test players: join/password verification, actual player/chat/score/OPR updates, a complete ranked queue-to-ELO match, inactivity behavior, Discord match resource cleanup, and IP scoping with actual player data. Nick has confirmed two players can participate. The droplet initially blocked game ports; temporary source-restricted rules are now prepared as described below. A real Rebuilt 1v1 preflight reached running, listened on TCP and UDP port 11115 with public discovery off, and stopped. This is not a completed Discord ranked match or a real-client reachability test. +The fresh production snapshot includes Override/BioBuzz ranked modes missing from the older public catalog copied into staging. Only its 53 public game-mode records were copied to the isolated test database; no production user data was uploaded. With no active server sessions, only the test bot was restarted. Discord REST confirmed registered standard choices for BioBuzz 1v1/2v2, Override 1v1/2v2, and Rebuilt 1v1/2v2/3v3. + ### Production prerequisites -- Rehearse migrations on a **fresh production backup**. Only the older local copy and a fresh empty database have been rehearsed so far. +- Fresh production-backup migration rehearsal **passed October 30** as recorded above. Take another consistent backup immediately before the eventual authorized cutover. +- Namecheap currently runs Python 3.9.23/Django 3.2.25. Python 3.12 is installed at `/opt/alt/python312/bin/python3.12`; the application's live Python selection/virtual environment must be moved to 3.12 at the separately authorized cutover. It has not been changed. The candidate requires Django 5.x and CI uses Python 3.12. +- Production currently has no subscriptions tables. The rehearsal seeds the default Host plan; the approved fixed $5/$12 Supporter/VIP plans, allowances, and production Polar mappings still need explicit configuration before exposing premium. Local/staging plan customization is not production configuration. - Verify the production host can run the official binary and sustain planned concurrency. The current test host required private newer libc and temporary swap. - Configure production product/meter/benefit IDs, webhook secret, portal URL, service tokens, and recurring workers separately. Sandbox IDs must not be reused in production. - Review all three PRs together and obtain separate merge/cutover authorization. Website main pushes trigger automatic deployment. @@ -108,5 +116,5 @@ The copied catalog's stale saved-presets promise was cleaned using the existing - No production/main-guild resources were changed, and all coordinated PRs remain unmerged. - Follow the [ordered remaining live-test sequence](premium-live-test-sequence.md), including exact test queue/channel and evidence requirements. Temporary UFW rules now allow TCP/UDP ports 11115–11117 only from the current SSH client address; a private cleanup record is stored at `acceptance-firewall.json` under the test root. A second player on another network requires a separate source rule. - Remaining billing gate: a failed-renewal/past-due lifecycle and payment recovery. Standalone recurring credit replenishment, no rollover, and rejecting actual zero-balance launches are now proven. -- Next owner participation: join a real test game and arrange players for a complete ranked match; provide a consistent fresh production database backup through a private file path for migration rehearsal. +- Next owner participation: confirm the second player's account and readiness for a real test game/full ranked match. Both players share the current network, and its address matches the prepared source-restricted game rules. The fresh production backup has been supplied through SSH and rehearsed successfully. - Production fixed $5/$12 product configuration, credentials, capacity validation, recurring workers, coordinated review, and separate cutover authorization remain required before launch. diff --git a/docs/premium-live-test-sequence.md b/docs/premium-live-test-sequence.md index 20f45f3..45bd465 100644 --- a/docs/premium-live-test-sequence.md +++ b/docs/premium-live-test-sequence.md @@ -14,7 +14,7 @@ or merge is authorized. Completed evidence remains in | 5 | Both players use `/queuestandard`, selecting **Rebuilt 1v1**, in [the test queue channel](https://discord.com/channels/663201408581107712/1565227916432900126). | Bot creates private match resources, supplies a working server/password, and assigns the correct players. No production account creation or leaderboard writes. | | 6 | Players play and submit actual red/blue scores with `/submit` in the match channel; continue until the bot reports the series complete. | Staging match rows and ELO history match submitted scores; channels/roles and game process are cleaned up after the series. Respect the command's two-minute cooldown. | | 7 | Agent coordinates a fresh test match for idle timeout, then a separate restart/network-disruption check. Players follow the requested join/leave steps. | Idle/restart cleanup removes stale Discord resources and game processes; a transient network failure does not erase a live match; a new match can start afterward. | -| 8 | Owner supplies a working local production SSH alias and absolute SQLite path. Agent makes a consistent snapshot through a read-only SQLite connection and runs the rehearsal on a disposable copy. | Migrations, modeled columns, and foreign-key checks pass. Production database and service processes are unchanged. | +| 8 — passed | Agent used the supplied `ssh namecheap` alias to take a consistent read-only SQLite snapshot and migrate a disposable local copy on October 30. | All modeled columns present; zero foreign-key violations; existing data preserved exactly across 22 application tables; original snapshot unchanged. Production was not migrated. | | 9 | Agent reconciles evidence, fixes findings, runs relevant checks, and updates the three draft PRs. | Every required gate has recorded evidence or a specific unresolved blocker. Merge and production cutover remain a separate owner decision. | ## Inputs still needed @@ -24,13 +24,13 @@ or merge is authorized. Completed evidence remains in - The second player's Discord ID if they are not the existing `easierthanpai` test-guild member. Any profile seeded for ranked acceptance is a test fixture; it does not count as an additional OAuth login test. -- Production SSH alias and absolute database path. Keep credentials in local SSH - configuration or the SSH agent, not chat. Do not copy a live SQLite file using - plain file copying while writes may be occurring. If the database path is - unknown, provide the working alias first; the agent can locate it read-only. -- Complete the pending Polar sandbox customer-portal email verification in Edge. - The acceptance alias contains `+premium-acceptance-20261029`; its mail arrives - in the owner's usual Gmail inbox. Keep the verification code out of chat. +- Production SSH access and the fresh-backup rehearsal are complete. The live + application's Python 3.9 → 3.12 switch remains a cutover prerequisite; it has + not been performed. +- Polar reauthentication is complete. The failure card is now selected, and the + shortened failed renewal is scheduled for October 30, 15:09:47 UTC. Observe the + actual payment attempt after the fifteen-minute billing delay before judging + past-due behavior. Keep credentials and verification codes out of chat. Temporary UFW rules now permit game TCP/UDP ports 11115–11117 from the current SSH client address only, in addition to the existing SSH and Nginx rules. The diff --git a/docs/premium-release-checklist.md b/docs/premium-release-checklist.md index cf8cf4e..fc42a4a 100644 --- a/docs/premium-release-checklist.md +++ b/docs/premium-release-checklist.md @@ -66,7 +66,7 @@ The automated checks cover the service contracts and mocked failure cases. They ### 4. Rehearse against a fresh production database backup -The old local SQLite copy migrated successfully with all modeled columns and no foreign-key violations. It already contained subscriptions migrations through `0003`, so this does **not** replace testing a current backup from the actual production host. +**Passed October 30:** a fresh consistent snapshot from `ssh namecheap`, `/home/bottxleg/repositories/SecondWebsite/db.sqlite3`, migrated successfully on a disposable local copy. All modeled columns were present, foreign-key violations were zero, and existing data matched exactly across 22 application tables. Production was opened read-only and was not migrated. The private snapshot remains outside Git and was not uploaded into staging. Take another fresh consistent backup immediately before cutover. Run against a downloaded backup, using a dependency-equipped Python 3.12 environment: @@ -80,6 +80,10 @@ The utility opens the source read-only, makes a disposable backup, runs `migrate After green CI, sandbox/real-game acceptance, and the current-backup rehearsal, review the three PRs together. Explicit approval to merge/deploy remains a separate step. At cutover: pause new launches, drain legacy servers, back up/migrate the website, start the orchestrator, then switch the bot to the matching release. Keep public discovery disabled during the initial beta. Record all deployed SHAs. +The actual Namecheap application currently uses Python 3.9.23 and Django 3.2.25. Python 3.12 is installed, but the live application still needs to select it. **At the authorized cutover**, use cPanel → Setup Python App → edit the SecondWebsite application → select Python 3.12. Copy the resulting virtual-environment activation command from cPanel, verify `python --version`, and install the release requirements in that environment before migrations/restart. Do not reuse the old `3.9/bin/activate` command or assume changing the SSH shell's Python changes Passenger. [Namecheap's Python application settings](https://www.namecheap.com/support/knowledgebase/article.aspx/10048/2182/how-to-work-with-python-app/). + +The fresh production database has no subscriptions tables yet; the migration seeds a default Host plan. Configure the approved Supporter ($5, 360 credits, 120-minute cap) and VIP ($12, 1,500 credits, 240-minute cap) plans and their production Polar IDs explicitly, then deactivate any unintended default plan before exposing checkout. Staging's mapped products and customized plans are not present in production. + **The existing website workflow deploys automatically after a push to `main`. Merging its PR is therefore a deployment trigger. Keep it unmerged until the coordinated cutover is authorized, or explicitly pause that workflow first.** Install and verify these recurring tasks on the actual host at cutover, using its Python environment and environment variables: From eb4b78d0464dcb0b04c8ed74caec3c96cb1ffa8e Mon Sep 17 00:00:00 2001 From: Nick Bottone Date: Fri, 30 Oct 2026 11:41:24 -0400 Subject: [PATCH 27/28] fix: charge minimum usage for subsecond ready sessions --- subscriptions/services.py | 4 ++++ subscriptions/tests.py | 42 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/subscriptions/services.py b/subscriptions/services.py index c9022a2..40ca12c 100644 --- a/subscriptions/services.py +++ b/subscriptions/services.py @@ -500,6 +500,10 @@ def sync_pending_polar_usage(limit=100): def _record_orchestrator_usage(session, stopped_at, runtime_seconds=None): if runtime_seconds is not None: billed_minutes = _rounded_runtime_minutes_from_seconds(runtime_seconds, session.allocated_minutes) + # Orchestrator runtime is floored to whole seconds. A ready session + # with positive sub-second runtime still incurs the minimum charge. + if billed_minutes == 0 and session.ready_at and stopped_at > session.ready_at: + billed_minutes = session.rounded_runtime_minutes(stopped_at=stopped_at) elif session.ready_at: billed_minutes = session.rounded_runtime_minutes(stopped_at=stopped_at) else: diff --git a/subscriptions/tests.py b/subscriptions/tests.py index 71ebcd8..9d033d8 100644 --- a/subscriptions/tests.py +++ b/subscriptions/tests.py @@ -300,6 +300,48 @@ def test_orchestrator_ready_and_stopped_events_record_usage_once(self): self.assertEqual(UsageLedgerEntry.objects.get(server_session=session).credits, 10) self.assertEqual(WebhookEvent.objects.filter(provider='orchestrator').count(), 2) + def test_subsecond_ready_session_records_minimum_usage_once(self): + user = create_user() + create_entitlement(user) + session = start_server_session(user, requested_minutes=60, status=ServerSession.STATUS_LAUNCHING) + ready_at = timezone.now() - timedelta(milliseconds=500) + stopped_at = ready_at + timedelta(milliseconds=400) + process_orchestrator_event(str(session.id), { + 'event_id': 'subsecond-ready', + 'event': 'ready', + 'occurred_at': ready_at.isoformat(), + 'ready_at': ready_at.isoformat(), + }) + stopped = { + 'event_id': 'subsecond-stopped', + 'event': 'stopped', + 'occurred_at': stopped_at.isoformat(), + 'stopped_at': stopped_at.isoformat(), + 'runtime_seconds': 0, + } + + process_orchestrator_event(str(session.id), stopped) + process_orchestrator_event(str(session.id), stopped) + + self.assertEqual(UsageLedgerEntry.objects.filter(server_session=session).count(), 1) + self.assertEqual(UsageLedgerEntry.objects.get(server_session=session).credits, 5) + + def test_stop_before_readiness_does_not_record_usage(self): + user = create_user() + create_entitlement(user) + session = start_server_session(user, requested_minutes=60, status=ServerSession.STATUS_LAUNCHING) + + process_orchestrator_event(str(session.id), { + 'event_id': 'unready-stopped', + 'event': 'stopped', + 'occurred_at': timezone.now().isoformat(), + 'runtime_seconds': 0, + }) + + session.refresh_from_db() + self.assertEqual(session.status, ServerSession.STATUS_STOPPED) + self.assertEqual(UsageLedgerEntry.objects.count(), 0) + def test_orchestrator_launch_failed_does_not_record_usage(self): user = create_user() create_entitlement(user) From 773b10ed3354cdb63108411fa3833697919f5652 Mon Sep 17 00:00:00 2001 From: Nick Bottone Date: Fri, 30 Oct 2026 11:45:11 -0400 Subject: [PATCH 28/28] docs: record failed renewal recovery and live billing regression --- docs/premium-acceptance-2026-10-29.md | 24 ++++++++++-------------- docs/premium-live-test-sequence.md | 13 ++++++------- docs/premium-release-checklist.md | 2 +- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/docs/premium-acceptance-2026-10-29.md b/docs/premium-acceptance-2026-10-29.md index faa7795..266cf46 100644 --- a/docs/premium-acceptance-2026-10-29.md +++ b/docs/premium-acceptance-2026-10-29.md @@ -8,7 +8,7 @@ No production deployment, merge, production billing change, or main-guild role c | Candidate | Code revision | Verification | | --- | --- | --- | -| Website | `020a90c` | 45 tests pass locally and on the Linux droplet, including real Django/Fastify HTTP contracts and three concurrent SQLite regressions; final code CI passes | +| Website | `eb4b78d` | 47 tests pass locally and on the Linux droplet, including real Django/Fastify HTTP contracts, three concurrent SQLite regressions, and the sub-second billing regression; updated candidate CI must also pass | | Ranked bot | `9927f3e` | 31 tests plus 2 log-parser tests pass on Linux; CI passes | | Orchestrator | `971a3b1` | 28 tests pass locally and in CI; build and website-contract CI pass; preceding 27-test suite also passed on the droplet | @@ -39,6 +39,7 @@ On October 30, a fresh production SQLite snapshot was copied into private local | Upgrade | After changing only the private acceptance products to fixed pricing, a real upgrade selected VIP from the actual product ID despite stale supporter metadata. The website showed 1,500 credits and a 240-minute limit; Polar also reported 1,500. | | Scheduled downgrade | Polar scheduled a Supporter downgrade for the next period while current website access remained VIP. The test schedule was subsequently cleared. | | Standalone renewal / no rollover | Fixed-price VIP renewal produced paid `subscription_cycle` order `60cd3321-9a77-4818-890b-1245fc3bcfa2`. After the provider's fifteen-minute order delay, both Polar and the website reported exactly 1,500 credits, consumed units zero, and the new period October 30–November 30. The previous 1,500 credits did not roll over; no webhook failures were present. | +| Failed renewal / recovery | Real decline-after-attachment test card caused order `408a841b-5c25-42b8-af20-a1e96819cf30` to remain pending and subscription to become past due. Signed events denied website access and a real launch (403, no session/usage); Polar removed VIP while retaining guild membership. Restoring the successful card automatically paid the same order, restored active access, exactly 1,500 credits, and the VIP role. A real post-fix 0.637-second ready session then charged five credits once, with matching provider/website balance 1,495 and no duplicate-stop charge. | | Revocation | Immediate revocation of the newly created test subscription removed access. A launch was rejected with HTTP 403 and created no session. A fresh VIP checkout was initially delayed, then completed. Real Polar VIP access and its 1,500-credit balance are restored. | | Billing portal entry | `https://sandbox.polar.sh/secondrobotics/portal` was verified as this organization's customer portal sign-in page and configured as the test website's Manage billing destination. Customer portal sign-in and both Discord benefit connections were completed; actual portal UI billing changes remain separate from the API lifecycle checks. | @@ -49,26 +50,21 @@ On October 30, a fresh production SQLite snapshot was copied into private local 3. **SQLite callback/response races:** stop-response recording, launch-response recording, and callback ownership lookup could upgrade a retained read lock while another writer was active. Stop uses a guarded SQL update; launch locks the user before reading; callback ownership is resolved before its write transaction. Three concurrent file-backed SQLite regressions pass, along with the complete Linux suite. 4. **Private discovery:** `SERVER_PUBLIC_DISCOVERY=false` forces `Register=Off` even when callers request public registration. This is enabled on the test orchestrator and documented for the initial beta. 5. **Return credential handling:** Polar appends a customer-session credential to the checkout success URL. The website discards it from the URL and redacts it from Django access logs. Reverse-proxy access logs must also avoid recording that parameter at eventual cutover. +6. **Sub-second ready-session billing:** a real server ran ready for 0.983 seconds, but the orchestrator's whole-second runtime was zero, so the website omitted its usage entry. Positive ready/stopped timestamps now apply normal five-minute rounding even when whole-second runtime is zero. Sessions stopped before readiness remain uncharged. Both regressions pass, and a real patched 0.637-second session produced one five-credit entry; provider and website reconciled to 1,495. Duplicate stop created no second charge. -## Findings that still block launch clearance +## Billing evidence and remaining launch prerequisites ### Billing lifecycle and product configuration The original sandbox Supporter/VIP products use **custom/name-your-price** prices. Polar rejects changing a subscription **to** a custom-price product. Fixed-price copies support the tested upgrade and scheduled downgrade. Nick approved **fixed $5/$12 launch pricing**. Configure production products accordingly; do not retain custom-price targets if normal plan switching is promised. Original products were not modified; only the private acceptance products use fixed prices. -The first deliberately shortened billing period advanced, and the meter's consumed units reset. Its old 360-credit benefit did **not** replenish before the later separate VIP upgrade granted 1,500. That upgrade was not proof of monthly replenishment. The separate follow-up below now proves standalone renewal and no rollover; a real zero-balance launch was also rejected. The organization's `reset` proration mode is disabled; tests used the supported `invoice` mode instead of changing organization settings. +Standalone renewal and no rollover passed: paid recurring order `60cd3321-9a77-4818-890b-1245fc3bcfa2` replenished exactly 1,500 credits after the prior 1,500 expired. Both provider and website agreed. Polar defers recurring order creation for fifteen minutes after the period cutoff, so a temporary zero-credit interval before the paid grant is expected and was observed ([order task](https://github.com/polarsource/polar/blob/main/server/polar/order/tasks.py), [paid-order handling](https://github.com/polarsource/polar/blob/main/server/polar/order/service.py)). The first shortened-cycle test was inconclusive because upgrade invoice `bab5689d-5225-4aff-bc82-2f7342356614` collected both the pending $5 Supporter cycle and the $12 VIP upgrade; it was not used as renewal evidence. The organization's `reset` proration mode is disabled; tests used supported `invoice` behavior without changing organization settings. -A real past-due/failed-renewal payment lifecycle has not been exercised. Revocation, period-end cancellation, and mocked failure behavior are covered, but they do not replace this check. +A genuine failed renewal also passed. After selecting Stripe's public sandbox decline-after-attachment card ending `0341`, the period ended at `2026-10-30T15:09:47.809701Z`. Recurring order `408a841b-5c25-42b8-af20-a1e96819cf30` was created at `15:24:48Z`, remained pending, and scheduled its next payment attempt for November 1. Polar marked the subscription `past_due`. Its signed event disabled website access; launch returned HTTP 403 without creating a server session or usage entry. Polar removed the VIP role while retaining guild membership; Supporter remained absent. No application webhook records failed. See [Stripe's test-card behavior](https://docs.stripe.com/testing#declined-payments). -Follow-up renewal diagnosis: the sandbox organization reports `subscription_renewals=true` and zero-day benefit-revocation grace. A second test shortened the **fixed-price** restored VIP subscription to `2026-10-30T01:58:55.051431Z`. Its period advanced to November 30, but initially there was no cycle order, and both provider balance and website spendable credits became zero. Polar's current public implementation deliberately defers recurring order creation for fifteen minutes after the cutoff, then cycles credit grants after the renewal order is paid ([order task](https://github.com/polarsource/polar/blob/main/server/polar/order/tasks.py), [paid-order handling](https://github.com/polarsource/polar/blob/main/server/polar/order/service.py)). At `02:14:26Z`, the deployed sandbox was observed to have produced paid recurring order `60cd3321-9a77-4818-890b-1245fc3bcfa2`; both provider and website had exactly 1,500 credits, and no webhooks had failed. Standalone replenishment and no rollover are now proven. The temporary zero-credit renewal interval is real behavior to account for in launch messaging. +Restoring successful `4242` method `07587eda-50df-4821-ac3d-bf4d313f3b19` as the default automatically retried **the same order**, which became paid with no next attempt scheduled. The provider and website restored active VIP access and exactly 1,500 credits; the actual Discord VIP role returned. The failure method remains attached but is not the default. No replacement subscription or manual role grant was used. A real post-recovery launch exposed the sub-second rounding defect described above. After the fix, a 0.637-second ready session charged five credits exactly once, including after a duplicate stop; both provider and website reconciled to **1,495 credits**, with no failed webhook records. -The earlier upgrade order `bab5689d-5225-4aff-bc82-2f7342356614` has a $17 pre-tax subtotal with two lines: a $5 non-prorated Supporter cycle starting `14:15:57Z`, and a $12 VIP upgrade starting `14:24:09Z`. Thus the pending Supporter cycle was collected in the upgrade invoice; its absence as a separate cycle order does not demonstrate an uncharged renewal. The upgrade/grant still cannot prove standalone recurring credit replenishment. - -The customer portal's Add Payment Method flow initially stalled on a dimmed loading overlay. One reload and retry opened the form successfully. Stripe's documented decline-after-attachment card ending `0341` was attached in sandbox. Polar automatically made the new card the default; a subsequent API check caught this, and the successful `4242` method `07587eda-50df-4821-ac3d-bf4d313f3b19` was restored and verified as default before the pending renewal charge. The failure method `aca3b621-e73d-4619-94a0-e5f3f953a649` remains attached for the subsequent failed-renewal test; attachment alone does not prove a payment failure. See [Stripe's test-card behavior](https://docs.stripe.com/testing#declined-payments). - -After successful renewal, selecting the saved failure card showed “Failed to update default payment method / Failed to update customer.” A fresh attachment attempt stalled; refreshing returned the portal to sign-in. The session is now awaiting the owner's emailed verification code in Edge. The failure cause is not established. Final API verification still shows the successful `4242` card as default, subscription active through November 30, and no second shortened period scheduled. Resume failed-renewal setup after reauthentication; do not claim that gate passed. - -October 30 follow-up: the owner completed fresh portal authentication, and selecting the existing failure card succeeded. The provider API verified `0341` as default. The test period was shortened to `2026-10-30T15:09:47.809701Z`; observe the failed charge after the fifteen-minute billing delay. The localhost SSH tunnel had stopped overnight while the official webhook listener remained alive, causing connection-refused forwarding failures. The tunnel was restored, its HTTP endpoint returned 200, and a metadata-only update preserving existing metadata requested a fresh signed full-state event. This local forwarding interruption must not be confused with application webhook rejection. The earlier portal/card-setting blockage is resolved; failed payment and recovery remain under observation. +Expired portal authentication initially caused card-setting failures; fresh owner login resolved them. The localhost SSH tunnel also stopped overnight while the official webhook listener remained alive, causing forwarding connection refusals. The tunnel was restored, HTTP readiness returned 200, and a metadata-only update preserving existing metadata delivered fresh signed subscription state. Neither interruption remains an unresolved application webhook failure. ### Discord benefit and real players @@ -111,10 +107,10 @@ The copied catalog's stale saved-presets promise was cleaned using the existing ## Current state and next gates -- The real Polar VIP subscription `8889315a-38ce-40ee-aa7b-b7c2cd09cde2` passed standalone renewal: both provider and website report **1,500 credits** for October 30–November 30. The temporary complimentary fallback remains deactivated. +- The real Polar VIP subscription `8889315a-38ce-40ee-aa7b-b7c2cd09cde2` passed renewal, actual failed renewal, and payment recovery. The successful `4242` card is restored as default; website and provider report active VIP and **1,495 credits** after one verified five-credit post-fix session. The temporary complimentary fallback remains deactivated. - Isolated Discord role acceptance is complete: VIP grant/removal/regrant and Supporter grant/removal were verified directly in Discord. VIP and guild membership were retained during final Supporter removal. Both private products have their intended final benefits, and the temporary extra Supporter benefit was removed from VIP. [Exact configuration and evidence](premium-discord-test-setup.md). - No production/main-guild resources were changed, and all coordinated PRs remain unmerged. - Follow the [ordered remaining live-test sequence](premium-live-test-sequence.md), including exact test queue/channel and evidence requirements. Temporary UFW rules now allow TCP/UDP ports 11115–11117 only from the current SSH client address; a private cleanup record is stored at `acceptance-firewall.json` under the test root. A second player on another network requires a separate source rule. -- Remaining billing gate: a failed-renewal/past-due lifecycle and payment recovery. Standalone recurring credit replenishment, no rollover, and rejecting actual zero-balance launches are now proven. +- Sandbox billing gates are passed: checkout, usage/debit, outages/retry, upgrade/downgrade, cancellation/revocation, standalone renewal/no rollover, zero-balance rejection, actual failed renewal, role revocation, and payment/access/role recovery. A separate production billing configuration and controlled production lifecycle remain cutover prerequisites. - Next owner participation: confirm the second player's account and readiness for a real test game/full ranked match. Both players share the current network, and its address matches the prepared source-restricted game rules. The fresh production backup has been supplied through SSH and rehearsed successfully. - Production fixed $5/$12 product configuration, credentials, capacity validation, recurring workers, coordinated review, and separate cutover authorization remain required before launch. diff --git a/docs/premium-live-test-sequence.md b/docs/premium-live-test-sequence.md index 45bd465..e9c36fa 100644 --- a/docs/premium-live-test-sequence.md +++ b/docs/premium-live-test-sequence.md @@ -8,7 +8,7 @@ or merge is authorized. Completed evidence remains in | Order | Operator action | Required evidence | | --- | --- | --- | | 1 — passed | Agent scheduled a short fixed-price sandbox billing period and observed provider orders, credits, signed webhooks, and website entitlement. | Paid `subscription_cycle` order `60cd3321-9a77-4818-890b-1245fc3bcfa2`; exactly 1,500 credits after the fifteen-minute billing delay, without rollover; website and provider agree. | -| 2 | Agent configures a supported sandbox payment failure and triggers another renewal. Owner handles private payment/account authentication if necessary. | Provider marks past due; website denies new paid launches; benefits follow the configured zero-day grace; restoring payment recovers access without duplicate credit or usage. | +| 2 — passed | A real sandbox decline card caused a failed renewal; restoring the successful default card automatically retried the same order. | Past due denied launches without creating sessions/usage, removed VIP without removing guild membership, then recovered active access, exactly 1,500 credits, and the VIP role. A real post-fix 0.637-second session charged five credits once; provider and website ended at 1,495. | | 3 | Owner confirms player networks and Discord accounts. Agent opens only the required test game access, checks profiles in the disposable database, and starts a casual server. | Both real clients reach the advertised address. Wrong password fails; correct password succeeds. Public discovery remains off. | | 4 | Players join opposing alliances, send an identifiable test chat message, and play a scored game. Agent inspects API/events/logs and stops the server. | Player presence, chat, score and OPR agree with the clients; scopes protect actual player IP data; stop closes the process and debits once. | | 5 | Both players use `/queuestandard`, selecting **Rebuilt 1v1**, in [the test queue channel](https://discord.com/channels/663201408581107712/1565227916432900126). | Bot creates private match resources, supplies a working server/password, and assigns the correct players. No production account creation or leaderboard writes. | @@ -19,18 +19,17 @@ or merge is authorized. Completed evidence remains in ## Inputs still needed -- Whether players share a public network; if not, the second player's network must - be included in the temporary game firewall rules. Do not share SSH keys with players. +- Both players are confirmed on the current network, which matches the prepared + game firewall rules. Confirm when they are ready with xRC v21.2 and Discord. - The second player's Discord ID if they are not the existing `easierthanpai` test-guild member. Any profile seeded for ranked acceptance is a test fixture; it does not count as an additional OAuth login test. - Production SSH access and the fresh-backup rehearsal are complete. The live application's Python 3.9 → 3.12 switch remains a cutover prerequisite; it has not been performed. -- Polar reauthentication is complete. The failure card is now selected, and the - shortened failed renewal is scheduled for October 30, 15:09:47 UTC. Observe the - actual payment attempt after the fifteen-minute billing delay before judging - past-due behavior. Keep credentials and verification codes out of chat. +- Polar billing acceptance is complete. The successful `4242` card is restored + as default, the subscription is active, and both balances are 1,495 after the + verified five-credit usage debit. Keep credentials and codes out of chat. Temporary UFW rules now permit game TCP/UDP ports 11115–11117 from the current SSH client address only, in addition to the existing SSH and Nginx rules. The diff --git a/docs/premium-release-checklist.md b/docs/premium-release-checklist.md index fc42a4a..8b1c964 100644 --- a/docs/premium-release-checklist.md +++ b/docs/premium-release-checklist.md @@ -19,7 +19,7 @@ This is a coordinated website, ranked-bot, and orchestrator release. Nothing in ### 1. Finish the existing Polar sandbox configuration -The 2026-10-29 acceptance run proves checkout, initial credits, real usage ingestion/debit, outage holds, upgrades with fixed prices, cancellation, revocation, restored access after a fresh checkout, both Discord role lifecycles, and standalone recurring replenishment without rollover. Actual zero-provider-balance launches are denied without creating sessions. Fixed $5/$12 pricing is approved; configure the production products that way (Polar rejects upgrades to custom-price targets). Failed-renewal behavior and recovery still require verification. See the linked evidence report before repeating completed setup. +The October 29–30 acceptance run proves checkout, initial credits, real usage ingestion/debit, outage holds, upgrades with fixed prices, cancellation, revocation, restored access after a fresh checkout, both Discord role lifecycles, standalone renewal without rollover, and actual failed-renewal/payment recovery. Actual zero-provider-balance and past-due launches are denied without creating sessions. The recovered subscription and VIP role returned, and a real sub-second ready session verified the fixed five-credit minimum charge. Fixed $5/$12 pricing is approved; configure production products that way (Polar rejects upgrades to custom-price targets). See the linked evidence report before repeating completed sandbox setup. Local configuration already specifies `POLAR_SERVER=sandbox`, an access token, and a meter ID. Do not paste secrets into chat. Add missing values to the appropriate local/private configuration or secret store: