fix: close race conditions in script builder session and slug creation

- script_builder endpoint: pg_advisory_xact_lock on user_id before
  session count check, preventing concurrent creates from both passing
  the MAX_SESSIONS_PER_USER guard
- script_builder_service send_message: pg_advisory_xact_lock on session_id
  before message count check, preventing concurrent sends from both
  passing the MAX_MESSAGES_PER_SESSION guard
- script_builder_service save_to_library: replace check-then-insert slug
  logic with IntegrityError retry loop (3 attempts with fresh UUID suffix);
  add unique constraint on script_templates.slug (migration 070)
- ScriptBuilderPage: add creatingSessionRef to serialize concurrent
  handleSend calls that would otherwise both call createSession() while
  session is still null

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
chihlasm
2026-04-01 05:09:42 +00:00
parent 41b0f0a627
commit cb33787c08
5 changed files with 105 additions and 34 deletions

View File

@@ -0,0 +1,29 @@
"""add unique constraint to script_templates.slug
Revision ID: 070
Revises: 069
Create Date: 2026-04-01
"""
from alembic import op
revision = "070"
down_revision = "069"
branch_labels = None
depends_on = None
def upgrade() -> None:
op.create_unique_constraint(
"uq_script_templates_slug",
"script_templates",
["slug"],
)
def downgrade() -> None:
op.drop_constraint(
"uq_script_templates_slug",
"script_templates",
type_="unique",
)