Repository navigation
feat(budgets): run the ledger's post-call spend counter step in rust - #52166
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Merging this PR will not alter performance1 warning
Comparing |
c00dc90 to
a712582
Compare
a712582 to
683026b
Compare
683026b to
a86e617
Compare
84076ca to
8654578
Compare
There was a problem hiding this comment.
Devin Review found 4 potential issues.
5 flags not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)
| SpendCounter::TeamMember { | ||
| user_id: user_id.clone(), | ||
| team_id: team_id.clone(), | ||
| }, |
There was a problem hiding this comment.
🟡 Double charges for composite team IDs
When a membership reservation uses a colon-containing team ID, plan charges it again. Parsed and constructed counters differ despite identical keys.
Learn more
Reservation counters and planned counters use different representations of the same Redis key. held_counters parses reservation keys, and SpendCounter::parse splits a membership key at its last colon. The plan instead preserves the original user and team IDs as separate fields. ensure_warmed_counters compares these enum values to decide whether reconciliation already owns the charge. Unequal values therefore bypass reservation skipping even when they serialize to the same key. The same representation problem affects team IDs containing :window:.
Example: A user alice belongs to team org:engineering. Both representations write spend:team_member:alice:org:engineering, but parsing produces user alice:org and team engineering. With a reservation of 5 and an actual cost of 2, reconciliation applies -3 and the direct charge adds another 2, leaving 4 instead of 2.
Recommended fix: Use canonical Redis-key identity for reservation skipping and affected-counter deduplication, or normalize all planned counters through the same parser as reservation counters. Add a regression covering a membership reservation with a colon-containing team ID.
Was this helpful? React with 👍 or 👎 to provide feedback.
| run_hosted( | ||
| move |host| async move { | ||
| budgets | ||
| .charge_counters(&host, request.as_ref(), &step, record.as_ref()) | ||
| .await |
There was a problem hiding this comment.
🟡 Missing request context for budget metrics
With ledger accounting enabled, charge_counters omits RedisCall scoping. Counter reads and seeds lose spend-counter labels and request spans.
Learn more
The Python-facing store wrapper captures the caller's context in call and runs each operation inside a RedisCall scope. The hosted counter step bypasses those wrappers and never installs that scope. ServiceLoggingEvents::call consequently selects its empty background context and raw Redis operation names. The Rust logger captured by Budgets does not install a Redis call tag. A request's separately scoped post-call flush can still retain its context, but direct reads and seeds do not.
Example: A ledger request that cold-seeds a team counter reports its MGET and SET without the request's parent span or spend-counter service target. The previous path reported those operations through the scoped store wrappers.
Recommended fix: Capture the caller's Redis reporting context at the bridge entrypoint and propagate it through the hosted execution. Preserve operation-specific labels for direct reads and seeds, while keeping request-pipeline flush attribution unchanged. Add a service-event regression that asserts the target and parent span.
Was this helpful? React with 👍 or 👎 to provide feedback.
| loop: Final = asyncio.new_event_loop() | ||
| thread: Final = threading.Thread(target=loop.run_forever, daemon=True) | ||
| thread.start() | ||
| server: Final = asyncio.run_coroutine_threadsafe(asyncio.start_server(redis.serve, "127.0.0.1", 0), loop).result( | ||
| timeout=5 |
There was a problem hiding this comment.
🟡 Network servers inside isolated unit tests
rust_redis_store opens TCP connections inside unit tests. The new redis_port fixture does likewise, violating the repository's explicit no-network unit-test rule.
Learn more
The repository assigns in-process tests to tests/unit and explicitly prohibits network access there. This helper starts a TCP Redis server on a background event loop, and redis_port starts another TCP server. The ledger tests that call these helpers therefore depend on sockets and background-server scheduling.
Example: Running a ledger counter test that uses rust_redis_store binds an ephemeral localhost port and connects NativeRedisClient to it, even though the test resides in the isolated unit-test suite.
Recommended fix: Move the TCP-backed scenarios and their server fixtures to the integration suite. Keep genuinely in-process Rust memory-store scenarios in the mapped unit-test files.
Was this helpful? React with 👍 or 👎 to provide feedback.
| """The ledger's post-call counter step, which ``increment_spend_counters`` hands to Rust, run on the Rust spend | ||
| counter store: in memory, and over a Redis server.""" |
There was a problem hiding this comment.
🟡 Ledger tests bypass required path mapping
ledger_scenarios duplicates existing tests across new ledger files. Repository rules require extending mapped files, including test_rust_spend_counters.py for the new module.
Learn more
Repository rules require unit-test paths to mirror production paths and require additions for existing behavior to extend the existing mapped test file. The new production module is rust_spend_counters, but its dedicated scenarios live under a different name. The PR also creates parallel ledger files for existing counter, callback and reservation tests through ledger_scenarios. These are another execution mode of existing coverage rather than separately mapped production features.
Example: Selecting the mapped unit test for the new rust_spend_counters module does not select test_rust_counter_step.py. Existing callback coverage is also split between its original mapped file and a new ledger twin.
Recommended fix: Put the new module's dedicated tests in tests/unit/proxy/spend_tracking/test_rust_spend_counters.py. Parameterize the existing mapped callback, counter and reservation tests for ledger mode instead of generating parallel test modules.
Was this helpful? React with 👍 or 👎 to provide feedback.
8654578 to
fd968ae
Compare
yujonglee-berri
left a comment
There was a problem hiding this comment.
Looks good, counter step reads cleanly
daa5d04 to
4086291
Compare
4086291 to
f54bac8
Compare
f54bac8 to
a575627
Compare
b172961 to
eb66710
Compare
|
|
Budgets::charge_counters(callbacks-budgets/src/charge.rs), callable directly from the Rust spend log callback; Python's ledger-onincrement_spend_countersreaches it throughNativeBudgets.charge_countersSET NXseeds (concurrent), committed twins, one pipeline with invalidation on failure, budget windows,applied_adjustment/finalizedstampscounters.rs: key, team, member, user, end user, tags, access groups, org, project, customer windows, committed twins; rawsk-keys hashed; reserved counters skippedtest_customer_window_accounting.pyledger-on cases run on the Rust store (in-memory, or a scripted RESP server so "one MGET, one pipeline" is still asserted), 610 passed;litellm-callbacks-budgets81 unit + 10 admission + 19 charge; wide run same 34 failures as mainSET NXkeeps it safe); error text not matched to Python