Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(gateway): hold a budget's limit and period to the server's bounds

Petr Bauchf43f705a7801
diff --git a/gateway/src/data/api.ts b/gateway/src/data/api.ts
index dede25312..e00379510 100644
--- a/gateway/src/data/api.ts
+++ b/gateway/src/data/api.ts
@@ -1744,6 +1744,11 @@ export interface UsageTotal {
amount: number;
}
+// The server's bounds on a budget: at most a leap year, and at most the largest
+// integer a JavaScript number holds exactly.
+export const MAX_BUDGET_PERIOD_HOURS = 8784;
+export const MAX_BUDGET_AMOUNT = Number.MAX_SAFE_INTEGER;
+
export interface Budget {
id: string;
// Null covers every agent in the workspace.
diff --git a/gateway/src/pages/usage/BudgetDialog.tsx b/gateway/src/pages/usage/BudgetDialog.tsx
index d39847c87..f4962ed27 100644
--- a/gateway/src/pages/usage/BudgetDialog.tsx
+++ b/gateway/src/pages/usage/BudgetDialog.tsx
@@ -14,13 +14,15 @@ import { useCallback, useEffect, useState } from "react";
import {
type Budget,
type UsageMetric,
+ MAX_BUDGET_AMOUNT,
+ MAX_BUDGET_PERIOD_HOURS,
PER_MODEL_METRICS,
USAGE_METRICS,
createBudget,
updateBudget,
} from "../../data/api";
import { useAgents } from "../../data/hooks";
-import { metricLabel } from "./usageFormat";
+import { formatAmount, metricLabel, parseBound } from "./usageFormat";
const WHOLE_WORKSPACE = "";
@@ -54,15 +56,13 @@ export default function BudgetDialog({ tenantId, budget, open, onClose, onSaved
}, [open, budget]);
const perModel = PER_MODEL_METRICS.includes(metric);
- const limitValue = Number(limit);
- const periodValue = Number(periodHours);
- const valid =
- Number.isInteger(limitValue) &&
- limitValue > 0 &&
- Number.isInteger(periodValue) &&
- periodValue > 0;
+ const limitValue = parseBound(limit, MAX_BUDGET_AMOUNT);
+ const periodValue = parseBound(periodHours, MAX_BUDGET_PERIOD_HOURS);
+ const limitInvalid = limit.trim() !== "" && limitValue === null;
+ const periodInvalid = periodHours.trim() !== "" && periodValue === null;
const handleSubmit = useCallback(async () => {
+ if (limitValue === null || periodValue === null) return;
setSubmitting(true);
setError(null);
try {
@@ -154,7 +154,9 @@ export default function BudgetDialog({ tenantId, budget, open, onClose, onSaved
onChange={(e) => setLimit(e.target.value)}
required
fullWidth
- slotProps={{ htmlInput: { min: 1, step: 1 } }}
+ error={limitInvalid}
+ helperText={limitInvalid ? `A whole number from 1 to ${formatAmount(MAX_BUDGET_AMOUNT)}.` : undefined}
+ slotProps={{ htmlInput: { min: 1, max: MAX_BUDGET_AMOUNT, step: 1 } }}
/>
<TextField
label="Period (hours)"
@@ -163,8 +165,13 @@ export default function BudgetDialog({ tenantId, budget, open, onClose, onSaved
onChange={(e) => setPeriodHours(e.target.value)}
required
fullWidth
- helperText="24 is a day, 168 a week."
- slotProps={{ htmlInput: { min: 1, step: 1 } }}
+ error={periodInvalid}
+ helperText={
+ periodInvalid
+ ? `A whole number of hours from 1 to ${formatAmount(MAX_BUDGET_PERIOD_HOURS)}.`
+ : "24 is a day, 168 a week."
+ }
+ slotProps={{ htmlInput: { min: 1, max: MAX_BUDGET_PERIOD_HOURS, step: 1 } }}
/>
</Stack>
</Stack>
@@ -174,7 +181,7 @@ export default function BudgetDialog({ tenantId, budget, open, onClose, onSaved
<Button
variant="contained"
onClick={handleSubmit}
- disabled={submitting || !valid}
+ disabled={submitting || limitValue === null || periodValue === null}
startIcon={submitting ? <CircularProgress size={16} /> : undefined}
>
{budget ? "Save" : "Add"}
diff --git a/gateway/src/pages/usage/usageFormat.ts b/gateway/src/pages/usage/usageFormat.ts
index 3ffc678c1..49b8fabf2 100644
--- a/gateway/src/pages/usage/usageFormat.ts
+++ b/gateway/src/pages/usage/usageFormat.ts
@@ -9,6 +9,13 @@ const METRIC_LABELS: Record<UsageMetric, string> = {
cache_write_tokens: "Cache write tokens",
};
+/** A whole number from 1 to `max` typed as digits only, or null. */
+export function parseBound(raw: string, max: number): number | null {
+ if (!/^\d+$/.test(raw.trim())) return null;
+ const value = Number(raw.trim());
+ return value >= 1 && value <= max ? value : null;
+}
+
export function metricLabel(metric: UsageMetric): string {
return METRIC_LABELS[metric];
}