Skip to content

fix(ledger): zero historical live balances - #4726

Merged
GAlexIHU merged 1 commit into
mainfrom
codex/fix-historical-live-balance
Jul 23, 2026
Merged

GAlexIHU merged 1 commit into
mainfrom
codex/fix-historical-live-balance

Conversation

@GAlexIHU

@GAlexIHU GAlexIHU commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • return live: "0" for customer credit balance requests with an explicit timestamp
  • skip open-charge and live-source calculation for historical balance queries
  • preserve current live-balance calculation when timestamp is omitted
  • document the historical behavior in TypeSpec and regenerate OpenAPI and SDK artifacts
  • add regression coverage for current and historical balance behavior
  • document the same-worktree concurrent nix develop verification race discovered during this change

Why

Historical requests previously mixed ledger balances at the requested timestamp with current open-charge impacts and current usage totals. That could produce a live balance that never represented a coherent point in time.

Open-charge state cannot currently be reconstructed historically, so historical responses now return zero instead of presenting a misleading value.

Impact

Current balance requests are unchanged. For requests with an explicit timestamp:

  • settled and pending retain their historical behavior
  • live is always zero and is not calculated

Validation

  • go test -tags=dynamic ./openmeter/ledger/customerbalance
  • go vet -tags=dynamic ./openmeter/ledger/customerbalance
  • go test -tags=dynamic ./api/v3/handlers/customers/credits
  • go vet -tags=dynamic ./api/v3/handlers/customers/credits
  • go test -tags=dynamic ./test/credits
  • make lint-api-spec
  • make test-go-sdk
  • git diff --check

Summary by CodeRabbit

  • Bug Fixes

    • Historical credit balance queries now consistently report “live” credits as zero when using the timestamp/point-in-time parameter.
    • Current balance queries continue to include applicable live charge impacts.
    • Point-in-time requests now correctly separate “retrieved at” time from the historical cutoff.
  • Documentation

    • Updated API/client documentation to clarify that live is always zero for historical balance responses.
  • Tests

    • Added coverage for historical “live = 0” behavior and updated related balance assertions.

Greptile Summary

This PR makes historical customer credit balances return a zero live amount. The main changes are:

  • Skip current open-charge and usage calculations when timestamp is provided.
  • Preserve live-balance calculation when timestamp is omitted.
  • Update TypeSpec and generated API and SDK documentation.
  • Add tests for current and historical balance behavior.
  • Document sequential Nix verification in the same worktree.

Confidence Score: 5/5

This looks safe to merge.

  • Historical requests consistently skip current-only live calculations.
  • Requests without a timestamp preserve the current live-balance path.
  • Source specifications, generated artifacts, and tests agree on the updated behavior.
  • No blocking issues were found in the changed code.

Important Files Changed

Filename Overview
api/v3/handlers/customers/credits/get_balance.go Separates the response timestamp from the optional historical cutoff.
openmeter/ledger/customerbalance/service.go Calculates live balances only for requests without an explicit as-of timestamp.
openmeter/ledger/customerbalance/facade_test.go Covers nonzero current live balances and zero historical live balances.
openmeter/ledger/customerbalance/service_test.go Updates historical balance expectations to the new zero-live contract.
api/spec/packages/aip/src/customers/credits/balance.tsp Documents the historical behavior of the live balance field.
api/spec/packages/aip/src/customers/credits/operations.tsp Documents how the timestamp parameter affects historical responses.

Reviews (3): Last reviewed commit: "fix(ledger): zero historical live balanc..." | Re-trigger Greptile

Context used (3)

  • Context used - CLAUDE.md (source)
  • Context used - AGENTS.md (source)
  • Context used - api/spec/AGENTS.md (source)

@GAlexIHU GAlexIHU added the release-note/bug-fix Release note: Bug Fixes label Jul 16, 2026
@coderabbitai

coderabbitai Bot commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Historical credit balance queries now return live = 0. The handler separates retrieval time from the historical cutoff, tests cover the behavior, and API documentation plus generated specifications describe the updated contract.

Changes

Historical credit balance semantics

Layer / File(s) Summary
Historical live-balance calculation
openmeter/ledger/customerbalance/service.go, openmeter/ledger/customerbalance/*_test.go
Historical AsOf queries skip live charge impacts and return zero live balance; tests verify settled and live values.
API timestamp and response wiring
api/v3/handlers/customers/credits/get_balance.go
The handler separates RetrievedAt from optional HistoricalAsOf, forwards the historical cutoff, and maps retrieval time into the response.
Credit balance contract documentation
api/spec/packages/aip/..., api/spec/packages/aip-client-javascript/..., api/v3/api.gen.go, api/v3/client/models_customers.go
Credit balance and timestamp documentation, generated models, and embedded Swagger data describe zero live balances for historical queries.

Development workflow guidance

Layer / File(s) Summary
Sequential Nix development commands
AGENTS.md
Development guidance now prohibits concurrent nix develop invocations in the same worktree.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant APIClient
  participant GetCustomerCreditBalanceHandler
  participant CustomerBalanceFacade
  participant CustomerBalanceService
  APIClient->>GetCustomerCreditBalanceHandler: Request with optional timestamp
  GetCustomerCreditBalanceHandler->>CustomerBalanceFacade: GetBalances with HistoricalAsOf
  CustomerBalanceFacade->>CustomerBalanceService: Calculate balance
  CustomerBalanceService-->>CustomerBalanceFacade: Historical live balance is zero
  CustomerBalanceFacade-->>GetCustomerCreditBalanceHandler: Return balances
  GetCustomerCreditBalanceHandler-->>APIClient: Response with RetrievedAt
Loading

Possibly related PRs

Suggested labels: area/billing

Suggested reviewers: tothandras, borosr, rolosp, borbelyr-kong

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main behavior change: historical ledger live balances now return zero.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/fix-historical-live-balance

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@GAlexIHU
GAlexIHU force-pushed the codex/fix-historical-live-balance branch from 68694b5 to b2fdce2 Compare July 16, 2026 15:04
@GAlexIHU
GAlexIHU marked this pull request as ready for review July 16, 2026 15:18
@GAlexIHU
GAlexIHU requested a review from a team as a code owner July 16, 2026 15:18
turip
turip previously approved these changes Jul 16, 2026
@GAlexIHU

Copy link
Copy Markdown
Contributor Author

@tothandras @turip i'd actually prefer if response type was changed so live is optional and only returned if timestamp is not specified. wdyt?

@GAlexIHU
GAlexIHU force-pushed the codex/fix-historical-live-balance branch from b2fdce2 to 74f00f1 Compare July 23, 2026 10:01
@GAlexIHU
GAlexIHU merged commit fab8c28 into main Jul 23, 2026
30 checks passed
@GAlexIHU
GAlexIHU deleted the codex/fix-historical-live-balance branch July 23, 2026 10:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/bug-fix Release note: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants