diff --git a/ui/src/components/BudgetIncidentCard.tsx b/ui/src/components/BudgetIncidentCard.tsx index 8a1146b5dae..f38474ae6c5 100644 --- a/ui/src/components/BudgetIncidentCard.tsx +++ b/ui/src/components/BudgetIncidentCard.tsx @@ -1,5 +1,5 @@ import { useState } from "react"; -import type { BudgetIncident } from "@paperclipai/shared"; +import type { BudgetIncident, BudgetPolicySummary } from "@paperclipai/shared"; import { AlertOctagon, AlertTriangle, ArrowUpRight, PauseCircle } from "lucide-react"; import { formatCents } from "../lib/utils"; import { Badge } from "@/components/ui/badge"; @@ -27,22 +27,30 @@ function incidentStateLabel(incident: BudgetIncident) { export function BudgetIncidentCard({ incident, + policy, onRaiseAndResume, onKeepPaused, isMutating, }: { incident: BudgetIncident; + policy?: BudgetPolicySummary | null; onRaiseAndResume: (amountCents: number) => void; onKeepPaused: () => void; isMutating?: boolean; }) { const isWarning = incident.thresholdType === "soft"; + const effectiveObserved = policy ? policy.observedAmount : incident.amountObserved; + const effectiveLimit = policy ? policy.amount : incident.amountLimit; const isIncompleteAccountingHardStop = - incident.thresholdType === "hard" && incident.amountObserved < incident.amountLimit; + incident.thresholdType === "hard" && + (policy + ? policy.unpricedUsagePolicy !== "allow" && + (policy.unpricedEventCount > 0 || (policy.status === "hard_stop" && policy.observedAmount < policy.amount)) + : incident.amountObserved < incident.amountLimit); const [draftAmount, setDraftAmount] = useState( centsInputValue(isWarning - ? Math.max(incident.amountObserved, incident.amountLimit) + 1000 - : Math.max(incident.amountObserved + 1000, incident.amountLimit)), + ? Math.max(effectiveObserved, effectiveLimit) + 1000 + : Math.max(effectiveObserved + 1000, effectiveLimit)), ); const parsed = parseDollarInput(draftAmount); const stateLabel = incidentStateLabel(incident); @@ -65,8 +73,8 @@ export function BudgetIncidentCard({ {incident.scopeName} {isIncompleteAccountingHardStop - ? `Known spend is ${formatCents(incident.amountObserved)} of ${formatCents(incident.amountLimit)}, but accounting is incomplete (unpriced or unaccounted usage) and this policy blocks incomplete accounting.` - : `Spending reached ${formatCents(incident.amountObserved)} against a limit of ${formatCents(incident.amountLimit)}.`} + ? `Known spend is ${formatCents(effectiveObserved)} of ${formatCents(effectiveLimit)}, but accounting is incomplete (unpriced or unaccounted usage) and this policy blocks incomplete accounting.` + : `Spending reached ${formatCents(effectiveObserved)} against a limit of ${formatCents(effectiveLimit)}.`} {isWarning ? ( @@ -121,7 +129,7 @@ export function BudgetIncidentCard({ /> - {parsed !== null && parsed <= incident.amountObserved ? ( + {parsed !== null && parsed <= effectiveObserved ? (

The new budget must exceed current observed spend.

diff --git a/ui/src/components/PausedBudgetNotices.test.tsx b/ui/src/components/PausedBudgetNotices.test.tsx index 54af74666a2..56ac56f2004 100644 --- a/ui/src/components/PausedBudgetNotices.test.tsx +++ b/ui/src/components/PausedBudgetNotices.test.tsx @@ -3,7 +3,7 @@ import { act } from "react"; import { createRoot, type Root } from "react-dom/client"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -import type { Agent, BudgetIncident } from "@paperclipai/shared"; +import type { Agent, BudgetIncident, BudgetPolicySummary } from "@paperclipai/shared"; import { MemoryRouter } from "@/lib/router"; import { IssueAssigneePausedNotice } from "./IssueAssigneePausedNotice"; import { BudgetIncidentCard } from "./BudgetIncidentCard"; @@ -136,4 +136,47 @@ describe("IssueAssigneePausedNotice and BudgetIncidentCard", () => { expect(container.textContent).toContain("Keeping paused dismisses this alert without resuming work."); expect(container.textContent).not.toContain("Raise budget & resume"); }); + + it("restores Raise budget & resume when live policy state shows usage settled above the limit", () => { + const settledPolicy: BudgetPolicySummary = { + policyId: "policy-1", + companyId: "company-1", + scopeType: "agent", + scopeId: "agent-dorian", + scopeName: "Dorian", + metric: "billed_cents", + windowKind: "calendar_month_utc", + amount: 100000, + observedAmount: 120000, + unpricedEventCount: 0, + pendingRunCount: 0, + remainingAmount: 0, + utilizationPercent: 120, + warnPercent: 80, + hardStopEnabled: true, + unpricedUsagePolicy: "block", + notifyEnabled: true, + isActive: true, + status: "hard_stop", + paused: true, + pauseReason: "budget", + windowStart: new Date("2026-12-01T00:00:00Z"), + windowEnd: new Date("2027-01-01T00:00:00Z"), + }; + + act(() => { + root.render( + , + ); + }); + + expect(container.textContent).toContain("Spending reached $1,200.00 against a limit of $1,000.00."); + expect(container.textContent).toContain("Raise budget & resume"); + expect(container.textContent).not.toContain("incomplete accounting stop"); + }); }); diff --git a/ui/src/pages/AgentBudgetsPanel.tsx b/ui/src/pages/AgentBudgetsPanel.tsx index 8b405dd9d4c..384ea563326 100644 --- a/ui/src/pages/AgentBudgetsPanel.tsx +++ b/ui/src/pages/AgentBudgetsPanel.tsx @@ -87,6 +87,7 @@ export function AgentBudgetsPanel({ agent, companyId }: { agent: BudgetAgent; co
incident.mutate({ incidentId: entry.id, action: "keep_paused" })} onRaiseAndResume={(amount) => incident.mutate({ incidentId: entry.id, action: "raise_budget_and_resume", amount })} diff --git a/ui/src/pages/Costs.tsx b/ui/src/pages/Costs.tsx index f3ca4c24bcf..982b441233e 100644 --- a/ui/src/pages/Costs.tsx +++ b/ui/src/pages/Costs.tsx @@ -892,6 +892,7 @@ export function Costs({ p.policyId === incident.policyId) ?? null} isMutating={incidentMutation.isPending} onKeepPaused={() => incidentMutation.mutate({ incidentId: incident.id, action: "keep_paused" })} onRaiseAndResume={(amount) => @@ -976,6 +977,7 @@ export function Costs({ p.policyId === incident.policyId) ?? null} isMutating={incidentMutation.isPending} onKeepPaused={() => incidentMutation.mutate({ incidentId: incident.id, action: "keep_paused" })} onRaiseAndResume={(amount) =>