mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-18 21:02:50 +03:00
* feat(api): add provider quota telemetry, adaptive routing, and status inventory Adds a read-only OmniRoute status/inventory surface plus supporting resilience and usage-tracking infrastructure: - src/lib/quota/providerQuotaTelemetry.ts, providerCapabilities.ts: provider quota state and capability signals, sourced from configured metadata rather than invented values; unknown stays unknown. - src/lib/resilience/adaptiveCircuit.ts, failureClassification.ts: circuit state with lazy recovery and explicit failure classification. - src/lib/usage/usageLedger.ts, budgetGuard.ts, modelPricingRegistry.ts: internal usage tracking and budget allow/warn/deny decisions, kept separate from upstream-reported quota (never conflated). - src/lib/routing/adaptiveRouting.ts: excludes exhausted-quota and open-circuit candidates from routing, penalizes approaching-limit. - src/lib/omnirouteStatus.ts + src/app/api/omniroute/status, route/preview: read-only status endpoint; never issues a live upstream model request (asserted via liveRequestExecuted: false). - src/lib/db/quotaPools.ts: adds ensurePool() for idempotent pool management by automation/CLI callers, following the existing group-demo default-group convention. - scripts/omniroute-verify.mjs (+ omniroute:verify script): local verification against the running gateway. 9 new unit tests, all passing. typecheck:core clean relative to base (release/v3.8.50) -- the 2 pre-existing gateways.ts errors are tracked separately in #9985 and untouched by this change. * test(cli): align cli-machine-token assertions with HMAC-SHA256 64-char format The quota-telemetry feature hardens cliToken to HMAC-SHA256(machineId, SALT) (64-char hex, pristine machine id). Update the regression test to the new format and mirror the production derivation in the different-machine-id check. Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com> --------- Co-authored-by: adevwithpurpose <adevwithpurpose@users.noreply.github.com> Co-authored-by: desamours-hub <desamours-hub@users.noreply.github.com> Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
78 lines
2.7 KiB
TypeScript
78 lines
2.7 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
|
|
import { observeCircuit, createAdaptiveCircuit } from "@/lib/resilience/adaptiveCircuit";
|
|
import { evaluateBudget } from "@/lib/usage/budgetGuard";
|
|
import { ModelPricingRegistry } from "@/lib/usage/modelPricingRegistry";
|
|
import { createUsageRecord, summarizeUsage } from "@/lib/usage/usageLedger";
|
|
|
|
test("adaptive circuit opens, probes, and closes after recovery", () => {
|
|
const now = new Date("2026-01-01T00:00:00.000Z");
|
|
let circuit = createAdaptiveCircuit();
|
|
circuit = observeCircuit(circuit, "failure", {
|
|
now,
|
|
failureThreshold: 2,
|
|
cooldownMs: 1000,
|
|
reason: "timeout",
|
|
});
|
|
circuit = observeCircuit(circuit, "failure", {
|
|
now: new Date(now.getTime() + 10),
|
|
failureThreshold: 2,
|
|
cooldownMs: 1000,
|
|
reason: "timeout",
|
|
});
|
|
assert.equal(circuit.state, "open");
|
|
circuit = observeCircuit(circuit, "probe", { now: new Date(now.getTime() + 1011) });
|
|
assert.equal(circuit.state, "half_open");
|
|
circuit = observeCircuit(circuit, "success", { now: new Date(now.getTime() + 1002) });
|
|
assert.equal(circuit.state, "closed");
|
|
assert.equal(circuit.failureCount, 0);
|
|
});
|
|
|
|
test("internal budget returns allow, warn, and deny without upstream quota claims", () => {
|
|
const limit = {
|
|
id: "b",
|
|
scope: "global" as const,
|
|
period: "daily" as const,
|
|
limitType: "currency" as const,
|
|
limitValue: 10,
|
|
warningThreshold: 0.75,
|
|
enabled: true,
|
|
};
|
|
assert.equal(evaluateBudget(limit, { currency: 2, tokens: 0, requests: 0 }).decision, "allow");
|
|
assert.equal(evaluateBudget(limit, { currency: 8, tokens: 0, requests: 0 }).decision, "warn");
|
|
assert.equal(evaluateBudget(limit, { currency: 10, tokens: 0, requests: 0 }).decision, "deny");
|
|
});
|
|
|
|
test("unknown pricing remains unknown while configured pricing is estimated", () => {
|
|
const registry = new ModelPricingRegistry();
|
|
assert.equal(registry.estimate("codex", "unknown", 1000, 1000), undefined);
|
|
registry.set({
|
|
providerId: "codex",
|
|
modelId: "gpt-5",
|
|
inputPricePerMillionTokens: 1,
|
|
outputPricePerMillionTokens: 2,
|
|
source: "admin",
|
|
});
|
|
assert.equal(registry.estimate("codex", "gpt-5", 1000, 1000), 0.003);
|
|
const record = createUsageRecord(
|
|
{
|
|
id: "r1",
|
|
providerId: "codex",
|
|
modelId: "gpt-5",
|
|
inputTokens: 100,
|
|
outputTokens: 50,
|
|
latencyMs: 10,
|
|
status: "success",
|
|
createdAt: "2026-01-01T00:00:00.000Z",
|
|
},
|
|
registry
|
|
);
|
|
assert.equal(record.totalTokens, 150);
|
|
assert.equal(record.estimatedCostUsd, 0.0002);
|
|
const summary = summarizeUsage([record]);
|
|
assert.equal(summary.requests, 1);
|
|
assert.equal(summary.successes, 1);
|
|
assert.equal(summary.estimatedCostUsd, 0.0002);
|
|
});
|