feat(ai-gateway): add project usage governance - #543
Open
dviejokfs wants to merge 2 commits into
Open
Conversation
Attribute deployment-token usage to projects and environments, and enforce shared model, rate, and monthly cost policies with atomic database-backed reservations.
📓 Changelog previewThis is what your commits will add to the generated ## [Unreleased]
### Added
- **ai-gateway:** Add project usage governance
### Fixed
- **ai-gateway:** Harden governance lifecycle |
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
Policies apply only from trusted deployment-token attribution. BYOK requests still obey allowlists and RPM limits but do not consume operator-funded monthly budgets. Internal control-plane AI calls remain outside this deployment-token HTTP boundary.
Closes #376
Evidence
AI governance lifecycle and concurrency (real PostgreSQL/TimescaleDB)
cargo test -p temps-ai-gateway --test governance_integration -- --nocaptureCoverage includes cross-instance token RPM enforcement, concurrent project budget reservation, atomic replacement of a reservation with actual usage, and durable conservative settlement after an unresolved stream exceeds the provider timeout window.
Gateway behavior and regression coverage
cargo test --lib -p temps-ai-gatewayIncludes trusted scope attribution, scoped usage filtering, token policies, cost conversion, fail-closed remote-image projection for budgeted calls, streaming usage forcing, and conflicting output-limit alias protection.
Migration applied on a fresh local database
The fresh database reports project_id, environment_id, deployment_id, deployment_token_id, and billing_period on ai_usage_logs, plus the governance rate-event and cost-reservation tables.
Migration rollback (real TimescaleDB)
cargo test -p temps-migrations --test migration_tests test_migration_down -- --exact --nocaptureRepository gates
cargo check --lib cargo clippy -p temps-ai-gateway --lib -- -D warnings cargo test --lib -p temps-entities -p temps-migrations git diff origin/main...HEAD --checkRust review: APPROVE. Migration/test review: APPROVE. Security re-review: APPROVE.