fix(budgets): drive BudgetIncidentCard recovery state from live policy summary

Pass the matching BudgetPolicySummary into BudgetIncidentCard from AgentBudgetsPanel and Costs so an incident originally opened below the cap still shows Raise budget & resume if usage later settles above the limit, and uses live observed/limit amounts.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip committed 2026-12-03 15:38:52 -06:00
1 parent 244439ebd7
commit c04fc96bfd
4 files changed
+63 -9

No files matched your search

+16 -8
View File
@@ -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({
<CardTitle className={isWarning ? "mt-1 text-base" : "mt-1 text-base text-red-950 dark:text-red-50"}>{incident.scopeName}</CardTitle>
<CardDescription className={isWarning ? "mt-1" : "mt-1 text-red-900/75 dark:text-red-100/70"}>
{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)}.`}
</CardDescription>
</div>
{isWarning ? (
@@ -121,7 +129,7 @@ export function BudgetIncidentCard({
/>
<Button
className="gap-2"
disabled={isMutating || parsed === null || parsed <= incident.amountObserved}
disabled={isMutating || parsed === null || parsed <= effectiveObserved}
onClick={() => {
if (typeof parsed === "number") onRaiseAndResume(parsed);
}}
@@ -130,7 +138,7 @@ export function BudgetIncidentCard({
{isMutating ? "Applying..." : isWarning ? "Raise budget" : "Raise budget & resume"}
</Button>
</div>
{parsed !== null && parsed <= incident.amountObserved ? (
{parsed !== null && parsed <= effectiveObserved ? (
<p className="mt-2 text-xs text-red-700 dark:text-red-200/80">
The new budget must exceed current observed spend.
</p>
+44 -1
View File
@@ -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(
<BudgetIncidentCard
incident={makeIncident({ amountObserved: 167, amountLimit: 100000 })}
policy={settledPolicy}
onRaiseAndResume={vi.fn()}
onKeepPaused={vi.fn()}
/>,
);
});
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");
});
});
+1
View File
@@ -87,6 +87,7 @@ export function AgentBudgetsPanel({ agent, companyId }: { agent: BudgetAgent; co
<div key={entry.id} className="space-y-2">
<BudgetIncidentCard
incident={entry}
policy={summary.policyId === entry.policyId ? summary : null}
isMutating={incident.isPending}
onKeepPaused={() => incident.mutate({ incidentId: entry.id, action: "keep_paused" })}
onRaiseAndResume={(amount) => incident.mutate({ incidentId: entry.id, action: "raise_budget_and_resume", amount })}
+2
View File
@@ -892,6 +892,7 @@ export function Costs({
<BudgetIncidentCard
key={incident.id}
incident={incident}
policy={budgetData?.policies.find((p) => 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({
<BudgetIncidentCard
key={incident.id}
incident={incident}
policy={budgetData?.policies.find((p) => p.policyId === incident.policyId) ?? null}
isMutating={incidentMutation.isPending}
onKeepPaused={() => incidentMutation.mutate({ incidentId: incident.id, action: "keep_paused" })}
onRaiseAndResume={(amount) =>