Files
codex/codex-rs/core/src/tasks/compact.rs
T
Alex Chang cc7a62e19c Report API spend limit errors by scope (#62329)
## Why

Generic quota errors hide whether the project or organization spend limit
blocked a request. Reporting the scope tells users which limit to check.

## What changed

Recognize API key, project, and organization spend limit errors across
HTTP, SSE, and WebSocket responses. Show a message that names the scope and
treat the error as non-retryable, even when the provider sends
`Retry-After`. Apply the existing usage-limit handling to goals and
compaction.

Expose distinct `CodexErrorInfo` values in the app-server protocol and
generated bindings so clients can identify the limit in live errors and
failed turn history.

## Testing

Extend the transport tests to cover all three scopes and verify that they
stop retries. Extend the app-server test to check that the error survives
`thread/read` and that a subsequent turn can succeed in the same thread.

GitOrigin-RevId: 03341f7a78daa7540e7baa519f1a29ec07e34509
2026-11-18 18:11:54 +00:00

110 lines
3.7 KiB
Rust

use std::sync::Arc;
use super::SessionTask;
use super::SessionTaskResult;
use super::emit_compact_metric;
use crate::session::TurnInput;
use crate::session::session::Session;
use crate::session::turn_context::TurnContext;
use crate::state::TaskKind;
use codex_features::Feature;
use codex_model_provider::RemoteCompactionSupport;
use codex_user_input::ResolvedUserInput;
use tokio_util::sync::CancellationToken;
#[derive(Clone, Copy, Default)]
pub(crate) struct CompactTask;
impl SessionTask for CompactTask {
fn kind(&self) -> TaskKind {
TaskKind::Compact
}
fn span_name(&self) -> &'static str {
"session_task.compact"
}
async fn run(
self: Arc<Self>,
session: Arc<Session>,
ctx: Arc<TurnContext>,
_input: Vec<TurnInput>,
cancellation_token: CancellationToken,
) -> SessionTaskResult {
let _profile_guard = ctx.turn_timing_state.begin_compaction();
let _compaction_span = tracing::trace_span!(
"codex.compaction",
codex.turn.phase = "compaction",
conversation.id = %session.thread_id,
turn.id = %ctx.sub_id,
);
// Preparation errors must reach the task runner, which reports them to the client.
session.emit_turn_started(&ctx, TaskKind::Compact).await;
session.check_rollout_budget()?;
let step_context = session
.capture_step_context(Arc::clone(&ctx), &cancellation_token)
.await?;
let world_state = Arc::new(
session
.build_world_state_for_step(&step_context, /*new_window*/ true)
.await?,
);
if ctx.config.features.enabled(Feature::TokenBudget) {
// Preparing the context may wait; this reset never sends a model request.
session.check_rollout_budget()?;
crate::compact_token_budget::run_manual_compact_task(
session,
step_context,
world_state,
)
.await?;
return Ok(None);
}
let result = match ctx.provider.capabilities().remote_compaction {
RemoteCompactionSupport::V2 => {
emit_compact_metric(
&session.services.session_telemetry,
"remote_v2",
/*manual*/ true,
);
crate::compact_remote_v2::run_remote_compact_task(
session.clone(),
step_context,
world_state,
)
.await
}
RemoteCompactionSupport::Unsupported => {
emit_compact_metric(
&session.services.session_telemetry,
"local",
/*manual*/ true,
);
let input = vec![ResolvedUserInput::text(
ctx.config
.compact_prompt
.as_deref()
.unwrap_or(crate::compact::SUMMARIZATION_PROMPT)
.to_string(),
)];
crate::compact::run_compact_task(session.clone(), step_context, world_state, input)
.await
}
};
if let Err(err) = result {
if super::ends_turn_as_interrupted(&err) {
return Err(err);
}
let error = err.to_codex_protocol_error();
if error.is_usage_limit() {
// Compaction already emitted the error; notify extensions without emitting it twice.
session
.emit_turn_error_lifecycle(ctx.as_ref(), error, err.details())
.await;
}
}
Ok(None)
}
}