chore: revert agent subscription usage in settings and chat tooltip (#1940)
* chore: revert agent subscription usage in settings and chat tooltip
Reverts #1697 (55c8b461b). The subscription usage plugin now covers this,
so the built-in Settings > Agents section and the context-doughnut tooltip
rows are no longer needed.
Conflicts resolved to preserve later work on the same files:
- token-usage-display.tsx/.test.tsx: kept the pinnable tooltip (#1869),
the rebuilt context-window layout (#1741), and the ACP/API source row
(#1752); removed only SessionUsageRows and its imports.
- settings/agents/page.tsx: #1741 moved <AgentUsageSection /> above the
pre-existing <Separator />, so the revert missed the JSX; removed it
and kept the separator.
- agent-usage-section.tsx/.test.tsx: deleted (later changes were cosmetic
tweaks to the section itself).
Office-side subscription surfaces (dashboard quota card, agent detail
QuotaCard, internal/agent/usage clients) predate #1697 and are untouched.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* fix(usage): keep #1697 client fixes when reverting its UI surface
#1697 bundled two things: the Settings > Agents section plus doughnut
tooltip rows, and a set of genuine bug fixes to the underlying provider
usage clients. Reverting it wholesale threw away the second group, which
is still live code behind the office dashboard quota card and the agent
detail QuotaCard.
Restores the client layer to its post-#1697 state (billing.go, cache.go,
client_claude.go, client_codex.go, fileutil.go, types.go and the two
client tests), fixing the defects CodeRabbit flagged on #1940:
- billing type is recomputed per call again instead of being frozen for
the process lifetime by sync.OnceValue, so login/logout is observed
without a backend restart
- usage cache regains fetch coalescing and short negative caching, so a
provider outage no longer means one outbound call per request
- Claude usage parses float utilization and the richer limits[] array
instead of failing to decode
- Claude token refresh patches only claudeAiOauth in a decoded map and
writes atomically via temp+rename, preserving unknown keys in the
credentials file the real CLI shares
- Codex reads tokens.access_token rather than a nonexistent flat token
field, and its doc comment matches the ~/.codex/auth.json path
The removed UI/API surface stays reverted: GET /api/v1/agents/usage, the
host usage lister, the settings section, and the tooltip rows.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,17 +1,12 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { cleanup, fireEvent, render } from "@testing-library/react";
|
||||
import { useSessionContextWindow } from "@/hooks/domains/session/use-session-context-window";
|
||||
import { useSessionAgentUsage } from "@/hooks/domains/session/use-session-agent-usage";
|
||||
import { isContextWindowReliable, TokenUsageDisplay } from "./token-usage-display";
|
||||
|
||||
vi.mock("@/hooks/domains/session/use-session-context-window", () => ({
|
||||
useSessionContextWindow: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/hooks/domains/session/use-session-agent-usage", () => ({
|
||||
useSessionAgentUsage: vi.fn(() => null),
|
||||
}));
|
||||
|
||||
vi.mock("@kandev/ui/tooltip", () => ({
|
||||
TooltipProvider: ({ children }: { children: React.ReactNode }) => <>{children}</>,
|
||||
Tooltip: ({ children, open }: { children: React.ReactNode; open?: boolean }) => (
|
||||
@@ -94,74 +89,6 @@ describe("TokenUsageDisplay", () => {
|
||||
|
||||
expect(getByTestId("tooltip-root").getAttribute("data-open")).toBe("false");
|
||||
});
|
||||
|
||||
it("shows subscription usage rows in the tooltip when the agent has them", () => {
|
||||
vi.mocked(useSessionContextWindow).mockReturnValue({
|
||||
size: 200_000,
|
||||
used: 56_047,
|
||||
remaining: 143_953,
|
||||
efficiency: 28,
|
||||
});
|
||||
vi.mocked(useSessionAgentUsage).mockReturnValue({
|
||||
agent_id: "claude-acp",
|
||||
display_name: "Claude",
|
||||
usage: {
|
||||
provider: "anthropic",
|
||||
plan: "max",
|
||||
windows: [
|
||||
{
|
||||
label: "5-hour",
|
||||
utilization_pct: 86,
|
||||
reset_at: new Date(Date.now() + 3 * 3_600_000).toISOString(),
|
||||
},
|
||||
{
|
||||
label: "7-day",
|
||||
utilization_pct: 19,
|
||||
reset_at: new Date(Date.now() + 30 * 3_600_000).toISOString(),
|
||||
},
|
||||
],
|
||||
fetched_at: new Date().toISOString(),
|
||||
},
|
||||
});
|
||||
|
||||
const { container, getByRole, getByTestId, getByText } = render(
|
||||
<TokenUsageDisplay sessionId="sess-1" />,
|
||||
);
|
||||
|
||||
expect(container.querySelector('[data-testid="doughnut-subscription-usage"]')).not.toBeNull();
|
||||
expect(getByRole("button", { name: "Context window: 28% used" })).toBeDefined();
|
||||
expect(getByText("Context window")).toBeDefined();
|
||||
expect(getByText("56.0K of 200.0K tokens")).toBeDefined();
|
||||
expect(getByText(/Subscription · max/i)).toBeDefined();
|
||||
expect(
|
||||
getByTestId("context-window-usage").compareDocumentPosition(
|
||||
getByTestId("doughnut-subscription-usage"),
|
||||
) & Node.DOCUMENT_POSITION_FOLLOWING,
|
||||
).toBeTruthy();
|
||||
// Worst window is 86% → provider status "High".
|
||||
expect(getByText("High")).toBeDefined();
|
||||
expect(getByText("5h")).toBeDefined();
|
||||
expect(getByText("86%")).toBeDefined();
|
||||
expect(getByText("7d")).toBeDefined();
|
||||
// Reset countdown column (≈3h out) and colored bars per window.
|
||||
expect(getByText(/^(3h 0m|2h 59m)$/)).toBeDefined();
|
||||
expect(container.querySelector(".bg-amber-500")).not.toBeNull(); // 86% bar
|
||||
expect(container.querySelector(".bg-emerald-500")).not.toBeNull(); // 19% bar
|
||||
});
|
||||
|
||||
it("omits the subscription block when the agent has no usage", () => {
|
||||
vi.mocked(useSessionContextWindow).mockReturnValue({
|
||||
size: 200_000,
|
||||
used: 56_047,
|
||||
remaining: 143_953,
|
||||
efficiency: 28,
|
||||
});
|
||||
vi.mocked(useSessionAgentUsage).mockReturnValue(null);
|
||||
|
||||
const { container } = render(<TokenUsageDisplay sessionId="sess-1" />);
|
||||
|
||||
expect(container.querySelector('[data-testid="doughnut-subscription-usage"]')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("TokenUsageDisplay context source", () => {
|
||||
|
||||
@@ -4,9 +4,7 @@ import { memo, useEffect, useId, useRef, useState } from "react";
|
||||
import { IconInfoCircle } from "@tabler/icons-react";
|
||||
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@kandev/ui/tooltip";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { UsageWindowRows, usageStatus } from "@/components/usage/usage-window-rows";
|
||||
import { useSessionContextWindow } from "@/hooks/domains/session/use-session-context-window";
|
||||
import { useSessionAgentUsage } from "@/hooks/domains/session/use-session-agent-usage";
|
||||
|
||||
type TokenUsageDisplayProps = {
|
||||
sessionId: string | null;
|
||||
@@ -153,33 +151,6 @@ function ContextWindowSource({ source }: { source: "acp" | "api" | undefined })
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscription utilization rows for the session's agent, rendered inside the
|
||||
* doughnut tooltip. Mounted only while the tooltip is open, so each hover
|
||||
* triggers a fresh provider fetch (server-clamped to one per 15 s).
|
||||
*/
|
||||
function SessionUsageRows({ sessionId }: { sessionId: string | null }) {
|
||||
const agentUsage = useSessionAgentUsage(sessionId);
|
||||
const usage = agentUsage?.usage;
|
||||
if (!usage || usage.windows.length === 0) return null;
|
||||
const status = usageStatus(usage);
|
||||
|
||||
return (
|
||||
<div
|
||||
className="pt-2 mt-2 border-t border-border/60 space-y-2 min-w-64 opacity-80"
|
||||
data-testid="doughnut-subscription-usage"
|
||||
>
|
||||
<div className="flex items-center justify-between gap-4">
|
||||
<span className="text-[10px] font-medium uppercase text-muted-foreground">
|
||||
Subscription{usage.plan ? ` · ${usage.plan}` : ""}
|
||||
</span>
|
||||
<span className={cn("text-[10px] font-medium", status.className)}>{status.label}</span>
|
||||
</div>
|
||||
<UsageWindowRows usage={usage} className="text-[11px]" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export const TokenUsageDisplay = memo(function TokenUsageDisplay({
|
||||
sessionId,
|
||||
className,
|
||||
@@ -248,7 +219,6 @@ export const TokenUsageDisplay = memo(function TokenUsageDisplay({
|
||||
<ContextWindowSource source={source} />
|
||||
</div>
|
||||
</div>
|
||||
<SessionUsageRows sessionId={sessionId} />
|
||||
</div>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
|
||||
Reference in New Issue
Block a user