Skip to content

feat: Implement comprehensive credit-based cost calculation and lifecycle management #836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: angel-credit_balances
Choose a base branch
from

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 from 1a8a07c to e871285 Compare August 14, 2025 12:52
@amalcaraz amalcaraz force-pushed the angel-compute_credit_costs branch from e871285 to 96663a1 Compare August 14, 2025 13:44
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.

2 participants