fix(token-spy): bound usage database queries - #2969
Open
tang-vu wants to merge 1 commit into
Open
Conversation
This was referenced Aug 22, 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.
Why this matters
The authenticated Token Spy usage endpoints passed arbitrary
hoursandlimitvalues directly to the database layer. A zero/negative SQLiteLIMITcan remove the row cap, while very large windows or limits create avoidable database work and unpredictable API behavior. The dashboard's supported all-time window is one year, so the public contract is now 1-8760 hours and 1-1000 usage rows.Root cause: the FastAPI query parameters were typed but had no range constraints.
Behavioral invariant: invalid query ranges are rejected at the HTTP boundary before any database query; documented boundary values still reach the database unchanged.
Overlap check
Searched open and closed upstream PRs for Token Spy usage/query/hour/limit terms and checked PRs changing
ods/extensions/services/token-spy/main.pyand its tests. Existing work covers atomic writes, exception handling, pricing, logging, streaming error bodies, and SSE cursors; none bounds the/api/usage,/token-usage, or/api/summarydatabase queries. This scope is independent.What changed
hoursto 1-8760 on usage and summary endpoints.limitto 1-1000 on both the canonical endpoint and its compatibility alias.Validation
pytest tests/test_api_query_bounds.py -q— 11 passed.pytest tests -q— 31 passed, 1 skipped.python -m py_compile main.py tests/test_api_query_bounds.py— passed.git diff --check— passed.Tradeoffs and rollback
The one-year window matches the dashboard's existing all-time selector, while 1000 rows is twice its current 500-row request and leaves headroom for API clients. Clients requesting larger exports must paginate or narrow the window. Rollback is a single commit; no persisted state or schema changes are involved.
Batch compatibility
Validated as an independent ten-PR batch from upstream
main6ff9b4fc5190099705043acaab7e9b6ad9c8b8f1. The final PR heads merged without conflicts in this order: #2964 -> #2965 -> #2967 -> #2969 -> #2970 -> #2971 -> #2972 -> #2973 -> #2974 -> #2975. The resulting local synthetic merge head is4ae60eadad9696a9accb735aad71af87d2be802d.Combined validation on that exact tree:
git diff --check: passed.The scopes are behaviorally independent. The stated order is the tested rollback/merge sequence for shared-file changes; each PR remains individually useful and revertible.