perf(daemon): throttle agent_usage emission with an in-memory limiter - #2029
Merged
Conversation
should_emit_agent_usage acquired the global metrics-DB mutex and ran a SELECT + upsert transaction on every AI checkpoint just to rate-limit agent_usage telemetry to one event per session per 150s. That put a sqlite round trip (and contention on the shared DB lock) on the checkpoint processing path. Replace it with a bounded in-memory limiter: a HashMap + expiry deque capped at 10,000 sessions, expiring entries after the emission interval and evicting the oldest when full. Sub-microsecond, no I/O, no shared lock with metrics delivery. A daemon restart resets the limiter and may re-emit one event per active session; the downstream pipeline already tolerates duplicate agent_usage events. The sqlite agent_usage_throttle table and its accessor remain for schema compatibility but are no longer on the checkpoint path. Ported from #1979 (closed) onto current main. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Member
Author
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
should_emit_agent_usageacquired the global metrics-DB mutex and ran a SELECT + upsert sqlite transaction on every AI checkpoint, just to rate-limitagent_usagetelemetry to one event per session per 150s.agent_usageevents.agent_usage_throttletable and its accessor remain for schema compatibility but are no longer on the checkpoint path.Test coverage
mock_aithrottle-bypass test retained.Ported from #1979 (closed) onto current main.
Depends on #2028.
🤖 Generated with Claude Code