Skip to content

feat: production Stripe webhook async architecture with Redis, Jules worker, and optional AI providers#355

Open
Copilot wants to merge 5 commits into
mainfrom
copilot/refactor-stripe-webhook-for-asynchronous-processin
Open

feat: production Stripe webhook async architecture with Redis, Jules worker, and optional AI providers#355
Copilot wants to merge 5 commits into
mainfrom
copilot/refactor-stripe-webhook-for-asynchronous-processin

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jun 2, 2026

Replaces the monolithic Stripe webhook handler with a layered async architecture: the HTTP layer validates, deduplicates, and enqueues in <10ms; all business logic runs in a separate Jules worker process consuming from Redis.

HTTP layer (api/index.py → thin adapter)

  • Removed 220-line fat handler; replaced with ~50-line adapter importing from services/
  • webhook(): verify sig → Redis idempotency check → mark_event_receivedenqueue_event → 200
  • Static slug map for WebhookVerificationError responses (fixes CodeQL py/stack-trace-exposure)
  • Dropped in-memory processed_events: set / queued_events: list (not safe across Vercel invocations)

Services layer (api/services/)

  • redis_client — singleton with _MemoryRedis fallback for dev (no Redis required locally)
  • redis_lock — distributed lock via SET NX EX with token-checked release (prevents releasing a different worker's lock on TTL expiry)
  • stripe_serviceverify_webhook() decoupled from Flask; raises typed WebhookVerificationError
  • event_store — Redis hash state machine: RECEIVED → PROCESSING → PROCESSED / FAILED; includes retry counter and TOCTOU guard (re-check after lock acquisition)
  • queue_serviceLPUSH/BRPOP main queue + sorted-set delayed requeue + DLQ

Jules worker (api/orchestrators/)

  • decision_engine — pure Python, no I/O; decide_event_flow(event, context) → Decision
  • execution_engine — dispatches to handler; calls AI enrichment via _run_ai_optional() (swallows all errors — AI failure never blocks business logic)
  • jules_worker — blocking loop: flush delayed → dequeue → lock → decide → execute → mark done or exponential-backoff retry (2s/4s/8s) → DLQ at MAX_RETRIES
# Run worker externally (not on Vercel)
PYTHONPATH=api python -m orchestrators.jules_worker

AI providers (api/ai_providers/)

  • AIProvider ABC + GoogleAIStudioProvider (optional google-generativeai SDK) + ManusAIProvider (plain HTTP)
  • Per-provider CircuitBreaker (3-failure threshold, 60s recovery window)
  • run_ai_task(task_type, payload, provider=None) — provider selected by AI_PROVIDER env var; never called from the HTTP layer

Models & config

  • Decision dataclass: action, reason, priority, requires_ai, provider, retryable, metadata
  • EventAction enum covers: mark_order_paid, mark_payment_failed, activate_subscription, handle_refund, request_manual_review, invoke_ai_enrichment, ignore_event
  • api/config.py — single source of truth for all env vars with validate() at startup

Infrastructure

  • api/requirements.txt — added redis>=5.0.0
  • .env.example — documents all vars: Stripe, Redis, AI providers, worker tuning

What's mocked / needs real implementation

  • handlers/stripe_handlers.py — TODO stubs for DB writes, emails, fulfillment
  • ManusAIProvider._build_request_body / _parse_response — update once Manus API schema is confirmed
  • _MemoryRedis — single-process dev only; set REDIS_URL for any multi-process deployment

Copilot AI changed the title feat: production Stripe webhook async architecture with Redis, Jules worker and AI providers feat: production Stripe webhook async architecture with Redis, Jules worker, and optional AI providers Jun 2, 2026
@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 2, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
tryonyou-app-1776375445 Error Error Jun 2, 2026 2:33pm
tryonyou-app-work Error Error Jun 2, 2026 2:33pm
workspace Error Error Jun 2, 2026 2:33pm

Copy link
Copy Markdown
Member

@LVT-ENG LVT-ENG left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

He'd

@LVT-ENG LVT-ENG marked this pull request as ready for review June 3, 2026 10:52
Copy link
Copy Markdown
Member

@LVT-ENG LVT-ENG left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants