Repository navigation
Conversation
update_stats, subtract_coins and the conversation counter no longer SELECT the counter row before bumping it. A new _bump helper runs the atomic UPDATE and, only when it matches no row, falls back to the dialect INSERT ... ON CONFLICT DO UPDATE with the same increments, which also covers concurrent first requests. The savepoints and swallowed IntegrityErrors go away. A steady-state bill is one UPDATE per counter and draws no serial id. Part of #171
Member
Author
|
How to test |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
_bump(model, key, increments, insert_values)inlumen/services/llm.py: runs the atomic UPDATE and, only when it matches no row (an entity's first use), falls back to the dialectINSERT ... ON CONFLICT (key) DO UPDATEwith the same increments. Postgres or SQLiteinsertis picked fromdb.engine.dialect.update_stats(model_stats,entity_stats),subtract_coins(entity_balances) and the chat route's_count_conversation_starteduse it. The existence SELECTs, savepoints and swallowedIntegrityErrors are gone.coins_left = max(0, starting - cost),last_refill_at = now. The UPDATE and the ON CONFLICT branch both applyGREATEST(0, coins_left - cost)to an existing row.idofmodel_stats/entity_balancesbefore it detects the conflict, so every request would burn a sequence value.api/routes.pythat pointed at the old pattern is updated.Steady-state billing statements go from SELECT+UPDATE x3 + INSERT to UPDATE x3 + INSERT.
Testing
tests/unit/test_billing_counters.py(SQLite):update_stats+subtract_coinsissue no SELECT and no SAVEPOINT (statement recorder, same technique astest_preflight_queries.py)rowcount == 0; the ON CONFLICT branch adds exactly once (and floors the balance)tests/integration/test_billing_counters_postgres.py(gated onLUMEN_TEST_POSTGRES_URL): 8 threads bill a fresh entity at once, givingrequests == 8,balance == starting - sum(cost), no errors; 10 steady-state bills leavemodel_stats_id_seqandentity_balances_id_sequnchanged. This runs in CI; no local Postgres was available.test_llm_coins.py,test_entity_stat.pyandtest_chat_routes.pystill pass. The full SQLite suite andruff checkpass.Part of #171