Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 87 additions & 9 deletions apps/desktop-tauri/src-tauri/src/commands/usage_spend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ pub struct UsageSpendRow {
pub display_name: String,
pub seven_day: Option<f64>,
pub thirty_day: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub seven_day_estimate: Option<codexbar::spend_contract::LocalCostEstimate>,
#[serde(skip_serializing_if = "Option::is_none")]
pub thirty_day_estimate: Option<codexbar::spend_contract::LocalCostEstimate>,
pub seven_day_tokens: Option<u64>,
pub thirty_day_tokens: Option<u64>,
pub currency: String,
Expand Down Expand Up @@ -496,6 +500,7 @@ fn build_usage_spend_summary(
})
.unwrap_or_else(|| provider_id.clone());

let mut local_cost_estimates = None;
let spend = match provider_id.as_str() {
"codex" => SpendValues {
seven_day: codex_7_contract.known_cost_usd,
Expand Down Expand Up @@ -586,17 +591,11 @@ fn build_usage_spend_summary(
spend
}
"antigravity" => {
use codexbar::providers::antigravity::local_sessions::LocalHistoryCoverage;
let seven = codexbar::providers::antigravity::local_sessions::summarize(7);
let thirty = codexbar::providers::antigravity::local_sessions::summarize(30);
let mut spend = cached_spend(cached_snapshot);
spend.seven_day_tokens = matches!(seven.coverage, LocalHistoryCoverage::Complete)
.then_some(seven.total_tokens);
spend.thirty_day_tokens = matches!(thirty.coverage, LocalHistoryCoverage::Complete)
.then_some(thirty.total_tokens);
if matches!(thirty.coverage, LocalHistoryCoverage::Complete) {
spend.source = "local Antigravity history".to_string();
}
let spend =
antigravity_spend_values(cached_spend(cached_snapshot), &seven, &thirty);
local_cost_estimates = Some((seven.cost_estimate, thirty.cost_estimate));
spend
}
_ => cached_spend(cached_snapshot),
Expand All @@ -618,11 +617,16 @@ fn build_usage_spend_summary(
.collect()
})
.unwrap_or_default();
let (seven_day_estimate, thirty_day_estimate) = local_cost_estimates
.map(|(seven, thirty)| (Some(seven), Some(thirty)))
.unwrap_or((None, None));
rows.push(UsageSpendRow {
provider_id: provider_id.clone(),
display_name,
seven_day: spend.seven_day,
thirty_day: spend.thirty_day,
seven_day_estimate,
thirty_day_estimate,
seven_day_tokens: spend.seven_day_tokens,
thirty_day_tokens: spend.thirty_day_tokens,
currency,
Expand Down Expand Up @@ -701,6 +705,29 @@ fn total_token_mix(mix: &codexbar::spend_contract::SpendTokenMix) -> Option<u64>
saw.then_some(total)
}

fn antigravity_spend_values(
mut spend: SpendValues,
seven: &codexbar::spend_contract::LocalTokenHistorySummary,
thirty: &codexbar::spend_contract::LocalTokenHistorySummary,
) -> SpendValues {
use codexbar::spend_contract::LocalHistoryCoverage;

spend.seven_day = seven.total_usd();
spend.thirty_day = thirty.total_usd();
spend.seven_day_tokens =
(seven.coverage == LocalHistoryCoverage::Complete).then_some(seven.total_tokens);
spend.thirty_day_tokens =
(thirty.coverage == LocalHistoryCoverage::Complete).then_some(thirty.total_tokens);
if spend.thirty_day.is_some() {
spend.source = "local Antigravity history · API list-price estimate".to_string();
} else if thirty.cost_estimate.known_subtotal_usd.is_some() {
spend.source = "local Antigravity history · known API list-price subtotal".to_string();
} else if thirty.coverage == LocalHistoryCoverage::Complete {
spend.source = "local Antigravity history · unpriced".to_string();
}
spend
}

fn cached_spend(snapshot: Option<&ProviderUsageSnapshot>) -> SpendValues {
let Some(snapshot) = snapshot else {
return SpendValues {
Expand Down Expand Up @@ -778,6 +805,27 @@ fn cached_spend(snapshot: Option<&ProviderUsageSnapshot>) -> SpendValues {
mod cache_key_tests {
use super::*;

fn local_history(
total_tokens: u64,
coverage: codexbar::spend_contract::LocalHistoryCoverage,
known_subtotal_usd: Option<f64>,
unpriced: u32,
) -> codexbar::spend_contract::LocalTokenHistorySummary {
codexbar::spend_contract::LocalTokenHistorySummary {
total_tokens,
session_count: if total_tokens > 0 { 1 } else { 0 },
coverage,
cost_estimate: codexbar::spend_contract::LocalCostEstimate {
known_subtotal_usd,
coverage: codexbar::spend_contract::CostCoverageCounts {
estimated: if known_subtotal_usd.is_some() { 1 } else { 0 },
unpriced,
..Default::default()
},
},
}
}

#[test]
fn invalidated_owner_clears_orphaned_indexing_activity() {
let mut coordinator = UsageSpendCoordinator::default();
Expand Down Expand Up @@ -834,4 +882,34 @@ mod cache_key_tests {
assert!(include_in_shared_overview("claude", false, true));
assert!(!include_in_shared_overview("codex", false, false));
}

#[test]
fn antigravity_partial_history_exposes_only_the_known_subtotal() {
use codexbar::spend_contract::LocalHistoryCoverage;

let seven = local_history(100, LocalHistoryCoverage::Partial, Some(1.25), 0);
let thirty = local_history(200, LocalHistoryCoverage::Partial, Some(2.50), 0);
let spend = antigravity_spend_values(cached_spend(None), &seven, &thirty);

assert_eq!(spend.seven_day, None);
assert_eq!(spend.thirty_day, None);
assert_eq!(spend.seven_day_tokens, None);
assert_eq!(spend.thirty_day_tokens, None);
assert!(spend.source.contains("known API list-price subtotal"));
}

#[test]
fn antigravity_complete_empty_history_is_a_known_zero() {
use codexbar::spend_contract::LocalHistoryCoverage;

let seven = local_history(0, LocalHistoryCoverage::Complete, None, 0);
let thirty = local_history(0, LocalHistoryCoverage::Complete, None, 0);
let spend = antigravity_spend_values(cached_spend(None), &seven, &thirty);

assert_eq!(spend.seven_day, Some(0.0));
assert_eq!(spend.thirty_day, Some(0.0));
assert_eq!(spend.seven_day_tokens, Some(0));
assert_eq!(spend.thirty_day_tokens, Some(0));
assert!(spend.source.contains("API list-price estimate"));
}
}
13 changes: 13 additions & 0 deletions apps/desktop-tauri/src/lib/usageSpendSharing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";

import {
formatUsageSpendReportingDay,
formatSpendMetric,
filterUsageSpendSummaryForOverview,
renderUsageSpendSharePng,
usageSpendShareFooter,
Expand All @@ -10,6 +11,18 @@ import {
import type { SpendContract, UsageSpendRow, UsageSpendSummary } from "../types/bridge";

describe("usage spend sharing", () => {
it("labels a mixed-pricing subtotal without presenting it as a total", () => {
expect(formatSpendMetric(null, 1_500, "USD", "tokens", 0.0125)).toMatch(/^≥.* known/);
});

it("renders a complete known-zero total instead of a subtotal", () => {
const metric = formatSpendMetric(0, 0, "USD", "tokens", 9);
expect(metric).not.toBe("—");
expect(metric).not.toContain("≥");
expect(metric).not.toContain("9.00");
expect(metric).toContain("0 tokens");
});

it.each([
[0, "0 subscriptions"],
[1, "1 subscription"],
Expand Down
23 changes: 20 additions & 3 deletions apps/desktop-tauri/src/lib/usageSpendSharing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,14 @@ export function formatSpendMetric(
tokens: number | null | undefined,
currency: string,
tokenLabel: string,
knownSubtotal?: number | null,
): string {
const parts: string[] = [];
if (cost != null && Number.isFinite(cost)) parts.push(formatUsd(cost, currency));
if (cost != null && Number.isFinite(cost)) {
parts.push(formatUsd(cost, currency));
} else if (knownSubtotal != null && Number.isFinite(knownSubtotal)) {
parts.push(`≥${formatUsd(knownSubtotal, currency)} known`);
}
if (tokens != null && Number.isFinite(tokens)) {
parts.push(`${Math.max(0, tokens).toLocaleString()} ${tokenLabel}`);
}
Expand Down Expand Up @@ -202,8 +207,20 @@ export function renderUsageSpendSharePng(summary: UsageSpendSummary, title: stri
const y = y0 + (index + 1) * rowH;
const cells = [
row.displayName,
formatSpendMetric(row.sevenDay, row.sevenDayTokens, row.currency, "tokens"),
formatSpendMetric(row.thirtyDay, row.thirtyDayTokens, row.currency, "tokens"),
formatSpendMetric(
row.sevenDay,
row.sevenDayTokens,
row.currency,
"tokens",
row.sevenDayEstimate?.knownSubtotalUsd,
),
formatSpendMetric(
row.thirtyDay,
row.thirtyDayTokens,
row.currency,
"tokens",
row.thirtyDayEstimate?.knownSubtotalUsd,
),
row.currency || "USD",
row.source,
];
Expand Down
16 changes: 15 additions & 1 deletion apps/desktop-tauri/src/surfaces/TrayPanel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,27 @@ describe("TrayPanel provider grid", () => {
source: "hidden",
includedInOverview: false,
},
{
providerId: "antigravity",
displayName: "Antigravity",
sevenDay: null,
thirtyDay: null,
thirtyDayEstimate: {
knownSubtotalUsd: 9,
coverage: { priced: 0, unpriced: 1, unmetered: 0, estimated: 1 },
},
currency: "USD",
source: "known subtotal",
includedInOverview: true,
},
],
});

renderTrayPanel([provider("codex", "Codex", 35)]);

expect(await screen.findByRole("button", { name: "UsageSpendShare" })).toBeInTheDocument();
expect(screen.getByText(/1 of 1 OverviewSpendProviderCoverage/)).toBeInTheDocument();
expect(screen.getByText("~$2.00")).toBeInTheDocument();
expect(screen.getByText(/1 of 2 OverviewSpendProviderCoverage/)).toBeInTheDocument();
});

it("dismisses the tray panel on unmodified Escape", async () => {
Expand Down
20 changes: 18 additions & 2 deletions apps/desktop-tauri/src/surfaces/settings/tabs/UsageSpendTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,24 @@ export default function UsageSpendTab(_props: TabProps) {
{(summary?.rows ?? []).map((row) => (
<tr key={row.providerId}>
<td>{row.displayName}</td>
<td>{formatSpendMetric(row.sevenDay, row.sevenDayTokens, row.currency, t("UsageSpendTokens"))}</td>
<td>{formatSpendMetric(row.thirtyDay, row.thirtyDayTokens, row.currency, t("UsageSpendTokens"))}</td>
<td>
{formatSpendMetric(
row.sevenDay,
row.sevenDayTokens,
row.currency,
t("UsageSpendTokens"),
row.sevenDayEstimate?.knownSubtotalUsd,
)}
</td>
<td>
{formatSpendMetric(
row.thirtyDay,
row.thirtyDayTokens,
row.currency,
t("UsageSpendTokens"),
row.thirtyDayEstimate?.knownSubtotalUsd,
)}
</td>
<td>{row.currency || "USD"}</td>
<td className="usage-spend-table__source">
{row.source}
Expand Down
7 changes: 7 additions & 0 deletions apps/desktop-tauri/src/types/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ export interface UsageSpendRow {
displayName: string;
sevenDay: number | null;
thirtyDay: number | null;
sevenDayEstimate?: LocalCostEstimate;
thirtyDayEstimate?: LocalCostEstimate;
sevenDayTokens?: number | null;
thirtyDayTokens?: number | null;
currency: string;
Expand All @@ -391,6 +393,11 @@ export interface UsageSpendRow {
staleUpdatedAt?: string;
}

export interface LocalCostEstimate {
knownSubtotalUsd: number | null;
coverage: CostCoverageCounts;
}

export interface UsageSpendSummary {
rows: UsageSpendRow[];
contract: SpendContract;
Expand Down
Loading