Skip to content

feat(agent): ユーザ単位の日次レート制限を導入(#521 / ADR-0023)#533

Merged
yusuke0610 merged 1 commit into
mainfrom
feat/agent-rate-limit
Jul 21, 2026
Merged

feat(agent): ユーザ単位の日次レート制限を導入(#521 / ADR-0023)#533
yusuke0610 merged 1 commit into
mainfrom
feat/agent-rate-limit

Conversation

@yusuke0610

Copy link
Copy Markdown
Owner

概要

機能整理ロードマップ #517 のサブ issue #521。ADR-0023(#520 / PR #532)の撤去順序に従い、プリペイド課金撤去(#522)で消える abuse 防止の代替を先行導入する。Agent の LLM を叩くエンドポイントにユーザ×日の回数上限を設ける。

⚠️ マージ順序: ADR-0023 を起票する #532 のマージ後にマージしてください。

実装

  • services/agent/rate_limit.py(決定論ロジック・mutmut 対象)を TDD(red→green→refactor)で実装。今日(JST)のカウントを原子的に増やし、AGENT_DAILY_LIMIT 超過で AgentRateLimitExceededError。limit=N なら N 回目まで許可・N+1 で拒否
  • models/agent_usage.py(AgentDailyUsage) + repositories/agent_rate_limit.py。ADR-0005 単一インスタンス前提の原子的 UPDATE(billing で実証済みパターンの再利用。IntegrityError は再取得で吸収)
  • マイグレーション 0050agent_daily_usage テーブル。down_revision = 0049_drop_blog_tables
  • /agent/chat/agent/resume-draft/run に適用(LLM を起動する側。/pdf はキャッシュ再レンダリングで LLM 非呼び出しのため対象外 — ユーザー確認済み)。超過で 429 + AGENT_DAILY_LIMIT_EXCEEDED(既存 AppErrorResponse 契約)
  • env AGENT_DAILY_LIMIT(既定 50) を env_keys / docker-compose / docs/api.md に同期
  • web: errorCodes.ts / errorMessages.tsAGENT_DAILY_LIMIT_EXCEEDED を追加

テスト

  • 単体 6(境界・上限超過・日次リセット(JST)・ユーザ独立・env 解決)+ 統合 1(429)。すべて実 SQLite(DB モックなし)
  • Tdd-Red: assert 0 == 1(enforce_daily_limit スタブが増分しない失敗を確認済み)

検証

  • make ci green(backend 650 passed / web 376 passed / ruff・pyright 0 / lint-env-keys・lint-adr-index・lint-tdd OK)
  • alembic チェーン: 0048 → 0049_drop_blog_tables → 0050_create_agent_daily_usage(単一 head。実 SQLite で確認)

受け入れ条件

  • make ci green
  • 上限到達で 429 + 日本語メッセージ(AGENT_DAILY_LIMIT_EXCEEDED
  • 既存のクレジット残高チェックとは独立に動作(本 PR では課金は触らない)

Closes #521

🤖 Generated with Claude Code

プリペイド課金撤去(#522)で消える abuse 防止の代替を先行導入する。Agent の
LLM を叩くエンドポイントにユーザ×日の回数上限を設ける。

- services/agent/rate_limit.py(決定論ロジック・mutmut 対象)を TDD で実装。
  今日(JST)のカウントを原子的に増やし、AGENT_DAILY_LIMIT 超過で
  AgentRateLimitExceededError。limit=N なら N 回目まで許可・N+1 で拒否
- models/agent_usage.py(AgentDailyUsage)+ repositories/agent_rate_limit.py。
  ADR-0005 単一インスタンス前提の原子的 UPDATE(billing で実証済みパターン)
- drop→create マイグレーション 0050(agent_daily_usage)
- /agent/chat・/agent/resume-draft/run に適用し、超過で 429 +
  AGENT_DAILY_LIMIT_EXCEEDED を返す(既存 AppErrorResponse 契約)
- env AGENT_DAILY_LIMIT(既定 50)を env_keys / docker-compose / docs/api.md に同期
- web: errorCodes.ts / errorMessages.ts に AGENT_DAILY_LIMIT_EXCEEDED を追加
- test: 単体 6(境界・超過・日次リセット・ユーザ独立・env 解決)+ 統合 1(429)

Tdd-Red: assert 0 == 1(enforce_daily_limit スタブが増分しない失敗を確認済み)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 21, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@yusuke0610, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 54 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 4f03f76c-81f5-4eed-8c27-0226f5628963

📥 Commits

Reviewing files that changed from the base of the PR and between 090daeb and 0ea9ba5.

📒 Files selected for processing (16)
  • backend/alembic_migrations/versions/0050_create_agent_daily_usage.py
  • backend/app/core/env_keys.py
  • backend/app/core/errors.py
  • backend/app/messages.json
  • backend/app/models/__init__.py
  • backend/app/models/agent_usage.py
  • backend/app/repositories/agent_rate_limit.py
  • backend/app/routers/agent.py
  • backend/app/services/agent/rate_limit.py
  • backend/pyproject.toml
  • backend/tests/test_agent.py
  • backend/tests/test_agent_rate_limit.py
  • docker-compose.yml
  • docs/api.md
  • web/src/constants/errorCodes.ts
  • web/src/constants/errorMessages.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/agent-rate-limit

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added agent DevForge Agent feature 新機能 documentation Improvements or additions to documentation backend バックエンド web フロントエンド (web) test テスト追加・修正 labels Jul 21, 2026
@yusuke0610
yusuke0610 merged commit 96bc72c into main Jul 21, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent DevForge Agent backend バックエンド documentation Improvements or additions to documentation feature 新機能 test テスト追加・修正 web フロントエンド (web)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Agent 整理 2/4] ユーザ単位レート制限の導入(課金撤去の前提)

1 participant