Repository navigation
Conversation
saveRequestUsage treated an existing usageHistory row with the same timestamp, provider, model, connection, apiKey and token counts as a repeat of the request being saved, and returned without inserting. The only work that branch actually did was back-fill a missing endpoint; everything else it swallowed. timestamp is an ISO string, so the match is only millisecond-precise. Two real requests to the same model on the same connection returning the same token counts in the same millisecond — ordinary under parallel load — collapsed into one row, taking their totalRequestsLifetime increments with them. 100 concurrent saves left 1. Limit the match to rows that are still missing an endpoint, which is the case the branch was written for.
rabbi696
added a commit
to golamrabbi696/EzRouter
that referenced
this pull request
Aug 27, 2026
rabbi696
added a commit
to golamrabbi696/EzRouter
that referenced
this pull request
Aug 27, 2026
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.
Problem
tests/unit/db-concurrent.test.jshas been failing onmaster:It is not a transaction race — the driver is native
better-sqlite3and the comment above the transaction is right that a sync driver cannot yield mid-transaction. It is the dedupe query at the top of it:entry.timestampis an ISO 8601 string, sotimestamp = ?is only millisecond-precise. Two genuinely different requests to the same model on the same connection that return the same prompt/completion counts within the same millisecond match this predicate, and the second one is thrown away — along with itstotalRequestsLifetimeincrement and itsusageDailyaggregation.Every production write reaches this through
saveUsageStats, which stampstimestamp: new Date().toISOString()at save time, so nothing upstream keeps them apart.Measured directly, 100 concurrent
saveRequestUsagecalls on a fresh DB:base + ims)The repo's own test sees 2, 14 or 1 depending on where the millisecond boundary lands mid-loop, which is why the numbers move between runs.
Fix
The only thing that branch ever did was back-fill an endpoint onto a row that was written without one. Everything else was a silent drop. So match only the rows that case is about:
What this trades away
If some path really does save the same request twice with an endpoint both times, it is now counted twice. That path was already only collapsed when both saves landed in the same millisecond — one millisecond later the old code double-counted too — so this makes the behaviour consistent rather than timing-dependent. I could not find such a path: each of
streamingHandler,nonStreamingHandlerandsseToJsonHandlercallssaveUsageStatsonce per request on mutually exclusive branches, and no test covers a double save. If one exists, the durable fix is a per-request id in the dedupe key rather than a value tuple — happy to do that instead if you can point me at it.Verification
tests/unit/usage-row-dedupe.test.js, 4 tests against a real temp-dir SQLite: requests sharing a millisecond survive (with and without an endpoint), the endpoint back-fill still collapses to one row, and a different model in the same millisecond is not folded into an endpoint-less row.Restoring the wide match:
Full offline suite (
vitest run unit auth translator,CI=true, excludingreal/), same command on both sides:origin/master(699edac)The three that flipped to passing are exactly the
db-concurrentones above —common the sorted full test names shows nothing added.eslintclean.(
db-concurrent.test.jsalso reports a suite-levelEBUSY ... unlink data.sqliteduringafterAllon Windows, because the SQLite handle is still open when the temp dir is removed. That is pre-existing, platform-specific and unrelated; I left it alone.)