Skip to content

fix(ledger): hydrate tax behavior - #4475

Merged
GAlexIHU merged 1 commit into
mainfrom
codex/fix-ledger-tax-behavior-hydration
Jun 4, 2026
Merged

GAlexIHU merged 1 commit into
mainfrom
codex/fix-ledger-tax-behavior-hydration

Conversation

@GAlexIHU

@GAlexIHU GAlexIHU commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • hydrate TaxBehavior when rebuilding historical ledger transaction entries
  • add a focused GetTransactionGroup regression test for v2 tax-behavior routes

Tests

  • nix develop --impure .#ci -c env POSTGRES_HOST=127.0.0.1 go vet -tags=dynamic ./openmeter/ledger/historical/adapter
  • nix develop --impure .#ci -c env POSTGRES_HOST=127.0.0.1 go test -tags=dynamic ./openmeter/ledger/historical/adapter

Summary by CodeRabbit

  • Bug Fixes

    • Fixed a data integrity issue where tax behavior configuration was not properly preserved when retrieving historical transaction records, ensuring consistent and accurate tax handling.
  • Tests

    • Added test coverage to validate that tax behavior settings are correctly maintained in historical transactions.

@GAlexIHU
GAlexIHU requested a review from a team as a code owner June 3, 2026 12:36
@coderabbitai

coderabbitai Bot commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR patches a bug where the TaxBehavior field was left unset when reconstructing historical transaction routes from persisted route edge data. The fix adds one line to populate that field, paired with integration test coverage and a small test helper refactoring.

Changes

TaxBehavior Field Hydration

Layer / File(s) Summary
Restore TaxBehavior field in historical route hydration
openmeter/ledger/historical/adapter/ledger.go
Line 57: The hydrateHistoricalTransaction function now sets TaxBehavior when rebuilding ledger.Route from persisted sub-account route edge data.
Integration test and test infrastructure
openmeter/ledger/historical/adapter/ledger_test.go
New TestRepo_GetTransactionGroup_PreservesTaxBehavior validates that hydrated transaction group routes preserve tax behavior and routing key version V2. Test helpers refactored: createSubAccount now delegates to a new createSubAccountOfType method for flexible account type specification.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

release-note/bug-fix

Suggested reviewers

  • tothandras
  • turip
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: fixing tax behavior hydration in the ledger adapter when reconstructing historical transactions.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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-ledger-tax-behavior-hydration

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 and usage tips.

@GAlexIHU GAlexIHU added release-note/ignore Ignore this change when generating release notes release-note/bug-fix Release note: Bug Fixes and removed release-note/ignore Ignore this change when generating release notes labels Jun 3, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@openmeter/ledger/historical/adapter/ledger_test.go`:
- Around line 189-196: The loop over hydratedGroup.Transactions()[0].Entries()
can vacuously pass if Entries() is empty; add an explicit check before the loop
to assert the expected number of entries (e.g., require.Len on
hydratedGroup.Transactions()[0].Entries()) so the test fails when hydration
drops entries; update the test around the existing require.Len(t,
hydratedGroup.Transactions(), 1) and the subsequent for-loop that iterates
Entries() to include this additional assertion referencing
hydratedGroup.Transactions(), Entries(), and the existing entry assertions on
PostingAddress().Route().
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 0a651456-9576-4ab3-9f96-e529e72bb245

📥 Commits

Reviewing files that changed from the base of the PR and between d839096 and ff6af96.

📒 Files selected for processing (2)
  • openmeter/ledger/historical/adapter/ledger.go
  • openmeter/ledger/historical/adapter/ledger_test.go

Comment on lines +189 to +196
require.Len(t, hydratedGroup.Transactions(), 1)

for _, entry := range hydratedGroup.Transactions()[0].Entries() {
hydratedRoute := entry.PostingAddress().Route().Route()
require.NotNil(t, hydratedRoute.TaxBehavior)
require.Equal(t, taxBehavior, *hydratedRoute.TaxBehavior)
require.Equal(t, ledger.RoutingKeyVersionV2, entry.PostingAddress().Route().RoutingKey().Version())
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Nice test! One small gap: guard against a vacuous pass.

You assert there's exactly one transaction, but the entry assertions live inside the for loop over Entries(). If hydration ever dropped entries and returned an empty slice, the loop body would never run and the test would happily pass without checking anything. A quick require.Len on the entries closes that hole.

💚 Suggested tweak
 	require.NoError(t, err)
 	require.Len(t, hydratedGroup.Transactions(), 1)
 
-	for _, entry := range hydratedGroup.Transactions()[0].Entries() {
+	entries := hydratedGroup.Transactions()[0].Entries()
+	require.Len(t, entries, 2)
+	for _, entry := range entries {
 		hydratedRoute := entry.PostingAddress().Route().Route()
 		require.NotNil(t, hydratedRoute.TaxBehavior)
 		require.Equal(t, taxBehavior, *hydratedRoute.TaxBehavior)
 		require.Equal(t, ledger.RoutingKeyVersionV2, entry.PostingAddress().Route().RoutingKey().Version())
 	}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
require.Len(t, hydratedGroup.Transactions(), 1)
for _, entry := range hydratedGroup.Transactions()[0].Entries() {
hydratedRoute := entry.PostingAddress().Route().Route()
require.NotNil(t, hydratedRoute.TaxBehavior)
require.Equal(t, taxBehavior, *hydratedRoute.TaxBehavior)
require.Equal(t, ledger.RoutingKeyVersionV2, entry.PostingAddress().Route().RoutingKey().Version())
}
require.Len(t, hydratedGroup.Transactions(), 1)
entries := hydratedGroup.Transactions()[0].Entries()
require.Len(t, entries, 2)
for _, entry := range entries {
hydratedRoute := entry.PostingAddress().Route().Route()
require.NotNil(t, hydratedRoute.TaxBehavior)
require.Equal(t, taxBehavior, *hydratedRoute.TaxBehavior)
require.Equal(t, ledger.RoutingKeyVersionV2, entry.PostingAddress().Route().RoutingKey().Version())
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@openmeter/ledger/historical/adapter/ledger_test.go` around lines 189 - 196,
The loop over hydratedGroup.Transactions()[0].Entries() can vacuously pass if
Entries() is empty; add an explicit check before the loop to assert the expected
number of entries (e.g., require.Len on
hydratedGroup.Transactions()[0].Entries()) so the test fails when hydration
drops entries; update the test around the existing require.Len(t,
hydratedGroup.Transactions(), 1) and the subsequent for-loop that iterates
Entries() to include this additional assertion referencing
hydratedGroup.Transactions(), Entries(), and the existing entry assertions on
PostingAddress().Route().

@GAlexIHU
GAlexIHU merged commit 8e5f04d into main Jun 4, 2026
33 of 38 checks passed
@GAlexIHU
GAlexIHU deleted the codex/fix-ledger-tax-behavior-hydration branch June 4, 2026 11:10

This branch was previously deployed

1 inactive deployment
dev — ff6af96a Deployed Jun 3, 2026 by GAlexIHU via Migration Checks #14798
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