Repository navigation
fix(spend): retain billed failure accounting when error logs are disabled - #53271
Conversation
|
@greptileai review |
|
bugbot run |
|
@veria-ai |
PR overviewNo security issues were found in this pull request. Reviews
|
Bugbot is paused — your team has an unpaid invoiceBugbot is paused because your team has an unpaid invoice. Pay it from the billing page in the Cursor dashboard and Bugbot resumes automatically. (requestId: 49961743-18cc-4f44-8eba-4f9f20348f0b) |
[Medium impact] The PR appears safe to merge, with no actionable issues found.
|
surlymochan
left a comment
There was a problem hiding this comment.
Checked the gate against the helpers it calls: failure_row_response_cost already covers the recovered-stream cost and the in-spend guardrail cost, and _billed_failed_attempts returns the stashed retry attempts, so _failure_billed_the_provider ORs exactly the three ways a failure can have billed the provider. The old condition dropped all three whenever error logs were off and there was no completed cost, which is the spend leak this fixes. The negative-cost and guardrail-outside-spend cases in the new tests confirm the > 0 boundary holds, nothing billed still means nothing written. The rust-side test mirrors the same matrix, which matters since the two writers have to agree on what counts as billed.
…bled (BerriAI#53271) Adapted for lts/1.108.x. This line's failure hook has no finalized-cost branch and no tracks_failure_spend helper, so the billed check joins the hook's own disable_error_logs gate, and the Rust spend ledger test is left out because this line has no Rust spend ledger. The unit tests use this line's DualCache counters. Co-authored-by: Joshua Valluru <326636767+joshua-berri@users.noreply.github.com> (cherry picked from commit b5ff853)
Handwritten 1-3 sentence description
Details
TLDR
Problem this solves:
How it solves it:
User Flow
Before, failed requests can incur charges missing from tracked spend.
disable_error_logs: trueon the gateway.After, those charges remain accounted for without detailed error records.
disable_error_logs: trueon the gateway.Relevant issues
Fixes #53156
Pre-Submission checklist
Screenshots / Proof of Fix
Baseline
e7b4b5c7203197ba4be8825aa1e351d6582c6e34.Candidate
1f4e87b5b7c2a92f5450b68477db346156652c1d.Shared setup
Executed against source builds at the two SHAs above, with PostgreSQL and a dedicated Redis per proxy, two proxy workers, and matching native extensions. Set
DATABASE_URLto a disposable local PostgreSQL admin database,VENV_BINto the prepared proxy environment's bin directory, andANTHROPIC_API_KEYfor the real-provider case. The harness creates and drops only databases namedt53156_<rig>; use an isolated verification environment.Save the following files under
harness/, thenchmod +x harness/*.sh. The scripts contain the exact generated config, requests, SQL snapshots, callback tracing, and acknowledged flush commands. Fixture credentials are throwaway literals. The only portability edit from execution replaces the verifier's virtualenv path withVENV_BIN.Repeat the paired rigs with
falsefor the error-log flag. Legacy controls useofffor the ledger argument. Disabled-accounting controls useofffor the spend argument. The real-provider and Responses streaming pairs ran only with ledger enabled. No build or service ran on the author's laptop.Before (
e7b4b5c7203197ba4be8825aa1e351d6582c6e34)With
disable_error_logs: true, every positive, unfinalized failure below produced zero deliveries, zero detailed logs, and zero key spend. The corresponding flag-false cases recorded the cost. Finalized-cost and successful controls already worked.After (
1f4e87b5b7c2a92f5450b68477db346156652c1d)The same flag-true requests record these amounts, with zero detailed error logs. Entity/daily deltas match each case's flag-false control. Every case had four flushes acknowledged by both workers, with identical tables after flushes two and four.
/v1/chat/completions, interrupted stream/v1/chat/completions, read timeout/v1/chat/completions, three billed failed attempts/v1/chat/completions, billed retry then zero-cost terminal failure/v1/chat/completions, pre-call guardrail rejection/v1/chat/completions, guardrail pass then read timeout/v1/messages, read timeout/v1/responses, read timeout/v1/responses, interrupted streamclaude-haiku-5-5The real-provider relay cut the stream after three text deltas. Baseline flag-true recovered the same $0.0000097 but persisted nothing; head persisted one accounting-only delivery. These amounts are LiteLLM usage estimates, not invoice reconciliation. Synthetic upstream fixtures exercise the remaining failure shapes.
Legacy accounting with the ledger disabled also retains the charges without detailed logs. Existing flag-false results, finalized guardrail rejection ($0.000314), success ($0.000014), zero-cost rejection/connect failure, and disabled-accounting behavior remain unchanged.
Read projections: key, team, user, organization, project, end-user and team-member spend; daily user/team/organization/tag/end-user rows. For Messages/Responses bodies without an end user, that counter correctly remains unchanged. Budget tables, daily agent rows and Redis counters were not inspected. Tag and organization-membership counters had no seeded rows, so they were not tested live. Daily tag spend was inspected; it is separate from TagTable spend.
Executed test command and environment provenance
Python 3.12.16, pytest 9.0.3, coverage 7.14.0, PostgreSQL 14.24, Redis server 8.10.3. The verifier reused its full proxy virtualenv and put each exact checkout first on
PYTHONPATH. The native extension was built from baseline withCARGO_TARGET_DIRisolated,CARGO_PROFILE_DEV_DEBUG=0,SQLX_OFFLINE=true; the exact build invocation was not retained. Both checkouts used an identical extension, valid because the candidate changes no Rust sources. No claim is made that the shared full environment was freshly resolved from the lockfile; the lint environment useduv sync --frozen --only-group dev --no-install-project.Paths above are portable substitutions for the verifier's scratch/virtualenv paths. CI independently resolves its canonical environments.
Regression tests and gates
tracks_failure_spend: 8/8 branch arcs covered. Whole touched module: 96%.1f4e87b5. Veria: no security issues at the same tip. Review threads and inline comments contain no findings.Verification limits
harness/case.sh
harness/fake_upstream.py
harness/flush.sh
harness/matrix.sh
harness/relay.py
harness/rig.sh
harness/snap.sh
harness/summarize.py
harness/t53156_trace.py
Type
Bug Fix
Final Attestation