Open
Conversation
Adds UsageTracker to src/runtime/usage.zig: - Rolling 5-hour window with automatic reset - Thread-safe: all mutations guarded by mutex - record(backend): increments call count for claude or codex - nearLimit(backend, quota, warn_at_percent): checks if calls exceed threshold fraction of quota - callCount/totalCalls: current window counts - windowRemainingSeconds: time left in current window - Global singleton via usage.global() with lazy init Wires into dispatch.zig: every dispatch() call now records usage for the dispatched backend. 9 new tests: record, nearLimit (trigger/below/zero-quota), window reset, remaining seconds, global singleton, concurrent safety with 4 threads x 100 records. All 12 tests pass (9 usage + 3 types). Made-with: Cursor
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.
Summary
UsageTracker(src/runtime/usage.zig): Thread-safe call counter with a rolling 5-hour windowrecord(backend): increment call count per backendnearLimit(backend, quota, warn_pct): check if calls exceed thresholdcallCount()/totalCalls(): current window countswindowRemainingSeconds(): time left in windowusage.global()with lazy initdispatch.zig: Everydispatch()call now records usage viausage.global().record()This provides the counting layer (Layer 0 per issue spec). The load-balancing in
resolve()(Layer 1) can usenearLimit()to overflow to the secondary provider.Test plan
zig test src/runtime/usage.zig— 12/12 tests pass (9 usage + 3 types)Closes #268
Made with Cursor