Skip to content

Conversation

amalcaraz
Copy link
Collaborator

This PR depends on #827

Summary

Implements a complete credit-based payment system for Aleph resources,
extending the existing cost calculation framework to support credit
payments alongside hold and stream payments. Includes cost calculation,
balance validation, automated resource cleanup, and 1-day minimum runtime
protection.

Key Features

  • Credit Cost Calculation: Per-hour credit consumption for all message
    types (VMs, programs, storage)
  • Balance Validation: Pre-flight and runtime validation with 1-day minimum
    runtime requirement
  • Automated Lifecycle Management: Cron job monitors credit balances and
    manages resource cleanup
  • Reusable Framework: DRY implementation eliminates code duplication across
    handlers

Major Changes

Database & Configuration

  • Migration 0036: Added cost_credit column to account_costs table
  • Migration 0037: Registered credit_balance cron job for automated
    management
  • Default Pricing: Added credit rates matching pay-as-you-go pricing across
    all service tiers

Cost System

  • Extended cost calculations to include credit costs for all resource types
  • Updated payment type detection: credit → superfluid → hold (priority
    order)
  • Created reusable validate_balance_for_payment() function for all payment
    types

Message Handlers

  • VM & Store Handlers: Added credit balance validation with 1-day runtime
    protection
  • Pre-check Balance: Store handler now supports credit payments in both
    validation methods
  • Exception Handling: New InsufficientCreditException with detailed error
    reporting

Automated Management

  • Credit Balance Job: Hourly cron job monitors credit changes and manages
    resource lifecycle
  • Grace Period: 24-hour deletion delay for resource recovery
  • State Management: Handles PROCESSED ↔ REMOVING ↔ REMOVED transitions

Technical Implementation

  • 1-Day Runtime Rule: daily_cost = hourly_cost * 24 - ensures minimum
    24-hour operation
  • Payment Detection: Uses content.payment.is_credit for automatic credit
    payment detection
  • Resource Recovery: Automatically restores resources when credits become
    sufficient

Files Changed

  • Database models, cost services, message handlers, cron jobs, pricing
    configuration
  • 2 database migrations for schema and cron job registration
  • New reusable validation framework eliminates code duplication

Backward Compatibility

  • All existing payment methods (hold, superfluid) unchanged
  • No breaking API changes - credit system is purely additive
  • Database migrations include defaults for existing records

Deployment

  1. Run migrations: alembic upgrade head
  2. Restart services to activate credit balance monitoring
  3. Credit payments work immediately with default pricing

This provides a complete credit payment ecosystem with automated resource
management while maintaining full backward compatibility.

@amalcaraz amalcaraz requested a review from nesitor August 11, 2025 17:18
@amalcaraz amalcaraz self-assigned this Aug 11, 2025
@amalcaraz amalcaraz added the enhancement New feature or request label Aug 11, 2025
@nesitor nesitor changed the base branch from main to angel-credit_balances August 12, 2025 14:48
@amalcaraz amalcaraz force-pushed the angel-compute_credit_costs branch 2 times, most recently from e871285 to 96663a1 Compare August 14, 2025 13:44
nesitor
nesitor previously approved these changes Aug 14, 2025
@amalcaraz amalcaraz marked this pull request as ready for review September 2, 2025 09:36
@amalcaraz amalcaraz force-pushed the angel-compute_credit_costs branch from 374c799 to a694825 Compare September 9, 2025 11:24
@amalcaraz amalcaraz force-pushed the angel-credit_balances branch from 8c90358 to 635619f Compare September 9, 2025 11:28
@amalcaraz amalcaraz force-pushed the angel-compute_credit_costs branch from a694825 to 1cce176 Compare September 9, 2025 11:30
@amalcaraz amalcaraz changed the base branch from angel-credit_balances to main September 9, 2025 11:31
@amalcaraz amalcaraz dismissed nesitor’s stale review September 9, 2025 11:31

The base branch was changed.

@amalcaraz amalcaraz requested a review from nesitor September 10, 2025 01:45
@amalcaraz amalcaraz force-pushed the angel-compute_credit_costs branch from 253d2d7 to e50dd4a Compare September 23, 2025 14:42
@nesitor nesitor force-pushed the angel-compute_credit_costs branch from 1d91bd5 to 53a9ac4 Compare October 8, 2025 13:31
@nesitor nesitor merged commit 6970556 into main Oct 8, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants