[codex] Thread backend-selected near-limit prompt state through client models

This commit is contained in:
James Chu
2026-05-03 23:33:48 -04:00
parent ff27d01676
commit 53dbdbfa9d
28 changed files with 558 additions and 3 deletions
@@ -2425,6 +2425,16 @@
}
]
},
"currentUsageLimitNudge": {
"anyOf": [
{
"$ref": "#/definitions/UsageLimitNudge"
},
{
"type": "null"
}
]
},
"limitId": {
"type": [
"string",
@@ -4373,6 +4383,30 @@
],
"type": "string"
},
"UsageLimitNudge": {
"properties": {
"action": {
"$ref": "#/definitions/UsageLimitNudgeAction"
},
"threshold": {
"format": "uint8",
"minimum": 0.0,
"type": "integer"
}
},
"required": [
"action",
"threshold"
],
"type": "object"
},
"UsageLimitNudgeAction": {
"enum": [
"add_credits",
"upgrade"
],
"type": "string"
},
"UserInput": {
"oneOf": [
{
@@ -12770,6 +12770,16 @@
}
]
},
"currentUsageLimitNudge": {
"anyOf": [
{
"$ref": "#/definitions/v2/UsageLimitNudge"
},
{
"type": "null"
}
]
},
"limitId": {
"type": [
"string",
@@ -17804,6 +17814,30 @@
"title": "TurnSteerResponse",
"type": "object"
},
"UsageLimitNudge": {
"properties": {
"action": {
"$ref": "#/definitions/v2/UsageLimitNudgeAction"
},
"threshold": {
"format": "uint8",
"minimum": 0.0,
"type": "integer"
}
},
"required": [
"action",
"threshold"
],
"type": "object"
},
"UsageLimitNudgeAction": {
"enum": [
"add_credits",
"upgrade"
],
"type": "string"
},
"UserInput": {
"oneOf": [
{
@@ -9423,6 +9423,16 @@
}
]
},
"currentUsageLimitNudge": {
"anyOf": [
{
"$ref": "#/definitions/UsageLimitNudge"
},
{
"type": "null"
}
]
},
"limitId": {
"type": [
"string",
@@ -15690,6 +15700,30 @@
"title": "TurnSteerResponse",
"type": "object"
},
"UsageLimitNudge": {
"properties": {
"action": {
"$ref": "#/definitions/UsageLimitNudgeAction"
},
"threshold": {
"format": "uint8",
"minimum": 0.0,
"type": "integer"
}
},
"required": [
"action",
"threshold"
],
"type": "object"
},
"UsageLimitNudgeAction": {
"enum": [
"add_credits",
"upgrade"
],
"type": "string"
},
"UserInput": {
"oneOf": [
{
@@ -61,6 +61,16 @@
}
]
},
"currentUsageLimitNudge": {
"anyOf": [
{
"$ref": "#/definitions/UsageLimitNudge"
},
{
"type": "null"
}
]
},
"limitId": {
"type": [
"string",
@@ -141,6 +151,30 @@
"usedPercent"
],
"type": "object"
},
"UsageLimitNudge": {
"properties": {
"action": {
"$ref": "#/definitions/UsageLimitNudgeAction"
},
"threshold": {
"format": "uint8",
"minimum": 0.0,
"type": "integer"
}
},
"required": [
"action",
"threshold"
],
"type": "object"
},
"UsageLimitNudgeAction": {
"enum": [
"add_credits",
"upgrade"
],
"type": "string"
}
},
"properties": {
@@ -61,6 +61,16 @@
}
]
},
"currentUsageLimitNudge": {
"anyOf": [
{
"$ref": "#/definitions/UsageLimitNudge"
},
{
"type": "null"
}
]
},
"limitId": {
"type": [
"string",
@@ -141,6 +151,30 @@
"usedPercent"
],
"type": "object"
},
"UsageLimitNudge": {
"properties": {
"action": {
"$ref": "#/definitions/UsageLimitNudgeAction"
},
"threshold": {
"format": "uint8",
"minimum": 0.0,
"type": "integer"
}
},
"required": [
"action",
"threshold"
],
"type": "object"
},
"UsageLimitNudgeAction": {
"enum": [
"add_credits",
"upgrade"
],
"type": "string"
}
},
"properties": {
@@ -5,5 +5,6 @@ import type { PlanType } from "../PlanType";
import type { CreditsSnapshot } from "./CreditsSnapshot";
import type { RateLimitReachedType } from "./RateLimitReachedType";
import type { RateLimitWindow } from "./RateLimitWindow";
import type { UsageLimitNudge } from "./UsageLimitNudge";
export type RateLimitSnapshot = { limitId: string | null, limitName: string | null, primary: RateLimitWindow | null, secondary: RateLimitWindow | null, credits: CreditsSnapshot | null, planType: PlanType | null, rateLimitReachedType: RateLimitReachedType | null, };
export type RateLimitSnapshot = { limitId: string | null, limitName: string | null, primary: RateLimitWindow | null, secondary: RateLimitWindow | null, credits: CreditsSnapshot | null, planType: PlanType | null, rateLimitReachedType: RateLimitReachedType | null, currentUsageLimitNudge: UsageLimitNudge | null, };
@@ -0,0 +1,6 @@
// GENERATED CODE! DO NOT MODIFY BY HAND!
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { UsageLimitNudgeAction } from "./UsageLimitNudgeAction";
export type UsageLimitNudge = { threshold: number, action: UsageLimitNudgeAction, };
@@ -0,0 +1,5 @@
// GENERATED CODE! DO NOT MODIFY BY HAND!
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
export type UsageLimitNudgeAction = "add_credits" | "upgrade";
@@ -428,6 +428,8 @@ export type { TurnStartedNotification } from "./TurnStartedNotification";
export type { TurnStatus } from "./TurnStatus";
export type { TurnSteerParams } from "./TurnSteerParams";
export type { TurnSteerResponse } from "./TurnSteerResponse";
export type { UsageLimitNudge } from "./UsageLimitNudge";
export type { UsageLimitNudgeAction } from "./UsageLimitNudgeAction";
export type { UserInput } from "./UserInput";
export type { WarningNotification } from "./WarningNotification";
export type { WebSearchAction } from "./WebSearchAction";
@@ -101,6 +101,8 @@ use codex_protocol::protocol::SubAgentSource as CoreSubAgentSource;
use codex_protocol::protocol::ThreadGoalStatus as CoreThreadGoalStatus;
use codex_protocol::protocol::TokenUsage as CoreTokenUsage;
use codex_protocol::protocol::TokenUsageInfo as CoreTokenUsageInfo;
use codex_protocol::protocol::UsageLimitNudge as CoreUsageLimitNudge;
use codex_protocol::protocol::UsageLimitNudgeAction as CoreUsageLimitNudgeAction;
use codex_protocol::request_permissions::PermissionGrantScope as CorePermissionGrantScope;
use codex_protocol::request_permissions::RequestPermissionProfile as CoreRequestPermissionProfile;
use codex_protocol::user_input::ByteRange as CoreByteRange;
@@ -7867,6 +7869,7 @@ pub struct RateLimitSnapshot {
pub credits: Option<CreditsSnapshot>,
pub plan_type: Option<PlanType>,
pub rate_limit_reached_type: Option<RateLimitReachedType>,
pub current_usage_limit_nudge: Option<UsageLimitNudge>,
}
impl From<CoreRateLimitSnapshot> for RateLimitSnapshot {
@@ -7881,6 +7884,54 @@ impl From<CoreRateLimitSnapshot> for RateLimitSnapshot {
rate_limit_reached_type: value
.rate_limit_reached_type
.map(RateLimitReachedType::from),
current_usage_limit_nudge: value
.current_usage_limit_nudge
.as_ref()
.and_then(CoreUsageLimitNudge::from_payload)
.map(UsageLimitNudge::from),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct UsageLimitNudge {
pub threshold: u8,
pub action: UsageLimitNudgeAction,
}
impl From<CoreUsageLimitNudge> for UsageLimitNudge {
fn from(value: CoreUsageLimitNudge) -> Self {
Self {
threshold: value.threshold.as_percent(),
action: value.action.into(),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, JsonSchema, TS)]
#[serde(rename_all = "snake_case")]
#[ts(export_to = "v2/", rename_all = "snake_case")]
pub enum UsageLimitNudgeAction {
AddCredits,
Upgrade,
}
impl From<CoreUsageLimitNudgeAction> for UsageLimitNudgeAction {
fn from(value: CoreUsageLimitNudgeAction) -> Self {
match value {
CoreUsageLimitNudgeAction::AddCredits => Self::AddCredits,
CoreUsageLimitNudgeAction::Upgrade => Self::Upgrade,
}
}
}
impl From<UsageLimitNudgeAction> for CoreUsageLimitNudgeAction {
fn from(value: UsageLimitNudgeAction) -> Self {
match value {
UsageLimitNudgeAction::AddCredits => Self::AddCredits,
UsageLimitNudgeAction::Upgrade => Self::Upgrade,
}
}
}
@@ -11309,4 +11360,75 @@ mod tests {
"unexpected error: {err}"
);
}
#[test]
fn rate_limit_snapshot_preserves_current_usage_limit_nudge() {
let active: RateLimitSnapshot = serde_json::from_value(json!({
"limitId": "codex",
"limitName": null,
"primary": null,
"secondary": null,
"credits": null,
"planType": null,
"rateLimitReachedType": null,
"currentUsageLimitNudge": {
"threshold": 75,
"action": "upgrade"
}
}))
.expect("active snapshot should deserialize");
assert_eq!(
active.current_usage_limit_nudge,
Some(UsageLimitNudge {
threshold: 75,
action: UsageLimitNudgeAction::Upgrade,
})
);
assert_eq!(
serde_json::to_value(&active)
.expect("active snapshot should serialize")
.get("currentUsageLimitNudge"),
Some(&json!({
"threshold": 75,
"action": "upgrade"
}))
);
let inactive: RateLimitSnapshot = serde_json::from_value(json!({
"limitId": "codex",
"limitName": null,
"primary": null,
"secondary": null,
"credits": null,
"planType": null,
"rateLimitReachedType": null,
"currentUsageLimitNudge": null
}))
.expect("inactive snapshot should deserialize");
assert_eq!(inactive.current_usage_limit_nudge, None);
assert_eq!(
serde_json::to_value(&inactive)
.expect("inactive snapshot should serialize")
.get("currentUsageLimitNudge"),
Some(&json!(null))
);
let missing: RateLimitSnapshot = serde_json::from_value(json!({
"limitId": "codex",
"limitName": null,
"primary": null,
"secondary": null,
"credits": null,
"planType": null,
"rateLimitReachedType": null
}))
.expect("missing snapshot should deserialize");
assert_eq!(missing.current_usage_limit_nudge, None);
assert_eq!(
serde_json::to_value(&missing)
.expect("missing snapshot should serialize")
.get("currentUsageLimitNudge"),
Some(&json!(null))
);
}
}
+2 -1
View File
@@ -1720,7 +1720,7 @@ Field notes:
```json
{ "method": "account/rateLimits/read", "id": 7 }
{ "id": 7, "result": { "rateLimits": { "primary": { "usedPercent": 25, "windowDurationMins": 15, "resetsAt": 1730947200 }, "secondary": null, "rateLimitReachedType": null } } }
{ "id": 7, "result": { "rateLimits": { "primary": { "usedPercent": 75, "windowDurationMins": 15, "resetsAt": 1730947200 }, "secondary": null, "rateLimitReachedType": null, "currentUsageLimitNudge": { "threshold": 75, "action": "add_credits" } } } }
{ "method": "account/rateLimits/updated", "params": { "rateLimits": { … } } }
```
@@ -1730,6 +1730,7 @@ Field notes:
- `windowDurationMins` is the quota window length.
- `resetsAt` is a Unix timestamp (seconds) for the next reset.
- `rateLimitReachedType` identifies the backend-classified limit state when one has been reached.
- `currentUsageLimitNudge` is the current backend-selected near-limit prompt, or `null` when no prompt should be shown.
### 8) Notify a workspace owner about a limit
@@ -3521,6 +3521,7 @@ mod tests {
}),
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
handle_token_count_event(
+3 -1
View File
@@ -787,6 +787,7 @@ mod tests {
credits: None,
plan_type: Some(PlanType::Plus),
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
},
});
@@ -806,7 +807,8 @@ mod tests {
"secondary": null,
"credits": null,
"planType": "plus",
"rateLimitReachedType": null
"rateLimitReachedType": null,
"currentUsageLimitNudge": null
}
},
}),
@@ -183,6 +183,7 @@ async fn get_account_rate_limits_returns_snapshot() -> Result<()> {
credits: None,
plan_type: Some(AccountPlanType::Pro),
rate_limit_reached_type: Some(RateLimitReachedType::WorkspaceMemberUsageLimitReached),
current_usage_limit_nudge: None,
},
rate_limits_by_limit_id: Some(
[
@@ -206,6 +207,7 @@ async fn get_account_rate_limits_returns_snapshot() -> Result<()> {
rate_limit_reached_type: Some(
RateLimitReachedType::WorkspaceMemberUsageLimitReached,
),
current_usage_limit_nudge: None,
},
),
(
@@ -222,6 +224,7 @@ async fn get_account_rate_limits_returns_snapshot() -> Result<()> {
credits: None,
plan_type: Some(AccountPlanType::Pro),
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
},
),
]
+66
View File
@@ -15,6 +15,8 @@ use codex_protocol::protocol::CreditsSnapshot;
use codex_protocol::protocol::RateLimitReachedType;
use codex_protocol::protocol::RateLimitSnapshot;
use codex_protocol::protocol::RateLimitWindow;
use codex_protocol::protocol::UsageLimitNudgeAction;
use codex_protocol::protocol::UsageLimitNudgePayload;
use reqwest::StatusCode;
use reqwest::header::CONTENT_TYPE;
use reqwest::header::HeaderMap;
@@ -452,6 +454,9 @@ impl Client {
.rate_limit_reached_type
.flatten()
.and_then(|details| Self::map_rate_limit_reached_type(details.kind));
let current_usage_limit_nudge = payload
.current_usage_limit_nudge
.map(|nudge| Self::map_usage_limit_nudge(*nudge));
let mut snapshots = vec![Self::make_rate_limit_snapshot(
Some("codex".to_string()),
/*limit_name*/ None,
@@ -459,6 +464,7 @@ impl Client {
payload.credits.flatten().map(|details| *details),
plan_type,
rate_limit_reached_type,
current_usage_limit_nudge,
)];
if let Some(additional) = payload.additional_rate_limits.flatten() {
snapshots.extend(additional.into_iter().map(|details| {
@@ -469,6 +475,7 @@ impl Client {
/*credits*/ None,
plan_type,
/*rate_limit_reached_type*/ None,
/*current_usage_limit_nudge*/ None,
)
}));
}
@@ -482,6 +489,7 @@ impl Client {
credits: Option<crate::types::CreditStatusDetails>,
plan_type: Option<AccountPlanType>,
rate_limit_reached_type: Option<RateLimitReachedType>,
current_usage_limit_nudge: Option<UsageLimitNudgePayload>,
) -> RateLimitSnapshot {
let (primary, secondary) = match rate_limit {
Some(details) => (
@@ -498,6 +506,19 @@ impl Client {
credits: Self::map_credits(credits),
plan_type,
rate_limit_reached_type,
current_usage_limit_nudge,
}
}
fn map_usage_limit_nudge(nudge: crate::types::UsageLimitNudge) -> UsageLimitNudgePayload {
UsageLimitNudgePayload {
threshold: nudge.threshold,
action: match nudge.action {
crate::types::UsageLimitNudgeAction::AddCredits => {
UsageLimitNudgeAction::AddCredits
}
crate::types::UsageLimitNudgeAction::Upgrade => UsageLimitNudgeAction::Upgrade,
},
}
}
@@ -661,6 +682,7 @@ mod tests {
rate_limit_reached_type: Some(Some(BackendRateLimitReachedType {
kind: RateLimitReachedKind::WorkspaceMemberCreditsDepleted,
})),
current_usage_limit_nudge: None,
};
let snapshots = Client::rate_limit_snapshots_from_payload(payload);
@@ -713,6 +735,7 @@ mod tests {
}])),
credits: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
let snapshots = Client::rate_limit_snapshots_from_payload(payload);
@@ -739,6 +762,7 @@ mod tests {
credits: None,
plan_type: Some(AccountPlanType::Pro),
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
},
RateLimitSnapshot {
limit_id: Some("codex".to_string()),
@@ -752,6 +776,7 @@ mod tests {
credits: None,
plan_type: Some(AccountPlanType::Pro),
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
},
];
@@ -796,6 +821,7 @@ mod tests {
credits: None,
additional_rate_limits: None,
rate_limit_reached_type: Some(Some(BackendRateLimitReachedType { kind })),
current_usage_limit_nudge: None,
};
let snapshots = Client::rate_limit_snapshots_from_payload(payload);
@@ -811,12 +837,52 @@ mod tests {
credits: None,
additional_rate_limits: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
let snapshots = Client::rate_limit_snapshots_from_payload(payload);
assert_eq!(snapshots[0].rate_limit_reached_type, None);
}
#[test]
fn usage_payload_preserves_current_usage_limit_nudge() {
let payload = RateLimitStatusPayload {
plan_type: crate::types::PlanType::Plus,
rate_limit: None,
credits: None,
additional_rate_limits: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: Some(Box::new(crate::types::UsageLimitNudge {
threshold: 75,
action: crate::types::UsageLimitNudgeAction::AddCredits,
})),
};
let snapshots = Client::rate_limit_snapshots_from_payload(payload);
assert_eq!(
snapshots[0].current_usage_limit_nudge,
Some(UsageLimitNudgePayload {
threshold: 75,
action: UsageLimitNudgeAction::AddCredits,
})
);
}
#[test]
fn usage_payload_preserves_missing_current_usage_limit_nudge() {
let payload = RateLimitStatusPayload {
plan_type: crate::types::PlanType::Plus,
rate_limit: None,
credits: None,
additional_rate_limits: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
let snapshots = Client::rate_limit_snapshots_from_payload(payload);
assert_eq!(snapshots[0].current_usage_limit_nudge, None);
}
#[test]
fn add_credits_nudge_email_uses_expected_paths_and_bodies() {
let codex_client = Client {
+2
View File
@@ -7,6 +7,8 @@ pub use codex_backend_openapi_models::models::RateLimitStatusDetails;
pub use codex_backend_openapi_models::models::RateLimitStatusPayload;
pub use codex_backend_openapi_models::models::RateLimitWindowSnapshot;
pub use codex_backend_openapi_models::models::TaskListItem;
pub use codex_backend_openapi_models::models::UsageLimitNudge;
pub use codex_backend_openapi_models::models::UsageLimitNudgeAction;
use serde::Deserialize;
use serde::de::Deserializer;
+2
View File
@@ -94,6 +94,7 @@ pub fn parse_rate_limit_for_limit(
credits,
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
})
}
@@ -158,6 +159,7 @@ pub fn parse_rate_limit_event(payload: &str) -> Option<RateLimitSnapshot> {
credits,
plan_type: event.plan_type,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
})
}
@@ -44,3 +44,7 @@ pub use self::rate_limit_window_snapshot::RateLimitWindowSnapshot;
pub(crate) mod credit_status_details;
pub use self::credit_status_details::CreditStatusDetails;
pub(crate) mod usage_limit_nudge;
pub use self::usage_limit_nudge::UsageLimitNudge;
pub use self::usage_limit_nudge::UsageLimitNudgeAction;
@@ -44,6 +44,12 @@ pub struct RateLimitStatusPayload {
skip_serializing_if = "Option::is_none"
)]
pub rate_limit_reached_type: Option<Option<RateLimitReachedType>>,
#[serde(
rename = "current_usage_limit_nudge",
default,
skip_serializing_if = "Option::is_none"
)]
pub current_usage_limit_nudge: Option<Box<models::UsageLimitNudge>>,
}
impl RateLimitStatusPayload {
@@ -54,6 +60,7 @@ impl RateLimitStatusPayload {
credits: None,
additional_rate_limits: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
}
}
}
@@ -0,0 +1,28 @@
/*
* codex-backend
*
* codex-backend
*
* The version of the OpenAPI document: 0.0.1
*
* Generated by: https://openapi-generator.tech
*/
use serde::Deserialize;
use serde::Serialize;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct UsageLimitNudge {
#[serde(rename = "threshold")]
pub threshold: u8,
#[serde(rename = "action")]
pub action: UsageLimitNudgeAction,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum UsageLimitNudgeAction {
#[serde(rename = "add_credits")]
AddCredits,
#[serde(rename = "upgrade")]
Upgrade,
}
+6
View File
@@ -2392,6 +2392,7 @@ async fn set_rate_limits_retains_previous_credits() {
}),
plan_type: Some(codex_protocol::account::PlanType::Plus),
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
state.set_rate_limits(initial.clone());
@@ -2411,6 +2412,7 @@ async fn set_rate_limits_retains_previous_credits() {
credits: None,
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
state.set_rate_limits(update.clone());
@@ -2424,6 +2426,7 @@ async fn set_rate_limits_retains_previous_credits() {
credits: initial.credits,
plan_type: initial.plan_type,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
})
);
}
@@ -2499,6 +2502,7 @@ async fn set_rate_limits_updates_plan_type_when_present() {
}),
plan_type: Some(codex_protocol::account::PlanType::Plus),
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
state.set_rate_limits(initial.clone());
@@ -2514,6 +2518,7 @@ async fn set_rate_limits_updates_plan_type_when_present() {
credits: None,
plan_type: Some(codex_protocol::account::PlanType::Pro),
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
state.set_rate_limits(update.clone());
@@ -2527,6 +2532,7 @@ async fn set_rate_limits_updates_plan_type_when_present() {
credits: initial.credits,
plan_type: update.plan_type,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
})
);
}
+6
View File
@@ -50,6 +50,7 @@ async fn set_rate_limits_defaults_limit_id_to_codex_when_missing() {
credits: None,
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
});
assert_eq!(
@@ -78,6 +79,7 @@ async fn set_rate_limits_defaults_to_codex_when_limit_id_missing_after_other_buc
credits: None,
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
});
state.set_rate_limits(RateLimitSnapshot {
limit_id: None,
@@ -91,6 +93,7 @@ async fn set_rate_limits_defaults_to_codex_when_limit_id_missing_after_other_buc
credits: None,
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
});
assert_eq!(
@@ -123,6 +126,7 @@ async fn set_rate_limits_carries_credits_and_plan_type_from_codex_to_codex_other
}),
plan_type: Some(codex_protocol::account::PlanType::Plus),
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
});
state.set_rate_limits(RateLimitSnapshot {
@@ -137,6 +141,7 @@ async fn set_rate_limits_carries_credits_and_plan_type_from_codex_to_codex_other
credits: None,
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
});
assert_eq!(
@@ -157,6 +162,7 @@ async fn set_rate_limits_carries_credits_and_plan_type_from_codex_to_codex_other
}),
plan_type: Some(codex_protocol::account::PlanType::Plus),
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
})
);
}
@@ -13,6 +13,7 @@ fn snapshot(
credits: None,
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
}
}
+1
View File
@@ -37,6 +37,7 @@ fn rate_limit_snapshot() -> RateLimitSnapshot {
credits: None,
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
}
}
+97
View File
@@ -2122,6 +2122,9 @@ pub struct RateLimitSnapshot {
pub credits: Option<CreditsSnapshot>,
pub plan_type: Option<crate::account::PlanType>,
pub rate_limit_reached_type: Option<RateLimitReachedType>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[ts(optional)]
pub current_usage_limit_nudge: Option<UsageLimitNudgePayload>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, JsonSchema, TS)]
@@ -2135,6 +2138,58 @@ pub enum RateLimitReachedType {
WorkspaceMemberUsageLimitReached,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, JsonSchema, TS)]
pub struct UsageLimitNudgePayload {
pub threshold: u8,
pub action: UsageLimitNudgeAction,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UsageLimitNudge {
pub threshold: UsageLimitNudgeThreshold,
pub action: UsageLimitNudgeAction,
}
impl UsageLimitNudge {
pub fn from_payload(payload: &UsageLimitNudgePayload) -> Option<Self> {
Some(Self {
threshold: UsageLimitNudgeThreshold::from_percent(payload.threshold)?,
action: payload.action,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UsageLimitNudgeThreshold {
Percent75,
Percent90,
}
impl UsageLimitNudgeThreshold {
fn from_percent(percent: u8) -> Option<Self> {
match percent {
75 => Some(Self::Percent75),
90 => Some(Self::Percent90),
_ => None,
}
}
pub fn as_percent(self) -> u8 {
match self {
Self::Percent75 => 75,
Self::Percent90 => 90,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, JsonSchema, TS)]
#[serde(rename_all = "snake_case")]
#[ts(rename_all = "snake_case")]
pub enum UsageLimitNudgeAction {
AddCredits,
Upgrade,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema, TS)]
pub struct RateLimitWindow {
/// Percentage (0-100) of the window that has been consumed.
@@ -3961,6 +4016,48 @@ mod tests {
assert_eq!(HookSource::Unknown.is_managed(), false);
}
fn rate_limit_snapshot_with_nudge(
current_usage_limit_nudge: Option<UsageLimitNudgePayload>,
) -> RateLimitSnapshot {
RateLimitSnapshot {
limit_id: Some("codex".to_string()),
limit_name: None,
primary: None,
secondary: None,
credits: None,
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge,
}
}
#[test]
fn current_usage_limit_nudge_payload_validates_thresholds() {
assert_eq!(
rate_limit_snapshot_with_nudge(Some(UsageLimitNudgePayload {
threshold: 75,
action: UsageLimitNudgeAction::AddCredits,
}))
.current_usage_limit_nudge
.as_ref()
.and_then(UsageLimitNudge::from_payload),
Some(UsageLimitNudge {
threshold: UsageLimitNudgeThreshold::Percent75,
action: UsageLimitNudgeAction::AddCredits,
})
);
assert_eq!(
rate_limit_snapshot_with_nudge(Some(UsageLimitNudgePayload {
threshold: 80,
action: UsageLimitNudgeAction::AddCredits,
}))
.current_usage_limit_nudge
.as_ref()
.and_then(UsageLimitNudge::from_payload),
None
);
}
fn sorted_writable_roots(roots: Vec<WritableRoot>) -> Vec<(PathBuf, Vec<PathBuf>)> {
let mut sorted_roots: Vec<(PathBuf, Vec<PathBuf>)> = roots
.into_iter()
@@ -114,6 +114,7 @@ pub(super) fn snapshot(percent: f64) -> RateLimitSnapshot {
credits: None,
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
}
}
@@ -246,6 +246,7 @@ async fn rate_limit_snapshot_keeps_prior_credits_when_missing_from_headers() {
}),
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
}));
let initial_balance = chat
.rate_limit_snapshots_by_limit_id
@@ -266,6 +267,7 @@ async fn rate_limit_snapshot_keeps_prior_credits_when_missing_from_headers() {
credits: None,
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
}));
let display = chat
@@ -305,6 +307,7 @@ async fn rate_limit_snapshot_updates_and_retains_plan_type() {
credits: None,
plan_type: Some(PlanType::Plus),
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
}));
assert_eq!(chat.plan_type, Some(PlanType::Plus));
@@ -324,6 +327,7 @@ async fn rate_limit_snapshot_updates_and_retains_plan_type() {
credits: None,
plan_type: Some(PlanType::Pro),
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
}));
assert_eq!(chat.plan_type, Some(PlanType::Pro));
@@ -343,6 +347,7 @@ async fn rate_limit_snapshot_updates_and_retains_plan_type() {
credits: None,
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
}));
assert_eq!(chat.plan_type, Some(PlanType::Pro));
}
@@ -367,6 +372,7 @@ async fn rate_limit_snapshots_keep_separate_entries_per_limit_id() {
}),
plan_type: Some(PlanType::Pro),
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
}));
chat.on_rate_limit_snapshot(Some(RateLimitSnapshot {
@@ -381,6 +387,7 @@ async fn rate_limit_snapshots_keep_separate_entries_per_limit_id() {
credits: None,
plan_type: Some(PlanType::Pro),
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
}));
let codex = chat
@@ -434,6 +441,7 @@ async fn rate_limit_switch_prompt_skips_non_codex_limit() {
credits: None,
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
}));
assert!(matches!(
+13
View File
@@ -226,6 +226,7 @@ async fn status_snapshot_includes_reasoning_details() {
credits: None,
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
let rate_display = rate_limit_snapshot_display(&snapshot, captured_at);
@@ -754,6 +755,7 @@ async fn status_snapshot_includes_monthly_limit() {
credits: None,
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
let rate_display = rate_limit_snapshot_display(&snapshot, captured_at);
@@ -806,6 +808,7 @@ async fn status_snapshot_shows_unlimited_credits() {
}),
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
let rate_display = rate_limit_snapshot_display(&snapshot, captured_at);
let model_slug = crate::legacy_core::test_support::get_model_offline(config.model.as_deref());
@@ -856,6 +859,7 @@ async fn status_snapshot_shows_positive_credits() {
}),
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
let rate_display = rate_limit_snapshot_display(&snapshot, captured_at);
let model_slug = crate::legacy_core::test_support::get_model_offline(config.model.as_deref());
@@ -906,6 +910,7 @@ async fn status_snapshot_hides_zero_credits() {
}),
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
let rate_display = rate_limit_snapshot_display(&snapshot, captured_at);
let model_slug = crate::legacy_core::test_support::get_model_offline(config.model.as_deref());
@@ -954,6 +959,7 @@ async fn status_snapshot_hides_when_has_no_credits_flag() {
}),
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
let rate_display = rate_limit_snapshot_display(&snapshot, captured_at);
let model_slug = crate::legacy_core::test_support::get_model_offline(config.model.as_deref());
@@ -1060,6 +1066,7 @@ async fn status_snapshot_truncates_in_narrow_terminal() {
credits: None,
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
let rate_display = rate_limit_snapshot_display(&snapshot, captured_at);
@@ -1225,6 +1232,7 @@ async fn status_snapshot_shows_refreshing_limits_notice() {
credits: None,
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
let rate_display = rate_limit_snapshot_display(&snapshot, captured_at);
@@ -1296,6 +1304,7 @@ async fn status_snapshot_includes_credits_and_limits() {
}),
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
let rate_display = rate_limit_snapshot_display(&snapshot, captured_at);
@@ -1350,6 +1359,7 @@ async fn status_snapshot_shows_unavailable_limits_message() {
credits: None,
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
let captured_at = chrono::Local
.with_ymd_and_hms(2024, 6, 7, 8, 9, 10)
@@ -1407,6 +1417,7 @@ async fn status_snapshot_treats_refreshing_empty_limits_as_unavailable() {
credits: None,
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
let captured_at = chrono::Local
.with_ymd_and_hms(2024, 6, 7, 8, 9, 10)
@@ -1478,6 +1489,7 @@ async fn status_snapshot_shows_stale_limits_message() {
credits: None,
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
let rate_display = rate_limit_snapshot_display(&snapshot, captured_at);
let now = captured_at + ChronoDuration::minutes(20);
@@ -1549,6 +1561,7 @@ async fn status_snapshot_cached_limits_hide_credits_without_flag() {
}),
plan_type: None,
rate_limit_reached_type: None,
current_usage_limit_nudge: None,
};
let rate_display = rate_limit_snapshot_display(&snapshot, captured_at);
let now = captured_at + ChronoDuration::minutes(20);