Add account-bound authentication for analytics requests (#45742)

## What changed

- Add `AnalyticsSession` to load local ChatGPT credentials, bind requests to the initial account and user, and reject identity changes before requests or when accepting results.
- Reload credentials and provide bounded recovery for unauthorized requests, using a backend client that disables redirects.
- Add lazy TUI analytics session initialization, account display metadata, and plan-specific credit groupings for future dashboard integration.

## Testing

Add regression tests for account and user changes, missing or API-key-only authentication, credential reloads, recovery after a `401` response, and session initialization after signing in.

GitOrigin-RevId: 2ba0d96fb44936d12d13aedffc3f2e61aedea560
This commit is contained in:
Felipe Coury authored and copyberry committed 2026-09-15 17:38:43 +00:00
1 parent 7224096b85
commit db078158c3
7 files changed
+452 -1

No files matched your search

+33
View File
@@ -1,5 +1,27 @@
//! Private analytics display types, independent of the app-server wire protocol.
use codex_protocol::account::PlanType;
/// Billing families match the App's consumer, business, and workspace scopes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum AccountKind {
Consumer,
Business,
Enterprise,
Unknown,
}
impl From<Option<PlanType>> for AccountKind {
fn from(plan: Option<PlanType>) -> Self {
match plan {
None | Some(PlanType::Unknown) => Self::Unknown,
Some(plan) if plan.is_team_like() => Self::Business,
Some(plan) if plan.is_workspace_account() => Self::Enterprise,
Some(_) => Self::Consumer,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum AccountAnalyticsReport {
Usage,
@@ -20,6 +42,17 @@ pub(crate) enum AccountAnalyticsGrouping {
TokenType,
}
impl AccountAnalyticsGrouping {
/// Credit breakdowns supported by the account's billing report.
pub(crate) fn credit_groupings(plan: Option<PlanType>) -> &'static [Self] {
match AccountKind::from(plan) {
AccountKind::Business => &[Self::Surface, Self::Model, Self::Speed],
AccountKind::Enterprise => &[Self::Surface, Self::Model, Self::Speed, Self::Reasoning],
AccountKind::Consumer | AccountKind::Unknown => &[Self::Surface],
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum AccountAnalyticsUnit {
RelativeUsage,