Question
Implement a ledger for compute-credit grants and debits whose events may arrive out of order. Each grant has a unique ID, amount, activation timestamp, and expiration duration. A grant is available through its expiration boundary. `subtract(amount, timestamp)` consumes credits that expire soonest. `get_balance(timestamp)` returns the balance that would have been available at that point in time by evaluating all applicable events in chronological order. Define deterministic behavior for duplicate IDs, insufficient balance, equal timestamps, and late-arriving events. Preserve enough history to answer past-balance queries correctly. ## Follow-ups 1. Return an auditable allocation of each debit across grants. 2. Support cancellation or correction of an earlier event. 3. Discuss indexing and checkpointing when the ledger contains millions of events.