Bundle integration steer

From Vrooli/Vrooli · View interactive version

Bundle integration steer is an AI agent skill from the GitHub repository Vrooli/Vrooli. Integrate `scenarios/{{TARGET}}/` into the **Vrooli Business Suite bundle** so that it participates in subscription gating, credit-based metering, and authenticated downloads managed by the Landing Pa

How to use this skill

Skill file

Steer focus: Bundle Integration

Integrate scenarios/{{TARGET}}/ into the Vrooli Business Suite bundle so that it participates in subscription gating, credit-based metering, and authenticated downloads managed by the Landing Page Business Suite (LPBS) scenario.

Your goal is to ensure {{TARGET}} enforces entitlements and consumes credits consistently with every other bundled app, so that a single subscription unlocks and meters all apps in the bundle.

Optional reading:


0. Why This Skill Exists

The Business Suite bundle is a collection of apps sold under a single subscription. Each bundled app must:

  1. Authenticate users via LPBS-issued JWT tokens (one identity system, not per-app accounts).
  2. Enforce entitlements — only active subscribers can access gated features or downloads.
  3. Consume credits — metered features (AI calls, exports, compute) deduct from a shared credit wallet.
  4. Register in LPBS — so the landing page, download system, and entitlements API know the app exists.

Without a shared integration pattern, each app invents its own auth, its own gating, its own credit logic. The result: inconsistent enforcement, double-charging bugs, bypassed gates, and a fractured user experience. This skill exists to prevent that divergence.

This skill is NOT about:


1. Scope Boundaries

In scope

Out of scope


2. Architecture Overview: How Bundle Integration Works

┌──────────────────────────────────────────────────────────────────┐
│                    LPBS (Landing Page Business Suite)            │
│                                                                  │
│  ┌────────────┐  ┌──────────────┐  ┌───────────────────────┐   │
│  │ Auth       │  │ Subscriptions│  │ Credit Wallets        │   │
│  │ (JWT)      │  │ & Entitle-   │  │ (balance, consume,    │   │
│  │            │  │ ments        │  │  top-up)              │   │
│  └─────┬──────┘  └──────┬───────┘  └───────────┬───────────┘   │
│        │                │                       │               │
│  Issues tokens    Checks status           Tracks balance        │
│        │                │                       │               │
└────────┼────────────────┼───────────────────────┼───────────────┘
         │                │                       │
         ▼                ▼                       ▼
┌──────────────────────────────────────────────────────────────────┐
│                    {{TARGET}} (Your App)                         │
│                                                                  │
│  ┌────────────────┐  ┌─────────────────┐  ┌──────────────────┐ │
│  │ Auth Middleware │  │ Entitlement     │  │ Credit Gate      │ │
│  │ (validate JWT) │  │ Check           │  │ (consume before  │ │
│  │                │  │ (subscription   │  │  expensive ops)  │ │
│  │                │  │  active?)       │  │                  │ │
│  └────────────────┘  └─────────────────┘  └──────────────────┘ │
│                                                                  │
│  All three call LPBS APIs — {{TARGET}} never stores user        │
│  accounts, subscriptions, or credit balances itself.            │
└──────────────────────────────────────────────────────────────────┘

Key principle: {{TARGET}} is a consumer of LPBS services, never a replica. User accounts, subscription state, and credit balances live in LPBS. {{TARGET}} validates tokens and makes API calls — it does not maintain its own copy of this data.


3. JWT Authentication Contract

LPBS issues JWT tokens via magic-link email auth. {{TARGET}} must validate these tokens to identify users.

3.1 Token Structure

LPBS access tokens contain these claims:

{
  "uid": "user-uuid",
  "email": "user@example.com",
  "sid": "session-uuid",
  "iss": "landing-page-business-suite",
  "sub": "user-uuid",
  "exp": 1711200000,
  "iat": 1711198000,
  "nbf": 1711198000
}
3.2 Validation Rules

| Rule | Detail |

|------|--------|

| Signing algorithm | HS256 (HMAC-SHA256) |

| Shared secret | JWT_SECRET environment variable (32-byte hex string, shared between LPBS and {{TARGET}}) |

| Issuer | Must match JWT_ISSUER env var (default: "landing-page-business-suite") |

| Expiration | Access tokens expire after 15 minutes; always check exp claim |

| Token location | Check Authorization: Bearer <token> header first, fall back to access_token cookie |

3.3 Middleware Pattern (Go)
func requireLPBSAuth(jwtSecret []byte, jwtIssuer string) gin.HandlerFunc {
    return func(c *gin.Context) {
        tokenStr := extractBearerToken(c.GetHeader("Authorization"))
        if tokenStr == "" {
            tokenStr, _ = c.Cookie("access_token")
        }
        if tokenStr == "" {
            c.AbortWithStatusJSON(401, gin.H{
                "error":      "authentication required",
                "error_type": "unauthorized",
                "retryable":  false,
            })
            return
        }

        claims := &LPBSClaims{}
        token, err := jwt.ParseWithClaims(tokenStr, claims,
            func(t *jwt.Token) (interface{}, error) {
                if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
                    return nil, fmt.Errorf("unexpected signing method: %v", t.Header["alg"])
                }
                return jwtSecret, nil
            },
            jwt.WithIssuer(jwtIssuer),
            jwt.WithValidMethods([]string{"HS256"}),
        )
        if err != nil || !token.Valid {
            c.AbortWithStatusJSON(401, gin.H{
                "error":      "invalid or expired token",
                "error_type": "unauthorized",
                "retryable":  false,
            })
            return
        }

        c.Set("user_email", claims.Email)
        c.Set("user_id", claims.UID)
        c.Next()
    }
}

type LPBSClaims struct {
    UID   string `json:"uid"`
    Email string `json:"email"`
    SID   string `json:"sid"`
    jwt.RegisteredClaims
}

func extractBearerToken(header string) string {
    if len(header) > 7 && header[:7] == "Bearer " {
        return header[7:]
    }
    return ""
}
3.4 Auth Decision Tree
Request arrives at {{TARGET}}
│
▼
Is this endpoint public (health, landing, docs)?
├─ YES → skip auth, serve directly
└─ NO
   │
   ▼
   Extract token from Authorization header
   │
   ▼
   Token found?
   ├─ NO → check access_token cookie
   │       │
   │       ▼
   │       Cookie found?
   │       ├─ NO → 401 Unauthorized
   │       └─ YES → validate cookie token
   └─ YES → validate header token
            │
            ▼
            Token valid (signature, expiry, issuer)?
            ├─ NO → 401 Unauthorized
            └─ YES → set user context, continue to handler
3.5 What {{TARGET}} Must NOT Do

4. Entitlement Checking

Before serving gated features or downloads, {{TARGET}} must verify the user has an active subscription.

4.1 When to Check Entitlements
User requests a feature in {{TARGET}}
│
▼
Is this feature gated (requires subscription)?
├─ NO → serve directly
└─ YES
   │
   ▼
   Call LPBS: GET /api/v1/entitlements
   (Authorization: Bearer <user's token>)
   │
   ▼
   Response status field?
   ├─ "active" or "trialing" → allow access
   ├─ "past_due"             → allow access with warning (grace period)
   ├─ "canceled"             → deny, show "subscription ended" message
   └─ "inactive" / other     → deny, show upgrade prompt
4.2 Entitlement API Call
GET <LPBS_BASE_URL>/api/v1/entitlements
Authorization: Bearer <user_access_token>

Response (200 OK):

{
  "status": "active",
  "plan_tier": "pro",
  "price_id": "price_...",
  "features": ["feature1", "feature2"],
  "billing_cycle_start": 15,
  "credits": {
    "balance_credits": 5000000,
    "display_credits_label": "credits",
    "display_credits_multiplier": 0.001
  },
  "subscription": {
    "subscription_id": "sub_...",
    "bundle_key": "business_suite"
  }
}

Response (401): Token invalid or missing.

Response (403): No active subscription.

4.3 Entitlement Check Pattern (Go)
type EntitlementResponse struct {
    Status            string          `json:"status"`
    PlanTier          string          `json:"plan_tier"`
    Features          []string        `json:"features"`
    BillingCycleStart int             `json:"billing_cycle_start"`
    Credits           *CreditBalance  `json:"credits,omitempty"`
}

type CreditBalance struct {
    BalanceCredits          int64   `json:"balance_credits"`
    DisplayCreditsLabel     string  `json:"display_credits_label"`
    DisplayCreditsMultiplier float64 `json:"display_credits_multiplier"`
}

func (c *LPBSClient) CheckEntitlements(ctx context.Context, userToken string) (*EntitlementResponse, error) {
    req, _ := http.NewRequestWithContext(ctx, "GET", c.baseURL+"/api/v1/entitlements", nil)
    req.Header.Set("Authorization", "Bearer "+userToken)

    resp, err := c.httpClient.Do(req)
    if err != nil {
        return nil, fmt.Errorf("lpbs unreachable: %w", err)
    }
    defer resp.Body.Close()

    switch resp.StatusCode {
    case 200:
        var result EntitlementResponse
        if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
            return nil, fmt.Errorf("decode entitlements: %w", err)
        }
        return &result, nil
    case 401:
        return nil, ErrUnauthorized
    case 403:
        return nil, ErrNoActiveSubscription
    default:
        return nil, fmt.Errorf("lpbs returned %d", resp.StatusCode)
    }
}
4.4 Caching Entitlements

LPBS caches subscription status for 60 seconds internally. {{TARGET}} may apply a short local cache (30-60 seconds) to avoid hammering LPBS on every request, but:


5. Credit Consumption

Credits are the metering unit for expensive operations (AI calls, video exports, compute time, etc.). {{TARGET}} consumes credits by calling LPBS — it never manages balances itself.

5.1 What Gets Credit-Gated
Does this feature cost real money to run (API calls, GPU, storage)?
│
├─ YES → credit-gated (consume credits per use)
│
└─ NO
   │
   ▼
   Is this a core differentiator of the paid product?
   │
   ├─ YES → subscription-gated (check entitlements, no credit cost)
   │
   └─ NO → free / ungated
5.2 Credit Consumption Decision Table

| Question | If YES | If NO |

|----------|--------|-------|

| Is the cost per-invocation (single API call, one export)? | Simple consumption — deduct fixed amount | Continue... |

| Is the cost proportional to input size (tokens, pixels, duration)? | Calculate amount from input, then consume | Continue... |

| Is it a long-running job (minutes of compute)? | Reserve credits upfront, refund unused portion on completion | Continue... |

| Can the user retry if it fails mid-operation? | Non-idempotent ConsumeCredits is acceptable | Use idempotent consumption with operation ID |

5.3 Credit Consumption API Call

Simple (non-idempotent) consumption:

POST <LPBS_BASE_URL>/api/v1/credits/consume
Authorization: Bearer <user_access_token>
Content-Type: application/json

{
  "email": "user@example.com",
  "amount": 500000,
  "reason": "ai_chat_completion",
  "metadata": {
    "scenario": "{{TARGET}}",
    "model": "claude-sonnet-4-20250514",
    "input_tokens": 1200,
    "output_tokens": 800
  }
}

Idempotent consumption (for non-retryable operations):

POST <LPBS_BASE_URL>/api/v1/credits/consume
Authorization: Bearer <user_access_token>
Content-Type: application/json

{
  "email": "user@example.com",
  "amount": 500000,
  "reason": "video_export",
  "idempotency_key": "export-<user_id>-<export_job_id>",
  "metadata": {
    "scenario": "{{TARGET}}",
    "export_format": "mp4",
    "duration_seconds": 120
  }
}

Response (200 OK):

{
  "remaining_credits": 4500000,
  "consumed": 500000
}

Response (402):

{
  "error": "insufficient credits",
  "error_type": "forbidden",
  "retryable": false,
  "balance_credits": 200000,
  "required_credits": 500000
}
5.4 Consumption Pattern (Go)
type ConsumeRequest struct {
    Email          string            `json:"email"`
    Amount         int64             `json:"amount"`
    Reason         string            `json:"reason"`
    IdempotencyKey string            `json:"idempotency_key,omitempty"`
    Metadata       map[string]string `json:"metadata,omitempty"`
}

type ConsumeResponse struct {
    RemainingCredits int64 `json:"remaining_credits"`
    Consumed         int64 `json:"consumed"`
}

func (c *LPBSClient) ConsumeCredits(ctx context.Context, userToken string, req ConsumeRequest) (*ConsumeResponse, error) {
    body, _ := json.Marshal(req)
    httpReq, _ := http.NewRequestWithContext(ctx, "POST",
        c.baseURL+"/api/v1/credits/consume", bytes.NewReader(body))
    httpReq.Header.Set("Authorization", "Bearer "+userToken)
    httpReq.Header.Set("Content-Type", "application/json")

    resp, err := c.httpClient.Do(httpReq)
    if err != nil {
        return nil, fmt.Errorf("lpbs unreachable: %w", err)
    }
    defer resp.Body.Close()

    switch resp.StatusCode {
    case 200:
        var result ConsumeResponse
        json.NewDecoder(resp.Body).Decode(&result)
        return &result, nil
    case 402:
        return nil, ErrInsufficientCredits
    case 401:
        return nil, ErrUnauthorized
    default:
        return nil, fmt.Errorf("lpbs returned %d", resp.StatusCode)
    }
}
5.5 Credit Consumption Safety Rules

6. LPBS-Side Registration

For {{TARGET}} to appear in the bundle's download system and entitlements, it must be registered in LPBS's database.

6.1 What Gets Registered

Two tables need entries:

download_apps — one row per app:

| Column | Value | Notes |

|--------|-------|-------|

| bundle_key | "business_suite" | Must match the bundle in plans.json |

| app_key | "{{TARGET}}" | Unique identifier for this app in the bundle |

| name | Human-readable name | e.g., "Browser Automation Studio" |

| tagline | Short description | Shown on download page |

| description | Full description | Markdown supported |

| icon_url | URL to app icon | Used in download listings |

| install_overview | Installation summary | Brief install instructions |

| install_steps | JSON array of steps | Step-by-step install guide |

| storefronts | JSON array | Which storefronts list this app |

| display_order | Integer | Sort order in download listings |

download_assets — one row per platform per app:

| Column | Value | Notes |

|--------|-------|-------|

| bundle_key | "business_suite" | |

| app_key | "{{TARGET}}" | Must match download_apps.app_key |

| platform | "windows", "mac", or "linux" | One row per platform |

| variant_key | "default" | Use "default" unless multiple variants exist |

| artifact_url | Download URL or path | Direct URL or managed artifact reference |

| artifact_source | "direct" or "managed" | "direct" = URL is the download; "managed" = S3 artifact |

| release_version | Semver string | e.g., "1.0.0" |

| release_notes | Changelog text | What's new in this version |

| checksum | SHA-256 hash | For download verification |

| requires_entitlement | true or false | Set to true for paid apps |

6.2 Registration Decision Tree
Is {{TARGET}} a paid/premium app?
│
├─ YES → requires_entitlement = true for all assets
│        (user must have active subscription to download)
│
└─ NO (free tier / demo / open source)
   │
   ▼
   Does it have premium features that consume credits?
   │
   ├─ YES → requires_entitlement = false (anyone can download)
   │        but credit-gate the premium features at runtime
   │
   └─ NO → requires_entitlement = false, no credit gates needed
          (consider whether it belongs in the bundle at all)
6.3 Registration SQL
-- Register the app
INSERT INTO download_apps (
    bundle_key, app_key, name, tagline, description,
    icon_url, install_overview, display_order
) VALUES (
    'business_suite',
    '{{TARGET}}',
    'Human-Readable App Name',
    'Short tagline for download listings',
    'Full description of what this app does.',
    '/icons/{{TARGET}}.png',
    'Download and run the installer for your platform.',
    10  -- adjust display_order as needed
);

-- Register platform assets (one per platform the app supports)
INSERT INTO download_assets (
    bundle_key, app_key, platform, variant_key,
    artifact_url, artifact_source, release_version,
    requires_entitlement, checksum
) VALUES
    ('business_suite', '{{TARGET}}', 'windows', 'default',
     'https://downloads.example.com/{{TARGET}}/{{TARGET}}-setup.exe',
     'direct', '1.0.0', true, 'sha256-...'),
    ('business_suite', '{{TARGET}}', 'mac', 'default',
     'https://downloads.example.com/{{TARGET}}/{{TARGET}}.dmg',
     'direct', '1.0.0', true, 'sha256-...'),
    ('business_suite', '{{TARGET}}', 'linux', 'default',
     'https://downloads.example.com/{{TARGET}}/{{TARGET}}.AppImage',
     'direct', '1.0.0', true, 'sha256-...');
6.4 Verifying Registration

After registering, verify the full loop works:

  1. Download listing: GET /api/v1/downloads?app={{TARGET}}&platform=linux with a valid subscriber token → should return the artifact URL.
  2. Entitlement gating: Same request with an expired/missing token → should return 401 or 403.
  3. Entitlements response: GET /api/v1/entitlements should include {{TARGET}} in available features (if feature-flagged) or the download should simply work for any active subscriber.

7. LPBS Client Configuration

{{TARGET}} needs to know how to reach LPBS. Use environment variables for all connection details.

7.1 Required Environment Variables

| Variable | Purpose | Example |

|----------|---------|---------|

| LPBS_BASE_URL | LPBS API base URL | http://localhost:5080 |

| JWT_SECRET | Shared HMAC signing key (same as LPBS) | 32-byte hex string |

| JWT_ISSUER | Expected token issuer | landing-page-business-suite |

7.2 Client Initialization Pattern
type LPBSClient struct {
    baseURL    string
    httpClient *http.Client
}

func NewLPBSClient(baseURL string) *LPBSClient {
    return &LPBSClient{
        baseURL: strings.TrimRight(baseURL, "/"),
        httpClient: &http.Client{
            Timeout: 10 * time.Second,
        },
    }
}
7.3 Resilience When LPBS Is Unreachable
{{TARGET}} calls LPBS and gets network error or timeout
│
▼
Is this an auth validation call?
├─ YES → JWT validation is local (shared secret), no network needed
│        Only token refresh requires LPBS — fail gracefully
│
└─ NO (entitlement check or credit consumption)
   │
   ▼
   Is there a cached entitlement result < 60 seconds old?
   ├─ YES → use cached result, log warning about LPBS unavailability
   └─ NO  → fail closed (deny access), log error
            Do NOT fail open — that would give free access during outages

Critical rule: When LPBS is unreachable and no cache exists, fail closed (deny access). Failing open means any network issue grants free access to paid features.


8. Error Response Consistency

{{TARGET}} must return LPBS-compatible error shapes so the user experience is uniform across all bundled apps.

8.1 Standard Error Shape
{
  "error": "Human-readable message safe for UI display",
  "error_type": "unauthorized|forbidden|validation|rate_limited|server_error",
  "retryable": false
}
8.2 LPBS Integration Error Mapping

| Scenario | HTTP Status | error_type | User-facing message |

|----------|------------|------------|---------------------|

| Missing/invalid JWT | 401 | unauthorized | "Please sign in to continue" |

| Valid JWT, no subscription | 403 | forbidden | "An active subscription is required" |

| Valid JWT, insufficient credits | 402 | forbidden | "Insufficient credits — top up to continue" |

| LPBS unreachable, no cache | 503 | server_error | "Service temporarily unavailable" |

| Rate limited by LPBS | 429 | rate_limited | "Too many requests — please wait" |


9. Integration Checklist (Per Bundled App)

Use this checklist to verify {{TARGET}} is fully integrated. Every item should be true before considering integration complete.

9.1 Authentication
9.2 Entitlements
9.3 Credit Consumption
9.4 LPBS Registration
9.5 Error Handling

10. Bundle Integration Audit (Brownfield Assessment)

When integrating an existing {{TARGET}} that may already have partial or ad-hoc integration, audit the current state first.

10.1 Discovery Commands
# Find existing auth middleware or JWT handling
rg -n "jwt\|JWT\|Bearer\|access_token\|Authorization" scenarios/{{TARGET}}/api/ --type go

# Find any hardcoded secrets or credentials
rg -n "JWT_SECRET\|jwt_secret\|signing.key\|hmac" scenarios/{{TARGET}}/ --type go

# Find existing HTTP client calls (may be calling LPBS already)
rg -n "lpbs\|landing-page\|entitlement\|credits\|subscription" scenarios/{{TARGET}}/ --type go -i

# Find user/account models (should NOT exist if fully LPBS-integrated)
rg -n "type User struct\|type Account struct\|user_password\|bcrypt" scenarios/{{TARGET}}/api/ --type go

# Find any existing credit/billing logic (should delegate to LPBS)
rg -n "credit\|balance\|wallet\|billing\|stripe" scenarios/{{TARGET}}/ --type go -i

# Check environment variable usage
rg -n "LPBS_BASE_URL\|JWT_SECRET\|JWT_ISSUER" scenarios/{{TARGET}}/
10.2 Red Flags
10.3 Document Findings

At session start, read existing findings:

At session end, update findings:

Template:

# Bundle Integration Status — {{TARGET}}

## Last Updated
[Date]

## Integration Status
| Area | Status | Notes |
|------|--------|-------|
| JWT Auth | ✅/⚠️/❌ | [details] |
| Entitlements | ✅/⚠️/❌ | [details] |
| Credit Consumption | ✅/⚠️/❌ | [details] |
| LPBS Registration | ✅/⚠️/❌ | [details] |
| Error Handling | ✅/⚠️/❌ | [details] |

## Gated Features Inventory
| Feature | Gate Type | Credit Cost | Idempotent? | Notes |
|---------|-----------|-------------|-------------|-------|
| [feature] | credit/subscription/free | [amount] | yes/no | [notes] |

## Issues Found
- [Issue with file:line references]

## Priority Actions
1. [Most important next step]

11. Memory Management with Visited Tracker

Use visited-tracker to track which files in {{TARGET}} have been audited for bundle integration compliance.

# Find files not yet audited
visited-tracker least-visited --location scenarios/{{TARGET}} --tag bundle-integration --limit 5

# After auditing a file
visited-tracker visit <file-path> --location scenarios/{{TARGET}} --tag bundle-integration --note "<summary>"

12. Output Expectations

You may:

You must:

You must NOT:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.