Skip to content

perf(daemon): throttle agent_usage emission with an in-memory limiter - #2029

Merged
svarlamov merged 1 commit into
mainfrom
feat/agent-usage-memory-limiter
Aug 2, 2026
Merged

perf(daemon): throttle agent_usage emission with an in-memory limiter#2029
svarlamov merged 1 commit into
mainfrom
feat/agent-usage-memory-limiter

Conversation

@svarlamov

@svarlamov svarlamov commented Jul 29, 2026

Copy link
Copy Markdown
Member

Summary

  • should_emit_agent_usage acquired the global metrics-DB mutex and ran a SELECT + upsert sqlite transaction on every AI checkpoint, just to rate-limit agent_usage telemetry to one event per session per 150s.
  • Replace it with a bounded in-memory limiter: HashMap + expiry deque capped at 10,000 sessions, entries expiring after the emission interval, oldest-valid eviction when full. Sub-microsecond, no I/O, no contention with metrics delivery on the shared DB lock.
  • 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.

Test coverage

  • Limiter unit tests: interval suppression, bounded capacity with oldest-valid eviction, stale-entry expiry before capacity eviction, poisoned-lock fail-open.
  • Existing mock_ai throttle-bypass test retained.
  • Release build verified (the limiter is the non-test cfg path).

Ported from #1979 (closed) onto current main.

Depends on #2028.

🤖 Generated with Claude Code

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>

svarlamov commented Jul 29, 2026

Copy link
Copy Markdown
Member Author

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

Base automatically changed from feat/claude-skip-readonly-hooks to main July 29, 2026 16:03
@svarlamov
svarlamov merged commit 0b843ca into main Aug 2, 2026
32 checks passed
@svarlamov
svarlamov deleted the feat/agent-usage-memory-limiter branch August 2, 2026 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant