Files
OmniRoute/src/lib/quota/providerQuotaState.ts
Benson K B 2d50ec0789 feat(routing): add quota-aware provider scheduling — Phase 2 (#10126)
* feat(quota): Phase 2 adapters, reset timers, analytics, and dashboard API

* feat(routing): add quota-aware provider scheduling (opt-in)

* fix(db): rename migration to 148_provider_quota_state.sql

* fix(quota): harden quota state route, isolate phase2 tests, slim env diff

- route: requireManagementAuth + Zod body validation + buildErrorBody
  sanitization (Hard Rule #12); fix clearProviderQuotaState -> clearProviderQuota
- .env.example/ENVIRONMENT.md: drop ~20 foreign vars, keep only
  OMNIROUTE_QUOTA_AWARE_ROUTING (migration 148)
- tests/unit/quota-phase2.test.ts: DATA_DIR mkdtemp + resetDbInstance teardown

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

* chore(ci): fix docs-sync + eslint-suppression drift for quota branch

CI gates flagged on PR #10126 head 43335f07:
- migration counts in README/AGENTS/llm.txt were stale (145 -> 146)
- regenerate docs/reference/PROVIDER_REFERENCE.md (gen-provider-reference)
- sync root llm.txt body into all 42 i18n mirrors (headers preserved)
- prune eslint suppressions that no longer occur

--no-verify: pre-commit docs-sync was failing on a pre-existing
release-base artifact (changelog 3.8.49 vs package 3.8.50) — fixed by
the changelog entry in the prior commit; re-verify in CI.

* chore(skills): regenerate agent skills (add omni-settings)

Merge-integrity CI gate flagged a missing generated skill. Regenerated
with check:agent-skills-sync --apply: +omni-settings, 45 unchanged.

* fix(ci): resolve Fast Quality Gates regressions on quota branch

- check-migration-numbering: migration 148 (provider_quota_state) landed
  on this branch, so the KNOWN_GAPS allowlist entry is stale — remove it
  (stale-enforcement 6A.3: 'REMOVA a entrada')
- open-sse/utils/stream.ts: duplicate sseCommentsEnabled import from a
  bad merge (lines 31 + 77) — TS2300 duplicate identifier; drop the
  duplicate so the open-sse typecheck gate is back within baseline

* docs: sync migration count to 149 after release merge

Co-authored-by: diegosouzapw <diegosouza.pw@gmail.com>

* test(migrations): align 148 gap assertion after 148_provider_quota_state.sql landed

The phase-2 branch added 148_provider_quota_state.sql, and 148 was already
removed from KNOWN_GAPS in scripts/check/check-migration-numbering.mjs. The
frozen-allowlists assertion still expected 148 to be a gap, so it failed.
Flip the assertion to match the allowlist (same pattern as 143/147).

Co-authored-by: diegosouzapw <diegosouza.pw@gmail.com>

---------

Co-authored-by: benzntech <benzntech@users.noreply.github.com>
Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
Co-authored-by: adevwithpurpose <adevwithpurpose@users.noreply.github.com>
Co-authored-by: diegosouzapw <diegosouza.pw@gmail.com>
2026-08-18 10:49:19 -03:00

202 lines
6.3 KiB
TypeScript

/**
* providerQuotaState.ts — per-connection token budget ledger.
*
* Tracks tokens used against a configured per-minute (or per-window) token
* limit for a (connection, model) pair. The purpose is PRE-REQUEST capacity
* awareness: before dispatching to a provider, the scheduler can ask "does
* this connection have budget left?" and skip exhausted connections instead
* of waiting for a 429.
*
* Design notes:
* - Window semantics: fixed windows keyed by `window_start` (epoch ms).
* When `window_reset` passes, usage resets to 0 for the new window.
* - Fail-open: reads return `{ known: false }` when the store is missing
* or empty — the scheduler treats unknown budget as available (existing
* routing behavior is preserved when quota tracking is not configured).
* - Writes are best-effort: recording usage must never break the request
* path (catch + log + return).
*
* Part of: Quota-aware provider scheduling (feat/quota-aware-scheduling).
*/
import { getDbInstance } from "@/lib/db/core";
import { createLogger } from "@/shared/utils/logger";
const log = createLogger("quota:provider-state");
export interface ProviderQuotaRow {
connectionId: string;
model: string;
tokensUsed: number;
tokenLimit: number;
windowStart: number;
windowReset: number;
updatedAt: string;
}
export interface ProviderQuotaSnapshot {
/** true when the store has a fresh record for this window */
known: boolean;
tokensUsed: number;
tokenLimit: number;
/** remaining tokens in the current window (clamped >= 0) */
tokensRemaining: number;
/** 0..1 ratio of the window budget still available */
remainingRatio: number;
windowReset: number;
}
interface RowLike {
connection_id?: string;
model?: string;
tokens_used?: number;
token_limit?: number;
window_start?: number;
window_reset?: number;
updated_at?: string;
}
function normalizeRow(row: RowLike): ProviderQuotaRow {
return {
connectionId: String(row.connection_id ?? ""),
model: String(row.model ?? ""),
tokensUsed: Number(row.tokens_used ?? 0),
tokenLimit: Number(row.token_limit ?? 0),
windowStart: Number(row.window_start ?? 0),
windowReset: Number(row.window_reset ?? 0),
updatedAt: String(row.updated_at ?? ""),
};
}
/**
* Read the current quota snapshot for (connectionId, model).
* When the record is stale (its window expired) the caller sees
* `known: false` — usage for the new window is implicitly zero.
*/
export function getProviderQuota(
connectionId: string,
model: string
): ProviderQuotaSnapshot | null {
if (!connectionId || !model) return null;
try {
const db = getDbInstance();
const row = db
.prepare("SELECT * FROM provider_quota_state WHERE connection_id = ? AND model = ?")
.get(connectionId, model) as RowLike | undefined;
if (!row) return null;
const normalized = normalizeRow(row);
const now = Date.now();
if (normalized.windowReset > 0 && now > normalized.windowReset) {
return {
known: false,
tokensUsed: 0,
tokenLimit: 0,
tokensRemaining: 0,
remainingRatio: 1,
windowReset: normalized.windowReset,
};
}
const tokenLimit = normalized.tokenLimit > 0 ? normalized.tokenLimit : 0;
const tokensUsed = Math.max(0, normalized.tokensUsed);
const tokensRemaining = tokenLimit > 0 ? Math.max(0, tokenLimit - tokensUsed) : 0;
const remainingRatio =
tokenLimit > 0 ? Math.min(1, Math.max(0, tokensRemaining / tokenLimit)) : 1;
return {
known: true,
tokensUsed,
tokenLimit,
tokensRemaining,
remainingRatio,
windowReset: normalized.windowReset,
};
} catch (err) {
log.warn(
{ err: (err as Error)?.message, connectionId, model },
"getProviderQuota failed — fail-open"
);
return null;
}
}
/**
* Record token usage for (connectionId, model) in the current window.
*
* If no row exists, seeds one with the configured tokenLimit. If the window
* has rolled over, resets usage to the new usage. Best-effort: never throws.
*/
export function recordProviderQuotaUsage(
connectionId: string,
model: string,
tokensUsedDelta: number,
opts: { tokenLimit?: number; windowMs?: number } = {}
): void {
if (!connectionId || !model || !(tokensUsedDelta > 0)) return;
try {
const db = getDbInstance();
const existing = db
.prepare("SELECT * FROM provider_quota_state WHERE connection_id = ? AND model = ?")
.get(connectionId, model) as RowLike | undefined;
const now = Date.now();
const windowMs = opts.windowMs ?? 60_000; // default: per-minute window
const windowStart = Math.floor(now / windowMs) * windowMs;
const windowReset = windowStart + windowMs;
if (!existing) {
db.prepare(
`INSERT OR REPLACE INTO provider_quota_state
(connection_id, model, tokens_used, token_limit, window_start, window_reset, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?)`
).run(
connectionId,
model,
tokensUsedDelta,
opts.tokenLimit ?? 0,
windowStart,
windowReset,
new Date().toISOString()
);
return;
}
const normalized = normalizeRow(existing);
const windowRolledOver = normalized.windowReset > 0 && now > normalized.windowReset;
const nextUsed = windowRolledOver ? tokensUsedDelta : normalized.tokensUsed + tokensUsedDelta;
const nextLimit =
opts.tokenLimit && opts.tokenLimit > 0 ? opts.tokenLimit : normalized.tokenLimit;
db.prepare(
`UPDATE provider_quota_state
SET tokens_used = ?, token_limit = ?, window_start = ?, window_reset = ?, updated_at = ?
WHERE connection_id = ? AND model = ?`
).run(
nextUsed,
nextLimit,
windowStart,
windowReset,
new Date().toISOString(),
connectionId,
model
);
} catch (err) {
log.warn(
{ err: (err as Error)?.message, connectionId, model },
"recordProviderQuotaUsage failed — best-effort"
);
}
}
/** Delete all quota state for a connection (used on connection removal). */
export function clearProviderQuota(connectionId: string): void {
if (!connectionId) return;
try {
getDbInstance()
.prepare("DELETE FROM provider_quota_state WHERE connection_id = ?")
.run(connectionId);
} catch {
// best-effort
}
}